awsm-renderer 0.3.2

awsm-renderer
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
// compute.wgsl — the opaque MATERIAL kernel (skybox-free; the canonical skybox
// bucket uses skybox_primary.wgsl instead). Shared preamble is factored out.
{% include "material_opaque_wgsl/opaque_kernel_includes.wgsl" %}

{% if prep_present %}
// Plan B (stage 5a/5b): a per-thread context each entry point sets once, that the
// shared texture_uv() / vertex_color() / shadow helpers branch on — so ONE set
// of helpers serves cs_opaque (PRIMARY, reads the prep array at sample-0 coords)
// AND cs_edge (5b-shadow: EDGE reads the compact per-edge-sample shadow buffer;
// UV/vcolor still RECOMPUTE — 5b-attrs deferred) AND the non-prep path, with no
// forked MSAA copies.
//   PRIMARY   — cs_opaque: shadow source = full-screen prep_shadow_visibility at
//               `coords` (sample 0).
//   EDGE      — cs_edge per sample: shadow source = compact prep_edge_shadow at
//               `edge_shadow_xy` (= the 2D texel for this edge_pixel × sample);
//               UV/vcolor recompute (5b-attrs deferred).
//   RECOMPUTE — non-prep / fallback: inline-sample shadows + recompute attrs.
const PREP_MODE_RECOMPUTE: u32 = 0u;
const PREP_MODE_PRIMARY: u32 = 1u;
const PREP_MODE_EDGE: u32 = 2u;
struct PrepReadContext { mode: u32, coords: vec2<i32>, edge_shadow_xy: vec2<i32> }
var<private> g_prep_ctx: PrepReadContext;
{% if multisampled_geometry %}
// Stage 5b-shadow: the compact per-edge-sample shadow buffer key. MUST match
// material_prep's cs_prep_edge: `idx = edge_pixel_id * MAX_EDGE_SHADOW_SAMPLES +
// sample`, 2D coords `(idx % W, idx / W)`; layer = slot/4 (read in apply_lighting).
const PREP_EDGE_SHADOW_SAMPLES: u32 = 4u;
const PREP_EDGE_SHADOW_TEX_WIDTH: u32 = {{ edge_shadow_tex_width }}u;
fn prep_edge_shadow_xy(edge_pixel_id: u32, sample: u32) -> vec2<i32> {
    let idx = edge_pixel_id * PREP_EDGE_SHADOW_SAMPLES + sample;
    return vec2<i32>(i32(idx % PREP_EDGE_SHADOW_TEX_WIDTH), i32(idx / PREP_EDGE_SHADOW_TEX_WIDTH));
}
{% endif %}
{% endif %}


{% if !multisampled_geometry %}
// Non-MSAA interior shading. Under MSAA the renderer dispatches `cs_shade`
// (interior + edge in one kernel) instead, so `cs_opaque` is NOT emitted in
// the multisampled module — invariant: a compiled module carries only the
// entry points its AA config dispatches (no cross-AA code).
@compute @workgroup_size(8, 8)
fn cs_opaque(
    @builtin(workgroup_id) wg_id: vec3<u32>,
    @builtin(local_invocation_id) lid: vec3<u32>
) {
    // Tile lookup — the material classify pass populated
    // `classify_buckets.tiles` with packed
    // `(tile_x, tile_y)` coords per `shader_id` bucket. Our
    // pipeline's specialized `shader_id` picks the matching offset
    // statically; `workgroup_id.x` is the bucket entry index;
    // `local_invocation_id.xy` is the 8×8 thread → pixel offset.
    // Templated bucket_offset lookup — the pipeline is specialized
    // for one shader_id. Data-driven layout: read this pipeline's own tile
    // offset by its bucket index (resolved at template-render time, same value
    // the old per-name `<name>_offset` field held) from the `offsets` array —
    // no O(N) per-bucket field walk.
    let bucket_offset = classify_buckets.offsets[{{ bucket_index }}u];
    let tile = classify_buckets.tiles[bucket_offset + wg_id.x];
    let coords = vec2<i32>(i32(tile.x * 8u + lid.x), i32(tile.y * 8u + lid.y));
    {% if prep_present %}g_prep_ctx = PrepReadContext(PREP_MODE_PRIMARY, coords, vec2<i32>(0, 0));{% endif %}
    let screen_dims = textureDimensions(opaque_tex);
    let screen_dims_i32 = vec2<i32>(i32(screen_dims.x), i32(screen_dims.y));
    let screen_dims_f32 = vec2<f32>(f32(screen_dims.x), f32(screen_dims.y));
    let pixel_center = vec2<f32>(f32(coords.x) + 0.5, f32(coords.y) + 0.5);

    // Bounds check
    if (coords.x >= screen_dims_i32.x || coords.y >= screen_dims_i32.y) {
        return;
    }

    let visibility_data_info = textureLoad(visibility_data_tex, coords, 0);

    let triangle_index = join32(visibility_data_info.x, visibility_data_info.y);
    let material_meta_offset = join32(visibility_data_info.z, visibility_data_info.w);


    let camera = camera_from_raw(camera_raw);
    let frame_globals = frame_globals_from_raw(frame_globals_raw);


    // early return if we only hit skybox / no geometry (for all samples if MSAA).
    //
    // This is the pure material kernel — it never writes the skybox. The
    // dedicated skybox_primary.wgsl pipeline (compiled for the canonical skybox
    // bucket) owns skybox/uncovered pixels; every material pipeline just skips
    // them here so the output isn't double-written.
    {% if multisampled_geometry %}
        // With MSAA, check if ANY sample hit geometry before early returning
        var any_sample_hit = false;
        for (var s = 0u; s < {{ msaa_sample_count }}u; s++) {
            var vis_check: vec4<u32>;
            switch(s) {
                case 0u: { vis_check = textureLoad(visibility_data_tex, coords, 0); }
                case 1u: { vis_check = textureLoad(visibility_data_tex, coords, 1); }
                case 2u: { vis_check = textureLoad(visibility_data_tex, coords, 2); }
                case 3u, default: { vis_check = textureLoad(visibility_data_tex, coords, 3); }
            }
            if (join32(vis_check.x, vis_check.y) != U32_MAX) {
                any_sample_hit = true;
                break;
            }
        }

        if (!any_sample_hit) {
            // Skybox / fully-uncovered tile — the dedicated skybox_primary
            // pipeline writes these pixels; the material kernel just skips them.
            return;
        }
    {% else %}
        if (triangle_index == U32_MAX) {
            // Skybox pixel — handled by skybox_primary; skip.
            return;
        }
    {% endif %}

    // Sample 0 (the primary sample) is skybox but other samples hit
    // geometry — a silhouette edge pixel. This pure material kernel writes
    // nothing for it: skybox_primary owns the skybox contribution and
    // Stage 3 edge_resolve / final_blend own the per-sample blend, so the
    // kernel just skips the pixel (below) to avoid double-writing.
    {% if multisampled_geometry %}
        if (triangle_index == U32_MAX) {
            // Sample-0 skybox at a silhouette edge — skybox_primary writes the
            // base color; the material kernel skips here.
            return;
        }
    {% endif %}

    // If we've reached this point, the main sample hit geometry.
    let material_mesh_meta = material_mesh_metas[material_meta_offset / META_SIZE_IN_BYTES];

    // return early if the geometry hit is hud element (will be redrawn in transparency pass)
    if (material_mesh_meta.is_hud == 1u) {
        // this may bleed a little due to MSAA, but that's okay since huds are redrawn later
        return;
    }


    // Barycentric tex is RGBA16uint: RG = bary.xy as u16 fixed-point,
    // BA = instance_id (split u32 via join32). Unpack to f32 here; the
    // instance_id is consumed at the bottom of the function for per-instance
    // tint application.
    let barycentric_raw = textureLoad(barycentric_tex, coords, 0);
    let bary_xy = vec2<f32>(f32(barycentric_raw.x), f32(barycentric_raw.y)) / 65535.0;
    let barycentric = vec3<f32>(bary_xy.x, bary_xy.y, 1.0 - bary_xy.x - bary_xy.y);
    let main_instance_id = join32(barycentric_raw.z, barycentric_raw.w);

    let material_offset = material_mesh_meta.material_offset;
    let shader_id = material_load_shader_id(material_offset);

    // Per-pixel `shader_id` guard. The material classify pass already
    // scopes our dispatch to tiles containing our specialized
    // `shader_id`, so the guard rejects only pixels of a *different*
    // shader_id that share a mixed-material tile with ours. The guard
    // is on the numeric (registry-allocated) id regardless of `base`:
    // a specialized PBR variant routes only its own id's pixels here.
    if (shader_id != {{ shader_id.as_u32() }}u) { return; }

    let vertex_attribute_stride = material_mesh_meta.vertex_attribute_stride / 4; // 4 bytes per float
    let attribute_indices_offset = material_mesh_meta.vertex_attribute_indices_offset / 4;
    let attribute_data_offset = material_mesh_meta.vertex_attribute_data_offset / 4;
    let visibility_geometry_data_offset = material_mesh_meta.visibility_geometry_data_offset / 4;
    let uv_sets_index = material_mesh_meta.uv_sets_index;
    let color_sets_index = material_mesh_meta.color_sets_index;
    let uv_set_count = material_mesh_meta.uv_set_count;
    let color_set_count = material_mesh_meta.color_set_count;

    let base_triangle_index = attribute_indices_offset + (triangle_index * 3u);
    let triangle_indices = vec3<u32>(
        bitcast<u32>(visibility_data[base_triangle_index]),
        bitcast<u32>(visibility_data[base_triangle_index + 1]),
        bitcast<u32>(visibility_data[base_triangle_index + 2])
    );

    let standard_coordinates = get_standard_coordinates(coords, screen_dims);

    // Load world-space TBN directly from geometry pass output (already transformed with morphs/skins)
    let packed_nt = textureLoad(normal_tangent_tex, coords, 0);
    let tbn = unpack_normal_tangent(packed_nt);
    let world_normal = tbn.N;

    {% if inc.light_access %}
    let lights_info = get_lights_info();
    {% endif %}

    // Compute material color and apply lighting based on shader type.
    // Each opaque pipeline is specialized for one `shader_id`; the
    // template emits only the matching material's shading path
    // (PBR / Unlit / Toon). The dropped runtime if/else used to live
    // here — the askama match below replaces it.
    var color: vec3<f32>;
    var base_alpha: f32;

    {% if base == ShadingBase::Unlit %}
        // Unlit material path
        let unlit_material = unlit_get_material(material_offset);
        {% match mipmap %}
            {% when MipmapMode::Gradient %}
                let bary_derivs = textureLoad(barycentric_derivatives_tex, coords, 0);
                let unlit_color = compute_unlit_material_color(
                    triangle_indices,
                    attribute_data_offset,
                    unlit_material,
                    barycentric,
                    vertex_attribute_stride,
                    uv_sets_index,
                    bary_derivs,
                    world_normal,
                    camera.view,
                );
            {% when MipmapMode::None %}
                let unlit_color = compute_unlit_material_color(
                    triangle_indices,
                    attribute_data_offset,
                    unlit_material,
                    barycentric,
                    vertex_attribute_stride,
                    uv_sets_index,
                );
        {% endmatch %}
        color = compute_unlit_output(unlit_color);
        base_alpha = unlit_color.base.a;
    {% else if base == ShadingBase::Toon %}
        // Toon material path — banded N·L + stepped Blinn-Phong + rim.
        // Reads world position from the standard coordinates the surrounding
        // code already computes; doesn't sample textures (v1).
        let toon_material = toon_get_material(material_offset);
        color = compute_toon_lit_color(
            toon_material,
            world_normal,
            standard_coordinates.surface_to_camera,
            standard_coordinates.world_position,
            lights_info,
        );
        base_alpha = toon_material.base_color_factor.a;
    {% else if base == ShadingBase::Pbr %}
        // PBR material path (default)
        let pbr_material = pbr_get_material(material_offset);

        {% match mipmap %}
            {% when MipmapMode::Gradient %}
                let bary_derivs = textureLoad(barycentric_derivatives_tex, coords, 0);
                let material_color = compute_material_color(
                    camera,
                    triangle_indices,
                    attribute_data_offset,
                    triangle_index,
                    pbr_material,
                    barycentric,
                    vertex_attribute_stride,
                    uv_sets_index,
                    color_sets_index,
                    tbn,
                    bary_derivs,
                );
            {% when MipmapMode::None %}
                let material_color = compute_material_color(
                    camera,
                    triangle_indices,
                    attribute_data_offset,
                    triangle_index,
                    pbr_material,
                    barycentric,
                    vertex_attribute_stride,
                    uv_sets_index,
                    color_sets_index,
                    tbn,
                );
        {% endmatch %}

        if(pbr_material.debug_bitmask != 0u) {
            color = pbr_debug_material_color(pbr_material, material_color);
            base_alpha = 1.0;
            textureStore(opaque_tex, coords, vec4<f32>(color, base_alpha));
            return;
        }

        {% if use_froxel_lights %}
            // Unified froxel path: every opaque mesh shades punctual
            // lights from its per-pixel froxel light list (the GPU light
            // cull). This replaces the old per-mesh-slice / oversized-
            // sentinel split — clustered (froxel) culling is generic and
            // camera-correct for any mesh size, so there's no gate to
            // tune. Directional lights are walked flat (see lights.wgsl).
            color = apply_lighting_per_froxel(
                material_color,
                standard_coordinates.surface_to_camera,
                standard_coordinates.world_position,
                lights_info,
                (material_mesh_meta.receive_shadows & material_mesh_meta.shadow_receiver_gate),
                vec2<f32>(f32(coords.x), f32(coords.y)),
            );
        {% else %}
            color = apply_lighting(
                material_color,
                standard_coordinates.surface_to_camera,
                standard_coordinates.world_position,
                lights_info,
                (material_mesh_meta.receive_shadows & material_mesh_meta.shadow_receiver_gate),
            );
        {% endif %}
        base_alpha = material_color.base.a;
    {% else if base == ShadingBase::Flipbook %}
        // FlipBook: grid-uniform sprite-sheet, sampled per
        // `frame_globals.time + time_offset`. Tints by `material.tint`.
        let flipbook_material = flipbook_get_material(material_offset);
        var flipbook_sampled: vec4<f32> = vec4<f32>(1.0);
        if flipbook_material.atlas_tex_info.exists {
            let flipbook_uv_attr = texture_uv(
                attribute_data_offset,
                triangle_indices,
                barycentric,
                flipbook_material.atlas_tex_info,
                vertex_attribute_stride,
                uv_sets_index,
            );
            let flipbook_cell_uv = flipbook_compute_cell_uv(
                flipbook_material,
                flipbook_uv_attr,
                frame_globals.time,
            );
            // Mip-mode-aware sample. Even on the gradient template,
            // flipbook quads sample at the cell-UV (which jumps
            // discontinuously between cells, breaking hardware
            // derivative-driven mip selection); pass zero derivatives
            // so the grad path lands at mip 0.
            {% match mipmap %}
                {% when MipmapMode::Gradient %}
                    let flipbook_uv_derivs = UvDerivs(vec2<f32>(0.0), vec2<f32>(0.0));
                    flipbook_sampled = texture_pool_sample_grad(
                        flipbook_material.atlas_tex_info,
                        flipbook_cell_uv,
                        flipbook_uv_derivs,
                    );
                {% when MipmapMode::None %}
                    flipbook_sampled = texture_pool_sample_no_mips(
                        flipbook_material.atlas_tex_info,
                        flipbook_cell_uv,
                    );
            {% endmatch %}
        }
        let flipbook_result = flipbook_finalize_color(
            flipbook_material,
            flipbook_sampled,
            frame_globals.time,
        );
        color = flipbook_result.rgb;
        base_alpha = flipbook_result.a;
    {% else if base == ShadingBase::Custom %}
        // Dynamic custom material — wrapped fragment lives above.
        let dyn_material = material_data_load(material_offset);
        let dyn_input = OpaqueShadingInput(
            coords,
            screen_dims,
            triangle_index,
            barycentric,
            main_instance_id,
            world_normal,
            standard_coordinates.world_position,
            standard_coordinates.surface_to_camera,
            tbn.T,
            tbn.B,
            triangle_indices,
            attribute_data_offset,
            vertex_attribute_stride,
            color_sets_index,
            uv_sets_index,
            color_set_count,
            uv_set_count,
            material_offset,
            dyn_material,
        );
        let dyn_out = custom_shade_dynamic(dyn_input);
        color = dyn_out.color;
        base_alpha = dyn_out.alpha;
    {% endif %}


    // Edge-resolve is owned by the Stage 3 dispatch chain
    // (classify → per-shader edge_resolve → final_blend). Primary
    // opaque always writes the sample-0 shaded color here; final_blend
    // overwrites at classify-detected edge pixels with the proper
    // 4-sample average. This keeps the primary-opaque SPIR-V scoped
    // to a single shader_id (the per-pipeline specialization) — no
    // cross-shader switch inlined, no growth as dynamic materials
    // register. See https://github.com/dakom/awsm-renderer/pull/99 § Priority 3.

    {% if debug.normals %}
        // Debug visualization: encode normal as color
        textureStore(opaque_tex, coords, vec4<f32>(debug_normals(world_normal), 1.0));
        return;
    {% endif %}

    // Apply per-instance tint (color × tint.rgb, alpha × tint.a × attr.alpha).
    if (main_instance_id != INSTANCE_ATTR_NONE) {
        let attr = instance_attrs[main_instance_id];
        let tint = unpack4x8unorm(attr.color_packed);
        color = color * tint.rgb;
        base_alpha = base_alpha * tint.a * attr.alpha;
    }

    {% if debug.views %}
    // Global wireframe view — replace the shaded surface with a uniform clay
    // fill and draw the triangle edges on top, so meshes read as a wireframe
    // regardless of their material (not edges tinted onto the lit result).
    // Constant barycentric threshold — derivatives aren't available in a
    // compute kernel.
    if (cull_params.debug_wireframe == 1u) {
        let wire_edge = min(min(barycentric.x, barycentric.y), barycentric.z);
        let wire = 1.0 - smoothstep(0.0, 0.02, wire_edge);
        color = mix(vec3<f32>(0.55, 0.57, 0.60), vec3<f32>(0.05, 0.05, 0.07), wire);
    }
    {% endif %}

    // Write to output texture for non-edge pixel
    textureStore(opaque_tex, coords, vec4<f32>(color, base_alpha));
}
{% endif %}

fn get_triangle_indices(attribute_indices_offset: u32, triangle_index: u32) -> vec3<u32> {
    let base = attribute_indices_offset + (triangle_index * 3u);
    return vec3<u32>(
        bitcast<u32>(visibility_data[base]),
        bitcast<u32>(visibility_data[base + 1u]),
        bitcast<u32>(visibility_data[base + 2u]),
    );
}

{% if multisampled_geometry %}
// ════════════════════════════════════════════════════════════════════
// UNIFIED MODULE — shared MSAA per-sample shading helper.
//
// `shade_sample` is the per-sample shading body used by the `cs_shade`
// entry point below (it shades each MSAA edge sample this shader_id owns
// + accumulates into the per-material accumulator slot). It shares the
// embedded per-material shading + the dynamic-material wrapper + all
// helper includes already pulled in by the shared preamble above (each
// global/binding/fn appears exactly once across both entry points). The
// `edge_data` / `edge_layout` bindings cs_shade reads are declared (gated
// on `multisampled_geometry`) in bind_groups.wgsl at group(3) bindings
// 10/11.
//
// No atomics. Each (edge_pixel_id, slot_index) is owned by exactly
// one shader_id, so concurrent writes are race-free.
//
// See https://github.com/dakom/awsm-renderer/pull/99 § Pass structure step 4.
// ════════════════════════════════════════════════════════════════════

// Shade a single MSAA sample for this shader_id and return
// (color, alpha). Reads visibility/barycentric/normal textures at the
// given (coords, sample_index). Returns a sentinel zero on samples
// that don't belong to this shader_id (the caller's mask gate filters
// these out upstream, but we keep the bail for robustness).
fn shade_sample(
    coords: vec2<i32>,
    sample_index: u32,
    camera: Camera,
    screen_dims: vec2<u32>,
    screen_dims_f32: vec2<f32>,
    {% if inc.light_access %}
    lights_info: LightsInfo,
    {% endif %}
) -> vec4<f32> {
    let textures = msaa_load_sample_textures(coords, sample_index);
    let tri_id = join32(textures.vis_data.x, textures.vis_data.y);
    let mat_meta_off = join32(textures.vis_data.z, textures.vis_data.w);

    // Skybox / no geometry — caller's mask should never put us here
    // for a non-skybox shader_id, but bail safely.
    if (tri_id == U32_MAX) {
        return vec4<f32>(0.0);
    }
    let sample_mesh_meta = material_mesh_metas[mat_meta_off / META_SIZE_IN_BYTES];
    if (sample_mesh_meta.is_hud == 1u) {
        return vec4<f32>(0.0);
    }

    let bary_xy = vec2<f32>(f32(textures.bary.x), f32(textures.bary.y)) / 65535.0;
    let sample_bary = vec3<f32>(bary_xy.x, bary_xy.y, 1.0 - bary_xy.x - bary_xy.y);
    let sample_instance_id = join32(textures.bary.z, textures.bary.w);

    let sample_tbn = unpack_normal_tangent(textures.normal_tangent);
    let sample_normal = sample_tbn.N;
    // NOTE: temporarily back on sample-0 depth (main-branch behaviour).
    // The per-sample variant produced dark per-sample shading deltas at
    // tessellated-curve silhouettes that, once averaged, looked like
    // wireframe artifacts at every intra-mesh triangle seam classify
    // detects as an edge.
    let standard_coordinates = get_standard_coordinates(coords, screen_dims);

    let sample_mat_offset = sample_mesh_meta.material_offset;
    let sample_stride = sample_mesh_meta.vertex_attribute_stride / 4;
    let sample_indices_off = sample_mesh_meta.vertex_attribute_indices_offset / 4;
    let sample_data_off = sample_mesh_meta.vertex_attribute_data_offset / 4;
    let sample_uv_sets_idx = sample_mesh_meta.uv_sets_index;
    let sample_color_sets_idx = sample_mesh_meta.color_sets_index;
    let sample_uv_set_count = sample_mesh_meta.uv_set_count;
    let sample_color_set_count = sample_mesh_meta.color_set_count;

    let base_tri = sample_indices_off + (tri_id * 3u);
    let sample_tri_indices = vec3<u32>(
        bitcast<u32>(visibility_data[base_tri]),
        bitcast<u32>(visibility_data[base_tri + 1u]),
        bitcast<u32>(visibility_data[base_tri + 2u])
    );

    // Per-pixel shader_id guard. The classify pass already restricts
    // this dispatch to samples of our shader_id, but the templated
    // guard catches any registry drift between classify + this
    // pipeline.
    let sample_shader_id = material_load_shader_id(sample_mat_offset);
    // Guard on the numeric (registry-allocated) id regardless of `base`.
    if (sample_shader_id != {{ shader_id.as_u32() }}u) { return vec4<f32>(0.0); }

    var color: vec3<f32>;
    var base_alpha: f32;

    {% if base == ShadingBase::Unlit %}
        let unlit_material = unlit_get_material(sample_mat_offset);
        {% match mipmap %}
            {% when MipmapMode::Gradient %}
                let unlit_color = compute_unlit_material_color(
                    sample_tri_indices,
                    sample_data_off,
                    unlit_material,
                    sample_bary,
                    sample_stride,
                    sample_uv_sets_idx,
                    textures.bary_derivs,
                    sample_normal,
                    camera.view,
                );
            {% when MipmapMode::None %}
                let unlit_color = compute_unlit_material_color(
                    sample_tri_indices,
                    sample_data_off,
                    unlit_material,
                    sample_bary,
                    sample_stride,
                    sample_uv_sets_idx,
                );
        {% endmatch %}
        color = compute_unlit_output(unlit_color);
        base_alpha = unlit_color.base.a;
    {% else if base == ShadingBase::Toon %}
        let toon_material = toon_get_material(sample_mat_offset);
        color = compute_toon_lit_color(
            toon_material,
            sample_normal,
            standard_coordinates.surface_to_camera,
            standard_coordinates.world_position,
            lights_info,
        );
        base_alpha = toon_material.base_color_factor.a;
    {% else if base == ShadingBase::Pbr %}
        let pbr_material = pbr_get_material(sample_mat_offset);
        {% match mipmap %}
            {% when MipmapMode::Gradient %}
                let material_color = compute_material_color(
                    camera,
                    sample_tri_indices,
                    sample_data_off,
                    tri_id,
                    pbr_material,
                    sample_bary,
                    sample_stride,
                    sample_uv_sets_idx,
                    sample_color_sets_idx,
                    sample_tbn,
                    textures.bary_derivs,
                );
            {% when MipmapMode::None %}
                let material_color = compute_material_color(
                    camera,
                    sample_tri_indices,
                    sample_data_off,
                    tri_id,
                    pbr_material,
                    sample_bary,
                    sample_stride,
                    sample_uv_sets_idx,
                    sample_color_sets_idx,
                    sample_tbn,
                );
        {% endmatch %}
        {% if use_froxel_lights %}
            // Unified froxel path (mirrors the main compute pass): every
            // edge sample shades punctual lights from its per-pixel froxel
            // list. No per-mesh-slice / oversized-sentinel split.
            color = apply_lighting_per_froxel(
                material_color,
                standard_coordinates.surface_to_camera,
                standard_coordinates.world_position,
                lights_info,
                (sample_mesh_meta.receive_shadows & sample_mesh_meta.shadow_receiver_gate),
                vec2<f32>(f32(coords.x), f32(coords.y)),
            );
        {% else %}
            color = apply_lighting(
                material_color,
                standard_coordinates.surface_to_camera,
                standard_coordinates.world_position,
                lights_info,
                (sample_mesh_meta.receive_shadows & sample_mesh_meta.shadow_receiver_gate),
            );
        {% endif %}
        base_alpha = material_color.base.a;
    {% else if base == ShadingBase::Flipbook %}
        let flipbook_material = flipbook_get_material(sample_mat_offset);
        var flipbook_sampled: vec4<f32> = vec4<f32>(1.0);
        if flipbook_material.atlas_tex_info.exists {
            let flipbook_uv_attr = texture_uv(
                sample_data_off,
                sample_tri_indices,
                sample_bary,
                flipbook_material.atlas_tex_info,
                sample_stride,
                sample_uv_sets_idx,
            );
            let frame_globals_e = frame_globals_from_raw(frame_globals_raw);
            let flipbook_cell_uv = flipbook_compute_cell_uv(
                flipbook_material,
                flipbook_uv_attr,
                frame_globals_e.time,
            );
            {% match mipmap %}
                {% when MipmapMode::Gradient %}
                    let flipbook_uv_derivs = UvDerivs(vec2<f32>(0.0), vec2<f32>(0.0));
                    flipbook_sampled = texture_pool_sample_grad(
                        flipbook_material.atlas_tex_info,
                        flipbook_cell_uv,
                        flipbook_uv_derivs,
                    );
                {% when MipmapMode::None %}
                    flipbook_sampled = texture_pool_sample_no_mips(
                        flipbook_material.atlas_tex_info,
                        flipbook_cell_uv,
                    );
            {% endmatch %}
        }
        let frame_globals_e2 = frame_globals_from_raw(frame_globals_raw);
        let flipbook_result = flipbook_finalize_color(
            flipbook_material,
            flipbook_sampled,
            frame_globals_e2.time,
        );
        color = flipbook_result.rgb;
        base_alpha = flipbook_result.a;
    {% else if base == ShadingBase::Custom %}
        let dyn_material = material_data_load(sample_mat_offset);
        let dyn_input = OpaqueShadingInput(
            coords,
            screen_dims,
            tri_id,
            sample_bary,
            sample_instance_id,
            sample_normal,
            standard_coordinates.world_position,
            standard_coordinates.surface_to_camera,
            sample_tbn.T,
            sample_tbn.B,
            sample_tri_indices,
            sample_data_off,
            sample_stride,
            sample_color_sets_idx,
            sample_uv_sets_idx,
            sample_color_set_count,
            sample_uv_set_count,
            sample_mat_offset,
            dyn_material,
        );
        let dyn_out = custom_shade_dynamic(dyn_input);
        color = dyn_out.color;
        base_alpha = dyn_out.alpha;
    {% endif %}

    // Per-instance tint.
    if (sample_instance_id != INSTANCE_ATTR_NONE) {
        let attr = instance_attrs[sample_instance_id];
        let tint = unpack4x8unorm(attr.color_packed);
        color = color * tint.rgb;
        base_alpha = base_alpha * tint.a * attr.alpha;
    }

    {% if debug.views %}
    // Global wireframe view — mirror the compute kernel (uses this pass's
    // per-sample barycentric): uniform clay fill + dark triangle edges, so the
    // surface reads as a wireframe rather than edges over the shaded material.
    if (cull_params.debug_wireframe == 1u) {
        let wire_edge = min(min(sample_bary.x, sample_bary.y), sample_bary.z);
        let wire = 1.0 - smoothstep(0.0, 0.02, wire_edge);
        color = mix(vec3<f32>(0.55, 0.57, 0.60), vec3<f32>(0.05, 0.05, 0.07), wire);
    }
    {% endif %}

    return vec4<f32>(color, base_alpha);
}

// ════════════════════════════════════════════════════════════════════
// UNIFIED MODULE — `cs_shade` entry point (U1, unified-edge-shading.md).
//
// ONE kernel merging `cs_opaque` (interior, sample 0 → opaque_tex) +
// `cs_edge` (edge, per-sample → accumulator slot) into a single body,
// driven by `edge_id_tex` + the U0 ANY-sample tile dispatch (NOT the
// per-bucket edge-sample lists). Dispatched over THIS bucket's tile list
// (8×8 tile = workgroup, 1 thread/pixel) exactly like `cs_opaque`.
//
// Per pixel: read `edge_id_tex` ONCE.
//   * `edge_id == U32_MAX` (interior): do EXACTLY what `cs_opaque` does —
//     shade sample 0 (with the same per-pixel shader_id guard) and write
//     opaque_tex; else skip. The body below is `cs_opaque`'s body verbatim.
//   * else (edge): do EXACTLY what `cs_edge` does for this bucket's owned
//     samples — call the SAME `shade_sample(coords, s, ...)` and accumulate
//     into this material's accumulator slot via the SAME `edge_slot_map`
//     slot-find + the SAME accumulate write. The ONLY difference from
//     `cs_edge` is where (edge_id, which-samples) comes from: `edge_id_tex`
//     + reading the 4 per-sample materials at this pixel, instead of the
//     compact edge-sample-list entry.
//
// Reuses the existing per-material accumulator + edge_slot_map + final_blend
// resolve UNCHANGED (this is what makes the toggle-ON output byte-identical
// to the toggle-OFF cs_opaque+cs_edge path). The OLD cs_opaque/cs_edge entry
// points stay in this module for the toggle-OFF path.
// ════════════════════════════════════════════════════════════════════
@compute @workgroup_size(8, 8)
fn cs_shade(
    @builtin(workgroup_id) wg_id: vec3<u32>,
    @builtin(local_invocation_id) lid: vec3<u32>
) {
    // Tile lookup — identical to cs_opaque (this bucket's ANY-sample tile
    // list; `workgroup_id.x` is the bucket entry index, `local_invocation_id`
    // the 8×8 thread → pixel offset).
    let bucket_offset = classify_buckets.offsets[{{ bucket_index }}u];
    let tile = classify_buckets.tiles[bucket_offset + wg_id.x];
    let coords = vec2<i32>(i32(tile.x * 8u + lid.x), i32(tile.y * 8u + lid.y));
    let screen_dims = textureDimensions(opaque_tex);
    let screen_dims_i32 = vec2<i32>(i32(screen_dims.x), i32(screen_dims.y));
    let screen_dims_f32 = vec2<f32>(f32(screen_dims.x), f32(screen_dims.y));

    // Bounds check (same as cs_opaque).
    if (coords.x >= screen_dims_i32.x || coords.y >= screen_dims_i32.y) {
        return;
    }

    // Read the per-pixel edge-id ONCE. U32_MAX → interior; else → edge
    // (the compact edge_pixel_id = the accumulator base).
    let edge_id = textureLoad(edge_id_tex, coords).x;

    if (edge_id == U32_MAX) {
        // ── INTERIOR ARM (cs_opaque body, verbatim) ──────────────────
        {% if prep_present %}g_prep_ctx = PrepReadContext(PREP_MODE_PRIMARY, coords, vec2<i32>(0, 0));{% endif %}
        let pixel_center = vec2<f32>(f32(coords.x) + 0.5, f32(coords.y) + 0.5);

        let visibility_data_info = textureLoad(visibility_data_tex, coords, 0);

        let triangle_index = join32(visibility_data_info.x, visibility_data_info.y);
        let material_meta_offset = join32(visibility_data_info.z, visibility_data_info.w);

        let camera = camera_from_raw(camera_raw);
        let frame_globals = frame_globals_from_raw(frame_globals_raw);

        // early return if we only hit skybox / no geometry (for all samples).
        // This is the pure material kernel — it never writes the skybox.
        var any_sample_hit = false;
        for (var s = 0u; s < {{ msaa_sample_count }}u; s++) {
            var vis_check: vec4<u32>;
            switch(s) {
                case 0u: { vis_check = textureLoad(visibility_data_tex, coords, 0); }
                case 1u: { vis_check = textureLoad(visibility_data_tex, coords, 1); }
                case 2u: { vis_check = textureLoad(visibility_data_tex, coords, 2); }
                case 3u, default: { vis_check = textureLoad(visibility_data_tex, coords, 3); }
            }
            if (join32(vis_check.x, vis_check.y) != U32_MAX) {
                any_sample_hit = true;
                break;
            }
        }
        if (!any_sample_hit) {
            return;
        }

        // Sample-0 skybox at a silhouette edge — skybox owns the base color.
        if (triangle_index == U32_MAX) {
            return;
        }

        let material_mesh_meta = material_mesh_metas[material_meta_offset / META_SIZE_IN_BYTES];
        if (material_mesh_meta.is_hud == 1u) {
            return;
        }

        let barycentric_raw = textureLoad(barycentric_tex, coords, 0);
        let bary_xy = vec2<f32>(f32(barycentric_raw.x), f32(barycentric_raw.y)) / 65535.0;
        let barycentric = vec3<f32>(bary_xy.x, bary_xy.y, 1.0 - bary_xy.x - bary_xy.y);
        let main_instance_id = join32(barycentric_raw.z, barycentric_raw.w);

        let material_offset = material_mesh_meta.material_offset;
        let shader_id = material_load_shader_id(material_offset);

        // Per-pixel `shader_id` guard (same as cs_opaque).
        if (shader_id != {{ shader_id.as_u32() }}u) { return; }

        let vertex_attribute_stride = material_mesh_meta.vertex_attribute_stride / 4;
        let attribute_indices_offset = material_mesh_meta.vertex_attribute_indices_offset / 4;
        let attribute_data_offset = material_mesh_meta.vertex_attribute_data_offset / 4;
        let visibility_geometry_data_offset = material_mesh_meta.visibility_geometry_data_offset / 4;
        let uv_sets_index = material_mesh_meta.uv_sets_index;
        let color_sets_index = material_mesh_meta.color_sets_index;
        let uv_set_count = material_mesh_meta.uv_set_count;
        let color_set_count = material_mesh_meta.color_set_count;

        let base_triangle_index = attribute_indices_offset + (triangle_index * 3u);
        let triangle_indices = vec3<u32>(
            bitcast<u32>(visibility_data[base_triangle_index]),
            bitcast<u32>(visibility_data[base_triangle_index + 1]),
            bitcast<u32>(visibility_data[base_triangle_index + 2])
        );

        let standard_coordinates = get_standard_coordinates(coords, screen_dims);

        let packed_nt = textureLoad(normal_tangent_tex, coords, 0);
        let tbn = unpack_normal_tangent(packed_nt);
        let world_normal = tbn.N;

        {% if inc.light_access %}
        let lights_info = get_lights_info();
        {% endif %}

        var color: vec3<f32>;
        var base_alpha: f32;

        {% if base == ShadingBase::Unlit %}
            let unlit_material = unlit_get_material(material_offset);
            {% match mipmap %}
                {% when MipmapMode::Gradient %}
                    let bary_derivs = textureLoad(barycentric_derivatives_tex, coords, 0);
                    let unlit_color = compute_unlit_material_color(
                        triangle_indices,
                        attribute_data_offset,
                        unlit_material,
                        barycentric,
                        vertex_attribute_stride,
                        uv_sets_index,
                        bary_derivs,
                        world_normal,
                        camera.view,
                    );
                {% when MipmapMode::None %}
                    let unlit_color = compute_unlit_material_color(
                        triangle_indices,
                        attribute_data_offset,
                        unlit_material,
                        barycentric,
                        vertex_attribute_stride,
                        uv_sets_index,
                    );
            {% endmatch %}
            color = compute_unlit_output(unlit_color);
            base_alpha = unlit_color.base.a;
        {% else if base == ShadingBase::Toon %}
            let toon_material = toon_get_material(material_offset);
            color = compute_toon_lit_color(
                toon_material,
                world_normal,
                standard_coordinates.surface_to_camera,
                standard_coordinates.world_position,
                lights_info,
            );
            base_alpha = toon_material.base_color_factor.a;
        {% else if base == ShadingBase::Pbr %}
            let pbr_material = pbr_get_material(material_offset);

            {% match mipmap %}
                {% when MipmapMode::Gradient %}
                    let bary_derivs = textureLoad(barycentric_derivatives_tex, coords, 0);
                    let material_color = compute_material_color(
                        camera,
                        triangle_indices,
                        attribute_data_offset,
                        triangle_index,
                        pbr_material,
                        barycentric,
                        vertex_attribute_stride,
                        uv_sets_index,
                        color_sets_index,
                        tbn,
                        bary_derivs,
                    );
                {% when MipmapMode::None %}
                    let material_color = compute_material_color(
                        camera,
                        triangle_indices,
                        attribute_data_offset,
                        triangle_index,
                        pbr_material,
                        barycentric,
                        vertex_attribute_stride,
                        uv_sets_index,
                        color_sets_index,
                        tbn,
                    );
            {% endmatch %}

            if(pbr_material.debug_bitmask != 0u) {
                color = pbr_debug_material_color(pbr_material, material_color);
                base_alpha = 1.0;
                textureStore(opaque_tex, coords, vec4<f32>(color, base_alpha));
                return;
            }

            {% if use_froxel_lights %}
                color = apply_lighting_per_froxel(
                    material_color,
                    standard_coordinates.surface_to_camera,
                    standard_coordinates.world_position,
                    lights_info,
                    (material_mesh_meta.receive_shadows & material_mesh_meta.shadow_receiver_gate),
                    vec2<f32>(f32(coords.x), f32(coords.y)),
                );
            {% else %}
                color = apply_lighting(
                    material_color,
                    standard_coordinates.surface_to_camera,
                    standard_coordinates.world_position,
                    lights_info,
                    (material_mesh_meta.receive_shadows & material_mesh_meta.shadow_receiver_gate),
                );
            {% endif %}
            base_alpha = material_color.base.a;
        {% else if base == ShadingBase::Flipbook %}
            let flipbook_material = flipbook_get_material(material_offset);
            var flipbook_sampled: vec4<f32> = vec4<f32>(1.0);
            if flipbook_material.atlas_tex_info.exists {
                let flipbook_uv_attr = texture_uv(
                    attribute_data_offset,
                    triangle_indices,
                    barycentric,
                    flipbook_material.atlas_tex_info,
                    vertex_attribute_stride,
                    uv_sets_index,
                );
                let flipbook_cell_uv = flipbook_compute_cell_uv(
                    flipbook_material,
                    flipbook_uv_attr,
                    frame_globals.time,
                );
                {% match mipmap %}
                    {% when MipmapMode::Gradient %}
                        let flipbook_uv_derivs = UvDerivs(vec2<f32>(0.0), vec2<f32>(0.0));
                        flipbook_sampled = texture_pool_sample_grad(
                            flipbook_material.atlas_tex_info,
                            flipbook_cell_uv,
                            flipbook_uv_derivs,
                        );
                    {% when MipmapMode::None %}
                        flipbook_sampled = texture_pool_sample_no_mips(
                            flipbook_material.atlas_tex_info,
                            flipbook_cell_uv,
                        );
                {% endmatch %}
            }
            let flipbook_result = flipbook_finalize_color(
                flipbook_material,
                flipbook_sampled,
                frame_globals.time,
            );
            color = flipbook_result.rgb;
            base_alpha = flipbook_result.a;
        {% else if base == ShadingBase::Custom %}
            let dyn_material = material_data_load(material_offset);
            let dyn_input = OpaqueShadingInput(
                coords,
                screen_dims,
                triangle_index,
                barycentric,
                main_instance_id,
                world_normal,
                standard_coordinates.world_position,
                standard_coordinates.surface_to_camera,
                tbn.T,
                tbn.B,
                triangle_indices,
                attribute_data_offset,
                vertex_attribute_stride,
                color_sets_index,
                uv_sets_index,
                color_set_count,
                uv_set_count,
                material_offset,
                dyn_material,
            );
            let dyn_out = custom_shade_dynamic(dyn_input);
            color = dyn_out.color;
            base_alpha = dyn_out.alpha;
        {% endif %}

        {% if debug.normals %}
            textureStore(opaque_tex, coords, vec4<f32>(debug_normals(world_normal), 1.0));
            return;
        {% endif %}

        if (main_instance_id != INSTANCE_ATTR_NONE) {
            let attr = instance_attrs[main_instance_id];
            let tint = unpack4x8unorm(attr.color_packed);
            color = color * tint.rgb;
            base_alpha = base_alpha * tint.a * attr.alpha;
        }

        {% if debug.views %}
        if (cull_params.debug_wireframe == 1u) {
            let wire_edge = min(min(barycentric.x, barycentric.y), barycentric.z);
            let wire = 1.0 - smoothstep(0.0, 0.02, wire_edge);
            color = mix(vec3<f32>(0.55, 0.57, 0.60), vec3<f32>(0.05, 0.05, 0.07), wire);
        }
        {% endif %}

        textureStore(opaque_tex, coords, vec4<f32>(color, base_alpha));
        return;
    }

    // ── EDGE ARM (cs_edge per-sample shade + accumulate, verbatim) ────
    // `edge_id` (= the compact edge_pixel_id) is this pixel's accumulator
    // base. We reconstruct the per-sample ownership mask cs_edge read from
    // the compact list by reading the 4 sample materials at this pixel and
    // testing each against THIS bucket's shader_id (the same gate
    // `shade_sample` applies); the slot-find against `edge_slot_map` then
    // gates the single accumulator write to exactly this material's slot
    // (identical to cs_edge — a sample owned by a different bucket sharing
    // our shader_id finds no matching slot here and is skipped).
    let edge_pixel_id = edge_id;

    // Find our slot in the slot_map (IDENTICAL to cs_edge).
    {% if edge_slot_bits == 16 %}
    let slot_w0 = edge_data[edge_layout.edge_slot_map_base + edge_pixel_id * 2u];
    let slot_w1 = edge_data[edge_layout.edge_slot_map_base + edge_pixel_id * 2u + 1u];
    {% else %}
    let slot_map = edge_data[edge_layout.edge_slot_map_base + edge_pixel_id];
    {% endif %}
    var slot_index: u32 = 4u;
    for (var i = 0u; i < 4u; i++) {
        {% if edge_slot_bits == 16 %}
        let word = select(slot_w0, slot_w1, i >= 2u);
        let field = (word >> ((i % 2u) * 16u)) & 0xFFFFu;
        {% else %}
        let field = (slot_map >> (i * 8u)) & 0xFFu;
        {% endif %}
        if (field == {{ bucket_index }}u) {
            slot_index = i;
            break;
        }
    }
    if (slot_index >= 4u) {
        return;
    }

    let edge_camera = camera_from_raw(camera_raw);
    let edge_screen_dims_u = textureDimensions(visibility_data_tex);
    let edge_screen_dims = vec2<u32>(edge_screen_dims_u.x, edge_screen_dims_u.y);
    let edge_screen_dims_f32 = vec2<f32>(f32(edge_screen_dims.x), f32(edge_screen_dims.y));
    {% if inc.light_access %}
    let lights_info = get_lights_info();
    {% endif %}{% if prep_present %}
    // EDGE mode (same as cs_edge): read the compact per-edge-sample shadow
    // buffer; `edge_shadow_xy` is set PER SAMPLE in the loop below.
    g_prep_ctx = PrepReadContext(PREP_MODE_EDGE, coords, vec2<i32>(0, 0));
{% endif %}
    var color_sum = vec3<f32>(0.0);
    var alpha_sum: f32 = 0.0;
    var sample_count: u32 = 0u;

    for (var s = 0u; s < 4u; s++) {
        // Per-sample ownership: the same shader_id gate `shade_sample`
        // applies. Reading the sample material here reconstructs cs_edge's
        // compact-list sample_mask (which-samples-this-bucket-owns).
        var owns_sample = false;
        {
            var vis_s: vec4<u32>;
            switch(s) {
                case 0u: { vis_s = textureLoad(visibility_data_tex, coords, 0); }
                case 1u: { vis_s = textureLoad(visibility_data_tex, coords, 1); }
                case 2u: { vis_s = textureLoad(visibility_data_tex, coords, 2); }
                case 3u, default: { vis_s = textureLoad(visibility_data_tex, coords, 3); }
            }
            let tri_s = join32(vis_s.x, vis_s.y);
            if (tri_s != U32_MAX) {
                let mat_off_s = join32(vis_s.z, vis_s.w);
                let mesh_meta_s = material_mesh_metas[mat_off_s / META_SIZE_IN_BYTES];
                if (mesh_meta_s.is_hud != 1u) {
                    let sid_s = material_load_shader_id(mesh_meta_s.material_offset);
                    if (sid_s == {{ shader_id.as_u32() }}u) {
                        owns_sample = true;
                    }
                }
            }
        }
        if (owns_sample) {
            {% if prep_present %}
            g_prep_ctx.edge_shadow_xy = prep_edge_shadow_xy(edge_pixel_id, s);
            {% endif %}
            let shaded = shade_sample(coords, s, edge_camera, edge_screen_dims, edge_screen_dims_f32{% if inc.light_access %}, lights_info{% endif %});
            color_sum += shaded.rgb;
            alpha_sum += shaded.a;
            sample_count += 1u;
        }
    }

    if (sample_count == 0u) {
        return;
    }

    // Accumulate into accumulator[edge_pixel_id × 4 + slot_index] (IDENTICAL
    // to cs_edge — same format the unchanged final_blend resolves).
    let accum_word_index = edge_layout.accumulator_base + (edge_pixel_id * 4u + slot_index) * 4u;
    edge_data[accum_word_index + 0u] = bitcast<u32>(color_sum.x);
    edge_data[accum_word_index + 1u] = bitcast<u32>(color_sum.y);
    edge_data[accum_word_index + 2u] = bitcast<u32>(color_sum.z);
    edge_data[accum_word_index + 3u] = bitcast<u32>(f32(sample_count));
}
{% endif %}