rafx_framework/render_features/
macro_render_feature.rs

1/// Use to declare a new render feature that can be registered. Registration allows easy global
2/// access to the render feature index from anywhere in the binary
3///
4/// Use like this:
5///      rafx::declare_render_feature!(Debug3DRenderFeature, DEBUG_3D_RENDER_FEATURE);
6///
7/// The first name is all that really matters, the second name just needs to be a constant that is
8/// exposed via the first name (i.e. Debug3DRenderFeature::feature_index())
9///
10/// This macro will also define the following helper functions in the same scope.
11/// - `render_feature_index()`: Syntactic sugar for Debug3DRenderFeature::feature_index().
12/// - `render_feature_debug_name()`: Syntactic sugar for Debug3DRenderFeature::feature_debug_name().
13/// - `render_feature_debug_constants()`: Returns a struct containing `&'static str` debug strings for the feature.
14#[macro_export]
15macro_rules! declare_render_feature {
16    ($struct_name:ident, $atomic_constant_name:ident) => {
17        static $atomic_constant_name: std::sync::atomic::AtomicI32 =
18            std::sync::atomic::AtomicI32::new(-1);
19
20        pub struct $struct_name;
21
22        static RENDER_FEATURE_DEBUG_CONSTANTS: $crate::render_features::RenderFeatureDebugConstants =
23                $crate::render_features::RenderFeatureDebugConstants {
24            feature_name: stringify!($struct_name),
25
26            begin_per_frame_extract: stringify!($struct_name begin_per_frame_extract),
27            extract_render_object_instance: stringify!($struct_name extract_render_object_instance),
28            extract_render_object_instance_per_view: stringify!($struct_name extract_render_object_instance_per_view),
29            end_per_view_extract: stringify!($struct_name end_per_view_extract),
30            end_per_frame_extract: stringify!($struct_name end_per_frame_extract),
31
32            begin_per_frame_prepare: stringify!($struct_name begin_per_frame_prepare),
33            prepare_render_object_instance: stringify!($struct_name prepare_render_object_instance),
34            prepare_render_object_instance_per_view: stringify!($struct_name prepare_render_object_instance_per_view),
35            end_per_view_prepare: stringify!($struct_name end_per_view_prepare),
36            end_per_frame_prepare: stringify!($struct_name end_per_frame_prepare),
37
38            on_begin_execute_graph: stringify!($struct_name on_begin_execute_graph),
39            render_submit_node: stringify!($struct_name render_submit_node),
40            begin_submit_node_batch: stringify!($struct_name begin_submit_node_batch),
41        };
42
43        impl $crate::render_features::RenderFeature for $struct_name {
44            fn set_feature_index(index: $crate::render_features::RenderFeatureIndex) {
45                assert_eq!(
46                    $struct_name::feature_index(),
47                    $crate::render_features::RenderFeatureIndex::MAX,
48                    "feature {} was already registered",
49                    $struct_name::feature_debug_name(),
50                );
51
52                $atomic_constant_name.store(
53                    index.try_into().unwrap(),
54                    std::sync::atomic::Ordering::Release,
55                );
56            }
57
58            fn feature_index() -> $crate::render_features::RenderFeatureIndex {
59                $atomic_constant_name.load(std::sync::atomic::Ordering::Acquire)
60                    as $crate::render_features::RenderFeatureIndex
61            }
62
63            fn feature_debug_name() -> &'static str {
64                render_feature_debug_name()
65            }
66
67            fn feature_debug_constants() -> &'static $crate::render_features::RenderFeatureDebugConstants {
68                render_feature_debug_constants()
69            }
70        }
71
72        #[inline(always)]
73        fn render_feature_index() -> $crate::render_features::RenderFeatureIndex {
74            $struct_name::feature_index()
75        }
76
77        #[inline(always)]
78        fn render_feature_debug_name() -> &'static str {
79            render_feature_debug_constants().feature_name
80        }
81
82        #[inline(always)]
83        fn render_feature_debug_constants() -> &'static $crate::render_features::RenderFeatureDebugConstants {
84            &RENDER_FEATURE_DEBUG_CONSTANTS
85        }
86    };
87}