Summary

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 GpuBuffer.UsageFlags. Using the default 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:
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:
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 );
}

Constructors

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

Properties

ElementCount Number of elements in the buffer.
ElementSize Size of a single element in the buffer.
IsValid
Usage What sort of buffer this is

Methods

CopyStructureCount For 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 Destroys the GPU buffer, don't use it no more
GetData
SetCounterValue Sets the counter value for Sandbox.GpuBuffer.UsageFlags.Append or Sandbox.GpuBuffer.UsageFlags.Counter structured buffers.
SetData
people
Log in to reply
You can't reply if you're not logged in. That would be crazy.