包含此页的版本:
不含此页的版本:
在呈现过程中运行计算着色器时,可以分配缓冲区来为计算提供输入数据着色器在 GPU 上运行的程序。更多信息
请参阅术语表.
按着这些次序:
创建一个图形缓冲区,然后在通道数据中为其添加句柄。例如:
// 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));
}
在缓冲区中设置数据。例如:
var inputValues = new List<int> { 1, 2, 3, 4, 5 };
inputBuffer.SetData(inputValues);
使用ImportBufferrender graph API 将缓冲区转换为渲染图系统可以使用的句柄,然后将BufferHandle字段。例如:
BufferHandle inputHandleRG = renderGraph.ImportBuffer(inputBuffer);
passData.input = inputHandleRG;
使用UseBuffer将缓冲区设置为渲染图系统中的可读缓冲区的方法。例如:
builder.UseBuffer(passData.input, AccessFlags.Read);
在您的SetRenderFunc方法,使用SetComputeBufferParamAPI 将缓冲区附加到计算着色器。例如:
// 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);
有关完整示例,请参阅通用渲染管线(URP)包示例中名为 计算(Compute) 的示例。