ugly-game 0.15.29 → 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.
- package/dist/gpuShadow.d.ts +85 -0
- package/dist/gpuShadow.js +135 -0
- package/package.json +1 -1
package/dist/gpuShadow.d.ts
CHANGED
|
@@ -152,6 +152,46 @@ export declare function packFaceMats(m: readonly number[]): [number, number];
|
|
|
152
152
|
* change away from being quietly wrong.
|
|
153
153
|
*/
|
|
154
154
|
export declare const INSTANCE_BYTES: number;
|
|
155
|
+
/** Floats per instance record. Pairs with INSTANCE_OFF for writing records directly. */
|
|
156
|
+
export declare const INSTANCE_FLOATS = 20;
|
|
157
|
+
/**
|
|
158
|
+
* THE INSTANCE RECORD'S FIELD OFFSETS, so a producer can write the GPU layout DIRECTLY.
|
|
159
|
+
*
|
|
160
|
+
* ⚠️ THIS EXISTS TO DELETE A PER-INSTANCE JAVASCRIPT OBJECT FROM THE HOT PATH. The object route is
|
|
161
|
+
* `updateInstanceRange(CubeInstance[])`, which is convenient and costs two heap allocations plus
|
|
162
|
+
* ~15 property reads for every instance, on the main thread, every time anything changes. Measured
|
|
163
|
+
* in all-mine-sky: 149 ms of allocation for a full draw set, then ~0.1 µs an instance to read the
|
|
164
|
+
* objects back and pack them — together the whole of a 445 ms frame at N=512.
|
|
165
|
+
*
|
|
166
|
+
* A mesher that already walks its cells can write these twenty floats as it goes and hand over bytes
|
|
167
|
+
* the GPU can take verbatim. See `updateInstanceRaw`.
|
|
168
|
+
*
|
|
169
|
+
* ⚠️ SLOTS 16 AND 17 ARE OVERLOADED, and which reading applies is decided by `tile`:
|
|
170
|
+
* tile >= 0 or -1 occupancy — 16 is occ's low 14 bits, 17 its high 13 PLUS the face mask above them
|
|
171
|
+
* tile == -2 six FACE MATERIALS, three per slot (see packFaceMats)
|
|
172
|
+
* tile == -3 PATCHWORK — 16 is the table offset, 17 the texels per face edge
|
|
173
|
+
* Writing the wrong pair for the tile sentinel is silent: the shader reads whichever the sentinel
|
|
174
|
+
* says, so an occupancy written under -2 becomes six material ids and the cube retextures itself.
|
|
175
|
+
*/
|
|
176
|
+
export declare const INSTANCE_OFF: {
|
|
177
|
+
readonly x: 0;
|
|
178
|
+
readonly y: 1;
|
|
179
|
+
readonly z: 2;
|
|
180
|
+
readonly hue: 3;
|
|
181
|
+
readonly sx: 4;
|
|
182
|
+
readonly sy: 5;
|
|
183
|
+
readonly sz: 6;
|
|
184
|
+
readonly emissive: 7;
|
|
185
|
+
readonly rot: 8;
|
|
186
|
+
readonly tile: 12;
|
|
187
|
+
readonly up: 13;
|
|
188
|
+
readonly decalPos: 14;
|
|
189
|
+
readonly decalKind: 15;
|
|
190
|
+
readonly aux0: 16;
|
|
191
|
+
readonly aux1: 17;
|
|
192
|
+
readonly jitter: 18;
|
|
193
|
+
readonly texFit: 19;
|
|
194
|
+
};
|
|
155
195
|
/**
|
|
156
196
|
* One rigid transform over a contiguous run of instances — see `GpuShadowRenderer.setBodyTransforms`.
|
|
157
197
|
* `first`/`count` are INSTANCE SLOTS in the buffer, matching what `updateInstanceRange` wrote.
|
|
@@ -598,6 +638,9 @@ export declare class GpuShadowRenderer {
|
|
|
598
638
|
private giGridBuf;
|
|
599
639
|
private giProbeBuf;
|
|
600
640
|
private count;
|
|
641
|
+
/** Ranges `clearInstanceRange` has been asked to blank, flushed into the next frame's encoder. */
|
|
642
|
+
private pendingClears;
|
|
643
|
+
private clearedTotal;
|
|
601
644
|
private cam;
|
|
602
645
|
/**
|
|
603
646
|
* Scratch for the per-cascade copy of `cam`, with that cascade's matrix parked at `lightVP[0]`.
|
|
@@ -841,7 +884,49 @@ export declare class GpuShadowRenderer {
|
|
|
841
884
|
* Out-of-range and empty writes are no-ops rather than errors: a caller assembling a frame should not
|
|
842
885
|
* have to special-case "this segment happens to be empty right now".
|
|
843
886
|
*/
|
|
887
|
+
/**
|
|
888
|
+
* WRITE ALREADY-PACKED RECORDS AT SLOT `first` — no objects, no repack, no scratch.
|
|
889
|
+
*
|
|
890
|
+
* `src` must be in this engine's layout (INSTANCE_FLOATS floats each, INSTANCE_OFF for the
|
|
891
|
+
* fields). The bytes go straight to the GPU: there is no per-instance JavaScript on this path at
|
|
892
|
+
* all, which is the entire point. A worker that already walks its cells can produce this directly
|
|
893
|
+
* and the main thread never touches an instance again.
|
|
894
|
+
*
|
|
895
|
+
* Partial by construction — write one entry's range without disturbing its neighbours, the same
|
|
896
|
+
* contract `updateInstanceRange` offers and the reason a mostly-static scene need not re-upload.
|
|
897
|
+
*
|
|
898
|
+
* ⚠️ IT BYPASSES THE CPU MIRROR, WHICH IS SAFE ONLY BECAUSE NOTHING READS PAST WHAT IT WROTE.
|
|
899
|
+
* `scratch` is filled and then uploaded over the same range by every other path, so it is never
|
|
900
|
+
* read beyond the range just written. Do not add a reader that assumes `scratch` mirrors the
|
|
901
|
+
* whole buffer, or ranges written through here will be silently stale to it.
|
|
902
|
+
*/
|
|
903
|
+
updateInstanceRaw(src: Float32Array<ArrayBuffer>, first: number): void;
|
|
844
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;
|
|
845
930
|
/** How many instances the cull + draw passes consider, whatever is in the buffer past that. Pairs with
|
|
846
931
|
* `updateInstanceRange`: shrinking the dynamic tail is a count change, not an upload. */
|
|
847
932
|
setInstanceCount(n: number): void;
|
package/dist/gpuShadow.js
CHANGED
|
@@ -70,6 +70,46 @@ const INST_F = 20;
|
|
|
70
70
|
* change away from being quietly wrong.
|
|
71
71
|
*/
|
|
72
72
|
export const INSTANCE_BYTES = INST_F * 4;
|
|
73
|
+
/** Floats per instance record. Pairs with INSTANCE_OFF for writing records directly. */
|
|
74
|
+
export const INSTANCE_FLOATS = INST_F;
|
|
75
|
+
/**
|
|
76
|
+
* THE INSTANCE RECORD'S FIELD OFFSETS, so a producer can write the GPU layout DIRECTLY.
|
|
77
|
+
*
|
|
78
|
+
* ⚠️ THIS EXISTS TO DELETE A PER-INSTANCE JAVASCRIPT OBJECT FROM THE HOT PATH. The object route is
|
|
79
|
+
* `updateInstanceRange(CubeInstance[])`, which is convenient and costs two heap allocations plus
|
|
80
|
+
* ~15 property reads for every instance, on the main thread, every time anything changes. Measured
|
|
81
|
+
* in all-mine-sky: 149 ms of allocation for a full draw set, then ~0.1 µs an instance to read the
|
|
82
|
+
* objects back and pack them — together the whole of a 445 ms frame at N=512.
|
|
83
|
+
*
|
|
84
|
+
* A mesher that already walks its cells can write these twenty floats as it goes and hand over bytes
|
|
85
|
+
* the GPU can take verbatim. See `updateInstanceRaw`.
|
|
86
|
+
*
|
|
87
|
+
* ⚠️ SLOTS 16 AND 17 ARE OVERLOADED, and which reading applies is decided by `tile`:
|
|
88
|
+
* tile >= 0 or -1 occupancy — 16 is occ's low 14 bits, 17 its high 13 PLUS the face mask above them
|
|
89
|
+
* tile == -2 six FACE MATERIALS, three per slot (see packFaceMats)
|
|
90
|
+
* tile == -3 PATCHWORK — 16 is the table offset, 17 the texels per face edge
|
|
91
|
+
* Writing the wrong pair for the tile sentinel is silent: the shader reads whichever the sentinel
|
|
92
|
+
* says, so an occupancy written under -2 becomes six material ids and the cube retextures itself.
|
|
93
|
+
*/
|
|
94
|
+
export const INSTANCE_OFF = {
|
|
95
|
+
x: 0,
|
|
96
|
+
y: 1,
|
|
97
|
+
z: 2,
|
|
98
|
+
hue: 3,
|
|
99
|
+
sx: 4,
|
|
100
|
+
sy: 5,
|
|
101
|
+
sz: 6,
|
|
102
|
+
emissive: 7,
|
|
103
|
+
rot: 8, // 8..11, xyzw; identity is (0,0,0,1)
|
|
104
|
+
tile: 12,
|
|
105
|
+
up: 13,
|
|
106
|
+
decalPos: 14,
|
|
107
|
+
decalKind: 15,
|
|
108
|
+
aux0: 16, // occ low 14 | faceMats[0] | patch.at
|
|
109
|
+
aux1: 17, // occ high 13 + faceMask<<13 | faceMats[1] | patch.res
|
|
110
|
+
jitter: 18,
|
|
111
|
+
texFit: 19,
|
|
112
|
+
};
|
|
73
113
|
const OVERLAY_CAP = 512; // max translucent overlay cubes per frame (the place ghost + headroom)
|
|
74
114
|
/** Rigid-body table: `bodies[0]` is the header, so this is bodies + 1. See `struct Body` in the WGSL. */
|
|
75
115
|
export const MAX_BODIES = 32;
|
|
@@ -2157,6 +2197,15 @@ fn cull(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
|
2157
2197
|
let i = gid.x;
|
|
2158
2198
|
if (i >= u32(cc.cnt.x)) { return; }
|
|
2159
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; }
|
|
2160
2209
|
// The cull pass indexes the instance buffer DIRECTLY, so its instance index is already global.
|
|
2161
2210
|
let bd = bodyOf(i);
|
|
2162
2211
|
let c = bd.pos.xyz + qr(bd.rot, inst.pos.xyz);
|
|
@@ -2904,6 +2953,9 @@ export class GpuShadowRenderer {
|
|
|
2904
2953
|
giGridBuf;
|
|
2905
2954
|
giProbeBuf;
|
|
2906
2955
|
count = 0;
|
|
2956
|
+
/** Ranges `clearInstanceRange` has been asked to blank, flushed into the next frame's encoder. */
|
|
2957
|
+
pendingClears = [];
|
|
2958
|
+
clearedTotal = 0;
|
|
2907
2959
|
// camVP(16) + lightVP[MAX_CASCADES](16 each) + sun + sunCol + tint + atlas + up + csm + eye
|
|
2908
2960
|
cam = new Float32Array(16 + MAX_CASCADES * 16 + 7 * 4);
|
|
2909
2961
|
/**
|
|
@@ -3699,6 +3751,9 @@ export class GpuShadowRenderer {
|
|
|
3699
3751
|
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
3700
3752
|
});
|
|
3701
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;
|
|
3702
3757
|
this.makeBindGroups();
|
|
3703
3758
|
}
|
|
3704
3759
|
updateInstances(instances) {
|
|
@@ -3732,6 +3787,33 @@ export class GpuShadowRenderer {
|
|
|
3732
3787
|
* Out-of-range and empty writes are no-ops rather than errors: a caller assembling a frame should not
|
|
3733
3788
|
* have to special-case "this segment happens to be empty right now".
|
|
3734
3789
|
*/
|
|
3790
|
+
/**
|
|
3791
|
+
* WRITE ALREADY-PACKED RECORDS AT SLOT `first` — no objects, no repack, no scratch.
|
|
3792
|
+
*
|
|
3793
|
+
* `src` must be in this engine's layout (INSTANCE_FLOATS floats each, INSTANCE_OFF for the
|
|
3794
|
+
* fields). The bytes go straight to the GPU: there is no per-instance JavaScript on this path at
|
|
3795
|
+
* all, which is the entire point. A worker that already walks its cells can produce this directly
|
|
3796
|
+
* and the main thread never touches an instance again.
|
|
3797
|
+
*
|
|
3798
|
+
* Partial by construction — write one entry's range without disturbing its neighbours, the same
|
|
3799
|
+
* contract `updateInstanceRange` offers and the reason a mostly-static scene need not re-upload.
|
|
3800
|
+
*
|
|
3801
|
+
* ⚠️ IT BYPASSES THE CPU MIRROR, WHICH IS SAFE ONLY BECAUSE NOTHING READS PAST WHAT IT WROTE.
|
|
3802
|
+
* `scratch` is filled and then uploaded over the same range by every other path, so it is never
|
|
3803
|
+
* read beyond the range just written. Do not add a reader that assumes `scratch` mirrors the
|
|
3804
|
+
* whole buffer, or ranges written through here will be silently stale to it.
|
|
3805
|
+
*/
|
|
3806
|
+
updateInstanceRaw(src, first) {
|
|
3807
|
+
const capN = (this.scratch.length / INST_F) | 0;
|
|
3808
|
+
if (first < 0 || first >= capN)
|
|
3809
|
+
return;
|
|
3810
|
+
const n = Math.min((src.length / INST_F) | 0, capN - first);
|
|
3811
|
+
if (n <= 0)
|
|
3812
|
+
return;
|
|
3813
|
+
this.device.queue.writeBuffer(this.instBuf, first * INST_F * 4, src, 0, n * INST_F);
|
|
3814
|
+
if (first + n > this.count)
|
|
3815
|
+
this.count = first + n;
|
|
3816
|
+
}
|
|
3735
3817
|
updateInstanceRange(instances, first) {
|
|
3736
3818
|
const capN = (this.scratch.length / INST_F) | 0;
|
|
3737
3819
|
if (first < 0 || first >= capN)
|
|
@@ -3746,6 +3828,48 @@ export class GpuShadowRenderer {
|
|
|
3746
3828
|
if (first + n > this.count)
|
|
3747
3829
|
this.count = first + n;
|
|
3748
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
|
+
}
|
|
3749
3873
|
/** How many instances the cull + draw passes consider, whatever is in the buffer past that. Pairs with
|
|
3750
3874
|
* `updateInstanceRange`: shrinking the dynamic tail is a count change, not an upload. */
|
|
3751
3875
|
setInstanceCount(n) {
|
|
@@ -3967,6 +4091,17 @@ export class GpuShadowRenderer {
|
|
|
3967
4091
|
* occluder that wants to cull per cascade has to be handed every cascade matrix up front, here.
|
|
3968
4092
|
*/
|
|
3969
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
|
+
}
|
|
3970
4105
|
// One matrix or many — a single Float32Array is the 1-cascade case and stays byte-identical.
|
|
3971
4106
|
const mats = Array.isArray(lightVP) ? lightVP : [lightVP];
|
|
3972
4107
|
const nc = Math.max(1, Math.min(MAX_CASCADES, mats.length));
|