rafx_framework/render_features/
macro_render_feature_flag.rs

1/// Use to declare a new render feature flag that can be registered. Registration allows easy global
2/// access to the render feature flag index from anywhere in the binary
3///
4/// Each RenderView contains a bitmask of supported feature flags. The supported feature flags can
5/// be queried or changed at run-time allowing features to provide custom behavior on a per-view
6/// basis depending on the state of the RenderView's feature flags.
7///
8/// Use like this:
9///      rafx::declare_render_feature_flag!(MeshBasicUnlitRenderFeatureFlag, MESH_UNLIT_FLAG_INDEX);
10///
11/// The first name is all that really matters, the second name just needs to be a constant that is
12/// exposed via the first name (i.e. MeshBasicUnlitRenderFeatureFlag::feature_flag_index())
13#[macro_export]
14macro_rules! declare_render_feature_flag {
15    ($struct_name:ident, $atomic_constant_name:ident) => {
16        static $atomic_constant_name: std::sync::atomic::AtomicI32 =
17            std::sync::atomic::AtomicI32::new(-1);
18
19        pub struct $struct_name;
20
21        impl $crate::render_features::RenderFeatureFlag for $struct_name {
22            fn set_feature_flag_index(index: $crate::render_features::RenderFeatureFlagIndex) {
23                assert_eq!(
24                    $struct_name::feature_flag_index(),
25                    $crate::render_features::RenderFeatureFlagIndex::MAX,
26                    "feature flag {} was already registered",
27                    $struct_name::feature_flag_debug_name(),
28                );
29
30                $atomic_constant_name.store(
31                    index.try_into().unwrap(),
32                    std::sync::atomic::Ordering::Release,
33                );
34            }
35
36            fn feature_flag_index() -> $crate::render_features::RenderFeatureFlagIndex {
37                $atomic_constant_name.load(std::sync::atomic::Ordering::Acquire)
38                    as $crate::render_features::RenderFeatureFlagIndex
39            }
40
41            fn feature_flag_debug_name() -> &'static str {
42                stringify!($struct_name)
43            }
44        }
45    };
46}