DirectCompute Resources

DirectCompute enables developers to leverage the parallel processing power of the GPU for general-purpose computation. This section details the types of resources that can be used with DirectCompute, including buffers and textures, and how they are accessed and manipulated.

Understanding DirectCompute Resource Types

DirectCompute can operate on various GPU resources. The primary resource types relevant to computation are:

Buffer Resources

Buffers are linear collections of data. DirectCompute supports several types of buffers:

Structured Buffers

Structured buffers store data in structures. They are ideal when your data can be neatly organized into records.

Note: When creating a structured buffer for DirectCompute, ensure the structure size is a multiple of 16 bytes.

Example structure definition in HLSL:


struct Particle {
    float3 position;
    float mass;
    uint id;
};

StructuredBuffer<Particle> particleBuffer : register(t0);

Raw Buffers

Raw buffers treat memory as a contiguous stream of bytes. This provides maximum flexibility for data manipulation.


ByteAddressBuffer rawBuffer : register(t1);

Accessing data from a raw buffer requires explicit byte offsets and type casting:


// Read 4 bytes starting at offset 16 as a float
float data = asfloat(rawBuffer.Load(16));

Typed Buffers

Typed buffers provide access to data of specific types, like floats, integers, or vectors.


Buffer<float4> float4Buffer : register(t2);

Texture Resources

DirectCompute can read from and write to textures. This is particularly useful for image processing, simulation, and data visualization tasks.

Texture Access

Textures can be sampled using standard texture samplers or accessed directly via their coordinates. For compute shaders, read/write access is typically managed through Unordered Access Views (UAVs).


// Read-only texture access (often via SRV)
Texture2D<float4> inputTexture : register(t3);

// Read/Write texture access (via UAV)
RWTexture2D<float4> outputTexture : register(u0);

Resource Creation and Binding

In your application code (e.g., C++ using DirectX APIs), you create these resources on the GPU. They are then bound to shader resource views (SRVs) or unordered access views (UAVs) which are then bound to the compute shader pipeline stages.

Key API Functions

Resource States

Resources, especially those used for writing (like UAVs), must be in the correct state before being accessed by the compute shader. Common states include:

You can use ID3D11DeviceContext::ResourceBarrier to transition resources between these states.

Tip: For optimal performance, only bind the resources that your compute shader needs and ensure they are in the correct state.

Further Reading