From ef3b14f3fa4fc8b17ed1295cdad2f2d095c96c2f Mon Sep 17 00:00:00 2001 From: Fabien Houlmann Date: Mon, 1 Jun 2020 15:24:30 -0400 Subject: [PATCH] add color clear pass while rendering XR occlusion mesh to avoid leaks --- com.unity.render-pipelines.high-definition/CHANGELOG.md | 1 + .../Runtime/RenderPipeline/HDRenderPipeline.Prepass.cs | 3 ++- .../RenderPipeline/HDRenderPipeline.RenderGraphUtils.cs | 3 ++- .../Runtime/RenderPipeline/HDRenderPipeline.cs | 5 ++++- .../Runtime/RenderPipeline/HDStringConstants.cs | 1 + .../Runtime/RenderPipeline/XR/XRPass.cs | 5 +++-- .../Runtime/ShaderLibrary/XROcclusionMesh.shader | 6 ++++-- 7 files changed, 17 insertions(+), 7 deletions(-) diff --git a/com.unity.render-pipelines.high-definition/CHANGELOG.md b/com.unity.render-pipelines.high-definition/CHANGELOG.md index 891e9d9c64b..62e5ee47856 100644 --- a/com.unity.render-pipelines.high-definition/CHANGELOG.md +++ b/com.unity.render-pipelines.high-definition/CHANGELOG.md @@ -760,6 +760,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Changed default exposure compensation to 0. - Refactored shadow caching system. - Removed experimental namespace for ray tracing code. +- Add color clear pass while rendering XR occlusion mesh to avoid leaks. ## [7.1.1] - 2019-09-05 diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.Prepass.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.Prepass.cs index c6bafbc3155..9d8ee4c46b4 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.Prepass.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.Prepass.cs @@ -115,7 +115,8 @@ PrepassOutput RenderPrepass(RenderGraph renderGraph, TextureHandle sssBuffer, Cu result.motionVectorsBuffer = CreateMotionVectorBuffer(renderGraph, msaa, clearMotionVectors); result.depthBuffer = CreateDepthBuffer(renderGraph, msaa); - RenderXROcclusionMeshes(renderGraph, hdCamera, result.depthBuffer); + // TODO RENDERGRAPH : XR occlusion mesh also need to write to color buffer + //RenderXROcclusionMeshes(renderGraph, hdCamera, result.depthBuffer); using (new XRSinglePassScope(renderGraph, hdCamera)) { diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraphUtils.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraphUtils.cs index 1b351d1df5b..61d7461365c 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraphUtils.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.RenderGraphUtils.cs @@ -143,7 +143,8 @@ void RenderXROcclusionMeshes(RenderGraph renderGraph, HDCamera hdCamera, Texture builder.SetRenderFunc( (RenderOcclusionMeshesPassData data, RenderGraphContext ctx) => { - data.hdCamera.xr.RenderOcclusionMeshes(ctx.cmd, ctx.resources.GetTexture(data.depthBuffer)); + // TODO RENDERGRAPH : XR occlusion mesh also need to write to color buffer + //data.hdCamera.xr.RenderOcclusionMeshes(ctx.cmd, ctx.resources.GetTexture(data.depthBuffer)); }); } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs index 6da2dffe9d0..7ad3e03d60c 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDRenderPipeline.cs @@ -2253,8 +2253,11 @@ AOVRequestData aovRequest // Render XR occlusion mesh to depth buffer early in the frame to improve performance if (hdCamera.xr.enabled && m_Asset.currentPlatformRenderPipelineSettings.xrSettings.occlusionMesh) { + bool msaa = hdCamera.frameSettings.IsEnabled(FrameSettingsField.MSAA); + Color clearColor = GetColorBufferClearColor(hdCamera); + hdCamera.xr.StopSinglePass(cmd); - hdCamera.xr.RenderOcclusionMeshes(cmd, m_SharedRTManager.GetDepthStencilBuffer(hdCamera.frameSettings.IsEnabled(FrameSettingsField.MSAA))); + hdCamera.xr.RenderOcclusionMeshes(cmd, clearColor, msaa ? m_CameraColorMSAABuffer : m_CameraColorBuffer, m_SharedRTManager.GetDepthStencilBuffer(msaa)); hdCamera.xr.StartSinglePass(cmd); } diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs index 6db4c395dec..9c0ab2a54d1 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/HDStringConstants.cs @@ -186,6 +186,7 @@ static class HDShaderIDs public static readonly int _InputDepth = Shader.PropertyToID("_InputDepthTexture"); + public static readonly int _ClearColor = Shader.PropertyToID("_ClearColor"); public static readonly int _SrcBlend = Shader.PropertyToID("_SrcBlend"); public static readonly int _DstBlend = Shader.PropertyToID("_DstBlend"); diff --git a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/XR/XRPass.cs b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/XR/XRPass.cs index 40a635b4a39..c6d53e39e57 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/XR/XRPass.cs +++ b/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/XR/XRPass.cs @@ -255,7 +255,7 @@ internal void EndCamera(CommandBuffer cmd, HDCamera hdCamera) } } - internal void RenderOcclusionMeshes(CommandBuffer cmd, RTHandle depthBuffer) + internal void RenderOcclusionMeshes(CommandBuffer cmd, Color clearColor, RTHandle colorBuffer, RTHandle depthBuffer) { if (enabled && xrSdkEnabled && occlusionMeshMaterial != null) { @@ -267,7 +267,8 @@ internal void RenderOcclusionMeshes(CommandBuffer cmd, RTHandle depthBuffer) { if (views[viewId].occlusionMesh != null) { - CoreUtils.SetRenderTarget(cmd, depthBuffer, ClearFlag.None, 0, CubemapFace.Unknown, viewId); + CoreUtils.SetRenderTarget(cmd, colorBuffer, depthBuffer, ClearFlag.None, clearColor, 0, CubemapFace.Unknown, viewId); + cmd.SetGlobalVector(HDShaderIDs._ClearColor, clearColor); cmd.DrawMesh(views[viewId].occlusionMesh, m, occlusionMeshMaterial); } } diff --git a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/XROcclusionMesh.shader b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/XROcclusionMesh.shader index 05730dc8889..e6f68e75096 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/XROcclusionMesh.shader +++ b/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/XROcclusionMesh.shader @@ -23,8 +23,11 @@ Shader "Hidden/HDRP/XROcclusionMesh" return output; } - void Frag(out float outputDepth : SV_Depth) + float4 _ClearColor; + + void Frag(out float4 outputColor : SV_Target, out float outputDepth : SV_Depth) { + outputColor = _ClearColor; outputDepth = UNITY_NEAR_CLIP_VALUE; } ENDHLSL @@ -36,7 +39,6 @@ Shader "Hidden/HDRP/XROcclusionMesh" Pass { ZWrite On ZTest Always Blend Off Cull Off - ColorMask 0 HLSLPROGRAM #pragma vertex Vert