rafx_framework/render_features/
render_feature_mask.rs

1use crate::render_features::registry::{RenderFeatureMaskInnerType, MAX_RENDER_FEATURE_COUNT};
2use crate::render_features::{RenderFeature, RenderFeatureIndex};
3
4#[derive(Default)]
5pub struct RenderFeatureMaskBuilder(RenderFeatureMaskInnerType);
6
7impl RenderFeatureMaskBuilder {
8    pub fn add_render_feature<RenderFeatureT: RenderFeature>(mut self) -> RenderFeatureMaskBuilder {
9        let index = RenderFeatureT::feature_index();
10        assert!(
11            index < MAX_RENDER_FEATURE_COUNT,
12            "feature {} is not registered",
13            RenderFeatureT::feature_debug_name()
14        );
15        self.0 |= 1 << RenderFeatureT::feature_index();
16        self
17    }
18
19    pub fn build(self) -> RenderFeatureMask {
20        RenderFeatureMask(self.0)
21    }
22}
23
24#[derive(Copy, Clone, Debug)]
25pub struct RenderFeatureMask(RenderFeatureMaskInnerType);
26
27impl RenderFeatureMask {
28    pub fn is_included<RenderFeatureT: RenderFeature>(&self) -> bool {
29        let index = RenderFeatureT::feature_index();
30        assert!(
31            index < MAX_RENDER_FEATURE_COUNT,
32            "feature {} is not registered",
33            RenderFeatureT::feature_debug_name()
34        );
35
36        self.is_included_index_unchecked(index)
37    }
38
39    #[inline(always)]
40    pub fn is_included_index(
41        &self,
42        index: RenderFeatureIndex,
43    ) -> bool {
44        assert!(
45            index < MAX_RENDER_FEATURE_COUNT,
46            "feature index {} is invalid (did you forget to register a feature?)",
47            index
48        );
49
50        self.is_included_index_unchecked(index)
51    }
52
53    pub fn empty() -> Self {
54        RenderFeatureMask(0)
55    }
56
57    #[inline(always)]
58    fn is_included_index_unchecked(
59        &self,
60        index: RenderFeatureIndex,
61    ) -> bool {
62        (self.0 & 1 << index) != 0
63    }
64}