@null3d/vite-plugin 0.1.0 → 0.1.1
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 +8 -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 +12 -2
- package/lib/hot.js +25 -4
- package/lib/index.d.ts +9 -0
- package/lib/index.js +185 -43
- package/lib/material-builds.d.ts +2 -0
- package/lib/material-builds.js +2 -0
- package/lib/paragraphs.d.ts +9 -3
- package/lib/paragraphs.js +25 -11
- package/lib/shader-list.d.ts +127 -0
- package/lib/shader-list.js +352 -0
- package/package.json +3 -3
|
@@ -0,0 +1,352 @@
|
|
|
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. The set holds every build of every word, gigabytes in
|
|
3
|
+
// thousands of files, and a page downloads the few that its device needs. With a list, a build
|
|
4
|
+
// copies only the builds of the listed words: a device file keeps the listed builds and drops the
|
|
5
|
+
// others, and a file that holds no listed build is replaced by one module without builds. A page
|
|
6
|
+
// that then asks for a word outside the list gets the engine's message that names the word.
|
|
7
|
+
import { readFileSync, statSync } from 'node:fs';
|
|
8
|
+
import MagicString from 'magic-string';
|
|
9
|
+
import { parseAst } from 'vite';
|
|
10
|
+
import { DRAW_INDEX, HALF, TONE_MAP } from "./material-builds.js";
|
|
11
|
+
/**
|
|
12
|
+
* The permutation bits that a device fixes when it starts. A listed word leaves them out, so one
|
|
13
|
+
* list serves every device module, and a build matches a word with those bits cleared.
|
|
14
|
+
*/
|
|
15
|
+
export const DEVICE_BITS = DRAW_INDEX | TONE_MAP | HALF;
|
|
16
|
+
/** The largest permutation word: the shader build keeps a word in 32 bits. */
|
|
17
|
+
const MAX_WORD = 0xffff_ffff;
|
|
18
|
+
/**
|
|
19
|
+
* The words of a list file's text: one word on each line, as hexadecimal with `0x` or as a decimal
|
|
20
|
+
* number, with `#` starting a comment, the rule that the engine's shader build reads. Throws with
|
|
21
|
+
* the line of a word it cannot read, and for a list without a word.
|
|
22
|
+
*/
|
|
23
|
+
export function parseShaderList(text, file) {
|
|
24
|
+
const words = [];
|
|
25
|
+
for (const [index, raw] of text.split('\n').entries()) {
|
|
26
|
+
const word = (raw.split('#')[0] ?? '').trim();
|
|
27
|
+
if (word === '')
|
|
28
|
+
continue;
|
|
29
|
+
const value = /^0[xX][0-9a-fA-F]+$/.test(word)
|
|
30
|
+
? Number.parseInt(word.slice(2), 16)
|
|
31
|
+
: /^\d+$/.test(word)
|
|
32
|
+
? Number(word)
|
|
33
|
+
: Number.NaN;
|
|
34
|
+
if (!(value <= MAX_WORD))
|
|
35
|
+
throw new Error(`${file} line ${index + 1}: "${word}" is not a permutation word. Write one hexadecimal word on each line, such as 0x00000010.`);
|
|
36
|
+
words.push(value);
|
|
37
|
+
}
|
|
38
|
+
if (words.length === 0)
|
|
39
|
+
throw new Error(`${file} lists no permutation word. Record the words with bunx @null3d/cli shaders record.`);
|
|
40
|
+
return words;
|
|
41
|
+
}
|
|
42
|
+
/** A project's list of permutation words. */
|
|
43
|
+
export class ShaderList {
|
|
44
|
+
file;
|
|
45
|
+
/** The listed words, without the bits that a device fixes. */
|
|
46
|
+
words;
|
|
47
|
+
constructor(
|
|
48
|
+
/** The list's file, as the build names it in its messages. */
|
|
49
|
+
file, words) {
|
|
50
|
+
this.file = file;
|
|
51
|
+
this.words = new Set([...words].map((word) => word >>> 0));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Reads the list at `path`, which messages name as `name`. Throws for a list without the word
|
|
55
|
+
* that has no bit set, which every page's start draws its own passes with.
|
|
56
|
+
*/
|
|
57
|
+
static read(path, name = path) {
|
|
58
|
+
let text;
|
|
59
|
+
try {
|
|
60
|
+
text = readFileSync(path, 'utf8');
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
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.`);
|
|
64
|
+
}
|
|
65
|
+
const list = new ShaderList(name, parseShaderList(text, name));
|
|
66
|
+
if (!list.words.has(0))
|
|
67
|
+
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.`);
|
|
68
|
+
return list;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* True when the list names the build whose permutation word is `permutation`: the word itself,
|
|
72
|
+
* or the word with the bits that a device fixes cleared.
|
|
73
|
+
*/
|
|
74
|
+
holds(permutation) {
|
|
75
|
+
return this.words.has(permutation >>> 0) || this.words.has((permutation & ~DEVICE_BITS) >>> 0);
|
|
76
|
+
}
|
|
77
|
+
/** The listed words that none of `held`, the words that a set's builds answer, holds. */
|
|
78
|
+
missing(held) {
|
|
79
|
+
return [...this.words].filter((word) => !held.has(word)).sort((a, b) => a - b);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The dev server's check of the words that its pages draw with against the project's list. The
|
|
84
|
+
* dev server draws with the whole shader set, so a page there draws a word that the list lacks,
|
|
85
|
+
* while a production build with the list stops that page. The check names such a word when a page
|
|
86
|
+
* first draws with it. It reads the list again whenever the file changes, so a recording made
|
|
87
|
+
* while the server runs counts at once.
|
|
88
|
+
*/
|
|
89
|
+
export class UnlistedWords {
|
|
90
|
+
path;
|
|
91
|
+
name;
|
|
92
|
+
/** The list file's change time when it was last read, or undefined before the first read. */
|
|
93
|
+
stamp;
|
|
94
|
+
list;
|
|
95
|
+
/** Why the list cannot be read, when it cannot. */
|
|
96
|
+
problem;
|
|
97
|
+
/** The words that the check has warned about since the list last changed. */
|
|
98
|
+
warned = new Set();
|
|
99
|
+
/** True once the check has reported the list's problem since the list last changed. */
|
|
100
|
+
problemShown = false;
|
|
101
|
+
constructor(path,
|
|
102
|
+
/** The list's file, as the warnings name it. */
|
|
103
|
+
name) {
|
|
104
|
+
this.path = path;
|
|
105
|
+
this.name = name;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The warning about `word`, which the page at `page` draws with, without the bits that a device
|
|
109
|
+
* fixes. Undefined when the list holds the word.
|
|
110
|
+
*/
|
|
111
|
+
check(word, page) {
|
|
112
|
+
this.refresh();
|
|
113
|
+
if (this.list === undefined) {
|
|
114
|
+
const first = !this.problemShown;
|
|
115
|
+
this.problemShown = true;
|
|
116
|
+
return { message: this.problem ?? '', first };
|
|
117
|
+
}
|
|
118
|
+
if (this.list.holds(word))
|
|
119
|
+
return undefined;
|
|
120
|
+
const first = !this.warned.has(word >>> 0);
|
|
121
|
+
this.warned.add(word >>> 0);
|
|
122
|
+
const text = wordText(word);
|
|
123
|
+
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 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}.`,
|
|
125
|
+
first,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
/** Reads the list again when its file changed since the last read. */
|
|
129
|
+
refresh() {
|
|
130
|
+
const stamp = statSync(this.path, { throwIfNoEntry: false })?.mtimeMs ?? -1;
|
|
131
|
+
if (stamp === this.stamp)
|
|
132
|
+
return;
|
|
133
|
+
this.stamp = stamp;
|
|
134
|
+
this.warned.clear();
|
|
135
|
+
this.problemShown = false;
|
|
136
|
+
try {
|
|
137
|
+
this.list = ShaderList.read(this.path, this.name);
|
|
138
|
+
this.problem = undefined;
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
this.list = undefined;
|
|
142
|
+
this.problem = error.message;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/** Adds to `held` each word that a build with the permutation word `permutation` answers. */
|
|
147
|
+
export function holdWord(held, permutation) {
|
|
148
|
+
held.add(permutation >>> 0);
|
|
149
|
+
held.add((permutation & ~DEVICE_BITS) >>> 0);
|
|
150
|
+
}
|
|
151
|
+
/** A word as the list writes it: eight hexadecimal digits after `0x`. */
|
|
152
|
+
export function wordText(word) {
|
|
153
|
+
return `0x${(word >>> 0).toString(16).padStart(8, '0')}`;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* A line of a generated shader module that holds a build's permutation word. The hash workers
|
|
157
|
+
* read the same lines in `hash-worker.js`, which cannot import this module.
|
|
158
|
+
*/
|
|
159
|
+
export const PERMUTATION_LINE = /^\t*permutation: (\d+),$/gm;
|
|
160
|
+
/** The permutation word of each build that a generated module's text holds. */
|
|
161
|
+
export function moduleWords(text) {
|
|
162
|
+
return [...text.matchAll(PERMUTATION_LINE)].map((match) => Number(match[1]));
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* A build's permutation word in any form that a built file holds it: a minified object literal, or
|
|
166
|
+
* the JSON text in which a module packs a custom material's builds, escaped inside a string.
|
|
167
|
+
*/
|
|
168
|
+
const BUILT_PERMUTATION = /\\?"?permutation\\?"?:\s*(\d+)/g;
|
|
169
|
+
/**
|
|
170
|
+
* The permutation words that a built file's text names: those of the builds that it bundles, such
|
|
171
|
+
* as the main shader module's own, a custom material's and an add-on's. A key of that name in other
|
|
172
|
+
* code adds a word too, so the words that a check reads from here can only be more, never fewer.
|
|
173
|
+
*/
|
|
174
|
+
export function builtWords(text) {
|
|
175
|
+
return [...text.matchAll(BUILT_PERMUTATION)].map((match) => Number(match[1]));
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* The module that stands for a device file whose builds the list leaves out. It holds no build,
|
|
179
|
+
* so a page that asks for one of those words loads it, finds no build, and the engine names the
|
|
180
|
+
* word.
|
|
181
|
+
*/
|
|
182
|
+
export const UNLISTED_MODULE = `// The builds of a device module that the project's shader list leaves out.\nexport const SHADERS = {};\n`;
|
|
183
|
+
/** The generated module that names the listed words, in place of the set's own. */
|
|
184
|
+
export function listedPermutationsModule(words) {
|
|
185
|
+
const sorted = [...words].sort((a, b) => a - b);
|
|
186
|
+
return `export const LISTED_PERMUTATIONS = [${sorted.join(', ')}];\n`;
|
|
187
|
+
}
|
|
188
|
+
/** An object literal's properties, or undefined for any other node. */
|
|
189
|
+
function propertiesOf(node) {
|
|
190
|
+
if (node?.type !== 'ObjectExpression')
|
|
191
|
+
return undefined;
|
|
192
|
+
const properties = node.properties;
|
|
193
|
+
return properties.every((property) => property.type === 'Property')
|
|
194
|
+
? properties
|
|
195
|
+
: undefined;
|
|
196
|
+
}
|
|
197
|
+
/** The name of a property's key, written as a name or as a string. */
|
|
198
|
+
function keyName(property) {
|
|
199
|
+
const { key } = property;
|
|
200
|
+
if (key.type === 'Identifier')
|
|
201
|
+
return key.name;
|
|
202
|
+
if (key.type === 'Literal' && typeof key.value === 'string')
|
|
203
|
+
return key.value;
|
|
204
|
+
return undefined;
|
|
205
|
+
}
|
|
206
|
+
/** Each name that `node` and the nodes inside it read. */
|
|
207
|
+
function namesIn(node, names) {
|
|
208
|
+
if (Array.isArray(node)) {
|
|
209
|
+
for (const item of node)
|
|
210
|
+
namesIn(item, names);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (node === null || typeof node !== 'object')
|
|
214
|
+
return;
|
|
215
|
+
const { type } = node;
|
|
216
|
+
if (type === 'Identifier')
|
|
217
|
+
names.add(node.name);
|
|
218
|
+
for (const [field, value] of Object.entries(node))
|
|
219
|
+
if (field !== 'type' && value !== null && typeof value === 'object')
|
|
220
|
+
namesIn(value, names);
|
|
221
|
+
}
|
|
222
|
+
/** The end of a statement that ends at `end`, with its line break, so that no blank line stays. */
|
|
223
|
+
function lineEnd(text, end) {
|
|
224
|
+
return text[end] === '\n' ? end + 1 : end;
|
|
225
|
+
}
|
|
226
|
+
/** Why a module cannot be narrowed: its form is not the one the shader build writes. */
|
|
227
|
+
function unreadable(file, why) {
|
|
228
|
+
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.`);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* The text of the generated device module `text` with only the builds whose permutation word
|
|
232
|
+
* `keep` takes, and the counts of the builds it kept and held. A shader keeps its entry with no
|
|
233
|
+
* build. Each shared paragraph and imported name that no kept build reads goes too, and so does an
|
|
234
|
+
* import of a table of paragraphs that no kept build reads. Throws when the module is not in the
|
|
235
|
+
* form that the shader build writes, so a build never ships a module it misread.
|
|
236
|
+
*/
|
|
237
|
+
export function narrowShaderModule(text, keep, file = 'a shader module') {
|
|
238
|
+
const program = parseAst(text);
|
|
239
|
+
const out = new MagicString(text);
|
|
240
|
+
/** Each top-level constant by its name, with the statement that declares it. */
|
|
241
|
+
const constants = new Map();
|
|
242
|
+
let shaders;
|
|
243
|
+
for (const statement of program.body) {
|
|
244
|
+
const declaration = statement.type === 'ExportNamedDeclaration' ? statement.declaration : statement;
|
|
245
|
+
if (declaration?.type !== 'VariableDeclaration')
|
|
246
|
+
continue;
|
|
247
|
+
for (const declarator of declaration.declarations) {
|
|
248
|
+
const name = declarator.id.name;
|
|
249
|
+
if (name === undefined)
|
|
250
|
+
continue;
|
|
251
|
+
if (name === 'SHADERS' && statement.type === 'ExportNamedDeclaration')
|
|
252
|
+
shaders = propertiesOf(declarator.init);
|
|
253
|
+
else if (statement === declaration)
|
|
254
|
+
constants.set(name, statement);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (shaders === undefined)
|
|
258
|
+
throw unreadable(file, 'it exports no object named SHADERS');
|
|
259
|
+
const kept = [];
|
|
260
|
+
let builds = 0;
|
|
261
|
+
for (const shader of shaders) {
|
|
262
|
+
const variants = propertiesOf(shader.value);
|
|
263
|
+
if (variants === undefined)
|
|
264
|
+
throw unreadable(file, `its shader ${keyName(shader)} is not an object of builds`);
|
|
265
|
+
for (const [index, variant] of variants.entries()) {
|
|
266
|
+
const word = propertiesOf(variant.value)?.find((field) => keyName(field) === 'permutation')
|
|
267
|
+
?.value;
|
|
268
|
+
if (word?.type !== 'Literal' || typeof word.value !== 'number')
|
|
269
|
+
throw unreadable(file, `its build ${keyName(variant)} has no permutation word`);
|
|
270
|
+
builds++;
|
|
271
|
+
if (keep(word.value)) {
|
|
272
|
+
kept.push(variant.value);
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
// A build goes with the text up to the next one, so the builds left keep their commas.
|
|
276
|
+
const next = variants[index + 1];
|
|
277
|
+
out.remove(variant.start, next === undefined ? shader.value.end - 1 : next.start);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
// The paragraphs that the kept builds read, and those that those paragraphs read.
|
|
281
|
+
const read = new Set();
|
|
282
|
+
namesIn(kept, read);
|
|
283
|
+
const pending = [...read];
|
|
284
|
+
for (let name = pending.pop(); name !== undefined; name = pending.pop()) {
|
|
285
|
+
const statement = constants.get(name);
|
|
286
|
+
if (statement === undefined)
|
|
287
|
+
continue;
|
|
288
|
+
const inner = new Set();
|
|
289
|
+
namesIn(statement, inner);
|
|
290
|
+
for (const more of inner)
|
|
291
|
+
if (!read.has(more)) {
|
|
292
|
+
read.add(more);
|
|
293
|
+
pending.push(more);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
for (const statement of program.body) {
|
|
297
|
+
if (statement.type === 'VariableDeclaration') {
|
|
298
|
+
const names = statement.declarations.map((declarator) => declarator.id.name ?? '');
|
|
299
|
+
const plain = statement.declarations.every((declarator) => {
|
|
300
|
+
const init = declarator.init;
|
|
301
|
+
return init?.type === 'TemplateLiteral' || init?.type === 'Literal';
|
|
302
|
+
});
|
|
303
|
+
if (plain && names.every((name) => !read.has(name)))
|
|
304
|
+
out.remove(statement.start, lineEnd(text, statement.end));
|
|
305
|
+
}
|
|
306
|
+
else if (statement.type === 'ImportDeclaration') {
|
|
307
|
+
const source = statement.source.value;
|
|
308
|
+
if (typeof source !== 'string' || !source.startsWith('./shader-paragraphs-'))
|
|
309
|
+
continue;
|
|
310
|
+
const specifiers = statement.specifiers;
|
|
311
|
+
const used = specifiers.filter((specifier) => specifier.type === 'ImportSpecifier' &&
|
|
312
|
+
read.has(specifier.local.name));
|
|
313
|
+
if (used.length === specifiers.length)
|
|
314
|
+
continue;
|
|
315
|
+
// The import keeps the shader build's form, which the reader of a copy's tables reads, and
|
|
316
|
+
// one that goes takes its line with it, since a blank line ends the imports.
|
|
317
|
+
if (used.length === 0)
|
|
318
|
+
out.remove(statement.start, lineEnd(text, statement.end));
|
|
319
|
+
else
|
|
320
|
+
out.overwrite(statement.start, statement.end, `import { ${used.map((specifier) => text.slice(specifier.start, specifier.end)).join(', ')} } from '${source}';`);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return { text: out.toString(), kept: kept.length, builds };
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* The text of a table of shared paragraphs with only the paragraphs that `names` lists, the names
|
|
327
|
+
* that the build's copies import from it. A paragraph of a table reads no other value, so each
|
|
328
|
+
* one stays or goes on its own. Throws when the table is not in the form that the shader build
|
|
329
|
+
* writes.
|
|
330
|
+
*/
|
|
331
|
+
export function narrowParagraphTable(text, names, file = 'a table of paragraphs') {
|
|
332
|
+
const program = parseAst(text);
|
|
333
|
+
const out = new MagicString(text);
|
|
334
|
+
for (const statement of program.body) {
|
|
335
|
+
if (statement.type !== 'ExportNamedDeclaration')
|
|
336
|
+
continue;
|
|
337
|
+
const declaration = statement.declaration;
|
|
338
|
+
const declarators = (declaration?.declarations ?? []);
|
|
339
|
+
const plain = declaration?.type === 'VariableDeclaration' &&
|
|
340
|
+
declarators.every((declarator) => {
|
|
341
|
+
const init = declarator.init;
|
|
342
|
+
return (init?.type === 'Literal' ||
|
|
343
|
+
(init?.type === 'TemplateLiteral' && init.expressions.length === 0));
|
|
344
|
+
});
|
|
345
|
+
if (!plain)
|
|
346
|
+
throw unreadable(file, 'one of its paragraphs is not plain text');
|
|
347
|
+
const unused = declarators.every((declarator) => !names.has(declarator.id.name ?? ''));
|
|
348
|
+
if (unused)
|
|
349
|
+
out.remove(statement.start, lineEnd(text, statement.end));
|
|
350
|
+
}
|
|
351
|
+
return out.toString();
|
|
352
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@null3d/vite-plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
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.1",
|
|
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.1",
|
|
65
65
|
"vite": "8.3.1"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|