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.
You can’t both read from and write to the same texture in a render pass. Refer to Change the render target during a render pass for more information.
To set a texture as an input for a custom render pass, follow these steps:
In the RecordRenderGraph
method, 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 example:
// Add the texture handle to the data
RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
TextureHandle textureHandle = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);
passData.textureToUse = textureHandle;
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 such as Blitter.BlitTexture
.
To set a texture as the output for commands such as Blit
, in the RecordRenderGraph
method, use the SetRenderAttachment
method. The SetRenderAttachment
method sets the texture as write-only by default.
For example, the following command creates a temporary texture and sets it as the render target for the render pass:
// Create texture properties
RenderTextureDescriptor textureProperties = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
// Create the texture
TextureHandle targetTexture = UniversalRenderer.CreateRenderGraphTexture(renderGraph, textureProperties, "My texture", false);
// Set the texture as the render target
// The second parameter is the index the shader uses to access the texture
builder.SetRenderAttachment(targetTexture, 0);
In your SetRenderFunc
method, you can now write to the texture using APIs such as Blitter.BlitTexture
.
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.
If you need to draw objects to the render target, refer to Draw objects in a render pass for additional information.
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. Refer to Use Compatibility Mode APIs in render graph render passes for more information and an example.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.
If you blitA shorthand term for “bit block transfer”. A blit operation is the process of transferring blocks of data from one place in memory to another.
See in Glossary between several textures with different properties, rendering might be slow because URP can’t merge the blits into a single native render pass. Use the AddUnSafePass
API and the SetRenderTarget()
method instead.
Refer to the following:
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.