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
//! Vulkan shader hot-reload: `VkContext::reload_shaders` rebuilds every live
//! built-in pipeline from the checkout's shader sources. The `reload-shaders`
//! debug command sets the shared flag, and the main thread polls it at the top
//! of `draw_frame`.
//!
//! Entirely a dev-loop concern: the flag exists only when `VkContext::new` is
//! called with `hot_reload = true`. Production `cn run` never sets it. Mirrors
//! `directx/hot_reload.rs` and `metal/hot_reload.rs`.

use ash::vk;
use concinnity_core::render::backend_init;
use concinnity_core::render::error::{RenderError, RenderResult};
use concinnity_core::render::world_pipelines::{check_rebuild, replace_bucket};
use std::sync::atomic::Ordering;

use super::context::VkContext;
use super::pipeline::{
    BindlessSpv, BucketPipelines, build_bucket_pipeline, compile_bindless_shaders,
    compile_composite_shaders, compile_cull_shader, compile_cull_shader_phase2,
    compile_text_shaders, create_composite_pipeline, create_text_pipeline,
};
use super::pipeline_desc::compute_pipeline;

// Rebuild a feature's pipeline(s) into a temporary only when `$cond` says the
// feature is live, propagating any compile/create error out of the enclosing
// `reload_shaders`. Expands to `Some(build?)` when live, `None` otherwise. A
// feature whose resources sit behind an `Option` maps over that `Option`
// instead, so its build borrows them directly. Mirrors
// `directx/hot_reload.rs::rebuild_if_live!`.
macro_rules! rebuild_if_live {
    ($cond:expr_2021, $build:expr_2021 $(,)?) => {
        if $cond { Some($build?) } else { None }
    };
}

impl VkContext {
    // True when the shared shader-reload flag is set. Cheap atomic load;
    // called at the top of `draw_frame`. Returns false when hot-reload is
    // off so the production path never enters the reload branch.
    pub(in crate::vulkan) fn shader_reload_requested(&self) -> bool {
        self.hot_reload
            .reload_pending
            .as_ref()
            .map(|f| f.load(Ordering::SeqCst))
            .unwrap_or(false)
    }

    // Clear the pending-reload flag. Called after `reload_shaders`
    // regardless of outcome so a failed rebuild does not loop forever.
    pub(in crate::vulkan) fn clear_shader_reload_flag(&self) {
        if let Some(flag) = &self.hot_reload.reload_pending {
            flag.store(false, Ordering::SeqCst);
        }
    }

    // Rebuild every built-in Vulkan pipeline from disk-resident source.
    // Each pipeline is constructed into a temporary first; only when every
    // rebuild succeeds does the context swap them in (after destroying
    // the displaced ones). Any GLSL compile or pipeline-create failure
    // logs the underlying message and leaves the live pipelines untouched:
    // a typo in a shader edit won't crash the running session.
    //
    // Covers every runtime-bundled pipeline whose source lives in
    // `vulkan/shaders/`: composite, text, bloom (prefilter / downsample /
    // upsample), every shader bucket's main and G-buffer pre-pass pair (when
    // the bindless main pass is live), GPU-cull compute, auto-exposure
    // (build + average), projected-decal, volumetric-fog, SSAO (depth copy,
    // kernel, blur), SSR (resolve), the reflection composite (blur, composite),
    // TAA (resolve), the sky, and the G-buffer pre-pass with the sky's motion
    // behind it. The shadow / instanced / skinned pipelines remain out of
    // scope; same split as DirectX. The caller
    // has already `device_wait_idle`'d so swapping pipelines out from
    // under in-flight command buffers is safe.
    pub(in crate::vulkan) fn reload_shaders(&mut self) -> RenderResult<()> {
        if !self.hot_reload.enabled {
            return Ok(());
        }
        self.hot_reload.generation += 1;
        let device = self.hw.device.clone();
        let device = &device;
        let hr = true;

        // Build every replacement into a temporary first. A `?` early-return
        // here means we never overwrite a live pipeline with a failed build:
        // any compile error leaves the running session rendering with the
        // previous shader source.

        // Composite (always live).
        let (composite_vs, composite_ps) = compile_composite_shaders(hr)?;
        let composite_pipeline = create_composite_pipeline(
            device,
            self.composite.render_pass.handle(),
            self.composite.pipeline_layout.handle(),
            &composite_vs,
            &composite_ps,
        )?;

        // Text (only when the world declared text atlases).
        let text_pipeline = rebuild_if_live!(self.text.pipeline.is_some(), {
            let (tv, tf) = compile_text_shaders(hr)?;
            create_text_pipeline(
                device,
                self.composite.render_pass.handle(),
                self.text.pipeline_layout.handle(),
                &tv,
                &tf,
                vk::SampleCountFlags::TYPE_1,
            )
        });

        // Bloom (live from init to teardown, 3 pipelines).
        let bloom_rebuilt = rebuild_if_live!(
            self.bloom.is_some(),
            concinnity_core::render::post::bloom::build_pipelines(&self.post_device(0))
        );

        // Every shader bucket's main and G-buffer pre-pass pipelines, from the
        // engine's freshly compiled programs; a world Shader's own files are
        // spliced into the same templates, so they are rebuilt against them
        // too. Each bucket rebuilds as a pair, so shading and the G-buffer
        // never compile from different templates.
        let bindless_main_pipeline = rebuild_if_live!(
            self.cull.bindless_pipeline_layout.is_some() && self.cull.bindless_pipeline.is_some(),
            {
                let engine = compile_bindless_shaders(hr)?;
                let pipelines =
                    self.build_world_main_pipeline(self.world_shader.as_ref(), &engine)?;
                let world_buckets = self.rebuild_world_buckets(&engine)?;
                Ok::<_, RenderError>((pipelines, world_buckets, engine))
            }
        );
        let sky_pipeline = crate::vulkan::sky::build_sky_pipeline(
            &self.hw.device,
            self.sky.layout(),
            self.targets.main_render_pass.handle(),
            self.targets.msaa_samples,
            hr,
        )?;
        // The sky's motion behind the G-buffer pre-pass; the pre-pass's own
        // pipelines rebuild with the main pass's above.
        let gbuffer_sky_pipeline = self
            .gbuffer
            .as_ref()
            .map(|gb| {
                gb.sky
                    .rebuild_pipeline(device, gb.prepass_render_pass.handle())
            })
            .transpose()?;
        // The cull kernel, and its phase-2 twin (two-pass occlusion) when built:
        // the same source with the `CULL_PHASE2` define, over the shared layout.
        let cull_pipelines = self
            .cull
            .cull_kernels
            .as_ref()
            .map(|kernels| {
                let layout = kernels.pipeline_layout.handle();
                let pipeline = compute_pipeline(device, layout, &compile_cull_shader(hr)?, "cull")?;
                let phase2 = kernels
                    .pipeline_phase2
                    .as_ref()
                    .map(|_| {
                        compute_pipeline(device, layout, &compile_cull_shader_phase2(hr)?, "cull")
                    })
                    .transpose()?;
                Ok::<_, RenderError>((pipeline, phase2))
            })
            .transpose()?;
        // Hi-Z build kernels (live alongside the cull pipeline).
        let hiz_pipelines = self
            .cull
            .hiz
            .as_ref()
            .map(|hiz| hiz.recompile_pipelines(device, hr))
            .transpose()?;

        // Auto-exposure (gated on the post-process config): the histogram build
        // + average compute pipelines.
        let auto_exposure_pipelines = self
            .auto_exposure
            .resources
            .as_ref()
            .map(|ae| ae.rebuild_pipelines(device, hr))
            .transpose()?;

        // Decal (always built when DecalResources exists, which is
        // unconditional in `VkContext::new`).
        let msaa = self.targets.msaa_samples != vk::SampleCountFlags::TYPE_1;
        let decal_pipeline = self
            .decal
            .resources
            .as_ref()
            .map(|decals| super::decal::rebuild_decal_pipeline(device, decals, msaa, hr))
            .transpose()?;

        // Lines (only once a frame published some and the lazy build ran).
        let line_pipeline = self
            .lines
            .resources
            .as_ref()
            .map(|lines| super::line::rebuild_line_pipeline(device, lines, msaa, hr))
            .transpose()?;

        // Fog (only when the world declared a VolumetricFog). Rebuilds both the
        // fullscreen render pipeline and the froxel-volume compute kernel; the
        // trailing `.map` tuples them into one Result.
        let fog_pipelines = self
            .fog
            .resources
            .as_ref()
            .map(|fog| {
                let render = super::fog::rebuild_fog_pipeline(device, fog, msaa, hr)?;
                super::fog::rebuild_fog_froxel_pipeline(device, fog, hr)
                    .map(|froxel| (render, froxel))
            })
            .transpose()?;

        // SSAO (only when PostProcessConfig opted in). Rebuilds the depth
        // copy, kernel + blur.
        let ssao_rebuilt = rebuild_if_live!(
            self.ssao.is_some(),
            concinnity_core::render::post::ssao::build_pipelines(&self.post_device(0))
        );

        // SSR (only when PostProcessConfig opted in). Rebuilds the resolve.
        let ssr_rebuilt = rebuild_if_live!(
            self.ssr.is_some(),
            concinnity_core::render::post::ssr::build_pipeline(&self.post_device(0))
        );

        // SSGI (only when indirect_lighting: ssgi). Rebuilds its four pipelines.
        let ssgi_rebuilt = rebuild_if_live!(
            self.ssgi.is_some(),
            concinnity_core::render::post::ssgi::build_pipelines(&self.post_device(0))
        );

        // RT reflections (only when the world opted in + the GPU supports it).
        // Rebuilds the flat + textured ray-query pipelines.
        let rt_rebuilt = self
            .rt_reflections
            .as_ref()
            .map(|rt| crate::vulkan::post::rt_reflections::rebuild_rt_pipelines(device, rt, hr))
            .transpose()?;

        // Reflection composite (only when a reflection path owns the scene image).
        // Rebuilds the roughness blur + composite pipelines.
        let reflection_composite_rebuilt = rebuild_if_live!(
            self.reflection_composite.is_some(),
            concinnity_core::render::post::reflection_composite::build_pipelines(
                &self.post_device(0)
            )
        );

        // TAA (only when PostProcessConfig opted in). Rebuilds the resolve
        // pipeline; the velocity channel lives on the unified G-buffer pre-pass.
        let taa_rebuilt = rebuild_if_live!(
            self.taa.is_some(),
            concinnity_core::render::post::taa::build_pipeline(&self.post_device(0))
        );

        // Particles (only when ≥1 emitter is live or has ever been
        // added at runtime). Rebuilds the compute + render pipelines in
        // one shot.
        let particle_rebuilt = self
            .particle
            .resources
            .as_ref()
            .map(|particles| particles.rebuild_pipelines(device, hr))
            .transpose()?;

        // All builds succeeded: swap the freshly compiled pipelines in. Each
        // assignment drops the pipeline it displaces, which retires it through
        // the device's queue rather than destroying it under a submission that
        // may still name it.
        self.composite.pipeline = composite_pipeline;

        if let Some(new_pipeline) = text_pipeline {
            self.text.pipeline = Some(new_pipeline);
        }

        if let (Some(rebuilt), Some(bloom)) = (bloom_rebuilt, self.bloom.as_mut()) {
            bloom.swap_pipelines(rebuilt);
        }

        if let Some((pipelines, world_buckets, engine)) = bindless_main_pipeline {
            self.cull.bindless_pipeline = Some(pipelines.main);
            self.cull.prepass_pipeline = pipelines.prepass;
            for (bucket, fresh) in world_buckets {
                if let Some(live) = self.cull.world_pipelines.get_mut(bucket) {
                    *live = fresh;
                }
            }
            self.cull.bindless_main_spv = engine;
        }
        self.sky.swap_pipeline(sky_pipeline);
        if let (Some(sky), Some(gb)) = (gbuffer_sky_pipeline, self.gbuffer.as_mut()) {
            gb.sky.swap_pipeline(sky);
        }
        // The wireframe twins were built from the pre-reload shaders; drop them
        // so the next wireframe frame rebuilds against these.
        self.invalidate_wireframe_pipelines();
        if let (Some((pipeline, phase2)), Some(kernels)) =
            (cull_pipelines, self.cull.cull_kernels.as_mut())
        {
            kernels.pipeline = pipeline;
            kernels.pipeline_phase2 = phase2;
        }
        if let (Some((init, downsample)), Some(hiz)) = (hiz_pipelines, self.cull.hiz.as_mut()) {
            hiz.swap_pipelines(init, downsample);
        }

        if let (Some((build, average)), Some(ae)) = (
            auto_exposure_pipelines,
            self.auto_exposure.resources.as_mut(),
        ) {
            ae.swap_pipelines(build, average);
        }

        if let (Some(new_pipeline), Some(decals)) = (decal_pipeline, self.decal.resources.as_mut())
        {
            decals.pipeline = new_pipeline;
        }
        if let (Some(new_pipeline), Some(lines)) = (line_pipeline, self.lines.resources.as_mut()) {
            lines.pipeline = new_pipeline;
        }
        if let (Some((render, froxel)), Some(fog)) = (fog_pipelines, self.fog.resources.as_mut()) {
            fog.pipeline = render;
            fog.froxel_pipeline = froxel;
        }
        if let (Some(rebuilt), Some(ssao)) = (ssao_rebuilt, self.ssao.as_mut()) {
            ssao.swap_pipelines(rebuilt);
        }
        if let (Some(rebuilt), Some(ssr)) = (ssr_rebuilt, self.ssr.as_mut()) {
            ssr.swap_pipeline(rebuilt);
        }
        if let (Some(rebuilt), Some(ssgi)) = (ssgi_rebuilt, self.ssgi.as_mut()) {
            ssgi.swap_pipelines(rebuilt);
        }
        if let (Some(rebuilt), Some(rt)) = (rt_rebuilt, self.rt_reflections.as_mut()) {
            rt.swap_pipelines(rebuilt);
        }
        if let (Some(rebuilt), Some(rc)) = (
            reflection_composite_rebuilt,
            self.reflection_composite.as_mut(),
        ) {
            rc.swap_pipelines(rebuilt);
        }
        if let (Some(rebuilt), Some(taa)) = (taa_rebuilt, self.taa.as_mut()) {
            taa.swap_pipelines(rebuilt);
        }
        if let (Some((cp, rp)), Some(p)) = (particle_rebuilt, self.particle.resources.as_mut()) {
            p.swap_pipelines(cp, rp);
        }
        Ok(())
    }

    // Rebuild bucket 0 of the GPU-driven main pass and its G-buffer pre-pass
    // from the world default Shader's freshly compiled programs and hot-swap
    // them, for `update_world_shader`, or swap in `prepared` when a worker
    // already built them. Mirrors the rebuild-then-swap safety pattern of
    // `reload_shaders`: the replacements are constructed first and the swap only
    // runs when the build succeeds, so a typo in a shader edit leaves the live
    // pipelines untouched and the session keeps rendering.
    pub(in crate::vulkan) fn update_default_world_shader(
        &mut self,
        programs: &concinnity_core::components::ShaderPrograms,
        prepared: Option<BucketPipelines>,
    ) -> RenderResult<()> {
        let new = replace_bucket(
            0,
            self.cull.prepass_pipeline.is_some(),
            prepared,
            |p: &BucketPipelines| p.prepass.is_some(),
            || self.build_world_main_pipeline(Some(programs), &self.cull.bindless_main_spv),
        )?;
        // Drain the GPU before destroying the displaced pipeline so no in-flight
        // command buffer still references it: the debug hot-reload drive does
        // not `wait_idle` for us, unlike the built-in `reload_shaders` path the
        // draw loop guards.
        self.wait_idle();
        self.cull.bindless_pipeline = Some(new.main);
        self.cull.prepass_pipeline = new.prepass;
        self.world_shader = Some(programs.clone());
        self.invalidate_wireframe_pipelines();
        Ok(())
    }

    // Bucket 0's pipelines against the live layouts: the world default Shader's
    // programs where `world` declares one, `engine` otherwise. Errors when the
    // GPU-driven pass is not live, which means the world has nothing to draw,
    // when the main pipeline fails, or when the rebuild would drop the live
    // pre-pass, so a live pair is kept whole.
    fn build_world_main_pipeline(
        &self,
        world: Option<&concinnity_core::components::ShaderPrograms>,
        engine: &BindlessSpv,
    ) -> RenderResult<BucketPipelines> {
        let targets = self.bucket_pipeline_targets().ok_or_else(|| {
            RenderError::Other("the GPU-driven main pass is not live".to_string())
        })?;
        let pipelines = build_bucket_pipeline(
            &self.hw.device,
            targets,
            0,
            backend_init::WorldShader {
                programs: world,
                deferred: false,
            },
            engine,
        )?;
        let live_prepass = self.cull.prepass_pipeline.is_some();
        check_rebuild(0, live_prepass, pipelines.prepass.is_some())?;
        Ok(pipelines)
    }
}