From 19b2969e580ffc3f44580e645963537a314204f7 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 21 Dec 2024 17:06:07 +0100 Subject: [PATCH] Added macros to change nearest-depth and depth-clipping logic based on whether the Unity graphics API uses reversed Z or not --- .../Shaders/2_pass_cs/sgsr2_convert.compute | 14 +++++++------- .../Shaders/3_pass_cs/sgsr2_activate.compute | 2 +- .../Shaders/3_pass_cs/sgsr2_convert.compute | 16 ++++++++-------- .../Upscaling/SGSR2/Shaders/sgsr2_common.hlsl | 8 ++++++++ 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_convert.compute b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_convert.compute index f5a64cb..aa214fe 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_convert.compute +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_convert.compute @@ -38,16 +38,16 @@ void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) float4 bottomLeft = GATHER_RED_TEXTURE2D_X(InputDepth, S_POINT_CLAMP, (gatherCoord+v12)); float2 v14 = float2(ViewportSizeInverse.x*2.0, ViewportSizeInverse.y*2.0); float4 bottomRight = GATHER_RED_TEXTURE2D_X(InputDepth, S_POINT_CLAMP, (gatherCoord+v14)); - float maxC = max(max(max(topleft.y,topRight.x),bottomLeft.z),bottomRight.w); - float topleft4 = max(max(max(topleft.y,topleft.x),topleft.z),topleft.w); - float topLeftMax9 = max(bottomLeft.w,max(max(maxC,topleft4),topRight.w)); + float maxC = DEPTH_NEAREST(DEPTH_NEAREST(DEPTH_NEAREST(topleft.y,topRight.x),bottomLeft.z),bottomRight.w); + float topleft4 = DEPTH_NEAREST(DEPTH_NEAREST(DEPTH_NEAREST(topleft.y,topleft.x),topleft.z),topleft.w); + float topLeftMax9 = DEPTH_NEAREST(bottomLeft.w,DEPTH_NEAREST(DEPTH_NEAREST(maxC,topleft4),topRight.w)); float depthclip = 0.0; - if (maxC > 1.0e-05f) + if (DEPTH_CLIP(maxC)) { - float topRight4 = max(max(max(topRight.y,topRight.x),topRight.z),topRight.w); - float bottomLeft4 = max(max(max(bottomLeft.y,bottomLeft.x),bottomLeft.z),bottomLeft.w); - float bottomRight4 = max(max(max(bottomRight.y,bottomRight.x),bottomRight.z),bottomRight.w); + float topRight4 = DEPTH_NEAREST(DEPTH_NEAREST(DEPTH_NEAREST(topRight.y,topRight.x),topRight.z),topRight.w); + float bottomLeft4 = DEPTH_NEAREST(DEPTH_NEAREST(DEPTH_NEAREST(bottomLeft.y,bottomLeft.x),bottomLeft.z),bottomLeft.w); + float bottomRight4 = DEPTH_NEAREST(DEPTH_NEAREST(DEPTH_NEAREST(bottomRight.y,bottomRight.x),bottomRight.z),bottomRight.w); float Wdepth = 0.0; float Ksep = 1.37e-05f; diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_activate.compute b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_activate.compute index 240b40c..7527649 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_activate.compute +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_activate.compute @@ -47,7 +47,7 @@ void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) #endif float depthclip = 0.0; - if (depth > 1.0e-05f) { + if (DEPTH_CLIP(depth)) { float2 Prevf_sample = PrevUV * float2(renderSize) - 0.5f; float2 Prevfrac = Prevf_sample - floor(Prevf_sample); float OneMinusPrevfacx = 1.0 - Prevfrac.x; diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_convert.compute b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_convert.compute index 395af7f..33214e1 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_convert.compute +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_convert.compute @@ -36,22 +36,22 @@ void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) float4 topleft = GATHER_RED_TEXTURE2D_X(InputDepth, S_POINT_CLAMP, gatherCoord); - NearestZ = max(topleft.x, NearestZ); - NearestZ = max(topleft.y, NearestZ); - NearestZ = max(topleft.z, NearestZ); - NearestZ = max(topleft.w, NearestZ); + NearestZ = DEPTH_NEAREST(topleft.x, NearestZ); + NearestZ = DEPTH_NEAREST(topleft.y, NearestZ); + NearestZ = DEPTH_NEAREST(topleft.z, NearestZ); + NearestZ = DEPTH_NEAREST(topleft.w, NearestZ); float2 v11 = float2(renderSizeRcp.x, 0.0); float2 topRight = GATHER_RED_TEXTURE2D_X(InputDepth, S_POINT_CLAMP, (gatherCoord + v11)).yz; - NearestZ = max(topRight.x, NearestZ); - NearestZ = max(topRight.y, NearestZ); + NearestZ = DEPTH_NEAREST(topRight.x, NearestZ); + NearestZ = DEPTH_NEAREST(topRight.y, NearestZ); float2 v13 = float2(0.0, renderSizeRcp.y); float2 bottomLeft = GATHER_RED_TEXTURE2D_X(InputDepth, S_POINT_CLAMP, (gatherCoord + v13)).xy; - NearestZ = max(bottomLeft.x, NearestZ); - NearestZ = max(bottomLeft.y, NearestZ); + NearestZ = DEPTH_NEAREST(bottomLeft.x, NearestZ); + NearestZ = DEPTH_NEAREST(bottomLeft.y, NearestZ); //refer to ue/fsr2 PostProcessFFX_FSR2ConvertVelocity.usf, and using nearest depth for dilated motion diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_common.hlsl b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_common.hlsl index 27f282e..56a5d1e 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_common.hlsl +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_common.hlsl @@ -1,5 +1,13 @@ #define EPSILON 1.192e-07f +#ifdef UNITY_REVERSED_Z +#define DEPTH_NEAREST(a, b) max((a), (b)) +#define DEPTH_CLIP(depth) ((depth) > 1.0e-05f) +#else +#define DEPTH_NEAREST(a, b) min((a), (b)) +#define DEPTH_CLIP(depth) ((depth) < 1.0f - 1.0e-05f) +#endif + cbuffer Params : register(b0) { uint2 renderSize;