pngine 4.3.1 → 4.3.2

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.
@@ -54,6 +54,10 @@ let workerUrl = null;
54
54
  * contents of every `map-read` buffer the frame copies into, once per submit,
55
55
  * as one number per 64-bit lane. Written for timestamp and occlusion query
56
56
  * results; a compute readback has to write 64-bit lanes to survive it.
57
+ * @param {(readback: import("./init-core.js").TextureReadback) => void} [options.onTextureReadback] -
58
+ * Receives the bytes of every `copy-texture-to-buffer` into a MAP_READ|COPY_DST
59
+ * buffer, once per submit, as an owned Uint8Array plus the padded layout and
60
+ * subresource metadata that describe exactly what those bytes contain.
57
61
  * @returns {Promise<Pngine>}
58
62
  */
59
63
  export async function pngine(source, options = {}) {
package/src/viewer.js CHANGED
@@ -1,7 +1,7 @@
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, writeBuffer, setPointer, getBuffers, getStats, restart, restartSettled } from "./anim.js";
4
+ export { draw, play, pause, stop, seek, setFrame, setUniform, setUniforms, getUniforms, writeBuffer, setPointer, getBuffers, writeTexture, getTextures, getStats, restart, restartSettled } from "./anim.js";
5
5
  export { extractBytecode, detectFormat, isPng, isZip, isPngb } from "./extract.js";
6
6
  // The payload walker (02.C2): re-exported here so the types gate pins its
7
7
  // declaration in the published d.ts (index.js's explicit payloadsOf export
@@ -59,12 +59,20 @@ export const MSG = {
59
59
  // only the listing. docs/plans/public-write-buffer.md.
60
60
  GET_BUFFERS: "getBuffers",
61
61
  BUFFERS: "buffers",
62
+ // Host texture writes ride DRAW too (`textures`); this pair is the listing
63
+ // (docs/plans/texture-write/04-live-host-api.md D12/D13).
64
+ GET_TEXTURES: "getTextures",
65
+ TEXTURES: "textures",
62
66
  GET_STATS: "getStats",
63
67
  STATS: "stats",
64
68
  RESIZE: "resize",
65
69
  READY: "ready",
66
70
  ERROR: "error",
67
71
  UNIFORMS: "uniforms",
72
+ // Texture→buffer bytes (plan 05, readback contract): the payload is the
73
+ // plan's exact record, posted with its owned byte buffer in the transfer
74
+ // list. Query numbers keep arriving as "queryResult".
75
+ TEXTURE_READBACK: "textureReadback",
68
76
  PREFETCH: "prefetch",
69
77
  SET_PLAYING: "setPlaying",
70
78
  THUMB_SPACING: "thumb-spacing",
@@ -214,6 +222,11 @@ export function createWorkerHandlers(env, opts = {}) {
214
222
  gpu.setOnShaderError((err) => { post({ type: "shader-error", ...err }); });
215
223
  gpu.setOnError((err) => reportCommandError(err.message, err.source));
216
224
  gpu.setOnQueryResult((results) => { post({ type: "queryResult", data: results }); });
225
+ gpu.setOnTextureReadback((readback) => {
226
+ // The byte array is an owned copy whose buffer nothing else aliases, so
227
+ // the worker message transfers it rather than doubling the pixel bytes.
228
+ post({ type: MSG.TEXTURE_READBACK, data: readback }, [readback.bytes.buffer]);
229
+ });
217
230
  }
218
231
 
219
232
  // Instantiate `bytes` as this worker's executor and rebind everything that
@@ -601,12 +614,13 @@ export function createWorkerHandlers(env, opts = {}) {
601
614
  wasm.setDataLen(dataLen);
602
615
 
603
616
  // Parse uniform table from bytecode before init (for runtime reflection).
604
- // The same section carries the buffer-name block, when the payload has
605
- // one; a payload without it names nothing, and every host write by name
606
- // is refused as unknown rather than guessed.
607
- const { uniforms, buffers, strings } = parseUniformTable(bytecodeArray);
617
+ // The same section carries the buffer- and texture-name blocks, when the
618
+ // payload has them; a payload without one names nothing, and every host
619
+ // write by that kind of name is refused as unknown rather than guessed.
620
+ const { uniforms, buffers, textures, strings } = parseUniformTable(bytecodeArray);
608
621
  uniformMap = uniforms;
609
622
  bufferNames = buffers ?? new Map();
623
+ const textureNames = textures ?? new Map();
610
624
  if (debugMode && uniforms.size > 0) {
611
625
  console.log(`[Worker] Parsed ${uniforms.size} uniform fields:`, [...uniforms.keys()]);
612
626
  }
@@ -626,9 +640,10 @@ export function createWorkerHandlers(env, opts = {}) {
626
640
  await gpu.execute(initPtr);
627
641
  }
628
642
 
629
- // Set uniform table after resources are created (buffers exist now)
643
+ // Set uniform table after resources are created (buffers/textures exist now)
630
644
  gpu.setUniformTable(uniformMap);
631
645
  gpu.setBufferNames(bufferNames);
646
+ gpu.setTextureNames(textureNames);
632
647
 
633
648
  // The payload's frames, by name, out of the executor that just indexed
634
649
  // them. init() cleared any pin on the executor side, so the tracker says
@@ -743,6 +758,17 @@ export function createWorkerHandlers(env, opts = {}) {
743
758
  const n = gpu.writeBuffers(data.buffers, wasm.getFrameCounter?.());
744
759
  if (debugMode && n > 0) console.log(`[Worker] Wrote ${n} host buffers`);
745
760
  }
761
+ // Host texture writes follow the buffers in the same locked section
762
+ // (D12: uniforms → buffers → textures), so the frame's own static queue
763
+ // operations, which run after this section, may overwrite them. The
764
+ // counter is read here, inside the lock and before frame(): `"active"`
765
+ // must resolve the member this frame's pooled bindings read, not a
766
+ // post-frame value (a preview moves and restores the counter under the
767
+ // same lock, so an outside read could be a thumbnail's).
768
+ if (data.textures && typeof data.textures === "object") {
769
+ const n = gpu.writeTextures(data.textures, wasm.getFrameCounter?.());
770
+ if (debugMode && n > 0) console.log(`[Worker] Wrote ${n} host textures`);
771
+ }
746
772
  // The frame this draw renders: a name, or null for the executor's own
747
773
  // choice (its first frame, or the animation table's scene). Applied
748
774
  // only when it differs from what the executor already holds; a draw
@@ -839,6 +865,15 @@ export function createWorkerHandlers(env, opts = {}) {
839
865
  post({ type: MSG.BUFFERS, buffers: gpu?.getBuffers?.() ?? {} });
840
866
  }
841
867
 
868
+ // Report the payload's named textures — what a host may write, by name, and
869
+ // why not (`writable`, `frameWritten`), from the current descriptors. Empty
870
+ // before init, after destroy, and while the device is lost: the dispatcher's
871
+ // listing would be the dead device's stale resources (plan 04 D13).
872
+ function handleGetTextures() {
873
+ const textures = initialized && !deviceLost && gpu ? gpu.getTextures?.() ?? {} : {};
874
+ post({ type: MSG.TEXTURES, textures });
875
+ }
876
+
842
877
  // Report live GPU-resource counts + the executor's WASM memory size.
843
878
  //
844
879
  // The instrument for a long-running session: everything else the host can see
@@ -938,6 +973,9 @@ export function createWorkerHandlers(env, opts = {}) {
938
973
  case MSG.GET_BUFFERS:
939
974
  handleGetBuffers();
940
975
  break;
976
+ case MSG.GET_TEXTURES:
977
+ handleGetTextures();
978
+ break;
941
979
  case MSG.GET_STATS:
942
980
  handleGetStats();
943
981
  break;
@@ -982,12 +1020,13 @@ export function createWorkerHandlers(env, opts = {}) {
982
1020
  let chain = Promise.resolve();
983
1021
  // Superseded rAF draws are dropped rather than rendered back-to-back once the
984
1022
  // stall clears: a frame whose time has already been overtaken is work nobody
985
- // will see. A draw carrying `uniforms` or `buffers` is NEVER dropped — that
986
- // state is host-supplied (anim.js draw(p, {uniforms, buffers})) and the next
987
- // frame does not re-send it, so collapsing it would silently lose a write.
1023
+ // will see. A draw carrying `uniforms`, `buffers` or `textures` is NEVER
1024
+ // dropped — that state is host-supplied (anim.js draw(p, {uniforms,
1025
+ // buffers, textures})) and the next frame does not re-send it, so collapsing
1026
+ // it would silently lose a write.
988
1027
  let queuedDraws = 0;
989
1028
  function dispatch(message) {
990
- const supersedable = message.type === MSG.DRAW && !message.uniforms && !message.buffers;
1029
+ const supersedable = message.type === MSG.DRAW && !message.uniforms && !message.buffers && !message.textures;
991
1030
  if (supersedable) queuedDraws++;
992
1031
  const run = () => {
993
1032
  if (supersedable && --queuedDraws > 0) return;
Binary file
package/wasm/pngine.wasm CHANGED
Binary file