Write to and read output data from a compute shader in URP
Create a render pass that writes to and reads output data from a compute shader.
読み終わるまでの所要時間 3 分最終更新 1ヶ月前
To create a render pass that writes to and reads output data from a compute shader, do the following:
- Set up the render pass to use a compute shader.
- Add an output buffer.
- Pass in and execute the compute shader.
- Get the output data from the output buffer.
To check if a platform supports compute shaders, use the API
SystemInfo.supportsComputeShadersSet up the render pass to use a compute shader
When you create a , do the following:
ScriptableRenderPass- Use instead of
AddComputePass.AddRasterRenderPass - Use instead of
ComputeGraphContext.RasterGraphContext
For example:
class ComputePass : ScriptableRenderPass{ ... public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer contextData) { ... // Use AddComputePass instead of AddRasterRenderPass. using (var builder = renderGraph.AddComputePass("MyComputePass", out PassData data)) { ... // Use ComputeGraphContext instead of RasterGraphContext. builder.SetRenderFunc(static (PassData data, ComputeGraphContext context) => ExecutePass(data, context)); ... } }}
Add an output buffer
To create a buffer the compute shader outputs to, follow these steps:
-
Inside, create a graphics buffer using
RecordRenderGraphand keep the handle. For example:CreateBufferpublic override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData){ // Create the output buffer as a structured buffer of 20 ints BufferDesc desc = new BufferDesc { name = "OutputBuffer", count = 20, stride = sizeof(int), target = GraphicsBuffer.Target.Structured }; m_OutputBufferHandle = renderGraph.CreateBuffer(desc); // Add the compute pass using (var builder = renderGraph.AddComputePass("ComputePass", out PassData passData)) { // Set the pass data passData.output = m_OutputBufferHandle; }} -
Use themethod to declare the buffer as an output of this pass in the render graph system. For example:
UseBufferbuilder.UseBuffer(passData.output, AccessFlags.Write);
Pass in and execute the compute shader
Follow these steps:
-
Pass the compute shader to the render pass. For example, in aclass, expose a
ScriptableRendererFeatureproperty, then pass the compute shader into the render pass class.ComputeShader -
Add afield to your pass data, and set it to the compute shader. For example:
ComputeShader// Add a `ComputeShader` field to your pass dataclass PassData{ ... public ComputeShader computeShader;}// Set the `ComputeShader` field to the compute shaderpassData.computeShader = yourComputeShader; -
In yourmethod, use the
SetRenderFuncAPI to attach the buffer to the compute shader. For example:SetComputeBufferParam// The first parameter is the compute shader// The second parameter is the function that uses the buffer// The third parameter is the StructuredBuffer output variable to attach the buffer to// The fourth parameter is the handle to the output buffercontext.commandBuffer.SetComputeBufferParam(passData.computeShader, passData.computeShader.FindKernel("Main"), "outputData", passData.output); -
Use theAPI to execute the compute shader.
DispatchComputecontext.commandBuffer.DispatchCompute(passData.computeShader, passData.computeShader.FindKernel("Main"), 1, 1, 1);
Get the output data from the output buffer
To fetch the data from the output buffer, add a pass to read the data back from the GPU to the CPU using the API.
RequestAsyncReadbackYou can only fetch the data after the render pass executes and the compute shader finishes running.
For example:
-
Create data for the pass that will read the buffer back.// Data container for the pass that reads back from the GPUclass ReadbackPassData{ // The buffer to read back from public BufferHandle processedDataBuffer;}
-
Provide the output buffer handle to the pass.passData.processedDataBuffer = m_OutputBufferHandle;
-
Give the pass read access to the buffer.// Declare read access to the bufferbuilder.UseBuffer(passData.processedDataBuffer, AccessFlags.Read);
-
Request asynchronous readback with theAPI.
RequestAsyncReadbackcontext.commandBuffer.RequestAsyncReadback(passData.processedDataBuffer, (AsyncGPUReadbackRequest request) =>{ // Get the data int result = request.GetData<int>();})
Example
For a full example, refer to the example called Compute in the render graph system URP package samples.