viewport-lib 0.19.0

3D viewport rendering library
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
//! Shadow depth pass: directional cascades and point-light cube-map faces.

use super::*;

impl ViewportRenderer {
    /// Render the shadow depth pass: directional CSM cascades into the atlas
    /// tiles and point-light cube-map faces, including per-cascade plugin draws.
    #[allow(clippy::too_many_arguments)]
    pub(super) fn prepare_shadow_pass(
        resources: &mut DeviceResources,
        instancing: &mut InstancingState,
        compute_filter_results: &[crate::resources::ComputeFilterResult],
        plugins: &std::collections::HashMap<
            &'static str,
            Box<dyn crate::plugin_api::ItemTypePlugin>,
        >,
        plugin_frame_index: u64,
        lighting: &crate::renderer::types::LightingSettings,
        scene_items: &[SceneRenderItem],
        light: &LightingFrame,
        shadows_skipped: bool,
        last_stats: &mut crate::renderer::stats::FrameStats,
        ts_query_set: Option<&wgpu::QuerySet>,
        ts_written_mask: &std::sync::atomic::AtomicU32,
        device: &wgpu::Device,
        queue: &wgpu::Queue,
        frame: &FrameData,
    ) {
        // Shadow-pass instrumentation. The stall reported on some mobile backends
        // shows up at `present` because the shadow depth work is GPU-bound and only
        // forced to completion later. When the `viewport_lib::shadow` target is
        // enabled at debug level, bracket the pass and poll the device to completion
        // so the shadow GPU cost is attributed here instead of hiding inside present.
        // The poll is skipped entirely (zero overhead) when the target is off.
        // Enable with `RUST_LOG=viewport_lib::shadow=debug`. For non-perturbing
        // timing in shipping builds, use GPU timestamp queries instead.
        let shadow_instrument =
            tracing::enabled!(target: "viewport_lib::shadow", tracing::Level::DEBUG);
        let shadow_start = std::time::Instant::now();

        // Shadow depth pass : CSM: render each cascade into its atlas tile.
        // Skip the pass entirely when over budget and shadow reduction is allowed.
        // ------------------------------------------------------------------
        let skip_shadows = shadows_skipped;

        // When skipping the shadow pass (budget pressure or empty scene), clear the
        // atlas to max depth so that stale values from a previous frame or a previous
        // showcase don't produce phantom shadows.
        if lighting.shadows_enabled && (skip_shadows || scene_items.is_empty()) {
            let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("shadow_clear_encoder"),
            });
            let _ = enc.begin_render_pass(&wgpu::RenderPassDescriptor {
                label: Some("shadow_clear_pass"),
                color_attachments: &[],
                depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
                    view: &resources.shadow_map_view,
                    depth_ops: Some(wgpu::Operations {
                        load: wgpu::LoadOp::Clear(1.0),
                        store: wgpu::StoreOp::Store,
                    }),
                    stencil_ops: None,
                }),
                timestamp_writes: None,
                occlusion_query_set: None,
            });
            queue.submit(std::iter::once(enc.finish()));
        }

        if lighting.shadows_enabled && !scene_items.is_empty() && !skip_shadows {
            // ------------------------------------------------------------------
            // Shadow GPU cull dispatch
            //
            // For each active cascade, dispatch `cull_instances` + `write_indirect_args`
            // with the cascade frustum. Results land in `shadow_vis_bufs[c]` and
            // `shadow_indirect_bufs[c]`, consumed by the shadow render pass below.
            // All cascade dispatches share the same `batch_counter_buf`; each
            // `write_indirect_args` dispatch resets the counters for the next cascade.
            // ------------------------------------------------------------------
            if instancing.gpu_culling_enabled
                && instancing.use_instancing
                && !instancing.batches.is_empty()
                && instancing.cached_instance_count > 0
            {
                // Mutable operations first.
                if instancing.cull_resources.is_none() {
                    instancing.cull_resources =
                        Some(crate::renderer::indirect::CullResources::new(device));
                }
                resources.ensure_cull_instance_pipelines(device);

                let instance_count = instancing.cached_instance_count as u32;
                let batch_count = instancing.batches.len() as u32;
                instancing
                    .shadow_cull
                    .ensure_outputs(device, instance_count, batch_count);
                // Drop shadow cull bind groups whose binding-0 instance storage
                // buffer was rebuilt this frame; ensure_outputs already handles a
                // resized shadow-vis buffer.
                if instancing.shadow_cull.built_gen != instancing.instance_gen {
                    instancing.shadow_cull.shadow_cull_instance_bgs = [None, None, None, None];
                    instancing.shadow_cull.built_gen = instancing.instance_gen;
                }
                for c in 0..light.effective_cascade_count {
                    resources.get_shadow_cull_instance_bind_group(
                        &mut instancing.shadow_cull,
                        device,
                        c,
                    );
                }

                if let (Some(aabb_buf), Some(meta_buf), Some(counter_buf)) = (
                    resources.cull.aabb_buf.as_ref(),
                    resources.cull.batch_meta_buf.as_ref(),
                    instancing.shadow_cull.batch_counter_buf.as_ref(),
                ) {
                    let cull = instancing.cull_resources.as_ref().unwrap();
                    let mut shadow_cull_encoder =
                        device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
                            label: Some("shadow_cull_encoder"),
                        });
                    for c in 0..light.effective_cascade_count {
                        if let (Some(shadow_vis_buf), Some(shadow_indirect_buf)) = (
                            instancing.shadow_cull.shadow_vis_bufs[c].as_ref(),
                            instancing.shadow_cull.shadow_indirect_bufs[c].as_ref(),
                        ) {
                            let cpu_frustum = crate::camera::frustum::Frustum::from_view_proj(
                                &light.cascade_view_projs[c],
                            );
                            let sub = crate::plugin_api::CullSubmission {
                                instance_aabbs: aabb_buf,
                                instance_count,
                                batch_meta: meta_buf,
                                batch_count,
                                counter: counter_buf,
                                visible_out: shadow_vis_buf,
                                indirect_out: shadow_indirect_buf,
                                shadow_pass: true,
                            };
                            cull.dispatch(
                                &mut shadow_cull_encoder,
                                device,
                                queue,
                                &cpu_frustum,
                                Some(c),
                                &sub,
                                None,
                                None,
                            );
                        }
                    }
                    queue.submit(std::iter::once(shadow_cull_encoder.finish()));
                }
            }

            let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("shadow_pass_encoder"),
            });
            {
                let shadow_ts_writes = ts_query_set.map(|qs| {
                    ts_written_mask.fetch_or(
                        1 << crate::renderer::GPU_TS_SHADOW,
                        std::sync::atomic::Ordering::Relaxed,
                    );
                    wgpu::RenderPassTimestampWrites {
                        query_set: qs,
                        beginning_of_pass_write_index: Some(crate::renderer::GPU_TS_SHADOW * 2),
                        end_of_pass_write_index: Some(crate::renderer::GPU_TS_SHADOW * 2 + 1),
                    }
                });
                let mut shadow_pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
                    label: Some("shadow_pass"),
                    color_attachments: &[],
                    depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
                        view: &resources.shadow_map_view,
                        depth_ops: Some(wgpu::Operations {
                            load: wgpu::LoadOp::Clear(1.0),
                            store: wgpu::StoreOp::Store,
                        }),
                        stencil_ops: None,
                    }),
                    timestamp_writes: shadow_ts_writes,
                    occlusion_query_set: None,
                });

                let mut shadow_draws = 0u32;
                let tile_px = light.tile_size as f32;

                if instancing.use_instancing {
                    let use_shadow_indirect = instancing.gpu_culling_enabled
                        && resources.cull.shadow_pipeline.is_some()
                        && instancing.shadow_cull.shadow_vis_bufs[0].is_some();

                    if use_shadow_indirect {
                        // GPU-culled indirect shadow path.
                        for cascade in 0..light.effective_cascade_count {
                            let tile_col = (cascade % 2) as f32;
                            let tile_row = (cascade / 2) as f32;
                            shadow_pass.set_viewport(
                                tile_col * tile_px,
                                tile_row * tile_px,
                                tile_px,
                                tile_px,
                                0.0,
                                1.0,
                            );
                            shadow_pass.set_scissor_rect(
                                (tile_col * tile_px) as u32,
                                (tile_row * tile_px) as u32,
                                light.tile_size,
                                light.tile_size,
                            );

                            // Write cascade view-projection matrix.
                            queue.write_buffer(
                                resources.instancing.shadow_cascade_bufs[cascade]
                                    .as_ref()
                                    .expect("shadow_instanced_cascade_bufs not allocated"),
                                0,
                                bytemuck::cast_slice(
                                    &light.cascade_view_projs[cascade].to_cols_array_2d(),
                                ),
                            );

                            let Some(pipeline) = resources.cull.shadow_pipeline.as_ref() else {
                                continue;
                            };
                            let Some(pipeline_two_sided) =
                                resources.cull.shadow_two_sided_pipeline.as_ref()
                            else {
                                continue;
                            };
                            let Some(cascade_bg) =
                                resources.instancing.shadow_cascade_bgs[cascade].as_ref()
                            else {
                                continue;
                            };
                            let Some(inst_cull_bg) =
                                instancing.shadow_cull.shadow_cull_instance_bgs[cascade].as_ref()
                            else {
                                continue;
                            };
                            let Some(shadow_indirect_buf) =
                                instancing.shadow_cull.shadow_indirect_bufs[cascade].as_ref()
                            else {
                                continue;
                            };

                            shadow_pass.set_bind_group(0, cascade_bg, &[]);
                            shadow_pass.set_bind_group(1, inst_cull_bg, &[]);

                            // Batches are sorted with two_sided in the key, so one- and
                            // two-sided runs are contiguous; switch the pipeline only when
                            // the flag changes. Two-sided batches use the `cull_mode: None`
                            // pipeline so foliage casters are not Front-culled away.
                            let mut cur_two_sided: Option<bool> = None;
                            for (bi, batch) in instancing.batches.iter().enumerate() {
                                if batch.is_transparent {
                                    continue;
                                }
                                let Some(mesh) = resources.mesh_store.get(batch.mesh_id) else {
                                    continue;
                                };
                                if cur_two_sided != Some(batch.two_sided) {
                                    shadow_pass.set_pipeline(if batch.two_sided {
                                        pipeline_two_sided
                                    } else {
                                        pipeline
                                    });
                                    cur_two_sided = Some(batch.two_sided);
                                }
                                shadow_pass.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
                                shadow_pass.set_index_buffer(
                                    mesh.index_buffer.slice(..),
                                    wgpu::IndexFormat::Uint32,
                                );
                                shadow_pass
                                    .draw_indexed_indirect(shadow_indirect_buf, bi as u64 * 20);
                                shadow_draws += 1;
                            }
                        }
                    } else if let (Some(pipeline), Some(pipeline_two_sided), Some(instance_bg)) = (
                        &resources.instancing.shadow_pipeline,
                        &resources.instancing.shadow_two_sided_pipeline,
                        instancing.batches.first().and_then(|b| {
                            resources.instancing.bind_groups.get(&(
                                b.texture_id.map(|t| t.raw()).unwrap_or(u64::MAX),
                                b.normal_map_id.map(|t| t.raw()).unwrap_or(u64::MAX),
                                b.ao_map_id.map(|t| t.raw()).unwrap_or(u64::MAX),
                            ))
                        }),
                    ) {
                        // Direct draw shadow path (fallback when GPU culling is off).
                        for cascade in 0..light.effective_cascade_count {
                            let tile_col = (cascade % 2) as f32;
                            let tile_row = (cascade / 2) as f32;
                            shadow_pass.set_viewport(
                                tile_col * tile_px,
                                tile_row * tile_px,
                                tile_px,
                                tile_px,
                                0.0,
                                1.0,
                            );
                            shadow_pass.set_scissor_rect(
                                (tile_col * tile_px) as u32,
                                (tile_row * tile_px) as u32,
                                light.tile_size,
                                light.tile_size,
                            );

                            queue.write_buffer(
                                resources.instancing.shadow_cascade_bufs[cascade]
                                    .as_ref()
                                    .expect("shadow_instanced_cascade_bufs not allocated"),
                                0,
                                bytemuck::cast_slice(
                                    &light.cascade_view_projs[cascade].to_cols_array_2d(),
                                ),
                            );

                            let cascade_bg = resources.instancing.shadow_cascade_bgs[cascade]
                                .as_ref()
                                .expect("shadow_instanced_cascade_bgs not allocated");
                            shadow_pass.set_bind_group(0, cascade_bg, &[]);
                            shadow_pass.set_bind_group(1, instance_bg, &[]);

                            // See the indirect path above: switch the pipeline only when
                            // the contiguous-sorted two_sided flag changes.
                            let mut cur_two_sided: Option<bool> = None;
                            for batch in &instancing.batches {
                                if batch.is_transparent {
                                    continue;
                                }
                                let Some(mesh) = resources.mesh_store.get(batch.mesh_id) else {
                                    continue;
                                };
                                if cur_two_sided != Some(batch.two_sided) {
                                    shadow_pass.set_pipeline(if batch.two_sided {
                                        pipeline_two_sided
                                    } else {
                                        pipeline
                                    });
                                    cur_two_sided = Some(batch.two_sided);
                                }
                                shadow_pass.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
                                shadow_pass.set_index_buffer(
                                    mesh.index_buffer.slice(..),
                                    wgpu::IndexFormat::Uint32,
                                );
                                shadow_pass.draw_indexed(
                                    0..mesh.index_count,
                                    0,
                                    batch.instance_offset
                                        ..batch.instance_offset + batch.instance_count,
                                );
                                shadow_draws += 1;
                            }
                        }
                    }

                    // Per-item shadow casters for items excluded from
                    // instanced batches.
                    //
                    // `sorted_items` in the instancing builder filters out
                    // items with: active scalar attributes, two-sided
                    // materials, matcap shading, UV/param visualisation,
                    // per-instance deformer data (skinned), and
                    // position/normal overrides. Those items render through
                    // the per-item lit path; without this loop they would
                    // also be silently dropped from the shadow atlas. Draw
                    // them here with the non-instanced `shadow_pipeline`
                    // (group 0 = `shadow_bind_group` with cascade dynamic
                    // offset, group 1 = mesh.object_bind_group, group 2 =
                    // per-mesh deform sidecar).
                    let filter_results = compute_filter_results;
                    for cascade in 0..light.effective_cascade_count {
                        let tile_col = (cascade % 2) as f32;
                        let tile_row = (cascade / 2) as f32;
                        shadow_pass.set_viewport(
                            tile_col * tile_px,
                            tile_row * tile_px,
                            tile_px,
                            tile_px,
                            0.0,
                            1.0,
                        );
                        shadow_pass.set_scissor_rect(
                            (tile_col * tile_px) as u32,
                            (tile_row * tile_px) as u32,
                            light.tile_size,
                            light.tile_size,
                        );
                        shadow_pass.set_bind_group(
                            0,
                            &resources.shadow_bind_group,
                            &[cascade as u32 * 256],
                        );

                        let cascade_frustum = crate::camera::frustum::Frustum::from_view_proj(
                            &light.cascade_view_projs[cascade],
                        );

                        for item in scene_items.iter() {
                            if item.settings.hidden
                                || !item.settings.cast_shadows
                                || item.settings.opacity < 1.0
                            {
                                continue;
                            }
                            let Some(mesh) = resources.mesh_store.get(item.mesh_id) else {
                                continue;
                            };

                            // Mirror the inclusion filter from
                            // `sorted_items` (instancing builder). When
                            // every condition holds, the item was drawn by
                            // the instanced shadow path and must not be
                            // drawn again here. Two-sided (`Identical`) meshes
                            // are now in the instanced batches, so they are
                            // excluded here via `backface_needs_per_object`.
                            let in_instanced_batch = item.active_attribute.is_none()
                                && !backface_needs_per_object(item)
                                && item.material.matcap_id().is_none()
                                && item.material.param_vis.is_none()
                                && !filter_results.iter().any(|r| r.mesh_id == item.mesh_id)
                                && !resources.deform.has_per_instance_deform_data(
                                    item.mesh_id,
                                    item.deform_instance,
                                )
                                && mesh.position_override_buffer.is_none()
                                && mesh.normal_override_buffer.is_none();
                            if in_instanced_batch {
                                continue;
                            }

                            let world_aabb = mesh
                                .aabb
                                .transformed(&glam::Mat4::from_cols_array_2d(&item.model));
                            if cascade_frustum.cull_aabb(&world_aabb) {
                                continue;
                            }

                            // Two-sided materials cast through the cull-none
                            // pipeline so both faces rasterise; its larger
                            // caster-side bias keeps the surface from
                            // self-shadowing where it is its own receiver.
                            if item.material.is_two_sided() {
                                shadow_pass.set_pipeline(&resources.shadow_pipeline_two_sided);
                            } else {
                                shadow_pass.set_pipeline(&resources.shadow_pipeline);
                            }
                            shadow_pass.set_bind_group(1, &mesh.object_bind_group, &[]);
                            shadow_pass.set_bind_group(
                                2,
                                resources
                                    .deform
                                    .instance_bind_group_for(item.mesh_id, item.deform_instance),
                                &[],
                            );
                            shadow_pass.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
                            shadow_pass.set_index_buffer(
                                mesh.index_buffer.slice(..),
                                wgpu::IndexFormat::Uint32,
                            );
                            shadow_pass.draw_indexed(0..mesh.index_count, 0, 0..1);
                            shadow_draws += 1;
                        }
                    }
                } else {
                    for cascade in 0..light.effective_cascade_count {
                        let tile_col = (cascade % 2) as f32;
                        let tile_row = (cascade / 2) as f32;
                        shadow_pass.set_viewport(
                            tile_col * tile_px,
                            tile_row * tile_px,
                            tile_px,
                            tile_px,
                            0.0,
                            1.0,
                        );
                        shadow_pass.set_scissor_rect(
                            (tile_col * tile_px) as u32,
                            (tile_row * tile_px) as u32,
                            light.tile_size,
                            light.tile_size,
                        );

                        shadow_pass.set_bind_group(
                            0,
                            &resources.shadow_bind_group,
                            &[cascade as u32 * 256],
                        );

                        let cascade_frustum = crate::camera::frustum::Frustum::from_view_proj(
                            &light.cascade_view_projs[cascade],
                        );

                        for item in scene_items.iter() {
                            if item.settings.hidden {
                                continue;
                            }
                            if !item.settings.cast_shadows {
                                continue;
                            }
                            if item.settings.opacity < 1.0 {
                                continue;
                            }
                            let Some(mesh) = resources.mesh_store.get(item.mesh_id) else {
                                continue;
                            };

                            let world_aabb = mesh
                                .aabb
                                .transformed(&glam::Mat4::from_cols_array_2d(&item.model));
                            if cascade_frustum.cull_aabb(&world_aabb) {
                                continue;
                            }

                            // Two-sided materials cast through the cull-none
                            // pipeline (see the instanced path above).
                            if item.material.is_two_sided() {
                                shadow_pass.set_pipeline(&resources.shadow_pipeline_two_sided);
                            } else {
                                shadow_pass.set_pipeline(&resources.shadow_pipeline);
                            }
                            shadow_pass.set_bind_group(1, &mesh.object_bind_group, &[]);
                            shadow_pass.set_bind_group(
                                2,
                                resources
                                    .deform
                                    .instance_bind_group_for(item.mesh_id, item.deform_instance),
                                &[],
                            );
                            shadow_pass.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
                            shadow_pass.set_index_buffer(
                                mesh.index_buffer.slice(..),
                                wgpu::IndexFormat::Uint32,
                            );
                            shadow_pass.draw_indexed(0..mesh.index_count, 0, 0..1);
                            shadow_draws += 1;
                        }
                    }
                }

                // Item-type plugin shadow casting: per-cascade dispatch
                // with viewport + scissor + cascade bind group set up by
                // the lib. The plugin map is read through a raw pointer
                // captured before the mutable resources borrow split off.
                if !plugins.is_empty() && !frame.scene.plugin_items.is_empty() {
                    for cascade in 0..light.effective_cascade_count {
                        let tile_col = (cascade % 2) as f32;
                        let tile_row = (cascade / 2) as f32;
                        shadow_pass.set_viewport(
                            tile_col * tile_px,
                            tile_row * tile_px,
                            tile_px,
                            tile_px,
                            0.0,
                            1.0,
                        );
                        shadow_pass.set_scissor_rect(
                            (tile_col * tile_px) as u32,
                            (tile_row * tile_px) as u32,
                            light.tile_size,
                            light.tile_size,
                        );
                        shadow_pass.set_bind_group(
                            0,
                            &resources.shadow_bind_group,
                            &[cascade as u32 * 256],
                        );
                        let ctx = crate::plugin_api::ShadowCastContext {
                            cascade_idx: cascade as u32,
                            light_view_proj: light.cascade_view_projs[cascade],
                            camera: &frame.camera.render_camera,
                            viewport_index: frame.camera.viewport_index,
                            frame_index: plugin_frame_index,
                        };
                        for (name, plugin) in plugins.iter() {
                            if let Some(items) = frame.scene.plugin_items.get(*name) {
                                plugin.cast_shadow_pass(&mut shadow_pass, &ctx, items.as_ref());
                            }
                        }
                    }
                }

                drop(shadow_pass);
                last_stats.shadow_draw_calls = shadow_draws;
            }
            queue.submit(std::iter::once(encoder.finish()));
        }

        // ----------------------------------------------------------------
        // Point-light cubemap shadow passes.
        //
        // One depth-only render pass per (slot, face). Reuses the cascade
        // shadow rasterisation conventions (front-face culling, opaque
        // casters only) but writes linear distance-to-light to frag_depth
        // via `shadow_point_pipeline`. Per-face culling uses the standard
        // CPU frustum from the face's view-projection.
        // ----------------------------------------------------------------
        if lighting.shadows_enabled
            && !scene_items.is_empty()
            && !light.point_shadow_faces.is_empty()
        {
            // Make sure each casting item's `mesh.object_uniform_buf` carries
            // the item's current world model matrix. When instancing is on,
            // the per-item write-buffer pass earlier in `prepare_scene_internal`
            // skips items that go through the instanced path, leaving the
            // shared per-mesh uniform stale. The cascade shadow pass works
            // around this by drawing instanced items through a different
            // pipeline; the point shadow path here renders every caster
            // through `shadow_point_pipeline` (non-instanced), so it needs
            // a fresh per-mesh write here. Multi-item-per-mesh scenes need a
            // dedicated per-item shadow uniform; the single-item case is the
            // priority bug fix.
            for item in scene_items.iter() {
                if item.settings.hidden
                    || !item.settings.cast_shadows
                    || item.settings.opacity < 1.0
                {
                    continue;
                }
                let Some(mesh) = resources.mesh_store.get(item.mesh_id) else {
                    continue;
                };
                // Only the model matrix (offset 0, 64 bytes) is read by
                // `shadow_point.wgsl`. Write just that prefix so we don't
                // clobber the rest of the per-mesh `ObjectUniform`.
                queue.write_buffer(
                    &mesh.object_uniform_buf,
                    0,
                    bytemuck::cast_slice(&item.model),
                );
            }

            let mut enc = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
                label: Some("point_shadow_encoder"),
            });
            for fc in &light.point_shadow_faces {
                let layer = fc.slot * 6 + fc.face;
                let view = &resources.point_shadow_face_views[layer as usize];
                let mut pass = enc.begin_render_pass(&wgpu::RenderPassDescriptor {
                    label: Some("point_shadow_face_pass"),
                    color_attachments: &[],
                    depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
                        view,
                        depth_ops: Some(wgpu::Operations {
                            load: wgpu::LoadOp::Clear(1.0),
                            store: wgpu::StoreOp::Store,
                        }),
                        stencil_ops: None,
                    }),
                    timestamp_writes: None,
                    occlusion_query_set: None,
                });
                pass.set_pipeline(&resources.shadow_point_pipeline);
                let dyn_offset = layer * POINT_FACE_STRIDE as u32;
                pass.set_bind_group(0, &resources.shadow_point_face_bind_group, &[dyn_offset]);

                let face_frustum = crate::camera::frustum::Frustum::from_view_proj(&fc.view_proj);

                for item in scene_items.iter() {
                    if item.settings.hidden
                        || !item.settings.cast_shadows
                        || item.settings.opacity < 1.0
                    {
                        continue;
                    }
                    let Some(mesh) = resources.mesh_store.get(item.mesh_id) else {
                        continue;
                    };
                    let world_aabb = mesh
                        .aabb
                        .transformed(&glam::Mat4::from_cols_array_2d(&item.model));
                    if face_frustum.cull_aabb(&world_aabb) {
                        continue;
                    }
                    pass.set_bind_group(1, &mesh.object_bind_group, &[]);
                    pass.set_bind_group(
                        2,
                        resources
                            .deform
                            .instance_bind_group_for(item.mesh_id, item.deform_instance),
                        &[],
                    );
                    pass.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
                    pass.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint32);
                    pass.draw_indexed(0..mesh.index_count, 0, 0..1);
                }
            }
            queue.submit(std::iter::once(enc.finish()));
        }

        if shadow_instrument && lighting.shadows_enabled {
            // Force the just-submitted shadow work to finish so the measured time
            // reflects shadow GPU execution rather than landing later at present.
            // Other work submitted before this point in prepare is minor, so this
            // is a good attribution of the shadow cost.
            device
                .poll(wgpu::PollType::Wait {
                    submission_index: None,
                    timeout: Some(std::time::Duration::from_millis(2000)),
                })
                .ok();
            tracing::debug!(
                target: "viewport_lib::shadow",
                ms = shadow_start.elapsed().as_secs_f32() * 1000.0,
                cascades = light.effective_cascade_count,
                atlas = resources.shadow_atlas_size,
                draws = last_stats.shadow_draw_calls,
                point_faces = light.point_shadow_faces.len(),
                "shadow pass + gpu completion"
            );
        }
    }
}