You can use the render graph system API to set a texture as an input or output for a custom render pass, so you can read from or write to it.
Note: To copy between textures with less code, use the AddBlitPass method instead. For more information, refer to Blit using the render graph system.
To set a texture as an input for a custom render pass, follow these steps:
In your pass data, add a texture handle field to the data your pass uses.
For example:
// Create the data your pass uses
public class MyPassData
{
// Add a texture handle
public TextureHandle textureToUse;
}
Set the texture handle to the texture you want to use. For more information, refer to Create a temporary texture for a single frame or Import a texture into the render graph system.
Call the UseTexture method to set the texture as an input.
For example:
builder.UseTexture(passData.textureToUse, AccessFlags.Read);
In your SetRenderFunc method, you can now use the TextureHandle object in the pass data as an input for APIs.
To set a texture as the render target, in the RecordRenderGraph method, use the SetRenderAttachment method. The SetRenderAttachment method sets the texture as write-only by default.
builder.SetRenderAttachment(textureHandle, 0);
You don’t need to add the texture to your pass data. The render graph system sets up the texture for you automatically before it executes the render pass.
You can’t use UseTexture and SetRenderAttachment on the same texture in an AddRasterRenderPass render pass. Refer to Change the render target during a render pass for more information.
If you need to draw objects to the render target, refer to Draw objects in a render pass.
You can’t change which texture URP writes to during a render graph system render pass.
You can do either of the following instead:
builder.SetRenderAttachment during the second render pass to change the render target.UnsafePass API so you can use the SetRenderTarget API in the SetRenderFunc method. For more information, refer to Use the CommandBuffer interface in a render graph.You can use these methods to read from and write to the same texture, by first copying from the texture to a temporary texture you create, then copying back.
AddRasterRenderPass can only merge render passes into a single native render pass if the textures you use have very similar properties. If you use several textures with different properties, use the AddUnsafePass API and the SetRenderTarget() method instead. Rendering might be slower because there will be more render passes.
Refer to the following: