|
| 1 | +// Copyright 2025 The Khronos Group, Inc. |
| 2 | +// SPDX-License-Identifier: CC-BY-4.0 |
| 3 | + |
| 4 | +ifndef::chapters[:chapters:] |
| 5 | +ifndef::images[:images: images/] |
| 6 | + |
| 7 | +[[descriptor-buffer]] |
| 8 | += Descriptor Buffer |
| 9 | + |
| 10 | +This chapter aims to illustrate better how link:https://github.com/KhronosGroup/Vulkan-Docs/blob/main/proposals/VK_EXT_descriptor_buffer.adoc[VK_EXT_descriptor_buffer] mapping of memory works. |
| 11 | + |
| 12 | +The goal here is **not** to show a real example or recommended usage, but instead help understand how the API is mapping data to the shader, so that afterwards you can use this API in any way you want. |
| 13 | + |
| 14 | +[NOTE] |
| 15 | +==== |
| 16 | +This will only use Storage Buffers because it's simpler to demonstrate the mappings. Samplers and images work in the same general way, but with caveats better explained in the extension proposal. |
| 17 | +==== |
| 18 | + |
| 19 | +== Terminology Overload |
| 20 | + |
| 21 | +To try and clear some terms up first: |
| 22 | + |
| 23 | +* "descriptor buffers" are just `VkBuffer` created with the `VK_BUFFER_USAGE_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT` flags |
| 24 | +* "samplers" are `VkSampler` (`VK_DESCRIPTOR_TYPE_SAMPLER`) |
| 25 | +* "resources" are all other VkDescriptorType |
| 26 | +** This could be a `VkBuffer`, which can be called a "resource buffer" |
| 27 | + |
| 28 | +== The Example Shader |
| 29 | + |
| 30 | +We will take a basic shader that has 3 sets, each with 3 descriptors inside of them. Each descriptor gets written a unique value. |
| 31 | + |
| 32 | +[source,glsl] |
| 33 | +---- |
| 34 | +layout (set = 0, binding = 0) buffer A { uint a; }; |
| 35 | +layout (set = 0, binding = 1) buffer B { uint b; }; |
| 36 | +layout (set = 0, binding = 2) buffer C { uint c; }; |
| 37 | +
|
| 38 | +layout (set = 1, binding = 0) buffer D { uint d; }; |
| 39 | +layout (set = 1, binding = 1) buffer E { uint e; }; |
| 40 | +layout (set = 1, binding = 2) buffer F { uint f; }; |
| 41 | +
|
| 42 | +layout (set = 2, binding = 0) buffer G { uint g; } array[3]; |
| 43 | +
|
| 44 | +void main() { |
| 45 | + a = 10; |
| 46 | + b = 20; |
| 47 | + c = 30; |
| 48 | + d = 40; |
| 49 | + e = 50; |
| 50 | + f = 60; |
| 51 | + array[0].g = 70; |
| 52 | + array[1].g = 80; |
| 53 | + array[2].g = 90; |
| 54 | +} |
| 55 | +---- |
| 56 | + |
| 57 | +With this shader, we will then have 3 `VkDescriptorSetLayout` in a single pipeline that exactly matches the shader interface. |
| 58 | + |
| 59 | +[source,c++] |
| 60 | +---- |
| 61 | +// Set 0 and 1 |
| 62 | +VkDescriptorSetLayoutBinding bindings_a[3] { |
| 63 | + { binding = 0, descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, descriptorCount = 1 }, |
| 64 | + { binding = 1, descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, descriptorCount = 1 }, |
| 65 | + { binding = 2, descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, descriptorCount = 1 } |
| 66 | +} |
| 67 | +
|
| 68 | +// Set 2 |
| 69 | +VkDescriptorSetLayoutBinding bindings_b[1] { |
| 70 | + { binding = 0, descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, descriptorCount = 3 } |
| 71 | +} |
| 72 | +
|
| 73 | +VkDescriptorSetLayout ds_layout_0(bindings_a); // Set 0 |
| 74 | +VkDescriptorSetLayout ds_layout_1(bindings_a); // Set 1 |
| 75 | +VkDescriptorSetLayout ds_layout_2(bindings_b); // Set 2 |
| 76 | +VkPipelineLayout pipeline_layot([ds_layout_0, ds_layout_1, ds_layout_2]); |
| 77 | +---- |
| 78 | + |
| 79 | +== Query Descriptor Set Layout Sizes |
| 80 | + |
| 81 | +Now using the `vkGetDescriptorSetLayoutSizeEXT` and `vkGetDescriptorSetLayoutBindingOffsetEXT` commands, we can get info from the driver what size it needs to properly use these `VkDescriptorSetLayout`. |
| 82 | + |
| 83 | +[NOTE] |
| 84 | +==== |
| 85 | +Don't make the assumption that `binding 0` will be the lowest offset! The driver might sort bindings in a more optimal way such that the offsets might not be incremental as the binding numbers. |
| 86 | +==== |
| 87 | + |
| 88 | +[source,c++] |
| 89 | +---- |
| 90 | +// This could be done in an array where the set/binding are indexes used to get the size/offsets |
| 91 | +VkDeviceSize set_0_size, set_1_size, set_2_size; |
| 92 | +vkGetDescriptorSetLayoutSizeEXT(device, ds_layout_0, &set_0_size); |
| 93 | +vkGetDescriptorSetLayoutSizeEXT(device, ds_layout_1, &set_1_size); |
| 94 | +vkGetDescriptorSetLayoutSizeEXT(device, ds_layout_2, &set_2_size); |
| 95 | +
|
| 96 | +VkDeviceSize binding_0_offset, binding_1_offset, binding_2_offset; |
| 97 | +vkGetDescriptorSetLayoutBindingOffsetEXT(device, ds_layout_0, 0, &binding_0_offset); |
| 98 | +vkGetDescriptorSetLayoutBindingOffsetEXT(device, ds_layout_0, 1, &binding_1_offset); |
| 99 | +vkGetDescriptorSetLayoutBindingOffsetEXT(device, ds_layout_0, 2, &binding_2_offset); |
| 100 | +
|
| 101 | +// ... |
| 102 | +---- |
| 103 | + |
| 104 | +== Creating Descriptor Buffers |
| 105 | + |
| 106 | +We will create a "special" `VkBuffer` with `VK_BUFFER_USAGE_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT` that now makes it a "descriptor buffer" `VkBuffer`. |
| 107 | + |
| 108 | +These buffers can be large and they hold a "look up table" to your resources and samplers. |
| 109 | + |
| 110 | +[NOTE] |
| 111 | +==== |
| 112 | +The minimum required limit for `VkPhysicalDeviceDescriptorBufferPropertiesEXT::descriptorBufferAddressSpaceSize` is 128MB, but many devices can support 4GB |
| 113 | +==== |
| 114 | + |
| 115 | +For this demo, we will create two of them. |
| 116 | + |
| 117 | +image::{images}descriptor_buffer_1.svg[descriptor_buffer_1.svg] |
| 118 | + |
| 119 | +== Creating Resources |
| 120 | + |
| 121 | +For this demo, we will create a couple of "normal" `VkBuffer` that we will use as our resources. These are small since all the descriptors in our shader as declared as `uint`. |
| 122 | + |
| 123 | +image::{images}descriptor_buffer_2.svg[descriptor_buffer_2.svg] |
| 124 | + |
| 125 | +== Mapping Resources to the Descriptor Buffer |
| 126 | + |
| 127 | +Using `vkGetDescriptorEXT` we find a spot in the "descriptor buffer" and map it you our resources. |
| 128 | + |
| 129 | +The following code will map the 3 of the descriptors using a single resource buffer. |
| 130 | + |
| 131 | +[source,c++] |
| 132 | +---- |
| 133 | +// vkMapMemory() |
| 134 | +uint8_t* descriptor_ptr = descriptor_buffer_a.GetMappedMemory(); |
| 135 | +
|
| 136 | +// 64 in this example |
| 137 | +size_t descriptor_size = VkPhysicalDeviceDescriptorBufferPropertiesEXT::storageBufferDescriptorSize; |
| 138 | +
|
| 139 | +VkDeviceAddress buffer_x_address = vkGetBufferDeviceAddress(buffer_x); |
| 140 | +
|
| 141 | +// Example results from vkGetDescriptorSetLayoutBindingOffsetEXT |
| 142 | +VkDeviceSize binding_0_offset = 0; |
| 143 | +VkDeviceSize binding_1_offset = 64; |
| 144 | +VkDeviceSize binding_2_offset = 128; |
| 145 | +
|
| 146 | +VkDescriptorGetInfoEXT get_info; |
| 147 | +get_info.type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; |
| 148 | +
|
| 149 | +get_info.data.pStorageBuffer->range = 4; |
| 150 | +get_info.data.pStorageBuffer->address = buffer_x_address; |
| 151 | +vkGetDescriptorEXT(get_info, descriptor_size, descriptor_ptr + binding_0_offset); |
| 152 | +
|
| 153 | +get_info.data.pStorageBuffer->address = buffer_x_address + 4; |
| 154 | +vkGetDescriptorEXT(get_info, descriptor_size, descriptor_ptr + binding_1_offset); |
| 155 | +
|
| 156 | +get_info.data.pStorageBuffer->address = buffer_x_address + 12; |
| 157 | +vkGetDescriptorEXT(get_info, descriptor_size, descriptor_ptr + binding_2_offset); |
| 158 | +---- |
| 159 | + |
| 160 | +image::{images}descriptor_buffer_3.svg[descriptor_buffer_3.svg] |
| 161 | + |
| 162 | +We can also have each descriptor map to its own resource buffer. |
| 163 | + |
| 164 | +[source,c++] |
| 165 | +---- |
| 166 | +// Switching descriptor buffers |
| 167 | +descriptor_ptr = descriptor_buffer_b.GetMappedMemory(); |
| 168 | +
|
| 169 | +get_info.data.pStorageBuffer->address = buffer_y1_address; |
| 170 | +vkGetDescriptorEXT(get_info, descriptor_size, descriptor_ptr + binding_0_offset); |
| 171 | +
|
| 172 | +get_info.data.pStorageBuffer->address = buffer_y2_address; |
| 173 | +vkGetDescriptorEXT(get_info, descriptor_size, descriptor_ptr + binding_1_offset); |
| 174 | +
|
| 175 | +get_info.data.pStorageBuffer->address = buffer_y3_address; |
| 176 | +vkGetDescriptorEXT(get_info, descriptor_size, descriptor_ptr + binding_2_offset); |
| 177 | +---- |
| 178 | + |
| 179 | +image::{images}descriptor_buffer_4.svg[descriptor_buffer_4.svg] |
| 180 | + |
| 181 | +And finally we can bind our last set. |
| 182 | + |
| 183 | +[source,c++] |
| 184 | +---- |
| 185 | +size_t set_offset = 256; |
| 186 | +assert(set_offset > set_1_size); |
| 187 | +assert(set_offset.IsAligned(VkPhysicalDeviceDescriptorBufferPropertiesEXT::descriptorBufferOffsetAlignment)); |
| 188 | +
|
| 189 | +get_info.data.pStorageBuffer->address = buffer_z0_address; |
| 190 | +vkGetDescriptorEXT(get_info, descriptor_size, descriptor_ptr + set_offset + binding_0_offset); |
| 191 | +
|
| 192 | +get_info.data.pStorageBuffer->address = buffer_z1_address; |
| 193 | +vkGetDescriptorEXT(get_info, descriptor_size, descriptor_ptr + set_offset + binding_1_offset); |
| 194 | +
|
| 195 | +get_info.data.pStorageBuffer->address = buffer_z2_address; |
| 196 | +vkGetDescriptorEXT(get_info, descriptor_size, descriptor_ptr + set_offset + binding_2_offset); |
| 197 | +---- |
| 198 | + |
| 199 | +image::{images}descriptor_buffer_5.svg[descriptor_buffer_5.svg] |
| 200 | + |
| 201 | +== Binding Descriptor Buffers to the Command Buffer |
| 202 | + |
| 203 | +With `vkCmdBindDescriptorBuffersEXT` we will now bind the "descriptor buffer" to the command buffer. |
| 204 | + |
| 205 | +[NOTE] |
| 206 | +==== |
| 207 | +While you can create multiple descriptor buffers, there is a stricter limit how many are bound. |
| 208 | +The validation layers will warn you if you go over limits such as `maxDescriptorBufferBindings` or `maxResourceDescriptorBufferBindings`. |
| 209 | +==== |
| 210 | + |
| 211 | +[source,c++] |
| 212 | +---- |
| 213 | +VkDescriptorBufferBindingInfoEXT binding_info[2]; |
| 214 | +binding_info[0].address = descriptor_buffer_a.Address(); |
| 215 | +binding_info[0].usage = VK_BUFFER_USAGE_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT; |
| 216 | +binding_info[1].address = descriptor_buffer_b.Address(); |
| 217 | +binding_info[1].usage = VK_BUFFER_USAGE_RESOURCE_DESCRIPTOR_BUFFER_BIT_EXT; |
| 218 | +vkCmdBindDescriptorBuffersEXT(commandbuffer, 2, binding_info); |
| 219 | +---- |
| 220 | + |
| 221 | +image::{images}descriptor_buffer_6.svg[descriptor_buffer_6.svg] |
| 222 | + |
| 223 | +== Binding Offsets |
| 224 | + |
| 225 | +Next we will call `vkCmdSetDescriptorBufferOffsetsEXT` and line up the `VkDescriptorSetLayout` (from the `VkPipelineLayout`) to our descriptor buffer. |
| 226 | + |
| 227 | +[NOTE] |
| 228 | +==== |
| 229 | +Most commands recorded in a command buffer can be in any order as long as it's in/out of a render pass, and before a draw. |
| 230 | +`vkCmdSetDescriptorBufferOffsetsEXT` needs to be called **after** `vkCmdBindDescriptorBuffersEXT`. |
| 231 | +==== |
| 232 | + |
| 233 | +[source,c++] |
| 234 | +---- |
| 235 | +size_t set_offset = 256; // from above |
| 236 | +
|
| 237 | +uint32_t first_set = 0; |
| 238 | +uint32_t set_count = 3; |
| 239 | +uint32_t buffer_index[3] = {0, 1, 1}; |
| 240 | +VkDeviceSize buffer_offset[3] = {0, 0, set_offset}; |
| 241 | +vkCmdSetDescriptorBufferOffsetsEXT(commandbuffer, pipeline_bind_point, pipeline_layout, first_set, set_count, buffer_index, buffer_offset); |
| 242 | +---- |
| 243 | + |
| 244 | +image::{images}descriptor_buffer_7.svg[descriptor_buffer_7.svg] |
| 245 | + |
| 246 | +== Draw away |
| 247 | + |
| 248 | +That is it, from here you can just call `vkCmdDraw` (or other action commands such as `vkCmdDispatch`) and everything should be working! |
| 249 | + |
| 250 | +image::{images}descriptor_buffer_8.svg[descriptor_buffer_8.svg] |
0 commit comments