レンダーパスでコンピュートシェーダーを実行 する場合、コンピュートシェーダーの入力データを提供するバッファを割り当てることができます。
以下の手順に従ってください。
グラフィックスバッファを作成し、そのハンドルをパスデータに追加します。例:
// 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 という例を参照してください。