concinnity-device 0.18.65

GPU backends (Metal, Vulkan, DirectX) behind a device facade for Concinnity
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
// src/vulkan/main.rs
//
// Main scene pass for the Vulkan backend: linear-light HDR off-screen
// render that draws every visible static / instanced / skinned object
// into the multisampled HDR colour + depth attachments (resolved into
// `hdr_resolve` for the post stack). One Vulkan render pass with three
// sub-passes-by-pipeline:
//
//   1. Bindless build-time statics via `cmd_draw_indexed_indirect` over
//      the GPU-cull-written indirect buffer.
//   2. Legacy per-draw fallback (custom shaders + streamed `VoxelWorld`
//      chunks past `draw.n_objects`).
//   3. Instanced clusters via per-instance storage buffers.
//   4. Skinned meshes via LBS vertex shader + per-object joint sets.
//
// The shape mirrors `metal/draw/main.rs::encode_main_pass`; the graph
// executor in [`graph_exec.rs`](graph_exec.rs) dispatches
// `PassId::Main` here. The Shadow → Main `shadow_map` read edge in
// the frame graph pins Shadow before Main via toposort; the encoder
// itself only deals with the HDR pass.

use ash::vk;

use crate::gfx::frustum::Frustum;
use crate::vulkan::uniforms::MainPush;

use super::context::VkContext;

impl VkContext {
    // Recompute every instanced cluster's per-LOD-bucket partition for the
    // current camera and memcpy the bucket-ordered instance matrices into this
    // frame's mapped per-cluster SSBOs, storing the partition in
    // `instanced.lod_buckets` for every instanced draw site to read.
    //
    // Run on `&mut self` from `execute_graph` BEFORE the render-graph fan-out,
    // mirroring `prepare_particle_pass`: the SSAO / SSR / velocity pre-passes
    // run earlier in the graph than Main but share the same per-cluster upload
    // buffer, so the upload has to happen up front (every pre-pass + Main then
    // see this frame's bucket order). With LOD bucketing the buffer order
    // depends on `cam_pos`, so it cannot be deferred into the Main pass the way
    // the old single-LOD upload was. Mirrors `DxContext::build_instance_upload`.
    pub(in crate::vulkan) fn prepare_instanced_clusters(
        &mut self,
        frame_idx: usize,
        cam_pos: [f32; 3],
    ) {
        if self.instanced.clusters.is_empty() || self.instanced.buffers.is_empty() {
            return;
        }
        // Re-shape on a runtime cluster-count change (asset hot-reload), then
        // clear each row in place to reuse its heap allocation.
        if self.instanced.lod_buckets.len() != self.instanced.clusters.len() {
            self.instanced
                .lod_buckets
                .resize(self.instanced.clusters.len(), Vec::new());
        }
        const STRIDE: usize = std::mem::size_of::<[[f32; 4]; 4]>();
        for (cluster_idx, cluster) in self.instanced.clusters.iter().enumerate() {
            let row = &mut self.instanced.lod_buckets[cluster_idx];
            row.clear();
            if cluster.instances.is_empty() {
                continue;
            }
            let upload = &self.instanced.buffers[frame_idx][cluster_idx];
            let buckets = cluster.lod_buckets(cam_pos);
            row.reserve(buckets.len());
            let mut prefix_instances: usize = 0;
            for bucket in buckets {
                let count = bucket.instances.len();
                upload.write_slice(prefix_instances * STRIDE, &bucket.instances);
                prefix_instances += count;
                row.push(bucket);
            }
        }
    }

    // Encode the main HDR scene pass for frame slot `frame_idx`. Draws
    // every visible static / instanced / skinned object in the
    // `visible` set into the multisampled colour + depth attachments
    // of `framebuffers[frame_idx]`; the render pass resolves into
    // `hdr_resolve` (the post-stack input) on `cmd_end_render_pass`.
    //
    // Bindless draws come from the GPU-culled indirect buffer the
    // cull compute kernel wrote earlier this frame; legacy /
    // instanced / skinned draws iterate the CPU-culled `visible`
    // list. Cluster culling repeats the frustum / distance check
    // here because instanced clusters aren't in the BVH.
    pub(in crate::vulkan) fn encode_main_pass(
        &self,
        cmd: vk::CommandBuffer,
        frame_idx: usize,
        visible: &[u32],
        frustum: &Frustum,
        cam_pos: [f32; 3],
        world_hidden: bool,
    ) {
        let device = self.device.clone();
        let device = &device;
        let extent = self.render_extent;

        // Opaque menu backdrop, MSAA path: skip the main render pass entirely.
        // Beginning it would clear the MSAA colour+depth and, on
        // `end_render_pass`, resolve the (undrawn) MSAA colour into hdr_resolve:
        // a full-render-resolution resolve of a frame nothing presents (the
        // composite samples the post-stack scene + the opaque overlay on top).
        // On an immediate-mode GPU that resolve is the bulk of the paused
        // frame's GPU cost. Skipping it drops the main pass to a lone layout
        // barrier (~0us, so it falls off the passes HUD like Metal / DirectX),
        // and the barrier leaves hdr_resolve sampled-ready for the plain-world
        // composite path (no TAA / reflections sample it directly). Its
        // contents are irrelevant under the opaque overlay, matching the
        // DirectX paused path (which likewise skips the resolve). The
        // single-sample path below has no resolve to skip (hdr_resolve is the
        // colour attachment), so it keeps its cheap clear-only render pass.
        if world_hidden && self.msaa_samples != vk::SampleCountFlags::TYPE_1 {
            super::texture::transition_image_layout(
                device,
                cmd,
                self.hdr_resolve_images[frame_idx].image,
                vk::ImageLayout::UNDEFINED,
                vk::ImageLayout::SHADER_READ_ONLY_OPTIMAL,
                vk::ImageAspectFlags::COLOR,
            );
            return;
        }

        // Clears for the main HDR attachments. With MSAA on, the
        // resolve attachment doesn't need a clear (resolve overwrites);
        // a `ClearValue::default()` placeholder keeps the slice index
        // aligned with `main_render_pass`'s attachment count.
        let [r, g, b, a] = self.view.clear_color;
        let clear_color = vk::ClearValue {
            color: vk::ClearColorValue {
                float32: [r, g, b, a],
            },
        };
        let clear_depth = vk::ClearValue {
            depth_stencil: vk::ClearDepthStencilValue {
                depth: 1.0,
                stencil: 0,
            },
        };
        let clears: &[vk::ClearValue] = if self.msaa_samples != vk::SampleCountFlags::TYPE_1 {
            &[clear_color, clear_depth, vk::ClearValue::default()]
        } else {
            &[clear_color, clear_depth]
        };

        // Under two-pass occlusion, phase 1 renders into a variant render pass
        // that STORE's the MSAA colour (so `Main2` can load + composite onto
        // it) and leaves the colour in COLOR_ATTACHMENT_OPTIMAL. The clears are
        // identical (phase 1 still CLEAR's), so only the render pass differs.
        // The raymarch pass needs the same STORE-colour treatment (it loads +
        // re-resolves the MSAA colour after AutoExposure), so when raymarch is
        // active but two-pass occlusion is not, switch to its store-colour pass.
        let render_pass = if self.two_pass_occlusion_active() {
            self.cull
                .main_render_pass_phase1
                .as_ref()
                .unwrap_or(&self.main_render_pass)
        } else if let Some(rp) = self
            .raymarch
            .as_ref()
            .and_then(|r| r.main_store_color_pass.as_ref())
        {
            rp
        } else {
            &self.main_render_pass
        };
        let rp_begin = vk::RenderPassBeginInfo::default()
            .render_pass(render_pass.handle())
            .framebuffer(self.framebuffers[frame_idx].handle())
            .render_area(vk::Rect2D::default().extent(extent))
            .clear_values(clears);

        // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
        // these commands name is live for the call.
        unsafe { device.cmd_begin_render_pass(cmd, &rp_begin, vk::SubpassContents::INLINE) };

        // Opaque menu backdrop, single-sample path: the render pass above
        // already cleared hdr_resolve (the colour attachment) and there is no
        // resolve to skip, so just end immediately; nothing of the world draws
        // behind the menu. (The MSAA path returned earlier without beginning a
        // render pass at all.)
        if world_hidden {
            // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
            // these commands name is live for the call.
            unsafe { device.cmd_end_render_pass(cmd) };
            return;
        }

        // Viewport: negative height flips Y to match Metal coordinate system.
        let vp = vk::Viewport {
            x: 0.0,
            y: extent.height as f32,
            width: extent.width as f32,
            height: -(extent.height as f32),
            min_depth: 0.0,
            max_depth: 1.0,
        };
        let scissor = vk::Rect2D::default().extent(extent);
        // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
        // these commands name is live for the call.
        unsafe {
            device.cmd_set_viewport(cmd, 0, std::slice::from_ref(&vp));
            device.cmd_set_scissor(cmd, 0, std::slice::from_ref(&scissor));
        }

        // Geometry buffers, pipeline-layout-independent, so bound once for
        // both the bindless and legacy main sub-passes below.
        // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
        // these commands name is live for the call.
        unsafe {
            device.cmd_bind_vertex_buffers(cmd, 0, &[self.geometry.vertex_buffer.buffer()], &[0]);
            device.cmd_bind_index_buffer(
                cmd,
                self.geometry.index_buffer.buffer(),
                0,
                vk::IndexType::UINT32,
            );
        }

        // Build-time static objects render through the bindless pipeline
        // driven by the GPU-culled indirect command buffer the cull
        // compute pass wrote above. One `cmd_draw_indexed_indirect`
        // issues every build-time object's draw; culled / disabled objects
        // were written with `instance_count = 0` (a no-op). Each draw is
        // stateless apart from the object id, which rides `first_instance`
        // (Vulkan's `gl_InstanceIndex` includes it); model/material/textures
        // are fetched from the per-frame GpuObjectData SSBO + the bindless
        // texture pool. Streamed VoxelWorld chunks keep the legacy per-draw
        // pipeline below.
        let use_bindless = self.cull.bindless_pipeline.is_some() && self.cull_count() > 0;
        if use_bindless {
            let pipeline = self.wireframe_or(
                self.cull
                    .bindless_pipeline
                    .as_ref()
                    .expect("bindless pipeline is live"),
                self.wireframe.bindless.as_ref(),
            );
            let layout = self
                .cull
                .bindless_pipeline_layout
                .as_ref()
                .expect("bindless pipeline layout is live alongside its pipeline");
            // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
            // these commands name is live for the call.
            unsafe {
                device.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, pipeline.handle());
                device.cmd_bind_descriptor_sets(
                    cmd,
                    vk::PipelineBindPoint::GRAPHICS,
                    layout.handle(),
                    0,
                    &[
                        self.descriptors.global_sets[frame_idx],
                        self.cull.bindless_sets[frame_idx],
                    ],
                    &[],
                );
                // Indirect draw #1: the static + instance prefix
                // `[0, skinned_record_base())` against the static VB/IB bound above.
                // The skinned tail is drawn by a second indirect draw below (deformed
                // VB + skinned IB), reading the same indirect buffer from
                // `skinned_record_base()` on.
                //
                // Once per shader bucket: bucket 0 runs under the bindless pipeline
                // bound above, each later bucket under its material shader's own
                // pipeline. The cull kernel wrote every record's command into
                // exactly one bucket's region, so the regions never double-draw.
                device.cmd_draw_indexed_indirect(
                    cmd,
                    self.cull.indirect_buffers[frame_idx].buffer(),
                    0,
                    self.skinned_record_base() as u32,
                    std::mem::size_of::<vk::DrawIndexedIndirectCommand>() as u32,
                );
            }
            // GPU expands the indirect buffer to up to `draw.n_objects` draw
            // commands inside, but the call count surfaced to the profiler
            // is the host-side draw. Mirrors DirectX / Metal.
            self.inc_draw_calls(1);
            self.inc_draw_calls(self.draw_bucket_regions(
                cmd,
                self.cull.indirect_buffers[frame_idx].buffer(),
                self.skinned_record_base() as u32,
            ));
            // Restore the default bindless pipeline for the sub-paths below.
            if self.shader_bucket_count() > 1 {
                // SAFETY: `cmd` is a command buffer in the recording state, and every handle and
                // slice these commands name is live for the call.
                unsafe {
                    device.cmd_bind_pipeline(
                        cmd,
                        vk::PipelineBindPoint::GRAPHICS,
                        pipeline.handle(),
                    )
                };
            }
        }

        // Legacy per-draw main pass. Draws every visible object when the
        // bindless pass is inactive (custom shader / no build-time geometry);
        // otherwise only runtime clones (streamed VoxelWorld chunks now fold into
        // the bindless indirect draw as their own records). Shared with the
        // instanced + skinned passes' fragment shader.
        let legacy_needed = !use_bindless || !self.clone.slot_by_draw_idx.is_empty();
        let skip_seethrough = self.mesh_glass_active();
        if legacy_needed {
            // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
            // these commands name is live for the call.
            unsafe {
                device.cmd_bind_pipeline(
                    cmd,
                    vk::PipelineBindPoint::GRAPHICS,
                    self.wireframe_or(&self.main_pipeline, self.wireframe.main.as_ref())
                        .handle(),
                );
                device.cmd_bind_descriptor_sets(
                    cmd,
                    vk::PipelineBindPoint::GRAPHICS,
                    self.main_pipeline_layout.handle(),
                    0,
                    std::slice::from_ref(&self.descriptors.global_sets[frame_idx]),
                    &[],
                );
            }
            for &draw_idx in visible {
                let i = draw_idx as usize;
                if use_bindless && i < self.draw.n_objects {
                    continue; // build-time object, already drawn bindless
                }
                if use_bindless
                    && i >= self.draw.n_objects
                    && !self.clone.slot_by_draw_idx.contains_key(&i)
                {
                    continue; // streamed chunk, already drawn bindless (folded record)
                }
                let obj = &self.draw.objects[i];
                if !obj.visible || !obj.resident {
                    continue;
                }
                // See-through glass meshes (Layer 2) draw in the transparent
                // pass when the RT path is live, so skip them here: leaving one in
                // would both paint it opaque and stamp its depth over the scene
                // the refraction tap reads.
                if skip_seethrough && obj.material.see_through != 0 {
                    continue;
                }

                // Per-object descriptor set (albedo + normal map). Streamed
                // `VoxelWorld` chunks are appended past the build-time object
                // count and share one descriptor set bound to the world's
                // chunk material; build-time objects use their own baked
                // (albedo, normal) set; runtime clones (from
                // `clone_static_draw_object`) carry their own per-clone set
                // stored in `clone.object_sets` and looked up via
                // `clone.slot_by_draw_idx`.
                let obj_set = if i >= self.draw.n_objects {
                    if let Some(&offset) = self.clone.slot_by_draw_idx.get(&i) {
                        match self.clone.object_sets.get(offset) {
                            Some(&s) => s,
                            None => continue,
                        }
                    } else {
                        match self.chunk_stream.object_set {
                            Some(s) => s,
                            None => continue,
                        }
                    }
                } else {
                    self.descriptors.object_sets
                        [i.min(self.descriptors.object_sets.len().saturating_sub(1))]
                };

                // SAFETY: `cmd` is a command buffer in the recording state, and every handle and
                // slice these commands name is live for the call.
                unsafe {
                    device.cmd_bind_descriptor_sets(
                        cmd,
                        vk::PipelineBindPoint::GRAPHICS,
                        self.main_pipeline_layout.handle(),
                        1,
                        std::slice::from_ref(&obj_set),
                        &[],
                    );
                }

                // Per-frame active LOD pick. Streamed VoxelWorld chunks
                // (past `draw.n_objects`) never declare LOD alternates, so the
                // pick collapses to LOD0 for them.
                let d = crate::gfx::lod::camera_distance(obj, cam_pos);
                let (index_offset, index_count) = obj.active_lod(d);
                let push = MainPush {
                    model: obj.model,
                    roughness: obj.material.roughness,
                    metallic: obj.material.metallic,
                    _mpad0: 0.0,
                    _mpad1: 0.0,
                    tint: obj.material.tint,
                    _mpad2: 0.0,
                    emissive: obj.material.emissive,
                    _mpad3: 0.0,
                };
                // SAFETY: `cmd` is a command buffer in the recording state, and every handle and
                // slice these commands name is live for the call.
                unsafe {
                    device.cmd_push_constants(
                        cmd,
                        self.main_pipeline_layout.handle(),
                        vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT,
                        0,
                        std::slice::from_raw_parts(
                            &push as *const MainPush as *const u8,
                            std::mem::size_of::<MainPush>(),
                        ),
                    );
                    device.cmd_draw_indexed(
                        cmd,
                        index_count as u32,
                        1,
                        index_offset as u32,
                        obj.base_vertex,
                        0,
                    );
                }
                self.inc_draw_calls(1);
            }
        }

        // Instanced clusters main pass. Skipped when the bindless merge is active:
        // each instance is then a `GpuObjectData` record at `draw.n_objects + k` in the
        // cull buffers, drawn by the bindless `cmd_draw_indexed_indirect` above.
        if let (Some(inst_pipeline), Some(inst_pipeline_layout)) = (
            self.instanced.pipeline.as_ref(),
            self.instanced.pipeline_layout.as_ref(),
        ) && !self.instanced.clusters.is_empty()
            && !use_bindless
        {
            // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
            // these commands name is live for the call.
            unsafe {
                device.cmd_bind_pipeline(
                    cmd,
                    vk::PipelineBindPoint::GRAPHICS,
                    self.wireframe_or(inst_pipeline, self.wireframe.instanced.as_ref())
                        .handle(),
                );
                device.cmd_bind_descriptor_sets(
                    cmd,
                    vk::PipelineBindPoint::GRAPHICS,
                    inst_pipeline_layout.handle(),
                    0,
                    std::slice::from_ref(&self.descriptors.global_sets[frame_idx]),
                    &[],
                );
            }

            for (cluster_idx, cluster) in self.instanced.clusters.iter().enumerate() {
                if cluster.instances.is_empty() {
                    continue;
                }
                if cluster.cullable() {
                    if !frustum.intersects_aabb(cluster.cluster_bb_min, cluster.cluster_bb_max) {
                        continue;
                    }
                    if cluster.cull_distance > 0.0 {
                        let d2 = crate::gfx::frustum::aabb_distance_sq(
                            cam_pos,
                            cluster.cluster_bb_min,
                            cluster.cluster_bb_max,
                        );
                        if d2 > cluster.cull_distance * cluster.cull_distance {
                            continue;
                        }
                    }
                }

                // Per-cluster (albedo, normal) sampler + this frame's
                // bucket-ordered instance SSBO. The matrices were uploaded by
                // `prepare_instanced_clusters`; issue one draw per LOD bucket,
                // offsetting into the SSBO via `first_instance` (the instanced
                // VS reads `instances[gl_InstanceIndex]`).
                let Some(buckets) = self.instanced.lod_buckets.get(cluster_idx) else {
                    continue;
                };
                // SAFETY: `cmd` is a command buffer in the recording state, and every handle and
                // slice these commands name is live for the call.
                unsafe {
                    // Set 1: per-cluster (albedo, normal) sampler.
                    device.cmd_bind_descriptor_sets(
                        cmd,
                        vk::PipelineBindPoint::GRAPHICS,
                        inst_pipeline_layout.handle(),
                        1,
                        std::slice::from_ref(&self.instanced.object_sets[cluster_idx]),
                        &[],
                    );
                    // Set 2: per-instance storage buffer for this frame.
                    device.cmd_bind_descriptor_sets(
                        cmd,
                        vk::PipelineBindPoint::GRAPHICS,
                        inst_pipeline_layout.handle(),
                        2,
                        std::slice::from_ref(&self.instanced.sets[frame_idx][cluster_idx]),
                        &[],
                    );

                    let push = MainPush {
                        model: [[0.0; 4]; 4], // ignored by instanced VS
                        roughness: cluster.material.roughness,
                        metallic: cluster.material.metallic,
                        _mpad0: 0.0,
                        _mpad1: 0.0,
                        tint: cluster.material.tint,
                        _mpad2: 0.0,
                        emissive: cluster.material.emissive,
                        _mpad3: 0.0,
                    };
                    device.cmd_push_constants(
                        cmd,
                        inst_pipeline_layout.handle(),
                        vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT,
                        0,
                        std::slice::from_raw_parts(
                            &push as *const MainPush as *const u8,
                            std::mem::size_of::<MainPush>(),
                        ),
                    );
                    let mut first_instance: u32 = 0;
                    for bucket in buckets {
                        let count = bucket.instances.len() as u32;
                        device.cmd_draw_indexed(
                            cmd,
                            bucket.index_count as u32,
                            count,
                            bucket.index_offset as u32,
                            0,
                            first_instance,
                        );
                        first_instance += count;
                    }
                }
                self.inc_draw_calls(buckets.len() as u32);
            }
        }

        // Skinned meshes main pass. When the GPU-driven bindless fold is active,
        // skinned objects ride the same cull buffers as static + instances and are
        // drawn (as rigid deformed geometry) by a 2nd `cmd_draw_indexed_indirect`
        // over this frame's deformed-vertex buffer + the skinned index buffer,
        // reading the cull-written indirect buffer from `skinned_record_base()`. The
        // `encode_skin` compute pass (Cull graph arm) has already posed the deformed
        // buffer. Otherwise the legacy per-draw skinned pass runs (custom-shader
        // worlds, or a pure-skinned world with no static geometry to engage bindless).
        if use_bindless && self.draw.n_skinned > 0 {
            if let (Some(bindless_pipeline), Some(bindless_layout), Some(deformed)) = (
                self.cull.bindless_pipeline.as_ref(),
                self.cull.bindless_pipeline_layout.as_ref(),
                self.skinned.deformed.get(frame_idx),
            ) {
                // SAFETY: `cmd` is a command buffer in the recording state, and every handle and
                // slice these commands name is live for the call.
                unsafe {
                    device.cmd_bind_pipeline(
                        cmd,
                        vk::PipelineBindPoint::GRAPHICS,
                        self.wireframe_or(bindless_pipeline, self.wireframe.bindless.as_ref())
                            .handle(),
                    );
                    device.cmd_bind_descriptor_sets(
                        cmd,
                        vk::PipelineBindPoint::GRAPHICS,
                        bindless_layout.handle(),
                        0,
                        &[
                            self.descriptors.global_sets[frame_idx],
                            self.cull.bindless_sets[frame_idx],
                        ],
                        &[],
                    );
                    // Bind the deformed verts (base_vertex = 0, global skinned
                    // indexing) + the skinned IB.
                    device.cmd_bind_vertex_buffers(
                        cmd,
                        0,
                        std::slice::from_ref(&deformed.buffer),
                        &[0],
                    );
                    device.cmd_bind_index_buffer(
                        cmd,
                        self.skinned.index_buffer.buffer(),
                        0,
                        vk::IndexType::UINT32,
                    );
                    // Indirect draw #2: the skinned tail
                    // `[skinned_record_base(), cull_count())`, byte-offset into the
                    // same indirect command buffer.
                    let cmd_stride = std::mem::size_of::<vk::DrawIndexedIndirectCommand>();
                    device.cmd_draw_indexed_indirect(
                        cmd,
                        self.cull.indirect_buffers[frame_idx].buffer(),
                        (self.skinned_record_base() * cmd_stride) as u64,
                        self.draw.n_skinned as u32,
                        cmd_stride as u32,
                    );
                }
                self.inc_draw_calls(1);
            }
        } else if let (Some(sk_pipeline), Some(sk_pl)) = (
            self.skinned.pipeline.as_ref(),
            self.skinned.pipeline_layout.as_ref(),
        ) && !self.skinned.draw_objects.is_empty()
        {
            let (sk_vbuf, sk_ibuf) = self.skinned_geometry();
            // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
            // these commands name is live for the call.
            unsafe {
                device.cmd_bind_pipeline(
                    cmd,
                    vk::PipelineBindPoint::GRAPHICS,
                    self.wireframe_or(sk_pipeline, self.wireframe.skinned.as_ref())
                        .handle(),
                );
                device.cmd_bind_descriptor_sets(
                    cmd,
                    vk::PipelineBindPoint::GRAPHICS,
                    sk_pl.handle(),
                    0,
                    std::slice::from_ref(&self.descriptors.global_sets[frame_idx]),
                    &[],
                );
                device.cmd_bind_vertex_buffers(cmd, 0, std::slice::from_ref(&sk_vbuf), &[0]);
                device.cmd_bind_index_buffer(cmd, sk_ibuf, 0, vk::IndexType::UINT32);
            }
            for (i, obj) in self.skinned.draw_objects.iter().enumerate() {
                if !obj.visible {
                    continue;
                }
                // Pick the LOD by camera distance to the object's placement
                // (skinned meshes deform every frame, so they have no static
                // AABB); the shadow + SSR / SSAO pre-passes pick the same slice.
                let d = crate::gfx::lod::skinned_camera_distance(obj, cam_pos);
                let (index_offset, index_count) = obj.active_lod(d);
                let push = MainPush {
                    model: obj.model,
                    roughness: obj.material.roughness,
                    metallic: obj.material.metallic,
                    _mpad0: 0.0,
                    _mpad1: 0.0,
                    tint: obj.material.tint,
                    _mpad2: 0.0,
                    emissive: obj.material.emissive,
                    _mpad3: 0.0,
                };
                // SAFETY: `cmd` is a command buffer in the recording state, and every handle and
                // slice these commands name is live for the call.
                unsafe {
                    device.cmd_bind_descriptor_sets(
                        cmd,
                        vk::PipelineBindPoint::GRAPHICS,
                        sk_pl.handle(),
                        1,
                        std::slice::from_ref(&self.skinned.object_sets[i]),
                        &[],
                    );
                    device.cmd_bind_descriptor_sets(
                        cmd,
                        vk::PipelineBindPoint::GRAPHICS,
                        sk_pl.handle(),
                        2,
                        std::slice::from_ref(&self.skinned.joint_sets[frame_idx][i]),
                        &[],
                    );
                    device.cmd_push_constants(
                        cmd,
                        sk_pl.handle(),
                        vk::ShaderStageFlags::VERTEX | vk::ShaderStageFlags::FRAGMENT,
                        0,
                        std::slice::from_raw_parts(
                            &push as *const MainPush as *const u8,
                            std::mem::size_of::<MainPush>(),
                        ),
                    );
                    device.cmd_draw_indexed(cmd, index_count as u32, 1, index_offset as u32, 0, 0);
                }
                self.inc_draw_calls(1);
            }
        }

        // End the main scene pass. The render pass leaves the HDR resolve
        // image in SHADER_READ_ONLY_OPTIMAL for the composite pass to sample.
        // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
        // these commands name is live for the call.
        unsafe { device.cmd_end_render_pass(cmd) };
    }

    // Phase-2 main pass for two-pass occlusion. Loads (does not clear) the
    // phase-1 HDR colour + depth, re-runs the bindless indirect draw (static
    // objects + merged instances, both Hi-Z-tested through the cull buffer) over
    // the phase-2 indirect buffer `Cull2` wrote, and resolves the combined scene
    // into `hdr_resolve` for the post stack. Skinned geometry is not Hi-Z-culled,
    // so it was fully drawn in phase 1 and is not repeated here. A no-op unless
    // the bindless path is active with build-time geometry. Mirrors
    // `directx/draw/main.rs::encode_main_pass_phase2`.
    //
    // `bindless_pipeline` was created against `main_render_pass` but is used
    // here with `main_render_pass_phase2`; that is valid because the two passes
    // are render-pass-compatible (identical attachment count / formats / sample
    // counts, only load/store ops + layouts differ). Keep them so if the
    // phase-2 attachment set ever diverges, build a phase-2-specific pipeline.
    pub(in crate::vulkan) fn encode_main_pass_phase2(
        &self,
        cmd: vk::CommandBuffer,
        frame_idx: usize,
    ) {
        let (Some(render_pass), Some(pipeline), Some(layout)) = (
            self.cull.main_render_pass_phase2.as_ref(),
            self.cull.bindless_pipeline.as_ref(),
            self.cull.bindless_pipeline_layout.as_ref(),
        ) else {
            return;
        };
        let pipeline = self.wireframe_or(pipeline, self.wireframe.bindless.as_ref());
        if self.draw.n_objects == 0 || self.cull.indirect_buffers2.is_empty() {
            return;
        }
        let device = self.device.clone();
        let device = &device;
        let extent = self.render_extent;

        // The phase-2 render pass LOADs the phase-1 colour + depth. Because it
        // shares the main render pass's subpass dependency (so the shared
        // framebuffer + bindless pipeline stay render-pass-compatible), that
        // dependency does not order the phase-1 writes before this LOAD. Emit
        // the ordering explicitly here: phase-1 Main's colour + depth writes
        // (and HizBuild's depth restore) -> this pass's attachment load. Spans
        // the command-buffer boundary via submission order under parallel
        // recording.
        let load_barrier = vk::MemoryBarrier::default()
            .src_access_mask(
                vk::AccessFlags::COLOR_ATTACHMENT_WRITE
                    | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE,
            )
            .dst_access_mask(
                vk::AccessFlags::COLOR_ATTACHMENT_READ
                    | vk::AccessFlags::COLOR_ATTACHMENT_WRITE
                    | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_READ
                    | vk::AccessFlags::DEPTH_STENCIL_ATTACHMENT_WRITE,
            );
        // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
        // these commands name is live for the call.
        unsafe {
            device.cmd_pipeline_barrier(
                cmd,
                vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT
                    | vk::PipelineStageFlags::LATE_FRAGMENT_TESTS,
                vk::PipelineStageFlags::COLOR_ATTACHMENT_OUTPUT
                    | vk::PipelineStageFlags::EARLY_FRAGMENT_TESTS,
                vk::DependencyFlags::empty(),
                std::slice::from_ref(&load_barrier),
                &[],
                &[],
            );
        }

        // LOAD render pass: no clears (loadOp = LOAD / DONT_CARE), so no clear
        // values are required.
        let rp_begin = vk::RenderPassBeginInfo::default()
            .render_pass(render_pass.handle())
            .framebuffer(self.framebuffers[frame_idx].handle())
            .render_area(vk::Rect2D::default().extent(extent));
        // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
        // these commands name is live for the call.
        unsafe { device.cmd_begin_render_pass(cmd, &rp_begin, vk::SubpassContents::INLINE) };

        // Same negative-height viewport flip as the phase-1 main pass so the
        // disoccluded geometry rasterises into identical pixels.
        let vp = vk::Viewport {
            x: 0.0,
            y: extent.height as f32,
            width: extent.width as f32,
            height: -(extent.height as f32),
            min_depth: 0.0,
            max_depth: 1.0,
        };
        let scissor = vk::Rect2D::default().extent(extent);
        // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
        // these commands name is live for the call.
        unsafe {
            device.cmd_set_viewport(cmd, 0, std::slice::from_ref(&vp));
            device.cmd_set_scissor(cmd, 0, std::slice::from_ref(&scissor));
            device.cmd_bind_vertex_buffers(cmd, 0, &[self.geometry.vertex_buffer.buffer()], &[0]);
            device.cmd_bind_index_buffer(
                cmd,
                self.geometry.index_buffer.buffer(),
                0,
                vk::IndexType::UINT32,
            );

            device.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, pipeline.handle());
            device.cmd_bind_descriptor_sets(
                cmd,
                vk::PipelineBindPoint::GRAPHICS,
                layout.handle(),
                0,
                &[
                    self.descriptors.global_sets[frame_idx],
                    self.cull.bindless_sets[frame_idx],
                ],
                &[],
            );
            // Indirect draw #1: the static + instance prefix against the static
            // VB/IB bound above, once per shader bucket.
            device.cmd_draw_indexed_indirect(
                cmd,
                self.cull.indirect_buffers2[frame_idx].buffer(),
                0,
                self.skinned_record_base() as u32,
                std::mem::size_of::<vk::DrawIndexedIndirectCommand>() as u32,
            );
        }
        self.inc_draw_calls(1);
        self.inc_draw_calls(self.draw_bucket_regions(
            cmd,
            self.cull.indirect_buffers2[frame_idx].buffer(),
            self.skinned_record_base() as u32,
        ));

        // Indirect draw #2: the skinned tail against the deformed VB + skinned IB.
        // The descriptor sets bound above persist, so only the pipeline (a bucket
        // may have replaced it) and the vertex/index buffers rebind. Skinned draws
        // always render bucket 0.
        if self.draw.n_skinned > 0
            && let Some(deformed) = self.skinned.deformed.get(frame_idx)
        {
            let cmd_stride = std::mem::size_of::<vk::DrawIndexedIndirectCommand>();
            // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
            // these commands name is live for the call.
            unsafe {
                device.cmd_bind_pipeline(cmd, vk::PipelineBindPoint::GRAPHICS, pipeline.handle());
                device.cmd_bind_vertex_buffers(
                    cmd,
                    0,
                    std::slice::from_ref(&deformed.buffer),
                    &[0],
                );
                device.cmd_bind_index_buffer(
                    cmd,
                    self.skinned.index_buffer.buffer(),
                    0,
                    vk::IndexType::UINT32,
                );
                device.cmd_draw_indexed_indirect(
                    cmd,
                    self.cull.indirect_buffers2[frame_idx].buffer(),
                    (self.skinned_record_base() * cmd_stride) as u64,
                    self.draw.n_skinned as u32,
                    cmd_stride as u32,
                );
            }
            self.inc_draw_calls(1);
        }

        // End the phase-2 pass. The render pass resolves the combined phase-1 +
        // phase-2 MSAA colour into `hdr_resolve` and leaves it
        // SHADER_READ_ONLY_OPTIMAL, so the post stack reads the combined scene.
        // SAFETY: `cmd` is a command buffer in the recording state, and every handle and slice
        // these commands name is live for the call.
        unsafe { device.cmd_end_render_pass(cmd) };
    }
}