-
Notifications
You must be signed in to change notification settings - Fork 27
/
Ch05WindowSurface.diff
154 lines (127 loc) · 6.39 KB
/
Ch05WindowSurface.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
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch04LogicalDevice.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch05WindowSurface.java"
index 10d7127..c5bae31 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch04LogicalDevice.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch05WindowSurface.java"
@@ -12,15 +12,18 @@ import java.util.stream.IntStream;
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.vkDestroySurfaceKHR;
+import static org.lwjgl.vulkan.KHRSurface.vkGetPhysicalDeviceSurfaceSupportKHR;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch04LogicalDevice {
+public class Ch05WindowSurface {
private static class HelloTriangleApplication {
@@ -71,11 +74,15 @@ public class Ch04LogicalDevice {
// We use Integer to use null as the empty value
private Integer graphicsFamily;
+ private Integer presentFamily;
private boolean isComplete() {
- return graphicsFamily != null;
+ return graphicsFamily != null && presentFamily != null;
}
+ public int[] unique() {
+ return IntStream.of(graphicsFamily, presentFamily).distinct().toArray();
+ }
}
// ======= FIELDS ======= //
@@ -83,9 +90,11 @@ public class Ch04LogicalDevice {
private long window;
private VkInstance instance;
private long debugMessenger;
+ private long surface;
private VkPhysicalDevice physicalDevice;
private VkDevice device;
private VkQueue graphicsQueue;
+ private VkQueue presentQueue;
// ======= METHODS ======= //
@@ -117,6 +126,7 @@ public class Ch04LogicalDevice {
private void initVulkan() {
createInstance();
setupDebugMessenger();
+ createSurface();
pickPhysicalDevice();
createLogicalDevice();
}
@@ -137,6 +147,8 @@ public class Ch04LogicalDevice {
destroyDebugUtilsMessengerEXT(instance, debugMessenger, null);
}
+ vkDestroySurfaceKHR(instance, surface, null);
+
vkDestroyInstance(instance, null);
glfwDestroyWindow(window);
@@ -219,6 +231,20 @@ public class Ch04LogicalDevice {
}
}
+ 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()) {
@@ -255,11 +281,16 @@ public class Ch04LogicalDevice {
QueueFamilyIndices indices = findQueueFamilies(physicalDevice);
- VkDeviceQueueCreateInfo.Buffer queueCreateInfos = VkDeviceQueueCreateInfo.calloc(1, stack);
+ int[] uniqueQueueFamilies = indices.unique();
+
+ VkDeviceQueueCreateInfo.Buffer queueCreateInfos = VkDeviceQueueCreateInfo.calloc(uniqueQueueFamilies.length, stack);
- queueCreateInfos.sType(VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO);
- queueCreateInfos.queueFamilyIndex(indices.graphicsFamily);
- queueCreateInfos.pQueuePriorities(stack.floats(1.0f));
+ 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);
@@ -283,11 +314,13 @@ public class Ch04LogicalDevice {
device = new VkDevice(pDevice.get(0), physicalDevice, createInfo);
- PointerBuffer pGraphicsQueue = stack.pointers(VK_NULL_HANDLE);
+ PointerBuffer pQueue = stack.pointers(VK_NULL_HANDLE);
- vkGetDeviceQueue(device, indices.graphicsFamily, 0, pGraphicsQueue);
+ vkGetDeviceQueue(device, indices.graphicsFamily, 0, pQueue);
+ graphicsQueue = new VkQueue(pQueue.get(0), device);
- graphicsQueue = new VkQueue(pGraphicsQueue.get(0), device);
+ vkGetDeviceQueue(device, indices.presentFamily, 0, pQueue);
+ presentQueue = new VkQueue(pQueue.get(0), device);
}
}
@@ -312,10 +345,20 @@ public class Ch04LogicalDevice {
vkGetPhysicalDeviceQueueFamilyProperties(device, queueFamilyCount, queueFamilies);
- IntStream.range(0, queueFamilies.capacity())
- .filter(index -> (queueFamilies.get(index).queueFlags() & VK_QUEUE_GRAPHICS_BIT) != 0)
- .findFirst()
- .ifPresent(index -> indices.graphicsFamily = index);
+ 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;
}