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