lambda_platform/gfx/
mod.rs

1// -------------------------- GFX PLATFORM EXPORTS -----------------------------
2
3pub mod api;
4pub mod assembler;
5pub mod buffer;
6pub mod command;
7pub mod fence;
8pub mod framebuffer;
9pub mod gpu;
10pub mod pipeline;
11pub mod render_pass;
12pub mod resource;
13pub mod shader;
14pub mod surface;
15pub mod viewport;
16
17use gfx_hal::{
18  Backend,
19  Instance as _,
20};
21
22// ----------------------- INSTANCE BUILDER AND INSTANCE -------------------------------
23
24pub struct InstanceBuilder {}
25
26#[cfg(test)]
27use mockall::automock;
28
29#[cfg_attr(test, automock)]
30impl InstanceBuilder {
31  pub fn new() -> Self {
32    return Self {};
33  }
34
35  /// Builds a graphical instance for the current platform.
36  pub fn build<RenderBackend: Backend>(
37    self,
38    name: &str,
39  ) -> Instance<RenderBackend> {
40    return Instance::new(name);
41  }
42}
43
44pub struct Instance<RenderBackend: Backend> {
45  gfx_hal_instance: RenderBackend::Instance,
46}
47
48impl<RenderBackend: Backend> Instance<RenderBackend> {
49  /// Create a new GfxInstance connected to the current platforms primary backend.
50  fn new(name: &str) -> Self {
51    let instance = RenderBackend::Instance::create(name, 1)
52      .expect("gfx backend not supported by the current platform");
53
54    return Self {
55      gfx_hal_instance: instance,
56    };
57  }
58}
59
60impl<RenderBackend: Backend> Instance<RenderBackend> {
61  /// Returns a list of all available adapters.
62  pub(super) fn enumerate_adapters(
63    &self,
64  ) -> Vec<gfx_hal::adapter::Adapter<RenderBackend>> {
65    return self.gfx_hal_instance.enumerate_adapters();
66  }
67
68  pub(super) fn first_adapter(
69    &self,
70  ) -> gfx_hal::adapter::Adapter<RenderBackend> {
71    return self.gfx_hal_instance.enumerate_adapters().remove(0);
72  }
73
74  pub(super) fn create_surface(
75    &self,
76    window_handle: &crate::winit::WindowHandle,
77  ) -> RenderBackend::Surface {
78    return unsafe {
79      self
80        .gfx_hal_instance
81        .create_surface(&window_handle.window_handle)
82        .expect("Failed to create a surface using the current instance and window handle.")
83    };
84  }
85
86  pub(super) fn destroy_surface(&self, surface: RenderBackend::Surface) {
87    unsafe {
88      self.gfx_hal_instance.destroy_surface(surface);
89    }
90  }
91}
92
93// ----------------------- INTERNAL INSTANCE OPERATIONS ------------------------
94
95pub mod internal {
96  pub use super::shader::internal::*;
97}