Skip to content
Permalink
Browse files

[Rendering] PostFX: remove dependency to CameraComponent

  • Loading branch information
xen2 committed Feb 12, 2019
1 parent 25ec89f commit ff279b024d5db2a34754ea4d52c83e071d9fce81
@@ -159,7 +159,7 @@ protected override void DrawCore(RenderDrawContext context)

var outputTexture = GetSafeOutput(0);

var camera = context.RenderContext.GetCurrentCamera();
var renderView = context.RenderContext.RenderView;

//---------------------------------
// Ambient Occlusion
@@ -172,31 +172,28 @@ protected override void DrawCore(RenderDrawContext context)

aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOKeys.Count, NumberOfSamples > 0 ? NumberOfSamples : 9);

if (camera != null)
{
// Set Near/Far pre-calculated factors to speed up the linear depth reconstruction
aoRawImageEffect.Parameters.Set(CameraKeys.ZProjection, CameraKeys.ZProjectionACalculate(camera.NearClipPlane, camera.FarClipPlane));

Vector4 screenSize = new Vector4(originalColorBuffer.Width, originalColorBuffer.Height, 0, 0);
screenSize.Z = screenSize.X / screenSize.Y;
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ScreenInfo, screenSize);

// Projection infor used to reconstruct the View space position from linear depth
var p00 = camera.ProjectionMatrix.M11;
var p11 = camera.ProjectionMatrix.M22;
var p02 = camera.ProjectionMatrix.M13;
var p12 = camera.ProjectionMatrix.M23;
Vector4 projInfo = new Vector4(-2.0f / (screenSize.X * p00), -2.0f / (screenSize.Y * p11), (1.0f - p02) / p00, (1.0f + p12) / p11);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ProjInfo, projInfo);

//**********************************
// User parameters
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamProjScale, ParamProjScale);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamIntensity, ParamIntensity);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamBias, ParamBias);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamRadius, ParamRadius);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamRadiusSquared, ParamRadius * ParamRadius);
}
// Set Near/Far pre-calculated factors to speed up the linear depth reconstruction
aoRawImageEffect.Parameters.Set(CameraKeys.ZProjection, CameraKeys.ZProjectionACalculate(renderView.NearClipPlane, renderView.FarClipPlane));

Vector4 screenSize = new Vector4(originalColorBuffer.Width, originalColorBuffer.Height, 0, 0);
screenSize.Z = screenSize.X / screenSize.Y;
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ScreenInfo, screenSize);

// Projection infor used to reconstruct the View space position from linear depth
var p00 = renderView.Projection.M11;
var p11 = renderView.Projection.M22;
var p02 = renderView.Projection.M13;
var p12 = renderView.Projection.M23;
Vector4 projInfo = new Vector4(-2.0f / (screenSize.X * p00), -2.0f / (screenSize.Y * p11), (1.0f - p02) / p00, (1.0f + p12) / p11);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ProjInfo, projInfo);

//**********************************
// User parameters
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamProjScale, ParamProjScale);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamIntensity, ParamIntensity);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamBias, ParamBias);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamRadius, ParamRadius);
aoRawImageEffect.Parameters.Set(AmbientOcclusionRawAOShaderKeys.ParamRadiusSquared, ParamRadius * ParamRadius);

aoRawImageEffect.SetInput(0, originalDepthBuffer);
aoRawImageEffect.SetOutput(aoTexture1);
@@ -218,12 +215,9 @@ protected override void DrawCore(RenderDrawContext context)
nameGaussianBlurV = string.Format("AmbientOcclusionBlurV{0}x{0}", offsetsWeights.Length);
}

if (camera != null)
{
// Set Near/Far pre-calculated factors to speed up the linear depth reconstruction
blurH.Parameters.Set(CameraKeys.ZProjection, CameraKeys.ZProjectionACalculate(camera.NearClipPlane, camera.FarClipPlane));
blurV.Parameters.Set(CameraKeys.ZProjection, CameraKeys.ZProjectionACalculate(camera.NearClipPlane, camera.FarClipPlane));
}
// Set Near/Far pre-calculated factors to speed up the linear depth reconstruction
blurH.Parameters.Set(CameraKeys.ZProjection, CameraKeys.ZProjectionACalculate(renderView.NearClipPlane, renderView.FarClipPlane));
blurV.Parameters.Set(CameraKeys.ZProjection, CameraKeys.ZProjectionACalculate(renderView.NearClipPlane, renderView.FarClipPlane));

// Update permutation parameters
blurH.Parameters.Set(AmbientOcclusionBlurKeys.Count, offsetsWeights.Length);
@@ -359,10 +359,8 @@ protected override void DrawCore(RenderDrawContext context)
// Render target will contain "CoC"(16 bits) "Linear depth"(16bits).
var cocLinearDepthTexture = GetScopedRenderTarget(originalColorBuffer.Description, 1f, PixelFormat.R16G16_Float);

var cameraState = context.RenderContext.GetCurrentCamera();
if (cameraState == null) throw new InvalidOperationException("No valid camera");

var farPlane = cameraState.FarClipPlane;
var renderView = context.RenderContext.RenderView;
var farPlane = renderView.FarClipPlane;

var depthAreas = DOFAreas;
if (AutoFocus)
@@ -372,16 +370,14 @@ protected override void DrawCore(RenderDrawContext context)
var maxAmplitude = farPlane * 0.2f;
diffToTarget = MathUtil.Clamp(diffToTarget, -maxAmplitude, maxAmplitude);
autoFocusDistanceCurrent = autoFocusDistanceCurrent + 0.1f * diffToTarget;
if (autoFocusDistanceCurrent < cameraState.NearClipPlane * 2.0f) autoFocusDistanceCurrent = cameraState.NearClipPlane * 2.0f;
depthAreas = new Vector4(cameraState.NearClipPlane, autoFocusDistanceCurrent, autoFocusDistanceCurrent, autoFocusDistanceCurrent + farPlane * 0.5f);
if (autoFocusDistanceCurrent < renderView.NearClipPlane * 2.0f) autoFocusDistanceCurrent = renderView.NearClipPlane * 2.0f;
depthAreas = new Vector4(renderView.NearClipPlane, autoFocusDistanceCurrent, autoFocusDistanceCurrent, autoFocusDistanceCurrent + farPlane * 0.5f);
}

coclinearDepthMapEffect.SetInput(0, originalDepthBuffer);
coclinearDepthMapEffect.SetOutput(cocLinearDepthTexture);
coclinearDepthMapEffect.Parameters.Set(CircleOfConfusionKeys.depthAreas, depthAreas);
var camera = context.RenderContext.GetCurrentCamera();
if (camera != null)
coclinearDepthMapEffect.Parameters.Set(CameraKeys.ZProjection, CameraKeys.ZProjectionACalculate(camera.NearClipPlane, camera.FarClipPlane));
coclinearDepthMapEffect.Parameters.Set(CameraKeys.ZProjection, CameraKeys.ZProjectionACalculate(renderView.NearClipPlane, renderView.FarClipPlane));
coclinearDepthMapEffect.Draw(context, "CoC_LinearDepth");

if (AutoFocus)
@@ -258,7 +258,7 @@ public enum DebugModes
/// </summary>
private class TemporalFrameCache
{
public CameraComponent Camera;
public RenderView RenderView;
public int LastUsageFrame;
public Texture TemporalBuffer;
public Matrix PrevViewProjection;
@@ -285,15 +285,15 @@ public void Dispose()
private readonly List<TemporalFrameCache> frameCache = new List<TemporalFrameCache>(4);

[NotNull]
private TemporalFrameCache GetFrameCache(int frameIndex, CameraComponent camera)
private TemporalFrameCache GetFrameCache(int frameIndex, RenderView renderView)
{
TemporalFrameCache cache;

// Find free temporal cache
for (int i = 0; i < frameCache.Count; i++)
{
cache = frameCache[i];
if (cache.Camera == camera && cache.LastUsageFrame != frameIndex)
if (cache.RenderView == renderView && cache.LastUsageFrame != frameIndex)
{
cache.LastUsageFrame = frameIndex;
return cache;
@@ -302,9 +302,9 @@ private TemporalFrameCache GetFrameCache(int frameIndex, CameraComponent camera)

// Create new one
cache = new TemporalFrameCache();
cache.Camera = camera;
cache.RenderView = renderView;
cache.LastUsageFrame = frameIndex;
cache.PrevViewProjection = camera.ViewProjectionMatrix;
cache.PrevViewProjection = renderView.ViewProjection;
frameCache.Add(cache);

return cache;
@@ -370,17 +370,15 @@ private TemporalFrameCache Prepare(RenderDrawContext context, Texture outputBuff
{
TemporalFrameCache cache = null;

var currentCamera = context.RenderContext.GetCurrentCamera();
if (currentCamera == null)
throw new InvalidOperationException("No valid camera");
Matrix viewMatrix = currentCamera.ViewMatrix;
Matrix projectionMatrix = currentCamera.ProjectionMatrix;
Matrix viewProjectionMatrix = currentCamera.ViewProjectionMatrix;
var renderView = context.RenderContext.RenderView;
Matrix viewMatrix = renderView.View;
Matrix projectionMatrix = renderView.Projection;
Matrix viewProjectionMatrix = renderView.ViewProjection;
Matrix inverseViewMatrix = Matrix.Invert(viewMatrix);
Matrix inverseViewProjectionMatrix = Matrix.Invert(viewProjectionMatrix);
Vector4 eye = inverseViewMatrix.Row4;
float nearclip = currentCamera.NearClipPlane;
float farclip = currentCamera.FarClipPlane;
float nearclip = renderView.NearClipPlane;
float farclip = renderView.FarClipPlane;
Vector4 viewInfo = new Vector4(1.0f / projectionMatrix.M11, 1.0f / projectionMatrix.M22, farclip / (farclip - nearclip), (-farclip * nearclip) / (farclip - nearclip) / farclip);
Vector3 cameraPos = new Vector3(eye.X, eye.Y, eye.Z);

@@ -397,7 +395,7 @@ private TemporalFrameCache Prepare(RenderDrawContext context, Texture outputBuff

temporalTime = (float)time;

cache = GetFrameCache(gameTime.FrameCount, currentCamera);
cache = GetFrameCache(gameTime.FrameCount, renderView);
}

var traceBufferSize = GetBufferResolution(outputBuffer, RayTracePassResolution);

0 comments on commit ff279b0

Please sign in to comment.
You can’t perform that action at this time.