diff --git a/Assets/Scripts/Fsr2.cs b/Assets/Scripts/Fsr2.cs index 6216d50..f601b23 100644 --- a/Assets/Scripts/Fsr2.cs +++ b/Assets/Scripts/Fsr2.cs @@ -157,6 +157,18 @@ namespace FidelityFX public float CameraFovAngleVertical; public float ViewSpaceToMetersFactor; } + + public class GenerateReactiveDescription + { + public Texture ColorOpaqueOnly; + public Texture ColorPreUpscale; + public RenderTexture OutReactive; + public Vector2Int RenderSize; + public float Scale; + public float CutoffThreshold; + public float BinaryValue; + public uint Flags; + } [Serializable, StructLayout(LayoutKind.Sequential)] internal struct Fsr2Constants diff --git a/Assets/Scripts/Fsr2Context.cs b/Assets/Scripts/Fsr2Context.cs index 5b79bfe..d01236a 100644 --- a/Assets/Scripts/Fsr2Context.cs +++ b/Assets/Scripts/Fsr2Context.cs @@ -63,11 +63,6 @@ namespace FidelityFX CreatePipelines(); } - // private void InitShaders() - // { - // LoadComputeShader("FSR2/ffx_fsr2_autogen_reactive_pass", ref _generateReactiveShader, out _generateReactiveKernel); - // } - private void CreatePipelines() { _computeLuminancePyramidPipeline = new Fsr2ComputeLuminancePyramidPipeline(_contextDescription, _resources, _fsr2ConstantsBuffer, _spdConstantsBuffer); @@ -77,6 +72,7 @@ namespace FidelityFX _accumulatePipeline = new Fsr2AccumulatePipeline(_contextDescription, _resources, _fsr2ConstantsBuffer); _accumulateSharpenPipeline = new Fsr2AccumulateSharpenPipeline(_contextDescription, _resources, _fsr2ConstantsBuffer); _rcasPipeline = new Fsr2RcasPipeline(_contextDescription, _resources, _fsr2ConstantsBuffer, _rcasConstantsBuffer); + _generateReactivePipeline = new Fsr2GenerateReactivePipeline(_contextDescription, _resources, _generateReactiveConstantsBuffer); } public void Destroy() @@ -194,6 +190,25 @@ namespace FidelityFX Fsr2Pipeline.UnregisterResources(_commandBuffer); } + public void GenerateReactiveMask(Fsr2.GenerateReactiveDescription dispatchParams) + { + _commandBuffer.Clear(); + + const int threadGroupWorkRegionDim = 8; + int dispatchSrcX = (dispatchParams.RenderSize.x + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim; + int dispatchSrcY = (dispatchParams.RenderSize.y + (threadGroupWorkRegionDim - 1)) / threadGroupWorkRegionDim; + + GenReactiveConsts.scale = dispatchParams.Scale; + GenReactiveConsts.threshold = dispatchParams.CutoffThreshold; + GenReactiveConsts.binaryValue = dispatchParams.BinaryValue; + GenReactiveConsts.flags = dispatchParams.Flags; + _generateReactiveConstantsBuffer.SetData(_generateReactiveConstantsArray); + + ((Fsr2GenerateReactivePipeline)_generateReactivePipeline).ScheduleDispatch(_commandBuffer, dispatchParams, dispatchSrcX, dispatchSrcY); + + Graphics.ExecuteCommandBuffer(_commandBuffer); + } + private void SetupConstants(Fsr2.DispatchDescription dispatchParams, bool resetAccumulation) { ref Fsr2.Fsr2Constants constants = ref Constants; diff --git a/Assets/Scripts/Fsr2Pipeline.cs b/Assets/Scripts/Fsr2Pipeline.cs index 952373e..6559e37 100644 --- a/Assets/Scripts/Fsr2Pipeline.cs +++ b/Assets/Scripts/Fsr2Pipeline.cs @@ -52,6 +52,7 @@ namespace FidelityFX protected static readonly int UavInternalUpscaled = Shader.PropertyToID("rw_internal_upscaled_color"); protected static readonly int UavLockStatus = Shader.PropertyToID("rw_lock_status"); protected static readonly int UavLumaHistory = Shader.PropertyToID("rw_luma_history"); + protected static readonly int UavAutoReactive = Shader.PropertyToID("rw_output_autoreactive"); // Constant buffer bindings protected static readonly int CbFsr2 = Shader.PropertyToID("cbFSR2"); @@ -338,4 +339,32 @@ namespace FidelityFX commandBuffer.DispatchCompute(ComputeShader, KernelIndex, dispatchX, dispatchY, 1); } } + + internal class Fsr2GenerateReactivePipeline : Fsr2Pipeline + { + private readonly ComputeBuffer _generateReactiveConstants; + + public Fsr2GenerateReactivePipeline(Fsr2.ContextDescription contextDescription, Fsr2Resources resources, ComputeBuffer generateReactiveConstants) + : base(contextDescription, resources, null) + { + _generateReactiveConstants = generateReactiveConstants; + + LoadComputeShader("FSR2/ffx_fsr2_autogen_reactive_pass"); + } + + public override void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.DispatchDescription dispatchParams, int frameIndex, int dispatchX, int dispatchY) + { + } + + public void ScheduleDispatch(CommandBuffer commandBuffer, Fsr2.GenerateReactiveDescription dispatchParams, int dispatchX, int dispatchY) + { + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, SrvOpaqueOnly, dispatchParams.ColorOpaqueOnly); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, SrvInputColor, dispatchParams.ColorPreUpscale); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, UavAutoExposure, dispatchParams.OutReactive); + + commandBuffer.SetComputeConstantBufferParam(ComputeShader, CbGenReactive, _generateReactiveConstants, 0, Marshal.SizeOf()); + + commandBuffer.DispatchCompute(ComputeShader, KernelIndex, dispatchX, dispatchY, 1); + } + } }