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
The `glfwSetWindowSizeCallback` function can be used to specify a callback for
146
-
the window resize event. Unfortunately it only accepts a function pointer as
147
-
argument, so we can't directly use a member function. Luckily GLFW allows us to
148
-
store an arbitrary pointer in the window object with `glfwSetWindowUserPointer`,
149
-
so we can specify a static class member and get the original class instance back
150
-
with `glfwGetWindowUserPointer`. We can then proceed to call
151
-
`recreateSwapChain`, but only if the size of the window is non-zero. This case
152
-
occurs when the window is minimized and it will cause swap chain creation to
153
-
fail.
154
-
155
-
The `chooseSwapExtent` function should also be updated to take the current width
156
-
and height of the window into account instead of the initial `WIDTH` and
157
-
`HEIGHT`:
158
-
159
-
```c++
160
-
int width, height;
161
-
glfwGetWindowSize(window, &width, &height);
162
-
163
-
VkExtent2D actualExtent = {width, height};
164
-
```
165
-
166
-
We have to be careful with this, because the width and height may be 0 if the
167
-
window is minimized, for example. Therefore we should check if it makes sense
168
-
to recreate the swap chain:
169
-
170
-
```c++
171
-
voidrecreateSwapChain() {
172
-
int width, height;
173
-
glfwGetWindowSize(window, &width, &height);
174
-
if (width == 0 || height == 0) return;
175
-
176
-
vkDeviceWaitIdle(device);
177
-
178
-
...
179
-
}
180
-
```
181
-
182
117
## Suboptimal or out-of-date swap chain
183
118
184
-
It is also possible for Vulkan to tell us that the swap chain is no longer
185
-
compatible during presentation. The `vkAcquireNextImageKHR` and
119
+
Now we just need to figure out when swap chain recreation is necessary and call
120
+
our new `recreateSwapChain` function. Luckily, Vulkan will usually just tell us that the swap chain is no longer adequate during presentation. The `vkAcquireNextImageKHR` and
186
121
`vkQueuePresentKHR` functions can return the following special values to
187
122
indicate this.
188
123
189
124
*`VK_ERROR_OUT_OF_DATE_KHR`: The swap chain has become incompatible with the
190
-
surface and can no longer be used for rendering.
125
+
surface and can no longer be used for rendering. Usually happens after a window resize.
191
126
*`VK_SUBOPTIMAL_KHR`: The swap chain can still be used to successfully present
192
-
to the surface, but the surface properties are no longer matched exactly. For
193
-
example, the platform may be simply resizing the image to fit the window now.
127
+
to the surface, but the surface properties are no longer matched exactly.
194
128
195
129
```c++
196
130
VkResult result = vkAcquireNextImageKHR(device, swapChain, std::numeric_limits<uint64_t>::max(), imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex);
0 commit comments