kas_wgpu/draw/
mod.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//! Drawing API for `kas_wgpu`
7//!
8//! Extensions to the API of [`kas::draw`], plus some utility types.
9
10mod atlases;
11mod common;
12mod custom;
13mod draw_pipe;
14mod flat_round;
15mod images;
16mod round_2col;
17mod shaded_round;
18mod shaded_square;
19mod shaders;
20mod text_pipe;
21
22use kas::draw::WindowCommon;
23use kas::geom::{Offset, Rect};
24use shaders::ShaderManager;
25use wgpu::TextureFormat;
26
27pub use custom::{CustomPipe, CustomPipeBuilder, CustomWindow, DrawCustom};
28
29/// Output format
30///
31/// Required by WGPU to be BGRA, either sRGB or Unorm. Currently we assume sRGB
32/// and let the graphics pipeline handle colour conversions.
33pub(crate) const RENDER_TEX_FORMAT: TextureFormat = TextureFormat::Bgra8UnormSrgb;
34
35type Scale = [f32; 4];
36
37#[derive(Debug)]
38struct ClipRegion {
39    rect: Rect,
40    offset: Offset,
41    order: u64,
42}
43
44impl Default for ClipRegion {
45    fn default() -> Self {
46        ClipRegion {
47            rect: Rect::ZERO,
48            offset: Offset::ZERO,
49            order: 1,
50        }
51    }
52}
53
54/// Shared pipeline data
55pub struct DrawPipe<C> {
56    pub(crate) adapter: wgpu::Adapter,
57    pub(crate) device: wgpu::Device,
58    queue: wgpu::Queue,
59    staging_belt: wgpu::util::StagingBelt,
60    bgl_common: wgpu::BindGroupLayout,
61    light_norm_buf: wgpu::Buffer,
62    bg_common: Vec<(wgpu::Buffer, wgpu::BindGroup)>,
63    images: images::Images,
64    shaded_square: shaded_square::Pipeline,
65    shaded_round: shaded_round::Pipeline,
66    flat_round: flat_round::Pipeline,
67    round_2col: round_2col::Pipeline,
68    custom: C,
69}
70
71kas::impl_scope! {
72    /// Per-window pipeline data
73    #[impl_default(where CW: Default)]
74    pub struct DrawWindow<CW: CustomWindow> {
75        pub(crate) common: WindowCommon,
76        scale: Scale,
77        clip_regions: Vec<ClipRegion> = vec![Default::default()],
78        images: images::Window,
79        shaded_square: shaded_square::Window,
80        shaded_round: shaded_round::Window,
81        flat_round: flat_round::Window,
82        round_2col: round_2col::Window,
83        custom: CW,
84    }
85}