Browse Source

Unity-fied all SGSR2 shaders by changing all texture declarations and accesses into TextureXR-style macros, with a shared BiRP definitions file.

sgsr2_fs
Nico de Poel 1 year ago
parent
commit
7217b0fc32
  1. 53
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_convert.compute
  2. 43
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_upscale.compute
  3. 41
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_activate.compute
  4. 64
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_convert.compute
  5. 43
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_upscale.compute
  6. 26
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_birp.hlsl
  7. 3
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_birp.hlsl.meta

53
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_convert.compute

@ -1,4 +1,5 @@
#pragma kernel CS #pragma kernel CS
#include "../sgsr2_birp.hlsl"
//============================================================================================================ //============================================================================================================
// //
@ -8,22 +9,13 @@
// //
//============================================================================================================ //============================================================================================================
float2 decodeVelocityFromTexture(float2 ev) {
const float inv_div = 1.0f / (0.499f * 0.5f);
float2 dv;
dv.xy = ev.xy * inv_div - 32767.0f / 65535.0f * inv_div;
//dv.z = uintBitsToFloat((uint(round(ev.z * 65535.0f)) << 16) | uint(round(ev.w * 65535.0f)));
return dv;
}
Texture2D InputColor : register(t0);
Texture2D<float> InputDepth : register(t1);
Texture2D<float2> InputVelocity : register(t2);
RWTexture2D<float4> MotionDepthClipAlphaBuffer : register(u0);
RWTexture2D<uint> YCoCgColor : register(u1);
TEXTURE2D_X(InputColor) : register(t0);
TYPED_TEXTURE2D_X(float, InputDepth) : register(t1);
TYPED_TEXTURE2D_X(float2, InputVelocity) : register(t2);
RW_TEXTURE2D_X(float4, MotionDepthClipAlphaBuffer) : register(u0);
RW_TEXTURE2D_X(uint, YCoCgColor) : register(u1);
cbuffer Params : register(b0)
{
CBUFFER_START(Params)
uint2 renderSize; uint2 renderSize;
uint2 displaySize; uint2 displaySize;
float2 renderSizeRcp; float2 renderSizeRcp;
@ -37,31 +29,30 @@ cbuffer Params : register(b0)
float MinLerpContribution; float MinLerpContribution;
uint bSameCamera; uint bSameCamera;
uint reset; uint reset;
};
SamplerState s_PointClamp : register(s0);
SamplerState s_LinearClamp : register(s1);
CBUFFER_END
[numthreads(8, 8, 1)] [numthreads(8, 8, 1)]
void CS(uint3 globalInvocationID : SV_DispatchThreadID)
void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID)
{ {
UNITY_XR_ASSIGN_VIEW_INDEX(gl_GlobalInvocationID.z);
float Exposure_co_rcp = preExposure; float Exposure_co_rcp = preExposure;
float2 ViewportSizeInverse = displaySizeRcp.xy; float2 ViewportSizeInverse = displaySizeRcp.xy;
uint2 InputPos = globalInvocationID.xy;
uint2 InputPos = gl_GlobalInvocationID.xy;
float2 gatherCoord = float2(globalInvocationID.xy) * ViewportSizeInverse;
float2 gatherCoord = float2(gl_GlobalInvocationID.xy) * ViewportSizeInverse;
float2 ViewportUV = gatherCoord + 0.5f * ViewportSizeInverse; float2 ViewportUV = gatherCoord + 0.5f * ViewportSizeInverse;
//derived from ffx_fsr2_reconstruct_dilated_velocity_and_previous_depth.h //derived from ffx_fsr2_reconstruct_dilated_velocity_and_previous_depth.h
//FindNearestDepth //FindNearestDepth
float4 topleft = InputDepth.GatherRed(s_PointClamp, gatherCoord);
float4 topleft = GATHER_RED_TEXTURE2D_X(InputDepth, S_POINT_CLAMP, gatherCoord);
float2 v10 = float2(ViewportSizeInverse.x*2.0, 0.0); float2 v10 = float2(ViewportSizeInverse.x*2.0, 0.0);
float4 topRight = InputDepth.GatherRed(s_PointClamp, (gatherCoord+v10));
float4 topRight = GATHER_RED_TEXTURE2D_X(InputDepth, S_POINT_CLAMP, (gatherCoord+v10));
float2 v12 = float2(0.0, ViewportSizeInverse.y*2.0); float2 v12 = float2(0.0, ViewportSizeInverse.y*2.0);
float4 bottomLeft = InputDepth.GatherRed(s_PointClamp, (gatherCoord+v12));
float4 bottomLeft = GATHER_RED_TEXTURE2D_X(InputDepth, S_POINT_CLAMP, (gatherCoord+v12));
float2 v14 = float2(ViewportSizeInverse.x*2.0, ViewportSizeInverse.y*2.0); float2 v14 = float2(ViewportSizeInverse.x*2.0, ViewportSizeInverse.y*2.0);
float4 bottomRight = InputDepth.GatherRed(s_PointClamp, (gatherCoord+v14));
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 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 topleft4 = max(max(max(topleft.y,topleft.x),topleft.z),topleft.w);
float topLeftMax9 = max(bottomLeft.w,max(max(maxC,topleft4),topRight.w)); float topLeftMax9 = max(bottomLeft.w,max(max(maxC,topleft4),topRight.w));
@ -90,7 +81,7 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
//refer to ue/fsr2 PostProcessFFX_FSR2ConvertVelocity.usf, and using nearest depth for dilated motion //refer to ue/fsr2 PostProcessFFX_FSR2ConvertVelocity.usf, and using nearest depth for dilated motion
float2 EncodedVelocity = InputVelocity[InputPos];
float2 EncodedVelocity = LOAD_TEXTURE2D_X(InputVelocity, InputPos);
float2 motion; float2 motion;
if (EncodedVelocity.x > 0.0) if (EncodedVelocity.x > 0.0)
@ -110,10 +101,8 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
motion = Position.xy - PreScreen; motion = Position.xy - PreScreen;
} }
motion = EncodedVelocity;
////////////compute luma ////////////compute luma
float3 Colorrgb = InputColor[InputPos].xyz;
float3 Colorrgb = LOAD_TEXTURE2D_X(InputColor, InputPos).xyz;
///simple tonemap ///simple tonemap
float ColorMax = max(max(Colorrgb.x, Colorrgb.y), Colorrgb.z) + Exposure_co_rcp; float ColorMax = max(max(Colorrgb.x, Colorrgb.y), Colorrgb.z) + Exposure_co_rcp;
@ -129,8 +118,8 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
uint y11 = uint(Colorycocg.y * 2047.5); uint y11 = uint(Colorycocg.y * 2047.5);
uint z10 = uint(Colorycocg.z * 1023.5); uint z10 = uint(Colorycocg.z * 1023.5);
YCoCgColor[InputPos] = ((x11 << 21u) | (y11 << 10u)) | z10;
YCoCgColor[COORD_TEXTURE2D_X(InputPos)] = ((x11 << 21u) | (y11 << 10u)) | z10;
float4 v29 = float4(motion, depthclip, ColorMax); float4 v29 = float4(motion, depthclip, ColorMax);
MotionDepthClipAlphaBuffer[InputPos] = v29;
MotionDepthClipAlphaBuffer[COORD_TEXTURE2D_X(InputPos)] = v29;
} }

43
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_cs/sgsr2_upscale.compute

@ -1,4 +1,5 @@
#pragma kernel CS #pragma kernel CS
#include "../sgsr2_birp.hlsl"
//============================================================================================================ //============================================================================================================
// //
@ -29,14 +30,13 @@ float3 DecodeColor(uint sample32)
return samplecolor; return samplecolor;
} }
Texture2D PrevHistoryOutput : register(t0);
Texture2D MotionDepthClipAlphaBuffer : register(t1);
Texture2D<uint> YCoCgColor : register(t2);
RWTexture2D<float4> SceneColorOutput : register(u0);
RWTexture2D<float4> HistoryOutput : register(u1);
TEXTURE2D_X(PrevHistoryOutput) : register(t0);
TEXTURE2D_X(MotionDepthClipAlphaBuffer) : register(t1);
TYPED_TEXTURE2D_X(uint, YCoCgColor) : register(t2);
RW_TEXTURE2D_X(float4, SceneColorOutput) : register(u0);
RW_TEXTURE2D_X(float4, HistoryOutput) : register(u1);
cbuffer Params : register(b0)
{
CBUFFER_START(Params)
uint2 renderSize; uint2 renderSize;
uint2 displaySize; uint2 displaySize;
float2 renderSizeRcp; float2 renderSizeRcp;
@ -50,14 +50,13 @@ cbuffer Params : register(b0)
float MinLerpContribution; float MinLerpContribution;
uint bSameCamera; uint bSameCamera;
uint reset; uint reset;
};
SamplerState s_PointClamp : register(s0);
SamplerState s_LinearClamp : register(s1);
CBUFFER_END
[numthreads(8, 8, 1)] [numthreads(8, 8, 1)]
void CS(uint3 globalInvocationID : SV_DispatchThreadID)
void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID)
{ {
UNITY_XR_ASSIGN_VIEW_INDEX(gl_GlobalInvocationID.z);
float Biasmax_viewportXScale = min(float(displaySize.x) / float(renderSize.x), 1.99); //Biasmax_viewportXScale float Biasmax_viewportXScale = min(float(displaySize.x) / float(renderSize.x), 1.99); //Biasmax_viewportXScale
float scalefactor = min(20.0, pow((float(displaySize.x) / float(renderSize.x)) * (float(displaySize.y) / float(renderSize.y)), 3.0)); float scalefactor = min(20.0, pow((float(displaySize.x) / float(renderSize.x)) * (float(displaySize.y) / float(renderSize.y)), 3.0));
float f2 = preExposure; //1.0; //preExposure float f2 = preExposure; //1.0; //preExposure
@ -65,13 +64,13 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
float2 HistoryInfoViewportSize = float2(displaySize); float2 HistoryInfoViewportSize = float2(displaySize);
float2 InputJitter = jitterOffset; float2 InputJitter = jitterOffset;
float2 InputInfoViewportSize = float2(renderSize); float2 InputInfoViewportSize = float2(renderSize);
float2 Hruv = (float2(globalInvocationID.xy) + 0.5f) * HistoryInfoViewportSizeInverse;
float2 Hruv = (float2(gl_GlobalInvocationID.xy) + 0.5f) * HistoryInfoViewportSizeInverse;
float2 Jitteruv; float2 Jitteruv;
Jitteruv.x = clamp(Hruv.x + (InputJitter.x * HistoryInfoViewportSizeInverse.x), 0.0, 1.0); Jitteruv.x = clamp(Hruv.x + (InputJitter.x * HistoryInfoViewportSizeInverse.x), 0.0, 1.0);
Jitteruv.y = clamp(Hruv.y + (InputJitter.y * HistoryInfoViewportSizeInverse.y), 0.0, 1.0); Jitteruv.y = clamp(Hruv.y + (InputJitter.y * HistoryInfoViewportSizeInverse.y), 0.0, 1.0);
int2 InputPos = int2(Jitteruv * InputInfoViewportSize); int2 InputPos = int2(Jitteruv * InputInfoViewportSize);
float4 mda = MotionDepthClipAlphaBuffer.SampleLevel(s_LinearClamp, Jitteruv, 0).xyzw;
float4 mda = SAMPLE_TEXTURE2D_X_LOD(MotionDepthClipAlphaBuffer, S_LINEAR_CLAMP, Jitteruv, 0).xyzw;
float2 Motion = mda.xy; float2 Motion = mda.xy;
///ScreenPosToViewportScale&Bias ///ScreenPosToViewportScale&Bias
@ -86,7 +85,7 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
float depthfactor = mda.z; float depthfactor = mda.z;
float ColorMax = mda.w; float ColorMax = mda.w;
float4 History = PrevHistoryOutput.SampleLevel(s_LinearClamp, PrevUV, 0);
float4 History = SAMPLE_TEXTURE2D_X_LOD(PrevHistoryOutput, S_LINEAR_CLAMP, PrevUV, 0);
float3 HistoryColor = History.xyz; float3 HistoryColor = History.xyz;
float Historyw = History.w; float Historyw = History.w;
float Wfactor = clamp(abs(Historyw), 0.0, 1.0); float Wfactor = clamp(abs(Historyw), 0.0, 1.0);
@ -113,8 +112,8 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
int2 InputPosBtmRight = 1 + InputPos; int2 InputPosBtmRight = 1 + InputPos;
float2 gatherCoord = float2(InputPos) * renderSizeRcp; float2 gatherCoord = float2(InputPos) * renderSizeRcp;
uint btmRight = YCoCgColor[InputPosBtmRight].x;
uint4 topleft = YCoCgColor.GatherRed(s_PointClamp, gatherCoord);
uint btmRight = LOAD_TEXTURE2D_X(YCoCgColor, InputPosBtmRight).x;
uint4 topleft = GATHER_RED_TEXTURE2D_X(YCoCgColor, S_POINT_CLAMP, gatherCoord);
uint2 topRight = 0; uint2 topRight = 0;
uint2 bottomLeft = 0; uint2 bottomLeft = 0;
@ -122,12 +121,12 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
if (sameCameraFrmNum!=0u) if (sameCameraFrmNum!=0u)
{ {
topRight = YCoCgColor.GatherRed(s_PointClamp, gatherCoord + float2(renderSizeRcp.x, 0.0)).yz;
bottomLeft = YCoCgColor.GatherRed(s_PointClamp, gatherCoord + float2(0.0, renderSizeRcp.y)).xy;
topRight = GATHER_RED_TEXTURE2D_X(YCoCgColor, S_POINT_CLAMP, gatherCoord + float2(renderSizeRcp.x, 0.0)).yz;
bottomLeft = GATHER_RED_TEXTURE2D_X(YCoCgColor, S_POINT_CLAMP, gatherCoord + float2(0.0, renderSizeRcp.y)).xy;
} }
else else
{ {
uint2 btmRight = YCoCgColor.GatherRed(s_PointClamp, gatherCoord + float2(renderSizeRcp.x, renderSizeRcp.y)).xz;
uint2 btmRight = GATHER_RED_TEXTURE2D_X(YCoCgColor, S_POINT_CLAMP, gatherCoord + float2(renderSizeRcp.x, renderSizeRcp.y)).xz;
bottomLeft.y = btmRight.x; bottomLeft.y = btmRight.x;
topRight.x = btmRight.y; topRight.x = btmRight.y;
} }
@ -313,7 +312,7 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
float alpha = clamp(Upsampledcw.w / alphasum + float(reset), 0.0, 1.0); float alpha = clamp(Upsampledcw.w / alphasum + float(reset), 0.0, 1.0);
Upsampledcw.xyz = lerp(HistoryColor, Upsampledcw.xyz, alpha); Upsampledcw.xyz = lerp(HistoryColor, Upsampledcw.xyz, alpha);
HistoryOutput[globalInvocationID.xy] = float4(Upsampledcw.xyz, Wfactor);
HistoryOutput[COORD_TEXTURE2D_X(gl_GlobalInvocationID.xy)] = float4(Upsampledcw.xyz, Wfactor);
////ycocg to rgb ////ycocg to rgb
float x_z = Upsampledcw.x - Upsampledcw.z; float x_z = Upsampledcw.x - Upsampledcw.z;
@ -328,5 +327,5 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
if (ColorMax > 4000.0f) scale = ColorMax; if (ColorMax > 4000.0f) scale = ColorMax;
Upsampledcw.xyz = Upsampledcw.xyz * scale; Upsampledcw.xyz = Upsampledcw.xyz * scale;
SceneColorOutput[globalInvocationID.xy] = Upsampledcw;
SceneColorOutput[COORD_TEXTURE2D_X(gl_GlobalInvocationID.xy)] = Upsampledcw;
} }

41
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_activate.compute

@ -1,4 +1,5 @@
#pragma kernel CS #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. // TODO: what about REQUEST_NDC_Y_UP? Might be graphics API-dependent, look at Unity's shader includes.
@ -27,14 +28,13 @@ float2 unpackHalf2x16(uint x)
return f16tof32(uint2(x & 0xFFFF, x >> 16)); return f16tof32(uint2(x & 0xFFFF, x >> 16));
} }
Texture2D<uint> PrevLumaHistory : register(t0);
Texture2D MotionDepthAlphaBuffer : register(t1);
Texture2D<uint> YCoCgColor : register(t2);
RWTexture2D<float4> MotionDepthClipAlphaBuffer : register(u0);
RWTexture2D<uint> LumaHistory : register(u1);
TYPED_TEXTURE2D_X(uint, PrevLumaHistory) : register(t0);
TEXTURE2D_X(MotionDepthAlphaBuffer) : register(t1);
TYPED_TEXTURE2D_X(uint, YCoCgColor) : register(t2);
RW_TEXTURE2D_X(float4, MotionDepthClipAlphaBuffer) : register(u0);
RW_TEXTURE2D_X(uint, LumaHistory) : register(u1);
cbuffer Params : register(b0)
{
CBUFFER_START(Params)
uint2 renderSize; uint2 renderSize;
uint2 displaySize; uint2 displaySize;
float2 ViewportSizeInverse; float2 ViewportSizeInverse;
@ -48,14 +48,13 @@ cbuffer Params : register(b0)
float MinLerpContribution; float MinLerpContribution;
uint bSameCamera; uint bSameCamera;
uint reset; uint reset;
};
SamplerState s_PointClamp : register(s0);
SamplerState s_LinearClamp : register(s1);
CBUFFER_END
[numthreads(8, 8, 1)] [numthreads(8, 8, 1)]
void CS(uint3 globalInvocationID : SV_DispatchThreadID)
void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID)
{ {
UNITY_XR_ASSIGN_VIEW_INDEX(gl_GlobalInvocationID.z);
int2 sampleOffset[4] = { int2 sampleOffset[4] = {
int2(-1, -1), int2(-1, -1),
int2(-1, +0), int2(-1, +0),
@ -63,14 +62,14 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
int2(+0, +0) int2(+0, +0)
}; };
uint2 InputPos = globalInvocationID.xy;
uint2 InputPos = gl_GlobalInvocationID.xy;
float2 ViewportUV = (float2(globalInvocationID.xy) + 0.5f) * ViewportSizeInverse;
float2 ViewportUV = (float2(gl_GlobalInvocationID.xy) + 0.5f) * ViewportSizeInverse;
float2 gatherCoord = ViewportUV + 0.5f * ViewportSizeInverse; float2 gatherCoord = ViewportUV + 0.5f * ViewportSizeInverse;
uint luma_reference32 = YCoCgColor.GatherRed(s_PointClamp, gatherCoord).w;
uint luma_reference32 = GATHER_RED_TEXTURE2D_X(YCoCgColor, S_POINT_CLAMP, gatherCoord).w;
float luma_reference = DecodeColorY(luma_reference32); float luma_reference = DecodeColorY(luma_reference32);
float4 mda = MotionDepthAlphaBuffer[globalInvocationID.xy].xyzw; //motion depth alpha
float4 mda = LOAD_TEXTURE2D_X(MotionDepthAlphaBuffer, gl_GlobalInvocationID.xy).xyzw; //motion depth alpha
float depth = mda.z; float depth = mda.z;
float alphamask = mda.w; float alphamask = mda.w;
float2 motion = mda.xy; float2 motion = mda.xy;
@ -101,7 +100,7 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
float Kfov = cameraFovAngleHor; float Kfov = cameraFovAngleHor;
float Ksep_Kfov_diagonal = Ksep * Kfov * diagonal_length; float Ksep_Kfov_diagonal = Ksep * Kfov * diagonal_length;
for (int index = 0; index < 4; index+=2){ for (int index = 0; index < 4; index+=2){
float4 gPrevdepth = MotionDepthAlphaBuffer.GatherBlue(s_PointClamp, PrevUV, sampleOffset[index]);
float4 gPrevdepth = GATHER_BLUE_TEXTURE2D_X_OFFSET(MotionDepthAlphaBuffer, S_POINT_CLAMP, PrevUV, sampleOffset[index]);
float tdepth1 = min(gPrevdepth.x, gPrevdepth.y); float tdepth1 = min(gPrevdepth.x, gPrevdepth.y);
float tdepth2 = min(gPrevdepth.z, gPrevdepth.w); float tdepth2 = min(gPrevdepth.z, gPrevdepth.w);
float fPrevdepth = min(tdepth1, tdepth2); float fPrevdepth = min(tdepth1, tdepth2);
@ -110,7 +109,7 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
float weight = Bilinweights[index]; float weight = Bilinweights[index];
Wdepth += clamp(Depthsep / (abs(fPrevdepth - depth) + EPSILON), 0.0, 1.0) * weight; Wdepth += clamp(Depthsep / (abs(fPrevdepth - depth) + EPSILON), 0.0, 1.0) * weight;
float2 gPrevdepth2 = MotionDepthAlphaBuffer.GatherBlue(s_PointClamp, PrevUV, sampleOffset[index + int(1)]).zw;
float2 gPrevdepth2 = GATHER_BLUE_TEXTURE2D_X_OFFSET(MotionDepthAlphaBuffer, S_POINT_CLAMP, PrevUV, sampleOffset[index + int(1)]).zw;
fPrevdepth = min(min(gPrevdepth2.x, gPrevdepth2.y), tdepth2); fPrevdepth = min(min(gPrevdepth2.x, gPrevdepth2.y), tdepth2);
Depthsep = Ksep_Kfov_diagonal * (1.0 - min(fPrevdepth, depth)); Depthsep = Ksep_Kfov_diagonal * (1.0 - min(fPrevdepth, depth));
weight = Bilinweights[index + int(1)]; weight = Bilinweights[index + int(1)];
@ -120,7 +119,7 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
} }
float2 current_luma_diff; float2 current_luma_diff;
uint prev_luma_diff_pack = PrevLumaHistory.GatherRed(s_PointClamp, PrevUV).w;
uint prev_luma_diff_pack = GATHER_RED_TEXTURE2D_X(PrevLumaHistory, S_POINT_CLAMP, PrevUV).w;
float2 prev_luma_diff; float2 prev_luma_diff;
prev_luma_diff.x = unpackHalf2x16(prev_luma_diff_pack >> 16u).x; prev_luma_diff.x = unpackHalf2x16(prev_luma_diff_pack >> 16u).x;
prev_luma_diff.y = unpackHalf2x16((prev_luma_diff_pack & uint(0xFFFF))).x; prev_luma_diff.y = unpackHalf2x16((prev_luma_diff_pack & uint(0xFFFF))).x;
@ -141,6 +140,6 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
} }
alphamask = floor(alphamask) + 0.5f * float((current_luma_diff.x != 0.0f) && (abs(current_luma_diff.y) != abs(luma_diff))); alphamask = floor(alphamask) + 0.5f * float((current_luma_diff.x != 0.0f) && (abs(current_luma_diff.y) != abs(luma_diff)));
LumaHistory[InputPos] = (packHalf2x16(float2(current_luma_diff.x, 0.0)) << 16u) | packHalf2x16(float2(current_luma_diff.y, 0.0));
MotionDepthClipAlphaBuffer[InputPos] = float4(motion, depthclip, alphamask);
LumaHistory[COORD_TEXTURE2D_X(InputPos)] = (packHalf2x16(float2(current_luma_diff.x, 0.0)) << 16u) | packHalf2x16(float2(current_luma_diff.y, 0.0));
MotionDepthClipAlphaBuffer[COORD_TEXTURE2D_X(InputPos)] = float4(motion, depthclip, alphamask);
} }

64
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_convert.compute

@ -1,4 +1,5 @@
#pragma kernel CS #pragma kernel CS
#include "../sgsr2_birp.hlsl"
//============================================================================================================ //============================================================================================================
// //
@ -8,23 +9,14 @@
// //
//============================================================================================================ //============================================================================================================
float2 decodeVelocityFromTexture(float2 ev) {
const float inv_div = 1.0f / (0.499f * 0.5f);
float2 dv;
dv.xy = ev.xy * inv_div - 32767.0f / 65535.0f * inv_div;
//dv.z = uintBitsToFloat((uint(round(ev.z * 65535.0f)) << 16) | uint(round(ev.w * 65535.0f)));
return dv;
}
Texture2D InputOpaqueColor : register(t0);
Texture2D InputColor : register(t1);
Texture2D<float> InputDepth : register(t2);
Texture2D<float2> InputVelocity : register(t3);
RWTexture2D<float4> MotionDepthAlphaBuffer : register(u0);
RWTexture2D<uint> YCoCgColor : register(u1);
TEXTURE2D_X(InputOpaqueColor) : register(t0);
TEXTURE2D_X(InputColor) : register(t1);
TYPED_TEXTURE2D_X(float, InputDepth) : register(t2);
TYPED_TEXTURE2D_X(float2, InputVelocity) : register(t3);
RW_TEXTURE2D_X(float4, MotionDepthAlphaBuffer) : register(u0);
RW_TEXTURE2D_X(uint, YCoCgColor) : register(u1);
cbuffer Params : register(b0)
{
CBUFFER_START(Params)
uint2 renderSize; uint2 renderSize;
uint2 displaySize; uint2 displaySize;
float2 ViewportSizeInverse; float2 ViewportSizeInverse;
@ -38,25 +30,26 @@ cbuffer Params : register(b0)
float MinLerpContribution; float MinLerpContribution;
uint bSameCamera; uint bSameCamera;
uint reset; uint reset;
};
SamplerState s_PointClamp : register(s0);
SamplerState s_LinearClamp : register(s1);
CBUFFER_END
[numthreads(8, 8, 1)] [numthreads(8, 8, 1)]
void CS(uint3 globalInvocationID : SV_DispatchThreadID)
void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID)
{ {
float2 gatherCoord = float2(globalInvocationID.xy) * ViewportSizeInverse;
UNITY_XR_ASSIGN_VIEW_INDEX(gl_GlobalInvocationID.z);
half h0 = preExposure;
uint2 InputPos = gl_GlobalInvocationID.xy;
float2 gatherCoord = float2(gl_GlobalInvocationID.xy) * ViewportSizeInverse;
float2 ViewportUV = gatherCoord + 0.5f * ViewportSizeInverse; float2 ViewportUV = gatherCoord + 0.5f * ViewportSizeInverse;
uint2 InputPos = globalInvocationID.xy;
//derived from ffx_fsr2_reconstruct_dilated_velocity_and_previous_depth.h //derived from ffx_fsr2_reconstruct_dilated_velocity_and_previous_depth.h
//FindNearestDepth //FindNearestDepth
int2 InputPosBtmRight = int2(1, 1) + int2(InputPos); int2 InputPosBtmRight = int2(1, 1) + int2(InputPos);
float NearestZ = InputDepth[InputPosBtmRight].x;
float NearestZ = LOAD_TEXTURE2D_X(InputDepth, InputPosBtmRight).x;
float4 topleft = InputDepth.GatherRed(s_PointClamp, gatherCoord);
float4 topleft = GATHER_RED_TEXTURE2D_X(InputDepth, S_POINT_CLAMP, gatherCoord);
NearestZ = max(topleft.x, NearestZ); NearestZ = max(topleft.x, NearestZ);
NearestZ = max(topleft.y, NearestZ); NearestZ = max(topleft.y, NearestZ);
@ -64,21 +57,20 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
NearestZ = max(topleft.w, NearestZ); NearestZ = max(topleft.w, NearestZ);
float2 v11 = float2(ViewportSizeInverse.x, 0.0); float2 v11 = float2(ViewportSizeInverse.x, 0.0);
float2 topRight = InputDepth.GatherRed(s_PointClamp, (gatherCoord + v11)).yz;
float2 topRight = GATHER_RED_TEXTURE2D_X(InputDepth, S_POINT_CLAMP, (gatherCoord + v11)).yz;
NearestZ = max(topRight.x, NearestZ); NearestZ = max(topRight.x, NearestZ);
NearestZ = max(topRight.y, NearestZ); NearestZ = max(topRight.y, NearestZ);
float2 v13 = float2(0.0, ViewportSizeInverse.y); float2 v13 = float2(0.0, ViewportSizeInverse.y);
float2 bottomLeft = InputDepth.GatherRed(s_PointClamp, (gatherCoord + v13)).xy;
float2 bottomLeft = GATHER_RED_TEXTURE2D_X(InputDepth, S_POINT_CLAMP, (gatherCoord + v13)).xy;
NearestZ = max(bottomLeft.x, NearestZ); NearestZ = max(bottomLeft.x, NearestZ);
NearestZ = max(bottomLeft.y, NearestZ); NearestZ = max(bottomLeft.y, NearestZ);
//refer to ue/fsr2 PostProcessFFX_FSR2ConvertVelocity.usf, and using nearest depth for dilated motion //refer to ue/fsr2 PostProcessFFX_FSR2ConvertVelocity.usf, and using nearest depth for dilated motion
// TODO: wondering if this whole song and dance about decoding velocity is really necessary for Unity
float2 EncodedVelocity = InputVelocity[InputPos];
float2 EncodedVelocity = LOAD_TEXTURE2D_X(InputVelocity, InputPos);
float2 motion; float2 motion;
if (EncodedVelocity.x > 0.0) if (EncodedVelocity.x > 0.0)
@ -98,13 +90,11 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
motion = Position.xy - PreScreen; motion = Position.xy - PreScreen;
} }
motion = EncodedVelocity;
////////////compute luma ////////////compute luma
float3 Colorrgb = InputColor[InputPos].xyz;
float3 Colorrgb = LOAD_TEXTURE2D_X(InputColor, InputPos).xyz;
///simple tonemap ///simple tonemap
Colorrgb /= max(max(Colorrgb.x, Colorrgb.y), Colorrgb.z) + preExposure;
Colorrgb /= max(max(Colorrgb.x, Colorrgb.y), Colorrgb.z) + h0;
float3 Colorycocg; float3 Colorycocg;
Colorycocg.x = 0.25 * (Colorrgb.x + 2.0 * Colorrgb.y + Colorrgb.z); Colorycocg.x = 0.25 * (Colorrgb.x + 2.0 * Colorrgb.y + Colorrgb.z);
@ -116,14 +106,14 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
uint y11 = uint(Colorycocg.y * 2047.5); uint y11 = uint(Colorycocg.y * 2047.5);
uint z10 = uint(Colorycocg.z * 1023.5); uint z10 = uint(Colorycocg.z * 1023.5);
float3 Colorprergb = InputOpaqueColor[InputPos].xyz;
float3 Colorprergb = LOAD_TEXTURE2D_X(InputOpaqueColor, InputPos).xyz;
///simple tonemap ///simple tonemap
Colorprergb /= max(max(Colorprergb.x, Colorprergb.y), Colorprergb.z) + preExposure;
Colorprergb /= max(max(Colorprergb.x, Colorprergb.y), Colorprergb.z) + h0;
float3 delta = abs(Colorrgb - Colorprergb); float3 delta = abs(Colorrgb - Colorprergb);
float alpha_mask = max(delta.x, max(delta.y, delta.z)); float alpha_mask = max(delta.x, max(delta.y, delta.z));
alpha_mask = (0.35f * 1000.0f) * alpha_mask; alpha_mask = (0.35f * 1000.0f) * alpha_mask;
YCoCgColor[InputPos] = ((x11 << 21u) | (y11 << 10u)) | z10;
MotionDepthAlphaBuffer[InputPos] = float4(motion, NearestZ, alpha_mask);
YCoCgColor[COORD_TEXTURE2D_X(InputPos)] = ((x11 << 21u) | (y11 << 10u)) | z10;
MotionDepthAlphaBuffer[COORD_TEXTURE2D_X(InputPos)] = float4(motion, NearestZ, alpha_mask);
} }

43
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/3_pass_cs/sgsr2_upscale.compute

@ -1,4 +1,5 @@
#pragma kernel CS #pragma kernel CS
#include "../sgsr2_birp.hlsl"
//============================================================================================================ //============================================================================================================
// //
@ -29,14 +30,13 @@ float3 DecodeColor(uint sample32)
return samplecolor; return samplecolor;
} }
Texture2D PrevHistoryOutput : register(t0);
Texture2D MotionDepthClipAlphaBuffer : register(t1);
Texture2D<uint> YCoCgColor : register(t2);
RWTexture2D<float4> SceneColorOutput : register(u0);
RWTexture2D<float4> HistoryOutput : register(u1);
TEXTURE2D_X(PrevHistoryOutput) : register(t0);
TEXTURE2D_X(MotionDepthClipAlphaBuffer) : register(t1);
TYPED_TEXTURE2D_X(uint, YCoCgColor) : register(t2);
RW_TEXTURE2D_X(float4, SceneColorOutput) : register(u0);
RW_TEXTURE2D_X(float4, HistoryOutput) : register(u1);
cbuffer Params : register(b0)
{
CBUFFER_START(Params)
uint2 renderSize; uint2 renderSize;
uint2 displaySize; uint2 displaySize;
float2 renderSizeRcp; float2 renderSizeRcp;
@ -50,14 +50,13 @@ cbuffer Params : register(b0)
float MinLerpContribution; float MinLerpContribution;
uint bSameCamera; uint bSameCamera;
uint reset; uint reset;
};
SamplerState s_PointClamp : register(s0);
SamplerState s_LinearClamp : register(s1);
CBUFFER_END
[numthreads(8, 8, 1)] [numthreads(8, 8, 1)]
void CS(uint3 globalInvocationID : SV_DispatchThreadID)
void CS(uint3 gl_GlobalInvocationID : SV_DispatchThreadID)
{ {
UNITY_XR_ASSIGN_VIEW_INDEX(gl_GlobalInvocationID.z);
float Biasmax_viewportXScale = min(float(displaySize.x) / float(renderSize.x), 1.99); //Biasmax_viewportXScale float Biasmax_viewportXScale = min(float(displaySize.x) / float(renderSize.x), 1.99); //Biasmax_viewportXScale
float scalefactor = min(20.0, pow((float(displaySize.x) / float(renderSize.x)) * (float(displaySize.y) / float(renderSize.y)), 3.0)); float scalefactor = min(20.0, pow((float(displaySize.x) / float(renderSize.x)) * (float(displaySize.y) / float(renderSize.y)), 3.0));
float f2 = preExposure; //1.0; //preExposure float f2 = preExposure; //1.0; //preExposure
@ -65,7 +64,7 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
float2 HistoryInfoViewportSize = float2(displaySize); float2 HistoryInfoViewportSize = float2(displaySize);
float2 InputJitter = jitterOffset; float2 InputJitter = jitterOffset;
float2 InputInfoViewportSize = float2(renderSize); float2 InputInfoViewportSize = float2(renderSize);
float2 Hruv = (float2(globalInvocationID.xy) + 0.5f) * HistoryInfoViewportSizeInverse;
float2 Hruv = (float2(gl_GlobalInvocationID.xy) + 0.5f) * HistoryInfoViewportSizeInverse;
float2 Jitteruv; float2 Jitteruv;
Jitteruv.x = clamp(Hruv.x + (InputJitter.x * HistoryInfoViewportSizeInverse.x), 0.0, 1.0); Jitteruv.x = clamp(Hruv.x + (InputJitter.x * HistoryInfoViewportSizeInverse.x), 0.0, 1.0);
Jitteruv.y = clamp(Hruv.y + (InputJitter.y * HistoryInfoViewportSizeInverse.y), 0.0, 1.0); Jitteruv.y = clamp(Hruv.y + (InputJitter.y * HistoryInfoViewportSizeInverse.y), 0.0, 1.0);
@ -73,8 +72,8 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
int2 InputPos = int2(Jitteruv * InputInfoViewportSize); int2 InputPos = int2(Jitteruv * InputInfoViewportSize);
//float2 Motion = texelFetch(MotionDepthClipAlphaBuffer, InputPos, 0).xy; //float2 Motion = texelFetch(MotionDepthClipAlphaBuffer, InputPos, 0).xy;
float alphab = MotionDepthClipAlphaBuffer[InputPos].w;
float3 mda = MotionDepthClipAlphaBuffer.SampleLevel(s_LinearClamp, Jitteruv, 0).xyz;
float alphab = LOAD_TEXTURE2D_X(MotionDepthClipAlphaBuffer, InputPos).w;
float3 mda = SAMPLE_TEXTURE2D_X_LOD(MotionDepthClipAlphaBuffer, S_LINEAR_CLAMP, Jitteruv, 0).xyz;
float2 Motion = mda.xy; float2 Motion = mda.xy;
///ScreenPosToViewportScale&Bias ///ScreenPosToViewportScale&Bias
@ -91,7 +90,7 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
float alphamask = (alphab - history_value) * 0.001f; float alphamask = (alphab - history_value) * 0.001f;
history_value *= 2.0; history_value *= 2.0;
float4 History = PrevHistoryOutput.SampleLevel(s_LinearClamp, PrevUV, 0);
float4 History = SAMPLE_TEXTURE2D_X_LOD(PrevHistoryOutput, S_LINEAR_CLAMP, PrevUV, 0);
float3 HistoryColor = History.xyz; float3 HistoryColor = History.xyz;
float Historyw = History.w; float Historyw = History.w;
float Wfactor = max(clamp(abs(Historyw), 0.0, 1.0), alphamask); float Wfactor = max(clamp(abs(Historyw), 0.0, 1.0), alphamask);
@ -118,10 +117,10 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
int2 InputPosBtmRight = 1 + InputPos; int2 InputPosBtmRight = 1 + InputPos;
float2 gatherCoord = float2(InputPos) * renderSizeRcp; float2 gatherCoord = float2(InputPos) * renderSizeRcp;
uint btmRight = YCoCgColor[InputPosBtmRight].x;
uint4 topleft = YCoCgColor.GatherRed(s_PointClamp, gatherCoord);
uint2 topRight = YCoCgColor.GatherRed(s_PointClamp, gatherCoord + float2(renderSizeRcp.x, 0.0)).yz;
uint2 bottomLeft = YCoCgColor.GatherRed(s_PointClamp, gatherCoord + float2(0.0, renderSizeRcp.y)).xy;
uint btmRight = LOAD_TEXTURE2D_X(YCoCgColor, InputPosBtmRight).x;
uint4 topleft = GATHER_RED_TEXTURE2D_X(YCoCgColor, S_POINT_CLAMP, gatherCoord);
uint2 topRight = GATHER_RED_TEXTURE2D_X(YCoCgColor, S_POINT_CLAMP, gatherCoord + float2(renderSizeRcp.x, 0.0)).yz;
uint2 bottomLeft = GATHER_RED_TEXTURE2D_X(YCoCgColor, S_POINT_CLAMP, gatherCoord + float2(0.0, renderSizeRcp.y)).xy;
float3 rectboxmin; float3 rectboxmin;
float3 rectboxmax; float3 rectboxmax;
@ -298,7 +297,7 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
float alpha = clamp(Upsampledcw.w / alphasum + float(reset), 0.0, 1.0); float alpha = clamp(Upsampledcw.w / alphasum + float(reset), 0.0, 1.0);
Upsampledcw.xyz = lerp(HistoryColor, Upsampledcw.xyz, alpha); Upsampledcw.xyz = lerp(HistoryColor, Upsampledcw.xyz, alpha);
HistoryOutput[globalInvocationID.xy] = float4(Upsampledcw.xyz, Wfactor);
HistoryOutput[COORD_TEXTURE2D_X(gl_GlobalInvocationID.xy)] = float4(Upsampledcw.xyz, Wfactor);
////ycocg to grb ////ycocg to grb
float x_z = Upsampledcw.x - Upsampledcw.z; float x_z = Upsampledcw.x - Upsampledcw.z;
@ -312,5 +311,5 @@ void CS(uint3 globalInvocationID : SV_DispatchThreadID)
float scale = preExposure / ((1.0f + 1.0f / 65504.0f) - compMax); //(1.0f + 1.0f / 65504.0f) = 1.000015e+00 float scale = preExposure / ((1.0f + 1.0f / 65504.0f) - compMax); //(1.0f + 1.0f / 65504.0f) = 1.000015e+00
Upsampledcw.xyz = Upsampledcw.xyz * scale; Upsampledcw.xyz = Upsampledcw.xyz * scale;
SceneColorOutput[globalInvocationID.xy] = Upsampledcw;
SceneColorOutput[COORD_TEXTURE2D_X(gl_GlobalInvocationID.xy)] = Upsampledcw;
} }

26
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_birp.hlsl

@ -0,0 +1,26 @@
#include "UnityCG.cginc"
#define TEXTURE2D_X(textureName) Texture2D textureName
#define TYPED_TEXTURE2D_X(type, textureName) Texture2D<type> textureName
#define RW_TEXTURE2D_X(type, textureName) RWTexture2D<type> textureName
#define COORD_TEXTURE2D_X(pixelCoord) pixelCoord
#define LOAD_TEXTURE2D_X(textureName, unCoord2) textureName[unCoord2]
#define SAMPLE_TEXTURE2D_X_LOD(textureName, samplerName, coord2, lod) textureName.SampleLevel(samplerName, coord2, lod)
#define GATHER_RED_TEXTURE2D_X(textureName, samplerName, coord2) textureName.GatherRed(samplerName, coord2)
#define GATHER_BLUE_TEXTURE2D_X_OFFSET(textureName, samplerName, coord2, offset) textureName.GatherBlue(samplerName, coord2, offset)
SamplerState s_PointClamp : register(s0);
SamplerState s_LinearClamp : register(s1);
#define S_POINT_CLAMP s_PointClamp
#define S_LINEAR_CLAMP s_LinearClamp
#define UNITY_XR_ASSIGN_VIEW_INDEX(viewIndex)
inline float2 decodeVelocityFromTexture(float2 ev)
{
// Nothing to do, motion vectors are not encoded
return ev;
}

3
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/sgsr2_birp.hlsl.meta

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f3edae28a3f74031996d08ca5a87c28e
timeCreated: 1734775340
Loading…
Cancel
Save