RI::writeDescriptors¶
What it does¶
RI::writeDescriptors(device, heap, writes) writes a list of descriptors into the resource slots of an
RI::DescriptorHeap. Each RI::DescriptorWrite names a slot and a descriptor, which is
one of RI::ReadView, RI::ReadWriteView, RI::ConstantBufferView, or
RI::AccelerationStructureView (see
ray tracing for acceleration structures). ReadView and ReadWriteView name
either a texture view (.texture) or a buffer range (.buffer); ConstantBufferView
names a buffer range.
| View | Shader type | Usage needed |
|---|---|---|
RI::ReadView{.texture = view} |
Texture2D<T>, Texture2DArray<T>, Texture2DMS<T> |
TextureUsage::eSampled |
RI::ReadWriteView{.texture = view} |
RWTexture2D<T>, RWTexture2DArray<T> |
TextureUsage::eStorage |
RI::ReadView{.buffer = buffer} |
StructuredBuffer<T>, ByteAddressBuffer |
BufferUsage::eRead |
RI::ReadWriteView{.buffer = buffer} |
RWStructuredBuffer<T>, RWByteAddressBuffer |
BufferUsage::eReadWrite |
RI::ConstantBufferView |
ConstantBuffer<T> |
BufferUsage::eConstant |
A texture view comes either from texture.view (the whole texture) or from
createView (a mip range, a layer range, another format, or one aspect). Set either
.texture or .buffer, not both: when .texture holds a view, the buffer fields are ignored.
You do not pass a texture layout, because RI tracks each texture's layout internally.
Each buffer view covers a byte range of a buffer: offset and size in bytes, where the default
size of RI::kWholeSize extends to the end of the buffer. ReadView and ReadWriteView also
take a stride: the element size in bytes for a structured buffer, or 0 for a raw
(ByteAddressBuffer or RWByteAddressBuffer) view.
The writes take effect immediately: there is no separate commit step. Pass either a braced list
(for a single write, pass a one-element list) or a std::span built at run time, such as a
std::vector.
Signature¶
// <RI/RI.hpp>, namespace RI
void writeDescriptors(Device &device, DescriptorHeap &heap, std::span<const DescriptorWrite> writes);
void writeDescriptors(Device &device, DescriptorHeap &heap, std::initializer_list<DescriptorWrite> writes);
- D3D12 header: Writes descriptors into heap slots (D1, P94): one native call per write on D3D12.
- Vulkan header: Writes descriptors into heap slots: one native call for the whole list (D1, P94).
| Field | Type | Default | Description |
|---|---|---|---|
slot |
uint32 |
0 |
|
descriptor |
Descriptor |
// <RI/RI.hpp>, namespace RI
using Descriptor = std::variant<ReadView, ReadWriteView, ConstantBufferView, AccelerationStructureView>;
// <RI/RI.hpp>, namespace RI
struct ReadView {
TextureView texture; // a texture view (Texture2D), or
Buffer buffer;
uint64 offset = 0;
uint64 size = kWholeSize;
uint32 stride = 0;
};
From the header: Views (P87). A read or read-write view names either a texture view or a buffer range. Buffer ranges: stride 0 is a raw view (ByteAddressBuffer); otherwise the element size of a StructuredBuffer. size kWholeSize: to the end.
| Field | Type | Default | Description |
|---|---|---|---|
texture |
TextureView |
a texture view (Texture2D), or | |
buffer |
Buffer |
||
offset |
uint64 |
0 |
|
size |
uint64 |
kWholeSize |
|
stride |
uint32 |
0 |
// <RI/RI.hpp>, namespace RI
struct ReadWriteView {
TextureView texture; // a texture view (RWTexture2D), or
Buffer buffer;
uint64 offset = 0;
uint64 size = kWholeSize;
uint32 stride = 0;
};
| Field | Type | Default | Description |
|---|---|---|---|
texture |
TextureView |
a texture view (RWTexture2D), or | |
buffer |
Buffer |
||
offset |
uint64 |
0 |
|
size |
uint64 |
kWholeSize |
|
stride |
uint32 |
0 |
// <RI/RI.hpp>, namespace RI
struct ConstantBufferView {
Buffer buffer;
uint64 offset = 0;
uint64 size = kWholeSize;
};
| Field | Type | Default | Description |
|---|---|---|---|
buffer |
Buffer |
||
offset |
uint64 |
0 |
|
size |
uint64 |
kWholeSize |
From the header: The rest of a buffer from offset (view sizes).
Example¶
// 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 list built at run time, with a view of part of a buffer:
// A list built at run time: pass a span (one write is a list of one).
std::vector<RI::DescriptorWrite> writes;
writes.push_back({.slot = 3, .descriptor = RI::ReadView{.buffer = output, .offset = 1024, .size = 1024, .stride = 4}});
RI::writeDescriptors(device, heap, writes);
Texture views:
// Texture views go in heap slots like buffer views, through `.texture`.
RI::DescriptorHeap heap = RI::createDescriptorHeap(device, {.resourceCount = 16});
RI::writeDescriptors(device, heap,
{{.slot = 0, .descriptor = RI::ReadView{.texture = colorSrgb}}, // Texture2D<float4>
{.slot = 1, .descriptor = RI::ReadView{.texture = depthOnly}}, // Texture2D<float>
{.slot = 2, .descriptor = RI::ReadWriteView{.texture = storageMip1}}}); // RWTexture2D<float4>
In the shader, the slot comes from root data:
// 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];
The same in native code¶
// 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}}});
// Slot = array element. Read and read-write views are both storage
// buffers in SPIR-V; the constant buffer is a uniform buffer.
const VkDescriptorBufferInfo inputView{.buffer = input, .offset = 0, .range = kBytes};
const VkDescriptorBufferInfo outputView{.buffer = output, .offset = 0, .range = kBytes};
const VkDescriptorBufferInfo paramsView{.buffer = params, .offset = 0, .range = 256};
const auto write = [&](uint32_t slot, VkDescriptorType type, const VkDescriptorBufferInfo *view) {
return VkWriteDescriptorSet{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.dstSet = heap,
.dstBinding = 0,
.dstArrayElement = slot,
.descriptorCount = 1,
.descriptorType = type,
.pBufferInfo = view};
};
const VkWriteDescriptorSet writes[] = {
write(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, &inputView),
write(1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, &outputView),
write(2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, ¶msView),
};
vkUpdateDescriptorSets(device, 3, writes, 0, nullptr);
What to watch for: in SPIR-V, read-only and read-write structured buffers are both
storage buffers; constant buffers are uniform buffers. Because RI's resource heap uses
mutable descriptors, each VkWriteDescriptorSet specifies the concrete VkDescriptorType.
RI writes the whole list with one vkUpdateDescriptorSets call.
Texture descriptors are sampled images and storage images, and they name the layout the image is in when the shader reads it:
// A sampled image descriptor names the layout the image will be in when
// the shader reads it (written into a heap set as in snippets/vulkan/compute.cpp).
const VkDescriptorImageInfo sampled{.imageView = colorSrgb, .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL};
const VkDescriptorImageInfo sampledDepth{.imageView = depthOnly,
.imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL};
// A slot's CPU address: heap start + slot * descriptor size (vendor-specific).
const UINT descriptorSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
const D3D12_CPU_DESCRIPTOR_HANDLE start = viewHeap->GetCPUDescriptorHandleForHeapStart();
const auto slot = [&](UINT index) { return D3D12_CPU_DESCRIPTOR_HANDLE{start.ptr + index * descriptorSize}; };
// Slot 0: StructuredBuffer<float>. Structured views count elements of StructureByteStride.
const D3D12_SHADER_RESOURCE_VIEW_DESC srv{
.Format = DXGI_FORMAT_UNKNOWN,
.ViewDimension = D3D12_SRV_DIMENSION_BUFFER,
.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
.Buffer = {.FirstElement = 0, .NumElements = kCount, .StructureByteStride = sizeof(float)}};
device->CreateShaderResourceView(input.Get(), &srv, slot(0));
// Slot 1: RWStructuredBuffer<float>.
const D3D12_UNORDERED_ACCESS_VIEW_DESC uav{
.Format = DXGI_FORMAT_UNKNOWN,
.ViewDimension = D3D12_UAV_DIMENSION_BUFFER,
.Buffer = {.FirstElement = 0, .NumElements = kCount, .StructureByteStride = sizeof(float)}};
device->CreateUnorderedAccessView(output.Get(), nullptr, &uav, slot(1));
// Slot 2: ConstantBuffer<Params>, by GPU address; 256-byte aligned and sized.
const D3D12_CONSTANT_BUFFER_VIEW_DESC cbv{.BufferLocation = params->GetGPUVirtualAddress(), .SizeInBytes = 256};
device->CreateConstantBufferView(&cbv, slot(2));
What to watch for: buffer views count elements, not bytes: FirstElement and
NumElements in units of StructureByteStride, or of 4 bytes for raw views
(R32_TYPELESS with the RAW flag). Constant buffer views are addressed by GPU address
and sized in 256-byte blocks. RI makes one call per write.
Texture descriptors are shader-resource and unordered-access views of the texture, in the view's format (for a depth texture, the shader-readable format of the same bits):
// Shader views go in the shader-visible heap (see snippets/d3d12/compute.cpp):
// the color texture read as sRGB, the depth texture read as R32_FLOAT.
const D3D12_DESCRIPTOR_HEAP_DESC viewHeapDesc{.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
.NumDescriptors = 16,
.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE};
ComPtr<ID3D12DescriptorHeap> viewHeap;
check(device->CreateDescriptorHeap(&viewHeapDesc, IID_PPV_ARGS(&viewHeap)), "CreateDescriptorHeap");
const UINT viewSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
const D3D12_CPU_DESCRIPTOR_HANDLE slot0 = viewHeap->GetCPUDescriptorHandleForHeapStart();
const D3D12_SHADER_RESOURCE_VIEW_DESC colorSrv{.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, // castable
.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D,
.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
.Texture2D = {.MostDetailedMip = 0, .MipLevels = 1}};
device->CreateShaderResourceView(color.Get(), &colorSrv, slot0);
const D3D12_SHADER_RESOURCE_VIEW_DESC depthSrv{.Format = DXGI_FORMAT_R32_FLOAT,
.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D,
.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
.Texture2D = {.MostDetailedMip = 0, .MipLevels = 1}};
device->CreateShaderResourceView(depth.Get(), &depthSrv, D3D12_CPU_DESCRIPTOR_HANDLE{slot0.ptr + viewSize});
Rules and errors¶
All of the following are checked in validation builds (programmer error):
- The slot must be inside the heap (
slot 8 is outside the resource heap (it has 8 slots)). - Read and read-write buffer views must fit in the buffer, be non-empty, start at an
offsetthat is a multiple of 16 bytes and of the element size (stride, or 4 bytes for raw views), and have asizethat covers whole elements. - Constant buffer views must start at an
offsetthat is a multiple of 256 bytes. Both backends roundsizeup to a multiple of 256 bytes, and the rounded range must fit in the buffer, so size constant buffers in multiples of 256 bytes. - A read or read-write buffer view covers at most
kMaxBufferViewElements(2^27) elements on every backend, which is D3D12's limit (512 MiB for a raw view). It also covers at mostDeviceLimits::maxBufferViewSizebytes, and a constant view covers at mostDeviceLimits::maxConstantViewSizebytes (64 KiB, or less on some Vulkan GPUs). For larger ranges, use several views in separate slots. - The heap and the referenced buffer, texture, or acceleration structure must be alive (not destroyed).
Texture views:
- Shaders cannot read a view that has both depth and stencil aspects; create a view with
Aspect::eDepthorAspect::eStencil(programmer error on Vulkan). - A
ReadWriteViewrequires a format the GPU can store to; sRGB formats cannot be used (checkformatSupportedwithTextureUsage::eStorage). - A view of a multi-layer texture (
layers > 1) is accessed asTexture2DArrayorRWTexture2DArray, a multisampled view asTexture2DMS(orTexture2DMSArraywhenlayers > 1), and a 3D texture view asTexture3DorRWTexture3D; for cube textures, whole-cubeReadViews read asTextureCubeorTextureCubeArray, while partial views andReadWriteViews use 2D arrays (see shapes).
Also:
- Never overwrite a slot that GPU work still in flight may read; wait for its timeline point first.
- Samplers go into the sampler heap with
writeSamplers.