From 745f5c8ba44b8e3c9d1aa4e076deb6bf52a6a3cc Mon Sep 17 00:00:00 2001 From: Janice See Date: Wed, 2 Aug 2023 13:14:32 -0700 Subject: [PATCH] [URP] RTHandles Load/Store actions optimizations fix This is a backport of PR #6334 to fix reported foveation issues. --- .../Runtime/Passes/ColorGradingLutPass.cs | 4 +++- .../Runtime/Passes/CopyColorPass.cs | 8 ++++---- .../Runtime/Passes/CopyDepthPass.cs | 9 +++++++-- .../Runtime/ScriptableRenderer.cs | 4 ++++ .../Runtime/UniversalRenderer.cs | 8 +++++--- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Packages/com.unity.render-pipelines.universal/Runtime/Passes/ColorGradingLutPass.cs b/Packages/com.unity.render-pipelines.universal/Runtime/Passes/ColorGradingLutPass.cs index 49d69f3c4ab..78eb2aea4f4 100644 --- a/Packages/com.unity.render-pipelines.universal/Runtime/Passes/ColorGradingLutPass.cs +++ b/Packages/com.unity.render-pipelines.universal/Runtime/Passes/ColorGradingLutPass.cs @@ -172,8 +172,10 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData.cameraData.xr.StopSinglePass(cmd); + cmd.SetGlobalTexture(ShaderPropertyId.sourceTex, m_InternalLut.Identifier()); + CoreUtils.SetRenderTarget(cmd, m_InternalLut.Identifier(), RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store, ClearFlag.None, Color.clear); // Render the lut - cmd.Blit(null, m_InternalLut.id, material); + cmd.Blit(null, BuiltinRenderTextureType.CurrentActive, material, 0); renderingData.cameraData.xr.StartSinglePass(cmd); } diff --git a/Packages/com.unity.render-pipelines.universal/Runtime/Passes/CopyColorPass.cs b/Packages/com.unity.render-pipelines.universal/Runtime/Passes/CopyColorPass.cs index a2c603bd586..5e907f745c1 100644 --- a/Packages/com.unity.render-pipelines.universal/Runtime/Passes/CopyColorPass.cs +++ b/Packages/com.unity.render-pipelines.universal/Runtime/Passes/CopyColorPass.cs @@ -93,17 +93,17 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData switch (m_DownsamplingMethod) { case Downsampling.None: - RenderingUtils.Blit(cmd, source, opaqueColorRT, m_CopyColorMaterial, 0, useDrawProceduleBlit); + RenderingUtils.Blit(cmd, source, opaqueColorRT, m_CopyColorMaterial, 0, useDrawProceduleBlit, RenderBufferLoadAction.DontCare); break; case Downsampling._2xBilinear: - RenderingUtils.Blit(cmd, source, opaqueColorRT, m_CopyColorMaterial, 0, useDrawProceduleBlit); + RenderingUtils.Blit(cmd, source, opaqueColorRT, m_CopyColorMaterial, 0, useDrawProceduleBlit, RenderBufferLoadAction.DontCare); break; case Downsampling._4xBox: m_SamplingMaterial.SetFloat(m_SampleOffsetShaderHandle, 2); - RenderingUtils.Blit(cmd, source, opaqueColorRT, m_SamplingMaterial, 0, useDrawProceduleBlit); + RenderingUtils.Blit(cmd, source, opaqueColorRT, m_SamplingMaterial, 0, useDrawProceduleBlit, RenderBufferLoadAction.DontCare); break; case Downsampling._4xBilinear: - RenderingUtils.Blit(cmd, source, opaqueColorRT, m_CopyColorMaterial, 0, useDrawProceduleBlit); + RenderingUtils.Blit(cmd, source, opaqueColorRT, m_CopyColorMaterial, 0, useDrawProceduleBlit, RenderBufferLoadAction.DontCare); break; } } diff --git a/Packages/com.unity.render-pipelines.universal/Runtime/Passes/CopyDepthPass.cs b/Packages/com.unity.render-pipelines.universal/Runtime/Passes/CopyDepthPass.cs index 5f923b05a3b..5038fa17a37 100644 --- a/Packages/com.unity.render-pipelines.universal/Runtime/Passes/CopyDepthPass.cs +++ b/Packages/com.unity.render-pipelines.universal/Runtime/Passes/CopyDepthPass.cs @@ -19,12 +19,15 @@ public class CopyDepthPass : ScriptableRenderPass internal bool AllocateRT { get; set; } internal int MssaSamples { get; set; } Material m_CopyDepthMaterial; - public CopyDepthPass(RenderPassEvent evt, Material copyDepthMaterial) + internal bool m_ShouldClear; + + public CopyDepthPass(RenderPassEvent evt, Material copyDepthMaterial, bool shouldClear = false) { base.profilingSampler = new ProfilingSampler(nameof(CopyDepthPass)); AllocateRT = true; m_CopyDepthMaterial = copyDepthMaterial; renderPassEvent = evt; + m_ShouldClear = shouldClear; } /// @@ -51,7 +54,9 @@ public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderin // On Metal iOS, prevent camera attachments to be bound and cleared during this pass. ConfigureTarget(new RenderTargetIdentifier(destination.Identifier(), 0, CubemapFace.Unknown, -1), descriptor.depthStencilFormat, descriptor.width, descriptor.height, descriptor.msaaSamples, true); - ConfigureClear(ClearFlag.None, Color.black); + + if (m_ShouldClear) + ConfigureClear(ClearFlag.All, Color.black); } /// diff --git a/Packages/com.unity.render-pipelines.universal/Runtime/ScriptableRenderer.cs b/Packages/com.unity.render-pipelines.universal/Runtime/ScriptableRenderer.cs index 9629d5c1cee..38b7e2d29e4 100644 --- a/Packages/com.unity.render-pipelines.universal/Runtime/ScriptableRenderer.cs +++ b/Packages/com.unity.render-pipelines.universal/Runtime/ScriptableRenderer.cs @@ -1175,6 +1175,10 @@ void SetRenderPassAttachments(CommandBuffer cmd, ScriptableRenderPass renderPass finalClearFlag |= (cameraClearFlag & ClearFlag.Color); finalClearColor = CoreUtils.ConvertSRGBToActiveColorSpace(camera.backgroundColor); + // on platforms that support Load and Store actions having the clear flag means that the action will be DontCare, which is something we want when the color target is bound the first time + if (SystemInfo.usesLoadStoreActions) + finalClearFlag |= renderPass.clearFlag; + if (m_FirstTimeCameraDepthTargetIsBound) { // m_CameraColorTarget can be an opaque pointer to a RenderTexture with depth-surface. diff --git a/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs b/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs index ab1754af3f6..777f329bbf0 100644 --- a/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs +++ b/Packages/com.unity.render-pipelines.universal/Runtime/UniversalRenderer.cs @@ -248,7 +248,7 @@ public UniversalRenderer(UniversalRendererData data) : base(data) if (this.renderingMode == RenderingMode.Forward) { - m_PrimedDepthCopyPass = new CopyDepthPass(RenderPassEvent.AfterRenderingPrePasses, m_CopyDepthMaterial); + m_PrimedDepthCopyPass = new CopyDepthPass(RenderPassEvent.AfterRenderingPrePasses, m_CopyDepthMaterial, true); } if (this.renderingMode == RenderingMode.Deferred) @@ -280,7 +280,7 @@ public UniversalRenderer(UniversalRendererData data) : base(data) new ShaderTagId("LightweightForward") // Legacy shaders (do not have a gbuffer pass) are considered forward-only for backward compatibility }; int forwardOnlyStencilRef = stencilData.stencilReference | (int)StencilUsage.MaterialUnlit; - m_GBufferCopyDepthPass = new CopyDepthPass(RenderPassEvent.BeforeRenderingGbuffer + 1, m_CopyDepthMaterial); + m_GBufferCopyDepthPass = new CopyDepthPass(RenderPassEvent.BeforeRenderingGbuffer + 1, m_CopyDepthMaterial, true); m_TileDepthRangePass = new TileDepthRangePass(RenderPassEvent.BeforeRenderingGbuffer + 2, m_DeferredLights, 0); m_TileDepthRangeExtraPass = new TileDepthRangePass(RenderPassEvent.BeforeRenderingGbuffer + 3, m_DeferredLights, 1); m_DeferredPass = new DeferredPass(RenderPassEvent.BeforeRenderingDeferredLights, m_DeferredLights); @@ -290,7 +290,7 @@ public UniversalRenderer(UniversalRendererData data) : base(data) // Always create this pass even in deferred because we use it for wireframe rendering in the Editor or offscreen depth texture rendering. m_RenderOpaqueForwardPass = new DrawObjectsPass(URPProfileId.DrawOpaqueObjects, true, RenderPassEvent.BeforeRenderingOpaques, RenderQueueRange.opaque, data.opaqueLayerMask, m_DefaultStencilState, stencilData.stencilReference); - m_CopyDepthPass = new CopyDepthPass(RenderPassEvent.AfterRenderingSkybox, m_CopyDepthMaterial); + m_CopyDepthPass = new CopyDepthPass(RenderPassEvent.AfterRenderingSkybox, m_CopyDepthMaterial, true); m_DrawSkyboxPass = new DrawSkyboxPass(RenderPassEvent.BeforeRenderingSkybox); m_CopyColorPass = new CopyColorPass(RenderPassEvent.AfterRenderingSkybox, m_SamplingMaterial, m_BlitMaterial); #if ADAPTIVE_PERFORMANCE_2_1_0_OR_NEWER @@ -772,6 +772,8 @@ public override void Setup(ScriptableRenderContext context, ref RenderingData re m_RenderOpaqueForwardPass.ConfigureColorStoreAction(opaquePassColorStoreAction); m_RenderOpaqueForwardPass.ConfigureDepthStoreAction(opaquePassDepthStoreAction); + m_RenderOpaqueForwardPass.ConfigureClear(ClearFlag.All, Color.black); + EnqueuePass(m_RenderOpaqueForwardPass); }