diff --git a/Assets/Scripts/Fsr2.cs b/Assets/Scripts/Fsr2.cs index 2232dd6..399391f 100644 --- a/Assets/Scripts/Fsr2.cs +++ b/Assets/Scripts/Fsr2.cs @@ -6,7 +6,7 @@ namespace FidelityFX public static class Fsr2 { // Allow overriding of certain Unity resource management behaviors by the programmer - public static Fsr2Callbacks Callbacks { get; set; } = new Fsr2Callbacks(); + public static Fsr2Callbacks GlobalCallbacks { get; set; } = new Fsr2Callbacks(); public enum QualityMode { @@ -17,7 +17,7 @@ namespace FidelityFX } [Flags] - public enum InitializationFlagBits + public enum InitializationFlags { EnableHighDynamicRange = 1 << 0, EnableDisplayResolutionMotionVectors = 1 << 1, @@ -29,10 +29,51 @@ namespace FidelityFX EnableTexture1DUsage = 1 << 7, } - public static Fsr2Context CreateContext() + public class ContextDescription { + public InitializationFlags Flags; + public Vector2Int MaxRenderSize; + public Vector2Int DisplaySize; + public Fsr2Callbacks Callbacks; + } + + public class DispatchDescription + { + // Texture2D Color, Depth, MotionVectors; // Will be passed using ComputeShader.SetTextureFromGlobal + public Texture2D Exposure; + public Texture2D Reactive; + public Texture2D TransparencyAndComposition; + public RenderTexture Input; + public RenderTexture Output; + public Vector2 JitterOffset; + public Vector2 MotionVectorScale; + public Vector2Int RenderSize; + public bool EnableSharpening; + public float Sharpness; + public float FrameTimeDelta; + public float PreExposure; + public bool Reset; + public float CameraNear; + public float CameraFar; + public float CameraFovAngleVertical; + } + + /// + /// Creates a new FSR2 context with standard parameters that are appropriate for the current platform. + /// + public static Fsr2Context CreateContext(InitializationFlags flags = 0) + { + // flags |= InitializationFlags.EnableDepthInverted; // Depends on the runtime platform + + var contextDescription = new ContextDescription + { + Flags = flags, + DisplaySize = new Vector2Int(Screen.width, Screen.height), + Callbacks = GlobalCallbacks, + }; + var context = new Fsr2Context(); - context.Create(Callbacks); + context.Create(contextDescription); return context; } diff --git a/Assets/Scripts/Fsr2Callbacks.cs b/Assets/Scripts/Fsr2Callbacks.cs index b4a6272..0238bee 100644 --- a/Assets/Scripts/Fsr2Callbacks.cs +++ b/Assets/Scripts/Fsr2Callbacks.cs @@ -6,11 +6,7 @@ namespace FidelityFX { public virtual Shader LoadShader(string name) { -#if UNITY_EDITOR - return Shader.Find(name); -#else return Resources.Load(name); -#endif } public virtual ComputeShader LoadComputeShader(string name) diff --git a/Assets/Scripts/Fsr2Context.cs b/Assets/Scripts/Fsr2Context.cs index dd904f3..7746636 100644 --- a/Assets/Scripts/Fsr2Context.cs +++ b/Assets/Scripts/Fsr2Context.cs @@ -7,6 +7,8 @@ namespace FidelityFX { public class Fsr2Context { + private Fsr2.ContextDescription _contextDescription; + private ComputeShader _rcasComputeShader; private ComputeBuffer _fsr2ConstantsBuffer; @@ -18,32 +20,34 @@ namespace FidelityFX private ComputeBuffer _rcasConstantsBuffer; private readonly RcasConstants[] _rcasConstantsArray = new RcasConstants[1]; - public void Create(Fsr2Callbacks callbacks) + public void Create(Fsr2.ContextDescription contextDescription) { + _contextDescription = contextDescription; + if (_rcasComputeShader == null) - _rcasComputeShader = callbacks.LoadComputeShader("FSR2/ffx_fsr2_rcas_pass"); + _rcasComputeShader = _contextDescription.Callbacks.LoadComputeShader("FSR2/ffx_fsr2_rcas_pass"); _fsr2ConstantsBuffer = new ComputeBuffer(1, Marshal.SizeOf(), ComputeBufferType.Constant); _spdConstantsBuffer = new ComputeBuffer(1, Marshal.SizeOf(), ComputeBufferType.Constant); _rcasConstantsBuffer = new ComputeBuffer(1, Marshal.SizeOf(), ComputeBufferType.Constant); } - public void Dispatch(RenderTexture src, RenderTexture dest, Texture2D exposure, bool enableSharpening, float sharpness) // TODO: collect parameters into a single object + public void Dispatch(Fsr2.DispatchDescription dispatchDescription) { - _fsr2ConstantsArray[0].preExposure = 1.0f; + _fsr2ConstantsArray[0].preExposure = dispatchDescription.PreExposure; _fsr2ConstantsBuffer.SetData(_fsr2ConstantsArray); - if (enableSharpening) + if (dispatchDescription.EnableSharpening) { - int sharpnessIndex = Mathf.RoundToInt(Mathf.Clamp01(sharpness) * (RcasConfigs.Count - 1)); + int sharpnessIndex = Mathf.RoundToInt(Mathf.Clamp01(dispatchDescription.Sharpness) * (RcasConfigs.Count - 1)); _rcasConstantsArray[0] = RcasConfigs[sharpnessIndex]; _rcasConstantsBuffer.SetData(_rcasConstantsArray); // Run the RCAS sharpening filter on the upscaled image int rcasKernel = _rcasComputeShader.FindKernel("CS"); - _rcasComputeShader.SetTexture(rcasKernel, "r_exposure", exposure); - _rcasComputeShader.SetTexture(rcasKernel, "r_rcas_input", src); - _rcasComputeShader.SetTexture(rcasKernel, "rw_upscaled_output", dest); + _rcasComputeShader.SetTexture(rcasKernel, "r_exposure", dispatchDescription.Exposure); + _rcasComputeShader.SetTexture(rcasKernel, "r_rcas_input", dispatchDescription.Input); + _rcasComputeShader.SetTexture(rcasKernel, "rw_upscaled_output", dispatchDescription.Output); _rcasComputeShader.SetConstantBuffer("cbFSR2", _fsr2ConstantsBuffer, 0, Marshal.SizeOf()); _rcasComputeShader.SetConstantBuffer("cbRCAS", _rcasConstantsBuffer, 0, Marshal.SizeOf()); @@ -55,7 +59,7 @@ namespace FidelityFX } else { - Graphics.Blit(src, dest); + Graphics.Blit(dispatchDescription.Input, dispatchDescription.Output); } } diff --git a/Assets/Scripts/Fsr2Controller.cs b/Assets/Scripts/Fsr2Controller.cs index aaab6c7..73096a6 100644 --- a/Assets/Scripts/Fsr2Controller.cs +++ b/Assets/Scripts/Fsr2Controller.cs @@ -2,9 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; -using System.Runtime.InteropServices; using FidelityFX; -using UnityEditor; using UnityEngine; using UnityEngine.Rendering; @@ -31,9 +29,10 @@ public class Fsr2Controller : MonoBehaviour public float renderScale; private Fsr2Context _context; + private readonly Fsr2.DispatchDescription _dispatchDescription = new Fsr2.DispatchDescription(); private RenderTexture _upscaleRT; - private RenderTexture _rcasOutput; + private RenderTexture _outputRT; private Texture2D _exposure; private Material _testMaterial; @@ -43,7 +42,7 @@ public class Fsr2Controller : MonoBehaviour { if (_testMaterial == null) { - var testShader = Fsr2.Callbacks.LoadShader("FSR2/FSRTest"); + var testShader = Fsr2.GlobalCallbacks.LoadShader("Shaders/FSRTest"); _testMaterial = new Material(testShader); } @@ -60,9 +59,9 @@ public class Fsr2Controller : MonoBehaviour _upscaleRT = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGBHalf); _upscaleRT.Create(); - _rcasOutput = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGBHalf); - _rcasOutput.enableRandomWrite = true; - _rcasOutput.Create(); + _outputRT = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGBHalf); + _outputRT.enableRandomWrite = true; + _outputRT.Create(); _exposure = new Texture2D(1, 1); _exposure.name = "FSR2 Exposure"; @@ -78,10 +77,10 @@ public class Fsr2Controller : MonoBehaviour _exposure = null; } - if (_rcasOutput != null) + if (_outputRT != null) { - _rcasOutput.Release(); - _rcasOutput = null; + _outputRT.Release(); + _outputRT = null; } if (_upscaleRT != null) @@ -111,9 +110,22 @@ public class Fsr2Controller : MonoBehaviour // Do a dumb upscale first Graphics.Blit(gameCamera.targetTexture, _upscaleRT, TestMaterial); - _context.Dispatch(_upscaleRT, _rcasOutput, _exposure, performSharpenPass, sharpness); + _dispatchDescription.Input = _upscaleRT; + _dispatchDescription.Output = _outputRT; + _dispatchDescription.Exposure = _exposure; + _dispatchDescription.PreExposure = 1.0f; + _dispatchDescription.EnableSharpening = performSharpenPass; + _dispatchDescription.Sharpness = sharpness; + _dispatchDescription.MotionVectorScale.x = gameCamera.pixelWidth; + _dispatchDescription.MotionVectorScale.y = gameCamera.pixelHeight; + _dispatchDescription.FrameTimeDelta = Time.unscaledDeltaTime; + _dispatchDescription.CameraNear = gameCamera.nearClipPlane; + _dispatchDescription.CameraFar = gameCamera.farClipPlane; + _dispatchDescription.CameraFovAngleVertical = gameCamera.fieldOfView * Mathf.Deg2Rad; + + _context.Dispatch(_dispatchDescription); // Output sharpened image to screen - Graphics.Blit(_rcasOutput, dest); + Graphics.Blit(_outputRT, dest); } } diff --git a/Assets/Scripts/SubsampleTest.cs b/Assets/Scripts/SubsampleTest.cs index 8ffe1dd..d1e2723 100644 --- a/Assets/Scripts/SubsampleTest.cs +++ b/Assets/Scripts/SubsampleTest.cs @@ -62,13 +62,13 @@ public class SubsampleTest : MonoBehaviour // Adjust texture mipmap LOD bias by log2(renderResolution/displayResolution) - 1.0; // May need to leave this to the game dev, as we don't know which textures do and don't belong to the 3D scene float biasOffset = GetMipmapBiasOffset(); - Fsr2.Callbacks.ApplyMipmapBias(biasOffset); + Fsr2.GlobalCallbacks.ApplyMipmapBias(biasOffset); } private void OnDisable() { float biasOffset = GetMipmapBiasOffset(); - Fsr2.Callbacks.ApplyMipmapBias(-biasOffset); + Fsr2.GlobalCallbacks.ApplyMipmapBias(-biasOffset); gameCamera.targetTexture.Release(); gameCamera.targetTexture = null;