Get the current framebuffer from GPU memory in URP
To speed up rendering, use the SetInputAttachment API to read the frame that Unity has rendered so far.
Read time 3 minutesLast updated 21 days ago
To more efficiently read a frame that Unity rendered in a previous pass, use the API to set the output of a render pass as the input of the next render pass.
SetInputAttachmentUsing means the render pass can access the framebuffer from the on-chip memory of the GPU, instead of video memory. This speeds up rendering, because the GPU doesn't need to copy the texture to and from its video memory. This process is sometimes called framebuffer fetch.
SetInputAttachmentOn mobile devices, rendering speed improves on mobile devices that use tile-based deferred rendering (TBDR). The GPU keeps the frame in its on-chip tile memory between render passes, using less memory bandwidth and processing time.
If you use the API, URP merges the render passes that write to the framebuffer and read from it. You can check this in the Render Graph Viewer window.
SetInputAttachmentYou can also use to read other render targets, for example a G-buffer.
SetInputAttachmentRequirements
The API is supported if you use one of the following graphics APIs:
SetInputAttachment- Vulkan
- Metal
If you use with an unsupported API, Unity copies the texture to and from video memory, so there's no effect on rendering speed.
SetInputAttachmentUse the SetInputAttachment
API
SetInputAttachmentTo use the API in a render pass, use the following:
SetInputAttachment- The API to set the output of the previous render pass as the input of the new render pass.
SetInputAttachment - The macro in your fragment shader code to read the pixel from the previous render pass, instead of a sampling macro such as
LOAD_FRAMEBUFFER_X_INPUT.SAMPLE_TEXTURE2D
The following steps assume you already have one render pass that writes to a called .
TextureHandlesourceTextureHandle-
Create a custom shader, then create a material from the shader. For more information about creating a custom shader, refer to Writing custom shaders.
-
Inside the HLSLPROGRAM of your custom shader, make sure you import thefile:
Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlslHLSLPROGRAM...#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"...ENDHLSL -
Inside the HLSLPROGRAM, use one of the following to declare the texture from the previous render pass. For example:
FRAMEBUFFER_INPUT_X_HALFFRAMEBUFFER_INPUT_X_FLOATFRAMEBUFFER_INPUT_X_INTFRAMEBUFFER_INPUT_X_UINT
For example:FRAMEBUFFER_INPUT_X_HALF(0); -
In the fragment function, useto sample the texture from on-chip GPU memory. For example:
LOAD_FRAMEBUFFER_X_INPUThalf4 frag() : SV_Target{ half4 colorFromPreviousRenderPass = LOAD_FRAMEBUFFER_X_INPUT(0, input.positionCS.xy); return colorFromPreviousRenderPass;} -
In a new render graph render pass, add the material you created to your. For example:
PassDataclass PassData{ public Material exampleMaterial;} -
Useto set the output of the previous render pass as the input of the new render pass. For example:
builder.SetInputAttachmentbuilder.SetInputAttachment(sourceTextureHandle, 0, AccessFlags.Read); -
In yourmethod, use a command such as
SetRenderFuncto render using the material. For example:BlitTexturestatic void ExecutePass(PassData data, RasterGraphContext context){ Blitter.BlitTexture(context.cmd, new Vector4(1, 1, 0, 0), exampleMaterial, 1);}
Example
For a full example, refer to the example called FrameBufferFetch in the render graph system URP package samples.