rendy_init/
lib.rs

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
//!
//! Simple rendy initialization.
//! Takes most bolierplate required for init rendy on different platforms/backends.
//! It is still possible to construct everything manually if your case is not supported by this module.
//!

// #[allow(unused)]
use {
    rendy_command::Families,
    rendy_core::{
        backend_enum,
        hal::{device::CreationError, Backend, Instance as _, UnsupportedBackend},
        rendy_backend, rendy_with_dx12_backend, rendy_with_empty_backend, rendy_with_gl_backend,
        rendy_with_metal_backend, rendy_with_vulkan_backend, EnabledBackend, Instance,
    },
    rendy_factory::{Config, DevicesConfigure, Factory, HeapsConfigure, QueuesConfigure},
};

#[cfg(feature = "winit")]
mod windowed;

#[cfg(feature = "winit")]
pub use windowed::*;

/// Error during rendy initialization
#[derive(Clone, Debug, PartialEq)]
pub enum RendyInitError {
    /// Gfx creation error.
    CreationError(CreationError),

    /// Backend is unsupported.
    UnsupportedBackend(UnsupportedBackend),
}

impl From<CreationError> for RendyInitError {
    fn from(err: CreationError) -> Self {
        RendyInitError::CreationError(err)
    }
}

impl From<UnsupportedBackend> for RendyInitError {
    fn from(err: UnsupportedBackend) -> Self {
        RendyInitError::UnsupportedBackend(err)
    }
}

impl std::fmt::Display for RendyInitError {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RendyInitError::CreationError(err) => write!(fmt, "Cannot init rendy: {:#?}", err),
            RendyInitError::UnsupportedBackend(err) => write!(fmt, "Cannot init rendy: {:#?}", err),
        }
    }
}

impl std::error::Error for RendyInitError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            RendyInitError::CreationError(_err) => None, // Should be `Some(err)`
            RendyInitError::UnsupportedBackend(_err) => None, // Should be `Some(err)`
        }
    }
}

/// Initialized rendy instance without window.
/// Create with `Rendy::init`.
///
/// OpenGL can't be initialized without window, see `WindowedRendy` to initialize rendy on OpenGL.
#[derive(Debug)]
pub struct Rendy<B: Backend> {
    pub families: Families<B>,
    pub factory: Factory<B>,
}

impl<B: Backend> Rendy<B> {
    pub fn init(
        config: &Config<impl DevicesConfigure, impl HeapsConfigure, impl QueuesConfigure>,
    ) -> Result<Self, RendyInitError> {
        let instance = B::Instance::create("Rendy", 1)?;
        let (factory, families) =
            rendy_factory::init_with_instance(Instance::new(instance), config)?;
        Ok(Rendy { factory, families })
    }
}

/// Error type that may be returned by `AnyRendy::init_auto`
pub struct RendyAutoInitError {
    pub errors: Vec<(EnabledBackend, RendyInitError)>,
}

impl std::fmt::Debug for RendyAutoInitError {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(self, fmt)
    }
}

impl std::fmt::Display for RendyAutoInitError {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if fmt.alternate() {
            if self.errors.is_empty() {
                writeln!(fmt, "No enabled backends among:")?;
                for &backend in &BASIC_PRIORITY {
                    writeln!(fmt, "  {:#}", backend)?;
                }
            } else {
                writeln!(fmt, "Initialization failed for all backends")?;
                for (backend, error) in &self.errors {
                    writeln!(fmt, "  {:#}: {:#}", backend, error)?;
                }
            }
        } else {
            if self.errors.is_empty() {
                write!(fmt, "No enabled backends among:")?;
                for &backend in &BASIC_PRIORITY {
                    write!(fmt, "  {}", backend)?;
                }
            } else {
                write!(fmt, "Initialization failed for all backends")?;
                for (backend, error) in &self.errors {
                    write!(fmt, "  {}: {}", backend, error)?;
                }
            }
        }
        Ok(())
    }
}

backend_enum! { #[derive(Debug)] pub enum AnyRendy(Rendy); }

impl AnyRendy {
    pub fn init_auto(
        config: &Config<impl DevicesConfigure, impl HeapsConfigure, impl QueuesConfigure>,
    ) -> Result<Self, RendyAutoInitError> {
        let mut errors = Vec::with_capacity(5);

        for backend in BASIC_PRIORITY
            .iter()
            .filter_map(|b| std::convert::TryInto::try_into(*b).ok())
        {
            match Self::init(backend, config) {
                Ok(rendy) => return Ok(rendy),
                Err(err) => errors.push((backend, err)),
            }
        }

        Err(RendyAutoInitError { errors })
    }

    #[rustfmt::skip]
    pub fn init(
        back: EnabledBackend,
        config: &Config<impl DevicesConfigure, impl HeapsConfigure, impl QueuesConfigure>,
    ) -> Result<Self, RendyInitError> {
        #![allow(unused_variables)]
        rendy_backend!(match (back): EnabledBackend {
            Dx12 => { Ok(AnyRendy::Dx12(Rendy::<rendy_core::dx12::Backend>::init(config)?)) }
            Empty => { Ok(AnyRendy::Empty(Rendy::<rendy_core::empty::Backend>::init(config)?)) }
            Gl => { Ok(AnyRendy::Gl(Rendy::<rendy_core::gl::Backend>::init(config)?)) }
            Metal => { Ok(AnyRendy::Metal(Rendy::<rendy_core::metal::Backend>::init(config)?)) }
            Vulkan => { Ok(AnyRendy::Vulkan(Rendy::<rendy_core::vulkan::Backend>::init(config)?)) }
        })
    }
}

/// Get available backends
pub fn available_backends() -> smallvec::SmallVec<[EnabledBackend; 5]> {
    #[allow(unused_mut)]
    let mut backends = smallvec::SmallVec::<[EnabledBackend; 5]>::new();
    rendy_with_dx12_backend!(backends.push(EnabledBackend::Dx12));
    rendy_with_empty_backend!(backends.push(EnabledBackend::Empty));
    rendy_with_gl_backend!(backends.push(EnabledBackend::Gl));
    rendy_with_metal_backend!(backends.push(EnabledBackend::Metal));
    rendy_with_vulkan_backend!(backends.push(EnabledBackend::Vulkan));
    backends
}

pub const BASIC_PRIORITY: [rendy_core::Backend; 4] = [
    rendy_core::Backend::Vulkan,
    rendy_core::Backend::Dx12,
    rendy_core::Backend::Metal,
    rendy_core::Backend::Gl,
];

pub fn pick_backend(
    priority: impl IntoIterator<Item = rendy_core::Backend>,
) -> Option<EnabledBackend> {
    priority
        .into_iter()
        .filter_map(|b| std::convert::TryInto::try_into(b).ok())
        .next()
}