pngine 2.0.0 → 2.2.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 +20 -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 +64 -24
- package/src/anim.js +78 -13
- package/src/dev.js +1 -1
- package/src/gpu-queue-commands.js +29 -11
- package/src/gpu-resource-pass-commands.js +73 -15
- package/src/gpu-stats.js +57 -0
- package/src/gpu.js +195 -28
- package/src/init-core.js +112 -26
- package/src/init.js +41 -24
- package/src/mini.js +189 -93
- package/src/viewer-init.js +20 -18
- package/src/viewer.js +1 -1
- package/src/worker-core.js +115 -2
- package/src/worker-prefetch.js +14 -1
- package/src/worker-viewer.js +0 -1
- package/wasm/pngine-compiler.wasm +0 -0
- package/wasm/pngine.wasm +0 -0
package/src/init.js
CHANGED
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
|
|
4
4
|
import { extractBytecode, extractAudio } from "./extract.js";
|
|
5
5
|
import { createAudioPlayer } from "./audio.js";
|
|
6
|
-
|
|
6
|
+
// routeWorkerMessages is NOT called here: awaitWorkerReady installs it at the
|
|
7
|
+
// instant `ready` arrives, so no window exists in which neither owns the
|
|
8
|
+
// worker's messages (the audio phase below used to be exactly that window).
|
|
9
|
+
import { attachPointerTracking, attachAutoResize, awaitWorkerReady, createPngine, destroy, sizeCanvasForInit } from "./init-core.js";
|
|
7
10
|
import { initialPlayback } from "./playback-state.js";
|
|
8
11
|
|
|
9
12
|
/** @typedef {import("./init-core.js").Pngine} Pngine */
|
|
@@ -73,7 +76,7 @@ export async function pngine(source, options = {}) {
|
|
|
73
76
|
const { logicalW, logicalH, deviceW, deviceH } = sizeCanvasForInit(canvas, options);
|
|
74
77
|
|
|
75
78
|
// Pointer/keyboard tracking — must attach before transferControlToOffscreen.
|
|
76
|
-
const { pointer: ptr,
|
|
79
|
+
const { pointer: ptr, detach: detachListeners } = attachPointerTracking(canvas);
|
|
77
80
|
|
|
78
81
|
// Get OffscreenCanvas
|
|
79
82
|
const offscreen = canvas.transferControlToOffscreen();
|
|
@@ -104,26 +107,20 @@ export async function pngine(source, options = {}) {
|
|
|
104
107
|
options
|
|
105
108
|
);
|
|
106
109
|
|
|
107
|
-
//
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
// Route worker, shader-compilation, and WebGPU uncaptured errors (plus query
|
|
117
|
-
// results) to options.onError / options.onQueryResult.
|
|
118
|
-
routeWorkerMessages(worker, options);
|
|
119
|
-
|
|
120
|
-
// Create POJO (report logical/CSS dimensions, not physical)
|
|
110
|
+
// The instance exists BEFORE the audio phase, because the audio phase is the
|
|
111
|
+
// first thing that can fail after the worker reports ready — and by then the
|
|
112
|
+
// GPUDevice is live, the canvas has been transferred, the bytecode went with
|
|
113
|
+
// it, and seven listeners are attached through an AbortController that only
|
|
114
|
+
// this handle can reach. A truncated pNGa is enough: createAudioPlayer's
|
|
115
|
+
// first act is WebAssembly.instantiate. Rejecting from here without a handle
|
|
116
|
+
// stranded every one of those for the page's life.
|
|
117
|
+
//
|
|
118
|
+
// (Report logical/CSS dimensions, not physical.)
|
|
121
119
|
const p = createPngine({
|
|
122
120
|
canvas,
|
|
123
121
|
worker,
|
|
124
122
|
pointer: ptr,
|
|
125
|
-
|
|
126
|
-
onKeyUp,
|
|
123
|
+
detachListeners,
|
|
127
124
|
width: logicalW,
|
|
128
125
|
height: logicalH,
|
|
129
126
|
frameCount: result.frameCount,
|
|
@@ -131,9 +128,22 @@ export async function pngine(source, options = {}) {
|
|
|
131
128
|
ready: true,
|
|
132
129
|
pb: initialPlayback(),
|
|
133
130
|
animationId: null,
|
|
134
|
-
audio,
|
|
131
|
+
audio: null,
|
|
135
132
|
});
|
|
136
133
|
|
|
134
|
+
// Extract and initialize audio from PNG (pNGa chunk) on main thread. A
|
|
135
|
+
// failed setup and a host teardown are the same release, so they are the
|
|
136
|
+
// same call — destroy() acks the worker's device release before terminating.
|
|
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;
|
|
@@ -147,12 +157,19 @@ export async function pngine(source, options = {}) {
|
|
|
147
157
|
* Initialize from image element
|
|
148
158
|
*/
|
|
149
159
|
async function initFromImage(img, options) {
|
|
150
|
-
// Wait for image to load if needed
|
|
160
|
+
// Wait for image to load if needed. Both handlers are CLEARED once settled:
|
|
161
|
+
// each closes over the promise's continuation — i.e. the rest of pngine() —
|
|
162
|
+
// and they are pinned to an element the host owns, so leaving them attached
|
|
163
|
+
// keeps the whole init alive for as long as the page keeps the <img>.
|
|
151
164
|
if (!img.complete) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
165
|
+
try {
|
|
166
|
+
await new Promise((resolve, reject) => {
|
|
167
|
+
img.onload = resolve;
|
|
168
|
+
img.onerror = reject;
|
|
169
|
+
});
|
|
170
|
+
} finally {
|
|
171
|
+
img.onload = img.onerror = null;
|
|
172
|
+
}
|
|
156
173
|
}
|
|
157
174
|
|
|
158
175
|
const { naturalWidth: w, naturalHeight: h } = img;
|
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
|
-
//
|
|
34
|
-
//
|
|
35
|
-
let
|
|
36
|
-
|
|
37
|
-
|
|
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) {
|
|
38
53
|
const R=o=>V.getUint16(o,!0),G=o=>V.getUint32(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;
|
|
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]
|
|
71
|
+
bf[id] ??= dv.createBuffer({size: sz, usage: f}); p += 8; break;
|
|
48
72
|
}
|
|
49
|
-
case 4: sh[R(p)]
|
|
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)
|
|
52
|
-
pp[id]
|
|
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??'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'}});
|
|
79
|
+
}
|
|
53
80
|
p += 10; break;
|
|
54
81
|
}
|
|
55
82
|
case 6: {
|
|
56
|
-
const id = R(p)
|
|
57
|
-
cp[id]
|
|
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??'main'}});
|
|
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,39 @@ 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
|
-
|
|
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
116
|
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;
|
|
85
117
|
case 17: cP = -1; if (!en) en = dv.createCommandEncoder(); ps = en.beginComputePass(); break;
|
|
86
118
|
case 18: { const pid = R(p); cP = pid; ps.setPipeline(pp[pid]||cp[pid]); p += 2; break; }
|
|
87
|
-
|
|
119
|
+
// Auto-layout rebuild, cached per (bind group, pipeline) in `dd.a` —
|
|
120
|
+
// mirrors gpu-resource-pass-commands.js 0x13. Uncached this made a fresh
|
|
121
|
+
// GPUBindGroup on EVERY frame that bound the group, none of them
|
|
122
|
+
// released; keyed by pipeline because one slot would thrash on a payload
|
|
123
|
+
// alternating two pipelines over one group.
|
|
124
|
+
//
|
|
125
|
+
// This branch was UNREACHABLE from a frame until `bd` became per-instance:
|
|
126
|
+
// only the init call populated it, and it was a local, so `dd` was always
|
|
127
|
+
// undefined here. The visible symptom was not the leak the cache prevents
|
|
128
|
+
// but the correctness bug underneath it — a group created against one
|
|
129
|
+
// pipeline and bound under another was handed over unrebuilt, which WebGPU
|
|
130
|
+
// rejects silently.
|
|
131
|
+
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
132
|
case 20: ps.setVertexBuffer(d[p], bf[R(p+1)]); p += 3; break;
|
|
89
133
|
case 21: ps.draw(G(p), G(p+4)); p += 16; break;
|
|
90
134
|
case 22: ps.drawIndexed(G(p), G(p+4)); p += 20; break;
|
|
91
135
|
case 23: ps.end(); ps = null; break;
|
|
92
136
|
case 24: ps.dispatchWorkgroups(G(p), G(p+4), G(p+8)); p += 12; break;
|
|
93
|
-
|
|
94
|
-
|
|
137
|
+
// Staged through the instance's own `ti`, not a `new Float32Array([…])`
|
|
138
|
+
// per frame: at 60Hz that is sixty allocations a second whose only purpose
|
|
139
|
+
// is to be copied by writeBuffer and dropped. Its neighbour below already
|
|
140
|
+
// avoided exactly this (the §310 lesson, applied to one arm and not the
|
|
141
|
+
// other).
|
|
142
|
+
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;
|
|
143
|
+
// The buffer is written wholesale — it already IS the wire layout, so
|
|
144
|
+
// there is no per-frame Float32Array to build.
|
|
145
|
+
case 38: dv.queue.writeBuffer(bf[R(p)], 0, pt); p += 8; break;
|
|
95
146
|
case 240: if (en) { dv.queue.submit([en.finish()]); en = null; } break;
|
|
96
147
|
case 255: return;
|
|
97
148
|
// An opcode mini can't decode. Its operands would be re-read as opcodes and
|
|
@@ -145,86 +196,131 @@ export async function miniPngine(canvas, source, opts) {
|
|
|
145
196
|
// Surface WebGPU validation / OOM the way the viewer does; without this a bad
|
|
146
197
|
// draw is silently wrong (WebGPU never throws synchronously for these).
|
|
147
198
|
dv.onuncapturederror = ev => report(gpuError(ev.error.message, 'uncaptured'));
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
//
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
//
|
|
160
|
-
//
|
|
161
|
-
//
|
|
162
|
-
//
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
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;
|
|
199
|
+
|
|
200
|
+
// Everything from here on can throw, and everything from here on is something
|
|
201
|
+
// that has to be released: the device above, the configured context, the five
|
|
202
|
+
// canvas listeners, every resource the init commands create. `inst` — the only
|
|
203
|
+
// object that used to carry a destroy() — is not built until the very end, so
|
|
204
|
+
// a throw in between rejected this promise with NOTHING able to release any of
|
|
205
|
+
// it: device, context and listeners stayed alive for the page's life. The
|
|
206
|
+
// worker tier documents this exact class and fixes it (init-core.js: "every
|
|
207
|
+
// failure path TERMINATES the worker before rejecting").
|
|
208
|
+
//
|
|
209
|
+
// So the teardown exists BEFORE the things it releases, and there is only one
|
|
210
|
+
// of it: a setup failure and a host destroy() are the same release, run from
|
|
211
|
+
// the same closure. `dead` makes it idempotent — a second destroy() used to
|
|
212
|
+
// call ax.close() on an already-closed AudioContext (an unhandled rejection)
|
|
213
|
+
// and dv.destroy() twice — and it is also what keeps play() from resurrecting
|
|
214
|
+
// the loop, since WebGPU calls on a destroyed device do not throw
|
|
215
|
+
// synchronously and frame()'s try/catch would never have fired.
|
|
216
|
+
let on = 0, t0 = 0, tm = 0, raf = 0, dead = 0;
|
|
217
|
+
let ax, ab2, sr, ctx;
|
|
218
|
+
const teardown = () => {
|
|
219
|
+
if (dead) return; dead = 1;
|
|
184
220
|
on = 0; cancelAnimationFrame(raf);
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
221
|
+
if (AUDIO && sr) { try { sr.stop(); } catch(_){} sr = null; }
|
|
222
|
+
if (AUDIO && ax && ax.state !== 'closed') ax.close();
|
|
223
|
+
// Nulling the five on* handlers is the mini tier's whole listener story:
|
|
224
|
+
// they are properties, not addEventListener registrations, and each closes
|
|
225
|
+
// over the canvas. Leaving them set kept the canvas and this instance's
|
|
226
|
+
// pointer state alive for as long as the host held the element.
|
|
227
|
+
canvas.onpointerdown = canvas.onpointermove = canvas.onpointerup = canvas.onpointercancel = canvas.onwheel = null;
|
|
228
|
+
dv.onuncapturederror = null;
|
|
229
|
+
ctx?.unconfigure?.();
|
|
230
|
+
dv.destroy();
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
ctx = /** @type {GPUCanvasContext} */ (canvas.getContext('webgpu'));
|
|
235
|
+
const fm = navigator.gpu.getPreferredCanvasFormat();
|
|
236
|
+
// pNGf flags bit 0 = opaque canvas (mirrors the PNGB header flag; spec/04).
|
|
237
|
+
ctx.configure({device: dv, format: fm, alphaMode: fl[1] & 1 ? 'opaque' : 'premultiplied'});
|
|
238
|
+
|
|
239
|
+
const W = canvas.width, H = canvas.height;
|
|
240
|
+
const V = new DataView(fl.buffer, fl.byteOffset, fl.byteLength);
|
|
241
|
+
const il = V.getUint32(2, true), frl = V.getUint32(6, true);
|
|
242
|
+
const ds = 14 + il + frl;
|
|
243
|
+
const bf = [], sh = [], pp = [], cp_ = [], bg = [];
|
|
244
|
+
|
|
245
|
+
// Pointer/wheel listeners fill this instance's 12 pointer-inputs floats.
|
|
246
|
+
// Per-instance, not module-level: see the layout note above X().
|
|
247
|
+
// Modifier bits 1/2/4/8 (shift/ctrl/alt/meta) mirror init-core.js; the Space
|
|
248
|
+
// bit (16) is omitted — the minimal viewer attaches no document keyboard
|
|
249
|
+
// listeners. Scroll (9/10) accumulates sign(delta) like init-core.
|
|
250
|
+
const pt = new Float32Array(12);
|
|
251
|
+
let lx=0,ly=0;
|
|
252
|
+
const M=e=>(e.shiftKey?1:0)|(e.ctrlKey?2:0)|(e.altKey?4:0)|(e.metaKey?8:0);
|
|
253
|
+
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)};
|
|
254
|
+
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};
|
|
255
|
+
canvas.onpointerup=()=>{pt[2]=-Math.abs(pt[2]);pt[3]=-Math.abs(pt[3]);pt[6]=0;pt[7]=0};
|
|
256
|
+
canvas.onpointercancel=canvas.onpointerup;
|
|
257
|
+
canvas.onwheel=e=>{e.preventDefault();pt[9]+=Math.sign(e.deltaX);pt[10]+=Math.sign(e.deltaY);pt[8]=M(e)};
|
|
198
258
|
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
//
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
259
|
+
// The bind-group rebuild cache and the time-uniform staging array: per
|
|
260
|
+
// instance, and threaded into every X() call so they survive between them.
|
|
261
|
+
const bd = [], ti = new Float32Array(4);
|
|
262
|
+
|
|
263
|
+
// Run init commands
|
|
264
|
+
X(fl, ds, 14, il, V, dv, ctx, fm, bf, sh, pp, cp_, bg, 0, W, H, pt, bd, ti);
|
|
265
|
+
// Run first frame
|
|
266
|
+
X(fl, ds, 14 + il, frl, V, dv, ctx, fm, bf, sh, pp, cp_, bg, 0, W, H, pt, bd, ti);
|
|
267
|
+
|
|
268
|
+
// Device loss stops the loop and reports once. `destroyed` is our own destroy()
|
|
269
|
+
// teardown — ignore it, and so is anything arriving after `dead` (a device can
|
|
270
|
+
// be lost for another reason in the same turn we tear it down). mini keeps no
|
|
271
|
+
// self-recovery (size play); the host decides whether to re-create the player.
|
|
272
|
+
dv.lost.then(info => {
|
|
273
|
+
if (dead || info.reason === 'destroyed') return;
|
|
207
274
|
on = 0; cancelAnimationFrame(raf);
|
|
208
|
-
report(
|
|
209
|
-
|
|
275
|
+
report(gpuError('pngine/mini: WebGPU device lost — ' + (info.message || info.reason || 'unknown'), 'device-lost'));
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
if (AUDIO && au) {
|
|
279
|
+
const {instance: ai} = await WebAssembly.instantiate(/** @type {BufferSource} */ (/** @type {unknown} */ (au)), /** @type {WebAssembly.Imports} */ (/** @type {unknown} */ ({m: Math})));
|
|
280
|
+
const ax2 = /** @type {SointuExports} */ (/** @type {unknown} */ (ai.exports));
|
|
281
|
+
const m = ax2.m, s = ax2.s.value, l = ax2.l.value, t = ax2.t.value === 1;
|
|
282
|
+
const fr = t ? l / 4 : l / 8;
|
|
283
|
+
const smp = t ? new Int16Array(m.buffer, s, fr * 2) : new Float32Array(m.buffer, s, fr * 2);
|
|
284
|
+
ax = new AudioContext({sampleRate: 44100});
|
|
285
|
+
ab2 = ax.createBuffer(2, fr, 44100);
|
|
286
|
+
deinterleaveStereo(ab2, smp, fr, t);
|
|
210
287
|
}
|
|
211
|
-
pdx = 0; pdy = 0; // reset pointer deltas per frame
|
|
212
|
-
raf = requestAnimationFrame(frame);
|
|
213
|
-
}
|
|
214
288
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
289
|
+
function frame() {
|
|
290
|
+
if (!on) return;
|
|
291
|
+
tm = (performance.now() - t0) / 1e3;
|
|
292
|
+
// A runtime fault (undecodable opcode, lost device) stops the loop and
|
|
293
|
+
// reports, instead of throwing uncaught out of rAF on every frame.
|
|
294
|
+
try {
|
|
295
|
+
X(fl, ds, 14 + il, frl, V, dv, ctx, fm, bf, sh, pp, cp_, bg, tm, W, H, pt, bd, ti);
|
|
296
|
+
} catch (err) {
|
|
297
|
+
on = 0; cancelAnimationFrame(raf);
|
|
298
|
+
report(err);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
pt[4] = 0; pt[5] = 0; // reset pointer deltas per frame
|
|
302
|
+
raf = requestAnimationFrame(frame);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const inst = {
|
|
306
|
+
// `dead` and not just `on`: destroy() left no flag, so play() — which
|
|
307
|
+
// checked only `if (on) return` — restarted the rAF chain on a destroyed
|
|
308
|
+
// device. Nothing threw (WebGPU is asynchronous about that), so the loop
|
|
309
|
+
// reschedules at 60Hz for the page's life, retaining the canvas, the payload
|
|
310
|
+
// and every closure in this function.
|
|
311
|
+
play() {
|
|
312
|
+
if (on || dead) return;
|
|
313
|
+
on = 1; t0 = performance.now() - tm * 1e3;
|
|
314
|
+
if (AUDIO && ax) { if (ax.state === 'suspended') ax.resume(); sr = ax.createBufferSource(); sr.buffer = ab2; sr.connect(ax.destination); sr.start(0, tm); }
|
|
315
|
+
frame();
|
|
316
|
+
},
|
|
317
|
+
pause() { on = 0; cancelAnimationFrame(raf); if (AUDIO && sr) { try { sr.stop(); } catch(_){} sr = null; } },
|
|
318
|
+
stop() { on = 0; tm = 0; cancelAnimationFrame(raf); if (AUDIO && sr) { try { sr.stop(); } catch(_){} sr = null; } },
|
|
319
|
+
destroy: teardown,
|
|
320
|
+
get time() { return tm; },
|
|
321
|
+
get isPlaying() { return !!on; }
|
|
322
|
+
};
|
|
323
|
+
if (opts?.autoplay) inst.play();
|
|
324
|
+
return inst;
|
|
325
|
+
} catch (err) { teardown(); throw err; }
|
|
230
326
|
}
|
package/src/viewer-init.js
CHANGED
|
@@ -3,7 +3,9 @@
|
|
|
3
3
|
|
|
4
4
|
import { extractBytecode, extractAudio } from "./extract.js";
|
|
5
5
|
import { createAudioPlayer } from "./audio.js";
|
|
6
|
-
|
|
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,7 +88,7 @@ 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,
|
|
91
|
+
const { pointer: ptr, detach: detachListeners } = attachPointerTracking(canvas);
|
|
90
92
|
|
|
91
93
|
const offscreen = canvas.transferControlToOffscreen();
|
|
92
94
|
const worker = new Worker(getWorkerUrl(), { type: "module" });
|
|
@@ -105,25 +107,14 @@ export async function pngine(source, options = {}) {
|
|
|
105
107
|
options
|
|
106
108
|
);
|
|
107
109
|
|
|
108
|
-
//
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const audioWasm = await extractAudio(rawData);
|
|
112
|
-
if (audioWasm) {
|
|
113
|
-
audio = await createAudioPlayer(audioWasm);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
// Routes worker, shader-compilation, and WebGPU uncaptured errors (plus query
|
|
118
|
-
// results) to options.onError / options.onQueryResult.
|
|
119
|
-
routeWorkerMessages(worker, options);
|
|
120
|
-
|
|
110
|
+
// Built BEFORE the audio phase — see init.js for why: by this point the
|
|
111
|
+
// device, the transferred canvas and seven listeners are all committed, and
|
|
112
|
+
// this handle is the only thing that can release them.
|
|
121
113
|
const p = createPngine({
|
|
122
114
|
canvas,
|
|
123
115
|
worker,
|
|
124
116
|
pointer: ptr,
|
|
125
|
-
|
|
126
|
-
onKeyUp,
|
|
117
|
+
detachListeners,
|
|
127
118
|
width: logicalW,
|
|
128
119
|
height: logicalH,
|
|
129
120
|
frameCount: result.frameCount,
|
|
@@ -131,9 +122,20 @@ export async function pngine(source, options = {}) {
|
|
|
131
122
|
ready: true,
|
|
132
123
|
pb: initialPlayback(),
|
|
133
124
|
animationId: null,
|
|
134
|
-
audio,
|
|
125
|
+
audio: null,
|
|
135
126
|
});
|
|
136
127
|
|
|
128
|
+
// Extract and initialize audio from PNG (pNGa chunk) on main thread.
|
|
129
|
+
try {
|
|
130
|
+
if (rawData) {
|
|
131
|
+
const audioWasm = await extractAudio(rawData);
|
|
132
|
+
if (audioWasm) p._.audio = await createAudioPlayer(audioWasm);
|
|
133
|
+
}
|
|
134
|
+
} catch (err) {
|
|
135
|
+
destroy(p);
|
|
136
|
+
throw err;
|
|
137
|
+
}
|
|
138
|
+
|
|
137
139
|
// Keep the backing store synced to the canvas's rendered size / DPR.
|
|
138
140
|
p._.disconnectResize = attachAutoResize(canvas, worker, deviceW, deviceH, (w, h) => {
|
|
139
141
|
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";
|