bevy_render_macros/
lib.rs

1#![expect(missing_docs, reason = "Not all docs are written yet, see #3492.")]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4mod as_bind_group;
5mod extract_component;
6mod extract_resource;
7mod specializer;
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::shared().get_path("bevy_render")
16}
17
18pub(crate) fn bevy_ecs_path() -> syn::Path {
19    BevyManifest::shared().get_path("bevy_ecs")
20}
21
22#[proc_macro_derive(ExtractResource)]
23pub fn derive_extract_resource(input: TokenStream) -> TokenStream {
24    extract_resource::derive_extract_resource(input)
25}
26
27/// Implements `ExtractComponent` trait for a component.
28///
29/// The component must implement [`Clone`].
30/// The component will be extracted into the render world via cloning.
31/// Note that this only enables extraction of the component, it does not execute the extraction.
32/// See `ExtractComponentPlugin` to actually perform the extraction.
33///
34/// If you only want to extract a component conditionally, you may use the `extract_component_filter` attribute.
35///
36/// # Example
37///
38/// ```no_compile
39/// use bevy_ecs::component::Component;
40/// use bevy_render_macros::ExtractComponent;
41///
42/// #[derive(Component, Clone, ExtractComponent)]
43/// #[extract_component_filter(With<Camera>)]
44/// pub struct Foo {
45///     pub should_foo: bool,
46/// }
47///
48/// // Without a filter (unconditional).
49/// #[derive(Component, Clone, ExtractComponent)]
50/// pub struct Bar {
51///     pub should_bar: bool,
52/// }
53/// ```
54#[proc_macro_derive(ExtractComponent, attributes(extract_component_filter))]
55pub fn derive_extract_component(input: TokenStream) -> TokenStream {
56    extract_component::derive_extract_component(input)
57}
58
59#[proc_macro_derive(
60    AsBindGroup,
61    attributes(
62        uniform,
63        storage_texture,
64        texture,
65        sampler,
66        bind_group_data,
67        storage,
68        bindless,
69        data
70    )
71)]
72pub fn derive_as_bind_group(input: TokenStream) -> TokenStream {
73    let input = parse_macro_input!(input as DeriveInput);
74
75    as_bind_group::derive_as_bind_group(input).unwrap_or_else(|err| err.to_compile_error().into())
76}
77
78/// Derive macro generating an impl of the trait `RenderLabel`.
79///
80/// This does not work for unions.
81#[proc_macro_derive(RenderLabel)]
82pub fn derive_render_label(input: TokenStream) -> TokenStream {
83    let input = parse_macro_input!(input as DeriveInput);
84    let mut trait_path = bevy_render_path();
85    trait_path
86        .segments
87        .push(format_ident!("render_graph").into());
88    trait_path
89        .segments
90        .push(format_ident!("RenderLabel").into());
91    derive_label(input, "RenderLabel", &trait_path)
92}
93
94/// Derive macro generating an impl of the trait `RenderSubGraph`.
95///
96/// This does not work for unions.
97#[proc_macro_derive(RenderSubGraph)]
98pub fn derive_render_sub_graph(input: TokenStream) -> TokenStream {
99    let input = parse_macro_input!(input as DeriveInput);
100    let mut trait_path = bevy_render_path();
101    trait_path
102        .segments
103        .push(format_ident!("render_graph").into());
104    trait_path
105        .segments
106        .push(format_ident!("RenderSubGraph").into());
107    derive_label(input, "RenderSubGraph", &trait_path)
108}
109
110/// Derive macro generating an impl of the trait `Specializer`
111///
112/// This only works for structs whose members all implement `Specializer`
113#[proc_macro_derive(Specializer, attributes(specialize, key, base_descriptor))]
114pub fn derive_specialize(input: TokenStream) -> TokenStream {
115    specializer::impl_specializer(input)
116}
117
118/// Derive macro generating the most common impl of the trait `SpecializerKey`
119#[proc_macro_derive(SpecializerKey)]
120pub fn derive_specializer_key(input: TokenStream) -> TokenStream {
121    specializer::impl_specializer_key(input)
122}
123
124#[proc_macro_derive(ShaderLabel)]
125pub fn derive_shader_label(input: TokenStream) -> TokenStream {
126    let input = parse_macro_input!(input as DeriveInput);
127    let mut trait_path = bevy_render_path();
128    trait_path
129        .segments
130        .push(format_ident!("render_phase").into());
131    trait_path
132        .segments
133        .push(format_ident!("ShaderLabel").into());
134    derive_label(input, "ShaderLabel", &trait_path)
135}
136
137#[proc_macro_derive(DrawFunctionLabel)]
138pub fn derive_draw_function_label(input: TokenStream) -> TokenStream {
139    let input = parse_macro_input!(input as DeriveInput);
140    let mut trait_path = bevy_render_path();
141    trait_path
142        .segments
143        .push(format_ident!("render_phase").into());
144    trait_path
145        .segments
146        .push(format_ident!("DrawFunctionLabel").into());
147    derive_label(input, "DrawFunctionLabel", &trait_path)
148}