@vcollab/shader-library 0.0.0-stage → 1.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/CHANGELOG.md +56 -0
- package/LICENSE +8 -0
- package/README.md +109 -2
- package/dist/bind.d.ts +53 -0
- package/dist/bundled.cjs +236 -0
- package/dist/bundled.cjs.map +1 -0
- package/dist/bundled.d.ts +6 -0
- package/dist/bundled.js +22 -0
- package/dist/bundled.js.map +1 -0
- package/dist/chunk-ELLNQZJX.js +257 -0
- package/dist/chunk-ELLNQZJX.js.map +1 -0
- package/dist/derived/dispatch.d.ts +80 -0
- package/dist/derived/errors.d.ts +4 -0
- package/dist/derived/index.cjs +451 -0
- package/dist/derived/index.cjs.map +1 -0
- package/dist/derived/index.d.ts +62 -0
- package/dist/derived/index.js +417 -0
- package/dist/derived/index.js.map +1 -0
- package/dist/derived/kernels.d.ts +73 -0
- package/dist/encoding.d.ts +39 -0
- package/dist/index.cjs +617 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +342 -0
- package/dist/index.js.map +1 -0
- package/dist/load.d.ts +60 -0
- package/dist/manifest.d.ts +213 -0
- package/dist/pack.d.ts +69 -0
- package/dist/pick.d.ts +78 -0
- package/dist/select.d.ts +47 -0
- package/dist/version.d.ts +2 -0
- package/package.json +70 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/bind.ts
|
|
4
|
+
var BindingError = class extends Error {
|
|
5
|
+
name = "BindingError";
|
|
6
|
+
};
|
|
7
|
+
var ResourceBindings = class {
|
|
8
|
+
#bySemantic;
|
|
9
|
+
/** Every binding, in the order the envelope records them (units ascending). */
|
|
10
|
+
all;
|
|
11
|
+
constructor(block) {
|
|
12
|
+
this.all = block.bindings ?? [];
|
|
13
|
+
const bySemantic = /* @__PURE__ */ new Map();
|
|
14
|
+
for (const record of this.all) {
|
|
15
|
+
const existing = bySemantic.get(record.semantic);
|
|
16
|
+
if (existing === void 0) bySemantic.set(record.semantic, [record]);
|
|
17
|
+
else existing.push(record);
|
|
18
|
+
}
|
|
19
|
+
this.#bySemantic = bySemantic;
|
|
20
|
+
}
|
|
21
|
+
/** Every semantic this pipeline binds, sorted. */
|
|
22
|
+
get semantics() {
|
|
23
|
+
return [...this.#bySemantic.keys()].sort();
|
|
24
|
+
}
|
|
25
|
+
/** How many texture units the pipeline spends in total. */
|
|
26
|
+
get unitCount() {
|
|
27
|
+
return this.all.length;
|
|
28
|
+
}
|
|
29
|
+
/** Whether the pipeline binds anything for `semantic`. */
|
|
30
|
+
has(semantic) {
|
|
31
|
+
return this.#bySemantic.has(semantic);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Every binding for `semantic` — more than one when the same resource is
|
|
35
|
+
* read in both stages, which the build gives separate units.
|
|
36
|
+
*/
|
|
37
|
+
forSemantic(semantic) {
|
|
38
|
+
return this.#bySemantic.get(semantic) ?? [];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The one binding for `semantic`.
|
|
42
|
+
*
|
|
43
|
+
* @throws {BindingError} if the pipeline binds nothing for it, or binds it
|
|
44
|
+
* in more than one stage — in which case use {@link forSemantic} and bind
|
|
45
|
+
* each.
|
|
46
|
+
*/
|
|
47
|
+
get(semantic) {
|
|
48
|
+
const records = this.forSemantic(semantic);
|
|
49
|
+
if (records.length === 1) return records[0];
|
|
50
|
+
if (records.length === 0) {
|
|
51
|
+
throw new BindingError(
|
|
52
|
+
`this pipeline binds nothing for the semantic ${semantic}; it binds ${this.semantics.join(", ") || "nothing"}.`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
throw new BindingError(
|
|
56
|
+
`${semantic} is bound in ${records.length} stages (${records.map((r) => r.stage).join(", ")}); use forSemantic() and bind each.`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
/** Every binding a given stage spends a unit on. */
|
|
60
|
+
forStage(stage) {
|
|
61
|
+
return this.all.filter((record) => record.stage === stage);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// src/encoding.ts
|
|
66
|
+
var ELEMENTS = {
|
|
67
|
+
float: { kind: "float", width: 1 },
|
|
68
|
+
int: { kind: "int", width: 1 },
|
|
69
|
+
uint: { kind: "uint", width: 1 },
|
|
70
|
+
vec2: { kind: "float", width: 2 },
|
|
71
|
+
vec3: { kind: "float", width: 3 },
|
|
72
|
+
vec4: { kind: "float", width: 4 },
|
|
73
|
+
ivec2: { kind: "int", width: 2 },
|
|
74
|
+
ivec3: { kind: "int", width: 3 },
|
|
75
|
+
ivec4: { kind: "int", width: 4 },
|
|
76
|
+
uvec2: { kind: "uint", width: 2 },
|
|
77
|
+
uvec3: { kind: "uint", width: 3 },
|
|
78
|
+
uvec4: { kind: "uint", width: 4 },
|
|
79
|
+
mat2: { kind: "float", width: 4, order: 2 },
|
|
80
|
+
mat3: { kind: "float", width: 9, order: 3 },
|
|
81
|
+
mat4: { kind: "float", width: 16, order: 4 }
|
|
82
|
+
};
|
|
83
|
+
var ARRAY_SUFFIX = "[]";
|
|
84
|
+
var KNOWN_ENCODINGS = Object.keys(ELEMENTS);
|
|
85
|
+
function describeEncoding(encoding) {
|
|
86
|
+
const isArray = encoding.endsWith(ARRAY_SUFFIX);
|
|
87
|
+
const element = isArray ? encoding.slice(0, -ARRAY_SUFFIX.length) : encoding;
|
|
88
|
+
if (!Object.hasOwn(ELEMENTS, element)) return void 0;
|
|
89
|
+
return { ...ELEMENTS[element], isArray };
|
|
90
|
+
}
|
|
91
|
+
var isWhole = (value) => typeof value === "number" && Number.isInteger(value);
|
|
92
|
+
function layoutProblem(name, layout, blockSize) {
|
|
93
|
+
if (typeof layout !== "object" || layout === null) {
|
|
94
|
+
return `${name} has no layout record.`;
|
|
95
|
+
}
|
|
96
|
+
const { offset, size, elementStride, encoding } = layout;
|
|
97
|
+
if (!isWhole(offset) || offset < 0 || offset % 4 !== 0) {
|
|
98
|
+
return `${name} is at offset ${String(offset)}, which is not a non-negative multiple of 4.`;
|
|
99
|
+
}
|
|
100
|
+
if (!isWhole(size) || size <= 0 || offset + size > blockSize) {
|
|
101
|
+
return `${name} occupies ${String(size)} byte(s) at offset ${offset}, which does not lie within the ${blockSize}-byte block.`;
|
|
102
|
+
}
|
|
103
|
+
if (encoding === null) return void 0;
|
|
104
|
+
if (typeof encoding !== "string") {
|
|
105
|
+
return `${name} has the encoding ${String(encoding)}, which is not a name.`;
|
|
106
|
+
}
|
|
107
|
+
const described = describeEncoding(encoding);
|
|
108
|
+
if (described === void 0) {
|
|
109
|
+
return `${name} has the encoding ${encoding}, which the packer does not know; it writes ${KNOWN_ENCODINGS.join(", ")}, and arrays of them.`;
|
|
110
|
+
}
|
|
111
|
+
const reach = described.order === void 0 ? described.width * 4 : (described.order - 1) * 16 + described.order * 4;
|
|
112
|
+
if (described.isArray) {
|
|
113
|
+
if (!isWhole(elementStride) || elementStride <= 0 || elementStride % 4 !== 0) {
|
|
114
|
+
return `${name} is ${encoding}, but the build reports an element stride of ${String(elementStride)}.`;
|
|
115
|
+
}
|
|
116
|
+
if (reach > elementStride || elementStride > size) {
|
|
117
|
+
return `${name} is ${encoding} with ${size} byte(s) at a ${elementStride}-byte stride, which does not hold one element.`;
|
|
118
|
+
}
|
|
119
|
+
return void 0;
|
|
120
|
+
}
|
|
121
|
+
if (reach > size) {
|
|
122
|
+
return `${name} is ${encoding}, which writes ${reach} byte(s), but the build gives it ${size}.`;
|
|
123
|
+
}
|
|
124
|
+
return void 0;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/manifest.ts
|
|
128
|
+
var SCHEMA_VERSION = 3;
|
|
129
|
+
|
|
130
|
+
// src/load.ts
|
|
131
|
+
var ShaderSetError = class extends Error {
|
|
132
|
+
name = "ShaderSetError";
|
|
133
|
+
};
|
|
134
|
+
function absoluteBase(baseUrl) {
|
|
135
|
+
if (baseUrl instanceof URL) return new URL(baseUrl.href);
|
|
136
|
+
if (URL.canParse(baseUrl)) return new URL(baseUrl);
|
|
137
|
+
const here = globalThis.location?.href;
|
|
138
|
+
if (typeof here === "string" && URL.canParse(baseUrl, here)) return new URL(baseUrl, here);
|
|
139
|
+
throw new ShaderSetError(
|
|
140
|
+
`${JSON.stringify(baseUrl)} is not an absolute URL, and there is no page location to resolve it against. Outside a browser, pass an absolute URL such as https://host/shaders/.`
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
function fetchSource(baseUrl, init) {
|
|
144
|
+
const base = absoluteBase(baseUrl);
|
|
145
|
+
if (!base.pathname.endsWith("/")) base.pathname += "/";
|
|
146
|
+
const read = async (path) => {
|
|
147
|
+
const url = new URL(path, base);
|
|
148
|
+
const response = await fetch(url, init);
|
|
149
|
+
if (!response.ok) {
|
|
150
|
+
throw new ShaderSetError(
|
|
151
|
+
`fetching ${url.href} failed: ${response.status} ${response.statusText}`
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
return response.json();
|
|
155
|
+
};
|
|
156
|
+
return {
|
|
157
|
+
readRegistry: () => read("registry.json"),
|
|
158
|
+
readEnvelope: (entry) => read(entry.envelope)
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function fsSource(directory) {
|
|
162
|
+
const read = async (path) => {
|
|
163
|
+
const [{ readFile }, { join }] = await Promise.all([
|
|
164
|
+
import('fs/promises'),
|
|
165
|
+
import('path')
|
|
166
|
+
]);
|
|
167
|
+
const full = join(directory, ...path.split("/"));
|
|
168
|
+
try {
|
|
169
|
+
return JSON.parse(await readFile(full, "utf8"));
|
|
170
|
+
} catch (cause) {
|
|
171
|
+
throw new ShaderSetError(`reading ${full} failed: ${cause.message}`, { cause });
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
return {
|
|
175
|
+
readRegistry: () => read("registry.json"),
|
|
176
|
+
readEnvelope: (entry) => read(entry.envelope)
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function assertSchema(document, what) {
|
|
180
|
+
if (document.schemaVersion !== SCHEMA_VERSION) {
|
|
181
|
+
throw new ShaderSetError(
|
|
182
|
+
`${what} is schema version ${String(document.schemaVersion)}, and this package reads ${SCHEMA_VERSION}. A document whose shape is not the one expected is refused rather than read with its fields misinterpreted.`
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
var isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
187
|
+
function assertTags(tags, what) {
|
|
188
|
+
if (!Array.isArray(tags) || !tags.every((tag) => typeof tag === "string")) {
|
|
189
|
+
throw new ShaderSetError(`${what} is not an array of capability tags.`);
|
|
190
|
+
}
|
|
191
|
+
const repeated = tags.filter((tag, index) => tags.indexOf(tag) !== index);
|
|
192
|
+
if (repeated.length > 0) {
|
|
193
|
+
throw new ShaderSetError(`${what} repeats the tag(s) ${[...new Set(repeated)].join(", ")}.`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function assertEntry(entry, index) {
|
|
197
|
+
if (!isObject(entry) || typeof entry.id !== "string") {
|
|
198
|
+
throw new ShaderSetError(`registry.json pipeline ${index} has no id.`);
|
|
199
|
+
}
|
|
200
|
+
for (const field of ["contractSha256", "envelope"]) {
|
|
201
|
+
if (typeof entry[field] !== "string") {
|
|
202
|
+
throw new ShaderSetError(`registry.json entry ${entry.id} has no ${field}.`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
assertTags(entry.capabilities, `the capabilities of registry.json entry ${entry.id}`);
|
|
206
|
+
}
|
|
207
|
+
function asRegistry(document) {
|
|
208
|
+
if (!isObject(document)) {
|
|
209
|
+
throw new ShaderSetError("registry.json is not an object.");
|
|
210
|
+
}
|
|
211
|
+
assertSchema(document, "registry.json");
|
|
212
|
+
if (!Array.isArray(document.pipelines)) {
|
|
213
|
+
throw new ShaderSetError("registry.json carries no pipelines array.");
|
|
214
|
+
}
|
|
215
|
+
assertTags(document.capabilityVocabulary, "the capabilityVocabulary of registry.json");
|
|
216
|
+
document.pipelines.forEach(assertEntry);
|
|
217
|
+
const ids = document.pipelines.map((entry) => entry.id);
|
|
218
|
+
const repeated = ids.filter((id, index) => ids.indexOf(id) !== index);
|
|
219
|
+
if (repeated.length > 0) {
|
|
220
|
+
throw new ShaderSetError(`registry.json lists ${repeated.join(", ")} more than once.`);
|
|
221
|
+
}
|
|
222
|
+
return document;
|
|
223
|
+
}
|
|
224
|
+
function assertLanguage(id, language, block) {
|
|
225
|
+
const where = `the ${language} block of the envelope for ${id}`;
|
|
226
|
+
if (!isObject(block)) throw new ShaderSetError(`${where} is not an object.`);
|
|
227
|
+
const { blockSize, layout, bindings, stages } = block;
|
|
228
|
+
if (typeof blockSize !== "number" || !Number.isInteger(blockSize) || blockSize <= 0) {
|
|
229
|
+
throw new ShaderSetError(`${where} reports a block size of ${String(blockSize)}.`);
|
|
230
|
+
}
|
|
231
|
+
if (!isObject(layout)) throw new ShaderSetError(`${where} carries no layout.`);
|
|
232
|
+
for (const [name, member] of Object.entries(layout)) {
|
|
233
|
+
const problem = layoutProblem(name, member, blockSize);
|
|
234
|
+
if (problem !== void 0) throw new ShaderSetError(`in ${where}, ${problem}`);
|
|
235
|
+
}
|
|
236
|
+
if (!Array.isArray(bindings)) throw new ShaderSetError(`${where} carries no bindings.`);
|
|
237
|
+
if (!isObject(stages)) throw new ShaderSetError(`${where} carries no stages.`);
|
|
238
|
+
}
|
|
239
|
+
function asEnvelope(document, entry) {
|
|
240
|
+
if (typeof document !== "object" || document === null) {
|
|
241
|
+
throw new ShaderSetError(`the envelope for ${entry.id} is not an object.`);
|
|
242
|
+
}
|
|
243
|
+
const envelope = document;
|
|
244
|
+
assertSchema(envelope, `the envelope for ${entry.id}`);
|
|
245
|
+
if (envelope.pipeline?.id !== entry.id) {
|
|
246
|
+
throw new ShaderSetError(
|
|
247
|
+
`${entry.envelope} describes pipeline ${String(envelope.pipeline?.id)}, but the index points at it for ${entry.id}.`
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
if (envelope.contractSha256 !== entry.contractSha256) {
|
|
251
|
+
throw new ShaderSetError(
|
|
252
|
+
`the envelope for ${entry.id} carries contract ${envelope.contractSha256}, but the index entry that pointed at it records ${entry.contractSha256}. The set is mixed: the index and this envelope are from different builds.`
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
assertTags(envelope.pipeline.capabilities, `the capabilities in the envelope for ${entry.id}`);
|
|
256
|
+
if (!isObject(envelope.languages)) {
|
|
257
|
+
throw new ShaderSetError(`the envelope for ${entry.id} carries no languages.`);
|
|
258
|
+
}
|
|
259
|
+
for (const [language, block] of Object.entries(envelope.languages)) {
|
|
260
|
+
assertLanguage(entry.id, language, block);
|
|
261
|
+
}
|
|
262
|
+
return envelope;
|
|
263
|
+
}
|
|
264
|
+
var ShaderSet = class _ShaderSet {
|
|
265
|
+
#source;
|
|
266
|
+
#entries;
|
|
267
|
+
#envelopes = /* @__PURE__ */ new Map();
|
|
268
|
+
/** The index, as read. */
|
|
269
|
+
registry;
|
|
270
|
+
constructor(source, registry) {
|
|
271
|
+
this.#source = source;
|
|
272
|
+
this.registry = registry;
|
|
273
|
+
this.#entries = new Map(registry.pipelines.map((entry) => [entry.id, entry]));
|
|
274
|
+
}
|
|
275
|
+
/** Read the index and open the set. */
|
|
276
|
+
static async open(source) {
|
|
277
|
+
return new _ShaderSet(source, asRegistry(await source.readRegistry()));
|
|
278
|
+
}
|
|
279
|
+
/** Every pipeline the set holds, in the order the index lists them. */
|
|
280
|
+
get pipelines() {
|
|
281
|
+
return this.registry.pipelines;
|
|
282
|
+
}
|
|
283
|
+
/** The closed set of capability tags any pipeline here may declare. */
|
|
284
|
+
get capabilityVocabulary() {
|
|
285
|
+
return this.registry.capabilityVocabulary;
|
|
286
|
+
}
|
|
287
|
+
/** The index entry for `id`. */
|
|
288
|
+
entry(id) {
|
|
289
|
+
const entry = this.#entries.get(id);
|
|
290
|
+
if (entry === void 0) {
|
|
291
|
+
throw new ShaderSetError(`no pipeline ${id} in this set; it holds ${this.#entries.size}.`);
|
|
292
|
+
}
|
|
293
|
+
return entry;
|
|
294
|
+
}
|
|
295
|
+
/** The envelope for `id`, fetched once and cached. */
|
|
296
|
+
async envelope(id) {
|
|
297
|
+
const entry = this.entry(id);
|
|
298
|
+
let pending = this.#envelopes.get(id);
|
|
299
|
+
if (pending === void 0) {
|
|
300
|
+
pending = this.#source.readEnvelope(entry).then((document) => asEnvelope(document, entry));
|
|
301
|
+
pending.catch(() => this.#envelopes.delete(id));
|
|
302
|
+
this.#envelopes.set(id, pending);
|
|
303
|
+
}
|
|
304
|
+
return pending;
|
|
305
|
+
}
|
|
306
|
+
/** The envelope's block for one language; `glsl` unless asked otherwise. */
|
|
307
|
+
static language(envelope, language = "glsl") {
|
|
308
|
+
const block = Object.hasOwn(envelope.languages, language) ? envelope.languages[language] : void 0;
|
|
309
|
+
if (block === void 0) {
|
|
310
|
+
const carried = Object.keys(envelope.languages).join(", ") || "none";
|
|
311
|
+
throw new ShaderSetError(
|
|
312
|
+
`the envelope for ${envelope.pipeline.id} carries no ${language}; it carries ${carried}.`
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
return block;
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
// src/pack.ts
|
|
320
|
+
var UniformPackError = class extends Error {
|
|
321
|
+
name = "UniformPackError";
|
|
322
|
+
};
|
|
323
|
+
var INT32_MIN = -2147483648;
|
|
324
|
+
var INT32_MAX = 2 ** 31 - 1;
|
|
325
|
+
var UINT32_MAX = 2 ** 32 - 1;
|
|
326
|
+
function scalarProblem(kind, value) {
|
|
327
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return "not a finite number";
|
|
328
|
+
if (kind === "float") return void 0;
|
|
329
|
+
if (!Number.isInteger(value)) return `not an integer, and the member is ${kind}`;
|
|
330
|
+
if (kind === "int" && (value < INT32_MIN || value > INT32_MAX)) {
|
|
331
|
+
return "outside the 32-bit signed range";
|
|
332
|
+
}
|
|
333
|
+
if (kind === "uint" && (value < 0 || value > UINT32_MAX)) {
|
|
334
|
+
return "outside the 32-bit unsigned range";
|
|
335
|
+
}
|
|
336
|
+
return void 0;
|
|
337
|
+
}
|
|
338
|
+
var UniformBlock = class {
|
|
339
|
+
#layout;
|
|
340
|
+
#floats;
|
|
341
|
+
#ints;
|
|
342
|
+
#uints;
|
|
343
|
+
/** The bytes to upload. */
|
|
344
|
+
buffer;
|
|
345
|
+
constructor(block) {
|
|
346
|
+
if (!Number.isInteger(block.blockSize) || block.blockSize <= 0) {
|
|
347
|
+
throw new UniformPackError(
|
|
348
|
+
`the envelope reports a block size of ${String(block.blockSize)}.`
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
for (const [name, layout] of Object.entries(block.layout)) {
|
|
352
|
+
const problem = layoutProblem(name, layout, block.blockSize);
|
|
353
|
+
if (problem !== void 0) throw new UniformPackError(problem);
|
|
354
|
+
}
|
|
355
|
+
this.#layout = block.layout;
|
|
356
|
+
this.buffer = new ArrayBuffer(block.blockSize);
|
|
357
|
+
this.#floats = new Float32Array(this.buffer);
|
|
358
|
+
this.#ints = new Int32Array(this.buffer);
|
|
359
|
+
this.#uints = new Uint32Array(this.buffer);
|
|
360
|
+
}
|
|
361
|
+
/** The block's size in bytes. */
|
|
362
|
+
get byteLength() {
|
|
363
|
+
return this.buffer.byteLength;
|
|
364
|
+
}
|
|
365
|
+
/** Whether the block declares `name`. */
|
|
366
|
+
has(name) {
|
|
367
|
+
return Object.hasOwn(this.#layout, name) && this.#layout[name]?.encoding != null;
|
|
368
|
+
}
|
|
369
|
+
/** Every member the block declares. */
|
|
370
|
+
get members() {
|
|
371
|
+
return Object.keys(this.#layout);
|
|
372
|
+
}
|
|
373
|
+
/** Zero the whole block. */
|
|
374
|
+
clear() {
|
|
375
|
+
this.#floats.fill(0);
|
|
376
|
+
return this;
|
|
377
|
+
}
|
|
378
|
+
#entry(name) {
|
|
379
|
+
const layout = Object.hasOwn(this.#layout, name) ? this.#layout[name] : void 0;
|
|
380
|
+
if (layout === void 0) {
|
|
381
|
+
throw new UniformPackError(
|
|
382
|
+
`the uniform block declares no ${name}. It declares ${this.members.length} member(s); a name absent here is either a typo or a uniform this pipeline does not carry.`
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
if (layout.encoding == null) {
|
|
386
|
+
throw new UniformPackError(
|
|
387
|
+
`${name} has no encoding the build could report, so its bytes would be a guess. It is part of the block and counts towards its size, but it cannot be written.`
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
return { layout, encoding: layout.encoding };
|
|
391
|
+
}
|
|
392
|
+
#writeScalars(kind, at, values, count, name) {
|
|
393
|
+
for (let index = 0; index < count; index++) {
|
|
394
|
+
const value = values[index];
|
|
395
|
+
const problem = scalarProblem(kind, value);
|
|
396
|
+
if (problem !== void 0) {
|
|
397
|
+
throw new UniformPackError(`${name}[${index}] is ${String(value)}, which is ${problem}.`);
|
|
398
|
+
}
|
|
399
|
+
const slot = at + index;
|
|
400
|
+
if (kind === "float") this.#floats[slot] = value;
|
|
401
|
+
else if (kind === "int") this.#ints[slot] = value;
|
|
402
|
+
else this.#uints[slot] = value;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Write one member.
|
|
407
|
+
*
|
|
408
|
+
* Accepts what the member's shape needs: a number for a scalar, a flat array
|
|
409
|
+
* of components for a vector or matrix, and either a flat array or an array
|
|
410
|
+
* of elements for an array member.
|
|
411
|
+
*
|
|
412
|
+
* A matrix is taken as column-major — the order GL APIs and every mat4
|
|
413
|
+
* library produce — and is written so that the emitted shaders apply *that*
|
|
414
|
+
* transform. See {@link UniformBlock} on why the bytes that achieve it are
|
|
415
|
+
* not the ones the block's `row_major` qualifier alone would suggest. Pass
|
|
416
|
+
* `majorness: "row"` only when the input array is itself row-major.
|
|
417
|
+
*/
|
|
418
|
+
set(name, value, options) {
|
|
419
|
+
const { layout, encoding } = this.#entry(name);
|
|
420
|
+
const { kind, width, order, isArray } = describeEncoding(encoding);
|
|
421
|
+
if (typeof value === "boolean") value = value ? 1 : 0;
|
|
422
|
+
if (typeof value === "number") {
|
|
423
|
+
if (isArray || width !== 1) {
|
|
424
|
+
throw new UniformPackError(`${name} is ${encoding}; a single number cannot fill it.`);
|
|
425
|
+
}
|
|
426
|
+
this.#writeScalars(kind, layout.offset / 4, [value], 1, name);
|
|
427
|
+
return this;
|
|
428
|
+
}
|
|
429
|
+
const flat = value.length > 0 && typeof value[0] === "object" ? value.flatMap((item) => Array.from(item)) : value;
|
|
430
|
+
if (order !== void 0) {
|
|
431
|
+
if (isArray)
|
|
432
|
+
throw new UniformPackError(`${name} is an array of matrices, which is not supported.`);
|
|
433
|
+
this.#setMatrix(name, layout, order, flat, options?.majorness ?? "column");
|
|
434
|
+
return this;
|
|
435
|
+
}
|
|
436
|
+
if (!isArray) {
|
|
437
|
+
if (flat.length !== width) {
|
|
438
|
+
throw new UniformPackError(
|
|
439
|
+
`${name} is ${encoding} and needs ${width} component(s); got ${flat.length}.`
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
this.#writeScalars(kind, layout.offset / 4, flat, width, name);
|
|
443
|
+
return this;
|
|
444
|
+
}
|
|
445
|
+
const stride = layout.elementStride;
|
|
446
|
+
const capacity = Math.floor(layout.size / stride);
|
|
447
|
+
if (flat.length % width !== 0) {
|
|
448
|
+
throw new UniformPackError(
|
|
449
|
+
`${name} is ${encoding}; ${flat.length} value(s) is not a whole number of ${width}-component elements.`
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
const count = flat.length / width;
|
|
453
|
+
if (count > capacity) {
|
|
454
|
+
throw new UniformPackError(`${name} holds ${capacity} element(s); got ${count}.`);
|
|
455
|
+
}
|
|
456
|
+
for (let index = 0; index < count; index++) {
|
|
457
|
+
const at = (layout.offset + index * stride) / 4;
|
|
458
|
+
this.#writeScalars(
|
|
459
|
+
kind,
|
|
460
|
+
at,
|
|
461
|
+
Array.prototype.slice.call(flat, index * width, (index + 1) * width),
|
|
462
|
+
width,
|
|
463
|
+
name
|
|
464
|
+
);
|
|
465
|
+
}
|
|
466
|
+
return this;
|
|
467
|
+
}
|
|
468
|
+
#setMatrix(name, layout, order, values, given) {
|
|
469
|
+
if (values.length !== order * order) {
|
|
470
|
+
throw new UniformPackError(
|
|
471
|
+
`${name} is mat${order} and needs ${order * order} values; got ${values.length}.`
|
|
472
|
+
);
|
|
473
|
+
}
|
|
474
|
+
const wanted = layout.majorness ?? "column";
|
|
475
|
+
const base = layout.offset / 4;
|
|
476
|
+
for (let major = 0; major < order; major++) {
|
|
477
|
+
for (let minor = 0; minor < order; minor++) {
|
|
478
|
+
const source = wanted === given ? minor * order + major : major * order + minor;
|
|
479
|
+
const value = values[source];
|
|
480
|
+
if (scalarProblem("float", value) !== void 0) {
|
|
481
|
+
throw new UniformPackError(
|
|
482
|
+
`${name}[${source}] is ${String(value)}, which is not a finite number.`
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
this.#floats[base + major * 4 + minor] = value;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
// src/select.ts
|
|
492
|
+
var PipelineSelectionError = class extends Error {
|
|
493
|
+
name = "PipelineSelectionError";
|
|
494
|
+
};
|
|
495
|
+
var asSet = (tags) => new Set(tags);
|
|
496
|
+
function sameSet(a, b) {
|
|
497
|
+
const other = asSet(b);
|
|
498
|
+
if (a.size !== other.size) return false;
|
|
499
|
+
for (const tag of other) if (!a.has(tag)) return false;
|
|
500
|
+
return true;
|
|
501
|
+
}
|
|
502
|
+
function validateCapabilities(registry, capabilities) {
|
|
503
|
+
const vocabulary = new Set(registry.capabilityVocabulary);
|
|
504
|
+
const unknown = [...asSet(capabilities)].filter((tag) => !vocabulary.has(tag));
|
|
505
|
+
if (unknown.length > 0) {
|
|
506
|
+
throw new PipelineSelectionError(
|
|
507
|
+
`capability tag(s) not in this set's vocabulary: ${unknown.join(", ")}. The vocabulary is closed, so no pipeline here can carry them. It holds: ${registry.capabilityVocabulary.join(", ")}.`
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
function matchCapabilities(entries, capabilities) {
|
|
512
|
+
const wanted = asSet(capabilities);
|
|
513
|
+
return entries.filter((entry) => sameSet(wanted, entry.capabilities));
|
|
514
|
+
}
|
|
515
|
+
function resolvePipeline(registry, capabilities) {
|
|
516
|
+
validateCapabilities(registry, capabilities);
|
|
517
|
+
const wanted = [...asSet(capabilities)].sort();
|
|
518
|
+
const matches = matchCapabilities(registry.pipelines, wanted);
|
|
519
|
+
if (matches.length === 1) return matches[0];
|
|
520
|
+
if (matches.length === 0) {
|
|
521
|
+
throw new PipelineSelectionError(
|
|
522
|
+
`no pipeline in this set has exactly the capabilities {${wanted.join(", ")}}. The match is exact: a pipeline carrying a capability you did not ask for reads an attribute you did not bind.`
|
|
523
|
+
);
|
|
524
|
+
}
|
|
525
|
+
throw new PipelineSelectionError(
|
|
526
|
+
`${matches.length} pipelines share the capabilities {${wanted.join(", ")}}: ${matches.map((entry) => entry.id).join(", ")}.`
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
function withCapabilities(entries, capabilities) {
|
|
530
|
+
const wanted = [...asSet(capabilities)];
|
|
531
|
+
return entries.filter((entry) => wanted.every((tag) => entry.capabilities.includes(tag)));
|
|
532
|
+
}
|
|
533
|
+
function capabilitiesInUse(entries) {
|
|
534
|
+
return [...new Set(entries.flatMap((entry) => [...entry.capabilities]))].sort();
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// src/pick.ts
|
|
538
|
+
var PICK_CAPABILITY = "pick";
|
|
539
|
+
var PICK_EMPTY = 4294967295;
|
|
540
|
+
var PICK_RESERVED = 0;
|
|
541
|
+
var PICK_CHANNELS = ["primitiveId", "depth", "reserved0", "reserved1"];
|
|
542
|
+
var DEFORM_CAPABILITIES = ["deform3", "deform6"];
|
|
543
|
+
var PickDecodeError = class extends Error {
|
|
544
|
+
name = "PickDecodeError";
|
|
545
|
+
};
|
|
546
|
+
var scratch = new ArrayBuffer(4);
|
|
547
|
+
var asUint = new Uint32Array(scratch);
|
|
548
|
+
var asFloat = new Float32Array(scratch);
|
|
549
|
+
function decodePick(texel, offset = 0) {
|
|
550
|
+
if (texel.length - offset < 4) {
|
|
551
|
+
throw new PickDecodeError(
|
|
552
|
+
`a pick texel is 4 channels; ${texel.length - offset} are available from offset ${offset}.`
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
for (let channel = 0; channel < 4; channel++) {
|
|
556
|
+
const value = texel[offset + channel];
|
|
557
|
+
if (!Number.isInteger(value) || value < 0 || value > 4294967295) {
|
|
558
|
+
throw new PickDecodeError(
|
|
559
|
+
`the ${PICK_CHANNELS[channel]} channel is ${String(value)}, which is not a 32-bit unsigned integer. The readback was decoded at the wrong width, so every field is suspect.`
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
const primitiveId = texel[offset];
|
|
564
|
+
if (primitiveId === PICK_EMPTY) return null;
|
|
565
|
+
asUint[0] = texel[offset + 1];
|
|
566
|
+
return { primitiveId, depth: asFloat[0] };
|
|
567
|
+
}
|
|
568
|
+
function pickPipelineFor(registry, color) {
|
|
569
|
+
const available = /* @__PURE__ */ new Set([...color.capabilities, PICK_CAPABILITY]);
|
|
570
|
+
const deform = DEFORM_CAPABILITIES.filter((tag) => available.has(tag));
|
|
571
|
+
const compatible = withCapabilities(registry.pipelines, [PICK_CAPABILITY, ...deform]).filter(
|
|
572
|
+
(entry) => entry.capabilities.every((tag) => available.has(tag))
|
|
573
|
+
);
|
|
574
|
+
if (compatible.length === 0) {
|
|
575
|
+
const variant = deform.length > 0 ? ` carrying ${deform.join(", ")}` : "";
|
|
576
|
+
throw new PipelineSelectionError(
|
|
577
|
+
`this set has no pick pipeline${variant} compatible with ${color.id}. Picking draws the same geometry a second time, so it needs a pick pipeline whose vertex stage matches.`
|
|
578
|
+
);
|
|
579
|
+
}
|
|
580
|
+
const best = Math.max(...compatible.map((entry) => entry.capabilities.length));
|
|
581
|
+
const winners = compatible.filter((entry) => entry.capabilities.length === best);
|
|
582
|
+
if (winners.length > 1) {
|
|
583
|
+
throw new PipelineSelectionError(
|
|
584
|
+
`${color.id} has ${winners.length} equally compatible pick pipelines: ${winners.map((entry) => entry.id).join(", ")}.`
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
return winners[0];
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// src/version.ts
|
|
591
|
+
var VERSION = "1.0.0";
|
|
592
|
+
|
|
593
|
+
exports.BindingError = BindingError;
|
|
594
|
+
exports.PICK_CAPABILITY = PICK_CAPABILITY;
|
|
595
|
+
exports.PICK_CHANNELS = PICK_CHANNELS;
|
|
596
|
+
exports.PICK_EMPTY = PICK_EMPTY;
|
|
597
|
+
exports.PICK_RESERVED = PICK_RESERVED;
|
|
598
|
+
exports.PickDecodeError = PickDecodeError;
|
|
599
|
+
exports.PipelineSelectionError = PipelineSelectionError;
|
|
600
|
+
exports.ResourceBindings = ResourceBindings;
|
|
601
|
+
exports.SCHEMA_VERSION = SCHEMA_VERSION;
|
|
602
|
+
exports.ShaderSet = ShaderSet;
|
|
603
|
+
exports.ShaderSetError = ShaderSetError;
|
|
604
|
+
exports.UniformBlock = UniformBlock;
|
|
605
|
+
exports.UniformPackError = UniformPackError;
|
|
606
|
+
exports.VERSION = VERSION;
|
|
607
|
+
exports.capabilitiesInUse = capabilitiesInUse;
|
|
608
|
+
exports.decodePick = decodePick;
|
|
609
|
+
exports.fetchSource = fetchSource;
|
|
610
|
+
exports.fsSource = fsSource;
|
|
611
|
+
exports.matchCapabilities = matchCapabilities;
|
|
612
|
+
exports.pickPipelineFor = pickPipelineFor;
|
|
613
|
+
exports.resolvePipeline = resolvePipeline;
|
|
614
|
+
exports.validateCapabilities = validateCapabilities;
|
|
615
|
+
exports.withCapabilities = withCapabilities;
|
|
616
|
+
//# sourceMappingURL=index.cjs.map
|
|
617
|
+
//# sourceMappingURL=index.cjs.map
|