concinnity-device 0.19.119

GPU backends (Metal, Vulkan, DirectX) behind a device facade for Concinnity
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
//! Post and world effects: the temporal upscaler, the fixed descriptor slots of
//! the live-toggleable passes, TAA and the screen-space passes sharing the
//! unified G-buffer, and the decal, fog, particle, auto-exposure, raymarch,
//! planar reflection and transparent resources.

use concinnity_core::components::{GlassPanel, WaterSurface};
use concinnity_core::gfx::auto_exposure;
use concinnity_core::gfx::render_types::{DrawObject, LightUniforms};
use concinnity_core::render::backend_init::{PlanarBudget, PostSettings, SdfVolumeSource, WorldFx};
use concinnity_core::render::decal::{self, DecalRecord};
use concinnity_core::render::error::{RenderError, RenderResult};
use concinnity_core::render::lights;
use concinnity_core::render::particles::{self, ParticleEmitterRecord};
use concinnity_core::render::planar_reflection::PlanarReflectors;
use concinnity_core::render::post::device::PostExtent;
use concinnity_core::render::post::ssao::settings::SsaoSettings;
use concinnity_core::render::volumetric_fog::FogSettings;
use windows::Win32::Graphics::Direct3D12::*;

use super::heap_layout::{DSV_GBUFFER_DEPTH_SLOT, DSV_GLASS_REFLECTION_DEPTH_SLOT, RtvHeapLayout};
use super::{Features, InitGpu, heaps};
use crate::directx::auto_exposure::AutoExposureState;
use crate::directx::context::{
    DxDescriptors, DxSceneAssets, DxTargets, FRAMES, SwapchainState, dump_on_err,
};
use crate::directx::decal::DecalState;
use crate::directx::descriptor_slot::SamplerSlot;
use crate::directx::draw::shadow::ShadowState;
use crate::directx::fog::FogState;
use crate::directx::particle::ParticleState;
use crate::directx::planar::PlanarReflectionSet;
use crate::directx::post::descriptors::PostDescriptors;
use crate::directx::post::gbuffer::{GbufferResources, GbufferSlots};
use crate::directx::post::post_device::DxPostDevice;
use crate::directx::post::ssao::{SsaoResources, SsaoState};
use crate::directx::post::ssgi::SsgiResources;
use crate::directx::post::ssr::SsrResources;
use crate::directx::post::taa::TaaResources;
use crate::directx::post::upscale::UpscaleState;
use crate::directx::quality::QualitySlotHandles;
use crate::directx::raymarch::RaymarchResources;
use crate::directx::texture::{create_fallback_white_resource, write_texture_srv};
use crate::directx::transparent::TransparentResources;

// Temporal upscaler (FSR3 via the FidelityFX SDK). Built ahead of the HDR /
// depth / post-effect targets, because its resolved render dimensions decide
// the size of every scene target. When the world's
// `PostProcessConfig.temporal_upscaling` is on AND the FFX DLL loads + the
// context creates successfully, the backend is `Some` and the scene renders at
// `output * upscale_scale`; FSR reconstructs the drawable resolution into the
// upscaler's output texture, which bloom + composite sample. Falls back
// silently (logs a warning, leaves render == output) when the SDK isn't on
// `PATH` or the GPU rejects the context build, so a missing SDK degrades to
// native-resolution TAA rather than a low-res bilinear stretch.
pub(super) fn build_upscale(
    gpu: &InitGpu<'_>,
    descriptors: &DxDescriptors,
    output: (u32, u32),
    post: &PostSettings,
) -> RenderResult<UpscaleState> {
    let hw = gpu.hw;
    let layout = &descriptors.layout;
    let (width, height) = output;
    let mut requested = crate::upscale_sdk::UpscaleRequest::from_post(post);
    let upscaler = if post.temporal_upscaling {
        crate::directx::post::upscale::build_upscaler(
            crate::directx::post::upscale::UpscaleDevice {
                device: &hw.device,
                command_queue: &hw.command_queue,
            },
            (width, height),
            post.upscale_scale,
            crate::directx::post::upscale::UpscalerDescriptors {
                uav_cpu: descriptors.slot_cpu(layout.upscale_uav_slot),
                srv_cpu: descriptors.slot_cpu(layout.upscale_srv_slot),
                srv_gpu: descriptors.slot_gpu(layout.upscale_srv_slot),
            },
            &mut requested,
        )?
    } else {
        None
    };
    if let Some(u) = &upscaler {
        let (render_w, render_h) = u.extent().render;
        tracing::info!(
            "DirectX: temporal upscaling active: scene render {}x{}, drawable {}x{}",
            render_w,
            render_h,
            width,
            height
        );
    }
    Ok(UpscaleState {
        backend: upscaler,
        requested,
        jitter: std::cell::Cell::new([0.0, 0.0]),
    })
}

// The fixed descriptor slots of the live-toggleable Quality effects, stashed so
// the runtime `apply_quality_settings` can build a launched-off feature into
// its slot without re-deriving the heap layout. The init builds take their
// slots from here too.
pub(super) fn build_quality_slots(
    gpu: &InitGpu<'_>,
    descriptors: &DxDescriptors,
    swapchain: &SwapchainState,
    targets: &DxTargets,
    rtv: &RtvHeapLayout,
) -> QualitySlotHandles {
    let layout = &descriptors.layout;
    let srv = |slot| (descriptors.slot_cpu(slot), descriptors.slot_gpu(slot));
    let dsv_descriptor_size =
        heaps::descriptor_size(&gpu.hw.device, D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
    QualitySlotHandles {
        rt_output_rtv: swapchain.rtv(rtv.rt_output_slot),
        rt_output_srv: srv(layout.rt_output_srv_slot),
        gbuffer: GbufferSlots {
            normal_depth_rtv: swapchain.rtv(rtv.gbuffer_base_slot),
            normal_depth_srv: srv(layout.gbuffer_srv_base_slot),
            roughness_rtv: swapchain.rtv(rtv.gbuffer_base_slot + 1),
            roughness_srv: srv(layout.gbuffer_srv_base_slot + 1),
            velocity_rtv: swapchain.rtv(rtv.gbuffer_base_slot + 2),
            velocity_srv: srv(layout.gbuffer_srv_base_slot + 2),
            depth_dsv: heaps::cpu_handle(
                &targets.depth.heap,
                dsv_descriptor_size,
                DSV_GBUFFER_DEPTH_SLOT,
            ),
        },
        glass_reflection: {
            let base = layout.glass_reflection_srv_base_slot;
            crate::directx::transparent::GlassReflectionSlots {
                rtv: [
                    swapchain.rtv(rtv.glass_reflection_base_slot),
                    swapchain.rtv(rtv.glass_reflection_base_slot + 1),
                ],
                dsv: heaps::cpu_handle(
                    &targets.depth.heap,
                    dsv_descriptor_size,
                    DSV_GLASS_REFLECTION_DEPTH_SLOT,
                ),
                srv_cpu: std::array::from_fn(|i| descriptors.slot_cpu(base + i)),
                windows: std::array::from_fn(|w| descriptors.slot_gpu(base + 2 * w)),
            }
        },
    }
}

// The device every shared post pass builds its pipelines and targets through
// at init. Nothing encodes before the context exists, so it carries no probe
// set.
pub(super) fn post_device<'a>(
    gpu: &InitGpu<'a>,
    descriptors: &'a DxDescriptors,
    post: &'a PostDescriptors,
) -> DxPostDevice<'a> {
    DxPostDevice {
        device: &gpu.hw.device,
        descriptors: post,
        srv_heap: &descriptors.srv_heap,
        info_queue: gpu.hw.info_queue.as_ref(),
        probes: None,
        hot_reload: gpu.hot_reload,
    }
}

// TAA history-resolve resources. Sized at render-res; the motion it
// reprojects through comes from the unified G-buffer pre-pass.
pub(super) fn build_taa(
    post_device: &DxPostDevice<'_>,
    features: &Features,
    targets: &DxTargets,
) -> RenderResult<Option<TaaResources>> {
    let taa = if features.taa_enabled {
        Some(TaaResources::new(
            post_device,
            targets.extent.render_width,
            targets.extent.render_height,
        )?)
    } else {
        None
    };
    Ok(taa)
}

// SSAO: 1x1 white fallback always populated so the main pass binds a
// pass-through occlusion when SSAO is off. The shared depth copy, kernel and
// blur take their targets' descriptors from the post block.
pub(super) fn build_ssao(
    gpu: &InitGpu<'_>,
    post_device: &DxPostDevice<'_>,
    descriptors: &DxDescriptors,
    targets: &DxTargets,
    settings: Option<SsaoSettings>,
) -> RenderResult<SsaoState> {
    let hw = gpu.hw;
    let device = hw.alloc.device();
    let white_slot = descriptors.layout.ssao_white_srv_slot;
    let ssao_white = create_fallback_white_resource(&hw.alloc)?;
    write_texture_srv(device, &ssao_white, descriptors.slot_cpu(white_slot));
    let ssao = match settings {
        Some(settings) => {
            let ao_output = targets
                .transient_pool
                .resource_for("ao_output")
                .ok_or_else(|| {
                    RenderError::Other(
                        "transient pool missing ao_output while SSAO is enabled".into(),
                    )
                })?;
            Some(SsaoResources::new(
                post_device,
                settings,
                PostExtent {
                    width: targets.extent.render_width,
                    height: targets.extent.render_height,
                },
                ao_output,
            )?)
        }
        None => None,
    };
    Ok(SsaoState {
        resources: ssao,
        white: ssao_white,
        white_srv_gpu: descriptors.slot_gpu(white_slot),
    })
}

// SSR: a fullscreen resolve reading the unified G-buffer pre-pass. The
// resources are built whenever SSR *or* SSGI *or* RT reflections are on (all
// reuse the G-buffer); `post.ssr` (the resolve half) stays `None` for a
// SSGI-only or RT-only build, which then holds no resolve or target.
pub(super) fn build_ssr(
    post_device: &DxPostDevice<'_>,
    targets: &DxTargets,
    post: &PostSettings,
) -> RenderResult<Option<SsrResources>> {
    let ssr = if post.ssr.is_some() || post.ssgi.is_some() || post.rt_reflections.is_some() {
        Some(SsrResources::new(
            post_device,
            targets.extent.render_width,
            targets.extent.render_height,
            post.ssr,
        )?)
    } else {
        None
    };
    Ok(ssr)
}

// SSGI: a Hi-Z traced, temporally accumulated trace over the unified G-buffer
// pre-pass. Its pyramids and accumulation live in the shared pass; the
// composite blends straight into the scene.
pub(super) fn build_ssgi(
    post_device: &DxPostDevice<'_>,
    targets: &DxTargets,
    post: &PostSettings,
) -> RenderResult<Option<SsgiResources>> {
    let ssgi = match post.ssgi {
        Some(settings) => Some(SsgiResources::new(
            post_device,
            targets.extent.render_width,
            targets.extent.render_height,
            settings,
        )?),
        None => None,
    };
    Ok(ssgi)
}

// Unified G-buffer pre-pass resources. Built whenever any screen-space
// consumer drives it (see `Features::gbuffer_enabled`). Its three MRT RTVs sit
// at the tail of the RTV heap (after the decal RTV), its private depth DSV
// right after the shadow DSVs, and its three SRVs in the reserved
// `gbuffer_srv_base_slot` block. The skinned PSO builds lazily in
// `upload_skinned` once the joint-bound vertex layout exists.
pub(super) fn build_gbuffer(
    gpu: &InitGpu<'_>,
    targets: &DxTargets,
    slots: &QualitySlotHandles,
    gbuffer_enabled: bool,
) -> RenderResult<Option<GbufferResources>> {
    let gbuffer = if gbuffer_enabled {
        // The three color targets are pooled, so the pool (built with the
        // render targets, before this) is what owns them.
        let pooled = targets.transient_pool.gbuffer_pooled().ok_or_else(|| {
            RenderError::Other("transient pool missing the gbuffer color targets".into())
        })?;
        Some(GbufferResources::new(
            crate::directx::post::gbuffer::GbufferDeviceCtx {
                alloc: &gpu.hw.alloc,
                info_queue: gpu.hw.info_queue.as_ref(),
                hot_reload: gpu.hot_reload,
            },
            crate::directx::post::gbuffer::GbufferExtent {
                width: targets.extent.render_width,
                height: targets.extent.render_height,
            },
            slots.gbuffer,
            &pooled,
        )?)
    } else {
        None
    };
    Ok(gbuffer)
}

// Projected decals: pipeline + unit-cube buffers + per-frame uniform rings.
// Always built so runtime `add_decal` works from a world that started with
// none; pre-authored decals get their albedo SRV written here.
pub(super) fn build_decals(
    gpu: &InitGpu<'_>,
    descriptors: &DxDescriptors,
    targets: &DxTargets,
    scene: &DxSceneAssets,
    decals: Vec<DecalRecord>,
) -> RenderResult<DecalState> {
    let hw = gpu.hw;
    let decal_srv_base_slot = descriptors.layout.decal_srv_base_slot;
    let decals_state = Some(crate::directx::decal::DecalResources::new(
        &hw.alloc,
        targets.hdr.msaa_samples,
        decal_srv_base_slot,
        targets.main_depth_srv_gpu,
        hw.info_queue.as_ref(),
        gpu.hot_reload,
    )?);
    // Pre-authored decals: write each one's albedo SRV into its reserved
    // heap slot. Runtime adds via `DxContext::add_decal` follow the same
    // pattern.
    if decals.len() > crate::directx::decal::MAX_DECALS {
        return Err(RenderError::Other(format!(
            "decals: {} authored decals exceed MAX_DECALS ({})",
            decals.len(),
            crate::directx::decal::MAX_DECALS
        )));
    }
    let last_tex = scene.textures.len().saturating_sub(1);
    for (i, rec) in decals.iter().enumerate() {
        let tex_idx = rec.texture_slot.min(last_tex);
        write_texture_srv(
            &hw.device,
            &scene.textures[tex_idx],
            descriptors.slot_cpu(decal_srv_base_slot + i),
        );
    }
    // The slot table the decal pass draws from. Each authored decal takes
    // the slot whose albedo SRV was just written above, in the same order.
    let mut decal_set = decal::DecalSet::new(crate::directx::decal::MAX_DECALS, FRAMES);
    for record in decals {
        decal_set.insert(record).map_err(|_| {
            RenderError::Other("decals: authored decals exceed MAX_DECALS".to_string())
        })?;
    }
    Ok(DecalState {
        state: decals_state,
        set: decal_set,
    })
}

// Volumetric fog: pipeline + per-frame uniform ring. Built only when the world
// declared a `VolumetricFog`; the encoder simply skips the pass when
// `settings` is `None`. The fog pass shares the main-depth SRV the decal pass
// binds.
pub(super) fn build_fog(
    gpu: &InitGpu<'_>,
    descriptors: &DxDescriptors,
    targets: &DxTargets,
    shadow: &ShadowState,
    settings: Option<FogSettings>,
    light_uniforms: &LightUniforms,
) -> RenderResult<FogState> {
    let hw = gpu.hw;
    let layout = &descriptors.layout;
    let fog_resources = if settings.is_some() {
        Some(crate::directx::fog::FogResources::new(
            &hw.alloc,
            crate::directx::fog::FogVolumeDescriptors {
                uav_cpu: descriptors.slot_cpu(layout.fog_froxel_uav_slot),
                uav_gpu: descriptors.slot_gpu(layout.fog_froxel_uav_slot),
                srv_cpu: descriptors.slot_cpu(layout.fog_froxel_srv_slot),
                srv_gpu: descriptors.slot_gpu(layout.fog_froxel_srv_slot),
            },
            crate::directx::fog::FogShaderResourceHandles {
                depth_srv_gpu: targets.main_depth_srv_gpu,
                shadow_srv_gpu: shadow.srv_gpu,
            },
            crate::directx::fog::FogDeviceParams {
                msaa_samples: targets.hdr.msaa_samples,
                hot_reload: gpu.hot_reload,
            },
            hw.info_queue.as_ref(),
        )?)
    } else {
        None
    };
    // The first directional light's direction and color * intensity, cached
    // for the volumetric-fog encoder since `LightUniforms` is uploaded rather
    // than pushed each frame. `update_directional_lights` re-derives both.
    Ok(FogState {
        resources: fog_resources,
        settings,
        sun_dir: shadow.light_dir,
        sun_color: lights::sun_color(light_uniforms),
    })
}

// Particles: compute + render pipelines + per-frame uniform rings, plus one
// persistent GPU pool per emitter. Built only when the world declared at least
// one emitter; the encoder skips the passes when `resources` is `None`, and
// runtime `add_emitter` builds the pipelines lazily the same way. The emitter
// cap matches the SRV-heap reservation.
pub(super) fn build_particles(
    gpu: &InitGpu<'_>,
    descriptors: &DxDescriptors,
    targets: &DxTargets,
    scene: &DxSceneAssets,
    particles: Vec<ParticleEmitterRecord>,
) -> RenderResult<ParticleState> {
    let hw = gpu.hw;
    let particle_srv_base_slot = descriptors.layout.particle_srv_base_slot;
    if particles.len() > crate::directx::particle::MAX_EMITTERS {
        return Err(RenderError::Other(format!(
            "particles: {} authored emitters exceed MAX_EMITTERS ({})",
            particles.len(),
            crate::directx::particle::MAX_EMITTERS
        )));
    }
    let (particle_resources, particle_records, particle_emitter_states) = if !particles.is_empty() {
        let resources = crate::directx::particle::ParticleResources::new(
            &hw.alloc,
            particle_srv_base_slot,
            targets.hdr.msaa_samples,
            targets.main_depth_srv_gpu,
            hw.info_queue.as_ref(),
            gpu.hot_reload,
        )?;
        let mut states: Vec<Option<crate::directx::particle::ParticleEmitterGpuState>> =
            Vec::with_capacity(particles.len());
        let last_tex = scene.textures.len().saturating_sub(1);
        for (i, rec) in particles.iter().enumerate() {
            let state = crate::directx::particle::build_emitter_gpu_state(&hw.alloc, rec)?;
            states.push(Some(state));
            // Write the per-emitter albedo SRV into its reserved heap slot.
            let tex_idx = rec.texture_slot.min(last_tex);
            write_texture_srv(
                &hw.device,
                &scene.textures[tex_idx],
                descriptors.slot_cpu(particle_srv_base_slot + i),
            );
        }
        let recs: Vec<Option<particles::ParticleEmitterRecord>> =
            particles.into_iter().map(Some).collect();
        (Some(resources), recs, states)
    } else {
        (None, Vec::new(), Vec::new())
    };
    Ok(ParticleState {
        resources: particle_resources,
        records: particle_records,
        emitter_state: particle_emitter_states,
        free_slots: Vec::new(),
        srv_base_slot: particle_srv_base_slot,
        last_elapsed: std::cell::Cell::new(0.0),
        frame_index: std::cell::Cell::new(0),
    })
}

// Auto-exposure: build the histogram + average compute pipelines plus
// the GPU buffers (histogram UAV, output UAV, per-frame readback)
// only when the world's PostProcessConfig opted in. With auto-exposure
// off every path below is None and the static authored EV continues
// to drive `post_process.exposure` unchanged.
pub(super) fn build_auto_exposure(
    gpu: &InitGpu<'_>,
    post: &PostSettings,
) -> RenderResult<AutoExposureState> {
    let hw = gpu.hw;
    let (resources, adaptation) = if let Some(settings) = post.auto_exposure.as_ref() {
        let resources = dump_on_err(
            hw.info_queue.as_ref(),
            crate::directx::auto_exposure::AutoExposureResources::new(&hw.alloc, gpu.hot_reload),
        )?;
        let adaptation =
            auto_exposure::ExposureAdaptation::new(*settings, post.auto_exposure_bias_ev);
        (Some(resources), Some(adaptation))
    } else {
        (None, None)
    };
    Ok(AutoExposureState {
        resources,
        adaptation,
        last_elapsed: 0.0,
    })
}

// Raymarched SDF volumes. Builds per-volume PSOs from each volume's
// distance-field payload and writes the raymarch SRV + sampler tables into
// their reserved blocks. With no volumes this returns `None` and the render
// graph never adds `PassId::Raymarch`. The
// shadow + IBL handles passed here mirror the matching slot-0/1/2
// bindings the main pass uses, so raymarched surfaces sample the
// same CSM cascades + IBL cubes as rasterized geometry.
pub(super) fn build_raymarch(
    gpu: &InitGpu<'_>,
    descriptors: &DxDescriptors,
    targets: &DxTargets,
    shadow: &ShadowState,
    scene: &DxSceneAssets,
    sdf_volumes: &[SdfVolumeSource],
) -> RenderResult<Option<RaymarchResources>> {
    let hw = gpu.hw;
    let raymarch_srv_base_slot = descriptors.layout.raymarch_srv_base_slot;
    let sampler_descriptor_size =
        heaps::descriptor_size(&hw.device, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
    let raymarch = RaymarchResources::try_new(
        crate::directx::raymarch::RaymarchDeviceContext {
            alloc: &hw.alloc,
            info_queue: hw.info_queue.as_ref(),
        },
        crate::directx::raymarch::RaymarchTargetConfig {
            width: targets.extent.render_width,
            height: targets.extent.render_height,
            msaa_samples: targets.hdr.msaa_samples,
        },
        crate::directx::raymarch::RaymarchSharedBindings {
            shadow_resource: shadow.resource.as_ref().map(|r| &r.resource),
            shadow_layers: shadow.layers,
            irradiance_resource: &scene.env_map.irradiance.resource,
            prefilter_resource: &scene.env_map.prefilter.resource,
        },
        crate::directx::raymarch::RaymarchDescriptorHandles {
            srv_base_cpu: descriptors.slot_cpu(raymarch_srv_base_slot),
            srv_base_gpu: descriptors.slot_gpu(raymarch_srv_base_slot),
            srv_descriptor_size: descriptors.srv_descriptor_size,
            sampler_base_cpu: heaps::cpu_handle(
                &descriptors.sampler_heap,
                sampler_descriptor_size,
                heaps::RAYMARCH_SAMPLER_BASE_SLOT,
            ),
            sampler_base_gpu: SamplerSlot::at(
                &descriptors.sampler_heap,
                sampler_descriptor_size,
                heaps::RAYMARCH_SAMPLER_BASE_SLOT,
            ),
            sampler_descriptor_size,
        },
        sdf_volumes,
        gpu.hot_reload,
    )?;
    Ok(raymarch)
}

// Planar reflections: group each transparent reflector's plane into a
// bounded set of distinct planes (near-coplanar reflectors share one mirror
// render; reflectors past the budget fall back to the probe cube). The
// distinct count sizes the reserved planar-resolve SRV block; `slots[i]` is
// reflector `i`'s resolve slot (or `None`). Planned before the descriptor
// heap is created so the block is sized first.
//
// Water first, then glass, matching the Metal backend, so the two slot
// ranges are `[..water_surfaces.len()]` and the rest.
pub(super) fn plan_planar(fx: &WorldFx, budget: PlanarBudget) -> PlanarReflectors {
    let planar = PlanarReflectors::plan(&fx.water_surfaces, &fx.glass_panels, budget);
    if planar.overflow() > 0 {
        tracing::warn!(
            "planar reflection: {} reflector plane(s) exceed the budget and fall back \
             to the box-projected probe cube",
            planar.overflow()
        );
    }
    planar
}

// One mirror-render resolve per distinct reflector plane (the
// `assign_planar_slots` representatives), each SRV in a reserved heap slot the
// transparent pass binds per record. `None` when no reflector was assigned a
// planar slot (no transparent content, or every plane degenerate / over
// budget). `n_cull` is the build-time draw-record count (matches
// `DxContext::cull_count`), which sizes each plane's region of the mirror-cull
// indirect buffer.
pub(super) fn build_planar_reflection(
    gpu: &InitGpu<'_>,
    descriptors: &DxDescriptors,
    targets: &DxTargets,
    planar: PlanarReflectors,
    n_cull: usize,
    clear_color: [f32; 4],
) -> RenderResult<Option<PlanarReflectionSet>> {
    let planar_resolve_srv_base_slot = descriptors.layout.planar_resolve_srv_base_slot;
    let planar_reflection = if planar.planes().is_empty() {
        None
    } else {
        let resolve_srv_cpu: Vec<_> = (0..planar.planes().len())
            .map(|i| descriptors.slot_cpu(planar_resolve_srv_base_slot + i))
            .collect();
        let resolve_srv_gpu: Vec<_> = (0..planar.planes().len())
            .map(|i| descriptors.slot_gpu(planar_resolve_srv_base_slot + i))
            .collect();
        Some(PlanarReflectionSet::new(
            &gpu.hw.alloc,
            crate::directx::planar::PlanarConfig {
                sample_count: targets.hdr.msaa_samples,
                width: targets.extent.render_width,
                height: targets.extent.render_height,
                n_cull,
            },
            planar,
            crate::directx::planar::PlanarTargets {
                resolve_srv_cpu: &resolve_srv_cpu,
                resolve_srv_gpu: &resolve_srv_gpu,
                clear_color,
            },
        )?)
    };
    Ok(planar_reflection)
}

pub(super) struct TransparentInputs<'a> {
    pub(super) descriptors: &'a DxDescriptors,
    pub(super) targets: &'a DxTargets,
    pub(super) reflection_slots: crate::directx::transparent::GlassReflectionSlots,
    // Per-axis divisor of the glass reflection pre-pass; 1 traces in place.
    pub(super) reflection_divisor: u32,
    pub(super) planar_slots: &'a [Option<usize>],
    pub(super) glass_panels: &'a [GlassPanel],
    pub(super) water_surfaces: &'a [WaterSurface],
    pub(super) draw_objects: &'a [DrawObject],
}

// The shared transparent pass and its producers: water surfaces, translucent
// glass panes, and see-through glass meshes. `Some` only when the world
// declared at least one of the three; the mesh case additionally needs a
// DXR-capable GPU, since its producer is ray-traced only and a pane-less,
// water-less world would otherwise build the whole pass for a producer that
// cannot exist. Shares the main-depth SRV with the decal pass; the scene-copy
// snapshot uses its own reserved heap slot. `planar_slots` gives each
// reflector its planar resolve slot (or `None` -> probe-cube fallback),
// numbered water first to match `plan_planar`.
pub(super) fn build_transparent(
    gpu: &InitGpu<'_>,
    inputs: TransparentInputs<'_>,
) -> RenderResult<Option<TransparentResources>> {
    let TransparentInputs {
        descriptors,
        targets,
        reflection_slots,
        reflection_divisor,
        planar_slots,
        glass_panels,
        water_surfaces,
        draw_objects,
    } = inputs;
    let hw = gpu.hw;
    let scene_copy_slot = descriptors.layout.transparent_scene_copy_srv_slot;
    // Layer 2 see-through glass is opt-in per `Material` (the `see_through`
    // arg, which implies `transparent`): see-through only looks right when the
    // space behind the glass is modeled. A material that is `transparent` but
    // NOT `see_through` renders as Layer 1 (opaque, low roughness, scene
    // reflections) = tinted reflective glass that hides the interior. This list
    // drives the transparent-pass producer, the opaque-pass skip and the
    // RT-BLAS exclude together.
    let seethrough_mesh_indices: Vec<usize> = draw_objects
        .iter()
        .enumerate()
        .filter(|(_, o)| o.material.transparent != 0 && o.material.see_through != 0)
        .map(|(i, _)| i)
        .collect();
    let has_seethrough_meshes = !seethrough_mesh_indices.is_empty() && hw.rt_capable;
    let transparent =
        if glass_panels.is_empty() && water_surfaces.is_empty() && !has_seethrough_meshes {
            None
        } else {
            let (water_planar_slots, glass_planar_slots) =
                planar_slots.split_at(water_surfaces.len());
            Some(TransparentResources::new(
                crate::directx::transparent::TransparentDeviceCtx { alloc: &hw.alloc },
                crate::directx::transparent::TransparentBuildConfig {
                    msaa_samples: targets.hdr.msaa_samples,
                    width: targets.extent.render_width,
                    height: targets.extent.render_height,
                    hot_reload: gpu.hot_reload,
                    reflection_divisor,
                },
                crate::directx::transparent::TransparentSceneTargets {
                    scene_copy_srv_cpu: descriptors.slot_cpu(scene_copy_slot),
                    scene_copy_srv_gpu: descriptors.slot_gpu(scene_copy_slot),
                    depth_srv_gpu: targets.main_depth_srv_gpu,
                    reflection_slots,
                },
                crate::directx::transparent::TransparentContent {
                    glass_panels,
                    glass_planar_slots,
                    water_surfaces,
                    water_planar_slots,
                    seethrough_mesh_indices: &seethrough_mesh_indices,
                },
                hw.info_queue.as_ref(),
            )?)
        };
    Ok(transparent)
}