Skip to main content

concinnity_device/
factory.rs

1// src/factory.rs
2//
3// The backend factory: route the assembled inputs to the backend selected at
4// compile time. The three backend_* cfgs are mutually exclusive, so at most one
5// arm compiles; a build with no backend feature compiles none and reports the
6// same "no backend" the callers already handle. This is the single construction
7// choke point - the client holds only a `Box<dyn RenderBackend>` and never names
8// a concrete backend context.
9
10/// Probe a cheap throwaway device handle to classify the GPU, so the auto-config
11/// quality ceiling can influence the render targets / effect pipelines the backend
12/// sizes at init. Each backend creates only the cheap handle it needs and
13/// classifies it: Metal the default-device handle, DirectX the DXGI adapter (no
14/// device / swapchain), Vulkan a surface-free instance (destroyed immediately).
15pub fn probe_gpu_profile() -> crate::gfx::backend::GpuProfile {
16    #[cfg(backend_dx)]
17    {
18        crate::directx::probe_gpu_profile()
19    }
20    #[cfg(backend_vk)]
21    {
22        crate::vulkan::probe_gpu_profile()
23    }
24    #[cfg(backend_metal)]
25    {
26        crate::metal::probe_gpu_profile()
27    }
28    #[cfg(not(any(backend_dx, backend_vk, backend_metal)))]
29    {
30        crate::gfx::backend::GpuProfile::UNKNOWN
31    }
32}
33
34/// Route the assembled `BackendInit` to the backend selected at compile time.
35/// Construction inputs are documented on `BackendInit` itself.
36pub fn init_backend(
37    init: crate::gfx::backend_init::BackendInit<'_>,
38) -> Option<Box<dyn crate::gfx::backend::RenderBackend>> {
39    #[cfg(backend_dx)]
40    {
41        match crate::directx::DxContext::new(init) {
42            Ok(dx) => Some(Box::new(dx)),
43            Err(e) => {
44                tracing::error!("GraphicsSystem: D3D12 init failed: {}", e);
45                None
46            }
47        }
48    }
49
50    #[cfg(backend_vk)]
51    {
52        match crate::vulkan::VkContext::new(init) {
53            Ok(vk) => Some(Box::new(vk)),
54            Err(e) => {
55                tracing::error!("GraphicsSystem: Vulkan init failed: {}", e);
56                None
57            }
58        }
59    }
60
61    #[cfg(backend_metal)]
62    {
63        match crate::metal::MtlContext::new(init) {
64            Ok(mtl) => Some(Box::new(mtl)),
65            Err(e) => {
66                tracing::error!("GraphicsSystem: Metal init failed: {}", e);
67                None
68            }
69        }
70    }
71
72    #[cfg(not(any(backend_dx, backend_vk, backend_metal)))]
73    {
74        let _ = init;
75        None
76    }
77}