Skip to content

RI::createDescriptorHeap, RI::destroyDescriptorHeap

What it does

A descriptor heap holds two descriptor arrays that bindless shaders index by slot: one for resource views (ResourceDescriptorHeap[slot]) and one for samplers (SamplerDescriptorHeap[slot]). RI::createDescriptorHeap(device, desc) creates a heap with desc.resourceCount resource slots (for buffer views, texture views, and acceleration structures) and desc.samplerCount sampler slots. All slots start empty; you fill resource slots with RI::writeDescriptors, fill sampler slots with RI::writeSamplers, and bind the heap to a command list with RI::cmdSetHeap.

How the heap works on each backend, and why, is explained in Descriptor heaps and bindless.

Most programs create one heap for the entire run and manage slots within it. Switching heaps in the middle of a command list is allowed, though it can be costly on some GPUs.

Signature

// <RI/RI.hpp>, namespace RI
DescriptorHeap createDescriptorHeap(Device &device, const DescriptorHeapDesc &desc);
// <RI/RI.hpp>, namespace RI
void destroyDescriptorHeap(Device &device, DescriptorHeap &heap);
// <RI/RI.hpp>, namespace RI
struct DescriptorHeap {
    uint32 resourceCount = 0;
    uint32 samplerCount = 0;
    // internal: backend-private state (not part of the API)
};

From the header: A resource heap and a sampler heap, written by slot (P42). Writes take effect at once; never overwrite a slot that in-flight GPU work may read (P59).

Field Type Default Description
resourceCount uint32 0
samplerCount uint32 0
// <RI/RI.hpp>, namespace RI
struct DescriptorHeapDesc {
    uint32 resourceCount = 0; // read, read-write and constant-buffer views
    uint32 samplerCount = 0;
    std::string_view name; // empty: unnamed (P106)
};

From the header: Sizes of a descriptor heap pair (P59), within the device's limits.

Field Type Default Description
resourceCount uint32 0 read, read-write and constant-buffer views
samplerCount uint32 0
name std::string_view empty: unnamed (P106)

Example

RI::DescriptorHeap heap = RI::createDescriptorHeap(device, {.resourceCount = 16});

The same in native code

RI::DescriptorHeap heap = RI::createDescriptorHeap(device, {.resourceCount = 16});

To emulate a resource heap in Vulkan, create a descriptor set layout (once per device in RI), a descriptor pool, and one descriptor set of the chosen size:

// 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");

What to watch for: the binding, the set layout, and the descriptor pool must all enable update-after-bind, and the mutable descriptor type list (VkMutableDescriptorTypeCreateInfoEXT) must be passed to both the layout and the pool. To support samplers as well, RI allocates a second descriptor set (set 1) for them.

// 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");

What to watch for: only one shader-visible heap of each type can be bound to a command list at a time, and a shader-visible CBV_SRV_UAV heap is limited to 1,000,000 descriptors (2,048 for a SAMPLER heap).

Rules and errors

  • desc.resourceCount must not exceed device.limits.maxResourceDescriptors, and desc.samplerCount must not exceed device.limits.maxSamplerDescriptors. Exceeding either limit reports a programmer error in every build. These limits are at most 1,000,000 resource descriptors and 2,048 sampler descriptors (matching D3D12's shader-visible heap limits), or lower if the GPU supports fewer.
  • A count of 0 is allowed for either resourceCount or samplerCount. RI still allocates one slot internally because some drivers reject empty arrays, but the corresponding field on DescriptorHeap (resourceCount or samplerCount) stays 0, so every slot index is out of range.
  • Destroy a heap only after all GPU work that uses it has finished (after its timeline point has been reached). RI::destroyDescriptorHeap resets heap to its default empty state.