From cac783bebc3ca6275e167bbce4bece0ac1d4f2c9 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Tue, 24 Dec 2024 16:44:03 +0100 Subject: [PATCH] Working SGSR2 2-pass FS implementation, bit dirty but good enough for testing. --- .../PostProcessing/PostProcessResources.asset | 2 + .../Runtime/Effects/Upscaling.cs | 2 + .../Runtime/Effects/Upscaling/SGSR2/SGSR2.cs | 6 +++ .../SGSR2/Shaders/2_pass_fs/sgsr2.shader | 14 +++---- .../Shaders/2_pass_fs/sgsr2_convert.hlsl | 7 +++- .../Shaders/2_pass_fs/sgsr2_upscale.hlsl | 5 +++ .../Upscaling/SGSR2/Shaders/sgsr2_common.hlsl | 2 +- .../Upscaling/SGSR2Upscaler_2PassCS.cs | 4 +- .../Upscaling/SGSR2Upscaler_2PassFS.cs | 39 +++++++++++++++++++ .../Upscaling/SGSR2Upscaler_2PassFS.cs.meta | 3 ++ .../Upscaling/SGSR2Upscaler_3PassCS.cs | 6 +-- .../Runtime/PostProcessResources.cs | 5 +++ 12 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassFS.cs create mode 100644 Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassFS.cs.meta diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/PostProcessResources.asset b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/PostProcessResources.asset index 5c81a3d..6041f70 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/PostProcessResources.asset +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/PostProcessResources.asset @@ -114,6 +114,8 @@ MonoBehaviour: scalableAO: {fileID: 4800000, guid: d7640629310e79646af0f46eb55ae466, type: 3} multiScaleAO: {fileID: 4800000, guid: 67f9497810829eb4791ec19e95781e51, type: 3} screenSpaceReflections: {fileID: 4800000, guid: f997a3dc9254c44459323cced085150c, type: 3} + sgsr2Upscaler: + twoPassFragment: {fileID: 4800000, guid: 9e367486dadedbc4da8313a481aa8a27, type: 3} computeShaders: autoExposure: {fileID: 7200000, guid: 34845e0ca016b7448842e965db5890a5, type: 3} exposureHistogram: {fileID: 7200000, guid: 8c2fcbdf9bc58664f89917f7b9d79501, type: 3} diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling.cs index 9848437..49fc4f0 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling.cs +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling.cs @@ -16,6 +16,7 @@ namespace UnityEngine.Rendering.PostProcessing [InspectorName("FidelityFX Super Resolution 2.2 (FSR2)")] FSR2, [InspectorName("FidelityFX Super Resolution 3.1 (FSR3)")] FSR3, //[InspectorName("Arm Accuracy Super Resolution (ASR)")] ASR, + [InspectorName("Snapdragon Game Super Resolution 2 (SGSR2) 2-Pass Fragment")] SGSR2_2PassFS, [InspectorName("Snapdragon Game Super Resolution 2 (SGSR2) 2-Pass Compute")] SGSR2_2PassCS, [InspectorName("Snapdragon Game Super Resolution 2 (SGSR2) 3-Pass Compute")] SGSR2_3PassCS, [InspectorName("PlayStation Spectral Super Resolution (PSSR)")] PSSR, @@ -181,6 +182,7 @@ namespace UnityEngine.Rendering.PostProcessing { UpscalerType.FSR2 when FSR2Upscaler.IsSupported => new FSR2Upscaler(), UpscalerType.FSR3 when FSR3Upscaler.IsSupported => new FSR3Upscaler(), + UpscalerType.SGSR2_2PassFS => new SGSR2Upscaler_2PassFS(), UpscalerType.SGSR2_2PassCS when SGSR2Upscaler.IsSupported => new SGSR2Upscaler_2PassCS(), UpscalerType.SGSR2_3PassCS when SGSR2Upscaler.IsSupported => new SGSR2Upscaler_3PassCS(), _ => new FSR2Upscaler(), // Fallback for when the selected upscaler is not supported on the current hardware diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/SGSR2.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/SGSR2.cs index 8100bac..cf8a280 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/SGSR2.cs +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/SGSR2.cs @@ -29,6 +29,12 @@ public static class SGSR2 public uint reset; } + [Serializable] + public class Shaders + { + public Shader twoPassFragment; + } + [Serializable] public class ComputeShaders { diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2.shader b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2.shader index fee70e2..84b89ab 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2.shader +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2.shader @@ -1,23 +1,18 @@ Shader "TND/sgsr2_2pass_fs" { - Properties - { - InputColor ("Texture", 2D) = "black" {} - InputDepth ("Texture", 2D) = "gray" {} - InputVelocity ("Texture", 2D) = "black" {} - PrevOutput ("Texture", 2D) = "black" {} - // TODO: MotionDepthAlphaBuffer? Or can we pass that directly from pass 0 to pass 1? - } SubShader { Cull Off ZWrite Off ZTest Always Pass // Convert { + Name "Convert" + HLSLPROGRAM #pragma vertex vert_img #pragma fragment sgsr2_convert #pragma target 4.5 + #pragma enable_d3d11_debug_symbols #include "sgsr2_convert.hlsl" ENDHLSL @@ -25,10 +20,13 @@ Shader "TND/sgsr2_2pass_fs" Pass // Upscale { + Name "Upscale" + HLSLPROGRAM #pragma vertex vert_img #pragma fragment sgsr2_upscale #pragma target 4.5 + #pragma enable_d3d11_debug_symbols #include "sgsr2_upscale.hlsl" ENDHLSL diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_convert.hlsl b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_convert.hlsl index 28548cc..abbf9e8 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_convert.hlsl +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_convert.hlsl @@ -12,8 +12,11 @@ // precision highp float; // precision highp int; -TYPED_TEXTURE2D_X(half, InputDepth); -TYPED_TEXTURE2D_X(half2, InputVelocity); +// TODO: should use the SAMPLE_DEPTH_TEXTURE macros here? +TYPED_TEXTURE2D_X(half, _CameraDepthTexture); +TYPED_TEXTURE2D_X(half2, _CameraMotionVectorsTexture); +#define InputDepth _CameraDepthTexture +#define InputVelocity _CameraMotionVectorsTexture void sgsr2_convert(v2f_img i, out float4 MotionDepthClipAlphaBuffer: SV_Target) { diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_upscale.hlsl b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_upscale.hlsl index 26303b1..06da4d7 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_upscale.hlsl +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2/Shaders/2_pass_fs/sgsr2_upscale.hlsl @@ -16,6 +16,11 @@ TYPED_TEXTURE2D_X(half4, PrevOutput); TYPED_TEXTURE2D_X(half4, MotionDepthClipAlphaBuffer); TYPED_TEXTURE2D_X(half4, InputColor); +// TODO: figure out the appropriate way of handling these SamplerStates in such a way that it all works with SRPs as well +SamplerState samplerPrevOutput; +SamplerState samplerMotionDepthClipAlphaBuffer; +SamplerState samplerInputColor; + void sgsr2_upscale(v2f_img i, out half4 Output: SV_Target) { const half2 texCoord = i.uv; 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 befb867..2fb1bf4 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 @@ -12,7 +12,7 @@ #define DEPTH_CLIP(depth) ((depth) < 1.0f - 1.0e-05f) #endif -cbuffer Params : register(b0) +cbuffer cbSGSR2 : register(b0) { uint2 renderSize; uint2 displaySize; diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassCS.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassCS.cs index 7e4dfbe..561a001 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassCS.cs +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassCS.cs @@ -17,7 +17,7 @@ namespace UnityEngine.Rendering.PostProcessing var shader = context.resources.computeShaders.sgsr2Upscaler.twoPassCompute.convert; int kernelIndex = shader.FindKernel("CS"); - cmd.SetComputeConstantBufferParam(shader, "Params", _paramsBuffer, 0, Marshal.SizeOf()); + cmd.SetComputeConstantBufferParam(shader, "cbSGSR2", _paramsBuffer, 0, Marshal.SizeOf()); cmd.SetComputeTextureParam(shader, kernelIndex, "InputColor", context.source); cmd.SetComputeTextureParam(shader, kernelIndex, "InputDepth", BuiltinRenderTextureType.CameraTarget, 0, RenderTextureSubElement.Depth); cmd.SetComputeTextureParam(shader, kernelIndex, "InputVelocity", BuiltinRenderTextureType.MotionVectors); @@ -36,7 +36,7 @@ namespace UnityEngine.Rendering.PostProcessing int kernelIndex = shader.FindKernel("CS"); uint frameIndex = _frameCount % 2; - cmd.SetComputeConstantBufferParam(shader, "Params", _paramsBuffer, 0, Marshal.SizeOf()); + cmd.SetComputeConstantBufferParam(shader, "cbSGSR2", _paramsBuffer, 0, Marshal.SizeOf()); cmd.SetComputeTextureParam(shader, kernelIndex, "PrevHistoryOutput", _upscaleHistory[frameIndex ^ 1]); cmd.SetComputeTextureParam(shader, kernelIndex, "MotionDepthClipAlphaBuffer", _motionDepthClipAlpha); cmd.SetComputeTextureParam(shader, kernelIndex, "YCoCgColor", _colorLuma); diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassFS.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassFS.cs new file mode 100644 index 0000000..2f67b19 --- /dev/null +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassFS.cs @@ -0,0 +1,39 @@ +using System.Runtime.InteropServices; + +namespace UnityEngine.Rendering.PostProcessing +{ + internal class SGSR2Upscaler_2PassFS: SGSR2Upscaler + { + protected override string VariantName => "SGSR2 2-Pass Fragment"; + + private Material _material; + + public override void CreateContext(PostProcessRenderContext context, Upscaling config) + { + base.CreateContext(context, config); + + _material = new Material(context.resources.shaders.sgsr2Upscaler.twoPassFragment); + } + + public override void DestroyContext() + { + RuntimeUtilities.Destroy(_material); + + base.DestroyContext(); + } + + protected override void DoRender(CommandBuffer cmd, PostProcessRenderContext context, Upscaling config) + { + uint frameIndex = _frameCount % 2; + + cmd.SetGlobalTexture("InputColor", context.source); + cmd.SetGlobalTexture("MotionDepthClipAlphaBuffer", _motionDepthClipAlpha); + cmd.SetGlobalTexture("PrevOutput", _upscaleHistory[frameIndex ^ 1]); + cmd.SetGlobalConstantBuffer(_paramsBuffer, "cbSGSR2", 0, Marshal.SizeOf()); + + cmd.Blit(BuiltinRenderTextureType.None, _motionDepthClipAlpha, _material, 0); + cmd.Blit(BuiltinRenderTextureType.None, _upscaleHistory[frameIndex], _material, 1); + cmd.Blit(_upscaleHistory[frameIndex], context.destination); + } + } +} diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassFS.cs.meta b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassFS.cs.meta new file mode 100644 index 0000000..b1bf38c --- /dev/null +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_2PassFS.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 50ebaec17d8940c0ac51a8721f9f9419 +timeCreated: 1734977118 \ No newline at end of file diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_3PassCS.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_3PassCS.cs index a32e2e3..6be8110 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_3PassCS.cs +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/SGSR2Upscaler_3PassCS.cs @@ -18,7 +18,7 @@ namespace UnityEngine.Rendering.PostProcessing var shader = context.resources.computeShaders.sgsr2Upscaler.threePassCompute.convert; int kernelIndex = shader.FindKernel("CS"); - cmd.SetComputeConstantBufferParam(shader, "Params", _paramsBuffer, 0, Marshal.SizeOf()); + cmd.SetComputeConstantBufferParam(shader, "cbSGSR2", _paramsBuffer, 0, Marshal.SizeOf()); cmd.SetComputeTextureParam(shader, kernelIndex, "InputOpaqueColor", config.ColorOpaqueOnly); cmd.SetComputeTextureParam(shader, kernelIndex, "InputColor", context.source); cmd.SetComputeTextureParam(shader, kernelIndex, "InputDepth", BuiltinRenderTextureType.CameraTarget, 0, RenderTextureSubElement.Depth); @@ -38,7 +38,7 @@ namespace UnityEngine.Rendering.PostProcessing int kernelIndex = shader.FindKernel("CS"); uint frameIndex = _frameCount % 2; - cmd.SetComputeConstantBufferParam(shader, "Params", _paramsBuffer, 0, Marshal.SizeOf()); + cmd.SetComputeConstantBufferParam(shader, "cbSGSR2", _paramsBuffer, 0, Marshal.SizeOf()); cmd.SetComputeTextureParam(shader, kernelIndex, "PrevLumaHistory", _lumaHistory[frameIndex ^ 1]); cmd.SetComputeTextureParam(shader, kernelIndex, "MotionDepthAlphaBuffer", _motionDepthAlpha); cmd.SetComputeTextureParam(shader, kernelIndex, "YCoCgColor", _colorLuma); @@ -57,7 +57,7 @@ namespace UnityEngine.Rendering.PostProcessing int kernelIndex = shader.FindKernel("CS"); uint frameIndex = _frameCount % 2; - cmd.SetComputeConstantBufferParam(shader, "Params", _paramsBuffer, 0, Marshal.SizeOf()); + cmd.SetComputeConstantBufferParam(shader, "cbSGSR2", _paramsBuffer, 0, Marshal.SizeOf()); cmd.SetComputeTextureParam(shader, kernelIndex, "PrevHistoryOutput", _upscaleHistory[frameIndex ^ 1]); cmd.SetComputeTextureParam(shader, kernelIndex, "MotionDepthClipAlphaBuffer", _motionDepthClipAlpha); cmd.SetComputeTextureParam(shader, kernelIndex, "YCoCgColor", _colorLuma); diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/PostProcessResources.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/PostProcessResources.cs index ce92f00..8d62f44 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/PostProcessResources.cs +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/PostProcessResources.cs @@ -140,6 +140,11 @@ namespace UnityEngine.Rendering.PostProcessing /// public Shader screenSpaceReflections; + /// + /// The shaders used by the SnapDragon Game Super Resolution 2 (SGSR2) Upscaler. + /// + public SGSR2.Shaders sgsr2Upscaler; + /// /// Returns a copy of this class and its content. ///