Skip to main content

kas_wgpu/
lib.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! KAS graphics backend over [WGPU]
7//!
8//! This crate implements a KAS's drawing APIs over [WGPU].
9//!
10//! This crate supports themes via the [`kas::theme`], and provides one
11//! additional theme, [`ShadedTheme`].
12//!
13//! Custom GPU-accelerated drawing is supported via [`draw::CustomPipe`]
14//! (see the [Mandlebrot example](https://github.com/kas-gui/kas/blob/master/kas-wgpu/examples/mandlebrot.rs)).
15//!
16//! By default, some environment variables are read for configuration.
17//! See [`options::Options::load_from_env`] for documentation.
18//!
19//! [WGPU]: https://github.com/gfx-rs/wgpu
20
21pub mod draw;
22mod draw_shaded;
23pub mod options;
24mod shaded_theme;
25mod surface;
26
27use crate::draw::{CustomPipeBuilder, DrawPipe};
28use kas::runner::{self, HasDisplayAndWindowHandle, RunError};
29
30pub use draw_shaded::{DrawShaded, DrawShadedImpl};
31pub use options::Options;
32pub use shaded_theme::ShadedTheme;
33pub extern crate wgpu;
34
35/// Graphics context
36pub struct Instance<CB: CustomPipeBuilder> {
37    options: Options,
38    instance: wgpu::Instance,
39    custom: CB,
40}
41
42impl<CB: CustomPipeBuilder> Instance<CB> {
43    /// Construct a new `Instance`
44    ///
45    /// [`Options`] are typically default-constructed then
46    /// [loaded from enviroment variables](Options::load_from_env).
47    pub fn new(options: Options, custom: CB) -> Self {
48        let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
49            backends: options.backend(),
50            ..Default::default()
51        });
52
53        Instance {
54            options,
55            instance,
56            custom,
57        }
58    }
59}
60
61impl<CB: CustomPipeBuilder> runner::GraphicsInstance for Instance<CB> {
62    type Shared = DrawPipe<CB::Pipe>;
63
64    type Surface = surface::Surface<CB::Pipe>;
65
66    fn new_shared(
67        &mut self,
68        surface: Option<&Self::Surface>,
69        features: runner::GraphicsFeatures,
70    ) -> Result<Self::Shared, RunError> {
71        DrawPipe::new(
72            &self.instance,
73            &mut self.custom,
74            &self.options,
75            surface.map(|s| &s.surface),
76            features,
77        )
78    }
79
80    fn new_surface(
81        &mut self,
82        window: std::sync::Arc<dyn HasDisplayAndWindowHandle + Send + Sync>,
83        transparent: bool,
84    ) -> std::result::Result<Self::Surface, RunError>
85    where
86        Self: Sized,
87    {
88        surface::Surface::new(&self.instance, window, transparent)
89    }
90}