Api Sandbox GpuBuffer
class

GpuBuffer

public class GpuBuffer

A GPU data buffer intended for use with a ComputeShader. You can read and write arbitrary data to and from the CPU and GPU. This allows for efficient parallel data processing on the GPU. Different GPU buffer types can be used depending on the provided UsageFlags. Using the default Structured type buffers map to StructuredBuffer<T> and RWStructuredBuffer<T> in HLSL.

Constructors

GpuBuffer(Int32 elementCount, Int32 elementSize, UsageFlags flags, String debugName)

Creates a new GPU buffer with a specified number of elements and a specific buffer type.

elementCount — The total number of elements that the GpuBuffer can hold. This represents the buffer's size in terms of elements, not bytes.
elementSize — The total number of elements that the GpuBuffer can hold. This represents the buffer's size in terms of elements, not bytes.
flags — Defines the usage pattern of the GPU buffer. This can affect performance depending on how the buffer is utilized.
debugName — Test

Properties

Name Type Description
ElementCount Int32 Number of elements in the buffer.
ElementSize Int32 Size of a single element in the buffer.
Usage UsageFlags What sort of buffer this is
IsValid virtual Boolean

Methods

virtual Void Dispose()

Destroys the GPU buffer, don't use it no more

Void GetData(Span<T> data)
Void GetData(Span<T> data, Int32 start, Int32 count)
Void GetDataAsync(ReadOnlySpan<T>> callback)
Void GetDataAsync(ReadOnlySpan<T>> callback, Int32 start, Int32 count)
Void SetData(Span<T> data, Int32 elementOffset = 0)
Void SetData(List<T> data, Int32 elementOffset = 0)
Void CopyStructureCount(GpuBuffer destBuffer, Int32 destBufferOffset = 0)

For Append buffers there is a hidden uint 32-bit atomic counter in the buffer that contains the number of writes to the buffer after invocation of the compute shader. In order to get the value of the counter, the data needs to be copied to another GPU buffer that can be used.

Void Clear(UInt32 value = 0)

Fills the entire buffer with a repeated uint32 value. Uses the native GPU fill command (vkCmdFillBuffer) — no CPU-side allocation needed.

value — The uint32 value to fill with. Defaults to zero.
Void SetCounterValue(UInt32 counterValue)

Sets the counter value for Append or Counter structured buffers.

Examples

This example shows how to use the GpuBuffer class to send data to a compute shader:

struct MyData
{
    public float Value;
}

// Allocate the GPU buffer
using (var buffer = new GpuBuffer&lt;MyData&gt;( 2 ))
{
	// Upload data to the GPU buffer
	var data = new MyData[] { new MyData { Value = 1.0f }, new MyData { Value = 2.0f } };
	buffer.SetData( data );

    // Pass the buffer to a compute shader
    ComputeShader.Attributes.Set( "myData", buffer );
    
    // Dispatch the shader
    ComputeShader.Dispatch();
}
This example shows how to retrieve data from a GPU using the GpuBuffer class:

struct MyData
{
    public float Value;
}

using (var buffer = new GpuBuffer&lt;MyData&gt;( 8 ))
{
    // Pass the buffer to a compute shader
    ComputeShader.Attributes.Set( "myData", buffer );
    
    // Dispatch the shader
    ComputeShader.Dispatch();
    
	// Retrieve the data from the GPU
	var data = new MyData[ 8 ];
	buffer.GetData( data, 0, 8 );
}

See Also

ComputeShader GpuBuffer@)
Assembly: Sandbox.Engine Namespace: Sandbox Full Name: Sandbox.GpuBuffer