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
// -------------------------- GFX PLATFORM EXPORTS -----------------------------

pub mod api;
pub mod assembler;
pub mod buffer;
pub mod command;
pub mod fence;
pub mod framebuffer;
pub mod gpu;
pub mod pipeline;
pub mod render_pass;
pub mod resource;
pub mod shader;
pub mod surface;
pub mod viewport;

use gfx_hal::{
  Backend,
  Instance as _,
};

// ----------------------- INSTANCE BUILDER AND INSTANCE -------------------------------

pub struct InstanceBuilder {}

#[cfg(test)]
use mockall::automock;

#[cfg_attr(test, automock)]
impl InstanceBuilder {
  pub fn new() -> Self {
    return Self {};
  }

  /// Builds a graphical instance for the current platform.
  pub fn build<RenderBackend: Backend>(
    self,
    name: &str,
  ) -> Instance<RenderBackend> {
    return Instance::new(name);
  }
}

pub struct Instance<RenderBackend: Backend> {
  gfx_hal_instance: RenderBackend::Instance,
}

impl<RenderBackend: Backend> Instance<RenderBackend> {
  /// Create a new GfxInstance connected to the current platforms primary backend.
  fn new(name: &str) -> Self {
    let instance = RenderBackend::Instance::create(name, 1)
      .expect("gfx backend not supported by the current platform");

    return Self {
      gfx_hal_instance: instance,
    };
  }
}

impl<RenderBackend: Backend> Instance<RenderBackend> {
  /// Returns a list of all available adapters.
  pub(super) fn enumerate_adapters(
    &self,
  ) -> Vec<gfx_hal::adapter::Adapter<RenderBackend>> {
    return self.gfx_hal_instance.enumerate_adapters();
  }

  pub(super) fn first_adapter(
    &self,
  ) -> gfx_hal::adapter::Adapter<RenderBackend> {
    return self.gfx_hal_instance.enumerate_adapters().remove(0);
  }

  pub(super) fn create_surface(
    &self,
    window_handle: &crate::winit::WindowHandle,
  ) -> RenderBackend::Surface {
    return unsafe {
      self
        .gfx_hal_instance
        .create_surface(&window_handle.window_handle)
        .expect("Failed to create a surface using the current instance and window handle.")
    };
  }

  pub(super) fn destroy_surface(&self, surface: RenderBackend::Surface) {
    unsafe {
      self.gfx_hal_instance.destroy_surface(surface);
    }
  }
}

// ----------------------- INTERNAL INSTANCE OPERATIONS ------------------------

pub mod internal {
  pub use super::shader::internal::*;
}