#if URP_7_OR_NEWER
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
#if URP_17_OR_NEWER
using UnityEngine.Rendering.RenderGraphModule;
#endif // URP_17_OR_NEWER
namespace UnityEngine.XR.Simulation
{
class SimulationCameraTextureReadbackPass : ScriptableRenderPass
{
///
/// The CameraTextureProvider class object that will execute the async readback logic.
///
CameraTextureProvider m_Provider;
#if URP_17_OR_NEWER
///
/// Data provided for the function.
///
class PassData
{
internal CameraTextureProvider cameraTextureProvider;
}
///
/// Name of our RenderGraph render pass.
///
static readonly string k_RenderGraphPassName = "SimulationCameraTextureReadbackPass (Render Graph Enabled)";
#endif // URP_17_OR_NEWER
///
/// Constructs a SimulationCameraTextureReadbackPass class instance.
///
/// The CameraTextureProvider MonoBehaviour object that
/// implements the asynchronous readback logic.
public SimulationCameraTextureReadbackPass(CameraTextureProvider cameraTextureProvider)
{
// Initialize the CameraTextureProvider object that will execute the async readback logic.
m_Provider = cameraTextureProvider;
// Specify that the async readback pass will occur after all effects are rendered.
renderPassEvent = RenderPassEvent.AfterRendering;
// Configure the camera's texture input types for both the RenderGraph and non-RenderGraph async readback
// passes.
ConfigureInput(ScriptableRenderPassInput.Color | ScriptableRenderPassInput.Depth);
}
///
/// Called by the renderer before rendering a camera. Configures the camera's texture input types, color and
/// depth, for the non-RenderGraph async readback pass.
///
/// The object to enqueue rendering commands.
/// Current rendering state information.
///
#pragma warning disable CS0672
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
ConfigureInput(ScriptableRenderPassInput.Color | ScriptableRenderPassInput.Depth);
}
///
/// Queries the object to perform Simulation camera texture async readback
/// with RenderGraph disabled.
///
/// The ScriptableRenderContext object that lets us execute the rendering commands on
/// the object for this render pass.
/// Current rendering state information. Unused for this render pass.
///
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
#pragma warning restore CS0672
{
using var commandBuffer = CommandBufferPool.Get("SimulationCameraTextureReadbackPass (Render Graph Disabled)");
if (m_Provider.TryConfigureReadbackCommandBuffer(commandBuffer))
{
context.ExecuteCommandBuffer(commandBuffer);
}
}
#if URP_17_OR_NEWER
///
/// Queries the object to perform Simulation camera texture async readback
/// with RenderGraph enabled.
///
/// The data that is passed to the function that executes this RenderGraph pass.
/// The UnsafeGraphContext object that gives us access to the native
/// object to enqueue rendering instructions for this RenderGraph pass.
///
static void ExecuteRenderGraphReadbackPass(PassData passData, UnsafeGraphContext unsafeContext)
{
var nativeCommandBuffer = CommandBufferHelpers.GetNativeCommandBuffer(unsafeContext.cmd);
passData.cameraTextureProvider.TryConfigureRenderGraphReadbackCommandBuffer(nativeCommandBuffer);
}
///
/// Add the unsafe pass to the RenderGraph object in order to perform async readback on the camera's color and/or
/// depth render textures.
///
///
/// This function needs to add an unsafe render pass to RenderGraph because a raster render pass, which is typically
/// used for rendering with RenderGraph, cannot perform the texture readback operations performed with the
/// in . Rendering Simulation camera textures is a
/// special case. Unsafe passes can do certain operations that raster render passes cannot do and have access to
/// the full command buffer API.
///
/// The RenderGraph object that we add the unsafe render pass to.
/// A ContextContainer object that is unused for this RenderGraph pass.
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
{
using (var builder = renderGraph.AddUnsafePass(
k_RenderGraphPassName,
out PassData passData,
profilingSampler))
{
passData.cameraTextureProvider = m_Provider;
builder.AllowPassCulling(false);
builder.SetRenderFunc(ExecuteRenderGraphReadbackPass);
}
}
#endif // URP_17_OR_NEWER
}
}
#endif // URP_7_OR_NEWER