From 8d6036c91ca250c2d93f26eba1c9d84fd6759580 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 21 Dec 2024 16:48:15 +0100 Subject: [PATCH] Moved a bunch of common definitions and functions into a separate file --- .../Shaders/2_pass_cs/sgsr2_convert.compute | 19 +------ .../Shaders/2_pass_cs/sgsr2_upscale.compute | 40 +------------- .../Shaders/3_pass_cs/sgsr2_activate.compute | 41 +------------- .../Shaders/3_pass_cs/sgsr2_convert.compute | 26 ++------- .../Shaders/3_pass_cs/sgsr2_upscale.compute | 40 +------------- .../Upscaling/SGSR2/Shaders/sgsr2_common.hlsl | 55 +++++++++++++++++++ .../SGSR2/Shaders/sgsr2_common.hlsl.meta | 3 + 7 files changed, 69 insertions(+), 155 deletions(-) create mode 100644 Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_common.hlsl create mode 100644 Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_common.hlsl.meta 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 582c930..f5a64cb 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 @@ -1,5 +1,6 @@ #pragma kernel CS #include "../sgsr2_birp.hlsl" +#include "../sgsr2_common.hlsl" //============================================================================================================ // @@ -15,23 +16,6 @@ TYPED_TEXTURE2D_X(float2, InputVelocity) : register(t2); RW_TEXTURE2D_X(half4, MotionDepthClipAlphaBuffer) : register(u0); RW_TEXTURE2D_X(uint, YCoCgColor) : register(u1); -cbuffer Params : register(b0) -{ - uint2 renderSize; - uint2 displaySize; - float2 renderSizeRcp; - float2 displaySizeRcp; - float2 jitterOffset; - float2 padding1; - float4 clipToPrevClip[4]; - float preExposure; - float cameraFovAngleHor; - float cameraNear; - float MinLerpContribution; - uint bSameCamera; - uint reset; -}; - [numthreads(8, 8, 1)] void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) { @@ -72,7 +56,6 @@ void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) float Ksep_Kfov_diagonal = Ksep * Kfov * diagonal_length; float Depthsep = Ksep_Kfov_diagonal * (1.0 - maxC); - float EPSILON = 1.19e-07f; Wdepth += clamp((Depthsep / (abs(maxC - topleft4) + EPSILON)), 0.0, 1.0); Wdepth += clamp((Depthsep / (abs(maxC - topRight4) + EPSILON)), 0.0, 1.0); Wdepth += clamp((Depthsep / (abs(maxC - bottomLeft4) + EPSILON)), 0.0, 1.0); diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_upscale.compute b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_upscale.compute index c3a675e..b84cf7f 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_upscale.compute +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_upscale.compute @@ -1,5 +1,6 @@ #pragma kernel CS #include "../sgsr2_birp.hlsl" +#include "../sgsr2_common.hlsl" //============================================================================================================ // @@ -9,50 +10,12 @@ // //============================================================================================================ -float FastLanczos(float base) -{ - float y = base - 1.0f; - float y2 = y * y; - float y_temp = 0.75f * y + y2; - return y_temp * y2; -} - -float3 DecodeColor(uint sample32) -{ - uint x11 = sample32 >> 21u; - uint y11 = sample32 & (2047u << 10u); - uint z10 = sample32 & 1023u; - float3 samplecolor; - samplecolor.x = (float(x11) * (1.0 / 2047.5)); - samplecolor.y = (float(y11) * (4.76953602e-7)) - 0.5; - samplecolor.z = (float(z10) * (1.0 / 1023.5)) - 0.5; - - return samplecolor; -} - TEXTURE2D_X(PrevHistoryOutput) : register(t0); TEXTURE2D_X(MotionDepthClipAlphaBuffer) : register(t1); TYPED_TEXTURE2D_X(uint, YCoCgColor) : register(t2); RW_TEXTURE2D_X(half4, SceneColorOutput) : register(u0); RW_TEXTURE2D_X(half4, HistoryOutput) : register(u1); -cbuffer Params : register(b0) -{ - uint2 renderSize; - uint2 displaySize; - float2 renderSizeRcp; - float2 displaySizeRcp; - float2 jitterOffset; - float2 padding1; - float4 clipToPrevClip[4]; - float preExposure; - float cameraFovAngleHor; - float cameraNear; - float MinLerpContribution; - uint bSameCamera; - uint reset; -}; - [numthreads(8, 8, 1)] void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) { @@ -290,7 +253,6 @@ void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) baseupdate = min(baseupdate, lerp(baseupdate, Upsampledcw.w, clamp(motion_viewport_len *0.05f, 0.0, 1.0))); float basealpha = baseupdate; - const float EPSILON = 1.192e-07f; float boxscale = max(depthfactor, clamp(motion_viewport_len * 0.05f, 0.0, 1.0)); float boxsize = lerp(scalefactor, 1.0f, boxscale); float3 sboxvar = rectboxvar * boxsize; 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 380f122..240b40c 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 @@ -1,7 +1,6 @@ #pragma kernel CS #include "../sgsr2_birp.hlsl" - -// TODO: what about REQUEST_NDC_Y_UP? Might be graphics API-dependent, look at Unity's shader includes. +#include "../sgsr2_common.hlsl" //============================================================================================================ // @@ -11,46 +10,12 @@ // //============================================================================================================ -#define EPSILON 1.19e-07f -float DecodeColorY(uint sample32) -{ - uint x11 = sample32 >> 21u; - return float(x11) * (1.0 / 2047.5); -} - -uint packHalf2x16(float2 value) -{ - return f32tof16(value.x) | (f32tof16(value.y) << 16); -} - -float2 unpackHalf2x16(uint x) -{ - return f16tof32(uint2(x & 0xFFFF, x >> 16)); -} - TYPED_TEXTURE2D_X(uint, PrevLumaHistory) : register(t0); TYPED_TEXTURE2D_X(half4, MotionDepthAlphaBuffer) : register(t1); TYPED_TEXTURE2D_X(uint, YCoCgColor) : register(t2); RW_TEXTURE2D_X(half4, MotionDepthClipAlphaBuffer) : register(u0); RW_TEXTURE2D_X(uint, LumaHistory) : register(u1); -cbuffer Params : register(b0) -{ - uint2 renderSize; - uint2 displaySize; - float2 ViewportSizeInverse; - float2 displaySizeRcp; - float2 jitterOffset; - float2 padding1; - float4 clipToPrevClip[4]; - float preExposure; - float cameraFovAngleHor; - float cameraNear; - float MinLerpContribution; - uint bSameCamera; - uint reset; -}; - [numthreads(8, 8, 1)] void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) { @@ -65,8 +30,8 @@ void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) uint2 InputPos = gl_GlobalInvocationID.xy; - float2 ViewportUV = (float2(gl_GlobalInvocationID.xy) + 0.5f) * ViewportSizeInverse; - float2 gatherCoord = ViewportUV + 0.5f * ViewportSizeInverse; + float2 ViewportUV = (float2(gl_GlobalInvocationID.xy) + 0.5f) * renderSizeRcp; + float2 gatherCoord = ViewportUV + 0.5f * renderSizeRcp; uint luma_reference32 = GATHER_RED_TEXTURE2D_X(YCoCgColor, S_POINT_CLAMP, gatherCoord).w; float luma_reference = DecodeColorY(luma_reference32); 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 3038414..395af7f 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 @@ -1,5 +1,6 @@ #pragma kernel CS #include "../sgsr2_birp.hlsl" +#include "../sgsr2_common.hlsl" //============================================================================================================ // @@ -16,23 +17,6 @@ TYPED_TEXTURE2D_X(float2, InputVelocity) : register(t3); RW_TEXTURE2D_X(half4, MotionDepthAlphaBuffer) : register(u0); RW_TEXTURE2D_X(uint, YCoCgColor) : register(u1); -cbuffer Params : register(b0) -{ - uint2 renderSize; - uint2 displaySize; - float2 ViewportSizeInverse; - float2 displaySizeRcp; - float2 jitterOffset; - float2 padding1; - float4 clipToPrevClip[4]; - float preExposure; - float cameraFovAngleHor; - float cameraNear; - float MinLerpContribution; - uint bSameCamera; - uint reset; -}; - [numthreads(8, 8, 1)] void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) { @@ -41,8 +25,8 @@ void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) half h0 = preExposure; uint2 InputPos = gl_GlobalInvocationID.xy; - float2 gatherCoord = float2(gl_GlobalInvocationID.xy) * ViewportSizeInverse; - float2 ViewportUV = gatherCoord + 0.5f * ViewportSizeInverse; + float2 gatherCoord = float2(gl_GlobalInvocationID.xy) * renderSizeRcp; + float2 ViewportUV = gatherCoord + 0.5f * renderSizeRcp; //derived from ffx_fsr2_reconstruct_dilated_velocity_and_previous_depth.h //FindNearestDepth @@ -57,13 +41,13 @@ void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) NearestZ = max(topleft.z, NearestZ); NearestZ = max(topleft.w, NearestZ); - float2 v11 = float2(ViewportSizeInverse.x, 0.0); + 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); - float2 v13 = float2(0.0, ViewportSizeInverse.y); + 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); diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_upscale.compute b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_upscale.compute index fbc1fed..8c87124 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_upscale.compute +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_upscale.compute @@ -1,5 +1,6 @@ #pragma kernel CS #include "../sgsr2_birp.hlsl" +#include "../sgsr2_common.hlsl" //============================================================================================================ // @@ -9,50 +10,12 @@ // //============================================================================================================ -float FastLanczos(float base) -{ - float y = base - 1.0f; - float y2 = y * y; - float y_temp = 0.75f * y + y2; - return y_temp * y2; -} - -float3 DecodeColor(uint sample32) -{ - uint x11 = sample32 >> 21u; - uint y11 = sample32 & (2047u << 10u); - uint z10 = sample32 & 1023u; - float3 samplecolor; - samplecolor.x = (float(x11) * (1.0 / 2047.5)); - samplecolor.y = (float(y11) * (4.76953602e-7)) - 0.5; - samplecolor.z = (float(z10) * (1.0 / 1023.5)) - 0.5; - - return samplecolor; -} - TEXTURE2D_X(PrevHistoryOutput) : register(t0); TEXTURE2D_X(MotionDepthClipAlphaBuffer) : register(t1); TYPED_TEXTURE2D_X(uint, YCoCgColor) : register(t2); RW_TEXTURE2D_X(half4, SceneColorOutput) : register(u0); RW_TEXTURE2D_X(half4, HistoryOutput) : register(u1); -cbuffer Params : register(b0) -{ - uint2 renderSize; - uint2 displaySize; - float2 renderSizeRcp; - float2 displaySizeRcp; - float2 jitterOffset; - float2 padding1; - float4 clipToPrevClip[4]; - float preExposure; - float cameraFovAngleHor; - float cameraNear; - float MinLerpContribution; - uint bSameCamera; - uint reset; -}; - [numthreads(8, 8, 1)] void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) { @@ -277,7 +240,6 @@ void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID) baseupdate = min(baseupdate, lerp(baseupdate, Upsampledcw.w, clamp(motion_viewport_len *0.05f, 0.0, 1.0))); float basealpha = baseupdate; - const float EPSILON = 1.192e-07f; float boxscale = max(depthfactor, clamp(motion_viewport_len * 0.05f, 0.0, 1.0)); float boxsize = lerp(scalefactor, 1.0f, boxscale); float3 sboxvar = rectboxvar * boxsize; 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 new file mode 100644 index 0000000..27f282e --- /dev/null +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_common.hlsl @@ -0,0 +1,55 @@ +#define EPSILON 1.192e-07f + +cbuffer Params : register(b0) +{ + uint2 renderSize; + uint2 displaySize; + float2 renderSizeRcp; + float2 displaySizeRcp; + float2 jitterOffset; + float2 padding1; + float4 clipToPrevClip[4]; + float preExposure; + float cameraFovAngleHor; + float cameraNear; + float MinLerpContribution; + uint bSameCamera; + uint reset; +}; + +float FastLanczos(float base) +{ + float y = base - 1.0f; + float y2 = y * y; + float y_temp = 0.75f * y + y2; + return y_temp * y2; +} + +float3 DecodeColor(uint sample32) +{ + uint x11 = sample32 >> 21u; + uint y11 = sample32 & (2047u << 10u); + uint z10 = sample32 & 1023u; + float3 samplecolor; + samplecolor.x = (float(x11) * (1.0 / 2047.5)); + samplecolor.y = (float(y11) * (4.76953602e-7)) - 0.5; + samplecolor.z = (float(z10) * (1.0 / 1023.5)) - 0.5; + + return samplecolor; +} + +float DecodeColorY(uint sample32) +{ + uint x11 = sample32 >> 21u; + return float(x11) * (1.0 / 2047.5); +} + +uint packHalf2x16(float2 value) +{ + return f32tof16(value.x) | (f32tof16(value.y) << 16); +} + +float2 unpackHalf2x16(uint x) +{ + return f16tof32(uint2(x & 0xFFFF, x >> 16)); +} diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_common.hlsl.meta b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_common.hlsl.meta new file mode 100644 index 0000000..d3bb9ed --- /dev/null +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_common.hlsl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: bff676b8468748078a48f9d10bb7eabd +timeCreated: 1734795662 \ No newline at end of file