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:
- Structured Buffers: Efficient for storing arbitrary data structures, allowing for flexible data access patterns.
- Raw Buffers: Provide byte-addressable access to memory, useful for custom data layouts or when fixed-size structures are not appropriate.
- Typed Buffers: Offer access to data in specific, predefined types, optimizing for common data formats.
- Textures: Can be accessed as 1D, 2D, or 3D arrays, allowing compute shaders to process image data or volumetric data.
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.
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
CreateCommittedResource: For creating GPU buffers and textures.CreateShaderResourceView: To create SRVs for read-only access.CreateUnorderedAccessView: To create UAVs for read/write access.ID3D11DeviceContext::CSSetShaderResources: To bind SRVs to the compute shader stage.ID3D11DeviceContext::CSSetUnorderedAccessViews: To bind UAVs to the compute shader stage.
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:
D3D11_RESOURCE_STATE_COMMOND3D11_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFERD3D11_RESOURCE_STATE_SHADER_RESOURCED3D11_RESOURCE_STATE_UNORDERED_ACCESSD3D11_RESOURCE_STATE_COPY_DESTD3D11_RESOURCE_STATE_COPY_SOURCED3D11_RESOURCE_STATE_RESOLVE_DESTD3D11_RESOURCE_STATE_RESOLVE_SOURCED3D11_RESOURCE_STATE_DEPTH_READD3D11_RESOURCE_STATE_DEPTH_WRITED3D11_RESOURCE_STATE_STENCIL_READD3D11_RESOURCE_STATE_STENCIL_WRITED3D11_RESOURCE_STATE_VIDEO_DECODED3D11_RESOURCE_STATE_VIDEO_ENCODED3D11_RESOURCE_STATE_RENDER_TARGETD3D11_RESOURCE_STATE_STREAM_OUTD3D11_RESOURCE_STATE_COPY_DESTD3D11_RESOURCE_STATE_COPY_SOURCE
You can use ID3D11DeviceContext::ResourceBarrier to transition resources between these states.