Descriptor heaps and bindless¶
What a descriptor is¶
A shader cannot use a raw pointer by itself to access a buffer or texture view. Instead, it accesses a resource through a descriptor: a small GPU record that says where the resource is, how big it is, and how the shader may use it (read-only, read-write, or as a constant buffer). RI names descriptor types by how the shader accesses them:
| RI | HLSL | D3D12 | Vulkan |
|---|---|---|---|
RI::ReadView |
StructuredBuffer, ByteAddressBuffer |
SRV | storage buffer |
RI::ReadWriteView |
RWStructuredBuffer, RWByteAddressBuffer |
UAV | storage buffer |
RI::ConstantBufferView |
ConstantBuffer<T> |
CBV | uniform buffer |
ReadView and ReadWriteView can also hold a TextureView (covered on the
textures page), and samplers have their own heap slots (covered in
writeSamplers).
Bindless: one heap, slots you choose¶
RI is bindless. Instead of binding each resource to a numbered register before a draw or dispatch, you keep one large array of descriptors, the descriptor heap, and the shader picks the one it wants by index:
// Bindless: fetch each buffer from the heap by the slot passed in root data.
StructuredBuffer<float> input = ResourceDescriptorHeap[root.inputSlot];
RWStructuredBuffer<float> output = ResourceDescriptorHeap[root.outputSlot];
ConstantBuffer<Params> params = ResourceDescriptorHeap[root.paramsSlot];
ResourceDescriptorHeap is HLSL's name for the heap (shader model 6.6). The slots usually
arrive through root data, as here, or are read from another buffer.
On the C++ side, you create a descriptor heap with sizes you choose (resourceCount and
samplerCount), within device.limits.maxResourceDescriptors and
device.limits.maxSamplerDescriptors (DeviceLimits):
You then write descriptors into slots you choose. RI never assigns slots for you; automatic slot assignment belongs in a helper built on top of RI.
// Slots are yours to choose; the shader receives them in root data.
RI::writeDescriptors(device, heap,
{{.slot = 0, .descriptor = RI::ReadView{.buffer = input, .stride = sizeof(float)}},
{.slot = 1, .descriptor = RI::ReadWriteView{.buffer = output, .stride = sizeof(float)}},
{.slot = 2, .descriptor = RI::ConstantBufferView{.buffer = params}}});
A DescriptorHeap is a pair of arrays, matching D3D12's two shader-visible heap types: one
array for resource views (resourceCount slots, written with writeDescriptors,
ResourceDescriptorHeap in HLSL) and one for samplers (samplerCount slots, written with
writeSamplers, SamplerDescriptorHeap in HLSL).
Rules¶
- Writes take effect at once. Descriptor writes update the heap immediately; there is no per-frame buffering. Never overwrite a slot that in-flight GPU work may still read: wait for that work's timeline point first, just as you do before destroying a resource.
- Set the heap before the pipeline. In a command list, call
cmdSetHeapbeforecmdSetPipeline. D3D12 requires the descriptor heaps to be bound before a root signature that indexes them (D3D12_ROOT_SIGNATURE_FLAG_CBV_SRV_UAV_HEAP_DIRECTLY_INDEXEDandD3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED), so RI enforces this order on every backend and checks it in validation builds. - View alignment. Both backends enforce the same alignment and range rules in validation
builds (because D3D12 counts buffer view ranges in elements and constant buffers in 256-byte
blocks):
- buffer
ReadViewandReadWriteViewranges must have anoffsetthat is a multiple of 16 bytes and of the element size (stride, or 4 bytes for raw views wherestride == 0), a non-zerosizethat is a multiple of the element size, and[offset, offset + size)must fit within the buffer; ConstantBufferViewranges must have anoffsetthat is a multiple of 256 bytes; theirsizeis rounded up to a multiple of 256 bytes, and[offset, offset + roundedSize)must fit within the buffer.
- buffer
- Slots are checked in validation builds: writing to a slot at or past
resourceCount(orsamplerCountfor samplers) is reported as a programmer error (on D3D12, an unchecked write would silently write past the heap).
How the heap works on each backend¶
createDescriptorHeap creates both arrays, and writeDescriptors writes any number of
resource descriptors in one call (writeSamplers does the same for samplers).
Vulkan has no descriptor heap in its core API. RI emulates one using the layout DXC
generates for ResourceDescriptorHeap[] by default:
- set 0, binding 0 is one large array of mutable descriptors
(
VK_EXT_mutable_descriptor_type): each slot can hold any supported resource descriptor (buffer, image, or acceleration structure); - set 1, binding 0 is the sampler array. DXC's default puts samplers at set 0,
binding 1; RI's compile rule moves them with
-fvk-bind-sampler-heap 0 1, so both arrays can be sized independently (Compiling shaders). A shader compiled without this flag is rejected at pipeline creation with a readable error; - both arrays use update-after-bind, partially bound, and variable descriptor count flags: you can write slots while the set is bound, leave unused slots empty, and choose each heap's size at creation time;
- RI creates one set layout per array for the whole device, so every pipeline is compatible with every heap.
writeDescriptors issues a single vkUpdateDescriptorSets call for the whole list.
// The "heap": set 0, binding 0, an array of mutable descriptors (each slot
// holds a storage or uniform buffer), of variable size, updatable after bind.
const uint32_t kMaxSlots = 65536;
const VkDescriptorType slotTypes[] = {VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER};
const VkMutableDescriptorTypeListEXT typeList{.descriptorTypeCount = 2, .pDescriptorTypes = slotTypes};
const VkMutableDescriptorTypeCreateInfoEXT mutableInfo{
.sType = VK_STRUCTURE_TYPE_MUTABLE_DESCRIPTOR_TYPE_CREATE_INFO_EXT,
.mutableDescriptorTypeListCount = 1,
.pMutableDescriptorTypeLists = &typeList,
};
const VkDescriptorBindingFlags bindingFlags = VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT |
VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT |
VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT;
const VkDescriptorSetLayoutBindingFlagsCreateInfo flagsInfo{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO,
.pNext = &mutableInfo,
.bindingCount = 1,
.pBindingFlags = &bindingFlags,
};
const VkDescriptorSetLayoutBinding binding{.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_MUTABLE_EXT,
.descriptorCount = kMaxSlots,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT};
const VkDescriptorSetLayoutCreateInfo layoutInfo{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pNext = &flagsInfo,
.flags = VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT,
.bindingCount = 1,
.pBindings = &binding,
};
VkDescriptorSetLayout setLayout = VK_NULL_HANDLE;
check(vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &setLayout), "vkCreateDescriptorSetLayout");
// A pool for one set, then the set itself with 16 slots.
const uint32_t slots = 16;
const VkDescriptorPoolSize poolSize{VK_DESCRIPTOR_TYPE_MUTABLE_EXT, slots};
const VkDescriptorPoolCreateInfo poolInfo{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.pNext = &mutableInfo, // which types the pool's mutable descriptors may hold
.flags = VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT,
.maxSets = 1,
.poolSizeCount = 1,
.pPoolSizes = &poolSize,
};
VkDescriptorPool pool = VK_NULL_HANDLE;
check(vkCreateDescriptorPool(device, &poolInfo, nullptr, &pool), "vkCreateDescriptorPool");
const VkDescriptorSetVariableDescriptorCountAllocateInfo countInfo{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO,
.descriptorSetCount = 1,
.pDescriptorCounts = &slots,
};
const VkDescriptorSetAllocateInfo setInfo{.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.pNext = &countInfo,
.descriptorPool = pool,
.descriptorSetCount = 1,
.pSetLayouts = &setLayout};
VkDescriptorSet heap = VK_NULL_HANDLE;
check(vkAllocateDescriptorSets(device, &setInfo, &heap), "vkAllocateDescriptorSets");
The closest native match to D3D12, VK_EXT_descriptor_heap, is not available on every
target driver yet and was measured to be slower or no faster on current drivers, so RI keeps
descriptor sets for now and will revisit the extension when drivers and DXC mature.
Two shader-visible ID3D12DescriptorHeap objects: D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV
and D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER. A slot's CPU handle is the heap's start handle
plus slot times the descriptor increment size. writeDescriptors calls the matching
Create*View method (CreateShaderResourceView, CreateUnorderedAccessView, or
CreateConstantBufferView) once per write.
// Shader-visible descriptor heaps: one for views (CBV/SRV/UAV), one for samplers.
const D3D12_DESCRIPTOR_HEAP_DESC viewHeapDesc{.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
.NumDescriptors = 16,
.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE};
const D3D12_DESCRIPTOR_HEAP_DESC samplerHeapDesc{.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER,
.NumDescriptors = 16,
.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE};
ComPtr<ID3D12DescriptorHeap> viewHeap;
ComPtr<ID3D12DescriptorHeap> samplerHeap;
check(device->CreateDescriptorHeap(&viewHeapDesc, IID_PPV_ARGS(&viewHeap)), "CreateDescriptorHeap");
check(device->CreateDescriptorHeap(&samplerHeapDesc, IID_PPV_ARGS(&samplerHeap)), "CreateDescriptorHeap");