@null3d/vite-plugin 0.0.0-stage → 0.0.0
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/LICENSE-APACHE +176 -0
- package/LICENSE-MIT +21 -0
- package/THIRD-PARTY-NOTICES.txt +398 -0
- package/dist/shader-compiler.stamp.json +4 -0
- package/dist/shader-compiler.wasm +0 -0
- package/lib/assets.d.ts +94 -0
- package/lib/assets.js +189 -0
- package/lib/client.d.ts +21 -0
- package/lib/compile-pool.d.ts +25 -0
- package/lib/compile-pool.js +90 -0
- package/lib/compile-worker.js +11 -0
- package/lib/compiler-calls.d.ts +26 -0
- package/lib/compiler-calls.js +97 -0
- package/lib/compiler-stamp.d.ts +8 -0
- package/lib/compiler-stamp.js +34 -0
- package/lib/declarations.d.ts +10 -0
- package/lib/declarations.js +80 -0
- package/lib/engine-shaders-local.d.ts +6 -0
- package/lib/engine-shaders-local.js +75 -0
- package/lib/engine-shaders.d.ts +61 -0
- package/lib/engine-shaders.js +170 -0
- package/lib/hash-pool.d.ts +24 -0
- package/lib/hash-pool.js +84 -0
- package/lib/hash-worker.js +53 -0
- package/lib/hot.d.ts +53 -0
- package/lib/hot.js +101 -0
- package/lib/index.d.ts +95 -0
- package/lib/index.js +895 -0
- package/lib/material-builds.d.ts +91 -0
- package/lib/material-builds.js +236 -0
- package/lib/offline.d.ts +92 -0
- package/lib/offline.js +304 -0
- package/lib/pack.d.ts +30 -0
- package/lib/pack.js +112 -0
- package/lib/package-files.d.ts +12 -0
- package/lib/package-files.js +39 -0
- package/lib/paragraphs.d.ts +19 -0
- package/lib/paragraphs.js +131 -0
- package/lib/shader-cache.d.ts +59 -0
- package/lib/shader-cache.js +213 -0
- package/lib/shader-compiler.d.ts +258 -0
- package/lib/shader-compiler.js +169 -0
- package/lib/shader-types.d.ts +261 -0
- package/lib/shader-types.js +4 -0
- package/lib/wgsl.d.ts +118 -0
- package/lib/wgsl.js +446 -0
- package/lib/worker-preload.d.ts +17 -0
- package/lib/worker-preload.js +65 -0
- package/package.json +69 -5
- package/README.md +0 -3
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// The shader compiler: the engine's shader build as a WebAssembly module, for build tools. It
|
|
2
|
+
// composes WGSL with the engine's shader library (`#import null3d::math` and the rest), checks it
|
|
3
|
+
// against the portable WGSL rules, and writes WGSL for WebGPU and GLSL ES 3.00 for WebGL2, with
|
|
4
|
+
// the reflection that the WebGL2 backend binds by. It runs in Node and Bun while a project builds,
|
|
5
|
+
// so pages never download a shader translator. `bun run build` builds the module from the
|
|
6
|
+
// `null3d-shaders-wasm` crate into this package's dist folder.
|
|
7
|
+
//
|
|
8
|
+
// The module takes and gives JSON, through `CompilerCalls`. The functions here run it on this
|
|
9
|
+
// thread: the first call loads the module, and later calls reuse it and the library modules it
|
|
10
|
+
// composed. The compiler pool runs the same calls on worker threads.
|
|
11
|
+
import { readFileSync } from 'node:fs';
|
|
12
|
+
import { fileURLToPath } from 'node:url';
|
|
13
|
+
import { CompilerCalls } from './compiler-calls.js';
|
|
14
|
+
/** Where `bun run build` writes the module. */
|
|
15
|
+
export const SHADER_COMPILER_URL = new URL('../dist/shader-compiler.wasm', import.meta.url);
|
|
16
|
+
/**
|
|
17
|
+
* Compiles every variant of a shader with the engine's shader library. The same problem in
|
|
18
|
+
* several variants comes once, with each variant's name.
|
|
19
|
+
*/
|
|
20
|
+
export function compileShader(shader) {
|
|
21
|
+
return shaderResult(call('compile', shader));
|
|
22
|
+
}
|
|
23
|
+
/** A shader compile's result from the module's response. */
|
|
24
|
+
export function shaderResult(response) {
|
|
25
|
+
return response.ok
|
|
26
|
+
? { ok: true, variants: response.output, warnings: response.warnings ?? [] }
|
|
27
|
+
: response;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Builds a custom material's WGSL into every variant of the engine's standard material, which
|
|
31
|
+
* then calls its functions. Problems in the WGSL name its own lines.
|
|
32
|
+
*/
|
|
33
|
+
export function compileMaterial(material) {
|
|
34
|
+
return materialResult(call('compile_material', material));
|
|
35
|
+
}
|
|
36
|
+
/** A custom material's result from the module's response. */
|
|
37
|
+
export function materialResult(response) {
|
|
38
|
+
return response.ok
|
|
39
|
+
? { ok: true, material: response.output, warnings: response.warnings ?? [] }
|
|
40
|
+
: response;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Builds a custom effect's WGSL into every variant of the engine's effect template, or a custom
|
|
44
|
+
* tone curve's into every variant of its final pass. Problems in the WGSL name its own lines.
|
|
45
|
+
*/
|
|
46
|
+
export function compileEffect(effect) {
|
|
47
|
+
return effectResult(call('compile_effect', effect));
|
|
48
|
+
}
|
|
49
|
+
/** A custom effect's or tone curve's result from the module's response. */
|
|
50
|
+
export function effectResult(response) {
|
|
51
|
+
return response.ok
|
|
52
|
+
? { ok: true, effect: response.output, warnings: response.warnings ?? [] }
|
|
53
|
+
: response;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Builds a sky layer's WGSL into every variant of the engine's sky-layer template. Problems in the
|
|
57
|
+
* WGSL name its own lines.
|
|
58
|
+
*/
|
|
59
|
+
export function compileSkyLayer(layer) {
|
|
60
|
+
return skyLayerResult(call('compile_sky_layer', layer));
|
|
61
|
+
}
|
|
62
|
+
/** A sky layer's result from the module's response. */
|
|
63
|
+
export function skyLayerResult(response) {
|
|
64
|
+
return response.ok
|
|
65
|
+
? { ok: true, layer: response.output, warnings: response.warnings ?? [] }
|
|
66
|
+
: response;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* The result of a custom material from the results of its shares: every build, by name in the
|
|
70
|
+
* order that the compiler gives them, with every warning, or every problem of every share. Each
|
|
71
|
+
* problem and warning comes once, with every build that has it.
|
|
72
|
+
*/
|
|
73
|
+
export function joinShares(shares) {
|
|
74
|
+
const problems = new Map();
|
|
75
|
+
const warnings = new Map();
|
|
76
|
+
const builds = [];
|
|
77
|
+
for (const share of shares) {
|
|
78
|
+
if (share.ok) {
|
|
79
|
+
builds.push(share.material);
|
|
80
|
+
addOnce(warnings, share.warnings);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
addOnce(problems, share.problems);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
const [first] = builds;
|
|
87
|
+
if (problems.size > 0 || !first)
|
|
88
|
+
return { ok: false, problems: [...problems.values()] };
|
|
89
|
+
const all = Object.assign({}, ...builds.map((build) => build.variants));
|
|
90
|
+
const variants = {};
|
|
91
|
+
// The compiler's builds come in the byte order of their names, as Rust's sorted map gives them.
|
|
92
|
+
for (const name of Object.keys(all).sort())
|
|
93
|
+
variants[name] = all[name];
|
|
94
|
+
return { ok: true, material: { ...first, variants }, warnings: [...warnings.values()] };
|
|
95
|
+
}
|
|
96
|
+
/** Adds problems to a map of them, each once with the builds of every copy, sorted. */
|
|
97
|
+
function addOnce(known, problems) {
|
|
98
|
+
for (const problem of problems) {
|
|
99
|
+
const { file, line, column, feature, message } = problem;
|
|
100
|
+
const key = JSON.stringify([file, line, column, feature, message]);
|
|
101
|
+
const before = known.get(key);
|
|
102
|
+
const variants = before ? [...new Set([...before.variants, ...problem.variants])] : [];
|
|
103
|
+
known.set(key, before ? { ...before, variants: variants.sort() } : problem);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** Compiles on this thread. */
|
|
107
|
+
export const compileHere = {
|
|
108
|
+
shader: async (shader) => compileShader(shader),
|
|
109
|
+
material: async (material) => compileMaterial(material),
|
|
110
|
+
effect: async (effect) => compileEffect(effect),
|
|
111
|
+
skyLayer: async (layer) => compileSkyLayer(layer),
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Builds every variant in a shader manifest, as the native `bun run shaders` does. The answer may
|
|
115
|
+
* be large — the engine's own manifest answers with hundreds of megabytes — and the module hands
|
|
116
|
+
* it over in pieces, which `CompilerCalls` joins before this reads it.
|
|
117
|
+
*/
|
|
118
|
+
export function buildShaders(inputs) {
|
|
119
|
+
return call('build', inputs);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Prepares the engine's own manifest, whose sources the compiler carries, and lists the file names
|
|
123
|
+
* of every module it makes. No text is rendered yet: the modules come one at a time, so a build
|
|
124
|
+
* never holds the whole set beside its sources.
|
|
125
|
+
*/
|
|
126
|
+
export function prepareEngineModules() {
|
|
127
|
+
const result = call('prepare_modules', {});
|
|
128
|
+
if (!result.ok)
|
|
129
|
+
throw new Error(problemsText(result.problems));
|
|
130
|
+
return result.output;
|
|
131
|
+
}
|
|
132
|
+
/** One module of the prepared engine build: its file name and its text. */
|
|
133
|
+
export function engineModule(name) {
|
|
134
|
+
const result = call('module', { name });
|
|
135
|
+
if (!result.ok)
|
|
136
|
+
throw new Error(problemsText(result.problems));
|
|
137
|
+
return result.output;
|
|
138
|
+
}
|
|
139
|
+
/** The problems of a failed call as one message, for a failure that ends a build. */
|
|
140
|
+
function problemsText(problems) {
|
|
141
|
+
return problems
|
|
142
|
+
.map((problem) => `${problem.file ?? 'the shader build'}: ${problem.message}`)
|
|
143
|
+
.join('\n');
|
|
144
|
+
}
|
|
145
|
+
let compiled;
|
|
146
|
+
let calls;
|
|
147
|
+
/**
|
|
148
|
+
* The compiled module, from the file that `bun run build` writes. The first call compiles it, and
|
|
149
|
+
* throws when the file is missing.
|
|
150
|
+
*/
|
|
151
|
+
export function compilerModule() {
|
|
152
|
+
if (compiled)
|
|
153
|
+
return compiled;
|
|
154
|
+
// A copy of the file's bytes, in memory of its own that WebAssembly accepts under every lib.
|
|
155
|
+
let bytes;
|
|
156
|
+
try {
|
|
157
|
+
bytes = new Uint8Array(readFileSync(SHADER_COMPILER_URL));
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
throw new Error(`null3D: the shader compiler is missing from ${fileURLToPath(SHADER_COMPILER_URL)}. Reinstall @null3d/vite-plugin; in a copy of the engine's source, run bun run build first.`);
|
|
161
|
+
}
|
|
162
|
+
compiled = new WebAssembly.Module(bytes);
|
|
163
|
+
return compiled;
|
|
164
|
+
}
|
|
165
|
+
/** Runs one export on a request on this thread. The first call compiles the module. */
|
|
166
|
+
function call(name, request) {
|
|
167
|
+
calls ??= new CompilerCalls(compilerModule());
|
|
168
|
+
return JSON.parse(calls.call(name, JSON.stringify(request)));
|
|
169
|
+
}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/** The WGSL bind group and binding of a resource. */
|
|
2
|
+
export interface ShaderBinding {
|
|
3
|
+
readonly group: number;
|
|
4
|
+
readonly binding: number;
|
|
5
|
+
}
|
|
6
|
+
/** A uniform block of one GLSL shader, and the WGSL uniform buffer it stands for. */
|
|
7
|
+
export interface GlslUniformBlock extends ShaderBinding {
|
|
8
|
+
/** The block name, for `getUniformBlockIndex`. */
|
|
9
|
+
readonly name: string;
|
|
10
|
+
}
|
|
11
|
+
/** A texture uniform of one GLSL shader, which joins a WGSL texture and its sampler. */
|
|
12
|
+
export interface GlslTexture extends ShaderBinding {
|
|
13
|
+
/** The uniform name, for `getUniformLocation`. */
|
|
14
|
+
readonly name: string;
|
|
15
|
+
/** The sampler that the shader samples the texture with, or null when it only loads texels. */
|
|
16
|
+
readonly sampler: ShaderBinding | null;
|
|
17
|
+
}
|
|
18
|
+
/** One GLSL ES 3.00 shader, and the names that its resources have in it. */
|
|
19
|
+
export interface GlslStage {
|
|
20
|
+
readonly source: string;
|
|
21
|
+
readonly uniformBlocks: readonly GlslUniformBlock[];
|
|
22
|
+
readonly textures: readonly GlslTexture[];
|
|
23
|
+
}
|
|
24
|
+
/** The shaders of one render pipeline, to link into one WebGL2 program. */
|
|
25
|
+
export interface GlslProgram {
|
|
26
|
+
readonly vertex: GlslStage;
|
|
27
|
+
readonly fragment: GlslStage;
|
|
28
|
+
}
|
|
29
|
+
/** The WGSL entry points of one render pipeline. */
|
|
30
|
+
export interface WgslPipeline {
|
|
31
|
+
readonly vertex: string;
|
|
32
|
+
readonly fragment: string;
|
|
33
|
+
}
|
|
34
|
+
/** One WGSL module for WebGPU, and the entry points of its render pipelines. */
|
|
35
|
+
export interface WgslShader<Pipeline extends string = string> {
|
|
36
|
+
readonly source: string;
|
|
37
|
+
readonly pipelines: Readonly<Record<Pipeline, WgslPipeline>>;
|
|
38
|
+
}
|
|
39
|
+
/** One variant of a shader, with its output for each backend it targets. */
|
|
40
|
+
export interface ShaderVariant<Pipeline extends string = string> {
|
|
41
|
+
/**
|
|
42
|
+
* The permutation bits that the variant was built with, as a render pipeline's permutation
|
|
43
|
+
* word holds them: 0 for a variant without permutation bits.
|
|
44
|
+
*/
|
|
45
|
+
readonly permutation: number;
|
|
46
|
+
/** WGSL for WebGPU, or null when the variant does not target WebGPU. */
|
|
47
|
+
readonly wgsl: WgslShader<Pipeline> | null;
|
|
48
|
+
/** GLSL programs for WebGL2 by pipeline, or null when the variant does not target WebGL2. */
|
|
49
|
+
readonly glsl: Readonly<Record<Pipeline, GlslProgram>> | null;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* WGSL from a project's modules, compiled by the null3D Vite plugin: a `.wgsl` file that a module
|
|
53
|
+
* imports, or a template literal that a `wgsl` block comment tags. WGSL with entry points is a
|
|
54
|
+
* whole shader. WGSL without entry points holds the functions of a custom material, the function
|
|
55
|
+
* of a custom effect, a custom tone curve, or a sky layer.
|
|
56
|
+
*/
|
|
57
|
+
export type CompiledWgsl = CompiledShader | CompiledMaterial | CompiledEffect | CompiledToneCurve | CompiledSkyLayer;
|
|
58
|
+
/**
|
|
59
|
+
* A whole shader from a project's modules. It has one render pipeline for each `@fragment` entry
|
|
60
|
+
* point, named after it, with the shader's `@vertex` entry point.
|
|
61
|
+
*/
|
|
62
|
+
export interface CompiledShader {
|
|
63
|
+
/** Marks a whole shader. */
|
|
64
|
+
readonly kind: 'shader';
|
|
65
|
+
/** The shader for WebGPU, with every `#import null3d::...` resolved. */
|
|
66
|
+
readonly webgpu: ShaderVariant;
|
|
67
|
+
/**
|
|
68
|
+
* The shader for WebGL2, compiled with the shader def `WEBGL2`: one GLSL ES 3.00 program for
|
|
69
|
+
* each render pipeline. Null when the shader has only compute entry points.
|
|
70
|
+
*/
|
|
71
|
+
readonly webgl2: ShaderVariant | null;
|
|
72
|
+
/** On the dev server, the key that the plugin sends this WGSL's hot updates under. */
|
|
73
|
+
readonly hot?: string;
|
|
74
|
+
}
|
|
75
|
+
/** A type that a uniform of a custom material can have, as a field of its `struct Uniforms`. */
|
|
76
|
+
export type UniformType = 'f32' | 'i32' | 'u32' | 'vec2f' | 'vec3f' | 'vec4f';
|
|
77
|
+
/** A uniform of a custom material, and where the engine writes its value. */
|
|
78
|
+
export interface CompiledUniform<Name extends string = string, Type extends UniformType = UniformType> {
|
|
79
|
+
/** The field's name, which `set()` and the `uniforms` option take. */
|
|
80
|
+
readonly name: Name;
|
|
81
|
+
/** The field's type. */
|
|
82
|
+
readonly type: Type;
|
|
83
|
+
/** The float of the material's row of custom values where the uniform starts. */
|
|
84
|
+
readonly offset: number;
|
|
85
|
+
}
|
|
86
|
+
/** A texture of a custom material or effect, and where the engine writes its layer. */
|
|
87
|
+
export interface CompiledTexture<Name extends string = string> {
|
|
88
|
+
/** The variable's name, which the `textures` option takes. */
|
|
89
|
+
readonly name: Name;
|
|
90
|
+
/** The float of the material's row of custom values, or of the effect's uniforms, that holds
|
|
91
|
+
* the texture's layer. */
|
|
92
|
+
readonly offset: number;
|
|
93
|
+
/** True for a declaration of a texture array, `var name: texture_2d_array<f32>;`, whose
|
|
94
|
+
* layers the WGSL picks. */
|
|
95
|
+
readonly array?: boolean;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* A custom material from a project's modules, which `materials.shader` draws with: functions such
|
|
99
|
+
* as `fn surface`, built into every variant of the engine's standard material, or a full shader,
|
|
100
|
+
* whose `@vertex` entry point takes an `InstanceIn` from `null3d::mesh`. `Uniforms` gives each
|
|
101
|
+
* uniform's type by name, and `Textures` the names of its textures, as the declaration that the
|
|
102
|
+
* plugin writes beside a `.wgsl` file does, so that `materials.shader` checks the names and values
|
|
103
|
+
* of its `uniforms` and `textures` options and of `set()`.
|
|
104
|
+
*/
|
|
105
|
+
export interface CompiledMaterial<Uniforms extends Readonly<Record<string, UniformType>> = Readonly<Record<string, UniformType>>, Textures extends string = string> {
|
|
106
|
+
/** Marks the WGSL of a custom material. */
|
|
107
|
+
readonly kind: 'material';
|
|
108
|
+
/**
|
|
109
|
+
* True for a draw shader: its `@vertex` entry point takes the vertex and instance indices and
|
|
110
|
+
* no mesh attribute, and the material draws with a vertex count instead of a mesh.
|
|
111
|
+
*/
|
|
112
|
+
readonly meshless?: boolean;
|
|
113
|
+
/** The functions that the WGSL declares for the engine to call, such as `surface`. */
|
|
114
|
+
readonly functions: readonly string[];
|
|
115
|
+
/** The fields of the WGSL's `struct Uniforms`, in order. */
|
|
116
|
+
readonly uniforms: readonly {
|
|
117
|
+
readonly [Name in keyof Uniforms & string]: CompiledUniform<Name, Uniforms[Name]>;
|
|
118
|
+
}[keyof Uniforms & string][];
|
|
119
|
+
/** The textures that the WGSL declares as `var name: texture_2d<f32>;` or
|
|
120
|
+
* `var name: texture_2d_array<f32>;`, in order. */
|
|
121
|
+
readonly textures: readonly CompiledTexture<Textures>[];
|
|
122
|
+
/**
|
|
123
|
+
* The standard material's variants with the WGSL's functions, or a full shader's, by name. A
|
|
124
|
+
* module's value holds none: its builds load on first use, through `builds`.
|
|
125
|
+
*/
|
|
126
|
+
readonly variants: Readonly<Record<string, ShaderVariant>>;
|
|
127
|
+
/**
|
|
128
|
+
* Imports one set of the material's builds, which load on first use: those of a GPU path
|
|
129
|
+
* (`'wgsl'` or `'glsl'`) whose skinning bit and the bits that a device fixes are those of
|
|
130
|
+
* `set`. The engine imports its device's set when it first uses the material, and another set
|
|
131
|
+
* when a pipeline needs one. A page never downloads the sets of other devices.
|
|
132
|
+
*/
|
|
133
|
+
readonly builds?: (target: 'wgsl' | 'glsl', set: number) => Promise<Readonly<Record<string, ShaderVariant>>>;
|
|
134
|
+
/** In a module's value, every permutation bit of the material's builds, joined. */
|
|
135
|
+
readonly permutations?: number;
|
|
136
|
+
/** The vertex shader locations that the vertex stage reads from a mesh's vertices. */
|
|
137
|
+
readonly locations: readonly number[];
|
|
138
|
+
/** The optional vertex attributes that those locations read, as the engine's format bits. */
|
|
139
|
+
readonly attributes: number;
|
|
140
|
+
/** True when the shader reads the material's base color and opacity, as the template does. */
|
|
141
|
+
readonly baseColor: boolean;
|
|
142
|
+
/** True when the WGSL reads an instance's index or an object's id. */
|
|
143
|
+
readonly objectIds: boolean;
|
|
144
|
+
/** True when the WGSL reads the copy of the scene's depth through `sceneDepth`. */
|
|
145
|
+
readonly sceneDepth: boolean;
|
|
146
|
+
/**
|
|
147
|
+
* True when the WGSL sets or reads the surface's alpha, so the material has the builds that cut
|
|
148
|
+
* its own shape: alpha to coverage, the alpha hash, and a masked shadow caster and motion.
|
|
149
|
+
*/
|
|
150
|
+
readonly alpha: boolean;
|
|
151
|
+
/**
|
|
152
|
+
* On the dev server, the key that the plugin sends this WGSL's hot updates under. The engine
|
|
153
|
+
* swaps the shader of each material made from it when an update comes.
|
|
154
|
+
*/
|
|
155
|
+
readonly hot?: string;
|
|
156
|
+
}
|
|
157
|
+
/** The top-level items that a custom effect's or tone curve's piece adds to a WGSL host. */
|
|
158
|
+
export interface WgslPiece {
|
|
159
|
+
readonly items: readonly string[];
|
|
160
|
+
/** The function that the engine's chain or hook calls. */
|
|
161
|
+
readonly run: string;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* The top-level items that a piece adds to a GLSL fragment shader of a host, and the uniform
|
|
165
|
+
* blocks and textures that it adds or reads differently.
|
|
166
|
+
*/
|
|
167
|
+
export interface GlslPiece extends WgslPiece {
|
|
168
|
+
readonly uniformBlocks: readonly GlslUniformBlock[];
|
|
169
|
+
readonly textures: readonly GlslTexture[];
|
|
170
|
+
}
|
|
171
|
+
/** One build of a piece, for each backend that the host's build targets. */
|
|
172
|
+
export interface ShaderPiece {
|
|
173
|
+
/** The permutation bits of the host's build that the piece joins. */
|
|
174
|
+
readonly permutation: number;
|
|
175
|
+
readonly wgsl: WgslPiece | null;
|
|
176
|
+
readonly glsl: GlslPiece | null;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* The pieces that the engine joins into its hosts at run time, by the name of the host's build:
|
|
180
|
+
* for the host of joined effects, and for the final pass that effects and a tone curve fold into.
|
|
181
|
+
*/
|
|
182
|
+
export interface EffectPieces {
|
|
183
|
+
readonly group: Readonly<Record<string, ShaderPiece>>;
|
|
184
|
+
readonly fold: Readonly<Record<string, ShaderPiece>>;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* A custom effect from a project's modules, which `post.addEffect` and a full-screen pass of
|
|
188
|
+
* `render.addPass` draw with: WGSL that declares `fn effect(input: EffectInput) -> vec4f`, built
|
|
189
|
+
* into every variant of the engine's effect template. `Uniforms` gives each uniform's type by
|
|
190
|
+
* name, and `Textures` the names of its textures, as the declaration that the plugin writes beside
|
|
191
|
+
* a `.wgsl` file does, so that the calls check the names and values of the uniforms and textures.
|
|
192
|
+
*/
|
|
193
|
+
export interface CompiledEffect<Uniforms extends Readonly<Record<string, UniformType>> = Readonly<Record<string, UniformType>>, Textures extends string = string> {
|
|
194
|
+
/** Marks the WGSL of a custom effect. */
|
|
195
|
+
readonly kind: 'effect';
|
|
196
|
+
/** The fields of the WGSL's `struct Uniforms`, in order. */
|
|
197
|
+
readonly uniforms: readonly {
|
|
198
|
+
readonly [Name in keyof Uniforms & string]: CompiledUniform<Name, Uniforms[Name]>;
|
|
199
|
+
}[keyof Uniforms & string][];
|
|
200
|
+
/** True when the effect reads the scene's depth. */
|
|
201
|
+
readonly depth: boolean;
|
|
202
|
+
/** True when the effect reads a level of bloom's chain, which `bloomLevel` names. */
|
|
203
|
+
readonly bloom: boolean;
|
|
204
|
+
/** True when the effect reads its input image at its own pixel alone and declares no
|
|
205
|
+
* textures, so it joins the effect before it. */
|
|
206
|
+
readonly joins: boolean;
|
|
207
|
+
/** The textures that the WGSL declares as `var name: texture_2d<f32>;` or
|
|
208
|
+
* `var name: texture_2d_array<f32>;`, in order. */
|
|
209
|
+
readonly textures: readonly CompiledTexture<Textures>[];
|
|
210
|
+
/**
|
|
211
|
+
* The effect template's variants with the WGSL, by name. A module's value holds none: they load
|
|
212
|
+
* on first use, through `builds`.
|
|
213
|
+
*/
|
|
214
|
+
readonly variants: Readonly<Record<string, ShaderVariant>>;
|
|
215
|
+
/**
|
|
216
|
+
* Imports an effect's builds for one GPU path, which load on first use. The engine imports them
|
|
217
|
+
* when it first uses the effect. The set is 0, as an effect's build takes no permutation bit
|
|
218
|
+
* that picks a set.
|
|
219
|
+
*/
|
|
220
|
+
readonly builds?: (target: 'wgsl' | 'glsl', set: number) => Promise<Readonly<Record<string, ShaderVariant>>>;
|
|
221
|
+
/** The pieces that the engine joins into its hosts: none for an effect with textures. */
|
|
222
|
+
readonly pieces: EffectPieces;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* A custom tone curve from a project's modules, which `post.set({ toneMapping })` takes: WGSL that
|
|
226
|
+
* declares `fn toneCurve(color: vec3f) -> vec3f`, built into every variant of the engine's final
|
|
227
|
+
* pass.
|
|
228
|
+
*/
|
|
229
|
+
export interface CompiledToneCurve {
|
|
230
|
+
/** Marks the WGSL of a custom tone curve. */
|
|
231
|
+
readonly kind: 'toneCurve';
|
|
232
|
+
/**
|
|
233
|
+
* The final pass's variants with the WGSL, by name. A module's value holds none: they load on
|
|
234
|
+
* first use, through `builds`.
|
|
235
|
+
*/
|
|
236
|
+
readonly variants: Readonly<Record<string, ShaderVariant>>;
|
|
237
|
+
/**
|
|
238
|
+
* Imports a tone curve's builds for one GPU path, which load on first use. The engine imports
|
|
239
|
+
* them when it first uses the curve. The set is 0, as a curve's build takes no permutation bit
|
|
240
|
+
* that picks a set.
|
|
241
|
+
*/
|
|
242
|
+
readonly builds?: (target: 'wgsl' | 'glsl', set: number) => Promise<Readonly<Record<string, ShaderVariant>>>;
|
|
243
|
+
/** The pieces that the engine folds into the final pass. */
|
|
244
|
+
readonly pieces: EffectPieces;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* A sky layer from a project's modules, which `scene.setSkyLayer` draws over the background: WGSL
|
|
248
|
+
* that declares `fn skyLayer(input: SkyLayerInput) -> SkyLayer`, built into every variant of the
|
|
249
|
+
* engine's sky-layer template and of the shader that fills a sky map's cube faces.
|
|
250
|
+
*/
|
|
251
|
+
export interface CompiledSkyLayer {
|
|
252
|
+
/** Marks the WGSL of a sky layer. */
|
|
253
|
+
readonly kind: 'skyLayer';
|
|
254
|
+
/** The sky-layer template's variants with the WGSL, by name. */
|
|
255
|
+
readonly variants: Readonly<Record<string, ShaderVariant>>;
|
|
256
|
+
/**
|
|
257
|
+
* The sky-map shader's variants with the WGSL, by name: the draws that fill the cube faces of
|
|
258
|
+
* the sky's environment map, which make its reflections and its diffuse light.
|
|
259
|
+
*/
|
|
260
|
+
readonly map: Readonly<Record<string, ShaderVariant>>;
|
|
261
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// The records of a compiled shader. The engine's generated shader module declares the same
|
|
2
|
+
// records, so the engine takes the shader compiler's output as it is. This file imports nothing,
|
|
3
|
+
// so the types of `.wgsl` imports in `client.d.ts` hold in projects for browsers too.
|
|
4
|
+
export {};
|
package/lib/wgsl.d.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { type ShaderCompiler, type ShaderProblem } from './shader-compiler.js';
|
|
2
|
+
import type { CompiledWgsl } from './shader-types.js';
|
|
3
|
+
/** The block comment that tags a template literal as WGSL, with any spacing inside it. */
|
|
4
|
+
export declare const WGSL_TAG: RegExp;
|
|
5
|
+
/** A template literal that a `wgsl` block comment tags, in a module's code. */
|
|
6
|
+
export interface TaggedWgsl {
|
|
7
|
+
/** The offset of the literal's opening backtick in the code. */
|
|
8
|
+
readonly start: number;
|
|
9
|
+
/** The offset just after the literal's closing backtick. */
|
|
10
|
+
readonly end: number;
|
|
11
|
+
/** The WGSL between the backticks, with Windows line ends made plain. */
|
|
12
|
+
readonly source: string;
|
|
13
|
+
/** The offset of the first `${` in the literal, or null when it has no substitution. */
|
|
14
|
+
readonly substitution: number | null;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The template literals that a `wgsl` block comment tags in a script module. Only a real comment
|
|
18
|
+
* directly before a real template literal counts, so the same text inside a string or a line
|
|
19
|
+
* comment does not. Code that does not parse gives none: Vite reports its syntax error.
|
|
20
|
+
*/
|
|
21
|
+
export declare function findTaggedWgsl(code: string, file: string): TaggedWgsl[];
|
|
22
|
+
/** A place in a text: a 1-based line, and a 1-based column in UTF-16 code units. */
|
|
23
|
+
export interface Place {
|
|
24
|
+
readonly line: number;
|
|
25
|
+
readonly column: number;
|
|
26
|
+
}
|
|
27
|
+
/** The place of an offset in a text. */
|
|
28
|
+
export declare function placeOf(text: string, offset: number): Place;
|
|
29
|
+
/**
|
|
30
|
+
* The result of compiling WGSL from a project: the shader with its warnings, or the problems that
|
|
31
|
+
* stopped it.
|
|
32
|
+
*/
|
|
33
|
+
export type WgslCompile = {
|
|
34
|
+
readonly ok: true;
|
|
35
|
+
readonly shader: CompiledWgsl;
|
|
36
|
+
readonly warnings: readonly ShaderProblem[];
|
|
37
|
+
/** The builds that the compile made, which the warnings' variants name. */
|
|
38
|
+
readonly builds: readonly string[];
|
|
39
|
+
} | {
|
|
40
|
+
readonly ok: false;
|
|
41
|
+
readonly problems: readonly ShaderProblem[];
|
|
42
|
+
/** The builds that the compile tried, which the problems' variants name. */
|
|
43
|
+
readonly builds: readonly string[];
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
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 functions build into every
|
|
48
|
+
* variant of the engine's standard material, and a custom effect's or tone curve's into its
|
|
49
|
+
* template. `path` names the file in messages, and `hint` ends the message about WGSL that is
|
|
50
|
+
* none of these. `compiler` runs the compile, on this thread by default.
|
|
51
|
+
*/
|
|
52
|
+
export declare function compileWgsl(path: string, source: string, hint: string, compiler?: ShaderCompiler): Promise<WgslCompile>;
|
|
53
|
+
/** Where WGSL starts in the file that holds it: the file as messages name it, and a place. */
|
|
54
|
+
export interface WgslOrigin extends Place {
|
|
55
|
+
readonly path: string;
|
|
56
|
+
}
|
|
57
|
+
/** An error in the form that Vite and Rolldown show with its place and the code around it. */
|
|
58
|
+
export interface WgslError {
|
|
59
|
+
readonly message: string;
|
|
60
|
+
/** The module that the error stopped. */
|
|
61
|
+
readonly id: string;
|
|
62
|
+
/**
|
|
63
|
+
* The place to show. Its column counts from 1, as editors and the message do, although Rollup
|
|
64
|
+
* counts its own columns from 0: Vite and Rolldown only print the place, and Vite's overlay
|
|
65
|
+
* opens the editor at it.
|
|
66
|
+
*/
|
|
67
|
+
readonly loc: {
|
|
68
|
+
readonly file: string;
|
|
69
|
+
readonly line: number;
|
|
70
|
+
readonly column: number;
|
|
71
|
+
};
|
|
72
|
+
readonly frame: string;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The error for WGSL that did not compile. It lists every problem, and shows the first one in the
|
|
76
|
+
* WGSL's own file, or else the WGSL's start. `id` is the file's module id, and `text` its text.
|
|
77
|
+
*/
|
|
78
|
+
export declare function wgslError(failed: Extract<WgslCompile, {
|
|
79
|
+
ok: false;
|
|
80
|
+
}>, source: string, origin: WgslOrigin, id: string, text: string): WgslError;
|
|
81
|
+
/**
|
|
82
|
+
* Compiled WGSL with its warnings, or the error that stops the module that holds it. The shader
|
|
83
|
+
* works on every browser that the warnings do not name.
|
|
84
|
+
*/
|
|
85
|
+
export type ShaderOrError = {
|
|
86
|
+
readonly shader: CompiledWgsl;
|
|
87
|
+
readonly warnings: readonly string[];
|
|
88
|
+
} | {
|
|
89
|
+
readonly error: WgslError;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* 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
|
+
*/
|
|
95
|
+
export declare function compileWgslFile(path: string, id: string, text: string, compiler?: ShaderCompiler): Promise<ShaderOrError>;
|
|
96
|
+
/**
|
|
97
|
+
* Compiles one tagged template literal of a script module, whose code is `code`: the compiled
|
|
98
|
+
* WGSL that takes its place, or the error that stops the module.
|
|
99
|
+
*/
|
|
100
|
+
export declare function compileLiteral(literal: TaggedWgsl, code: string, id: string, path: string, compiler?: ShaderCompiler): Promise<ShaderOrError>;
|
|
101
|
+
/** A tagged template literal of a module, and the compiled WGSL that takes its place. */
|
|
102
|
+
export interface TaggedShader {
|
|
103
|
+
readonly start: number;
|
|
104
|
+
readonly end: number;
|
|
105
|
+
readonly shader: CompiledWgsl;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Compiles each tagged template literal in a script module, all at once, with every literal's
|
|
109
|
+
* warnings, or gives the error that stops the module at its first literal that does not compile.
|
|
110
|
+
*/
|
|
111
|
+
export declare function compileTaggedWgsl(code: string, id: string, path: string, compiler?: ShaderCompiler): Promise<{
|
|
112
|
+
readonly shaders: readonly TaggedShader[];
|
|
113
|
+
readonly warnings: readonly string[];
|
|
114
|
+
} | {
|
|
115
|
+
readonly error: WgslError;
|
|
116
|
+
}>;
|
|
117
|
+
/** Lines of text around a place, each with its number, and a caret under the place's column. */
|
|
118
|
+
export declare function codeFrame(text: string, line: number, column: number): string;
|