concinnity-device 0.18.64

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
// src/directx/post/ssao.rs
//
// SSAO (GTAO) for the D3D12 backend. Owns the GTAO horizon-search kernel
// pipeline, the depth-aware blur pipeline, and the `encode_ssao` per-frame
// encoder. The view normal + linear depth it samples come from the unified
// G-buffer pre-pass (post/gbuffer.rs).
//
// The main pass samples `ssao.ao_srv_gpu` (the blurred occlusion) to modulate
// its ambient term; when SSAO is disabled the renderer binds the 1×1 white
// fallback (built once in init/effects.rs) so the multiplier is a pass-through
// 1.0. Mirrors src/metal/post/ssao.rs.

use windows::Win32::Foundation::RECT;
use windows::Win32::Graphics::Direct3D12::*;
use windows::Win32::Graphics::Dxgi::Common::*;

use crate::gfx::render_types::SsaoParams;

use crate::directx::com;
use crate::directx::context::{DxContext, dump_on_err};
use crate::directx::pipeline::serialize_desc_and_create;
use crate::directx::slang_builtins;
use crate::directx::texture::{
    create_rt_target, transition_barrier, write_format_rtv, write_format_srv,
};

// Single-channel occlusion target format. 1.0 = unoccluded; the main pass
// multiplies the ambient term by this value. Both the GTAO kernel and the
// depth-aware blur target this format.
pub(crate) const SSAO_OCCLUSION_FORMAT: DXGI_FORMAT = DXGI_FORMAT_R8_UNORM;

// Shader compilation

// Compiled bytecode for every SSAO shader stage.
struct SsaoShaders {
    fullscreen_vs: Vec<u8>,
    kernel_ps: Vec<u8>,
    blur_ps: Vec<u8>,
}

// Compile every SSAO shader stage. Both the kernel + blur are fullscreen
// passes that read the unified G-buffer; neither has a geometry input.
fn compile_ssao_shaders(hot_reload: bool) -> Result<SsaoShaders, String> {
    Ok(SsaoShaders {
        fullscreen_vs: slang_builtins::FULLSCREEN_VERT.compile(hot_reload)?,
        kernel_ps: slang_builtins::SSAO_KERNEL.compile(hot_reload)?,
        blur_ps: slang_builtins::SSAO_BLUR.compile(hot_reload)?,
    })
}

// Root signatures + PSOs

// Root signature for the GTAO kernel fullscreen pass: four 32-bit root
// constants at b0 (SsaoParams: radius, intensity, tan_half_fov_y, aspect),
// a 1-SRV descriptor table at t0 (the pre-pass G-buffer), and a static
// linear-clamp sampler at s0.
fn create_ssao_kernel_root_signature(device: &ID3D12Device) -> Result<ID3D12RootSignature, String> {
    let gbuffer_range = D3D12_DESCRIPTOR_RANGE {
        RangeType: D3D12_DESCRIPTOR_RANGE_TYPE_SRV,
        NumDescriptors: 1,
        BaseShaderRegister: 0, // t0
        RegisterSpace: 0,
        OffsetInDescriptorsFromTableStart: D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,
    };
    let params = [
        D3D12_ROOT_PARAMETER {
            ParameterType: D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS,
            Anonymous: D3D12_ROOT_PARAMETER_0 {
                Constants: D3D12_ROOT_CONSTANTS {
                    ShaderRegister: 0,
                    RegisterSpace: 0,
                    Num32BitValues: 4,
                },
            },
            ShaderVisibility: D3D12_SHADER_VISIBILITY_PIXEL,
        },
        D3D12_ROOT_PARAMETER {
            ParameterType: D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE,
            Anonymous: D3D12_ROOT_PARAMETER_0 {
                DescriptorTable: D3D12_ROOT_DESCRIPTOR_TABLE {
                    NumDescriptorRanges: 1,
                    pDescriptorRanges: &gbuffer_range,
                },
            },
            ShaderVisibility: D3D12_SHADER_VISIBILITY_PIXEL,
        },
    ];
    let static_sampler = D3D12_STATIC_SAMPLER_DESC {
        Filter: D3D12_FILTER_MIN_MAG_MIP_LINEAR,
        AddressU: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
        AddressV: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
        AddressW: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
        ComparisonFunc: D3D12_COMPARISON_FUNC_ALWAYS,
        BorderColor: D3D12_STATIC_BORDER_COLOR_OPAQUE_BLACK,
        MinLOD: 0.0,
        MaxLOD: f32::MAX,
        ShaderRegister: 0,
        RegisterSpace: 0,
        ShaderVisibility: D3D12_SHADER_VISIBILITY_PIXEL,
        ..Default::default()
    };
    let desc = D3D12_ROOT_SIGNATURE_DESC {
        NumParameters: params.len() as u32,
        pParameters: params.as_ptr(),
        NumStaticSamplers: 1,
        pStaticSamplers: &static_sampler,
        Flags: D3D12_ROOT_SIGNATURE_FLAG_NONE,
    };
    serialize_desc_and_create(device, &desc, "ssao kernel root sig")
}

// Root signature for the depth-aware blur pass: two 1-SRV descriptor tables
// (raw occlusion at t0, G-buffer at t1) and static linear-clamp samplers at
// s0 / s1 -- one per source, because slangc splits each combined sampler in the
// single source into its own texture/sampler pair.
fn create_ssao_blur_root_signature(device: &ID3D12Device) -> Result<ID3D12RootSignature, String> {
    let ao_range = D3D12_DESCRIPTOR_RANGE {
        RangeType: D3D12_DESCRIPTOR_RANGE_TYPE_SRV,
        NumDescriptors: 1,
        BaseShaderRegister: 0, // t0
        RegisterSpace: 0,
        OffsetInDescriptorsFromTableStart: D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,
    };
    let gbuffer_range = D3D12_DESCRIPTOR_RANGE {
        RangeType: D3D12_DESCRIPTOR_RANGE_TYPE_SRV,
        NumDescriptors: 1,
        BaseShaderRegister: 1, // t1
        RegisterSpace: 0,
        OffsetInDescriptorsFromTableStart: D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND,
    };
    let params = [
        D3D12_ROOT_PARAMETER {
            ParameterType: D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE,
            Anonymous: D3D12_ROOT_PARAMETER_0 {
                DescriptorTable: D3D12_ROOT_DESCRIPTOR_TABLE {
                    NumDescriptorRanges: 1,
                    pDescriptorRanges: &ao_range,
                },
            },
            ShaderVisibility: D3D12_SHADER_VISIBILITY_PIXEL,
        },
        D3D12_ROOT_PARAMETER {
            ParameterType: D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE,
            Anonymous: D3D12_ROOT_PARAMETER_0 {
                DescriptorTable: D3D12_ROOT_DESCRIPTOR_TABLE {
                    NumDescriptorRanges: 1,
                    pDescriptorRanges: &gbuffer_range,
                },
            },
            ShaderVisibility: D3D12_SHADER_VISIBILITY_PIXEL,
        },
    ];
    // s0 / s1: raw occlusion, G-buffer. Identical descriptors; the split is the
    // shader's, not the pass's.
    let static_samplers = [0u32, 1].map(|reg| D3D12_STATIC_SAMPLER_DESC {
        Filter: D3D12_FILTER_MIN_MAG_MIP_LINEAR,
        AddressU: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
        AddressV: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
        AddressW: D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
        ComparisonFunc: D3D12_COMPARISON_FUNC_ALWAYS,
        BorderColor: D3D12_STATIC_BORDER_COLOR_OPAQUE_BLACK,
        MinLOD: 0.0,
        MaxLOD: f32::MAX,
        ShaderRegister: reg,
        RegisterSpace: 0,
        ShaderVisibility: D3D12_SHADER_VISIBILITY_PIXEL,
        ..Default::default()
    });
    let desc = D3D12_ROOT_SIGNATURE_DESC {
        NumParameters: params.len() as u32,
        pParameters: params.as_ptr(),
        NumStaticSamplers: static_samplers.len() as u32,
        pStaticSamplers: static_samplers.as_ptr(),
        Flags: D3D12_ROOT_SIGNATURE_FLAG_NONE,
    };
    serialize_desc_and_create(device, &desc, "ssao blur root sig")
}

// PSO for the fullscreen GTAO kernel and the depth-aware blur. Both write
// `SSAO_OCCLUSION_FORMAT`, share the fullscreen-triangle VS, and disable
// depth + blending.
fn create_ssao_fullscreen_pso(
    device: &ID3D12Device,
    root_sig: &ID3D12RootSignature,
    vs: &[u8],
    ps: &[u8],
    label: &str,
) -> Result<ID3D12PipelineState, String> {
    let pso_desc = D3D12_GRAPHICS_PIPELINE_STATE_DESC {
        pRootSignature: com::borrowed(root_sig),
        VS: D3D12_SHADER_BYTECODE {
            pShaderBytecode: vs.as_ptr() as _,
            BytecodeLength: vs.len(),
        },
        PS: D3D12_SHADER_BYTECODE {
            pShaderBytecode: ps.as_ptr() as _,
            BytecodeLength: ps.len(),
        },
        PrimitiveTopologyType: D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE,
        NumRenderTargets: 1,
        RTVFormats: {
            let mut a = [DXGI_FORMAT_UNKNOWN; 8];
            a[0] = SSAO_OCCLUSION_FORMAT;
            a
        },
        DSVFormat: DXGI_FORMAT_UNKNOWN,
        SampleDesc: DXGI_SAMPLE_DESC {
            Count: 1,
            Quality: 0,
        },
        SampleMask: u32::MAX,
        RasterizerState: D3D12_RASTERIZER_DESC {
            FillMode: D3D12_FILL_MODE_SOLID,
            CullMode: D3D12_CULL_MODE_NONE,
            FrontCounterClockwise: true.into(),
            DepthClipEnable: false.into(),
            ..Default::default()
        },
        DepthStencilState: D3D12_DEPTH_STENCIL_DESC {
            DepthEnable: false.into(),
            DepthWriteMask: D3D12_DEPTH_WRITE_MASK_ZERO,
            StencilEnable: false.into(),
            ..Default::default()
        },
        BlendState: D3D12_BLEND_DESC {
            RenderTarget: {
                let mut arr = [D3D12_RENDER_TARGET_BLEND_DESC::default(); 8];
                arr[0] = D3D12_RENDER_TARGET_BLEND_DESC {
                    BlendEnable: false.into(),
                    RenderTargetWriteMask: D3D12_COLOR_WRITE_ENABLE_ALL.0 as u8,
                    ..Default::default()
                };
                arr
            },
            ..Default::default()
        },
        ..Default::default()
    };
    // SAFETY: `desc` outlives this synchronous call, and so do the root signature, shader bytecode
    // and input-element array whose raw pointers it borrows.
    unsafe { crate::directx::pso_library::create_graphics(device, &pso_desc) }
        .map_err(|e| format!("create ssao {label} PSO: {e}"))
}

// Resources

// SSAO resources held by `DxContext` when `PostProcessConfig.ssao` is on.
// Drops cleanly with the context: all D3D12 objects are COM-refcounted.
pub(in crate::directx) struct SsaoResources {
    // Resolved authored tunables; turned into a per-frame `SsaoParams` push.
    pub(in crate::directx) settings: crate::gfx::ssao::SsaoSettings,

    // Raw GTAO kernel output (R8) and the blurred final occlusion (R8) the
    // main pass samples.
    pub(in crate::directx) ao_raw: ID3D12Resource,
    pub(in crate::directx) ao_raw_rtv: D3D12_CPU_DESCRIPTOR_HANDLE,
    pub(in crate::directx) ao_raw_srv_gpu: D3D12_GPU_DESCRIPTOR_HANDLE,
    // The blurred `ao_output` the main pass samples is the graph's transient and
    // is owned by `DxContext::transient_pool` (a placed resource); SSAO holds
    // only its RTV (blur writes it) + SRV (main samples it), written from the
    // pooled resource at build / resize time.
    pub(in crate::directx) ao_rtv: D3D12_CPU_DESCRIPTOR_HANDLE,
    pub(in crate::directx) ao_srv_gpu: D3D12_GPU_DESCRIPTOR_HANDLE,

    // GTAO horizon-search kernel + depth-aware blur (fullscreen triangle).
    pub(in crate::directx) kernel_root_sig: ID3D12RootSignature,
    pub(in crate::directx) kernel_pso: ID3D12PipelineState,
    pub(in crate::directx) blur_root_sig: ID3D12RootSignature,
    pub(in crate::directx) blur_pso: ID3D12PipelineState,
}

// GPU device handles the SSAO builder needs: the device and the optional debug
// info queue. They always travel together through `new`.
#[derive(Clone, Copy)]
pub(in crate::directx) struct SsaoDeviceCtx<'a> {
    pub device: &'a ID3D12Device,
    pub info_queue: Option<&'a ID3D12InfoQueue>,
}

// Descriptor handles for the SSAO raw + blurred occlusion targets. Each target
// has a CPU RTV plus a (CPU, GPU) SRV pair reserved in the parent heaps.
#[derive(Clone, Copy)]
pub(in crate::directx) struct SsaoDescriptorHandles {
    pub ao_raw_rtv: D3D12_CPU_DESCRIPTOR_HANDLE,
    pub ao_raw_srv: (D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_GPU_DESCRIPTOR_HANDLE),
    pub ao_rtv: D3D12_CPU_DESCRIPTOR_HANDLE,
    pub ao_srv: (D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_GPU_DESCRIPTOR_HANDLE),
}

impl SsaoResources {
    // Build all SSAO resources. Called from `DxContext::new` only when the
    // world's `PostProcessConfig` enables SSAO. `ao_raw_*` / `ao_*` reserve
    // heap slots in the SRV + RTV heaps the caller laid out at init; the view
    // normal + depth the kernel samples come from the unified G-buffer pre-pass.
    pub(in crate::directx) fn new(
        ctx: SsaoDeviceCtx,
        width: u32,
        height: u32,
        settings: crate::gfx::ssao::SsaoSettings,
        handles: SsaoDescriptorHandles,
        // The pooled `ao_output` resource (placed in `DxContext::transient_pool`);
        // SSAO writes its RTV + SRV but does not own it.
        ao_resource: &ID3D12Resource,
        hot_reload: bool,
    ) -> Result<Self, String> {
        let SsaoDeviceCtx { device, info_queue } = ctx;
        let SsaoDescriptorHandles {
            ao_raw_rtv,
            ao_raw_srv,
            ao_rtv,
            ao_srv,
        } = handles;
        // Raw occlusion is SSAO-internal (committed); the blurred `ao` is the
        // pooled `ao_output`, so SSAO only writes its RTV + SRV.
        let ao_raw = create_rt_target(device, width, height, SSAO_OCCLUSION_FORMAT)?;
        write_format_rtv(device, &ao_raw, ao_raw_rtv, SSAO_OCCLUSION_FORMAT);
        write_format_srv(device, &ao_raw, ao_raw_srv.0, SSAO_OCCLUSION_FORMAT);
        write_format_rtv(device, ao_resource, ao_rtv, SSAO_OCCLUSION_FORMAT);
        write_format_srv(device, ao_resource, ao_srv.0, SSAO_OCCLUSION_FORMAT);

        // Pipelines.
        let shaders = compile_ssao_shaders(hot_reload)?;
        let kernel_root_sig = dump_on_err(info_queue, create_ssao_kernel_root_signature(device))?;
        let kernel_pso = dump_on_err(
            info_queue,
            create_ssao_fullscreen_pso(
                device,
                &kernel_root_sig,
                &shaders.fullscreen_vs,
                &shaders.kernel_ps,
                "kernel",
            ),
        )?;
        let blur_root_sig = dump_on_err(info_queue, create_ssao_blur_root_signature(device))?;
        let blur_pso = dump_on_err(
            info_queue,
            create_ssao_fullscreen_pso(
                device,
                &blur_root_sig,
                &shaders.fullscreen_vs,
                &shaders.blur_ps,
                "blur",
            ),
        )?;

        Ok(Self {
            settings,
            ao_raw,
            ao_raw_rtv,
            ao_raw_srv_gpu: ao_raw_srv.1,
            ao_rtv,
            ao_srv_gpu: ao_srv.1,
            kernel_root_sig,
            kernel_pso,
            blur_root_sig,
            blur_pso,
        })
    }
}

// Replacement SSAO PSOs returned by [`rebuild_ssao_pipelines`]. The caller
// swaps them in atomically only if every build succeeded. Mirrors the safety
// pattern used by Metal's shader hot-reload.
pub(in crate::directx) struct RebuiltSsaoPipelines {
    pub kernel_pso: ID3D12PipelineState,
    pub blur_pso: ID3D12PipelineState,
}

impl SsaoResources {
    // Rebuild the raw + blurred occlusion targets at a new resolution. The
    // descriptor *slots* stay where they were; only the resources backing them
    // change.
    pub(in crate::directx) fn resize_to(
        &mut self,
        device: &ID3D12Device,
        width: u32,
        height: u32,
        srv_cpu_base: D3D12_CPU_DESCRIPTOR_HANDLE,
        srv_gpu_base: D3D12_GPU_DESCRIPTOR_HANDLE,
        // The rebuilt pooled `ao_output` resource; SSAO rewrites its RTV + SRV.
        ao_resource: &ID3D12Resource,
    ) -> Result<(), String> {
        let srv_cpu = |gpu: D3D12_GPU_DESCRIPTOR_HANDLE| D3D12_CPU_DESCRIPTOR_HANDLE {
            ptr: srv_cpu_base.ptr + (gpu.ptr - srv_gpu_base.ptr) as usize,
        };

        self.ao_raw = create_rt_target(device, width, height, SSAO_OCCLUSION_FORMAT)?;
        write_format_rtv(device, &self.ao_raw, self.ao_raw_rtv, SSAO_OCCLUSION_FORMAT);
        write_format_srv(
            device,
            &self.ao_raw,
            srv_cpu(self.ao_raw_srv_gpu),
            SSAO_OCCLUSION_FORMAT,
        );

        write_format_rtv(device, ao_resource, self.ao_rtv, SSAO_OCCLUSION_FORMAT);
        write_format_srv(
            device,
            ao_resource,
            srv_cpu(self.ao_srv_gpu),
            SSAO_OCCLUSION_FORMAT,
        );

        Ok(())
    }
}

// Rebuild every SSAO PSO against fresh shader source. Reuses each PSO's
// existing root signature, so descriptor-table layouts stay stable. Returns
// the new PSOs for the caller to swap into the live `SsaoResources`.
pub(in crate::directx) fn rebuild_ssao_pipelines(
    device: &ID3D12Device,
    ssao: &SsaoResources,
    hot_reload: bool,
    info_queue: Option<&ID3D12InfoQueue>,
) -> Result<RebuiltSsaoPipelines, String> {
    let shaders = compile_ssao_shaders(hot_reload)?;
    let kernel_pso = dump_on_err(
        info_queue,
        create_ssao_fullscreen_pso(
            device,
            &ssao.kernel_root_sig,
            &shaders.fullscreen_vs,
            &shaders.kernel_ps,
            "kernel",
        ),
    )?;
    let blur_pso = dump_on_err(
        info_queue,
        create_ssao_fullscreen_pso(
            device,
            &ssao.blur_root_sig,
            &shaders.fullscreen_vs,
            &shaders.blur_ps,
            "blur",
        ),
    )?;
    Ok(RebuiltSsaoPipelines {
        kernel_pso,
        blur_pso,
    })
}

// Encoder

impl DxContext {
    // GPU descriptor handle of the AO SRV the main pass should sample.
    // Returns the blurred SSAO output when SSAO is on, otherwise the 1x1
    // white fallback so the ambient multiplier is a constant 1.0.
    pub(in crate::directx) fn ssao_ao_srv_gpu(&self) -> D3D12_GPU_DESCRIPTOR_HANDLE {
        match &self.ssao.resources {
            Some(s) => s.ao_srv_gpu,
            None => self.ssao.white_srv_gpu,
        }
    }

    // Encode the SSAO depth + normal pre-pass, the GTAO horizon-search
    // kernel, and the depth-aware blur. Called from `encode_main_pass` after
    // the main-pass RT/viewport setup so the main fragment shader can sample
    // the blurred occlusion. No-op when SSAO is disabled.
    pub(in crate::directx) fn encode_ssao(
        &self,
        cmd: &ID3D12GraphicsCommandList,
        fov_y_radians: f32,
        aspect: f32,
    ) {
        let ssao = match &self.ssao.resources {
            Some(s) => s,
            None => return,
        };

        // SSAO reads the unified G-buffer pre-pass (view normal + linear
        // depth) and runs no geometry redraw of its own; skip if the G-buffer
        // is absent.
        let gbuffer_srv = match &self.gbuffer {
            Some(g) => g.normal_depth_srv_gpu,
            None => return,
        };
        let params = ssao.settings.params(fov_y_radians, aspect);
        let w = self.extent.render_width;
        let h = self.extent.render_height;

        // The kernel + blur are fullscreen passes; restore the viewport /
        // scissor / primitive topology the (now removed) geometry pre-pass used
        // to leave bound. This pass records into its own command list, where the
        // topology starts UNDEFINED, so it must be set here for the draws below.
        // SAFETY: the command list is in the recording state, and every resource, descriptor and
        // slice these commands name is live for the call.
        unsafe {
            let vp = D3D12_VIEWPORT {
                TopLeftX: 0.0,
                TopLeftY: 0.0,
                Width: w as f32,
                Height: h as f32,
                MinDepth: 0.0,
                MaxDepth: 1.0,
            };
            cmd.RSSetViewports(&[vp]);
            let scissor = RECT {
                left: 0,
                top: 0,
                right: w as i32,
                bottom: h as i32,
            };
            cmd.RSSetScissorRects(&[scissor]);
            cmd.IASetPrimitiveTopology(
                windows::Win32::Graphics::Direct3D::D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST,
            );
        }

        // Kernel: GTAO horizon search over the G-buffer → raw occlusion.
        let to_rt = transition_barrier(
            &ssao.ao_raw,
            D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
            D3D12_RESOURCE_STATE_RENDER_TARGET,
        );
        // SAFETY: the command list is in the recording state, and every resource, descriptor and
        // slice these commands name is live for the call.
        unsafe { cmd.ResourceBarrier(&[to_rt]) };
        // SAFETY: the command list is in the recording state, and every resource, descriptor and
        // slice these commands name is live for the call.
        unsafe {
            cmd.OMSetRenderTargets(1, Some(&ssao.ao_raw_rtv), false, None);
            cmd.SetPipelineState(&ssao.kernel_pso);
            cmd.SetGraphicsRootSignature(&ssao.kernel_root_sig);
            cmd.SetDescriptorHeaps(&[Some(self.descriptors.srv_heap.clone())]);
            cmd.SetGraphicsRoot32BitConstants(
                0,
                4,
                &params as *const SsaoParams as *const std::ffi::c_void,
                0,
            );
            cmd.SetGraphicsRootDescriptorTable(1, gbuffer_srv);
            cmd.IASetVertexBuffers(0, None);
            cmd.IASetIndexBuffer(None);
            cmd.DrawInstanced(3, 1, 0, 0);
        }
        let raw_to_psr = transition_barrier(
            &ssao.ao_raw,
            D3D12_RESOURCE_STATE_RENDER_TARGET,
            D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
        );
        // SAFETY: the command list is in the recording state, and every resource, descriptor and
        // slice these commands name is live for the call.
        unsafe { cmd.ResourceBarrier(&[raw_to_psr]) };

        // Blur: depth-aware smoothing of raw occlusion → final AO. The blurred
        // `ao` is the graph's `ao_output` resource: its transition into
        // RENDER_TARGET before this draw and back to PIXEL_SHADER_RESOURCE for
        // the main pass is graph-driven (the executor emits ao_output's
        // `barriers_before` around the SsaoBlur and Main passes), so no inline
        // barrier on `ao` is issued here. `ao_raw` stays inline above.
        // SAFETY: the command list is in the recording state, and every resource, descriptor and
        // slice these commands name is live for the call.
        unsafe {
            cmd.OMSetRenderTargets(1, Some(&ssao.ao_rtv), false, None);
            cmd.SetPipelineState(&ssao.blur_pso);
            cmd.SetGraphicsRootSignature(&ssao.blur_root_sig);
            cmd.SetGraphicsRootDescriptorTable(0, ssao.ao_raw_srv_gpu);
            cmd.SetGraphicsRootDescriptorTable(1, gbuffer_srv);
            cmd.IASetVertexBuffers(0, None);
            cmd.IASetIndexBuffer(None);
            cmd.DrawInstanced(3, 1, 0, 0);
        }

        // Restore the SRV + sampler heaps the main pass expects.
        // SAFETY: the command list is in the recording state, and every resource, descriptor and
        // slice these commands name is live for the call.
        unsafe {
            cmd.SetDescriptorHeaps(&[
                Some(self.descriptors.srv_heap.clone()),
                Some(self.descriptors.sampler_heap.clone()),
            ]);
        }
    }
}