Skip to main content

concinnity_device/
precompile.rs

1//! Export-time compilation of the engine's built-in shaders. The DirectX and
2//! Vulkan backends declare their compile set as static data (each backend's
3//! builtins.rs); this module iterates those declarations and makes sure every
4//! enumerable variant's artifact exists in a bundle's shader-cache/ directory.
5//! Compilation is pure CPU (FXC / DXC / shaderc need no GPU device), so this
6//! runs inside `cn export` with no window, no adapter, and no child process.
7//! Renderer init compiles through the same declarations and the same cache
8//! keys, so a shipped bundle's first launch reuses every artifact written here;
9//! anything not enumerable (a world-authored SdfVolume fragment, an unusual
10//! runtime parameter) still compiles at init exactly as before.
11
12use std::path::Path;
13
14use crate::shader_cache::Ensured;
15
16/// Outcome of a built-in shader precompile: how many artifacts were already in
17/// place or copied from the local cache, how many compiled fresh, and the
18/// programs that failed (with their compile diagnostics). Failures do not
19/// abort the run -- the affected shader falls back to compiling at the
20/// bundle's first launch.
21#[derive(Debug, Default)]
22pub struct Report {
23    /// Artifacts already in place or copied from the local cache.
24    pub reused: usize,
25    /// Artifacts compiled fresh this run.
26    pub compiled: usize,
27    /// Programs that failed, with their compile diagnostics.
28    pub failed: Vec<String>,
29}
30
31impl Report {
32    pub(crate) fn record(&mut self, label: &str, result: Result<Ensured, String>) {
33        match result {
34            Ok(Ensured::Present) | Ok(Ensured::Copied) => self.reused += 1,
35            Ok(Ensured::Compiled) => self.compiled += 1,
36            Err(e) => self.failed.push(format!("{label}: {e}")),
37        }
38    }
39
40    /// Total artifacts now present in the output directory.
41    pub fn cached(&self) -> usize {
42        self.reused + self.compiled
43    }
44}
45
46/// Compile every enumerable built-in shader variant into `out_dir` (a bundle's
47/// `shader-cache/`).
48///
49/// Nothing about the exported world enters this: every variant a backend can
50/// take is a property of the device the bundle eventually runs on -- its MSAA
51/// mode, its probe cube-array length, whether it seats the bindless pool at its
52/// ceiling -- so each is baked at what a desktop driver affords, and a device
53/// that differs misses those entries and compiles at first launch.
54pub fn precompile_builtin_shaders(out_dir: &Path) -> Report {
55    let mut report = Report::default();
56    #[cfg(backend_dx)]
57    {
58        crate::directx::builtins::precompile(out_dir, &mut report);
59        crate::directx::slang_builtins::precompile(out_dir, &mut report);
60    }
61    #[cfg(backend_vk)]
62    crate::vulkan::builtins::precompile(out_dir, &mut report);
63    report
64}
65
66#[cfg(test)]
67mod tests {
68    use super::*;
69
70    #[test]
71    fn report_tallies_reuse_compile_and_failure() {
72        let mut r = Report::default();
73        r.record("a", Ok(Ensured::Present));
74        r.record("b", Ok(Ensured::Copied));
75        r.record("c", Ok(Ensured::Compiled));
76        r.record("d ps_5_1", Err("boom".to_string()));
77        assert_eq!(r.reused, 2);
78        assert_eq!(r.compiled, 1);
79        assert_eq!(r.cached(), 3);
80        assert_eq!(r.failed, vec!["d ps_5_1: boom".to_string()]);
81    }
82}