Blit using the render graph system in URP
Blit from one texture to another in the render graph system with the AddBlitPass API.
Read time 3 minutesLast updated 24 days ago
To blit from one texture to another in the render graph system in the Universal Render Pipeline (URP), use the API. The API generates a render pass automatically, so you don't need to use a method like .
AddBlitPassAddRasterRenderPassFollow these steps:
-
To create a shader and material that works with a blit render pass, from the main menu select Assets > Create >> SRP Blit Shader, then create a material from it.Shader
?
To use shader 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. -
Addto your render pass script.
using UnityEngine.Rendering.RenderGraphModule.Util -
In your render pass, create a field for the blit material. For example:public class MyBlitPass : ScriptableRenderPass{ Material blitMaterial;}
-
Set up the texture to blit from and blit to. For example:TextureHandle sourceTexture = renderGraph.CreateTexture(sourceTextureProperties);TextureHandle destinationTexture = renderGraph.CreateTexture(destinationTextureProperties);For more information, refer to Get data from the current frame in URP and Create a temporary texture for a single render pass.
-
To set up the material, textures, and shader pass for the blit operation, create aobject. For example:
RenderGraphUtils.BlitMaterialParameters// Create a BlitMaterialParameters object with the blit material, source texture, destination texture, and shader pass to use.var blitParams = new RenderGraphUtils.BlitMaterialParameters(sourceTexture, destinationTexture, blitMaterial, 0); -
To add a blit pass, call themethod with the blit parameters in your render pass's
AddBlitPassmethod. For example:RecordRenderGraphrenderGraph.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 with a default material, Unity might use the 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 API.
AddBlitPassAddCopyPassAddCopyPassAvoid 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 parameter set to . The APIs then return the object that you usually receive as from a method like .
returnBuildertrueIBaseRenderGraphBuildervar builderAddRasterRenderPassFor 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
- Get the current framebuffer from GPU memory
- Render graph system
- Universal Render Pipeline (URP) package samples: refer to the example which uses AddCopyPass.Blit
?