ugly-game 0.15.30 → 0.15.31

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.
@@ -638,6 +638,9 @@ export declare class GpuShadowRenderer {
638
638
  private giGridBuf;
639
639
  private giProbeBuf;
640
640
  private count;
641
+ /** Ranges `clearInstanceRange` has been asked to blank, flushed into the next frame's encoder. */
642
+ private pendingClears;
643
+ private clearedTotal;
641
644
  private cam;
642
645
  /**
643
646
  * Scratch for the per-cascade copy of `cam`, with that cascade's matrix parked at `lightVP[0]`.
@@ -899,6 +902,31 @@ export declare class GpuShadowRenderer {
899
902
  */
900
903
  updateInstanceRaw(src: Float32Array<ArrayBuffer>, first: number): void;
901
904
  updateInstanceRange(instances: CubeInstance[], first: number): void;
905
+ /**
906
+ * BLANK A RANGE OF SLOTS ON THE GPU — the free() half of managing this buffer as an arena.
907
+ *
908
+ * A caller that allocates and releases sub-ranges (one chunk of a voxel world per allocation, say)
909
+ * has to blank what it releases: the cull walks `[0, count)` and does not know which slots the
910
+ * caller still considers live, so a stale range keeps drawing. Doing that with `updateInstanceRange`
911
+ * means shipping zeros over the bus — measured on all-mine-sky's chunk ladder, blanking was 47 % of
912
+ * ALL instance-buffer write traffic under a churning LOD ladder, and none of it was geometry.
913
+ *
914
+ * `clearBuffer` is a GPU-local fill: no staging, no upload bandwidth, no per-instance JavaScript.
915
+ * The clears queue here and flush at the top of the next `frame()`, because a clear needs a command
916
+ * encoder and callers free from wherever their streaming happens to land.
917
+ *
918
+ * ⚠️ IT WRITES ZEROS, AND ZERO SCALE IS WHAT MAKES THAT SAFE. `clearBuffer` cannot write a sentinel
919
+ * position, so a blanked instance sits at the ORIGIN — inside most frustums. The cull rejects
920
+ * nonpositive extent for exactly this reason; without that test this method would be a way to fill
921
+ * the visible list with degenerate cubes rather than a way to free space.
922
+ *
923
+ * ⚠️ IT DOES NOT MOVE `count`. Freeing the tail should also lower the draw count, but only the
924
+ * caller knows whether the tail is now free — call `setInstanceCount` as well when it is.
925
+ */
926
+ clearInstanceRange(first: number, n: number): void;
927
+ /** Instances blanked by `clearInstanceRange` since the counter was last read. The fragmentation and
928
+ * write-cost readout a caller managing this buffer as an arena needs, and cheap enough to leave on. */
929
+ get clearedInstances(): number;
902
930
  /** How many instances the cull + draw passes consider, whatever is in the buffer past that. Pairs with
903
931
  * `updateInstanceRange`: shrinking the dynamic tail is a count change, not an upload. */
904
932
  setInstanceCount(n: number): void;
package/dist/gpuShadow.js CHANGED
@@ -2197,6 +2197,15 @@ fn cull(@builtin(global_invocation_id) gid: vec3<u32>) {
2197
2197
  let i = gid.x;
2198
2198
  if (i >= u32(cc.cnt.x)) { return; }
2199
2199
  let inst = insts[i];
2200
+ // ── A FREED SLOT IS A ZEROED SLOT ────────────────────────────────────────────────────────────────
2201
+ //
2202
+ // The instance buffer is an ARENA when the caller manages it as one: clearInstanceRange blanks a
2203
+ // freed range on the GPU, and clearBuffer can only write zeros. A zero-scale box is a POINT, and a
2204
+ // point at the origin is inside most frustums — so without this test a freed slot survives the cull,
2205
+ // takes a visible-list entry and an indirect instance, and rasterises a degenerate cube every frame
2206
+ // for as long as it stays free. Rejecting nonpositive extent is also just correct: a box with no
2207
+ // width draws nothing, so nothing that was ever visible is lost here.
2208
+ if (inst.scl.x <= 0.0 || inst.scl.y <= 0.0 || inst.scl.z <= 0.0) { return; }
2200
2209
  // The cull pass indexes the instance buffer DIRECTLY, so its instance index is already global.
2201
2210
  let bd = bodyOf(i);
2202
2211
  let c = bd.pos.xyz + qr(bd.rot, inst.pos.xyz);
@@ -2944,6 +2953,9 @@ export class GpuShadowRenderer {
2944
2953
  giGridBuf;
2945
2954
  giProbeBuf;
2946
2955
  count = 0;
2956
+ /** Ranges `clearInstanceRange` has been asked to blank, flushed into the next frame's encoder. */
2957
+ pendingClears = [];
2958
+ clearedTotal = 0;
2947
2959
  // camVP(16) + lightVP[MAX_CASCADES](16 each) + sun + sunCol + tint + atlas + up + csm + eye
2948
2960
  cam = new Float32Array(16 + MAX_CASCADES * 16 + 7 * 4);
2949
2961
  /**
@@ -3739,6 +3751,9 @@ export class GpuShadowRenderer {
3739
3751
  usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
3740
3752
  });
3741
3753
  this.count = 0;
3754
+ // ⚠️ THE OLD BUFFER IS GONE, SO ITS PENDING CLEARS ARE MEANINGLESS — and worse than meaningless:
3755
+ // they name offsets in a buffer that no longer exists, and the fresh one is already zeroed.
3756
+ this.pendingClears.length = 0;
3742
3757
  this.makeBindGroups();
3743
3758
  }
3744
3759
  updateInstances(instances) {
@@ -3813,6 +3828,48 @@ export class GpuShadowRenderer {
3813
3828
  if (first + n > this.count)
3814
3829
  this.count = first + n;
3815
3830
  }
3831
+ /**
3832
+ * BLANK A RANGE OF SLOTS ON THE GPU — the free() half of managing this buffer as an arena.
3833
+ *
3834
+ * A caller that allocates and releases sub-ranges (one chunk of a voxel world per allocation, say)
3835
+ * has to blank what it releases: the cull walks `[0, count)` and does not know which slots the
3836
+ * caller still considers live, so a stale range keeps drawing. Doing that with `updateInstanceRange`
3837
+ * means shipping zeros over the bus — measured on all-mine-sky's chunk ladder, blanking was 47 % of
3838
+ * ALL instance-buffer write traffic under a churning LOD ladder, and none of it was geometry.
3839
+ *
3840
+ * `clearBuffer` is a GPU-local fill: no staging, no upload bandwidth, no per-instance JavaScript.
3841
+ * The clears queue here and flush at the top of the next `frame()`, because a clear needs a command
3842
+ * encoder and callers free from wherever their streaming happens to land.
3843
+ *
3844
+ * ⚠️ IT WRITES ZEROS, AND ZERO SCALE IS WHAT MAKES THAT SAFE. `clearBuffer` cannot write a sentinel
3845
+ * position, so a blanked instance sits at the ORIGIN — inside most frustums. The cull rejects
3846
+ * nonpositive extent for exactly this reason; without that test this method would be a way to fill
3847
+ * the visible list with degenerate cubes rather than a way to free space.
3848
+ *
3849
+ * ⚠️ IT DOES NOT MOVE `count`. Freeing the tail should also lower the draw count, but only the
3850
+ * caller knows whether the tail is now free — call `setInstanceCount` as well when it is.
3851
+ */
3852
+ clearInstanceRange(first, n) {
3853
+ const capN = (this.scratch.length / INST_F) | 0;
3854
+ if (first < 0 || first >= capN)
3855
+ return;
3856
+ const count = Math.min(n, capN - first);
3857
+ if (count <= 0)
3858
+ return;
3859
+ // Merge into the previous pending clear when they touch — a coarsening pass frees many adjacent
3860
+ // entries in one go, and one encoder call per entry is pure overhead for the same bytes.
3861
+ const n0 = this.pendingClears.length;
3862
+ const last = n0 > 0 ? this.pendingClears[n0 - 1] : undefined;
3863
+ if (last !== undefined && last.first + last.n === first)
3864
+ last.n += count;
3865
+ else
3866
+ this.pendingClears.push({ first, n: count });
3867
+ }
3868
+ /** Instances blanked by `clearInstanceRange` since the counter was last read. The fragmentation and
3869
+ * write-cost readout a caller managing this buffer as an arena needs, and cheap enough to leave on. */
3870
+ get clearedInstances() {
3871
+ return this.clearedTotal;
3872
+ }
3816
3873
  /** How many instances the cull + draw passes consider, whatever is in the buffer past that. Pairs with
3817
3874
  * `updateInstanceRange`: shrinking the dynamic tail is a count change, not an upload. */
3818
3875
  setInstanceCount(n) {
@@ -4034,6 +4091,17 @@ export class GpuShadowRenderer {
4034
4091
  * occluder that wants to cull per cascade has to be handed every cascade matrix up front, here.
4035
4092
  */
4036
4093
  extraShadowCull, up = [0, 1, 0], linearOut = false) {
4094
+ // ⚠️ FREES FIRST, BEFORE ANYTHING READS THE BUFFER THIS FRAME. `clearInstanceRange` queues rather
4095
+ // than encodes, because a caller frees where its streaming lands and has no encoder there; this is
4096
+ // the first point in the frame that has one, and it is ahead of the cull that would otherwise read
4097
+ // the stale slots.
4098
+ if (this.pendingClears.length > 0) {
4099
+ for (const c of this.pendingClears) {
4100
+ enc.clearBuffer(this.instBuf, c.first * INST_F * 4, c.n * INST_F * 4);
4101
+ this.clearedTotal += c.n;
4102
+ }
4103
+ this.pendingClears.length = 0;
4104
+ }
4037
4105
  // One matrix or many — a single Float32Array is the 1-cascade case and stays byte-identical.
4038
4106
  const mats = Array.isArray(lightVP) ? lightVP : [lightVP];
4039
4107
  const nc = Math.max(1, Math.min(MAX_CASCADES, mats.length));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.30",
3
+ "version": "0.15.31",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {