Skip to content

Commit

Permalink
Fix support MSAA copy depth
Browse files Browse the repository at this point in the history
  • Loading branch information
alelievr committed Jan 24, 2023
1 parent c29509d commit 041aecc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class CurrentDepthToCustomDepth : CustomPass
Shader depthToCustomDepthShader;

readonly int currentCameraDepth = Shader.PropertyToID("_CurrentCameraDepth");
static readonly int currentCameraDepthMSAA = Shader.PropertyToID("_CurrentCameraDepthMSAA");
private static readonly int sampleMSAA = Shader.PropertyToID("_SampleMSAA");

protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
{
Expand All @@ -29,7 +31,11 @@ protected override void Setup(ScriptableRenderContext renderContext, CommandBuff

protected override void Execute(CustomPassContext ctx)
{
depthToCustomDepth.SetTexture(currentCameraDepth, ctx.cameraDepthBuffer);
depthToCustomDepth.SetFloat(sampleMSAA, ctx.cameraDepthBuffer.isMSAAEnabled ? 1 : 0);
if (ctx.cameraDepthBuffer.isMSAAEnabled)
depthToCustomDepth.SetTexture(currentCameraDepthMSAA, ctx.cameraDepthBuffer);
else
depthToCustomDepth.SetTexture(currentCameraDepth, ctx.cameraDepthBuffer);
CoreUtils.SetRenderTarget(ctx.cmd, ctx.customDepthBuffer.Value);
CoreUtils.DrawFullScreen(ctx.cmd, depthToCustomDepth, shaderPassId: 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,25 @@ Shader "Hidden/FullScreen/CurrentDepthToCustomDepth"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassCommon.hlsl"
#pragma enable_d3d11_debug_symbols

// The PositionInputs struct allow you to retrieve a lot of useful information for your fullScreenShader:
// struct PositionInputs
// {
// float3 positionWS; // World space position (could be camera-relative)
// float2 positionNDC; // Normalized screen coordinates within the viewport : [0, 1) (with the half-pixel offset)
// uint2 positionSS; // Screen space pixel coordinates : [0, NumPixels)
// uint2 tileCoord; // Screen tile coordinates : [0, NumTiles)
// float deviceDepth; // Depth from the depth buffer : [0, 1] (typically reversed)
// float linearDepth; // View space Z coordinate : [Near, Far]
// };

// To sample custom buffers, you have access to these functions:
// But be careful, on most platforms you can't sample to the bound color buffer. It means that you
// can't use the SampleCustomColor when the pass color buffer is set to custom (and same for camera the buffer).
// float4 CustomPassSampleCustomColor(float2 uv);
// float4 CustomPassLoadCustomColor(uint2 pixelCoords);
// float LoadCustomDepth(uint2 pixelCoords);
// float SampleCustomDepth(float2 uv);

// There are also a lot of utility function you can use inside Common.hlsl and Color.hlsl,
// you can check them out in the source code of the core SRP package.

TEXTURE2D_X(_CurrentCameraDepth);
TEXTURE2D_X_MSAA(float, _CurrentCameraDepthMSAA);

int _MSAASampleCount;

float FullScreenPass(Varyings varyings) : SV_Depth
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(varyings);

return LOAD_TEXTURE2D_X(_CurrentCameraDepth, uint2(varyings.positionCS.xy));
if (_MSAASampleCount > 1)
{
// Resolve MSAA depth:
float depth = 10000000.0f;
for (int i = 0; i < _MSAASampleCount; i++)
depth = min(depth, LOAD_TEXTURE2D_X_MSAA(_CurrentCameraDepthMSAA, uint2(varyings.positionCS.xy), i));
return depth;
}
else
return LOAD_TEXTURE2D_X(_CurrentCameraDepth, uint2(varyings.positionCS.xy));
}

ENDHLSL
Expand Down

0 comments on commit 041aecc

Please sign in to comment.