This page describes how to write a render pass using the render graph system.
To illustrate the description, the page uses the example render pass that copies the cameraA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary’s active color texture to a destination texture. To simplify the code, this example does not use the destination texture elsewhere in the frame. You can use the frame debugger to inspect its contents.
Declare a render pass as a class that inherits from the ScriptableRenderPass class.
Inside the render pass, declare a class that contains the resources that the render pass uses.
The resources can be regular C# variables and render graph resource references. The render graph system can access this data structure during the rendering code execution. Ensure that you declare only the variables that the render pass uses. Adding unnecessary variables can reduce performance.
class PassData { internal TextureHandle copySourceTexture; }
The RecordRenderGraph method populates the data and the render graph passes it as a parameter to the rendering function.
Use the RecordRenderGraph method to add and configure one or more render passes in the render graph system.
Unity calls this method during the render graph configuration step and lets you register relevant passes and resources for the render graph execution. Use this method to implement custom rendering.
In the RecordRenderGraph method you declare render pass inputs and outputs, but do not add commands to command buffers.
The following section describes the main elements of the RecordRenderGraph method and provides an example implementation.
The builder
variable is an instance of the IRasterRenderGraphBuilder
interface. This variable is the entry point for configuring the information related to the render pass.
The UniversalResourceData class contains all the texture resources used by URP, including the active color and depth textures of the camera.
The UniversalCameraData class contains the data related to the currently active camera.
For demonstrative purposes, this sample creates a temporary destination texture. UniversalRenderer.CreateRenderGraphTexture
is a helper method that calls the RenderGraph.CreateTexture
method.
TextureHandle destination = UniversalRenderer.CreateRenderGraphTexture(renderGraph, desc, "CopyTexture", false);
The builder.UseTexture
method declares that this render pass uses the source texture as a read-only input:
builder.UseTexture(passData.copySourceTexture);
In this example, the builder.SetRenderAttachment
method declares that this render pass uses the temporary destination texture as its color render target. This declaration is similar to the cmd.SetRenderTarget
API which you can use in the Compatibility mode (without the render graph API).
Declare a rendering function that generates the rendering commands for the render pass.
static void ExecutePass(PassData data, RasterGraphContext context) { // Records a rendering command to copy, or blit, the contents of the source texture // to the color render target of the render pass. Blitter.BlitTexture(context.cmd, data.copySourceTexture, new Vector4(1, 1, 0, 0), 0, false); }
The SetRenderFunc
method in the RecordRenderGraph
method sets the ExecutePass
method as the rendering function that render graph calls when executing the render pass. This sample uses a lambda expression to avoid memory allocations.
builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context));
The complete example of the RecordRenderGraph method:
// This method adds and configures one or more render passes in the render graph. // This process includes declaring their inputs and outputs, // but does not include adding commands to command buffers. public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData) { string passName = "Copy To Debug Texture"; // Add a raster render pass to the render graph. The PassData type parameter determines // the type of the passData output variable. using (var builder = renderGraph.AddRasterRenderPass<PassData>(passName, out var passData)) { // UniversalResourceData contains all the texture references used by URP, // including the active color and depth textures of the camera. UniversalResourceData resourceData = frameData.Get<UniversalResourceData>(); // Populate passData with the data needed by the rendering function // of the render pass. // Use the camera's active color texture // as the source texture for the copy operation. passData.copySourceTexture = resourceData.activeColorTexture; // Create a destination texture for the copy operation based on the settings, // such as dimensions, of the textures that the camera uses. // Set msaaSamples to 1 to get a non-multisampled destination texture. // Set depthBufferBits to 0 to ensure that the CreateRenderGraphTexture method // creates a color texture and not a depth texture. UniversalCameraData cameraData = frameData.Get<UniversalCameraData>(); RenderTextureDescriptor desc = cameraData.cameraTargetDescriptor; desc.msaaSamples = 1; desc.depthBufferBits = 0; // For demonstrative purposes, this sample creates a temporary destination texture. // UniversalRenderer.CreateRenderGraphTexture is a helper method // that calls the RenderGraph.CreateTexture method. // Using a RenderTextureDescriptor instance instead of a TextureDesc instance // simplifies your code. TextureHandle destination = UniversalRenderer.CreateRenderGraphTexture(renderGraph, desc, "CopyTexture", false); // Declare that this render pass uses the source texture as a read-only input. builder.UseTexture(passData.copySourceTexture); // Declare that this render pass uses the temporary destination texture // as its color render target. // This is similar to cmd.SetRenderTarget prior to the RenderGraph API. builder.SetRenderAttachment(destination, 0); // RenderGraph automatically determines that it can remove this render pass // because its results, which are stored in the temporary destination texture, // are not used by other passes. // For demonstrative purposes, this sample turns off this behavior to make sure // that render graph executes the render pass. builder.AllowPassCulling(false); // Set the ExecutePass method as the rendering function that render graph calls // for the render pass. // This sample uses a lambda expression to avoid memory allocations. builder.SetRenderFunc((PassData data, RasterGraphContext context) => ExecutePass(data, context)); } }
To inject the scriptable render pass instance into the renderer, use the AddRenderPasses
method from a Renderer Feature implementation. URP calls the AddRenderPasses
method every frame, once for each Camera.
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) { renderer.EnqueuePass(m_CopyRenderPass); }
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.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.