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
21#![cfg_attr(docsrs, feature(doc_auto_cfg))]
22
23pub mod draw;
24mod draw_shaded;
25pub mod options;
26mod shaded_theme;
27mod surface;
28
29use crate::draw::{CustomPipeBuilder, DrawPipe};
30use kas::runner::{self, Result};
31use wgpu::rwh;
32
33pub use draw_shaded::{DrawShaded, DrawShadedImpl};
34pub use options::Options;
35pub use shaded_theme::ShadedTheme;
36pub extern crate wgpu;
37
38/// Graphics context
39pub struct Instance<CB: CustomPipeBuilder> {
40    options: Options,
41    instance: wgpu::Instance,
42    custom: CB,
43}
44
45impl<CB: CustomPipeBuilder> Instance<CB> {
46    /// Construct a new `Instance`
47    ///
48    /// [`Options`] are typically default-constructed then
49    /// [loaded from enviroment variables](Options::load_from_env).
50    pub fn new(options: Options, custom: CB) -> Self {
51        let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
52            backends: options.backend(),
53            ..Default::default()
54        });
55
56        Instance {
57            options,
58            instance,
59            custom,
60        }
61    }
62}
63
64impl<CB: CustomPipeBuilder> runner::GraphicsInstance for Instance<CB> {
65    type Shared = DrawPipe<CB::Pipe>;
66
67    type Surface<'a> = surface::Surface<'a, CB::Pipe>;
68
69    fn new_shared(&mut self, surface: Option<&Self::Surface<'_>>) -> Result<Self::Shared> {
70        DrawPipe::new(
71            &self.instance,
72            &mut self.custom,
73            &self.options,
74            surface.map(|s| &s.surface),
75        )
76    }
77
78    fn new_surface<'window, W>(
79        &mut self,
80        window: W,
81        transparent: bool,
82    ) -> Result<Self::Surface<'window>>
83    where
84        W: rwh::HasWindowHandle + rwh::HasDisplayHandle + Send + Sync + 'window,
85        Self: Sized,
86    {
87        surface::Surface::new(&self.instance, window, transparent)
88    }
89}