-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvulkan.mjs
91 lines (68 loc) · 2.19 KB
/
vulkan.mjs
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
import glfw from '../index.js';
import EventEmitter from 'node:events';
const isVulkanSupported = glfw.vulkanSupported();
console.log('Vulkan is', isVulkanSupported ? 'supported.' : 'unsupported.');
let instancePtr = null;
if (isVulkanSupported) {
const vkExt = glfw.getRequiredInstanceExtensions();
console.log('Vulkan extensions:', vkExt);
instancePtr = glfw.vulkanCreateInstance('vulkan-example', vkExt);
console.log('Created instance:', instancePtr);
}
const emitterObj = new EventEmitter();
const emitter = { emit: (t, e) => emitterObj.emit(t, e) };
glfw.windowHint(glfw.RESIZABLE, glfw.TRUE);
glfw.windowHint(glfw.VISIBLE, glfw.TRUE);
glfw.windowHint(glfw.AUTO_ICONIFY, glfw.FALSE);
glfw.windowHint(glfw.DECORATED, glfw.TRUE);
glfw.windowHint(glfw.CLIENT_API, glfw.NO_API);
const windowPtr = glfw.createWindow(
800,
600,
emitter,
'vulkan example',
null,
true,
);
glfw.windowHint(glfw.CLIENT_API, glfw.OPENGL_API);
let deviceInfo = null;
if (instancePtr) {
const surfacePtr = glfw.createWindowSurface(instancePtr, windowPtr);
console.log('Created surface:', surfacePtr);
deviceInfo = glfw.vulkanCreateDevice(instancePtr, windowPtr);
console.log('Created device:', deviceInfo);
const isSupported = glfw.getPhysicalDevicePresentationSupport(
instancePtr, deviceInfo.physicalDevice, deviceInfo.queueFamily,
);
console.log('Presentation supported:', isSupported);
const vkGetDeviceProcAddrPtr = glfw.getInstanceProcAddress(instancePtr, 'vkGetDeviceProcAddr');
console.log('Got `vkGetDeviceProcAddr`:', vkGetDeviceProcAddrPtr);
}
const draw = () => {
glfw.pollEvents();
};
const close = () => {
if (deviceInfo) {
glfw.vulkanDestroyDevice(instancePtr, deviceInfo.device);
console.log('Deleted device.');
}
// Close the window and terminate GLFW
glfw.destroyWindow(windowPtr);
if (instancePtr) {
glfw.vulkanDestroyInstance(instancePtr);
console.log('Deleted instance.');
}
glfw.terminate();
process.exit(0);
};
const loopFunc = () => {
const shouldClose = glfw.windowShouldClose(windowPtr);
const isEscPressed = glfw.getKey(windowPtr, glfw.KEY_ESCAPE);
if (shouldClose || isEscPressed) {
close();
return;
}
draw();
setImmediate(loopFunc);
};
setImmediate(loopFunc);