You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add a class member to hold the descriptor set handle and allocate it with
119
+
In our case we will create one descriptor set for each swap chain image, all with the same layout. Unfortunately we do need all the copies of the layout because the next function expects an array matching the number of sets.
120
+
121
+
Add a class member to hold the descriptor set handles and allocate them with
120
122
`vkAllocateDescriptorSets`:
121
123
122
124
```c++
123
125
VkDescriptorPool descriptorPool;
124
-
VkDescriptorSet descriptorSet;
126
+
std::vector<VkDescriptorSet> descriptorSets;
125
127
126
128
...
127
129
128
-
if (vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet) != VK_SUCCESS) {
129
-
throw std::runtime_error("failed to allocate descriptor set!");
130
+
descriptorSets.resize(swapChainImages.size());
131
+
if (vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets[0]) != VK_SUCCESS) {
132
+
throw std::runtime_error("failed to allocate descriptor sets!");
130
133
}
131
134
```
132
135
133
136
You don't need to explicitly clean up descriptor sets, because they will be
134
137
automatically freed when the descriptor pool is destroyed. The call to
135
-
`vkAllocateDescriptorSets` will allocate one descriptor set with one uniform
138
+
`vkAllocateDescriptorSets` will allocate descriptor sets, each with one uniform
136
139
buffer descriptor.
137
140
138
-
The descriptor set has been allocated now, but the descriptors within still need
139
-
to be configured. Descriptors that refer to buffers, like our uniform buffer
141
+
The descriptor sets have been allocated now, but the descriptors within still need
142
+
to be configured. We'll now add a loop to populate every descriptor:
143
+
144
+
```c++
145
+
for (size_t i = 0; i < swapChainImages.size(); i++) {
146
+
147
+
}
148
+
```
149
+
150
+
Descriptors that refer to buffers, like our uniform buffer
140
151
descriptor, are configured with a `VkDescriptorBufferInfo` struct. This
141
152
structure specifies the buffer and the region within it that contains the data
142
-
for the descriptor:
153
+
for the descriptor.
143
154
144
155
```c++
145
-
VkDescriptorBufferInfo bufferInfo = {};
146
-
bufferInfo.buffer = uniformBuffer;
147
-
bufferInfo.offset = 0;
148
-
bufferInfo.range = sizeof(UniformBufferObject);
156
+
for (size_t i = 0; i < swapChainImages.size(); i++) {
157
+
VkDescriptorBufferInfo bufferInfo = {};
158
+
bufferInfo.buffer = uniformBuffers[i];
159
+
bufferInfo.offset = 0;
160
+
bufferInfo.range = sizeof(UniformBufferObject);
161
+
}
149
162
```
150
163
151
164
If you're overwriting the whole buffer, like we are in this case, then it is is also possible to use the `VK_WHOLE_SIZE` value for the range. The configuration of descriptors is updated using the `vkUpdateDescriptorSets`
@@ -154,7 +167,7 @@ function, which takes an array of `VkWriteDescriptorSet` structs as parameter.
@@ -196,14 +209,13 @@ arrays as parameters: an array of `VkWriteDescriptorSet` and an array of
196
209
`VkCopyDescriptorSet`. The latter can be used to copy descriptors to each other,
197
210
as its name implies.
198
211
199
-
## Using a descriptor set
212
+
## Using descriptor sets
200
213
201
214
We now need to update the `createCommandBuffers` function to actually bind the
202
-
descriptor set to the descriptors in the shader with `cmdBindDescriptorSets`,
203
-
this needs to be done before the `vkCmdDrawIndexed` call:
215
+
right descriptor set for each swap chain image to the descriptors in the shader with `cmdBindDescriptorSets`. This needs to be done before the `vkCmdDrawIndexed` call:
0 commit comments