-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch01InstanceCreation.java
119 lines (82 loc) · 3.61 KB
/
Ch01InstanceCreation.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
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 static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.glfw.GLFW.glfwTerminate;
import static org.lwjgl.glfw.GLFWVulkan.glfwGetRequiredInstanceExtensions;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.system.MemoryUtil.NULL;
import static org.lwjgl.vulkan.VK10.*;
public class Ch01InstanceCreation {
private static class HelloTriangleApplication {
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
// ======= FIELDS ======= //
private long window;
private VkInstance instance;
// ======= 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();
}
private void mainLoop() {
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
}
private void cleanup() {
vkDestroyInstance(instance, null);
glfwDestroyWindow(window);
glfwTerminate();
}
private void createInstance() {
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(glfwGetRequiredInstanceExtensions());
// same with enabledLayerCount
createInfo.ppEnabledLayerNames(null);
// 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);
}
}
}
public static void main(String[] args) {
HelloTriangleApplication app = new HelloTriangleApplication();
app.run();
}
}