Skip to content

Commit 4ee0e78

Browse files
Avoid building the mip chain a second time for SSR for transparent objects. (#93)
* - Avoid building the mip chain a second time for SSR for transparent objects. * Update ScreenSpaceReflections.compute * fix merge issue Co-authored-by: sebastienlagarde <[email protected]>
1 parent 723d4a6 commit 4ee0e78

File tree

5 files changed

+45
-10
lines changed

5 files changed

+45
-10
lines changed

com.unity.render-pipelines.high-definition/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
661661
- Raytracing: Removed the dynamic lightmap multicompile.
662662
- Raytracing: Remove the LOD cross fade multi compile for ray tracing.
663663
- Cookie are now supported in lightmaper. All lights casting cookie and baked will now include cookie influence.
664+
- Avoid building the mip chain a second time for SSR for transparent objects.
664665

665666
## [7.1.1] - 2019-09-05
666667

com.unity.render-pipelines.high-definition/Runtime/Lighting/ScreenSpaceLighting/ScreenSpaceReflections.compute

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#pragma kernel ScreenSpaceReflectionsTracing SSR_TRACE
99
#pragma kernel ScreenSpaceReflectionsReprojection SSR_REPROJECT
1010

11+
#pragma multi_compile _ DEPTH_SOURCE_NOT_FROM_MIP_CHAIN
12+
1113
// Tweak parameters.
1214
// #define DEBUG
1315
#define SSR_TRACE_BEHIND_OBJECTS
@@ -41,6 +43,20 @@
4143
RW_TEXTURE2D(float4, _SsrDebugTexture);
4244
#endif
4345

46+
// For opaque we do the following operation:
47+
// - Render opaque object in depth buffer
48+
// - Generate depth pyramid from opaque depth buffer
49+
// - Trigger ray from position recover from depth pyramid and raymarch with depth pyramid
50+
// For transparent reflection we chose to not regenerate a depth pyramid to save performance. So we have
51+
// - Generate depth pyramid from opaque depth buffer
52+
// - Trigger ray from position recover from depth buffer (use depth pyramid) and raymarch with depth pyramid
53+
// - Render transparent object with reflection in depth buffer in transparent prepass
54+
// - Trigger ray from position recover from new depth buffer and raymarch with opaque depth pyramid
55+
// So we need a seperate texture for the mip chain and for the depth source when doing the transprent reflection
56+
#ifdef DEPTH_SOURCE_NOT_FROM_MIP_CHAIN
57+
TEXTURE2D_X(_DepthTexture);
58+
#endif
59+
4460
#ifdef SSR_TRACE
4561
TEXTURE2D_X_UINT2( _StencilTexture);
4662
RW_TEXTURE2D_X(float2, _SsrHitPointTexture);
@@ -104,7 +120,12 @@ void ScreenSpaceReflectionsTracing(uint3 groupId : SV_GroupID,
104120
}
105121

106122
float2 positionNDC = positionSS * _ScreenSize.zw + (0.5 * _ScreenSize.zw); // Should we precompute the half-texel bias? We seem to use it a lot.
123+
124+
#ifdef DEPTH_SOURCE_NOT_FROM_MIP_CHAIN
125+
float deviceDepth = LOAD_TEXTURE2D_X(_DepthTexture, positionSS).r;
126+
#else
107127
float deviceDepth = LOAD_TEXTURE2D_X(_CameraDepthTexture, positionSS).r;
128+
#endif
108129

109130
bool killRay = deviceDepth == UNITY_RAW_FAR_CLIP_VALUE;
110131

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.LightLoop.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ LightingOutput RenderDeferredLighting( RenderGraph renderGraph,
244244
class RenderSSRPassData
245245
{
246246
public RenderSSRParameters parameters;
247+
public TextureHandle depthBuffer;
247248
public TextureHandle depthPyramid;
248249
public TextureHandle colorPyramid;
249250
public TextureHandle stencilBuffer;
@@ -257,6 +258,7 @@ TextureHandle RenderSSR( RenderGraph renderGraph,
257258
HDCamera hdCamera,
258259
TextureHandle normalBuffer,
259260
TextureHandle motionVectorsBuffer,
261+
TextureHandle depthBuffer,
260262
TextureHandle depthPyramid,
261263
TextureHandle stencilBuffer,
262264
TextureHandle clearCoatMask)
@@ -285,7 +287,8 @@ TextureHandle RenderSSR( RenderGraph renderGraph,
285287

286288
var colorPyramid = renderGraph.ImportTexture(hdCamera.GetPreviousFrameRT((int)HDCameraFrameHistoryType.ColorBufferMipChain));
287289

288-
passData.parameters = PrepareSSRParameters(hdCamera, m_DepthBufferMipChainInfo);
290+
passData.parameters = PrepareSSRParameters(hdCamera, m_DepthBufferMipChainInfo, true);
291+
passData.depthBuffer = builder.ReadTexture(depthBuffer);
289292
passData.depthPyramid = builder.ReadTexture(depthPyramid);
290293
passData.colorPyramid = builder.ReadTexture(colorPyramid);
291294
passData.stencilBuffer = builder.ReadTexture(stencilBuffer);
@@ -308,6 +311,7 @@ TextureHandle RenderSSR( RenderGraph renderGraph,
308311
{
309312
var res = context.resources;
310313
RenderSSR(data.parameters,
314+
res.GetTexture(data.depthBuffer),
311315
res.GetTexture(data.depthPyramid),
312316
res.GetTexture(data.hitPointsTexture),
313317
res.GetTexture(data.stencilBuffer),

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraph.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ void ExecuteWithRenderGraph( RenderRequest renderRequest,
7575
hdCamera,
7676
prepassOutput.resolvedNormalBuffer,
7777
prepassOutput.resolvedMotionVectorsBuffer,
78+
prepassOutput.depthBuffer,
7879
prepassOutput.depthPyramidTexture,
7980
prepassOutput.stencilBuffer,
8081
clearCoatMask);

com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3878,6 +3878,7 @@ struct RenderSSRParameters
38783878
public ComputeShader ssrCS;
38793879
public int tracingKernel;
38803880
public int reprojectionKernel;
3881+
public bool transparentSSR;
38813882

38823883
public int width, height, viewCount;
38833884

@@ -3887,14 +3888,15 @@ struct RenderSSRParameters
38873888
public ShaderVariablesScreenSpaceReflection cb;
38883889
}
38893890

3890-
RenderSSRParameters PrepareSSRParameters(HDCamera hdCamera, in HDUtils.PackedMipChainInfo depthPyramid)
3891+
RenderSSRParameters PrepareSSRParameters(HDCamera hdCamera, in HDUtils.PackedMipChainInfo depthPyramid, bool transparentSSR)
38913892
{
38923893
var volumeSettings = hdCamera.volumeStack.GetComponent<ScreenSpaceReflection>();
38933894
var parameters = new RenderSSRParameters();
38943895

38953896
parameters.ssrCS = m_ScreenSpaceReflectionsCS;
38963897
parameters.tracingKernel = m_SsrTracingKernel;
38973898
parameters.reprojectionKernel = m_SsrReprojectionKernel;
3899+
parameters.transparentSSR = transparentSSR;
38983900

38993901
parameters.width = hdCamera.actualWidth;
39003902
parameters.height = hdCamera.actualHeight;
@@ -3927,6 +3929,7 @@ RenderSSRParameters PrepareSSRParameters(HDCamera hdCamera, in HDUtils.PackedMip
39273929
}
39283930

39293931
static void RenderSSR( in RenderSSRParameters parameters,
3932+
RTHandle depthTexture,
39303933
RTHandle depthPyramid,
39313934
RTHandle SsrHitPointTexture,
39323935
RTHandle stencilBuffer,
@@ -3941,6 +3944,12 @@ static void RenderSSR( in RenderSSRParameters parameters,
39413944
using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.SsrTracing)))
39423945
{
39433946
// cmd.SetComputeTextureParam(cs, kernel, "_SsrDebugTexture", m_SsrDebugTexture);
3947+
// Bind the non mip chain if we are rendering the transaprent version
3948+
if (parameters.transparentSSR)
3949+
{
3950+
CoreUtils.SetKeyword(cmd, "DEPTH_SOURCE_NOT_FROM_MIP_CHAIN", true);
3951+
cmd.SetComputeTextureParam(cs, parameters.tracingKernel, HDShaderIDs._DepthTexture, depthTexture);
3952+
}
39443953
cmd.SetComputeTextureParam(cs, parameters.tracingKernel, HDShaderIDs._CameraDepthTexture, depthPyramid);
39453954
cmd.SetComputeTextureParam(cs, parameters.tracingKernel, HDShaderIDs._SsrClearCoatMaskTexture, clearCoatMask);
39463955
cmd.SetComputeTextureParam(cs, parameters.tracingKernel, HDShaderIDs._SsrHitPointTexture, SsrHitPointTexture);
@@ -3960,6 +3969,9 @@ static void RenderSSR( in RenderSSRParameters parameters,
39603969
ConstantBuffer.Push(cmd, parameters.cb, cs, HDShaderIDs._ShaderVariablesScreenSpaceReflection);
39613970

39623971
cmd.DispatchCompute(cs, parameters.tracingKernel, HDUtils.DivRoundUp(parameters.width, 8), HDUtils.DivRoundUp(parameters.height, 8), parameters.viewCount);
3972+
3973+
if (parameters.transparentSSR)
3974+
CoreUtils.SetKeyword(cmd, "DEPTH_SOURCE_NOT_FROM_MIP_CHAIN", false);
39633975
}
39643976

39653977
using (new ProfilingScope(cmd, ProfilingSampler.Get(HDProfileId.SsrReprojection)))
@@ -3997,8 +4009,8 @@ void RenderSSR(HDCamera hdCamera, CommandBuffer cmd, ScriptableRenderContext ren
39974009
// Evaluate the clear coat mask texture based on the lit shader mode
39984010
RTHandle clearCoatMask = hdCamera.frameSettings.litShaderMode == LitShaderMode.Deferred ? m_GbufferManager.GetBuffer(2) : TextureXR.GetBlackTexture();
39994011

4000-
var parameters = PrepareSSRParameters(hdCamera, m_SharedRTManager.GetDepthBufferMipChainInfo());
4001-
RenderSSR(parameters, m_SharedRTManager.GetDepthTexture(), m_SsrHitPointTexture,
4012+
var parameters = PrepareSSRParameters(hdCamera, m_SharedRTManager.GetDepthBufferMipChainInfo(), false);
4013+
RenderSSR(parameters, m_SharedRTManager.GetDepthStencilBuffer(), m_SharedRTManager.GetDepthTexture(), m_SsrHitPointTexture,
40024014
m_SharedRTManager.GetStencilBuffer(hdCamera.frameSettings.IsEnabled(FrameSettingsField.MSAA)), clearCoatMask, previousColorPyramid,
40034015
m_SsrLightingTexture, cmd, renderContext);
40044016

@@ -4027,16 +4039,12 @@ void RenderSSRTransparent(HDCamera hdCamera, CommandBuffer cmd, ScriptableRender
40274039
// Clear the SSR lighting buffer (not sure it is required)
40284040
CoreUtils.SetRenderTarget(cmd, m_SsrLightingTexture, ClearFlag.Color, Color.clear);
40294041
CoreUtils.SetRenderTarget(cmd, m_SsrHitPointTexture, ClearFlag.Color, Color.clear);
4030-
4031-
// Invalid the depth pyramid and regenerate the depth pyramid
4032-
m_IsDepthBufferCopyValid = false;
4033-
GenerateDepthPyramid(hdCamera, cmd, FullScreenDebugMode.DepthPyramid);
40344042
}
40354043

40364044
// Evaluate the screen space reflection for the transparent pixels
40374045
var previousColorPyramid = hdCamera.GetPreviousFrameRT((int)HDCameraFrameHistoryType.ColorBufferMipChain);
4038-
var parameters = PrepareSSRParameters(hdCamera, m_SharedRTManager.GetDepthBufferMipChainInfo());
4039-
RenderSSR(parameters, m_SharedRTManager.GetDepthTexture(), m_SsrHitPointTexture, m_SharedRTManager.GetStencilBuffer(), TextureXR.GetBlackTexture(), previousColorPyramid, m_SsrLightingTexture, cmd, renderContext);
4046+
var parameters = PrepareSSRParameters(hdCamera, m_SharedRTManager.GetDepthBufferMipChainInfo(), true);
4047+
RenderSSR(parameters, m_SharedRTManager.GetDepthStencilBuffer(), m_SharedRTManager.GetDepthTexture(), m_SsrHitPointTexture, m_SharedRTManager.GetStencilBuffer(), TextureXR.GetBlackTexture(), previousColorPyramid, m_SsrLightingTexture, cmd, renderContext);
40404048

40414049
// If color pyramid was not valid, we bind a black texture
40424050
if (!hdCamera.colorPyramidHistoryIsValid)

0 commit comments

Comments
 (0)