concinnity-device 0.19.53

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
//! GlassPanel: one producer of the engine's transparent pass on the Vulkan
//! backend (`transparent.rs` owns the render pass itself, the scene snapshot, the
//! shared descriptor / pipeline layouts and the combined back-to-front draw
//! order; `water.rs` is the other producer). Each panel is a flat world-space
//! quad, built once at init; the fragment shader refracts the pass's scene
//! snapshot, tints it, and mixes a reflection over it by a Schlick Fresnel term
//! (see shaders/glass.hlsl, the single source all three backends compile).
//!
//! Same uniform layouts, back-to-front ordering and manual depth-occlusion test
//! as the DirectX and Metal hosts.

use ash::vk;
use concinnity_core::components::GlassPanel;
use concinnity_core::geometry::glass_quad::build_glass_quad;
use concinnity_core::gfx::mesh_payload::Vertex;
use concinnity_core::render::error::RenderResult;
// `GlassParams` (the per-panel UBO) is a GPU-free layout struct that lives in
// `core::render`; re-export it so `crate::vulkan::glass::GlassParams` is
// unchanged for the `glass_params_from` path.
pub(in crate::vulkan) use concinnity_core::render::uniforms::GlassParams;

use super::allocator::DeviceAllocator;
use crate::vulkan::builtin_shaders::CompileProgram;
use crate::vulkan::transparent::{
    GlassMeshProducer, ProducerCtx, RecordUpload, TracedGlassPipelines, TransparentProducer,
    TransparentRecord, TransparentVertexInput, create_glass_reflection_pipeline,
    create_transparent_pipeline,
};

// Build the per-panel `GlassParams` from an authored panel. `planar` is 1.0 when
// the pane has a planar reflection slot, else 0.0. Pure; unit tested. Mirrors
// `directx::glass::glass_params_from`.
fn glass_params_from(panel: &GlassPanel, planar: f32) -> GlassParams {
    let n = panel.normal; // already unit-length from GlassPanel::from_args
    GlassParams {
        center: [panel.center[0], panel.center[1], panel.center[2], 0.0],
        normal: [n[0], n[1], n[2], 0.0],
        tint: [panel.tint[0], panel.tint[1], panel.tint[2], 0.0],
        opacity: panel.opacity,
        refraction_strength: panel.refraction_strength,
        fresnel_power: panel.fresnel_power,
        planar,
    }
}

// Compile the glass vertex + fragment shaders, the fragment at the main
// depth's sample count so its depth read matches the resource.
fn compile_glass_shaders(hot_reload: bool, msaa: bool) -> RenderResult<(Vec<u8>, Vec<u8>)> {
    let vert = super::builtin_shaders::GLASS_VERT.compile(hot_reload)?;
    let frag = super::builtin_shaders::GLASS_FRAG
        .at(msaa)
        .compile(hot_reload)?;
    Ok((vert, frag))
}

// SPIR-V blobs for the ray-traced glass pipelines: the shared vertex stage (the
// same one the base pass uses -- the trace is entirely in the fragment), the
// flat fragment, and the textured fragment (`None` when the bindless pool is
// absent). Mirrors `post::rt_reflections::RtShaders`.
struct GlassRtShaders {
    vs: Vec<u8>,
    flat_fs: Vec<u8>,
    textured_fs: Option<Vec<u8>>,
    // The reflection pre-pass fragments, paired the same way.
    reflection_flat_fs: Vec<u8>,
    reflection_textured_fs: Option<Vec<u8>>,
}

// Compile the glass vertex shader + the ray-traced glass fragment (flat, plus
// the textured variant when `pool_size > 0`). dxc emits `SPV_KHR_ray_query`
// for the traversal, which the device already advertises wherever these
// pipelines are built.
fn compile_glass_rt_shaders(
    hot_reload: bool,
    msaa: bool,
    pool_size: usize,
) -> RenderResult<GlassRtShaders> {
    let vs = super::builtin_shaders::GLASS_VERT.compile(hot_reload)?;
    let flat_fs = super::builtin_shaders::GLASS_FRAG_RT
        .at(msaa)
        .compile(hot_reload)?;
    let reflection_flat_fs = super::builtin_shaders::GLASS_REFLECTION_FRAG
        .at(msaa)
        .compile(hot_reload)?;
    let (textured_fs, reflection_textured_fs) = if pool_size > 0 {
        (
            Some(
                super::builtin_shaders::GLASS_FRAG_RT_TEXTURED
                    .at(msaa)
                    .compile(hot_reload)?,
            ),
            Some(
                super::builtin_shaders::GLASS_REFLECTION_FRAG_TEXTURED
                    .at(msaa)
                    .compile(hot_reload)?,
            ),
        )
    } else {
        (None, None)
    };
    Ok(GlassRtShaders {
        vs,
        flat_fs,
        textured_fs,
        reflection_flat_fs,
        reflection_textured_fs,
    })
}

// Upload one panel's static quad VB + IB and its per-panel `GlassParams` UBO,
// then allocate + write the panel's descriptor set.
fn build_panel_record(
    alloc: &DeviceAllocator,
    ctx: &ProducerCtx,
    panel: &GlassPanel,
    planar_slot: Option<usize>,
) -> RenderResult<TransparentRecord> {
    let (verts, idxs) = build_glass_quad(panel.center, panel.normal, panel.half_size);

    // Flatten into the standard engine `Vertex` layout. Tangent is a placeholder
    // (the glass shader rebuilds its frame from the panel normal) and per-vertex
    // color is unused.
    let packed: Vec<Vertex> = verts
        .into_iter()
        .map(|(pos, normal, color, uv)| Vertex {
            pos,
            normal,
            tangent: [1.0, 0.0, 0.0],
            color,
            uv,
        })
        .collect();

    let params = glass_params_from(panel, if planar_slot.is_some() { 1.0 } else { 0.0 });
    TransparentRecord::upload(
        alloc,
        ctx.record_descriptors(planar_slot),
        RecordUpload {
            vertices: &packed,
            indices: &idxs,
            params: bytemuck::bytes_of(&params),
            visible: panel.visible,
            center: panel.center,
            planar_slot,
        },
    )
}

// Build the glass pipelines and one record per authored panel. The RT pair is
// built whenever the pass has RT pipeline layouts (regardless of whether RT is on
// at launch, so a live `quality-set ray_traced_reflections` selects it with no
// pipeline rebuild); a compile failure leaves it absent and the base
// probe/planar path runs.
pub(in crate::vulkan) fn build_glass_producer(
    ctx: ProducerCtx,
    panels: &[GlassPanel],
    // Per-pane planar resolve slot (aligned with `panels`); `None` panes keep the
    // probe/sky reflection. From `assign_planar_slots`.
    planar_slots: &[Option<usize>],
) -> RenderResult<TransparentProducer> {
    let (vert_spv, frag_spv) = compile_glass_shaders(ctx.hot_reload, ctx.msaa)?;
    let pipeline = create_transparent_pipeline(
        ctx.device,
        ctx.render_pass,
        ctx.layout,
        &vert_spv,
        &frag_spv,
        TransparentVertexInput::Position,
    )?;

    let traced = match ctx.rt_layout_flat {
        Some(flat_layout) => match build_glass_rt_pipelines(&ctx, flat_layout) {
            Ok(p) => Some(p),
            Err(e) => {
                tracing::warn!(
                    "glass RT pipelines failed to build ({e}); using the probe / planar glass path"
                );
                None
            }
        },
        None => None,
    };
    let (flat_rt_pso, textured_rt_pso, reflection_flat_pso, reflection_textured_pso) = match traced
    {
        Some(p) => (
            Some(p.shade_flat),
            p.shade_textured,
            Some(p.reflection_flat),
            p.reflection_textured,
        ),
        None => (None, None, None, None),
    };

    let mut records = Vec::with_capacity(panels.len());
    for (i, panel) in panels.iter().enumerate() {
        let planar_slot = planar_slots.get(i).copied().flatten();
        records.push(build_panel_record(ctx.alloc, &ctx, panel, planar_slot)?);
    }

    Ok(TransparentProducer {
        pipeline,
        flat_rt_pso,
        textured_rt_pso,
        reflection_flat_pso,
        reflection_textured_pso,
        records,
    })
}

// The traced RT glass pipelines. The textured pair is skipped when the bindless
// pool is absent or the device could not spare a fifth descriptor set, leaving
// the flat trace.
fn build_glass_rt_pipelines(
    ctx: &ProducerCtx,
    flat_layout: vk::PipelineLayout,
) -> RenderResult<TracedGlassPipelines> {
    let shaders = compile_glass_rt_shaders(ctx.hot_reload, ctx.msaa, ctx.bindless_pool_size)?;
    build_traced_pipelines(ctx, flat_layout, &shaders, TransparentVertexInput::Position)
}

// Both traced pairs over one vertex stage: the shading pipelines target the
// transparent pass, the reflection ones the glass reflection pre-pass.
fn build_traced_pipelines(
    ctx: &ProducerCtx,
    flat_layout: vk::PipelineLayout,
    shaders: &GlassRtShaders,
    vertex_input: TransparentVertexInput,
) -> RenderResult<TracedGlassPipelines> {
    let shade = |layout, fs: &[u8]| {
        create_transparent_pipeline(
            ctx.device,
            ctx.render_pass,
            layout,
            &shaders.vs,
            fs,
            vertex_input,
        )
    };
    let reflection = |layout, fs: &[u8]| {
        create_glass_reflection_pipeline(
            ctx.device,
            ctx.reflection_render_pass,
            layout,
            &shaders.vs,
            fs,
            vertex_input,
        )
    };
    let (shade_textured, reflection_textured) = match (
        ctx.rt_layout_textured,
        &shaders.textured_fs,
        &shaders.reflection_textured_fs,
    ) {
        (Some(layout), Some(fs), Some(rfs)) => {
            (Some(shade(layout, fs)?), Some(reflection(layout, rfs)?))
        }
        _ => (None, None),
    };
    Ok(TracedGlassPipelines {
        shade_flat: shade(flat_layout, &shaders.flat_fs)?,
        shade_textured,
        reflection_flat: reflection(flat_layout, &shaders.reflection_flat_fs)?,
        reflection_textured,
    })
}

// Compile the see-through mesh vertex stage + its ray-traced fragments (flat,
// plus the textured variant when the bindless pool is live). No non-RT pair: the
// trace is what makes the mesh see-through.
fn compile_glass_mesh_shaders(
    hot_reload: bool,
    msaa: bool,
    pool_size: usize,
) -> RenderResult<GlassRtShaders> {
    let vs = super::builtin_shaders::GLASS_MESH_VERT.compile(hot_reload)?;
    let flat_fs = super::builtin_shaders::GLASS_MESH_FRAG_RT
        .at(msaa)
        .compile(hot_reload)?;
    let reflection_flat_fs = super::builtin_shaders::GLASS_MESH_REFLECTION_FRAG
        .at(msaa)
        .compile(hot_reload)?;
    let (textured_fs, reflection_textured_fs) = if pool_size > 0 {
        (
            Some(
                super::builtin_shaders::GLASS_MESH_FRAG_RT_TEXTURED
                    .at(msaa)
                    .compile(hot_reload)?,
            ),
            Some(
                super::builtin_shaders::GLASS_MESH_REFLECTION_FRAG_TEXTURED
                    .at(msaa)
                    .compile(hot_reload)?,
            ),
        )
    } else {
        (None, None)
    };
    Ok(GlassRtShaders {
        vs,
        flat_fs,
        textured_fs,
        reflection_flat_fs,
        reflection_textured_fs,
    })
}

// Build the see-through mesh pipelines and the per-frame params ring one block
// per mesh deep. The producer holds no records: a mesh's geometry lives in the
// shared scene buffers and its params change per frame, so the encoder rebuilds
// its draw list each frame (see `collect_mesh_draws`). Only called when the pass
// has RT pipeline layouts.
pub(in crate::vulkan) fn build_glass_mesh_producer(
    ctx: ProducerCtx,
    flat_layout: vk::PipelineLayout,
    object_indices: &[usize],
) -> RenderResult<GlassMeshProducer> {
    let shaders = compile_glass_mesh_shaders(ctx.hot_reload, ctx.msaa, ctx.bindless_pool_size)?;
    let pipelines = build_traced_pipelines(
        &ctx,
        flat_layout,
        &shaders,
        TransparentVertexInput::PositionAndNormal,
    )?;
    GlassMeshProducer::new(&ctx, pipelines, object_indices.to_vec())
}

#[cfg(test)]
mod tests {
    use super::*;

    // The `TransparentView` / `GlassParams` layout tests live with the structs
    // in `concinnity_core::render::uniforms`, and are checked against the compiled
    // shader by `shader_layout`.

    // The see-through mesh family compiles from the shared single source at
    // runtime, so a syntax or binding error would otherwise surface only as an
    // init failure on a GPU host.
    #[test]
    fn glass_mesh_shaders_compile() {
        if !concinnity_shader::dxc_available() {
            return;
        }
        for msaa in [false, true] {
            super::compile_glass_mesh_shaders(false, msaa, 16)
                .unwrap_or_else(|e| panic!("glass_mesh shaders (msaa={msaa}) must compile: {e}"));
        }
    }

    #[test]
    fn glass_params_from_maps_fields() {
        let panel = GlassPanel {
            center: [1.0, 2.0, 3.0],
            normal: [0.0, 0.0, 1.0],
            tint: [0.6, 0.85, 0.9],
            opacity: 0.45,
            refraction_strength: 0.04,
            fresnel_power: 4.0,
            ..Default::default()
        };
        let p = glass_params_from(&panel, 1.0);
        assert_eq!(p.center, [1.0, 2.0, 3.0, 0.0]);
        assert_eq!(p.normal, [0.0, 0.0, 1.0, 0.0]);
        assert_eq!(p.tint, [0.6, 0.85, 0.9, 0.0]);
        assert_eq!(p.opacity, 0.45);
        assert_eq!(p.refraction_strength, 0.04);
        assert_eq!(p.fresnel_power, 4.0);
        assert_eq!(p.planar, 1.0);
        // A slotless pane gets planar = 0.0 (probe/sky fallback path).
        assert_eq!(glass_params_from(&panel, 0.0).planar, 0.0);
    }

    // Compile the glass vertex + fragment shaders (both MSAA variants) so a
    // regression fails the suite without a GPU. Mirrors the decal / fog compile
    // guards.
    #[test]
    fn glass_shaders_compile() {
        if !concinnity_shader::dxc_available() {
            return;
        }
        super::compile_glass_shaders(false, true).expect("glass compiles (msaa)");
        super::compile_glass_shaders(false, false).expect("glass compiles (no msaa)");
    }

    // Compile the ray-traced glass shaders (both MSAA variants, both flat +
    // textured) so a regression in glass.hlsl's `GLASS_RT` arm (the shared
    // `{RT_TRACE}` traversal + the probe `{PROBE_COMMON}` injection + the
    // `RT_TEXTURED` split) fails the suite without a GPU. Mirrors
    // `rt_reflections_shaders_compile`. The CPU<->GPU `RtParams` / `RtGeomEntry`
    // layouts are guarded by the `rt_params_layout_*` / `rt_geom_entry_*` tests
    // in gfx::render_types.
    #[test]
    fn glass_rt_shaders_compile() {
        if !concinnity_shader::dxc_available() {
            return;
        }
        for &msaa in &[true, false] {
            let shaders =
                super::compile_glass_rt_shaders(false, msaa, 4).expect("glass rt shaders compile");
            assert!(crate::vulkan::pipeline::is_spirv(&shaders.vs));
            assert!(crate::vulkan::pipeline::is_spirv(&shaders.flat_fs));
            assert!(
                shaders.textured_fs.is_some(),
                "pool_size>0 builds the textured variant"
            );
        }
        // pool_size 0 builds only the flat variant.
        let flat_only =
            super::compile_glass_rt_shaders(false, false, 0).expect("glass rt flat compiles");
        assert!(flat_only.textured_fs.is_none());
    }
}