From e08a8f9c2268e53d297c45226d1c29108a419109 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 25 Feb 2023 00:52:00 +0100 Subject: [PATCH] Playing around with resetting and recreating FSR context upon resolution or quality mode change. Seems to be working quite well. --- Assets/Scripts/Fsr2.cs | 2 ++ Assets/Scripts/Fsr2Controller.cs | 22 ++++++++++++--------- Assets/Scripts/SubsampleTest.cs | 33 +++++++++++++++++++------------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/Assets/Scripts/Fsr2.cs b/Assets/Scripts/Fsr2.cs index f601b23..74c3fe0 100644 --- a/Assets/Scripts/Fsr2.cs +++ b/Assets/Scripts/Fsr2.cs @@ -20,6 +20,8 @@ namespace FidelityFX else flags &= ~InitializationFlags.EnableDepthInverted; + Debug.Log($"Setting up FSR2 with render size: {maxRenderSize.x}x{maxRenderSize.y}, display size: {displaySize.x}x{displaySize.y}, flags: {flags}"); + var contextDescription = new ContextDescription { Flags = flags, diff --git a/Assets/Scripts/Fsr2Controller.cs b/Assets/Scripts/Fsr2Controller.cs index fe7bbdd..ba6a4bd 100644 --- a/Assets/Scripts/Fsr2Controller.cs +++ b/Assets/Scripts/Fsr2Controller.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Linq; using FidelityFX; using UnityEngine; -using UnityEngine.Experimental.Rendering; using UnityEngine.Rendering; /// @@ -20,6 +19,9 @@ public class Fsr2Controller : MonoBehaviour [SerializeField, Range(0, 1)] private float sharpness = 0.8f; + [SerializeField] + private bool reset; + [HideInInspector] public Camera gameCamera; @@ -37,7 +39,7 @@ public class Fsr2Controller : MonoBehaviour private Fsr2Context _context; private readonly Fsr2.DispatchDescription _dispatchDescription = new Fsr2.DispatchDescription(); - private RenderTexture _outputRT; + private RenderTexture _upscaledOutput; private Material _copyMotionMat; private Material CopyMotionVectorsMaterial @@ -73,16 +75,16 @@ public class Fsr2Controller : MonoBehaviour // TODO: do we need a depth buffer for the output? We will need depth & motion vectors for subsequent post-FX. How should FSR2 output these? // TODO: can probably be a temporary RT - _outputRT = new RenderTexture(DisplaySize.x, DisplaySize.y, 24, RenderTextureFormat.ARGBHalf) { name = "FSR2 Upscaled Output", enableRandomWrite = true }; - _outputRT.Create(); + _upscaledOutput = new RenderTexture(DisplaySize.x, DisplaySize.y, 24, RenderTextureFormat.ARGBHalf) { name = "FSR2 Upscaled Output", enableRandomWrite = true }; + _upscaledOutput.Create(); } private void OnDisable() { - if (_outputRT != null) + if (_upscaledOutput != null) { - _outputRT.Release(); - _outputRT = null; + _upscaledOutput.Release(); + _upscaledOutput = null; } if (_context != null) @@ -116,7 +118,7 @@ public class Fsr2Controller : MonoBehaviour _dispatchDescription.ColorDepth = renderBuffer; _dispatchDescription.MotionVectors = motionVectors; - _dispatchDescription.Output = _outputRT; + _dispatchDescription.Output = _upscaledOutput; _dispatchDescription.Exposure = null; _dispatchDescription.Reactive = null; _dispatchDescription.PreExposure = 0; @@ -130,6 +132,8 @@ public class Fsr2Controller : MonoBehaviour _dispatchDescription.CameraFar = gameCamera.farClipPlane; _dispatchDescription.CameraFovAngleVertical = gameCamera.fieldOfView * Mathf.Deg2Rad; _dispatchDescription.ViewSpaceToMetersFactor = 1.0f; // 1 unit is 1 meter in Unity + _dispatchDescription.Reset = reset; + reset = false; _context.Dispatch(_dispatchDescription); @@ -137,6 +141,6 @@ public class Fsr2Controller : MonoBehaviour // Output upscaled image to screen // TODO: we should probably use a shader to include depth & motion vectors into the output - Graphics.Blit(_outputRT, dest); + Graphics.Blit(_upscaledOutput, dest); } } diff --git a/Assets/Scripts/SubsampleTest.cs b/Assets/Scripts/SubsampleTest.cs index 5214b85..21fedfb 100644 --- a/Assets/Scripts/SubsampleTest.cs +++ b/Assets/Scripts/SubsampleTest.cs @@ -22,6 +22,9 @@ public class SubsampleTest : MonoBehaviour private Fsr2Controller _fsr2Controller; private float _renderScale; + private Fsr2.QualityMode _prevQualityMode; + private Vector2Int _prevScreenSize; + private void OnEnable() { _renderScale = 1.0f / Fsr2.GetUpscaleRatioFromQualityMode(qualityMode); @@ -35,23 +38,18 @@ public class SubsampleTest : MonoBehaviour //outputCameraObject.transform.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector; outputCamera = outputCameraObject.AddComponent(); - outputCamera.eventMask = 0; - - _fsr2Controller = outputCameraObject.AddComponent(); - _fsr2Controller.gameCamera = gameCamera; - _fsr2Controller.outputCamera = outputCamera; - _fsr2Controller.renderScale = _renderScale; - _fsr2Controller.enabled = true; - //outputCamera.CopyFrom(gameCamera); - + outputCamera.eventMask = 0; outputCamera.cullingMask = 0; outputCamera.clearFlags = CameraClearFlags.Nothing; + + _fsr2Controller = outputCameraObject.AddComponent(); } - else - { - _fsr2Controller.enabled = true; - } + + _fsr2Controller.gameCamera = gameCamera; + _fsr2Controller.outputCamera = outputCamera; + _fsr2Controller.renderScale = _renderScale; + _fsr2Controller.enabled = true; Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, Screen.width, Screen.height, qualityMode); @@ -69,6 +67,9 @@ public class SubsampleTest : MonoBehaviour // 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 = Fsr2.GetMipmapBiasOffset(_renderScale); Fsr2.GlobalCallbacks.ApplyMipmapBias(biasOffset); + + _prevScreenSize = new Vector2Int(Screen.width, Screen.height); + _prevQualityMode = qualityMode; } private void OnDisable() @@ -96,6 +97,12 @@ public class SubsampleTest : MonoBehaviour //outputCamera.CopyFrom(gameCamera); outputCamera.clearFlags = CameraClearFlags.Color; + + if (Screen.width != _prevScreenSize.x || Screen.height != _prevScreenSize.y || qualityMode != _prevQualityMode) + { + enabled = false; + enabled = true; + } } private Rect _tempRect;