Version: Unity 6.0 (6000.0)
言語 : 日本語
URP のレンダーパス内でのコンピュートシェーダーの実行
URP でのレンダーグラフの分析

URP でのコンピュートシェーダーの入力データの作成

レンダーパスでコンピュートシェーダーを実行 する場合、コンピュートシェーダーの入力データを提供するバッファを割り当てることができます。

以下の手順に従ってください。

  1. グラフィックスバッファを作成し、そのハンドルをパスデータに追加します。例:

    // 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));
    }
    
  2. バッファにデータを設定します。例:

    var inputValues = new List<int> { 1, 2, 3, 4, 5 };
    inputBuffer.SetData(inputValues);
    
  3. ImportBuffer レンダーグラフ API を使用してバッファをレンダーグラフシステムが使用できるハンドルに変換し、パスデータの BufferHandle フィールドを設定します。例:

    BufferHandle inputHandleRG = renderGraph.ImportBuffer(inputBuffer);
    passData.input = inputHandleRG;
    
  4. UseBuffer メソッドを使用して、バッファをレンダーグラフシステムで読み取り可能なバッファとして設定します。例:

    builder.UseBuffer(passData.input, AccessFlags.Read);
    
  5. 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 という例を参照してください。

追加リソース

URP のレンダーパス内でのコンピュートシェーダーの実行
URP でのレンダーグラフの分析