Skip to main content

pebble/graphics/types/
flags.rs

1/// Generates a bitflags-style wrapper struct around a `wgpu` flags type, so
2/// no `wgpu` type leaks into the public API.
3macro_rules! bitflags_mirror {
4    (
5        $(#[$doc:meta])*
6        pub struct $Name:ident => $Wgpu:ty {
7            $(const $Flag:ident = $val:expr;)*
8        }
9    ) => {
10        $(#[$doc])*
11        #[derive(Copy, Clone, PartialEq, Eq, Hash)]
12        pub struct $Name(u32);
13
14        impl $Name {
15            $(pub const $Flag: Self = Self($val);)*
16
17            pub const fn empty() -> Self {
18                Self(0)
19            }
20
21            pub const fn bits(self) -> u32 {
22                self.0
23            }
24
25            pub const fn contains(self, other: Self) -> bool {
26                (self.0 & other.0) == other.0
27            }
28
29            pub const fn intersects(self, other: Self) -> bool {
30                (self.0 & other.0) != 0
31            }
32        }
33
34        impl core::ops::BitOr for $Name {
35            type Output = Self;
36            fn bitor(self, rhs: Self) -> Self {
37                Self(self.0 | rhs.0)
38            }
39        }
40
41        impl core::ops::BitOrAssign for $Name {
42            fn bitor_assign(&mut self, rhs: Self) {
43                self.0 |= rhs.0;
44            }
45        }
46
47        impl From<$Name> for $Wgpu {
48            fn from(value: $Name) -> Self {
49                <$Wgpu>::from_bits_truncate(value.0)
50            }
51        }
52    };
53}
54
55bitflags_mirror! {
56    /// Which shader stages a bind group entry is visible to.
57    pub struct ShaderStages => wgpu::ShaderStages {
58        const NONE = 0;
59        const VERTEX = 1 << 0;
60        const FRAGMENT = 1 << 1;
61        const COMPUTE = 1 << 2;
62        const VERTEX_FRAGMENT = (1 << 0) | (1 << 1);
63        const TASK = 1 << 3;
64        const MESH = 1 << 4;
65        const RAY_GENERATION = 1 << 5;
66        const ANY_HIT = 1 << 6;
67        const CLOSEST_HIT = 1 << 7;
68        const MISS = 1 << 8;
69    }
70}
71
72bitflags_mirror! {
73    /// What a GPU buffer can be used for — passed to [`BufferBuilder::with_usage`](crate::graphics::pipeline::buffers::BufferBuilder::with_usage).
74    pub struct BufferUsages => wgpu::BufferUsages {
75        const MAP_READ = 1 << 0;
76        const MAP_WRITE = 1 << 1;
77        const COPY_SRC = 1 << 2;
78        const COPY_DST = 1 << 3;
79        const INDEX = 1 << 4;
80        const VERTEX = 1 << 5;
81        const UNIFORM = 1 << 6;
82        const STORAGE = 1 << 7;
83        const INDIRECT = 1 << 8;
84        const QUERY_RESOLVE = 1 << 9;
85        const BLAS_INPUT = 1 << 10;
86        const TLAS_INPUT = 1 << 11;
87    }
88}
89
90bitflags_mirror! {
91    /// What a GPU texture can be used for — passed to [`RenderTargetTextureBuilder::with_usage`](crate::graphics::pipeline::texture_view::RenderTargetTextureBuilder::with_usage).
92    pub struct TextureUsages => wgpu::TextureUsages {
93        const COPY_SRC = 1 << 0;
94        const COPY_DST = 1 << 1;
95        const TEXTURE_BINDING = 1 << 2;
96        const STORAGE_BINDING = 1 << 3;
97        const RENDER_ATTACHMENT = 1 << 4;
98        const STORAGE_ATOMIC = 1 << 16;
99        const TRANSIENT = 1 << 17;
100    }
101}
102
103bitflags_mirror! {
104    /// Which color channels a render pipeline writes — see [`ColorTargetState`](super::pipeline_state::ColorTargetState).
105    pub struct ColorWrites => wgpu::ColorWrites {
106        const RED = 1 << 0;
107        const GREEN = 1 << 1;
108        const BLUE = 1 << 2;
109        const ALPHA = 1 << 3;
110        const COLOR = (1 << 0) | (1 << 1) | (1 << 2);
111        const ALL = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3);
112    }
113}
114
115impl Default for ColorWrites {
116    fn default() -> Self {
117        Self::ALL
118    }
119}