Version: Unity 6.2 (6000.2)
Language : English
Write a render pass using the render graph system in URP
Textures in the Render Graph system in URP

Blit using the render graph system in URP

To 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
from one texture to another in the render graph system in the Universal Render PipelineA series of operations that take the contents of a Scene, and displays them on a screen. Unity lets you choose from pre-built render pipelines, or write your own. More info
See in Glossary
(URP), use the AddBlitPass API. The API generates a render pass automatically, so you don’t need to use a method like AddRasterRenderPass.

Follow these steps:

  1. To create a shader and material that works with a blit render pass, from the main menu, select Assets > Create > Shader > SRP Blit Shader, then create a material from it.

    To use shaderA program that runs on the GPU. More info
    See in Glossary
    graph instead, refer to Create a low-code custom post-processing effect.

    For more information about writing shaders in URP, refer to Writing custom shaders in URP.

  2. Add using UnityEngine.Rendering.RenderGraphModule.Util to your render pass script.

  3. In your render pass, create a field for the blit material. For example:

    public class MyBlitPass : ScriptableRenderPass
    {
        Material blitMaterial;
    }
    
  4. In the RecordRenderGraph method, set up the texture to blit from. For example, to fetch the active color texture:

    public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameContext)
    {
        UniversalResourceData frameData = frameContext.Get<UniversalResourceData>();
        TextureHandle sourceTexture = frameData.activeColorTexture;
    }    
    

    For more information, refer to Get data from the current frame in URP.

  5. Set up a texture to blit to. For example, to create a texture that matches 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
    color texture:

    var destinationProperties = renderGraph.GetTextureDesc(source);
    TextureHandle destinationTexture = renderGraph.CreateTexture(destinationProperties);
    

    For more information, refer to Create a texture in the render graph system.

  6. To set up the material, textures, and shader pass for the blit operation, create a RenderGraphUtils.BlitMaterialParameters object. For example:

    // Create a BlitMaterialParameters object with the blit material, source texture, destination texture, and shader pass to use.
    var blitParams = new RenderGraphUtils.BlitMaterialParameters(blitMaterial, sourceTexture, destinationTexture, 0);
    
  7. To add a blit pass, call the AddBlitPass method with the blit parameters. For example:

    renderGraph.AddBlitPass(blitParams, "Pass created with AddBlitPass");
    

For a complete example, refer to the example called BlitWithMaterial in the render graph examples of the Universal Render Pipeline (URP) package samples.

If you use AddBlitPass with a default material, Unity might use the AddCopyPass API instead, to optimize the render pass so it accesses the framebuffer from the on-chip memory of the GPU instead of video memory. This process is sometimes called framebuffer fetch. For more information, refer to AddCopyPass API.

Avoid blitting back

After a blit, you usually blit the destination texture back to the active color texture. However in the render graph system, you can update the frame data to point to the destination texture instead, so you only blit once. For example:

// Set the camera color as the destination texture you blitted to.
frameData.cameraColor = destinationTexture;

For more information, refer to Avoid blitting to and from the color buffer.

Customize the render pass

To customize the render pass that the methods generate, for example to change settings or add more resources, call the APIs with the returnBuilder parameter set to true. The APIs then return the IBaseRenderGraphBuilder object that you usually receive as var builder from a method like AddRasterRenderPass.

For example:

using (var builder = renderGraph.AddBlitPass(blitParams, "Pass created with AddBlitPass", returnBuilder: true))
{
    // Use the builder variable to customize the render pass here.
}

Additional resources

Write a render pass using the render graph system in URP
Textures in the Render Graph system in URP