# Sandbox.GpuBuffer

A GPU data buffer intended for use with a [Sandbox.ComputeShader](/api/Sandbox.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 [Sandbox.GpuBuffer.UsageFlags](/api/Sandbox.GpuBuffer.UsageFlags).
Using the default [Sandbox.GpuBuffer.UsageFlags.Structured](/api/Sandbox.GpuBuffer.UsageFlags/Structured) type buffers map to StructuredBuffer<T> and RWStructuredBuffer<T> in HLSL.

## Examples

This example shows how to use the GpuBuffer class to send data to a compute shader:

```csharp
struct MyData
{
    public float Value;
}

// Allocate the GPU buffer
using (var buffer = new GpuBuffer<MyData>( 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:

```csharp
struct MyData
{
    public float Value;
}

using (var buffer = new GpuBuffer<MyData>( 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 );
}
```

- Kind: class
- Namespace: `Sandbox`
- Assembly: `Sandbox.Engine`

## Constructors

- [`GpuBuffer`](/api/Sandbox.GpuBuffer/.ctor): Creates a new GPU buffer with a specified number of elements and a specific buffer type.

## Properties

- [`ElementCount`](/api/Sandbox.GpuBuffer/ElementCount): Number of elements in the buffer.
- [`ElementSize`](/api/Sandbox.GpuBuffer/ElementSize): Size of a single element in the buffer.
- [`IsValid`](/api/Sandbox.GpuBuffer/IsValid)
- [`Usage`](/api/Sandbox.GpuBuffer/Usage): What sort of buffer this is

## Methods

- [`Clear`](/api/Sandbox.GpuBuffer/Clear): Fills the entire buffer with a repeated uint32 value. Uses the native GPU fill command (vkCmdFillBuffer) — no CPU-side allocation needed.
- [`CopyStructureCount`](/api/Sandbox.GpuBuffer/CopyStructureCount): For [Sandbox.GpuBuffer.UsageFlags.Append](/api/Sandbox.GpuBuffer.UsageFlags/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.
- [`Dispose`](/api/Sandbox.GpuBuffer/Dispose): Destroys the GPU buffer, don't use it no more
- [`GetData`](/api/Sandbox.GpuBuffer/GetData)
- [`GetDataAsync`](/api/Sandbox.GpuBuffer/GetDataAsync)
- [`SetCounterValue`](/api/Sandbox.GpuBuffer/SetCounterValue): Sets the counter value for [Sandbox.GpuBuffer.UsageFlags.Append](/api/Sandbox.GpuBuffer.UsageFlags/Append) or [Sandbox.GpuBuffer.UsageFlags.Counter](/api/Sandbox.GpuBuffer.UsageFlags/Counter) structured buffers.
- [`SetData`](/api/Sandbox.GpuBuffer/SetData)
