Skip to main content

pebble/wgpu/
flags.rs

1//! Hand-rolled bitflag mirrors for wgpu's flag-style types — small enough
2//! (at most a couple dozen bits) that pulling in the `bitflags` crate isn't
3//! worth it. Each mirrors its `wgpu` counterpart's bit layout exactly, so
4//! converting into wgpu is just `from_bits_truncate`.
5
6macro_rules! bitflags_mirror {
7    (
8        $(#[$outer:meta])*
9        pub struct $Name:ident => $Wgpu:ty {
10            $($(#[$inner:meta])* const $Flag:ident = $val:expr;)*
11        }
12    ) => {
13        $(#[$outer])*
14        #[derive(Copy, Clone, PartialEq, Eq, Hash)]
15        pub struct $Name(u32);
16
17        impl $Name {
18            $($(#[$inner])* pub const $Flag: Self = Self($val);)*
19
20            /// No flags set.
21            pub const fn empty() -> Self {
22                Self(0)
23            }
24
25            pub const fn bits(self) -> u32 {
26                self.0
27            }
28
29            /// Whether every flag set in `other` is also set in `self`.
30            pub const fn contains(self, other: Self) -> bool {
31                (self.0 & other.0) == other.0
32            }
33
34            /// Whether `self` and `other` share any set flag.
35            pub const fn intersects(self, other: Self) -> bool {
36                (self.0 & other.0) != 0
37            }
38        }
39
40        impl core::ops::BitOr for $Name {
41            type Output = Self;
42            fn bitor(self, rhs: Self) -> Self {
43                Self(self.0 | rhs.0)
44            }
45        }
46
47        impl core::ops::BitOrAssign for $Name {
48            fn bitor_assign(&mut self, rhs: Self) {
49                self.0 |= rhs.0;
50            }
51        }
52
53        impl From<$Name> for $Wgpu {
54            fn from(value: $Name) -> Self {
55                <$Wgpu>::from_bits_truncate(value.0)
56            }
57        }
58    };
59}
60
61bitflags_mirror! {
62    /// Which shader stage(s) a binding is visible from — mirrors
63    /// `wgpu::ShaderStages` bit-for-bit.
64    pub struct ShaderStages => wgpu::ShaderStages {
65        /// Not visible from any shader stage.
66        const NONE = 0;
67        /// Visible from the vertex shader of a render pipeline.
68        const VERTEX = 1 << 0;
69        /// Visible from the fragment shader of a render pipeline.
70        const FRAGMENT = 1 << 1;
71        /// Visible from the compute shader of a compute pipeline.
72        const COMPUTE = 1 << 2;
73        /// Visible from the vertex and fragment shaders of a render pipeline.
74        const VERTEX_FRAGMENT = (1 << 0) | (1 << 1);
75        /// Visible from the task shader of a mesh pipeline.
76        const TASK = 1 << 3;
77        /// Visible from the mesh shader of a mesh pipeline.
78        const MESH = 1 << 4;
79        /// Visible from the ray generation shader of a ray tracing pipeline.
80        const RAY_GENERATION = 1 << 5;
81        /// Visible from the ray any-hit shader of a ray tracing pipeline.
82        const ANY_HIT = 1 << 6;
83        /// Visible from the ray closest-hit shader of a ray tracing pipeline.
84        const CLOSEST_HIT = 1 << 7;
85        /// Visible from the ray miss shader of a ray tracing pipeline.
86        const MISS = 1 << 8;
87    }
88}
89
90bitflags_mirror! {
91    /// Allowed usages of a buffer — mirrors `wgpu::BufferUsages` bit-for-bit.
92    pub struct BufferUsages => wgpu::BufferUsages {
93        const MAP_READ = 1 << 0;
94        const MAP_WRITE = 1 << 1;
95        const COPY_SRC = 1 << 2;
96        const COPY_DST = 1 << 3;
97        const INDEX = 1 << 4;
98        const VERTEX = 1 << 5;
99        const UNIFORM = 1 << 6;
100        const STORAGE = 1 << 7;
101        const INDIRECT = 1 << 8;
102        const QUERY_RESOLVE = 1 << 9;
103        /// Allows a buffer to be used as input for a bottom level acceleration structure build.
104        const BLAS_INPUT = 1 << 10;
105        /// Allows a buffer to be used as input for a top level acceleration structure build.
106        const TLAS_INPUT = 1 << 11;
107    }
108}
109
110bitflags_mirror! {
111    /// Allowed usages of a texture — mirrors `wgpu::TextureUsages` bit-for-bit.
112    pub struct TextureUsages => wgpu::TextureUsages {
113        const COPY_SRC = 1 << 0;
114        const COPY_DST = 1 << 1;
115        const TEXTURE_BINDING = 1 << 2;
116        const STORAGE_BINDING = 1 << 3;
117        const RENDER_ATTACHMENT = 1 << 4;
118        /// Allows a texture to be used with image atomics. Requires a native-only feature.
119        const STORAGE_ATOMIC = 1 << 16;
120        /// The contents of this texture will not be reused in another pass.
121        const TRANSIENT = 1 << 17;
122    }
123}
124
125bitflags_mirror! {
126    /// Which color channels a render pipeline writes — mirrors
127    /// `wgpu::ColorWrites` bit-for-bit.
128    pub struct ColorWrites => wgpu::ColorWrites {
129        const RED = 1 << 0;
130        const GREEN = 1 << 1;
131        const BLUE = 1 << 2;
132        const ALPHA = 1 << 3;
133        /// Red, green, and blue, but not alpha.
134        const COLOR = (1 << 0) | (1 << 1) | (1 << 2);
135        /// Every channel.
136        const ALL = (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3);
137    }
138}
139
140impl Default for ColorWrites {
141    /// Matches `wgpu::ColorWrites`'s default — every channel.
142    fn default() -> Self {
143        Self::ALL
144    }
145}