@null3d/vite-plugin 0.1.1 → 0.1.3
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/README.md +6 -0
- package/dist/shader-compiler.stamp.json +1 -1
- package/dist/shader-compiler.wasm +0 -0
- package/lib/hot.d.ts +13 -2
- package/lib/hot.js +28 -3
- package/lib/index.d.ts +1 -1
- package/lib/index.js +263 -74
- package/lib/material-builds.d.ts +19 -2
- package/lib/material-builds.js +61 -8
- package/lib/pack.d.ts +11 -1
- package/lib/pack.js +70 -1
- package/lib/paragraphs.d.ts +81 -10
- package/lib/paragraphs.js +289 -46
- package/lib/shader-list.d.ts +41 -2
- package/lib/shader-list.js +56 -10
- package/lib/wgsl.d.ts +10 -9
- package/lib/wgsl.js +31 -28
- package/package.json +3 -3
package/lib/paragraphs.js
CHANGED
|
@@ -8,13 +8,28 @@
|
|
|
8
8
|
// device module's imports and emits the tables they name, in the folder the bundler put the module
|
|
9
9
|
// in, at the names the imports use. The tables then sit beside every module that imports them,
|
|
10
10
|
// whatever the project's assets folder and asset naming pattern are.
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
//
|
|
12
|
+
// Each table, and each module that imports one, takes a name that carries the hash of its final
|
|
13
|
+
// bytes. A host may then keep every file under a given name for a year, and a changed table or a
|
|
14
|
+
// changed module always has a new address. The table's content depends on the union of the names
|
|
15
|
+
// that its importers take, so the build works out every table before it names the modules.
|
|
16
|
+
import { createHash } from 'node:crypto';
|
|
17
|
+
import { closeSync, existsSync, openSync, readFileSync, readSync, statSync, writeSync, } from 'node:fs';
|
|
18
|
+
import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep } from 'node:path';
|
|
19
|
+
import { narrowParagraphTable } from "./shader-list.js";
|
|
20
|
+
/** The prefix of a table's file name, before its kind. */
|
|
21
|
+
const TABLE_PREFIX = 'shader-paragraphs-';
|
|
13
22
|
/**
|
|
14
23
|
* An import of a table of shared paragraphs, at the top of a device module of its kind, with the
|
|
15
|
-
* names it takes from the table.
|
|
24
|
+
* names it takes from the table. A table that the build already named by its hash reads the same,
|
|
25
|
+
* so a module that the build reads again keeps its imports. The dot before the hash reads too, so
|
|
26
|
+
* that a file the build wrote before the dash form keeps its imports.
|
|
16
27
|
*/
|
|
17
|
-
const PARAGRAPH_IMPORT = /import\s*\{([^}]*)\}\s*from '\.\/(shader-paragraphs-[a-z0-9-]
|
|
28
|
+
const PARAGRAPH_IMPORT = /import\s*\{([^}]*)\}\s*from '\.\/(shader-paragraphs-[a-z0-9-]+?)(?:[.-][0-9a-f]{8})?\.js';/g;
|
|
29
|
+
/** The source of an import statement's relative address, for the rewrite below. */
|
|
30
|
+
const TABLE_SPECIFIER = /(from\s*'\.\/)(shader-paragraphs-[a-z0-9-]+?)(?:[.-][0-9a-f]{8})?\.js'/g;
|
|
31
|
+
/** A name that a table of shared paragraphs exports. */
|
|
32
|
+
const TABLE_EXPORT = /^export\s+(?:const|let|var|function|class)\s+([A-Za-z_$][\w$]*)/gm;
|
|
18
33
|
/**
|
|
19
34
|
* How much of a copied file this reads at first. The shader build writes the imports among the
|
|
20
35
|
* first lines, and a device module is hundreds of kilobytes, so the rest is never read. A device
|
|
@@ -54,10 +69,6 @@ function tableImports(read, length) {
|
|
|
54
69
|
window = Math.min(window * 2, length);
|
|
55
70
|
}
|
|
56
71
|
}
|
|
57
|
-
/** The names of the paragraph tables that a device module imports. */
|
|
58
|
-
function tableStems(read, length) {
|
|
59
|
-
return tableImports(read, length).map((entry) => entry.stem);
|
|
60
|
-
}
|
|
61
72
|
/**
|
|
62
73
|
* The tables that a device module on disk imports, with the names it takes from each. The file is
|
|
63
74
|
* read from its start only, so a build covers the whole generated set without holding any of it
|
|
@@ -78,13 +89,129 @@ export function tableImportsOfFile(file) {
|
|
|
78
89
|
closeSync(fd);
|
|
79
90
|
}
|
|
80
91
|
}
|
|
92
|
+
/** The hash that a file's final bytes give it: the first eight hexadecimal digits of SHA-256. */
|
|
93
|
+
export function contentHash(source) {
|
|
94
|
+
return createHash('sha256').update(source).digest('hex').slice(0, 8);
|
|
95
|
+
}
|
|
96
|
+
/** The kind of a table: its file name without the shared prefix and `.js`, such as `wgsl-morph`. */
|
|
97
|
+
function kindOf(path) {
|
|
98
|
+
return basename(path, '.js').slice(TABLE_PREFIX.length);
|
|
99
|
+
}
|
|
100
|
+
/** A table that lacks a name that its importers take. The caller names the modules. */
|
|
101
|
+
export class TableExportError extends Error {
|
|
102
|
+
path;
|
|
103
|
+
missing;
|
|
104
|
+
constructor(path, missing) {
|
|
105
|
+
super(`${basename(path)} does not export ${missing.join(', ')}`);
|
|
106
|
+
this.path = path;
|
|
107
|
+
this.missing = missing;
|
|
108
|
+
this.name = 'TableExportError';
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Works out each table's final content and name before any module that imports it takes a name. A
|
|
113
|
+
* build with a list of permutation words keeps only the paragraphs that the importing modules
|
|
114
|
+
* name, so a table's content follows the union of those names. A build without a list keeps the
|
|
115
|
+
* table whole. Throws a `TableExportError` when a table lacks a name that its importers take.
|
|
116
|
+
*/
|
|
117
|
+
export function planParagraphTables(tables, narrow) {
|
|
118
|
+
const plan = new Map();
|
|
119
|
+
for (const [path, names] of tables) {
|
|
120
|
+
const whole = readFileSync(path);
|
|
121
|
+
const text = narrow
|
|
122
|
+
? Buffer.from(narrowParagraphTable(whole.toString('utf8'), names, path))
|
|
123
|
+
: whole;
|
|
124
|
+
const exported = new Set([...text.toString('utf8').matchAll(TABLE_EXPORT)].map((match) => match[1]));
|
|
125
|
+
const missing = [...names].filter((name) => !exported.has(name)).sort();
|
|
126
|
+
if (missing.length > 0)
|
|
127
|
+
throw new TableExportError(path, missing);
|
|
128
|
+
plan.set(path, {
|
|
129
|
+
name: `${TABLE_PREFIX}${kindOf(path)}-${contentHash(text)}.js`,
|
|
130
|
+
source: Buffer.from(text),
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return plan;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The text of a device module with each table import pointing at the table's content-addressed
|
|
137
|
+
* file. `names` maps each table's stem, such as `shader-paragraphs-wgsl`, to its final file name.
|
|
138
|
+
*/
|
|
139
|
+
export function rewriteTableImports(text, names) {
|
|
140
|
+
return text.replace(TABLE_SPECIFIER, (match, before, stem) => {
|
|
141
|
+
const name = names.get(stem);
|
|
142
|
+
return name === undefined ? match : `${before}${name}'`;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/** How much of a module this copies at a time after its import head. */
|
|
146
|
+
const COPY_CHUNK = 1024 * 1024;
|
|
147
|
+
/**
|
|
148
|
+
* Reads a module's import head from its start, growing the window to the first blank line after
|
|
149
|
+
* the imports. Undefined for a file that names no import, which is no device module. The byte
|
|
150
|
+
* offset lets the caller copy the rest of the source without reading it into memory.
|
|
151
|
+
*/
|
|
152
|
+
function readHead(fd, length) {
|
|
153
|
+
let window = Math.min(IMPORT_WINDOW, length);
|
|
154
|
+
for (;;) {
|
|
155
|
+
const buffer = Buffer.alloc(window);
|
|
156
|
+
const count = readSync(fd, buffer, 0, window, 0);
|
|
157
|
+
const first = buffer.indexOf('import ', 0);
|
|
158
|
+
if (first < 0)
|
|
159
|
+
return undefined;
|
|
160
|
+
const end = buffer.indexOf('\n\n', first);
|
|
161
|
+
if (end >= 0) {
|
|
162
|
+
const stop = end + 2;
|
|
163
|
+
return { text: buffer.subarray(0, stop).toString('utf8'), end: stop };
|
|
164
|
+
}
|
|
165
|
+
if (window >= length)
|
|
166
|
+
return { text: buffer.subarray(0, count).toString('utf8'), end: count };
|
|
167
|
+
window = Math.min(window * 2, length);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Writes the module at `source` to `target` with its table imports pointing at their hashed files,
|
|
172
|
+
* and gives the hash of the bytes written. It reads only the import head, writes that rewritten,
|
|
173
|
+
* and copies the rest of the source in chunks, so a large module never sits in memory whole.
|
|
174
|
+
* Undefined for a file that names no import, which the caller copies as it is.
|
|
175
|
+
*/
|
|
176
|
+
export function rewriteCopyFile(source, target, names) {
|
|
177
|
+
const input = openSync(source, 'r');
|
|
178
|
+
let output;
|
|
179
|
+
try {
|
|
180
|
+
const length = statSync(source).size;
|
|
181
|
+
const head = readHead(input, length);
|
|
182
|
+
if (head === undefined)
|
|
183
|
+
return undefined;
|
|
184
|
+
const rewritten = Buffer.from(rewriteTableImports(head.text, names), 'utf8');
|
|
185
|
+
const hash = createHash('sha256').update(rewritten);
|
|
186
|
+
output = openSync(target, 'w');
|
|
187
|
+
writeSync(output, rewritten);
|
|
188
|
+
const chunk = Buffer.allocUnsafe(Math.min(COPY_CHUNK, Math.max(1, length - head.end)));
|
|
189
|
+
for (let position = head.end; position < length;) {
|
|
190
|
+
const count = readSync(input, chunk, 0, Math.min(chunk.length, length - position), position);
|
|
191
|
+
if (count <= 0)
|
|
192
|
+
break;
|
|
193
|
+
hash.update(chunk.subarray(0, count));
|
|
194
|
+
writeSync(output, chunk, 0, count);
|
|
195
|
+
position += count;
|
|
196
|
+
}
|
|
197
|
+
return hash.digest('hex').slice(0, 8);
|
|
198
|
+
}
|
|
199
|
+
finally {
|
|
200
|
+
if (output !== undefined)
|
|
201
|
+
closeSync(output);
|
|
202
|
+
closeSync(input);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
81
205
|
/**
|
|
82
206
|
* The file of the table that `file` imports, or undefined when the engine's package does not hold
|
|
83
207
|
* it: a table the build did not write, which would otherwise fail as a load in a browser and never
|
|
84
208
|
* in the build.
|
|
85
209
|
*/
|
|
86
|
-
function tablePath(file, stem, root) {
|
|
87
|
-
|
|
210
|
+
function tablePath(file, stem, root, workerFiles) {
|
|
211
|
+
const names = file.originalFileNames.length > 0
|
|
212
|
+
? file.originalFileNames
|
|
213
|
+
: (workerFiles?.get(file.fileName)?.moduleIds ?? []);
|
|
214
|
+
for (const name of names) {
|
|
88
215
|
// A build names a copied file as the page's root sees it, relative or absolute; the table
|
|
89
216
|
// sits beside the file it came from.
|
|
90
217
|
const folder = isAbsolute(name) ? dirname(name) : resolve(root, dirname(name));
|
|
@@ -94,52 +221,168 @@ function tablePath(file, stem, root) {
|
|
|
94
221
|
}
|
|
95
222
|
return undefined;
|
|
96
223
|
}
|
|
224
|
+
/** A source path as a message names it: from the project's root when it is inside it. */
|
|
225
|
+
function displayPath(root, name) {
|
|
226
|
+
const path = isAbsolute(name) ? relative(root, name) : name;
|
|
227
|
+
return path.split(sep).join('/');
|
|
228
|
+
}
|
|
229
|
+
/** A bundle asset's original name without its extension, or its file name when it has none. */
|
|
230
|
+
function assetStem(file) {
|
|
231
|
+
const name = file.name?.length ? file.name : file.fileName;
|
|
232
|
+
return basename(name).replace(/\.[cm]?js$/, '');
|
|
233
|
+
}
|
|
234
|
+
/** Replaces every mention of a renamed file in the bundle's scripts and text assets. */
|
|
235
|
+
function renameReferences(bundle, from, to) {
|
|
236
|
+
for (const file of Object.values(bundle)) {
|
|
237
|
+
if (file.fileName === to)
|
|
238
|
+
continue;
|
|
239
|
+
if (file.type === 'chunk') {
|
|
240
|
+
if (file.code.includes(from))
|
|
241
|
+
file.code = file.code.replaceAll(from, to);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (!/\.(?:[cm]?js|html|css|json)$/.test(file.fileName))
|
|
245
|
+
continue;
|
|
246
|
+
const text = typeof file.source === 'string' ? file.source : new TextDecoder().decode(file.source);
|
|
247
|
+
if (!text.includes(from))
|
|
248
|
+
continue;
|
|
249
|
+
file.source = text.replaceAll(from, to);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
97
252
|
/**
|
|
98
|
-
* Emits each shared table of shader paragraphs beside the
|
|
99
|
-
*
|
|
100
|
-
*
|
|
253
|
+
* Emits each shared table of shader paragraphs beside the modules that import it, gives the files
|
|
254
|
+
* of the tables that each bundle file names, and names each bundle asset that imports one by the
|
|
255
|
+
* hash of its final bytes. A build's bundle is the same after a second call, because a table is
|
|
256
|
+
* emitted once for each folder it belongs in.
|
|
101
257
|
*/
|
|
102
|
-
export function emitParagraphTables(bundle,
|
|
103
|
-
const
|
|
104
|
-
|
|
258
|
+
export function emitParagraphTables(bundle, options = {}) {
|
|
259
|
+
const { root = process.cwd(), narrow = false, workerFiles, copies = [] } = options;
|
|
260
|
+
/** The names that the importers take from each table, by the table's source file. */
|
|
261
|
+
const names = new Map();
|
|
262
|
+
/** The output folders that hold a module that names each table. */
|
|
263
|
+
const folders = new Map();
|
|
264
|
+
const add = (path, imported, folder) => {
|
|
265
|
+
let table = names.get(path);
|
|
266
|
+
if (table === undefined) {
|
|
267
|
+
table = new Set();
|
|
268
|
+
names.set(path, table);
|
|
269
|
+
}
|
|
270
|
+
for (const name of imported)
|
|
271
|
+
table.add(name);
|
|
272
|
+
let where = folders.get(path);
|
|
273
|
+
if (where === undefined) {
|
|
274
|
+
where = new Set();
|
|
275
|
+
folders.set(path, where);
|
|
276
|
+
}
|
|
277
|
+
where.add(folder);
|
|
278
|
+
};
|
|
279
|
+
/** The tables that each bundle asset names, with its source name and the table files. */
|
|
280
|
+
const assets = new Map();
|
|
105
281
|
for (const file of Object.values(bundle)) {
|
|
106
282
|
if (file.type !== 'asset')
|
|
107
283
|
continue;
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
];
|
|
111
|
-
if (stems.length === 0)
|
|
284
|
+
const imports = tableImports((bytes) => headOf(file.source, bytes), file.source.length);
|
|
285
|
+
if (imports.length === 0)
|
|
112
286
|
continue;
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const stem = posix.basename(table, '.js');
|
|
119
|
-
const path = tablePath(file, stem, root);
|
|
287
|
+
const source = file.originalFileNames[0] ?? workerFiles?.get(file.fileName)?.moduleIds[0] ?? file.fileName;
|
|
288
|
+
const module = displayPath(root, source);
|
|
289
|
+
const paths = [];
|
|
290
|
+
for (const { stem, names: imported } of imports) {
|
|
291
|
+
const path = tablePath(file, stem, root, workerFiles);
|
|
120
292
|
if (!path)
|
|
121
|
-
|
|
122
|
-
|
|
293
|
+
this.error(`null3D: ${module} imports ${stem}.js, which the engine's shader modules do not hold. Run \`bun run shaders\` again, and reinstall @null3d/engine if the build still fails.`);
|
|
294
|
+
paths.push(path);
|
|
295
|
+
add(path, imported, posix.dirname(file.fileName));
|
|
296
|
+
}
|
|
297
|
+
assets.set(file, { module, paths });
|
|
298
|
+
}
|
|
299
|
+
// Each copy's head is read once and its names join the union, which holds each name once.
|
|
300
|
+
for (const copy of copies) {
|
|
301
|
+
for (const { stem, names: imported } of tableImportsOfFile(copy.file)) {
|
|
302
|
+
const path = join(dirname(copy.source), `${stem}.js`);
|
|
303
|
+
if (!existsSync(path))
|
|
304
|
+
this.error(`null3D: ${copy.module} imports ${stem}.js, which the engine's shader modules do not hold. Run \`bun run shaders\` again, and reinstall @null3d/engine if the build still fails.`);
|
|
305
|
+
add(path, imported, copy.folder);
|
|
123
306
|
}
|
|
124
|
-
tables.set(file.fileName, own);
|
|
125
307
|
}
|
|
126
|
-
|
|
308
|
+
let plan;
|
|
309
|
+
try {
|
|
310
|
+
plan = planParagraphTables(names, narrow);
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
if (!(error instanceof TableExportError))
|
|
314
|
+
throw error;
|
|
315
|
+
this.error(missingExportMessage(error, copies, assets));
|
|
316
|
+
}
|
|
317
|
+
/** The final name of the table that a module's source file names. */
|
|
318
|
+
const nameOf = (path) => {
|
|
319
|
+
const table = plan.get(path);
|
|
320
|
+
if (table === undefined)
|
|
321
|
+
throw new Error(`null3D: the build planned no table for ${basename(path)}.`);
|
|
322
|
+
return table.name;
|
|
323
|
+
};
|
|
324
|
+
// Each table keeps the folder of every module that names it, so a relative import resolves.
|
|
325
|
+
const tables = new Map();
|
|
326
|
+
for (const [path, where] of folders) {
|
|
327
|
+
const table = plan.get(path);
|
|
328
|
+
if (table === undefined)
|
|
329
|
+
continue;
|
|
330
|
+
for (const folder of where) {
|
|
331
|
+
const output = posix.join(folder, table.name);
|
|
332
|
+
if (tables.has(output))
|
|
333
|
+
continue;
|
|
334
|
+
tables.set(output, table);
|
|
335
|
+
this.emitFile({ type: 'asset', fileName: output, source: table.source });
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const files = new Map();
|
|
339
|
+
const renamed = new Map();
|
|
340
|
+
const renames = [];
|
|
341
|
+
for (const [file, { paths }] of assets) {
|
|
342
|
+
const names = new Map(paths.map((path) => [basename(path, '.js'), nameOf(path)]));
|
|
343
|
+
const text = typeof file.source === 'string' ? file.source : new TextDecoder().decode(file.source);
|
|
344
|
+
const rewritten = rewriteTableImports(text, names);
|
|
345
|
+
const folder = posix.dirname(file.fileName);
|
|
346
|
+
const loads = paths.map((path) => posix.join(folder, nameOf(path)));
|
|
347
|
+
const source = Buffer.isBuffer(file.source) ? file.source : Buffer.from(file.source);
|
|
348
|
+
const after = Buffer.from(rewritten);
|
|
349
|
+
if (after.equals(source)) {
|
|
350
|
+
files.set(file.fileName, loads);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
const to = posix.join(folder, `${assetStem(file)}-${contentHash(after)}.js`);
|
|
354
|
+
files.set(to, loads);
|
|
355
|
+
if (bundle[to] !== undefined)
|
|
356
|
+
this.error(`null3D: two shader files would share the name ${to}. Run \`bun run shaders\` again, and reinstall @null3d/engine if the build still fails.`);
|
|
357
|
+
renames.push({ from: file.fileName, to, file: { ...file, fileName: to, source: after } });
|
|
358
|
+
}
|
|
359
|
+
for (const { from, to, file } of renames) {
|
|
360
|
+
delete bundle[from];
|
|
361
|
+
bundle[to] = file;
|
|
362
|
+
renamed.set(from, to);
|
|
363
|
+
renameReferences(bundle, from, to);
|
|
364
|
+
}
|
|
365
|
+
return { files, plan, tables, renamed };
|
|
127
366
|
}
|
|
128
367
|
/**
|
|
129
|
-
*
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* through `emitParagraphTables`, and no build emits a table twice.
|
|
368
|
+
* The build error for a table that lacks a name. It reads the modules again, because only a failed
|
|
369
|
+
* build pays for naming them, and a build that keeps a module name for every imported name holds
|
|
370
|
+
* too much memory.
|
|
133
371
|
*/
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
372
|
+
function missingExportMessage(error, copies, assets) {
|
|
373
|
+
const missing = new Set(error.missing);
|
|
374
|
+
const modules = new Set();
|
|
375
|
+
for (const copy of copies) {
|
|
376
|
+
const named = tableImportsOfFile(copy.file).some((entry) => join(dirname(copy.source), `${entry.stem}.js`) === error.path &&
|
|
377
|
+
entry.names.some((name) => missing.has(name)));
|
|
378
|
+
if (named)
|
|
379
|
+
modules.add(copy.module);
|
|
380
|
+
}
|
|
381
|
+
for (const [file, { module, paths }] of assets) {
|
|
382
|
+
const imports = tableImports((bytes) => headOf(file.source, bytes), file.source.length);
|
|
383
|
+
const named = imports.some((entry, index) => paths[index] === error.path && entry.names.some((name) => missing.has(name)));
|
|
384
|
+
if (named)
|
|
385
|
+
modules.add(module);
|
|
386
|
+
}
|
|
387
|
+
return `null3D: ${[...modules].join(', ') || 'a shader module'} imports ${error.missing.join(', ')} from ${basename(error.path)}, and that table does not export ${error.missing.length === 1 ? 'it' : 'them'}. Run \`bun run shaders\` again, and reinstall @null3d/engine if the build still fails.`;
|
|
145
388
|
}
|
package/lib/shader-list.d.ts
CHANGED
|
@@ -3,6 +3,33 @@
|
|
|
3
3
|
* list serves every device module, and a build matches a word with those bits cleared.
|
|
4
4
|
*/
|
|
5
5
|
export declare const DEVICE_BITS: number;
|
|
6
|
+
/**
|
|
7
|
+
* A GPU path that a build can ship: WGSL builds for WebGPU, or GLSL ES 3.00 builds for WebGL2.
|
|
8
|
+
* WebGPU's compatibility mode draws with the WebGPU builds, so it counts as `webgpu`.
|
|
9
|
+
*/
|
|
10
|
+
export type GpuPath = 'webgpu' | 'webgl2';
|
|
11
|
+
/** The shader language of a GPU path: WGSL for WebGPU, GLSL for WebGL2. */
|
|
12
|
+
export type ShaderTarget = 'wgsl' | 'glsl';
|
|
13
|
+
/** The shader target of a GPU path. */
|
|
14
|
+
export declare function targetOfPath(path: GpuPath): ShaderTarget;
|
|
15
|
+
/**
|
|
16
|
+
* The shader target of a generated shader module, from its name, or null for the modules that both
|
|
17
|
+
* paths share, such as the main module and the marker module. A name that no longer names a target
|
|
18
|
+
* cannot slip through: the build only drops a module whose target it read here.
|
|
19
|
+
*/
|
|
20
|
+
export declare function targetOfModule(name: string): ShaderTarget | null;
|
|
21
|
+
/** The shader targets that a project's build keeps. */
|
|
22
|
+
export interface ShippedTargets {
|
|
23
|
+
readonly wgsl: boolean;
|
|
24
|
+
readonly glsl: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The shader targets that the plugin's options ship. Both unless `gpuPaths` names one. Throws for
|
|
28
|
+
* a list that names no GPU path, an unknown path, or one without WebGPU: the shader compiler builds
|
|
29
|
+
* every shader for WebGPU, and a build that leaves it out would have no form for the shaders that a
|
|
30
|
+
* project's own modules and its add-ons declare.
|
|
31
|
+
*/
|
|
32
|
+
export declare function shippedTargets(options: ShaderOptions | undefined): ShippedTargets;
|
|
6
33
|
/** The shader options of the plugin. */
|
|
7
34
|
export interface ShaderOptions {
|
|
8
35
|
/**
|
|
@@ -15,6 +42,14 @@ export interface ShaderOptions {
|
|
|
15
42
|
* with and the list lacks.
|
|
16
43
|
*/
|
|
17
44
|
list?: string;
|
|
45
|
+
/**
|
|
46
|
+
* The GPU paths whose shaders the build ships, such as `['webgpu']` for an app whose browser
|
|
47
|
+
* frame always offers WebGPU, as an Electron desktop app's does. A build then drops the other
|
|
48
|
+
* path's shader files and fails with E1308 on a device that would need them. The default is
|
|
49
|
+
* both paths. `webgpu` is required, because the shader compiler builds every shader for it. The
|
|
50
|
+
* dev server keeps both paths. Docs: guides/assets-pipeline.md#ship-a-subset-of-the-gpu-paths.
|
|
51
|
+
*/
|
|
52
|
+
gpuPaths?: readonly GpuPath[];
|
|
18
53
|
}
|
|
19
54
|
/**
|
|
20
55
|
* The words of a list file's text: one word on each line, as hexadecimal with `0x` or as a decimal
|
|
@@ -104,8 +139,12 @@ export declare function builtWords(text: string): number[];
|
|
|
104
139
|
* word.
|
|
105
140
|
*/
|
|
106
141
|
export declare const UNLISTED_MODULE = "// The builds of a device module that the project's shader list leaves out.\nexport const SHADERS = {};\n";
|
|
107
|
-
/**
|
|
108
|
-
|
|
142
|
+
/**
|
|
143
|
+
* The generated marker module's text for a build: the listed words, or null for a build that copies
|
|
144
|
+
* the whole set, and the shipped GPU paths, or null for a build that ships both. The module that
|
|
145
|
+
* the shader build writes declares both names, so the engine reads one shape either way.
|
|
146
|
+
*/
|
|
147
|
+
export declare function shaderMarkersModule(words: ReadonlySet<number> | null, paths: readonly GpuPath[] | null): string;
|
|
109
148
|
/**
|
|
110
149
|
* The text of the generated device module `text` with only the builds whose permutation word
|
|
111
150
|
* `keep` takes, and the counts of the builds it kept and held. A shader keeps its entry with no
|
package/lib/shader-list.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// A project's list of the permutation words that its pages draw with, and the narrowing of the
|
|
2
|
-
// engine's shader set to that list
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
2
|
+
// engine's shader set: to that list, and to the GPU paths that the project's build ships. The set
|
|
3
|
+
// holds every build of every word for both paths, gigabytes in thousands of files, and a page
|
|
4
|
+
// downloads the few that its device needs. With a list, a build copies only the builds of the
|
|
5
|
+
// listed words: a device file keeps the listed builds and drops the others, and a file that holds
|
|
6
|
+
// no listed build is replaced by one module without builds. A page that then asks for a word
|
|
7
|
+
// outside the list gets the engine's message that names the word. With a path set, a build drops
|
|
8
|
+
// the other path's files, and the engine refuses that path on a device that needs it.
|
|
7
9
|
import { readFileSync, statSync } from 'node:fs';
|
|
8
10
|
import MagicString from 'magic-string';
|
|
9
11
|
import { parseAst } from 'vite';
|
|
@@ -13,6 +15,45 @@ import { DRAW_INDEX, HALF, TONE_MAP } from "./material-builds.js";
|
|
|
13
15
|
* list serves every device module, and a build matches a word with those bits cleared.
|
|
14
16
|
*/
|
|
15
17
|
export const DEVICE_BITS = DRAW_INDEX | TONE_MAP | HALF;
|
|
18
|
+
/** The shader target of a GPU path. */
|
|
19
|
+
export function targetOfPath(path) {
|
|
20
|
+
return path === 'webgl2' ? 'glsl' : 'wgsl';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The shader target of a generated shader module, from its name, or null for the modules that both
|
|
24
|
+
* paths share, such as the main module and the marker module. A name that no longer names a target
|
|
25
|
+
* cannot slip through: the build only drops a module whose target it read here.
|
|
26
|
+
*/
|
|
27
|
+
export function targetOfModule(name) {
|
|
28
|
+
const base = name.slice(name.lastIndexOf('/') + 1).replace(/\.(?:[cm]?[jt]s)$/, '');
|
|
29
|
+
const tokens = base.split('-');
|
|
30
|
+
if (tokens.includes('glsl'))
|
|
31
|
+
return 'glsl';
|
|
32
|
+
if (tokens.includes('wgsl'))
|
|
33
|
+
return 'wgsl';
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The shader targets that the plugin's options ship. Both unless `gpuPaths` names one. Throws for
|
|
38
|
+
* a list that names no GPU path, an unknown path, or one without WebGPU: the shader compiler builds
|
|
39
|
+
* every shader for WebGPU, and a build that leaves it out would have no form for the shaders that a
|
|
40
|
+
* project's own modules and its add-ons declare.
|
|
41
|
+
*/
|
|
42
|
+
export function shippedTargets(options) {
|
|
43
|
+
const paths = options?.gpuPaths;
|
|
44
|
+
if (paths === undefined)
|
|
45
|
+
return { wgsl: true, glsl: true };
|
|
46
|
+
const known = paths.filter((path) => path !== 'webgpu' && path !== 'webgl2');
|
|
47
|
+
if (known.length > 0)
|
|
48
|
+
throw new Error(`null3D: shaders.gpuPaths takes 'webgpu' or 'webgl2', not ${known.map((path) => `"${path}"`).join(', ')}.`);
|
|
49
|
+
if (paths.length === 0)
|
|
50
|
+
throw new Error("null3D: shaders.gpuPaths is empty. Name the GPU paths to ship, such as ['webgpu'], or leave it out for both.");
|
|
51
|
+
if (!paths.includes('webgpu'))
|
|
52
|
+
throw new Error("null3D: shaders.gpuPaths must include 'webgpu'. The shader compiler builds every shader for WebGPU, so a build without it has no form for your own shaders and your add-ons' shaders. Ship both GPU paths instead.");
|
|
53
|
+
if (!paths.includes('webgl2'))
|
|
54
|
+
return { wgsl: true, glsl: false };
|
|
55
|
+
return { wgsl: true, glsl: true };
|
|
56
|
+
}
|
|
16
57
|
/** The largest permutation word: the shader build keeps a word in 32 bits. */
|
|
17
58
|
const MAX_WORD = 0xffff_ffff;
|
|
18
59
|
/**
|
|
@@ -121,7 +162,7 @@ export class UnlistedWords {
|
|
|
121
162
|
this.warned.add(word >>> 0);
|
|
122
163
|
const text = wordText(word);
|
|
123
164
|
return {
|
|
124
|
-
message: `${page} draws with the permutation word ${text}, which the shader list ${this.name} lacks. The dev server draws it, but a production build with this list
|
|
165
|
+
message: `${page} draws with the permutation word ${text}, which the shader list ${this.name} lacks. The dev server draws it, but a production build with this list skips the pipeline or pass that asks for the word and reports it. Record the page again with bunx @null3d/cli shaders record, add the word by hand with bunx @null3d/cli shaders record --extra ${text}, or check the build with bunx @null3d/cli shaders verify.`,
|
|
125
166
|
first,
|
|
126
167
|
};
|
|
127
168
|
}
|
|
@@ -180,10 +221,15 @@ export function builtWords(text) {
|
|
|
180
221
|
* word.
|
|
181
222
|
*/
|
|
182
223
|
export const UNLISTED_MODULE = `// The builds of a device module that the project's shader list leaves out.\nexport const SHADERS = {};\n`;
|
|
183
|
-
/**
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
224
|
+
/**
|
|
225
|
+
* The generated marker module's text for a build: the listed words, or null for a build that copies
|
|
226
|
+
* the whole set, and the shipped GPU paths, or null for a build that ships both. The module that
|
|
227
|
+
* the shader build writes declares both names, so the engine reads one shape either way.
|
|
228
|
+
*/
|
|
229
|
+
export function shaderMarkersModule(words, paths) {
|
|
230
|
+
const listed = words === null ? 'null' : `[${[...words].sort((a, b) => a - b).join(', ')}]`;
|
|
231
|
+
const shipped = paths === null ? 'null' : `[${paths.map((path) => `'${path}'`).join(', ')}]`;
|
|
232
|
+
return `export const LISTED_PERMUTATIONS: readonly number[] | null = ${listed};\nexport const SHIPPED_GPU_PATHS: readonly ('webgpu' | 'webgl2')[] | null = ${shipped};\n`;
|
|
187
233
|
}
|
|
188
234
|
/** An object literal's properties, or undefined for any other node. */
|
|
189
235
|
function propertiesOf(node) {
|
package/lib/wgsl.d.ts
CHANGED
|
@@ -44,12 +44,13 @@ export type WgslCompile = {
|
|
|
44
44
|
};
|
|
45
45
|
/**
|
|
46
46
|
* Compiles WGSL from a project with the engine's shader library. A whole shader builds for WebGPU,
|
|
47
|
-
* and for WebGL2 when it has a render pipeline. A custom material's
|
|
48
|
-
* variant of the engine's standard material, and a custom effect's or
|
|
49
|
-
* template. `path` names the file in messages, and `hint` ends the message
|
|
50
|
-
* none of these. `compiler` runs the compile, on this thread by default.
|
|
47
|
+
* and for WebGL2 when it has a render pipeline and the compile ships that path. A custom material's
|
|
48
|
+
* functions build into every variant of the engine's standard material, and a custom effect's or
|
|
49
|
+
* tone curve's into its template. `path` names the file in messages, and `hint` ends the message
|
|
50
|
+
* about WGSL that is none of these. `compiler` runs the compile, on this thread by default. `glsl`
|
|
51
|
+
* false builds the WebGPU path alone, for a project whose build ships no WebGL2 shaders.
|
|
51
52
|
*/
|
|
52
|
-
export declare function compileWgsl(path: string, source: string, hint: string, compiler?: ShaderCompiler): Promise<WgslCompile>;
|
|
53
|
+
export declare function compileWgsl(path: string, source: string, hint: string, compiler?: ShaderCompiler, glsl?: boolean): Promise<WgslCompile>;
|
|
53
54
|
/** Where WGSL starts in the file that holds it: the file as messages name it, and a place. */
|
|
54
55
|
export interface WgslOrigin extends Place {
|
|
55
56
|
readonly path: string;
|
|
@@ -90,14 +91,14 @@ export type ShaderOrError = {
|
|
|
90
91
|
};
|
|
91
92
|
/**
|
|
92
93
|
* Compiles a `.wgsl` file. `path` names the file in messages, `id` is its module id, and `text` its
|
|
93
|
-
* content as read from disk.
|
|
94
|
+
* content as read from disk. `glsl` false builds the WebGPU path alone.
|
|
94
95
|
*/
|
|
95
|
-
export declare function compileWgslFile(path: string, id: string, text: string, compiler?: ShaderCompiler): Promise<ShaderOrError>;
|
|
96
|
+
export declare function compileWgslFile(path: string, id: string, text: string, compiler?: ShaderCompiler, glsl?: boolean): Promise<ShaderOrError>;
|
|
96
97
|
/**
|
|
97
98
|
* Compiles one tagged template literal of a script module, whose code is `code`: the compiled
|
|
98
99
|
* WGSL that takes its place, or the error that stops the module.
|
|
99
100
|
*/
|
|
100
|
-
export declare function compileLiteral(literal: TaggedWgsl, code: string, id: string, path: string, compiler?: ShaderCompiler): Promise<ShaderOrError>;
|
|
101
|
+
export declare function compileLiteral(literal: TaggedWgsl, code: string, id: string, path: string, compiler?: ShaderCompiler, glsl?: boolean): Promise<ShaderOrError>;
|
|
101
102
|
/** A tagged template literal of a module, and the compiled WGSL that takes its place. */
|
|
102
103
|
export interface TaggedShader {
|
|
103
104
|
readonly start: number;
|
|
@@ -108,7 +109,7 @@ export interface TaggedShader {
|
|
|
108
109
|
* Compiles each tagged template literal in a script module, all at once, with every literal's
|
|
109
110
|
* warnings, or gives the error that stops the module at its first literal that does not compile.
|
|
110
111
|
*/
|
|
111
|
-
export declare function compileTaggedWgsl(code: string, id: string, path: string, compiler?: ShaderCompiler): Promise<{
|
|
112
|
+
export declare function compileTaggedWgsl(code: string, id: string, path: string, compiler?: ShaderCompiler, glsl?: boolean): Promise<{
|
|
112
113
|
readonly shaders: readonly TaggedShader[];
|
|
113
114
|
readonly warnings: readonly string[];
|
|
114
115
|
} | {
|