@orkestrel/wgsl 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +9 -2
- package/dist/src/core/index.cjs +1836 -0
- package/dist/src/core/index.cjs.map +1 -0
- package/dist/src/core/index.d.cts +1207 -0
- package/dist/src/core/index.d.ts +1207 -0
- package/dist/src/core/index.js +1805 -0
- package/dist/src/core/index.js.map +1 -0
- package/package.json +99 -5
|
@@ -0,0 +1,1207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Collects every fragment the roots reach, each once, in dependency order.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* The walk is an iterative postorder over each fragment's `uses`, so a fragment follows every
|
|
6
|
+
* fragment it uses. A fragment reached twice, as in a diamond, appears once, keyed on identity.
|
|
7
|
+
* Thrown with code `VALIDATION` when two distinct fragments share one name, and when a fragment
|
|
8
|
+
* reaches itself, with the cycle's names in order in `context.cycle`.
|
|
9
|
+
*
|
|
10
|
+
* @param roots - The fragments an entry body calls, in the order they are listed
|
|
11
|
+
* @returns The reached fragments, frozen
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const square = createWGSLFragment({ name: 'square', source: 'fn square(value: f32) -> f32 { return value * value; }' })
|
|
16
|
+
* const cube = createWGSLFragment({ name: 'cube', source: 'fn cube(value: f32) -> f32 { return square(value) * value; }', uses: [square] })
|
|
17
|
+
* collectWGSLFragments([cube, square]).map((fragment) => fragment.name) // ['square', 'cube']
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function collectWGSLFragments(roots: readonly WGSLFragment[]): readonly WGSLFragment[];
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Computes a dispatch's workgroup counts for a total: the x axis up to the limit, then the y axis.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* A program reads its workgroup's index as
|
|
27
|
+
* `invocation.workgroup_id.x + invocation.workgroup_id.y * invocation.num_workgroups.x`, so a
|
|
28
|
+
* workgroup past the total finds no work. A total of 0 gives one workgroup. Thrown with code
|
|
29
|
+
* `ARGUMENT` when `total` is not a non-negative safe integer, `-0` included, or `limit` is not a
|
|
30
|
+
* positive safe integer, and with code `LIMIT` when the total passes `limit * limit`.
|
|
31
|
+
*
|
|
32
|
+
* @param total - The number of workgroups
|
|
33
|
+
* @param limit - The device's `maxComputeWorkgroupsPerDimension`
|
|
34
|
+
* @returns The counts on the x, y, and z axes
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* computeWGSLGrid(70_000, 65_535) // [65535, 2, 1]
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export declare function computeWGSLGrid(total: number, limit: number): WGSLGrid;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Computes the smallest multiple of an alignment at or above an offset.
|
|
45
|
+
*
|
|
46
|
+
* @remarks
|
|
47
|
+
* Thrown with code `ARGUMENT` when `offset` is not a non-negative safe integer, `-0` included, or
|
|
48
|
+
* `alignment` is not a positive safe integer.
|
|
49
|
+
*
|
|
50
|
+
* @param offset - The byte offset to align
|
|
51
|
+
* @param alignment - The byte alignment
|
|
52
|
+
* @returns The aligned byte offset
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* computeWGSLOffset(36, 256) // 256
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare function computeWGSLOffset(offset: number, alignment: number): number;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Creates a frozen fragment from author-written WGSL text and the fragments it uses.
|
|
63
|
+
*
|
|
64
|
+
* @remarks
|
|
65
|
+
* The text reaches the compiler unchanged. Thrown with code `ARGUMENT` when the name is empty, when
|
|
66
|
+
* a source is not a string, and when the variant names an extension outside `WGSL_EXTENSIONS`.
|
|
67
|
+
*
|
|
68
|
+
* @param input - The fragment's name, text, used fragments, and optional variant
|
|
69
|
+
* @returns The fragment, frozen
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* createWGSLFragment({ name: 'square', source: 'fn square(value: f32) -> f32 { return value * value; }' }).uses // []
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export declare function createWGSLFragment(input: WGSLFragmentInput): WGSLFragment;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Creates a frozen compute program and links every fragment its entry body reaches.
|
|
80
|
+
*
|
|
81
|
+
* @remarks
|
|
82
|
+
* The slots keep their declaration order, which is their binding order. Thrown with code
|
|
83
|
+
* `ARGUMENT` when a name is empty, when the entry size is not three positive safe integers, when the
|
|
84
|
+
* entry source is not a string, and when a slot names an access outside `WGSL_ACCESSES` or an
|
|
85
|
+
* element outside `WGSL_SCALARS`; `context.slots` lists each such slot. Thrown with code
|
|
86
|
+
* `VALIDATION` when the linker finds two distinct fragments of one name or a cycle.
|
|
87
|
+
*
|
|
88
|
+
* @param input - The program's name, entry, slots, uniform, and used fragments
|
|
89
|
+
* @returns The program, frozen
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* createWGSLProgram({ name: 'clear', entry, slots, uniform }).fragments // []
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export declare function createWGSLProgram<S extends WGSLSlotMap, F extends WGSLFieldMap>(input: WGSLProgramInput<S, F>): WGSLProgram<S, F>;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Creates an open session that prepares programs and runs batches on a borrowed device.
|
|
100
|
+
*
|
|
101
|
+
* @remarks
|
|
102
|
+
* The session never requests or destroys the device; its `destroy` releases only the buffers it
|
|
103
|
+
* created. Thrown with code `ARGUMENT` when `uniform.size` is not a positive safe multiple of 4.
|
|
104
|
+
*
|
|
105
|
+
* @param device - The device the session borrows
|
|
106
|
+
* @param options - The extensions to switch off and the uniform slab's byte size
|
|
107
|
+
* @returns The session
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* const session = createWGSLSession(device, { extensions: { f16: false } })
|
|
112
|
+
* session.status // 'open'
|
|
113
|
+
* ```
|
|
114
|
+
*/
|
|
115
|
+
export declare function createWGSLSession(device: GPUDevice, options?: WGSLSessionOptions): WGSLSessionInterface;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Creates a frozen uniform struct with its scalar layout and its rendered WGSL declaration.
|
|
119
|
+
*
|
|
120
|
+
* @remarks
|
|
121
|
+
* Each `u32`, `i32`, and `f32` member aligns to 4 bytes and takes 4 bytes in the `uniform` address
|
|
122
|
+
* space, so members sit in declaration order and the struct's size is the member count times 4.
|
|
123
|
+
* Thrown with code `ARGUMENT` when `name` is empty, when `fields` is not a plain record or declares
|
|
124
|
+
* no field, and when a field names a type outside `WGSL_SCALARS`; `context.fields` lists each such
|
|
125
|
+
* field.
|
|
126
|
+
*
|
|
127
|
+
* @param name - The struct's WGSL name
|
|
128
|
+
* @param fields - Each field's scalar type, in declaration order
|
|
129
|
+
* @returns The struct, frozen
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* createWGSLStruct('Shape', { rows: 'u32', epsilon: 'f32' }).offsets // { rows: 0, epsilon: 4 }
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
export declare function createWGSLStruct<F extends WGSLFieldMap>(name: string, fields: F): WGSLStruct<F>;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Checks whether a value names a storage slot access mode.
|
|
140
|
+
*
|
|
141
|
+
* @param value - The value to inspect
|
|
142
|
+
* @returns True if the value is one of the `WGSL_ACCESSES` members; false otherwise
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* isWGSLAccess('read_write') // true
|
|
147
|
+
* isWGSLAccess('write') // false
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export declare function isWGSLAccess(value: unknown): value is WGSLAccess;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Checks whether an unknown value is a WGSL error.
|
|
154
|
+
*
|
|
155
|
+
* @remarks
|
|
156
|
+
* Recognition requires a native `Error` whose prototype is not `Error.prototype`, an own brand
|
|
157
|
+
* property holding the tested value itself, a declared condition code, and an object, callable, or
|
|
158
|
+
* absent context. It recognizes an error from another installed copy of this package, and refuses a
|
|
159
|
+
* look-alike with the same code and no brand, a proxy over a branded error, and a plain object that
|
|
160
|
+
* only carries the brand.
|
|
161
|
+
*
|
|
162
|
+
* @param value - The value to inspect
|
|
163
|
+
* @returns True if the value is a `WGSLError`; false otherwise
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* ```ts
|
|
167
|
+
* isWGSLError(new WGSLError('The batch was aborted.', { code: 'ABORT' })) // true
|
|
168
|
+
* isWGSLError(new Error('The batch was aborted.')) // false
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
export declare function isWGSLError(value: unknown): value is WGSLError;
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Checks whether a value names a WGSL extension a fragment variant can enable.
|
|
175
|
+
*
|
|
176
|
+
* @remarks
|
|
177
|
+
* Only an own key of `WGSL_EXTENSIONS` passes, so an inherited name such as `toString` fails.
|
|
178
|
+
*
|
|
179
|
+
* @param value - The value to inspect
|
|
180
|
+
* @returns True if the value is a key of `WGSL_EXTENSIONS`; false otherwise
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* isWGSLExtension('subgroups') // true
|
|
185
|
+
* isWGSLExtension('shader-f16') // false
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
export declare function isWGSLExtension(value: unknown): value is WGSLExtension;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Checks whether a value holds a positive safe integer count on each of three axes.
|
|
192
|
+
*
|
|
193
|
+
* @remarks
|
|
194
|
+
* The value must be an array of length 3, and each of its indices must hold a safe integer of at
|
|
195
|
+
* least 1, so a sparse array, a zero count, and a fraction fail.
|
|
196
|
+
*
|
|
197
|
+
* @param value - The value to inspect
|
|
198
|
+
* @returns True if the value is a `WGSLGrid`; false otherwise
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* isWGSLGrid([64, 1, 1]) // true
|
|
203
|
+
* isWGSLGrid([0, 1, 1]) // false
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
export declare function isWGSLGrid(value: unknown): value is WGSLGrid;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Checks whether a value names a scalar type a uniform struct member can hold.
|
|
210
|
+
*
|
|
211
|
+
* @param value - The value to inspect
|
|
212
|
+
* @returns True if the value is one of the `WGSL_SCALARS` members; false otherwise
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* isWGSLScalar('f32') // true
|
|
217
|
+
* isWGSLScalar('f64') // false
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
export declare function isWGSLScalar(value: unknown): value is WGSLScalar;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Renders a program's complete WGSL text for one set of active extensions.
|
|
224
|
+
*
|
|
225
|
+
* @remarks
|
|
226
|
+
* The text holds, in order: an `enable` directive for each extension a linked fragment's active
|
|
227
|
+
* variant names, in `WGSL_EXTENSIONS` order; the uniform struct declaration; the `Invocation` entry
|
|
228
|
+
* input struct, which carries `local_invocation_index`, `workgroup_id`, and `num_workgroups`, plus
|
|
229
|
+
* `subgroup_invocation_id` and `subgroup_size` when `enable subgroups;` is emitted; one
|
|
230
|
+
* `@group(0) @binding(N)` storage line per slot in declaration order and the `@group(1) @binding(0)`
|
|
231
|
+
* uniform line; each linked fragment's active body, unchanged; and the entry function, which takes
|
|
232
|
+
* the input struct as `invocation`. A fragment's variant is active when `extensions` switches its
|
|
233
|
+
* extension on.
|
|
234
|
+
*
|
|
235
|
+
* @param program - The program to render
|
|
236
|
+
* @param extensions - Whether each extension is active
|
|
237
|
+
* @returns The WGSL text, ending in a line feed
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* renderWGSLProgram(program, { f16: false, subgroups: false }).startsWith('struct Shape {') // true
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
export declare function renderWGSLProgram(program: WGSLProgram, extensions: Readonly<Record<WGSLExtension, boolean>>): string;
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Lists the access modes a storage slot can declare.
|
|
248
|
+
*
|
|
249
|
+
* @remarks
|
|
250
|
+
* Each member mirrors the WGSL `var<storage, ACCESS>` access mode of the same spelling.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* WGSL_ACCESSES.includes('read_write') // true
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
export declare const WGSL_ACCESSES: readonly ["read", "read_write"];
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Keys the global brand a `WGSLError` carries, so another installed copy recognizes it.
|
|
261
|
+
*
|
|
262
|
+
* @remarks
|
|
263
|
+
* The key follows the package-and-class form, `Symbol.for('<package>.<ClassName>')`. The brand is
|
|
264
|
+
* a recognition marker and never proof that the class's constructor ran.
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* WGSL_ERROR_BRAND === Symbol.for('@orkestrel/wgsl.WGSLError') // true
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
export declare const WGSL_ERROR_BRAND: unique symbol;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Lists the condition codes a `WGSLError` carries.
|
|
275
|
+
*
|
|
276
|
+
* @remarks
|
|
277
|
+
* `ARGUMENT` marks a malformed argument, `COMPILE` a shader the compiler refuses, `VALIDATION` a
|
|
278
|
+
* declaration or command the package or the device refuses, `LIMIT` an exceeded device limit,
|
|
279
|
+
* `HOST` a lost device, `DESTROYED` a call after `destroy`, and `ABORT` an aborted signal or batch.
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```ts
|
|
283
|
+
* WGSL_ERROR_CODES.includes('VALIDATION') // true
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
export declare const WGSL_ERROR_CODES: readonly ["ARGUMENT", "COMPILE", "VALIDATION", "LIMIT", "HOST", "DESTROYED", "ABORT"];
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Maps each WGSL extension a fragment variant can enable to the device feature that admits it.
|
|
290
|
+
*
|
|
291
|
+
* @remarks
|
|
292
|
+
* Each key mirrors the WGSL `enable` directive of the same spelling, and each value mirrors the
|
|
293
|
+
* WebGPU `GPUFeatureName` a device reports in `device.features`. The key order is the order in
|
|
294
|
+
* which a rendered program emits its `enable` directives.
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```ts
|
|
298
|
+
* WGSL_EXTENSIONS.f16 // 'shader-f16'
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
export declare const WGSL_EXTENSIONS: Readonly<{
|
|
302
|
+
readonly f16: 'shader-f16';
|
|
303
|
+
readonly subgroups: 'subgroups';
|
|
304
|
+
}>;
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Holds the fragment that widens the IEEE 754 half-precision bits in the low 16 bits of a `u32` to an `f32`.
|
|
308
|
+
*
|
|
309
|
+
* @remarks
|
|
310
|
+
* It declares `fn widen(bits: u32) -> f32`, which builds the result from integer bit operations, so
|
|
311
|
+
* signed zeros, subnormals, infinities, and NaN payloads widen exactly and no `f16` extension is
|
|
312
|
+
* needed. An exponent of `0x1f` keeps the fraction under an all-ones `f32` exponent, so infinities
|
|
313
|
+
* stay infinite and NaN payloads survive. A subnormal half rebuilds its `f32` bits from
|
|
314
|
+
* `firstLeadingBit(fraction)`: the leading bit sets the exponent and the bits under it shift into
|
|
315
|
+
* the `f32` fraction, with no float arithmetic. It reads no binding and no uniform member.
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* ```ts
|
|
319
|
+
* WGSL_HALF.name // 'half'
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
export declare const WGSL_HALF: WGSLFragment;
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Mirrors the WebGPU `GPUMapMode` bit values under one-word members.
|
|
326
|
+
*
|
|
327
|
+
* @remarks
|
|
328
|
+
* `read` mirrors `READ` and `write` mirrors `WRITE`.
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
* ```ts
|
|
332
|
+
* WGSL_MAP.read // 1
|
|
333
|
+
* ```
|
|
334
|
+
*/
|
|
335
|
+
export declare const WGSL_MAP: Readonly<{
|
|
336
|
+
readonly read: 1;
|
|
337
|
+
readonly write: 2;
|
|
338
|
+
}>;
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Holds the fragment that finds the largest `f32` value across the invocations of a 64-invocation workgroup.
|
|
342
|
+
*
|
|
343
|
+
* @remarks
|
|
344
|
+
* It declares `var<workgroup> peaks: array<f32, 64>` and
|
|
345
|
+
* `fn peak(invocation: Invocation, value: f32) -> f32`, which returns the workgroup's maximum to
|
|
346
|
+
* every invocation. The entry's workgroup size must hold 64 invocations, and every invocation must
|
|
347
|
+
* call it from uniform control flow. The default body reduces with `max` in a tree of halving
|
|
348
|
+
* strides. The `subgroups` variant stores each subgroup's `subgroupMax` partial at its first
|
|
349
|
+
* invocation's index and the lowest finite `f32` (`-3.4028234663852886e38`) elsewhere, and invocation 0 folds
|
|
350
|
+
* the 64 entries with `max` between 2 barriers (matching `@orkestrel/infer`, so it returns `-3.4028234663852886e38`
|
|
351
|
+
* rather than `-Infinity` when every input is `-Infinity`). It reads no binding and no uniform member.
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```ts
|
|
355
|
+
* WGSL_PEAK.variant?.extension // 'subgroups'
|
|
356
|
+
* ```
|
|
357
|
+
*/
|
|
358
|
+
export declare const WGSL_PEAK: WGSLFragment;
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Holds the fragment that reads a workgroup's two-axis index from the entry input struct.
|
|
362
|
+
*
|
|
363
|
+
* @remarks
|
|
364
|
+
* It declares `fn place(invocation: Invocation) -> u32`, which returns
|
|
365
|
+
* `invocation.workgroup_id.x + invocation.workgroup_id.y * invocation.num_workgroups.x`, the
|
|
366
|
+
* statement of infer's `readPlace` recast onto the `Invocation` struct. Pair it with
|
|
367
|
+
* `computeWGSLGrid`, and multiply by the workgroup size and add `local_invocation_index` for an
|
|
368
|
+
* invocation's index. It reads no binding and no uniform member.
|
|
369
|
+
*
|
|
370
|
+
* @example
|
|
371
|
+
* ```ts
|
|
372
|
+
* createWGSLProgram({ name: 'clear', entry, slots, uniform, uses: [WGSL_PLACE] }).fragments // [WGSL_PLACE]
|
|
373
|
+
* ```
|
|
374
|
+
*/
|
|
375
|
+
export declare const WGSL_PLACE: WGSLFragment;
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Lists the scalar types a uniform struct member can hold.
|
|
379
|
+
*
|
|
380
|
+
* @remarks
|
|
381
|
+
* Each scalar aligns to 4 bytes and takes 4 bytes in the `uniform` and `storage` address spaces.
|
|
382
|
+
*
|
|
383
|
+
* @example
|
|
384
|
+
* ```ts
|
|
385
|
+
* WGSL_SCALARS.includes('f32') // true
|
|
386
|
+
* ```
|
|
387
|
+
*/
|
|
388
|
+
export declare const WGSL_SCALARS: readonly ["u32", "i32", "f32"];
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Mirrors the WebGPU `GPUShaderStage` bit value of the compute stage.
|
|
392
|
+
*
|
|
393
|
+
* @remarks
|
|
394
|
+
* `compute` mirrors `COMPUTE`.
|
|
395
|
+
*
|
|
396
|
+
* @example
|
|
397
|
+
* ```ts
|
|
398
|
+
* WGSL_STAGE.compute // 4
|
|
399
|
+
* ```
|
|
400
|
+
*/
|
|
401
|
+
export declare const WGSL_STAGE: Readonly<{
|
|
402
|
+
readonly compute: 4;
|
|
403
|
+
}>;
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Holds the fragment that sums one `f32` value from each invocation of a 64-invocation workgroup.
|
|
407
|
+
*
|
|
408
|
+
* @remarks
|
|
409
|
+
* It declares `var<workgroup> sums: array<f32, 64>` and
|
|
410
|
+
* `fn sum(invocation: Invocation, value: f32) -> f32`, which returns the workgroup's total to every
|
|
411
|
+
* invocation. The entry's workgroup size must hold 64 invocations, and every invocation must call
|
|
412
|
+
* it from uniform control flow. The default body reduces in a tree of halving strides. The
|
|
413
|
+
* `subgroups` variant stores each subgroup's `subgroupAdd` partial at its first invocation's index
|
|
414
|
+
* and `0.0` elsewhere, and invocation 0 adds the 64 entries in index order between 2 barriers, so
|
|
415
|
+
* its rounding order differs from the default body's. It reads no binding and no uniform member.
|
|
416
|
+
*
|
|
417
|
+
* @example
|
|
418
|
+
* ```ts
|
|
419
|
+
* WGSL_SUM.variant?.extension // 'subgroups'
|
|
420
|
+
* ```
|
|
421
|
+
*/
|
|
422
|
+
export declare const WGSL_SUM: WGSLFragment;
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Holds the default byte capacity of a session's uniform slab.
|
|
426
|
+
*
|
|
427
|
+
* @remarks
|
|
428
|
+
* The session's `uniform.size` option replaces it. At a 256-byte offset alignment the default
|
|
429
|
+
* holds 256 dispatches before a batch flushes.
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* ```ts
|
|
433
|
+
* WGSL_UNIFORM_SIZE // 65536
|
|
434
|
+
* ```
|
|
435
|
+
*/
|
|
436
|
+
export declare const WGSL_UNIFORM_SIZE = 65536;
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Mirrors the WebGPU `GPUBufferUsage` bit values under one-word members.
|
|
440
|
+
*
|
|
441
|
+
* @remarks
|
|
442
|
+
* `read` mirrors `MAP_READ`, `write` mirrors `MAP_WRITE`, `source` mirrors `COPY_SRC`,
|
|
443
|
+
* `destination` mirrors `COPY_DST`, `uniform` mirrors `UNIFORM`, `storage` mirrors `STORAGE`,
|
|
444
|
+
* `indirect` mirrors `INDIRECT`, and `query` mirrors `QUERY_RESOLVE`. The library declares
|
|
445
|
+
* `GPUBufferUsage` only as a type, so `src/core` reads these values instead.
|
|
446
|
+
*
|
|
447
|
+
* @example
|
|
448
|
+
* ```ts
|
|
449
|
+
* WGSL_USAGE.uniform | WGSL_USAGE.destination // 72
|
|
450
|
+
* ```
|
|
451
|
+
*/
|
|
452
|
+
export declare const WGSL_USAGE: Readonly<{
|
|
453
|
+
readonly read: 1;
|
|
454
|
+
readonly write: 2;
|
|
455
|
+
readonly source: 4;
|
|
456
|
+
readonly destination: 8;
|
|
457
|
+
readonly uniform: 64;
|
|
458
|
+
readonly storage: 128;
|
|
459
|
+
readonly indirect: 256;
|
|
460
|
+
readonly query: 512;
|
|
461
|
+
}>;
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Names the access mode of a storage slot.
|
|
465
|
+
*
|
|
466
|
+
* @example
|
|
467
|
+
* ```ts
|
|
468
|
+
* const access: WGSLAccess = 'read_write'
|
|
469
|
+
* ```
|
|
470
|
+
*/
|
|
471
|
+
export declare type WGSLAccess = (typeof WGSL_ACCESSES)[number];
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Records one command encoder's worth of dispatches and buffer reads, then submits them together.
|
|
475
|
+
*
|
|
476
|
+
* @remarks
|
|
477
|
+
* The batch owns its command encoder and its compute pass, and keeps one pass open across
|
|
478
|
+
* consecutive `encode` calls, skipping a `setPipeline` or a `setBindGroup(0, ...)` that repeats the
|
|
479
|
+
* previous one. Each dispatch writes its uniform values to the session's slab and binds them through
|
|
480
|
+
* the 5-argument `setBindGroup(1, group, offsets, 0, 1)` overload. When the slab is full, the batch
|
|
481
|
+
* ends its pass, uploads the slab, submits, clears the slab, and opens a fresh encoder. The pass
|
|
482
|
+
* ends before each `read` copy. Every upload and submit sits between a synchronous push and pop of a
|
|
483
|
+
* `validation` and an `out-of-memory` error scope; a device error on a submitted command buffer
|
|
484
|
+
* moves the session to `failed` with code `VALIDATION`. `read` resolves after the submission to a
|
|
485
|
+
* caller-owned copy of the bytes. Thrown with code `VALIDATION` when a call other than `abort` or
|
|
486
|
+
* `execute` arrives after `submit` or `abort`; with code `ARGUMENT` for a malformed grid or read
|
|
487
|
+
* range; and with code `LIMIT` when a grid axis passes the device's
|
|
488
|
+
* `maxComputeWorkgroupsPerDimension`. Open a batch through `session.batch()`, which supplies the
|
|
489
|
+
* options.
|
|
490
|
+
*
|
|
491
|
+
* @example
|
|
492
|
+
* ```ts
|
|
493
|
+
* const batch = session.batch()
|
|
494
|
+
* batch.encode(binding, { count: 64 }, [1, 1, 1])
|
|
495
|
+
* const bytes = batch.read(buffer, 0, 256)
|
|
496
|
+
* await batch.execute()
|
|
497
|
+
* new Uint32Array(await bytes)[0] // the first word the dispatch wrote
|
|
498
|
+
* ```
|
|
499
|
+
*/
|
|
500
|
+
export declare class WGSLBatch implements WGSLBatchInterface {
|
|
501
|
+
#private;
|
|
502
|
+
/**
|
|
503
|
+
* Creates an open batch with a fresh command encoder.
|
|
504
|
+
*
|
|
505
|
+
* @param device - The device whose encoder records the commands and whose queue runs them
|
|
506
|
+
* @param options - The session's slab and the session hooks
|
|
507
|
+
*/
|
|
508
|
+
constructor(device: GPUDevice, options: WGSLBatchOptions);
|
|
509
|
+
get submitted(): boolean;
|
|
510
|
+
encode<S extends WGSLSlotMap, F extends WGSLFieldMap>(binding: WGSLBinding<S, F>, values: WGSLValueMap<F>, grid: WGSLGrid): void;
|
|
511
|
+
read(buffer: GPUBuffer, offset: number, size: number): Promise<ArrayBuffer>;
|
|
512
|
+
submit(): void;
|
|
513
|
+
execute(signal?: AbortSignal): Promise<void>;
|
|
514
|
+
abort(): void;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/**
|
|
518
|
+
* Records dispatches and buffer reads into one submission.
|
|
519
|
+
*
|
|
520
|
+
* @example
|
|
521
|
+
* ```ts
|
|
522
|
+
* const batch: WGSLBatchInterface = session.batch()
|
|
523
|
+
* ```
|
|
524
|
+
*/
|
|
525
|
+
export declare interface WGSLBatchInterface {
|
|
526
|
+
/** Reports whether the batch has been submitted. */
|
|
527
|
+
readonly submitted: boolean;
|
|
528
|
+
/** Records one dispatch of a bound statement. */
|
|
529
|
+
encode<S extends WGSLSlotMap, F extends WGSLFieldMap>(binding: WGSLBinding<S, F>, values: WGSLValueMap<F>, grid: WGSLGrid): void;
|
|
530
|
+
/** Records a copy of a buffer range and resolves to its bytes after submission. */
|
|
531
|
+
read(buffer: GPUBuffer, offset: number, size: number): Promise<ArrayBuffer>;
|
|
532
|
+
/** Hands the recorded commands to the device queue. */
|
|
533
|
+
submit(): void;
|
|
534
|
+
/** Submits the batch and resolves when the device finishes it. */
|
|
535
|
+
execute(signal?: AbortSignal): Promise<void>;
|
|
536
|
+
/** Discards the unsubmitted commands and rejects pending reads. */
|
|
537
|
+
abort(): void;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Configures a batch: the session's slab and the session hooks the batch reports its lifecycle to.
|
|
542
|
+
*
|
|
543
|
+
* @remarks
|
|
544
|
+
* `check` throws when the session can no longer run commands. `lease` hands out a staging buffer of
|
|
545
|
+
* at least `size` bytes and `restore` takes it back. `release` frees the session's open-batch slot
|
|
546
|
+
* when the batch is submitted or aborted. `fail` records a submitted batch's device failure as the
|
|
547
|
+
* session's terminal failure. `track` registers a settling promise that `destroy` waits for; the
|
|
548
|
+
* promise never rejects.
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
551
|
+
* ```ts
|
|
552
|
+
* const batch = new WGSLBatch(device, { slab, check, lease, restore, release, fail, track })
|
|
553
|
+
* ```
|
|
554
|
+
*/
|
|
555
|
+
export declare interface WGSLBatchOptions {
|
|
556
|
+
/** Holds the session's uniform slab. */
|
|
557
|
+
readonly slab: WGSLSlabInterface;
|
|
558
|
+
/** Throws when the session is failed or destroyed. */
|
|
559
|
+
readonly check: () => void;
|
|
560
|
+
/** Hands out an idle staging buffer of at least `size` bytes. */
|
|
561
|
+
readonly lease: (size: number) => GPUBuffer;
|
|
562
|
+
/** Takes back a staging buffer `lease` handed out. */
|
|
563
|
+
readonly restore: (buffer: GPUBuffer) => void;
|
|
564
|
+
/** Frees the session's open-batch slot. */
|
|
565
|
+
readonly release: (batch: WGSLBatchInterface) => void;
|
|
566
|
+
/** Records a submitted batch's device failure as the session's terminal failure. */
|
|
567
|
+
readonly fail: (error: WGSLError) => void;
|
|
568
|
+
/** Registers a promise that settles when the batch's device work and reads finish. */
|
|
569
|
+
readonly track: (work: Promise<unknown>) => void;
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* Names the lifecycle status of a batch.
|
|
574
|
+
*
|
|
575
|
+
* @remarks
|
|
576
|
+
* `open` records dispatches and reads, `submitted` has handed its commands to the queue, and
|
|
577
|
+
* `aborted` discarded its unsubmitted commands.
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* ```ts
|
|
581
|
+
* const status: WGSLBatchStatus = 'submitted'
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
export declare type WGSLBatchStatus = 'open' | 'submitted' | 'aborted';
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Holds a statement's storage bind group for one set of slot buffers.
|
|
588
|
+
*
|
|
589
|
+
* @example
|
|
590
|
+
* ```ts
|
|
591
|
+
* const binding: WGSLBinding<typeof slots, { rows: 'u32' }> = statement.bind({ destination: { buffer } })
|
|
592
|
+
* ```
|
|
593
|
+
*/
|
|
594
|
+
export declare interface WGSLBinding<S extends WGSLSlotMap, F extends WGSLFieldMap> {
|
|
595
|
+
/** Holds the statement the binding belongs to. */
|
|
596
|
+
readonly statement: WGSLStatementInterface<S, F>;
|
|
597
|
+
/** Holds the storage bind group. */
|
|
598
|
+
readonly group: GPUBindGroup;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
/**
|
|
602
|
+
* Holds one level of a statement's bind cache: the next level for each slot's buffer, offset, and size, and the binding a complete path reaches.
|
|
603
|
+
*
|
|
604
|
+
* @remarks
|
|
605
|
+
* Level `N` keys slot `N`'s range, so a path of one entry per slot ends at the node whose `binding`
|
|
606
|
+
* is set; every inner node's `binding` is `undefined`. The outer `WeakMap` lets a buffer's entries
|
|
607
|
+
* leave with the buffer.
|
|
608
|
+
*
|
|
609
|
+
* @example
|
|
610
|
+
* ```ts
|
|
611
|
+
* const node: WGSLBindingNode<typeof slots, { rows: 'u32' }> = { next: new WeakMap(), binding: undefined }
|
|
612
|
+
* ```
|
|
613
|
+
*/
|
|
614
|
+
export declare interface WGSLBindingNode<S extends WGSLSlotMap, F extends WGSLFieldMap> {
|
|
615
|
+
/** Maps each slot buffer, then its byte offset, then its byte size, to the next level. */
|
|
616
|
+
readonly next: WeakMap<GPUBuffer, Map<number, Map<number, WGSLBindingNode<S, F>>>>;
|
|
617
|
+
/** Holds the binding a complete path reaches, or `undefined` on an inner level. */
|
|
618
|
+
readonly binding: WGSLBinding<S, F> | undefined;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Declares a program's compute entry point: its function name, its workgroup size, and its body.
|
|
623
|
+
*
|
|
624
|
+
* @example
|
|
625
|
+
* ```ts
|
|
626
|
+
* const entry: WGSLEntry = { name: 'main', size: [64, 1, 1], source: 'destination[invocation.local_invocation_index] = 0.0;' }
|
|
627
|
+
* ```
|
|
628
|
+
*/
|
|
629
|
+
export declare interface WGSLEntry {
|
|
630
|
+
/** Names the entry point function. */
|
|
631
|
+
readonly name: string;
|
|
632
|
+
/** Holds the workgroup size on each axis. */
|
|
633
|
+
readonly size: WGSLGrid;
|
|
634
|
+
/** Holds the entry point's WGSL body. */
|
|
635
|
+
readonly source: string;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* Reports one WGSL failure under a stable condition code.
|
|
640
|
+
*
|
|
641
|
+
* @remarks
|
|
642
|
+
* Every refusal the factories, the linker, the helpers, and the slab raise arrives as one of these,
|
|
643
|
+
* so a caller branches on `code` rather than on message text. Narrow a caught value with
|
|
644
|
+
* `isWGSLError` before reading `code`.
|
|
645
|
+
*
|
|
646
|
+
* @example
|
|
647
|
+
* ```ts
|
|
648
|
+
* const error = new WGSLError('The slab capacity must be a multiple of 4.', { code: 'ARGUMENT' })
|
|
649
|
+
* error.code // 'ARGUMENT'
|
|
650
|
+
* ```
|
|
651
|
+
*/
|
|
652
|
+
export declare class WGSLError extends Error {
|
|
653
|
+
readonly name = "WGSLError";
|
|
654
|
+
readonly code: WGSLErrorCode;
|
|
655
|
+
readonly context: Readonly<Record<string, unknown>> | undefined;
|
|
656
|
+
/**
|
|
657
|
+
* Creates one classified WGSL failure.
|
|
658
|
+
*
|
|
659
|
+
* @param message - Human-readable description of what failed
|
|
660
|
+
* @param options - The condition, optional structured context, and optional cause
|
|
661
|
+
*/
|
|
662
|
+
constructor(message: string, options: WGSLErrorOptions);
|
|
663
|
+
}
|
|
664
|
+
|
|
665
|
+
/**
|
|
666
|
+
* Names the condition a `WGSLError` reports.
|
|
667
|
+
*
|
|
668
|
+
* @example
|
|
669
|
+
* ```ts
|
|
670
|
+
* const code: WGSLErrorCode = 'VALIDATION'
|
|
671
|
+
* ```
|
|
672
|
+
*/
|
|
673
|
+
export declare type WGSLErrorCode = (typeof WGSL_ERROR_CODES)[number];
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Configures one WGSL failure's condition, optional context, and optional cause.
|
|
677
|
+
*
|
|
678
|
+
* @example
|
|
679
|
+
* ```ts
|
|
680
|
+
* const options: WGSLErrorOptions = { code: 'LIMIT', context: { total: 70000 } }
|
|
681
|
+
* ```
|
|
682
|
+
*/
|
|
683
|
+
export declare interface WGSLErrorOptions {
|
|
684
|
+
/** Names the failure's condition. */
|
|
685
|
+
readonly code: WGSLErrorCode;
|
|
686
|
+
/** Holds the values that locate the failure. */
|
|
687
|
+
readonly context?: Readonly<Record<string, unknown>>;
|
|
688
|
+
/** Holds the failure this one wraps, kept without coercion. */
|
|
689
|
+
readonly cause?: unknown;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/**
|
|
693
|
+
* Names a WGSL extension a fragment variant can enable.
|
|
694
|
+
*
|
|
695
|
+
* @example
|
|
696
|
+
* ```ts
|
|
697
|
+
* const extension: WGSLExtension = 'subgroups'
|
|
698
|
+
* ```
|
|
699
|
+
*/
|
|
700
|
+
export declare type WGSLExtension = keyof typeof WGSL_EXTENSIONS;
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Maps each uniform struct field name to its scalar type, in declaration order.
|
|
704
|
+
*
|
|
705
|
+
* @example
|
|
706
|
+
* ```ts
|
|
707
|
+
* const fields: WGSLFieldMap = { rows: 'u32', epsilon: 'f32' }
|
|
708
|
+
* ```
|
|
709
|
+
*/
|
|
710
|
+
export declare type WGSLFieldMap = Readonly<Record<string, WGSLScalar>>;
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* Describes a named piece of author-written WGSL text and the fragments it uses.
|
|
714
|
+
*
|
|
715
|
+
* @example
|
|
716
|
+
* ```ts
|
|
717
|
+
* const fragment: WGSLFragment = createWGSLFragment({ name: 'square', source: 'fn square(value: f32) -> f32 { return value * value; }' })
|
|
718
|
+
* ```
|
|
719
|
+
*/
|
|
720
|
+
export declare interface WGSLFragment {
|
|
721
|
+
/** Names the fragment; a linked program holds one fragment per name. */
|
|
722
|
+
readonly name: string;
|
|
723
|
+
/** Holds the fragment's default WGSL text. */
|
|
724
|
+
readonly source: string;
|
|
725
|
+
/** Lists the fragments this fragment's text calls. */
|
|
726
|
+
readonly uses: readonly WGSLFragment[];
|
|
727
|
+
/** Holds the body that replaces `source` when its extension is active. */
|
|
728
|
+
readonly variant?: WGSLVariant;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Carries the input `createWGSLFragment` builds a fragment from.
|
|
733
|
+
*
|
|
734
|
+
* @example
|
|
735
|
+
* ```ts
|
|
736
|
+
* const input: WGSLFragmentInput = { name: 'square', source: 'fn square(value: f32) -> f32 { return value * value; }' }
|
|
737
|
+
* ```
|
|
738
|
+
*/
|
|
739
|
+
export declare interface WGSLFragmentInput {
|
|
740
|
+
/** Names the fragment. */
|
|
741
|
+
readonly name: string;
|
|
742
|
+
/** Holds the fragment's default WGSL text. */
|
|
743
|
+
readonly source: string;
|
|
744
|
+
/** Lists the fragments this fragment's text calls. Default: none. */
|
|
745
|
+
readonly uses?: readonly WGSLFragment[];
|
|
746
|
+
/** Holds the body that replaces `source` when its extension is active. */
|
|
747
|
+
readonly variant?: WGSLVariant;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/**
|
|
751
|
+
* Holds a count on each of the x, y, and z axes: a workgroup size or a dispatch's workgroup counts.
|
|
752
|
+
*
|
|
753
|
+
* @example
|
|
754
|
+
* ```ts
|
|
755
|
+
* const grid: WGSLGrid = [64, 1, 1]
|
|
756
|
+
* ```
|
|
757
|
+
*/
|
|
758
|
+
export declare type WGSLGrid = readonly [x: number, y: number, z: number];
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Describes one uniform struct member: its name, its scalar type, and its offset in 32-bit words.
|
|
762
|
+
*
|
|
763
|
+
* @example
|
|
764
|
+
* ```ts
|
|
765
|
+
* const member: WGSLMember = { name: 'epsilon', element: 'f32', word: 1 }
|
|
766
|
+
* ```
|
|
767
|
+
*/
|
|
768
|
+
export declare interface WGSLMember {
|
|
769
|
+
/** Names the member. */
|
|
770
|
+
readonly name: string;
|
|
771
|
+
/** Names the member's scalar type. */
|
|
772
|
+
readonly element: WGSLScalar;
|
|
773
|
+
/** Holds the member's offset from the struct's start, in 32-bit words. */
|
|
774
|
+
readonly word: number;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* Describes one compute program: its entry point, its slots, its uniform, and its linked fragments.
|
|
779
|
+
*
|
|
780
|
+
* @remarks
|
|
781
|
+
* `fragments` lists every fragment the program reaches, each once, in dependency order.
|
|
782
|
+
*
|
|
783
|
+
* @example
|
|
784
|
+
* ```ts
|
|
785
|
+
* const program: WGSLProgram = createWGSLProgram({ name: 'clear', entry, slots, uniform })
|
|
786
|
+
* ```
|
|
787
|
+
*/
|
|
788
|
+
export declare interface WGSLProgram<S extends WGSLSlotMap = WGSLSlotMap, F extends WGSLFieldMap = WGSLFieldMap> {
|
|
789
|
+
/** Names the program. */
|
|
790
|
+
readonly name: string;
|
|
791
|
+
/** Declares the program's entry point. */
|
|
792
|
+
readonly entry: WGSLEntry;
|
|
793
|
+
/** Declares the program's storage slots. */
|
|
794
|
+
readonly slots: S;
|
|
795
|
+
/** Declares the program's uniform. */
|
|
796
|
+
readonly uniform: WGSLUniform<F>;
|
|
797
|
+
/** Lists the linked fragments, each once, in dependency order. */
|
|
798
|
+
readonly fragments: readonly WGSLFragment[];
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
/**
|
|
802
|
+
* Carries the input `createWGSLProgram` builds a program from.
|
|
803
|
+
*
|
|
804
|
+
* @example
|
|
805
|
+
* ```ts
|
|
806
|
+
* const input: WGSLProgramInput<typeof slots, { rows: 'u32' }> = { name: 'clear', entry, slots, uniform }
|
|
807
|
+
* ```
|
|
808
|
+
*/
|
|
809
|
+
export declare interface WGSLProgramInput<S extends WGSLSlotMap, F extends WGSLFieldMap> {
|
|
810
|
+
/** Names the program. */
|
|
811
|
+
readonly name: string;
|
|
812
|
+
/** Declares the program's entry point. */
|
|
813
|
+
readonly entry: WGSLEntry;
|
|
814
|
+
/** Declares the program's storage slots. */
|
|
815
|
+
readonly slots: S;
|
|
816
|
+
/** Declares the program's uniform. */
|
|
817
|
+
readonly uniform: WGSLUniform<F>;
|
|
818
|
+
/** Lists the fragments the entry body calls. Default: none. */
|
|
819
|
+
readonly uses?: readonly WGSLFragment[];
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
/**
|
|
823
|
+
* Holds one read a batch recorded: the staging buffer its copy lands in, its byte count, and the caller's promise.
|
|
824
|
+
*
|
|
825
|
+
* @example
|
|
826
|
+
* ```ts
|
|
827
|
+
* const read: WGSLRead = [staging, 256, Promise.withResolvers<ArrayBuffer>()]
|
|
828
|
+
* ```
|
|
829
|
+
*/
|
|
830
|
+
export declare type WGSLRead = readonly [
|
|
831
|
+
staging: GPUBuffer,
|
|
832
|
+
size: number,
|
|
833
|
+
result: PromiseWithResolvers<ArrayBuffer>
|
|
834
|
+
];
|
|
835
|
+
|
|
836
|
+
/**
|
|
837
|
+
* Names a scalar type a uniform struct member holds.
|
|
838
|
+
*
|
|
839
|
+
* @example
|
|
840
|
+
* ```ts
|
|
841
|
+
* const element: WGSLScalar = 'f32'
|
|
842
|
+
* ```
|
|
843
|
+
*/
|
|
844
|
+
export declare type WGSLScalar = (typeof WGSL_SCALARS)[number];
|
|
845
|
+
|
|
846
|
+
/**
|
|
847
|
+
* Prepares programs as statements on a borrowed device and opens one batch of dispatches at a time.
|
|
848
|
+
*
|
|
849
|
+
* @remarks
|
|
850
|
+
* The session reads its limits and features from the device and never requests or destroys the
|
|
851
|
+
* device. An extension is active when the device reports its feature and the `extensions` option
|
|
852
|
+
* does not switch it off. Pipelines sit in a table keyed on the rendered WGSL text: a ready map
|
|
853
|
+
* beside a pending map, so a warm `prepare` creates no pipeline. A pending entry that fails with any
|
|
854
|
+
* code but `COMPILE` leaves the table, so a later `prepare` retries it. Shader compilation runs
|
|
855
|
+
* inside one synchronous push and pop of a `validation` error scope, and a compiler error rejects
|
|
856
|
+
* that `prepare` with code `COMPILE` and its `line` and `column` in `context`; the session stays
|
|
857
|
+
* `open`. Thrown with code `LIMIT` when a program's workgroup size or slot count passes the device's
|
|
858
|
+
* limits, and with code `VALIDATION` from `batch()` while another batch is open. A device failure on
|
|
859
|
+
* a submitted batch, or the device's `lost` promise, moves the session to `failed`, making
|
|
860
|
+
* `prepare`, `batch`, and any open batch's `encode`, `read`, and `submit` throw that failure while
|
|
861
|
+
* `destroy` resolves. `destroy` moves the session to `destroyed` at once, aborts the open batch,
|
|
862
|
+
* waits for submitted work and reads, and destroys the uniform buffer and the staging buffers the
|
|
863
|
+
* session created; a repeated `destroy` returns the same promise, settled `abort` and `execute`
|
|
864
|
+
* replay their batch's own settlement, and `prepare`, `batch`, `encode`, `read`, and `submit` throw
|
|
865
|
+
* with code `DESTROYED`. Thrown with code `ARGUMENT`
|
|
866
|
+
* from the constructor when `uniform.size` is not a positive safe multiple of 4.
|
|
867
|
+
*
|
|
868
|
+
* @example
|
|
869
|
+
* ```ts
|
|
870
|
+
* const session = new WGSLSession(device, { extensions: { f16: false } })
|
|
871
|
+
* const statement = await session.prepare(program)
|
|
872
|
+
* await statement.execute(statement.bind({ destination: { buffer } }), { count: 64 }, [1, 1, 1])
|
|
873
|
+
* await session.destroy()
|
|
874
|
+
* ```
|
|
875
|
+
*/
|
|
876
|
+
export declare class WGSLSession implements WGSLSessionInterface {
|
|
877
|
+
#private;
|
|
878
|
+
/**
|
|
879
|
+
* Creates an open session over a borrowed device, with its uniform slab and uniform layout.
|
|
880
|
+
*
|
|
881
|
+
* @param device - The device the session borrows
|
|
882
|
+
* @param options - The extensions to switch off and the slab's byte size
|
|
883
|
+
*/
|
|
884
|
+
constructor(device: GPUDevice, options?: WGSLSessionOptions);
|
|
885
|
+
get device(): GPUDevice;
|
|
886
|
+
get extensions(): Readonly<Record<WGSLExtension, boolean>>;
|
|
887
|
+
get status(): WGSLSessionStatus;
|
|
888
|
+
prepare<S extends WGSLSlotMap, F extends WGSLFieldMap>(program: WGSLProgram<S, F>): Promise<WGSLStatementInterface<S, F>>;
|
|
889
|
+
batch(): WGSLBatchInterface;
|
|
890
|
+
destroy(): Promise<void>;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
/**
|
|
894
|
+
* Prepares programs on a borrowed device and opens batches of dispatches.
|
|
895
|
+
*
|
|
896
|
+
* @example
|
|
897
|
+
* ```ts
|
|
898
|
+
* const session: WGSLSessionInterface = createWGSLSession(device)
|
|
899
|
+
* ```
|
|
900
|
+
*/
|
|
901
|
+
export declare interface WGSLSessionInterface {
|
|
902
|
+
/** Holds the borrowed device. */
|
|
903
|
+
readonly device: GPUDevice;
|
|
904
|
+
/** Reports whether each extension is active on this session. */
|
|
905
|
+
readonly extensions: Readonly<Record<WGSLExtension, boolean>>;
|
|
906
|
+
/** Reports the session's lifecycle status. */
|
|
907
|
+
readonly status: WGSLSessionStatus;
|
|
908
|
+
/** Prepares a program as a statement on the session's device. */
|
|
909
|
+
prepare<S extends WGSLSlotMap, F extends WGSLFieldMap>(program: WGSLProgram<S, F>): Promise<WGSLStatementInterface<S, F>>;
|
|
910
|
+
/** Opens the session's one batch. */
|
|
911
|
+
batch(): WGSLBatchInterface;
|
|
912
|
+
/** Releases every resource the session created, leaving the device open. */
|
|
913
|
+
destroy(): Promise<void>;
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
/**
|
|
917
|
+
* Configures a session's extensions and its uniform slab.
|
|
918
|
+
*
|
|
919
|
+
* @remarks
|
|
920
|
+
* `extensions` switches an extension the device supports off; it never switches one on.
|
|
921
|
+
* `uniform.size` sets the slab's byte capacity. Default: `WGSL_UNIFORM_SIZE`.
|
|
922
|
+
*
|
|
923
|
+
* @example
|
|
924
|
+
* ```ts
|
|
925
|
+
* const options: WGSLSessionOptions = { extensions: { f16: false }, uniform: { size: 16384 } }
|
|
926
|
+
* ```
|
|
927
|
+
*/
|
|
928
|
+
export declare interface WGSLSessionOptions {
|
|
929
|
+
/** Switches each named extension off when `false`. */
|
|
930
|
+
readonly extensions?: Partial<Readonly<Record<WGSLExtension, boolean>>>;
|
|
931
|
+
/** Configures the uniform slab. */
|
|
932
|
+
readonly uniform?: {
|
|
933
|
+
readonly size?: number;
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* Names the lifecycle status of a session.
|
|
939
|
+
*
|
|
940
|
+
* @example
|
|
941
|
+
* ```ts
|
|
942
|
+
* const status: WGSLSessionStatus = 'open'
|
|
943
|
+
* ```
|
|
944
|
+
*/
|
|
945
|
+
export declare type WGSLSessionStatus = 'open' | 'failed' | 'destroyed';
|
|
946
|
+
|
|
947
|
+
/**
|
|
948
|
+
* Packs each dispatch's uniform values into one host buffer at the device's uniform offset alignment and uploads them in one write.
|
|
949
|
+
*
|
|
950
|
+
* @remarks
|
|
951
|
+
* The slab owns one `ArrayBuffer` with a `Uint32Array` view and a `Float32Array` view over it, and
|
|
952
|
+
* steps a struct's frozen `members` for each write. An `f32` member writes through the float view,
|
|
953
|
+
* so `-0` and subnormals keep their binary32 patterns; a `u32` or `i32` member writes through the
|
|
954
|
+
* unsigned view, so `-5` stores `0xFFFFFFFB`. Each write starts at the next multiple of the
|
|
955
|
+
* device's `minUniformBufferOffsetAlignment`. A write that meets a missing value zeroes the span it
|
|
956
|
+
* started and throws, so the bytes past `used` stay zero. `clear` zeroes the bytes the written
|
|
957
|
+
* values spanned.
|
|
958
|
+
* The constructor throws with code `ARGUMENT` when `capacity` is not a positive safe multiple of 4
|
|
959
|
+
* or passes the buffer's size, when the buffer lacks the `uniform` or `destination` usage, and when
|
|
960
|
+
* the device reports an alignment that is not a positive safe multiple of 4.
|
|
961
|
+
*
|
|
962
|
+
* @example
|
|
963
|
+
* ```ts
|
|
964
|
+
* const slab = new WGSLSlab(device, { buffer, capacity: 65536 })
|
|
965
|
+
* slab.write(createWGSLStruct('Shape', { rows: 'u32' }), { rows: 64 }) // 0
|
|
966
|
+
* slab.upload()
|
|
967
|
+
* ```
|
|
968
|
+
*/
|
|
969
|
+
export declare class WGSLSlab implements WGSLSlabInterface {
|
|
970
|
+
#private;
|
|
971
|
+
/**
|
|
972
|
+
* Creates an empty slab over a uniform buffer.
|
|
973
|
+
*
|
|
974
|
+
* @param device - The device whose queue receives the upload and whose limits set the alignment
|
|
975
|
+
* @param options - The uniform buffer and the slab's byte capacity
|
|
976
|
+
*/
|
|
977
|
+
constructor(device: GPUDevice, options: WGSLSlabOptions);
|
|
978
|
+
get capacity(): number;
|
|
979
|
+
get used(): number;
|
|
980
|
+
write<F extends WGSLFieldMap>(struct: WGSLStruct<F>, values: WGSLValueMap<F>): number | undefined;
|
|
981
|
+
upload(): void;
|
|
982
|
+
clear(): void;
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
/**
|
|
986
|
+
* Packs each dispatch's uniform values at aligned offsets and uploads them in one write.
|
|
987
|
+
*
|
|
988
|
+
* @example
|
|
989
|
+
* ```ts
|
|
990
|
+
* const slab: WGSLSlabInterface = new WGSLSlab(device, { buffer, capacity: 65536 })
|
|
991
|
+
* ```
|
|
992
|
+
*/
|
|
993
|
+
export declare interface WGSLSlabInterface {
|
|
994
|
+
/** Holds the slab's byte capacity. */
|
|
995
|
+
readonly capacity: number;
|
|
996
|
+
/** Reports the byte length the written values span. */
|
|
997
|
+
readonly used: number;
|
|
998
|
+
/** Writes one dispatch's values and returns their byte offset, or `undefined` when full. */
|
|
999
|
+
write<F extends WGSLFieldMap>(struct: WGSLStruct<F>, values: WGSLValueMap<F>): number | undefined;
|
|
1000
|
+
/** Uploads the written values to the slab's buffer. */
|
|
1001
|
+
upload(): void;
|
|
1002
|
+
/** Empties the slab for the next batch. */
|
|
1003
|
+
clear(): void;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
/**
|
|
1007
|
+
* Configures a uniform slab: the uniform buffer it uploads to and its byte capacity.
|
|
1008
|
+
*
|
|
1009
|
+
* @remarks
|
|
1010
|
+
* `buffer` must carry the `uniform` and `destination` usages, and `capacity` must not pass its
|
|
1011
|
+
* size.
|
|
1012
|
+
*
|
|
1013
|
+
* @example
|
|
1014
|
+
* ```ts
|
|
1015
|
+
* const options: WGSLSlabOptions = { buffer, capacity: 65536 }
|
|
1016
|
+
* ```
|
|
1017
|
+
*/
|
|
1018
|
+
export declare interface WGSLSlabOptions {
|
|
1019
|
+
/** Holds the uniform buffer the slab uploads to. */
|
|
1020
|
+
readonly buffer: GPUBuffer;
|
|
1021
|
+
/** Holds the slab's byte capacity. */
|
|
1022
|
+
readonly capacity: number;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
/**
|
|
1026
|
+
* Declares one storage slot of a program: its access mode and its array element type.
|
|
1027
|
+
*
|
|
1028
|
+
* @example
|
|
1029
|
+
* ```ts
|
|
1030
|
+
* const slot: WGSLSlot = { access: 'read_write', element: 'f32' }
|
|
1031
|
+
* ```
|
|
1032
|
+
*/
|
|
1033
|
+
export declare interface WGSLSlot {
|
|
1034
|
+
/** Names the slot's access mode. */
|
|
1035
|
+
readonly access: WGSLAccess;
|
|
1036
|
+
/** Names the scalar type of the slot's array elements. */
|
|
1037
|
+
readonly element: WGSLScalar;
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
/**
|
|
1041
|
+
* Maps each storage slot name of a program to its declaration, in binding order.
|
|
1042
|
+
*
|
|
1043
|
+
* @example
|
|
1044
|
+
* ```ts
|
|
1045
|
+
* const slots: WGSLSlotMap = { source: { access: 'read', element: 'f32' } }
|
|
1046
|
+
* ```
|
|
1047
|
+
*/
|
|
1048
|
+
export declare type WGSLSlotMap = Readonly<Record<string, WGSLSlot>>;
|
|
1049
|
+
|
|
1050
|
+
/**
|
|
1051
|
+
* Runs one prepared program on its session's device and caches a storage bind group per set of slot ranges.
|
|
1052
|
+
*
|
|
1053
|
+
* @remarks
|
|
1054
|
+
* `bind` walks a per-statement `WeakMap` trie keyed on each slot's buffer, byte offset, and byte
|
|
1055
|
+
* size, so a repeated range set returns the same `WGSLBinding` without allocating, and a destroyed
|
|
1056
|
+
* buffer's entries leave with the buffer. A range set the trie lacks is checked before its bind
|
|
1057
|
+
* group is created: thrown with code `ARGUMENT` when the slot names differ from the program's, when
|
|
1058
|
+
* a buffer lacks the `storage` usage, when an offset is not a multiple of the device's
|
|
1059
|
+
* `minStorageBufferOffsetAlignment`, when a size is under 4 bytes or not a multiple of 4, and when a
|
|
1060
|
+
* range passes its buffer's end; with code `LIMIT` when a size passes the device's
|
|
1061
|
+
* `maxStorageBufferBindingSize`; and with code `VALIDATION` when a `read_write` slot shares its
|
|
1062
|
+
* `GPUBuffer` with any other slot, even over disjoint ranges, because Dawn refuses that pair.
|
|
1063
|
+
* `execute` opens a batch on the session, records one dispatch, and resolves when the device
|
|
1064
|
+
* finishes it.
|
|
1065
|
+
*
|
|
1066
|
+
* @example
|
|
1067
|
+
* ```ts
|
|
1068
|
+
* const statement = await session.prepare(program)
|
|
1069
|
+
* const binding = statement.bind({ destination: { buffer } })
|
|
1070
|
+
* await statement.execute(binding, { count: 64 }, [1, 1, 1])
|
|
1071
|
+
* ```
|
|
1072
|
+
*/
|
|
1073
|
+
export declare class WGSLStatement<S extends WGSLSlotMap, F extends WGSLFieldMap> implements WGSLStatementInterface<S, F> {
|
|
1074
|
+
#private;
|
|
1075
|
+
readonly program: WGSLProgram<S, F>;
|
|
1076
|
+
readonly pipeline: GPUComputePipeline;
|
|
1077
|
+
readonly uniform: GPUBindGroup;
|
|
1078
|
+
/**
|
|
1079
|
+
* Creates a statement over a pipeline its session prepared.
|
|
1080
|
+
*
|
|
1081
|
+
* @param session - The session whose device creates the bind groups and whose batches run the dispatches
|
|
1082
|
+
* @param options - The program, the pipeline, the storage layout, and the uniform bind group
|
|
1083
|
+
*/
|
|
1084
|
+
constructor(session: WGSLSessionInterface, options: WGSLStatementOptions<S, F>);
|
|
1085
|
+
bind(buffers: {
|
|
1086
|
+
readonly [K in keyof S]: GPUBufferBinding;
|
|
1087
|
+
}): WGSLBinding<S, F>;
|
|
1088
|
+
execute(binding: WGSLBinding<S, F>, values: WGSLValueMap<F>, grid: WGSLGrid, signal?: AbortSignal): Promise<void>;
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
/**
|
|
1092
|
+
* Runs one prepared program on its session's device.
|
|
1093
|
+
*
|
|
1094
|
+
* @example
|
|
1095
|
+
* ```ts
|
|
1096
|
+
* const statement: WGSLStatementInterface<typeof slots, { rows: 'u32' }> = await session.prepare(program)
|
|
1097
|
+
* ```
|
|
1098
|
+
*/
|
|
1099
|
+
export declare interface WGSLStatementInterface<S extends WGSLSlotMap, F extends WGSLFieldMap> {
|
|
1100
|
+
/** Holds the prepared program. */
|
|
1101
|
+
readonly program: WGSLProgram<S, F>;
|
|
1102
|
+
/** Holds the compute pipeline. */
|
|
1103
|
+
readonly pipeline: GPUComputePipeline;
|
|
1104
|
+
/** Holds the uniform bind group over the session's slab. */
|
|
1105
|
+
readonly uniform: GPUBindGroup;
|
|
1106
|
+
/** Binds one buffer range to each slot. */
|
|
1107
|
+
bind(buffers: {
|
|
1108
|
+
readonly [K in keyof S]: GPUBufferBinding;
|
|
1109
|
+
}): WGSLBinding<S, F>;
|
|
1110
|
+
/** Dispatches the statement once and resolves when the device finishes it. */
|
|
1111
|
+
execute(binding: WGSLBinding<S, F>, values: WGSLValueMap<F>, grid: WGSLGrid, signal?: AbortSignal): Promise<void>;
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
/**
|
|
1115
|
+
* Configures a statement: its program, its compute pipeline, its storage bind-group layout, and its uniform bind group.
|
|
1116
|
+
*
|
|
1117
|
+
* @remarks
|
|
1118
|
+
* `layout` is the `@group(0)` storage layout the pipeline was created with, and `uniform` is the
|
|
1119
|
+
* session's `@group(1)` bind group over the slab for the program's struct size.
|
|
1120
|
+
*
|
|
1121
|
+
* @example
|
|
1122
|
+
* ```ts
|
|
1123
|
+
* const options: WGSLStatementOptions<typeof slots, { rows: 'u32' }> = { program, pipeline, layout, uniform }
|
|
1124
|
+
* ```
|
|
1125
|
+
*/
|
|
1126
|
+
export declare interface WGSLStatementOptions<S extends WGSLSlotMap, F extends WGSLFieldMap> {
|
|
1127
|
+
/** Holds the prepared program. */
|
|
1128
|
+
readonly program: WGSLProgram<S, F>;
|
|
1129
|
+
/** Holds the compute pipeline. */
|
|
1130
|
+
readonly pipeline: GPUComputePipeline;
|
|
1131
|
+
/** Holds the storage bind-group layout of the pipeline's `@group(0)`. */
|
|
1132
|
+
readonly layout: GPUBindGroupLayout;
|
|
1133
|
+
/** Holds the uniform bind group of the pipeline's `@group(1)`. */
|
|
1134
|
+
readonly uniform: GPUBindGroup;
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
/**
|
|
1138
|
+
* Describes a uniform struct: its fields, its byte layout, and its rendered WGSL declaration.
|
|
1139
|
+
*
|
|
1140
|
+
* @remarks
|
|
1141
|
+
* `size` is the struct's byte size, `offsets` maps each field to its byte offset, `members` lists
|
|
1142
|
+
* the fields in declaration order with their word offsets, and `source` holds the rendered
|
|
1143
|
+
* `struct` declaration.
|
|
1144
|
+
*
|
|
1145
|
+
* @example
|
|
1146
|
+
* ```ts
|
|
1147
|
+
* const struct: WGSLStruct<{ rows: 'u32' }> = createWGSLStruct('Shape', { rows: 'u32' })
|
|
1148
|
+
* ```
|
|
1149
|
+
*/
|
|
1150
|
+
export declare interface WGSLStruct<F extends WGSLFieldMap = WGSLFieldMap> {
|
|
1151
|
+
/** Names the struct in WGSL. */
|
|
1152
|
+
readonly name: string;
|
|
1153
|
+
/** Maps each field to its scalar type, in declaration order. */
|
|
1154
|
+
readonly fields: F;
|
|
1155
|
+
/** Holds the struct's size in bytes. */
|
|
1156
|
+
readonly size: number;
|
|
1157
|
+
/** Maps each field to its offset from the struct's start, in bytes. */
|
|
1158
|
+
readonly offsets: Readonly<Record<keyof F, number>>;
|
|
1159
|
+
/** Lists the struct's members in declaration order. */
|
|
1160
|
+
readonly members: readonly WGSLMember[];
|
|
1161
|
+
/** Holds the rendered WGSL `struct` declaration. */
|
|
1162
|
+
readonly source: string;
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
/**
|
|
1166
|
+
* Declares a program's uniform: the variable name its text reads and the struct it holds.
|
|
1167
|
+
*
|
|
1168
|
+
* @example
|
|
1169
|
+
* ```ts
|
|
1170
|
+
* const uniform: WGSLUniform<{ rows: 'u32' }> = { name: 'shape', struct: createWGSLStruct('Shape', { rows: 'u32' }) }
|
|
1171
|
+
* ```
|
|
1172
|
+
*/
|
|
1173
|
+
export declare interface WGSLUniform<F extends WGSLFieldMap = WGSLFieldMap> {
|
|
1174
|
+
/** Names the uniform variable in WGSL. */
|
|
1175
|
+
readonly name: string;
|
|
1176
|
+
/** Holds the uniform's struct. */
|
|
1177
|
+
readonly struct: WGSLStruct<F>;
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
/**
|
|
1181
|
+
* Maps each field of a uniform struct to the number one dispatch writes for it.
|
|
1182
|
+
*
|
|
1183
|
+
* @example
|
|
1184
|
+
* ```ts
|
|
1185
|
+
* const values: WGSLValueMap<{ rows: 'u32'; epsilon: 'f32' }> = { rows: 64, epsilon: 1e-6 }
|
|
1186
|
+
* ```
|
|
1187
|
+
*/
|
|
1188
|
+
export declare type WGSLValueMap<F extends WGSLFieldMap> = {
|
|
1189
|
+
readonly [K in keyof F]: number;
|
|
1190
|
+
};
|
|
1191
|
+
|
|
1192
|
+
/**
|
|
1193
|
+
* Describes a fragment body that replaces the default body when its extension is active.
|
|
1194
|
+
*
|
|
1195
|
+
* @example
|
|
1196
|
+
* ```ts
|
|
1197
|
+
* const variant: WGSLVariant = { extension: 'subgroups', source: 'fn total(value: f32) -> f32 { return subgroupAdd(value); }' }
|
|
1198
|
+
* ```
|
|
1199
|
+
*/
|
|
1200
|
+
export declare interface WGSLVariant {
|
|
1201
|
+
/** Names the extension the variant needs. */
|
|
1202
|
+
readonly extension: WGSLExtension;
|
|
1203
|
+
/** Holds the variant's WGSL text, with the same signatures as the default body. */
|
|
1204
|
+
readonly source: string;
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
export { }
|