-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch06SwapChainCreation.diff
319 lines (289 loc) · 12.8 KB
/
Ch06SwapChainCreation.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch05WindowSurface.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch06SwapChainCreation.java"
index c5bae31..149ef45 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch05WindowSurface.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch06SwapChainCreation.java"
@@ -6,9 +6,9 @@ import org.lwjgl.vulkan.*;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.*;
import java.util.stream.IntStream;
+import java.util.stream.Stream;
import static java.util.stream.Collectors.toSet;
import static org.lwjgl.glfw.GLFW.*;
@@ -19,14 +19,16 @@ import static org.lwjgl.system.MemoryStack.stackGet;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.system.MemoryUtil.NULL;
import static org.lwjgl.vulkan.EXTDebugUtils.*;
-import static org.lwjgl.vulkan.KHRSurface.vkDestroySurfaceKHR;
-import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfaceSupportKHR;
+import static org.lwjgl.vulkan.KHRSurface.*;
+import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch05WindowSurface {
+public class Ch06SwapChainCreation {
private static class HelloTriangleApplication {
+ private static final int UINT32_MAX = 0xFFFFFFFF;
+
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
@@ -43,6 +45,11 @@ public class Ch05WindowSurface {
}
}
+ private static final Set<String> DEVICE_EXTENSIONS = Stream.of(VK_KHR_SWAPCHAIN_EXTENSION_NAME)
+ .collect(toSet());
+
+
+
private static int debugCallback(int messageSeverity, int messageType, long pCallbackData, long pUserData) {
VkDebugUtilsMessengerCallbackDataEXT callbackData = VkDebugUtilsMessengerCallbackDataEXT.create(pCallbackData);
@@ -83,19 +90,39 @@ public class Ch05WindowSurface {
public int[] unique() {
return IntStream.of(graphicsFamily, presentFamily).distinct().toArray();
}
+
+ public int[] array() {
+ return new int[] {graphicsFamily, presentFamily};
+ }
+ }
+
+ private class SwapChainSupportDetails {
+
+ private VkSurfaceCapabilitiesKHR capabilities;
+ private VkSurfaceFormatKHR.Buffer formats;
+ private IntBuffer presentModes;
+
}
// ======= FIELDS ======= //
private long window;
+
private VkInstance instance;
private long debugMessenger;
private long surface;
+
private VkPhysicalDevice physicalDevice;
private VkDevice device;
+
private VkQueue graphicsQueue;
private VkQueue presentQueue;
+ private long swapChain;
+ private List<Long> swapChainImages;
+ private int swapChainImageFormat;
+ private VkExtent2D swapChainExtent;
+
// ======= METHODS ======= //
public void run() {
@@ -129,6 +156,7 @@ public class Ch05WindowSurface {
createSurface();
pickPhysicalDevice();
createLogicalDevice();
+ createSwapChain();
}
private void mainLoop() {
@@ -141,6 +169,8 @@ public class Ch05WindowSurface {
private void cleanup() {
+ vkDestroySwapchainKHR(device, swapChain, null);
+
vkDestroyDevice(device, null);
if(ENABLE_VALIDATION_LAYERS) {
@@ -184,7 +214,7 @@ public class Ch05WindowSurface {
if(ENABLE_VALIDATION_LAYERS) {
- createInfo.ppEnabledLayerNames(validationLayersAsPointerBuffer(stack));
+ createInfo.ppEnabledLayerNames(asPointerBuffer(stack, VALIDATION_LAYERS));
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo = VkDebugUtilsMessengerCreateInfoEXT.calloc(stack);
populateDebugMessengerCreateInfo(debugCreateInfo);
@@ -302,8 +332,10 @@ public class Ch05WindowSurface {
createInfo.pEnabledFeatures(deviceFeatures);
+ createInfo.ppEnabledExtensionNames(asPointerBuffer(stack, DEVICE_EXTENSIONS));
+
if(ENABLE_VALIDATION_LAYERS) {
- createInfo.ppEnabledLayerNames(validationLayersAsPointerBuffer(stack));
+ createInfo.ppEnabledLayerNames(asPointerBuffer(stack, VALIDATION_LAYERS));
}
PointerBuffer pDevice = stack.pointers(VK_NULL_HANDLE);
@@ -324,11 +356,176 @@ public class Ch05WindowSurface {
}
}
+ private void createSwapChain() {
+
+ try(MemoryStack stack = stackPush()) {
+
+ SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice, stack);
+
+ VkSurfaceFormatKHR surfaceFormat = chooseSwapSurfaceFormat(swapChainSupport.formats);
+ int presentMode = chooseSwapPresentMode(swapChainSupport.presentModes);
+ VkExtent2D extent = chooseSwapExtent(stack, swapChainSupport.capabilities);
+
+ IntBuffer imageCount = stack.ints(swapChainSupport.capabilities.minImageCount() + 1);
+
+ if(swapChainSupport.capabilities.maxImageCount() > 0 && imageCount.get(0) > swapChainSupport.capabilities.maxImageCount()) {
+ imageCount.put(0, swapChainSupport.capabilities.maxImageCount());
+ }
+
+ VkSwapchainCreateInfoKHR createInfo = VkSwapchainCreateInfoKHR.calloc(stack);
+
+ createInfo.sType(VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR);
+ createInfo.surface(surface);
+
+ // Image settings
+ createInfo.minImageCount(imageCount.get(0));
+ createInfo.imageFormat(surfaceFormat.format());
+ createInfo.imageColorSpace(surfaceFormat.colorSpace());
+ createInfo.imageExtent(extent);
+ createInfo.imageArrayLayers(1);
+ createInfo.imageUsage(VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
+
+ QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
+
+ if(!indices.graphicsFamily.equals(indices.presentFamily)) {
+ createInfo.imageSharingMode(VK_SHARING_MODE_CONCURRENT);
+ createInfo.pQueueFamilyIndices(stack.ints(indices.graphicsFamily, indices.presentFamily));
+ } else {
+ createInfo.imageSharingMode(VK_SHARING_MODE_EXCLUSIVE);
+ }
+
+ createInfo.preTransform(swapChainSupport.capabilities.currentTransform());
+ createInfo.compositeAlpha(VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR);
+ createInfo.presentMode(presentMode);
+ createInfo.clipped(true);
+
+ createInfo.oldSwapchain(VK_NULL_HANDLE);
+
+ LongBuffer pSwapChain = stack.longs(VK_NULL_HANDLE);
+
+ if(vkCreateSwapchainKHR(device, createInfo, null, pSwapChain) != VK_SUCCESS) {
+ throw new RuntimeException("Failed to create swap chain");
+ }
+
+ swapChain = pSwapChain.get(0);
+
+ vkGetSwapchainImagesKHR(device, swapChain, imageCount, null);
+
+ LongBuffer pSwapchainImages = stack.mallocLong(imageCount.get(0));
+
+ vkGetSwapchainImagesKHR(device, swapChain, imageCount, pSwapchainImages);
+
+ swapChainImages = new ArrayList<>(imageCount.get(0));
+
+ for(int i = 0;i < pSwapchainImages.capacity();i++) {
+ swapChainImages.add(pSwapchainImages.get(i));
+ }
+
+ swapChainImageFormat = surfaceFormat.format();
+ swapChainExtent = VkExtent2D.create().set(extent);
+ }
+ }
+
+ private VkSurfaceFormatKHR chooseSwapSurfaceFormat(VkSurfaceFormatKHR.Buffer availableFormats) {
+ return availableFormats.stream()
+ .filter(availableFormat -> availableFormat.format() == VK_FORMAT_B8G8R8_UNORM)
+ .filter(availableFormat -> availableFormat.colorSpace() == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
+ .findAny()
+ .orElse(availableFormats.get(0));
+ }
+
+ private int chooseSwapPresentMode(IntBuffer availablePresentModes) {
+
+ for(int i = 0;i < availablePresentModes.capacity();i++) {
+ if(availablePresentModes.get(i) == VK_PRESENT_MODE_MAILBOX_KHR) {
+ return availablePresentModes.get(i);
+ }
+ }
+
+ return VK_PRESENT_MODE_FIFO_KHR;
+ }
+
+ private VkExtent2D chooseSwapExtent(MemoryStack stack, VkSurfaceCapabilitiesKHR capabilities) {
+
+ if(capabilities.currentExtent().width() != UINT32_MAX) {
+ return capabilities.currentExtent();
+ }
+
+ VkExtent2D actualExtent = VkExtent2D.malloc(stack).set(WIDTH, HEIGHT);
+
+ VkExtent2D minExtent = capabilities.minImageExtent();
+ VkExtent2D maxExtent = capabilities.maxImageExtent();
+
+ actualExtent.width(clamp(minExtent.width(), maxExtent.width(), actualExtent.width()));
+ actualExtent.height(clamp(minExtent.height(), maxExtent.height(), actualExtent.height()));
+
+ return actualExtent;
+ }
+
+ private int clamp(int min, int max, int value) {
+ return Math.max(min, Math.min(max, value));
+ }
+
private boolean isDeviceSuitable(VkPhysicalDevice device) {
QueueFamilyIndices indices = findQueueFamilies(device);
- return indices.isComplete();
+ boolean extensionsSupported = checkDeviceExtensionSupport(device);
+ boolean swapChainAdequate = false;
+
+ if(extensionsSupported) {
+ try(MemoryStack stack = stackPush()) {
+ SwapChainSupportDetails swapChainSupport = querySwapChainSupport(device, stack);
+ swapChainAdequate = swapChainSupport.formats.hasRemaining() && swapChainSupport.presentModes.hasRemaining();
+ }
+ }
+
+ return indices.isComplete() && extensionsSupported && swapChainAdequate;
+ }
+
+ private boolean checkDeviceExtensionSupport(VkPhysicalDevice device) {
+
+ try(MemoryStack stack = stackPush()) {
+
+ IntBuffer extensionCount = stack.ints(0);
+
+ vkEnumerateDeviceExtensionProperties(device, (String)null, extensionCount, null);
+
+ VkExtensionProperties.Buffer availableExtensions = VkExtensionProperties.malloc(extensionCount.get(0), stack);
+
+ vkEnumerateDeviceExtensionProperties(device, (String)null, extensionCount, availableExtensions);
+
+ return availableExtensions.stream()
+ .map(VkExtensionProperties::extensionNameString)
+ .collect(toSet())
+ .containsAll(DEVICE_EXTENSIONS);
+ }
+ }
+
+ private SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device, MemoryStack stack) {
+
+ SwapChainSupportDetails details = new SwapChainSupportDetails();
+
+ details.capabilities = VkSurfaceCapabilitiesKHR.malloc(stack);
+ vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, details.capabilities);
+
+ IntBuffer count = stack.ints(0);
+
+ vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, count, null);
+
+ if(count.get(0) != 0) {
+ details.formats = VkSurfaceFormatKHR.malloc(count.get(0), stack);
+ vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, count, details.formats);
+ }
+
+ vkGetPhysicalDeviceSurfacePresentModesKHR(device,surface, count, null);
+
+ if(count.get(0) != 0) {
+ details.presentModes = stack.mallocInt(count.get(0));
+ vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, count, details.presentModes);
+ }
+
+ return details;
}
private QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {
@@ -364,11 +561,11 @@ public class Ch05WindowSurface {
}
}
- private PointerBuffer validationLayersAsPointerBuffer(MemoryStack stack) {
+ private PointerBuffer asPointerBuffer(MemoryStack stack, Collection<String> collection) {
- PointerBuffer buffer = stack.mallocPointer(VALIDATION_LAYERS.size());
+ PointerBuffer buffer = stack.mallocPointer(collection.size());
- VALIDATION_LAYERS.stream()
+ collection.stream()
.map(stack::UTF8)
.forEach(buffer::put);