@null3d/vite-plugin 0.0.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 +95 -0
- package/THIRD-PARTY-NOTICES.txt +18 -3
- package/dist/shader-compiler.stamp.json +1 -1
- package/dist/shader-compiler.wasm +0 -0
- package/lib/assets.d.ts +6 -6
- package/lib/assets.js +3 -3
- package/lib/compress.d.ts +47 -0
- package/lib/compress.js +391 -0
- 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 +38 -2
- package/lib/index.js +271 -52
- 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/lib/wgsl.js +2 -1
- package/lib/worker-preload.d.ts +13 -0
- package/lib/worker-preload.js +19 -0
- package/package.json +3 -3
package/lib/paragraphs.js
CHANGED
|
@@ -10,8 +10,11 @@
|
|
|
10
10
|
// whatever the project's assets folder and asset naming pattern are.
|
|
11
11
|
import { closeSync, existsSync, openSync, readFileSync, readSync, statSync } from 'node:fs';
|
|
12
12
|
import { dirname, isAbsolute, join, posix, resolve } from 'node:path';
|
|
13
|
-
/**
|
|
14
|
-
|
|
13
|
+
/**
|
|
14
|
+
* 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.
|
|
16
|
+
*/
|
|
17
|
+
const PARAGRAPH_IMPORT = /import\s*\{([^}]*)\}\s*from '\.\/(shader-paragraphs-[a-z0-9-]+)\.js';/g;
|
|
15
18
|
/**
|
|
16
19
|
* How much of a copied file this reads at first. The shader build writes the imports among the
|
|
17
20
|
* first lines, and a device module is hundreds of kilobytes, so the rest is never read. A device
|
|
@@ -26,11 +29,11 @@ function headOf(source, bytes) {
|
|
|
26
29
|
return new TextDecoder().decode(source.subarray(0, bytes));
|
|
27
30
|
}
|
|
28
31
|
/**
|
|
29
|
-
* The
|
|
30
|
-
* kind's table and one for each feature family whose paragraphs it holds, so the module
|
|
31
|
-
* relief feature imports several. The imports end at the first blank line after them.
|
|
32
|
+
* The paragraph tables that a device module imports, with the names it takes from each. A module
|
|
33
|
+
* imports its kind's table and one for each feature family whose paragraphs it holds, so the module
|
|
34
|
+
* of a relief feature imports several. The imports end at the first blank line after them.
|
|
32
35
|
*/
|
|
33
|
-
function
|
|
36
|
+
function tableImports(read, length) {
|
|
34
37
|
let window = Math.min(IMPORT_WINDOW, length);
|
|
35
38
|
for (;;) {
|
|
36
39
|
const head = read(window);
|
|
@@ -41,15 +44,26 @@ function tableStems(read, length) {
|
|
|
41
44
|
return [];
|
|
42
45
|
const complete = window >= length || head.indexOf('\n\n', first) >= 0;
|
|
43
46
|
if (complete)
|
|
44
|
-
return [...head.matchAll(PARAGRAPH_IMPORT)].map((match) =>
|
|
47
|
+
return [...head.matchAll(PARAGRAPH_IMPORT)].map((match) => ({
|
|
48
|
+
stem: match[2],
|
|
49
|
+
names: match[1]
|
|
50
|
+
.split(',')
|
|
51
|
+
.map((name) => name.trim())
|
|
52
|
+
.filter((name) => name !== ''),
|
|
53
|
+
}));
|
|
45
54
|
window = Math.min(window * 2, length);
|
|
46
55
|
}
|
|
47
56
|
}
|
|
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
|
+
}
|
|
48
61
|
/**
|
|
49
|
-
* The tables that a device module on disk imports
|
|
50
|
-
* build covers the whole generated set without holding any of it
|
|
62
|
+
* The tables that a device module on disk imports, with the names it takes from each. The file is
|
|
63
|
+
* read from its start only, so a build covers the whole generated set without holding any of it
|
|
64
|
+
* in memory.
|
|
51
65
|
*/
|
|
52
|
-
export function
|
|
66
|
+
export function tableImportsOfFile(file) {
|
|
53
67
|
const fd = openSync(file, 'r');
|
|
54
68
|
try {
|
|
55
69
|
const length = statSync(file).size;
|
|
@@ -58,7 +72,7 @@ export function tableStemsOfFile(file) {
|
|
|
58
72
|
const count = readSync(fd, buffer, 0, buffer.length, 0);
|
|
59
73
|
return buffer.subarray(0, count).toString('utf8');
|
|
60
74
|
};
|
|
61
|
-
return
|
|
75
|
+
return tableImports(read, length);
|
|
62
76
|
}
|
|
63
77
|
finally {
|
|
64
78
|
closeSync(fd);
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The permutation bits that a device fixes when it starts. A listed word leaves them out, so one
|
|
3
|
+
* list serves every device module, and a build matches a word with those bits cleared.
|
|
4
|
+
*/
|
|
5
|
+
export declare const DEVICE_BITS: number;
|
|
6
|
+
/** The shader options of the plugin. */
|
|
7
|
+
export interface ShaderOptions {
|
|
8
|
+
/**
|
|
9
|
+
* The file that lists the permutation words that the project's pages draw with, one
|
|
10
|
+
* hexadecimal word on each line, as `bunx @null3d/cli shaders record` writes it. The path is
|
|
11
|
+
* relative to Vite's root. A production build then copies only the shader builds of those words,
|
|
12
|
+
* for every value of the bits a device fixes, and fails when the engine's shader set lacks a
|
|
13
|
+
* listed word. Without it, a build copies the whole set. The dev server draws with the whole
|
|
14
|
+
* set, and warns in the terminal and the browser's console about each word that a page draws
|
|
15
|
+
* with and the list lacks.
|
|
16
|
+
*/
|
|
17
|
+
list?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* The words of a list file's text: one word on each line, as hexadecimal with `0x` or as a decimal
|
|
21
|
+
* number, with `#` starting a comment, the rule that the engine's shader build reads. Throws with
|
|
22
|
+
* the line of a word it cannot read, and for a list without a word.
|
|
23
|
+
*/
|
|
24
|
+
export declare function parseShaderList(text: string, file: string): number[];
|
|
25
|
+
/** A project's list of permutation words. */
|
|
26
|
+
export declare class ShaderList {
|
|
27
|
+
/** The list's file, as the build names it in its messages. */
|
|
28
|
+
readonly file: string;
|
|
29
|
+
/** The listed words, without the bits that a device fixes. */
|
|
30
|
+
readonly words: ReadonlySet<number>;
|
|
31
|
+
constructor(
|
|
32
|
+
/** The list's file, as the build names it in its messages. */
|
|
33
|
+
file: string, words: Iterable<number>);
|
|
34
|
+
/**
|
|
35
|
+
* Reads the list at `path`, which messages name as `name`. Throws for a list without the word
|
|
36
|
+
* that has no bit set, which every page's start draws its own passes with.
|
|
37
|
+
*/
|
|
38
|
+
static read(path: string, name?: string): ShaderList;
|
|
39
|
+
/**
|
|
40
|
+
* True when the list names the build whose permutation word is `permutation`: the word itself,
|
|
41
|
+
* or the word with the bits that a device fixes cleared.
|
|
42
|
+
*/
|
|
43
|
+
holds(permutation: number): boolean;
|
|
44
|
+
/** The listed words that none of `held`, the words that a set's builds answer, holds. */
|
|
45
|
+
missing(held: ReadonlySet<number>): number[];
|
|
46
|
+
}
|
|
47
|
+
/** The dev server's warning about a word that a page draws with and the project's list lacks. */
|
|
48
|
+
export interface UnlistedWord {
|
|
49
|
+
readonly message: string;
|
|
50
|
+
/** True for the first warning about the word since the list last changed. */
|
|
51
|
+
readonly first: boolean;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The dev server's check of the words that its pages draw with against the project's list. The
|
|
55
|
+
* dev server draws with the whole shader set, so a page there draws a word that the list lacks,
|
|
56
|
+
* while a production build with the list stops that page. The check names such a word when a page
|
|
57
|
+
* first draws with it. It reads the list again whenever the file changes, so a recording made
|
|
58
|
+
* while the server runs counts at once.
|
|
59
|
+
*/
|
|
60
|
+
export declare class UnlistedWords {
|
|
61
|
+
private readonly path;
|
|
62
|
+
/** The list's file, as the warnings name it. */
|
|
63
|
+
private readonly name;
|
|
64
|
+
/** The list file's change time when it was last read, or undefined before the first read. */
|
|
65
|
+
private stamp;
|
|
66
|
+
private list;
|
|
67
|
+
/** Why the list cannot be read, when it cannot. */
|
|
68
|
+
private problem;
|
|
69
|
+
/** The words that the check has warned about since the list last changed. */
|
|
70
|
+
private readonly warned;
|
|
71
|
+
/** True once the check has reported the list's problem since the list last changed. */
|
|
72
|
+
private problemShown;
|
|
73
|
+
constructor(path: string,
|
|
74
|
+
/** The list's file, as the warnings name it. */
|
|
75
|
+
name: string);
|
|
76
|
+
/**
|
|
77
|
+
* The warning about `word`, which the page at `page` draws with, without the bits that a device
|
|
78
|
+
* fixes. Undefined when the list holds the word.
|
|
79
|
+
*/
|
|
80
|
+
check(word: number, page: string): UnlistedWord | undefined;
|
|
81
|
+
/** Reads the list again when its file changed since the last read. */
|
|
82
|
+
private refresh;
|
|
83
|
+
}
|
|
84
|
+
/** Adds to `held` each word that a build with the permutation word `permutation` answers. */
|
|
85
|
+
export declare function holdWord(held: Set<number>, permutation: number): void;
|
|
86
|
+
/** A word as the list writes it: eight hexadecimal digits after `0x`. */
|
|
87
|
+
export declare function wordText(word: number): string;
|
|
88
|
+
/**
|
|
89
|
+
* A line of a generated shader module that holds a build's permutation word. The hash workers
|
|
90
|
+
* read the same lines in `hash-worker.js`, which cannot import this module.
|
|
91
|
+
*/
|
|
92
|
+
export declare const PERMUTATION_LINE: RegExp;
|
|
93
|
+
/** The permutation word of each build that a generated module's text holds. */
|
|
94
|
+
export declare function moduleWords(text: string): number[];
|
|
95
|
+
/**
|
|
96
|
+
* The permutation words that a built file's text names: those of the builds that it bundles, such
|
|
97
|
+
* as the main shader module's own, a custom material's and an add-on's. A key of that name in other
|
|
98
|
+
* code adds a word too, so the words that a check reads from here can only be more, never fewer.
|
|
99
|
+
*/
|
|
100
|
+
export declare function builtWords(text: string): number[];
|
|
101
|
+
/**
|
|
102
|
+
* The module that stands for a device file whose builds the list leaves out. It holds no build,
|
|
103
|
+
* so a page that asks for one of those words loads it, finds no build, and the engine names the
|
|
104
|
+
* word.
|
|
105
|
+
*/
|
|
106
|
+
export declare const UNLISTED_MODULE = "// The builds of a device module that the project's shader list leaves out.\nexport const SHADERS = {};\n";
|
|
107
|
+
/** The generated module that names the listed words, in place of the set's own. */
|
|
108
|
+
export declare function listedPermutationsModule(words: ReadonlySet<number>): string;
|
|
109
|
+
/**
|
|
110
|
+
* The text of the generated device module `text` with only the builds whose permutation word
|
|
111
|
+
* `keep` takes, and the counts of the builds it kept and held. A shader keeps its entry with no
|
|
112
|
+
* build. Each shared paragraph and imported name that no kept build reads goes too, and so does an
|
|
113
|
+
* import of a table of paragraphs that no kept build reads. Throws when the module is not in the
|
|
114
|
+
* form that the shader build writes, so a build never ships a module it misread.
|
|
115
|
+
*/
|
|
116
|
+
export declare function narrowShaderModule(text: string, keep: (permutation: number) => boolean, file?: string): {
|
|
117
|
+
text: string;
|
|
118
|
+
kept: number;
|
|
119
|
+
builds: number;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* The text of a table of shared paragraphs with only the paragraphs that `names` lists, the names
|
|
123
|
+
* that the build's copies import from it. A paragraph of a table reads no other value, so each
|
|
124
|
+
* one stays or goes on its own. Throws when the table is not in the form that the shader build
|
|
125
|
+
* writes.
|
|
126
|
+
*/
|
|
127
|
+
export declare function narrowParagraphTable(text: string, names: ReadonlySet<string>, file?: string): string;
|
|
@@ -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/lib/wgsl.js
CHANGED
|
@@ -107,6 +107,7 @@ const MATERIAL_FUNCTIONS = new Set([
|
|
|
107
107
|
'surface',
|
|
108
108
|
'vertexOffset',
|
|
109
109
|
'lobe',
|
|
110
|
+
'coverage',
|
|
110
111
|
'unlit',
|
|
111
112
|
]);
|
|
112
113
|
/**
|
|
@@ -195,7 +196,7 @@ function pipelinesOf(path, source, hint) {
|
|
|
195
196
|
problem: problemAt(path, source, at, message),
|
|
196
197
|
});
|
|
197
198
|
if (entries.length === 0) {
|
|
198
|
-
return problem(0, `the WGSL has no entry point and no function of a custom material, effect or sky layer. For a custom material, declare \`fn surface(input: SurfaceInput) -> Surface\`, \`fn vertexOffset(input: VertexInput) -> vec3f\`, \`fn lobe(input: LobeInput) -> vec3f\`, or some of them. For an effect, declare \`fn effect(input: EffectInput) -> vec4f\`, for a tone curve \`fn toneCurve(color: vec3f) -> vec3f\`, and for a sky layer \`fn skyLayer(input: SkyLayerInput) -> SkyLayer\`. For a shader of your own, give it a \`@vertex\` and a \`@fragment\` entry point, or a \`@compute\` one. ${hint}`);
|
|
199
|
+
return problem(0, `the WGSL has no entry point and no function of a custom material, effect or sky layer. For a custom material, declare \`fn surface(input: SurfaceInput) -> Surface\`, \`fn vertexOffset(input: VertexInput) -> vec3f\`, \`fn lobe(input: LobeInput) -> vec3f\`, \`fn coverage(input: SurfaceInput) -> f32\`, or some of them. For a surface that shows its own color with no light, declare \`fn unlit(input: SurfaceInput) -> vec4f\` in place of \`surface\`. For an effect, declare \`fn effect(input: EffectInput) -> vec4f\`, for a tone curve \`fn toneCurve(color: vec3f) -> vec3f\`, and for a sky layer \`fn skyLayer(input: SkyLayerInput) -> SkyLayer\`. For a shader of your own, give it a \`@vertex\` and a \`@fragment\` entry point, or a \`@compute\` one. ${hint}`);
|
|
199
200
|
}
|
|
200
201
|
if (secondVertex) {
|
|
201
202
|
return problem(secondVertex.at, 'the WGSL has more than one `@vertex` entry point. The plugin pairs each `@fragment` entry point with the one `@vertex` entry point of its shader, so keep one, or split the shader in two.');
|
package/lib/worker-preload.d.ts
CHANGED
|
@@ -13,5 +13,18 @@ export declare function staticImports(fileName: string, chunks: Readonly<Record<
|
|
|
13
13
|
export declare function preloadImports(code: MagicString, fileName: string, chunks: Readonly<Record<string, {
|
|
14
14
|
imports: readonly string[];
|
|
15
15
|
}>>): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Adds to the code of a chunk that a worker imports by its address an import of every chunk that it
|
|
18
|
+
* reaches through others. A worker asks for a file's imports only once that file has arrived, and
|
|
19
|
+
* has no preload to ask sooner, so a chain of imports costs one round trip per step. The sketch
|
|
20
|
+
* worker imports a sketch this way, and a job worker a task. With every file named in the chunk
|
|
21
|
+
* itself, the worker asks for them all once the chunk arrives. The imports go after the chunk's
|
|
22
|
+
* code. A module runs its imports in the order of their places in its code, and each added chunk
|
|
23
|
+
* already runs earlier through the import that reaches it, so no module runs at another time than
|
|
24
|
+
* before. Returns true when the code changed.
|
|
25
|
+
*/
|
|
26
|
+
export declare function directImports(code: MagicString, fileName: string, chunks: Readonly<Record<string, {
|
|
27
|
+
imports: readonly string[];
|
|
28
|
+
}>>): boolean;
|
|
16
29
|
/** The plugin for each worker build. */
|
|
17
30
|
export declare function workerPreloadPlugin(): Plugin;
|
package/lib/worker-preload.js
CHANGED
|
@@ -48,6 +48,25 @@ export function preloadImports(code, fileName, chunks) {
|
|
|
48
48
|
}
|
|
49
49
|
return changed;
|
|
50
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Adds to the code of a chunk that a worker imports by its address an import of every chunk that it
|
|
53
|
+
* reaches through others. A worker asks for a file's imports only once that file has arrived, and
|
|
54
|
+
* has no preload to ask sooner, so a chain of imports costs one round trip per step. The sketch
|
|
55
|
+
* worker imports a sketch this way, and a job worker a task. With every file named in the chunk
|
|
56
|
+
* itself, the worker asks for them all once the chunk arrives. The imports go after the chunk's
|
|
57
|
+
* code. A module runs its imports in the order of their places in its code, and each added chunk
|
|
58
|
+
* already runs earlier through the import that reaches it, so no module runs at another time than
|
|
59
|
+
* before. Returns true when the code changed.
|
|
60
|
+
*/
|
|
61
|
+
export function directImports(code, fileName, chunks) {
|
|
62
|
+
const direct = new Set(chunks[fileName]?.imports ?? []);
|
|
63
|
+
const reached = [...staticImports(fileName, chunks)].filter((name) => name !== fileName && !direct.has(name) && chunks[name] !== undefined);
|
|
64
|
+
if (reached.length === 0)
|
|
65
|
+
return false;
|
|
66
|
+
const lines = reached.map((name) => `import ${JSON.stringify(importAddress(fileName, name))};`);
|
|
67
|
+
code.append(`\n${lines.join('\n')}\n`);
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
51
70
|
/** The plugin for each worker build. */
|
|
52
71
|
export function workerPreloadPlugin() {
|
|
53
72
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@null3d/vite-plugin",
|
|
3
|
-
"version": "0.
|
|
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": {
|