@ignifx/core 0.2.1 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/async-pipelines-CI5uT5iZ.js +26 -0
- package/dist/async-pipelines-CI5uT5iZ.js.map +1 -0
- package/dist/color-DrR6GFTd.js +351 -0
- package/dist/color-DrR6GFTd.js.map +1 -0
- package/dist/effect-task-CMbuO6Kb.js +205 -0
- package/dist/effect-task-CMbuO6Kb.js.map +1 -0
- package/dist/environment-C_JfuAjj.js +224 -0
- package/dist/environment-C_JfuAjj.js.map +1 -0
- package/dist/ignifx-error-R1ma_Lzv.js +544 -0
- package/dist/ignifx-error-R1ma_Lzv.js.map +1 -0
- package/dist/index.d.ts +3756 -1656
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6404 -5777
- package/dist/index.js.map +1 -1
- package/dist/instanced-mesh-instance-Cgx1H87p.js +701 -0
- package/dist/instanced-mesh-instance-Cgx1H87p.js.map +1 -0
- package/dist/material-plugin-CTaWZXEk.js +485 -0
- package/dist/material-plugin-CTaWZXEk.js.map +1 -0
- package/dist/math-utils-BcDnL-gi.js +277 -0
- package/dist/math-utils-BcDnL-gi.js.map +1 -0
- package/dist/mesh-CnK_PX46.js +811 -0
- package/dist/mesh-CnK_PX46.js.map +1 -0
- package/dist/post-effect-C42XJFmo.js +155 -0
- package/dist/post-effect-C42XJFmo.js.map +1 -0
- package/dist/post-effect-compiler-DTlkT1si.js +311 -0
- package/dist/post-effect-compiler-DTlkT1si.js.map +1 -0
- package/dist/shader-declaration-5q__jycG.js +165 -0
- package/dist/shader-declaration-5q__jycG.js.map +1 -0
- package/dist/shader-material-DssOqsvZ.js +1009 -0
- package/dist/shader-material-DssOqsvZ.js.map +1 -0
- package/dist/shader-material-Xk7Oc9UG.js +207 -0
- package/dist/shader-material-Xk7Oc9UG.js.map +1 -0
- package/dist/shader-pragma-CNAK4SIb.js +685 -0
- package/dist/shader-pragma-CNAK4SIb.js.map +1 -0
- package/dist/surface-shader-C2HtysNN.js +1397 -0
- package/dist/surface-shader-C2HtysNN.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,1009 @@
|
|
|
1
|
+
import { o as CoreErrorCode, t as IgnifxError } from "./ignifx-error-R1ma_Lzv.js";
|
|
2
|
+
import { t as Color } from "./color-DrR6GFTd.js";
|
|
3
|
+
import { n as IGNIFX_UNIFORM_TYPES, r as LITE_SYSTEM_UNIFORM_NAMES, t as IGNIFX_UNIFORM_NAMES } from "./shader-declaration-5q__jycG.js";
|
|
4
|
+
//#region \0rolldown/runtime.js
|
|
5
|
+
var __defProp = Object.defineProperty;
|
|
6
|
+
var __exportAll = (all, no_symbols) => {
|
|
7
|
+
let target = {};
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true
|
|
11
|
+
});
|
|
12
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
13
|
+
return target;
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/render/shader-material.ts
|
|
17
|
+
var shader_material_exports = /* @__PURE__ */ __exportAll({
|
|
18
|
+
ShaderMaterialRegistry: () => ShaderMaterialRegistry,
|
|
19
|
+
ShaderMaterialState: () => ShaderMaterialState,
|
|
20
|
+
buildShaderMaterialState: () => buildShaderMaterialState,
|
|
21
|
+
loadShaderMaterialAdapter: () => loadShaderMaterialAdapter,
|
|
22
|
+
shaderMaterialAdapter: () => shaderMaterialAdapter,
|
|
23
|
+
shaderMaterialsOf: () => shaderMaterialsOf,
|
|
24
|
+
toShaderMaterialOptions: () => toShaderMaterialOptions
|
|
25
|
+
});
|
|
26
|
+
/**
|
|
27
|
+
* The `"shader"` material family (`docs/plan/2026-09-terrain-particles-shaders.md` §3.1): a
|
|
28
|
+
* `ShaderAsset` plus the values a material sets on it.
|
|
29
|
+
*
|
|
30
|
+
* Nothing here imports Babylon Lite: {@link toShaderMaterialOptions} is the whole mapping onto the
|
|
31
|
+
* shape `createShaderMaterial` takes, as plain data, and the adapter itself arrives through
|
|
32
|
+
* {@link loadShaderMaterialAdapter} so a project that loads no `.wgsl` never fetches that chunk
|
|
33
|
+
* (`./shader-support.ts`).
|
|
34
|
+
*
|
|
35
|
+
* Two Lite rules shape the design:
|
|
36
|
+
*
|
|
37
|
+
* - A declared sampler must have something bound (error 309), so an unbound declared texture gets
|
|
38
|
+
* the declaration's 1x1 fallback and `setTexture(name, null)` restores it rather than unbinding.
|
|
39
|
+
* - A declared storage buffer must have something bound too (error 310), and `array<T>` has no 1x1
|
|
40
|
+
* equivalent, so such a material is not drawable until `setStorageBuffer` binds one.
|
|
41
|
+
*/
|
|
42
|
+
/** The uniform names Babylon Lite and ignifx write themselves, which a material may not set. */
|
|
43
|
+
const ENGINE_UNIFORM_NAMES = Object.freeze([...LITE_SYSTEM_UNIFORM_NAMES, ...IGNIFX_UNIFORM_NAMES]);
|
|
44
|
+
/** How many floats the `PreRender` system's frame buffer holds: direction, colour, ambient. */
|
|
45
|
+
const FRAME_FLOATS = 9;
|
|
46
|
+
/** Where the main light's direction starts inside the frame buffer. */
|
|
47
|
+
const DIRECTION_OFFSET = 0;
|
|
48
|
+
/** Where the main light's colour starts inside the frame buffer. */
|
|
49
|
+
const LIGHT_COLOR_OFFSET = 3;
|
|
50
|
+
/** Where the ambient colour starts inside the frame buffer. */
|
|
51
|
+
const AMBIENT_OFFSET = 6;
|
|
52
|
+
/** How many elements each uniform type holds. */
|
|
53
|
+
const ELEMENT_COUNTS = Object.freeze({
|
|
54
|
+
f32: 1,
|
|
55
|
+
u32: 1,
|
|
56
|
+
i32: 1,
|
|
57
|
+
"vec2<f32>": 2,
|
|
58
|
+
"vec3<f32>": 3,
|
|
59
|
+
"vec4<f32>": 4,
|
|
60
|
+
"mat4x4<f32>": 16
|
|
61
|
+
});
|
|
62
|
+
/**
|
|
63
|
+
* The premultiplied-alpha blend state, which Babylon Lite's two named modes cannot express.
|
|
64
|
+
*
|
|
65
|
+
* @remarks
|
|
66
|
+
* `blendMode: "alpha"` compiles `srcFactor: "src-alpha"` (`lib/material/shader/shader-pipeline.js`),
|
|
67
|
+
* which multiplies the source colour by its alpha a second time. A premultiplied surface has
|
|
68
|
+
* already done that, so it needs `srcFactor: "one"` — the state that lets additive and over-blended
|
|
69
|
+
* particles share one draw.
|
|
70
|
+
*/
|
|
71
|
+
const PREMULTIPLIED_BLEND = Object.freeze({
|
|
72
|
+
color: Object.freeze({
|
|
73
|
+
srcFactor: "one",
|
|
74
|
+
dstFactor: "one-minus-src-alpha",
|
|
75
|
+
operation: "add"
|
|
76
|
+
}),
|
|
77
|
+
alpha: Object.freeze({
|
|
78
|
+
srcFactor: "one",
|
|
79
|
+
dstFactor: "one-minus-src-alpha",
|
|
80
|
+
operation: "add"
|
|
81
|
+
})
|
|
82
|
+
});
|
|
83
|
+
/**
|
|
84
|
+
* Maps a shader file's declaration and a material's `define` overrides onto the shape
|
|
85
|
+
* `createShaderMaterial` takes.
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* Pure data in, pure data out, so every mapping rule is asserted headlessly:
|
|
89
|
+
*
|
|
90
|
+
* - `blend opaque` leaves blending off; `alpha` and `additive` are Lite's two named modes;
|
|
91
|
+
* `premultiplied` supplies an explicit `GPUBlendState`, because Lite's `"alpha"` mode multiplies
|
|
92
|
+
* by alpha a second time.
|
|
93
|
+
* - `depthWrite` is passed explicitly, because the declaration already resolved Lite's
|
|
94
|
+
* "blended defaults to off unless stated" rule.
|
|
95
|
+
* - `depthTest off` becomes `depthCompare: "always"`; `depthTest on` passes `null`, which leaves
|
|
96
|
+
* Lite's reverse-Z default in place.
|
|
97
|
+
* - `instancing matrices` and `matrices-colors` differ only in `useThinInstanceColors`; which
|
|
98
|
+
* pipeline layout a mesh draws with is Lite's decision at build time, from whether the mesh has a
|
|
99
|
+
* thin-instance colour stream.
|
|
100
|
+
* - A colour uniform's declared default is sRGB and is decoded here, which is the one place the
|
|
101
|
+
* conversion happens for defaults.
|
|
102
|
+
*
|
|
103
|
+
* @param name - The material's name, for GPU debug labels.
|
|
104
|
+
* @param source - The whole `.wgsl` file.
|
|
105
|
+
* @param declaration - What the file's pragmas declared.
|
|
106
|
+
* @param uniforms - The initial value of every custom uniform, keyed by name, already **linear**.
|
|
107
|
+
* @param defines - The resolved `define` values.
|
|
108
|
+
* @returns The options.
|
|
109
|
+
*
|
|
110
|
+
* @internal
|
|
111
|
+
*/
|
|
112
|
+
function toShaderMaterialOptions(name, source, declaration, uniforms, defines) {
|
|
113
|
+
const entries = [];
|
|
114
|
+
for (const system of declaration.system) entries.push(system);
|
|
115
|
+
for (const ignifx of declaration.ignifx) {
|
|
116
|
+
const type = IGNIFX_UNIFORM_TYPES[ignifx];
|
|
117
|
+
entries.push({
|
|
118
|
+
name: ignifx,
|
|
119
|
+
type,
|
|
120
|
+
defaultValue: zeros(ELEMENT_COUNTS[type])
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
for (const uniform of declaration.uniforms) entries.push({
|
|
124
|
+
name: uniform.name,
|
|
125
|
+
type: uniform.type,
|
|
126
|
+
defaultValue: Array.from(uniforms.get(uniform.name) ?? uniformStore(uniform))
|
|
127
|
+
});
|
|
128
|
+
const samplers = [];
|
|
129
|
+
for (const texture of declaration.textures) samplers.push({
|
|
130
|
+
name: texture.name,
|
|
131
|
+
viewDimension: texture.array ? "2d-array" : "2d"
|
|
132
|
+
});
|
|
133
|
+
const storageBuffers = [];
|
|
134
|
+
for (const storage of declaration.storage) storageBuffers.push({
|
|
135
|
+
name: storage.name,
|
|
136
|
+
type: storage.type
|
|
137
|
+
});
|
|
138
|
+
const pipeline = declaration.pipeline;
|
|
139
|
+
const blended = pipeline.blend !== "opaque";
|
|
140
|
+
return {
|
|
141
|
+
name,
|
|
142
|
+
source,
|
|
143
|
+
attributes: declaration.attributes,
|
|
144
|
+
uniforms: entries,
|
|
145
|
+
samplers,
|
|
146
|
+
storageBuffers,
|
|
147
|
+
defines,
|
|
148
|
+
useThinInstanceColors: pipeline.instancing === "matrices-colors",
|
|
149
|
+
needAlphaBlending: blended,
|
|
150
|
+
blendMode: pipeline.blend === "additive" ? "additive" : "alpha",
|
|
151
|
+
blend: pipeline.blend === "premultiplied" ? PREMULTIPLIED_BLEND : null,
|
|
152
|
+
transmissive: pipeline.transmissive,
|
|
153
|
+
backFaceCulling: pipeline.cull === "back",
|
|
154
|
+
depthWrite: pipeline.depthWrite,
|
|
155
|
+
depthCompare: pipeline.depthTest ? null : "always"
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/** The dynamically imported Babylon Lite adapter, memoised for the process. */
|
|
159
|
+
let adapter = null;
|
|
160
|
+
/**
|
|
161
|
+
* Loads the custom-WGSL half of the Babylon Lite adapter, once per process.
|
|
162
|
+
*
|
|
163
|
+
* @remarks
|
|
164
|
+
* The shader **loader** awaits this before it hands back a `ShaderAsset`, which is what makes
|
|
165
|
+
* {@link shaderMaterialAdapter} safe to call from the synchronous material builders. Loading it also
|
|
166
|
+
* turns on Lite's cached uniform serialization plans, which is what makes the per-frame `time`
|
|
167
|
+
* upload rewrite only the slots that changed.
|
|
168
|
+
*
|
|
169
|
+
* @returns A promise that resolves once the adapter is in place.
|
|
170
|
+
*
|
|
171
|
+
* @internal
|
|
172
|
+
*/
|
|
173
|
+
async function loadShaderMaterialAdapter() {
|
|
174
|
+
if (adapter !== null) return;
|
|
175
|
+
const loaded = await import("./shader-material-Xk7Oc9UG.js");
|
|
176
|
+
loaded.enableShaderUniformCaching();
|
|
177
|
+
adapter = loaded;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* The adapter, if it has been loaded.
|
|
181
|
+
*
|
|
182
|
+
* @returns The module, or `null` before any `.wgsl` has been loaded.
|
|
183
|
+
*
|
|
184
|
+
* @internal
|
|
185
|
+
*/
|
|
186
|
+
function shaderMaterialAdapter() {
|
|
187
|
+
return adapter;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* The adapter, or the failure a material built before the `.wgsl` loader ran produces.
|
|
191
|
+
*
|
|
192
|
+
* @param shader - The shader being built, for the message.
|
|
193
|
+
* @returns The adapter.
|
|
194
|
+
* @throws IgnifxError with code `IGX-0715` when the chunk has not been loaded.
|
|
195
|
+
*/
|
|
196
|
+
function requireShaderMaterialAdapter(shader) {
|
|
197
|
+
const module = shaderMaterialAdapter();
|
|
198
|
+
if (module === null) throw new IgnifxError(CoreErrorCode.shaderCompileFailed, `${shader.address} cannot be built because the shader adapter has not been loaded.`, {
|
|
199
|
+
context: {
|
|
200
|
+
asset: shader.address,
|
|
201
|
+
message: "adapter not loaded"
|
|
202
|
+
},
|
|
203
|
+
hint: "Load the .wgsl through app.assets, which is what loads the adapter."
|
|
204
|
+
});
|
|
205
|
+
return module;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* The per-app state the shader material family needs: the 1x1 fallback textures declared samplers
|
|
209
|
+
* fall back to, and the list of live materials whose per-frame ignifx uniforms have to be written.
|
|
210
|
+
*
|
|
211
|
+
* @remarks
|
|
212
|
+
* It hangs off the app through a lazily created `WeakMap` rather than a service, so a shader
|
|
213
|
+
* material works whether or not an extension registered anything, and two apps in one process keep
|
|
214
|
+
* separate state (`CONSTITUTION.md` §3.6).
|
|
215
|
+
*
|
|
216
|
+
* @internal
|
|
217
|
+
*/
|
|
218
|
+
var ShaderMaterialRegistry = class {
|
|
219
|
+
#app;
|
|
220
|
+
/** Live materials that declare at least one ignifx uniform, in no particular order. */
|
|
221
|
+
#animated = [];
|
|
222
|
+
#fallbacks = /* @__PURE__ */ new Map();
|
|
223
|
+
/**
|
|
224
|
+
* Creates the registry.
|
|
225
|
+
*
|
|
226
|
+
* @param app - The app it belongs to.
|
|
227
|
+
*/
|
|
228
|
+
constructor(app) {
|
|
229
|
+
this.#app = app;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* The materials the `PreRender` uniform system has to write this frame.
|
|
233
|
+
*
|
|
234
|
+
* @returns The list; the registry's own array, iterated without copying.
|
|
235
|
+
*/
|
|
236
|
+
get animated() {
|
|
237
|
+
return this.#animated;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Starts writing a material's ignifx uniforms every frame.
|
|
241
|
+
*
|
|
242
|
+
* @param state - The material.
|
|
243
|
+
*/
|
|
244
|
+
add(state) {
|
|
245
|
+
if (!this.#animated.includes(state)) this.#animated.push(state);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Stops writing a material's ignifx uniforms.
|
|
249
|
+
*
|
|
250
|
+
* @param state - The material.
|
|
251
|
+
*/
|
|
252
|
+
remove(state) {
|
|
253
|
+
const index = this.#animated.indexOf(state);
|
|
254
|
+
if (index < 0) return;
|
|
255
|
+
const last = this.#animated.pop();
|
|
256
|
+
if (last !== void 0 && index < this.#animated.length) this.#animated[index] = last;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* The 1x1 texture a declared-but-unbound sampler is filled with, created on first use and shared
|
|
260
|
+
* for the life of the app.
|
|
261
|
+
*
|
|
262
|
+
* @param declaration - The sampler declaration, for its `default` and `array` flags.
|
|
263
|
+
* @returns The texture, or `null` under a headless app or before the adapter has loaded.
|
|
264
|
+
*/
|
|
265
|
+
fallbackTexture(declaration) {
|
|
266
|
+
const module = shaderMaterialAdapter();
|
|
267
|
+
if (module === null || this.#app.isHeadless) return null;
|
|
268
|
+
return module.resolveFallbackTexture(this.#app.lite.engine, this.#fallbacks, declaration.fallback, declaration.array);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
/** The per-app registries, created on first use so module scope allocates nothing. */
|
|
272
|
+
let registries = null;
|
|
273
|
+
/**
|
|
274
|
+
* The shader material state of one app.
|
|
275
|
+
*
|
|
276
|
+
* @param app - The app.
|
|
277
|
+
* @returns Its registry, created on first use.
|
|
278
|
+
*
|
|
279
|
+
* @internal
|
|
280
|
+
*/
|
|
281
|
+
function shaderMaterialsOf(app) {
|
|
282
|
+
const map = registries ??= /* @__PURE__ */ new WeakMap();
|
|
283
|
+
const existing = map.get(app);
|
|
284
|
+
if (existing !== void 0) return existing;
|
|
285
|
+
const created = new ShaderMaterialRegistry(app);
|
|
286
|
+
map.set(app, created);
|
|
287
|
+
return created;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* One live `"shader"` material: the Babylon Lite material, the CPU-side copy of every value written
|
|
291
|
+
* to it, and the bindings it has to replay when the shader file or a `define` changes.
|
|
292
|
+
*
|
|
293
|
+
* @remarks
|
|
294
|
+
* `MaterialAsset` owns one of these and forwards `setUniform`, `getUniform`, `setTexture`,
|
|
295
|
+
* `setDefine`, and `setStorageBuffer` to it. It is `@internal` because the public surface is the
|
|
296
|
+
* material's methods, not this object.
|
|
297
|
+
*
|
|
298
|
+
* @internal
|
|
299
|
+
*/
|
|
300
|
+
var ShaderMaterialState = class {
|
|
301
|
+
/** The definition the material was built from; a clone replays it. */
|
|
302
|
+
definition;
|
|
303
|
+
#app;
|
|
304
|
+
#values = /* @__PURE__ */ new Map();
|
|
305
|
+
#textures = /* @__PURE__ */ new Map();
|
|
306
|
+
#storage = /* @__PURE__ */ new Map();
|
|
307
|
+
#defines = /* @__PURE__ */ new Map();
|
|
308
|
+
/** The last light values written, so an unchanged light costs no upload. */
|
|
309
|
+
#light = new Float32Array(FRAME_FLOATS);
|
|
310
|
+
/** The ignifx-provided uniforms the file declares, paired with their stores. */
|
|
311
|
+
#engineSlots = [];
|
|
312
|
+
#shader;
|
|
313
|
+
#material;
|
|
314
|
+
/**
|
|
315
|
+
* The adapter every write goes through. Captured once, because a state can only be built after
|
|
316
|
+
* {@link buildLiteShaderMaterial} has proved the chunk is in memory.
|
|
317
|
+
*/
|
|
318
|
+
#adapter;
|
|
319
|
+
#hasLight = false;
|
|
320
|
+
#hasWarnedAboutLight = false;
|
|
321
|
+
#onReplaced = null;
|
|
322
|
+
/**
|
|
323
|
+
* Builds the material. Use {@link buildShaderMaterialState}.
|
|
324
|
+
*
|
|
325
|
+
* @param app - The app that owns the engine, the log, and the registry.
|
|
326
|
+
* @param shader - The shader the material sets values on.
|
|
327
|
+
* @param definition - The declaration.
|
|
328
|
+
* @param material - The already-built Babylon Lite material.
|
|
329
|
+
* @param liteAdapter - The custom-WGSL adapter every write goes through.
|
|
330
|
+
*/
|
|
331
|
+
constructor(app, shader, definition, material, liteAdapter) {
|
|
332
|
+
this.#app = app;
|
|
333
|
+
this.#adapter = liteAdapter;
|
|
334
|
+
this.#shader = shader;
|
|
335
|
+
this.definition = definition;
|
|
336
|
+
this.#material = material;
|
|
337
|
+
this.#light.fill(NaN);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* The shader the material is values for.
|
|
341
|
+
*
|
|
342
|
+
* @returns The shader asset, which a hot reload replaces.
|
|
343
|
+
*/
|
|
344
|
+
get shader() {
|
|
345
|
+
return this.#shader;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* The Babylon Lite material, which `setDefine` and a hot reload replace with a new object.
|
|
349
|
+
*
|
|
350
|
+
* @returns The material.
|
|
351
|
+
*/
|
|
352
|
+
get material() {
|
|
353
|
+
return this.#material;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Whether the shader declares any ignifx-provided uniform, and so needs a per-frame write.
|
|
357
|
+
*
|
|
358
|
+
* @returns `true` when the `PreRender` system has work to do for this material.
|
|
359
|
+
*/
|
|
360
|
+
get isAnimated() {
|
|
361
|
+
return this.#shader.declaration.ignifx.length > 0;
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Whether every declared storage buffer has a buffer bound, which is what Babylon Lite needs
|
|
365
|
+
* before the material can be drawn at all.
|
|
366
|
+
*
|
|
367
|
+
* @returns `true` when the material is drawable.
|
|
368
|
+
*/
|
|
369
|
+
get isDrawable() {
|
|
370
|
+
const declared = this.#shader.declaration.storage;
|
|
371
|
+
for (let index = 0; index < declared.length; index += 1) {
|
|
372
|
+
const name = declared[index]?.name;
|
|
373
|
+
if (name !== void 0 && (this.#storage.get(name) ?? null) === null) return false;
|
|
374
|
+
}
|
|
375
|
+
return true;
|
|
376
|
+
}
|
|
377
|
+
/**
|
|
378
|
+
* Writes a declared uniform.
|
|
379
|
+
*
|
|
380
|
+
* @param name - The uniform's name.
|
|
381
|
+
* @param value - The value: a number, a numeric array, or — for a `color` uniform — an sRGB
|
|
382
|
+
* colour, which is uploaded linear.
|
|
383
|
+
* @throws IgnifxError with code `IGX-0712` when the shader declares no such uniform or the engine
|
|
384
|
+
* writes it, or `IGX-0713` when the value has the wrong shape.
|
|
385
|
+
*/
|
|
386
|
+
setUniform(name, value) {
|
|
387
|
+
const declaration = this.#writableUniform(name);
|
|
388
|
+
const store = this.#values.get(name) ?? uniformStore(declaration);
|
|
389
|
+
this.#values.set(name, store);
|
|
390
|
+
writeUniformValue(store, declaration, value, this.definition.name);
|
|
391
|
+
this.#write(name, store);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Reads the current CPU-side value of a declared uniform.
|
|
395
|
+
*
|
|
396
|
+
* @param name - The uniform's name.
|
|
397
|
+
* @param out - Receives the value for a vector or matrix uniform; a fresh array is allocated when
|
|
398
|
+
* it is omitted.
|
|
399
|
+
* @returns The number for a scalar uniform, or the filled array.
|
|
400
|
+
* @throws IgnifxError with code `IGX-0712` when the shader declares no such uniform, or
|
|
401
|
+
* `IGX-0713` when `out` is too short.
|
|
402
|
+
*/
|
|
403
|
+
getUniform(name, out) {
|
|
404
|
+
const declaration = this.#uniform(name);
|
|
405
|
+
const store = this.#values.get(name) ?? uniformStore(declaration);
|
|
406
|
+
this.#values.set(name, store);
|
|
407
|
+
if (store.length === 1 && out === void 0) return store[0] ?? 0;
|
|
408
|
+
if (out === void 0) return Float32Array.from(store);
|
|
409
|
+
if (out.length < store.length) throw new IgnifxError(CoreErrorCode.shaderValueMismatch, `${name} on ${this.definition.name} expects ${String(store.length)} floats, not ${String(out.length)}.`, {
|
|
410
|
+
context: {
|
|
411
|
+
name,
|
|
412
|
+
material: this.definition.name,
|
|
413
|
+
expected: store.length,
|
|
414
|
+
actual: out.length
|
|
415
|
+
},
|
|
416
|
+
hint: "Pass an out array at least as long as the uniform's element count."
|
|
417
|
+
});
|
|
418
|
+
out.set(store);
|
|
419
|
+
return out;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Binds a texture to a declared sampler, or restores the declaration's fallback.
|
|
423
|
+
*
|
|
424
|
+
* @param name - The sampler's name.
|
|
425
|
+
* @param texture - The texture, or `null` for the declared 1x1 fallback.
|
|
426
|
+
* @throws IgnifxError with code `IGX-0712` when the shader declares no such sampler.
|
|
427
|
+
*/
|
|
428
|
+
setTexture(name, texture) {
|
|
429
|
+
const declaration = this.#texture(name);
|
|
430
|
+
this.#textures.set(name, texture);
|
|
431
|
+
this.#bindTexture(declaration, texture);
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Binds a storage buffer to a declared binding.
|
|
435
|
+
*
|
|
436
|
+
* @param name - The binding's name.
|
|
437
|
+
* @param buffer - The buffer, or `null` to unbind. A material with an unbound declared storage
|
|
438
|
+
* buffer cannot be drawn.
|
|
439
|
+
* @throws IgnifxError with code `IGX-0712` when the shader declares no such storage buffer.
|
|
440
|
+
*/
|
|
441
|
+
setStorageBuffer(name, buffer) {
|
|
442
|
+
const declared = this.#shader.declaration.storage;
|
|
443
|
+
let found = false;
|
|
444
|
+
for (let index = 0; index < declared.length; index += 1) if (declared[index]?.name === name) {
|
|
445
|
+
found = true;
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
if (!found) throw this.#unknownBinding(name, "storage buffer");
|
|
449
|
+
this.#storage.set(name, buffer);
|
|
450
|
+
this.#bindStorage(name, buffer);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Overrides a declared `define` and rebuilds the Babylon Lite material, because a define is a
|
|
454
|
+
* WGSL `const` baked into the compiled pipeline.
|
|
455
|
+
*
|
|
456
|
+
* @remarks
|
|
457
|
+
* The rebuild replaces the Lite material **object**. `MeshRenderer` re-reads
|
|
458
|
+
* `MaterialAsset.lite.material` every `PreRender` and writes it onto its mesh when it changed,
|
|
459
|
+
* and Lite's `mesh.material` setter enqueues a material swap that the next frame drains
|
|
460
|
+
* (`lib/scene/mesh-scene-registry.js`), so the new pipeline is picked up on the following frame
|
|
461
|
+
* with no explicit topology flag.
|
|
462
|
+
*
|
|
463
|
+
* @param name - The define's name.
|
|
464
|
+
* @param value - The new value.
|
|
465
|
+
* @throws IgnifxError with code `IGX-0712` when the shader declares no such define.
|
|
466
|
+
*/
|
|
467
|
+
setDefine(name, value) {
|
|
468
|
+
const declared = this.#shader.declaration.defines;
|
|
469
|
+
let fileValue;
|
|
470
|
+
for (let index = 0; index < declared.length; index += 1) {
|
|
471
|
+
const definition = declared[index];
|
|
472
|
+
if (definition?.name === name) {
|
|
473
|
+
fileValue = definition.value;
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
if (fileValue === void 0) throw this.#unknownBinding(name, "define");
|
|
478
|
+
if ((this.#defines.get(name) ?? fileValue) === value) return;
|
|
479
|
+
this.#defines.set(name, value);
|
|
480
|
+
this.#rebuild(this.#shader);
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Writes this frame's ignifx uniforms. The `PreRender` system calls it.
|
|
484
|
+
*
|
|
485
|
+
* @param time - Scaled seconds since `app.start()`.
|
|
486
|
+
* @param unscaledTime - Unscaled seconds since `app.start()`.
|
|
487
|
+
* @param deltaTime - Scaled seconds since the previous frame.
|
|
488
|
+
* @param light - Nine floats: the main light's world direction, its linear colour times its
|
|
489
|
+
* intensity, and the ambient colour.
|
|
490
|
+
* @param hasLight - Whether an enabled directional light supplied the first six.
|
|
491
|
+
*/
|
|
492
|
+
writeFrameUniforms(time, unscaledTime, deltaTime, light, hasLight) {
|
|
493
|
+
let lightChanged = this.#hasLight !== hasLight;
|
|
494
|
+
this.#hasLight = hasLight;
|
|
495
|
+
for (let index = 0; index < this.#light.length; index += 1) if (this.#light[index] !== light[index]) {
|
|
496
|
+
this.#light[index] = light[index] ?? 0;
|
|
497
|
+
lightChanged = true;
|
|
498
|
+
}
|
|
499
|
+
const slots = this.#engineSlots;
|
|
500
|
+
for (let index = 0; index < slots.length; index += 1) {
|
|
501
|
+
const slot = slots[index];
|
|
502
|
+
if (slot === void 0) continue;
|
|
503
|
+
switch (slot.name) {
|
|
504
|
+
case "time":
|
|
505
|
+
this.#writeScalar(slot.name, slot.store, time);
|
|
506
|
+
break;
|
|
507
|
+
case "unscaledTime":
|
|
508
|
+
this.#writeScalar(slot.name, slot.store, unscaledTime);
|
|
509
|
+
break;
|
|
510
|
+
case "deltaTime":
|
|
511
|
+
this.#writeScalar(slot.name, slot.store, deltaTime);
|
|
512
|
+
break;
|
|
513
|
+
case "mainLightDirection":
|
|
514
|
+
if (lightChanged) this.#writeTriple(slot.name, slot.store, light, DIRECTION_OFFSET);
|
|
515
|
+
break;
|
|
516
|
+
case "mainLightColor":
|
|
517
|
+
if (lightChanged) this.#writeTriple(slot.name, slot.store, light, LIGHT_COLOR_OFFSET);
|
|
518
|
+
break;
|
|
519
|
+
case "ambientColor": if (lightChanged) this.#writeTriple(slot.name, slot.store, light, AMBIENT_OFFSET);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
if (!hasLight && !this.#hasWarnedAboutLight && this.#needsLight()) {
|
|
523
|
+
this.#hasWarnedAboutLight = true;
|
|
524
|
+
const declared = this.#engineSlots.map((slot) => slot.name).join(", ");
|
|
525
|
+
this.#app.log.warnOnce(`${CoreErrorCode.shaderLightUniformWithoutLight}:${this.definition.name}`, `${CoreErrorCode.shaderLightUniformWithoutLight}: ${this.definition.name} declares ${declared}, but this world has no enabled directional light; zeros are uploaded.`);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Replays the definition's values, textures, defines, and storage bindings onto a freshly built
|
|
530
|
+
* Babylon Lite material, and connects the hot-reload hook. Called once, by
|
|
531
|
+
* {@link buildShaderMaterialState}.
|
|
532
|
+
*
|
|
533
|
+
* @param textures - The texture handles the loader resolved, in declaration order.
|
|
534
|
+
*/
|
|
535
|
+
initialise(textures) {
|
|
536
|
+
const declaration = this.#shader.declaration;
|
|
537
|
+
for (const name of Object.keys(this.definition.defines)) {
|
|
538
|
+
const value = this.definition.defines[name];
|
|
539
|
+
if (value !== void 0) this.#defines.set(name, value);
|
|
540
|
+
}
|
|
541
|
+
for (const name of Object.keys(this.definition.values)) {
|
|
542
|
+
const value = this.definition.values[name];
|
|
543
|
+
if (value !== void 0) this.setUniform(name, value);
|
|
544
|
+
}
|
|
545
|
+
const addresses = Object.keys(this.definition.textures);
|
|
546
|
+
for (let index = 0; index < declaration.textures.length; index += 1) {
|
|
547
|
+
const declared = declaration.textures[index];
|
|
548
|
+
if (declared === void 0) continue;
|
|
549
|
+
const slot = addresses.indexOf(declared.name);
|
|
550
|
+
const handle = slot < 0 ? void 0 : textures[slot];
|
|
551
|
+
this.#textures.set(declared.name, handle ?? null);
|
|
552
|
+
this.#bindTexture(declared, handle ?? null);
|
|
553
|
+
}
|
|
554
|
+
this.#refreshEngineSlots();
|
|
555
|
+
if (this.isAnimated) shaderMaterialsOf(this.#app).add(this);
|
|
556
|
+
const source = this.#app.assets.get(this.definition.shader);
|
|
557
|
+
this.#onReplaced = source?.onReplaced.connect((value) => {
|
|
558
|
+
this.#reload(value);
|
|
559
|
+
}) ?? null;
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Stops the per-frame writes and the hot-reload hook. The `material` asset type's `unload` runs
|
|
563
|
+
* it when the last holder releases the material.
|
|
564
|
+
*/
|
|
565
|
+
dispose() {
|
|
566
|
+
this.#onReplaced?.();
|
|
567
|
+
this.#onReplaced = null;
|
|
568
|
+
shaderMaterialsOf(this.#app).remove(this);
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Rebuilds the Lite material for a replaced shader file, keeping the previous one on failure.
|
|
572
|
+
*
|
|
573
|
+
* @param shader - The re-parsed shader asset.
|
|
574
|
+
*/
|
|
575
|
+
#reload(shader) {
|
|
576
|
+
try {
|
|
577
|
+
this.#rebuild(shader);
|
|
578
|
+
this.#shader = shader;
|
|
579
|
+
} catch (error) {
|
|
580
|
+
this.#app.onError.emit({
|
|
581
|
+
error: new IgnifxError(CoreErrorCode.shaderCompileFailed, `${shader.address} failed to rebuild; the previous material stays in use. ${String(error)}`, {
|
|
582
|
+
context: {
|
|
583
|
+
asset: shader.address,
|
|
584
|
+
message: String(error)
|
|
585
|
+
},
|
|
586
|
+
cause: error,
|
|
587
|
+
hint: "Fix the shader file; the running material is the last one that built."
|
|
588
|
+
}),
|
|
589
|
+
source: "asset",
|
|
590
|
+
phase: null,
|
|
591
|
+
entity: null,
|
|
592
|
+
component: null
|
|
593
|
+
});
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Builds a new Lite material from a declaration and replays every binding that still exists.
|
|
598
|
+
*
|
|
599
|
+
* @param shader - The shader to build from.
|
|
600
|
+
*/
|
|
601
|
+
#rebuild(shader) {
|
|
602
|
+
const declaration = shader.declaration;
|
|
603
|
+
const previous = this.#shader;
|
|
604
|
+
const previousMaterial = this.#material;
|
|
605
|
+
const previousValues = new Map(this.#values);
|
|
606
|
+
this.#shader = shader;
|
|
607
|
+
try {
|
|
608
|
+
const values = /* @__PURE__ */ new Map();
|
|
609
|
+
for (let index = 0; index < declaration.uniforms.length; index += 1) {
|
|
610
|
+
const uniform = declaration.uniforms[index];
|
|
611
|
+
if (uniform === void 0) continue;
|
|
612
|
+
const kept = this.#values.get(uniform.name);
|
|
613
|
+
values.set(uniform.name, kept !== void 0 && kept.length === ELEMENT_COUNTS[uniform.type] ? kept : uniformStore(uniform));
|
|
614
|
+
}
|
|
615
|
+
this.#material = buildLiteShaderMaterial(this.definition.name, shader, values, this.#resolveDefines(shader));
|
|
616
|
+
this.#values.clear();
|
|
617
|
+
for (const [name, store] of values) this.#values.set(name, store);
|
|
618
|
+
this.#rebind(declaration);
|
|
619
|
+
} catch (error) {
|
|
620
|
+
this.#shader = previous;
|
|
621
|
+
this.#material = previousMaterial;
|
|
622
|
+
this.#values.clear();
|
|
623
|
+
for (const [name, store] of previousValues) this.#values.set(name, store);
|
|
624
|
+
throw error;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Re-applies the texture and storage bindings that the new declaration still has, dropping the
|
|
629
|
+
* ones it no longer declares.
|
|
630
|
+
*
|
|
631
|
+
* @param declaration - The new declaration.
|
|
632
|
+
*/
|
|
633
|
+
#rebind(declaration) {
|
|
634
|
+
const textures = new Map(this.#textures);
|
|
635
|
+
this.#textures.clear();
|
|
636
|
+
for (let index = 0; index < declaration.textures.length; index += 1) {
|
|
637
|
+
const declared = declaration.textures[index];
|
|
638
|
+
if (declared === void 0) continue;
|
|
639
|
+
const handle = textures.get(declared.name) ?? null;
|
|
640
|
+
this.#textures.set(declared.name, handle);
|
|
641
|
+
this.#bindTexture(declared, handle);
|
|
642
|
+
}
|
|
643
|
+
const storage = new Map(this.#storage);
|
|
644
|
+
this.#storage.clear();
|
|
645
|
+
for (let index = 0; index < declaration.storage.length; index += 1) {
|
|
646
|
+
const name = declaration.storage[index]?.name;
|
|
647
|
+
if (name === void 0) continue;
|
|
648
|
+
const handle = storage.get(name) ?? null;
|
|
649
|
+
this.#storage.set(name, handle);
|
|
650
|
+
this.#bindStorage(name, handle);
|
|
651
|
+
}
|
|
652
|
+
this.#refreshEngineSlots();
|
|
653
|
+
const registry = shaderMaterialsOf(this.#app);
|
|
654
|
+
if (this.isAnimated) registry.add(this);
|
|
655
|
+
else registry.remove(this);
|
|
656
|
+
this.#light.fill(NaN);
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* The declared `define` values with this material's overrides applied.
|
|
660
|
+
*
|
|
661
|
+
* @param shader - The shader whose declaration supplies the defaults.
|
|
662
|
+
* @returns The resolved values.
|
|
663
|
+
*/
|
|
664
|
+
#resolveDefines(shader) {
|
|
665
|
+
const resolved = {};
|
|
666
|
+
const declared = shader.declaration.defines;
|
|
667
|
+
for (let index = 0; index < declared.length; index += 1) {
|
|
668
|
+
const define = declared[index];
|
|
669
|
+
if (define === void 0) continue;
|
|
670
|
+
resolved[define.name] = this.#defines.get(define.name) ?? define.value;
|
|
671
|
+
}
|
|
672
|
+
return resolved;
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Binds a texture, or the declaration's fallback when there is none.
|
|
676
|
+
*
|
|
677
|
+
* @param declaration - The sampler declaration.
|
|
678
|
+
* @param handle - The texture handle, or `null`.
|
|
679
|
+
*/
|
|
680
|
+
#bindTexture(declaration, handle) {
|
|
681
|
+
const loaded = handle?.state === "loaded" ? handle.value.lite.texture : null;
|
|
682
|
+
this.#adapter.writeShaderTexture(this.#material, declaration.name, loaded ?? shaderMaterialsOf(this.#app).fallbackTexture(declaration));
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Binds a storage buffer.
|
|
686
|
+
*
|
|
687
|
+
* @param name - The binding's name.
|
|
688
|
+
* @param handle - The buffer handle, or `null`.
|
|
689
|
+
*/
|
|
690
|
+
#bindStorage(name, handle) {
|
|
691
|
+
this.#adapter.writeShaderStorageBuffer(this.#material, name, handle?.state === "loaded" ? handle.value.lite.buffer : null);
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Writes one CPU-side value through to Babylon Lite.
|
|
695
|
+
*
|
|
696
|
+
* @param name - The uniform's name.
|
|
697
|
+
* @param store - The value.
|
|
698
|
+
*/
|
|
699
|
+
#write(name, store) {
|
|
700
|
+
this.#adapter.writeShaderUniform(this.#material, name, store);
|
|
701
|
+
}
|
|
702
|
+
/**
|
|
703
|
+
* Writes one engine-owned scalar uniform through its CPU-side store.
|
|
704
|
+
*
|
|
705
|
+
* @param name - The uniform's name.
|
|
706
|
+
* @param store - The store, which is also what `getUniform` reads.
|
|
707
|
+
* @param value - The value.
|
|
708
|
+
*/
|
|
709
|
+
#writeScalar(name, store, value) {
|
|
710
|
+
store[0] = value;
|
|
711
|
+
this.#adapter.writeShaderUniform(this.#material, name, store);
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Writes three floats of an engine-owned `vec3<f32>` uniform, without allocating.
|
|
715
|
+
*
|
|
716
|
+
* @param name - The uniform's name.
|
|
717
|
+
* @param store - The store, which is also what `getUniform` reads.
|
|
718
|
+
* @param source - The nine-float frame buffer.
|
|
719
|
+
* @param offset - Where the triple starts inside it.
|
|
720
|
+
*/
|
|
721
|
+
#writeTriple(name, store, source, offset) {
|
|
722
|
+
store.set(source.subarray(offset, offset + 3));
|
|
723
|
+
this.#adapter.writeShaderUniform(this.#material, name, store);
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Resolves the CPU-side store of every ignifx-provided uniform the file declares, so the
|
|
727
|
+
* per-frame write is an indexed loop with no name lookup (coding standards §7).
|
|
728
|
+
*/
|
|
729
|
+
#refreshEngineSlots() {
|
|
730
|
+
const slots = this.#engineSlots;
|
|
731
|
+
slots.length = 0;
|
|
732
|
+
const declared = this.#shader.declaration.ignifx;
|
|
733
|
+
for (let index = 0; index < declared.length; index += 1) {
|
|
734
|
+
const name = declared[index];
|
|
735
|
+
if (name === void 0) continue;
|
|
736
|
+
const count = ELEMENT_COUNTS[IGNIFX_UNIFORM_TYPES[name]];
|
|
737
|
+
const store = this.#values.get(name) ?? new Float32Array(count);
|
|
738
|
+
this.#values.set(name, store);
|
|
739
|
+
slots.push({
|
|
740
|
+
name,
|
|
741
|
+
store
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Whether the shader declares a uniform that needs a directional light.
|
|
747
|
+
*
|
|
748
|
+
* @returns `true` when `mainLightDirection` or `mainLightColor` is declared.
|
|
749
|
+
*/
|
|
750
|
+
#needsLight() {
|
|
751
|
+
const declared = this.#shader.declaration.ignifx;
|
|
752
|
+
for (let index = 0; index < declared.length; index += 1) {
|
|
753
|
+
const name = declared[index];
|
|
754
|
+
if (name === "mainLightDirection" || name === "mainLightColor") return true;
|
|
755
|
+
}
|
|
756
|
+
return false;
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Looks a custom uniform declaration up.
|
|
760
|
+
*
|
|
761
|
+
* @param name - The uniform's name.
|
|
762
|
+
* @returns The declaration.
|
|
763
|
+
* @throws IgnifxError with code `IGX-0712`.
|
|
764
|
+
*/
|
|
765
|
+
#uniform(name) {
|
|
766
|
+
const custom = this.#customUniform(name);
|
|
767
|
+
if (custom !== null) return custom;
|
|
768
|
+
const engine = this.#engineUniform(name);
|
|
769
|
+
if (engine !== null) return engine;
|
|
770
|
+
throw this.#unknownBinding(name, "uniform");
|
|
771
|
+
}
|
|
772
|
+
/**
|
|
773
|
+
* Looks a custom uniform declaration up, refusing the ones the engine writes.
|
|
774
|
+
*
|
|
775
|
+
* @param name - The uniform's name.
|
|
776
|
+
* @returns The declaration.
|
|
777
|
+
* @throws IgnifxError with code `IGX-0712`.
|
|
778
|
+
*/
|
|
779
|
+
#writableUniform(name) {
|
|
780
|
+
const custom = this.#customUniform(name);
|
|
781
|
+
if (custom !== null) return custom;
|
|
782
|
+
if (ENGINE_UNIFORM_NAMES.includes(name)) throw new IgnifxError(CoreErrorCode.unknownShaderBinding, `${name} on ${this.definition.name} is written by the engine every frame and cannot be set.`, {
|
|
783
|
+
context: {
|
|
784
|
+
material: this.definition.name,
|
|
785
|
+
name
|
|
786
|
+
},
|
|
787
|
+
hint: "System uniforms come from Babylon Lite and the ignifx uniforms from the PreRender system."
|
|
788
|
+
});
|
|
789
|
+
throw this.#unknownBinding(name, "uniform");
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* The file's own declaration of a uniform, if it declares one by that name.
|
|
793
|
+
*
|
|
794
|
+
* @param name - The uniform's name.
|
|
795
|
+
* @returns The declaration, or `null`.
|
|
796
|
+
*/
|
|
797
|
+
#customUniform(name) {
|
|
798
|
+
const declared = this.#shader.declaration.uniforms;
|
|
799
|
+
for (let index = 0; index < declared.length; index += 1) {
|
|
800
|
+
const uniform = declared[index];
|
|
801
|
+
if (uniform !== void 0 && uniform.name === name) return uniform;
|
|
802
|
+
}
|
|
803
|
+
return null;
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
* The synthetic declaration of an ignifx-provided uniform the file declared, so `getUniform` can
|
|
807
|
+
* read back what the `PreRender` system wrote.
|
|
808
|
+
*
|
|
809
|
+
* @param name - The uniform's name.
|
|
810
|
+
* @returns The declaration, or `null` when the file declares no such engine uniform.
|
|
811
|
+
*/
|
|
812
|
+
#engineUniform(name) {
|
|
813
|
+
const declared = this.#shader.declaration.ignifx;
|
|
814
|
+
for (let index = 0; index < declared.length; index += 1) {
|
|
815
|
+
const ignifx = declared[index];
|
|
816
|
+
if (ignifx !== void 0 && ignifx === name) return {
|
|
817
|
+
name: ignifx,
|
|
818
|
+
type: IGNIFX_UNIFORM_TYPES[ignifx],
|
|
819
|
+
defaultValue: zeros(ELEMENT_COUNTS[IGNIFX_UNIFORM_TYPES[ignifx]]),
|
|
820
|
+
color: false,
|
|
821
|
+
range: null,
|
|
822
|
+
step: null,
|
|
823
|
+
tooltip: null
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
return null;
|
|
827
|
+
}
|
|
828
|
+
/**
|
|
829
|
+
* Looks a sampler declaration up.
|
|
830
|
+
*
|
|
831
|
+
* @param name - The sampler's name.
|
|
832
|
+
* @returns The declaration.
|
|
833
|
+
* @throws IgnifxError with code `IGX-0712`.
|
|
834
|
+
*/
|
|
835
|
+
#texture(name) {
|
|
836
|
+
const declared = this.#shader.declaration.textures;
|
|
837
|
+
for (let index = 0; index < declared.length; index += 1) {
|
|
838
|
+
const texture = declared[index];
|
|
839
|
+
if (texture !== void 0 && texture.name === name) return texture;
|
|
840
|
+
}
|
|
841
|
+
throw this.#unknownBinding(name, "texture");
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
* Builds the `IGX-0712` failure an undeclared binding name produces.
|
|
845
|
+
*
|
|
846
|
+
* @param name - The name that was asked for.
|
|
847
|
+
* @param what - What kind of binding was expected.
|
|
848
|
+
* @returns The error to throw.
|
|
849
|
+
*/
|
|
850
|
+
#unknownBinding(name, what) {
|
|
851
|
+
return new IgnifxError(CoreErrorCode.unknownShaderBinding, `${this.definition.name} declares no ${what} named ${name}.`, {
|
|
852
|
+
context: {
|
|
853
|
+
material: this.definition.name,
|
|
854
|
+
name
|
|
855
|
+
},
|
|
856
|
+
hint: `Declare it in ${this.#shader.address} with an // @ignifx ${what} pragma.`
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
};
|
|
860
|
+
/**
|
|
861
|
+
* Builds the live state of a `"shader"` material: the Babylon Lite material, then every value,
|
|
862
|
+
* texture, and define the definition names.
|
|
863
|
+
*
|
|
864
|
+
* @param app - The app that owns the engine, the log, and the registry.
|
|
865
|
+
* @param shader - The shader the material sets values on.
|
|
866
|
+
* @param definition - The declaration.
|
|
867
|
+
* @param textures - The texture handles the loader resolved, in the order the definition's
|
|
868
|
+
* `textures` record names them.
|
|
869
|
+
* @returns The state.
|
|
870
|
+
* @throws IgnifxError with code `IGX-0715` when the adapter has not been loaded, `IGX-0712` for an
|
|
871
|
+
* undeclared value or define name, or `IGX-0713` for a value of the wrong shape.
|
|
872
|
+
*
|
|
873
|
+
* @internal
|
|
874
|
+
*/
|
|
875
|
+
function buildShaderMaterialState(app, shader, definition, textures) {
|
|
876
|
+
const defines = {};
|
|
877
|
+
const declared = shader.declaration.defines;
|
|
878
|
+
for (let index = 0; index < declared.length; index += 1) {
|
|
879
|
+
const define = declared[index];
|
|
880
|
+
if (define !== void 0) defines[define.name] = definition.defines[define.name] ?? define.value;
|
|
881
|
+
}
|
|
882
|
+
for (const name of Object.keys(definition.defines)) if (!(name in defines)) throw new IgnifxError(CoreErrorCode.unknownShaderBinding, `${definition.name} declares no define named ${name}.`, {
|
|
883
|
+
context: {
|
|
884
|
+
material: definition.name,
|
|
885
|
+
name
|
|
886
|
+
},
|
|
887
|
+
hint: `Declare it in ${shader.address} with an // @ignifx define pragma.`
|
|
888
|
+
});
|
|
889
|
+
const state = new ShaderMaterialState(app, shader, definition, buildLiteShaderMaterial(definition.name, shader, /* @__PURE__ */ new Map(), defines), requireShaderMaterialAdapter(shader));
|
|
890
|
+
state.initialise(textures);
|
|
891
|
+
return state;
|
|
892
|
+
}
|
|
893
|
+
/**
|
|
894
|
+
* Builds the Babylon Lite material a declaration describes, through the dynamically loaded adapter.
|
|
895
|
+
*
|
|
896
|
+
* @param name - The material's name.
|
|
897
|
+
* @param shader - The shader.
|
|
898
|
+
* @param values - Current custom uniform values to seed the declaration's defaults with.
|
|
899
|
+
* @param defines - The resolved `define` values.
|
|
900
|
+
* @returns The Lite material.
|
|
901
|
+
* @throws IgnifxError with code `IGX-0715` when the adapter has not been loaded yet.
|
|
902
|
+
*/
|
|
903
|
+
function buildLiteShaderMaterial(name, shader, values, defines) {
|
|
904
|
+
return requireShaderMaterialAdapter(shader).createShaderMaterialFromOptions(toShaderMaterialOptions(name, shader.source, shader.declaration, values, defines));
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* A number, or an array of that many zeros, matching what a uniform of that arity starts at.
|
|
908
|
+
*
|
|
909
|
+
* @param count - How many elements the uniform holds.
|
|
910
|
+
* @returns The zero value.
|
|
911
|
+
*/
|
|
912
|
+
function zeros(count) {
|
|
913
|
+
if (count === 1) return 0;
|
|
914
|
+
const values = [];
|
|
915
|
+
for (let index = 0; index < count; index += 1) values.push(0);
|
|
916
|
+
return values;
|
|
917
|
+
}
|
|
918
|
+
/**
|
|
919
|
+
* A fresh CPU-side store for one uniform, filled with its declared default — **linear** for a
|
|
920
|
+
* colour, because that is what the GPU reads.
|
|
921
|
+
*
|
|
922
|
+
* @param declaration - The uniform declaration.
|
|
923
|
+
* @returns The store.
|
|
924
|
+
*/
|
|
925
|
+
function uniformStore(declaration) {
|
|
926
|
+
const count = ELEMENT_COUNTS[declaration.type];
|
|
927
|
+
const store = new Float32Array(count);
|
|
928
|
+
const value = declaration.defaultValue;
|
|
929
|
+
if (typeof value === "number") store[0] = value;
|
|
930
|
+
else store.set(value.slice(0, count));
|
|
931
|
+
if (declaration.color) decodeColourComponents(store);
|
|
932
|
+
return store;
|
|
933
|
+
}
|
|
934
|
+
/**
|
|
935
|
+
* Writes a caller's value into a uniform's CPU-side store, checking its shape and decoding a
|
|
936
|
+
* colour.
|
|
937
|
+
*
|
|
938
|
+
* @param store - The store to write into.
|
|
939
|
+
* @param declaration - The uniform declaration.
|
|
940
|
+
* @param value - The caller's value.
|
|
941
|
+
* @param material - The material's name, for diagnostics.
|
|
942
|
+
* @throws IgnifxError with code `IGX-0713` when the value's shape does not match the declared type.
|
|
943
|
+
*/
|
|
944
|
+
function writeUniformValue(store, declaration, value, material) {
|
|
945
|
+
const count = store.length;
|
|
946
|
+
if (typeof value === "number") {
|
|
947
|
+
if (count !== 1) throw shapeMismatch(declaration, material, `${declaration.type} (${String(count)} floats)`, "a single number");
|
|
948
|
+
store[0] = value;
|
|
949
|
+
return;
|
|
950
|
+
}
|
|
951
|
+
if (isColorLike(value)) {
|
|
952
|
+
if (count !== 3 && count !== 4) throw shapeMismatch(declaration, material, `${declaration.type} (${String(count)} floats)`, "a colour");
|
|
953
|
+
store[0] = Color.srgbToLinear(value.r);
|
|
954
|
+
store[1] = Color.srgbToLinear(value.g);
|
|
955
|
+
store[2] = Color.srgbToLinear(value.b);
|
|
956
|
+
if (count === 4) store[3] = value.a;
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
if (value.length !== count) throw shapeMismatch(declaration, material, `${declaration.type} (${String(count)} floats)`, `${String(value.length)} floats`);
|
|
960
|
+
store.set(value instanceof Float32Array ? value : Float32Array.from(value));
|
|
961
|
+
if (declaration.color) decodeColourComponents(store);
|
|
962
|
+
}
|
|
963
|
+
/**
|
|
964
|
+
* Decodes a store's first three components from sRGB to linear, in place.
|
|
965
|
+
*
|
|
966
|
+
* @remarks
|
|
967
|
+
* A `subarray` view and `map` rather than three indexed reads: a `Float32Array` element is typed
|
|
968
|
+
* `number | undefined` under `noUncheckedIndexedAccess`, and `map` hands the callback a `number`.
|
|
969
|
+
* It runs on a value write, never per frame.
|
|
970
|
+
*
|
|
971
|
+
* @param store - The store, at least three floats long.
|
|
972
|
+
*/
|
|
973
|
+
function decodeColourComponents(store) {
|
|
974
|
+
const rgb = store.subarray(0, 3);
|
|
975
|
+
rgb.set(rgb.map((component) => Color.srgbToLinear(component)));
|
|
976
|
+
}
|
|
977
|
+
/**
|
|
978
|
+
* Whether a value is an sRGB colour rather than a numeric array.
|
|
979
|
+
*
|
|
980
|
+
* @param value - The candidate.
|
|
981
|
+
* @returns `true` for an object carrying `r`, `g`, `b`, and `a`.
|
|
982
|
+
*/
|
|
983
|
+
function isColorLike(value) {
|
|
984
|
+
return !Array.isArray(value) && !(value instanceof Float32Array);
|
|
985
|
+
}
|
|
986
|
+
/**
|
|
987
|
+
* Builds the `IGX-0713` failure a wrongly shaped value produces.
|
|
988
|
+
*
|
|
989
|
+
* @param declaration - The uniform declaration.
|
|
990
|
+
* @param material - The material's name.
|
|
991
|
+
* @param expected - What the uniform takes.
|
|
992
|
+
* @param actual - What the caller passed.
|
|
993
|
+
* @returns The error to throw.
|
|
994
|
+
*/
|
|
995
|
+
function shapeMismatch(declaration, material, expected, actual) {
|
|
996
|
+
return new IgnifxError(CoreErrorCode.shaderValueMismatch, `${declaration.name} on ${material} expects ${expected}, not ${actual}.`, {
|
|
997
|
+
context: {
|
|
998
|
+
name: declaration.name,
|
|
999
|
+
material,
|
|
1000
|
+
expected,
|
|
1001
|
+
actual
|
|
1002
|
+
},
|
|
1003
|
+
hint: "A colour uniform takes a ColorLike or its element count; every other uniform takes its element count."
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
//#endregion
|
|
1007
|
+
export { shader_material_exports as n, shaderMaterialsOf as t };
|
|
1008
|
+
|
|
1009
|
+
//# sourceMappingURL=shader-material-DssOqsvZ.js.map
|