ugly-game 0.15.27 → 0.15.29

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.
@@ -9,6 +9,18 @@ export interface CubeInstance {
9
9
  hue: number;
10
10
  emissive?: number;
11
11
  rot?: [number, number, number, number];
12
+ /**
13
+ * WHICH FACES TO DRAW: bit d, in FACE_DIRS order (+X −X +Y −Y +Z −Z). See FACE_MASK_ALL.
14
+ *
15
+ * ⚠️ 0 IS "UNKNOWN ⇒ DRAW ALL SIX", NOT "DRAW NOTHING" — the same sentinel `occ` uses, and what
16
+ * makes this safe to adopt one producer at a time. "Draw nothing" never needs expressing: a cube
17
+ * with no visible face is dropped by the mesher and never reaches the buffer.
18
+ *
19
+ * ⚠️ NOT A SUBSTITUTE FOR `occ`, WHICH IS STILL REQUIRED IN FULL. `occ` is 27-bit OCCUPANCY and
20
+ * vertex AO reads its DIAGONALS; this is only the six axial answers culling needs, shipped rather
21
+ * than re-derived per vertex. Ignored in faceMats/patchwork mode, where the slots carry materials.
22
+ */
23
+ faceMask?: number;
12
24
  /**
13
25
  * ATLAS TILE ID, or a sentinel.
14
26
  *
@@ -129,6 +141,17 @@ export interface AtlasLayout {
129
141
  * wrapping to an unrelated texture.
130
142
  */
131
143
  export declare function packFaceMats(m: readonly number[]): [number, number];
144
+ /**
145
+ * Bytes one instance occupies in the instance storage buffer.
146
+ *
147
+ * EXPORTED BECAUSE THE CALLER HAS TO SIZE `requiredLimits` WITH IT. A device built by a plain
148
+ * `requestDevice()` gets WebGPU's DEFAULT limits — 256 MiB `maxBufferSize`, 128 MiB
149
+ * `maxStorageBufferBindingSize` — no matter what the adapter advertises, so a consumer that wants a
150
+ * capacity past ~1.67 M instances must request the higher limit at device creation or the allocation
151
+ * is rejected. Working that out requires this number, and a caller that hardcodes 80 is one struct
152
+ * change away from being quietly wrong.
153
+ */
154
+ export declare const INSTANCE_BYTES: number;
132
155
  /**
133
156
  * One rigid transform over a contiguous run of instances — see `GpuShadowRenderer.setBodyTransforms`.
134
157
  * `first`/`count` are INSTANCE SLOTS in the buffer, matching what `updateInstanceRange` wrote.
@@ -478,10 +501,33 @@ BEVEL_SEG?: number): number;
478
501
  * is set rather than silently producing holes.
479
502
  */
480
503
  export declare const FACE_DIRS: readonly (readonly [number, number, number])[];
481
- /** Is face `d` open to air? A SET bit means the neighbour is SOLID — see occAt. */
482
- export declare function faceExposed(mask: number, d: number): boolean;
483
- /** How many faces this instance actually has to draw. mask 0 is the "unknown" sentinel ⇒ all six. */
484
- export declare function exposedFaceCount(mask: number): number;
504
+ /**
505
+ * THE FACE MASK: BIT d MEANS "DRAW FACE d". Six bits, in FACE_DIRS order.
506
+ *
507
+ * ⚠️ THIS IS NOT `occ`, AND THE TWO MUST NOT BE CONFLATED. `occ` is a 27-bit OCCUPANCY mask — a set
508
+ * bit is a SOLID neighbour — and it is still required in full, because vertex AO reads the DIAGONAL
509
+ * neighbours (`cornerAOAt` samples n+a1, n+a2 and n+a1+a2) and the bevel shapes edges from the same
510
+ * neighbourhood. Face culling needs only the six axial answers, and the producer already knows them:
511
+ * deriving them again per vertex in the shader meant a six-iteration loop calling `occAt`, which
512
+ * recomputes a 3D neighbour index every time, in the hottest shader in the engine.
513
+ *
514
+ * So the mesher ships what it already computed. `occ` keeps its 27 bits for AO and bevel; this
515
+ * carries the six that culling needs.
516
+ *
517
+ * ⚠️ ZERO IS "UNKNOWN ⇒ DRAW ALL SIX", NOT "DRAW NOTHING", which is the same sentinel `occ` uses.
518
+ * That is what makes the change safe to land incrementally: a producer that has not been taught to
519
+ * fill this in writes 0 and keeps its whole cube, rather than silently vanishing. "Draw nothing"
520
+ * never needs expressing — a cube with no visible face is dropped by the mesher and never reaches
521
+ * the buffer at all.
522
+ */
523
+ export declare const FACE_MASK_ALL = 63;
524
+ /** Is face `d` drawn? Bit d set ⇒ yes. 0 is the unknown sentinel ⇒ every face. */
525
+ export declare function faceExposed(faceMask: number, d: number): boolean;
526
+ /** How many faces this instance actually has to draw. 0 is the "unknown" sentinel ⇒ all six. */
527
+ export declare function exposedFaceCount(faceMask: number): number;
528
+ /** The six axial bits of a 27-bit occupancy mask, inverted into a face mask. For producers that
529
+ * already hold an `occ` — a solid neighbour hides its face. */
530
+ export declare function faceMaskFromOcc(occ: number): number;
485
531
  /**
486
532
  * Vertex counts for the three face-cull buckets, and the classifier that picks one.
487
533
  *
@@ -494,7 +540,7 @@ export declare function exposedFaceCount(mask: number): number;
494
540
  * making as cheap as it can be — a 6x cut for the common case.
495
541
  */
496
542
  export declare const FACE_CULL_BUCKET_VERTS: readonly number[];
497
- export declare function faceCullClass(mask: number): number;
543
+ export declare function faceCullClass(faceMask: number): number;
498
544
  export declare function bevelVertex(corner: readonly [number, number, number], home: readonly [number, number, number], mask: number, bevel: number, role?: number, axis?: readonly [number, number, number],
499
545
  /** The CONCAVE radius. Separate from `bevel` (the convex one) so the two halves of the rounding
500
546
  * can be turned up, down and off independently — the only way to tell which one a defect is in.
@@ -754,7 +800,20 @@ export declare class GpuShadowRenderer {
754
800
  setAtlas(tex: GPUTexture, layout: AtlasLayout): void;
755
801
  /** Static scene: sizes the buffer to exactly these instances. */
756
802
  setScene(instances: CubeInstance[]): void;
757
- /** Dynamic scene: pre-allocate for `cap` instances; call updateInstances() each frame. */
803
+ /**
804
+ * Dynamic scene: pre-allocate for `cap` instances; call updateInstances() each frame.
805
+ *
806
+ * ⚠️ THIS THROWS RATHER THAN LETTING THE DEVICE REJECT THE BUFFER, because the rejection is
807
+ * COMPLETELY SILENT and costs days. `createBuffer` past a device limit does not throw — it returns
808
+ * an INVALID buffer, and the failure only surfaces as `[Invalid Buffer] is invalid due to a
809
+ * previous error` on an `onuncapturederror` handler that most apps never install. Every bind group
810
+ * built from it is invalid, every pass using them is a no-op, and the symptom is a black frame with
811
+ * correct geometry, a full instance buffer, and zero visible instances in EVERY frustum at once.
812
+ * That is indistinguishable by inspection from a cull bug, a camera bug, or a matrix bug, and it
813
+ * was mistaken for all three before anyone thought to compare `device.limits` against the size.
814
+ *
815
+ * The limits are checkable up front and the message names the fix, so it is checked up front.
816
+ */
758
817
  setCapacity(cap: number): void;
759
818
  updateInstances(instances: CubeInstance[]): void;
760
819
  /**
package/dist/gpuShadow.js CHANGED
@@ -59,6 +59,17 @@ export function packFaceMats(m) {
59
59
  // anyway. Spending the other two on a spare slot is free, and the last time this buffer grew it was
60
60
  // for the contact decal, which had to widen it for exactly the same reason.
61
61
  const INST_F = 20;
62
+ /**
63
+ * Bytes one instance occupies in the instance storage buffer.
64
+ *
65
+ * EXPORTED BECAUSE THE CALLER HAS TO SIZE `requiredLimits` WITH IT. A device built by a plain
66
+ * `requestDevice()` gets WebGPU's DEFAULT limits — 256 MiB `maxBufferSize`, 128 MiB
67
+ * `maxStorageBufferBindingSize` — no matter what the adapter advertises, so a consumer that wants a
68
+ * capacity past ~1.67 M instances must request the higher limit at device creation or the allocation
69
+ * is rejected. Working that out requires this number, and a caller that hardcodes 80 is one struct
70
+ * change away from being quietly wrong.
71
+ */
72
+ export const INSTANCE_BYTES = INST_F * 4;
62
73
  const OVERLAY_CAP = 512; // max translucent overlay cubes per frame (the place ghost + headroom)
63
74
  /** Rigid-body table: `bodies[0]` is the header, so this is bodies + 1. See `struct Body` in the WGSL. */
64
75
  export const MAX_BODIES = 32;
@@ -1041,22 +1052,54 @@ export const FACE_DIRS = [
1041
1052
  [0, 0, 1],
1042
1053
  [0, 0, -1],
1043
1054
  ];
1044
- /** Is face `d` open to air? A SET bit means the neighbour is SOLID — see occAt. */
1045
- export function faceExposed(mask, d) {
1046
- const o = FACE_DIRS[d];
1047
- const idx = o[0] + 1 + (o[1] + 1) * 3 + (o[2] + 1) * 9;
1048
- return ((mask >>> idx) & 1) === 0;
1055
+ /**
1056
+ * THE FACE MASK: BIT d MEANS "DRAW FACE d". Six bits, in FACE_DIRS order.
1057
+ *
1058
+ * ⚠️ THIS IS NOT `occ`, AND THE TWO MUST NOT BE CONFLATED. `occ` is a 27-bit OCCUPANCY mask — a set
1059
+ * bit is a SOLID neighbour — and it is still required in full, because vertex AO reads the DIAGONAL
1060
+ * neighbours (`cornerAOAt` samples n+a1, n+a2 and n+a1+a2) and the bevel shapes edges from the same
1061
+ * neighbourhood. Face culling needs only the six axial answers, and the producer already knows them:
1062
+ * deriving them again per vertex in the shader meant a six-iteration loop calling `occAt`, which
1063
+ * recomputes a 3D neighbour index every time, in the hottest shader in the engine.
1064
+ *
1065
+ * So the mesher ships what it already computed. `occ` keeps its 27 bits for AO and bevel; this
1066
+ * carries the six that culling needs.
1067
+ *
1068
+ * ⚠️ ZERO IS "UNKNOWN ⇒ DRAW ALL SIX", NOT "DRAW NOTHING", which is the same sentinel `occ` uses.
1069
+ * That is what makes the change safe to land incrementally: a producer that has not been taught to
1070
+ * fill this in writes 0 and keeps its whole cube, rather than silently vanishing. "Draw nothing"
1071
+ * never needs expressing — a cube with no visible face is dropped by the mesher and never reaches
1072
+ * the buffer at all.
1073
+ */
1074
+ export const FACE_MASK_ALL = 0b111111;
1075
+ /** Is face `d` drawn? Bit d set ⇒ yes. 0 is the unknown sentinel ⇒ every face. */
1076
+ export function faceExposed(faceMask, d) {
1077
+ return faceMask === 0 || ((faceMask >>> d) & 1) === 1;
1049
1078
  }
1050
- /** How many faces this instance actually has to draw. mask 0 is the "unknown" sentinel ⇒ all six. */
1051
- export function exposedFaceCount(mask) {
1052
- if (mask === 0)
1079
+ /** How many faces this instance actually has to draw. 0 is the "unknown" sentinel ⇒ all six. */
1080
+ export function exposedFaceCount(faceMask) {
1081
+ if (faceMask === 0)
1053
1082
  return 6;
1054
1083
  let n = 0;
1055
1084
  for (let d = 0; d < 6; d++)
1056
- if (faceExposed(mask, d))
1085
+ if ((faceMask >>> d) & 1)
1057
1086
  n++;
1058
1087
  return n;
1059
1088
  }
1089
+ /** The six axial bits of a 27-bit occupancy mask, inverted into a face mask. For producers that
1090
+ * already hold an `occ` — a solid neighbour hides its face. */
1091
+ export function faceMaskFromOcc(occ) {
1092
+ if (occ === 0)
1093
+ return 0; // unknown stays unknown
1094
+ let m = 0;
1095
+ for (let d = 0; d < 6; d++) {
1096
+ const o = FACE_DIRS[d];
1097
+ const idx = o[0] + 1 + (o[1] + 1) * 3 + (o[2] + 1) * 9;
1098
+ if (((occ >>> idx) & 1) === 0)
1099
+ m |= 1 << d;
1100
+ }
1101
+ return m;
1102
+ }
1060
1103
  /**
1061
1104
  * Vertex counts for the three face-cull buckets, and the classifier that picks one.
1062
1105
  *
@@ -1069,8 +1112,8 @@ export function exposedFaceCount(mask) {
1069
1112
  * making as cheap as it can be — a 6x cut for the common case.
1070
1113
  */
1071
1114
  export const FACE_CULL_BUCKET_VERTS = [6, 18, 36];
1072
- export function faceCullClass(mask) {
1073
- const n = exposedFaceCount(mask);
1115
+ export function faceCullClass(faceMask) {
1116
+ const n = exposedFaceCount(faceMask);
1074
1117
  return n <= 1 ? 6 : n <= 3 ? 18 : 36;
1075
1118
  }
1076
1119
  // THE RIM TURN IS GONE, AND ITS ABSENCE IS THE POINT. rimTurn() clipped a face's corner and grew a
@@ -1576,29 +1619,27 @@ fn occAt(mask: u32, o: vec3<f32>) -> f32 {
1576
1619
  // ── FACE CULLING ────────────────────────────────────────────────────────────────────────────────
1577
1620
  // Mirrors faceExposed / exposedFaceCount / faceCullRemap in TS — ONE source, spliced into both the
1578
1621
  // draw and cull shaders, so there is no third copy. See the FACE CULLING block above buildCube.
1579
- const FACE_N = array<vec3<f32>, 6>(
1580
- vec3(1.0, 0.0, 0.0), vec3(-1.0, 0.0, 0.0),
1581
- vec3(0.0, 1.0, 0.0), vec3(0.0, -1.0, 0.0),
1582
- vec3(0.0, 0.0, 1.0), vec3(0.0, 0.0, -1.0),
1583
- );
1584
- fn faceExposedW(mask: u32, d: u32) -> bool {
1585
- return occAt(mask, FACE_N[d]) < 0.5; // a SET bit is a SOLID neighbour
1622
+ // ⚠️ THESE TAKE THE FACE MASK, NOT occ. Bit d means "draw face d". The mesher already computed the
1623
+ // six axial answers, so the shader no longer re-derives them from the 27-bit occupancy mask: that
1624
+ // cost a six-iteration loop calling occAt — which recomputes a 3D neighbour index per iteration —
1625
+ // for every vertex of every instance. occ is still needed in FULL for AO (cornerAOAt reads the
1626
+ // diagonals) and the bevel; it is simply not what culling asks.
1627
+ fn faceExposedW(fm: u32, d: u32) -> bool {
1628
+ return fm == 0u || ((fm >> d) & 1u) == 1u; // 0 = unknown, draw everything
1586
1629
  }
1587
- fn exposedFaceCountW(mask: u32) -> u32 {
1588
- if (mask == 0u) { return 6u; } // unknown ⇒ draw everything, same sentinel as the bevel
1589
- var n = 0u;
1590
- for (var d = 0u; d < 6u; d = d + 1u) { if (faceExposedW(mask, d)) { n = n + 1u; } }
1591
- return n;
1630
+ fn exposedFaceCountW(fm: u32) -> u32 {
1631
+ if (fm == 0u) { return 6u; } // unknown ⇒ draw everything, same sentinel as the bevel
1632
+ return countOneBits(fm & 63u);
1592
1633
  }
1593
1634
  // Rewrite a sharp-cube vertex index onto the (vi / 6)-th EXPOSED face, keeping its corner. Returns
1594
1635
  // 0xffffffff when this slot has no face left — the draw's bucket is sized for the WORST instance in
1595
1636
  // it, so most instances have slots to spare and the vertex shader collapses those to a point.
1596
- fn faceCullRemap(mask: u32, vi: u32) -> u32 {
1637
+ fn faceCullRemap(fm: u32, vi: u32) -> u32 {
1597
1638
  let slot = vi / 6u;
1598
1639
  let corner = vi % 6u;
1599
1640
  var seen = 0u;
1600
1641
  for (var d = 0u; d < 6u; d = d + 1u) {
1601
- if (faceExposedW(mask, d)) {
1642
+ if (faceExposedW(fm, d)) {
1602
1643
  if (seen == slot) { return d * 6u + corner; }
1603
1644
  seen = seen + 1u;
1604
1645
  }
@@ -2189,9 +2230,11 @@ fn cull(@builtin(global_invocation_id) gid: vec3<u32>) {
2189
2230
  // faceCull.test.mts are what hold that, one per mode.
2190
2231
  var b = 0u;
2191
2232
  if (cc.lod.y > 0.5) {
2192
- let mask = select(u32(inst.aux2.x) | (u32(inst.aux2.y) << 14u), 0u, inst.aux.x < -1.5);
2233
+ let raw2 = select(u32(inst.aux2.y), 0u, inst.aux.x < -1.5);
2234
+ let mask = select(u32(inst.aux2.x) | ((raw2 & 8191u) << 14u), 0u, inst.aux.x < -1.5);
2235
+ let fmask = raw2 >> 13u; // the six face bits riding above occ's high 13
2193
2236
  if (cc.lod.y > 1.5) {
2194
- let n = exposedFaceCountW(mask);
2237
+ let n = exposedFaceCountW(fmask);
2195
2238
  if (n > 3u) { b = 2u; } else if (n > 1u) { b = 1u; }
2196
2239
  } else {
2197
2240
  let cls = bevelClass(mask, cc.lod.z, cc.lod.w);
@@ -2299,7 +2342,10 @@ fn faceVec(code: f32) -> vec3<f32> {
2299
2342
  // TS template literal, so one ends the shader mid-string and the error surfaces as a TS parse failure
2300
2343
  // dozens of lines later.
2301
2344
  let isPatch = inst.aux.x < -1.5;
2302
- let mask = select(u32(inst.aux2.x) | (u32(inst.aux2.y) << 14u), 0u, isPatch);
2345
+ let raw2 = select(u32(inst.aux2.y), 0u, isPatch);
2346
+ let mask = select(u32(inst.aux2.x) | ((raw2 & 8191u) << 14u), 0u, isPatch);
2347
+ // The six face bits the mesher already computed, riding above occ's high 13. 0 = unknown ⇒ all six.
2348
+ let fmask = raw2 >> 13u;
2303
2349
  // THE BEVELED CUBE. cam.eye.w is the bevel amount; at 0 — every caller that has not opted in —
2304
2350
  // bevelPos returns the sharp corner and this is buildCube()'s geometry exactly. The draw is only
2305
2351
  // 36 vertices long in that case anyway (see the indirect args), so the tail is never even reached.
@@ -2315,7 +2361,7 @@ fn faceVec(code: f32) -> vec3<f32> {
2315
2361
  var vv = vi;
2316
2362
  var faceless = false; // this slot has no exposed face left → collapse it below
2317
2363
  if (cam.csm.w > 0.5 && cam.eye.w <= 0.0 && cam.csm.z <= 0.0 && mask != 0u) {
2318
- let r = faceCullRemap(mask, vi);
2364
+ let r = faceCullRemap(fmask, vi);
2319
2365
  if (r == 0xffffffffu) { faceless = true; } else { vv = r; }
2320
2366
  }
2321
2367
  let sharp = BEV_CORNER[vv];
@@ -3434,7 +3480,19 @@ export class GpuShadowRenderer {
3434
3480
  const pt = c.tile === -3 ? c.patch : undefined;
3435
3481
  const occ = c.occ ?? 0;
3436
3482
  scratch[o + 16] = pt ? pt.at : fm ? fm[0] : occ & 0x3fff;
3437
- scratch[o + 17] = pt ? pt.res : fm ? fm[1] : (occ / 0x4000) | 0;
3483
+ // ⚠️ THE FACE MASK RIDES ABOVE occ's HIGH 13 BITS, IN THE SAME FLOAT, AT NO WIRE COST. occ is
3484
+ // 27 bits split 14/13, so slot 17 carries values under 2^13 and an f32 holds integers exactly
3485
+ // to 2^24 — six more bits fit with room to spare (8192·64 = 524 288). The face mask could not
3486
+ // simply REPLACE occ: vertex AO reads the DIAGONAL neighbours (cornerAOAt samples n+a1+a2) and
3487
+ // the bevel shapes edges from the same neighbourhood, so all 27 bits are still required.
3488
+ //
3489
+ // Never in patchwork or face-material mode — there slots 16/17 are SIX MATERIAL IDS and the
3490
+ // shader already forces the mask to the 0 "unknown" sentinel.
3491
+ scratch[o + 17] = pt
3492
+ ? pt.res
3493
+ : fm
3494
+ ? fm[1]
3495
+ : ((occ / 0x4000) | 0) + (c.faceMask ?? 0) * 0x2000;
3438
3496
  scratch[o + 18] = c.jitter ?? 0;
3439
3497
  scratch[o + 19] = c.texFit ?? 0; // texFit: one atlas tile per face at any draw scale (see CubeInstance.texFit)
3440
3498
  }
@@ -3606,8 +3664,30 @@ export class GpuShadowRenderer {
3606
3664
  this.device.queue.writeBuffer(this.instBuf, 0, this.scratch);
3607
3665
  this.makeBindGroups();
3608
3666
  }
3609
- /** Dynamic scene: pre-allocate for `cap` instances; call updateInstances() each frame. */
3667
+ /**
3668
+ * Dynamic scene: pre-allocate for `cap` instances; call updateInstances() each frame.
3669
+ *
3670
+ * ⚠️ THIS THROWS RATHER THAN LETTING THE DEVICE REJECT THE BUFFER, because the rejection is
3671
+ * COMPLETELY SILENT and costs days. `createBuffer` past a device limit does not throw — it returns
3672
+ * an INVALID buffer, and the failure only surfaces as `[Invalid Buffer] is invalid due to a
3673
+ * previous error` on an `onuncapturederror` handler that most apps never install. Every bind group
3674
+ * built from it is invalid, every pass using them is a no-op, and the symptom is a black frame with
3675
+ * correct geometry, a full instance buffer, and zero visible instances in EVERY frustum at once.
3676
+ * That is indistinguishable by inspection from a cull bug, a camera bug, or a matrix bug, and it
3677
+ * was mistaken for all three before anyone thought to compare `device.limits` against the size.
3678
+ *
3679
+ * The limits are checkable up front and the message names the fix, so it is checked up front.
3680
+ */
3610
3681
  setCapacity(cap) {
3682
+ const need = Math.max(INST_F * 4, cap * INST_F * 4);
3683
+ const lim = this.device.limits;
3684
+ if (need > lim.maxBufferSize || need > lim.maxStorageBufferBindingSize)
3685
+ throw new Error(`GpuShadowRenderer.setCapacity(${cap}): the instance buffer needs ${(need / 1048576).toFixed(1)} MiB, ` +
3686
+ `past this device's maxBufferSize ${(lim.maxBufferSize / 1048576).toFixed(1)} MiB / ` +
3687
+ `maxStorageBufferBindingSize ${(lim.maxStorageBufferBindingSize / 1048576).toFixed(1)} MiB. ` +
3688
+ `A plain requestDevice() grants WebGPU's DEFAULT limits regardless of what the adapter supports — ` +
3689
+ `pass requiredLimits: { maxBufferSize, maxStorageBufferBindingSize } at device creation, ` +
3690
+ `clamped to adapter.limits, or lower the capacity.`);
3611
3691
  this.cap = cap;
3612
3692
  this.scratch = new Float32Array(cap * INST_F);
3613
3693
  this.sceneReady = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.27",
3
+ "version": "0.15.29",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {