@lincyaw/addon-exec-webgpu 0.0.0-stage → 1.0.0-rc.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +29 -2
- package/package.json +43 -3
- package/src/client/executor.ts +81 -0
- package/src/client/index.ts +3 -0
- package/src/client/plugin.ts +33 -0
- package/src/client/protocol.ts +9 -0
- package/src/client/webgpu.worker.ts +17 -0
- package/src/domain/compute.ts +57 -0
- package/src/domain/config.ts +23 -0
- package/src/domain/index.ts +9 -0
- package/src/domain/messages.ts +69 -0
- package/src/domain/params.ts +151 -0
- package/src/domain/result.ts +20 -0
- package/src/domain/summary.ts +46 -0
- package/src/node/presmium.ts +32 -0
- package/src/node/recorder.ts +119 -0
- package/src/node/vite.ts +24 -0
- package/src/shared/gpu-compute.d.mts +4 -0
- package/src/shared/gpu-compute.mjs +172 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lincyaw/addon-exec-webgpu
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The browser's GPU: WGSL compute shaders, a Python bridge and jax-js, recorded in Chromium at build time.
|
|
4
|
+
|
|
5
|
+
Part of [Presmium](https://github.com/Lincyaw/presmium), which compiles canonical lecture.json teaching documents into web pages, slides, Jupyter notebooks and narrated playback.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add -D @lincyaw/addon-exec-webgpu
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Add this package to `metadata.plugins` in the document's `lecture.json`.
|
|
14
|
+
|
|
15
|
+
It is a Presmium plugin: its components work on the web page and in the slides. It needs `@lincyaw/cli` and the renderers in the same project, and declares the Presmium packages it shares with them as peer dependencies (Presmium 1.x).
|
|
16
|
+
|
|
17
|
+
## What it provides
|
|
18
|
+
|
|
19
|
+
- An executor for WGSL code in a run-panel component: WebGPU compute shaders run in the browser and can be recorded in Playwright's Chromium at build time; list it together with `@lincyaw/addon-exec`.
|
|
20
|
+
- jax-js for JavaScript and TypeScript code, and the Python bridge `presmium_gpu`.
|
|
21
|
+
- Named environments of `kind: webgpu`.
|
|
22
|
+
|
|
23
|
+
## Documentation
|
|
24
|
+
|
|
25
|
+
- [Author guide](https://github.com/Lincyaw/presmium/blob/main/site/guide/locales/en/lecture.json) (Chinese: `site/guide/lecture.json`)
|
|
26
|
+
- [Document contract](https://github.com/Lincyaw/presmium/blob/main/.agents/skills/presmium-author/references/document.md): normalized entities, parameters, states and shared validation
|
|
27
|
+
|
|
28
|
+
## License
|
|
29
|
+
|
|
30
|
+
Apache-2.0
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/addon-exec-webgpu",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0-rc.10",
|
|
4
|
+
"description": "Presmium plugin: WGSL compute shaders and jax-js on the browser's GPU, recorded in Chromium at build time",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/Lincyaw/presmium.git",
|
|
9
|
+
"directory": "packages/addon-exec-webgpu"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/Lincyaw/presmium/tree/main/packages/addon-exec-webgpu#readme",
|
|
12
|
+
"bugs": "https://github.com/Lincyaw/presmium/issues",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"presmium"
|
|
15
|
+
],
|
|
16
|
+
"type": "module",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=22.19"
|
|
19
|
+
},
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
"./domain": "./src/domain/index.ts",
|
|
25
|
+
"./client": "./src/client/index.ts",
|
|
26
|
+
"./presmium": "./src/node/presmium.ts",
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"src"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@jax-js/jax": "0.1.25",
|
|
34
|
+
"playwright-chromium": "1.64.1",
|
|
35
|
+
"yaml": "^2.9.1"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@lincyaw/kernel": "^1.0.0-rc.10",
|
|
39
|
+
"@lincyaw/host": "^1.0.0-rc.10"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@lincyaw/cli": "1.0.0-rc.10",
|
|
43
|
+
"@lincyaw/host": "1.0.0-rc.10",
|
|
44
|
+
"@lincyaw/kernel": "1.0.0-rc.10"
|
|
45
|
+
}
|
|
6
46
|
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { currentLocale } from '@lincyaw/host'
|
|
2
|
+
import { failedRun } from '@lincyaw/kernel'
|
|
3
|
+
import type { Executor, RunResult } from '@lincyaw/kernel'
|
|
4
|
+
import { computeRequest, isWgsl, MESSAGES, paramsSummary, programError, runResultOf } from '../domain/index.ts'
|
|
5
|
+
import type { WorkerReply, WorkerRequest } from './protocol.ts'
|
|
6
|
+
|
|
7
|
+
const STOP_GRACE_MS = 200
|
|
8
|
+
|
|
9
|
+
export type WebgpuExecutor = Executor & {
|
|
10
|
+
dispose(): void
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createWebgpuExecutor(options: { id: string, timeoutMs: number }): WebgpuExecutor {
|
|
14
|
+
const { id } = options
|
|
15
|
+
const running = new Map<Worker, () => void>()
|
|
16
|
+
let disposed = false
|
|
17
|
+
return {
|
|
18
|
+
id,
|
|
19
|
+
runsIn: 'browser',
|
|
20
|
+
supports: isWgsl,
|
|
21
|
+
params(lang) {
|
|
22
|
+
if (!isWgsl(lang)) return undefined
|
|
23
|
+
const text = MESSAGES[currentLocale()]
|
|
24
|
+
return { labels: text.labels, fields: ['buffers'], summary: args => paramsSummary(args, text) }
|
|
25
|
+
},
|
|
26
|
+
run(request, signal) {
|
|
27
|
+
const text = MESSAGES[currentLocale()]
|
|
28
|
+
if (disposed || signal?.aborted) return Promise.resolve(failedRun(id, text.cancelled))
|
|
29
|
+
const made = computeRequest(request.code, request.args, text)
|
|
30
|
+
if ('error' in made) return Promise.resolve(programError(id, made.error, 0))
|
|
31
|
+
return new Promise<RunResult>((resolve) => {
|
|
32
|
+
if (signal?.aborted) return resolve(failedRun(id, text.cancelled))
|
|
33
|
+
const worker = new Worker(new URL('./webgpu.worker.ts', import.meta.url), { type: 'module', name: 'presmium-webgpu' })
|
|
34
|
+
const started = performance.now()
|
|
35
|
+
const elapsed = () => Math.round(performance.now() - started)
|
|
36
|
+
const timeoutMs = request.timeoutMs ?? options.timeoutMs
|
|
37
|
+
let settled = false
|
|
38
|
+
let stopping: ReturnType<typeof setTimeout> | undefined
|
|
39
|
+
const end = () => {
|
|
40
|
+
clearTimeout(stopping)
|
|
41
|
+
running.delete(worker)
|
|
42
|
+
worker.terminate()
|
|
43
|
+
}
|
|
44
|
+
const finish = (result: RunResult) => {
|
|
45
|
+
if (settled) return
|
|
46
|
+
settled = true
|
|
47
|
+
clearTimeout(timer)
|
|
48
|
+
signal?.removeEventListener('abort', onAbort)
|
|
49
|
+
resolve(result)
|
|
50
|
+
}
|
|
51
|
+
const stop = (result: RunResult) => {
|
|
52
|
+
if (settled) return
|
|
53
|
+
finish(result)
|
|
54
|
+
worker.postMessage({ type: 'stop' } satisfies WorkerRequest)
|
|
55
|
+
stopping = setTimeout(end, STOP_GRACE_MS)
|
|
56
|
+
}
|
|
57
|
+
const timer = setTimeout(() => stop({ ...failedRun(id, text.timedOut(timeoutMs), elapsed()), timedOut: true }), timeoutMs)
|
|
58
|
+
const onAbort = () => stop(failedRun(id, text.cancelled, elapsed()))
|
|
59
|
+
signal?.addEventListener('abort', onAbort)
|
|
60
|
+
running.set(worker, () => {
|
|
61
|
+
finish(failedRun(id, text.cancelled, elapsed()))
|
|
62
|
+
end()
|
|
63
|
+
})
|
|
64
|
+
worker.onmessage = ({ data }: MessageEvent<WorkerReply>) => {
|
|
65
|
+
end()
|
|
66
|
+
if (data.type === 'done') finish(runResultOf(id, data.outcome, elapsed(), text))
|
|
67
|
+
}
|
|
68
|
+
worker.onerror = (event) => {
|
|
69
|
+
end()
|
|
70
|
+
finish(failedRun(id, text.crashed(event.message), elapsed()))
|
|
71
|
+
}
|
|
72
|
+
worker.postMessage({ type: 'run', request: made.request } satisfies WorkerRequest)
|
|
73
|
+
})
|
|
74
|
+
},
|
|
75
|
+
dispose() {
|
|
76
|
+
disposed = true
|
|
77
|
+
running.forEach(cancel => cancel())
|
|
78
|
+
running.clear()
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { environmentExecutor, EXECUTOR } from '@lincyaw/kernel'
|
|
2
|
+
import type { PluginDefinition, Unsubscribe } from '@lincyaw/kernel'
|
|
3
|
+
import { version } from '../../package.json'
|
|
4
|
+
import { DEFAULT_TIMEOUT_MS, EXEC_WEBGPU_ID, WEBGPU_EXECUTOR_ID, WEBGPU_KIND } from '../domain/index.ts'
|
|
5
|
+
import { createWebgpuExecutor } from './executor.ts'
|
|
6
|
+
import type { WebgpuExecutor } from './executor.ts'
|
|
7
|
+
|
|
8
|
+
let executors: WebgpuExecutor[] = []
|
|
9
|
+
let registrations: Unsubscribe[] = []
|
|
10
|
+
|
|
11
|
+
function release(): void {
|
|
12
|
+
registrations.forEach(unregister => unregister())
|
|
13
|
+
executors.forEach(executor => executor.dispose())
|
|
14
|
+
registrations = []
|
|
15
|
+
executors = []
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const execWebgpuPlugin: PluginDefinition = {
|
|
19
|
+
manifest: { id: EXEC_WEBGPU_ID, name: { 'zh-CN': 'WebGPU 运行', en: 'WebGPU Runner' }, version, kernel: '^1.0.0', requires: [{ token: EXECUTOR }], capabilities: ['exec'] },
|
|
20
|
+
setup(ctx) {
|
|
21
|
+
release()
|
|
22
|
+
const router = ctx.get(EXECUTOR)
|
|
23
|
+
const named = Object.entries(ctx.environments ?? {}).filter(([, spec]) => spec.kind === WEBGPU_KIND).map(([name]) => environmentExecutor(name))
|
|
24
|
+
for (const id of [WEBGPU_EXECUTOR_ID, ...named]) {
|
|
25
|
+
const executor = createWebgpuExecutor({ id, timeoutMs: DEFAULT_TIMEOUT_MS })
|
|
26
|
+
executors.push(executor)
|
|
27
|
+
registrations.push(router.register(executor))
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
dispose: release,
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default execWebgpuPlugin
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ComputeOutcome, ComputeRequest } from '../domain/index.ts'
|
|
2
|
+
|
|
3
|
+
export type WorkerRequest =
|
|
4
|
+
| { type: 'run', request: ComputeRequest }
|
|
5
|
+
| { type: 'stop' }
|
|
6
|
+
|
|
7
|
+
export type WorkerReply =
|
|
8
|
+
| { type: 'done', outcome: ComputeOutcome }
|
|
9
|
+
| { type: 'stopped' }
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// <reference lib="webworker" />
|
|
2
|
+
import { compute } from '../shared/gpu-compute.mjs'
|
|
3
|
+
import type { WorkerReply, WorkerRequest } from './protocol.ts'
|
|
4
|
+
|
|
5
|
+
declare const self: DedicatedWorkerGlobalScope
|
|
6
|
+
|
|
7
|
+
let device: { destroy(): void } | undefined
|
|
8
|
+
const reply = (message: WorkerReply) => self.postMessage(message)
|
|
9
|
+
|
|
10
|
+
self.onmessage = async ({ data }: MessageEvent<WorkerRequest>) => {
|
|
11
|
+
if (data.type === 'stop') {
|
|
12
|
+
device?.destroy()
|
|
13
|
+
return reply({ type: 'stopped' })
|
|
14
|
+
}
|
|
15
|
+
const gpu = (self.navigator as WorkerNavigator & { gpu?: unknown }).gpu
|
|
16
|
+
reply({ type: 'done', outcome: await compute(gpu, data.request, { device: (d) => { device = d } }) })
|
|
17
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export type ScalarType = 'f32' | 'i32' | 'u32'
|
|
2
|
+
|
|
3
|
+
export interface ComputeBuffer {
|
|
4
|
+
name: string
|
|
5
|
+
group: number
|
|
6
|
+
binding: number
|
|
7
|
+
usage: 'storage' | 'uniform'
|
|
8
|
+
access: 'read' | 'read_write'
|
|
9
|
+
type: ScalarType
|
|
10
|
+
length: number
|
|
11
|
+
data?: number[]
|
|
12
|
+
fill?: number
|
|
13
|
+
read: boolean
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ComputeRequest {
|
|
17
|
+
code: string
|
|
18
|
+
entry?: string
|
|
19
|
+
buffers: ComputeBuffer[]
|
|
20
|
+
dispatch: [number, number, number]
|
|
21
|
+
repeat: number
|
|
22
|
+
features: string[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface AdapterDescription {
|
|
26
|
+
vendor: string
|
|
27
|
+
architecture: string
|
|
28
|
+
description: string
|
|
29
|
+
fallback: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface BufferSummary {
|
|
33
|
+
name: string
|
|
34
|
+
type: ScalarType
|
|
35
|
+
length: number
|
|
36
|
+
head: number[]
|
|
37
|
+
tail: number[]
|
|
38
|
+
min: number
|
|
39
|
+
max: number
|
|
40
|
+
mean: number
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface Timing {
|
|
44
|
+
ms: number
|
|
45
|
+
samples: number
|
|
46
|
+
source: 'gpu' | 'wall'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type Unavailable = 'no-webgpu' | 'no-adapter' | 'feature' | 'limit' | 'device-lost'
|
|
50
|
+
|
|
51
|
+
export type ComputeOutcome =
|
|
52
|
+
| { ok: true, buffers: BufferSummary[], timing: Timing, adapter: AdapterDescription }
|
|
53
|
+
| { ok: false, reason: Unavailable | 'shader' | 'validation', detail: string, adapter?: AdapterDescription }
|
|
54
|
+
|
|
55
|
+
export function isUnavailable(reason: string): reason is Unavailable {
|
|
56
|
+
return ['no-webgpu', 'no-adapter', 'feature', 'limit', 'device-lost'].includes(reason)
|
|
57
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { languageId } from '@lincyaw/kernel'
|
|
2
|
+
import type { EnvironmentSpec } from '@lincyaw/kernel'
|
|
3
|
+
|
|
4
|
+
export const EXEC_WEBGPU_ID = 'presmium.execWebgpu'
|
|
5
|
+
|
|
6
|
+
export const WEBGPU_EXECUTOR_ID = 'presmium.webgpu'
|
|
7
|
+
|
|
8
|
+
export const WEBGPU_KIND = 'webgpu'
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_TIMEOUT_MS = 10_000
|
|
11
|
+
|
|
12
|
+
export const LIMITS = { maxBytes: 128 * 1024 * 1024, maxRepeat: 1000 }
|
|
13
|
+
|
|
14
|
+
export function isWgsl(lang: string): boolean {
|
|
15
|
+
return languageId(lang) === 'wgsl'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type AdapterChoice = 'auto' | 'gpu' | 'swiftshader'
|
|
19
|
+
|
|
20
|
+
export function adapterChoice(spec: EnvironmentSpec | undefined): AdapterChoice {
|
|
21
|
+
const value = spec?.adapter
|
|
22
|
+
return value === 'gpu' || value === 'swiftshader' ? value : 'auto'
|
|
23
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { isUnavailable } from './compute.ts'
|
|
2
|
+
export type { AdapterDescription, BufferSummary, ComputeBuffer, ComputeOutcome, ComputeRequest, ScalarType, Timing, Unavailable } from './compute.ts'
|
|
3
|
+
export { adapterChoice, DEFAULT_TIMEOUT_MS, EXEC_WEBGPU_ID, isWgsl, LIMITS, WEBGPU_EXECUTOR_ID, WEBGPU_KIND } from './config.ts'
|
|
4
|
+
export type { AdapterChoice } from './config.ts'
|
|
5
|
+
export { MESSAGES } from './messages.ts'
|
|
6
|
+
export type { WebgpuMessages } from './messages.ts'
|
|
7
|
+
export { computeRequest, paramsSummary, shaderBuffers } from './params.ts'
|
|
8
|
+
export { programError, runResultOf } from './result.ts'
|
|
9
|
+
export { adapterFingerprint, adapterName, bufferLine, failureText, formatNumber, summaryText } from './summary.ts'
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Locale } from '@lincyaw/kernel'
|
|
2
|
+
|
|
3
|
+
const zhCN = {
|
|
4
|
+
noWebgpu: '浏览器没有 WebGPU',
|
|
5
|
+
noAdapter: '没有可用的 GPU 适配器',
|
|
6
|
+
missingFeature: (features: string) => `GPU 适配器缺少特性 ${features}`,
|
|
7
|
+
missingLimit: (detail: string) => `GPU 适配器的限制不够:${detail}`,
|
|
8
|
+
deviceLost: (detail: string) => `GPU 设备丢失${detail ? `:${detail}` : ''}`,
|
|
9
|
+
timedOut: (ms: number) => `GPU 运行超过 ${ms} ms,已销毁设备并停止`,
|
|
10
|
+
crashed: (error: string) => `GPU 运行出错:${error}`,
|
|
11
|
+
cancelled: '已取消',
|
|
12
|
+
shader: (detail: string) => `着色器有错误:\n${detail}`,
|
|
13
|
+
validation: (detail: string) => `WebGPU 报告错误:${detail}`,
|
|
14
|
+
notYaml: (name: string, error: string) => `:params: 中的 ${name} 不是合法的 YAML:${error}`,
|
|
15
|
+
unknownParam: (name: string) => `:params: 中的 ${name} 不是 buffers、dispatch、entry、repeat、features 之一`,
|
|
16
|
+
noBuffers: ':params: 要写 buffers:着色器中每个缓冲区的数据或大小,例如 {a: [1, 2], out: {size: 2}}',
|
|
17
|
+
bufferShape: (name: string) => `缓冲区 ${name} 要写成数字列表,或 {data, size, fill, type, read}`,
|
|
18
|
+
bufferSize: (name: string) => `缓冲区 ${name} 要有 data 或 size(值的个数)`,
|
|
19
|
+
noDeclaration: (name: string) => `着色器中没有名为 ${name} 的缓冲区(var<storage> 或 var<uniform>)`,
|
|
20
|
+
noBuffer: (name: string) => `着色器的缓冲区 ${name} 没有写在 :params: 的 buffers 中`,
|
|
21
|
+
unsupportedType: (name: string, type: string) => `缓冲区 ${name} 的类型 ${type} 不受支持(f32、i32、u32)`,
|
|
22
|
+
badDispatch: 'dispatch 要写成 1 到 3 个正整数(每个维度的工作组数)',
|
|
23
|
+
badRepeat: (max: number) => `repeat 要是 1 到 ${max} 的整数`,
|
|
24
|
+
badEntry: 'entry 要写成入口函数的名字',
|
|
25
|
+
badFeatures: 'features 要写成特性名的列表,例如 [shader-f16]',
|
|
26
|
+
tooLarge: (bytes: number, max: number) => `缓冲区共 ${bytes} 字节,超过规模限制 ${max} 字节`,
|
|
27
|
+
stats: (min: string, max: string, mean: string) => `最小 ${min},最大 ${max},平均 ${mean}`,
|
|
28
|
+
time: (ms: string, samples: number, gpu: boolean, emulated: boolean) =>
|
|
29
|
+
`计时:每次派发 ${ms} ms(${[samples > 1 ? `${samples} 次的中位数` : '', gpu ? 'GPU 时间戳' : '挂钟时间', emulated ? 'CPU 模拟' : ''].filter(Boolean).join(';')})`,
|
|
30
|
+
adapter: (name: string, fallback: boolean) => `适配器:${name}${fallback ? '(回退适配器)' : ''}`,
|
|
31
|
+
labels: { buffers: '缓冲区', dispatch: '工作组数', entry: '入口函数', repeat: '重复次数', features: '所需特性' } as Record<string, string>,
|
|
32
|
+
values: (count: number) => `${count} 个值`,
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type WebgpuMessages = typeof zhCN
|
|
36
|
+
|
|
37
|
+
const en: WebgpuMessages = {
|
|
38
|
+
noWebgpu: 'This browser has no WebGPU',
|
|
39
|
+
noAdapter: 'No GPU adapter is available',
|
|
40
|
+
missingFeature: features => `The GPU adapter lacks the feature ${features}`,
|
|
41
|
+
missingLimit: detail => `The GPU adapter’s limits are too low: ${detail}`,
|
|
42
|
+
deviceLost: detail => `The GPU device was lost${detail ? `: ${detail}` : ''}`,
|
|
43
|
+
timedOut: ms => `The GPU ran over ${ms} ms; the device was destroyed and the run stopped`,
|
|
44
|
+
crashed: error => `The GPU run failed: ${error}`,
|
|
45
|
+
cancelled: 'Canceled',
|
|
46
|
+
shader: detail => `The shader has errors:\n${detail}`,
|
|
47
|
+
validation: detail => `WebGPU reported an error: ${detail}`,
|
|
48
|
+
notYaml: (name, error) => `${name} in :params: is not valid YAML: ${error}`,
|
|
49
|
+
unknownParam: name => `${name} in :params: is not one of buffers, dispatch, entry, repeat, features`,
|
|
50
|
+
noBuffers: ':params: needs buffers: the data or size of each buffer of the shader, e.g. {a: [1, 2], out: {size: 2}}',
|
|
51
|
+
bufferShape: name => `The buffer ${name} is a list of numbers, or {data, size, fill, type, read}`,
|
|
52
|
+
bufferSize: name => `The buffer ${name} needs data or size (its number of values)`,
|
|
53
|
+
noDeclaration: name => `The shader has no buffer named ${name} (var<storage> or var<uniform>)`,
|
|
54
|
+
noBuffer: name => `The shader’s buffer ${name} is not in the buffers of :params:`,
|
|
55
|
+
unsupportedType: (name, type) => `The type ${type} of the buffer ${name} is not supported (f32, i32, u32)`,
|
|
56
|
+
badDispatch: 'dispatch is 1 to 3 positive integers (workgroups in each dimension)',
|
|
57
|
+
badRepeat: max => `repeat is an integer from 1 to ${max}`,
|
|
58
|
+
badEntry: 'entry is the name of the entry point',
|
|
59
|
+
badFeatures: 'features is a list of feature names, e.g. [shader-f16]',
|
|
60
|
+
tooLarge: (bytes, max) => `The buffers take ${bytes} bytes, over the scale limit of ${max} bytes`,
|
|
61
|
+
stats: (min, max, mean) => `min ${min}, max ${max}, mean ${mean}`,
|
|
62
|
+
time: (ms, samples, gpu, emulated) =>
|
|
63
|
+
`time: ${ms} ms per dispatch (${[samples > 1 ? `median of ${samples}` : '', gpu ? 'GPU timestamps' : 'wall clock', emulated ? 'CPU emulation' : ''].filter(Boolean).join('; ')})`,
|
|
64
|
+
adapter: (name, fallback) => `adapter: ${name}${fallback ? ' (fallback adapter)' : ''}`,
|
|
65
|
+
labels: { buffers: 'Buffer', dispatch: 'Workgroups', entry: 'Entry point', repeat: 'Repeat', features: 'Features' },
|
|
66
|
+
values: count => `${count} value${count === 1 ? '' : 's'}`,
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const MESSAGES: Record<Locale, WebgpuMessages> = { 'zh-CN': zhCN, en }
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { isRecord } from '@lincyaw/kernel'
|
|
2
|
+
import { parse } from 'yaml'
|
|
3
|
+
import type { ComputeBuffer, ComputeRequest, ScalarType } from './compute.ts'
|
|
4
|
+
import { LIMITS } from './config.ts'
|
|
5
|
+
import type { WebgpuMessages } from './messages.ts'
|
|
6
|
+
|
|
7
|
+
const KEYS = ['buffers', 'dispatch', 'entry', 'repeat', 'features']
|
|
8
|
+
const TYPES = ['f32', 'i32', 'u32']
|
|
9
|
+
const DECLARATION = /((?:@\s*\w+\s*(?:\([^)]*\))?\s*)+)var\s*<\s*(storage|uniform)\s*(?:,\s*(read_write|read))?\s*>\s*([A-Za-z_]\w*)\s*:\s*([^;]+);/g
|
|
10
|
+
const SHORTHAND: Record<string, string> = { f: 'f32', i: 'i32', u: 'u32', h: 'f16' }
|
|
11
|
+
|
|
12
|
+
interface Declaration {
|
|
13
|
+
name: string
|
|
14
|
+
group: number
|
|
15
|
+
binding: number
|
|
16
|
+
usage: 'storage' | 'uniform'
|
|
17
|
+
access: 'read' | 'read_write'
|
|
18
|
+
scalar?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The buffers the shader declares (`var<storage>`, `var<uniform>`), with their group, binding, access and scalar type. */
|
|
22
|
+
export function shaderBuffers(code: string): Declaration[] {
|
|
23
|
+
const source = code.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '')
|
|
24
|
+
return [...source.matchAll(DECLARATION)].map(([, attributes, space, access, name, type]) => {
|
|
25
|
+
const attribute = (key: string) => Number(new RegExp(`@\\s*${key}\\s*\\(\\s*(\\d+)\\s*\\)`).exec(attributes!)?.[1] ?? 0)
|
|
26
|
+
const scalars = new Set([
|
|
27
|
+
...[...type!.matchAll(/\b(f32|i32|u32|f16)\b/g)].map(m => m[1]!),
|
|
28
|
+
...[...type!.matchAll(/\b(?:vec[234]|mat[234]x[234])([fiuh])\b/g)].map(m => SHORTHAND[m[1]!]!),
|
|
29
|
+
])
|
|
30
|
+
return {
|
|
31
|
+
name: name!,
|
|
32
|
+
group: attribute('group'),
|
|
33
|
+
binding: attribute('binding'),
|
|
34
|
+
usage: space as 'storage' | 'uniform',
|
|
35
|
+
access: space === 'storage' && access === 'read_write' ? 'read_write' : 'read',
|
|
36
|
+
scalar: scalars.size === 1 ? [...scalars][0] : undefined,
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const isNumber = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value)
|
|
42
|
+
const positive = (value: unknown, max = Infinity): value is number => Number.isInteger(value) && (value as number) >= 1 && (value as number) <= max
|
|
43
|
+
|
|
44
|
+
// The shared gpu-compute.mjs computes the same sizes; it runs alone in the recording's page and cannot import this.
|
|
45
|
+
function bytes(buffer: Pick<ComputeBuffer, 'length' | 'usage'>): number {
|
|
46
|
+
const size = Math.max(4, buffer.length * 4)
|
|
47
|
+
return buffer.usage === 'uniform' ? Math.ceil(size / 16) * 16 : size
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function buffer(declaration: Declaration, spec: unknown, text: WebgpuMessages): ComputeBuffer | string {
|
|
51
|
+
const { name } = declaration
|
|
52
|
+
const fields = Array.isArray(spec) ? { data: spec } : spec
|
|
53
|
+
if (!isRecord(fields)) return text.bufferShape(name)
|
|
54
|
+
const { data, size, fill, type, read } = fields
|
|
55
|
+
if (data !== undefined && !(Array.isArray(data) && data.every(isNumber))) return text.bufferShape(name)
|
|
56
|
+
if (size !== undefined && !positive(size)) return text.bufferShape(name)
|
|
57
|
+
if ((fill !== undefined && !isNumber(fill)) || (read !== undefined && typeof read !== 'boolean')) return text.bufferShape(name)
|
|
58
|
+
const scalar = typeof type === 'string' ? type : declaration.scalar ?? 'f32'
|
|
59
|
+
if (!TYPES.includes(scalar)) return text.unsupportedType(name, scalar)
|
|
60
|
+
const values = data as number[] | undefined
|
|
61
|
+
const length = Math.max(size as number | undefined ?? 0, values?.length ?? 0)
|
|
62
|
+
if (length === 0) return text.bufferSize(name)
|
|
63
|
+
return {
|
|
64
|
+
name,
|
|
65
|
+
group: declaration.group,
|
|
66
|
+
binding: declaration.binding,
|
|
67
|
+
usage: declaration.usage,
|
|
68
|
+
access: declaration.access,
|
|
69
|
+
type: scalar as ScalarType,
|
|
70
|
+
length,
|
|
71
|
+
...values ? { data: values } : {},
|
|
72
|
+
...fill !== undefined ? { fill: fill as number } : {},
|
|
73
|
+
read: read ?? declaration.access === 'read_write',
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function valueCount(spec: unknown): number | undefined {
|
|
78
|
+
const fields = Array.isArray(spec) ? { data: spec } : spec
|
|
79
|
+
if (!isRecord(fields)) return undefined
|
|
80
|
+
const counted = [Array.isArray(fields.data) ? fields.data.length : 0, positive(fields.size) ? fields.size : 0]
|
|
81
|
+
return Math.max(...counted) || undefined
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function paramsSummary(args: Record<string, string>, text: WebgpuMessages): string {
|
|
85
|
+
const read = (raw: string): unknown => {
|
|
86
|
+
try {
|
|
87
|
+
return parse(raw)
|
|
88
|
+
} catch {
|
|
89
|
+
return raw
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return Object.entries(args).map(([name, raw]) => {
|
|
93
|
+
const label = text.labels[name] ?? name
|
|
94
|
+
const value = read(raw)
|
|
95
|
+
if (name === 'buffers' && isRecord(value)) {
|
|
96
|
+
return `${label} ${Object.entries(value).map(([buffer, spec]) => {
|
|
97
|
+
const count = valueCount(spec)
|
|
98
|
+
return count === undefined ? buffer : `${buffer}: ${text.values(count)}`
|
|
99
|
+
}).join(', ')}`
|
|
100
|
+
}
|
|
101
|
+
return `${label} ${typeof value === 'string' ? value : JSON.stringify(value)}`
|
|
102
|
+
}).join(' · ')
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function computeRequest(code: string, args: Record<string, string> | undefined, text: WebgpuMessages): { request: ComputeRequest } | { error: string } {
|
|
106
|
+
const params: Record<string, unknown> = {}
|
|
107
|
+
for (const [name, raw] of Object.entries(args ?? {})) {
|
|
108
|
+
if (!KEYS.includes(name)) return { error: text.unknownParam(name) }
|
|
109
|
+
try {
|
|
110
|
+
params[name] = parse(raw)
|
|
111
|
+
} catch (error) {
|
|
112
|
+
return { error: text.notYaml(name, error instanceof Error ? error.message.split('\n')[0]! : String(error)) }
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const specs = params.buffers
|
|
117
|
+
if (!isRecord(specs)) return { error: text.noBuffers }
|
|
118
|
+
const declared = shaderBuffers(code)
|
|
119
|
+
for (const name of Object.keys(specs)) {
|
|
120
|
+
if (!declared.some(d => d.name === name)) return { error: text.noDeclaration(name) }
|
|
121
|
+
}
|
|
122
|
+
const buffers: ComputeBuffer[] = []
|
|
123
|
+
for (const declaration of declared) {
|
|
124
|
+
if (!(declaration.name in specs)) return { error: text.noBuffer(declaration.name) }
|
|
125
|
+
const made = buffer(declaration, specs[declaration.name], text)
|
|
126
|
+
if (typeof made === 'string') return { error: made }
|
|
127
|
+
buffers.push(made)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const dispatch = params.dispatch === undefined ? [1] : Array.isArray(params.dispatch) ? params.dispatch : [params.dispatch]
|
|
131
|
+
if (dispatch.length < 1 || dispatch.length > 3 || !dispatch.every(n => positive(n))) return { error: text.badDispatch }
|
|
132
|
+
const repeat = params.repeat ?? 1
|
|
133
|
+
if (!positive(repeat, LIMITS.maxRepeat)) return { error: text.badRepeat(LIMITS.maxRepeat) }
|
|
134
|
+
const features = params.features ?? []
|
|
135
|
+
if (!Array.isArray(features) || !features.every(f => typeof f === 'string')) return { error: text.badFeatures }
|
|
136
|
+
if (params.entry !== undefined && typeof params.entry !== 'string') return { error: text.badEntry }
|
|
137
|
+
const total = buffers.reduce((sum, b) => sum + bytes(b), 0)
|
|
138
|
+
if (total > LIMITS.maxBytes) return { error: text.tooLarge(total, LIMITS.maxBytes) }
|
|
139
|
+
|
|
140
|
+
const [x, y = 1, z = 1] = dispatch as number[]
|
|
141
|
+
return {
|
|
142
|
+
request: {
|
|
143
|
+
code,
|
|
144
|
+
...params.entry ? { entry: params.entry as string } : {},
|
|
145
|
+
buffers,
|
|
146
|
+
dispatch: [x!, y, z],
|
|
147
|
+
repeat,
|
|
148
|
+
features: features as string[],
|
|
149
|
+
},
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { failedRun } from '@lincyaw/kernel'
|
|
2
|
+
import type { RunResult } from '@lincyaw/kernel'
|
|
3
|
+
import type { ComputeOutcome } from './compute.ts'
|
|
4
|
+
import { isUnavailable } from './compute.ts'
|
|
5
|
+
import type { WebgpuMessages } from './messages.ts'
|
|
6
|
+
import { failureText, summaryText } from './summary.ts'
|
|
7
|
+
|
|
8
|
+
export function programError(id: string, message: string, durationMs: number): RunResult {
|
|
9
|
+
const stderr = `${message}\n`
|
|
10
|
+
return { ok: false, stdout: '', stderr, exitCode: 1, durationMs, timedOut: false, source: 'live', executorId: id, outputs: [{ output_type: 'stream', name: 'stderr', text: stderr }] }
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** The run's result: a summary of the buffers, or why the GPU could not run the shader (a failed run) or the shader failed (a program error). */
|
|
14
|
+
export function runResultOf(id: string, outcome: ComputeOutcome, durationMs: number, text: WebgpuMessages): RunResult {
|
|
15
|
+
if (outcome.ok) {
|
|
16
|
+
const stdout = summaryText(outcome, text)
|
|
17
|
+
return { ok: true, stdout, stderr: '', exitCode: 0, durationMs, timedOut: false, source: 'live', executorId: id, outputs: [{ output_type: 'stream', name: 'stdout', text: stdout }] }
|
|
18
|
+
}
|
|
19
|
+
return isUnavailable(outcome.reason) ? failedRun(id, failureText(outcome, text), durationMs) : programError(id, failureText(outcome, text), durationMs)
|
|
20
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { AdapterDescription, BufferSummary, ComputeOutcome } from './compute.ts'
|
|
2
|
+
import type { WebgpuMessages } from './messages.ts'
|
|
3
|
+
|
|
4
|
+
export function formatNumber(value: number): string {
|
|
5
|
+
if (Number.isInteger(value) && Math.abs(value) < 1e15) return String(value)
|
|
6
|
+
return Number.isFinite(value) ? String(Number(value.toPrecision(6))) : String(value)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function bufferLine(buffer: BufferSummary, text: WebgpuMessages): string {
|
|
10
|
+
const values = buffer.tail.length
|
|
11
|
+
? [...buffer.head.map(formatNumber), '…', ...buffer.tail.map(formatNumber)]
|
|
12
|
+
: buffer.head.map(formatNumber)
|
|
13
|
+
const line = `${buffer.name}: ${buffer.type}[${buffer.length}] = [${values.join(', ')}]`
|
|
14
|
+
return buffer.tail.length ? `${line} ${text.stats(formatNumber(buffer.min), formatNumber(buffer.max), formatNumber(buffer.mean))}` : line
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function adapterName(adapter: AdapterDescription): string {
|
|
18
|
+
return [adapter.vendor, adapter.architecture].filter(Boolean).join(' ') || adapter.description || 'unknown'
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function summaryText(outcome: Extract<ComputeOutcome, { ok: true }>, text: WebgpuMessages): string {
|
|
22
|
+
const { timing, adapter } = outcome
|
|
23
|
+
const ms = formatNumber(Number(timing.ms.toPrecision(3)))
|
|
24
|
+
return [
|
|
25
|
+
...outcome.buffers.map(b => bufferLine(b, text)),
|
|
26
|
+
text.time(ms, timing.samples, timing.source === 'gpu', adapter.fallback),
|
|
27
|
+
text.adapter(adapterName(adapter), adapter.fallback),
|
|
28
|
+
].join('\n') + '\n'
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function failureText(outcome: Extract<ComputeOutcome, { ok: false }>, text: WebgpuMessages): string {
|
|
32
|
+
switch (outcome.reason) {
|
|
33
|
+
case 'no-webgpu': return text.noWebgpu
|
|
34
|
+
case 'no-adapter': return text.noAdapter
|
|
35
|
+
case 'feature': return text.missingFeature(outcome.detail)
|
|
36
|
+
case 'limit': return text.missingLimit(outcome.detail)
|
|
37
|
+
case 'device-lost': return text.deviceLost(outcome.detail)
|
|
38
|
+
case 'shader': return text.shader(outcome.detail)
|
|
39
|
+
case 'validation': return text.validation(outcome.detail)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The environment's fingerprint: the browser and the adapter, marked when it is a fallback adapter. */
|
|
44
|
+
export function adapterFingerprint(browser: string, adapter: AdapterDescription): string {
|
|
45
|
+
return `${browser}; ${adapterName(adapter)}${adapter.fallback ? ' (fallback)' : ''}`
|
|
46
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createRequire } from 'node:module'
|
|
2
|
+
import { environmentExecutor, errorMessage } from '@lincyaw/kernel'
|
|
3
|
+
import type { EnvironmentProvider, PresmiumNodePlugin } from '@lincyaw/kernel'
|
|
4
|
+
import { minimalRunEnv } from '@lincyaw/kernel/node'
|
|
5
|
+
import { adapterChoice, EXEC_WEBGPU_ID, WEBGPU_EXECUTOR_ID, WEBGPU_KIND } from '../domain/index.ts'
|
|
6
|
+
import { createChromiumExecutor, findChromiumAdapter } from './recorder.ts'
|
|
7
|
+
import { jaxAssets } from './vite.ts'
|
|
8
|
+
|
|
9
|
+
const { version } = createRequire(import.meta.url)('../../package.json') as { version: string }
|
|
10
|
+
|
|
11
|
+
const webgpu: EnvironmentProvider = {
|
|
12
|
+
create: (name, spec) => createChromiumExecutor({ id: environmentExecutor(name), adapter: adapterChoice(spec) }),
|
|
13
|
+
async probe(_name, spec) {
|
|
14
|
+
try {
|
|
15
|
+
await findChromiumAdapter(adapterChoice(spec), minimalRunEnv())
|
|
16
|
+
return undefined
|
|
17
|
+
} catch (error) {
|
|
18
|
+
return errorMessage(error)
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const plugin: PresmiumNodePlugin = {
|
|
24
|
+
id: EXEC_WEBGPU_ID,
|
|
25
|
+
version,
|
|
26
|
+
kernel: '^1.0.0',
|
|
27
|
+
executors: [createChromiumExecutor({ id: WEBGPU_EXECUTOR_ID, adapter: 'auto' })],
|
|
28
|
+
environments: { [WEBGPU_KIND]: webgpu },
|
|
29
|
+
assets: () => [jaxAssets()],
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default plugin
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
2
|
+
import { createRequire } from 'node:module'
|
|
3
|
+
import { deckLocale, failedRun } from '@lincyaw/kernel'
|
|
4
|
+
import type { Executor, RunContext, RunResult } from '@lincyaw/kernel'
|
|
5
|
+
import { minimalRunEnv } from '@lincyaw/kernel/node'
|
|
6
|
+
import type { Browser, BrowserType, Page } from 'playwright-chromium'
|
|
7
|
+
import { adapterFingerprint, adapterName, computeRequest, DEFAULT_TIMEOUT_MS, failureText, isUnavailable, isWgsl, MESSAGES, programError, runResultOf } from '../domain/index.ts'
|
|
8
|
+
import type { AdapterChoice, AdapterDescription, ComputeOutcome, ComputeRequest } from '../domain/index.ts'
|
|
9
|
+
|
|
10
|
+
const CHROMIUM_ARGS: Record<Exclude<AdapterChoice, 'auto'>, string[]> = {
|
|
11
|
+
gpu: ['--enable-unsafe-webgpu', '--enable-features=Vulkan', '--use-angle=vulkan'],
|
|
12
|
+
swiftshader: ['--enable-unsafe-webgpu', '--use-webgpu-adapter=swiftshader', '--enable-unsafe-swiftshader'],
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const ORIGIN = 'https://webgpu.presmium.invalid'
|
|
16
|
+
const PAGE = `<!doctype html><meta charset="utf-8"><title>presmium webgpu</title>
|
|
17
|
+
<script type="module">import * as gpu from '/gpu-compute.mjs'; window.presmiumGpu = gpu</script>`
|
|
18
|
+
|
|
19
|
+
const require = createRequire(import.meta.url)
|
|
20
|
+
let chromium: BrowserType | undefined
|
|
21
|
+
let core: string | undefined
|
|
22
|
+
|
|
23
|
+
function browserType(): BrowserType {
|
|
24
|
+
chromium ??= (require('playwright-chromium') as { chromium: BrowserType }).chromium
|
|
25
|
+
return chromium
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function installed(): boolean {
|
|
29
|
+
try {
|
|
30
|
+
return existsSync(browserType().executablePath())
|
|
31
|
+
} catch {
|
|
32
|
+
return false
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type GpuWindow = { presmiumGpu: { probeAdapter(gpu: unknown): Promise<AdapterDescription | undefined>, compute(gpu: unknown, request: ComputeRequest): Promise<ComputeOutcome> } }
|
|
37
|
+
|
|
38
|
+
async function withPage<T>(args: string[], env: Readonly<Record<string, string>>, use: (page: Page, browser: Browser) => Promise<T>): Promise<T> {
|
|
39
|
+
const browser = await browserType().launch({ args, env: { ...env } })
|
|
40
|
+
try {
|
|
41
|
+
const page = await browser.newPage()
|
|
42
|
+
core ??= readFileSync(new URL('../shared/gpu-compute.mjs', import.meta.url), 'utf8')
|
|
43
|
+
await page.route(`${ORIGIN}/**`, route => route.fulfill(new URL(route.request().url()).pathname === '/gpu-compute.mjs'
|
|
44
|
+
? { contentType: 'text/javascript', body: core }
|
|
45
|
+
: { contentType: 'text/html', body: PAGE }))
|
|
46
|
+
await page.goto(`${ORIGIN}/`)
|
|
47
|
+
await page.waitForFunction(() => 'presmiumGpu' in window)
|
|
48
|
+
return await use(page, browser)
|
|
49
|
+
} finally {
|
|
50
|
+
await browser.close()
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Chromium's arguments that give it a WebGPU adapter, its version and the adapter. */
|
|
55
|
+
export interface ChromiumAdapter {
|
|
56
|
+
args: string[]
|
|
57
|
+
version: string
|
|
58
|
+
adapter: AdapterDescription
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Launches Chromium to find its WebGPU adapter: the GPU, SwiftShader, or with `auto` the GPU and else SwiftShader. */
|
|
62
|
+
export async function findChromiumAdapter(choice: AdapterChoice, env: Readonly<Record<string, string>>): Promise<ChromiumAdapter> {
|
|
63
|
+
const tries = choice === 'auto' ? [CHROMIUM_ARGS.gpu, CHROMIUM_ARGS.swiftshader] : [CHROMIUM_ARGS[choice]]
|
|
64
|
+
for (const args of tries) {
|
|
65
|
+
const found = await withPage(args, env, async (page, browser) => ({
|
|
66
|
+
version: `chromium ${browser.version()}`,
|
|
67
|
+
adapter: await page.evaluate(() => (window as unknown as GpuWindow).presmiumGpu.probeAdapter((navigator as Navigator & { gpu?: unknown }).gpu)),
|
|
68
|
+
}))
|
|
69
|
+
if (found.adapter) return { args, version: found.version, adapter: found.adapter }
|
|
70
|
+
}
|
|
71
|
+
throw new Error(`Chromium has no WebGPU adapter (${choice === 'auto' ? 'neither the GPU nor SwiftShader' : choice})`)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** Records WGSL runs at compile time: each run in a new Chromium page, with the adapter found once. */
|
|
75
|
+
export function createChromiumExecutor(options: { id: string, adapter: AdapterChoice }): Executor {
|
|
76
|
+
const { id } = options
|
|
77
|
+
let found: Promise<ChromiumAdapter> | undefined
|
|
78
|
+
const find = (env: Readonly<Record<string, string>>) => {
|
|
79
|
+
found ??= findChromiumAdapter(options.adapter, env).catch((error: unknown) => {
|
|
80
|
+
found = undefined
|
|
81
|
+
throw error
|
|
82
|
+
})
|
|
83
|
+
return found
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
id,
|
|
87
|
+
supports: lang => isWgsl(lang) && installed(),
|
|
88
|
+
async fingerprint() {
|
|
89
|
+
const { version, adapter } = await find(minimalRunEnv())
|
|
90
|
+
return adapterFingerprint(version, adapter)
|
|
91
|
+
},
|
|
92
|
+
async run(request, signal, ctx?: RunContext): Promise<RunResult> {
|
|
93
|
+
const text = MESSAGES[ctx?.lang ? deckLocale(ctx.lang) : 'en']
|
|
94
|
+
const made = computeRequest(request.code, request.args, text)
|
|
95
|
+
if ('error' in made) return programError(id, made.error, 0)
|
|
96
|
+
const env = ctx?.env ?? minimalRunEnv()
|
|
97
|
+
const { args } = await find(env)
|
|
98
|
+
const timeoutMs = request.timeoutMs ?? DEFAULT_TIMEOUT_MS
|
|
99
|
+
const started = Date.now()
|
|
100
|
+
const outcome = await withPage(args, env, page => new Promise<ComputeOutcome | 'timeout' | 'cancelled'>((resolve, reject) => {
|
|
101
|
+
const timer = setTimeout(() => resolve('timeout'), timeoutMs)
|
|
102
|
+
const cancel = () => resolve('cancelled')
|
|
103
|
+
if (signal?.aborted) cancel()
|
|
104
|
+
signal?.addEventListener('abort', cancel, { once: true })
|
|
105
|
+
page.evaluate(req => (window as unknown as GpuWindow).presmiumGpu.compute((navigator as Navigator & { gpu?: unknown }).gpu, req), made.request)
|
|
106
|
+
.then(resolve, reject)
|
|
107
|
+
.finally(() => {
|
|
108
|
+
clearTimeout(timer)
|
|
109
|
+
signal?.removeEventListener('abort', cancel)
|
|
110
|
+
})
|
|
111
|
+
}))
|
|
112
|
+
const durationMs = Date.now() - started
|
|
113
|
+
if (outcome === 'timeout') return { ...failedRun(id, text.timedOut(timeoutMs), durationMs), timedOut: true }
|
|
114
|
+
if (outcome === 'cancelled') return failedRun(id, text.cancelled, durationMs)
|
|
115
|
+
if (!outcome.ok && isUnavailable(outcome.reason)) throw new Error(`${failureText(outcome, text)} (${outcome.adapter ? adapterName(outcome.adapter) : 'Chromium'})`)
|
|
116
|
+
return runResultOf(id, outcome, durationMs, text)
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
}
|
package/src/node/vite.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { readdirSync, readFileSync } from 'node:fs'
|
|
2
|
+
import { findPackageJSON } from 'node:module'
|
|
3
|
+
import { dirname, join, resolve } from 'node:path'
|
|
4
|
+
import type { RuntimeAssets } from '@lincyaw/kernel'
|
|
5
|
+
|
|
6
|
+
const JAX_DIR = 'presmium/jax-js/'
|
|
7
|
+
const LICENSE = 'LICENSE'
|
|
8
|
+
|
|
9
|
+
function jaxFiles(): { dist: string, license: string } {
|
|
10
|
+
const manifest = findPackageJSON('@jax-js/jax', import.meta.url)
|
|
11
|
+
if (!manifest) throw new Error('@jax-js/jax is not installed')
|
|
12
|
+
const { exports } = JSON.parse(readFileSync(manifest, 'utf8')) as { exports: { '.': { import: string } } }
|
|
13
|
+
return { dist: dirname(resolve(dirname(manifest), exports['.'].import)), license: join(dirname(manifest), LICENSE) }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The ES module build of jax-js, at `<base>presmium/jax-js/`. */
|
|
17
|
+
export function jaxAssets(): RuntimeAssets {
|
|
18
|
+
const { dist, license } = jaxFiles()
|
|
19
|
+
const types = Object.fromEntries([
|
|
20
|
+
...readdirSync(dist).filter(file => file.endsWith('.js')).map(file => [file, 'text/javascript']),
|
|
21
|
+
[LICENSE, 'text/plain; charset=utf-8'],
|
|
22
|
+
])
|
|
23
|
+
return { name: 'presmium:jax-js', prefix: JAX_DIR, types, source: file => (file === LICENSE ? license : join(dist, file)) }
|
|
24
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { AdapterDescription, ComputeOutcome, ComputeRequest } from '../domain/compute.ts'
|
|
2
|
+
|
|
3
|
+
export function probeAdapter(gpu: unknown): Promise<AdapterDescription | undefined>
|
|
4
|
+
export function compute(gpu: unknown, request: ComputeRequest, hooks?: { device?(device: { destroy(): void }): void }): Promise<ComputeOutcome>
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
const ARRAYS = { f32: Float32Array, i32: Int32Array, u32: Uint32Array }
|
|
2
|
+
const ENDS = 4
|
|
3
|
+
|
|
4
|
+
function describeAdapter(adapter) {
|
|
5
|
+
const info = adapter.info ?? {}
|
|
6
|
+
const fallback = Boolean(info.isFallbackAdapter ?? adapter.isFallbackAdapter) || info.architecture === 'swiftshader'
|
|
7
|
+
return { vendor: info.vendor ?? '', architecture: info.architecture ?? '', description: info.description ?? '', fallback }
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function probeAdapter(gpu) {
|
|
11
|
+
const adapter = gpu ? await gpu.requestAdapter().catch(() => null) : null
|
|
12
|
+
return adapter ? describeAdapter(adapter) : undefined
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// The same sizes as the domain's params.ts: this file runs alone in the recording's page, so it imports nothing.
|
|
16
|
+
function bufferBytes(buffer) {
|
|
17
|
+
const bytes = Math.max(4, buffer.length * 4)
|
|
18
|
+
return buffer.usage === 'uniform' ? Math.ceil(bytes / 16) * 16 : bytes
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function initial(buffer) {
|
|
22
|
+
const values = new ARRAYS[buffer.type](buffer.length)
|
|
23
|
+
if (buffer.data) values.set(buffer.data)
|
|
24
|
+
else if (buffer.fill) values.fill(buffer.fill)
|
|
25
|
+
return values
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function summarize(name, type, values) {
|
|
29
|
+
let min = Infinity
|
|
30
|
+
let max = -Infinity
|
|
31
|
+
let sum = 0
|
|
32
|
+
for (const value of values) {
|
|
33
|
+
if (value < min) min = value
|
|
34
|
+
if (value > max) max = value
|
|
35
|
+
sum += value
|
|
36
|
+
}
|
|
37
|
+
const all = values.length <= ENDS * 2
|
|
38
|
+
return {
|
|
39
|
+
name,
|
|
40
|
+
type,
|
|
41
|
+
length: values.length,
|
|
42
|
+
head: Array.from(all ? values : values.subarray(0, ENDS)),
|
|
43
|
+
tail: all ? [] : Array.from(values.subarray(values.length - ENDS)),
|
|
44
|
+
min: values.length ? min : 0,
|
|
45
|
+
max: values.length ? max : 0,
|
|
46
|
+
mean: values.length ? sum / values.length : 0,
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function median(samples) {
|
|
51
|
+
const sorted = [...samples].sort((a, b) => a - b)
|
|
52
|
+
const middle = sorted.length >> 1
|
|
53
|
+
return sorted.length % 2 ? sorted[middle] : (sorted[middle - 1] + sorted[middle]) / 2
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function compute(gpu, request, hooks = {}) {
|
|
57
|
+
if (!gpu) return { ok: false, reason: 'no-webgpu', detail: '' }
|
|
58
|
+
const adapter = await gpu.requestAdapter().catch(() => null)
|
|
59
|
+
if (!adapter) return { ok: false, reason: 'no-adapter', detail: '' }
|
|
60
|
+
const described = describeAdapter(adapter)
|
|
61
|
+
const missing = request.features.filter(feature => !adapter.features.has(feature))
|
|
62
|
+
if (missing.length) return { ok: false, reason: 'feature', detail: missing.join(', '), adapter: described }
|
|
63
|
+
|
|
64
|
+
const sizes = usage => request.buffers.filter(b => b.usage === usage).map(bufferBytes)
|
|
65
|
+
const needs = {
|
|
66
|
+
maxBufferSize: Math.max(4, ...request.buffers.map(bufferBytes)),
|
|
67
|
+
maxStorageBufferBindingSize: Math.max(4, ...sizes('storage')),
|
|
68
|
+
maxUniformBufferBindingSize: Math.max(4, ...sizes('uniform')),
|
|
69
|
+
maxStorageBuffersPerShaderStage: sizes('storage').length,
|
|
70
|
+
maxUniformBuffersPerShaderStage: sizes('uniform').length,
|
|
71
|
+
}
|
|
72
|
+
for (const [limit, needed] of Object.entries(needs)) {
|
|
73
|
+
if (needed > adapter.limits[limit]) return { ok: false, reason: 'limit', detail: `${limit} ${adapter.limits[limit]} < ${needed}`, adapter: described }
|
|
74
|
+
}
|
|
75
|
+
const timestamps = adapter.features.has('timestamp-query')
|
|
76
|
+
let device
|
|
77
|
+
try {
|
|
78
|
+
device = await adapter.requestDevice({ requiredFeatures: [...request.features, ...timestamps ? ['timestamp-query'] : []], requiredLimits: needs })
|
|
79
|
+
} catch (error) {
|
|
80
|
+
return { ok: false, reason: 'limit', detail: String(error?.message ?? error), adapter: described }
|
|
81
|
+
}
|
|
82
|
+
hooks.device?.(device)
|
|
83
|
+
const lost = device.lost.then(info => info.reason === 'destroyed' ? new Promise(() => {}) : { ok: false, reason: 'device-lost', detail: info.message ?? '', adapter: described })
|
|
84
|
+
try {
|
|
85
|
+
return await Promise.race([lost, dispatch(device, request, described, timestamps)])
|
|
86
|
+
} catch (error) {
|
|
87
|
+
return { ok: false, reason: 'validation', detail: String(error?.message ?? error), adapter: described }
|
|
88
|
+
} finally {
|
|
89
|
+
device.destroy()
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function dispatch(device, request, adapter, timestamps) {
|
|
94
|
+
const usage = GPUBufferUsage
|
|
95
|
+
device.pushErrorScope('validation')
|
|
96
|
+
const module = device.createShaderModule({ code: request.code })
|
|
97
|
+
const errors = (await module.getCompilationInfo()).messages.filter(message => message.type === 'error')
|
|
98
|
+
if (errors.length) {
|
|
99
|
+
await device.popErrorScope()
|
|
100
|
+
return { ok: false, reason: 'shader', detail: errors.map(m => `${m.lineNum}:${m.linePos} ${m.message}`).join('\n'), adapter }
|
|
101
|
+
}
|
|
102
|
+
const groups = Math.max(0, ...request.buffers.map(b => b.group + 1))
|
|
103
|
+
const layouts = Array.from({ length: groups }, (_, group) => device.createBindGroupLayout({
|
|
104
|
+
entries: request.buffers.filter(b => b.group === group).map(b => ({
|
|
105
|
+
binding: b.binding,
|
|
106
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
107
|
+
buffer: { type: b.usage === 'uniform' ? 'uniform' : b.access === 'read' ? 'read-only-storage' : 'storage' },
|
|
108
|
+
})),
|
|
109
|
+
}))
|
|
110
|
+
const pipeline = device.createComputePipeline({
|
|
111
|
+
layout: device.createPipelineLayout({ bindGroupLayouts: layouts }),
|
|
112
|
+
compute: request.entry ? { module, entryPoint: request.entry } : { module },
|
|
113
|
+
})
|
|
114
|
+
const buffers = request.buffers.map((b) => {
|
|
115
|
+
const buffer = device.createBuffer({ size: bufferBytes(b), usage: (b.usage === 'uniform' ? usage.UNIFORM : usage.STORAGE) | usage.COPY_SRC | usage.COPY_DST, mappedAtCreation: true })
|
|
116
|
+
new ARRAYS[b.type](buffer.getMappedRange()).set(initial(b))
|
|
117
|
+
buffer.unmap()
|
|
118
|
+
return buffer
|
|
119
|
+
})
|
|
120
|
+
const bindGroups = layouts.map((layout, group) => device.createBindGroup({
|
|
121
|
+
layout,
|
|
122
|
+
entries: request.buffers.flatMap((b, i) => b.group === group ? [{ binding: b.binding, resource: { buffer: buffers[i] } }] : []),
|
|
123
|
+
}))
|
|
124
|
+
const encode = (encoder, timestampWrites) => {
|
|
125
|
+
const pass = encoder.beginComputePass(timestampWrites ? { timestampWrites } : {})
|
|
126
|
+
pass.setPipeline(pipeline)
|
|
127
|
+
bindGroups.forEach((group, i) => pass.setBindGroup(i, group))
|
|
128
|
+
pass.dispatchWorkgroups(...request.dispatch)
|
|
129
|
+
pass.end()
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const samples = []
|
|
133
|
+
let times
|
|
134
|
+
if (timestamps) {
|
|
135
|
+
const count = request.repeat * 2
|
|
136
|
+
const querySet = device.createQuerySet({ type: 'timestamp', count })
|
|
137
|
+
const resolved = device.createBuffer({ size: count * 8, usage: usage.QUERY_RESOLVE | usage.COPY_SRC })
|
|
138
|
+
times = device.createBuffer({ size: count * 8, usage: usage.MAP_READ | usage.COPY_DST })
|
|
139
|
+
const encoder = device.createCommandEncoder()
|
|
140
|
+
for (let i = 0; i < request.repeat; i++) encode(encoder, { querySet, beginningOfPassWriteIndex: i * 2, endOfPassWriteIndex: i * 2 + 1 })
|
|
141
|
+
encoder.resolveQuerySet(querySet, 0, count, resolved, 0)
|
|
142
|
+
encoder.copyBufferToBuffer(resolved, 0, times, 0, count * 8)
|
|
143
|
+
device.queue.submit([encoder.finish()])
|
|
144
|
+
} else {
|
|
145
|
+
for (let i = 0; i < request.repeat; i++) {
|
|
146
|
+
const encoder = device.createCommandEncoder()
|
|
147
|
+
encode(encoder)
|
|
148
|
+
const started = performance.now()
|
|
149
|
+
device.queue.submit([encoder.finish()])
|
|
150
|
+
await device.queue.onSubmittedWorkDone()
|
|
151
|
+
samples.push(performance.now() - started)
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const shown = request.buffers.flatMap((b, i) => b.read ? [{ b, copy: device.createBuffer({ size: bufferBytes(b), usage: usage.MAP_READ | usage.COPY_DST }), buffer: buffers[i] }] : [])
|
|
155
|
+
const encoder = device.createCommandEncoder()
|
|
156
|
+
for (const { b, copy, buffer } of shown) encoder.copyBufferToBuffer(buffer, 0, copy, 0, bufferBytes(b))
|
|
157
|
+
device.queue.submit([encoder.finish()])
|
|
158
|
+
const error = await device.popErrorScope()
|
|
159
|
+
if (error) return { ok: false, reason: 'validation', detail: error.message, adapter }
|
|
160
|
+
|
|
161
|
+
if (times) {
|
|
162
|
+
await times.mapAsync(GPUMapMode.READ)
|
|
163
|
+
const ns = new BigUint64Array(times.getMappedRange())
|
|
164
|
+
for (let i = 0; i < request.repeat; i++) samples.push(Number(ns[i * 2 + 1] - ns[i * 2]) / 1e6)
|
|
165
|
+
}
|
|
166
|
+
const summaries = []
|
|
167
|
+
for (const { b, copy } of shown) {
|
|
168
|
+
await copy.mapAsync(GPUMapMode.READ)
|
|
169
|
+
summaries.push(summarize(b.name, b.type, new ARRAYS[b.type](copy.getMappedRange(), 0, b.length)))
|
|
170
|
+
}
|
|
171
|
+
return { ok: true, buffers: summaries, timing: { ms: median(samples), samples: samples.length, source: timestamps ? 'gpu' : 'wall' }, adapter }
|
|
172
|
+
}
|