@null3d/vite-plugin 0.1.0 → 0.1.2
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 +14 -1
- package/dist/shader-compiler.stamp.json +1 -1
- package/dist/shader-compiler.wasm +0 -0
- package/lib/assets.d.ts +3 -3
- package/lib/assets.js +3 -3
- package/lib/engine-shaders.d.ts +4 -2
- package/lib/engine-shaders.js +34 -7
- package/lib/hash-pool.d.ts +12 -0
- package/lib/hash-pool.js +17 -4
- package/lib/hash-worker.js +32 -9
- package/lib/hot.d.ts +23 -2
- package/lib/hot.js +50 -4
- package/lib/index.d.ts +9 -0
- package/lib/index.js +367 -58
- package/lib/material-builds.d.ts +21 -2
- package/lib/material-builds.js +63 -8
- package/lib/pack.d.ts +11 -1
- package/lib/pack.js +70 -1
- package/lib/paragraphs.d.ts +9 -3
- package/lib/paragraphs.js +25 -11
- package/lib/shader-list.d.ts +166 -0
- package/lib/shader-list.js +398 -0
- package/lib/wgsl.d.ts +10 -9
- package/lib/wgsl.js +31 -28
- package/package.json +3 -3
|
@@ -0,0 +1,398 @@
|
|
|
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, 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.
|
|
9
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
10
|
+
import MagicString from 'magic-string';
|
|
11
|
+
import { parseAst } from 'vite';
|
|
12
|
+
import { DRAW_INDEX, HALF, TONE_MAP } from "./material-builds.js";
|
|
13
|
+
/**
|
|
14
|
+
* The permutation bits that a device fixes when it starts. A listed word leaves them out, so one
|
|
15
|
+
* list serves every device module, and a build matches a word with those bits cleared.
|
|
16
|
+
*/
|
|
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
|
+
}
|
|
57
|
+
/** The largest permutation word: the shader build keeps a word in 32 bits. */
|
|
58
|
+
const MAX_WORD = 0xffff_ffff;
|
|
59
|
+
/**
|
|
60
|
+
* The words of a list file's text: one word on each line, as hexadecimal with `0x` or as a decimal
|
|
61
|
+
* number, with `#` starting a comment, the rule that the engine's shader build reads. Throws with
|
|
62
|
+
* the line of a word it cannot read, and for a list without a word.
|
|
63
|
+
*/
|
|
64
|
+
export function parseShaderList(text, file) {
|
|
65
|
+
const words = [];
|
|
66
|
+
for (const [index, raw] of text.split('\n').entries()) {
|
|
67
|
+
const word = (raw.split('#')[0] ?? '').trim();
|
|
68
|
+
if (word === '')
|
|
69
|
+
continue;
|
|
70
|
+
const value = /^0[xX][0-9a-fA-F]+$/.test(word)
|
|
71
|
+
? Number.parseInt(word.slice(2), 16)
|
|
72
|
+
: /^\d+$/.test(word)
|
|
73
|
+
? Number(word)
|
|
74
|
+
: Number.NaN;
|
|
75
|
+
if (!(value <= MAX_WORD))
|
|
76
|
+
throw new Error(`${file} line ${index + 1}: "${word}" is not a permutation word. Write one hexadecimal word on each line, such as 0x00000010.`);
|
|
77
|
+
words.push(value);
|
|
78
|
+
}
|
|
79
|
+
if (words.length === 0)
|
|
80
|
+
throw new Error(`${file} lists no permutation word. Record the words with bunx @null3d/cli shaders record.`);
|
|
81
|
+
return words;
|
|
82
|
+
}
|
|
83
|
+
/** A project's list of permutation words. */
|
|
84
|
+
export class ShaderList {
|
|
85
|
+
file;
|
|
86
|
+
/** The listed words, without the bits that a device fixes. */
|
|
87
|
+
words;
|
|
88
|
+
constructor(
|
|
89
|
+
/** The list's file, as the build names it in its messages. */
|
|
90
|
+
file, words) {
|
|
91
|
+
this.file = file;
|
|
92
|
+
this.words = new Set([...words].map((word) => word >>> 0));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Reads the list at `path`, which messages name as `name`. Throws for a list without the word
|
|
96
|
+
* that has no bit set, which every page's start draws its own passes with.
|
|
97
|
+
*/
|
|
98
|
+
static read(path, name = path) {
|
|
99
|
+
let text;
|
|
100
|
+
try {
|
|
101
|
+
text = readFileSync(path, 'utf8');
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
throw new Error(`cannot read the shader list ${name} (${error.message}). Record it with bunx @null3d/cli shaders record, or fix shaders.list in the null3D plugin's options.`);
|
|
105
|
+
}
|
|
106
|
+
const list = new ShaderList(name, parseShaderList(text, name));
|
|
107
|
+
if (!list.words.has(0))
|
|
108
|
+
throw new Error(`the shader list ${name} lacks 0x00000000, the word of the engine's own passes, such as its culling, which every page draws with. Record the list again with bunx @null3d/cli shaders record, or add the word to it.`);
|
|
109
|
+
return list;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* True when the list names the build whose permutation word is `permutation`: the word itself,
|
|
113
|
+
* or the word with the bits that a device fixes cleared.
|
|
114
|
+
*/
|
|
115
|
+
holds(permutation) {
|
|
116
|
+
return this.words.has(permutation >>> 0) || this.words.has((permutation & ~DEVICE_BITS) >>> 0);
|
|
117
|
+
}
|
|
118
|
+
/** The listed words that none of `held`, the words that a set's builds answer, holds. */
|
|
119
|
+
missing(held) {
|
|
120
|
+
return [...this.words].filter((word) => !held.has(word)).sort((a, b) => a - b);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The dev server's check of the words that its pages draw with against the project's list. The
|
|
125
|
+
* dev server draws with the whole shader set, so a page there draws a word that the list lacks,
|
|
126
|
+
* while a production build with the list stops that page. The check names such a word when a page
|
|
127
|
+
* first draws with it. It reads the list again whenever the file changes, so a recording made
|
|
128
|
+
* while the server runs counts at once.
|
|
129
|
+
*/
|
|
130
|
+
export class UnlistedWords {
|
|
131
|
+
path;
|
|
132
|
+
name;
|
|
133
|
+
/** The list file's change time when it was last read, or undefined before the first read. */
|
|
134
|
+
stamp;
|
|
135
|
+
list;
|
|
136
|
+
/** Why the list cannot be read, when it cannot. */
|
|
137
|
+
problem;
|
|
138
|
+
/** The words that the check has warned about since the list last changed. */
|
|
139
|
+
warned = new Set();
|
|
140
|
+
/** True once the check has reported the list's problem since the list last changed. */
|
|
141
|
+
problemShown = false;
|
|
142
|
+
constructor(path,
|
|
143
|
+
/** The list's file, as the warnings name it. */
|
|
144
|
+
name) {
|
|
145
|
+
this.path = path;
|
|
146
|
+
this.name = name;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* The warning about `word`, which the page at `page` draws with, without the bits that a device
|
|
150
|
+
* fixes. Undefined when the list holds the word.
|
|
151
|
+
*/
|
|
152
|
+
check(word, page) {
|
|
153
|
+
this.refresh();
|
|
154
|
+
if (this.list === undefined) {
|
|
155
|
+
const first = !this.problemShown;
|
|
156
|
+
this.problemShown = true;
|
|
157
|
+
return { message: this.problem ?? '', first };
|
|
158
|
+
}
|
|
159
|
+
if (this.list.holds(word))
|
|
160
|
+
return undefined;
|
|
161
|
+
const first = !this.warned.has(word >>> 0);
|
|
162
|
+
this.warned.add(word >>> 0);
|
|
163
|
+
const text = wordText(word);
|
|
164
|
+
return {
|
|
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 stops the page with an error that names the word. Record the page again with bunx @null3d/cli shaders record, or add the word by hand with bunx @null3d/cli shaders record --extra ${text}.`,
|
|
166
|
+
first,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/** Reads the list again when its file changed since the last read. */
|
|
170
|
+
refresh() {
|
|
171
|
+
const stamp = statSync(this.path, { throwIfNoEntry: false })?.mtimeMs ?? -1;
|
|
172
|
+
if (stamp === this.stamp)
|
|
173
|
+
return;
|
|
174
|
+
this.stamp = stamp;
|
|
175
|
+
this.warned.clear();
|
|
176
|
+
this.problemShown = false;
|
|
177
|
+
try {
|
|
178
|
+
this.list = ShaderList.read(this.path, this.name);
|
|
179
|
+
this.problem = undefined;
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
this.list = undefined;
|
|
183
|
+
this.problem = error.message;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/** Adds to `held` each word that a build with the permutation word `permutation` answers. */
|
|
188
|
+
export function holdWord(held, permutation) {
|
|
189
|
+
held.add(permutation >>> 0);
|
|
190
|
+
held.add((permutation & ~DEVICE_BITS) >>> 0);
|
|
191
|
+
}
|
|
192
|
+
/** A word as the list writes it: eight hexadecimal digits after `0x`. */
|
|
193
|
+
export function wordText(word) {
|
|
194
|
+
return `0x${(word >>> 0).toString(16).padStart(8, '0')}`;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* A line of a generated shader module that holds a build's permutation word. The hash workers
|
|
198
|
+
* read the same lines in `hash-worker.js`, which cannot import this module.
|
|
199
|
+
*/
|
|
200
|
+
export const PERMUTATION_LINE = /^\t*permutation: (\d+),$/gm;
|
|
201
|
+
/** The permutation word of each build that a generated module's text holds. */
|
|
202
|
+
export function moduleWords(text) {
|
|
203
|
+
return [...text.matchAll(PERMUTATION_LINE)].map((match) => Number(match[1]));
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* A build's permutation word in any form that a built file holds it: a minified object literal, or
|
|
207
|
+
* the JSON text in which a module packs a custom material's builds, escaped inside a string.
|
|
208
|
+
*/
|
|
209
|
+
const BUILT_PERMUTATION = /\\?"?permutation\\?"?:\s*(\d+)/g;
|
|
210
|
+
/**
|
|
211
|
+
* The permutation words that a built file's text names: those of the builds that it bundles, such
|
|
212
|
+
* as the main shader module's own, a custom material's and an add-on's. A key of that name in other
|
|
213
|
+
* code adds a word too, so the words that a check reads from here can only be more, never fewer.
|
|
214
|
+
*/
|
|
215
|
+
export function builtWords(text) {
|
|
216
|
+
return [...text.matchAll(BUILT_PERMUTATION)].map((match) => Number(match[1]));
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The module that stands for a device file whose builds the list leaves out. It holds no build,
|
|
220
|
+
* so a page that asks for one of those words loads it, finds no build, and the engine names the
|
|
221
|
+
* word.
|
|
222
|
+
*/
|
|
223
|
+
export const UNLISTED_MODULE = `// The builds of a device module that the project's shader list leaves out.\nexport const SHADERS = {};\n`;
|
|
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`;
|
|
233
|
+
}
|
|
234
|
+
/** An object literal's properties, or undefined for any other node. */
|
|
235
|
+
function propertiesOf(node) {
|
|
236
|
+
if (node?.type !== 'ObjectExpression')
|
|
237
|
+
return undefined;
|
|
238
|
+
const properties = node.properties;
|
|
239
|
+
return properties.every((property) => property.type === 'Property')
|
|
240
|
+
? properties
|
|
241
|
+
: undefined;
|
|
242
|
+
}
|
|
243
|
+
/** The name of a property's key, written as a name or as a string. */
|
|
244
|
+
function keyName(property) {
|
|
245
|
+
const { key } = property;
|
|
246
|
+
if (key.type === 'Identifier')
|
|
247
|
+
return key.name;
|
|
248
|
+
if (key.type === 'Literal' && typeof key.value === 'string')
|
|
249
|
+
return key.value;
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
/** Each name that `node` and the nodes inside it read. */
|
|
253
|
+
function namesIn(node, names) {
|
|
254
|
+
if (Array.isArray(node)) {
|
|
255
|
+
for (const item of node)
|
|
256
|
+
namesIn(item, names);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (node === null || typeof node !== 'object')
|
|
260
|
+
return;
|
|
261
|
+
const { type } = node;
|
|
262
|
+
if (type === 'Identifier')
|
|
263
|
+
names.add(node.name);
|
|
264
|
+
for (const [field, value] of Object.entries(node))
|
|
265
|
+
if (field !== 'type' && value !== null && typeof value === 'object')
|
|
266
|
+
namesIn(value, names);
|
|
267
|
+
}
|
|
268
|
+
/** The end of a statement that ends at `end`, with its line break, so that no blank line stays. */
|
|
269
|
+
function lineEnd(text, end) {
|
|
270
|
+
return text[end] === '\n' ? end + 1 : end;
|
|
271
|
+
}
|
|
272
|
+
/** Why a module cannot be narrowed: its form is not the one the shader build writes. */
|
|
273
|
+
function unreadable(file, why) {
|
|
274
|
+
return new Error(`null3D cannot narrow ${file} to the shader list: ${why}. The plugin reads the form that the engine's shader build writes, so install the same version of @null3d/vite-plugin as of @null3d/engine.`);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* The text of the generated device module `text` with only the builds whose permutation word
|
|
278
|
+
* `keep` takes, and the counts of the builds it kept and held. A shader keeps its entry with no
|
|
279
|
+
* build. Each shared paragraph and imported name that no kept build reads goes too, and so does an
|
|
280
|
+
* import of a table of paragraphs that no kept build reads. Throws when the module is not in the
|
|
281
|
+
* form that the shader build writes, so a build never ships a module it misread.
|
|
282
|
+
*/
|
|
283
|
+
export function narrowShaderModule(text, keep, file = 'a shader module') {
|
|
284
|
+
const program = parseAst(text);
|
|
285
|
+
const out = new MagicString(text);
|
|
286
|
+
/** Each top-level constant by its name, with the statement that declares it. */
|
|
287
|
+
const constants = new Map();
|
|
288
|
+
let shaders;
|
|
289
|
+
for (const statement of program.body) {
|
|
290
|
+
const declaration = statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;
|
|
291
|
+
if (declaration?.type !== 'VariableDeclaration')
|
|
292
|
+
continue;
|
|
293
|
+
for (const declarator of declaration.declarations) {
|
|
294
|
+
const name = declarator.id.name;
|
|
295
|
+
if (name === undefined)
|
|
296
|
+
continue;
|
|
297
|
+
if (name === 'SHADERS' && statement.type === 'ExportNamedDeclaration')
|
|
298
|
+
shaders = propertiesOf(declarator.init);
|
|
299
|
+
else if (statement === declaration)
|
|
300
|
+
constants.set(name, statement);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (shaders === undefined)
|
|
304
|
+
throw unreadable(file, 'it exports no object named SHADERS');
|
|
305
|
+
const kept = [];
|
|
306
|
+
let builds = 0;
|
|
307
|
+
for (const shader of shaders) {
|
|
308
|
+
const variants = propertiesOf(shader.value);
|
|
309
|
+
if (variants === undefined)
|
|
310
|
+
throw unreadable(file, `its shader ${keyName(shader)} is not an object of builds`);
|
|
311
|
+
for (const [index, variant] of variants.entries()) {
|
|
312
|
+
const word = propertiesOf(variant.value)?.find((field) => keyName(field) === 'permutation')
|
|
313
|
+
?.value;
|
|
314
|
+
if (word?.type !== 'Literal' || typeof word.value !== 'number')
|
|
315
|
+
throw unreadable(file, `its build ${keyName(variant)} has no permutation word`);
|
|
316
|
+
builds++;
|
|
317
|
+
if (keep(word.value)) {
|
|
318
|
+
kept.push(variant.value);
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
// A build goes with the text up to the next one, so the builds left keep their commas.
|
|
322
|
+
const next = variants[index + 1];
|
|
323
|
+
out.remove(variant.start, next === undefined ? shader.value.end - 1 : next.start);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
// The paragraphs that the kept builds read, and those that those paragraphs read.
|
|
327
|
+
const read = new Set();
|
|
328
|
+
namesIn(kept, read);
|
|
329
|
+
const pending = [...read];
|
|
330
|
+
for (let name = pending.pop(); name !== undefined; name = pending.pop()) {
|
|
331
|
+
const statement = constants.get(name);
|
|
332
|
+
if (statement === undefined)
|
|
333
|
+
continue;
|
|
334
|
+
const inner = new Set();
|
|
335
|
+
namesIn(statement, inner);
|
|
336
|
+
for (const more of inner)
|
|
337
|
+
if (!read.has(more)) {
|
|
338
|
+
read.add(more);
|
|
339
|
+
pending.push(more);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
for (const statement of program.body) {
|
|
343
|
+
if (statement.type === 'VariableDeclaration') {
|
|
344
|
+
const names = statement.declarations.map((declarator) => declarator.id.name ?? '');
|
|
345
|
+
const plain = statement.declarations.every((declarator) => {
|
|
346
|
+
const init = declarator.init;
|
|
347
|
+
return init?.type === 'TemplateLiteral' || init?.type === 'Literal';
|
|
348
|
+
});
|
|
349
|
+
if (plain && names.every((name) => !read.has(name)))
|
|
350
|
+
out.remove(statement.start, lineEnd(text, statement.end));
|
|
351
|
+
}
|
|
352
|
+
else if (statement.type === 'ImportDeclaration') {
|
|
353
|
+
const source = statement.source.value;
|
|
354
|
+
if (typeof source !== 'string' || !source.startsWith('./shader-paragraphs-'))
|
|
355
|
+
continue;
|
|
356
|
+
const specifiers = statement.specifiers;
|
|
357
|
+
const used = specifiers.filter((specifier) => specifier.type === 'ImportSpecifier' &&
|
|
358
|
+
read.has(specifier.local.name));
|
|
359
|
+
if (used.length === specifiers.length)
|
|
360
|
+
continue;
|
|
361
|
+
// The import keeps the shader build's form, which the reader of a copy's tables reads, and
|
|
362
|
+
// one that goes takes its line with it, since a blank line ends the imports.
|
|
363
|
+
if (used.length === 0)
|
|
364
|
+
out.remove(statement.start, lineEnd(text, statement.end));
|
|
365
|
+
else
|
|
366
|
+
out.overwrite(statement.start, statement.end, `import { ${used.map((specifier) => text.slice(specifier.start, specifier.end)).join(', ')} } from '${source}';`);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return { text: out.toString(), kept: kept.length, builds };
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* The text of a table of shared paragraphs with only the paragraphs that `names` lists, the names
|
|
373
|
+
* that the build's copies import from it. A paragraph of a table reads no other value, so each
|
|
374
|
+
* one stays or goes on its own. Throws when the table is not in the form that the shader build
|
|
375
|
+
* writes.
|
|
376
|
+
*/
|
|
377
|
+
export function narrowParagraphTable(text, names, file = 'a table of paragraphs') {
|
|
378
|
+
const program = parseAst(text);
|
|
379
|
+
const out = new MagicString(text);
|
|
380
|
+
for (const statement of program.body) {
|
|
381
|
+
if (statement.type !== 'ExportNamedDeclaration')
|
|
382
|
+
continue;
|
|
383
|
+
const declaration = statement.declaration;
|
|
384
|
+
const declarators = (declaration?.declarations ?? []);
|
|
385
|
+
const plain = declaration?.type === 'VariableDeclaration' &&
|
|
386
|
+
declarators.every((declarator) => {
|
|
387
|
+
const init = declarator.init;
|
|
388
|
+
return (init?.type === 'Literal' ||
|
|
389
|
+
(init?.type === 'TemplateLiteral' && init.expressions.length === 0));
|
|
390
|
+
});
|
|
391
|
+
if (!plain)
|
|
392
|
+
throw unreadable(file, 'one of its paragraphs is not plain text');
|
|
393
|
+
const unused = declarators.every((declarator) => !names.has(declarator.id.name ?? ''));
|
|
394
|
+
if (unused)
|
|
395
|
+
out.remove(statement.start, lineEnd(text, statement.end));
|
|
396
|
+
}
|
|
397
|
+
return out.toString();
|
|
398
|
+
}
|
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
|
} | {
|
package/lib/wgsl.js
CHANGED
|
@@ -214,10 +214,12 @@ function pipelinesOf(path, source, hint) {
|
|
|
214
214
|
return { pipelines };
|
|
215
215
|
}
|
|
216
216
|
/** The builds of a shader from a project: one for WebGPU, and one for WebGL2. */
|
|
217
|
-
const
|
|
217
|
+
const ALL_BUILDS = {
|
|
218
218
|
webgpu: { targets: ['wgsl'] },
|
|
219
219
|
webgl2: { defs: ['WEBGL2'], targets: ['glsl'] },
|
|
220
220
|
};
|
|
221
|
+
/** The build that a compile of whole shaders makes when it ships the WebGPU path alone. */
|
|
222
|
+
const WEBGPU_BUILD = { webgpu: ALL_BUILDS.webgpu };
|
|
221
223
|
/**
|
|
222
224
|
* What a compile of a project's WGSL of `source` does. WGSL without entry points that declares a
|
|
223
225
|
* post function is an effect or a tone curve, one that declares `skyLayer` is a sky layer, and one
|
|
@@ -225,7 +227,7 @@ const BUILDS = {
|
|
|
225
227
|
* shader, one that the engine's mesh calls or a draw shader. `hint` ends the message about WGSL
|
|
226
228
|
* that is none of these.
|
|
227
229
|
*/
|
|
228
|
-
function planOf(path, source, hint) {
|
|
230
|
+
function planOf(path, source, hint, glsl) {
|
|
229
231
|
const noEntryPoints = entryPoints(source).length === 0;
|
|
230
232
|
if (noEntryPoints && declaresFunction(source, SKY_LAYER_FUNCTIONS))
|
|
231
233
|
return { kind: 'skyLayer' };
|
|
@@ -240,13 +242,13 @@ function planOf(path, source, hint) {
|
|
|
240
242
|
if ('problem' in shape)
|
|
241
243
|
return { kind: 'problem', problem: shape.problem };
|
|
242
244
|
const render = Object.keys(shape.pipelines).length > 0;
|
|
243
|
-
const variants = render
|
|
245
|
+
const variants = render && glsl ? ALL_BUILDS : WEBGPU_BUILD;
|
|
244
246
|
return { kind: 'shader', pipelines: shape.pipelines, variants, builds: Object.keys(variants) };
|
|
245
247
|
}
|
|
246
248
|
/** An effect's or a tone curve's compile as the result of compiling WGSL from a project. */
|
|
247
|
-
function effectOutcome(result) {
|
|
248
|
-
// Effects and tone curves build for
|
|
249
|
-
const builds = ['webgpu', 'webgl2'];
|
|
249
|
+
function effectOutcome(result, glsl) {
|
|
250
|
+
// Effects and tone curves build for the paths that the compile ships.
|
|
251
|
+
const builds = glsl ? ['webgpu', 'webgl2'] : ['webgpu'];
|
|
250
252
|
if (!result.ok)
|
|
251
253
|
return { ok: false, problems: result.problems, builds };
|
|
252
254
|
const { warnings } = result;
|
|
@@ -261,9 +263,9 @@ function effectOutcome(result) {
|
|
|
261
263
|
};
|
|
262
264
|
}
|
|
263
265
|
/** A sky layer's compile as the result of compiling WGSL from a project. */
|
|
264
|
-
function skyLayerOutcome(result) {
|
|
265
|
-
// A sky layer builds for
|
|
266
|
-
const builds = ['webgpu', 'webgl2'];
|
|
266
|
+
function skyLayerOutcome(result, glsl) {
|
|
267
|
+
// A sky layer builds for the paths that the compile ships.
|
|
268
|
+
const builds = glsl ? ['webgpu', 'webgl2'] : ['webgpu'];
|
|
267
269
|
if (!result.ok)
|
|
268
270
|
return { ok: false, problems: result.problems, builds };
|
|
269
271
|
return {
|
|
@@ -274,9 +276,9 @@ function skyLayerOutcome(result) {
|
|
|
274
276
|
};
|
|
275
277
|
}
|
|
276
278
|
/** A custom material's compile as the result of compiling WGSL from a project. */
|
|
277
|
-
function materialOutcome(result, draw) {
|
|
278
|
-
// Custom materials build for
|
|
279
|
-
const builds = ['webgpu', 'webgl2'];
|
|
279
|
+
function materialOutcome(result, draw, glsl) {
|
|
280
|
+
// Custom materials build for the paths that the compile ships.
|
|
281
|
+
const builds = glsl ? ['webgpu', 'webgl2'] : ['webgpu'];
|
|
280
282
|
if (!result.ok)
|
|
281
283
|
return { ok: false, problems: result.problems, builds };
|
|
282
284
|
const shader = {
|
|
@@ -298,22 +300,23 @@ function shaderOutcome(result, builds) {
|
|
|
298
300
|
}
|
|
299
301
|
/**
|
|
300
302
|
* Compiles WGSL from a project with the engine's shader library. A whole shader builds for WebGPU,
|
|
301
|
-
* and for WebGL2 when it has a render pipeline. A custom material's
|
|
302
|
-
* variant of the engine's standard material, and a custom effect's or
|
|
303
|
-
* template. `path` names the file in messages, and `hint` ends the message
|
|
304
|
-
* none of these. `compiler` runs the compile, on this thread by default.
|
|
303
|
+
* and for WebGL2 when it has a render pipeline and the compile ships that path. A custom material's
|
|
304
|
+
* functions build into every variant of the engine's standard material, and a custom effect's or
|
|
305
|
+
* tone curve's into its template. `path` names the file in messages, and `hint` ends the message
|
|
306
|
+
* about WGSL that is none of these. `compiler` runs the compile, on this thread by default. `glsl`
|
|
307
|
+
* false builds the WebGPU path alone, for a project whose build ships no WebGL2 shaders.
|
|
305
308
|
*/
|
|
306
|
-
export async function compileWgsl(path, source, hint, compiler = compileHere) {
|
|
307
|
-
const plan = planOf(path, source, hint);
|
|
309
|
+
export async function compileWgsl(path, source, hint, compiler = compileHere, glsl = true) {
|
|
310
|
+
const plan = planOf(path, source, hint, glsl);
|
|
308
311
|
switch (plan.kind) {
|
|
309
312
|
case 'problem':
|
|
310
313
|
return { ok: false, problems: [plan.problem], builds: [] };
|
|
311
314
|
case 'effect':
|
|
312
|
-
return effectOutcome(await compiler.effect({ path, source }));
|
|
315
|
+
return effectOutcome(await compiler.effect({ path, source }), glsl);
|
|
313
316
|
case 'skyLayer':
|
|
314
|
-
return skyLayerOutcome(await compiler.skyLayer({ path, source }));
|
|
317
|
+
return skyLayerOutcome(await compiler.skyLayer({ path, source }), glsl);
|
|
315
318
|
case 'material':
|
|
316
|
-
return materialOutcome(await compiler.material({ path, source }), plan.draw);
|
|
319
|
+
return materialOutcome(await compiler.material({ path, source }), plan.draw, glsl);
|
|
317
320
|
case 'shader':
|
|
318
321
|
return shaderOutcome(await compiler.shader({ path, source, pipelines: plan.pipelines, variants: plan.variants }), plan.builds);
|
|
319
322
|
}
|
|
@@ -387,25 +390,25 @@ function fileOutcome(compiled, source, origin, id) {
|
|
|
387
390
|
}
|
|
388
391
|
/**
|
|
389
392
|
* Compiles a `.wgsl` file. `path` names the file in messages, `id` is its module id, and `text` its
|
|
390
|
-
* content as read from disk.
|
|
393
|
+
* content as read from disk. `glsl` false builds the WebGPU path alone.
|
|
391
394
|
*/
|
|
392
|
-
export async function compileWgslFile(path, id, text, compiler = compileHere) {
|
|
395
|
+
export async function compileWgslFile(path, id, text, compiler = compileHere, glsl = true) {
|
|
393
396
|
const source = text.replaceAll('\r\n', '\n');
|
|
394
|
-
const compiled = await compileWgsl(path, source, FILE_HINT, compiler);
|
|
397
|
+
const compiled = await compileWgsl(path, source, FILE_HINT, compiler, glsl);
|
|
395
398
|
return fileOutcome(compiled, source, { path, line: 1, column: 1 }, id);
|
|
396
399
|
}
|
|
397
400
|
/**
|
|
398
401
|
* Compiles one tagged template literal of a script module, whose code is `code`: the compiled
|
|
399
402
|
* WGSL that takes its place, or the error that stops the module.
|
|
400
403
|
*/
|
|
401
|
-
export async function compileLiteral(literal, code, id, path, compiler = compileHere) {
|
|
404
|
+
export async function compileLiteral(literal, code, id, path, compiler = compileHere, glsl = true) {
|
|
402
405
|
if (literal.substitution !== null) {
|
|
403
406
|
const problem = problemAt(path, code, literal.substitution, SUBSTITUTION);
|
|
404
407
|
const failed = { ok: false, problems: [problem], builds: [] };
|
|
405
408
|
return { error: wgslError(failed, code, { path, line: 1, column: 1 }, id, code) };
|
|
406
409
|
}
|
|
407
410
|
const origin = { path, ...placeOf(code, literal.start + 1) };
|
|
408
|
-
const compiled = await compileWgsl(path, literal.source, TAG_HINT, compiler);
|
|
411
|
+
const compiled = await compileWgsl(path, literal.source, TAG_HINT, compiler, glsl);
|
|
409
412
|
if (compiled.ok)
|
|
410
413
|
return { shader: compiled.shader, warnings: warningLines(compiled, literal.source, origin) };
|
|
411
414
|
return { error: wgslError(compiled, literal.source, origin, id, code) };
|
|
@@ -414,9 +417,9 @@ export async function compileLiteral(literal, code, id, path, compiler = compile
|
|
|
414
417
|
* Compiles each tagged template literal in a script module, all at once, with every literal's
|
|
415
418
|
* warnings, or gives the error that stops the module at its first literal that does not compile.
|
|
416
419
|
*/
|
|
417
|
-
export async function compileTaggedWgsl(code, id, path, compiler = compileHere) {
|
|
420
|
+
export async function compileTaggedWgsl(code, id, path, compiler = compileHere, glsl = true) {
|
|
418
421
|
const literals = findTaggedWgsl(code, id);
|
|
419
|
-
const results = await Promise.all(literals.map((literal) => compileLiteral(literal, code, id, path, compiler)));
|
|
422
|
+
const results = await Promise.all(literals.map((literal) => compileLiteral(literal, code, id, path, compiler, glsl)));
|
|
420
423
|
const shaders = [];
|
|
421
424
|
const warnings = [];
|
|
422
425
|
for (const [k, result] of results.entries()) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@null3d/vite-plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Vite plugin for null3D projects: cross-origin isolation headers, HTTPS for testing on phones and tablets, WGSL compiled for WebGPU and WebGL2, glTF models optimized on import, and production builds that compile the sketch module and ship the engine core.",
|
|
5
5
|
"license": "MIT OR Apache-2.0",
|
|
6
6
|
"author": "Ramesh Nair (https://hiddentao.com)",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"prepack": "bun ../../tools/build-package.ts vite-plugin"
|
|
53
53
|
},
|
|
54
54
|
"peerDependencies": {
|
|
55
|
-
"@null3d/cli": "0.
|
|
55
|
+
"@null3d/cli": "0.1.2",
|
|
56
56
|
"vite": ">=8"
|
|
57
57
|
},
|
|
58
58
|
"peerDependenciesMeta": {
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
}
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@null3d/cli": "0.
|
|
64
|
+
"@null3d/cli": "0.1.2",
|
|
65
65
|
"vite": "8.3.1"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|