Skip to main content

kallo_api/
device.rs

1//! What this machine would render on.
2//!
3//! The model runs on two engines. An interface that lets a person choose between
4//! them needs to know what the machine has before it can say anything true about
5//! the choice, so this asks the renderer's own question: open the adapter a print
6//! would open, or say why there is not one.
7//!
8//! It is deliberately not a benchmark and not a capability list. "There is an
9//! adapter" is the one fact a settings screen needs; whether a particular frame
10//! fits on it is a property of the frame, and the renderer answers that with a
11//! fallback rather than with a form.
12
13/// What the machine offers for rendering.
14#[derive(Debug, Clone)]
15pub struct RenderDevice {
16    /// The adapter a render would open, as wgpu names it:
17    /// `Adreno (TM) 730 (Vulkan)`.
18    pub gpu: Option<String>,
19    /// Why there is no adapter, when there is none.
20    pub gpu_error: Option<String>,
21}
22
23/// Ask the machine what it would render on.
24///
25/// Exactly one of [`RenderDevice::gpu`] and [`RenderDevice::gpu_error`] is set:
26/// a machine either names its adapter or gives the reason it has none.
27#[must_use]
28pub fn render_device() -> RenderDevice {
29    #[cfg(feature = "gpu")]
30    {
31        match kallo_gpu::describe_adapter() {
32            Ok(gpu) => RenderDevice {
33                gpu: Some(gpu),
34                gpu_error: None,
35            },
36            Err(error) => RenderDevice {
37                gpu: None,
38                gpu_error: Some(error.to_string()),
39            },
40        }
41    }
42    #[cfg(not(feature = "gpu"))]
43    {
44        RenderDevice {
45            gpu: None,
46            gpu_error: Some("this build has no GPU backend".to_owned()),
47        }
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::render_device;
54
55    #[test]
56    fn an_adapter_is_either_named_or_its_absence_explained() {
57        let device = render_device();
58        assert_ne!(device.gpu.is_some(), device.gpu_error.is_some());
59    }
60}