From d5bbadaa775306c8918c3cca607cfed18bfe170c Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 22 Mar 2025 12:29:04 +0100 Subject: [PATCH] Made all the ASR shader keywords global and encapsulated all of the keyword management into a separate class, which will enable and disable keywords globally based on the initialization flags and dispatch parameters. --- .../Upscaling/ASR/Runtime/AsrContext.cs | 4 + .../Upscaling/ASR/Runtime/AsrKeywords.cs | 124 ++++++++++++++++++ .../Upscaling/ASR/Runtime/AsrKeywords.cs.meta | 3 + .../Effects/Upscaling/ASR/Runtime/AsrPass.cs | 77 ++--------- ...sr2_compute_luminance_pyramid_pass.compute | 8 +- .../ASR/Shaders/ffxm_fsr2_lock_pass.compute | 8 +- 6 files changed, 148 insertions(+), 76 deletions(-) create mode 100644 Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs create mode 100644 Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs.meta diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrContext.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrContext.cs index 4175bbe..a1587af 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrContext.cs +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrContext.cs @@ -65,6 +65,8 @@ namespace ArmASR private readonly Asr.GenerateReactiveConstants[] _generateReactiveConstantsArray = { new Asr.GenerateReactiveConstants() }; private ref Asr.GenerateReactiveConstants GenReactiveConsts => ref _generateReactiveConstantsArray[0]; + private AsrKeywords _keywords = new(); + private bool _firstExecution; private Vector2 _previousJitterOffset; private int _resourceFrameIndex; @@ -142,6 +144,8 @@ namespace ArmASR if (dispatchParams.UseTextureArrays) commandBuffer.EnableShaderKeyword("UNITY_FFXM_TEXTURE2D_X_ARRAY"); + _keywords.ApplyKeywords(commandBuffer, _contextDescription.Flags, dispatchParams); + if (_firstExecution) { commandBuffer.SetRenderTarget(_resources.LockStatus[0]); diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs new file mode 100644 index 0000000..673e57f --- /dev/null +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs @@ -0,0 +1,124 @@ +using UnityEngine; +using UnityEngine.Rendering; + +namespace ArmASR +{ + public class AsrKeywords + { + private static readonly string OptionHalfPrecision = "FFXM_HALF"; + private static readonly string OptionHdrColorInput = "FFXM_FSR2_OPTION_HDR_COLOR_INPUT"; + private static readonly string OptionLowResolutionMotionVectors = "FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS"; + private static readonly string OptionJitteredMotionVectors = "FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS"; + private static readonly string OptionInvertedDepth = "FFXM_FSR2_OPTION_INVERTED_DEPTH"; + private static readonly string OptionReprojectUseLut = "FFXM_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE"; + private static readonly string OptionApplySharpening = "FFXM_FSR2_OPTION_APPLY_SHARPENING"; + +#if UNITY_2021_2_OR_NEWER + private readonly GlobalKeyword _halfPrecisionKeyword; + private readonly GlobalKeyword _hdrColorInputKeyword; + private readonly GlobalKeyword _lowResMotionVectorsKeyword; + private readonly GlobalKeyword _jitteredMotionVectorsKeyword; + private readonly GlobalKeyword _invertedDepthKeyword; + private readonly GlobalKeyword _reprojectUseLutKeyword; + private readonly GlobalKeyword _applySharpeningKeyword; +#endif + + public AsrKeywords() + { +#if UNITY_2021_2_OR_NEWER + _halfPrecisionKeyword = new GlobalKeyword(OptionHalfPrecision); + _hdrColorInputKeyword = new GlobalKeyword(OptionHdrColorInput); + _lowResMotionVectorsKeyword = new GlobalKeyword(OptionLowResolutionMotionVectors); + _jitteredMotionVectorsKeyword = new GlobalKeyword(OptionJitteredMotionVectors); + _invertedDepthKeyword = new GlobalKeyword(OptionInvertedDepth); + _reprojectUseLutKeyword = new GlobalKeyword(OptionReprojectUseLut); + _applySharpeningKeyword = new GlobalKeyword(OptionApplySharpening); +#endif + } + + public void ApplyKeywords(CommandBuffer commandBuffer, Asr.InitializationFlags initFlags, in Asr.DispatchDescription dispatchParams) + { + bool useLut = false; +#if UNITY_2022_1_OR_NEWER // This will also work in 2020.3.43+ and 2021.3.14+ + if (SystemInfo.computeSubGroupSize == 64) + { + useLut = true; + } +#endif + + // This matches the permutation rules from the CreatePipeline* functions +#if UNITY_2021_2_OR_NEWER + if ((initFlags & Asr.InitializationFlags.EnableFP16Usage) != 0) + commandBuffer.EnableKeyword(_halfPrecisionKeyword); + else + commandBuffer.DisableKeyword(_halfPrecisionKeyword); + + if ((initFlags & Asr.InitializationFlags.EnableHighDynamicRange) != 0) + commandBuffer.EnableKeyword(_hdrColorInputKeyword); + else + commandBuffer.DisableKeyword(_hdrColorInputKeyword); + + if ((initFlags & Asr.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) + commandBuffer.EnableKeyword(_lowResMotionVectorsKeyword); + else + commandBuffer.DisableKeyword(_lowResMotionVectorsKeyword); + + if ((initFlags & Asr.InitializationFlags.EnableMotionVectorsJitterCancellation) != 0) + commandBuffer.EnableKeyword(_jitteredMotionVectorsKeyword); + else + commandBuffer.DisableKeyword(_jitteredMotionVectorsKeyword); + + if ((initFlags & Asr.InitializationFlags.EnableDepthInverted) != 0) + commandBuffer.EnableKeyword(_invertedDepthKeyword); + else + commandBuffer.DisableKeyword(_invertedDepthKeyword); + + if (useLut) + commandBuffer.EnableKeyword(_reprojectUseLutKeyword); + else + commandBuffer.DisableKeyword(_reprojectUseLutKeyword); + + if (dispatchParams.EnableSharpening) + commandBuffer.EnableKeyword(_applySharpeningKeyword); + else + commandBuffer.DisableKeyword(_applySharpeningKeyword); +#else + if ((initFlags & Asr.InitializationFlags.EnableFP16Usage) != 0) + commandBuffer.EnableShaderKeyword(OptionHalfPrecision); + else + commandBuffer.DisableShaderKeyword(OptionHalfPrecision); + + if ((initFlags & Asr.InitializationFlags.EnableHighDynamicRange) != 0) + commandBuffer.EnableShaderKeyword(OptionHdrColorInput); + else + commandBuffer.DisableShaderKeyword(OptionHdrColorInput); + + if ((initFlags & Asr.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) + commandBuffer.EnableShaderKeyword(OptionLowResolutionMotionVectors); + else + commandBuffer.DisableShaderKeyword(OptionLowResolutionMotionVectors); + + if ((initFlags & Asr.InitializationFlags.EnableMotionVectorsJitterCancellation) != 0) + commandBuffer.EnableShaderKeyword(OptionJitteredMotionVectors); + else + commandBuffer.DisableShaderKeyword(OptionJitteredMotionVectors); + + if ((initFlags & Asr.InitializationFlags.EnableDepthInverted) != 0) + commandBuffer.EnableShaderKeyword(OptionInvertedDepth); + else + commandBuffer.DisableShaderKeyword(OptionInvertedDepth); + + if (useLut) + commandBuffer.EnableShaderKeyword(OptionReprojectUseLut); + else + commandBuffer.DisableShaderKeyword(OptionReprojectUseLut); + + if (dispatchParams.EnableSharpening) + commandBuffer.EnableShaderKeyword(OptionApplySharpening); + else + commandBuffer.DisableShaderKeyword(OptionApplySharpening); +#endif + + } + } +} diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs.meta b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs.meta new file mode 100644 index 0000000..5b51256 --- /dev/null +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrKeywords.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a2636bdd1878444fb3d3475610d379df +timeCreated: 1742641520 \ No newline at end of file diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrPass.cs b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrPass.cs index a166814..27f62b7 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrPass.cs +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Runtime/AsrPass.cs @@ -46,7 +46,6 @@ namespace ArmASR protected Material FragmentMaterial; protected int FragmentPass; protected MaterialPropertyBlock FragmentProperties; - protected readonly List FragmentKeywords = new(); private CustomSampler _sampler; @@ -64,8 +63,6 @@ namespace ArmASR Asr.DestroyObject(FragmentMaterial); FragmentMaterial = null; } - - FragmentKeywords.Clear(); } public void ScheduleDispatch(CommandBuffer commandBuffer, Asr.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY) @@ -78,11 +75,6 @@ namespace ArmASR protected abstract void DoScheduleDispatch(CommandBuffer commandBuffer, Asr.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY); protected void InitComputeShader(string passName, ComputeShader shader) - { - InitComputeShader(passName, shader, ContextDescription.Flags); - } - - private void InitComputeShader(string passName, ComputeShader shader, Asr.InitializationFlags flags) { if (shader == null) { @@ -92,19 +84,9 @@ namespace ArmASR ComputeShader = shader; KernelIndex = ComputeShader.FindKernel("main"); _sampler = CustomSampler.Create(passName); - - foreach (string keyword in GetKeywords(flags)) - { - ComputeShader.EnableKeyword(keyword); - } } protected void InitFragmentShader(string passName, Shader shader, int passNumber) - { - InitFragmentShader(passName, shader, passNumber, ContextDescription.Flags); - } - - private void InitFragmentShader(string passName, Shader shader, int passNumber, Asr.InitializationFlags flags) { if (shader == null) { @@ -113,42 +95,22 @@ namespace ArmASR FragmentMaterial = new Material(shader); FragmentPass = passNumber; - _sampler = CustomSampler.Create(passName); - FragmentProperties = new MaterialPropertyBlock(); - foreach (string keyword in GetKeywords(flags)) - { - // TODO: also want to include keywords that should be disabled (false) - // TODO: might be better to just determine all the keywords once up front and set them globally, probably don't even need to manage them here but in the AsrContext instead - // NOTE: be mindful of UNITY_2021_2_OR_NEWER (Local & GlobalKeyword were introduced there) - FragmentKeywords.Add(new LocalKeyword(shader, keyword)); - } + _sampler = CustomSampler.Create(passName); } - protected void BlitFragment(CommandBuffer commandBuffer) + protected void BlitFragment(CommandBuffer commandBuffer, params RenderTargetIdentifier[] renderTargets) { + if (renderTargets == null || renderTargets.Length == 0) + commandBuffer.SetRenderTarget(BuiltinRenderTextureType.None); + else if (renderTargets.Length == 1) + commandBuffer.SetRenderTarget(renderTargets[0]); + else + commandBuffer.SetRenderTarget(renderTargets, BuiltinRenderTextureType.None); + commandBuffer.DrawProcedural(Matrix4x4.identity, FragmentMaterial, FragmentPass, MeshTopology.Triangles, 3, 1, FragmentProperties); } - private static IEnumerable GetKeywords(Asr.InitializationFlags flags) - { - bool useLut = false; -#if UNITY_2022_1_OR_NEWER // This will also work in 2020.3.43+ and 2021.3.14+ - if (SystemInfo.computeSubGroupSize == 64) - { - useLut = true; - } -#endif - - // This matches the permutation rules from the CreatePipeline* functions - if ((flags & Asr.InitializationFlags.EnableHighDynamicRange) != 0) yield return "FFXM_FSR2_OPTION_HDR_COLOR_INPUT"; - if ((flags & Asr.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) yield return "FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS"; - if ((flags & Asr.InitializationFlags.EnableMotionVectorsJitterCancellation) != 0) yield return "FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS"; - if ((flags & Asr.InitializationFlags.EnableDepthInverted) != 0) yield return "FFXM_FSR2_OPTION_INVERTED_DEPTH"; - if (useLut) yield return "FFXM_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE"; - if ((flags & Asr.InitializationFlags.EnableFP16Usage) != 0) yield return "FFXM_HALF"; - } - [Conditional("ENABLE_PROFILER")] protected void BeginSample(CommandBuffer cmd) { @@ -259,35 +221,14 @@ namespace ArmASR internal class AsrAccumulatePass : AsrPass { - private const string SharpeningKeyword = "FFXM_FSR2_OPTION_APPLY_SHARPENING"; - -#if UNITY_2021_2_OR_NEWER - private readonly LocalKeyword _sharpeningKeyword; -#endif - public AsrAccumulatePass(Asr.ContextDescription contextDescription, AsrResources resources, ComputeBuffer constants) : base(contextDescription, resources, constants) { InitFragmentShader("Reproject & Accumulate", contextDescription.Shaders.fragmentShader, 3); -#if UNITY_2021_2_OR_NEWER - _sharpeningKeyword = new LocalKeyword(ComputeShader, SharpeningKeyword); // TODO: dynamically enable this on MaterialPropertyBlock -#endif } protected override void DoScheduleDispatch(CommandBuffer commandBuffer, Asr.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY) { -#if UNITY_2021_2_OR_NEWER - if (dispatchParams.EnableSharpening) - commandBuffer.EnableKeyword(ComputeShader, _sharpeningKeyword); - else - commandBuffer.DisableKeyword(ComputeShader, _sharpeningKeyword); -#else - if (dispatchParams.EnableSharpening) - commandBuffer.EnableShaderKeyword(SharpeningKeyword); - else - commandBuffer.DisableShaderKeyword(SharpeningKeyword); -#endif - if ((ContextDescription.Flags & Asr.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) { commandBuffer.SetGlobalTexture(AsrShaderIDs.SrvDilatedMotionVectors, Resources.DilatedMotionVectors[frameIndex]); diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_compute_luminance_pyramid_pass.compute b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_compute_luminance_pyramid_pass.compute index cab6cf5..5d4044b 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_compute_luminance_pyramid_pass.compute +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_compute_luminance_pyramid_pass.compute @@ -1,9 +1,9 @@ #pragma kernel main -#pragma multi_compile_local __ FFXM_HALF -#pragma multi_compile_local __ FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS -#pragma multi_compile_local __ FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS -#pragma multi_compile_local __ FFXM_FSR2_OPTION_INVERTED_DEPTH +#pragma multi_compile __ FFXM_HALF +#pragma multi_compile __ FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS +#pragma multi_compile __ FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS +#pragma multi_compile __ FFXM_FSR2_OPTION_INVERTED_DEPTH #pragma multi_compile __ UNITY_FFXM_TEXTURE2D_X_ARRAY diff --git a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_lock_pass.compute b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_lock_pass.compute index 9e3a2a4..660b922 100644 --- a/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_lock_pass.compute +++ b/Packages/com.unity.postprocessing@3.2.2/PostProcessing/Runtime/Effects/Upscaling/ASR/Shaders/ffxm_fsr2_lock_pass.compute @@ -1,9 +1,9 @@ #pragma kernel main -#pragma multi_compile_local __ FFXM_HALF -#pragma multi_compile_local __ FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS -#pragma multi_compile_local __ FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS -#pragma multi_compile_local __ FFXM_FSR2_OPTION_INVERTED_DEPTH +#pragma multi_compile __ FFXM_HALF +#pragma multi_compile __ FFXM_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS +#pragma multi_compile __ FFXM_FSR2_OPTION_JITTERED_MOTION_VECTORS +#pragma multi_compile __ FFXM_FSR2_OPTION_INVERTED_DEPTH #pragma multi_compile __ UNITY_FFXM_TEXTURE2D_X_ARRAY