pngine 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +121 -0
- package/README.md +27 -10
- package/dist/core.mjs +4 -4
- package/dist/dev.d.ts +16 -0
- package/dist/dev.mjs +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.mjs +1 -0
- package/dist/mini-no-audio.mjs +1 -1
- package/dist/mini.mjs +1 -1
- package/dist/viewer.d.ts +16 -0
- package/dist/viewer.mjs +1 -1
- package/package.json +17 -9
- package/schema/pngine.sjon +973 -425
- package/src/anim.js +78 -13
- package/src/core.js +10 -5
- package/src/descriptor-decode.js +5 -1
- package/src/dev.js +1 -1
- package/src/gpu-queue-commands.js +29 -11
- package/src/gpu-resource-pass-commands.js +115 -21
- package/src/gpu-stats.js +57 -0
- package/src/gpu.js +239 -47
- package/src/init-core.js +118 -26
- package/src/init.js +89 -55
- package/src/mini.js +213 -96
- package/src/viewer-init.js +44 -34
- package/src/viewer.js +1 -1
- package/src/worker-core.js +162 -17
- package/src/worker-prefetch.js +21 -3
- package/src/worker-viewer.js +0 -1
- package/wasm/pngine-compiler.wasm +0 -0
- package/wasm/pngine.wasm +0 -0
package/src/anim.js
CHANGED
|
@@ -229,6 +229,59 @@ export function restart(p) {
|
|
|
229
229
|
return p;
|
|
230
230
|
}
|
|
231
231
|
|
|
232
|
+
// How long a request waits for its reply before resolving with the fallback.
|
|
233
|
+
const REQUEST_TIMEOUT_MS = 2000;
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* One request/reply round-trip with the worker.
|
|
237
|
+
*
|
|
238
|
+
* Both the listener and the promise are guaranteed to settle. The hand-rolled
|
|
239
|
+
* version of this removed its listener ONLY when the reply arrived, so a
|
|
240
|
+
* destroy (or a device loss, or a worker that threw) between the post and the
|
|
241
|
+
* reply orphaned the listener and left the promise pending forever — an awaiting
|
|
242
|
+
* caller's whole continuation, retained. A player torn down mid-request leaked
|
|
243
|
+
* one of these per request.
|
|
244
|
+
*
|
|
245
|
+
* The worker reference is captured locally rather than read back through `i`,
|
|
246
|
+
* because destroy() nulls that field — cleanup has to reach the same worker the
|
|
247
|
+
* listener was installed on.
|
|
248
|
+
*
|
|
249
|
+
* @template T
|
|
250
|
+
* @param {any} i - instance internals
|
|
251
|
+
* @param {object} request - message to post
|
|
252
|
+
* @param {string} replyType - `type` of the reply to wait for
|
|
253
|
+
* @param {(data: any) => T} read - extract the result from the reply
|
|
254
|
+
* @param {T} fallback - resolved value if no reply arrives
|
|
255
|
+
* @returns {Promise<T>}
|
|
256
|
+
*/
|
|
257
|
+
function workerRequest(i, request, replyType, read, fallback) {
|
|
258
|
+
const worker = i.worker;
|
|
259
|
+
const pending = (i.pendingRequests ??= new Set());
|
|
260
|
+
return new Promise((resolve) => {
|
|
261
|
+
let settled = false;
|
|
262
|
+
const finish = (value) => {
|
|
263
|
+
if (settled) return;
|
|
264
|
+
settled = true;
|
|
265
|
+
clearTimeout(timer);
|
|
266
|
+
pending.delete(abandon);
|
|
267
|
+
worker.removeEventListener("message", handler);
|
|
268
|
+
resolve(value);
|
|
269
|
+
};
|
|
270
|
+
// destroy() calls this for every request still in flight, so teardown
|
|
271
|
+
// settles them at once rather than leaving each to time out.
|
|
272
|
+
const abandon = () => finish(fallback);
|
|
273
|
+
const handler = (e) => {
|
|
274
|
+
if (e.data?.type === replyType) finish(read(e.data));
|
|
275
|
+
};
|
|
276
|
+
// Backstop for a worker that is wedged but NOT destroyed — a device loss
|
|
277
|
+
// mid-request, or a handler that threw before replying.
|
|
278
|
+
const timer = setTimeout(abandon, REQUEST_TIMEOUT_MS);
|
|
279
|
+
pending.add(abandon);
|
|
280
|
+
worker.addEventListener("message", handler);
|
|
281
|
+
worker.postMessage(request);
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
232
285
|
/**
|
|
233
286
|
* Get available uniform names from the loaded shader.
|
|
234
287
|
* Returns a promise that resolves to an object with uniform metadata.
|
|
@@ -238,18 +291,30 @@ export function restart(p) {
|
|
|
238
291
|
*/
|
|
239
292
|
export function getUniforms(p) {
|
|
240
293
|
const i = p._;
|
|
241
|
-
if (!i || !i.ready) {
|
|
242
|
-
|
|
243
|
-
|
|
294
|
+
if (!i || !i.ready) return Promise.resolve({});
|
|
295
|
+
return workerRequest(i, { type: "getUniforms" }, "uniforms", (d) => d.uniforms || {}, {});
|
|
296
|
+
}
|
|
244
297
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
298
|
+
/**
|
|
299
|
+
* Live resource counts for this instance — the instrument for a long-running
|
|
300
|
+
* session.
|
|
301
|
+
*
|
|
302
|
+
* Resolves to `{gpu: {live, total, cachedBindGroups, executed}, wasmBytes,
|
|
303
|
+
* frameCount, moduleLoaded, deviceLost}`, or nulls if the instance is torn down.
|
|
304
|
+
* `gpu.live` counts the GPU objects the dispatcher is holding right now, by
|
|
305
|
+
* kind; `wasmBytes` is the executor's WASM memory size.
|
|
306
|
+
*
|
|
307
|
+
* The two properties worth asserting over a soak: `gpu.live.*` stays flat as
|
|
308
|
+
* `gpu.executed` climbs (nothing is created per frame), and `wasmBytes` never
|
|
309
|
+
* changes (the shipped executor has no allocator at all, so growth there would
|
|
310
|
+
* itself be a bug).
|
|
311
|
+
*
|
|
312
|
+
* @param {Pngine} p - PNGine instance
|
|
313
|
+
* @returns {Promise<Object>}
|
|
314
|
+
*/
|
|
315
|
+
export function getStats(p) {
|
|
316
|
+
const i = p._;
|
|
317
|
+
const empty = { gpu: null, wasmBytes: 0, frameCount: 0, moduleLoaded: false, deviceLost: false };
|
|
318
|
+
if (!i || !i.ready) return Promise.resolve(empty);
|
|
319
|
+
return workerRequest(i, { type: "getStats" }, "stats", (d) => d, empty);
|
|
255
320
|
}
|
package/src/core.js
CHANGED
|
@@ -34,11 +34,16 @@ export function createCoreDispatcher(device, ctx) {
|
|
|
34
34
|
},
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
|
-
* Execute a command buffer at the given pointer.
|
|
37
|
+
* Execute a command buffer at the given pointer. Returns the
|
|
38
|
+
* dispatcher's promise, so a host can sequence an init stream that
|
|
39
|
+
* carries an async create (image bitmap, wasm module) before its first
|
|
40
|
+
* frame, and a rejection reaches the caller instead of becoming an
|
|
41
|
+
* unhandled rejection.
|
|
38
42
|
* @param {number} ptr - Command buffer pointer in WASM memory
|
|
43
|
+
* @returns {Promise<void>|void}
|
|
39
44
|
*/
|
|
40
45
|
execute(ptr) {
|
|
41
|
-
dispatcher.execute(ptr);
|
|
46
|
+
return dispatcher.execute(ptr);
|
|
42
47
|
},
|
|
43
48
|
|
|
44
49
|
/**
|
|
@@ -127,11 +132,11 @@ export async function getDevice(adapter) {
|
|
|
127
132
|
* @param {HTMLCanvasElement} canvas
|
|
128
133
|
* @param {GPUDevice} device
|
|
129
134
|
* @param {"opaque"|"premultiplied"} [alphaMode] - pass canvasAlphaMode(bytecode)
|
|
130
|
-
* to honor an authored `(canvas :alpha-mode …)`; defaults to the
|
|
131
|
-
*
|
|
135
|
+
* to honor an authored `(canvas :alpha-mode …)`; defaults to the
|
|
136
|
+
* GPUCanvasConfiguration default, opaque (spec/04, spec/09 D).
|
|
132
137
|
* @returns {GPUCanvasContext}
|
|
133
138
|
*/
|
|
134
|
-
export function configureCanvas(canvas, device, alphaMode = '
|
|
139
|
+
export function configureCanvas(canvas, device, alphaMode = 'opaque') {
|
|
135
140
|
const ctx = canvas.getContext('webgpu');
|
|
136
141
|
if (!ctx) {
|
|
137
142
|
throw new Error('Failed to get WebGPU context');
|
package/src/descriptor-decode.js
CHANGED
|
@@ -96,7 +96,11 @@ export function decodeTextureViewDescriptor(bytes) {
|
|
|
96
96
|
* @returns {object} - GPUSamplerDescriptor-shaped object
|
|
97
97
|
*/
|
|
98
98
|
export function decodeSamplerDescriptor(bytes) {
|
|
99
|
-
|
|
99
|
+
// Seeded with the GPUSamplerDescriptor defaults (spec/09 step D flipped the
|
|
100
|
+
// filters from linear). The encoder always writes all four fields, so this
|
|
101
|
+
// seed only shows through for a descriptor that omits one — which is to say,
|
|
102
|
+
// never from this compiler.
|
|
103
|
+
const desc = /** @type {Record<string, string|number>} */ ({ magFilter: "nearest", minFilter: "nearest", addressModeU: "clamp-to-edge", addressModeV: "clamp-to-edge" });
|
|
100
104
|
const v = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
101
105
|
let off = 2;
|
|
102
106
|
const fc = bytes[1];
|
package/src/dev.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// PNGine Dev API (full-feature profile)
|
|
2
2
|
|
|
3
3
|
export { pngine, destroy } from "./init.js";
|
|
4
|
-
export { draw, play, pause, stop, seek, setFrame, setUniform, setUniforms, getUniforms, restart } from "./anim.js";
|
|
4
|
+
export { draw, play, pause, stop, seek, setFrame, setUniform, setUniforms, getUniforms, getStats, restart } from "./anim.js";
|
|
5
5
|
export { extractBytecode, detectFormat, isPng, isZip, isPngb } from "./extract.js";
|
|
6
6
|
export { parsePayload, createExecutor, getExecutorImports, getExecutorVariantName } from "./loader.js";
|
|
@@ -42,21 +42,22 @@ export function dispatchQueueCommand(cmd, view, pos, ops) {
|
|
|
42
42
|
const { time, cw, ch } = ops.clock();
|
|
43
43
|
DEBUG && ops.debug() && console.log(`[GPU] writeTimeUniform buf[${id}] time=${time} cw=${cw} ch=${ch}`);
|
|
44
44
|
if (ops.buf[id]) {
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
// Staged through the dispatcher's own scratch rather than a fresh
|
|
46
|
+
// Float32Array per write: this runs every frame, once per queue step.
|
|
47
|
+
const s = ops.scratch;
|
|
48
|
+
s.time[0] = time; s.time[1] = cw; s.time[2] = ch; s.time[3] = cw / (ch || 1);
|
|
49
|
+
ops.device.queue.writeBuffer(ops.buf[id], offset, s.timeBytes, 0, Math.min(size, 16));
|
|
47
50
|
}
|
|
48
51
|
return pos + 8;
|
|
49
52
|
}
|
|
50
53
|
case 0x26: { // write_pointer_uniform
|
|
51
54
|
const id = view.getUint16(pos, true), offset = view.getUint32(pos + 2, true), size = view.getUint16(pos + 6, true);
|
|
52
|
-
const
|
|
53
|
-
DEBUG && ops.debug() && console.log(`[GPU] writePointerUniform buf[${id}] x=${
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
ops.device.queue.writeBuffer(ops.buf[id], offset, new Uint8Array(data.buffer, 0, Math.min(size, 48)));
|
|
59
|
-
}
|
|
55
|
+
const s = ops.scratch;
|
|
56
|
+
DEBUG && ops.debug() && console.log(`[GPU] writePointerUniform buf[${id}] x=${s.ptr[0]} y=${s.ptr[1]}`);
|
|
57
|
+
// 12 floats = 48 bytes; the 12th is padding. Layout is pinned by
|
|
58
|
+
// tests/npm/helpers/pointer-layout.js. setPointer writes the state
|
|
59
|
+
// straight into this array, so the write copies with no staging at all.
|
|
60
|
+
if (ops.buf[id]) ops.device.queue.writeBuffer(ops.buf[id], offset, s.ptrBytes, 0, Math.min(size, 48));
|
|
60
61
|
return pos + 8;
|
|
61
62
|
}
|
|
62
63
|
case 0x27: { // resolve_query_set
|
|
@@ -138,12 +139,20 @@ export function dispatchQueueCommand(cmd, view, pos, ops) {
|
|
|
138
139
|
case 0x30: { // init_wasm_module (async)
|
|
139
140
|
const id = view.getUint16(pos, true), ptr = view.getUint32(pos + 2, true), len = view.getUint32(pos + 6, true);
|
|
140
141
|
DEBUG && ops.debug() && console.log(`[GPU] init_wasm_module: id=${id}, ptr=${ptr}, len=${len}`);
|
|
141
|
-
|
|
142
|
+
// Guarded like the create_* handlers: each occurrence compiles a module
|
|
143
|
+
// AND allocates a fresh WebAssembly.Memory, dropping the previous
|
|
144
|
+
// instance to GC with no way to release it explicitly.
|
|
145
|
+
if (len === 0 || ops.wm[id]) return pos + 10;
|
|
142
146
|
const bytes = new Uint8Array(ops.mem.buffer, ptr, len).slice();
|
|
143
147
|
const importedMem = new WebAssembly.Memory({ initial: 1 });
|
|
144
148
|
const wasmImports = { env: { abort: () => { throw new Error('WASM abort'); }, memory: importedMem } };
|
|
145
149
|
return WebAssembly.compile(bytes).then(mod =>
|
|
146
150
|
WebAssembly.instantiate(mod, wasmImports).then(inst => {
|
|
151
|
+
// Same window as 0x0B: a dispatcher released mid-compile has an
|
|
152
|
+
// emptied table, and this instance carries its own
|
|
153
|
+
// WebAssembly.Memory. Nothing to release explicitly — just don't
|
|
154
|
+
// hand it to an array nobody will walk again.
|
|
155
|
+
if (!ops.alive()) return pos + 10;
|
|
147
156
|
ops.wm[id] = { inst, importedMem };
|
|
148
157
|
DEBUG && ops.debug() && console.log(`[GPU] init_wasm_module done: id=${id}, exports=`, Object.keys(inst.exports));
|
|
149
158
|
return pos + 10;
|
|
@@ -189,6 +198,15 @@ export function dispatchQueueCommand(cmd, view, pos, ops) {
|
|
|
189
198
|
onQueryResult(Array.from(data, v => Number(v)));
|
|
190
199
|
b.unmap();
|
|
191
200
|
}).catch((e) => {
|
|
201
|
+
// A throw in the success path above — getMappedRange, or the
|
|
202
|
+
// host's own onQueryResult — lands here with the buffer still
|
|
203
|
+
// MAPPED and unmap() unreached. That is permanent: 0x22 above
|
|
204
|
+
// skips a MAP_READ destination whose mapState isn't 'unmapped',
|
|
205
|
+
// so one bad callback kills readback for the rest of the session
|
|
206
|
+
// and leaves the buffer mapped. Guarded on mapState because a
|
|
207
|
+
// REJECTED mapAsync leaves the buffer unmapped per spec, where
|
|
208
|
+
// an unconditional unmap() would itself throw.
|
|
209
|
+
if (b.mapState === 'mapped') { try { b.unmap(); } catch { /* raced a teardown */ } }
|
|
192
210
|
// Swallowed, this waits forever: onQueryResult never fires either,
|
|
193
211
|
// so a failed readback looks exactly like one still in flight.
|
|
194
212
|
ops.reportError({ message: `query readback failed for buffer ${rb.id}: ${e?.message || e}`, source: "readback" });
|
|
@@ -3,6 +3,27 @@
|
|
|
3
3
|
|
|
4
4
|
import { decodeTextureViewDescriptor } from "./descriptor-decode.js";
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* A pass-scoped command outside an open pass is a dropped command — and a
|
|
8
|
+
* missing begin_*_pass drops its ENTIRE pass this way, silently (an executor
|
|
9
|
+
* variant without the compute plugin would erase a whole simulation with
|
|
10
|
+
* zero output). Report through the host's onError; worker-core dedupes
|
|
11
|
+
* consecutive identical gpu-error messages, so a persistently broken frame
|
|
12
|
+
* surfaces once, not per-command-per-frame.
|
|
13
|
+
*
|
|
14
|
+
* @param {GpuOps} ops
|
|
15
|
+
* @param {string} opName - WebGPU method name, used verbatim in the report
|
|
16
|
+
*/
|
|
17
|
+
function requirePass(ops, opName) {
|
|
18
|
+
const pass = ops.getPass();
|
|
19
|
+
if (!pass) {
|
|
20
|
+
const message = `${opName} with no active pass — command dropped`;
|
|
21
|
+
ops.log(`[GPU] ${message}`);
|
|
22
|
+
ops.reportError({ message, source: "command" });
|
|
23
|
+
}
|
|
24
|
+
return pass;
|
|
25
|
+
}
|
|
26
|
+
|
|
6
27
|
/**
|
|
7
28
|
* Dispatch core resource/pass commands.
|
|
8
29
|
*
|
|
@@ -55,6 +76,10 @@ export function dispatchResourcePassCommand(cmd, view, pos, ops) {
|
|
|
55
76
|
if (!ops.txv[id] && ops.tex[tid]) {
|
|
56
77
|
const desc = len ? decodeTextureViewDescriptor(new Uint8Array(ops.mem.buffer, ptr, len)) : {};
|
|
57
78
|
ops.txv[id] = ops.tex[tid].createView(desc);
|
|
79
|
+
// Remembered, not recomputed: the descriptor bytes live in the command
|
|
80
|
+
// stream, which is rewritten every frame, so by the time a resize needs
|
|
81
|
+
// to rebuild this view they are long gone.
|
|
82
|
+
ops.txs[id] = { tid, desc };
|
|
58
83
|
}
|
|
59
84
|
return pos + 12;
|
|
60
85
|
}
|
|
@@ -62,6 +87,11 @@ export function dispatchResourcePassCommand(cmd, view, pos, ops) {
|
|
|
62
87
|
const id = view.getUint16(pos, true);
|
|
63
88
|
const ptr = view.getUint32(pos + 2, true);
|
|
64
89
|
const len = view.getUint32(pos + 6, true);
|
|
90
|
+
// Guarded like every other create: the wire protocol has no destroy, so
|
|
91
|
+
// overwriting a populated slot would strand the previous object with no
|
|
92
|
+
// way to release it. A repeat create is a no-op, which is what turns a
|
|
93
|
+
// create landing in replayed bytecode from an unbounded leak into noise.
|
|
94
|
+
if (ops.qs[id]) return pos + 10;
|
|
65
95
|
const d = new Uint8Array(ops.mem.buffer, ptr, len);
|
|
66
96
|
// Descriptor: [type:u8] [count:u16 LE]
|
|
67
97
|
const type = d[0] === 1 ? 'timestamp' : 'occlusion';
|
|
@@ -80,7 +110,9 @@ export function dispatchResourcePassCommand(cmd, view, pos, ops) {
|
|
|
80
110
|
const id = view.getUint16(pos, true);
|
|
81
111
|
const ptr = view.getUint32(pos + 2, true);
|
|
82
112
|
const len = view.getUint32(pos + 6, true);
|
|
83
|
-
|
|
113
|
+
// Guarded (see 0x09): a decoded bitmap is megabytes of pixel memory, and
|
|
114
|
+
// an overwritten slot loses the only handle that could close() it.
|
|
115
|
+
if (len === 0 || ops.bmp[id]) return pos + 10;
|
|
84
116
|
// Blob format: [mime_len:u8][mime:bytes][image_data:bytes]
|
|
85
117
|
const raw = new Uint8Array(ops.mem.buffer, ptr, len);
|
|
86
118
|
const mimeLen = raw[0];
|
|
@@ -88,7 +120,11 @@ export function dispatchResourcePassCommand(cmd, view, pos, ops) {
|
|
|
88
120
|
const imageData = raw.slice(1 + mimeLen);
|
|
89
121
|
const blob = new Blob([imageData], { type: mimeStr });
|
|
90
122
|
return createImageBitmap(blob).then((bitmap) => {
|
|
91
|
-
|
|
123
|
+
// destroy() may have run during the decode. This closure holds the OLD
|
|
124
|
+
// dispatcher's table, already emptied — writing here strands megabytes
|
|
125
|
+
// of pixel memory whose only explicit release is the close() below.
|
|
126
|
+
if (!ops.alive()) bitmap.close();
|
|
127
|
+
else ops.bmp[id] = bitmap;
|
|
92
128
|
return pos + 10;
|
|
93
129
|
});
|
|
94
130
|
}
|
|
@@ -96,6 +132,7 @@ export function dispatchResourcePassCommand(cmd, view, pos, ops) {
|
|
|
96
132
|
const id = view.getUint16(pos, true);
|
|
97
133
|
const ptr = view.getUint32(pos + 2, true);
|
|
98
134
|
const len = view.getUint32(pos + 6, true);
|
|
135
|
+
if (ops.ppl[id]) return pos + 10; // guarded (see 0x09)
|
|
99
136
|
const desc = JSON.parse(ops.rs(ptr, len));
|
|
100
137
|
ops.ppl[id] = ops.device.createPipelineLayout({ bindGroupLayouts: desc.bindGroupLayouts.map((i) => ops.bgl[i]) });
|
|
101
138
|
return pos + 10;
|
|
@@ -104,11 +141,19 @@ export function dispatchResourcePassCommand(cmd, view, pos, ops) {
|
|
|
104
141
|
const id = view.getUint16(pos, true);
|
|
105
142
|
const ptr = view.getUint32(pos + 2, true);
|
|
106
143
|
const len = view.getUint32(pos + 6, true);
|
|
144
|
+
if (ops.bun[id]) return pos + 10; // guarded (see 0x09)
|
|
107
145
|
const d = new Uint8Array(ops.mem.buffer, ptr, len);
|
|
108
146
|
const dv = new DataView(d.buffer, d.byteOffset, d.byteLength);
|
|
109
147
|
let off = 0;
|
|
110
148
|
|
|
111
|
-
// Parse colorFormats
|
|
149
|
+
// Parse colorFormats. Count 0 is a LEGACY sentinel, not a live default:
|
|
150
|
+
// `:color-formats` became required in spec/09 step F, so no payload minted
|
|
151
|
+
// since then can reach the branch below. It stays because a count-0
|
|
152
|
+
// descriptor is frozen wire (docs/abi.md §6.5, change policy clause 2) and
|
|
153
|
+
// this runtime has to keep decoding every executor ever shipped — but it
|
|
154
|
+
// is the guess that made the key worth requiring: a bundle recorded for an
|
|
155
|
+
// offscreen target replayed here as if it had been recorded for the
|
|
156
|
+
// canvas, and only the GPU disagreed.
|
|
112
157
|
const cfCount = d[off++];
|
|
113
158
|
/** @type {GPUTextureFormat[]} */
|
|
114
159
|
const colorFormats = [];
|
|
@@ -168,12 +213,22 @@ export function dispatchResourcePassCommand(cmd, view, pos, ops) {
|
|
|
168
213
|
}
|
|
169
214
|
|
|
170
215
|
// === Pass Operations (0x10-0x1A) ===
|
|
171
|
-
case 0x10: { // begin_render_pass (
|
|
216
|
+
case 0x10: { // begin_render_pass — LEGACY 4×u8 clear (see 0x53)
|
|
217
|
+
// `/255` here rather than in ops.colorAttachment: this opcode is the only
|
|
218
|
+
// place a quantized clear enters, and no payload minted since spec/09
|
|
219
|
+
// step D carries one. Kept because the JS side decodes every executor
|
|
220
|
+
// ever shipped (docs/abi.md clause 1).
|
|
172
221
|
const resolveId = view.getUint16(pos + 10, true);
|
|
173
222
|
ops.curPip = -1;
|
|
174
|
-
ops.beginRenderPass(view.getUint16(pos, true), view.getUint8(pos + 2), view.getUint8(pos + 3), view.getUint16(pos + 4, true), view.getUint8(pos + 6), view.getUint8(pos + 7), view.getUint8(pos + 8), view.getUint8(pos + 9), resolveId);
|
|
223
|
+
ops.beginRenderPass(view.getUint16(pos, true), view.getUint8(pos + 2), view.getUint8(pos + 3), view.getUint16(pos + 4, true), view.getUint8(pos + 6) / 255, view.getUint8(pos + 7) / 255, view.getUint8(pos + 8) / 255, view.getUint8(pos + 9) / 255, resolveId);
|
|
175
224
|
return pos + 12;
|
|
176
225
|
}
|
|
226
|
+
case 0x53: { // begin_render_pass_f32 (with optional resolveTarget)
|
|
227
|
+
const resolveId = view.getUint16(pos + 22, true);
|
|
228
|
+
ops.curPip = -1;
|
|
229
|
+
ops.beginRenderPass(view.getUint16(pos, true), view.getUint8(pos + 2), view.getUint8(pos + 3), view.getUint16(pos + 4, true), view.getFloat32(pos + 6, true), view.getFloat32(pos + 10, true), view.getFloat32(pos + 14, true), view.getFloat32(pos + 18, true), resolveId);
|
|
230
|
+
return pos + 24;
|
|
231
|
+
}
|
|
177
232
|
case 0x11: { // begin_compute_pass
|
|
178
233
|
ops.curPip = -1;
|
|
179
234
|
ops.beginComputePass();
|
|
@@ -183,53 +238,73 @@ export function dispatchResourcePassCommand(cmd, view, pos, ops) {
|
|
|
183
238
|
const pipId = view.getUint16(pos, true);
|
|
184
239
|
ops.curPip = pipId;
|
|
185
240
|
ops.log(`[GPU] setPipeline(${pipId})`);
|
|
186
|
-
ops
|
|
241
|
+
requirePass(ops, "setPipeline")?.setPipeline(ops.pip[pipId]);
|
|
187
242
|
return pos + 2;
|
|
188
243
|
}
|
|
189
244
|
case 0x13: { // set_bind_group
|
|
190
245
|
const gi = view.getUint8(pos);
|
|
191
246
|
const bgId = view.getUint16(pos + 1, true);
|
|
192
247
|
let group = ops.bg[bgId];
|
|
193
|
-
//
|
|
248
|
+
// A bind group built against `layout: "auto"` belongs to the pipeline
|
|
249
|
+
// whose layout produced it; binding it under a different pipeline is
|
|
250
|
+
// invalid, so it has to be rebuilt for the bound one.
|
|
251
|
+
//
|
|
252
|
+
// CACHED PER (bind group, pipeline). This used to store the rebuilt group
|
|
253
|
+
// in a local and never write it back, and never update d.layoutId — so
|
|
254
|
+
// the condition stayed true and every frame that bound the group made a
|
|
255
|
+
// fresh GPUBindGroup, none of them released. A single write-back slot
|
|
256
|
+
// would not do either: a payload alternating two pipelines over one group
|
|
257
|
+
// needs both, and one slot would thrash every frame.
|
|
258
|
+
// Only AUTO-layout groups need this. A group built from an explicitly
|
|
259
|
+
// authored (bind-group-layout …) — layoutId carries BGL_TAG (0x8000) — is
|
|
260
|
+
// already usable with every pipeline built from that layout, and rebuilding
|
|
261
|
+
// it under `curPip`'s auto layout would swap the authored layout for the
|
|
262
|
+
// derived one: the exact substitution the explicit form exists to prevent,
|
|
263
|
+
// and one WebGPU rejects if the pipeline has an explicit layout. §339.
|
|
194
264
|
const d = ops.bgd[bgId];
|
|
195
|
-
if (d && ops.curPip >= 0 && d.layoutId !== ops.curPip) {
|
|
265
|
+
if (d && ops.curPip >= 0 && (d.layoutId & 0x8000) === 0 && d.layoutId !== ops.curPip) {
|
|
196
266
|
const p = ops.pip[ops.curPip];
|
|
197
267
|
if (p) {
|
|
198
|
-
|
|
199
|
-
|
|
268
|
+
const cache = d.alt ??= new Map();
|
|
269
|
+
group = cache.get(ops.curPip);
|
|
270
|
+
if (!group) {
|
|
271
|
+
group = ops.device.createBindGroup({ layout: p.getBindGroupLayout(d.gi), entries: ops.buildBindGroupEntries(d.entries) });
|
|
272
|
+
cache.set(ops.curPip, group);
|
|
273
|
+
ops.log(`[GPU] setBindGroup(${gi}, ${bgId}) recreated for pipeline ${ops.curPip}`);
|
|
274
|
+
}
|
|
200
275
|
}
|
|
201
276
|
}
|
|
202
277
|
ops.log(`[GPU] setBindGroup(${gi}, ${bgId}) pass=${ops.getPass() ? 'valid' : 'NULL'} bg[${bgId}]=${group ? 'exists' : 'MISSING'}`);
|
|
203
|
-
ops
|
|
278
|
+
requirePass(ops, "setBindGroup")?.setBindGroup(gi, group);
|
|
204
279
|
return pos + 3;
|
|
205
280
|
}
|
|
206
281
|
case 0x14: { // set_vertex_buffer
|
|
207
|
-
ops
|
|
282
|
+
requirePass(ops, "setVertexBuffer")?.setVertexBuffer(view.getUint8(pos), ops.buf[view.getUint16(pos + 1, true)]);
|
|
208
283
|
return pos + 3;
|
|
209
284
|
}
|
|
210
285
|
case 0x15: { // draw
|
|
211
286
|
const vc = view.getUint32(pos, true);
|
|
212
287
|
const ic = view.getUint32(pos + 4, true);
|
|
213
288
|
ops.log(`[GPU] draw(${vc}, ${ic}) pass=${ops.getPass() ? 'valid' : 'NULL'}`);
|
|
214
|
-
ops
|
|
289
|
+
requirePass(ops, "draw")?.draw(vc, ic, view.getUint32(pos + 8, true), view.getUint32(pos + 12, true));
|
|
215
290
|
return pos + 16;
|
|
216
291
|
}
|
|
217
292
|
case 0x16: { // draw_indexed
|
|
218
|
-
ops
|
|
293
|
+
requirePass(ops, "drawIndexed")?.drawIndexed(view.getUint32(pos, true), view.getUint32(pos + 4, true), view.getUint32(pos + 8, true), view.getInt32(pos + 12, true), view.getUint32(pos + 16, true));
|
|
219
294
|
return pos + 20;
|
|
220
295
|
}
|
|
221
296
|
case 0x17: { // end_pass
|
|
222
297
|
ops.log("[GPU] endPass");
|
|
223
|
-
ops
|
|
298
|
+
requirePass(ops, "endPass")?.end();
|
|
224
299
|
ops.setPass(null);
|
|
225
300
|
return pos;
|
|
226
301
|
}
|
|
227
302
|
case 0x18: { // dispatch
|
|
228
|
-
ops
|
|
303
|
+
requirePass(ops, "dispatchWorkgroups")?.dispatchWorkgroups(view.getUint32(pos, true), view.getUint32(pos + 4, true), view.getUint32(pos + 8, true));
|
|
229
304
|
return pos + 12;
|
|
230
305
|
}
|
|
231
306
|
case 0x19: { // set_index_buffer
|
|
232
|
-
ops
|
|
307
|
+
requirePass(ops, "setIndexBuffer")?.setIndexBuffer(ops.buf[view.getUint16(pos, true)], view.getUint8(pos + 2) === 1 ? "uint32" : "uint16");
|
|
233
308
|
return pos + 3;
|
|
234
309
|
}
|
|
235
310
|
case 0x1A: { // execute_bundles
|
|
@@ -239,10 +314,27 @@ export function dispatchResourcePassCommand(cmd, view, pos, ops) {
|
|
|
239
314
|
const bid = view.getUint16(pos + 1 + i * 2, true);
|
|
240
315
|
if (ops.bun[bid]) bundles.push(ops.bun[bid]);
|
|
241
316
|
}
|
|
242
|
-
if (bundles.length > 0) ops
|
|
317
|
+
if (bundles.length > 0) requirePass(ops, "executeBundles")?.executeBundles(bundles);
|
|
243
318
|
return pos + 1 + count * 2;
|
|
244
319
|
}
|
|
245
|
-
case 0x1B: { // begin_render_pass_mrt
|
|
320
|
+
case 0x1B: { // begin_render_pass_mrt — LEGACY 4×u8 clear (see 0x54)
|
|
321
|
+
ops.curPip = -1;
|
|
322
|
+
let off = pos;
|
|
323
|
+
const count = view.getUint8(off++);
|
|
324
|
+
const attachments = [];
|
|
325
|
+
for (let i = 0; i < count; i++) {
|
|
326
|
+
const tid = view.getUint16(off, true); off += 2;
|
|
327
|
+
const load = view.getUint8(off++);
|
|
328
|
+
const store = view.getUint8(off++);
|
|
329
|
+
const r = view.getUint8(off++) / 255, g = view.getUint8(off++) / 255;
|
|
330
|
+
const b = view.getUint8(off++) / 255, a = view.getUint8(off++) / 255;
|
|
331
|
+
attachments.push({ tid, load, store, r, g, b, a });
|
|
332
|
+
}
|
|
333
|
+
const depthId = view.getUint16(off, true); off += 2;
|
|
334
|
+
ops.beginRenderPassMRT(attachments, depthId);
|
|
335
|
+
return off;
|
|
336
|
+
}
|
|
337
|
+
case 0x54: { // begin_render_pass_mrt_f32
|
|
246
338
|
ops.curPip = -1;
|
|
247
339
|
let off = pos;
|
|
248
340
|
const count = view.getUint8(off++);
|
|
@@ -251,8 +343,10 @@ export function dispatchResourcePassCommand(cmd, view, pos, ops) {
|
|
|
251
343
|
const tid = view.getUint16(off, true); off += 2;
|
|
252
344
|
const load = view.getUint8(off++);
|
|
253
345
|
const store = view.getUint8(off++);
|
|
254
|
-
const r = view.
|
|
255
|
-
const
|
|
346
|
+
const r = view.getFloat32(off, true); off += 4;
|
|
347
|
+
const g = view.getFloat32(off, true); off += 4;
|
|
348
|
+
const b = view.getFloat32(off, true); off += 4;
|
|
349
|
+
const a = view.getFloat32(off, true); off += 4;
|
|
256
350
|
attachments.push({ tid, load, store, r, g, b, a });
|
|
257
351
|
}
|
|
258
352
|
const depthId = view.getUint16(off, true); off += 2;
|
package/src/gpu-stats.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Live-resource accounting for a command dispatcher.
|
|
2
|
+
//
|
|
3
|
+
// The instrument a long-running session needs: exact counts of the GPU objects
|
|
4
|
+
// the runtime is holding right now, independent of logs, DEBUG defines and GC
|
|
5
|
+
// timing. Heap size cannot answer "did 600 frames strand anything" — a GPU
|
|
6
|
+
// object is a few bytes of JS wrapper over driver memory the heap never sees —
|
|
7
|
+
// and forcing GC before every sample only tells you what was collectable, not
|
|
8
|
+
// what the runtime still believes it owns.
|
|
9
|
+
//
|
|
10
|
+
// Counts are DERIVED from the resource tables rather than accumulated at each
|
|
11
|
+
// create site. That is deliberate: a create site the instrumentation missed
|
|
12
|
+
// would report a leak as zero, which is the one failure mode a leak instrument
|
|
13
|
+
// must not have. Walking the tables cannot miss a resource that is in a table,
|
|
14
|
+
// and a resource that is in no table is not retained by the dispatcher at all.
|
|
15
|
+
//
|
|
16
|
+
// Cost is O(total ids) per call, and it is called per stats request — never per
|
|
17
|
+
// frame.
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Populated slots per table. The tables are sparse arrays indexed by resource
|
|
21
|
+
* id, so holes read as undefined and are skipped.
|
|
22
|
+
*
|
|
23
|
+
* @param {Record<string, ArrayLike<unknown>>} tables
|
|
24
|
+
* @returns {{live: Record<string, number>, total: number}}
|
|
25
|
+
*/
|
|
26
|
+
export function countLive(tables) {
|
|
27
|
+
/** @type {Record<string, number>} */
|
|
28
|
+
const live = {};
|
|
29
|
+
let total = 0;
|
|
30
|
+
for (const kind in tables) {
|
|
31
|
+
const table = tables[kind];
|
|
32
|
+
let n = 0;
|
|
33
|
+
for (let i = 0; i < table.length; i++) if (table[i]) n++;
|
|
34
|
+
live[kind] = n;
|
|
35
|
+
total += n;
|
|
36
|
+
}
|
|
37
|
+
return { live, total };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* GPUBindGroups alive beyond the ones in `bg`: set_bind_group caches a rebuilt
|
|
42
|
+
* group per (bind group, pipeline) when a group is bound under a pipeline other
|
|
43
|
+
* than the one whose auto-layout produced it. Those are real, retained
|
|
44
|
+
* GPUBindGroups that appear in no table — the exact shape of the leak that made
|
|
45
|
+
* this instrument worth having (register A1) — so a count that ignored them
|
|
46
|
+
* would report flat while the runtime grew.
|
|
47
|
+
*
|
|
48
|
+
* @param {ArrayLike<{alt?: Map<number, unknown>|null}>} bgd
|
|
49
|
+
*/
|
|
50
|
+
export function countCachedBindGroups(bgd) {
|
|
51
|
+
let n = 0;
|
|
52
|
+
for (let i = 0; i < bgd.length; i++) {
|
|
53
|
+
const alt = bgd[i]?.alt;
|
|
54
|
+
if (alt) n += alt.size;
|
|
55
|
+
}
|
|
56
|
+
return n;
|
|
57
|
+
}
|