ugly-game 0.15.25 → 0.15.26

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.
@@ -183,6 +183,13 @@ export declare class GpuPropsRenderer {
183
183
  private cullPipe;
184
184
  private cullU;
185
185
  private cullArgs;
186
+ /**
187
+ * The shared geometry arena: every model's vertices and indices, concatenated.
188
+ *
189
+ * One binding for the whole pass instead of five per model — see setModels for why that matters to
190
+ * the LOD system rather than merely to the encoder.
191
+ */
192
+ private geo;
186
193
  private cullVis;
187
194
  private cullBGs;
188
195
  /** Survivor lists are per (model, cascade) but packed into one buffer per model at this stride. */
@@ -394,6 +394,13 @@ export class GpuPropsRenderer {
394
394
  cullPipe = null;
395
395
  cullU = []; // planes + info, one per (model, cascade)
396
396
  cullArgs = []; // drawIndexedIndirect args
397
+ /**
398
+ * The shared geometry arena: every model's vertices and indices, concatenated.
399
+ *
400
+ * One binding for the whole pass instead of five per model — see setModels for why that matters to
401
+ * the LOD system rather than merely to the encoder.
402
+ */
403
+ geo = null;
397
404
  cullVis = []; // survivor list — per MODEL, see cullPass
398
405
  cullBGs = [];
399
406
  /** Survivor lists are per (model, cascade) but packed into one buffer per model at this stride. */
@@ -667,12 +674,8 @@ export class GpuPropsRenderer {
667
674
  setModels(meshes) {
668
675
  const dev = this.device, U = GPUBufferUsage;
669
676
  for (const p of this.models) {
670
- p.posB.destroy();
671
- p.nrmB.destroy();
672
- p.colB.destroy();
673
- p.uvB.destroy();
674
- p.idxB.destroy();
675
- // Only what this renderer allocated. See Packed.ownsTex — freeing a caller's GPUTexture makes
677
+ // Geometry is no longer per model — the shared arena is freed once, below, after the new one
678
+ // has been sized. Only what this renderer allocated. See Packed.ownsTex — freeing a caller's GPUTexture makes
676
679
  // a repeated setModels bind dead resources.
677
680
  if (p.ownsTex)
678
681
  p.tex?.destroy();
@@ -706,8 +709,43 @@ export class GpuPropsRenderer {
706
709
  dev.queue.writeTexture({ texture: t }, new Uint8Array([255, 255, 255, 255]), {}, [1, 1]);
707
710
  return t;
708
711
  })();
712
+ // ── ONE GEOMETRY ARENA FOR EVERY MODEL ────────────────────────────────────────────────────────
713
+ //
714
+ // Every model's vertices land in four shared buffers and every model's indices in one, so a draw
715
+ // pass binds geometry ONCE and addresses a model with `firstIndex` / `baseVertex` instead of
716
+ // rebinding. Per-model that is 8 encoder calls (4 setVertexBuffer + setIndexBuffer + 2
717
+ // setBindGroup + the draw) down to 3.
718
+ //
719
+ // ⚠️ THE POINT IS NOT THE ENCODER CALLS, IT IS WHAT THEY WERE COSTING THE LOD SYSTEM. A rung is a
720
+ // separate model here, so every extra rung in flight cost one full rebind per species — measured
721
+ // in all-mine-sky at ~28 draws per rung, which is why the cheapest configuration was the one that
722
+ // collapsed every plant onto a SINGLE rung and the LOD selector was penalised for using its
723
+ // ladder. Geometry rebinding was the thing making granularity expensive; it is now free, and a
724
+ // selector can spend rungs on quality without buying draw-call cost with them.
725
+ //
726
+ // Indices stay MODEL-LOCAL and are offset by `baseVertex` at draw time rather than being rewritten
727
+ // — rewriting them would work equally well for the camera pass and would silently break the
728
+ // occluder pass, whose index buffer is fed to `drawIndexedIndirect` with args the GPU writes.
729
+ const totalVerts = meshes.reduce((n, m) => n + m.positions.length / 3, 0);
730
+ const totalIdx = meshes.reduce((n, m) => n + m.indices.length, 0);
731
+ const allPos = new Float32Array(Math.max(3, totalVerts * 3));
732
+ const allNrm = new Float32Array(allPos.length);
733
+ const allCol = new Float32Array(allPos.length);
734
+ const allUv = new Float32Array(Math.max(2, totalVerts * 2));
735
+ const allIdx = new Uint32Array(Math.max(1, totalIdx));
736
+ let vCursor = 0, iCursor = 0;
709
737
  this.models = meshes.map((m) => {
710
738
  const verts = m.positions.length / 3;
739
+ const baseVertex = vCursor, firstIndex = iCursor;
740
+ allPos.set(m.positions, baseVertex * 3);
741
+ allNrm.set(m.normals, baseVertex * 3);
742
+ allCol.set(m.colors, baseVertex * 3);
743
+ // zero-filled when absent: the shader still reads location 3, and white x anything is anything
744
+ if (m.uvs)
745
+ allUv.set(m.uvs, baseVertex * 2);
746
+ allIdx.set(m.indices, firstIndex);
747
+ vCursor += verts;
748
+ iCursor += m.indices.length;
711
749
  // A GPUTexture is already uploaded and already mipped (the KTX2 path) — bind it directly.
712
750
  // Only an ImageBitmap needs uploadAlbedo's hand-rolled half-scale mip chain.
713
751
  const ownsTex = !!m.albedo && !(m.albedo instanceof GPUTexture);
@@ -728,12 +766,8 @@ export class GpuPropsRenderer {
728
766
  cutout,
729
767
  matBuf,
730
768
  ownsTex,
731
- posB: mk(m.positions, U.VERTEX | U.COPY_DST),
732
- nrmB: mk(m.normals, U.VERTEX | U.COPY_DST),
733
- colB: mk(m.colors, U.VERTEX | U.COPY_DST),
734
- // zero-filled when absent: the shader still reads location 3, and white x anything is anything
735
- uvB: mk(m.uvs ?? new Float32Array(verts * 2), U.VERTEX | U.COPY_DST),
736
- idxB: mk(m.indices, U.INDEX | U.COPY_DST),
769
+ firstIndex,
770
+ baseVertex,
737
771
  idxCount: m.indices.length,
738
772
  extent: measureExtent(m.positions),
739
773
  tex,
@@ -747,6 +781,19 @@ export class GpuPropsRenderer {
747
781
  }),
748
782
  };
749
783
  });
784
+ // Built AFTER the map, because the map is what fills the arenas.
785
+ this.geo?.destroy();
786
+ this.geo = {
787
+ pos: mk(allPos, U.VERTEX | U.COPY_DST),
788
+ nrm: mk(allNrm, U.VERTEX | U.COPY_DST),
789
+ col: mk(allCol, U.VERTEX | U.COPY_DST),
790
+ uv: mk(allUv, U.VERTEX | U.COPY_DST),
791
+ idx: mk(allIdx, U.INDEX | U.COPY_DST),
792
+ destroy() {
793
+ for (const b of [this.pos, this.nrm, this.col, this.uv, this.idx])
794
+ b.destroy();
795
+ },
796
+ };
750
797
  this.caps = meshes.map(() => 0);
751
798
  this.instBufs = meshes.map(() => null);
752
799
  this.bgs = meshes.map(() => null);
@@ -916,6 +963,11 @@ export class GpuPropsRenderer {
916
963
  this.cullStats.submitted += this.counts[m] * nc;
917
964
  reset[0] = this.models[m].idxCount;
918
965
  reset[1] = this.cullOn ? 0 : this.counts[m]; // cull off ⇒ draw them all, no dispatch
966
+ // ⚠️ THE ARENA OFFSETS RIDE IN THE INDIRECT ARGS. The occluder pass binds one shared index
967
+ // buffer, so without these every model would draw model 0's triangles — every shadow in the
968
+ // world cast by the same silhouette, which is a picture that looks plausible at a glance.
969
+ reset[2] = this.models[m].firstIndex;
970
+ reset[3] = this.models[m].baseVertex;
919
971
  for (let c = 0; c < nc; c++)
920
972
  this.device.queue.writeBuffer(this.cullArgs[m][c], 0, reset);
921
973
  }
@@ -989,6 +1041,10 @@ export class GpuPropsRenderer {
989
1041
  ], 16);
990
1042
  this.device.queue.writeBuffer(this.lightBufs[c], 0, ob);
991
1043
  pass.setPipeline(this.occPipe);
1044
+ if (!this.geo)
1045
+ return;
1046
+ pass.setVertexBuffer(0, this.geo.pos);
1047
+ pass.setIndexBuffer(this.geo.idx, 'uint32');
992
1048
  for (let m = 0; m < this.models.length; m++) {
993
1049
  const mdl = this.models[m];
994
1050
  // A model whose cull resources have not been allocated yet (its bucket was empty when the
@@ -1014,8 +1070,6 @@ export class GpuPropsRenderer {
1014
1070
  if (!args)
1015
1071
  continue;
1016
1072
  pass.setBindGroup(0, bg);
1017
- pass.setVertexBuffer(0, mdl.posB);
1018
- pass.setIndexBuffer(mdl.idxB, 'uint32');
1019
1073
  // ⚠️ INDIRECT, so the count comes from the GPU. Reading it back to skip empty models would cost
1020
1074
  // a stall per model per cascade to save a draw whose instanceCount is already 0 — and the draw
1021
1075
  // count is the number this whole exercise is not allowed to raise.
@@ -1072,18 +1126,22 @@ export class GpuPropsRenderer {
1072
1126
  },
1073
1127
  });
1074
1128
  rp.setBindGroup(1, this.shadowBG);
1129
+ // The arena, bound once for every model in both loops below.
1130
+ if (this.geo) {
1131
+ rp.setVertexBuffer(0, this.geo.pos);
1132
+ rp.setVertexBuffer(1, this.geo.nrm);
1133
+ rp.setVertexBuffer(2, this.geo.col);
1134
+ rp.setVertexBuffer(3, this.geo.uv);
1135
+ rp.setIndexBuffer(this.geo.idx, 'uint32');
1136
+ }
1075
1137
  const drawModel = (m) => {
1076
1138
  const bg = this.bgs[m], mdl = this.models[m];
1077
1139
  if (!bg || this.counts[m] === 0)
1078
1140
  return;
1079
1141
  rp.setBindGroup(0, bg);
1080
- rp.setVertexBuffer(0, mdl.posB);
1081
- rp.setVertexBuffer(1, mdl.nrmB);
1082
- rp.setVertexBuffer(2, mdl.colB);
1083
- rp.setVertexBuffer(3, mdl.uvB);
1084
1142
  rp.setBindGroup(2, mdl.matBG);
1085
- rp.setIndexBuffer(mdl.idxB, 'uint32');
1086
- rp.drawIndexed(mdl.idxCount, this.counts[m]);
1143
+ // Geometry is bound ONCE for the whole pass; a model is a range inside it.
1144
+ rp.drawIndexed(mdl.idxCount, this.counts[m], mdl.firstIndex, mdl.baseVertex);
1087
1145
  };
1088
1146
  // OPAQUE FIRST, then the blended models — the whole point of the second pass. A translucent
1089
1147
  // crystal drawn before an opaque rock composites against whatever the sky left in the target and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.25",
3
+ "version": "0.15.26",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {