@null3d/vite-plugin 0.1.1 → 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 +6 -0
- package/dist/shader-compiler.stamp.json +1 -1
- package/dist/shader-compiler.wasm +0 -0
- package/lib/hot.d.ts +13 -2
- package/lib/hot.js +28 -3
- package/lib/index.d.ts +1 -1
- package/lib/index.js +194 -27
- package/lib/material-builds.d.ts +19 -2
- package/lib/material-builds.js +61 -8
- package/lib/pack.d.ts +11 -1
- package/lib/pack.js +70 -1
- package/lib/shader-list.d.ts +41 -2
- package/lib/shader-list.js +55 -9
- package/lib/wgsl.d.ts +10 -9
- package/lib/wgsl.js +31 -28
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -70,6 +70,12 @@ null3d({ shaders: { list: 'shader-permutations.txt' } });
|
|
|
70
70
|
copies only the builds of the listed words, and fails when the engine's shader set lacks a listed
|
|
71
71
|
word. Without it, a build copies the whole set, thousands of files and gigabytes, and warns with
|
|
72
72
|
the size that it copied.
|
|
73
|
+
- `shaders.gpuPaths` names the GPU paths whose shaders a build ships, such as `['webgpu']` for an
|
|
74
|
+
app whose browser always offers WebGPU. A build then copies no WebGL2 shader files, and compiles
|
|
75
|
+
your own shaders for WebGPU alone. A page on a device that would need WebGL2 fails with E1308,
|
|
76
|
+
and the value must include `webgpu` (the default is both paths). The dev server keeps both paths
|
|
77
|
+
and warns when a page draws the one that a build drops, and the option combines with
|
|
78
|
+
`shaders.list`.
|
|
73
79
|
- `urlSwitches` lets the page's address set the engine's test switches, such as `?gpu=`, in a
|
|
74
80
|
production build too. Leave it out for a shipped game. Development builds then read the
|
|
75
81
|
switches and production builds do not, so a link cannot change how the game runs. An explicit
|
|
Binary file
|
package/lib/hot.d.ts
CHANGED
|
@@ -12,14 +12,19 @@ export declare const HOT_CLIENT_ADDRESS: RegExp;
|
|
|
12
12
|
export declare const PERMUTATION_WORD_EVENT = "null3d:permutation";
|
|
13
13
|
/** The event that carries the dev server's warning about a word that the project's list lacks. */
|
|
14
14
|
export declare const UNLISTED_WORD_EVENT = "null3d:unlisted";
|
|
15
|
+
/** The event that carries the GPU path that a page's engine started on, from the page to the server. */
|
|
16
|
+
export declare const GPU_PATH_EVENT = "null3d:gpu-path";
|
|
17
|
+
/** The event that carries the dev server's warning about a GPU path that a build does not ship. */
|
|
18
|
+
export declare const UNSHIPPED_PATH_EVENT = "null3d:unshipped-path";
|
|
15
19
|
/**
|
|
16
20
|
* The hot client's code. It also closes Vite's overlay, which may show an earlier WGSL error. In a
|
|
17
21
|
* project with a shader list (`listed`), it also sends the dev server each permutation word that
|
|
18
22
|
* the page's engines draw with, once, and shows the server's warning about a word that the list
|
|
19
23
|
* lacks in the browser's console. A recording run adds its page's words to the list, so it sends
|
|
20
|
-
* none.
|
|
24
|
+
* none. With `paths`, it sends the GPU path that each engine started on, and shows the server's
|
|
25
|
+
* warning about a path that a production build does not ship.
|
|
21
26
|
*/
|
|
22
|
-
export declare function hotClientCode(listed: boolean): string;
|
|
27
|
+
export declare function hotClientCode(listed: boolean, paths?: boolean): string;
|
|
23
28
|
/** One hot update: the new compiled WGSL under its key. */
|
|
24
29
|
export interface WgslUpdate {
|
|
25
30
|
readonly key: string;
|
|
@@ -61,3 +66,9 @@ export declare class HotState {
|
|
|
61
66
|
*/
|
|
62
67
|
swaps(key: string, shader: CompiledWgsl): boolean;
|
|
63
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* The dev server's warning when a page draws a GPU path that a production build does not ship, or
|
|
71
|
+
* undefined when the page draws a path that the build ships. Only the WebGL2 path can be left out:
|
|
72
|
+
* the shader compiler builds every shader for WebGPU.
|
|
73
|
+
*/
|
|
74
|
+
export declare function unshippedPathWarning(path: string, page: string, glsl: boolean): string | undefined;
|
package/lib/hot.js
CHANGED
|
@@ -11,14 +11,19 @@ export const HOT_CLIENT_ADDRESS = /^\0?virtual:null3d\/hot$/;
|
|
|
11
11
|
export const PERMUTATION_WORD_EVENT = 'null3d:permutation';
|
|
12
12
|
/** The event that carries the dev server's warning about a word that the project's list lacks. */
|
|
13
13
|
export const UNLISTED_WORD_EVENT = 'null3d:unlisted';
|
|
14
|
+
/** The event that carries the GPU path that a page's engine started on, from the page to the server. */
|
|
15
|
+
export const GPU_PATH_EVENT = 'null3d:gpu-path';
|
|
16
|
+
/** The event that carries the dev server's warning about a GPU path that a build does not ship. */
|
|
17
|
+
export const UNSHIPPED_PATH_EVENT = 'null3d:unshipped-path';
|
|
14
18
|
/**
|
|
15
19
|
* The hot client's code. It also closes Vite's overlay, which may show an earlier WGSL error. In a
|
|
16
20
|
* project with a shader list (`listed`), it also sends the dev server each permutation word that
|
|
17
21
|
* the page's engines draw with, once, and shows the server's warning about a word that the list
|
|
18
22
|
* lacks in the browser's console. A recording run adds its page's words to the list, so it sends
|
|
19
|
-
* none.
|
|
23
|
+
* none. With `paths`, it sends the GPU path that each engine started on, and shows the server's
|
|
24
|
+
* warning about a path that a production build does not ship.
|
|
20
25
|
*/
|
|
21
|
-
export function hotClientCode(listed) {
|
|
26
|
+
export function hotClientCode(listed, paths = false) {
|
|
22
27
|
const wgsl = ` import.meta.hot.on(${JSON.stringify(WGSL_UPDATE_EVENT)}, ({ updates }) => {
|
|
23
28
|
for (const overlay of document.querySelectorAll('vite-error-overlay')) overlay.close();
|
|
24
29
|
globalThis.dispatchEvent(new CustomEvent(${JSON.stringify(WGSL_UPDATE_EVENT)}, { detail: updates }));
|
|
@@ -35,7 +40,17 @@ export function hotClientCode(listed) {
|
|
|
35
40
|
import.meta.hot.on(${JSON.stringify(UNLISTED_WORD_EVENT)}, ({ message }) => console.warn('[null3D] ' + message));
|
|
36
41
|
}
|
|
37
42
|
`;
|
|
38
|
-
|
|
43
|
+
const gpuPaths = ` {
|
|
44
|
+
const sent = new Set();
|
|
45
|
+
globalThis.addEventListener(${JSON.stringify(GPU_PATH_EVENT)}, ({ detail: path }) => {
|
|
46
|
+
if (sent.has(path)) return;
|
|
47
|
+
sent.add(path);
|
|
48
|
+
import.meta.hot.send(${JSON.stringify(GPU_PATH_EVENT)}, { path, page: location.pathname });
|
|
49
|
+
});
|
|
50
|
+
import.meta.hot.on(${JSON.stringify(UNSHIPPED_PATH_EVENT)}, ({ message }) => console.warn('[null3D] ' + message));
|
|
51
|
+
}
|
|
52
|
+
`;
|
|
53
|
+
return `if (import.meta.hot) {\n${wgsl}${listed ? words : ''}${paths ? gpuPaths : ''}}\n`;
|
|
39
54
|
}
|
|
40
55
|
/**
|
|
41
56
|
* The key of a project's WGSL: a `.wgsl` file's path from the project's root, or that of a script
|
|
@@ -120,3 +135,13 @@ export class HotState {
|
|
|
120
135
|
return before !== undefined && before !== null && before === after;
|
|
121
136
|
}
|
|
122
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* The dev server's warning when a page draws a GPU path that a production build does not ship, or
|
|
140
|
+
* undefined when the page draws a path that the build ships. Only the WebGL2 path can be left out:
|
|
141
|
+
* the shader compiler builds every shader for WebGPU.
|
|
142
|
+
*/
|
|
143
|
+
export function unshippedPathWarning(path, page, glsl) {
|
|
144
|
+
if (glsl || path !== 'webgl2')
|
|
145
|
+
return undefined;
|
|
146
|
+
return `${page} draws with WebGL2, which a production build with shaders.gpuPaths: ['webgpu'] does not ship. That build stops the page with E1308. Run the page's tests on WebGPU, or build with both GPU paths.`;
|
|
147
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { type AssetOptions } from './assets.js';
|
|
|
3
3
|
import { type ShaderOptions } from './shader-list.js';
|
|
4
4
|
export type { AssetOptions } from './assets.js';
|
|
5
5
|
export { FILES_LIST, type OfflineFiles } from './offline.js';
|
|
6
|
-
export type { ShaderOptions } from './shader-list.js';
|
|
6
|
+
export type { GpuPath, ShaderOptions } from './shader-list.js';
|
|
7
7
|
export type * from './shader-types.js';
|
|
8
8
|
/** Headers that make a page cross-origin isolated, which shared memory and worker threads need. */
|
|
9
9
|
export declare const ISOLATION_HEADERS: Readonly<Record<string, string>>;
|
package/lib/index.js
CHANGED
|
@@ -11,14 +11,14 @@ import { writeWgslDeclaration } from "./declarations.js";
|
|
|
11
11
|
import { ENGINE_SHADERS_ENV, engineShaderFolder, engineShadersRoot, ensureEngineShaders, } from "./engine-shaders.js";
|
|
12
12
|
import { buildEngineShadersLocally } from "./engine-shaders-local.js";
|
|
13
13
|
import { HashPool } from "./hash-pool.js";
|
|
14
|
-
import { changedLiterals, HOT_CLIENT, HOT_CLIENT_ADDRESS, HOT_CLIENT_ID, HotState, hotClientCode, hotKey, PERMUTATION_WORD_EVENT, UNLISTED_WORD_EVENT, WGSL_UPDATE_EVENT, } from "./hot.js";
|
|
14
|
+
import { changedLiterals, GPU_PATH_EVENT, HOT_CLIENT, HOT_CLIENT_ADDRESS, HOT_CLIENT_ID, HotState, hotClientCode, hotKey, PERMUTATION_WORD_EVENT, UNLISTED_WORD_EVENT, UNSHIPPED_PATH_EVENT, unshippedPathWarning, WGSL_UPDATE_EVENT, } from "./hot.js";
|
|
15
15
|
import { MATERIAL_BUILDS_ADDRESS, MaterialBuilds, ModuleBuilds } from "./material-builds.js";
|
|
16
16
|
import { engineModule, FILES_LIST, featuresOf, listedFiles, missingWorkerAssets, offlineFiles, workerFilesPlugin, } from "./offline.js";
|
|
17
|
-
import { ShaderPack } from "./pack.js";
|
|
17
|
+
import { narrowShaderPack, ShaderPack, UNPACK } from "./pack.js";
|
|
18
18
|
import { null3dPackages } from "./package-files.js";
|
|
19
19
|
import { emitParagraphTables, shaderParagraphsPlugin, tableImportsOfFile } from "./paragraphs.js";
|
|
20
20
|
import { ShaderCache, shaderCacheOff } from "./shader-cache.js";
|
|
21
|
-
import { builtWords, holdWord,
|
|
21
|
+
import { builtWords, holdWord, moduleWords, narrowParagraphTable, narrowShaderModule, ShaderList, shaderMarkersModule, shippedTargets, targetOfModule, UNLISTED_MODULE, UnlistedWords, wordText, } from "./shader-list.js";
|
|
22
22
|
import { compileLiteral, compileTaggedWgsl, compileWgslFile, WGSL_TAG, } from "./wgsl.js";
|
|
23
23
|
import { directImports, workerPreloadPlugin } from "./worker-preload.js";
|
|
24
24
|
export { FILES_LIST } from "./offline.js";
|
|
@@ -268,6 +268,37 @@ const GENERATED_MODULE = /^shaders(?:-[a-z0-9-]+)?\.[cm]?[jt]s$/;
|
|
|
268
268
|
const LISTED_MODULE = /^shader-permutations\.[cm]?[jt]s$/;
|
|
269
269
|
/** The start of the name of the module that stands for every device file a list leaves out. */
|
|
270
270
|
const UNLISTED_STEM = 'shaders-unlisted';
|
|
271
|
+
/**
|
|
272
|
+
* The module id that stands for a generated module of a GPU path that a build does not ship. The
|
|
273
|
+
* names that the module exports follow it, so one stub serves every table that exports the same
|
|
274
|
+
* names, such as the tables of builds that need several features.
|
|
275
|
+
*/
|
|
276
|
+
const NO_SHADER_MARK = 'null3d:no-shader/';
|
|
277
|
+
const NO_SHADER_ID = `\0${NO_SHADER_MARK}`;
|
|
278
|
+
/** The start of the id of a stub module, with or without Vite's null byte. */
|
|
279
|
+
const NO_SHADER_PATTERN = /^\0?null3d:no-shader\//;
|
|
280
|
+
/** The code of a stub module, which exports each name that it stands for as an empty object. */
|
|
281
|
+
function stubModule(names) {
|
|
282
|
+
return `${names.map((name) => `export const ${name} = {};`).join('\n')}\n`;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* True for the folder of a generated shader set, which holds the marker module that every set has.
|
|
286
|
+
* Only a module of such a folder takes a stub, so a project's own file that happens to share a name
|
|
287
|
+
* never does.
|
|
288
|
+
*/
|
|
289
|
+
function inShaderSet(folder) {
|
|
290
|
+
return ['shader-permutations.ts', 'shader-permutations.js'].some((name) => existsSync(join(folder, name)));
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* The names that a generated shader module exports, in the order the shader build writes them. The
|
|
294
|
+
* stub of a module that a build drops exports the same names, so every import of it resolves.
|
|
295
|
+
*/
|
|
296
|
+
function exportNamesOf(text, file) {
|
|
297
|
+
const names = [...text.matchAll(/^export (?:const|function|class) ([A-Za-z_$][\w$]*)/gm)].map((match) => match[1]);
|
|
298
|
+
if (names.length === 0)
|
|
299
|
+
throw new Error(`null3D cannot read the exports of the engine's shader module ${file}, so it cannot stand an empty one in its place. Install the same version of @null3d/vite-plugin as of @null3d/engine, or run bun run shaders in a checkout.`);
|
|
300
|
+
return names;
|
|
301
|
+
}
|
|
271
302
|
/** The docs on a project's list of permutation words, which the build's report names. */
|
|
272
303
|
const SHADER_LIST_DOCS = 'https://github.com/null3d-engine/null3d/blob/generated/docs/guides/assets-pipeline.md#build-only-the-shader-permutations-you-draw-with';
|
|
273
304
|
/** A number of bytes in megabytes, as the build's report gives it. */
|
|
@@ -355,6 +386,11 @@ export default function null3d(options = {}) {
|
|
|
355
386
|
let building = false;
|
|
356
387
|
/** True for a production build of pages, which gets the early script. */
|
|
357
388
|
let buildingPages = false;
|
|
389
|
+
/**
|
|
390
|
+
* The GPU paths whose shaders a build ships. The dev server keeps both, so a developer can test
|
|
391
|
+
* the other path, and a build narrows the set and the project's own shaders to this.
|
|
392
|
+
*/
|
|
393
|
+
const shipped = shippedTargets(options.shaders);
|
|
358
394
|
/** The early script's module, once a build of pages that imports the engine has added it. */
|
|
359
395
|
let earlyCoreId;
|
|
360
396
|
/** True once the build has looked for the early script. */
|
|
@@ -407,12 +443,22 @@ export default function null3d(options = {}) {
|
|
|
407
443
|
return { name, bytes, source: source ?? bytes };
|
|
408
444
|
};
|
|
409
445
|
/**
|
|
410
|
-
* The copy of a generated shader file.
|
|
411
|
-
*
|
|
412
|
-
*
|
|
446
|
+
* The copy of a generated shader file. A file of a GPU path that this build does not ship gives
|
|
447
|
+
* way to one module without builds, and the build copies it for every such file. With a list, a
|
|
448
|
+
* file whose builds the list all names stays whole, a file of which it names some keeps only
|
|
449
|
+
* those, and a file of which it names none gives way to one module without builds.
|
|
413
450
|
*/
|
|
414
451
|
const copyOf = async (file) => {
|
|
415
452
|
const stem = file.slice(file.lastIndexOf('/') + 1).replace(/\.js$/, '');
|
|
453
|
+
const target = targetOfModule(file);
|
|
454
|
+
if (target !== null && !shipped[target]) {
|
|
455
|
+
// The words of a dropped file still answer the project's list, which may hold a word that
|
|
456
|
+
// only the other GPU path builds, such as a morph target's.
|
|
457
|
+
if (shaderList !== undefined)
|
|
458
|
+
for (const word of (await hasher.scan(file)).words)
|
|
459
|
+
holdWord(heldWords, word);
|
|
460
|
+
return narrowedCopy(UNLISTED_STEM, UNLISTED_MODULE);
|
|
461
|
+
}
|
|
416
462
|
const list = shaderList;
|
|
417
463
|
if (list === undefined)
|
|
418
464
|
return { name: `${stem}-${await hasher.hash(file)}.js`, bytes: file, source: file };
|
|
@@ -445,7 +491,7 @@ export default function null3d(options = {}) {
|
|
|
445
491
|
/** Keeps each compiled shader in the project's cache, so a later build skips the compile. */
|
|
446
492
|
const shaders = new ShaderCache(() => root, compiler, { disabled: () => noShaderCache });
|
|
447
493
|
/** Custom materials' builds, which load on first use from modules of their own by device. */
|
|
448
|
-
const materialBuilds = new MaterialBuilds();
|
|
494
|
+
const materialBuilds = new MaterialBuilds(() => !building || shipped.glsl);
|
|
449
495
|
/** What the dev server's pages run, to judge each change of WGSL. */
|
|
450
496
|
const hot = new HotState();
|
|
451
497
|
/** The key of a project module's WGSL on the dev server, which builds and packages lack. */
|
|
@@ -489,8 +535,11 @@ export default function null3d(options = {}) {
|
|
|
489
535
|
*/
|
|
490
536
|
const reportShaderCopies = (files, bytes) => {
|
|
491
537
|
const copied = `null3D: copied ${files.toLocaleString('en-US')} shader files, ${megabytes(bytes)}`;
|
|
538
|
+
const paths = shipped.glsl ? '' : ' for the WebGPU path alone';
|
|
492
539
|
if (shaderList !== undefined)
|
|
493
|
-
logger?.info(`${copied}: the builds of the ${shaderList.words.size} permutation words of ${shaderList.file}.`);
|
|
540
|
+
logger?.info(`${copied}${paths}: the builds of the ${shaderList.words.size} permutation words of ${shaderList.file}.`);
|
|
541
|
+
else if (!shipped.glsl)
|
|
542
|
+
logger?.info(`${copied}: every build of every permutation word of the WebGPU path. A page downloads only the few that its device needs. Record the words that your pages draw with in a list, and name it in the plugin's shaders.list option, to copy only their builds: ${SHADER_LIST_DOCS}`);
|
|
494
543
|
else
|
|
495
544
|
logger?.warn(`${copied}: every build of every permutation word in the engine's shader set. A page downloads only the few that its device needs, but the host stores them all. Record the words that your pages draw with in a list, and name it in the plugin's shaders.list option, to copy only their builds: ${SHADER_LIST_DOCS}`);
|
|
496
545
|
};
|
|
@@ -571,6 +620,8 @@ export default function null3d(options = {}) {
|
|
|
571
620
|
copiedTables.files = 0;
|
|
572
621
|
copiedTables.bytes = 0;
|
|
573
622
|
heldWords.clear();
|
|
623
|
+
// A rebuild reads the dropped files again, so the scan must answer the new list too.
|
|
624
|
+
scannedWords.clear();
|
|
574
625
|
if (narrowedFolder !== undefined)
|
|
575
626
|
rmSync(narrowedFolder, { recursive: true, force: true });
|
|
576
627
|
narrowedFolder = undefined;
|
|
@@ -658,7 +709,7 @@ export default function null3d(options = {}) {
|
|
|
658
709
|
},
|
|
659
710
|
async handler(id) {
|
|
660
711
|
if (id === HOT_CLIENT_ID)
|
|
661
|
-
return hotClientCode(options.shaders?.list !== undefined);
|
|
712
|
+
return hotClientCode(options.shaders?.list !== undefined, !shipped.glsl);
|
|
662
713
|
if (MATERIAL_BUILDS_ADDRESS.test(id))
|
|
663
714
|
return (materialBuilds.module(id) ??
|
|
664
715
|
this.error(`null3D has no custom material builds named ${id.slice(1)}. Reload the page so the dev server compiles its WGSL again.`));
|
|
@@ -692,7 +743,7 @@ export default function null3d(options = {}) {
|
|
|
692
743
|
return `export default new URL(import.meta.ROLLUP_FILE_URL_${ref}, import.meta.url).href;\n`;
|
|
693
744
|
}
|
|
694
745
|
const path = projectPath(root, id);
|
|
695
|
-
const compiled = await compileWgslFile(path, id, readFileSync(id, 'utf8'), shaders);
|
|
746
|
+
const compiled = await compileWgslFile(path, id, readFileSync(id, 'utf8'), shaders, !building || shipped.glsl);
|
|
696
747
|
if ('error' in compiled)
|
|
697
748
|
return this.error(compiled.error);
|
|
698
749
|
for (const warning of compiled.warnings)
|
|
@@ -709,7 +760,7 @@ export default function null3d(options = {}) {
|
|
|
709
760
|
}
|
|
710
761
|
}
|
|
711
762
|
const pack = new ShaderPack();
|
|
712
|
-
const builds = new ModuleBuilds();
|
|
763
|
+
const builds = new ModuleBuilds(!building || shipped.glsl);
|
|
713
764
|
const value = builds.value(pack, compiled.shader, key);
|
|
714
765
|
return {
|
|
715
766
|
code: `export default ${value};\n${pack.unpacker()}${materialBuilds.finish(id, builds)}`,
|
|
@@ -728,13 +779,13 @@ export default function null3d(options = {}) {
|
|
|
728
779
|
let out;
|
|
729
780
|
if (SCRIPT_FILE.test(file) && WGSL_TAG.test(code)) {
|
|
730
781
|
const path = projectPath(root, file);
|
|
731
|
-
const tagged = await compileTaggedWgsl(code, file, path, shaders);
|
|
782
|
+
const tagged = await compileTaggedWgsl(code, file, path, shaders, !building || shipped.glsl);
|
|
732
783
|
if ('error' in tagged)
|
|
733
784
|
return this.error(tagged.error);
|
|
734
785
|
for (const warning of tagged.warnings)
|
|
735
786
|
this.warn(warning);
|
|
736
787
|
const pack = new ShaderPack();
|
|
737
|
-
const builds = new ModuleBuilds();
|
|
788
|
+
const builds = new ModuleBuilds(!building || shipped.glsl);
|
|
738
789
|
for (const [literal, { start, end, shader }] of tagged.shaders.entries()) {
|
|
739
790
|
const key = keyOf(file, path, literal);
|
|
740
791
|
if (key !== undefined)
|
|
@@ -783,7 +834,7 @@ export default function null3d(options = {}) {
|
|
|
783
834
|
return;
|
|
784
835
|
const path = projectPath(root, file);
|
|
785
836
|
if (WGSL_FILE.test(file)) {
|
|
786
|
-
const compiled = await compileWgslFile(path, file, await read(), shaders);
|
|
837
|
+
const compiled = await compileWgslFile(path, file, await read(), shaders, !building || shipped.glsl);
|
|
787
838
|
if ('error' in compiled) {
|
|
788
839
|
showError(environment, compiled.error);
|
|
789
840
|
return otherModules(modules, file);
|
|
@@ -804,7 +855,7 @@ export default function null3d(options = {}) {
|
|
|
804
855
|
const changed = changedLiterals(before, code, file);
|
|
805
856
|
if (!changed)
|
|
806
857
|
return;
|
|
807
|
-
const results = await Promise.all(changed.map(({ literal }) => compileLiteral(literal, code, file, path, shaders)));
|
|
858
|
+
const results = await Promise.all(changed.map(({ literal }) => compileLiteral(literal, code, file, path, shaders, !building || shipped.glsl)));
|
|
808
859
|
const updates = [];
|
|
809
860
|
for (const [k, result] of results.entries()) {
|
|
810
861
|
if ('error' in result) {
|
|
@@ -1034,6 +1085,24 @@ export default function null3d(options = {}) {
|
|
|
1034
1085
|
client.send(UNLISTED_WORD_EVENT, { message: warning.message });
|
|
1035
1086
|
});
|
|
1036
1087
|
}
|
|
1088
|
+
// The dev server draws with both GPU paths. It warns when a page draws with a path that a
|
|
1089
|
+
// production build with these options does not ship, because that build stops the page.
|
|
1090
|
+
if (!shipped.glsl) {
|
|
1091
|
+
const warned = new Set();
|
|
1092
|
+
server.environments.client.hot.on(GPU_PATH_EVENT, (data, client) => {
|
|
1093
|
+
const { path, page } = (data ?? {});
|
|
1094
|
+
if (typeof path !== 'string' || typeof page !== 'string')
|
|
1095
|
+
return;
|
|
1096
|
+
if (warned.has(page))
|
|
1097
|
+
return;
|
|
1098
|
+
const message = unshippedPathWarning(path, page, shipped.glsl);
|
|
1099
|
+
if (message === undefined)
|
|
1100
|
+
return;
|
|
1101
|
+
warned.add(page);
|
|
1102
|
+
server.config.logger.warn(`null3D: ${message}`, { timestamp: true });
|
|
1103
|
+
client.send(UNSHIPPED_PATH_EVENT, { message });
|
|
1104
|
+
});
|
|
1105
|
+
}
|
|
1037
1106
|
server.middlewares.use(`${server.config.base}${ASSET_FOLDER}/`, (req, res, next) => {
|
|
1038
1107
|
const path = decodeURIComponent((req.url ?? '').split('?')[0]?.slice(1) ?? '');
|
|
1039
1108
|
const bytes = cachedFile(root, path);
|
|
@@ -1056,6 +1125,28 @@ export default function null3d(options = {}) {
|
|
|
1056
1125
|
server.middlewares.use(compressedFiles(resolve(config.root, config.build.outDir), config.base, config.preview.headers));
|
|
1057
1126
|
},
|
|
1058
1127
|
};
|
|
1128
|
+
/** The device files whose words this build has read for the project's shader list. */
|
|
1129
|
+
const scannedWords = new Set();
|
|
1130
|
+
/**
|
|
1131
|
+
* Adds to `heldWords` the words of every device file that a generated module names. A build
|
|
1132
|
+
* that drops the module must still answer the project's list with its words, which the module's
|
|
1133
|
+
* own text does not hold: a table of combined builds names them through the addresses below.
|
|
1134
|
+
*/
|
|
1135
|
+
const holdNamedWords = (text, file) => {
|
|
1136
|
+
if (shaderList === undefined)
|
|
1137
|
+
return;
|
|
1138
|
+
for (const match of text.matchAll(SCRIPT_URL)) {
|
|
1139
|
+
const address = match[2] ?? '';
|
|
1140
|
+
if (!isGeneratedShaderFile(file, address))
|
|
1141
|
+
continue;
|
|
1142
|
+
const named = resolve(dirname(file), address);
|
|
1143
|
+
if (scannedWords.has(named) || !existsSync(named))
|
|
1144
|
+
continue;
|
|
1145
|
+
scannedWords.add(named);
|
|
1146
|
+
for (const word of moduleWords(readFileSync(named, 'utf8')))
|
|
1147
|
+
holdWord(heldWords, word);
|
|
1148
|
+
}
|
|
1149
|
+
};
|
|
1059
1150
|
/**
|
|
1060
1151
|
* Names the generated shader files' copies before Vite's own asset step sees them, which runs
|
|
1061
1152
|
* after the plugins that declare no order. A generated module loads each of its builds by
|
|
@@ -1063,7 +1154,8 @@ export default function null3d(options = {}) {
|
|
|
1063
1154
|
* memory to emit it as an asset; here a pool of worker threads hashes each file and the build
|
|
1064
1155
|
* copies it on its own, and the module names the copy, so the build never holds the whole set
|
|
1065
1156
|
* at once. With a list of words, each copy holds only the listed builds, and the set's module of
|
|
1066
|
-
* listed words names the project's list.
|
|
1157
|
+
* listed words names the project's list. With a GPU path set, a file of the other path gives way
|
|
1158
|
+
* to one module without builds, so the build copies none of that path's text.
|
|
1067
1159
|
*
|
|
1068
1160
|
* The argument is a mark rather than an address: where a copy lands is fixed, but the file that
|
|
1069
1161
|
* names it can sit anywhere in the output, and only `generateBundle` knows where.
|
|
@@ -1076,15 +1168,32 @@ export default function null3d(options = {}) {
|
|
|
1076
1168
|
const file = id.split('?')[0] ?? id;
|
|
1077
1169
|
// The engine names a missing word as one that the list lacks, and the list to record.
|
|
1078
1170
|
const name = file.slice(file.lastIndexOf('/') + 1);
|
|
1079
|
-
if (shaderList !== undefined &&
|
|
1171
|
+
if ((shaderList !== undefined || !shipped.glsl) &&
|
|
1080
1172
|
LISTED_MODULE.test(name) &&
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1173
|
+
inShaderSet(dirname(file)) &&
|
|
1174
|
+
code.includes('LISTED_PERMUTATIONS')) {
|
|
1175
|
+
// The engine reads the shipped paths from the same module. A set that predates the
|
|
1176
|
+
// constant would keep both paths, so the build refuses rather than ship one silently.
|
|
1177
|
+
if (!code.includes('SHIPPED_GPU_PATHS'))
|
|
1178
|
+
this.error("null3D: the engine's shader set is older than the plugin: its shader-permutations module has no SHIPPED_GPU_PATHS. Install the same version of @null3d/engine as of @null3d/vite-plugin, or run bun run shaders in a checkout.");
|
|
1179
|
+
return {
|
|
1180
|
+
code: shaderMarkersModule(shaderList?.words ?? null, shipped.glsl ? null : ['webgpu']),
|
|
1181
|
+
map: null,
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1184
|
+
let text = code;
|
|
1185
|
+
let changed = false;
|
|
1186
|
+
// An add-on package's compiled shaders keep both GPU paths, which a build that ships one
|
|
1187
|
+
// path drops here, including the modules of the other path's build sets.
|
|
1188
|
+
if (!shipped.glsl && text.includes(UNPACK)) {
|
|
1189
|
+
text = narrowShaderPack(text, file);
|
|
1190
|
+
changed = true;
|
|
1191
|
+
}
|
|
1192
|
+
if (!text.includes('import.meta.url'))
|
|
1193
|
+
return changed ? { code: text, map: null } : undefined;
|
|
1085
1194
|
// The files this module names.
|
|
1086
1195
|
const named = [];
|
|
1087
|
-
for (const match of
|
|
1196
|
+
for (const match of text.matchAll(SCRIPT_URL)) {
|
|
1088
1197
|
const address = match[2] ?? '';
|
|
1089
1198
|
if (!isGeneratedShaderFile(file, address))
|
|
1090
1199
|
continue;
|
|
@@ -1094,7 +1203,7 @@ export default function null3d(options = {}) {
|
|
|
1094
1203
|
named.push({ match, target });
|
|
1095
1204
|
}
|
|
1096
1205
|
if (named.length === 0)
|
|
1097
|
-
return;
|
|
1206
|
+
return changed ? { code: text, map: null } : undefined;
|
|
1098
1207
|
// The pool reads each file in chunks and hashes it, so the build's own thread reads none
|
|
1099
1208
|
// of the files and holds none of their bytes; only a file that a list narrows is read
|
|
1100
1209
|
// here, to write the copy of its listed builds.
|
|
@@ -1102,12 +1211,70 @@ export default function null3d(options = {}) {
|
|
|
1102
1211
|
let out;
|
|
1103
1212
|
for (const { match, copy } of copies) {
|
|
1104
1213
|
copiedShaders.set(copy.name, copy);
|
|
1105
|
-
out ??= new MagicString(
|
|
1214
|
+
out ??= new MagicString(text);
|
|
1106
1215
|
out.overwrite(match.index, match.index + match[0].length, `((u) => new URL(u, import.meta.url))(${JSON.stringify(SHADER_URL_MARK + copy.name)})`);
|
|
1107
1216
|
}
|
|
1108
|
-
|
|
1109
|
-
? { code:
|
|
1110
|
-
|
|
1217
|
+
if (out === undefined)
|
|
1218
|
+
return changed ? { code: text, map: null } : undefined;
|
|
1219
|
+
return { code: out.toString(), map: out.generateMap({ hires: 'boundary' }) };
|
|
1220
|
+
},
|
|
1221
|
+
// A generated module of a GPU path that this build does not ship, named by a module of the
|
|
1222
|
+
// set itself, such as a table of builds that need several features, takes the place of one
|
|
1223
|
+
// stub module. The stub exports the names of the module it stands for, so every import of it
|
|
1224
|
+
// resolves, and the lookups find no build rather than failing to load the module.
|
|
1225
|
+
resolveId: {
|
|
1226
|
+
filter: { id: /^\.\/shaders(?:-[a-z0-9-]+)+$/ },
|
|
1227
|
+
handler(source, importer) {
|
|
1228
|
+
if (shipped.glsl || importer === undefined)
|
|
1229
|
+
return;
|
|
1230
|
+
const importerFile = importer.split('?')[0] ?? importer;
|
|
1231
|
+
const importerName = importerFile.slice(importerFile.lastIndexOf('/') + 1);
|
|
1232
|
+
if (!GENERATED_MODULE.test(importerName))
|
|
1233
|
+
return;
|
|
1234
|
+
if (!inShaderSet(dirname(importerFile)))
|
|
1235
|
+
return;
|
|
1236
|
+
if (targetOfModule(source) !== 'glsl')
|
|
1237
|
+
return;
|
|
1238
|
+
const named = resolve(dirname(importerFile), source);
|
|
1239
|
+
const file = [named, `${named}.ts`, `${named}.js`].find((path) => existsSync(path));
|
|
1240
|
+
if (file === undefined)
|
|
1241
|
+
return;
|
|
1242
|
+
const text = readFileSync(file, 'utf8');
|
|
1243
|
+
holdNamedWords(text, file);
|
|
1244
|
+
return `${NO_SHADER_ID}${exportNamesOf(text, file).join(',')}`;
|
|
1245
|
+
},
|
|
1246
|
+
},
|
|
1247
|
+
// A dropped generated module that a module imports by name, such as a whole shader that loads
|
|
1248
|
+
// on first use, gives way to a stub of the same exports. Its words still answer a project's
|
|
1249
|
+
// shader list, which may hold a word that only the other GPU path builds.
|
|
1250
|
+
load: {
|
|
1251
|
+
filter: {
|
|
1252
|
+
id: { include: [/shaders(?:-[a-z0-9-]+)?\.[cm]?[jt]s$/, NO_SHADER_PATTERN] },
|
|
1253
|
+
},
|
|
1254
|
+
handler(id) {
|
|
1255
|
+
const bare = id.startsWith('\0') ? id.slice(1) : id;
|
|
1256
|
+
if (bare.startsWith(NO_SHADER_MARK))
|
|
1257
|
+
return stubModule(bare
|
|
1258
|
+
.slice(NO_SHADER_MARK.length)
|
|
1259
|
+
.split(',')
|
|
1260
|
+
.filter((name) => name !== ''));
|
|
1261
|
+
if (shipped.glsl)
|
|
1262
|
+
return;
|
|
1263
|
+
const file = id.split('?')[0] ?? id;
|
|
1264
|
+
const name = file.slice(file.lastIndexOf('/') + 1);
|
|
1265
|
+
if (!GENERATED_MODULE.test(name))
|
|
1266
|
+
return;
|
|
1267
|
+
if (!inShaderSet(dirname(file)))
|
|
1268
|
+
return;
|
|
1269
|
+
if (targetOfModule(name) !== 'glsl')
|
|
1270
|
+
return;
|
|
1271
|
+
const text = readFileSync(file, 'utf8');
|
|
1272
|
+
if (shaderList !== undefined)
|
|
1273
|
+
for (const word of moduleWords(text))
|
|
1274
|
+
holdWord(heldWords, word);
|
|
1275
|
+
holdNamedWords(text, file);
|
|
1276
|
+
return stubModule(exportNamesOf(text, file));
|
|
1277
|
+
},
|
|
1111
1278
|
},
|
|
1112
1279
|
};
|
|
1113
1280
|
return [streamedShaderAssets, plugin];
|
package/lib/material-builds.d.ts
CHANGED
|
@@ -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
|
package/lib/material-builds.js
CHANGED
|
@@ -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
|
|
121
|
+
const kept = this.glsl ? shader : withoutGlsl(shader);
|
|
122
|
+
const kind = kept.kind;
|
|
112
123
|
const variants = isBuildKind(kind)
|
|
113
|
-
?
|
|
124
|
+
? kept.variants
|
|
114
125
|
: undefined;
|
|
115
126
|
if (variants === undefined || Object.keys(variants).length === 0)
|
|
116
|
-
return pack.value(
|
|
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({ ...
|
|
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'
|
|
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
|
-
/**
|
|
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
|
-
/**
|
|
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.
|
package/lib/shader-list.d.ts
CHANGED
|
@@ -3,6 +3,33 @@
|
|
|
3
3
|
* list serves every device module, and a build matches a word with those bits cleared.
|
|
4
4
|
*/
|
|
5
5
|
export declare const DEVICE_BITS: number;
|
|
6
|
+
/**
|
|
7
|
+
* A GPU path that a build can ship: WGSL builds for WebGPU, or GLSL ES 3.00 builds for WebGL2.
|
|
8
|
+
* WebGPU's compatibility mode draws with the WebGPU builds, so it counts as `webgpu`.
|
|
9
|
+
*/
|
|
10
|
+
export type GpuPath = 'webgpu' | 'webgl2';
|
|
11
|
+
/** The shader language of a GPU path: WGSL for WebGPU, GLSL for WebGL2. */
|
|
12
|
+
export type ShaderTarget = 'wgsl' | 'glsl';
|
|
13
|
+
/** The shader target of a GPU path. */
|
|
14
|
+
export declare function targetOfPath(path: GpuPath): ShaderTarget;
|
|
15
|
+
/**
|
|
16
|
+
* The shader target of a generated shader module, from its name, or null for the modules that both
|
|
17
|
+
* paths share, such as the main module and the marker module. A name that no longer names a target
|
|
18
|
+
* cannot slip through: the build only drops a module whose target it read here.
|
|
19
|
+
*/
|
|
20
|
+
export declare function targetOfModule(name: string): ShaderTarget | null;
|
|
21
|
+
/** The shader targets that a project's build keeps. */
|
|
22
|
+
export interface ShippedTargets {
|
|
23
|
+
readonly wgsl: boolean;
|
|
24
|
+
readonly glsl: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The shader targets that the plugin's options ship. Both unless `gpuPaths` names one. Throws for
|
|
28
|
+
* a list that names no GPU path, an unknown path, or one without WebGPU: the shader compiler builds
|
|
29
|
+
* every shader for WebGPU, and a build that leaves it out would have no form for the shaders that a
|
|
30
|
+
* project's own modules and its add-ons declare.
|
|
31
|
+
*/
|
|
32
|
+
export declare function shippedTargets(options: ShaderOptions | undefined): ShippedTargets;
|
|
6
33
|
/** The shader options of the plugin. */
|
|
7
34
|
export interface ShaderOptions {
|
|
8
35
|
/**
|
|
@@ -15,6 +42,14 @@ export interface ShaderOptions {
|
|
|
15
42
|
* with and the list lacks.
|
|
16
43
|
*/
|
|
17
44
|
list?: string;
|
|
45
|
+
/**
|
|
46
|
+
* The GPU paths whose shaders the build ships, such as `['webgpu']` for an app whose browser
|
|
47
|
+
* frame always offers WebGPU, as an Electron desktop app's does. A build then drops the other
|
|
48
|
+
* path's shader files and fails with E1308 on a device that would need them. The default is
|
|
49
|
+
* both paths. `webgpu` is required, because the shader compiler builds every shader for it. The
|
|
50
|
+
* dev server keeps both paths. Docs: guides/assets-pipeline.md#ship-a-subset-of-the-gpu-paths.
|
|
51
|
+
*/
|
|
52
|
+
gpuPaths?: readonly GpuPath[];
|
|
18
53
|
}
|
|
19
54
|
/**
|
|
20
55
|
* The words of a list file's text: one word on each line, as hexadecimal with `0x` or as a decimal
|
|
@@ -104,8 +139,12 @@ export declare function builtWords(text: string): number[];
|
|
|
104
139
|
* word.
|
|
105
140
|
*/
|
|
106
141
|
export declare const UNLISTED_MODULE = "// The builds of a device module that the project's shader list leaves out.\nexport const SHADERS = {};\n";
|
|
107
|
-
/**
|
|
108
|
-
|
|
142
|
+
/**
|
|
143
|
+
* The generated marker module's text for a build: the listed words, or null for a build that copies
|
|
144
|
+
* the whole set, and the shipped GPU paths, or null for a build that ships both. The module that
|
|
145
|
+
* the shader build writes declares both names, so the engine reads one shape either way.
|
|
146
|
+
*/
|
|
147
|
+
export declare function shaderMarkersModule(words: ReadonlySet<number> | null, paths: readonly GpuPath[] | null): string;
|
|
109
148
|
/**
|
|
110
149
|
* The text of the generated device module `text` with only the builds whose permutation word
|
|
111
150
|
* `keep` takes, and the counts of the builds it kept and held. A shader keeps its entry with no
|
package/lib/shader-list.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// A project's list of the permutation words that its pages draw with, and the narrowing of the
|
|
2
|
-
// engine's shader set to that list
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
2
|
+
// engine's shader set: to that list, and to the GPU paths that the project's build ships. The set
|
|
3
|
+
// holds every build of every word for both paths, gigabytes in thousands of files, and a page
|
|
4
|
+
// downloads the few that its device needs. With a list, a build copies only the builds of the
|
|
5
|
+
// listed words: a device file keeps the listed builds and drops the others, and a file that holds
|
|
6
|
+
// no listed build is replaced by one module without builds. A page that then asks for a word
|
|
7
|
+
// outside the list gets the engine's message that names the word. With a path set, a build drops
|
|
8
|
+
// the other path's files, and the engine refuses that path on a device that needs it.
|
|
7
9
|
import { readFileSync, statSync } from 'node:fs';
|
|
8
10
|
import MagicString from 'magic-string';
|
|
9
11
|
import { parseAst } from 'vite';
|
|
@@ -13,6 +15,45 @@ import { DRAW_INDEX, HALF, TONE_MAP } from "./material-builds.js";
|
|
|
13
15
|
* list serves every device module, and a build matches a word with those bits cleared.
|
|
14
16
|
*/
|
|
15
17
|
export const DEVICE_BITS = DRAW_INDEX | TONE_MAP | HALF;
|
|
18
|
+
/** The shader target of a GPU path. */
|
|
19
|
+
export function targetOfPath(path) {
|
|
20
|
+
return path === 'webgl2' ? 'glsl' : 'wgsl';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The shader target of a generated shader module, from its name, or null for the modules that both
|
|
24
|
+
* paths share, such as the main module and the marker module. A name that no longer names a target
|
|
25
|
+
* cannot slip through: the build only drops a module whose target it read here.
|
|
26
|
+
*/
|
|
27
|
+
export function targetOfModule(name) {
|
|
28
|
+
const base = name.slice(name.lastIndexOf('/') + 1).replace(/\.(?:[cm]?[jt]s)$/, '');
|
|
29
|
+
const tokens = base.split('-');
|
|
30
|
+
if (tokens.includes('glsl'))
|
|
31
|
+
return 'glsl';
|
|
32
|
+
if (tokens.includes('wgsl'))
|
|
33
|
+
return 'wgsl';
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The shader targets that the plugin's options ship. Both unless `gpuPaths` names one. Throws for
|
|
38
|
+
* a list that names no GPU path, an unknown path, or one without WebGPU: the shader compiler builds
|
|
39
|
+
* every shader for WebGPU, and a build that leaves it out would have no form for the shaders that a
|
|
40
|
+
* project's own modules and its add-ons declare.
|
|
41
|
+
*/
|
|
42
|
+
export function shippedTargets(options) {
|
|
43
|
+
const paths = options?.gpuPaths;
|
|
44
|
+
if (paths === undefined)
|
|
45
|
+
return { wgsl: true, glsl: true };
|
|
46
|
+
const known = paths.filter((path) => path !== 'webgpu' && path !== 'webgl2');
|
|
47
|
+
if (known.length > 0)
|
|
48
|
+
throw new Error(`null3D: shaders.gpuPaths takes 'webgpu' or 'webgl2', not ${known.map((path) => `"${path}"`).join(', ')}.`);
|
|
49
|
+
if (paths.length === 0)
|
|
50
|
+
throw new Error("null3D: shaders.gpuPaths is empty. Name the GPU paths to ship, such as ['webgpu'], or leave it out for both.");
|
|
51
|
+
if (!paths.includes('webgpu'))
|
|
52
|
+
throw new Error("null3D: shaders.gpuPaths must include 'webgpu'. The shader compiler builds every shader for WebGPU, so a build without it has no form for your own shaders and your add-ons' shaders. Ship both GPU paths instead.");
|
|
53
|
+
if (!paths.includes('webgl2'))
|
|
54
|
+
return { wgsl: true, glsl: false };
|
|
55
|
+
return { wgsl: true, glsl: true };
|
|
56
|
+
}
|
|
16
57
|
/** The largest permutation word: the shader build keeps a word in 32 bits. */
|
|
17
58
|
const MAX_WORD = 0xffff_ffff;
|
|
18
59
|
/**
|
|
@@ -180,10 +221,15 @@ export function builtWords(text) {
|
|
|
180
221
|
* word.
|
|
181
222
|
*/
|
|
182
223
|
export const UNLISTED_MODULE = `// The builds of a device module that the project's shader list leaves out.\nexport const SHADERS = {};\n`;
|
|
183
|
-
/**
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
224
|
+
/**
|
|
225
|
+
* The generated marker module's text for a build: the listed words, or null for a build that copies
|
|
226
|
+
* the whole set, and the shipped GPU paths, or null for a build that ships both. The module that
|
|
227
|
+
* the shader build writes declares both names, so the engine reads one shape either way.
|
|
228
|
+
*/
|
|
229
|
+
export function shaderMarkersModule(words, paths) {
|
|
230
|
+
const listed = words === null ? 'null' : `[${[...words].sort((a, b) => a - b).join(', ')}]`;
|
|
231
|
+
const shipped = paths === null ? 'null' : `[${paths.map((path) => `'${path}'`).join(', ')}]`;
|
|
232
|
+
return `export const LISTED_PERMUTATIONS: readonly number[] | null = ${listed};\nexport const SHIPPED_GPU_PATHS: readonly ('webgpu' | 'webgl2')[] | null = ${shipped};\n`;
|
|
187
233
|
}
|
|
188
234
|
/** An object literal's properties, or undefined for any other node. */
|
|
189
235
|
function propertiesOf(node) {
|
package/lib/wgsl.d.ts
CHANGED
|
@@ -44,12 +44,13 @@ export type WgslCompile = {
|
|
|
44
44
|
};
|
|
45
45
|
/**
|
|
46
46
|
* Compiles WGSL from a project with the engine's shader library. A whole shader builds for WebGPU,
|
|
47
|
-
* and for WebGL2 when it has a render pipeline. A custom material's
|
|
48
|
-
* variant of the engine's standard material, and a custom effect's or
|
|
49
|
-
* template. `path` names the file in messages, and `hint` ends the message
|
|
50
|
-
* none of these. `compiler` runs the compile, on this thread by default.
|
|
47
|
+
* and for WebGL2 when it has a render pipeline and the compile ships that path. A custom material's
|
|
48
|
+
* functions build into every variant of the engine's standard material, and a custom effect's or
|
|
49
|
+
* tone curve's into its template. `path` names the file in messages, and `hint` ends the message
|
|
50
|
+
* about WGSL that is none of these. `compiler` runs the compile, on this thread by default. `glsl`
|
|
51
|
+
* false builds the WebGPU path alone, for a project whose build ships no WebGL2 shaders.
|
|
51
52
|
*/
|
|
52
|
-
export declare function compileWgsl(path: string, source: string, hint: string, compiler?: ShaderCompiler): Promise<WgslCompile>;
|
|
53
|
+
export declare function compileWgsl(path: string, source: string, hint: string, compiler?: ShaderCompiler, glsl?: boolean): Promise<WgslCompile>;
|
|
53
54
|
/** Where WGSL starts in the file that holds it: the file as messages name it, and a place. */
|
|
54
55
|
export interface WgslOrigin extends Place {
|
|
55
56
|
readonly path: string;
|
|
@@ -90,14 +91,14 @@ export type ShaderOrError = {
|
|
|
90
91
|
};
|
|
91
92
|
/**
|
|
92
93
|
* Compiles a `.wgsl` file. `path` names the file in messages, `id` is its module id, and `text` its
|
|
93
|
-
* content as read from disk.
|
|
94
|
+
* content as read from disk. `glsl` false builds the WebGPU path alone.
|
|
94
95
|
*/
|
|
95
|
-
export declare function compileWgslFile(path: string, id: string, text: string, compiler?: ShaderCompiler): Promise<ShaderOrError>;
|
|
96
|
+
export declare function compileWgslFile(path: string, id: string, text: string, compiler?: ShaderCompiler, glsl?: boolean): Promise<ShaderOrError>;
|
|
96
97
|
/**
|
|
97
98
|
* Compiles one tagged template literal of a script module, whose code is `code`: the compiled
|
|
98
99
|
* WGSL that takes its place, or the error that stops the module.
|
|
99
100
|
*/
|
|
100
|
-
export declare function compileLiteral(literal: TaggedWgsl, code: string, id: string, path: string, compiler?: ShaderCompiler): Promise<ShaderOrError>;
|
|
101
|
+
export declare function compileLiteral(literal: TaggedWgsl, code: string, id: string, path: string, compiler?: ShaderCompiler, glsl?: boolean): Promise<ShaderOrError>;
|
|
101
102
|
/** A tagged template literal of a module, and the compiled WGSL that takes its place. */
|
|
102
103
|
export interface TaggedShader {
|
|
103
104
|
readonly start: number;
|
|
@@ -108,7 +109,7 @@ export interface TaggedShader {
|
|
|
108
109
|
* Compiles each tagged template literal in a script module, all at once, with every literal's
|
|
109
110
|
* warnings, or gives the error that stops the module at its first literal that does not compile.
|
|
110
111
|
*/
|
|
111
|
-
export declare function compileTaggedWgsl(code: string, id: string, path: string, compiler?: ShaderCompiler): Promise<{
|
|
112
|
+
export declare function compileTaggedWgsl(code: string, id: string, path: string, compiler?: ShaderCompiler, glsl?: boolean): Promise<{
|
|
112
113
|
readonly shaders: readonly TaggedShader[];
|
|
113
114
|
readonly warnings: readonly string[];
|
|
114
115
|
} | {
|
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.1.
|
|
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.1.
|
|
64
|
+
"@null3d/cli": "0.1.2",
|
|
65
65
|
"vite": "8.3.1"
|
|
66
66
|
},
|
|
67
67
|
"dependencies": {
|