When you run a compute shader in a render pass, you can allocate a buffer to provide input data for the compute shaderA program that runs on the GPU. More info
See in Glossary.
Follow these steps:
Create a graphics buffer, then add a handle to it in your pass data. For example:
// Declare an input buffer
public GraphicsBuffer inputBuffer;
// Add a handle to the input buffer in your pass data
class PassData
{
...
public BufferHandle input;
}
// Create the buffer in the render pass constructor
public ComputePass(ComputeShader computeShader)
{
// Create the input buffer as a structured buffer
// Create the buffer with a length of 5 integers, so you can input 5 values.
inputBuffer = new GraphicsBuffer(GraphicsBuffer.Target.Structured, 5, sizeof(int));
}
Set the data in the buffer. For example:
var inputValues = new List<int> { 1, 2, 3, 4, 5 };
inputBuffer.SetData(inputValues);
Use the ImportBuffer
render graph API to convert the buffer to a handle the render graph system can use, then set the BufferHandle
field in the pass data. For example:
BufferHandle inputHandleRG = renderGraph.ImportBuffer(inputBuffer);
passData.input = inputHandleRG;
Use the UseBuffer
method to set the buffer as a readable buffer in the render graph system. For example:
builder.UseBuffer(passData.input, AccessFlags.Read);
In your SetRenderFunc
method, use the SetComputeBufferParam
API to attach the buffer to the compute shader. For example:
// The first parameter is the compute shader
// The second parameter is the function that uses the buffer
// The third parameter is the RWStructuredBuffer input variable to attach the buffer to
// The fourth parameter is the handle to the input buffer
context.cmd.SetComputeBufferParam(passData.computeShader, passData.computeShader.FindKernel("Main"), "inputData", passData.input);
For a full example, refer to the example called Compute in the Universal Render Pipeline (URP) package samples.
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.