-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCh16SwapChainRecreation.diff
225 lines (195 loc) · 8.66 KB
/
Ch16SwapChainRecreation.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch15HelloTriangle.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch16SwapChainRecreation.java"
index e6ca21a..a9e8226 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch15HelloTriangle.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch16SwapChainRecreation.java"
@@ -3,6 +3,7 @@ package javavulkantutorial;
import javavulkantutorial.ShaderSPIRVUtils.SPIRV;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
+import org.lwjgl.system.Pointer;
import org.lwjgl.vulkan.*;
import java.nio.ByteBuffer;
@@ -22,13 +23,13 @@ import static org.lwjgl.glfw.GLFWVulkan.glfwGetRequiredInstanceExtensions;
import static org.lwjgl.system.Configuration.DEBUG;
import static org.lwjgl.system.MemoryStack.stackGet;
import static org.lwjgl.system.MemoryStack.stackPush;
-import static org.lwjgl.system.MemoryUtil.*;
+import static org.lwjgl.system.MemoryUtil.NULL;
import static org.lwjgl.vulkan.EXTDebugUtils.*;
import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch15HelloTriangle {
+public class Ch16SwapChainRecreation {
private static class HelloTriangleApplication {
@@ -144,6 +145,8 @@ public class Ch15HelloTriangle {
private Map<Integer, Frame> imagesInFlight;
private int currentFrame;
+ boolean framebufferResize;
+
// ======= METHODS ======= //
public void run() {
@@ -160,7 +163,7 @@ public class Ch15HelloTriangle {
}
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
- glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
+ glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
String title = getClass().getEnclosingClass().getSimpleName();
@@ -169,6 +172,20 @@ public class Ch15HelloTriangle {
if(window == NULL) {
throw new RuntimeException("Cannot create window");
}
+
+ // In Java, we don't really need a user pointer here, because
+ // we can simply pass an instance method reference to glfwSetFramebufferSizeCallback
+ // However, I will show you how can you pass a user pointer to glfw in Java just for learning purposes:
+ // long userPointer = JNINativeInterface.NewGlobalRef(this);
+ // glfwSetWindowUserPointer(window, userPointer);
+ // Please notice that the reference must be freed manually with JNINativeInterface.nDeleteGlobalRef
+ glfwSetFramebufferSizeCallback(window, this::framebufferResizeCallback);
+ }
+
+ private void framebufferResizeCallback(long window, int width, int height) {
+ // HelloTriangleApplication app = MemoryUtil.memGlobalRefToObject(glfwGetWindowUserPointer(window));
+ // app.framebufferResize = true;
+ framebufferResize = true;
}
private void initVulkan() {
@@ -177,13 +194,8 @@ public class Ch15HelloTriangle {
createSurface();
pickPhysicalDevice();
createLogicalDevice();
- createSwapChain();
- createImageViews();
- createRenderPass();
- createGraphicsPipeline();
- createFramebuffers();
createCommandPool();
- createCommandBuffers();
+ createSwapChainObjects();
createSyncObjects();
}
@@ -198,20 +210,12 @@ public class Ch15HelloTriangle {
vkDeviceWaitIdle(device);
}
- private void cleanup() {
-
- inFlightFrames.forEach(frame -> {
-
- vkDestroySemaphore(device, frame.renderFinishedSemaphore(), null);
- vkDestroySemaphore(device, frame.imageAvailableSemaphore(), null);
- vkDestroyFence(device, frame.fence(), null);
- });
- imagesInFlight.clear();
-
- vkDestroyCommandPool(device, commandPool, null);
+ private void cleanupSwapChain() {
swapChainFramebuffers.forEach(framebuffer -> vkDestroyFramebuffer(device, framebuffer, null));
+ try(MemoryStack stack = stackPush()) {vkFreeCommandBuffers(device, commandPool, asPointerBuffer(stack, commandBuffers));}
+
vkDestroyPipeline(device, graphicsPipeline, null);
vkDestroyPipelineLayout(device, pipelineLayout, null);
@@ -221,6 +225,21 @@ public class Ch15HelloTriangle {
swapChainImageViews.forEach(imageView -> vkDestroyImageView(device, imageView, null));
vkDestroySwapchainKHR(device, swapChain, null);
+ }
+
+ private void cleanup() {
+
+ cleanupSwapChain();
+
+ inFlightFrames.forEach(frame -> {
+
+ vkDestroySemaphore(device, frame.renderFinishedSemaphore(), null);
+ vkDestroySemaphore(device, frame.imageAvailableSemaphore(), null);
+ vkDestroyFence(device, frame.fence(), null);
+ });
+ inFlightFrames.clear();
+
+ vkDestroyCommandPool(device, commandPool, null);
vkDestroyDevice(device, null);
@@ -237,6 +256,35 @@ public class Ch15HelloTriangle {
glfwTerminate();
}
+ private void recreateSwapChain() {
+
+ try(MemoryStack stack = stackPush()) {
+
+ IntBuffer width = stack.ints(0);
+ IntBuffer height = stack.ints(0);
+
+ while(width.get(0) == 0 && height.get(0) == 0) {
+ glfwGetFramebufferSize(window, width, height);
+ glfwWaitEvents();
+ }
+ }
+
+ vkDeviceWaitIdle(device);
+
+ cleanupSwapChain();
+
+ createSwapChainObjects();
+ }
+
+ private void createSwapChainObjects() {
+ createSwapChain();
+ createImageViews();
+ createRenderPass();
+ createGraphicsPipeline();
+ createFramebuffers();
+ createCommandBuffers();
+ }
+
private void createInstance() {
if(ENABLE_VALIDATION_LAYERS && !checkValidationLayerSupport()) {
@@ -866,7 +914,13 @@ public class Ch15HelloTriangle {
IntBuffer pImageIndex = stack.mallocInt(1);
- vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, thisFrame.imageAvailableSemaphore(), VK_NULL_HANDLE, pImageIndex);
+ int vkResult = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, thisFrame.imageAvailableSemaphore(), VK_NULL_HANDLE, pImageIndex);
+
+ if(vkResult == VK_ERROR_OUT_OF_DATE_KHR) {
+ recreateSwapChain();
+ return;
+ }
+
final int imageIndex = pImageIndex.get(0);
if(imagesInFlight.containsKey(imageIndex)) {
@@ -902,7 +956,14 @@ public class Ch15HelloTriangle {
presentInfo.pImageIndices(pImageIndex);
- vkQueuePresentKHR(presentQueue, presentInfo);
+ vkResult = vkQueuePresentKHR(presentQueue, presentInfo);
+
+ if(vkResult == VK_ERROR_OUT_OF_DATE_KHR || vkResult == VK_SUBOPTIMAL_KHR || framebufferResize) {
+ framebufferResize = false;
+ recreateSwapChain();
+ } else if(vkResult != VK_SUCCESS) {
+ throw new RuntimeException("Failed to present swap chain image");
+ }
currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
}
@@ -952,7 +1013,12 @@ public class Ch15HelloTriangle {
return capabilities.currentExtent();
}
- VkExtent2D actualExtent = VkExtent2D.malloc(stack).set(WIDTH, HEIGHT);
+ IntBuffer width = stackGet().ints(0);
+ IntBuffer height = stackGet().ints(0);
+
+ glfwGetFramebufferSize(window, width, height);
+
+ VkExtent2D actualExtent = VkExtent2D.malloc(stack).set(width.get(0), height.get(0));
VkExtent2D minExtent = capabilities.minImageExtent();
VkExtent2D maxExtent = capabilities.maxImageExtent();
@@ -1073,6 +1139,15 @@ public class Ch15HelloTriangle {
return buffer.rewind();
}
+ private PointerBuffer asPointerBuffer(MemoryStack stack, List<? extends Pointer> list) {
+
+ PointerBuffer buffer = stack.mallocPointer(list.size());
+
+ list.forEach(buffer::put);
+
+ return buffer.rewind();
+ }
+
private PointerBuffer getRequiredExtensions(MemoryStack stack) {
PointerBuffer glfwExtensions = glfwGetRequiredInstanceExtensions();