Skip to main content

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;
20
21use kas::draw::WindowCommon;
22use kas::geom::{Offset, Rect};
23use shaders::ShaderManager;
24use wgpu::TextureFormat;
25
26pub use custom::{CustomPipe, CustomPipeBuilder, CustomWindow, DrawCustom};
27
28/// Output format
29///
30/// Required by WGPU to be BGRA, either sRGB or Unorm. Currently we assume sRGB
31/// and let the graphics pipeline handle colour conversions.
32pub(crate) const RENDER_TEX_FORMAT: TextureFormat = TextureFormat::Bgra8UnormSrgb;
33
34type Scale = [f32; 4];
35
36#[derive(Debug)]
37struct ClipRegion {
38    rect: Rect,
39    offset: Offset,
40    order: u64,
41}
42
43impl Default for ClipRegion {
44    fn default() -> Self {
45        ClipRegion {
46            rect: Rect::ZERO,
47            offset: Offset::ZERO,
48            order: 1,
49        }
50    }
51}
52
53/// Shared pipeline data
54pub struct DrawPipe<C> {
55    pub(crate) adapter: wgpu::Adapter,
56    pub(crate) device: wgpu::Device,
57    queue: wgpu::Queue,
58    staging_belt: wgpu::util::StagingBelt,
59    bgl_common: wgpu::BindGroupLayout,
60    light_norm_buf: wgpu::Buffer,
61    bg_common: Vec<(wgpu::Buffer, wgpu::BindGroup)>,
62    images: images::Images,
63    text: kas::text::raster::State,
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}