diff --git a/Assets/Scripts/Core/Fsr2.cs b/Assets/Scripts/Core/Fsr2.cs index 59cb113..89a8c42 100644 --- a/Assets/Scripts/Core/Fsr2.cs +++ b/Assets/Scripts/Core/Fsr2.cs @@ -159,32 +159,35 @@ namespace FidelityFX /// /// A structure wrapping all of the necessary information to bind a specific buffer or attachment of a render target to a compute shader. /// - public struct RenderTargetView + public readonly struct RenderTargetView { + public static readonly RenderTargetView Empty = new RenderTargetView(default(RenderTargetIdentifier)); + public RenderTargetView(in RenderTargetIdentifier renderTarget, RenderTextureSubElement subElement = RenderTextureSubElement.Default, int mipLevel = 0) { RenderTarget = renderTarget; SubElement = subElement; MipLevel = mipLevel; } - + + public bool IsEmpty => RenderTarget.Equals(default(RenderTargetIdentifier)); + public readonly RenderTargetIdentifier RenderTarget; public readonly RenderTextureSubElement SubElement; public readonly int MipLevel; } /// - /// The input and output resources are all optional. If they are null, the Fsr2Context won't try to bind them to any shaders. - /// This allows for customized and more efficient resource management outside of Fsr2Context, tailored to the specific scenario. + /// A structure encapsulating the parameters for dispatching the various passes of FidelityFX Super Resolution 2. /// public class DispatchDescription { public RenderTargetView Color; public RenderTargetView Depth; public RenderTargetView MotionVectors; - public RenderTargetView? Exposure; - public RenderTargetView? Reactive; - public RenderTargetView? TransparencyAndComposition; + public RenderTargetView Exposure; // optional + public RenderTargetView Reactive; // optional + public RenderTargetView TransparencyAndComposition; // optional public RenderTargetView Output; public Vector2 JitterOffset; public Vector2 MotionVectorScale; @@ -202,7 +205,7 @@ namespace FidelityFX // EXPERIMENTAL reactive mask generation parameters public bool EnableAutoReactive; - public RenderTargetView? ColorOpaqueOnly; + public RenderTargetView ColorOpaqueOnly; public float AutoTcThreshold = 0.05f; public float AutoTcScale = 1.0f; public float AutoReactiveScale = 5.0f; diff --git a/Assets/Scripts/Core/Fsr2Context.cs b/Assets/Scripts/Core/Fsr2Context.cs index 610f469..089f6bf 100644 --- a/Assets/Scripts/Core/Fsr2Context.cs +++ b/Assets/Scripts/Core/Fsr2Context.cs @@ -159,7 +159,7 @@ namespace FidelityFX // If auto exposure is enabled use the auto exposure SRV, otherwise what the app sends if ((_contextDescription.Flags & Fsr2.InitializationFlags.EnableAutoExposure) != 0) dispatchParams.Exposure = new Fsr2.RenderTargetView(_resources.AutoExposure); - else if (dispatchParams.Exposure == null) + else if (dispatchParams.Exposure.IsEmpty) dispatchParams.Exposure = new Fsr2.RenderTargetView(_resources.DefaultExposure); if (dispatchParams.EnableAutoReactive) @@ -167,9 +167,12 @@ namespace FidelityFX // Create the auto-TCR resources only when we need them if (_resources.AutoReactive == null) _resources.CreateTcrAutogenResources(_contextDescription); - + if (resetAccumulation) - commandBuffer.Blit(_resources.PrevPreAlpha[frameIndex ^ 1], (dispatchParams.ColorOpaqueOnly ?? new Fsr2.RenderTargetView(Fsr2ShaderIDs.SrvOpaqueOnly)).RenderTarget); + { + RenderTargetIdentifier opaqueOnly = dispatchParams.ColorOpaqueOnly.IsEmpty ? Fsr2ShaderIDs.SrvOpaqueOnly : dispatchParams.ColorOpaqueOnly.RenderTarget; + commandBuffer.Blit(_resources.PrevPreAlpha[frameIndex ^ 1], opaqueOnly); + } } else if (_resources.AutoReactive != null) { @@ -177,8 +180,8 @@ namespace FidelityFX _resources.DestroyTcrAutogenResources(); } - if (dispatchParams.Reactive == null) dispatchParams.Reactive = new Fsr2.RenderTargetView(_resources.DefaultReactive); - if (dispatchParams.TransparencyAndComposition == null) dispatchParams.TransparencyAndComposition = new Fsr2.RenderTargetView(_resources.DefaultReactive); + if (dispatchParams.Reactive.IsEmpty) dispatchParams.Reactive = new Fsr2.RenderTargetView(_resources.DefaultReactive); + if (dispatchParams.TransparencyAndComposition.IsEmpty) dispatchParams.TransparencyAndComposition = new Fsr2.RenderTargetView(_resources.DefaultReactive); Fsr2Resources.CreateAliasableResources(commandBuffer, _contextDescription, dispatchParams); SetupConstants(dispatchParams, resetAccumulation); @@ -433,7 +436,27 @@ namespace FidelityFX private void DebugCheckDispatch(Fsr2.DispatchDescription dispatchParams) { - if (dispatchParams.Exposure.HasValue && (_contextDescription.Flags & Fsr2.InitializationFlags.EnableAutoExposure) != 0) + if (dispatchParams.Color.IsEmpty) + { + Debug.LogError("Color resource is null"); + } + + if (dispatchParams.Depth.IsEmpty) + { + Debug.LogError("Depth resource is null"); + } + + if (dispatchParams.MotionVectors.IsEmpty) + { + Debug.LogError("MotionVectors resource is null"); + } + + if (dispatchParams.Output.IsEmpty) + { + Debug.LogError("Output resource is null"); + } + + if (!dispatchParams.Exposure.IsEmpty && (_contextDescription.Flags & Fsr2.InitializationFlags.EnableAutoExposure) != 0) { Debug.LogWarning("Exposure resource provided, however auto exposure flag is present"); } diff --git a/Assets/Scripts/Core/Fsr2Pipeline.cs b/Assets/Scripts/Core/Fsr2Pipeline.cs index 4b00278..c0aafd8 100644 --- a/Assets/Scripts/Core/Fsr2Pipeline.cs +++ b/Assets/Scripts/Core/Fsr2Pipeline.cs @@ -157,16 +157,12 @@ namespace FidelityFX ref var color = ref dispatchParams.Color; ref var depth = ref dispatchParams.Depth; ref var motionVectors = ref dispatchParams.MotionVectors; + ref var exposure = ref dispatchParams.Exposure; commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputColor, color.RenderTarget, color.MipLevel, color.SubElement); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputDepth, depth.RenderTarget, depth.MipLevel, depth.SubElement); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputMotionVectors, motionVectors.RenderTarget, motionVectors.MipLevel, motionVectors.SubElement); - - if (dispatchParams.Exposure.HasValue) - { - var exposure = dispatchParams.Exposure.Value; - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement); - } + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.UavDilatedMotionVectors, Resources.DilatedMotionVectors[frameIndex]); @@ -189,28 +185,16 @@ namespace FidelityFX ref var color = ref dispatchParams.Color; ref var depth = ref dispatchParams.Depth; ref var motionVectors = ref dispatchParams.MotionVectors; + ref var exposure = ref dispatchParams.Exposure; + ref var reactive = ref dispatchParams.Reactive; + ref var tac = ref dispatchParams.TransparencyAndComposition; commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputColor, color.RenderTarget, color.MipLevel, color.SubElement); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputDepth, depth.RenderTarget, depth.MipLevel, depth.SubElement); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputMotionVectors, motionVectors.RenderTarget, motionVectors.MipLevel, motionVectors.SubElement); - - if (dispatchParams.Exposure.HasValue) - { - var exposure = dispatchParams.Exposure.Value; - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement); - } - - if (dispatchParams.Reactive.HasValue) - { - var reactive = dispatchParams.Reactive.Value; - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvReactiveMask, reactive.RenderTarget, reactive.MipLevel, reactive.SubElement); - } - - if (dispatchParams.TransparencyAndComposition.HasValue) - { - var tac = dispatchParams.TransparencyAndComposition.Value; - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvTransparencyAndCompositionMask, tac.RenderTarget, tac.MipLevel, tac.SubElement); - } + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvReactiveMask, reactive.RenderTarget, reactive.MipLevel, reactive.SubElement); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvTransparencyAndCompositionMask, tac.RenderTarget, tac.MipLevel, tac.SubElement); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvReconstructedPrevNearestDepth, Fsr2ShaderIDs.UavReconstructedPrevNearestDepth); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvDilatedMotionVectors, Resources.DilatedMotionVectors[frameIndex]); @@ -284,11 +268,8 @@ namespace FidelityFX commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputMotionVectors, motionVectors.RenderTarget, motionVectors.MipLevel, motionVectors.SubElement); } - if (dispatchParams.Exposure.HasValue) - { - var exposure = dispatchParams.Exposure.Value; - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement); - } + ref var exposure = ref dispatchParams.Exposure; + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvDilatedReactiveMasks, Fsr2ShaderIDs.UavDilatedReactiveMasks); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInternalUpscaled, Resources.InternalUpscaled[frameIndex ^ 1]); @@ -327,12 +308,8 @@ namespace FidelityFX public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY) { - if (dispatchParams.Exposure.HasValue) - { - var exposure = dispatchParams.Exposure.Value; - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement); - } - + ref var exposure = ref dispatchParams.Exposure; + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputExposure, exposure.RenderTarget, exposure.MipLevel, exposure.SubElement); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvRcasInput, Resources.InternalUpscaled[frameIndex]); ref var output = ref dispatchParams.Output; @@ -391,31 +368,19 @@ namespace FidelityFX public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY) { - if (dispatchParams.ColorOpaqueOnly.HasValue) - { - var opaqueOnly = dispatchParams.ColorOpaqueOnly.Value; - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvOpaqueOnly, opaqueOnly.RenderTarget, opaqueOnly.MipLevel, opaqueOnly.SubElement); - } - ref var color = ref dispatchParams.Color; ref var motionVectors = ref dispatchParams.MotionVectors; + ref var opaqueOnly = ref dispatchParams.ColorOpaqueOnly; + ref var reactive = ref dispatchParams.Reactive; + ref var tac = ref dispatchParams.TransparencyAndComposition; + + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvOpaqueOnly, opaqueOnly.RenderTarget, opaqueOnly.MipLevel, opaqueOnly.SubElement); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputColor, color.RenderTarget, color.MipLevel, color.SubElement); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvInputMotionVectors, motionVectors.RenderTarget, motionVectors.MipLevel, motionVectors.SubElement); - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvPrevColorPreAlpha, Resources.PrevPreAlpha[frameIndex ^ 1]); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvPrevColorPostAlpha, Resources.PrevPostAlpha[frameIndex ^ 1]); - - if (dispatchParams.Reactive.HasValue) - { - var reactive = dispatchParams.Reactive.Value; - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvReactiveMask, reactive.RenderTarget, reactive.MipLevel, reactive.SubElement); - } - - if (dispatchParams.TransparencyAndComposition.HasValue) - { - var tac = dispatchParams.TransparencyAndComposition.Value; - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvTransparencyAndCompositionMask, tac.RenderTarget, tac.MipLevel, tac.SubElement); - } + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvReactiveMask, reactive.RenderTarget, reactive.MipLevel, reactive.SubElement); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.SrvTransparencyAndCompositionMask, tac.RenderTarget, tac.MipLevel, tac.SubElement); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.UavAutoReactive, Resources.AutoReactive); commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, Fsr2ShaderIDs.UavAutoComposition, Resources.AutoComposition); diff --git a/Assets/Scripts/Fsr2ImageEffect.cs b/Assets/Scripts/Fsr2ImageEffect.cs index 5da8016..f29e0dc 100644 --- a/Assets/Scripts/Fsr2ImageEffect.cs +++ b/Assets/Scripts/Fsr2ImageEffect.cs @@ -328,9 +328,9 @@ namespace FidelityFX _dispatchDescription.Color = new Fsr2.RenderTargetView(BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Color); _dispatchDescription.Depth = new Fsr2.RenderTargetView(BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth); _dispatchDescription.MotionVectors = new Fsr2.RenderTargetView(BuiltinRenderTextureType.MotionVectors); - _dispatchDescription.Exposure = null; - _dispatchDescription.Reactive = null; - _dispatchDescription.TransparencyAndComposition = null; + _dispatchDescription.Exposure = Fsr2.RenderTargetView.Empty; + _dispatchDescription.Reactive = Fsr2.RenderTargetView.Empty; + _dispatchDescription.TransparencyAndComposition = Fsr2.RenderTargetView.Empty; if (!enableAutoExposure && exposure != null) _dispatchDescription.Exposure = new Fsr2.RenderTargetView(exposure); if (reactiveMask != null) _dispatchDescription.Reactive = new Fsr2.RenderTargetView(reactiveMask);