From 8ceffcadd16b93b0e62ba1e37591da23f6a6a3e8 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Mon, 27 Feb 2023 14:48:22 +0100 Subject: [PATCH] Further cleanup and small details --- Assets/Scripts/Fsr2.cs | 4 +-- Assets/Scripts/Fsr2Context.cs | 2 ++ Assets/Scripts/SubsampleTest.cs | 48 ++++++++++++++++----------------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/Assets/Scripts/Fsr2.cs b/Assets/Scripts/Fsr2.cs index 0045681..fd995fe 100644 --- a/Assets/Scripts/Fsr2.cs +++ b/Assets/Scripts/Fsr2.cs @@ -61,9 +61,9 @@ namespace FidelityFX renderHeight = (int)(displayHeight / ratio); } - public static float GetMipmapBiasOffset(float renderScale) + public static float GetMipmapBiasOffset(int renderWidth, int displayWidth) { - return Mathf.Log(renderScale, 2.0f) - 1.0f; + return Mathf.Log((float)renderWidth / displayWidth, 2.0f) - 1.0f; } public static int GetJitterPhaseCount(int renderWidth, int displayWidth) diff --git a/Assets/Scripts/Fsr2Context.cs b/Assets/Scripts/Fsr2Context.cs index 209b6ac..476560c 100644 --- a/Assets/Scripts/Fsr2Context.cs +++ b/Assets/Scripts/Fsr2Context.cs @@ -225,6 +225,8 @@ namespace FidelityFX constants.renderSize = dispatchParams.RenderSize; constants.maxRenderSize = _contextDescription.MaxRenderSize; constants.inputColorResourceDimensions = dispatchParams.RenderSize; // We have no way to query the actual width & height of the input resources, so trust that it matches the render size + // TODO: if we want to support dynamic resolution, we may have to differentiate between renderSize and inputColorResourceDimensions again + // TODO: come to think of it, couldn't we use Unity's ScalableBufferManager for dealing with render scale altogether? So we don't have to deal with this nasty double camera business? // Compute the horizontal FOV for the shader from the vertical one float aspectRatio = (float)dispatchParams.RenderSize.x / dispatchParams.RenderSize.y; diff --git a/Assets/Scripts/SubsampleTest.cs b/Assets/Scripts/SubsampleTest.cs index 0c30b57..4fbcd51 100644 --- a/Assets/Scripts/SubsampleTest.cs +++ b/Assets/Scripts/SubsampleTest.cs @@ -6,27 +6,25 @@ using UnityEngine.Rendering; [RequireComponent(typeof(Camera))] public class SubsampleTest : MonoBehaviour // TODO: rename this to Fsr2Controller { + [SerializeField] + private Fsr2.QualityMode qualityMode; + private Camera _renderCamera; + private RenderTexture _originalRenderTarget; private GameObject _displayCameraObject; private Camera _displayCamera; - [SerializeField] - private Fsr2.QualityMode qualityMode; - private Fsr2Controller _fsr2Controller; - private float _renderScale; private Fsr2.QualityMode _prevQualityMode; private Vector2Int _prevScreenSize; private CommandBuffer _opaqueOnlyCommandBuffer; - private CommandBuffer _reactiveCommandBuffer; + private CommandBuffer _inputsCommandBuffer; private void OnEnable() { - _renderScale = 1.0f / Fsr2.GetUpscaleRatioFromQualityMode(qualityMode); - _renderCamera = GetComponent(); if (_displayCameraObject == null) { @@ -50,36 +48,36 @@ public class SubsampleTest : MonoBehaviour // TODO: rename this to Fsr2Controll } _fsr2Controller.renderCamera = _renderCamera; - _fsr2Controller.renderScale = _renderScale; + _fsr2Controller.renderScale = 1.0f / Fsr2.GetUpscaleRatioFromQualityMode(qualityMode); _fsr2Controller.enabled = true; Fsr2.GetRenderResolutionFromQualityMode(out var renderWidth, out var renderHeight, Screen.width, Screen.height, qualityMode); - // TODO: check if the camera doesn't already have a target texture + _originalRenderTarget = _renderCamera.targetTexture; // TODO: if this isn't null, could maybe reuse this for the output texture? + _renderCamera.targetTexture = new RenderTexture( renderWidth, renderHeight, - 24, // Can we copy depth value from original camera? - RenderTextureFormat.ARGBHalf); // Can we copy format from original camera? Or renderer quality settings? - - _renderCamera.depthTextureMode |= DepthTextureMode.Depth | DepthTextureMode.MotionVectors; + _originalRenderTarget != null ? _originalRenderTarget.depth : 32, + _originalRenderTarget != null ? _originalRenderTarget.format : RenderTextureFormat.ARGBHalf) { name = "FSR2 Input Texture" }; - _renderCamera.targetTexture.name = "FSR2 Input Texture"; _renderCamera.targetTexture.Create(); + _renderCamera.depthTextureMode |= DepthTextureMode.Depth | DepthTextureMode.MotionVectors; + _opaqueOnlyCommandBuffer = new CommandBuffer { name = "FSR2 Opaque Input" }; // TODO: may need to copy the opaque-only render buffer to a temp RT here, in which case we'll need an additional CommandBuffer to release the temp RT again _opaqueOnlyCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvOpaqueOnly, BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Color); _renderCamera.AddCommandBuffer(CameraEvent.BeforeImageEffectsOpaque, _opaqueOnlyCommandBuffer); - _reactiveCommandBuffer = new CommandBuffer { name = "FSR2 Inputs" }; - _reactiveCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputColor, BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Color); - _reactiveCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputDepth, BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth); - _reactiveCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputMotionVectors, BuiltinRenderTextureType.MotionVectors); - _renderCamera.AddCommandBuffer(CameraEvent.BeforeImageEffects, _reactiveCommandBuffer); + _inputsCommandBuffer = new CommandBuffer { name = "FSR2 Inputs" }; + _inputsCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputColor, BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Color); + _inputsCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputDepth, BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth); + _inputsCommandBuffer.SetGlobalTexture(Fsr2Pipeline.SrvInputMotionVectors, BuiltinRenderTextureType.MotionVectors); + _renderCamera.AddCommandBuffer(CameraEvent.BeforeImageEffects, _inputsCommandBuffer); // 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 = Fsr2.GetMipmapBiasOffset(_renderScale); + float biasOffset = Fsr2.GetMipmapBiasOffset(renderWidth, Screen.width); Fsr2.GlobalCallbacks.ApplyMipmapBias(biasOffset); _prevScreenSize = new Vector2Int(Screen.width, Screen.height); @@ -88,19 +86,19 @@ public class SubsampleTest : MonoBehaviour // TODO: rename this to Fsr2Controll private void OnDisable() { - float biasOffset = Fsr2.GetMipmapBiasOffset(_renderScale); + float biasOffset = Fsr2.GetMipmapBiasOffset(_renderCamera.targetTexture.width, _prevScreenSize.x); Fsr2.GlobalCallbacks.ApplyMipmapBias(-biasOffset); _renderCamera.RemoveCommandBuffer(CameraEvent.BeforeImageEffectsOpaque, _opaqueOnlyCommandBuffer); _opaqueOnlyCommandBuffer.Release(); _opaqueOnlyCommandBuffer = null; - _renderCamera.RemoveCommandBuffer(CameraEvent.BeforeImageEffects, _reactiveCommandBuffer); - _reactiveCommandBuffer.Release(); - _reactiveCommandBuffer = null; + _renderCamera.RemoveCommandBuffer(CameraEvent.BeforeImageEffects, _inputsCommandBuffer); + _inputsCommandBuffer.Release(); + _inputsCommandBuffer = null; _renderCamera.targetTexture.Release(); - _renderCamera.targetTexture = null; + _renderCamera.targetTexture = _originalRenderTarget; _fsr2Controller.enabled = false; _displayCamera.enabled = false;