@orkestrel/wgsl 0.0.2 → 0.0.3
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/src/core/index.cjs +198 -98
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +129 -44
- package/dist/src/core/index.d.ts +129 -44
- package/dist/src/core/index.js +199 -99
- package/dist/src/core/index.js.map +1 -1
- package/package.json +1 -1
package/dist/src/core/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { INTRINSICS, errorToMessage, holds, isArray, isError, isFunction, isNonEmptyString, isNonNegativeInteger, isObject, isRecord, isSafeInteger, isString, preview } from "@orkestrel/contract";
|
|
1
|
+
import { INTRINSICS, defaultOption, errorToMessage, groupOption, holds, isArray, isBoolean, isError, isFunction, isNonEmptyString, isNonNegativeInteger, isNumber, isObject, isRecord, isSafeInteger, isString, optionFaultToError, optionalGroupOption, optionalOption, preview, resolveOptions } from "@orkestrel/contract";
|
|
2
2
|
// src/core/constants.ts
|
|
3
3
|
/**
|
|
4
4
|
* Lists the condition codes a `WGSLError` carries.
|
|
@@ -145,14 +145,17 @@ var WGSL_STAGE = Object.freeze({ compute: 4 });
|
|
|
145
145
|
*/
|
|
146
146
|
var WGSL_UNIFORM_SIZE = 65536;
|
|
147
147
|
/**
|
|
148
|
-
* Holds the fragment that reads a workgroup's two-axis index from the entry
|
|
148
|
+
* Holds the fragment that reads a workgroup's two-axis index from the entry's `Dispatch` struct.
|
|
149
149
|
*
|
|
150
150
|
* @remarks
|
|
151
|
-
* It declares `fn
|
|
152
|
-
* `
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
151
|
+
* It declares `fn readPlace(dispatch: Dispatch) -> u32`, which returns
|
|
152
|
+
* `dispatch.workgroup_id.x + dispatch.workgroup_id.y * dispatch.num_workgroups.x`, the statement of
|
|
153
|
+
* `@orkestrel/infer`'s `readPlace`. `Dispatch` holds only values that stay uniform across a
|
|
154
|
+
* workgroup, so a call that returns early on the result stays in uniform control flow before a
|
|
155
|
+
* later `workgroupBarrier()`. `Invocation` holds the per-lane builtins and is not a parameter. Pair
|
|
156
|
+
* the fragment with `computeWGSLGrid`, and multiply by the workgroup size and add
|
|
157
|
+
* `invocation.local_invocation_index` for an invocation's index. It reads no binding and no uniform
|
|
158
|
+
* member.
|
|
156
159
|
*
|
|
157
160
|
* @example
|
|
158
161
|
* ```ts
|
|
@@ -160,10 +163,10 @@ var WGSL_UNIFORM_SIZE = 65536;
|
|
|
160
163
|
* ```
|
|
161
164
|
*/
|
|
162
165
|
var WGSL_PLACE = Object.freeze({
|
|
163
|
-
name: "
|
|
166
|
+
name: "readPlace",
|
|
164
167
|
source: [
|
|
165
|
-
"fn
|
|
166
|
-
" return
|
|
168
|
+
"fn readPlace(dispatch: Dispatch) -> u32 {",
|
|
169
|
+
" return dispatch.workgroup_id.x + dispatch.workgroup_id.y * dispatch.num_workgroups.x;",
|
|
167
170
|
"}"
|
|
168
171
|
].join("\n"),
|
|
169
172
|
uses: Object.freeze([])
|
|
@@ -172,13 +175,16 @@ var WGSL_PLACE = Object.freeze({
|
|
|
172
175
|
* Holds the fragment that sums one `f32` value from each invocation of a 64-invocation workgroup.
|
|
173
176
|
*
|
|
174
177
|
* @remarks
|
|
175
|
-
* It declares `var<workgroup>
|
|
176
|
-
* `fn
|
|
177
|
-
* invocation. The entry's workgroup size must hold 64 invocations, and every invocation
|
|
178
|
-
* it from uniform control flow.
|
|
178
|
+
* It declares `var<workgroup> sumShared: array<f32, 64>`, 256 bytes, and
|
|
179
|
+
* `fn sumPartials(invocation: Invocation, value: f32) -> f32`, which returns the workgroup's total
|
|
180
|
+
* to every invocation. The entry's workgroup size must hold 64 invocations, and every invocation
|
|
181
|
+
* must call it from uniform control flow. It takes `Invocation` and never `Dispatch`, because it
|
|
182
|
+
* reads only `local_invocation_index` and, in the variant, `subgroup_invocation_id`. The default
|
|
183
|
+
* body reduces in a tree of halving strides. The
|
|
179
184
|
* `subgroups` variant stores each subgroup's `subgroupAdd` partial at its first invocation's index
|
|
180
|
-
* and `0.0` elsewhere, and invocation 0 adds the 64 entries in index order between 2 barriers
|
|
181
|
-
* its rounding order differs from the default body's. It reads no
|
|
185
|
+
* and `0.0` elsewhere, and invocation 0 adds the 64 entries in index order between 2 barriers; the
|
|
186
|
+
* variant is `reordered`, so its rounding order differs from the default body's. It reads no
|
|
187
|
+
* binding and no uniform member.
|
|
182
188
|
*
|
|
183
189
|
* @example
|
|
184
190
|
* ```ts
|
|
@@ -186,21 +192,21 @@ var WGSL_PLACE = Object.freeze({
|
|
|
186
192
|
* ```
|
|
187
193
|
*/
|
|
188
194
|
var WGSL_SUM = Object.freeze({
|
|
189
|
-
name: "
|
|
195
|
+
name: "sumPartials",
|
|
190
196
|
source: [
|
|
191
|
-
"var<workgroup>
|
|
197
|
+
"var<workgroup> sumShared: array<f32, 64>;",
|
|
192
198
|
"",
|
|
193
|
-
"fn
|
|
199
|
+
"fn sumPartials(invocation: Invocation, value: f32) -> f32 {",
|
|
194
200
|
" let local = invocation.local_invocation_index;",
|
|
195
|
-
"
|
|
201
|
+
" sumShared[local] = value;",
|
|
196
202
|
" workgroupBarrier();",
|
|
197
203
|
" for (var stride = 32u; stride > 0u; stride = stride >> 1u) {",
|
|
198
204
|
" if (local < stride) {",
|
|
199
|
-
"
|
|
205
|
+
" sumShared[local] = sumShared[local] + sumShared[local + stride];",
|
|
200
206
|
" }",
|
|
201
207
|
" workgroupBarrier();",
|
|
202
208
|
" }",
|
|
203
|
-
" let total =
|
|
209
|
+
" let total = sumShared[0];",
|
|
204
210
|
" workgroupBarrier();",
|
|
205
211
|
" return total;",
|
|
206
212
|
"}"
|
|
@@ -209,22 +215,22 @@ var WGSL_SUM = Object.freeze({
|
|
|
209
215
|
variant: Object.freeze({
|
|
210
216
|
extension: "subgroups",
|
|
211
217
|
source: [
|
|
212
|
-
"var<workgroup>
|
|
218
|
+
"var<workgroup> sumShared: array<f32, 64>;",
|
|
213
219
|
"",
|
|
214
|
-
"fn
|
|
220
|
+
"fn sumPartials(invocation: Invocation, value: f32) -> f32 {",
|
|
215
221
|
" let local = invocation.local_invocation_index;",
|
|
216
222
|
" let partial = subgroupAdd(value);",
|
|
217
|
-
"
|
|
223
|
+
" sumShared[local] = select(0.0, partial, invocation.subgroup_invocation_id == 0u);",
|
|
218
224
|
" workgroupBarrier();",
|
|
219
225
|
" if (local == 0u) {",
|
|
220
226
|
" var acc = 0.0;",
|
|
221
227
|
" for (var i = 0u; i < 64u; i += 1u) {",
|
|
222
|
-
" acc = acc +
|
|
228
|
+
" acc = acc + sumShared[i];",
|
|
223
229
|
" }",
|
|
224
|
-
"
|
|
230
|
+
" sumShared[0] = acc;",
|
|
225
231
|
" }",
|
|
226
232
|
" workgroupBarrier();",
|
|
227
|
-
" let total =
|
|
233
|
+
" let total = sumShared[0];",
|
|
228
234
|
" workgroupBarrier();",
|
|
229
235
|
" return total;",
|
|
230
236
|
"}"
|
|
@@ -235,13 +241,14 @@ var WGSL_SUM = Object.freeze({
|
|
|
235
241
|
* Holds the fragment that finds the largest `f32` value across the invocations of a 64-invocation workgroup.
|
|
236
242
|
*
|
|
237
243
|
* @remarks
|
|
238
|
-
* It declares `var<workgroup>
|
|
239
|
-
* `fn
|
|
240
|
-
* every invocation. The entry's workgroup size must hold 64 invocations, and every
|
|
241
|
-
* call it from uniform control flow.
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
* the
|
|
244
|
+
* It declares `var<workgroup> peakShared: array<f32, 64>`, 256 bytes, and
|
|
245
|
+
* `fn peakPartials(invocation: Invocation, value: f32) -> f32`, which returns the workgroup's
|
|
246
|
+
* maximum to every invocation. The entry's workgroup size must hold 64 invocations, and every
|
|
247
|
+
* invocation must call it from uniform control flow. It takes `Invocation` and never `Dispatch`. The
|
|
248
|
+
* default body reduces with `max` in a tree of halving strides. The `subgroups` variant is
|
|
249
|
+
* `reordered`: it stores each subgroup's `subgroupMax` partial at its first invocation's index and
|
|
250
|
+
* the lowest finite `f32` (`-3.4028234663852886e38`) elsewhere, and invocation 0 folds the 64 entries
|
|
251
|
+
* with `max` between 2 barriers (matching `@orkestrel/infer`, so it returns `-3.4028234663852886e38`
|
|
245
252
|
* rather than `-Infinity` when every input is `-Infinity`). It reads no binding and no uniform member.
|
|
246
253
|
*
|
|
247
254
|
* @example
|
|
@@ -250,21 +257,21 @@ var WGSL_SUM = Object.freeze({
|
|
|
250
257
|
* ```
|
|
251
258
|
*/
|
|
252
259
|
var WGSL_PEAK = Object.freeze({
|
|
253
|
-
name: "
|
|
260
|
+
name: "peakPartials",
|
|
254
261
|
source: [
|
|
255
|
-
"var<workgroup>
|
|
262
|
+
"var<workgroup> peakShared: array<f32, 64>;",
|
|
256
263
|
"",
|
|
257
|
-
"fn
|
|
264
|
+
"fn peakPartials(invocation: Invocation, value: f32) -> f32 {",
|
|
258
265
|
" let local = invocation.local_invocation_index;",
|
|
259
|
-
"
|
|
266
|
+
" peakShared[local] = value;",
|
|
260
267
|
" workgroupBarrier();",
|
|
261
268
|
" for (var stride = 32u; stride > 0u; stride = stride >> 1u) {",
|
|
262
269
|
" if (local < stride) {",
|
|
263
|
-
"
|
|
270
|
+
" peakShared[local] = max(peakShared[local], peakShared[local + stride]);",
|
|
264
271
|
" }",
|
|
265
272
|
" workgroupBarrier();",
|
|
266
273
|
" }",
|
|
267
|
-
" let largest =
|
|
274
|
+
" let largest = peakShared[0];",
|
|
268
275
|
" workgroupBarrier();",
|
|
269
276
|
" return largest;",
|
|
270
277
|
"}"
|
|
@@ -273,22 +280,22 @@ var WGSL_PEAK = Object.freeze({
|
|
|
273
280
|
variant: Object.freeze({
|
|
274
281
|
extension: "subgroups",
|
|
275
282
|
source: [
|
|
276
|
-
"var<workgroup>
|
|
283
|
+
"var<workgroup> peakShared: array<f32, 64>;",
|
|
277
284
|
"",
|
|
278
|
-
"fn
|
|
285
|
+
"fn peakPartials(invocation: Invocation, value: f32) -> f32 {",
|
|
279
286
|
" let local = invocation.local_invocation_index;",
|
|
280
287
|
" let partial = subgroupMax(value);",
|
|
281
|
-
"
|
|
288
|
+
" peakShared[local] = select(-3.4028234663852886e38, partial, invocation.subgroup_invocation_id == 0u);",
|
|
282
289
|
" workgroupBarrier();",
|
|
283
290
|
" if (local == 0u) {",
|
|
284
291
|
" var acc = -3.4028234663852886e38;",
|
|
285
292
|
" for (var i = 0u; i < 64u; i += 1u) {",
|
|
286
|
-
" acc = max(acc,
|
|
293
|
+
" acc = max(acc, peakShared[i]);",
|
|
287
294
|
" }",
|
|
288
|
-
"
|
|
295
|
+
" peakShared[0] = acc;",
|
|
289
296
|
" }",
|
|
290
297
|
" workgroupBarrier();",
|
|
291
|
-
" let largest =
|
|
298
|
+
" let largest = peakShared[0];",
|
|
292
299
|
" workgroupBarrier();",
|
|
293
300
|
" return largest;",
|
|
294
301
|
"}"
|
|
@@ -299,7 +306,8 @@ var WGSL_PEAK = Object.freeze({
|
|
|
299
306
|
* Holds the fragment that widens the IEEE 754 half-precision bits in the low 16 bits of a `u32` to an `f32`.
|
|
300
307
|
*
|
|
301
308
|
* @remarks
|
|
302
|
-
* It declares `fn
|
|
309
|
+
* It declares `fn widenHalf(bits: u32) -> f32`, which takes neither `Invocation` nor `Dispatch`
|
|
310
|
+
* and builds the result from integer bit operations, so
|
|
303
311
|
* signed zeros, subnormals, infinities, and NaN payloads widen exactly and no `f16` extension is
|
|
304
312
|
* needed. An exponent of `0x1f` keeps the fraction under an all-ones `f32` exponent, so infinities
|
|
305
313
|
* stay infinite and NaN payloads survive. A subnormal half rebuilds its `f32` bits from
|
|
@@ -308,13 +316,13 @@ var WGSL_PEAK = Object.freeze({
|
|
|
308
316
|
*
|
|
309
317
|
* @example
|
|
310
318
|
* ```ts
|
|
311
|
-
* WGSL_HALF.name // '
|
|
319
|
+
* WGSL_HALF.name // 'widenHalf'
|
|
312
320
|
* ```
|
|
313
321
|
*/
|
|
314
322
|
var WGSL_HALF = Object.freeze({
|
|
315
|
-
name: "
|
|
323
|
+
name: "widenHalf",
|
|
316
324
|
source: [
|
|
317
|
-
"fn
|
|
325
|
+
"fn widenHalf(bits: u32) -> f32 {",
|
|
318
326
|
" let sign = (bits & 0x8000u) << 16u;",
|
|
319
327
|
" let exponent = (bits >> 10u) & 0x1fu;",
|
|
320
328
|
" let fraction = bits & 0x3ffu;",
|
|
@@ -527,13 +535,15 @@ function collectWGSLFragments(roots) {
|
|
|
527
535
|
*
|
|
528
536
|
* @remarks
|
|
529
537
|
* The text holds, in order: an `enable` directive for each extension a linked fragment's active
|
|
530
|
-
* variant names, in `WGSL_EXTENSIONS` order; the uniform struct declaration; the `
|
|
531
|
-
*
|
|
538
|
+
* variant names, in `WGSL_EXTENSIONS` order; the uniform struct declaration; the `Dispatch` struct,
|
|
539
|
+
* which carries the workgroup-uniform builtins `workgroup_id` and `num_workgroups`; the
|
|
540
|
+
* `Invocation` struct, which carries the per-lane builtin `local_invocation_index`, plus
|
|
532
541
|
* `subgroup_invocation_id` and `subgroup_size` when `enable subgroups;` is emitted; one
|
|
533
542
|
* `@group(0) @binding(N)` storage line per slot in declaration order and the `@group(1) @binding(0)`
|
|
534
|
-
* uniform line; each linked fragment's active body, unchanged; and the entry function
|
|
535
|
-
*
|
|
536
|
-
*
|
|
543
|
+
* uniform line; each linked fragment's active body, unchanged; and the entry function
|
|
544
|
+
* `fn <entry>(invocation: Invocation, dispatch: Dispatch)`. The split lets a helper that takes only
|
|
545
|
+
* `dispatch` return early before a barrier without a per-lane value reaching that condition. A
|
|
546
|
+
* fragment's variant is active when `extensions` switches its extension on.
|
|
537
547
|
*
|
|
538
548
|
* @param program - The program to render
|
|
539
549
|
* @param extensions - Whether each extension is active
|
|
@@ -547,12 +557,17 @@ function collectWGSLFragments(roots) {
|
|
|
547
557
|
function renderWGSLProgram(program, extensions) {
|
|
548
558
|
const variants = program.fragments.map((fragment) => fragment.variant !== void 0 && extensions[fragment.variant.extension] ? fragment.variant : void 0);
|
|
549
559
|
const enabled = Object.keys(WGSL_EXTENSIONS).filter((name) => variants.some((variant) => variant?.extension === name));
|
|
560
|
+
const subgroups = enabled.includes("subgroups") ? [" @builtin(subgroup_invocation_id) subgroup_invocation_id: u32,", " @builtin(subgroup_size) subgroup_size: u32,"] : [];
|
|
561
|
+
const dispatch = [
|
|
562
|
+
"struct Dispatch {",
|
|
563
|
+
" @builtin(workgroup_id) workgroup_id: vec3u,",
|
|
564
|
+
" @builtin(num_workgroups) num_workgroups: vec3u,",
|
|
565
|
+
"}"
|
|
566
|
+
];
|
|
550
567
|
const invocation = [
|
|
551
568
|
"struct Invocation {",
|
|
552
569
|
" @builtin(local_invocation_index) local_invocation_index: u32,",
|
|
553
|
-
|
|
554
|
-
" @builtin(num_workgroups) num_workgroups: vec3u,",
|
|
555
|
-
...enabled.includes("subgroups") ? [" @builtin(subgroup_invocation_id) subgroup_invocation_id: u32,", " @builtin(subgroup_size) subgroup_size: u32,"] : [],
|
|
570
|
+
...subgroups,
|
|
556
571
|
"}"
|
|
557
572
|
];
|
|
558
573
|
const bindings = [...Object.entries(program.slots).map(([name, slot], index) => `@group(0) @binding(${index}) var<storage, ${slot.access}> ${name}: array<${slot.element}>;`), `@group(1) @binding(0) var<uniform> ${program.uniform.name}: ${program.uniform.struct.name};`];
|
|
@@ -560,13 +575,14 @@ function renderWGSLProgram(program, extensions) {
|
|
|
560
575
|
const [x, y, z] = program.entry.size;
|
|
561
576
|
const entry = [
|
|
562
577
|
`@compute @workgroup_size(${x}, ${y}, ${z})`,
|
|
563
|
-
`fn ${program.entry.name}(invocation: Invocation) {`,
|
|
578
|
+
`fn ${program.entry.name}(invocation: Invocation, dispatch: Dispatch) {`,
|
|
564
579
|
program.entry.source,
|
|
565
580
|
"}"
|
|
566
581
|
];
|
|
567
582
|
return `${[
|
|
568
583
|
enabled.map((name) => `enable ${name};`).join("\n"),
|
|
569
584
|
program.uniform.struct.source,
|
|
585
|
+
dispatch.join("\n"),
|
|
570
586
|
invocation.join("\n"),
|
|
571
587
|
bindings.join("\n"),
|
|
572
588
|
...bodies,
|
|
@@ -578,8 +594,9 @@ function renderWGSLProgram(program, extensions) {
|
|
|
578
594
|
*
|
|
579
595
|
* @remarks
|
|
580
596
|
* A program reads its workgroup's index as
|
|
581
|
-
* `
|
|
582
|
-
*
|
|
597
|
+
* `dispatch.workgroup_id.x + dispatch.workgroup_id.y * dispatch.num_workgroups.x`, which the
|
|
598
|
+
* `WGSL_PLACE` fragment declares as `readPlace(dispatch: Dispatch)`, so a workgroup past the total
|
|
599
|
+
* finds no work. A total of 0 gives one workgroup. Thrown with code
|
|
583
600
|
* `ARGUMENT` when `total` is not a non-negative safe integer, `-0` included, or `limit` is not a
|
|
584
601
|
* positive safe integer, and with code `LIMIT` when the total passes `limit * limit`.
|
|
585
602
|
*
|
|
@@ -645,7 +662,7 @@ function computeWGSLOffset(offset, alignment) {
|
|
|
645
662
|
}
|
|
646
663
|
// src/core/WGSLBatch.ts
|
|
647
664
|
/**
|
|
648
|
-
* Records one command encoder's worth of dispatches and
|
|
665
|
+
* Records one command encoder's worth of dispatches, copies, clears, and reads, then submits them together.
|
|
649
666
|
*
|
|
650
667
|
* @remarks
|
|
651
668
|
* The batch owns its command encoder and its compute pass, and keeps one pass open across
|
|
@@ -653,14 +670,19 @@ function computeWGSLOffset(offset, alignment) {
|
|
|
653
670
|
* previous one. Each dispatch writes its uniform values to the session's slab and binds them through
|
|
654
671
|
* the 5-argument `setBindGroup(1, group, offsets, 0, 1)` overload. When the slab is full, the batch
|
|
655
672
|
* ends its pass, uploads the slab, submits, clears the slab, and opens a fresh encoder. The pass
|
|
656
|
-
* ends before each `read`
|
|
657
|
-
*
|
|
658
|
-
*
|
|
659
|
-
*
|
|
660
|
-
* `
|
|
661
|
-
*
|
|
662
|
-
*
|
|
663
|
-
*
|
|
673
|
+
* ends before each `read`, `copy`, and `clear`, which record `copyBufferToBuffer` and `clearBuffer`
|
|
674
|
+
* on the encoder; the next `encode` begins a new pass and sets its pipeline and groups again. Every
|
|
675
|
+
* upload and submit sits between a synchronous push and pop of a `validation` and an `out-of-memory`
|
|
676
|
+
* error scope; a device error on a submitted command buffer moves the session to `failed` with code
|
|
677
|
+
* `VALIDATION`. `read` resolves after the submission to a caller-owned copy of the bytes. Each
|
|
678
|
+
* `read`, `copy`, and `clear` checks the session, the batch, its buffers' usages, and its offsets,
|
|
679
|
+
* sizes, and ranges before it records anything, so a refused call leaves the open pass in place.
|
|
680
|
+
* Thrown with code `VALIDATION` when a call other than `abort` or `execute` arrives after `submit`
|
|
681
|
+
* or `abort`; with code `ARGUMENT` for a malformed grid, a malformed `read`, `copy`, or `clear`
|
|
682
|
+
* range, a buffer without the usage the call needs, or a `copy` between one buffer and itself; with
|
|
683
|
+
* code `DESTROYED` after the session's `destroy`; and with code `LIMIT` when a grid axis passes the
|
|
684
|
+
* device's `maxComputeWorkgroupsPerDimension`. Open a batch through `session.batch()`, which
|
|
685
|
+
* supplies the options.
|
|
664
686
|
*
|
|
665
687
|
* @example
|
|
666
688
|
* ```ts
|
|
@@ -752,26 +774,10 @@ var WGSLBatch = class {
|
|
|
752
774
|
}
|
|
753
775
|
read(buffer, offset, size) {
|
|
754
776
|
this.#check();
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
if (!isNonNegativeInteger(offset) || !isSafeInteger(offset) || offset % 4 !== 0) throw new WGSLError(`A read starts at ${preview(offset)}; a read offset must be a non-negative safe multiple of 4 bytes.`, {
|
|
760
|
-
code: "ARGUMENT",
|
|
761
|
-
context: { offset }
|
|
762
|
-
});
|
|
763
|
-
if (!isSafeInteger(size) || size < 4 || size % 4 !== 0) throw new WGSLError(`A read spans ${preview(size)} bytes; a read size must be a safe multiple of 4 bytes, at least 4.`, {
|
|
764
|
-
code: "ARGUMENT",
|
|
765
|
-
context: { size }
|
|
766
|
-
});
|
|
767
|
-
if (offset + size > buffer.size) throw new WGSLError(`A read ends at byte ${offset + size}, past its buffer's ${buffer.size} bytes; shorten the range.`, {
|
|
768
|
-
code: "ARGUMENT",
|
|
769
|
-
context: {
|
|
770
|
-
offset,
|
|
771
|
-
size,
|
|
772
|
-
length: buffer.size
|
|
773
|
-
}
|
|
774
|
-
});
|
|
777
|
+
this.#checkUsage(buffer, "source", "read");
|
|
778
|
+
this.#checkOffset(offset, "read");
|
|
779
|
+
this.#checkSize(size, "read");
|
|
780
|
+
this.#checkRange(buffer, offset, size, "read");
|
|
775
781
|
this.#close();
|
|
776
782
|
const staging = this.#options.lease(size);
|
|
777
783
|
this.#encoder.copyBufferToBuffer(buffer, offset, staging, 0, size);
|
|
@@ -784,6 +790,31 @@ var WGSLBatch = class {
|
|
|
784
790
|
]);
|
|
785
791
|
return result.promise;
|
|
786
792
|
}
|
|
793
|
+
copy(source, destination, size) {
|
|
794
|
+
this.#check();
|
|
795
|
+
const [from, start] = this.#readSlice(source, "source", "copy source");
|
|
796
|
+
const [to, at] = this.#readSlice(destination, "destination", "copy destination");
|
|
797
|
+
if (from === to) throw new WGSLError("A copy reads and writes one buffer; copy between two distinct buffers, or stage the range through a second buffer.", {
|
|
798
|
+
code: "ARGUMENT",
|
|
799
|
+
context: { label: from.label }
|
|
800
|
+
});
|
|
801
|
+
this.#checkSize(size, "copy");
|
|
802
|
+
this.#checkRange(from, start, size, "copy source");
|
|
803
|
+
this.#checkRange(to, at, size, "copy destination");
|
|
804
|
+
this.#close();
|
|
805
|
+
this.#encoder.copyBufferToBuffer(from, start, to, at, size);
|
|
806
|
+
}
|
|
807
|
+
clear(buffer, offset, size) {
|
|
808
|
+
this.#check();
|
|
809
|
+
this.#checkUsage(buffer, "destination", "clear");
|
|
810
|
+
const start = offset ?? 0;
|
|
811
|
+
this.#checkOffset(start, "clear");
|
|
812
|
+
const length = size ?? buffer.size - start;
|
|
813
|
+
this.#checkSize(length, "clear");
|
|
814
|
+
this.#checkRange(buffer, start, length, "clear");
|
|
815
|
+
this.#close();
|
|
816
|
+
this.#encoder.clearBuffer(buffer, start, length);
|
|
817
|
+
}
|
|
787
818
|
submit() {
|
|
788
819
|
this.#check();
|
|
789
820
|
this.#close();
|
|
@@ -823,6 +854,49 @@ var WGSLBatch = class {
|
|
|
823
854
|
context: { status: this.#status }
|
|
824
855
|
});
|
|
825
856
|
}
|
|
857
|
+
#checkUsage(buffer, usage, operation) {
|
|
858
|
+
if (isObject(buffer) && (buffer.usage & WGSL_USAGE[usage]) !== 0) return;
|
|
859
|
+
const received = isObject(buffer) ? buffer.usage : void 0;
|
|
860
|
+
throw new WGSLError(`A ${operation} needs a buffer with the ${usage} usage (WGSL_USAGE.${usage}); its usage is ${preview(received)}.`, {
|
|
861
|
+
code: "ARGUMENT",
|
|
862
|
+
context: { usage: received }
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
#checkOffset(offset, operation) {
|
|
866
|
+
if (isNonNegativeInteger(offset) && isSafeInteger(offset) && offset % 4 === 0) return;
|
|
867
|
+
throw new WGSLError(`A ${operation} starts at ${preview(offset)}; a ${operation} offset must be a non-negative safe multiple of 4 bytes.`, {
|
|
868
|
+
code: "ARGUMENT",
|
|
869
|
+
context: { offset }
|
|
870
|
+
});
|
|
871
|
+
}
|
|
872
|
+
#checkSize(size, operation) {
|
|
873
|
+
if (isSafeInteger(size) && size >= 4 && size % 4 === 0) return;
|
|
874
|
+
throw new WGSLError(`A ${operation} spans ${preview(size)} bytes; a ${operation} size must be a safe multiple of 4 bytes, at least 4.`, {
|
|
875
|
+
code: "ARGUMENT",
|
|
876
|
+
context: { size }
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
#checkRange(buffer, offset, size, operation) {
|
|
880
|
+
if (offset + size <= buffer.size) return;
|
|
881
|
+
throw new WGSLError(`A ${operation} ends at byte ${offset + size}, past its buffer's ${buffer.size} bytes; shorten the range.`, {
|
|
882
|
+
code: "ARGUMENT",
|
|
883
|
+
context: {
|
|
884
|
+
offset,
|
|
885
|
+
size,
|
|
886
|
+
length: buffer.size
|
|
887
|
+
}
|
|
888
|
+
});
|
|
889
|
+
}
|
|
890
|
+
#readSlice(slice, usage, operation) {
|
|
891
|
+
if (!isObject(slice)) throw new WGSLError(`A ${operation} must be a slice such as { buffer, offset: 0 }; received ${preview(slice)}.`, {
|
|
892
|
+
code: "ARGUMENT",
|
|
893
|
+
context: { slice }
|
|
894
|
+
});
|
|
895
|
+
const { buffer, offset = 0 } = slice;
|
|
896
|
+
this.#checkUsage(buffer, usage, operation);
|
|
897
|
+
this.#checkOffset(offset, operation);
|
|
898
|
+
return [buffer, offset];
|
|
899
|
+
}
|
|
826
900
|
#refuseGrid(grid) {
|
|
827
901
|
throw new WGSLError(`A dispatch grid must hold three positive safe integer workgroup counts, such as [4, 1, 1]; received ${preview(grid)}.`, {
|
|
828
902
|
code: "ARGUMENT",
|
|
@@ -1309,13 +1383,17 @@ var WGSLStatement = class {
|
|
|
1309
1383
|
* `open`. Thrown with code `LIMIT` when a program's workgroup size or slot count passes the device's
|
|
1310
1384
|
* limits, and with code `VALIDATION` from `batch()` while another batch is open. A device failure on
|
|
1311
1385
|
* a submitted batch, or the device's `lost` promise, moves the session to `failed`, making
|
|
1312
|
-
* `prepare`, `batch`, and any open batch's `encode`, `read`, and `submit` throw that failure while
|
|
1386
|
+
* `prepare`, `batch`, and any open batch's `encode`, `read`, `copy`, `clear`, and `submit` throw that failure while
|
|
1313
1387
|
* `destroy` resolves. `destroy` moves the session to `destroyed` at once, aborts the open batch,
|
|
1314
1388
|
* waits for submitted work and reads, and destroys the uniform buffer and the staging buffers the
|
|
1315
1389
|
* session created; a repeated `destroy` returns the same promise, settled `abort` and `execute`
|
|
1316
1390
|
* replay their batch's own settlement, and `prepare`, `batch`, `encode`, `read`, and `submit` throw
|
|
1317
|
-
* with code `DESTROYED
|
|
1318
|
-
*
|
|
1391
|
+
* with code `DESTROYED`, and so do `copy` and `clear` on its batch. The constructor reads its options
|
|
1392
|
+
* through `resolveOptions` and throws with code `ARGUMENT`, carrying the option fault as `cause`,
|
|
1393
|
+
* when `options` is not a record, when a key is not `extensions` or `uniform` at the top level, `f16`
|
|
1394
|
+
* or `subgroups` inside `extensions`, or `size` inside `uniform`, when an `extensions` value is not a
|
|
1395
|
+
* boolean, and when `uniform.size` is `null` or not a number. It throws with code `ARGUMENT` and the
|
|
1396
|
+
* received `size` in `context` when `uniform.size` is not a positive safe multiple of 4.
|
|
1319
1397
|
*
|
|
1320
1398
|
* @example
|
|
1321
1399
|
* ```ts
|
|
@@ -1348,12 +1426,34 @@ var WGSLSession = class {
|
|
|
1348
1426
|
* @param options - The extensions to switch off and the slab's byte size
|
|
1349
1427
|
*/
|
|
1350
1428
|
constructor(device, options) {
|
|
1351
|
-
const
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1429
|
+
const resolved = resolveOptions(options, {
|
|
1430
|
+
extensions: optionalGroupOption({
|
|
1431
|
+
f16: optionalOption(isBoolean),
|
|
1432
|
+
subgroups: optionalOption(isBoolean)
|
|
1433
|
+
}),
|
|
1434
|
+
uniform: groupOption({ size: defaultOption(isNumber, WGSL_UNIFORM_SIZE) })
|
|
1435
|
+
}, {
|
|
1436
|
+
refuse: (fault) => {
|
|
1437
|
+
const cause = optionFaultToError(fault, "createWGSLSession");
|
|
1438
|
+
return new WGSLError(`The session options are malformed: ${cause.message}`, {
|
|
1439
|
+
code: "ARGUMENT",
|
|
1440
|
+
context: {
|
|
1441
|
+
reason: fault.reason,
|
|
1442
|
+
path: fault.path
|
|
1443
|
+
},
|
|
1444
|
+
cause
|
|
1445
|
+
});
|
|
1446
|
+
},
|
|
1447
|
+
check: ({ uniform: { size } }) => {
|
|
1448
|
+
if (isSafeInteger(size) && size >= 4 && size % 4 === 0) return;
|
|
1449
|
+
throw new WGSLError(`A session's uniform size must be a positive safe multiple of 4 bytes; received ${preview(size)}.`, {
|
|
1450
|
+
code: "ARGUMENT",
|
|
1451
|
+
context: { size }
|
|
1452
|
+
});
|
|
1453
|
+
}
|
|
1355
1454
|
});
|
|
1356
|
-
const
|
|
1455
|
+
const size = resolved.uniform.size;
|
|
1456
|
+
const requested = resolved.extensions;
|
|
1357
1457
|
const features = device.features;
|
|
1358
1458
|
const usage = WGSL_USAGE.uniform | WGSL_USAGE.destination;
|
|
1359
1459
|
const entry = {
|