From b565ca029fb974506ecba5fe13429601353eaf38 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sun, 4 Aug 2024 00:14:45 +0200 Subject: [PATCH] Made flags a field of the FfxPassWithFlags class, obviating the need to hold on to a context description copy, or the need to pass flags around. --- Runtime/Common/FfxPassBase.cs | 17 +++++++++++------ Runtime/FSR2/Fsr2Pass.cs | 25 +++++++++---------------- Runtime/FSR3/Fsr3UpscalerPass.cs | 27 ++++++++++----------------- 3 files changed, 30 insertions(+), 39 deletions(-) diff --git a/Runtime/Common/FfxPassBase.cs b/Runtime/Common/FfxPassBase.cs index 3e9e4aa..af5a980 100644 --- a/Runtime/Common/FfxPassBase.cs +++ b/Runtime/Common/FfxPassBase.cs @@ -29,7 +29,7 @@ namespace FidelityFX protected abstract void DoScheduleDispatch(CommandBuffer commandBuffer, in TDispatch dispatchParams, int bufferIndex, int dispatchX, int dispatchY, int dispatchZ); - protected void InitComputeShader(string passName, ComputeShader shader, string kernelName = "CS") + protected virtual void InitComputeShader(string passName, ComputeShader shader, string kernelName = "CS") { if (shader == null) { @@ -50,14 +50,19 @@ namespace FidelityFX //where TDispatch: struct where TFlags: Enum { - protected FfxPassWithFlags(string techName): base(techName) { } + protected readonly TFlags Flags; - protected void InitComputeShader(string passName, ComputeShader shader, TFlags flags, string kernelName = "CS") + protected FfxPassWithFlags(string techName, TFlags flags) : base(techName) { - InitComputeShader(passName, shader, kernelName); - SetupShaderKeywords(flags); + Flags = flags; } - protected abstract void SetupShaderKeywords(TFlags flags); + protected sealed override void InitComputeShader(string passName, ComputeShader shader, string kernelName = "CS") + { + base.InitComputeShader(passName, shader, kernelName); + SetupShaderKeywords(); + } + + protected abstract void SetupShaderKeywords(); } } diff --git a/Runtime/FSR2/Fsr2Pass.cs b/Runtime/FSR2/Fsr2Pass.cs index 7384454..7db9e42 100644 --- a/Runtime/FSR2/Fsr2Pass.cs +++ b/Runtime/FSR2/Fsr2Pass.cs @@ -32,24 +32,17 @@ namespace FidelityFX.FSR2 { internal const int ShadingChangeMipLevel = 4; // This matches the FFX_FSR2_SHADING_CHANGE_MIP_LEVEL define - protected readonly Fsr2.ContextDescription ContextDescription; protected readonly Fsr2Resources Resources; protected readonly ComputeBuffer Constants; - protected Fsr2Pass(Fsr2.ContextDescription contextDescription, Fsr2Resources resources, ComputeBuffer constants) - : base("FSR2") + protected Fsr2Pass(in Fsr2.ContextDescription contextDescription, Fsr2Resources resources, ComputeBuffer constants) + : base("FSR2", contextDescription.Flags) { - ContextDescription = contextDescription; Resources = resources; Constants = constants; } - - protected void InitComputeShader(string passName, ComputeShader shader) - { - InitComputeShader(passName, shader, ContextDescription.Flags); - } - protected override void SetupShaderKeywords(Fsr2.InitializationFlags flags) + protected override void SetupShaderKeywords() { bool useLut = false; #if UNITY_2022_1_OR_NEWER // This will also work in 2020.3.43+ and 2021.3.14+ @@ -60,12 +53,12 @@ namespace FidelityFX.FSR2 #endif // This matches the permutation rules from the CreatePipeline* functions - if ((flags & Fsr2.InitializationFlags.EnableHighDynamicRange) != 0) ComputeShader.EnableKeyword("FFX_FSR2_OPTION_HDR_COLOR_INPUT"); - if ((flags & Fsr2.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) ComputeShader.EnableKeyword("FFX_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS"); - if ((flags & Fsr2.InitializationFlags.EnableMotionVectorsJitterCancellation) != 0) ComputeShader.EnableKeyword("FFX_FSR2_OPTION_JITTERED_MOTION_VECTORS"); - if ((flags & Fsr2.InitializationFlags.EnableDepthInverted) != 0) ComputeShader.EnableKeyword("FFX_FSR2_OPTION_INVERTED_DEPTH"); + if ((Flags & Fsr2.InitializationFlags.EnableHighDynamicRange) != 0) ComputeShader.EnableKeyword("FFX_FSR2_OPTION_HDR_COLOR_INPUT"); + if ((Flags & Fsr2.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) ComputeShader.EnableKeyword("FFX_FSR2_OPTION_LOW_RESOLUTION_MOTION_VECTORS"); + if ((Flags & Fsr2.InitializationFlags.EnableMotionVectorsJitterCancellation) != 0) ComputeShader.EnableKeyword("FFX_FSR2_OPTION_JITTERED_MOTION_VECTORS"); + if ((Flags & Fsr2.InitializationFlags.EnableDepthInverted) != 0) ComputeShader.EnableKeyword("FFX_FSR2_OPTION_INVERTED_DEPTH"); if (useLut) ComputeShader.EnableKeyword("FFX_FSR2_OPTION_REPROJECT_USE_LANCZOS_TYPE"); - if ((flags & Fsr2.InitializationFlags.EnableFP16Usage) != 0) ComputeShader.EnableKeyword("FFX_HALF"); + if ((Flags & Fsr2.InitializationFlags.EnableFP16Usage) != 0) ComputeShader.EnableKeyword("FFX_HALF"); } } @@ -196,7 +189,7 @@ namespace FidelityFX.FSR2 commandBuffer.DisableShaderKeyword(SharpeningKeyword); #endif - if ((ContextDescription.Flags & Fsr2.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) + if ((Flags & Fsr2.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) { commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvDilatedMotionVectors, Resources.DilatedMotionVectors[bufferIndex]); } diff --git a/Runtime/FSR3/Fsr3UpscalerPass.cs b/Runtime/FSR3/Fsr3UpscalerPass.cs index 941971c..44ae5c0 100644 --- a/Runtime/FSR3/Fsr3UpscalerPass.cs +++ b/Runtime/FSR3/Fsr3UpscalerPass.cs @@ -30,24 +30,17 @@ namespace FidelityFX.FSR3 /// internal abstract class Fsr3UpscalerPass: FfxPassWithFlags { - protected readonly Fsr3Upscaler.ContextDescription ContextDescription; protected readonly Fsr3UpscalerResources Resources; protected readonly ComputeBuffer Constants; - protected Fsr3UpscalerPass(Fsr3Upscaler.ContextDescription contextDescription, Fsr3UpscalerResources resources, ComputeBuffer constants) - : base("FSR3 Upscaler") + protected Fsr3UpscalerPass(in Fsr3Upscaler.ContextDescription contextDescription, Fsr3UpscalerResources resources, ComputeBuffer constants) + : base("FSR3 Upscaler", contextDescription.Flags) { - ContextDescription = contextDescription; Resources = resources; Constants = constants; } - - protected void InitComputeShader(string passName, ComputeShader shader) - { - InitComputeShader(passName, shader, ContextDescription.Flags); - } - - protected override void SetupShaderKeywords(Fsr3Upscaler.InitializationFlags flags) + + protected override void SetupShaderKeywords() { bool useLut = false; #if UNITY_2022_1_OR_NEWER // This will also work in 2020.3.43+ and 2021.3.14+ @@ -58,12 +51,12 @@ namespace FidelityFX.FSR3 #endif // This matches the permutation rules from the CreatePipeline* functions - if ((flags & Fsr3Upscaler.InitializationFlags.EnableHighDynamicRange) != 0) ComputeShader.EnableKeyword("FFX_FSR3UPSCALER_OPTION_HDR_COLOR_INPUT"); - if ((flags & Fsr3Upscaler.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) ComputeShader.EnableKeyword("FFX_FSR3UPSCALER_OPTION_LOW_RESOLUTION_MOTION_VECTORS"); - if ((flags & Fsr3Upscaler.InitializationFlags.EnableMotionVectorsJitterCancellation) != 0) ComputeShader.EnableKeyword("FFX_FSR3UPSCALER_OPTION_JITTERED_MOTION_VECTORS"); - if ((flags & Fsr3Upscaler.InitializationFlags.EnableDepthInverted) != 0) ComputeShader.EnableKeyword("FFX_FSR3UPSCALER_OPTION_INVERTED_DEPTH"); + if ((Flags & Fsr3Upscaler.InitializationFlags.EnableHighDynamicRange) != 0) ComputeShader.EnableKeyword("FFX_FSR3UPSCALER_OPTION_HDR_COLOR_INPUT"); + if ((Flags & Fsr3Upscaler.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) ComputeShader.EnableKeyword("FFX_FSR3UPSCALER_OPTION_LOW_RESOLUTION_MOTION_VECTORS"); + if ((Flags & Fsr3Upscaler.InitializationFlags.EnableMotionVectorsJitterCancellation) != 0) ComputeShader.EnableKeyword("FFX_FSR3UPSCALER_OPTION_JITTERED_MOTION_VECTORS"); + if ((Flags & Fsr3Upscaler.InitializationFlags.EnableDepthInverted) != 0) ComputeShader.EnableKeyword("FFX_FSR3UPSCALER_OPTION_INVERTED_DEPTH"); if (useLut) ComputeShader.EnableKeyword("FFX_FSR3UPSCALER_OPTION_REPROJECT_USE_LANCZOS_TYPE"); - if ((flags & Fsr3Upscaler.InitializationFlags.EnableFP16Usage) != 0) ComputeShader.EnableKeyword("FFX_HALF"); + if ((Flags & Fsr3Upscaler.InitializationFlags.EnableFP16Usage) != 0) ComputeShader.EnableKeyword("FFX_HALF"); } } @@ -267,7 +260,7 @@ namespace FidelityFX.FSR3 commandBuffer.SetComputeResourceParam(ComputeShader, KernelIndex, Fsr3ShaderIDs.SrvInputExposure, dispatchParams.Exposure); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr3ShaderIDs.SrvDilatedReactiveMasks, Fsr3ShaderIDs.UavDilatedReactiveMasks); - if ((ContextDescription.Flags & Fsr3Upscaler.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) + if ((Flags & Fsr3Upscaler.InitializationFlags.EnableDisplayResolutionMotionVectors) == 0) { commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr3ShaderIDs.SrvDilatedMotionVectors, Resources.DilatedVelocity); }