From b15e3b9c39525898ef5c9e97c4c51a8d30d69ac8 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Thu, 23 Feb 2023 12:38:43 +0100 Subject: [PATCH] Use temporary render textures for storing color, depth and motion vectors copied from the game camera. --- Assets/Scripts/Fsr2Controller.cs | 47 ++++++++++++-------------------- Assets/Scripts/SubsampleTest.cs | 4 +++ 2 files changed, 21 insertions(+), 30 deletions(-) diff --git a/Assets/Scripts/Fsr2Controller.cs b/Assets/Scripts/Fsr2Controller.cs index 483a5cf..a746ab6 100644 --- a/Assets/Scripts/Fsr2Controller.cs +++ b/Assets/Scripts/Fsr2Controller.cs @@ -88,17 +88,7 @@ public class Fsr2Controller : MonoBehaviour // TODO: enable motion vector jitter cancellation or not? _context = Fsr2.CreateContext(DisplaySize, RenderSize); - // TODO: could make these temporary RTs - _colorRT = new RenderTexture(RenderSize.x, RenderSize.y, 0, RenderTextureFormat.ARGBHalf) { name = "FSR2 Color Input" }; - _colorRT.Create(); - - _depthRT = new RenderTexture(RenderSize.x, RenderSize.y, 0, RenderTextureFormat.RFloat) { name = "FSR2 Depth Input" }; - _depthRT.Create(); - - _motionVectorsRT = new RenderTexture(RenderSize.x, RenderSize.y, 0, RenderTextureFormat.RGHalf) { name = "FSR2 Motion Vectors Input" }; - _motionVectorsRT.Create(); - - _outputRT = new RenderTexture(Screen.width, Screen.height, 24, RenderTextureFormat.ARGBHalf) { enableRandomWrite = true }; // TODO: does this need to be a UAV? + _outputRT = new RenderTexture(DisplaySize.x, DisplaySize.y, 24, RenderTextureFormat.ARGBHalf) { enableRandomWrite = true }; _outputRT.Create(); _exposure = new Texture2D(1, 1) { name = "FSR2 Exposure" }; @@ -120,24 +110,6 @@ public class Fsr2Controller : MonoBehaviour _outputRT = null; } - if (_motionVectorsRT != null) - { - _motionVectorsRT.Release(); - _motionVectorsRT = null; - } - - if (_depthRT != null) - { - _depthRT.Release(); - _depthRT = null; - } - - if (_colorRT != null) - { - _colorRT.Release(); - _colorRT = null; - } - if (_context != null) { _context.Destroy(); @@ -149,6 +121,12 @@ public class Fsr2Controller : MonoBehaviour public void UpdateInputResources(RenderTexture src) { + // I hate having to allocate extra RTs just to duplicate already existing Unity render buffers, but AFAIK there is no way to directly address these buffers individually from code + var renderSize = RenderSize; + _colorRT = RenderTexture.GetTemporary(renderSize.x, renderSize.y, 0, RenderTextureFormat.ARGBHalf); + _depthRT = RenderTexture.GetTemporary(renderSize.x, renderSize.y, 0, RenderTextureFormat.RFloat); + _motionVectorsRT = RenderTexture.GetTemporary(renderSize.x, renderSize.y, 0, RenderTextureFormat.RGHalf); + Graphics.Blit(src, _colorRT); Graphics.Blit(src, _depthRT, CopyDepthMaterial); Graphics.Blit(src, _motionVectorsRT, CopyMotionVectorsMaterial); @@ -163,6 +141,10 @@ public class Fsr2Controller : MonoBehaviour // For legacy built-in render pipeline private void OnRenderImage(RenderTexture src, RenderTexture dest) { + // Ensure the input resources were updated correctly before upscaling + if (_colorRT == null || _depthRT == null || _motionVectorsRT == null) + return; + _dispatchDescription.Color = _colorRT; _dispatchDescription.Depth = _depthRT; _dispatchDescription.MotionVectors = _motionVectorsRT; @@ -181,7 +163,12 @@ public class Fsr2Controller : MonoBehaviour _context.Dispatch(_dispatchDescription); - // Output sharpened image to screen + RenderTexture.ReleaseTemporary(_colorRT); + RenderTexture.ReleaseTemporary(_depthRT); + RenderTexture.ReleaseTemporary(_motionVectorsRT); + _colorRT = _depthRT = _motionVectorsRT = null; + + // Output upscaled image to screen Graphics.Blit(_outputRT, dest); } } diff --git a/Assets/Scripts/SubsampleTest.cs b/Assets/Scripts/SubsampleTest.cs index f8f9044..d1e091c 100644 --- a/Assets/Scripts/SubsampleTest.cs +++ b/Assets/Scripts/SubsampleTest.cs @@ -116,7 +116,11 @@ public class SubsampleTest : MonoBehaviour private void OnRenderImage(RenderTexture src, RenderTexture dest) { + // We don't actually output any image from this camera; instead we pass its render buffers on to the FSR2 system _fsr2Controller.UpdateInputResources(src); + + // Shut up Unity warning about not writing anything to the destination texture + Graphics.SetRenderTarget(dest); } private void OnPostRender()