-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathworker.js
89 lines (76 loc) · 1.9 KB
/
worker.js
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
async function createWebGPUDevice(data, adapterDesc, results) {
const adapter = await navigator.gpu.requestAdapter(adapterDesc);
if (adapter) {
results.adapter = true;
const device = await adapter.requestDevice();
if (device) {
results.device = true;
const {canvas} = data;
if (canvas) {
results.context = !!canvas.getContext('webgpu');
}
}
}
}
async function checkWebGPU(id, data) {
const results = {}
if (navigator.gpu) {
results.gpu = true;
}
try {
await createWebGPUDevice(data, {}, results);
} catch (e) {
results.error = (e.message || e).toString();
}
if (!results.adapter) {
try {
await createWebGPUDevice(data, {compatibilityMode: true}, results);
results.compat = true;
delete results.error;
} catch (e) {
results.error = (e.message || e).toString();
}
}
try {
const offscreenCanvas = new OffscreenCanvas(300, 150);
results.offscreen = true;
const ctx = offscreenCanvas.getContext('2d');
results.twoD = !!ctx;
} catch (e) {
console.error(e);
}
results.rAF = !!self.requestAnimationFrame;
this.postMessage({id, data: results});
}
async function ping(id) {
this.postMessage({id, data: { }});
}
const handlers = {
ping,
checkWebGPU,
};
function handleMessage(e) {
try {
const {command, id, data} = e.data;
const handler = handlers[command];
handler.call(this, id, data);
} catch(error) {
this.postMessage({data: 'messageerror'});
}
}
self.onmessage = function(e) {
handleMessage.call(e.source || self, e);
};
self.onconnect = function(e) {
const port = e.ports[0];
port.onmessage = function(event) {
handleMessage.call(port, event);
};
port.onmessageerror = function() {
port.postMessage({data: 'messageerror'});
};
};
self.onmessageerror = (e) => {
const source = e.source || self;
source.postMessage({data: 'messageerror'});
}