-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch02ValidationLayers.diff
226 lines (205 loc) · 8.86 KB
/
Ch02ValidationLayers.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
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch01InstanceCreation.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch02ValidationLayers.java"
index b20e928..4bf9c62 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch01InstanceCreation.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch02ValidationLayers.java"
@@ -2,28 +2,75 @@ package javavulkantutorial;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
-import org.lwjgl.vulkan.VkApplicationInfo;
-import org.lwjgl.vulkan.VkInstance;
-import org.lwjgl.vulkan.VkInstanceCreateInfo;
+import org.lwjgl.vulkan.*;
+import java.nio.IntBuffer;
+import java.nio.LongBuffer;
+import java.util.HashSet;
+import java.util.Set;
+
+import static java.util.stream.Collectors.toSet;
import static org.lwjgl.glfw.GLFW.*;
-import static org.lwjgl.glfw.GLFW.glfwTerminate;
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.VK10.*;
-public class Ch01InstanceCreation {
+public class Ch02ValidationLayers {
private static class HelloTriangleApplication {
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 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);
+ }
+
+ }
+
// ======= FIELDS ======= //
private long window;
private VkInstance instance;
+ private long debugMessenger;
// ======= METHODS ======= //
@@ -54,6 +101,7 @@ public class Ch01InstanceCreation {
private void initVulkan() {
createInstance();
+ setupDebugMessenger();
}
private void mainLoop() {
@@ -66,6 +114,10 @@ public class Ch01InstanceCreation {
private void cleanup() {
+ if(ENABLE_VALIDATION_LAYERS) {
+ destroyDebugUtilsMessengerEXT(instance, debugMessenger, null);
+ }
+
vkDestroyInstance(instance, null);
glfwDestroyWindow(window);
@@ -75,6 +127,10 @@ public class Ch01InstanceCreation {
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
@@ -93,9 +149,16 @@ public class Ch01InstanceCreation {
createInfo.sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
createInfo.pApplicationInfo(appInfo);
// enabledExtensionCount is implicitly set when you call ppEnabledExtensionNames
- createInfo.ppEnabledExtensionNames(glfwGetRequiredInstanceExtensions());
- // same with enabledLayerCount
- createInfo.ppEnabledLayerNames(null);
+ createInfo.ppEnabledExtensionNames(getRequiredExtensions(stack));
+
+ if(ENABLE_VALIDATION_LAYERS) {
+
+ createInfo.ppEnabledLayerNames(validationLayersAsPointerBuffer(stack));
+
+ 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);
@@ -108,6 +171,84 @@ public class Ch01InstanceCreation {
}
}
+ 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 PointerBuffer validationLayersAsPointerBuffer(MemoryStack stack) {
+
+ PointerBuffer buffer = stack.mallocPointer(VALIDATION_LAYERS.size());
+
+ VALIDATION_LAYERS.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) {
@@ -116,4 +257,5 @@ public class Ch01InstanceCreation {
app.run();
}
+
}