Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b072a18

Browse files
committed
Add explanation of why we require min + 1 swap chain images (fixes #123)
1 parent 342ac6c commit b072a18

1 file changed

Lines changed: 13 additions & 16 deletions

File tree

03_Drawing_a_triangle/01_Presentation/01_Swap_chain.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -409,23 +409,26 @@ void createSwapChain() {
409409
}
410410
```
411411

412-
Now, there is actually one more small thing that need to be decided upon, but it's
413-
so simple that it's not really worth creating a separate function for. That is
414-
deciding the number of images in the swap chain, essentially the queue
415-
length. The implementation specifies the minimum amount of images to function
416-
properly and we'll try to have one more than that to properly implement triple
417-
buffering.
412+
Aside from these properties we also have to decide how many images we would like to have in the swap chain. The implementation specifies the minimum number that it requires to function:
413+
414+
```c++
415+
uint32_t imageCount = swapChainSupport.capabilities.minImageCount;
416+
```
417+
418+
However, simply sticking to this minimum means that we may sometimes have to wait on the driver to complete internal operations before we can acquire another image to render to. Therefore it is recommended to request at least one more image than the minimum:
418419

419420
```c++
420421
uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1;
422+
```
423+
424+
We should also make sure to not exceed the maximum number of images while doing this, where `0` is a special value that means that there is no maximum:
425+
426+
```c++
421427
if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) {
422428
imageCount = swapChainSupport.capabilities.maxImageCount;
423429
}
424430
```
425431

426-
A value of `0` for `maxImageCount` means that there is no limit besides memory
427-
requirements, which is why we need to check for that.
428-
429432
As is tradition with Vulkan objects, creating the swap chain object requires
430433
filling in a large structure. It starts out very familiarly:
431434

@@ -583,9 +586,7 @@ don't need to add any cleanup code.
583586

584587
I'm adding the code to retrieve the handles to the end of the `createSwapChain`
585588
function, right after the `vkCreateSwapchainKHR` call. Retrieving them is very
586-
similar to the other times where we retrieved an array of objects from Vulkan.
587-
First query the number of images in the swap chain with a call to
588-
`vkGetSwapchainImagesKHR`, then resize the container and finally call it again
589+
similar to the other times where we retrieved an array of objects from Vulkan. Remember that we only specified a minimum number of images in the swap chain, so the implementation is allowed to create a swap chain with more. That's why we'll first query the final number of images with `vkGetSwapchainImagesKHR`, then resize the container and finally call it again
589590
to retrieve the handles.
590591

591592
```c++
@@ -594,10 +595,6 @@ swapChainImages.resize(imageCount);
594595
vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());
595596
```
596597
597-
Note that when we created the swap chain, we passed the number of desired images
598-
to a field called `minImageCount`. The implementation is allowed to create more
599-
images, which is why we need to explicitly query the amount again.
600-
601598
One last thing, store the format and extent we've chosen for the swap chain
602599
images in member variables. We'll need them in future chapters.
603600

0 commit comments

Comments
 (0)