-
Notifications
You must be signed in to change notification settings - Fork 27
/
Ch07ImageViews.java
664 lines (447 loc) · 24.7 KB
/
Ch07ImageViews.java
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
package javavulkantutorial;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.vulkan.*;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
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.*;
import static org.lwjgl.glfw.GLFWVulkan.glfwCreateWindowSurface;
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.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 Ch07ImageViews {
private static class HelloTriangleApplication {
private static final int UINT32_MAX = 0xFFFFFFFF;
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
private static final boolean ENABLE_VALIDATION_LAYERS = DEBUG.get(true);
private static final Set<String> VALIDATION_LAYERS;
static {
if(ENABLE_VALIDATION_LAYERS) {
VALIDATION_LAYERS = new HashSet<>();
VALIDATION_LAYERS.add("VK_LAYER_KHRONOS_validation");
} else {
// We are not going to use it, so we don't create it
VALIDATION_LAYERS = null;
}
}
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);
System.err.println("Validation layer: " + callbackData.pMessageString());
return VK_FALSE;
}
private static int createDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerCreateInfoEXT createInfo,
VkAllocationCallbacks allocationCallbacks, LongBuffer pDebugMessenger) {
if(vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT") != NULL) {
return vkCreateDebugUtilsMessengerEXT(instance, createInfo, allocationCallbacks, pDebugMessenger);
}
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
private static void destroyDebugUtilsMessengerEXT(VkInstance instance, long debugMessenger, VkAllocationCallbacks allocationCallbacks) {
if(vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT") != NULL) {
vkDestroyDebugUtilsMessengerEXT(instance, debugMessenger, allocationCallbacks);
}
}
private class QueueFamilyIndices {
// We use Integer to use null as the empty value
private Integer graphicsFamily;
private Integer presentFamily;
private boolean isComplete() {
return graphicsFamily != null && presentFamily != null;
}
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 List<Long> swapChainImageViews;
private int swapChainImageFormat;
private VkExtent2D swapChainExtent;
// ======= METHODS ======= //
public void run() {
initWindow();
initVulkan();
mainLoop();
cleanup();
}
private void initWindow() {
if(!glfwInit()) {
throw new RuntimeException("Cannot initialize GLFW");
}
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
String title = getClass().getEnclosingClass().getSimpleName();
window = glfwCreateWindow(WIDTH, HEIGHT, title, NULL, NULL);
if(window == NULL) {
throw new RuntimeException("Cannot create window");
}
}
private void initVulkan() {
createInstance();
setupDebugMessenger();
createSurface();
pickPhysicalDevice();
createLogicalDevice();
createSwapChain();
createImageViews();
}
private void mainLoop() {
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
}
private void cleanup() {
swapChainImageViews.forEach(imageView -> vkDestroyImageView(device, imageView, null));
vkDestroySwapchainKHR(device, swapChain, null);
vkDestroyDevice(device, null);
if(ENABLE_VALIDATION_LAYERS) {
destroyDebugUtilsMessengerEXT(instance, debugMessenger, null);
}
vkDestroySurfaceKHR(instance, surface, null);
vkDestroyInstance(instance, null);
glfwDestroyWindow(window);
glfwTerminate();
}
private void createInstance() {
if(ENABLE_VALIDATION_LAYERS && !checkValidationLayerSupport()) {
throw new RuntimeException("Validation requested but not supported");
}
try(MemoryStack stack = stackPush()) {
// Use calloc to initialize the structs with 0s. Otherwise, the program can crash due to random values
VkApplicationInfo appInfo = VkApplicationInfo.calloc(stack);
appInfo.sType(VK_STRUCTURE_TYPE_APPLICATION_INFO);
appInfo.pApplicationName(stack.UTF8Safe("Hello Triangle"));
appInfo.applicationVersion(VK_MAKE_VERSION(1, 0, 0));
appInfo.pEngineName(stack.UTF8Safe("No Engine"));
appInfo.engineVersion(VK_MAKE_VERSION(1, 0, 0));
appInfo.apiVersion(VK_API_VERSION_1_0);
VkInstanceCreateInfo createInfo = VkInstanceCreateInfo.calloc(stack);
createInfo.sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
createInfo.pApplicationInfo(appInfo);
// enabledExtensionCount is implicitly set when you call ppEnabledExtensionNames
createInfo.ppEnabledExtensionNames(getRequiredExtensions(stack));
if(ENABLE_VALIDATION_LAYERS) {
createInfo.ppEnabledLayerNames(asPointerBuffer(stack, VALIDATION_LAYERS));
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo = VkDebugUtilsMessengerCreateInfoEXT.calloc(stack);
populateDebugMessengerCreateInfo(debugCreateInfo);
createInfo.pNext(debugCreateInfo.address());
}
// We need to retrieve the pointer of the created instance
PointerBuffer instancePtr = stack.mallocPointer(1);
if(vkCreateInstance(createInfo, null, instancePtr) != VK_SUCCESS) {
throw new RuntimeException("Failed to create instance");
}
instance = new VkInstance(instancePtr.get(0), createInfo);
}
}
private void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo) {
debugCreateInfo.sType(VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT);
debugCreateInfo.messageSeverity(VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT);
debugCreateInfo.messageType(VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT);
debugCreateInfo.pfnUserCallback(HelloTriangleApplication::debugCallback);
}
private void setupDebugMessenger() {
if(!ENABLE_VALIDATION_LAYERS) {
return;
}
try(MemoryStack stack = stackPush()) {
VkDebugUtilsMessengerCreateInfoEXT createInfo = VkDebugUtilsMessengerCreateInfoEXT.calloc(stack);
populateDebugMessengerCreateInfo(createInfo);
LongBuffer pDebugMessenger = stack.longs(VK_NULL_HANDLE);
if(createDebugUtilsMessengerEXT(instance, createInfo, null, pDebugMessenger) != VK_SUCCESS) {
throw new RuntimeException("Failed to set up debug messenger");
}
debugMessenger = pDebugMessenger.get(0);
}
}
private void createSurface() {
try(MemoryStack stack = stackPush()) {
LongBuffer pSurface = stack.longs(VK_NULL_HANDLE);
if(glfwCreateWindowSurface(instance, window, null, pSurface) != VK_SUCCESS) {
throw new RuntimeException("Failed to create window surface");
}
surface = pSurface.get(0);
}
}
private void pickPhysicalDevice() {
try(MemoryStack stack = stackPush()) {
IntBuffer deviceCount = stack.ints(0);
vkEnumeratePhysicalDevices(instance, deviceCount, null);
if(deviceCount.get(0) == 0) {
throw new RuntimeException("Failed to find GPUs with Vulkan support");
}
PointerBuffer ppPhysicalDevices = stack.mallocPointer(deviceCount.get(0));
vkEnumeratePhysicalDevices(instance, deviceCount, ppPhysicalDevices);
for(int i = 0;i < ppPhysicalDevices.capacity();i++) {
VkPhysicalDevice device = new VkPhysicalDevice(ppPhysicalDevices.get(i), instance);
if(isDeviceSuitable(device)) {
physicalDevice = device;
return;
}
}
throw new RuntimeException("Failed to find a suitable GPU");
}
}
private void createLogicalDevice() {
try(MemoryStack stack = stackPush()) {
QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
int[] uniqueQueueFamilies = indices.unique();
VkDeviceQueueCreateInfo.Buffer queueCreateInfos = VkDeviceQueueCreateInfo.calloc(uniqueQueueFamilies.length, stack);
for(int i = 0;i < uniqueQueueFamilies.length;i++) {
VkDeviceQueueCreateInfo queueCreateInfo = queueCreateInfos.get(i);
queueCreateInfo.sType(VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO);
queueCreateInfo.queueFamilyIndex(uniqueQueueFamilies[i]);
queueCreateInfo.pQueuePriorities(stack.floats(1.0f));
}
VkPhysicalDeviceFeatures deviceFeatures = VkPhysicalDeviceFeatures.calloc(stack);
VkDeviceCreateInfo createInfo = VkDeviceCreateInfo.calloc(stack);
createInfo.sType(VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
createInfo.pQueueCreateInfos(queueCreateInfos);
// queueCreateInfoCount is automatically set
createInfo.pEnabledFeatures(deviceFeatures);
createInfo.ppEnabledExtensionNames(asPointerBuffer(stack, DEVICE_EXTENSIONS));
if(ENABLE_VALIDATION_LAYERS) {
createInfo.ppEnabledLayerNames(asPointerBuffer(stack, VALIDATION_LAYERS));
}
PointerBuffer pDevice = stack.pointers(VK_NULL_HANDLE);
if(vkCreateDevice(physicalDevice, createInfo, null, pDevice) != VK_SUCCESS) {
throw new RuntimeException("Failed to create logical device");
}
device = new VkDevice(pDevice.get(0), physicalDevice, createInfo);
PointerBuffer pQueue = stack.pointers(VK_NULL_HANDLE);
vkGetDeviceQueue(device, indices.graphicsFamily, 0, pQueue);
graphicsQueue = new VkQueue(pQueue.get(0), device);
vkGetDeviceQueue(device, indices.presentFamily, 0, pQueue);
presentQueue = new VkQueue(pQueue.get(0), device);
}
}
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 void createImageViews() {
swapChainImageViews = new ArrayList<>(swapChainImages.size());
try(MemoryStack stack = stackPush()) {
LongBuffer pImageView = stack.mallocLong(1);
for(long swapChainImage : swapChainImages) {
VkImageViewCreateInfo createInfo = VkImageViewCreateInfo.calloc(stack);
createInfo.sType(VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
createInfo.image(swapChainImage);
createInfo.viewType(VK_IMAGE_VIEW_TYPE_2D);
createInfo.format(swapChainImageFormat);
createInfo.components().r(VK_COMPONENT_SWIZZLE_IDENTITY);
createInfo.components().g(VK_COMPONENT_SWIZZLE_IDENTITY);
createInfo.components().b(VK_COMPONENT_SWIZZLE_IDENTITY);
createInfo.components().a(VK_COMPONENT_SWIZZLE_IDENTITY);
createInfo.subresourceRange().aspectMask(VK_IMAGE_ASPECT_COLOR_BIT);
createInfo.subresourceRange().baseMipLevel(0);
createInfo.subresourceRange().levelCount(1);
createInfo.subresourceRange().baseArrayLayer(0);
createInfo.subresourceRange().layerCount(1);
if (vkCreateImageView(device, createInfo, null, pImageView) != VK_SUCCESS) {
throw new RuntimeException("Failed to create image views");
}
swapChainImageViews.add(pImageView.get(0));
}
}
}
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);
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) {
QueueFamilyIndices indices = new QueueFamilyIndices();
try(MemoryStack stack = stackPush()) {
IntBuffer queueFamilyCount = stack.ints(0);
vkGetPhysicalDeviceQueueFamilyProperties(device, queueFamilyCount, null);
VkQueueFamilyProperties.Buffer queueFamilies = VkQueueFamilyProperties.malloc(queueFamilyCount.get(0), stack);
vkGetPhysicalDeviceQueueFamilyProperties(device, queueFamilyCount, queueFamilies);
IntBuffer presentSupport = stack.ints(VK_FALSE);
for(int i = 0;i < queueFamilies.capacity() || !indices.isComplete();i++) {
if((queueFamilies.get(i).queueFlags() & VK_QUEUE_GRAPHICS_BIT) != 0) {
indices.graphicsFamily = i;
}
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, presentSupport);
if(presentSupport.get(0) == VK_TRUE) {
indices.presentFamily = i;
}
}
return indices;
}
}
private PointerBuffer asPointerBuffer(MemoryStack stack, Collection<String> collection) {
PointerBuffer buffer = stack.mallocPointer(collection.size());
collection.stream()
.map(stack::UTF8)
.forEach(buffer::put);
return buffer.rewind();
}
private PointerBuffer getRequiredExtensions(MemoryStack stack) {
PointerBuffer glfwExtensions = glfwGetRequiredInstanceExtensions();
if(ENABLE_VALIDATION_LAYERS) {
PointerBuffer extensions = stack.mallocPointer(glfwExtensions.capacity() + 1);
extensions.put(glfwExtensions);
extensions.put(stack.UTF8(VK_EXT_DEBUG_UTILS_EXTENSION_NAME));
// Rewind the buffer before returning it to reset its position back to 0
return extensions.rewind();
}
return glfwExtensions;
}
private boolean checkValidationLayerSupport() {
try(MemoryStack stack = stackPush()) {
IntBuffer layerCount = stack.ints(0);
vkEnumerateInstanceLayerProperties(layerCount, null);
VkLayerProperties.Buffer availableLayers = VkLayerProperties.malloc(layerCount.get(0), stack);
vkEnumerateInstanceLayerProperties(layerCount, availableLayers);
Set<String> availableLayerNames = availableLayers.stream()
.map(VkLayerProperties::layerNameString)
.collect(toSet());
return availableLayerNames.containsAll(VALIDATION_LAYERS);
}
}
}
public static void main(String[] args) {
HelloTriangleApplication app = new HelloTriangleApplication();
app.run();
}
}