From 495350f569a6bddbe7e33157c4e2fa92b377a964 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Fri, 24 Mar 2023 12:00:14 +0100 Subject: [PATCH] Allow outputting to a user-provided color + depth buffer and optionally a motion vectors buffer. This can be used for passing the upscaled result to a different camera for additional post-processing and final output. Motion vectors aren't rescaled properly yet. --- Assets/Scripts/Fsr2ImageEffect.cs | 33 +++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/Fsr2ImageEffect.cs b/Assets/Scripts/Fsr2ImageEffect.cs index 7077e4f..9f9516d 100644 --- a/Assets/Scripts/Fsr2ImageEffect.cs +++ b/Assets/Scripts/Fsr2ImageEffect.cs @@ -43,6 +43,10 @@ namespace FidelityFX public Fsr2.GenerateReactiveFlags flags = Fsr2.GenerateReactiveFlags.ApplyTonemap | Fsr2.GenerateReactiveFlags.ApplyThreshold | Fsr2.GenerateReactiveFlags.UseComponentsMax; } + [Header("Output resources")] + public RenderTexture outputColorDepth; + public RenderTexture outputMotionVectors; + private Fsr2Context _context; private Vector2Int _renderSize; private Vector2Int _displaySize; @@ -65,6 +69,8 @@ namespace FidelityFX private CommandBuffer _dispatchCommandBuffer; private CommandBuffer _opaqueInputCommandBuffer; + private Material _copyWithDepthMaterial; + private RenderTextureFormat DefaultFormat => _renderCamera.allowHDR ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default; private void OnEnable() @@ -114,6 +120,8 @@ namespace FidelityFX _renderCamera.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer); } + _copyWithDepthMaterial = new Material(Shader.Find("Hidden/BlitCopyWithDepth")); + _prevDisplaySize = _displaySize; _prevQualityMode = qualityMode; _prevGenReactiveMask = autoGenerateReactiveMask; @@ -129,6 +137,12 @@ namespace FidelityFX _renderCamera.depthTextureMode = _originalDepthTextureMode; _renderCamera.targetTexture = _originalRenderTarget; + if (_copyWithDepthMaterial != null) + { + Destroy(_copyWithDepthMaterial); + _copyWithDepthMaterial = null; + } + if (_opaqueInputCommandBuffer != null) { _renderCamera.RemoveCommandBuffer(CameraEvent.BeforeForwardAlpha, _opaqueInputCommandBuffer); @@ -287,8 +301,23 @@ namespace FidelityFX _context.Dispatch(_dispatchDescription, _dispatchCommandBuffer); - // Output the upscaled image to the backbuffer - _dispatchCommandBuffer.Blit(Fsr2ShaderIDs.UavUpscaledOutput, _originalRenderTarget != null ? _originalRenderTarget : dest); + // Output the upscaled image + if (_originalRenderTarget != null || outputColorDepth != null) + { + var targetTexture = outputColorDepth != null ? outputColorDepth : _originalRenderTarget; + + // Output to the camera target texture, passing through depth and motion vectors + _dispatchCommandBuffer.SetGlobalTexture("_DepthTex", BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth); + _dispatchCommandBuffer.Blit(Fsr2ShaderIDs.UavUpscaledOutput, targetTexture, _copyWithDepthMaterial); + if (outputMotionVectors != null) + _dispatchCommandBuffer.Blit(BuiltinRenderTextureType.MotionVectors, outputMotionVectors); + } + else + { + // Output directly to the backbuffer + _dispatchCommandBuffer.Blit(Fsr2ShaderIDs.UavUpscaledOutput, dest); + } + _dispatchCommandBuffer.ReleaseTemporaryRT(Fsr2ShaderIDs.UavUpscaledOutput); if (autoGenerateReactiveMask)