ugly-game 0.15.4 → 0.15.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.
@@ -232,6 +232,35 @@ export declare class GpuShadowRenderer {
232
232
  /** Dynamic scene: pre-allocate for `cap` instances; call updateInstances() each frame. */
233
233
  setCapacity(cap: number): void;
234
234
  updateInstances(instances: CubeInstance[]): void;
235
+ /**
236
+ * Rewrite ONE RANGE of the instance buffer and leave the rest of it alone.
237
+ *
238
+ * `updateInstances` repacks and re-uploads EVERYTHING, so a caller whose scene is mostly static pays for
239
+ * all of it to move one cube. In a voxel game that is the whole cost: measured in all-mine-sky, a 63 000
240
+ * instance scene costs ~7 ms of main thread per frame to repack and write, and the only thing changing on
241
+ * a typical frame is a first-person viewmodel of a few dozen cubes. The pack is linear in instance count
242
+ * — ~0.1 µs each — so the fix is to not do it for instances that did not change.
243
+ *
244
+ * The contract is deliberately dumb: this writes `instances` starting at slot `first` and nothing else.
245
+ * It does not move the draw count (use `setInstanceCount`), and it does not know what your segments are.
246
+ * The caller owns the layout, because only the caller knows which of its sources changed.
247
+ *
248
+ * // ground 0..47999, sky 48000..62999, dynamic 63000..
249
+ * if (skyMoved) r.updateInstanceRange(sky, 48000)
250
+ * r.updateInstanceRange(dynamic, 63000)
251
+ * r.setInstanceCount(63000 + dynamic.length)
252
+ *
253
+ * ⚠️ SLOTS ARE NOT REORDERED. A range write leaves whatever was in the untouched slots exactly as it was,
254
+ * so a layout whose segment offsets shift when one segment resizes must re-upload from the first moved
255
+ * slot onward. Reserving fixed capacity per segment avoids that; packing tight does not.
256
+ *
257
+ * Out-of-range and empty writes are no-ops rather than errors: a caller assembling a frame should not
258
+ * have to special-case "this segment happens to be empty right now".
259
+ */
260
+ updateInstanceRange(instances: CubeInstance[], first: number): void;
261
+ /** How many instances the cull + draw passes consider, whatever is in the buffer past that. Pairs with
262
+ * `updateInstanceRange`: shrinking the dynamic tail is a count change, not an upload. */
263
+ setInstanceCount(n: number): void;
235
264
  /** Translucent overlay cubes drawn over the opaque scene; `emissive` carries per-cube alpha (0..1). The buffer
236
265
  * GROWS to fit (no hard cap). Pass `eye` to SORT the cubes back-to-front (farthest first) so overlapping
237
266
  * translucent cubes blend in the correct order — required for anything denser than the place ghost. By default
package/dist/gpuShadow.js CHANGED
@@ -1097,9 +1097,11 @@ export class GpuShadowRenderer {
1097
1097
  this.lightCullBG = this.lightArgs.map((a, i) => this.cullBG(a, this.lightVis[i], this.lightCull[i]));
1098
1098
  }
1099
1099
  scratch = new Float32Array(0);
1100
- fill(scratch, instances, n) {
1100
+ fill(scratch, instances, n,
1101
+ /** Destination slot of `instances[0]`. Non-zero only for `updateInstanceRange`. */
1102
+ at = 0) {
1101
1103
  for (let i = 0; i < n; i++) {
1102
- const c = instances[i], o = i * INST_F, r = c.rot;
1104
+ const c = instances[i], o = (at + i) * INST_F, r = c.rot;
1103
1105
  scratch[o] = c.x;
1104
1106
  scratch[o + 1] = c.y;
1105
1107
  scratch[o + 2] = c.z;
@@ -1257,6 +1259,50 @@ export class GpuShadowRenderer {
1257
1259
  this.device.queue.writeBuffer(this.instBuf, 0, this.scratch, 0, n * INST_F);
1258
1260
  this.count = n;
1259
1261
  }
1262
+ /**
1263
+ * Rewrite ONE RANGE of the instance buffer and leave the rest of it alone.
1264
+ *
1265
+ * `updateInstances` repacks and re-uploads EVERYTHING, so a caller whose scene is mostly static pays for
1266
+ * all of it to move one cube. In a voxel game that is the whole cost: measured in all-mine-sky, a 63 000
1267
+ * instance scene costs ~7 ms of main thread per frame to repack and write, and the only thing changing on
1268
+ * a typical frame is a first-person viewmodel of a few dozen cubes. The pack is linear in instance count
1269
+ * — ~0.1 µs each — so the fix is to not do it for instances that did not change.
1270
+ *
1271
+ * The contract is deliberately dumb: this writes `instances` starting at slot `first` and nothing else.
1272
+ * It does not move the draw count (use `setInstanceCount`), and it does not know what your segments are.
1273
+ * The caller owns the layout, because only the caller knows which of its sources changed.
1274
+ *
1275
+ * // ground 0..47999, sky 48000..62999, dynamic 63000..
1276
+ * if (skyMoved) r.updateInstanceRange(sky, 48000)
1277
+ * r.updateInstanceRange(dynamic, 63000)
1278
+ * r.setInstanceCount(63000 + dynamic.length)
1279
+ *
1280
+ * ⚠️ SLOTS ARE NOT REORDERED. A range write leaves whatever was in the untouched slots exactly as it was,
1281
+ * so a layout whose segment offsets shift when one segment resizes must re-upload from the first moved
1282
+ * slot onward. Reserving fixed capacity per segment avoids that; packing tight does not.
1283
+ *
1284
+ * Out-of-range and empty writes are no-ops rather than errors: a caller assembling a frame should not
1285
+ * have to special-case "this segment happens to be empty right now".
1286
+ */
1287
+ updateInstanceRange(instances, first) {
1288
+ const capN = (this.scratch.length / INST_F) | 0;
1289
+ if (first < 0 || first >= capN)
1290
+ return;
1291
+ const n = Math.min(instances.length, capN - first);
1292
+ if (n <= 0)
1293
+ return;
1294
+ this.fill(this.scratch, instances, n, first);
1295
+ this.device.queue.writeBuffer(this.instBuf, first * INST_F * 4, // byte offset — INST_F floats per instance, 4 bytes each
1296
+ this.scratch, first * INST_F, // element offset into scratch
1297
+ n * INST_F);
1298
+ if (first + n > this.count)
1299
+ this.count = first + n;
1300
+ }
1301
+ /** How many instances the cull + draw passes consider, whatever is in the buffer past that. Pairs with
1302
+ * `updateInstanceRange`: shrinking the dynamic tail is a count change, not an upload. */
1303
+ setInstanceCount(n) {
1304
+ this.count = Math.max(0, Math.min(n, (this.scratch.length / INST_F) | 0));
1305
+ }
1260
1306
  /** Translucent overlay cubes drawn over the opaque scene; `emissive` carries per-cube alpha (0..1). The buffer
1261
1307
  * GROWS to fit (no hard cap). Pass `eye` to SORT the cubes back-to-front (farthest first) so overlapping
1262
1308
  * translucent cubes blend in the correct order — required for anything denser than the place ghost. By default
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.4",
3
+ "version": "0.15.5",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {