@aardworx/wombat.rendering 0.9.3 → 0.9.5

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.
Files changed (74) hide show
  1. package/dist/core/buffer.d.ts.map +1 -1
  2. package/dist/core/buffer.js +65 -5
  3. package/dist/core/buffer.js.map +1 -1
  4. package/dist/core/index.d.ts +2 -0
  5. package/dist/core/index.d.ts.map +1 -1
  6. package/dist/core/index.js +1 -0
  7. package/dist/core/index.js.map +1 -1
  8. package/dist/core/provider.d.ts +38 -0
  9. package/dist/core/provider.d.ts.map +1 -0
  10. package/dist/core/provider.js +104 -0
  11. package/dist/core/provider.js.map +1 -0
  12. package/dist/core/renderObject.d.ts +17 -10
  13. package/dist/core/renderObject.d.ts.map +1 -1
  14. package/dist/core/sampler.d.ts.map +1 -1
  15. package/dist/core/sampler.js +71 -2
  16. package/dist/core/sampler.js.map +1 -1
  17. package/dist/core/texture.d.ts +9 -0
  18. package/dist/core/texture.d.ts.map +1 -1
  19. package/dist/core/texture.js +0 -0
  20. package/dist/core/texture.js.map +1 -1
  21. package/dist/resources/adaptiveTexture.d.ts.map +1 -1
  22. package/dist/resources/adaptiveTexture.js +7 -0
  23. package/dist/resources/adaptiveTexture.js.map +1 -1
  24. package/dist/resources/preparedRenderObject.d.ts.map +1 -1
  25. package/dist/resources/preparedRenderObject.js +20 -18
  26. package/dist/resources/preparedRenderObject.js.map +1 -1
  27. package/dist/resources/uniformBuffer.d.ts.map +1 -1
  28. package/dist/resources/uniformBuffer.js +23 -0
  29. package/dist/resources/uniformBuffer.js.map +1 -1
  30. package/dist/runtime/derivedUniforms/slots.d.ts.map +1 -1
  31. package/dist/runtime/derivedUniforms/slots.js +4 -0
  32. package/dist/runtime/derivedUniforms/slots.js.map +1 -1
  33. package/dist/runtime/heapAdapter.d.ts.map +1 -1
  34. package/dist/runtime/heapAdapter.js +72 -29
  35. package/dist/runtime/heapAdapter.js.map +1 -1
  36. package/dist/runtime/heapDecoder.d.ts +52 -0
  37. package/dist/runtime/heapDecoder.d.ts.map +1 -0
  38. package/dist/runtime/heapDecoder.js +363 -0
  39. package/dist/runtime/heapDecoder.js.map +1 -0
  40. package/dist/runtime/heapEffectIR.d.ts.map +1 -1
  41. package/dist/runtime/heapEffectIR.js +300 -3
  42. package/dist/runtime/heapEffectIR.js.map +1 -1
  43. package/dist/runtime/heapEligibility.d.ts.map +1 -1
  44. package/dist/runtime/heapEligibility.js +42 -5
  45. package/dist/runtime/heapEligibility.js.map +1 -1
  46. package/dist/runtime/heapIrBuilders.d.ts +64 -0
  47. package/dist/runtime/heapIrBuilders.d.ts.map +1 -0
  48. package/dist/runtime/heapIrBuilders.js +311 -0
  49. package/dist/runtime/heapIrBuilders.js.map +1 -0
  50. package/dist/runtime/heapScene.d.ts.map +1 -1
  51. package/dist/runtime/heapScene.js +286 -97
  52. package/dist/runtime/heapScene.js.map +1 -1
  53. package/dist/runtime/textureAtlas/atlasPool.d.ts +67 -4
  54. package/dist/runtime/textureAtlas/atlasPool.d.ts.map +1 -1
  55. package/dist/runtime/textureAtlas/atlasPool.js +152 -18
  56. package/dist/runtime/textureAtlas/atlasPool.js.map +1 -1
  57. package/package.json +4 -3
  58. package/src/core/buffer.ts +58 -6
  59. package/src/core/index.ts +11 -0
  60. package/src/core/provider.ts +143 -0
  61. package/src/core/renderObject.ts +17 -10
  62. package/src/core/sampler.ts +64 -2
  63. package/src/core/texture.ts +0 -0
  64. package/src/resources/adaptiveTexture.ts +7 -0
  65. package/src/resources/preparedRenderObject.ts +21 -16
  66. package/src/resources/uniformBuffer.ts +21 -0
  67. package/src/runtime/derivedUniforms/slots.ts +4 -0
  68. package/src/runtime/heapAdapter.ts +63 -29
  69. package/src/runtime/heapDecoder.ts +446 -0
  70. package/src/runtime/heapEffectIR.ts +314 -3
  71. package/src/runtime/heapEligibility.ts +34 -5
  72. package/src/runtime/heapIrBuilders.ts +365 -0
  73. package/src/runtime/heapScene.ts +295 -81
  74. package/src/runtime/textureAtlas/atlasPool.ts +152 -16
@@ -42,7 +42,7 @@
42
42
  // package adds the texture (binding 4) + sampler (binding 5) to that
43
43
  // group's bind-group layout; the user's FS WGSL declares them.
44
44
  import { Trafo3d, V3d, V4f, M44d } from "@aardworx/wombat.base";
45
- import { AVal, AdaptiveObject, AdaptiveToken } from "@aardworx/wombat.adaptive";
45
+ import { AVal, AdaptiveObject, AdaptiveToken, HashTable } from "@aardworx/wombat.adaptive";
46
46
  import { buildBucketLayout, compileHeapEffect, } from "./heapEffect.js";
47
47
  import { compileHeapEffectIR } from "./heapEffectIR.js";
48
48
  import { buildShaderFamily, compileShaderFamily, } from "./heapShaderFamily.js";
@@ -249,6 +249,17 @@ class GrowBuffer {
249
249
  * Same code path either way.
250
250
  */
251
251
  class UniformPool {
252
+ // Keyed by `aval<unknown>` *by reference* (a plain JS `Map`). These
253
+ // keys are overwhelmingly reactive `cval`s (per-object trafos,
254
+ // colours, …) and the hot path is `acquire`/`release` ~once per
255
+ // drawHeader field per RO. A content-keyed `HashTable` would buy
256
+ // nothing here — reactive avals never compare content-equal — and
257
+ // would cost: reactive avals have no `equals`/`getHashCode`, so a
258
+ // `HashTable` falls back to a WeakMap-counter identity hash per
259
+ // lookup, measurably slower than `Map`'s native hashing. (Constant-
260
+ // aval dedup matters where keys are *texture* avals — there the
261
+ // `AtlasPool` is content-keyed; constant avals there carry a cached
262
+ // hash and a fast `equals`.)
252
263
  byAval = new Map();
253
264
  has(aval) { return this.byAval.has(aval); }
254
265
  entry(aval) { return this.byAval.get(aval); }
@@ -499,22 +510,58 @@ class AttributeArena {
499
510
  }
500
511
  release(ref, dataBytes) {
501
512
  const allocBytes = ALIGN16(ALLOC_HEADER_PAD_TO + dataBytes);
502
- this.freeList.push({ off: ref, size: allocBytes });
503
- // Coalesce adjacent free entries for cleanliness.
504
- this.freeList.sort((a, b) => a.off - b.off);
505
- for (let i = 0; i < this.freeList.length - 1;) {
506
- const a = this.freeList[i], b = this.freeList[i + 1];
507
- if (a.off + a.size === b.off) {
508
- a.size += b.size;
509
- this.freeList.splice(i + 1, 1);
510
- }
511
- else
512
- i++;
513
- }
513
+ insertSortedFreeBlock(this.freeList, ref, allocBytes);
514
514
  }
515
515
  onResize(cb) { return this.buf.onResize(cb); }
516
516
  destroy() { this.buf.destroy(); }
517
517
  }
518
+ /**
519
+ * Insert `{off, size}` into a free-list kept sorted by `off`, then
520
+ * coalesce with the two immediate neighbours.
521
+ *
522
+ * The list invariant — sorted, non-overlapping, never-adjacent — is
523
+ * preserved across allocs (which take from the front or split a
524
+ * block) and releases (this function). The previous implementation
525
+ * did `push + Array.prototype.sort + linear coalesce scan`, which is
526
+ * O(N log N) per release. Under a 500-RO bulk-remove the sort
527
+ * dominated `removeDraw` (~41 ms of self-time in the heap-demo-sg
528
+ * toggle profile). Binary-search insert + 2-neighbour merge collapses
529
+ * that to O(log N + N-shift), and is principled — the sort never
530
+ * actually mattered since we already had the sorted prefix as an
531
+ * invariant.
532
+ */
533
+ function insertSortedFreeBlock(freeList, off, size) {
534
+ // Binary-search for the insertion index (first entry whose off > new).
535
+ let lo = 0, hi = freeList.length;
536
+ while (lo < hi) {
537
+ const mid = (lo + hi) >>> 1;
538
+ if (freeList[mid].off <= off)
539
+ lo = mid + 1;
540
+ else
541
+ hi = mid;
542
+ }
543
+ // lo is the index where the new entry would be inserted.
544
+ // Try merging with the predecessor first; if successful, the merged
545
+ // block may now be adjacent to its (former) successor too.
546
+ const prev = lo > 0 ? freeList[lo - 1] : undefined;
547
+ if (prev !== undefined && prev.off + prev.size === off) {
548
+ prev.size += size;
549
+ // Check forward-merge with what was freeList[lo].
550
+ const next = freeList[lo];
551
+ if (next !== undefined && prev.off + prev.size === next.off) {
552
+ prev.size += next.size;
553
+ freeList.splice(lo, 1);
554
+ }
555
+ return;
556
+ }
557
+ const next = freeList[lo];
558
+ if (next !== undefined && off + size === next.off) {
559
+ next.off = off;
560
+ next.size += size;
561
+ return;
562
+ }
563
+ freeList.splice(lo, 0, { off, size });
564
+ }
518
565
  /**
519
566
  * Element-bump allocator over an index GrowBuffer (units = u32). Each
520
567
  * draw's index range is allocated as one block; on release the block
@@ -578,17 +625,7 @@ class IndexAllocator {
578
625
  return off;
579
626
  }
580
627
  release(off, elements) {
581
- this.freeList.push({ off, size: elements });
582
- this.freeList.sort((a, b) => a.off - b.off);
583
- for (let i = 0; i < this.freeList.length - 1;) {
584
- const a = this.freeList[i], b = this.freeList[i + 1];
585
- if (a.off + a.size === b.off) {
586
- a.size += b.size;
587
- this.freeList.splice(i + 1, 1);
588
- }
589
- else
590
- i++;
591
- }
628
+ insertSortedFreeBlock(this.freeList, off, elements);
592
629
  }
593
630
  onResize(cb) { return this.buf.onResize(cb); }
594
631
  destroy() { this.buf.destroy(); }
@@ -812,6 +849,19 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
812
849
  throw new Error("buildHeapScene: framebuffer signature has no color attachment");
813
850
  }
814
851
  const colorFormat = sig.colors.tryFind(colorAttachmentName);
852
+ // All color targets, ordered by the signature's `colorNames`. Each
853
+ // entry's format flows into the pipeline's `fragment.targets[i].format`.
854
+ // The fragmentOutputLayout's `locations` map tells the WGSL emit which
855
+ // output @location maps to which color attachment; this array is the
856
+ // matching pipeline-side ordering.
857
+ const colorTargets = [];
858
+ for (const name of sig.colorNames) {
859
+ const fmt = sig.colors.tryFind(name);
860
+ if (fmt === undefined) {
861
+ throw new Error(`buildHeapScene: signature missing format for color '${name}'`);
862
+ }
863
+ colorTargets.push({ format: fmt });
864
+ }
815
865
  const depthFormat = sig.depthStencil?.format;
816
866
  // ─── Global arena (uniform/attribute data + index buffer) ────────
817
867
  // Initial capacities are just hints; both buffers pow2-grow on
@@ -974,7 +1024,11 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
974
1024
  // buckets) — `atlasAvalRefs` tracks all (bucket, localSlot) pairs so
975
1025
  // one swap rewrites N drawHeaders in one drain.
976
1026
  const atlasAvalDirty = new Set();
977
- const atlasAvalRefs = new Map();
1027
+ // Content-keyed (`HashTable`, not a JS `Map`) so distinct
1028
+ // `AVal.constant(tex)` wrappers sharing the same texture collapse to
1029
+ // one `(bucket, slot)` ref-list — matching `AtlasPool.entriesByAval`,
1030
+ // which is now content-keyed too. Reactive avals key by reference.
1031
+ const atlasAvalRefs = new HashTable();
978
1032
  /**
979
1033
  * Per-draw bucket dirty (rare in steady state — only fires when
980
1034
  * something forces a header rewrite, e.g. a drawHeap GrowBuffer
@@ -1175,30 +1229,59 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1175
1229
  // ─── Buckets ──────────────────────────────────────────────────────
1176
1230
  const buckets = [];
1177
1231
  const bucketByKey = new Map();
1178
- const familyByEffect = new Map();
1232
+ // Keyed by `effect.id` (content hash), NOT object identity. Two
1233
+ // Effect objects with identical content (e.g. produced by separate
1234
+ // calls to `effect(...)` or the pickChain composer) share one
1235
+ // FamilyState. This is the right correctness/perf knob: building a
1236
+ // family by content means an upstream caller that legitimately
1237
+ // produces different-but-identical Effect objects per leaf still
1238
+ // reuses one pipeline + one bucket.
1239
+ const familyByEffectId = new Map();
1179
1240
  let familyBuilt = false;
1180
1241
  const enableFamilyMerge = opts.enableFamilyMerge === true;
1181
1242
  function compileFamilyFor(effects, perInstanceByEffect) {
1182
- const schema = buildShaderFamily(effects, opts.fragmentOutputLayout, undefined, {
1243
+ // Family-merge (multi-effect dispatch via layoutId switch) has been
1244
+ // disabled — it was a perf illusion. Every effect compiles to its
1245
+ // own standalone pipeline; the per-bucket WGSL comes from
1246
+ // `compileHeapEffectIR(effect, layout, opts, "standalone")` and is
1247
+ // used directly as the bucket's shader module (no wrapper, no
1248
+ // dispatch). The schema is still derived from the same
1249
+ // `buildShaderFamily` path so the runtime's drawHeader packer,
1250
+ // bind-group layout, etc. don't change shape — `__layoutId`
1251
+ // remains a u32 slot that's written as 0 per RO (harmless).
1252
+ //
1253
+ // `heapShaderFamily.compileShaderFamily` is kept around but
1254
+ // unused; the function and its tests stay disabled.
1255
+ if (effects.length !== 1) {
1256
+ throw new Error("heapScene: multi-effect family-merge disabled (was a perf illusion). " +
1257
+ "Build one bucket per effect.");
1258
+ }
1259
+ const effect = effects[0];
1260
+ const schema = buildShaderFamily([effect], opts.fragmentOutputLayout, undefined, {
1183
1261
  atlasizeAllTextures: atlasPool !== undefined,
1184
1262
  perEffectPerInstance: perInstanceByEffect,
1185
1263
  });
1186
- const compiled = compileShaderFamily(schema, opts.fragmentOutputLayout);
1187
- const vsModule = device.createShaderModule({ code: compiled.vs, label: `heapScene/family/${schema.id}/vs` });
1188
- const fsModule = device.createShaderModule({ code: compiled.fs, label: `heapScene/family/${schema.id}/fs` });
1264
+ const compileOpts = opts.fragmentOutputLayout !== undefined
1265
+ ? { target: "wgsl", fragmentOutputLayout: opts.fragmentOutputLayout }
1266
+ : { target: "wgsl" };
1267
+ const ir = compileHeapEffectIR(effect, schema.drawHeaderUnion, compileOpts, "standalone");
1268
+ const vsModule = device.createShaderModule({ code: ir.vs, label: `heapScene/standalone/${schema.id}/vs` });
1269
+ const fsModule = device.createShaderModule({ code: ir.fs, label: `heapScene/standalone/${schema.id}/fs` });
1189
1270
  const fieldsForEffect = new Map();
1190
- for (const e of schema.effects) {
1191
- const s = schema.perEffectSchema.get(e);
1192
- const fields = new Set();
1193
- for (const a of s.attributes)
1194
- fields.add(a.name);
1195
- for (const u of s.uniforms)
1196
- fields.add(u.name);
1197
- for (const t of s.textures)
1198
- fields.add(t.name);
1199
- fieldsForEffect.set(e, fields);
1200
- }
1201
- return { schema, vsModule, fsModule, fieldsForEffect };
1271
+ const s = schema.perEffectSchema.get(effect);
1272
+ const fields = new Set();
1273
+ for (const a of s.attributes)
1274
+ fields.add(a.name);
1275
+ for (const u of s.uniforms)
1276
+ fields.add(u.name);
1277
+ for (const t of s.textures)
1278
+ fields.add(t.name);
1279
+ fieldsForEffect.set(effect.id, fields);
1280
+ return {
1281
+ schema, vsModule, fsModule, fieldsForEffect,
1282
+ vsEntryName: ir.vsEntry,
1283
+ fsEntryName: ir.fsEntry,
1284
+ };
1202
1285
  }
1203
1286
  function buildFamilyFromSpecs(specs) {
1204
1287
  if (familyBuilt)
@@ -1212,48 +1295,43 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1212
1295
  // layout to address per-instance attribute reads via `instId`
1213
1296
  // instead of `vertex_index` — without this, instanced effects
1214
1297
  // produce broken geometry.
1215
- const seen = new Set();
1298
+ // De-dupe by effect.id so two Effects with identical content but
1299
+ // distinct object identities collapse to one family + one bucket.
1300
+ const seenIds = new Set();
1216
1301
  const unique = [];
1217
- const perInstanceByEffect = new Map();
1302
+ const perInstanceByEffectId = new Map();
1218
1303
  for (const spec of specs) {
1219
1304
  const e = spec.effect;
1220
- let entry = perInstanceByEffect.get(e);
1305
+ let entry = perInstanceByEffectId.get(e.id);
1221
1306
  if (entry === undefined) {
1222
1307
  entry = { attributes: new Set(), uniforms: new Set() };
1223
- perInstanceByEffect.set(e, entry);
1308
+ perInstanceByEffectId.set(e.id, entry);
1224
1309
  }
1225
1310
  if (spec.instanceAttributes !== undefined) {
1226
1311
  for (const name of Object.keys(spec.instanceAttributes))
1227
1312
  entry.attributes.add(name);
1228
1313
  }
1229
- if (!seen.has(e)) {
1230
- seen.add(e);
1314
+ if (!seenIds.has(e.id)) {
1315
+ seenIds.add(e.id);
1231
1316
  unique.push(e);
1232
1317
  }
1233
1318
  }
1234
- if (enableFamilyMerge) {
1235
- const merged = compileFamilyFor(unique, perInstanceByEffect);
1236
- for (const e of unique)
1237
- familyByEffect.set(e, merged);
1238
- }
1239
- else {
1240
- // Default: one family per effect — no shared layoutId switch.
1241
- // Per-effect bucketing is at-or-better than merged on tested
1242
- // workloads; merge stays opt-in pending trace-based v2.
1243
- for (const e of unique) {
1244
- const perI = perInstanceByEffect.get(e);
1245
- const singleMap = new Map();
1246
- if (perI !== undefined)
1247
- singleMap.set(e, perI);
1248
- familyByEffect.set(e, compileFamilyFor([e], singleMap));
1249
- }
1319
+ // Family-merge disabled: always one bucket per effect.
1320
+ // `enableFamilyMerge` is ignored.
1321
+ void enableFamilyMerge;
1322
+ for (const e of unique) {
1323
+ const perI = perInstanceByEffectId.get(e.id);
1324
+ const singleMap = new Map();
1325
+ if (perI !== undefined)
1326
+ singleMap.set(e, perI);
1327
+ familyByEffectId.set(e.id, compileFamilyFor([e], singleMap));
1250
1328
  }
1251
1329
  familyBuilt = true;
1252
1330
  }
1253
1331
  function familyFor(effect) {
1254
- const f = familyByEffect.get(effect);
1332
+ const f = familyByEffectId.get(effect.id);
1255
1333
  if (f === undefined) {
1256
- const known = [...familyByEffect.keys()].map(e => e.id).join(",");
1334
+ const known = [...familyByEffectId.keys()].join(",");
1257
1335
  throw new Error(`heapScene: family is frozen; effect ${effect.id} not in {${known}}; ` +
1258
1336
  `reactive family rebuild is v2`);
1259
1337
  }
@@ -1279,17 +1357,102 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1279
1357
  }
1280
1358
  return id;
1281
1359
  };
1282
- const psIds = new WeakMap();
1283
- let psCounter = 0;
1360
+ // PipelineState content key. Hashes by the identities of the inner
1361
+ // aval references rather than the wrapper object itself: callers
1362
+ // (notably `wombat.dom`'s `derivePipelineState`) construct a fresh
1363
+ // PipelineState object per leaf even when every contributing aval
1364
+ // is shared — without a content-aware key, every leaf gets its own
1365
+ // bucket and the heap path's drawIndirect coalescing collapses to
1366
+ // one record per bucket. With this key, two leaves that pulled the
1367
+ // same `state.mode` / `state.cullMode` / `state.depthTest` / … avals
1368
+ // bucket together.
1369
+ //
1370
+ // For CONSTANT avals (`isConstant === true`) we key by *value* — two
1371
+ // distinct `AVal.constant(0)` objects produced at different call
1372
+ // sites must collapse to the same bucket. Reactive avals fall back
1373
+ // to reference identity (the right semantic — value can tick).
1374
+ const avalIds = new WeakMap();
1375
+ const valueIds = new Map();
1376
+ let avalCounter = 0;
1377
+ const avalIdOf = (av) => {
1378
+ if (av === undefined)
1379
+ return "_";
1380
+ // Reactive aval — key by reference. (`isConstant` is on the
1381
+ // public IAdaptive surface; guard via runtime check so off-spec
1382
+ // duck-types don't blow up.)
1383
+ const isConst = av.isConstant === true;
1384
+ if (!isConst) {
1385
+ let id = avalIds.get(av);
1386
+ if (id === undefined) {
1387
+ id = avalCounter++;
1388
+ avalIds.set(av, id);
1389
+ }
1390
+ return `a${id}`;
1391
+ }
1392
+ // Constant: key by value. Force is safe (constant: no upstream
1393
+ // dep) and only runs once per distinct aval-object thanks to the
1394
+ // outer avalIds cache below.
1395
+ let id = avalIds.get(av);
1396
+ if (id !== undefined)
1397
+ return `c${id}`;
1398
+ // Constant avals ignore the token; use AdaptiveToken.top to satisfy
1399
+ // the type and traverse a no-op evaluation.
1400
+ const v = av.getValue(AdaptiveToken.top);
1401
+ // Value-typed (`equals` + `getHashCode`)? Try to intern by hash
1402
+ // bucket + equals so two distinct AVal.constant(M44d.identity)
1403
+ // collapse. Falls back to a per-value string key otherwise.
1404
+ let vKey;
1405
+ if (v !== null && typeof v === "object" &&
1406
+ typeof v.getHashCode === "function" &&
1407
+ typeof v.equals === "function") {
1408
+ const hc = v.getHashCode() | 0;
1409
+ vKey = `hv:${hc}`;
1410
+ }
1411
+ else if (v === null || typeof v !== "object") {
1412
+ vKey = `pv:${typeof v}:${String(v)}`;
1413
+ }
1414
+ else {
1415
+ // Plain object — fall back to reference identity (matches the
1416
+ // memo runtime's behaviour for unhashable objects).
1417
+ let oid = avalIds.get(v);
1418
+ if (oid === undefined) {
1419
+ oid = avalCounter++;
1420
+ avalIds.set(v, oid);
1421
+ }
1422
+ vKey = `ov:${oid}`;
1423
+ }
1424
+ let vid = valueIds.get(vKey);
1425
+ if (vid === undefined) {
1426
+ vid = avalCounter++;
1427
+ valueIds.set(vKey, vid);
1428
+ }
1429
+ avalIds.set(av, vid);
1430
+ return `c${vid}`;
1431
+ };
1432
+ const psContentIds = new WeakMap();
1284
1433
  const psIdOf = (ps) => {
1285
1434
  if (ps === undefined)
1286
1435
  return "ps#default";
1287
- let id = psIds.get(ps);
1288
- if (id === undefined) {
1289
- id = `ps#${psCounter++}`;
1290
- psIds.set(ps, id);
1291
- }
1292
- return id;
1436
+ const cached = psContentIds.get(ps);
1437
+ if (cached !== undefined)
1438
+ return cached;
1439
+ const r = ps.rasterizer;
1440
+ const parts = [
1441
+ avalIdOf(r.topology),
1442
+ avalIdOf(r.cullMode),
1443
+ avalIdOf(r.frontFace),
1444
+ avalIdOf(r.depthBias),
1445
+ ps.depth !== undefined
1446
+ ? `d:${avalIdOf(ps.depth.write)}:${avalIdOf(ps.depth.compare)}:${avalIdOf(ps.depth.clamp)}`
1447
+ : "d:_",
1448
+ ps.stencil !== undefined ? "s:1" : "s:_",
1449
+ avalIdOf(ps.blends),
1450
+ avalIdOf(ps.alphaToCoverage),
1451
+ avalIdOf(ps.blendConstant),
1452
+ ];
1453
+ const key = `ps#${parts.join("|")}`;
1454
+ psContentIds.set(ps, key);
1455
+ return key;
1293
1456
  };
1294
1457
  function resolvePipelineState(ps) {
1295
1458
  if (ps === undefined) {
@@ -1474,14 +1637,14 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1474
1637
  const isAtlasBucket = layout.atlasTextureBindings.size > 0;
1475
1638
  const vsModule = fam.vsModule;
1476
1639
  const fsModule = fam.fsModule;
1477
- const vsEntry = "family_vs_main";
1478
- const fsEntry = "family_fs_main";
1640
+ const vsEntry = fam.vsEntryName;
1641
+ const fsEntry = fam.fsEntryName;
1479
1642
  const { pipelineLayout } = getBgl(layout, isAtlasBucket);
1480
1643
  const pipeline = device.createRenderPipeline({
1481
1644
  label: `heapScene/${bk}/pipeline`,
1482
1645
  layout: pipelineLayout,
1483
1646
  vertex: { module: vsModule, entryPoint: vsEntry, buffers: [] },
1484
- fragment: { module: fsModule, entryPoint: fsEntry, targets: [{ format: colorFormat }] },
1647
+ fragment: { module: fsModule, entryPoint: fsEntry, targets: colorTargets },
1485
1648
  primitive: { topology: ps.topology, cullMode: ps.cullMode, frontFace: ps.frontFace },
1486
1649
  ...(depthFormat !== undefined && ps.depth !== undefined
1487
1650
  ? { depthStencil: { format: depthFormat, depthWriteEnabled: ps.depth.write, depthCompare: ps.depth.compare } }
@@ -1508,7 +1671,7 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1508
1671
  localPosRefs: [], localNorRefs: [],
1509
1672
  localEntries: [], localToDrawId: [],
1510
1673
  localPerDrawAvals: [], localPerDrawRefs: [], localLayoutIds: [],
1511
- drawSlots: [], dirty: new Set(),
1674
+ drawSlots: new Set(), dirty: new Set(),
1512
1675
  drawTableDirtyMin: Infinity, drawTableDirtyMax: 0,
1513
1676
  recordCount: 0, slotToRecord: [], recordToSlot: [],
1514
1677
  totalEmitEstimate: 0,
@@ -1516,6 +1679,7 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1516
1679
  isAtlasBucket,
1517
1680
  localAtlasReleases: [],
1518
1681
  localAtlasTextures: [],
1682
+ localAtlasArrIdx: [],
1519
1683
  };
1520
1684
  {
1521
1685
  const dtBuf = new GrowBuffer(device, `heapScene/${bk}/drawTable`, GPUBufferUsage.STORAGE, 1024);
@@ -1709,7 +1873,21 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1709
1873
  };
1710
1874
  Object.defineProperty(stats, "groups", { get: () => buckets.length, configurable: true });
1711
1875
  // ─── addDraw / removeDraw ─────────────────────────────────────────
1876
+ // Public addDraw wrapper. Establishes a sceneObj.evaluateAlways
1877
+ // scope so external callers (no batched outer eval) still register
1878
+ // sceneObj as an output of any aval the spec touches. Internal call
1879
+ // sites (drainAsetWith, batched initial-population) already run
1880
+ // inside a single outer evaluateAlways and invoke addDrawImpl
1881
+ // directly with their token — collapsing 1000× nested
1882
+ // evaluateAlways into 1× outer.
1712
1883
  function addDraw(spec) {
1884
+ let id = -1;
1885
+ sceneObj.evaluateAlways(AdaptiveToken.top, (tok) => {
1886
+ id = addDrawImpl(spec, tok);
1887
+ });
1888
+ return id;
1889
+ }
1890
+ function addDrawImpl(spec, outerTok) {
1713
1891
  const drawId = nextDrawId++;
1714
1892
  // Family-merge (slice 3c): build the family lazily from this
1715
1893
  // single spec when no batched lazy-build occurred earlier (e.g.
@@ -1727,7 +1905,7 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1727
1905
  : new Set();
1728
1906
  const bucket = findOrCreateBucket(spec.effect, spec.textures, spec.pipelineState);
1729
1907
  const fam = familyFor(spec.effect);
1730
- const effectFields = fam.fieldsForEffect.get(spec.effect);
1908
+ const effectFields = fam.fieldsForEffect.get(spec.effect.id);
1731
1909
  // Indices live in their own INDEX-usage buffer (WebGPU constraint).
1732
1910
  // Aval-keyed: 19K instanced clones of the same mesh share one
1733
1911
  // index allocation + one upload.
@@ -1746,7 +1924,8 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1746
1924
  // emerges from aval identity either way.
1747
1925
  const perDrawAvals = [];
1748
1926
  const perDrawRefs = new Map();
1749
- sceneObj.evaluateAlways(AdaptiveToken.top, (tok) => {
1927
+ {
1928
+ const tok = outerTok;
1750
1929
  for (const f of bucket.layout.drawHeaderFields) {
1751
1930
  // Atlas-variant texture bindings carry inline values rather than
1752
1931
  // pool refs; packAtlasTextureFields fills them after this loop.
@@ -1811,14 +1990,14 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1811
1990
  perDrawRefs.set(f.name, ref);
1812
1991
  perDrawAvals.push(av);
1813
1992
  }
1814
- });
1993
+ }
1815
1994
  bucket.localPosRefs[localSlot] = perDrawRefs.get("Positions");
1816
1995
  bucket.localNorRefs[localSlot] = perDrawRefs.get("Normals");
1817
1996
  bucket.localEntries[localSlot] = {
1818
1997
  indexCount: idxAlloc.count, firstIndex: idxAlloc.firstIndex, instanceCount,
1819
1998
  };
1820
1999
  bucket.localToDrawId[localSlot] = drawId;
1821
- bucket.drawSlots.push(localSlot);
2000
+ bucket.drawSlots.add(localSlot);
1822
2001
  bucket.localPerDrawAvals[localSlot] = perDrawAvals;
1823
2002
  bucket.localPerDrawRefs[localSlot] = perDrawRefs;
1824
2003
  const layoutId = fam.schema.layoutIdOf.get(spec.effect);
@@ -1833,15 +2012,14 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1833
2012
  const sourceAval = spec.textures.sourceAval;
1834
2013
  const repackFn = spec.textures.repack;
1835
2014
  if (sourceAval !== undefined && repackFn !== undefined) {
1836
- sceneObj.evaluateAlways(AdaptiveToken.top, (tok) => {
1837
- // touch — registers sceneObj as an output of sourceAval.
1838
- sourceAval.getValue(tok);
1839
- });
2015
+ // touch — registers sceneObj as an output of sourceAval.
2016
+ sourceAval.getValue(outerTok);
1840
2017
  let arr = atlasAvalRefs.get(sourceAval);
1841
2018
  if (arr === undefined) {
1842
2019
  arr = [];
1843
2020
  atlasAvalRefs.set(sourceAval, arr);
1844
2021
  }
2022
+ bucket.localAtlasArrIdx[localSlot] = arr.length;
1845
2023
  arr.push({ bucket, localSlot, repack: repackFn, sampler: spec.textures.sampler });
1846
2024
  }
1847
2025
  }
@@ -1991,12 +2169,18 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
1991
2169
  const atlasTex = bucket.localAtlasTextures[localSlot];
1992
2170
  if (atlasTex !== undefined) {
1993
2171
  const sourceAval = atlasTex.sourceAval;
1994
- if (sourceAval !== undefined) {
2172
+ const i = bucket.localAtlasArrIdx[localSlot];
2173
+ if (sourceAval !== undefined && i !== undefined) {
1995
2174
  const arr = atlasAvalRefs.get(sourceAval);
1996
2175
  if (arr !== undefined) {
1997
- const i = arr.findIndex(r => r.bucket === bucket && r.localSlot === localSlot);
1998
- if (i >= 0)
1999
- arr.splice(i, 1);
2176
+ // Swap-pop: O(1) regardless of array length.
2177
+ const last = arr.length - 1;
2178
+ if (i !== last) {
2179
+ const moved = arr[last];
2180
+ arr[i] = moved;
2181
+ moved.bucket.localAtlasArrIdx[moved.localSlot] = i;
2182
+ }
2183
+ arr.pop();
2000
2184
  if (arr.length === 0) {
2001
2185
  atlasAvalRefs.delete(sourceAval);
2002
2186
  atlasAvalDirty.delete(sourceAval);
@@ -2006,6 +2190,7 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
2006
2190
  }
2007
2191
  bucket.localAtlasReleases[localSlot] = undefined;
2008
2192
  bucket.localAtlasTextures[localSlot] = undefined;
2193
+ bucket.localAtlasArrIdx[localSlot] = undefined;
2009
2194
  bucket.localPerDrawAvals[localSlot] = undefined;
2010
2195
  bucket.localPerDrawRefs[localSlot] = undefined;
2011
2196
  bucket.localLayoutIds[localSlot] = undefined;
@@ -2013,9 +2198,7 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
2013
2198
  bucket.localNorRefs[localSlot] = undefined;
2014
2199
  bucket.localEntries[localSlot] = undefined;
2015
2200
  bucket.localToDrawId[localSlot] = undefined;
2016
- const idx = bucket.drawSlots.indexOf(localSlot);
2017
- if (idx >= 0)
2018
- bucket.drawSlots.splice(idx, 1);
2201
+ bucket.drawSlots.delete(localSlot);
2019
2202
  bucket.dirty.delete(localSlot);
2020
2203
  bucket.drawHeap.release(localSlot);
2021
2204
  drawIdToBucket[drawId] = undefined;
@@ -2033,7 +2216,11 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
2033
2216
  const delta = asetReader.getChanges(tok);
2034
2217
  delta.iter((op) => {
2035
2218
  if (op.count > 0) {
2036
- const id = addDraw(op.value);
2219
+ // Re-use the outer evaluateAlways scope's token (caller is
2220
+ // already sceneObj) — collapses N nested evaluateAlways into
2221
+ // one for a bulk add. Saves a per-RO setUnsafeEvaluationDepth
2222
+ // + outputs.add(sceneObj) round trip.
2223
+ const id = addDrawImpl(op.value, tok);
2037
2224
  specToDrawId.set(op.value, id);
2038
2225
  }
2039
2226
  else {
@@ -2053,8 +2240,10 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
2053
2240
  const arr = initialDraws;
2054
2241
  if (arr.length > 0)
2055
2242
  buildFamilyFromSpecs(arr);
2056
- for (const d of arr)
2057
- addDraw(d);
2243
+ sceneObj.evaluateAlways(AdaptiveToken.top, (tok) => {
2244
+ for (const d of arr)
2245
+ addDrawImpl(d, tok);
2246
+ });
2058
2247
  }
2059
2248
  else {
2060
2249
  asetReader = initialDraws.getReader();
@@ -2077,7 +2266,7 @@ export function buildHeapScene(device, sig, initialDraws, opts = {}) {
2077
2266
  buildFamilyFromSpecs(adds);
2078
2267
  for (const op of remaining) {
2079
2268
  if (op.count > 0) {
2080
- const id = addDraw(op.value);
2269
+ const id = addDrawImpl(op.value, tok);
2081
2270
  specToDrawId.set(op.value, id);
2082
2271
  }
2083
2272
  else {