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

Skip to content

Commit 91f969b

Browse files
committed
Incorporate changes to chooseSwapExtent
1 parent 8bd0a5e commit 91f969b

2 files changed

Lines changed: 34 additions & 28 deletions

File tree

en/03_Drawing_a_triangle/01_Presentation/01_Swap_chain.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,28 @@ VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
318318
```
319319

320320
The swap extent is the resolution of the swap chain images and it's almost
321-
always exactly equal to the resolution of the window that we're drawing to. The
322-
range of the possible resolutions is defined in the `VkSurfaceCapabilitiesKHR`
323-
structure. Vulkan tells us to match the resolution of the window by setting the
324-
width and height in the `currentExtent` member. However, some window managers do
325-
allow us to differ here and this is indicated by setting the width and height in
326-
`currentExtent` to a special value: the maximum value of `uint32_t`. In that
327-
case we'll pick the resolution that best matches the window within the
328-
`minImageExtent` and `maxImageExtent` bounds.
321+
always exactly equal to the resolution of the window that we're drawing to _in
322+
pixels_ (more on that in a moment). The range of the possible resolutions is
323+
defined in the `VkSurfaceCapabilitiesKHR` structure. Vulkan tells us to match
324+
the resolution of the window by setting the width and height in the
325+
`currentExtent` member. However, some window managers do allow us to differ here
326+
and this is indicated by setting the width and height in `currentExtent` to a
327+
special value: the maximum value of `uint32_t`. In that case we'll pick the
328+
resolution that best matches the window within the `minImageExtent` and
329+
`maxImageExtent` bounds. But we must specify the resolution in the correct unit.
330+
331+
GLFW uses two units when measuring sizes: pixels and
332+
[screen coordinates](https://www.glfw.org/docs/latest/intro_guide.html#coordinate_systems).
333+
For example, the resolution `{WIDTH, HEIGHT}` that we specified earlier when
334+
creating the window is measured in screen coordinates. But Vulkan works with
335+
pixels, so the swap chain extent must be specified in pixels as well.
336+
Unfortunately, if you are using a high DPI display (like Apple's Retina
337+
display), screen coordinates don't correspond to pixels. Instead, due to the
338+
higher pixel density, the resolution of the window in pixel will be larger than
339+
the resolution in screen coordinates. So if Vulkan doesn't fix the swap extent
340+
for us, we can't just use the original `{WIDTH, HEIGHT}`. Instead, we must use
341+
`glfwGetFramebufferSize` to query the resolution of the window in pixel before
342+
matching it against the minimum and maximum image extent.
329343

330344
```c++
331345
#include <cstdint> // Necessary for UINT32_MAX
@@ -336,7 +350,13 @@ VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
336350
if (capabilities.currentExtent.width != UINT32_MAX) {
337351
return capabilities.currentExtent;
338352
} else {
339-
VkExtent2D actualExtent = {WIDTH, HEIGHT};
353+
int width, height;
354+
glfwGetFramebufferSize(window, &width, &height);
355+
356+
VkExtent2D actualExtent = {
357+
static_cast<uint32_t>(width),
358+
static_cast<uint32_t>(height)
359+
};
340360

341361
actualExtent.width = std::max(capabilities.minImageExtent.width, std::min(capabilities.maxImageExtent.width, actualExtent.width));
342362
actualExtent.height = std::max(capabilities.minImageExtent.height, std::min(capabilities.maxImageExtent.height, actualExtent.height));

en/03_Drawing_a_triangle/04_Swap_chain_recreation.md

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,11 @@ Instead I've opted to clean up the existing command buffers with the
114114
`vkFreeCommandBuffers` function. This way we can reuse the existing pool to
115115
allocate the new command buffers.
116116

117-
To handle window resizes properly, we also need to query the current size of the framebuffer to make sure that the swap chain images have the (new) right size. To do that change the `chooseSwapExtent` function to take the actual size into account:
118-
119-
```c++
120-
VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {
121-
if (capabilities.currentExtent.width != UINT32_MAX) {
122-
return capabilities.currentExtent;
123-
} else {
124-
int width, height;
125-
glfwGetFramebufferSize(window, &width, &height);
126-
127-
VkExtent2D actualExtent = {
128-
static_cast<uint32_t>(width),
129-
static_cast<uint32_t>(height)
130-
};
131-
132-
...
133-
}
134-
}
135-
```
117+
Note that in `chooseSwapExtent` we already query the new window resolution to
118+
make sure that the swap chain images have the (new) right size, so there's no
119+
need to modify `chooseSwapExtent` (remember that we already had to use
120+
`glfwGetFramebufferSize` get the resolution of the surface in pixel when
121+
creating the swap chain).
136122

137123
That's all it takes to recreate the swap chain! However, the disadvantage of
138124
this approach is that we need to stop all rendering before creating the new swap

0 commit comments

Comments
 (0)