bevy_render_macros/
lib.rs

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