⚠ Preview docs — built from the develop branch and may be unstable. Go to the stable docs.

class ComputeBuffer

A GPU storage buffer for shuttling data to and from compute shaders! In HLSL, this maps to StructuredBuffer or RWStructuredBuffer depending on the ComputeBufferType.

Your struct T needs to be unmanaged (no reference types!) and its layout must match the HLSL struct. Watch out for padding differences between C# and HLSL!

Instance Fields and Properties

   
int Count The capacity of the buffer, in number of T elements. This is the max you can Set or Get without clamping.
string Id Gets or sets the unique identifier of this asset resource! This can be helpful for debugging, managing your assets, or finding them later on!
int Stride Size of a single element in bytes, derived from your T struct. Handy for sanity-checking that your C# struct matches the HLSL side!

Instance Methods

   
ComputeBuffer Creates a GPU storage buffer with room for elementCount elements, initially uninitialized! The contents will be whatever was in GPU memory before, so make sure to write before you read.
GetData Read the full buffer back from the GPU! This blocks until the data is ready, and allocates a new array each call. For per-frame readbacks, prefer the GetData(ref T[]) overload to avoid GC pressure!
SetData Upload data from the CPU to the GPU! If data.Length exceeds the buffer’s capacity, it will be clamped with a warning.




Found an issue with these docs, or have some additional questions? Create an Issue on Github!