freya_winit/drivers/
mod.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
#[cfg(feature = "gl")]
mod gl;
#[cfg(feature = "vulkan")]
mod vulkan;

use freya_engine::prelude::Surface as SkiaSurface;
use tracing::info;
use winit::{
    dpi::PhysicalSize,
    event_loop::ActiveEventLoop,
    window::{
        Window,
        WindowAttributes,
    },
};

use crate::LaunchConfig;

pub enum GraphicsDriver {
    #[cfg(feature = "gl")]
    #[allow(dead_code)]
    OpenGl(gl::OpenGLDriver),
    #[cfg(feature = "vulkan")]
    Vulkan(vulkan::VulkanDriver),
}

impl GraphicsDriver {
    pub fn new<State: Clone + 'static>(
        event_loop: &ActiveEventLoop,
        window_attributes: WindowAttributes,
        config: &LaunchConfig<State>,
    ) -> (Self, Window) {
        #[cfg(feature = "vulkan")]
        {
            let (driver, window) = vulkan::VulkanDriver::new(event_loop, window_attributes, config);
            info!("Using vulkan.");

            return (Self::Vulkan(driver), window);
        }

        #[cfg(feature = "gl")]
        #[allow(unreachable_code, clippy::needless_return)]
        {
            let (driver, window) = gl::OpenGLDriver::new(event_loop, window_attributes, config);
            info!("Using OpenGL.");

            return (Self::OpenGl(driver), window);
        }

        #[cfg(not(all(feature = "vulkan", feature = "gl")))]
        #[allow(unreachable_code)]
        {
            unimplemented!("Enable `gl` or `vulkan` features.")
        }
    }

    #[allow(unused)]
    pub fn present(
        &mut self,
        size: PhysicalSize<u32>,
        window: &Window,
        render: impl FnOnce(&mut SkiaSurface, &mut SkiaSurface),
    ) {
        match self {
            #[cfg(feature = "gl")]
            Self::OpenGl(gl) => gl.present(render),
            #[cfg(feature = "vulkan")]
            Self::Vulkan(vk) => vk.present(size, window, render),
            #[cfg(not(all(feature = "vulkan", feature = "gl")))]
            _ => unimplemented!("Enable `gl` or `vulkan` features."),
        }
    }

    #[allow(unused)]
    pub fn resize(&mut self, size: PhysicalSize<u32>) {
        match self {
            #[cfg(feature = "gl")]
            Self::OpenGl(gl) => gl.resize(size),
            #[cfg(feature = "vulkan")]
            Self::Vulkan(vk) => vk.resize(),
            #[cfg(not(all(feature = "vulkan", feature = "gl")))]
            _ => unimplemented!("Enable `gl` or `vulkan` features."),
        }
    }
}