Use the CommandBuffer interface in a render graph in SRP Core
You can use the render graph AddUnsafePass API to use CommandBuffer interface APIs such as SetRenderTarget in render graph system render passes.
The AddUnsafePass API gives access to the UnsafeCommandBuffer, which gives access to more CommandBuffer functions than the RasterCommandBuffer API.
Note: Both AddUnsafePass and RasterCommandBuffer restrict access to certain commandBuffer functions so that RenderGraph can better optimize the frame. The most important function UnsafeCommandbuffer gives you access to is SetRenderTarget.
If you use the AddUnsafePass API, the following applies:
- You can't use the
SetRenderAttachmentmethod in theRecordRenderGraphmethod. UseSetRenderTargetin theSetRenderFuncmethod instead. - Rendering might be slower because the render graph system can't optimize the render pass. For example, if your render pass writes to the active color buffer, the render graph system can't detect if a later render pass writes to the same buffer. As a result, the render graph system can't merge the two render passes, and the GPU unnecessarily transfers the buffer in and out of memory.
Create an unsafe render pass
To create an unsafe render pass, follow these steps:
In your
RecordRenderGraphmethod, use theAddUnsafePassmethod instead of theAddRasterRenderPassmethod.For example:
using (var builder = renderGraph.AddUnsafePass<PassData>("My unsafe render pass", out var passData))When you call the
SetRenderFuncmethod, use theUnsafeGraphContexttype instead ofRasterGraphContext.For example:
builder.SetRenderFunc( static (PassData passData, UnsafeGraphContext context) => ExecutePass(passData, context) );If your render pass writes to a texture, add the texture as a field in your pass data class.
For example:
private class PassData { internal TextureHandle textureToWriteTo; }If your render pass writes to a texture, set the texture as writeable using the
UseTexturemethod.For example:
builder.UseTexture(passData.textureToWriteTo, AccessFlags.Write);
You can now use CommandBuffer interface APIs in your SetRenderFunc method. For example:
static void ExecutePass(PassData passData, UnsafeGraphContext context)
{
// Add a command to set the render target to the active color buffer so render graph draws to it
context.cmd.SetRenderTarget(passData.activeColorBuffer);
}