렌더 패스에서 컴퓨트 셰이더를 실행할 때 버퍼를 할당하여 컴퓨트 셰이더에 대한 입력 데이터를 제공할 수 있습니다.
다음 과정을 따르십시오.
그래픽스 버퍼를 생성한 다음 패스 데이터에서 그에 대한 핸들을 추가합니다. 예시:
// 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);
ImportBuffer 렌더 그래프 API를 사용하여 버퍼를 렌더 그래프 시스템이 사용할 수 있는 핸들로 전환한 다음 패스 데이터에서 BufferHandle 필드를 설정합니다. 예시:
BufferHandle inputHandleRG = renderGraph.ImportBuffer(inputBuffer);
passData.input = inputHandleRG;
UseBuffer 메서드를 사용하여 렌더 그래프 시스템에서 버퍼를 읽을 수 있는 버퍼로 설정합니다. 예시:
builder.UseBuffer(passData.input, AccessFlags.Read);
SetRenderFunc 메서드에서 SetComputeBufferParam API를 사용하여 버퍼를 컴퓨트 셰이더에 연결합니다. 예시:
// 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라는 예시를 참고하십시오.