sgl-2d 0.4.2

Caller-driven 2D rendering, assets, text, lighting, and immediate UI for SGL games
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
//! 2D lighting (R-6, PR-4/AR-6): the [`LightFrame`] seam + the GPU light
//! passes, replicating Godot's additive 2D light model.
//!
//! **Model** (PR-4, Godot "add" mode): the scene renders full-bright into an
//! albedo target; each point light *adds* `cookie × color × energy ×
//! normal_factor × shadow × coverage` on top; the composite is
//! `albedo × (canvas_modulate + Σ lights)` — i.e. the base scene modulated by
//! the canvas color plus every light's contribution multiplied by the base
//! albedo (exactly Godot's `base·modulate + Σ light·base`). With the default
//! white modulate and no lights this is the identity, so unlit scenes (menus,
//! demos) render unchanged.
//!
//! **Color space** (R-13 parity, D-11): Godot blends 2D in sRGB ("gamma")
//! space, so by default the light math here stays in gamma space too —
//! cookies are sampled raw (non-sRGB texture format), light colors and
//! `energy` upload as given (sRGB), and the accumulation target holds
//! gamma-space contributions the composite multiplies straight into the
//! gamma albedo. Under `LightingSpace::Linear` (`Renderer::with_lighting`)
//! this pass is *unchanged*: colors and `energy` still upload as given —
//! now meaning linear — and the composite multiplies the accumulation into a
//! linear albedo before encoding once. Cookies stay raw data in both modes.
//!
//! **Mechanisms**:
//! - **Cookies**: a light with `cookie: Some` is a texture
//!   (`assets/lights/*.png`) whose world footprint is `texture_size ×
//!   texture_scale`, centered on the light (lights live in world space and
//!   move with the camera). The cookie's intensity is `rgb × alpha` (the
//!   shipped cookies carry the falloff in alpha over a constant color).
//! - **Analytic lights**: a light with `cookie: None` has a `±radius`
//!   footprint and the `bevy_light_2d` radial intensity
//!   ([`analytic_attenuation`]: `(1 − s²)² / (1 + falloff·s²)`,
//!   `s = dist / radius`). Both kinds run through the one light pipeline:
//!   the shader computes both intensities and selects by the light's
//!   `radius` (0 = cookie), with a shared 1×1 white dummy cookie bound for
//!   analytic lights so the cookie bind group is always valid.
//! - **Normal mapping**: the scene pass writes a screen-space normal target
//!   (see `sprite.wgsl`); the light pass computes
//!   `max(dot(N, normalize(light_pos - frag, height)), 0)` and blends it to
//!   `1` where no normal map was written (Godot: sprites without a normal map
//!   are lit at full strength).
//! - **Light layers**: sprites carry a `light_mask` (layers 1/2/4 → the mask
//!   target's r/g/b); each light carries an `item_mask`; the light only
//!   contributes where the masks intersect ([`coverage`]). Alpha-blended
//!   sprites leave *fractional* coverage — a soft per-pixel approximation of
//!   Godot's per-item masking.
//! - **Shadows**: CPU-extruded shadow geometry (AR-6, Godot-style). For each
//!   occluder polygon edge, a quad (capped by a far vertex along the edge's
//!   normal when the edge spans more than 90° from the light) is extruded away from
//!   the light past its footprint ([`shadow_triangles`]); the union over
//!   all edges darkens everything *behind* the first surface seen from the
//!   light — matching Godot's 1D shadow map occlusion shape. Per shadow-casting light the triangles render
//!   into a shared R8 mask target (white = lit, black = shadow), which the
//!   light pass samples with a 3×3 box tap for a soft, PCF-ish edge
//!   (approximating Godot's `filter smooth 5.0`).
//!
//! **Budget**: up to [`MAX_LIGHTS`] lights per frame after view culling;
//! shadows on the first [`MAX_SHADOW_LIGHTS`] shadow-casting lights (extras
//! render unshadowed). Shadow vertices across those lights share one buffer,
//! capped by the device's `max_buffer_size` and u32 draw ranges; surplus
//! occluder edges are dropped whole in light/occluder order.

use std::collections::HashMap;

use glam::{Mat4, Vec2, Vec3, Vec4};
use wgpu::util::DeviceExt;

mod shadows;
pub use shadows::shadow_triangles;
use shadows::{ShadowVertex, grown_shadow_capacity, max_shadow_vertices, shadow_triangles_limited};

use crate::assets::{Handle, Texture};
use crate::canvas::camera::WorldUnits;
use crate::canvas::sprite::TextureError;

/// Max active lights per frame (after view culling); extras are dropped.
pub const MAX_LIGHTS: usize = 32;

/// Max shadow-casting lights per frame; extra shadowed lights render without
/// shadows (PR-4 keeps in-game shadow lights well under this).
pub const MAX_SHADOW_LIGHTS: usize = 8;

/// Minimum shadow extrusion length in world pixels — longer than any cookie
/// footprint (the largest cookie is 512 px at `texture_scale` 3.0 in the
/// menu). Divided by the camera's `pixels_per_unit` so it is the same screen
/// length in a world-unit camera. Lights with a larger footprint (a big
/// analytic `radius`) extrude to twice their half-extent instead, so shadows
/// always cover the footprint ([`shadow_triangles`]).
const SHADOW_FAR: f32 = 4096.0;

/// The light-accumulation target format: float so overlapping lights can sum
/// past 1.0 before the composite clamps (Godot's additive behavior).
pub const ACCUM_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba16Float;

/// The per-light shadow mask format (1 = lit, 0 = shadowed).
pub const SHADOW_MASK_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::R8Unorm;

/// One additive point light (the Godot `Light2D` in "add" mode, PR-4).
///
/// Two footprint/intensity models share the one light pipeline:
/// - **cookie** (`cookie: Some`): footprint = cookie pixel size ×
///   `texture_scale`; intensity = the sampled `cookie.rgb × cookie.a`;
/// - **analytic** (`cookie: None`): footprint = `±radius` world pixels;
///   intensity = [`analytic_attenuation`] of the distance to `pos`.
///
/// Either way the contribution is `intensity × color × energy ×
/// normal_factor × coverage × shadow`.
#[derive(Debug, Clone, Copy)]
pub struct PointLight {
    /// Light position in the world camera's units (logical pixels by
    /// default; world units under `Camera::with_units`).
    pub pos: Vec2,
    /// The cookie texture (must be registered via
    /// `Renderer::upload_light_cookie`; unknown cookies skip the light), or
    /// `None` for an analytic light shaped by `radius` / `falloff`.
    pub cookie: Option<Handle<Texture>>,
    /// Analytic lights only: the footprint half-extent in the world camera's
    /// units (pixels by default; no `pixels_per_unit` scaling applies) — the
    /// light reaches exactly `radius` from `pos`. Must be finite and `> 0`;
    /// any other value skips the light. Ignored by cookie lights.
    pub radius: f32,
    /// Analytic lights only: the falloff steepness of
    /// [`analytic_attenuation`] (`0` = the bare `(1 − s²)²` bell; shadow-sp's
    /// player glow uses 72). Must be finite and `≥ 0`; any other value skips
    /// the light. Ignored by cookie lights.
    pub falloff: f32,
    /// Light color, used as given: sRGB like Godot's `color` property under
    /// the default gamma lighting, linear under `LightingSpace::Linear`.
    pub color: [f32; 3],
    /// Godot `energy`: linear multiplier on the contribution (may exceed 1).
    pub energy: f32,
    /// Godot `texture_scale`: scales the cookie's world footprint
    /// (`cookie_px × texture_scale` pixels = that over `pixels_per_unit`
    /// units; see [`cookie_half_extent`]). Ignored by analytic lights.
    pub texture_scale: f32,
    /// Godot `height`: the light's z for normal-map shading, in the world
    /// camera's units (Godot's 128 px is 4.0 in a 32 px/unit camera).
    pub height: f32,
    /// Which sprite light-mask layers this light affects (bits 1|2|4 —
    /// Godot `range_item_cull_mask`).
    pub item_mask: u32,
    /// Whether occluder polygons cast shadows from this light.
    pub shadows: bool,
}

impl PointLight {
    /// A white light of `cookie` at `pos` with Godot-ish defaults: energy 1,
    /// `texture_scale` 1, height 128, item mask layer 1, no shadows.
    pub fn new(cookie: Handle<Texture>, pos: Vec2) -> Self {
        Self {
            pos,
            cookie: Some(cookie),
            radius: 0.0,
            falloff: 0.0,
            color: [1.0, 1.0, 1.0],
            energy: 1.0,
            texture_scale: 1.0,
            height: 128.0,
            item_mask: 1,
            shadows: false,
        }
    }

    /// A white analytic (cookie-less) light at `pos` reaching `radius` world
    /// pixels with the given `falloff` (see [`analytic_attenuation`]); the
    /// other fields take the same defaults as [`new`](Self::new).
    pub fn analytic(pos: Vec2, radius: f32, falloff: f32) -> Self {
        Self {
            pos,
            cookie: None,
            radius,
            falloff,
            color: [1.0, 1.0, 1.0],
            energy: 1.0,
            texture_scale: 1.0,
            height: 128.0,
            item_mask: 1,
            shadows: false,
        }
    }
}

/// A frame's worth of lighting — the game→renderer seam next to `DrawList`.
///
/// The default (white modulate, no lights, no occluders) is the identity:
/// `Renderer::render` uses it so unlit callers are untouched.
#[derive(Debug, Clone)]
pub struct LightFrame {
    /// Godot `CanvasModulate`: multiplies the base scene before lights add.
    /// Used as given — sRGB under the default gamma lighting (the in-game
    /// scene has none = white, the menu uses 0.0824 gray), linear under
    /// `LightingSpace::Linear` (shadow-sp's linear ambient × brightness).
    pub canvas_modulate: [f32; 4],
    /// The frame's point lights, world space.
    pub lights: Vec<PointLight>,
    /// Shadow-occluder polygons in world space (PR-3 wall occluders). Static
    /// per level — callers may fill this once and reuse the frame.
    pub occluders: Vec<Vec<Vec2>>,
}

impl Default for LightFrame {
    fn default() -> Self {
        Self {
            canvas_modulate: [1.0, 1.0, 1.0, 1.0],
            lights: Vec::new(),
            occluders: Vec::new(),
        }
    }
}

// --- Pure light math (the shaders' reference implementations) ---------------

/// Whether an analytic light's shape parameters are usable: a finite,
/// positive `radius` and a finite, non-negative `falloff` (a negative
/// falloff would put a pole of the attenuation inside the footprint).
/// [`LightPass::prepare`] skips lights that fail this instead of panicking
/// or uploading NaNs.
#[must_use]
pub fn analytic_params_valid(radius: f32, falloff: f32) -> bool {
    radius.is_finite() && radius > 0.0 && falloff.is_finite() && falloff >= 0.0
}

/// The analytic point-light intensity `dist` world pixels from the light
/// (`bevy_light_2d` semantics — shadow-sp's `REFERENCE_PORT` §6.4): with
/// `s = dist / radius`, `(1 − s²)² / (1 + falloff·s²)` for `s ≤ 1` and `0`
/// beyond the radius. Invalid parameters ([`analytic_params_valid`]) give
/// `0`. Mirrors `fs_light` in `light.wgsl`, which evaluates the same
/// expression on the light quad's normalized offset.
#[must_use]
pub fn analytic_attenuation(dist: f32, radius: f32, falloff: f32) -> f32 {
    if !analytic_params_valid(radius, falloff) {
        return 0.0;
    }
    let s = dist / radius;
    let s2 = s * s;
    let bell = (1.0 - s2).max(0.0);
    bell * bell / (1.0 + falloff * s2)
}

/// The PR-4 normal-shading factor: `max(dot(N, L), 0)` with
/// `L = normalize(light_pos - frag_pos, height)`. `normal` is the decoded
/// screen-space normal (y-down), so `light_pos`/`frag_pos` are y-down too
/// (under a y-up camera the shader negates the y delta first); a degenerate
/// normal (length ≈ 0, the "no normal map" encoding) returns 1 — Godot
/// lights unmapped sprites at full strength. Mirrors `fs_light` in
/// `light.wgsl`.
#[must_use]
pub fn normal_factor(light_pos: Vec2, height: f32, frag_pos: Vec2, normal: [f32; 3]) -> f32 {
    let n = Vec3::from_array(normal);
    let n_len = n.length();
    if n_len < 1e-3 {
        return 1.0;
    }
    let l = Vec3::new(light_pos.x - frag_pos.x, light_pos.y - frag_pos.y, height);
    let l_len = l.length();
    if l_len < 1e-6 {
        return 1.0;
    }
    ((n / n_len).dot(l / l_len)).max(0.0)
}

/// A cookie's half footprint in the camera's units: `cookie_px ×
/// texture_scale / 2` pixels over `pixels_per_unit` (1 under the pixel
/// convention, where `x / 1.0 == x` exactly).
#[must_use]
pub fn cookie_half_extent(cookie_px: Vec2, texture_scale: f32, pixels_per_unit: f32) -> Vec2 {
    cookie_px * texture_scale * 0.5 / pixels_per_unit
}

/// Split a light-layer mask into the mask target's r/g/b channels
/// (layers 1, 2, 4 — the only layers the game uses, PR-4). Higher bits are
/// ignored.
#[must_use]
pub fn mask_bits(mask: u32) -> [f32; 3] {
    [
        if mask & 1 != 0 { 1.0 } else { 0.0 },
        if mask & 2 != 0 { 1.0 } else { 0.0 },
        if mask & 4 != 0 { 1.0 } else { 0.0 },
    ]
}

/// Whether a sprite `light_mask` intersects a light `item_mask` on the three
/// supported layers.
#[must_use]
pub fn mask_matches(light_mask: u32, item_mask: u32) -> bool {
    light_mask & item_mask & 0b111 != 0
}

/// Per-pixel receiver coverage: the mask target's rgb (fractional layer
/// coverage) dotted with the light's layer selectors, clamped to 1. Mirrors
/// `fs_light`.
#[must_use]
pub fn coverage(mask_rgb: [f32; 3], item_mask: u32) -> f32 {
    let bits = mask_bits(item_mask);
    (mask_rgb[0] * bits[0] + mask_rgb[1] * bits[1] + mask_rgb[2] * bits[2]).clamp(0.0, 1.0)
}

// --- GPU light pass ----------------------------------------------------------

/// Per-light GPU record (matches `Light` in `light.wgsl`).
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct LightRaw {
    /// Light position xy + half footprint zw, in the camera's units.
    pos_half: [f32; 4],
    /// Light color rgb (as authored, see the module docs), energy in w.
    color_energy: [f32; 4],
    /// x = height, y = shadow flag (0/1), z = analytic radius (0 = cookie
    /// light), w = analytic falloff.
    params: [f32; 4],
    /// rgb = item-mask layer selectors, w pad.
    mask: [f32; 4],
}

impl LightRaw {
    const ZERO: Self = Self {
        pos_half: [0.0; 4],
        color_energy: [0.0; 4],
        params: [0.0; 4],
        mask: [0.0; 4],
    };
}

/// The light-pass uniform (matches `Uniforms` in `light.wgsl`).
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
struct LightsUniform {
    view_proj: [[f32; 4]; 4],
    /// Logical target size (w, h); z = the world → screen-down y sign
    /// (`+1` y-down pixels, `-1` a y-up camera); w pad.
    screen: [f32; 4],
    lights: [LightRaw; MAX_LIGHTS],
}

/// One uploaded cookie texture: its bind group + pixel size.
struct Cookie {
    bind_group: wgpu::BindGroup,
    size: Vec2,
}

/// One planned light draw for this frame.
struct LightDraw {
    /// Index into the uniform's light array (= the draw's first instance).
    index: u32,
    /// The cookie to bind; `None` binds the shared white dummy (analytic).
    cookie: Option<Handle<Texture>>,
    /// Shadow-geometry vertex range in the shadow VB, when shadowed.
    shadow: Option<std::ops::Range<u32>>,
}

/// Owns the light-accumulation target, the shared shadow-mask target, the
/// per-light additive pipeline and the shadow-extrusion pipeline.
pub struct LightPass {
    /// The accumulation texture (`COPY_SRC` so it can be read back).
    accum_texture: wgpu::Texture,
    accum_view: wgpu::TextureView,
    shadow_mask_view: wgpu::TextureView,

    light_pipeline: wgpu::RenderPipeline,
    shadow_pipeline: wgpu::RenderPipeline,
    uniform: wgpu::Buffer,
    /// Layout of `frame_bind_group` (kept for [`LightPass::resize`]).
    frame_bgl: wgpu::BindGroupLayout,
    /// Uniform + normal/mask/shadow-mask textures (light pass, group 0).
    frame_bind_group: wgpu::BindGroup,
    /// Uniform only (shadow-mask pass — it renders *into* the shadow mask,
    /// so that texture cannot be bound there).
    shadow_bind_group: wgpu::BindGroup,

    cookie_bgl: wgpu::BindGroupLayout,
    cookie_sampler: wgpu::Sampler,
    cookies: HashMap<Handle<Texture>, Cookie>,
    /// A 1×1 opaque-white cookie bound for analytic lights, so group 1 is
    /// always valid and the pipeline never branches on light kind.
    analytic_bind_group: wgpu::BindGroup,

    shadow_vb: wgpu::Buffer,
    shadow_capacity: u64,

    // Per-frame scratch (allocations persist).
    verts: Vec<ShadowVertex>,
    plan: Vec<LightDraw>,
}

const INITIAL_SHADOW_VERTS: u64 = 1024;

/// Create the light pass's accumulation texture and shadow-mask view at the
/// given pixel size.
fn light_targets(
    device: &wgpu::Device,
    width: u32,
    height: u32,
) -> (wgpu::Texture, wgpu::TextureView) {
    let target = |label: &str, format: wgpu::TextureFormat, extra: wgpu::TextureUsages| {
        device.create_texture(&wgpu::TextureDescriptor {
            label: Some(label),
            size: wgpu::Extent3d {
                width,
                height,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            format,
            usage: wgpu::TextureUsages::RENDER_ATTACHMENT
                | wgpu::TextureUsages::TEXTURE_BINDING
                | extra,
            view_formats: &[],
        })
    };
    (
        // Readable (tests / debug capture) like the composited target.
        target(
            "light accumulation target",
            ACCUM_FORMAT,
            wgpu::TextureUsages::COPY_SRC,
        ),
        target(
            "shadow mask target",
            SHADOW_MASK_FORMAT,
            wgpu::TextureUsages::empty(),
        )
        .create_view(&wgpu::TextureViewDescriptor::default()),
    )
}

/// A group-1 (cookie texture + sampler) bind group for the light pipeline.
fn cookie_bind_group(
    device: &wgpu::Device,
    layout: &wgpu::BindGroupLayout,
    sampler: &wgpu::Sampler,
    view: &wgpu::TextureView,
) -> wgpu::BindGroup {
    device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("light cookie bind group"),
        layout,
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: wgpu::BindingResource::TextureView(view),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: wgpu::BindingResource::Sampler(sampler),
            },
        ],
    })
}

/// The light pass's group-0 bind group (uniform + normal/mask/shadow-mask).
fn frame_bind_group(
    device: &wgpu::Device,
    layout: &wgpu::BindGroupLayout,
    uniform: &wgpu::Buffer,
    normal_view: &wgpu::TextureView,
    mask_view: &wgpu::TextureView,
    shadow_mask_view: &wgpu::TextureView,
) -> wgpu::BindGroup {
    device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("light frame bind group"),
        layout,
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: uniform.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: wgpu::BindingResource::TextureView(normal_view),
            },
            wgpu::BindGroupEntry {
                binding: 2,
                resource: wgpu::BindingResource::TextureView(mask_view),
            },
            wgpu::BindGroupEntry {
                binding: 3,
                resource: wgpu::BindingResource::TextureView(shadow_mask_view),
            },
        ],
    })
}

impl LightPass {
    /// Build the light pipelines and targets. `normal_view`/`mask_view` are
    /// the scene pass's MRT outputs at the same target size (game scenes
    /// keep the fixed logical resolution, PR-1; the editor resizes all
    /// offscreen targets together through [`LightPass::resize`]). `queue`
    /// seeds the analytic lights' shared 1×1 white dummy cookie.
    pub fn new(
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        normal_view: &wgpu::TextureView,
        mask_view: &wgpu::TextureView,
        logical_width: u32,
        logical_height: u32,
    ) -> Self {
        let (accum_texture, shadow_mask_view) =
            light_targets(device, logical_width, logical_height);
        let accum_view = accum_texture.create_view(&wgpu::TextureViewDescriptor::default());

        let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
            label: Some("light shader"),
            source: wgpu::ShaderSource::Wgsl(include_str!("light.wgsl").into()),
        });

        let uniform = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("lights uniform"),
            size: size_of::<LightsUniform>() as u64,
            usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        let uniform_entry = wgpu::BindGroupLayoutEntry {
            binding: 0,
            visibility: wgpu::ShaderStages::VERTEX_FRAGMENT,
            ty: wgpu::BindingType::Buffer {
                ty: wgpu::BufferBindingType::Uniform,
                has_dynamic_offset: false,
                min_binding_size: None,
            },
            count: None,
        };
        let tex_entry = |binding: u32| wgpu::BindGroupLayoutEntry {
            binding,
            visibility: wgpu::ShaderStages::FRAGMENT,
            ty: wgpu::BindingType::Texture {
                sample_type: wgpu::TextureSampleType::Float { filterable: true },
                view_dimension: wgpu::TextureViewDimension::D2,
                multisampled: false,
            },
            count: None,
        };

        // Group 0 (light pass): uniform + the three screen-space inputs.
        let frame_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("light frame bgl"),
            entries: &[uniform_entry, tex_entry(1), tex_entry(2), tex_entry(3)],
        });
        let frame_bind_group = frame_bind_group(
            device,
            &frame_bgl,
            &uniform,
            normal_view,
            mask_view,
            &shadow_mask_view,
        );

        // Group 0 (shadow pass): the uniform alone — the shadow mask is this
        // pass's render target and must not be bound.
        let shadow_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("shadow bgl"),
            entries: &[uniform_entry],
        });
        let shadow_bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
            label: Some("shadow bind group"),
            layout: &shadow_bgl,
            entries: &[wgpu::BindGroupEntry {
                binding: 0,
                resource: uniform.as_entire_binding(),
            }],
        });

        // Group 1 (light pass): the cookie. Linear filtering — cookies are
        // smooth gradients drawn scaled (texture_scale), nearest would band.
        let cookie_bgl = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
            label: Some("light cookie bgl"),
            entries: &[
                tex_entry(0),
                wgpu::BindGroupLayoutEntry {
                    binding: 1,
                    visibility: wgpu::ShaderStages::FRAGMENT,
                    ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering),
                    count: None,
                },
            ],
        });
        let cookie_sampler = device.create_sampler(&wgpu::SamplerDescriptor {
            label: Some("light cookie sampler (linear)"),
            address_mode_u: wgpu::AddressMode::ClampToEdge,
            address_mode_v: wgpu::AddressMode::ClampToEdge,
            mag_filter: wgpu::FilterMode::Linear,
            min_filter: wgpu::FilterMode::Linear,
            mipmap_filter: wgpu::MipmapFilterMode::Nearest,
            ..Default::default()
        });
        // Analytic lights bind this opaque-white 1×1 cookie: sampled it is
        // `rgb × a = 1`, and the shader selects the analytic intensity anyway.
        let white = device.create_texture_with_data(
            queue,
            &wgpu::TextureDescriptor {
                label: Some("analytic light dummy cookie (1x1 white)"),
                size: wgpu::Extent3d {
                    width: 1,
                    height: 1,
                    depth_or_array_layers: 1,
                },
                mip_level_count: 1,
                sample_count: 1,
                dimension: wgpu::TextureDimension::D2,
                format: wgpu::TextureFormat::Rgba8Unorm,
                usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
                view_formats: &[],
            },
            wgpu::util::TextureDataOrder::LayerMajor,
            &[255, 255, 255, 255],
        );
        let analytic_bind_group = cookie_bind_group(
            device,
            &cookie_bgl,
            &cookie_sampler,
            &white.create_view(&wgpu::TextureViewDescriptor::default()),
        );

        let light_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("light pipeline layout"),
            bind_group_layouts: &[Some(&frame_bgl), Some(&cookie_bgl)],
            immediate_size: 0,
        });
        let light_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("light pipeline"),
            layout: Some(&light_layout),
            vertex: wgpu::VertexState {
                module: &shader,
                entry_point: Some("vs_light"),
                buffers: &[],
                compilation_options: Default::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &shader,
                entry_point: Some("fs_light"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: ACCUM_FORMAT,
                    // Additive: each light adds its contribution (Godot add).
                    blend: Some(wgpu::BlendState {
                        color: wgpu::BlendComponent {
                            src_factor: wgpu::BlendFactor::One,
                            dst_factor: wgpu::BlendFactor::One,
                            operation: wgpu::BlendOperation::Add,
                        },
                        alpha: wgpu::BlendComponent {
                            src_factor: wgpu::BlendFactor::One,
                            dst_factor: wgpu::BlendFactor::One,
                            operation: wgpu::BlendOperation::Add,
                        },
                    }),
                    write_mask: wgpu::ColorWrites::ALL,
                })],
                compilation_options: Default::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                ..Default::default()
            },
            depth_stencil: None,
            multisample: wgpu::MultisampleState::default(),
            multiview_mask: None,
            cache: None,
        });

        let shadow_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
            label: Some("shadow pipeline layout"),
            bind_group_layouts: &[Some(&shadow_bgl)],
            immediate_size: 0,
        });
        let shadow_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
            label: Some("shadow mask pipeline"),
            layout: Some(&shadow_layout),
            vertex: wgpu::VertexState {
                module: &shader,
                entry_point: Some("vs_shadow"),
                buffers: &[Some(wgpu::VertexBufferLayout {
                    array_stride: size_of::<ShadowVertex>() as u64,
                    step_mode: wgpu::VertexStepMode::Vertex,
                    attributes: &wgpu::vertex_attr_array![0 => Float32x2],
                })],
                compilation_options: Default::default(),
            },
            fragment: Some(wgpu::FragmentState {
                module: &shader,
                entry_point: Some("fs_shadow"),
                targets: &[Some(wgpu::ColorTargetState {
                    format: SHADOW_MASK_FORMAT,
                    blend: None, // shadow fragments overwrite with 0
                    write_mask: wgpu::ColorWrites::ALL,
                })],
                compilation_options: Default::default(),
            }),
            primitive: wgpu::PrimitiveState {
                topology: wgpu::PrimitiveTopology::TriangleList,
                ..Default::default()
            },
            depth_stencil: None,
            multisample: wgpu::MultisampleState::default(),
            multiview_mask: None,
            cache: None,
        });

        let shadow_capacity =
            INITIAL_SHADOW_VERTS.min(max_shadow_vertices(device.limits().max_buffer_size));
        let shadow_vb = device.create_buffer(&wgpu::BufferDescriptor {
            label: Some("shadow vertex buffer"),
            size: shadow_capacity * size_of::<ShadowVertex>() as u64,
            usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
            mapped_at_creation: false,
        });

        Self {
            accum_texture,
            accum_view,
            shadow_mask_view,
            light_pipeline,
            shadow_pipeline,
            uniform,
            frame_bgl,
            frame_bind_group,
            shadow_bind_group,
            cookie_bgl,
            cookie_sampler,
            cookies: HashMap::new(),
            analytic_bind_group,
            shadow_vb,
            shadow_capacity,
            verts: Vec::new(),
            plan: Vec::new(),
        }
    }

    /// Recreate the size-dependent targets (accumulation + shadow mask) at
    /// a new pixel size and rebind them against the caller's freshly
    /// resized MRT views. Pipelines, cookies, and buffers survive — only
    /// the editor's native-res scene path resizes; game scenes stay at the
    /// fixed logical size (PR-1).
    pub fn resize(
        &mut self,
        device: &wgpu::Device,
        normal_view: &wgpu::TextureView,
        mask_view: &wgpu::TextureView,
        width: u32,
        height: u32,
    ) {
        let (accum_texture, shadow_mask_view) = light_targets(device, width, height);
        self.frame_bind_group = frame_bind_group(
            device,
            &self.frame_bgl,
            &self.uniform,
            normal_view,
            mask_view,
            &shadow_mask_view,
        );
        self.accum_view = accum_texture.create_view(&wgpu::TextureViewDescriptor::default());
        self.accum_texture = accum_texture;
        self.shadow_mask_view = shadow_mask_view;
    }

    /// The light-accumulation target view (the composite pass samples it).
    pub fn accum_view(&self) -> &wgpu::TextureView {
        &self.accum_view
    }

    /// The light-accumulation texture (`COPY_SRC`, [`ACCUM_FORMAT`]) for
    /// readback.
    pub fn accum_texture(&self) -> &wgpu::Texture {
        &self.accum_texture
    }

    /// Upload a cookie texture under its asset `handle` (standalone —
    /// cookies are never atlased). Idempotent per handle. Lights referencing
    /// an unregistered cookie are skipped. An invalid texture is refused
    /// with a [`TextureError`].
    pub fn upload_cookie(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        handle: Handle<Texture>,
        tex: &Texture,
    ) -> Result<(), TextureError> {
        TextureError::check(device, tex)?;
        if self.cookies.contains_key(&handle) {
            return Ok(());
        }
        let texture = device.create_texture(&wgpu::TextureDescriptor {
            label: Some("light cookie"),
            size: wgpu::Extent3d {
                width: tex.width,
                height: tex.height,
                depth_or_array_layers: 1,
            },
            mip_level_count: 1,
            sample_count: 1,
            dimension: wgpu::TextureDimension::D2,
            // NOT sRGB: cookies sample raw so the light math runs in gamma
            // space like Godot 2D (see the module docs / D-11).
            format: wgpu::TextureFormat::Rgba8Unorm,
            usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
            view_formats: &[],
        });
        queue.write_texture(
            wgpu::TexelCopyTextureInfo {
                texture: &texture,
                mip_level: 0,
                origin: wgpu::Origin3d::ZERO,
                aspect: wgpu::TextureAspect::All,
            },
            &tex.rgba,
            wgpu::TexelCopyBufferLayout {
                offset: 0,
                bytes_per_row: Some(4 * tex.width),
                rows_per_image: Some(tex.height),
            },
            wgpu::Extent3d {
                width: tex.width,
                height: tex.height,
                depth_or_array_layers: 1,
            },
        );
        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
        let bind_group = cookie_bind_group(device, &self.cookie_bgl, &self.cookie_sampler, &view);
        self.cookies.insert(
            handle,
            Cookie {
                bind_group,
                size: Vec2::new(tex.width as f32, tex.height as f32),
            },
        );
        Ok(())
    }

    /// Plan this frame's lights: cull to the view, cap counts, fill the
    /// uniform, and build the shadow geometry buffer. Positions, footprints,
    /// heights and occluders are in the world camera's `units`. Call once
    /// per frame before [`run`](Self::run). Shadow geometry shares the device's
    /// `max_buffer_size` / u32 draw-range budget across lights. Whole edge
    /// extrusions that do not fit, and subsequent edges, are dropped.
    pub fn prepare(
        &mut self,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        view_proj: Mat4,
        units: WorldUnits,
        frame: &LightFrame,
        logical_size: (u32, u32),
    ) {
        self.plan.clear();
        self.verts.clear();
        let max_vertices = max_shadow_vertices(device.limits().max_buffer_size);

        // The world rectangle the camera shows (ortho: unproject the corners
        // — already in the camera's units, whatever its convention).
        let inv = view_proj.inverse();
        let mut view_min = Vec2::splat(f32::MAX);
        let mut view_max = Vec2::splat(f32::MIN);
        for (cx, cy) in [(-1.0, -1.0), (1.0, -1.0), (-1.0, 1.0), (1.0, 1.0)] {
            let w = inv * Vec4::new(cx, cy, 0.5, 1.0);
            view_min = view_min.min(Vec2::new(w.x, w.y));
            view_max = view_max.max(Vec2::new(w.x, w.y));
        }

        let mut raw = [LightRaw::ZERO; MAX_LIGHTS];
        let mut shadow_count = 0usize;
        let mut shadow_full = false;
        // Two passes over the lights: unshadowed first so `run` can batch
        // them into the single clearing pass (order is irrelevant: additive).
        for want_shadow in [false, true] {
            for light in &frame.lights {
                if self.plan.len() == MAX_LIGHTS {
                    break;
                }
                if light.shadows != want_shadow || light.energy <= 0.0 {
                    continue;
                }
                // The footprint half-extent and the shader's analytic
                // radius/falloff (radius 0 selects the cookie intensity; a
                // cookie light's own `falloff` is ignored and never uploaded).
                let (half, radius, falloff) = if let Some(handle) = light.cookie {
                    let Some(cookie) = self.cookies.get(&handle) else {
                        continue; // Cookie never uploaded — skip, never panic.
                    };
                    (
                        cookie_half_extent(cookie.size, light.texture_scale, units.pixels_per_unit),
                        0.0,
                        0.0,
                    )
                } else {
                    if !analytic_params_valid(light.radius, light.falloff) {
                        continue; // Degenerate shape — skip, never panic.
                    }
                    // `radius` is authored in the camera's units already.
                    (Vec2::splat(light.radius), light.radius, light.falloff)
                };
                if light.pos.x - half.x > view_max.x
                    || light.pos.x + half.x < view_min.x
                    || light.pos.y - half.y > view_max.y
                    || light.pos.y + half.y < view_min.y
                {
                    continue; // Off-screen.
                }

                let shadow = if light.shadows && shadow_count < MAX_SHADOW_LIGHTS && !shadow_full {
                    let start = self.verts.len() as u32;
                    shadow_full = shadow_triangles_limited(
                        light.pos,
                        half,
                        &frame.occluders,
                        // Always extrude past the footprint: an analytic
                        // radius is caller-chosen and may exceed SHADOW_FAR
                        // (in the camera's units).
                        (SHADOW_FAR / units.pixels_per_unit).max(2.0 * half.max_element()),
                        &mut self.verts,
                        max_vertices,
                    );
                    let end = self.verts.len() as u32;
                    if end > start {
                        shadow_count += 1;
                        Some(start..end)
                    } else {
                        None // Nothing to occlude this light.
                    }
                } else {
                    None
                };

                let index = self.plan.len();
                let bits = mask_bits(light.item_mask);
                raw[index] = LightRaw {
                    pos_half: [light.pos.x, light.pos.y, half.x, half.y],
                    // Raw sRGB values: the light math runs in gamma space
                    // like Godot 2D (see the module docs).
                    color_energy: [light.color[0], light.color[1], light.color[2], light.energy],
                    params: [
                        light.height,
                        if shadow.is_some() { 1.0 } else { 0.0 },
                        radius,
                        falloff,
                    ],
                    mask: [bits[0], bits[1], bits[2], 0.0],
                };
                self.plan.push(LightDraw {
                    index: index as u32,
                    cookie: light.cookie,
                    shadow,
                });
            }
        }

        let uniform = LightsUniform {
            view_proj: view_proj.to_cols_array_2d(),
            screen: [
                logical_size.0 as f32,
                logical_size.1 as f32,
                units.screen_y_sign(),
                0.0,
            ],
            lights: raw,
        };
        queue.write_buffer(&self.uniform, 0, bytemuck::cast_slice(&[uniform]));

        if !self.verts.is_empty() {
            let needed = self.verts.len() as u64;
            if needed > self.shadow_capacity {
                let new_cap = grown_shadow_capacity(needed, max_vertices);
                self.shadow_vb = device.create_buffer(&wgpu::BufferDescriptor {
                    label: Some("shadow vertex buffer"),
                    size: new_cap * size_of::<ShadowVertex>() as u64,
                    usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
                    mapped_at_creation: false,
                });
                self.shadow_capacity = new_cap;
            }
            queue.write_buffer(&self.shadow_vb, 0, bytemuck::cast_slice(&self.verts));
        }
    }

    /// The group-1 bind group for a planned draw: its cookie, or the shared
    /// white dummy for analytic lights.
    fn cookie_bind_group_for(&self, draw: &LightDraw) -> &wgpu::BindGroup {
        draw.cookie.map_or(&self.analytic_bind_group, |handle| {
            &self.cookies[&handle].bind_group
        })
    }

    /// Encode the light passes planned by [`prepare`](Self::prepare):
    /// clear the accumulation target and add every unshadowed light, then per
    /// shadowed light re-render the shared shadow mask and add the light.
    /// Shadow vertex-buffer capacity in vertices; grows only past it.
    #[cfg(all(test, not(target_arch = "wasm32")))]
    pub(crate) fn shadow_capacity(&self) -> u64 {
        self.shadow_capacity
    }

    pub fn run(&self, encoder: &mut wgpu::CommandEncoder) {
        // --- Clear + all unshadowed lights in one pass.
        {
            let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("light accumulation pass"),
                color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                    view: &self.accum_view,
                    resolve_target: None,
                    depth_slice: None,
                    ops: wgpu::Operations {
                        load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
                        store: wgpu::StoreOp::Store,
                    },
                })],
                depth_stencil_attachment: None,
                timestamp_writes: None,
                occlusion_query_set: None,
                multiview_mask: None,
            });
            pass.set_pipeline(&self.light_pipeline);
            pass.set_bind_group(0, &self.frame_bind_group, &[]);
            for draw in self.plan.iter().filter(|d| d.shadow.is_none()) {
                pass.set_bind_group(1, self.cookie_bind_group_for(draw), &[]);
                pass.draw(0..6, draw.index..draw.index + 1);
            }
        }

        // --- Shadowed lights: shadow mask pass + additive light pass each.
        for draw in &self.plan {
            let Some(range) = &draw.shadow else { continue };
            {
                let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                    label: Some("shadow mask pass"),
                    color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                        view: &self.shadow_mask_view,
                        resolve_target: None,
                        depth_slice: None,
                        ops: wgpu::Operations {
                            load: wgpu::LoadOp::Clear(wgpu::Color::WHITE),
                            store: wgpu::StoreOp::Store,
                        },
                    })],
                    depth_stencil_attachment: None,
                    timestamp_writes: None,
                    occlusion_query_set: None,
                    multiview_mask: None,
                });
                pass.set_pipeline(&self.shadow_pipeline);
                pass.set_bind_group(0, &self.shadow_bind_group, &[]);
                pass.set_vertex_buffer(0, self.shadow_vb.slice(..));
                pass.draw(range.clone(), 0..1);
            }
            {
                let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                    label: Some("shadowed light pass"),
                    color_attachments: &[Some(wgpu::RenderPassColorAttachment {
                        view: &self.accum_view,
                        resolve_target: None,
                        depth_slice: None,
                        ops: wgpu::Operations {
                            load: wgpu::LoadOp::Load,
                            store: wgpu::StoreOp::Store,
                        },
                    })],
                    depth_stencil_attachment: None,
                    timestamp_writes: None,
                    occlusion_query_set: None,
                    multiview_mask: None,
                });
                pass.set_pipeline(&self.light_pipeline);
                pass.set_bind_group(0, &self.frame_bind_group, &[]);
                pass.set_bind_group(1, self.cookie_bind_group_for(draw), &[]);
                pass.draw(0..6, draw.index..draw.index + 1);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wasm_bindgen_test::wasm_bindgen_test;

    // --- normal_factor -------------------------------------------------------

    /// A light directly above the fragment with a flat +Z normal is full
    /// strength.
    #[wasm_bindgen_test(unsupported = test)]
    fn normal_factor_overhead_flat_is_one() {
        let f = normal_factor(
            Vec2::new(10.0, 10.0),
            128.0,
            Vec2::new(10.0, 10.0),
            [0.0, 0.0, 1.0],
        );
        assert!((f - 1.0).abs() < 1e-6);
    }

    /// Offset by exactly `height` horizontally, a flat normal sees the light
    /// at 45°: factor = cos 45° = √2/2.
    #[wasm_bindgen_test(unsupported = test)]
    fn normal_factor_offset_flat_is_cos45() {
        let f = normal_factor(Vec2::ZERO, 128.0, Vec2::new(128.0, 0.0), [0.0, 0.0, 1.0]);
        assert!((f - std::f32::consts::FRAC_1_SQRT_2).abs() < 1e-5, "{f}");
    }

    /// A normal tilted *toward* the light beats the flat normal; tilted away
    /// clamps at 0.
    #[wasm_bindgen_test(unsupported = test)]
    fn normal_factor_tilt_toward_and_away() {
        let inv = std::f32::consts::FRAC_1_SQRT_2;
        // Light is to the left (-x); fragment normal tilted left = toward it.
        let toward = normal_factor(Vec2::ZERO, 128.0, Vec2::new(128.0, 0.0), [-inv, 0.0, inv]);
        assert!((toward - 1.0).abs() < 1e-5, "{toward}");
        let away = normal_factor(Vec2::ZERO, 1.0, Vec2::new(128.0, 0.0), [inv, 0.0, -inv]);
        assert_eq!(away, 0.0);
    }

    /// The "no normal map" encoding (degenerate normal) lights at full
    /// strength (Godot behavior for unmapped sprites).
    #[wasm_bindgen_test(unsupported = test)]
    fn normal_factor_degenerate_normal_is_one() {
        let f = normal_factor(Vec2::ZERO, 128.0, Vec2::new(500.0, 0.0), [0.0, 0.0, 0.0]);
        assert_eq!(f, 1.0);
    }

    /// Unnormalized normals are normalized before the dot.
    #[wasm_bindgen_test(unsupported = test)]
    fn normal_factor_normalizes_input() {
        let a = normal_factor(Vec2::ZERO, 128.0, Vec2::new(64.0, 32.0), [0.0, 0.0, 1.0]);
        let b = normal_factor(Vec2::ZERO, 128.0, Vec2::new(64.0, 32.0), [0.0, 0.0, 7.5]);
        assert!((a - b).abs() < 1e-6);
    }

    // --- analytic attenuation -------------------------------------------------

    /// Hand-evaluated `(1 − s²)² / (1 + falloff·s²)` (shadow-sp `REFERENCE_PORT` §6.4):
    /// full strength at the center; at `s = 0.5` with falloff 72,
    /// `0.75² / (1 + 18) = 0.5625 / 19 ≈ 0.029605`; zero at and beyond the
    /// radius.
    #[wasm_bindgen_test(unsupported = test)]
    fn analytic_attenuation_matches_hand_values() {
        assert_eq!(analytic_attenuation(0.0, 16.0, 72.0), 1.0);
        let mid = analytic_attenuation(8.0, 16.0, 72.0);
        assert!((mid - 0.029_605).abs() < 1e-5, "{mid}");
        assert_eq!(analytic_attenuation(16.0, 16.0, 72.0), 0.0);
        assert_eq!(analytic_attenuation(20.0, 16.0, 72.0), 0.0);
        // Falloff 0 leaves the bare bell: s = 0.5 → 0.75² = 0.5625.
        assert!((analytic_attenuation(8.0, 16.0, 0.0) - 0.5625).abs() < 1e-6);
        // Radius scales the curve: dist 4 in radius 8 is the same s = 0.5.
        assert_eq!(
            analytic_attenuation(4.0, 8.0, 72.0),
            analytic_attenuation(8.0, 16.0, 72.0)
        );
    }

    /// Degenerate shapes contribute nothing (and never divide by zero).
    #[wasm_bindgen_test(unsupported = test)]
    fn analytic_attenuation_rejects_degenerate_params() {
        assert_eq!(analytic_attenuation(0.0, 0.0, 72.0), 0.0);
        assert_eq!(analytic_attenuation(0.0, -16.0, 72.0), 0.0);
        assert_eq!(analytic_attenuation(0.0, f32::NAN, 72.0), 0.0);
        assert_eq!(analytic_attenuation(0.0, f32::INFINITY, 72.0), 0.0);
        assert_eq!(analytic_attenuation(0.0, 16.0, -1.0), 0.0);
        assert_eq!(analytic_attenuation(0.0, 16.0, f32::NAN), 0.0);
        assert!(!analytic_params_valid(16.0, f32::INFINITY));
        assert!(analytic_params_valid(16.0, 0.0));
    }

    /// A 512 px cookie at `texture_scale` 2 spans 1024 px: a ±512 px
    /// footprint under the pixel convention, ±16 units at 32 px/unit.
    #[wasm_bindgen_test(unsupported = test)]
    fn cookie_half_extent_is_in_camera_units() {
        assert_eq!(
            cookie_half_extent(Vec2::splat(512.0), 2.0, 1.0),
            Vec2::splat(512.0)
        );
        assert_eq!(
            cookie_half_extent(Vec2::splat(512.0), 2.0, 32.0),
            Vec2::splat(16.0)
        );
    }

    // --- masks ---------------------------------------------------------------

    #[wasm_bindgen_test(unsupported = test)]
    fn mask_bits_split_layers() {
        assert_eq!(mask_bits(1), [1.0, 0.0, 0.0]);
        assert_eq!(mask_bits(2), [0.0, 1.0, 0.0]);
        assert_eq!(mask_bits(4), [0.0, 0.0, 1.0]);
        assert_eq!(mask_bits(7), [1.0, 1.0, 1.0]);
        // Layers above 4 are unsupported and ignored.
        assert_eq!(mask_bits(8), [0.0, 0.0, 0.0]);
    }

    #[wasm_bindgen_test(unsupported = test)]
    fn mask_matching_is_bit_intersection() {
        assert!(mask_matches(1, 1));
        assert!(mask_matches(7, 2));
        assert!(!mask_matches(1, 2));
        assert!(!mask_matches(4, 3));
        assert!(!mask_matches(8, 8), "layers above 4 never match");
    }

    /// Coverage dots the per-pixel layer coverage with the light's selectors
    /// and clamps: a layer-2 light ignores layer-1 coverage; multi-layer
    /// matches clamp to 1.
    #[wasm_bindgen_test(unsupported = test)]
    fn coverage_selects_and_clamps() {
        assert_eq!(coverage([1.0, 0.0, 0.0], 2), 0.0);
        assert_eq!(coverage([0.0, 0.75, 0.0], 2), 0.75);
        assert_eq!(coverage([1.0, 1.0, 1.0], 7), 1.0);
        assert_eq!(coverage([0.5, 0.5, 0.0], 3), 1.0);
    }

    /// `LightFrame::default` is the identity: white modulate, nothing else.
    #[wasm_bindgen_test(unsupported = test)]
    fn default_light_frame_is_identity() {
        let frame = LightFrame::default();
        assert_eq!(frame.canvas_modulate, [1.0, 1.0, 1.0, 1.0]);
        assert!(frame.lights.is_empty());
        assert!(frame.occluders.is_empty());
    }
}

/// Headless GPU tests: the real light pipeline over small dummy targets,
/// read back and compared against hand-computed values.
#[cfg(all(test, not(target_arch = "wasm32")))]
mod gpu_tests {
    use super::*;
    use crate::canvas::camera::Camera;
    use crate::canvas::sprite::{MASK_FORMAT, NORMAL_FORMAT};
    use crate::canvas::test_gpu::{device, read_texture, rgba16f_to_f32};

    /// Target side in pixels (`Camera::new(SIZE, SIZE)` maps world pixel
    /// `(x, y)` to target pixel `(x, y)`).
    const SIZE: u32 = 64;

    /// A light pass over a receiver that covers every pixel on layer 1 with
    /// no normal map (flat normals, zero normal weight → `normal_factor` 1).
    struct Rig {
        device: wgpu::Device,
        queue: wgpu::Queue,
        pass: LightPass,
    }

    fn rig() -> Option<Rig> {
        let (device, queue) = device()?;
        Some(rig_on(device, queue))
    }

    fn rig_on(device: wgpu::Device, queue: wgpu::Queue) -> Rig {
        let filled = |label: &str, format: wgpu::TextureFormat, texel: [u8; 4]| {
            device
                .create_texture_with_data(
                    &queue,
                    &wgpu::TextureDescriptor {
                        label: Some(label),
                        size: wgpu::Extent3d {
                            width: SIZE,
                            height: SIZE,
                            depth_or_array_layers: 1,
                        },
                        mip_level_count: 1,
                        sample_count: 1,
                        dimension: wgpu::TextureDimension::D2,
                        format,
                        usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
                        view_formats: &[],
                    },
                    wgpu::util::TextureDataOrder::LayerMajor,
                    &texel.repeat((SIZE * SIZE) as usize),
                )
                .create_view(&wgpu::TextureViewDescriptor::default())
        };
        // Flat +Z normal, weight 0; full layer-1 coverage.
        let normal = filled("test normal target", NORMAL_FORMAT, [128, 128, 255, 0]);
        let mask = filled("test mask target", MASK_FORMAT, [255, 0, 0, 0]);
        let pass = LightPass::new(&device, &queue, &normal, &mask, SIZE, SIZE);
        Rig {
            device,
            queue,
            pass,
        }
    }

    impl Rig {
        /// Plan + run `frame` and read the accumulation target back as
        /// `f32` rgba per pixel.
        fn render(&mut self, frame: &LightFrame) -> Vec<f32> {
            let view_proj = Camera::new(SIZE, SIZE).view_proj();
            self.pass.prepare(
                &self.device,
                &self.queue,
                view_proj,
                WorldUnits::default(),
                frame,
                (SIZE, SIZE),
            );
            let mut encoder = self
                .device
                .create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
            self.pass.run(&mut encoder);
            self.queue.submit(Some(encoder.finish()));
            let bytes = read_texture(
                &self.device,
                &self.queue,
                self.pass.accum_texture(),
                SIZE,
                SIZE,
                8,
            );
            rgba16f_to_f32(&bytes)
        }
    }

    fn rgb(px: &[f32], x: u32, y: u32) -> [f32; 3] {
        let i = ((y * SIZE + x) * 4) as usize;
        [px[i], px[i + 1], px[i + 2]]
    }

    /// #630: exercise the real allocation, upload and draw at an injected
    /// device limit. Validation catches power-of-two growth past the limit;
    /// sampled pixels distinguish retained walls from dropped walls.
    #[test]
    fn shadow_geometry_past_the_device_buffer_limit_is_dropped() {
        if device().is_none() {
            return;
        }
        let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle());
        let adapter =
            pollster::block_on(instance.request_adapter(&wgpu::RequestAdapterOptions::default()))
                .expect("an adapter, as device() found one");
        let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor {
            required_limits: wgpu::Limits {
                // Enough for readback, but 4098 vertices round up past this.
                max_buffer_size: 4099 * 8,
                ..wgpu::Limits::default()
            },
            ..Default::default()
        }))
        .unwrap();
        let validation = device.push_error_scope(wgpu::ErrorFilter::Validation);
        let mut rig = rig_on(device, queue);
        let right_wall = vec![Vec2::new(20.5, 31.0), Vec2::new(20.5, 34.0)];
        let upper_wall = vec![Vec2::new(9.0, 20.5), Vec2::new(12.0, 20.5)];
        let mut occluders = vec![right_wall; 400];
        occluders.push(upper_wall.clone());
        let mut frame = LightFrame {
            lights: vec![PointLight {
                shadows: true,
                ..PointLight::analytic(Vec2::new(10.5, 32.5), 100.0, 0.0)
            }],
            occluders,
            ..LightFrame::default()
        };
        let pixels = rig.render(&frame);
        assert!(
            rgb(&pixels, 40, 32)[0] < 1e-3,
            "retained right wall lost its shadow"
        );
        assert!(
            rgb(&pixels, 10, 10)[0] > 0.5,
            "surplus upper wall cast a shadow"
        );
        // A later frame can use the same buffer for the previously dropped
        // wall, with the old frame's geometry cleared.
        frame.occluders = vec![upper_wall];
        let pixels = rig.render(&frame);
        assert!(
            rgb(&pixels, 10, 10)[0] < 1e-3,
            "upper wall should fit the next frame"
        );
        assert!(
            rgb(&pixels, 40, 32)[0] > 0.5,
            "old right wall survived prepare"
        );
        rig.device
            .poll(wgpu::PollType::wait_indefinitely())
            .unwrap();
        if let Some(error) = pollster::block_on(validation.pop()) {
            panic!("{error}");
        }
    }

    /// #319: an analytic light 2 px above a long wall leaves the pixel
    /// directly behind the wall, inside its radius, unlit, while a pixel as
    /// far away on the light's side is lit.
    #[test]
    fn a_light_close_to_a_long_wall_is_blocked_behind_it() {
        let Some(mut rig) = rig() else { return };
        let frame = LightFrame {
            lights: vec![PointLight {
                shadows: true,
                ..PointLight::analytic(Vec2::new(32.5, 20.5), 40.0, 0.0)
            }],
            occluders: vec![vec![
                Vec2::new(-1000.0, 22.5),
                Vec2::new(1000.0, 22.5),
                Vec2::new(1000.0, 24.5),
                Vec2::new(-1000.0, 24.5),
            ]],
            ..LightFrame::default()
        };
        let px = rig.render(&frame);
        assert_eq!(rgb(&px, 32, 44), [0.0; 3], "behind the wall");
        assert!(rgb(&px, 8, 20)[0] > 0.3, "the light's side is lit");
    }

    /// An analytic light centered on pixel (32, 32) with radius 16 and
    /// falloff 72 accumulates `energy × color × attenuation`: the center
    /// reads the full energy, 8 px out reads `0.029605 × energy`, 12 px out
    /// reads `0.19140625 / 41.5 × energy = 0.0046122 × energy`, the quad's
    /// corner region (inside the drawn square, beyond the radius) reads 0,
    /// and pixels outside the footprint are untouched.
    #[test]
    fn analytic_light_accumulates_the_falloff_curve() {
        let Some(mut rig) = rig() else { return };
        let frame = LightFrame {
            lights: vec![PointLight {
                color: [1.0, 0.5, 0.0],
                energy: 0.75,
                ..PointLight::analytic(Vec2::new(32.5, 32.5), 16.0, 72.0)
            }],
            ..LightFrame::default()
        };
        let px = rig.render(&frame);

        let close = |got: [f32; 3], want: [f32; 3], tol: f32| {
            got.iter().zip(want).all(|(g, w)| (g - w).abs() <= tol)
        };
        let center = rgb(&px, 32, 32);
        assert!(close(center, [0.75, 0.375, 0.0], 2e-3), "center {center:?}");
        let mid = rgb(&px, 40, 32);
        let want = 0.029_605 * 0.75;
        assert!(close(mid, [want, want * 0.5, 0.0], 3e-4), "s=0.5 {mid:?}");
        let far = rgb(&px, 32, 44);
        let want = 0.004_612_2 * 0.75;
        assert!(close(far, [want, want * 0.5, 0.0], 1e-4), "s=0.75 {far:?}");
        // dist = 14√2 ≈ 19.8 > radius, but inside the ±16 quad.
        assert_eq!(rgb(&px, 46, 46), [0.0; 3], "quad corner past the radius");
        assert_eq!(rgb(&px, 32, 52), [0.0; 3], "outside the footprint");
    }

    /// A cookie light is unaffected by the analytic path: a 1×1 cookie of
    /// `rgba(255, 255, 255, 128)` scaled to a 32 px footprint reads
    /// `128/255 × energy` everywhere inside the footprint — including the
    /// corner region an analytic light would leave dark — and 0 outside.
    #[test]
    fn cookie_light_samples_the_cookie_everywhere_in_its_footprint() {
        let Some(mut rig) = rig() else { return };
        let mut assets = crate::assets::Assets::<Texture>::new();
        let handle = assets.insert(
            "cookie.png".into(),
            Texture {
                width: 1,
                height: 1,
                rgba: vec![255, 255, 255, 128],
            },
        );
        // #320: an empty cookie is refused and does not claim the handle,
        // so the valid upload below still registers it.
        let empty = Texture {
            width: 0,
            height: 1,
            rgba: Vec::new(),
        };
        assert_eq!(
            rig.pass
                .upload_cookie(&rig.device, &rig.queue, handle, &empty),
            Err(TextureError::Empty {
                width: 0,
                height: 1
            })
        );
        let tex = assets.get(handle).expect("just inserted");
        rig.pass
            .upload_cookie(&rig.device, &rig.queue, handle, tex)
            .unwrap();
        let frame = LightFrame {
            lights: vec![PointLight {
                texture_scale: 32.0,
                energy: 0.5,
                ..PointLight::new(handle, Vec2::new(32.5, 32.5))
            }],
            ..LightFrame::default()
        };
        let px = rig.render(&frame);
        let want = 128.0 / 255.0 * 0.5;
        for (x, y) in [(32, 32), (40, 32), (46, 46), (17, 17)] {
            let got = rgb(&px, x, y);
            assert!(
                got.iter().all(|c| (c - want).abs() < 2e-3),
                "({x}, {y}) = {got:?}, want {want}"
            );
        }
        assert_eq!(rgb(&px, 32, 52), [0.0; 3], "outside the footprint");
    }

    /// Degenerate analytic shapes and unknown cookies are skipped in
    /// `prepare` — nothing is planned, nothing is drawn, nothing panics.
    #[test]
    fn invalid_lights_are_skipped_without_panicking() {
        let Some(mut rig) = rig() else { return };
        let center = Vec2::new(32.5, 32.5);
        let frame = LightFrame {
            lights: vec![
                PointLight::analytic(center, 0.0, 72.0),
                PointLight::analytic(center, -16.0, 72.0),
                PointLight::analytic(center, f32::NAN, 72.0),
                PointLight::analytic(center, f32::INFINITY, 72.0),
                PointLight::analytic(center, 16.0, -1.0),
                PointLight::analytic(center, 16.0, f32::NAN),
            ],
            ..LightFrame::default()
        };
        let px = rig.render(&frame);
        assert!(rig.pass.plan.is_empty());
        // The pass still clears the target (rgb 0; alpha is the clear's 1).
        assert!(px.chunks_exact(4).all(|p| p[..3] == [0.0; 3]));
    }
}