@null3d/vite-plugin 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -40,9 +40,15 @@ export interface BuildSource {
40
40
  }
41
41
  /** The shaders of one module whose builds go to sets, in the order that the module holds them. */
42
42
  export declare class ModuleBuilds {
43
+ private readonly glsl;
43
44
  readonly sources: BuildSource[];
44
45
  /** How many shaders of each kind the module gave, which numbers their records in a set. */
45
46
  private readonly counts;
47
+ /**
48
+ * `glsl` false keeps the WebGPU path alone: each shader loses its WebGL2 builds and pieces
49
+ * before it is packed, so their text reaches neither the module nor a set of builds.
50
+ */
51
+ constructor(glsl?: boolean);
46
52
  /**
47
53
  * The JavaScript expression of `shader` in a module that `pack` packs, as `ShaderPack.value`
48
54
  * gives it. The value of a shader whose builds load on first use keeps none: it gets a
@@ -60,12 +66,20 @@ export interface BuildSet {
60
66
  /** The builds of each shader of the module that the set serves, by name. */
61
67
  readonly records: readonly Builds[];
62
68
  }
69
+ /**
70
+ * The compiled WGSL of `shader` with every build and piece of the WebGL2 path removed, for a build
71
+ * that ships WebGPU alone: a whole shader keeps no WebGL2 variant, a material keeps no GLSL
72
+ * variants, and an effect or a tone curve keeps no GLSL pieces. The text of the other path then
73
+ * never reaches the module or a set that the build writes.
74
+ */
75
+ export declare function withoutGlsl(shader: CompiledWgsl): CompiledWgsl;
63
76
  /**
64
77
  * The sets that hold `sources`' builds: for a material one for each GPU path and value of its fixed
65
78
  * bits, and for an effect or a tone curve one for each GPU path. Each set holds the builds of every
66
- * shader of its kind that the set serves.
79
+ * shader of its kind that the set serves. With `glsl` false, the sets of the WebGL2 path are left
80
+ * out, for a build that ships WebGPU alone.
67
81
  */
68
- export declare function buildSets(sources: readonly BuildSource[]): BuildSet[];
82
+ export declare function buildSets(sources: readonly BuildSource[], glsl?: boolean): BuildSet[];
69
83
  /** The code of the module that holds one set of builds. */
70
84
  export declare function setModule(records: readonly Builds[]): string;
71
85
  /**
@@ -76,9 +90,12 @@ export declare function setModule(records: readonly Builds[]): string;
76
90
  export declare function loadBuilds(sets: readonly BuildSet[], address: (name: string) => string): string;
77
91
  /** The sets of builds that the plugin wrote, by the name of each set's virtual module. */
78
92
  export declare class MaterialBuilds {
93
+ private readonly glsl;
79
94
  private readonly sets;
80
95
  /** The names of the sets of each module, so a module's new compile drops its old sets. */
81
96
  private readonly owners;
97
+ /** `glsl` says whether the WebGL2 sets are made; a build that ships WebGPU alone says false. */
98
+ constructor(glsl?: () => boolean);
82
99
  /**
83
100
  * The code that ends module `file`, whose shaders `builds` holds: the function that imports
84
101
  * each set, or nothing for a module without such shaders. The sets of the module's previous
@@ -32,6 +32,8 @@ export const TONE_MAP = 2;
32
32
  export const SKIN = 64;
33
33
  /** The permutation bit of builds that do their color math at half precision. */
34
34
  export const HALF = 8192;
35
+ /** The GPU paths, in the order that a build takes them. */
36
+ const TARGETS = ['wgsl', 'glsl'];
35
37
  /** The permutation bits that a device of each GPU path fixes, which split its builds into sets. */
36
38
  const FIXED = { wgsl: TONE_MAP, glsl: TONE_MAP | DRAW_INDEX };
37
39
  /** Each value of the bits of `mask`, from 0 up. */
@@ -99,28 +101,37 @@ function isBuildKind(kind) {
99
101
  }
100
102
  /** The shaders of one module whose builds go to sets, in the order that the module holds them. */
101
103
  export class ModuleBuilds {
104
+ glsl;
102
105
  sources = [];
103
106
  /** How many shaders of each kind the module gave, which numbers their records in a set. */
104
107
  counts = new Map();
108
+ /**
109
+ * `glsl` false keeps the WebGPU path alone: each shader loses its WebGL2 builds and pieces
110
+ * before it is packed, so their text reaches neither the module nor a set of builds.
111
+ */
112
+ constructor(glsl = true) {
113
+ this.glsl = glsl;
114
+ }
105
115
  /**
106
116
  * The JavaScript expression of `shader` in a module that `pack` packs, as `ShaderPack.value`
107
117
  * gives it. The value of a shader whose builds load on first use keeps none: it gets a
108
118
  * function that imports a set.
109
119
  */
110
120
  value(pack, shader, hot) {
111
- const kind = shader.kind;
121
+ const kept = this.glsl ? shader : withoutGlsl(shader);
122
+ const kind = kept.kind;
112
123
  const variants = isBuildKind(kind)
113
- ? shader.variants
124
+ ? kept.variants
114
125
  : undefined;
115
126
  if (variants === undefined || Object.keys(variants).length === 0)
116
- return pack.value(shader, hot);
127
+ return pack.value(kept, hot);
117
128
  const index = this.counts.get(kind) ?? 0;
118
129
  this.counts.set(kind, index + 1);
119
130
  this.sources.push({ kind: kind, variants });
120
131
  // An effect's pieces reach the thread that draws. An effect that reads the pixels beside its
121
132
  // own joins a host as the first member of a group or of the fold. The shader build gives no
122
133
  // pieces to an effect that declares textures or reads bloom's chain, which draws its own pass.
123
- return pack.value({ ...shader, variants: {}, permutations: permutationsOf(variants) }, hot, index, kind);
134
+ return pack.value({ ...kept, variants: {}, permutations: permutationsOf(variants) }, hot, index, kind);
124
135
  }
125
136
  }
126
137
  /** The builds of `variants` that the GPU path `target` draws, and no output for the other path. */
@@ -137,6 +148,42 @@ function onlyTarget(variants, target) {
137
148
  }
138
149
  return kept;
139
150
  }
151
+ /** The builds of `variants` for the WebGPU path alone: those with no WebGPU output go. */
152
+ function webgpuOnly(variants) {
153
+ return onlyTarget(variants, 'wgsl');
154
+ }
155
+ /** The pieces of `pieces` without their WebGL2 additions. */
156
+ function withoutGlslPieces(pieces) {
157
+ const strip = (group) => Object.fromEntries(Object.entries(group).map(([name, piece]) => [name, { ...piece, glsl: null }]));
158
+ return { group: strip(pieces.group), fold: strip(pieces.fold) };
159
+ }
160
+ /**
161
+ * The compiled WGSL of `shader` with every build and piece of the WebGL2 path removed, for a build
162
+ * that ships WebGPU alone: a whole shader keeps no WebGL2 variant, a material keeps no GLSL
163
+ * variants, and an effect or a tone curve keeps no GLSL pieces. The text of the other path then
164
+ * never reaches the module or a set that the build writes.
165
+ */
166
+ export function withoutGlsl(shader) {
167
+ switch (shader.kind) {
168
+ case 'shader':
169
+ return { ...shader, webgl2: null };
170
+ case 'material':
171
+ return { ...shader, variants: webgpuOnly(shader.variants) };
172
+ case 'effect':
173
+ case 'toneCurve':
174
+ return {
175
+ ...shader,
176
+ variants: webgpuOnly(shader.variants),
177
+ pieces: withoutGlslPieces(shader.pieces),
178
+ };
179
+ case 'skyLayer':
180
+ return {
181
+ ...shader,
182
+ variants: webgpuOnly(shader.variants),
183
+ map: webgpuOnly(shader.map),
184
+ };
185
+ }
186
+ }
140
187
  /** The builds of the shaders of `kind` in `sources`, in the module's order. */
141
188
  function variantsOf(sources, kind) {
142
189
  return sources.filter((source) => source.kind === kind).map((source) => source.variants);
@@ -150,11 +197,12 @@ function setOf(bits, key, records) {
150
197
  /**
151
198
  * The sets that hold `sources`' builds: for a material one for each GPU path and value of its fixed
152
199
  * bits, and for an effect or a tone curve one for each GPU path. Each set holds the builds of every
153
- * shader of its kind that the set serves.
200
+ * shader of its kind that the set serves. With `glsl` false, the sets of the WebGL2 path are left
201
+ * out, for a build that ships WebGPU alone.
154
202
  */
155
- export function buildSets(sources) {
203
+ export function buildSets(sources, glsl = true) {
156
204
  const sets = [];
157
- for (const target of ['wgsl', 'glsl']) {
205
+ for (const target of glsl ? TARGETS : ['wgsl']) {
158
206
  const materials = variantsOf(sources, 'material');
159
207
  if (materials.length > 0)
160
208
  for (const set of setsOf(target)) {
@@ -200,16 +248,21 @@ function ${LOAD_BUILDS}(kind, target, set) {
200
248
  }
201
249
  /** The sets of builds that the plugin wrote, by the name of each set's virtual module. */
202
250
  export class MaterialBuilds {
251
+ glsl;
203
252
  sets = new Map();
204
253
  /** The names of the sets of each module, so a module's new compile drops its old sets. */
205
254
  owners = new Map();
255
+ /** `glsl` says whether the WebGL2 sets are made; a build that ships WebGPU alone says false. */
256
+ constructor(glsl = () => true) {
257
+ this.glsl = glsl;
258
+ }
206
259
  /**
207
260
  * The code that ends module `file`, whose shaders `builds` holds: the function that imports
208
261
  * each set, or nothing for a module without such shaders. The sets of the module's previous
209
262
  * compile go, unless another module has the same.
210
263
  */
211
264
  finish(file, builds) {
212
- const sets = buildSets(builds.sources);
265
+ const sets = buildSets(builds.sources, this.glsl());
213
266
  const names = new Set(sets.map((set) => set.name));
214
267
  for (const set of sets)
215
268
  this.sets.set(set.name, [...set.records]);
package/lib/pack.d.ts CHANGED
@@ -1,5 +1,15 @@
1
- /** The function that the plugin adds to a module, which rebuilds the module's compiled shaders. */
1
+ /**
2
+ * The function that the plugin adds to a module, which rebuilds the module's compiled shaders.
3
+ */
2
4
  export declare const UNPACK = "__null3dShader";
5
+ /**
6
+ * The text of a packed shader module with its WebGL2 builds removed, for a build that ships WebGPU
7
+ * alone: each record keeps no WebGL2 output, the module keeps no table of GLSL text, and the loader
8
+ * of build sets loses the entries of the GLSL sets, so their modules are never imported or emitted.
9
+ * Throws when the text is not in the form that `ShaderPack` writes, so a build never ships a module
10
+ * that it misread.
11
+ */
12
+ export declare function narrowShaderPack(text: string, file: string): string;
3
13
  /**
4
14
  * The function of a module whose custom materials' builds load on first use, which imports the
5
15
  * builds of a GPU path and a set by its permutation bits, as `material-builds.ts` writes it.
package/lib/pack.js CHANGED
@@ -13,8 +13,77 @@
13
13
  // reads faster than an object literal of the same size, and which the transforms after the plugin
14
14
  // leave on one line. The function makes no code from strings, so a strict Content Security Policy
15
15
  // allows it.
16
- /** The function that the plugin adds to a module, which rebuilds the module's compiled shaders. */
16
+ /**
17
+ * The function that the plugin adds to a module, which rebuilds the module's compiled shaders.
18
+ */
17
19
  export const UNPACK = '__null3dShader';
20
+ /**
21
+ * The two tables of an unpacked module in its text: the WGSL table and the GLSL table, each the
22
+ * JavaScript string literal that `JSON.stringify(JSON.stringify(...))` writes.
23
+ */
24
+ const PATHS_TABLES = /__null3dShader\.paths = \[\s*("(?:[^"\\]|\\.)*")\s*,\s*("(?:[^"\\]|\\.)*")\s*\]/;
25
+ /** The call that a module makes to unpack each compiled shader, and its first argument. */
26
+ const PACK_CALL = /__null3dShader\(\s*("(?:[^"\\]|\\.)*")/g;
27
+ /** The entries of a build-set loader that hold the sets of one GPU path, one on each line. */
28
+ const SET_ENTRY = /[ \t]*"(?:material|effect|toneCurve) glsl \d+": \(\) => import\("[^"]*"\),\r?\n/g;
29
+ /** The JSON text of a table that holds no text, in the form that the module's tables take. */
30
+ const EMPTY_TABLE = JSON.stringify(JSON.stringify({ blocks: [], lists: [] }));
31
+ /** Why a packed shader module cannot be narrowed: its text is not the form that the pack writes. */
32
+ function unreadablePack(file) {
33
+ return new Error(`null3D cannot drop the WebGL2 builds of ${file}: the module is not in the form that the null3D plugin packs a compiled shader in. Install the same version of @null3d/vite-plugin as the package that wrote it.`);
34
+ }
35
+ /**
36
+ * Sets every `glsl` and `webgl2` field of a packed record to null, at any depth, so that no getter
37
+ * ever asks for a source of a table that no longer holds any. A material's variants, an effect's
38
+ * pieces and a whole shader's records all name their WebGL2 output that way.
39
+ */
40
+ function dropWebgl2(value) {
41
+ if (Array.isArray(value)) {
42
+ for (const item of value)
43
+ dropWebgl2(item);
44
+ return;
45
+ }
46
+ if (value === null || typeof value !== 'object')
47
+ return;
48
+ const record = value;
49
+ for (const key of Object.keys(record)) {
50
+ if (key === 'glsl' || key === 'webgl2')
51
+ record[key] = null;
52
+ else
53
+ dropWebgl2(record[key]);
54
+ }
55
+ }
56
+ /**
57
+ * The text of a packed shader module with its WebGL2 builds removed, for a build that ships WebGPU
58
+ * alone: each record keeps no WebGL2 output, the module keeps no table of GLSL text, and the loader
59
+ * of build sets loses the entries of the GLSL sets, so their modules are never imported or emitted.
60
+ * Throws when the text is not in the form that `ShaderPack` writes, so a build never ships a module
61
+ * that it misread.
62
+ */
63
+ export function narrowShaderPack(text, file) {
64
+ const tables = PATHS_TABLES.exec(text);
65
+ if (tables === null)
66
+ throw unreadablePack(file);
67
+ let narrowed = text.replace(PATHS_TABLES, (_match, wgsl, _glsl) => `__null3dShader.paths = [${wgsl}, ${EMPTY_TABLE}]`);
68
+ let calls = 0;
69
+ narrowed = narrowed.replace(PACK_CALL, (match, literal) => {
70
+ let packed;
71
+ let records;
72
+ try {
73
+ packed = JSON.parse(literal);
74
+ records = JSON.parse(packed);
75
+ }
76
+ catch {
77
+ throw unreadablePack(file);
78
+ }
79
+ dropWebgl2(records);
80
+ calls++;
81
+ return match.replace(literal, () => JSON.stringify(JSON.stringify(records)));
82
+ });
83
+ if (calls === 0)
84
+ throw unreadablePack(file);
85
+ return narrowed.replace(SET_ENTRY, '');
86
+ }
18
87
  /**
19
88
  * The function of a module whose custom materials' builds load on first use, which imports the
20
89
  * builds of a GPU path and a set by its permutation bits, as `material-builds.ts` writes it.
@@ -1,4 +1,4 @@
1
- import type { Plugin, Rollup } from 'vite';
1
+ import type { Rollup } from 'vite';
2
2
  /** One import of a table of shared paragraphs: the table's file name without `.js`, and its names. */
3
3
  export interface TableImport {
4
4
  readonly stem: string;
@@ -10,16 +10,87 @@ export interface TableImport {
10
10
  * in memory.
11
11
  */
12
12
  export declare function tableImportsOfFile(file: string): TableImport[];
13
+ /** The hash that a file's final bytes give it: the first eight hexadecimal digits of SHA-256. */
14
+ export declare function contentHash(source: string | Uint8Array): string;
13
15
  /**
14
- * Emits each shared table of shader paragraphs beside the copied device modules that import it,
15
- * and gives the files of the tables of each of those modules. A build's bundle is the same after a
16
- * second call, because a table is emitted once for each folder it belongs in.
16
+ * A module that imports tables of shared paragraphs, as the collector reads it. The names live in
17
+ * the shared union while the module is read, so a build never keeps a list of names for each of the
18
+ * thousands of copied modules.
17
19
  */
18
- export declare function emitParagraphTables(this: Rollup.PluginContext, bundle: Rollup.OutputBundle, root?: string): Map<string, string[]>;
20
+ export interface TableModuleFile {
21
+ /** The module's file on disk, whose head names its tables. */
22
+ readonly file: string;
23
+ /** Its source file, beside which its tables sit. */
24
+ readonly source: string;
25
+ /** The folder of the build's output that holds the module, where the table must sit. */
26
+ readonly folder: string;
27
+ /** Its name in a build error. */
28
+ readonly module: string;
29
+ }
30
+ /** A table that lacks a name that its importers take. The caller names the modules. */
31
+ export declare class TableExportError extends Error {
32
+ readonly path: string;
33
+ readonly missing: readonly string[];
34
+ constructor(path: string, missing: readonly string[]);
35
+ }
36
+ /** The names that the importing modules take from each table, by the table's source file. */
37
+ export type TableNames = ReadonlyMap<string, ReadonlySet<string>>;
38
+ /** A table of shared paragraphs, as the build emits it. */
39
+ export interface PlannedTable {
40
+ /** Its content-addressed file name, such as `shader-paragraphs-wgsl-1a2b3c4d.js`. */
41
+ readonly name: string;
42
+ /** Its final bytes, which its name hashes. */
43
+ readonly source: Buffer;
44
+ }
45
+ /** Each table's final content and name, by the source file that the table comes from. */
46
+ export type TablePlan = ReadonlyMap<string, PlannedTable>;
47
+ /**
48
+ * Works out each table's final content and name before any module that imports it takes a name. A
49
+ * build with a list of permutation words keeps only the paragraphs that the importing modules
50
+ * name, so a table's content follows the union of those names. A build without a list keeps the
51
+ * table whole. Throws a `TableExportError` when a table lacks a name that its importers take.
52
+ */
53
+ export declare function planParagraphTables(tables: TableNames, narrow: boolean): TablePlan;
54
+ /**
55
+ * The text of a device module with each table import pointing at the table's content-addressed
56
+ * file. `names` maps each table's stem, such as `shader-paragraphs-wgsl`, to its final file name.
57
+ */
58
+ export declare function rewriteTableImports(text: string, names: ReadonlyMap<string, string>): string;
59
+ /**
60
+ * Writes the module at `source` to `target` with its table imports pointing at their hashed files,
61
+ * and gives the hash of the bytes written. It reads only the import head, writes that rewritten,
62
+ * and copies the rest of the source in chunks, so a large module never sits in memory whole.
63
+ * Undefined for a file that names no import, which the caller copies as it is.
64
+ */
65
+ export declare function rewriteCopyFile(source: string, target: string, names: ReadonlyMap<string, string>): string | undefined;
66
+ /** What a build must give `emitParagraphTables` beyond the bundle it reads. */
67
+ export interface ParagraphTableOptions {
68
+ /** The project's root, which a relative source name resolves against. */
69
+ readonly root?: string;
70
+ /** True for a build with a list of permutation words, which keeps only the named paragraphs. */
71
+ readonly narrow?: boolean;
72
+ /** What each worker build made, which names the source of a worker's copied file. */
73
+ readonly workerFiles?: ReadonlyMap<string, {
74
+ readonly moduleIds: readonly string[];
75
+ }>;
76
+ /** The modules that the bundle lacks, such as the shader files that the build copies itself. */
77
+ readonly copies?: readonly TableModuleFile[];
78
+ }
79
+ /** What `emitParagraphTables` gives the caller. */
80
+ export interface ParagraphTables {
81
+ /** The tables that each bundle file names, by the file's final output name. */
82
+ readonly files: Map<string, string[]>;
83
+ /** Each table's final content and name, by the source file that it comes from. */
84
+ readonly plan: TablePlan;
85
+ /** The tables that the build emits, by each table's place in the output. */
86
+ readonly tables: Map<string, PlannedTable>;
87
+ /** The bundle files that this call renamed, by the name each had before. */
88
+ readonly renamed: Map<string, string>;
89
+ }
19
90
  /**
20
- * A plugin for each worker build, which emits the shared tables of shader paragraphs beside the
21
- * device modules that import them. A worker build copies the engine's device modules as the
22
- * client build does, so each build emits its own file of the table. The client build does the same
23
- * through `emitParagraphTables`, and no build emits a table twice.
91
+ * Emits each shared table of shader paragraphs beside the modules that import it, gives the files
92
+ * of the tables that each bundle file names, and names each bundle asset that imports one by the
93
+ * hash of its final bytes. A build's bundle is the same after a second call, because a table is
94
+ * emitted once for each folder it belongs in.
24
95
  */
25
- export declare function shaderParagraphsPlugin(): Plugin;
96
+ export declare function emitParagraphTables(this: Rollup.PluginContext, bundle: Rollup.OutputBundle, options?: ParagraphTableOptions): ParagraphTables;