Skip to content

Commit adcf661

Browse files
thomas-zengEvergreen
authored andcommitted
[Quest][URP] Added multiview support for the XR Occlusion Mesh pass.
Jira: https://jira.unity3d.com/browse/GFXFOUND-481 - Added multview C# occlusion mesh draw call - Added multiview handling to occlusion pass shader - yflip handling through model matrix when rendering to intermediate texture/eye texture (need test)
1 parent 6f7d3d7 commit adcf661

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

Packages/com.unity.render-pipelines.core/Runtime/XR/XROcclusionMesh.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,35 @@ internal bool hasValidOcclusionMesh
3939
}
4040
}
4141

42-
internal void RenderOcclusionMesh(CommandBuffer cmd, float occlusionMeshScale)
42+
internal void RenderOcclusionMesh(CommandBuffer cmd, float occlusionMeshScale, bool yFlip = false)
4343
{
4444
if (IsOcclusionMeshSupported())
4545
{
4646
using (new ProfilingScope(cmd, k_OcclusionMeshProfilingSampler))
4747
{
4848
if (m_Pass.singlePassEnabled)
4949
{
50-
if (m_CombinedMesh != null && SystemInfo.supportsRenderTargetArrayIndexFromVertexShader)
50+
// Prefer multiview draw
51+
if (m_CombinedMesh != null && SystemInfo.supportsMultiview)
52+
{
53+
// For the multiview code path, keep the multiview state on to propagate geometries to all eye texture slices
54+
cmd.EnableShaderKeyword("XR_OCCLUSION_MESH_COMBINED");
55+
Vector3 scale = new Vector3(occlusionMeshScale, yFlip? occlusionMeshScale : -occlusionMeshScale, 1.0f);
56+
cmd.DrawMesh(m_CombinedMesh, Matrix4x4.Scale(scale), m_Material);
57+
cmd.DisableShaderKeyword("XR_OCCLUSION_MESH_COMBINED");
58+
}
59+
else if (m_CombinedMesh != null && SystemInfo.supportsRenderTargetArrayIndexFromVertexShader)
5160
{
5261
m_Pass.StopSinglePass(cmd);
5362

5463
cmd.EnableShaderKeyword("XR_OCCLUSION_MESH_COMBINED");
55-
Vector3 scale = new Vector3(occlusionMeshScale, occlusionMeshScale, 1.0f);
64+
Vector3 scale = new Vector3(occlusionMeshScale, yFlip ? occlusionMeshScale : -occlusionMeshScale, 1.0f);
5665
cmd.DrawMesh(m_CombinedMesh, Matrix4x4.Scale(scale), m_Material);
5766
cmd.DisableShaderKeyword("XR_OCCLUSION_MESH_COMBINED");
5867

5968
m_Pass.StartSinglePass(cmd);
6069
}
70+
6171
}
6272
else
6373
{

Packages/com.unity.render-pipelines.core/Runtime/XR/XRPass.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,11 @@ public void StopSinglePass(BaseCommandBuffer cmd)
326326
/// "XR_OCCLUSION_MESH_COMBINED" is also enabled when rendering the combined mesh.
327327
/// </summary>
328328
/// <param name="cmd">CommandBuffer to modify</param>
329-
public void RenderOcclusionMesh(CommandBuffer cmd)
329+
/// <param name="renderIntoTexture">Set to true when rendering into a render texture. Used for handling Unity yflip.</param>
330+
public void RenderOcclusionMesh(CommandBuffer cmd, bool renderIntoTexture = false)
330331
{
331332
if(occlusionMeshScale > 0)
332-
m_OcclusionMesh.RenderOcclusionMesh(cmd, occlusionMeshScale);
333+
m_OcclusionMesh.RenderOcclusionMesh(cmd, occlusionMeshScale, renderIntoTexture);
333334
}
334335

335336
/// <summary>
@@ -339,10 +340,11 @@ public void RenderOcclusionMesh(CommandBuffer cmd)
339340
/// "XR_OCCLUSION_MESH_COMBINED" is also enabled when rendering the combined mesh.
340341
/// </summary>
341342
/// <param name="cmd">RasterCommandBuffer to modify</param>
342-
public void RenderOcclusionMesh(RasterCommandBuffer cmd)
343+
/// <param name="renderIntoTexture">Set to true when rendering into a render texture. Used for handling Unity yflip.</param>
344+
public void RenderOcclusionMesh(RasterCommandBuffer cmd, bool renderIntoTexture = false)
343345
{
344346
if (occlusionMeshScale > 0)
345-
m_OcclusionMesh.RenderOcclusionMesh(cmd.m_WrappedCommandBuffer, occlusionMeshScale);
347+
m_OcclusionMesh.RenderOcclusionMesh(cmd.m_WrappedCommandBuffer, occlusionMeshScale, renderIntoTexture);
346348
}
347349

348350
/// <summary>

Packages/com.unity.render-pipelines.universal/Runtime/Passes/XROcclusionMeshPass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private static void ExecutePass(RasterCommandBuffer cmd, PassData data)
3333
if (data.isActiveTargetBackBuffer)
3434
cmd.SetViewport(data.xr.GetViewport());
3535

36-
data.xr.RenderOcclusionMesh(cmd);
36+
data.xr.RenderOcclusionMesh(cmd, renderIntoTexture: !data.isActiveTargetBackBuffer);
3737
}
3838
}
3939

Packages/com.unity.render-pipelines.universal/Shaders/XR/XROcclusionMesh.shader

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ Shader "Hidden/Universal Render Pipeline/XR/XROcclusionMesh"
77

88
// Not all platforms properly support SV_RenderTargetArrayIndex
99
#if defined(SHADER_API_D3D11) || defined(SHADER_API_VULKAN) || defined(SHADER_API_GLCORE) || defined(SHADER_API_GLES3) || defined(SHADER_API_PSSL)
10-
#define USE_XR_OCCLUSION_MESH_COMBINED XR_OCCLUSION_MESH_COMBINED
10+
#if defined (UNITY_STEREO_MULTIVIEW_ENABLED)
11+
#define USE_XR_OCCLUSION_MESH_COMBINED_MULTIVIEW XR_OCCLUSION_MESH_COMBINED
12+
#else
13+
#define USE_XR_OCCLUSION_MESH_COMBINED_RT_ARRAY_INDEX XR_OCCLUSION_MESH_COMBINED
14+
#endif
1115
#endif
1216

1317
struct Attributes
@@ -19,7 +23,7 @@ Shader "Hidden/Universal Render Pipeline/XR/XROcclusionMesh"
1923
{
2024
float4 vertex : SV_POSITION;
2125

22-
#if USE_XR_OCCLUSION_MESH_COMBINED
26+
#if USE_XR_OCCLUSION_MESH_COMBINED_RT_ARRAY_INDEX
2327
uint rtArrayIndex : SV_RenderTargetArrayIndex;
2428
#endif
2529
};
@@ -29,7 +33,12 @@ Shader "Hidden/Universal Render Pipeline/XR/XROcclusionMesh"
2933
Varyings output;
3034
output.vertex = mul(UNITY_MATRIX_M, float4(input.vertex.xy * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), UNITY_NEAR_CLIP_VALUE, 1.0f));
3135

32-
#if USE_XR_OCCLUSION_MESH_COMBINED
36+
#if USE_XR_OCCLUSION_MESH_COMBINED_MULTIVIEW
37+
if (unity_StereoEyeIndex != uint(input.vertex.z))
38+
{
39+
output.vertex = float4(0.0f, 0.0f, 0.0f, 0.0f);
40+
}
41+
#elif USE_XR_OCCLUSION_MESH_COMBINED_RT_ARRAY_INDEX
3342
output.rtArrayIndex = input.vertex.z;
3443
#endif
3544

0 commit comments

Comments
 (0)