Browse Source

Reworked 2-pass FS implementation to integrate more directly with the PPV2 framework, allowing for multi-target rendering using the fullscreen triangle blit, which eliminates the need for an extra blit at the end.

Also fixed a bug where the scale factor was inverted, which caused FS upscaling output to be blurry.
sgsr2_fs
Nico de Poel 1 year ago
parent
commit
72df347315
  1. 4
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_convert.hlsl
  2. 10
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_ppv2.shader
  3. 2
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_ppv2.shader.meta
  4. 5
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_upscale.hlsl
  5. 2
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler.cs
  6. 12
      Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassFS.cs

4
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_convert.hlsl

@ -18,9 +18,9 @@ TYPED_TEXTURE2D_X(half2, _CameraMotionVectorsTexture);
#define InputDepth _CameraDepthTexture #define InputDepth _CameraDepthTexture
#define InputVelocity _CameraMotionVectorsTexture #define InputVelocity _CameraMotionVectorsTexture
void sgsr2_convert(v2f_img i, out float4 MotionDepthClipAlphaBuffer: SV_Target)
void sgsr2_convert(VaryingsDefault i, out float4 MotionDepthClipAlphaBuffer: SV_Target)
{ {
const half2 texCoord = i.uv;
const half2 texCoord = i.texcoord;
uint2 InputPos = uint2(texCoord * renderSize); uint2 InputPos = uint2(texCoord * renderSize);
float2 gatherCoord = texCoord - 0.5f * renderSizeRcp; float2 gatherCoord = texCoord - 0.5f * renderSizeRcp;

10
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2.shader → Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_ppv2.shader

@ -1,4 +1,4 @@
Shader "TND/sgsr2_2pass_fs"
Shader "TND/PPV2/sgsr2_2pass_fs"
{ {
SubShader SubShader
{ {
@ -9,11 +9,13 @@ Shader "TND/sgsr2_2pass_fs"
Name "Convert" Name "Convert"
HLSLPROGRAM HLSLPROGRAM
#pragma vertex vert_img
#pragma vertex VertDefault
#pragma fragment sgsr2_convert #pragma fragment sgsr2_convert
#pragma target 4.5 #pragma target 4.5
#pragma enable_d3d11_debug_symbols #pragma enable_d3d11_debug_symbols
#define UNITY_CG_INCLUDED
#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
#include "sgsr2_convert.hlsl" #include "sgsr2_convert.hlsl"
ENDHLSL ENDHLSL
} }
@ -23,11 +25,13 @@ Shader "TND/sgsr2_2pass_fs"
Name "Upscale" Name "Upscale"
HLSLPROGRAM HLSLPROGRAM
#pragma vertex vert_img
#pragma vertex VertDefault
#pragma fragment sgsr2_upscale #pragma fragment sgsr2_upscale
#pragma target 4.5 #pragma target 4.5
#pragma enable_d3d11_debug_symbols #pragma enable_d3d11_debug_symbols
#define UNITY_CG_INCLUDED
#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
#include "sgsr2_upscale.hlsl" #include "sgsr2_upscale.hlsl"
ENDHLSL ENDHLSL
} }

2
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2.shader.meta → Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_ppv2.shader.meta

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 9e367486dadedbc4da8313a481aa8a27
guid: 59bc1035dd975f64d8141148a7088d0a
ShaderImporter: ShaderImporter:
externalObjects: {} externalObjects: {}
defaultTextures: [] defaultTextures: []

5
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_upscale.hlsl

@ -21,9 +21,9 @@ SamplerState samplerPrevOutput;
SamplerState samplerMotionDepthClipAlphaBuffer; SamplerState samplerMotionDepthClipAlphaBuffer;
SamplerState samplerInputColor; SamplerState samplerInputColor;
void sgsr2_upscale(v2f_img i, out half4 Output: SV_Target)
void sgsr2_upscale(VaryingsDefault i, out half4 Output: SV_Target0, out half4 HistoryOutput: SV_Target1)
{ {
const half2 texCoord = i.uv;
const half2 texCoord = i.texcoord;
half Biasmax_viewportXScale = scaleRatio.x; half Biasmax_viewportXScale = scaleRatio.x;
half scalefactor = scaleRatio.y; half scalefactor = scaleRatio.y;
@ -262,4 +262,5 @@ void sgsr2_upscale(v2f_img i, out half4 Output: SV_Target)
Upsampledcw.xyz = lerp(HistoryColor, Upsampledcw.xyz, alpha); Upsampledcw.xyz = lerp(HistoryColor, Upsampledcw.xyz, alpha);
Output = half4(Upsampledcw.xyz, 0.0); Output = half4(Upsampledcw.xyz, 0.0);
HistoryOutput = Output;
} }

2
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler.cs

@ -70,7 +70,7 @@ namespace UnityEngine.Rendering.PostProcessing
parms.cameraFovAngleHor = Mathf.Tan(context.camera.fieldOfView * Mathf.Deg2Rad * 0.5f) * parms.renderSize.x * parms.renderSizeRcp.y; parms.cameraFovAngleHor = Mathf.Tan(context.camera.fieldOfView * Mathf.Deg2Rad * 0.5f) * parms.renderSize.x * parms.renderSizeRcp.y;
parms.cameraNear = context.camera.nearClipPlane; parms.cameraNear = context.camera.nearClipPlane;
parms.minLerpContribution = 0f; parms.minLerpContribution = 0f;
parms.scaleRatio = new Vector2(parms.renderSize.x * parms.displaySizeRcp.x, parms.renderSize.y * parms.displaySizeRcp.y);
parms.scaleRatio = new Vector2(parms.displaySize.x * parms.renderSizeRcp.x, parms.displaySize.y * parms.renderSizeRcp.y);
parms.bSameCamera = isCameraStill ? 1u : 0u; parms.bSameCamera = isCameraStill ? 1u : 0u;
parms.reset = config.Reset ? 1u : 0u; parms.reset = config.Reset ? 1u : 0u;
_paramsBuffer.UpdateBufferData(cmd); _paramsBuffer.UpdateBufferData(cmd);

12
Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassFS.cs

@ -7,6 +7,7 @@ namespace UnityEngine.Rendering.PostProcessing
protected override string VariantName => "SGSR2 2-Pass Fragment"; protected override string VariantName => "SGSR2 2-Pass Fragment";
private Material _material; private Material _material;
private readonly RenderTargetIdentifier[] _mrt = new RenderTargetIdentifier[2];
public override void CreateContext(PostProcessRenderContext context, Upscaling config) public override void CreateContext(PostProcessRenderContext context, Upscaling config)
{ {
@ -26,14 +27,19 @@ namespace UnityEngine.Rendering.PostProcessing
{ {
uint frameIndex = _frameCount % 2; uint frameIndex = _frameCount % 2;
// TODO: try using a PropertySheet here again, now that PPV2 is functional
cmd.SetGlobalTexture("InputColor", context.source); cmd.SetGlobalTexture("InputColor", context.source);
cmd.SetGlobalTexture("MotionDepthClipAlphaBuffer", _motionDepthClipAlpha); cmd.SetGlobalTexture("MotionDepthClipAlphaBuffer", _motionDepthClipAlpha);
cmd.SetGlobalTexture("PrevOutput", _upscaleHistory[frameIndex ^ 1]); cmd.SetGlobalTexture("PrevOutput", _upscaleHistory[frameIndex ^ 1]);
cmd.SetGlobalConstantBuffer(_paramsBuffer, "cbSGSR2", 0, Marshal.SizeOf<SGSR2.Params>()); cmd.SetGlobalConstantBuffer(_paramsBuffer, "cbSGSR2", 0, Marshal.SizeOf<SGSR2.Params>());
cmd.Blit(BuiltinRenderTextureType.None, _motionDepthClipAlpha, _material, 0);
cmd.Blit(BuiltinRenderTextureType.None, _upscaleHistory[frameIndex], _material, 1);
cmd.Blit(_upscaleHistory[frameIndex], context.destination);
cmd.SetRenderTarget(_motionDepthClipAlpha, context.source);
cmd.DrawMesh(RuntimeUtilities.fullscreenTriangle, Matrix4x4.identity, _material, 0, 0);
_mrt[0] = context.destination;
_mrt[1] = _upscaleHistory[frameIndex];
cmd.SetRenderTarget(_mrt, context.destination);
cmd.DrawMesh(RuntimeUtilities.fullscreenTriangle, Matrix4x4.identity, _material, 0, 1);
} }
} }
} }
Loading…
Cancel
Save