MSDN Library

Documentation for Microsoft Products

Compute Shader Structures

This section details the structures used within DirectCompute shaders, providing the building blocks for data manipulation and configuration.

Buffer Structures

These structures are commonly used to define and interact with various types of buffers accessible by compute shaders.

StructuredBuffer<T>

Represents a buffer of elements of type T. The elements are accessible by index.

T& operator[](uint index) const; Access an element by its index.
uint get_num_elements() const; Returns the total number of elements in the buffer.

AppendConsumeBuffer<T>

A specialized buffer that allows for append and consume operations. Useful for dynamic data generation.

void Append(T value); Appends a value to the end of the buffer.
T Consume(); Consumes and returns the next available value from the buffer.
uint get_num_append_elements() const; Returns the number of elements that have been appended.

ByteAddressBuffer

A buffer accessible by byte address. Allows for arbitrary data access.

uint32 Read(uint offset) const; Reads a 32-bit unsigned integer from the specified byte offset.
uint2 Read2(uint offset) const; Reads two 32-bit unsigned integers (a uint2) from the specified byte offset.
uint4 Read4(uint offset) const; Reads four 32-bit unsigned integers (a uint4) from the specified byte offset.
void Write(uint offset, uint32 value); Writes a 32-bit unsigned integer to the specified byte offset.
void Write(uint offset, uint2 value); Writes a uint2 to the specified byte offset.

Shader Resource Views (SRVs) and Unordered Access Views (UAVs)

While not strictly structures defined within HLSL, the concepts of SRVs and UAVs are fundamental to how buffers and textures are accessed by compute shaders. They are typically bound to specific shader registers.

Common SRV Structures (Implicitly Bound)

Common UAV Structures (Implicitly Bound)

System-Defined Structures

These structures provide access to hardware and dispatch information within the shader.

Input data for Thread Group Shared Variables

While not a direct structure, shared memory is allocated and accessed using specific syntax.


[numthreads(X, Y, Z)]
void CSMain(uint3 dispatchThreadID : SV_DispatchThreadID)
{
    // Shared memory is declared using static arrays
    static float shared_data[1024];

    // ... shader logic ...
}
                

SV_DispatchThreadID

A 3-component unsigned integer vector that identifies the current thread's dispatch ID.

Type: uint3

SV_GroupThreadID

A 3-component unsigned integer vector that identifies the current thread's ID within its thread group.

Type: uint3

SV_ThreadGroupID

A 3-component unsigned integer vector that identifies the current thread group's ID within the dispatch.

Type: uint3

SV_ThreadID

Deprecated. Use SV_DispatchThreadID instead.

SV_GroupSize

A 3-component unsigned integer vector that indicates the size of the thread group (defined by the [numthreads] attribute).

Type: uint3

SV_DispatchSize

A 3-component unsigned integer vector that indicates the total number of thread groups in the dispatch.

Type: uint3

SV_IsFrontFace

A boolean indicating if the current fragment is on the front face of a primitive. Primarily used in pixel shaders, but may be available in certain compute contexts.

Type: bool

Further Reading