Version: 2020.2

ComputeBuffer

class in UnityEngine

切换到手册

描述

GPU 数据缓冲区,主要与计算着色器结合使用。

ComputeShader 程序通常需要向内存缓冲区读写任意数据。 ComputeBuffer 类的作用正在于此 - 您可以从脚本代码创建和填充计算缓冲区, 并在计算着色器或常规着色器中使用计算缓冲区。

在计算着色器中始终支持计算缓冲区。可使用 SystemInfo.supportsComputeShaders 运行时查询计算着色器支持情况。请参阅计算着色器手册页,了解有关支持计算着色器的平台的更多信息。在常规图形着色器中,至少需要 shader model 4.5 才能支持计算缓冲区。

For a ComputeBuffer that uses a counter, Metal and Vulkan platforms don't have native counters and use separate small buffers that act as counters internally. These small buffers are bound separately from the ComputeBuffer and count towards the limit of possible buffers bound (31 for Metal, based on the device for Vulkan).

在着色器方面,具有默认 ComputeBufferType 的 ComputeBuffers 映射到 HLSL 中的 StructuredBuffer<T>RWStructuredBuffer<T>

另请参阅:ComputeShader 类、Shader.SetGlobalBufferMaterial.SetBuffer计算着色器概述。

变量

count缓冲区中元素的数量(只读)。
nameComputeBuffer 的调试标签。凡受支持的位置,此名称都会显示在分析和帧调试器工具中。
stride缓冲区中一个元素的大小(只读)。

构造函数

ComputeBuffer创建计算缓冲区。

公共函数

BeginWriteBegins a write operation to the buffer
EndWriteEnds a write operation to the buffer
GetData将该缓冲区中的数据值读取到数组中。该数组只能使用 blittable 类型。
GetNativeBufferPtr获取指向该缓冲区的原生(底层图形 API)指针。
IsValid如果该计算缓冲区有效,则返回 true,否则返回 false。
Release释放计算缓冲区。
SetCounterValue设置附加/使用缓冲区的计数器值。
SetData使用数组中的值设置该缓冲区。

静态函数

CopyCount将附加/使用缓冲区的计数器值复制到另一个缓冲区。