-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
310 lines (275 loc) · 7.71 KB
/
mod.ts
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import type { FetchOptions } from "@denosaurs/plug";
import { dlopen } from "@denosaurs/plug";
/**
* The library version.
*/
export const VERSION = "0.3.0-dev";
/**
* The winit dynamic library symbols.
*/
type winitDylibSymbols = {
readonly spawn_window: {
readonly parameters: readonly [
"buffer",
"buffer",
"u32",
"u32",
"function",
"function",
"function"
];
readonly result: "void";
};
};
/**
* The loaded winit dynamic library.
*/
type winitDylib = Deno.DynamicLibrary<winitDylibSymbols>;
/**
* The winit window class.
*/
export class WinitWindow {
private dylibPromise: Promise<winitDylib>;
private system: "win32" | "cocoa" | "wayland" | "x11" | null = null;
private windowTitle: string = "Deno + winit";
private windowIconPath: string | null = null;
private width: number = 512;
private height: number = 512;
private presentationFormat: GPUTextureFormat = "bgra8unorm";
private setupFunction: (
device: GPUDevice,
context: GPUCanvasContext
) => void = () => {};
private drawFunction: (device: GPUDevice, context: GPUCanvasContext) => void =
() => {};
private resizeFunction: (width: number, height: number) => void = () => {};
/**
* The winit window constructor.
* @param forceX11 Whether to force X11 on Linux.
* @param windowTitle The window title.
* @param windowIconPath The window icon absolute path. Only supported on Windows and X11.
* @param width The window width in pixels.
* @param height The window height in pixels.
* @param presentationFormat The presentation format.
* @param setupFunction The setup function.
* @param drawFunction The draw function.
* @param resizeFunction The resize function.
*/
constructor({
forceX11 = false,
windowTitle,
windowIconPath,
width,
height,
presentationFormat,
setupFunction,
drawFunction,
resizeFunction,
}: {
forceX11?: boolean;
windowTitle?: string;
windowIconPath?: string;
width?: number;
height?: number;
presentationFormat?: GPUTextureFormat;
setupFunction?: (device: GPUDevice, context: GPUCanvasContext) => void;
drawFunction?: (device: GPUDevice, context: GPUCanvasContext) => void;
resizeFunction?: (width: number, height: number) => void;
}) {
if (windowTitle) {
this.windowTitle = windowTitle;
}
if (windowIconPath) {
this.windowIconPath = windowIconPath;
}
if (width) {
this.width = width;
}
if (height) {
this.height = height;
}
if (presentationFormat) {
this.presentationFormat = presentationFormat;
}
if (setupFunction) {
this.setupFunction = setupFunction;
}
if (drawFunction) {
this.drawFunction = drawFunction;
}
if (resizeFunction) {
this.resizeFunction = resizeFunction;
}
switch (Deno.build.os) {
case "linux":
if (forceX11) {
this.system = "x11";
} else {
this.system = "wayland";
}
break;
case "windows":
this.system = "win32";
break;
case "darwin":
this.system = "cocoa";
break;
default:
throw new Error("Unsupported OS.");
}
const options: FetchOptions = {
name: "deno_winit",
url: `https://github.com/fazil47/deno_winit/releases/download/v${VERSION}/`,
};
const symbols = {
spawn_window: {
parameters: [
"buffer",
"buffer",
"u32",
"u32",
"function",
"function",
"function",
],
result: "void",
},
} as const;
this.dylibPromise =
Deno.env.get("DENO_WINIT_LOCAL_LIB") === "1"
? dlopen_Local(symbols)
: dlopen(options, symbols);
}
/**
* Spawns the winit window.
*/
public async spawn(): Promise<void> {
if (!this.dylibPromise) {
throw new Error("Dynamic library not loaded.");
}
if (!this.system) {
throw new Error("System not supported.");
}
const adapter = await navigator.gpu.requestAdapter();
if (!adapter) {
throw new Error("No GPU adapter found.");
}
const device = await adapter.requestDevice();
if (!device) {
throw new Error("No GPU device found.");
}
let surface: Deno.UnsafeWindowSurface | null = null;
let context: GPUCanvasContext | null = null;
let windowHandle: Deno.PointerValue<unknown> | null = null;
let displayHandle: Deno.PointerValue<unknown> | null = null;
const setupSurfaceAndContext = (
winHandle: Deno.PointerValue<unknown>,
dispHandle: Deno.PointerValue<unknown>,
width: number,
height: number
) => {
if (!this.system) {
console.error("System not supported.");
return;
}
surface = new Deno.UnsafeWindowSurface(
this.system,
winHandle,
dispHandle
);
context = surface.getContext("webgpu");
context.configure({
device,
format: this.presentationFormat,
width,
height,
});
};
const setupFunctionFfiCallback = new Deno.UnsafeCallback(
{ parameters: ["pointer", "pointer", "u32", "u32"], result: "void" },
(winHandle, dispHandle, width, height) => {
windowHandle = winHandle;
displayHandle = dispHandle;
setupSurfaceAndContext(windowHandle, displayHandle, width, height);
if (!context) {
console.error("Context not initialized.");
return;
}
this.setupFunction(device, context);
}
);
const drawFunctionCallback = () => {
if (!surface) {
console.error("Surface not initialized.");
return;
}
if (!context) {
console.error("Context not initialized.");
return;
}
this.drawFunction(device, context);
surface.present();
};
const drawFunctionFfiCallback = new Deno.UnsafeCallback(
{ parameters: [], result: "void" },
drawFunctionCallback
);
const resizeFunctionFfiCallback = new Deno.UnsafeCallback(
{ parameters: ["u32", "u32"], result: "void" },
(width, height) => {
this.width = width;
this.height = height;
this.resizeFunction(width, height);
if (this.system === "cocoa") {
// On macOS, the surface and context needs to be recreated
// and the draw function needs to be called again
setupSurfaceAndContext(windowHandle, displayHandle, width, height);
drawFunctionCallback();
}
}
);
const dylib = await this.dylibPromise;
dylib.symbols.spawn_window(
asCString(this.windowTitle),
this.windowIconPath ? asCString(this.windowIconPath) : null,
this.width,
this.height,
setupFunctionFfiCallback.pointer,
drawFunctionFfiCallback.pointer,
resizeFunctionFfiCallback.pointer
);
}
}
/**
* Opens the local dynamic library.
* @param symbols The dynamic library symbols.
* @returns The dynamic library.
*/
function dlopen_Local(symbols: winitDylibSymbols): Promise<winitDylib> {
let libFileName = "";
switch (Deno.build.os) {
case "linux":
libFileName = "libdeno_winit.so";
break;
case "windows":
libFileName = "deno_winit.dll";
break;
case "darwin":
libFileName = "libdeno_winit.dylib";
break;
default:
throw new Error("Unsupported OS.");
}
// Open library and define exported symbols
const libPath = `./target/release/${libFileName}`;
return Promise.resolve(Deno.dlopen(libPath, symbols));
}
/**
* Converts a string to a C string.
* @param str The string.
* @returns The C string.
*/
function asCString(str: string): Uint8Array {
const enc = new TextEncoder();
return enc.encode(`${str}\0`);
}