diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 3d32200..75963cf 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -320,6 +320,7 @@ MonoBehaviour: performSharpenPass: 1 sharpness: 0.8 enableFP16: 0 + generateReactiveMask: 0 --- !u!114 &963194230 MonoBehaviour: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Fsr2.cs b/Assets/Scripts/Fsr2.cs index 745f6e2..25cfafb 100644 --- a/Assets/Scripts/Fsr2.cs +++ b/Assets/Scripts/Fsr2.cs @@ -139,6 +139,10 @@ namespace FidelityFX public Fsr2Callbacks Callbacks; } + /// + /// 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. + /// public class DispatchDescription { public RenderTargetIdentifier? Color; @@ -162,16 +166,28 @@ namespace FidelityFX public float ViewSpaceToMetersFactor; } + /// + /// The default values for Scale, CutoffThreshold, BinaryValue and Flags were taken from the FSR2 demo project. + /// public class GenerateReactiveDescription { public RenderTargetIdentifier? ColorOpaqueOnly; public RenderTargetIdentifier? ColorPreUpscale; public RenderTargetIdentifier? OutReactive; public Vector2Int RenderSize; - public float Scale; - public float CutoffThreshold; - public float BinaryValue; - public uint Flags; + public float Scale = 1.0f; + public float CutoffThreshold = 0.2f; + public float BinaryValue = 0.9f; + public GenerateReactiveFlags Flags = GenerateReactiveFlags.ApplyTonemap | GenerateReactiveFlags.ApplyThreshold | GenerateReactiveFlags.UseComponentsMax; + } + + [Flags] + public enum GenerateReactiveFlags + { + ApplyTonemap = 1 << 0, + ApplyInverseTonemap = 1 << 1, + ApplyThreshold = 1 << 2, + UseComponentsMax = 1 << 3, } [Serializable, StructLayout(LayoutKind.Sequential)] diff --git a/Assets/Scripts/Fsr2Context.cs b/Assets/Scripts/Fsr2Context.cs index 63bbb3a..d0126e0 100644 --- a/Assets/Scripts/Fsr2Context.cs +++ b/Assets/Scripts/Fsr2Context.cs @@ -217,10 +217,10 @@ namespace FidelityFX GenReactiveConsts.scale = dispatchParams.Scale; GenReactiveConsts.threshold = dispatchParams.CutoffThreshold; GenReactiveConsts.binaryValue = dispatchParams.BinaryValue; - GenReactiveConsts.flags = dispatchParams.Flags; + GenReactiveConsts.flags = (uint)dispatchParams.Flags; _generateReactiveConstantsBuffer.SetData(_generateReactiveConstantsArray); - ((Fsr2GenerateReactivePipeline)_generateReactivePipeline).ScheduleDispatch(_commandBuffer, dispatchParams, dispatchSrcX, dispatchSrcY); + ((Fsr2GenerateReactivePipeline)_generateReactivePipeline).ScheduleDispatch(commandBuffer, dispatchParams, dispatchSrcX, dispatchSrcY); } private void SetupConstants(Fsr2.DispatchDescription dispatchParams, bool resetAccumulation) diff --git a/Assets/Scripts/Fsr2Controller.cs b/Assets/Scripts/Fsr2Controller.cs index 55c1b67..7ae8e99 100644 --- a/Assets/Scripts/Fsr2Controller.cs +++ b/Assets/Scripts/Fsr2Controller.cs @@ -21,6 +21,8 @@ namespace FidelityFX [SerializeField] private bool enableFP16 = false; + [SerializeField] private bool generateReactiveMask = false; + private Fsr2Context _context; private Vector2Int _renderSize; private Vector2Int _displaySize; @@ -135,12 +137,6 @@ namespace FidelityFX _renderCamera.aspect = (Screen.width * _originalRect.width) / (Screen.height * _originalRect.height); _renderCamera.rect = new Rect(0, 0, _originalRect.width * _renderSize.x / _displaySize.x, _originalRect.height * _renderSize.y / _displaySize.y); - _genReactiveDescription.RenderSize = _renderSize; - _genReactiveDescription.Scale = 1.0f; - _genReactiveDescription.CutoffThreshold = 0.2f; - _genReactiveDescription.BinaryValue = 0.9f; - _genReactiveDescription.Flags = 0; // ApplyTonemap, ApplyInverseTonemap, ApplyThreshold, UseComponentsMax - _dispatchDescription.Color = null; _dispatchDescription.Depth = null; _dispatchDescription.MotionVectors = null; @@ -181,10 +177,16 @@ namespace FidelityFX _renderCamera.ResetProjectionMatrix(); _dispatchCommandBuffer.Clear(); - - // UpscaleReactive: width, height, VK_FORMAT_R8_UNORM - //Context.GenerateReactiveMask(); - + + if (generateReactiveMask) + { + _genReactiveDescription.RenderSize = _renderSize; + _dispatchCommandBuffer.GetTemporaryRT(Fsr2Pipeline.UavAutoReactive, _renderSize.x, _renderSize.y, 0, default, GraphicsFormat.R8_UNorm, 1, true); + _context.GenerateReactiveMask(_genReactiveDescription, _dispatchCommandBuffer); + + _dispatchDescription.Reactive = Fsr2Pipeline.UavAutoReactive; + } + _dispatchDescription.InputResourceSize = new Vector2Int(src.width, src.height); if (dest != null) @@ -210,6 +212,11 @@ namespace FidelityFX _dispatchCommandBuffer.ReleaseTemporaryRT(Fsr2Pipeline.UavUpscaledOutput); } + if (generateReactiveMask) + { + _dispatchCommandBuffer.ReleaseTemporaryRT(Fsr2Pipeline.UavAutoReactive); + } + Graphics.ExecuteCommandBuffer(_dispatchCommandBuffer); // Shut up the Unity warning about not writing to the destination texture diff --git a/Assets/Scripts/Fsr2Pipeline.cs b/Assets/Scripts/Fsr2Pipeline.cs index 147b5a9..41c2e20 100644 --- a/Assets/Scripts/Fsr2Pipeline.cs +++ b/Assets/Scripts/Fsr2Pipeline.cs @@ -411,7 +411,7 @@ namespace FidelityFX commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, SrvInputColor, dispatchParams.ColorPreUpscale.Value, 0, RenderTextureSubElement.Color); if (dispatchParams.OutReactive.HasValue) - commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, UavAutoExposure, dispatchParams.OutReactive.Value); + commandBuffer.SetComputeTextureParam(ComputeShader, KernelIndex, UavAutoReactive, dispatchParams.OutReactive.Value); commandBuffer.SetComputeConstantBufferParam(ComputeShader, CbGenReactive, _generateReactiveConstants, 0, Marshal.SizeOf());