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
Copy file name to clipboardExpand all lines: 05_Uniform_buffers/00_Descriptor_layout_and_buffer.md
+38-23Lines changed: 38 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -234,18 +234,20 @@ the uniform buffer every frame, so it doesn't really make any sense to have a
234
234
staging buffer. It would just add extra overhead in this case and likely degrade
235
235
performance instead of improving it.
236
236
237
-
Add new class members for `uniformBuffer`, and `uniformBufferMemory`:
237
+
We should have multiple buffers, because multiple frames may be in flight at the same time and we don't want to update the buffer in preparation of the next frame while a previous one is still reading from it! We could either have a uniform buffer per frame or per swap chain image. However, since we need to refer to the uniform buffer from the command buffer that we have per swap chain image, it makes the most sense to also have a uniform buffer per swap chain image.
238
+
239
+
To that end, add new class members for `uniformBuffers`, and `uniformBuffersMemory`:
238
240
239
241
```c++
240
242
VkBuffer indexBuffer;
241
243
VkDeviceMemory indexBufferMemory;
242
244
243
-
VkBuffer uniformBuffer;
244
-
VkDeviceMemory uniformBufferMemory;
245
+
std::vector<VkBuffer> uniformBuffers;
246
+
std::vector<VkDeviceMemory> uniformBuffersMemory;
245
247
```
246
248
247
-
Similarly, create a new function `createUniformBuffer` that is called after
248
-
`createIndexBuffer` and allocates the buffer:
249
+
Similarly, create a new function `createUniformBuffers` that is called after
Create a new function `updateUniformBuffer` and add a call to it from the main
287
-
loop:
297
+
Create a new function `updateUniformBuffer` and add a call to it from the `drawFrame` function right after we know which swap chain image we're going to acquire:
288
298
289
299
```c++
290
-
voidmainLoop() {
291
-
while (!glfwWindowShouldClose(window)) {
292
-
glfwPollEvents();
300
+
voiddrawFrame() {
301
+
...
293
302
294
-
updateUniformBuffer();
295
-
drawFrame();
296
-
}
303
+
uint32_t imageIndex;
304
+
VkResult result = vkAcquireNextImageKHR(device, swapChain, std::numeric_limits<uint64_t>::max(), imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);
305
+
306
+
...
297
307
298
-
vkDeviceWaitIdle(device);
308
+
updateUniformBuffer(imageIndex);
309
+
310
+
VkSubmitInfo submitInfo = {};
311
+
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
312
+
313
+
...
299
314
}
300
315
301
316
...
302
317
303
-
voidupdateUniformBuffer() {
318
+
voidupdateUniformBuffer(uint32_t currentImage) {
304
319
305
320
}
306
321
```
@@ -328,7 +343,7 @@ timekeeping. We'll use this to make sure that the geometry rotates 90 degrees
328
343
per second regardless of frame rate.
329
344
330
345
```c++
331
-
voidupdateUniformBuffer() {
346
+
voidupdateUniformBuffer(uint32_t currentImage) {
332
347
static auto startTime = std::chrono::high_resolution_clock::now();
333
348
334
349
auto currentTime = std::chrono::high_resolution_clock::now();
@@ -381,22 +396,22 @@ sign on the scaling factor of the Y axis in the projection matrix. If you don't
381
396
do this, then the image will be rendered upside down.
382
397
383
398
All of the transformations are defined now, so we can copy the data in the
384
-
uniform buffer object to the uniform buffer. This happens in exactly the same
399
+
uniform buffer object to the current uniform buffer. This happens in exactly the same
385
400
way as we did for vertex buffers, except without a staging buffer:
0 commit comments