-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvulkan.ts
More file actions
86 lines (66 loc) · 2.31 KB
/
Copy pathvulkan.ts
File metadata and controls
86 lines (66 loc) · 2.31 KB
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
import EventEmitter from 'node:events';
import { glfw } from '@node-3d/glfw';
import type { TVulkanDevice } from '@node-3d/glfw';
const isVulkanSupported = glfw.vulkanSupported();
console.log('Vulkan is', isVulkanSupported ? 'supported.' : 'unsupported.');
let instancePtr: number | null = null;
if (isVulkanSupported) {
const vkExt = glfw.getRequiredInstanceExtensions();
console.log('Vulkan extensions:', vkExt);
instancePtr = glfw.vulkanCreateInstance();
console.log('Created instance:', instancePtr);
}
const emitterObj = new EventEmitter();
const emitter = { emit: (t: string, e: unknown) => 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(1280, 720, emitter, 'vulkan example', undefined, true);
glfw.windowHint(glfw.CLIENT_API, glfw.OPENGL_API);
let deviceInfo: TVulkanDevice | null = null;
if (instancePtr) {
const surfacePtr = glfw.createWindowSurface(instancePtr, windowPtr);
console.log('Created surface:', surfacePtr);
deviceInfo = glfw.vulkanCreateDevice(instancePtr);
console.log('Created device:', deviceInfo);
}
if (instancePtr && 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 (instancePtr && 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);