bevy_render_macros/
lib.rs

1// FIXME(15321): solve CI failures, then replace with `#![expect()]`.
2#![allow(missing_docs, reason = "Not all docs are written yet, see #3492.")]
3#![cfg_attr(docsrs, feature(doc_auto_cfg))]
4
5mod as_bind_group;
6mod extract_component;
7mod extract_resource;
8
9use bevy_macro_utils::{derive_label, BevyManifest};
10use proc_macro::TokenStream;
11use quote::format_ident;
12use syn::{parse_macro_input, DeriveInput};
13
14pub(crate) fn bevy_render_path() -> syn::Path {
15    BevyManifest::default()
16        .maybe_get_path("bevy_render")
17        // NOTE: If the derivation is within bevy_render, then we need to return 'crate'
18        .unwrap_or_else(|| BevyManifest::parse_str("crate"))
19}
20
21#[proc_macro_derive(ExtractResource)]
22pub fn derive_extract_resource(input: TokenStream) -> TokenStream {
23    extract_resource::derive_extract_resource(input)
24}
25
26/// Implements `ExtractComponent` trait for a component.
27///
28/// The component must implement [`Clone`].
29/// The component will be extracted into the render world via cloning.
30/// Note that this only enables extraction of the component, it does not execute the extraction.
31/// See `ExtractComponentPlugin` to actually perform the extraction.
32///
33/// If you only want to extract a component conditionally, you may use the `extract_component_filter` attribute.
34///
35/// # Example
36///
37/// ```no_compile
38/// use bevy_ecs::component::Component;
39/// use bevy_render_macros::ExtractComponent;
40///
41/// #[derive(Component, Clone, ExtractComponent)]
42/// #[extract_component_filter(With<Camera>)]
43/// pub struct Foo {
44///     pub should_foo: bool,
45/// }
46///
47/// // Without a filter (unconditional).
48/// #[derive(Component, Clone, ExtractComponent)]
49/// pub struct Bar {
50///     pub should_bar: bool,
51/// }
52/// ```
53#[proc_macro_derive(ExtractComponent, attributes(extract_component_filter))]
54pub fn derive_extract_component(input: TokenStream) -> TokenStream {
55    extract_component::derive_extract_component(input)
56}
57
58#[proc_macro_derive(
59    AsBindGroup,
60    attributes(uniform, storage_texture, texture, sampler, bind_group_data, storage)
61)]
62pub fn derive_as_bind_group(input: TokenStream) -> TokenStream {
63    let input = parse_macro_input!(input as DeriveInput);
64
65    as_bind_group::derive_as_bind_group(input).unwrap_or_else(|err| err.to_compile_error().into())
66}
67
68/// Derive macro generating an impl of the trait `RenderLabel`.
69///
70/// This does not work for unions.
71#[proc_macro_derive(RenderLabel)]
72pub fn derive_render_label(input: TokenStream) -> TokenStream {
73    let input = parse_macro_input!(input as DeriveInput);
74    let mut trait_path = bevy_render_path();
75    trait_path
76        .segments
77        .push(format_ident!("render_graph").into());
78    let mut dyn_eq_path = trait_path.clone();
79    trait_path
80        .segments
81        .push(format_ident!("RenderLabel").into());
82    dyn_eq_path.segments.push(format_ident!("DynEq").into());
83    derive_label(input, "RenderLabel", &trait_path, &dyn_eq_path)
84}
85
86/// Derive macro generating an impl of the trait `RenderSubGraph`.
87///
88/// This does not work for unions.
89#[proc_macro_derive(RenderSubGraph)]
90pub fn derive_render_sub_graph(input: TokenStream) -> TokenStream {
91    let input = parse_macro_input!(input as DeriveInput);
92    let mut trait_path = bevy_render_path();
93    trait_path
94        .segments
95        .push(format_ident!("render_graph").into());
96    let mut dyn_eq_path = trait_path.clone();
97    trait_path
98        .segments
99        .push(format_ident!("RenderSubGraph").into());
100    dyn_eq_path.segments.push(format_ident!("DynEq").into());
101    derive_label(input, "RenderSubGraph", &trait_path, &dyn_eq_path)
102}