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

Skip to content

Commit 90fc62c

Browse files
committed
Remove explicit handling of resize now that drivers properly report VK_ERROR_OUT_OF_DATE_KHR
1 parent 2242fc5 commit 90fc62c

14 files changed

Lines changed: 4 additions & 174 deletions

03_Drawing_a_triangle/04_Swap_chain_recreation.md

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -114,83 +114,17 @@ image from the old swap chain are still in-flight. You need to pass the previous
114114
swap chain to the `oldSwapChain` field in the `VkSwapchainCreateInfoKHR` struct
115115
and destroy the old swap chain as soon as you've finished using it.
116116

117-
## Window resizing
118-
119-
Now we just need to figure out when swap chain recreation is necessary and call
120-
our new `recreateSwapChain` function. One of the most common conditions is
121-
resizing of the window. Let's make the window resizable and catch that event.
122-
Change the `initWindow` function to no longer include the `GLFW_RESIZABLE` line
123-
or change its argument from `GLFW_FALSE` to `GLFW_TRUE`.
124-
125-
```c++
126-
void initWindow() {
127-
glfwInit();
128-
129-
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
130-
131-
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
132-
133-
glfwSetWindowUserPointer(window, this);
134-
glfwSetWindowSizeCallback(window, HelloTriangleApplication::onWindowResized);
135-
}
136-
137-
...
138-
139-
static void onWindowResized(GLFWwindow* window, int width, int height) {
140-
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
141-
app->recreateSwapChain();
142-
}
143-
```
144-
145-
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-
void recreateSwapChain() {
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-
182117
## Suboptimal or out-of-date swap chain
183118

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
186121
`vkQueuePresentKHR` functions can return the following special values to
187122
indicate this.
188123

189124
* `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.
191126
* `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.
194128

195129
```c++
196130
VkResult result = vkAcquireNextImageKHR(device, swapChain, std::numeric_limits<uint64_t>::max(), imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex);

code/16_swap_chain_recreation.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ class HelloTriangleApplication {
103103
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
104104

105105
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
106-
107-
glfwSetWindowUserPointer(window, this);
108-
glfwSetWindowSizeCallback(window, HelloTriangleApplication::onWindowResized);
109106
}
110107

111108
void initVulkan() {
@@ -173,11 +170,6 @@ class HelloTriangleApplication {
173170
glfwTerminate();
174171
}
175172

176-
static void onWindowResized(GLFWwindow* window, int width, int height) {
177-
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
178-
app->recreateSwapChain();
179-
}
180-
181173
void recreateSwapChain() {
182174
int width, height;
183175
glfwGetWindowSize(window, &width, &height);

code/17_vertex_input.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,6 @@ class HelloTriangleApplication {
142142
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
143143

144144
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
145-
146-
glfwSetWindowUserPointer(window, this);
147-
glfwSetWindowSizeCallback(window, HelloTriangleApplication::onWindowResized);
148145
}
149146

150147
void initVulkan() {
@@ -212,11 +209,6 @@ class HelloTriangleApplication {
212209
glfwTerminate();
213210
}
214211

215-
static void onWindowResized(GLFWwindow* window, int width, int height) {
216-
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
217-
app->recreateSwapChain();
218-
}
219-
220212
void recreateSwapChain() {
221213
int width, height;
222214
glfwGetWindowSize(window, &width, &height);

code/18_vertex_buffer.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ class HelloTriangleApplication {
146146
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
147147

148148
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
149-
150-
glfwSetWindowUserPointer(window, this);
151-
glfwSetWindowSizeCallback(window, HelloTriangleApplication::onWindowResized);
152149
}
153150

154151
void initVulkan() {
@@ -220,11 +217,6 @@ class HelloTriangleApplication {
220217
glfwTerminate();
221218
}
222219

223-
static void onWindowResized(GLFWwindow* window, int width, int height) {
224-
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
225-
app->recreateSwapChain();
226-
}
227-
228220
void recreateSwapChain() {
229221
int width, height;
230222
glfwGetWindowSize(window, &width, &height);

code/19_staging_buffer.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ class HelloTriangleApplication {
146146
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
147147

148148
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
149-
150-
glfwSetWindowUserPointer(window, this);
151-
glfwSetWindowSizeCallback(window, HelloTriangleApplication::onWindowResized);
152149
}
153150

154151
void initVulkan() {
@@ -220,11 +217,6 @@ class HelloTriangleApplication {
220217
glfwTerminate();
221218
}
222219

223-
static void onWindowResized(GLFWwindow* window, int width, int height) {
224-
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
225-
app->recreateSwapChain();
226-
}
227-
228220
void recreateSwapChain() {
229221
int width, height;
230222
glfwGetWindowSize(window, &width, &height);

code/20_index_buffer.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,6 @@ class HelloTriangleApplication {
153153
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
154154

155155
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
156-
157-
glfwSetWindowUserPointer(window, this);
158-
glfwSetWindowSizeCallback(window, HelloTriangleApplication::onWindowResized);
159156
}
160157

161158
void initVulkan() {
@@ -231,11 +228,6 @@ class HelloTriangleApplication {
231228
glfwTerminate();
232229
}
233230

234-
static void onWindowResized(GLFWwindow* window, int width, int height) {
235-
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
236-
app->recreateSwapChain();
237-
}
238-
239231
void recreateSwapChain() {
240232
int width, height;
241233
glfwGetWindowSize(window, &width, &height);

code/21_descriptor_layout.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ class HelloTriangleApplication {
166166
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
167167

168168
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
169-
170-
glfwSetWindowUserPointer(window, this);
171-
glfwSetWindowSizeCallback(window, HelloTriangleApplication::onWindowResized);
172169
}
173170

174171
void initVulkan() {
@@ -252,11 +249,6 @@ class HelloTriangleApplication {
252249
glfwTerminate();
253250
}
254251

255-
static void onWindowResized(GLFWwindow* window, int width, int height) {
256-
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
257-
app->recreateSwapChain();
258-
}
259-
260252
void recreateSwapChain() {
261253
int width, height;
262254
glfwGetWindowSize(window, &width, &height);

code/22_descriptor_set.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@ class HelloTriangleApplication {
169169
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
170170

171171
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
172-
173-
glfwSetWindowUserPointer(window, this);
174-
glfwSetWindowSizeCallback(window, HelloTriangleApplication::onWindowResized);
175172
}
176173

177174
void initVulkan() {
@@ -259,11 +256,6 @@ class HelloTriangleApplication {
259256
glfwTerminate();
260257
}
261258

262-
static void onWindowResized(GLFWwindow* window, int width, int height) {
263-
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
264-
app->recreateSwapChain();
265-
}
266-
267259
void recreateSwapChain() {
268260
int width, height;
269261
glfwGetWindowSize(window, &width, &height);

code/23_texture_image.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,6 @@ class HelloTriangleApplication {
175175
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
176176

177177
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
178-
179-
glfwSetWindowUserPointer(window, this);
180-
glfwSetWindowSizeCallback(window, HelloTriangleApplication::onWindowResized);
181178
}
182179

183180
void initVulkan() {
@@ -269,11 +266,6 @@ class HelloTriangleApplication {
269266
glfwTerminate();
270267
}
271268

272-
static void onWindowResized(GLFWwindow* window, int width, int height) {
273-
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
274-
app->recreateSwapChain();
275-
}
276-
277269
void recreateSwapChain() {
278270
int width, height;
279271
glfwGetWindowSize(window, &width, &height);

code/24_sampler.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ class HelloTriangleApplication {
177177
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
178178

179179
window = glfwCreateWindow(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);
180-
181-
glfwSetWindowUserPointer(window, this);
182-
glfwSetWindowSizeCallback(window, HelloTriangleApplication::onWindowResized);
183180
}
184181

185182
void initVulkan() {
@@ -276,11 +273,6 @@ class HelloTriangleApplication {
276273
glfwTerminate();
277274
}
278275

279-
static void onWindowResized(GLFWwindow* window, int width, int height) {
280-
HelloTriangleApplication* app = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(window));
281-
app->recreateSwapChain();
282-
}
283-
284276
void recreateSwapChain() {
285277
int width, height;
286278
glfwGetWindowSize(window, &width, &height);

0 commit comments

Comments
 (0)