Skip to main content

concinnity_engine/
platform.rs

1//! Which shader compile target this build's rendering backend consumes.
2//!
3//! The backend cfg is resolved once in build.rs; this is the one place that
4//! reads it as a value, so the runtime, the editor, and the cook all name the
5//! same platform without each resolving it again.
6
7use concinnity_core::platform::Platform;
8
9/// The shader platform this build's rendering backend consumes. Resolved from
10/// the backend cfg rather than the target OS, so a Windows Vulkan build
11/// correctly reports SPIR-V rather than DXIL.
12///
13/// A build with no backend runs nothing that consumes bytecode, but the cook
14/// still needs a language to produce; the one this target renders with is the
15/// useful answer, and no backend cfg contradicts it.
16pub fn current() -> Platform {
17    #[cfg(backend_metal)]
18    {
19        Platform::Metal
20    }
21    #[cfg(backend_dx)]
22    {
23        Platform::Hlsl
24    }
25    #[cfg(backend_vk)]
26    {
27        Platform::Glsl
28    }
29    #[cfg(not(any(backend_metal, backend_dx, backend_vk)))]
30    {
31        native_platform(std::env::consts::OS)
32    }
33}
34
35// What a target renders with when no backend is compiled in, mirroring how the
36// `native` feature resolves.
37#[cfg(any(test, not(any(backend_metal, backend_dx, backend_vk))))]
38fn native_platform(target_os: &str) -> Platform {
39    match target_os {
40        "macos" => Platform::Metal,
41        "windows" => Platform::Hlsl,
42        _ => Platform::Glsl,
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    // At most one backend cfg is on, so the resolved platform is one of the
51    // three.
52    #[test]
53    fn the_backend_resolves_to_one_platform() {
54        let platform = current();
55        assert!(["metal", "hlsl", "glsl"].contains(&platform.key()));
56    }
57
58    #[test]
59    fn every_target_has_a_native_platform() {
60        assert_eq!(native_platform("macos"), Platform::Metal);
61        assert_eq!(native_platform("windows"), Platform::Hlsl);
62        assert_eq!(native_platform("linux"), Platform::Glsl);
63    }
64}