diff --git a/com.unity.render-pipelines.universal/Runtime/2D/Passes/Render2DLightingPass.cs b/com.unity.render-pipelines.universal/Runtime/2D/Passes/Render2DLightingPass.cs index ffb3b213cd2..39e64928631 100644 --- a/com.unity.render-pipelines.universal/Runtime/2D/Passes/Render2DLightingPass.cs +++ b/com.unity.render-pipelines.universal/Runtime/2D/Passes/Render2DLightingPass.cs @@ -42,6 +42,7 @@ public Render2DLightingPass(Renderer2DData rendererData, Material blitMaterial, m_Renderer2DData = rendererData; m_BlitMaterial = blitMaterial; m_SamplingMaterial = samplingMaterial; + overrideCameraClear = true; m_CameraSortingLayerBoundsIndex = GetCameraSortingLayerBoundsIndex(); } @@ -235,12 +236,18 @@ private int DrawLayerBatches( var blendStylesCount = m_Renderer2DData.lightBlendStyles.Length; using (new ProfilingScope(cmd, m_ProfilingDrawRenderers)) { + // Clear the target only when the first layer is rendered and this is the base camera (not camera overlay) + bool needsClear = ((startIndex == 0) && (renderingData.cameraData.renderType == CameraRenderType.Base)); RenderBufferStoreAction initialStoreAction; if (msaaEnabled) initialStoreAction = resolveDuringBatch < startIndex ? RenderBufferStoreAction.Resolve : RenderBufferStoreAction.StoreAndResolve; else initialStoreAction = RenderBufferStoreAction.Store; - cmd.SetRenderTarget(colorAttachment, RenderBufferLoadAction.Load, initialStoreAction, depthAttachment, RenderBufferLoadAction.Load, initialStoreAction); + cmd.SetRenderTarget(colorAttachment, RenderBufferLoadAction.DontCare, initialStoreAction, depthAttachment, RenderBufferLoadAction.DontCare, initialStoreAction); + if (needsClear) + { + CoreUtils.ClearRenderTarget(cmd, ClearFlag.All, CoreUtils.ConvertSRGBToActiveColorSpace(renderingData.cameraData.camera.backgroundColor)); + } for (var i = startIndex; i < startIndex + batchesDrawn; i++) { diff --git a/com.unity.render-pipelines.universal/Runtime/Passes/ScriptableRenderPass.cs b/com.unity.render-pipelines.universal/Runtime/Passes/ScriptableRenderPass.cs index 2c2fd432499..acb0239d867 100644 --- a/com.unity.render-pipelines.universal/Runtime/Passes/ScriptableRenderPass.cs +++ b/com.unity.render-pipelines.universal/Runtime/Passes/ScriptableRenderPass.cs @@ -207,6 +207,8 @@ public Color clearColor /// protected internal ProfilingSampler profilingSampler { get; set; } internal bool overrideCameraTarget { get; set; } + // Use this flag to override the clear logic in ScriptableRenderer and assume responsibility of manually clearing the pass's targets (Color & Depth). + internal bool overrideCameraClear { get; set; } internal bool isBlitRenderPass { get; set; } internal bool useNativeRenderPass { get; set; } @@ -253,6 +255,7 @@ public ScriptableRenderPass() m_ClearFlag = ClearFlag.None; m_ClearColor = Color.black; overrideCameraTarget = false; + overrideCameraClear = false; isBlitRenderPass = false; profilingSampler = new ProfilingSampler($"Unnamed_{nameof(ScriptableRenderPass)}"); useNativeRenderPass = true; diff --git a/com.unity.render-pipelines.universal/Runtime/ScriptableRenderer.cs b/com.unity.render-pipelines.universal/Runtime/ScriptableRenderer.cs index a6ed15da98b..0dc9c253d06 100644 --- a/com.unity.render-pipelines.universal/Runtime/ScriptableRenderer.cs +++ b/com.unity.render-pipelines.universal/Runtime/ScriptableRenderer.cs @@ -970,7 +970,7 @@ void ExecuteRenderPass(ScriptableRenderContext context, ScriptableRenderPass ren void SetRenderPassAttachments(CommandBuffer cmd, ScriptableRenderPass renderPass, ref CameraData cameraData) { Camera camera = cameraData.camera; - ClearFlag cameraClearFlag = GetCameraClearFlag(ref cameraData); + ClearFlag cameraClearFlag = renderPass.overrideCameraClear ? ClearFlag.None : GetCameraClearFlag(ref cameraData); // Invalid configuration - use current attachment setup // Note: we only check color buffers. This is only technically correct because for shadowmaps and depth only passes