ugly-game 0.15.3 → 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.
@@ -899,6 +899,17 @@ export declare class GpuCrowdRenderer {
899
899
  * character's current rung each frame without branching.
900
900
  */
901
901
  enableFaceRig(slot: number, lod: number, dstSlot: number): void;
902
+ /**
903
+ * Copy `slot` (positions AND normals) into `dstSlot` — the seed the face pass then rewrites
904
+ * face vertices on top of. Called by `enableFaceRig`, and by `morph` whenever the slot is
905
+ * rigged (see the re-seed note there).
906
+ *
907
+ * ⚠️ VIA A STAGING BUFFER, because WebGPU REFUSES copyBufferToBuffer when the source and the
908
+ * destination are the SAME buffer — even for ranges that provably do not overlap. ("Source and
909
+ * destination are the same buffer.") Both slots live in the one rest pool, so the copy has to
910
+ * hop out and back.
911
+ */
912
+ private seedFaceSlot;
902
913
  /** Stop dispatching for this slot — it costs exactly what it cost before the rig existed. */
903
914
  disableFaceRig(slot: number): void;
904
915
  /**
@@ -2082,6 +2082,18 @@ export class GpuCrowdRenderer {
2082
2082
  cp.dispatchWorkgroups(Math.ceil(l.plan.vertCount / 64));
2083
2083
  cp.end();
2084
2084
  dev.queue.submit([enc.finish()]);
2085
+ // ⚠️ A RIGGED FACE IS A SECOND COPY OF THIS SLOT, and it does not follow the morph on its
2086
+ // own. The face pass rewrites only face vertices per frame; every other vertex of the
2087
+ // animated slot keeps whatever the seed copy held, so a re-morph that skips the re-seed
2088
+ // draws the OLD body with the NEW face — stretched between the two morphs across the scalp,
2089
+ // a second head floating above the first. Any slider that moves the skull shows it (age is
2090
+ // simply the biggest); no caller remembered, because nothing in `morph`'s signature says a
2091
+ // copy exists. Owned HERE, not by the caller, so the rule cannot be forgotten a second time.
2092
+ // The rung must match: a LOD change re-rigs through `enableFaceRig`, and a copy sized for
2093
+ // the new rung aimed at the old rung's slot would overrun its stride.
2094
+ const rig = this.rigged.get(slot);
2095
+ if (rig?.lod === lod)
2096
+ this.seedFaceSlot(slot, lod, rig.dst);
2085
2097
  }
2086
2098
  // ── the per-frame face rig ───────────────────────────────────────────────────────────────────
2087
2099
  /** Grow the weight and face-table buffers to hold `rows`, and rebuild the bind groups on them. */
@@ -2169,11 +2181,24 @@ export class GpuCrowdRenderer {
2169
2181
  this.rigged.set(slot, { row, lod, dst: dstSlot });
2170
2182
  if ((row + 1) * FACE_ROW_FLOATS > this.wCpu.length)
2171
2183
  this.allocFaceBuffers(growCapacity(row + 1, this.wCpu.length / FACE_ROW_FLOATS));
2184
+ this.seedFaceSlot(slot, lod, dstSlot);
2185
+ }
2186
+ /**
2187
+ * Copy `slot` (positions AND normals) into `dstSlot` — the seed the face pass then rewrites
2188
+ * face vertices on top of. Called by `enableFaceRig`, and by `morph` whenever the slot is
2189
+ * rigged (see the re-seed note there).
2190
+ *
2191
+ * ⚠️ VIA A STAGING BUFFER, because WebGPU REFUSES copyBufferToBuffer when the source and the
2192
+ * destination are the SAME buffer — even for ranges that provably do not overlap. ("Source and
2193
+ * destination are the same buffer.") Both slots live in the one rest pool, so the copy has to
2194
+ * hop out and back.
2195
+ */
2196
+ seedFaceSlot(slot, lod, dstSlot) {
2197
+ const l = maybe(this.lodGpu, lod);
2198
+ const pools = this.pools;
2199
+ if (!l || !pools)
2200
+ return;
2172
2201
  const bytes = l.plan.slotVerts * 16; // the pools are arrays of vec4
2173
- // ⚠️ VIA A STAGING BUFFER, because WebGPU REFUSES copyBufferToBuffer when the source and the
2174
- // destination are the SAME buffer — even for ranges that provably do not overlap. ("Source and
2175
- // destination are the same buffer.") Both slots live in the one rest pool, so the copy has to
2176
- // hop out and back. One-time, at enable, so it costs nothing per frame.
2177
2202
  const staging = this.device.createBuffer({
2178
2203
  size: bytes,
2179
2204
  usage: GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST,
@@ -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.3",
3
+ "version": "0.15.5",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {