#if ENABLE_VR && ENABLE_XR_MODULE
using System;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Experimental.Rendering;
namespace UnityEngine.Rendering.Universal
{
///
/// Draw the XR occlusion mesh into the current depth buffer when XR is enabled.
///
public class XROcclusionMeshPass : ScriptableRenderPass
{
PassData m_PassData;
///
/// Used to indicate if the active target of the pass is the back buffer
///
public bool m_IsActiveTargetBackBuffer; // TODO: Remove this when we remove non-RG path
public XROcclusionMeshPass(RenderPassEvent evt)
{
profilingSampler = new ProfilingSampler("Draw XR Occlusion Mesh");
renderPassEvent = evt;
m_PassData = new PassData();
m_IsActiveTargetBackBuffer = false;
}
private static void ExecutePass(RasterCommandBuffer cmd, PassData data)
{
if (data.xr.hasValidOcclusionMesh)
{
if (data.isActiveTargetBackBuffer)
cmd.SetViewport(data.xr.GetViewport());
data.xr.RenderOcclusionMesh(cmd, renderIntoTexture: !data.isActiveTargetBackBuffer);
}
}
///
[Obsolete(DeprecationMessage.CompatibilityScriptingAPIObsolete, false)]
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
m_PassData.xr = renderingData.cameraData.xr;
m_PassData.isActiveTargetBackBuffer = m_IsActiveTargetBackBuffer;
ExecutePass(CommandBufferHelpers.GetRasterCommandBuffer(renderingData.commandBuffer), m_PassData);
}
private class PassData
{
internal XRPass xr;
internal TextureHandle cameraColorAttachment;
internal TextureHandle cameraDepthAttachment;
internal bool isActiveTargetBackBuffer;
}
internal void Render(RenderGraph renderGraph, ContextContainer frameData, in TextureHandle cameraColorAttachment, in TextureHandle cameraDepthAttachment)
{
UniversalCameraData cameraData = frameData.Get();
UniversalResourceData resourceData = frameData.Get();
using (var builder = renderGraph.AddRasterRenderPass(passName, out var passData, profilingSampler))
{
passData.xr = cameraData.xr;
passData.cameraColorAttachment = cameraColorAttachment;
builder.SetRenderAttachment(cameraColorAttachment, 0);
passData.cameraDepthAttachment = cameraDepthAttachment;
builder.SetRenderAttachmentDepth(cameraDepthAttachment, AccessFlags.Write);
passData.isActiveTargetBackBuffer = resourceData.isActiveTargetBackBuffer;
// TODO RENDERGRAPH: culling? force culling off for testing
builder.AllowPassCulling(false);
builder.AllowGlobalStateModification(true);
if (cameraData.xr.enabled)
{
bool passSupportsFoveation = cameraData.xrUniversal.canFoveateIntermediatePasses || resourceData.isActiveTargetBackBuffer;
builder.EnableFoveatedRasterization(cameraData.xr.supportsFoveatedRendering && passSupportsFoveation);
}
builder.SetRenderFunc((PassData data, RasterGraphContext context) =>
{
ExecutePass(context.cmd, data);
});
return;
}
}
}
}
#endif