From bfa86b7bfd535ed3f56669407ae413b0107f8dc8 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Mon, 29 Jul 2024 22:57:46 +0200 Subject: [PATCH] Fixed an issue that would cause the wrong depth buffer to be passed to FSR when using forward rendering on newer Unity versions. --- Assets/Scripts/Fsr3UpscalerImageEffect.cs | 10 ++++++++-- .../PostProcessing/Runtime/Effects/SuperResolution.cs | 8 +++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Assets/Scripts/Fsr3UpscalerImageEffect.cs b/Assets/Scripts/Fsr3UpscalerImageEffect.cs index bbde974..ba8c529 100644 --- a/Assets/Scripts/Fsr3UpscalerImageEffect.cs +++ b/Assets/Scripts/Fsr3UpscalerImageEffect.cs @@ -312,7 +312,7 @@ namespace FidelityFX { // Set up the main FSR3 Upscaler dispatch parameters _dispatchDescription.Color = new ResourceView(BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Color); - _dispatchDescription.Depth = new ResourceView(BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth); + _dispatchDescription.Depth = new ResourceView(GetDepthTexture(), RenderTextureSubElement.Depth); _dispatchDescription.MotionVectors = new ResourceView(BuiltinRenderTextureType.MotionVectors); _dispatchDescription.Exposure = ResourceView.Unassigned; _dispatchDescription.Reactive = ResourceView.Unassigned; @@ -417,7 +417,7 @@ namespace FidelityFX if (_originalRenderTarget != null) { // Output to the camera target texture, passing through depth as well - _dispatchCommandBuffer.SetGlobalTexture("_DepthTex", BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth); + _dispatchCommandBuffer.SetGlobalTexture("_DepthTex", GetDepthTexture(), RenderTextureSubElement.Depth); _dispatchCommandBuffer.Blit(Fsr3ShaderIDs.UavUpscaledOutput, _originalRenderTarget, _copyWithDepthMaterial); } else @@ -448,6 +448,12 @@ namespace FidelityFX return _renderCamera.allowHDR ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default; } + + private BuiltinRenderTextureType GetDepthTexture() + { + RenderingPath renderingPath = _renderCamera.renderingPath; + return renderingPath == RenderingPath.Forward || renderingPath == RenderingPath.VertexLit ? BuiltinRenderTextureType.Depth : BuiltinRenderTextureType.CameraTarget; + } private Vector2Int GetDisplaySize() { diff --git a/Packages/com.unity.postprocessing/PostProcessing/Runtime/Effects/SuperResolution.cs b/Packages/com.unity.postprocessing/PostProcessing/Runtime/Effects/SuperResolution.cs index e997b3f..b6639e3 100644 --- a/Packages/com.unity.postprocessing/PostProcessing/Runtime/Effects/SuperResolution.cs +++ b/Packages/com.unity.postprocessing/PostProcessing/Runtime/Effects/SuperResolution.cs @@ -268,7 +268,7 @@ namespace UnityEngine.Rendering.PostProcessing // Set up the main FSR3 Upscaler dispatch parameters _dispatchDescription.Color = new ResourceView(context.source); - _dispatchDescription.Depth = new ResourceView(BuiltinRenderTextureType.CameraTarget, RenderTextureSubElement.Depth); + _dispatchDescription.Depth = new ResourceView(GetDepthTexture(context.camera), RenderTextureSubElement.Depth); _dispatchDescription.MotionVectors = new ResourceView(BuiltinRenderTextureType.MotionVectors); _dispatchDescription.Exposure = ResourceView.Unassigned; _dispatchDescription.Reactive = ResourceView.Unassigned; @@ -335,5 +335,11 @@ namespace UnityEngine.Rendering.PostProcessing return new Vector2Int(Mathf.CeilToInt(_maxRenderSize.x * ScalableBufferManager.widthScaleFactor), Mathf.CeilToInt(_maxRenderSize.y * ScalableBufferManager.heightScaleFactor)); } + + private static BuiltinRenderTextureType GetDepthTexture(Camera cam) + { + RenderingPath renderingPath = cam.renderingPath; + return renderingPath == RenderingPath.Forward || renderingPath == RenderingPath.VertexLit ? BuiltinRenderTextureType.Depth : BuiltinRenderTextureType.CameraTarget; + } } }