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/src/mini.js CHANGED
@@ -30,35 +30,66 @@ async function D(d) {
30
30
  return (d[1] & 1) ? inflateRaw(p) : new Uint8Array(p);
31
31
  }
32
32
 
33
- // Module-level pointer state (shared with X executor and miniPngine).
34
- // 12 floats: x,y,clickX,clickY,dx,dy,buttons,pressure,modifiers,scrollX,scrollY.
35
- let px=0,py=0,pcx=0,pcy=0,pdx=0,pdy=0,pbt=0,ppr=0,pm=0,psx=0,psy=0;
36
-
37
- function X(d, ds, p, e, V, dv, ctx, fm, bf, sh, pp, cp, bg, tm, W, H) {
38
- const R=o=>V.getUint16(o,!0),G=o=>V.getUint32(o,!0);
33
+ // The pointer-inputs buffer (`pt`) is allocated PER INSTANCE in miniPngine and
34
+ // threaded through here as the last parameter. It used to be eleven
35
+ // module-level `let`s shared by this executor and every player: two mini
36
+ // players on one page wrote into the same variables, so whichever canvas last
37
+ // received an event won for both, and a player nobody was touching followed the
38
+ // cursor over its neighbour. Scroll was worse — it ACCUMULATES, so the two
39
+ // players' wheel deltas summed into one running total.
40
+ //
41
+ // Layout (12 f32, pinned by tests/npm/helpers/pointer-layout.js, identical to
42
+ // init-core.js attachPointerTracking):
43
+ // 0 x · 1 y · 2 clickX · 3 clickY · 4 dx · 5 dy · 6 buttons · 7 pressure
44
+ // 8 modifiers · 9 scrollX · 10 scrollY · 11 _pad
45
+ //
46
+ // `bd` (the bind-group rebuild cache) and `ti` (the time-uniform staging array)
47
+ // are threaded in for the same reason `pt` is: they are PER-INSTANCE state that
48
+ // must survive from one call of this function to the next. `bd` used to be a
49
+ // `const bd = []` declared right here — populated only by the init call, so
50
+ // every frame call got an empty one and case 19's rebuild branch could never
51
+ // run (see there).
52
+ function X(d, ds, p, e, V, dv, ctx, fm, bf, sh, pp, cp, bg, tm, W, H, pt, bd, ti) {
53
+ const R=o=>V.getUint16(o,!0),G=o=>V.getUint32(o,!0),F=o=>V.getFloat32(o,!0);
39
54
  const rs = (o, l) => new TextDecoder().decode(d.subarray(ds + o, ds + o + l));
40
- e = p + e; let ps, en, cP = -1; const bd = [];
55
+ e = p + e; let ps, en, cP = -1;
41
56
  while (p < e) {
42
57
  const o = d[p++];
43
58
  switch (o) {
59
+ // The five create arms are IDEMPOTENT (pitfall 35 rule 1, mirroring
60
+ // gpu.js's `if (table[id]) return`). They used to assign unconditionally,
61
+ // so a create anywhere in the frame range minted a fresh GPU object at
62
+ // 60Hz — none released, and the id table repointed away from whatever the
63
+ // bind groups already held. The flat writer refuses to emit such a pNGf,
64
+ // but mini plays bytes that arrived in a PNG and nothing makes those the
65
+ // compiler's bytes. The guard also skips the arm's decode work (a
66
+ // TextDecoder pass, a JSON.parse), which is the per-frame cost that made
67
+ // this expensive as well as wrong.
44
68
  case 1: {
45
69
  const id = R(p), sz = G(p+2), u = R(p+6);
46
70
  let f = 0; if (u & 8) f |= 8; if (u & 0x20) f |= 32; if (u & 0x40) f |= 64; if (u & 0x80) f |= 128; if (u & 0x100) f |= 0x100;
47
- bf[id] = dv.createBuffer({size: sz, usage: f}); p += 8; break;
71
+ bf[id] ??= dv.createBuffer({size: sz, usage: f}); p += 8; break;
48
72
  }
49
- case 4: sh[R(p)] = dv.createShaderModule({code: rs(G(p+2), G(p+6))}); p += 10; break;
73
+ case 4: sh[R(p)] ??= dv.createShaderModule({code: rs(G(p+2), G(p+6))}); p += 10; break;
50
74
  case 5: {
51
- const id = R(p), dd = JSON.parse(rs(G(p+2), G(p+6)));
52
- pp[id] = dv.createRenderPipeline({layout:'auto', vertex:{module:sh[dd.vertex?.shader??0],entryPoint:dd.vertex?.entryPoint??'vs',buffers:dd.vertex?.buffers??[]}, fragment:{module:sh[dd.fragment?.shader??0],entryPoint:dd.fragment?.entryPoint??'fs',targets:[{format:fm}]}, primitive:dd.primitive??{topology:'triangle-list'}});
75
+ const id = R(p);
76
+ if (!pp[id]) {
77
+ const dd = JSON.parse(rs(G(p+2), G(p+6)));
78
+ pp[id] = dv.createRenderPipeline({layout:'auto', vertex:{module:sh[dd.vertex?.shader??0],entryPoint:dd.vertex?.entryPoint,buffers:dd.vertex?.buffers??[]}, fragment:{module:sh[dd.fragment?.shader??0],entryPoint:dd.fragment?.entryPoint,targets:[{format:fm}]}, primitive:dd.primitive??{topology:'triangle-list'}});
79
+ }
53
80
  p += 10; break;
54
81
  }
55
82
  case 6: {
56
- const id = R(p), dd = JSON.parse(rs(G(p+2), G(p+6)));
57
- cp[id] = dv.createComputePipeline({layout:'auto', compute:{module:sh[dd.compute?.shader??0],entryPoint:dd.compute?.entryPoint??'main'}});
83
+ const id = R(p);
84
+ if (!cp[id]) {
85
+ const dd = JSON.parse(rs(G(p+2), G(p+6)));
86
+ cp[id] = dv.createComputePipeline({layout:'auto', compute:{module:sh[dd.compute?.shader??0],entryPoint:dd.compute?.entryPoint}});
87
+ }
58
88
  p += 10; break;
59
89
  }
60
90
  case 7: {
61
91
  const id = R(p), pl = R(p+2), dp = G(p+4), dl = G(p+8);
92
+ if (bg[id]) { p += 12; break; }
62
93
  const b = d.subarray(ds + dp, ds + dp + dl), bv = new DataView(b.buffer, b.byteOffset, b.byteLength);
63
94
  let q = 2, gi = 0; const es = [];
64
95
  for (let k = 0; k < b[1]; k++) {
@@ -79,19 +110,48 @@ function X(d, ds, p, e, V, dv, ctx, fm, bf, sh, pp, cp, bg, tm, W, H) {
79
110
  // invited the belief that mini merely misparses textures.)
80
111
  else if (fi === 2 && vt === 3) { let ec = b[q++]; while (ec--) { const bi = b[q++], rt = b[q++], ri = bv.getUint16(q, true); q += 2; if (rt !== 0) throw new Error(`pngine/mini: bind-group resource type ${rt} is unsupported by the minimal player — re-export without --flat (the viewer runtime binds textures and samplers)`); es.push({binding:bi,resource:{buffer:bf[ri]}}); q += 8; } }
81
112
  }
82
- bd[id] = {pl, gi, es}; bg[id] = dv.createBindGroup({layout:(pp[pl]||cp[pl]).getBindGroupLayout(gi),entries:es}); p += 12; break;
113
+ // `a` caches the per-pipeline auto-layout rebuilds case 19 makes.
114
+ bd[id] = {pl, gi, es, a: /** @type {Record<number, GPUBindGroup>|null} */ (null)}; bg[id] = dv.createBindGroup({layout:(pp[pl]||cp[pl]).getBindGroupLayout(gi),entries:es}); p += 12; break;
83
115
  }
84
- case 16: cP = -1; en = dv.createCommandEncoder(); ps = en.beginRenderPass({colorAttachments:[{view:ctx.getCurrentTexture().createView(),loadOp:'clear',storeOp:'store',clearValue:[d[p+6]/255,d[p+7]/255,d[p+8]/255,d[p+9]/255]}]}); p += 10; break;
116
+ // 0x53 begin_render_pass_f32 — the clear value is four f32s, not four
117
+ // bytes decoding /255 (spec/09 step D). pNGf v1's 0x10 is refused at the
118
+ // chunk header, so there is no second decoder here.
119
+ // One encoder per frame, like case 17: an unconditional
120
+ // createCommandEncoder here REPLACED the encoder a preceding compute
121
+ // pass was recording into, unfinished and unsubmitted — the compute
122
+ // pass never reached the GPU and one abandoned encoder was garbage per
123
+ // frame. The load op honours the wire byte (1 = clear, else load), as
124
+ // gpu.js does; a two-render-pass frame must not clear twice.
125
+ case 83: cP = -1; if (!en) en = dv.createCommandEncoder(); ps = en.beginRenderPass({colorAttachments:[{view:ctx.getCurrentTexture().createView(),loadOp:d[p+2]===1?'clear':'load',storeOp:'store',clearValue:[F(p+6),F(p+10),F(p+14),F(p+18)]}]}); p += 22; break;
85
126
  case 17: cP = -1; if (!en) en = dv.createCommandEncoder(); ps = en.beginComputePass(); break;
86
127
  case 18: { const pid = R(p); cP = pid; ps.setPipeline(pp[pid]||cp[pid]); p += 2; break; }
87
- case 19: { let g = bg[R(p+1)]; const dd = bd[R(p+1)]; if (dd && cP >= 0 && dd.pl !== cP) { const P = pp[cP]||cp[cP]; if (P) g = dv.createBindGroup({layout:P.getBindGroupLayout(dd.gi),entries:dd.es}); } ps.setBindGroup(d[p], g); p += 3; break; }
128
+ // Auto-layout rebuild, cached per (bind group, pipeline) in `dd.a` —
129
+ // mirrors gpu-resource-pass-commands.js 0x13. Uncached this made a fresh
130
+ // GPUBindGroup on EVERY frame that bound the group, none of them
131
+ // released; keyed by pipeline because one slot would thrash on a payload
132
+ // alternating two pipelines over one group.
133
+ //
134
+ // This branch was UNREACHABLE from a frame until `bd` became per-instance:
135
+ // only the init call populated it, and it was a local, so `dd` was always
136
+ // undefined here. The visible symptom was not the leak the cache prevents
137
+ // but the correctness bug underneath it — a group created against one
138
+ // pipeline and bound under another was handed over unrebuilt, which WebGPU
139
+ // rejects silently.
140
+ case 19: { let g = bg[R(p+1)]; const dd = bd[R(p+1)]; if (dd && cP >= 0 && dd.pl !== cP) { const P = pp[cP]||cp[cP]; if (P) g = (dd.a ??= {})[cP] ??= dv.createBindGroup({layout:P.getBindGroupLayout(dd.gi),entries:dd.es}); } ps.setBindGroup(d[p], g); p += 3; break; }
88
141
  case 20: ps.setVertexBuffer(d[p], bf[R(p+1)]); p += 3; break;
89
142
  case 21: ps.draw(G(p), G(p+4)); p += 16; break;
90
143
  case 22: ps.drawIndexed(G(p), G(p+4)); p += 20; break;
91
144
  case 23: ps.end(); ps = null; break;
92
145
  case 24: ps.dispatchWorkgroups(G(p), G(p+4), G(p+8)); p += 12; break;
93
- case 33: dv.queue.writeBuffer(bf[R(p)], 0, new Float32Array([tm, W, H, W/H])); p += 8; break;
94
- case 38: dv.queue.writeBuffer(bf[R(p)], 0, new Float32Array([px,py,pcx,pcy,pdx,pdy,pbt,ppr,pm,psx,psy,0])); p += 8; break;
146
+ // Staged through the instance's own `ti`, not a `new Float32Array([…])`
147
+ // per frame: at 60Hz that is sixty allocations a second whose only purpose
148
+ // is to be copied by writeBuffer and dropped. Its neighbour below already
149
+ // avoided exactly this (the §310 lesson, applied to one arm and not the
150
+ // other).
151
+ case 33: ti[0]=tm; ti[1]=W; ti[2]=H; ti[3]=W/H; dv.queue.writeBuffer(bf[R(p)], 0, ti); p += 8; break;
152
+ // The buffer is written wholesale — it already IS the wire layout, so
153
+ // there is no per-frame Float32Array to build.
154
+ case 38: dv.queue.writeBuffer(bf[R(p)], 0, pt); p += 8; break;
95
155
  case 240: if (en) { dv.queue.submit([en.finish()]); en = null; } break;
96
156
  case 255: return;
97
157
  // An opcode mini can't decode. Its operands would be re-read as opcodes and
@@ -145,86 +205,143 @@ export async function miniPngine(canvas, source, opts) {
145
205
  // Surface WebGPU validation / OOM the way the viewer does; without this a bad
146
206
  // draw is silently wrong (WebGPU never throws synchronously for these).
147
207
  dv.onuncapturederror = ev => report(gpuError(ev.error.message, 'uncaptured'));
148
- const ctx = /** @type {GPUCanvasContext} */ (canvas.getContext('webgpu'));
149
- const fm = navigator.gpu.getPreferredCanvasFormat();
150
- // pNGf flags bit 0 = opaque canvas (mirrors the PNGB header flag; spec/04).
151
- ctx.configure({device: dv, format: fm, alphaMode: fl[1] & 1 ? 'opaque' : 'premultiplied'});
152
-
153
- const W = canvas.width, H = canvas.height;
154
- const V = new DataView(fl.buffer, fl.byteOffset, fl.byteLength);
155
- const il = V.getUint32(2, true), frl = V.getUint32(6, true);
156
- const ds = 14 + il + frl;
157
- const bf = [], sh = [], pp = [], cp_ = [], bg = [];
158
-
159
- // Pointer/wheel listeners fill the 12 pointer-inputs floats (px..psy + pad).
160
- // Modifier bits 1/2/4/8 (shift/ctrl/alt/meta) mirror init-core.js; the Space
161
- // bit (16) is omitted — the minimal viewer attaches no document keyboard
162
- // listeners. Scroll (psx/psy) accumulates sign(delta) like init-core.
163
- let lx=0,ly=0;
164
- const M=e=>(e.shiftKey?1:0)|(e.ctrlKey?2:0)|(e.altKey?4:0)|(e.metaKey?8:0);
165
- canvas.onpointerdown=e=>{const r=canvas.getBoundingClientRect(),x=(e.clientX-r.left)/r.width,y=(e.clientY-r.top)/r.height;px=x;py=y;pcx=x;pcy=y;pbt=e.buttons;ppr=e.pressure||.5;pm=M(e);lx=x;ly=y;canvas.setPointerCapture(e.pointerId)};
166
- canvas.onpointermove=e=>{const r=canvas.getBoundingClientRect(),x=Math.max(0,Math.min(1,(e.clientX-r.left)/r.width)),y=Math.max(0,Math.min(1,(e.clientY-r.top)/r.height));pdx=x-lx;pdy=y-ly;px=x;py=y;pbt=e.buttons;ppr=e.pressure||(e.buttons?.5:0);pm=M(e);lx=x;ly=y};
167
- canvas.onpointerup=()=>{pcx=-Math.abs(pcx);pcy=-Math.abs(pcy);pbt=0;ppr=0};
168
- canvas.onpointercancel=canvas.onpointerup;
169
- canvas.onwheel=e=>{e.preventDefault();psx+=Math.sign(e.deltaX);psy+=Math.sign(e.deltaY);pm=M(e)};
170
-
171
- // Run init commands
172
- X(fl, ds, 14, il, V, dv, ctx, fm, bf, sh, pp, cp_, bg, 0, W, H);
173
- // Run first frame
174
- X(fl, ds, 14 + il, frl, V, dv, ctx, fm, bf, sh, pp, cp_, bg, 0, W, H);
175
-
176
- let on = 0, t0 = 0, tm = 0, raf = 0;
177
- let ax, ab2, sr;
178
-
179
- // Device loss stops the loop and reports once. `destroyed` is our own destroy()
180
- // teardown — ignore it. mini keeps no self-recovery (size play); the host
181
- // decides whether to re-create the player.
182
- dv.lost.then(info => {
183
- if (info.reason === 'destroyed') return;
208
+
209
+ // Everything from here on can throw, and everything from here on is something
210
+ // that has to be released: the device above, the configured context, the five
211
+ // canvas listeners, every resource the init commands create. `inst` — the only
212
+ // object that used to carry a destroy() — is not built until the very end, so
213
+ // a throw in between rejected this promise with NOTHING able to release any of
214
+ // it: device, context and listeners stayed alive for the page's life. The
215
+ // worker tier documents this exact class and fixes it (init-core.js: "every
216
+ // failure path TERMINATES the worker before rejecting").
217
+ //
218
+ // So the teardown exists BEFORE the things it releases, and there is only one
219
+ // of it: a setup failure and a host destroy() are the same release, run from
220
+ // the same closure. `dead` makes it idempotent — a second destroy() used to
221
+ // call ax.close() on an already-closed AudioContext (an unhandled rejection)
222
+ // and dv.destroy() twice — and it is also what keeps play() from resurrecting
223
+ // the loop, since WebGPU calls on a destroyed device do not throw
224
+ // synchronously and frame()'s try/catch would never have fired.
225
+ let on = 0, t0 = 0, tm = 0, raf = 0, dead = 0, lost = 0;
226
+ let ax, ab2, sr, ctx;
227
+ const teardown = () => {
228
+ if (dead) return; dead = 1;
184
229
  on = 0; cancelAnimationFrame(raf);
185
- report(gpuError('pngine/mini: WebGPU device lost — ' + (info.message || info.reason || 'unknown'), 'device-lost'));
186
- });
187
-
188
- if (AUDIO && au) {
189
- const {instance: ai} = await WebAssembly.instantiate(/** @type {BufferSource} */ (/** @type {unknown} */ (au)), /** @type {WebAssembly.Imports} */ (/** @type {unknown} */ ({m: Math})));
190
- const ax2 = /** @type {SointuExports} */ (/** @type {unknown} */ (ai.exports));
191
- const m = ax2.m, s = ax2.s.value, l = ax2.l.value, t = ax2.t.value === 1;
192
- const fr = t ? l / 4 : l / 8;
193
- const smp = t ? new Int16Array(m.buffer, s, fr * 2) : new Float32Array(m.buffer, s, fr * 2);
194
- ax = new AudioContext({sampleRate: 44100});
195
- ab2 = ax.createBuffer(2, fr, 44100);
196
- deinterleaveStereo(ab2, smp, fr, t);
197
- }
230
+ if (AUDIO && sr) { try { sr.stop(); } catch(_){} sr = null; }
231
+ if (AUDIO && ax && ax.state !== 'closed') ax.close();
232
+ // Nulling the five on* handlers is the mini tier's whole listener story:
233
+ // they are properties, not addEventListener registrations, and each closes
234
+ // over the canvas. Leaving them set kept the canvas and this instance's
235
+ // pointer state alive for as long as the host held the element.
236
+ canvas.onpointerdown = canvas.onpointermove = canvas.onpointerup = canvas.onpointercancel = canvas.onwheel = null;
237
+ dv.onuncapturederror = null;
238
+ ctx?.unconfigure?.();
239
+ dv.destroy();
240
+ };
241
+
242
+ try {
243
+ // pNGf version (fl[0], the payload's own header — the chunk wrapper's two
244
+ // bytes are already stripped). mini follows flat.zig's command set exactly
245
+ // and carries no legacy decoders, so an unrecognised version has to be
246
+ // refused: a v1 payload's begin_render_pass (0x10) would otherwise fall
247
+ // through the switch mid-stream and desync everything after it. Nothing
248
+ // read this byte before spec/09 step D.
249
+ if (fl[0] !== 2) throw new Error(`pngine/mini: pNGf version ${fl[0]} is not supported by this player (expected 2)`);
250
+ ctx = /** @type {GPUCanvasContext} */ (canvas.getContext('webgpu'));
251
+ const fm = navigator.gpu.getPreferredCanvasFormat();
252
+ // pNGf flags bit 0 = premultiplied canvas (mirrors the PNGB header flag).
253
+ // Clear = 'opaque', the GPUCanvasConfiguration default. (spec/04, spec/09 D)
254
+ ctx.configure({device: dv, format: fm, alphaMode: fl[1] & 1 ? 'premultiplied' : 'opaque'});
255
+
256
+ const W = canvas.width, H = canvas.height;
257
+ const V = new DataView(fl.buffer, fl.byteOffset, fl.byteLength);
258
+ const il = V.getUint32(2, true), frl = V.getUint32(6, true);
259
+ const ds = 14 + il + frl;
260
+ const bf = [], sh = [], pp = [], cp_ = [], bg = [];
261
+
262
+ // Pointer/wheel listeners fill this instance's 12 pointer-inputs floats.
263
+ // Per-instance, not module-level: see the layout note above X().
264
+ // Modifier bits 1/2/4/8 (shift/ctrl/alt/meta) mirror init-core.js; the Space
265
+ // bit (16) is omitted — the minimal viewer attaches no document keyboard
266
+ // listeners. Scroll (9/10) accumulates sign(delta) like init-core.
267
+ const pt = new Float32Array(12);
268
+ let lx=0,ly=0;
269
+ const M=e=>(e.shiftKey?1:0)|(e.ctrlKey?2:0)|(e.altKey?4:0)|(e.metaKey?8:0);
270
+ canvas.onpointerdown=e=>{const r=canvas.getBoundingClientRect(),x=(e.clientX-r.left)/r.width,y=(e.clientY-r.top)/r.height;pt[0]=pt[2]=x;pt[1]=pt[3]=y;pt[6]=e.buttons;pt[7]=e.pressure||.5;pt[8]=M(e);lx=x;ly=y;canvas.setPointerCapture(e.pointerId)};
271
+ canvas.onpointermove=e=>{const r=canvas.getBoundingClientRect(),x=Math.max(0,Math.min(1,(e.clientX-r.left)/r.width)),y=Math.max(0,Math.min(1,(e.clientY-r.top)/r.height));pt[4]=x-lx;pt[5]=y-ly;pt[0]=x;pt[1]=y;pt[6]=e.buttons;pt[7]=e.pressure||(e.buttons?.5:0);pt[8]=M(e);lx=x;ly=y};
272
+ canvas.onpointerup=()=>{pt[2]=-Math.abs(pt[2]);pt[3]=-Math.abs(pt[3]);pt[6]=0;pt[7]=0};
273
+ canvas.onpointercancel=canvas.onpointerup;
274
+ canvas.onwheel=e=>{e.preventDefault();pt[9]+=Math.sign(e.deltaX);pt[10]+=Math.sign(e.deltaY);pt[8]=M(e)};
198
275
 
199
- function frame() {
200
- if (!on) return;
201
- tm = (performance.now() - t0) / 1e3;
202
- // A runtime fault (undecodable opcode, lost device) stops the loop and
203
- // reports, instead of throwing uncaught out of rAF on every frame.
204
- try {
205
- X(fl, ds, 14 + il, frl, V, dv, ctx, fm, bf, sh, pp, cp_, bg, tm, W, H);
206
- } catch (err) {
207
- on = 0; cancelAnimationFrame(raf);
208
- report(err);
209
- return;
276
+ // The bind-group rebuild cache and the time-uniform staging array: per
277
+ // instance, and threaded into every X() call so they survive between them.
278
+ const bd = [], ti = new Float32Array(4);
279
+
280
+ // Run init commands
281
+ X(fl, ds, 14, il, V, dv, ctx, fm, bf, sh, pp, cp_, bg, 0, W, H, pt, bd, ti);
282
+ // Run first frame
283
+ X(fl, ds, 14 + il, frl, V, dv, ctx, fm, bf, sh, pp, cp_, bg, 0, W, H, pt, bd, ti);
284
+
285
+ // Device loss stops the loop and reports once. `destroyed` is our own destroy()
286
+ // teardown — ignore it, and so is anything arriving after `dead` (a device can
287
+ // be lost for another reason in the same turn we tear it down). mini keeps no
288
+ // self-recovery (size play); the host decides whether to re-create the player.
289
+ // `lost` goes up too: play() checks it, and without it a host play button
290
+ // after the device-lost error restarted the rAF chain at 60 Hz on a lost
291
+ // device for the page's life (LEAK-05-D's other trigger). Not `dead` —
292
+ // that is teardown's own latch, and destroy() after a loss must still run.
293
+ dv.lost.then(info => {
294
+ if (dead || info.reason === 'destroyed') return;
295
+ on = 0; lost = 1; cancelAnimationFrame(raf);
296
+ report(gpuError('pngine/mini: WebGPU device lost — ' + (info.message || info.reason || 'unknown'), 'device-lost'));
297
+ });
298
+
299
+ if (AUDIO && au) {
300
+ const {instance: ai} = await WebAssembly.instantiate(/** @type {BufferSource} */ (/** @type {unknown} */ (au)), /** @type {WebAssembly.Imports} */ (/** @type {unknown} */ ({m: Math})));
301
+ const ax2 = /** @type {SointuExports} */ (/** @type {unknown} */ (ai.exports));
302
+ const m = ax2.m, s = ax2.s.value, l = ax2.l.value, t = ax2.t.value === 1;
303
+ const fr = t ? l / 4 : l / 8;
304
+ const smp = t ? new Int16Array(m.buffer, s, fr * 2) : new Float32Array(m.buffer, s, fr * 2);
305
+ ax = new AudioContext({sampleRate: 44100});
306
+ ab2 = ax.createBuffer(2, fr, 44100);
307
+ deinterleaveStereo(ab2, smp, fr, t);
210
308
  }
211
- pdx = 0; pdy = 0; // reset pointer deltas per frame
212
- raf = requestAnimationFrame(frame);
213
- }
214
309
 
215
- const inst = {
216
- play() {
217
- if (on) return;
218
- on = 1; t0 = performance.now() - tm * 1e3;
219
- if (AUDIO && ax) { if (ax.state === 'suspended') ax.resume(); sr = ax.createBufferSource(); sr.buffer = ab2; sr.connect(ax.destination); sr.start(0, tm); }
220
- frame();
221
- },
222
- pause() { on = 0; cancelAnimationFrame(raf); if (AUDIO && sr) { try { sr.stop(); } catch(_){} sr = null; } },
223
- stop() { on = 0; tm = 0; cancelAnimationFrame(raf); if (AUDIO && sr) { try { sr.stop(); } catch(_){} sr = null; } },
224
- destroy() { on = 0; cancelAnimationFrame(raf); if (AUDIO && sr) try { sr.stop(); } catch(_){} if (AUDIO && ax) ax.close(); dv.destroy(); },
225
- get time() { return tm; },
226
- get isPlaying() { return !!on; }
227
- };
228
- if (opts?.autoplay) inst.play();
229
- return inst;
310
+ function frame() {
311
+ if (!on) return;
312
+ tm = (performance.now() - t0) / 1e3;
313
+ // A runtime fault (undecodable opcode, lost device) stops the loop and
314
+ // reports, instead of throwing uncaught out of rAF on every frame.
315
+ try {
316
+ X(fl, ds, 14 + il, frl, V, dv, ctx, fm, bf, sh, pp, cp_, bg, tm, W, H, pt, bd, ti);
317
+ } catch (err) {
318
+ on = 0; cancelAnimationFrame(raf);
319
+ report(err);
320
+ return;
321
+ }
322
+ pt[4] = 0; pt[5] = 0; // reset pointer deltas per frame
323
+ raf = requestAnimationFrame(frame);
324
+ }
325
+
326
+ const inst = {
327
+ // `dead` and not just `on`: destroy() left no flag, so play() — which
328
+ // checked only `if (on) return` — restarted the rAF chain on a destroyed
329
+ // device. Nothing threw (WebGPU is asynchronous about that), so the loop
330
+ // reschedules at 60Hz for the page's life, retaining the canvas, the payload
331
+ // and every closure in this function.
332
+ play() {
333
+ if (on || dead || lost) return;
334
+ on = 1; t0 = performance.now() - tm * 1e3;
335
+ if (AUDIO && ax) { if (ax.state === 'suspended') ax.resume(); sr = ax.createBufferSource(); sr.buffer = ab2; sr.connect(ax.destination); sr.start(0, tm); }
336
+ frame();
337
+ },
338
+ pause() { on = 0; cancelAnimationFrame(raf); if (AUDIO && sr) { try { sr.stop(); } catch(_){} sr = null; } },
339
+ stop() { on = 0; tm = 0; cancelAnimationFrame(raf); if (AUDIO && sr) { try { sr.stop(); } catch(_){} sr = null; } },
340
+ destroy: teardown,
341
+ get time() { return tm; },
342
+ get isPlaying() { return !!on; }
343
+ };
344
+ if (opts?.autoplay) inst.play();
345
+ return inst;
346
+ } catch (err) { teardown(); throw err; }
230
347
  }
@@ -3,7 +3,9 @@
3
3
 
4
4
  import { extractBytecode, extractAudio } from "./extract.js";
5
5
  import { createAudioPlayer } from "./audio.js";
6
- import { attachPointerTracking, attachAutoResize, awaitWorkerReady, createPngine, destroy, routeWorkerMessages, sizeCanvasForInit } from "./init-core.js";
6
+ // routeWorkerMessages is NOT called here — awaitWorkerReady installs it at the
7
+ // instant `ready` arrives (see init.js).
8
+ import { attachPointerTracking, attachAutoResize, awaitWorkerReady, createPngine, destroy, sizeCanvasForInit } from "./init-core.js";
7
9
  import { initialPlayback } from "./playback-state.js";
8
10
 
9
11
  /** @typedef {import("./init-core.js").Pngine} Pngine */
@@ -86,44 +88,41 @@ export async function pngine(source, options = {}) {
86
88
  const { logicalW, logicalH, deviceW, deviceH } = sizeCanvasForInit(canvas, options);
87
89
 
88
90
  // Pointer/keyboard tracking — must attach before transferControlToOffscreen.
89
- const { pointer: ptr, onKeyDown, onKeyUp } = attachPointerTracking(canvas);
90
-
91
- const offscreen = canvas.transferControlToOffscreen();
92
- const worker = new Worker(getWorkerUrl(), { type: "module" });
93
-
94
- // Slice bytecode so the original buffer stays alive for extractAudio below.
95
- const bc = bytecode.slice();
96
- const result = await awaitWorkerReady(
97
- worker,
98
- {
99
- type: "init",
100
- canvas: offscreen,
101
- bytecode: bc,
102
- debug: options.debug || false,
103
- },
104
- [offscreen, bc.buffer],
105
- options
106
- );
107
-
108
- // Extract and initialize audio from PNG (pNGa chunk) on main thread
109
- let audio = null;
110
- if (rawData) {
111
- const audioWasm = await extractAudio(rawData);
112
- if (audioWasm) {
113
- audio = await createAudioPlayer(audioWasm);
114
- }
91
+ const { pointer: ptr, detach: detachListeners } = attachPointerTracking(canvas);
92
+
93
+ // Until the handle exists the listeners are ours to release — a worker that
94
+ // fails to come up rejects from this block (see init.js for the full note).
95
+ let worker, result;
96
+ try {
97
+ const offscreen = canvas.transferControlToOffscreen();
98
+ worker = new Worker(getWorkerUrl(), { type: "module" });
99
+
100
+ // Slice bytecode so the original buffer stays alive for extractAudio below.
101
+ const bc = bytecode.slice();
102
+ result = await awaitWorkerReady(
103
+ worker,
104
+ {
105
+ type: "init",
106
+ canvas: offscreen,
107
+ bytecode: bc,
108
+ debug: options.debug || false,
109
+ },
110
+ [offscreen, bc.buffer],
111
+ options
112
+ );
113
+ } catch (err) {
114
+ detachListeners();
115
+ throw err;
115
116
  }
116
117
 
117
- // Routes worker, shader-compilation, and WebGPU uncaptured errors (plus query
118
- // results) to options.onError / options.onQueryResult.
119
- routeWorkerMessages(worker, options);
120
-
118
+ // Built BEFORE the audio phase — see init.js for why: by this point the
119
+ // device, the transferred canvas and seven listeners are all committed, and
120
+ // this handle is the only thing that can release them.
121
121
  const p = createPngine({
122
122
  canvas,
123
123
  worker,
124
124
  pointer: ptr,
125
- onKeyDown,
126
- onKeyUp,
125
+ detachListeners,
127
126
  width: logicalW,
128
127
  height: logicalH,
129
128
  frameCount: result.frameCount,
@@ -131,9 +130,20 @@ export async function pngine(source, options = {}) {
131
130
  ready: true,
132
131
  pb: initialPlayback(),
133
132
  animationId: null,
134
- audio,
133
+ audio: null,
135
134
  });
136
135
 
136
+ // Extract and initialize audio from PNG (pNGa chunk) on main thread.
137
+ try {
138
+ if (rawData) {
139
+ const audioWasm = await extractAudio(rawData);
140
+ if (audioWasm) p._.audio = await createAudioPlayer(audioWasm);
141
+ }
142
+ } catch (err) {
143
+ destroy(p);
144
+ throw err;
145
+ }
146
+
137
147
  // Keep the backing store synced to the canvas's rendered size / DPR.
138
148
  p._.disconnectResize = attachAutoResize(canvas, worker, deviceW, deviceH, (w, h) => {
139
149
  p._.width = w;
package/src/viewer.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // PNGine Viewer API (lean production profile)
2
2
 
3
3
  export { pngine, destroy } from "./viewer-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";