ugly-game 0.9.0 → 0.10.0
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 +28 -3
- package/dist/gpuShadow.js +217 -76
- package/package.json +1 -1
package/dist/gpuShadow.d.ts
CHANGED
|
@@ -30,18 +30,26 @@ export declare const CUBE: {
|
|
|
30
30
|
export declare class GpuShadowRenderer {
|
|
31
31
|
private device;
|
|
32
32
|
private instBuf;
|
|
33
|
+
private sceneReady;
|
|
33
34
|
private camBuf;
|
|
34
35
|
private shadowTex;
|
|
35
36
|
private shadowView;
|
|
37
|
+
private shadowArrayView;
|
|
38
|
+
private shadowLayerViews;
|
|
39
|
+
private shadowCamBufs;
|
|
36
40
|
private shadowPipe;
|
|
37
41
|
private mainPipe;
|
|
38
|
-
private
|
|
42
|
+
private shadowBGs;
|
|
39
43
|
private mainBG;
|
|
40
44
|
private shadowBGL;
|
|
41
45
|
private mainBGL;
|
|
42
46
|
private shadowSampler;
|
|
43
47
|
private count;
|
|
44
48
|
private cam;
|
|
49
|
+
private shadowCam;
|
|
50
|
+
private cascades;
|
|
51
|
+
/** Width of the blend into the next cascade, in uv (0 = hard switch). Small: it costs a 2nd PCF. */
|
|
52
|
+
cascadeBand: number;
|
|
45
53
|
private atlasView;
|
|
46
54
|
private atlasSampler;
|
|
47
55
|
private tileRectBuf;
|
|
@@ -85,6 +93,11 @@ export declare class GpuShadowRenderer {
|
|
|
85
93
|
private makeCullBuf;
|
|
86
94
|
private cullBG;
|
|
87
95
|
private makeCullResources;
|
|
96
|
+
/** (Re)allocate the shadow array + per-cascade uniforms for `n` cascades. Idempotent per count. */
|
|
97
|
+
private ensureCascades;
|
|
98
|
+
/** Allocate the shadow array + per-cascade uniforms. Split from ensureCascades so makeBindGroups
|
|
99
|
+
* can guarantee resources exist without recursing back into it. */
|
|
100
|
+
private allocCascades;
|
|
88
101
|
/** (Re)create the coarse Hi-Z for a `w×h` depth target and rebind the cull groups to it. Idempotent per size. */
|
|
89
102
|
private ensureHiZ;
|
|
90
103
|
private scratch;
|
|
@@ -116,14 +129,26 @@ export declare class GpuShadowRenderer {
|
|
|
116
129
|
private writeCull;
|
|
117
130
|
/** The sun shadow-map depth view + its comparison sampler + texel size. Feed these into GpuPropsRenderer/
|
|
118
131
|
* GpuSkinRenderer.frame(lightVP, …) so mesh props & creatures RECEIVE the same sun shadow the cubes sample.
|
|
119
|
-
*
|
|
132
|
+
*
|
|
133
|
+
* CASCADES: this is a plain 2d view of layer 0 — the TIGHTEST cascade. Props and skinned meshes
|
|
134
|
+
* sample a `texture_depth_2d`, so handing them the array would be a breaking change to two more
|
|
135
|
+
* shaders; layer 0 is what they already effectively received when there was one map, so they are
|
|
136
|
+
* unaffected. The consequence is that a distant prop receives no shadow even when the terrain
|
|
137
|
+
* around it does. Making those two cascade-aware is the follow-up.
|
|
138
|
+
*
|
|
139
|
+
* NOTE: recreated when the cascade COUNT changes, so re-read it after a frame that changes it
|
|
140
|
+
* rather than caching the view across such a change. */
|
|
120
141
|
get shadowMapView(): GPUTextureView;
|
|
142
|
+
/** The full cascade array view, for a consumer that samples `texture_depth_2d_array`. */
|
|
143
|
+
get shadowMapArrayView(): GPUTextureView;
|
|
144
|
+
/** How many cascade layers are currently allocated. */
|
|
145
|
+
get cascadeCount(): number;
|
|
121
146
|
get shadowMapSampler(): GPUSampler;
|
|
122
147
|
get shadowMapSize(): number;
|
|
123
148
|
/** camVP/lightVP: column-major Float32Array(16). sunDir: travel direction. Pass `occlusion` (the depth target +
|
|
124
149
|
* its size) to enable Hi-Z occlusion culling on the camera pass — the renderer rebuilds a coarse Hi-Z from that
|
|
125
150
|
* depth each frame and the NEXT frame's cull uses it (hole-free: the first frame + any resize skip occlusion). */
|
|
126
|
-
frame(enc: GPUCommandEncoder, colorView: GPUTextureView, sceneDepth: GPUTextureView, camVP: Float32Array, lightVP: Float32Array, sunDir: [number, number, number], sunCol: [number, number, number], ambient: number, load?: boolean, occlusion?: {
|
|
151
|
+
frame(enc: GPUCommandEncoder, colorView: GPUTextureView, sceneDepth: GPUTextureView, camVP: Float32Array, lightVP: Float32Array | Float32Array[], sunDir: [number, number, number], sunCol: [number, number, number], ambient: number, load?: boolean, occlusion?: {
|
|
127
152
|
depth: GPUTextureView;
|
|
128
153
|
w: number;
|
|
129
154
|
h: number;
|
package/dist/gpuShadow.js
CHANGED
|
@@ -13,6 +13,15 @@
|
|
|
13
13
|
* place ghost, destroy marker, projectiles, and sparkles so effect cubes read as distinct from terrain). */
|
|
14
14
|
import { frustumPlanes } from './mat4';
|
|
15
15
|
const SHADOW_SIZE = 2048;
|
|
16
|
+
// CASCADED SHADOW MAPS. One ortho map has to choose between resolution and reach: AMS ran a single
|
|
17
|
+
// 90-block box against a 280-block draw distance, so everything past it was simply unshadowed and
|
|
18
|
+
// the far terrain read flat. Cascades split the range into nested boxes — tight and crisp near the
|
|
19
|
+
// camera, loose and far behind it — each rendered into its own layer of a depth-2d-array.
|
|
20
|
+
//
|
|
21
|
+
// A caller passing ONE matrix gets exactly the old behaviour, byte for byte: one layer, one cull,
|
|
22
|
+
// one sample, and the selection loop terminates immediately. That keeps the five demo pages and
|
|
23
|
+
// AMS's existing props/doors/skin path unmoved until each opts in.
|
|
24
|
+
const MAX_CASCADES = 4;
|
|
16
25
|
const CULL_WG = 64; // compute-cull workgroup size
|
|
17
26
|
const INST_F = 16; // floats per instance: pos.xyz+hue, scl.xyz+emissive, rot quat, aux (tile,up,_,_)
|
|
18
27
|
const OVERLAY_CAP = 512; // max translucent overlay cubes per frame (the place ghost + headroom)
|
|
@@ -53,12 +62,13 @@ export const CUBE = buildCube();
|
|
|
53
62
|
const COMMON_WGSL = /* wgsl */ `
|
|
54
63
|
struct Cam {
|
|
55
64
|
camVP: mat4x4<f32>,
|
|
56
|
-
lightVP: mat4x4<f32>,
|
|
65
|
+
lightVP: array<mat4x4<f32>, ${MAX_CASCADES}>, // CASCADES, tightest first. A 1-cascade caller uses only [0].
|
|
57
66
|
sun: vec4<f32>, // xyz = direction light travels, w = ambient
|
|
58
67
|
sunCol: vec4<f32>, // xyz = sun colour, w = shadow map size (texels)
|
|
59
68
|
tint: vec4<f32>, // rgb = per-planet tint · w = tint strength (0 = none)
|
|
60
69
|
atlas: vec4<f32>, // x = useAtlas (1/0)
|
|
61
|
-
up: vec4<f32>, // xyz = local gravity up (for hemisphere face-shading readability); w
|
|
70
|
+
up: vec4<f32>, // xyz = local gravity up (for hemisphere face-shading readability); w = linear-HDR flag
|
|
71
|
+
csm: vec4<f32>, // x = active cascade count · y = crossfade band in uv units
|
|
62
72
|
};
|
|
63
73
|
struct Inst { pos: vec4<f32>, scl: vec4<f32>, rot: vec4<f32>, aux: vec4<f32> }; // pos.w = hue (<0 ⇒ ground) · scl.w = emissive/alpha · rot = quaternion · aux.x = atlas tile (<0 ⇒ hue) · aux.y = up-face code
|
|
64
74
|
@group(0) @binding(0) var<uniform> cam: Cam;
|
|
@@ -196,13 +206,15 @@ const HIZ_DOWN = 8; // source pixels per Hi-Z texel (coarse ⇒ conservative, ch
|
|
|
196
206
|
const SHADOW_WGSL = COMMON_WGSL +
|
|
197
207
|
/* wgsl */ `
|
|
198
208
|
@group(0) @binding(2) var<storage, read> vis: array<u32>;
|
|
209
|
+
// Always index [0]: each cascade's shadow pass binds its OWN Cam buffer whose lightVP[0] holds that
|
|
210
|
+
// cascade's matrix. One uniform per cascade costs a few hundred bytes and avoids dynamic offsets.
|
|
199
211
|
@vertex fn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> @builtin(position) vec4<f32> {
|
|
200
|
-
return cam.lightVP * vec4(worldOf(insts[vis[ii]], vi), 1.0);
|
|
212
|
+
return cam.lightVP[0] * vec4(worldOf(insts[vis[ii]], vi), 1.0);
|
|
201
213
|
}
|
|
202
214
|
`;
|
|
203
215
|
const MAIN_WGSL = COMMON_WGSL +
|
|
204
216
|
/* wgsl */ `
|
|
205
|
-
@group(0) @binding(2) var shadowMap:
|
|
217
|
+
@group(0) @binding(2) var shadowMap: texture_depth_2d_array;
|
|
206
218
|
@group(0) @binding(3) var shadowSamp: sampler_comparison;
|
|
207
219
|
@group(0) @binding(4) var<storage, read> vis: array<u32>;
|
|
208
220
|
@group(0) @binding(5) var atlasTex: texture_2d<f32>;
|
|
@@ -236,23 +248,56 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
236
248
|
return o;
|
|
237
249
|
}
|
|
238
250
|
|
|
239
|
-
// PCF
|
|
240
|
-
|
|
241
|
-
|
|
251
|
+
// PCF 3x3 against ONE cascade layer. Returns 1 = fully lit, 0 = fully shadowed; the 'inside' out-param
|
|
252
|
+
// reports whether the point actually fell in this cascade's box, so the caller can try the next one.
|
|
253
|
+
fn pcfLayer(world: vec3<f32>, ndl: f32, layer: i32, inside: ptr<function, bool>, edge: ptr<function, f32>) -> f32 {
|
|
254
|
+
let lp = cam.lightVP[layer] * vec4(world, 1.0);
|
|
242
255
|
let proj = lp.xyz / lp.w;
|
|
243
256
|
let uv = proj.xy * vec2(0.5, -0.5) + vec2(0.5, 0.5);
|
|
244
|
-
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z > 1.0 || proj.z < 0.0) {
|
|
245
|
-
|
|
257
|
+
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z > 1.0 || proj.z < 0.0) {
|
|
258
|
+
*inside = false; *edge = 0.0; return 1.0;
|
|
259
|
+
}
|
|
260
|
+
*inside = true;
|
|
261
|
+
// How close to this cascade's border we are, 0 at the middle and 1 at the rim — drives the blend
|
|
262
|
+
// into the next cascade so the resolution change is a gradient rather than a visible seam.
|
|
263
|
+
let m = max(max(abs(uv.x - 0.5), abs(uv.y - 0.5)) * 2.0, abs(proj.z * 2.0 - 1.0));
|
|
264
|
+
*edge = clamp((m - (1.0 - cam.csm.y)) / max(cam.csm.y, 1e-5), 0.0, 1.0);
|
|
265
|
+
// Bias scales with the cascade's world extent: a far cascade covers far more world per texel, so
|
|
266
|
+
// the depth slope across one texel is larger and a near-cascade bias would let acne back in.
|
|
267
|
+
let scale = 1.0 + f32(layer) * 1.5;
|
|
268
|
+
let bias = max(0.0006 * (1.0 - ndl), 0.0004) * scale;
|
|
246
269
|
let texel = 1.0 / cam.sunCol.w;
|
|
247
270
|
var s = 0.0;
|
|
248
271
|
for (var dy = -1; dy <= 1; dy = dy + 1) {
|
|
249
272
|
for (var dx = -1; dx <= 1; dx = dx + 1) {
|
|
250
|
-
s = s + textureSampleCompareLevel(shadowMap, shadowSamp, uv + vec2(f32(dx), f32(dy)) * texel, proj.z - bias);
|
|
273
|
+
s = s + textureSampleCompareLevel(shadowMap, shadowSamp, uv + vec2(f32(dx), f32(dy)) * texel, layer, proj.z - bias);
|
|
251
274
|
}
|
|
252
275
|
}
|
|
253
276
|
return s / 9.0;
|
|
254
277
|
}
|
|
255
278
|
|
|
279
|
+
// Walk the cascades tightest-first and use the first that contains the point. Selection by
|
|
280
|
+
// CONTAINMENT rather than by view depth means no split distances have to be shipped to the shader
|
|
281
|
+
// and kept in sync with the matrices — the boxes themselves are the authority.
|
|
282
|
+
fn shadowFactor(world: vec3<f32>, ndl: f32) -> f32 {
|
|
283
|
+
let n = i32(cam.csm.x);
|
|
284
|
+
var inside = false; var edge = 0.0;
|
|
285
|
+
for (var c = 0; c < n; c = c + 1) {
|
|
286
|
+
let f = pcfLayer(world, ndl, c, &inside, &edge);
|
|
287
|
+
if (inside) {
|
|
288
|
+
// Near this cascade's rim, cross-fade into the next one (if there is a next one) so the
|
|
289
|
+
// resolution step never draws a hard line across the ground.
|
|
290
|
+
if (edge > 0.0 && c + 1 < n) {
|
|
291
|
+
var i2 = false; var e2 = 0.0;
|
|
292
|
+
let g = pcfLayer(world, ndl, c + 1, &i2, &e2);
|
|
293
|
+
if (i2) { return mix(f, g, edge); }
|
|
294
|
+
}
|
|
295
|
+
return f;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return 1.0; // beyond every cascade → unshadowed, same as the old single-map out-of-range case
|
|
299
|
+
}
|
|
300
|
+
|
|
256
301
|
// Per-block edge darkening (baked AO) — mirrors shared/voxelShade.ts blockShade so a flat face shows its
|
|
257
302
|
// individual voxels instead of reading as one uniform slab. fx,fz ∈ [0,1) within the block face.
|
|
258
303
|
fn blockShade(fx: f32, fz: f32) -> f32 {
|
|
@@ -335,18 +380,27 @@ struct AOut { @builtin(position) clip: vec4<f32>, @location(0) hue: f32, @locati
|
|
|
335
380
|
export class GpuShadowRenderer {
|
|
336
381
|
device;
|
|
337
382
|
instBuf;
|
|
383
|
+
sceneReady = false; // instBuf exists — setScene/setCapacity has run at least once
|
|
338
384
|
camBuf;
|
|
339
|
-
shadowTex;
|
|
340
|
-
shadowView;
|
|
385
|
+
shadowTex = null; // null until the first frame picks a cascade count
|
|
386
|
+
shadowView; // layer-0 2d view — the back-compat getter for props/skin
|
|
387
|
+
shadowArrayView; // the 2d-array view the main pass samples
|
|
388
|
+
shadowLayerViews = []; // one render-attachment view per cascade
|
|
389
|
+
shadowCamBufs = [];
|
|
341
390
|
shadowPipe;
|
|
342
391
|
mainPipe;
|
|
343
|
-
|
|
392
|
+
shadowBGs = [];
|
|
344
393
|
mainBG;
|
|
345
394
|
shadowBGL;
|
|
346
395
|
mainBGL;
|
|
347
396
|
shadowSampler;
|
|
348
397
|
count = 0;
|
|
349
|
-
|
|
398
|
+
// camVP(16) + lightVP[MAX_CASCADES](16 each) + sun + sunCol + tint + atlas + up + csm
|
|
399
|
+
cam = new Float32Array(16 + MAX_CASCADES * 16 + 6 * 4);
|
|
400
|
+
shadowCam = new Float32Array(16 + MAX_CASCADES * 16 + 6 * 4); // scratch for a per-cascade copy
|
|
401
|
+
cascades = 0; // layers currently allocated; 0 until the first frame sizes it
|
|
402
|
+
/** Width of the blend into the next cascade, in uv (0 = hard switch). Small: it costs a 2nd PCF. */
|
|
403
|
+
cascadeBand = 0.12;
|
|
350
404
|
atlasView;
|
|
351
405
|
atlasSampler;
|
|
352
406
|
tileRectBuf;
|
|
@@ -367,10 +421,10 @@ export class GpuShadowRenderer {
|
|
|
367
421
|
camVis;
|
|
368
422
|
camCull;
|
|
369
423
|
camCullBG;
|
|
370
|
-
lightArgs;
|
|
371
|
-
lightVis;
|
|
372
|
-
lightCull;
|
|
373
|
-
lightCullBG;
|
|
424
|
+
lightArgs = [];
|
|
425
|
+
lightVis = [];
|
|
426
|
+
lightCull = [];
|
|
427
|
+
lightCullBG = [];
|
|
374
428
|
statsBuf; // readback: [camVisible, lightVisible] for the IR/tests
|
|
375
429
|
/** Last frame's visible counts (main-pass camera cull, shadow-pass light cull) + total — for IR/telemetry. */
|
|
376
430
|
lastCull = { total: 0, camVisible: 0, lightVisible: 0 };
|
|
@@ -390,12 +444,6 @@ export class GpuShadowRenderer {
|
|
|
390
444
|
size: this.cam.byteLength,
|
|
391
445
|
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
392
446
|
});
|
|
393
|
-
this.shadowTex = device.createTexture({
|
|
394
|
-
size: [SHADOW_SIZE, SHADOW_SIZE],
|
|
395
|
-
format: 'depth32float',
|
|
396
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
|
|
397
|
-
});
|
|
398
|
-
this.shadowView = this.shadowTex.createView();
|
|
399
447
|
const shadowMod = device.createShaderModule({ code: SHADOW_WGSL });
|
|
400
448
|
const mainMod = device.createShaderModule({ code: MAIN_WGSL });
|
|
401
449
|
const shadowBGL = device.createBindGroupLayout({
|
|
@@ -442,7 +490,7 @@ export class GpuShadowRenderer {
|
|
|
442
490
|
{
|
|
443
491
|
binding: 2,
|
|
444
492
|
visibility: GPUShaderStage.FRAGMENT,
|
|
445
|
-
texture: { sampleType: 'depth' },
|
|
493
|
+
texture: { sampleType: 'depth', viewDimension: '2d-array' }, // cascades live in array layers
|
|
446
494
|
},
|
|
447
495
|
{
|
|
448
496
|
binding: 3,
|
|
@@ -669,17 +717,65 @@ export class GpuShadowRenderer {
|
|
|
669
717
|
this.camArgs = cam.args;
|
|
670
718
|
this.camVis = cam.vis;
|
|
671
719
|
this.camCull = cam.cull;
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
this.
|
|
720
|
+
// One light frustum per cascade: each box culls independently, so a far cascade still draws the
|
|
721
|
+
// distant occluders a tight near box would have thrown away.
|
|
722
|
+
const n = Math.max(1, this.cascades);
|
|
723
|
+
this.lightArgs = [];
|
|
724
|
+
this.lightVis = [];
|
|
725
|
+
this.lightCull = [];
|
|
726
|
+
for (let i = 0; i < n; i++) {
|
|
727
|
+
const lit = this.makeCullBuf();
|
|
728
|
+
this.lightArgs.push(lit.args);
|
|
729
|
+
this.lightVis.push(lit.vis);
|
|
730
|
+
this.lightCull.push(lit.cull);
|
|
731
|
+
}
|
|
676
732
|
this.camCullBG = this.cullBG(this.camArgs, this.camVis, this.camCull);
|
|
677
|
-
this.lightCullBG = this.
|
|
733
|
+
this.lightCullBG = this.lightArgs.map((a, i) => this.cullBG(a, this.lightVis[i], this.lightCull[i]));
|
|
678
734
|
this.statsBuf = this.device.createBuffer({
|
|
679
735
|
size: 4 * 4 * 2,
|
|
680
736
|
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
|
|
681
737
|
});
|
|
682
738
|
}
|
|
739
|
+
/** (Re)allocate the shadow array + per-cascade uniforms for `n` cascades. Idempotent per count. */
|
|
740
|
+
ensureCascades(n) {
|
|
741
|
+
if (this.cascades === n && this.shadowTex)
|
|
742
|
+
return;
|
|
743
|
+
this.allocCascades(n);
|
|
744
|
+
// setScene may not have run yet (no instance buffer to bind), in which case makeBindGroups will
|
|
745
|
+
// pick these up when it does.
|
|
746
|
+
if (this.sceneReady)
|
|
747
|
+
this.makeBindGroups();
|
|
748
|
+
}
|
|
749
|
+
/** Allocate the shadow array + per-cascade uniforms. Split from ensureCascades so makeBindGroups
|
|
750
|
+
* can guarantee resources exist without recursing back into it. */
|
|
751
|
+
allocCascades(n) {
|
|
752
|
+
this.shadowTex?.destroy();
|
|
753
|
+
for (const b of this.shadowCamBufs)
|
|
754
|
+
b.destroy();
|
|
755
|
+
this.cascades = n;
|
|
756
|
+
this.shadowTex = this.device.createTexture({
|
|
757
|
+
size: [SHADOW_SIZE, SHADOW_SIZE, n],
|
|
758
|
+
format: 'depth32float',
|
|
759
|
+
dimension: '2d',
|
|
760
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
|
|
761
|
+
});
|
|
762
|
+
this.shadowArrayView = this.shadowTex.createView({ dimension: '2d-array' });
|
|
763
|
+
this.shadowLayerViews = [];
|
|
764
|
+
for (let i = 0; i < n; i++)
|
|
765
|
+
this.shadowLayerViews.push(this.shadowTex.createView({
|
|
766
|
+
dimension: '2d',
|
|
767
|
+
baseArrayLayer: i,
|
|
768
|
+
arrayLayerCount: 1,
|
|
769
|
+
}));
|
|
770
|
+
// Layer 0 is the tightest cascade — the one a caller that only understands a single map wants.
|
|
771
|
+
this.shadowView = this.shadowLayerViews[0];
|
|
772
|
+
this.shadowCamBufs = [];
|
|
773
|
+
for (let i = 0; i < n; i++)
|
|
774
|
+
this.shadowCamBufs.push(this.device.createBuffer({
|
|
775
|
+
size: this.cam.byteLength,
|
|
776
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
777
|
+
}));
|
|
778
|
+
}
|
|
683
779
|
/** (Re)create the coarse Hi-Z for a `w×h` depth target and rebind the cull groups to it. Idempotent per size. */
|
|
684
780
|
ensureHiZ(w, h) {
|
|
685
781
|
const hw = Math.max(1, Math.ceil(w / HIZ_DOWN)), hh = Math.max(1, Math.ceil(h / HIZ_DOWN));
|
|
@@ -696,7 +792,7 @@ export class GpuShadowRenderer {
|
|
|
696
792
|
this.hizView = this.hizTex.createView();
|
|
697
793
|
this.occView = this.hizView; // the cull now samples the real Hi-Z
|
|
698
794
|
this.camCullBG = this.cullBG(this.camArgs, this.camVis, this.camCull);
|
|
699
|
-
this.lightCullBG = this.
|
|
795
|
+
this.lightCullBG = this.lightArgs.map((a, i) => this.cullBG(a, this.lightVis[i], this.lightCull[i]));
|
|
700
796
|
}
|
|
701
797
|
scratch = new Float32Array(0);
|
|
702
798
|
fill(scratch, instances, n) {
|
|
@@ -721,21 +817,25 @@ export class GpuShadowRenderer {
|
|
|
721
817
|
}
|
|
722
818
|
}
|
|
723
819
|
makeBindGroups() {
|
|
820
|
+
// A caller can setScene before the first frame has said how many cascades it wants; default to
|
|
821
|
+
// one, which is the old single-map behaviour, and frame() will re-allocate if it asks for more.
|
|
822
|
+
if (!this.shadowTex)
|
|
823
|
+
this.allocCascades(1);
|
|
724
824
|
this.makeCullResources(); // (re)create the cull buffers against the fresh instBuf/cap first — the draw passes bind their visible lists
|
|
725
|
-
this.
|
|
825
|
+
this.shadowBGs = this.lightVis.map((vis, i) => this.device.createBindGroup({
|
|
726
826
|
layout: this.shadowBGL,
|
|
727
827
|
entries: [
|
|
728
|
-
{ binding: 0, resource: { buffer: this.
|
|
828
|
+
{ binding: 0, resource: { buffer: this.shadowCamBufs[i] } },
|
|
729
829
|
{ binding: 1, resource: { buffer: this.instBuf } },
|
|
730
|
-
{ binding: 2, resource: { buffer:
|
|
830
|
+
{ binding: 2, resource: { buffer: vis } },
|
|
731
831
|
],
|
|
732
|
-
});
|
|
832
|
+
}));
|
|
733
833
|
this.mainBG = this.device.createBindGroup({
|
|
734
834
|
layout: this.mainBGL,
|
|
735
835
|
entries: [
|
|
736
836
|
{ binding: 0, resource: { buffer: this.camBuf } },
|
|
737
837
|
{ binding: 1, resource: { buffer: this.instBuf } },
|
|
738
|
-
{ binding: 2, resource: this.
|
|
838
|
+
{ binding: 2, resource: this.shadowArrayView },
|
|
739
839
|
{ binding: 3, resource: this.shadowSampler },
|
|
740
840
|
{ binding: 4, resource: { buffer: this.camVis } },
|
|
741
841
|
{ binding: 5, resource: this.atlasView },
|
|
@@ -781,6 +881,7 @@ export class GpuShadowRenderer {
|
|
|
781
881
|
this.cap = this.count;
|
|
782
882
|
this.scratch = new Float32Array(this.count * INST_F);
|
|
783
883
|
this.fill(this.scratch, instances, this.count);
|
|
884
|
+
this.sceneReady = true;
|
|
784
885
|
this.instBuf = this.device.createBuffer({
|
|
785
886
|
size: this.scratch.byteLength,
|
|
786
887
|
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
@@ -792,6 +893,7 @@ export class GpuShadowRenderer {
|
|
|
792
893
|
setCapacity(cap) {
|
|
793
894
|
this.cap = cap;
|
|
794
895
|
this.scratch = new Float32Array(cap * INST_F);
|
|
896
|
+
this.sceneReady = true;
|
|
795
897
|
this.instBuf = this.device.createBuffer({
|
|
796
898
|
size: this.scratch.byteLength,
|
|
797
899
|
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
@@ -886,10 +988,26 @@ export class GpuShadowRenderer {
|
|
|
886
988
|
}
|
|
887
989
|
/** The sun shadow-map depth view + its comparison sampler + texel size. Feed these into GpuPropsRenderer/
|
|
888
990
|
* GpuSkinRenderer.frame(lightVP, …) so mesh props & creatures RECEIVE the same sun shadow the cubes sample.
|
|
889
|
-
*
|
|
991
|
+
*
|
|
992
|
+
* CASCADES: this is a plain 2d view of layer 0 — the TIGHTEST cascade. Props and skinned meshes
|
|
993
|
+
* sample a `texture_depth_2d`, so handing them the array would be a breaking change to two more
|
|
994
|
+
* shaders; layer 0 is what they already effectively received when there was one map, so they are
|
|
995
|
+
* unaffected. The consequence is that a distant prop receives no shadow even when the terrain
|
|
996
|
+
* around it does. Making those two cascade-aware is the follow-up.
|
|
997
|
+
*
|
|
998
|
+
* NOTE: recreated when the cascade COUNT changes, so re-read it after a frame that changes it
|
|
999
|
+
* rather than caching the view across such a change. */
|
|
890
1000
|
get shadowMapView() {
|
|
891
1001
|
return this.shadowView;
|
|
892
1002
|
}
|
|
1003
|
+
/** The full cascade array view, for a consumer that samples `texture_depth_2d_array`. */
|
|
1004
|
+
get shadowMapArrayView() {
|
|
1005
|
+
return this.shadowArrayView;
|
|
1006
|
+
}
|
|
1007
|
+
/** How many cascade layers are currently allocated. */
|
|
1008
|
+
get cascadeCount() {
|
|
1009
|
+
return this.cascades;
|
|
1010
|
+
}
|
|
893
1011
|
get shadowMapSampler() {
|
|
894
1012
|
return this.shadowSampler;
|
|
895
1013
|
}
|
|
@@ -900,26 +1018,41 @@ export class GpuShadowRenderer {
|
|
|
900
1018
|
* its size) to enable Hi-Z occlusion culling on the camera pass — the renderer rebuilds a coarse Hi-Z from that
|
|
901
1019
|
* depth each frame and the NEXT frame's cull uses it (hole-free: the first frame + any resize skip occlusion). */
|
|
902
1020
|
frame(enc, colorView, sceneDepth, camVP, lightVP, sunDir, sunCol, ambient, load = false, occlusion, tint = [1, 1, 1, 0], extraShadow, up = [0, 1, 0], linearOut = false) {
|
|
1021
|
+
// One matrix or many — a single Float32Array is the 1-cascade case and stays byte-identical.
|
|
1022
|
+
const mats = Array.isArray(lightVP) ? lightVP : [lightVP];
|
|
1023
|
+
const nc = Math.max(1, Math.min(MAX_CASCADES, mats.length));
|
|
1024
|
+
this.ensureCascades(nc);
|
|
1025
|
+
const L = 16; // lightVP[0] starts here
|
|
1026
|
+
const S = 16 + MAX_CASCADES * 16; // ...and the scalars start after the whole array
|
|
903
1027
|
this.cam.set(camVP, 0);
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
this.cam[
|
|
907
|
-
this.cam[
|
|
908
|
-
this.cam[
|
|
909
|
-
this.cam[
|
|
910
|
-
this.cam[
|
|
911
|
-
this.cam[
|
|
912
|
-
this.cam[
|
|
913
|
-
this.cam[
|
|
914
|
-
this.cam[
|
|
915
|
-
this.cam[
|
|
916
|
-
this.cam[
|
|
917
|
-
this.cam[
|
|
918
|
-
this.cam[
|
|
919
|
-
this.cam[
|
|
920
|
-
this.cam[
|
|
921
|
-
this.cam[
|
|
1028
|
+
for (let i = 0; i < nc; i++)
|
|
1029
|
+
this.cam.set(mats[i], L + i * 16);
|
|
1030
|
+
this.cam[S + 0] = sunDir[0];
|
|
1031
|
+
this.cam[S + 1] = sunDir[1];
|
|
1032
|
+
this.cam[S + 2] = sunDir[2];
|
|
1033
|
+
this.cam[S + 3] = ambient;
|
|
1034
|
+
this.cam[S + 4] = sunCol[0];
|
|
1035
|
+
this.cam[S + 5] = sunCol[1];
|
|
1036
|
+
this.cam[S + 6] = sunCol[2];
|
|
1037
|
+
this.cam[S + 7] = SHADOW_SIZE;
|
|
1038
|
+
this.cam[S + 8] = tint[0];
|
|
1039
|
+
this.cam[S + 9] = tint[1];
|
|
1040
|
+
this.cam[S + 10] = tint[2];
|
|
1041
|
+
this.cam[S + 11] = tint[3];
|
|
1042
|
+
this.cam[S + 12] = this.useAtlas;
|
|
1043
|
+
this.cam[S + 16] = up[0];
|
|
1044
|
+
this.cam[S + 17] = up[1];
|
|
1045
|
+
this.cam[S + 18] = up[2];
|
|
1046
|
+
this.cam[S + 19] = linearOut ? 1 : 0; // up.w = linear-HDR output flag
|
|
1047
|
+
this.cam[S + 20] = nc; // csm.x = active cascade count
|
|
1048
|
+
this.cam[S + 21] = nc > 1 ? this.cascadeBand : 0; // csm.y = crossfade band (pointless with one box)
|
|
922
1049
|
this.device.queue.writeBuffer(this.camBuf, 0, this.cam);
|
|
1050
|
+
// Each cascade's shadow pass reads its OWN copy with that cascade's matrix parked at lightVP[0].
|
|
1051
|
+
for (let i = 0; i < nc; i++) {
|
|
1052
|
+
this.shadowCam.set(this.cam);
|
|
1053
|
+
this.shadowCam.set(mats[i], L);
|
|
1054
|
+
this.device.queue.writeBuffer(this.shadowCamBufs[i], 0, this.shadowCam);
|
|
1055
|
+
}
|
|
923
1056
|
if (occlusion)
|
|
924
1057
|
this.ensureHiZ(occlusion.w, occlusion.h);
|
|
925
1058
|
const occludeNow = !!occlusion && this.hizReady; // only once a Hi-Z from a prior frame exists (never on frame 1/resize)
|
|
@@ -928,34 +1061,42 @@ export class GpuShadowRenderer {
|
|
|
928
1061
|
// compacts its survivors into a visible list + counts them in instanceCount, which the matching drawIndirect consumes.
|
|
929
1062
|
const resetArgs = new Uint32Array([36, 0, 0, 0]);
|
|
930
1063
|
this.device.queue.writeBuffer(this.camArgs, 0, resetArgs);
|
|
931
|
-
|
|
1064
|
+
for (const a of this.lightArgs)
|
|
1065
|
+
this.device.queue.writeBuffer(a, 0, resetArgs);
|
|
932
1066
|
this.writeCull(this.camCull, camVP, occludeNow);
|
|
933
|
-
|
|
1067
|
+
for (let i = 0; i < nc; i++)
|
|
1068
|
+
this.writeCull(this.lightCull[i], mats[i], false);
|
|
934
1069
|
if (this.count > 0) {
|
|
935
1070
|
const cp = enc.beginComputePass();
|
|
936
1071
|
cp.setPipeline(this.cullPipe);
|
|
937
1072
|
cp.setBindGroup(0, this.camCullBG);
|
|
938
1073
|
cp.dispatchWorkgroups(Math.ceil(this.count / CULL_WG));
|
|
939
|
-
|
|
940
|
-
|
|
1074
|
+
for (let i = 0; i < nc; i++) {
|
|
1075
|
+
cp.setBindGroup(0, this.lightCullBG[i]);
|
|
1076
|
+
cp.dispatchWorkgroups(Math.ceil(this.count / CULL_WG));
|
|
1077
|
+
}
|
|
941
1078
|
cp.end();
|
|
942
1079
|
}
|
|
943
|
-
// Pass 1: occluder depth from the sun
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
1080
|
+
// Pass 1: occluder depth from the sun — one pass per cascade, into its own array layer.
|
|
1081
|
+
for (let i = 0; i < nc; i++) {
|
|
1082
|
+
const sp = enc.beginRenderPass({
|
|
1083
|
+
colorAttachments: [],
|
|
1084
|
+
depthStencilAttachment: {
|
|
1085
|
+
view: this.shadowLayerViews[i],
|
|
1086
|
+
depthClearValue: 1.0,
|
|
1087
|
+
depthLoadOp: 'clear',
|
|
1088
|
+
depthStoreOp: 'store',
|
|
1089
|
+
},
|
|
1090
|
+
});
|
|
1091
|
+
sp.setPipeline(this.shadowPipe);
|
|
1092
|
+
sp.setBindGroup(0, this.shadowBGs[i]);
|
|
1093
|
+
sp.drawIndirect(this.lightArgs[i], 0);
|
|
1094
|
+
// The app casts its own occluders (mesh props/trees) into EVERY cascade — an occluder missing
|
|
1095
|
+
// from a far cascade would leave a tree lit at distance while its neighbours are shadowed.
|
|
1096
|
+
if (extraShadow)
|
|
1097
|
+
extraShadow(sp, mats[i]);
|
|
1098
|
+
sp.end();
|
|
1099
|
+
}
|
|
959
1100
|
// Pass 2: camera, shadow-sampled (camera-frustum + Hi-Z-occlusion-culled).
|
|
960
1101
|
const mp = enc.beginRenderPass({
|
|
961
1102
|
colorAttachments: [
|
|
@@ -1012,7 +1153,7 @@ export class GpuShadowRenderer {
|
|
|
1012
1153
|
async readCullStats() {
|
|
1013
1154
|
const enc = this.device.createCommandEncoder();
|
|
1014
1155
|
enc.copyBufferToBuffer(this.camArgs, 0, this.statsBuf, 0, 16); // args[1] = instanceCount (the visible count)
|
|
1015
|
-
enc.copyBufferToBuffer(this.lightArgs, 0, this.statsBuf, 16, 16);
|
|
1156
|
+
enc.copyBufferToBuffer(this.lightArgs[0], 0, this.statsBuf, 16, 16);
|
|
1016
1157
|
this.device.queue.submit([enc.finish()]);
|
|
1017
1158
|
await this.statsBuf.mapAsync(GPUMapMode.READ);
|
|
1018
1159
|
const a = new Uint32Array(this.statsBuf.getMappedRange().slice(0));
|