
# Objective Closes #18075 In order to enable a number of patterns for dynamic materials in the engine, it's necessary to decouple the renderer from the `Material` trait. This opens the possibility for: - Materials that aren't coupled to `AsBindGroup`. - 2d using the underlying 3d bindless infrastructure. - Dynamic materials that can change their layout at runtime. - Materials that aren't even backed by a Rust struct at all. ## Solution In short, remove all trait bounds from render world material systems and resources. This means moving a bunch of stuff onto `MaterialProperties` and engaging in some hacks to make specialization work. Rather than storing the bind group data in `MaterialBindGroupAllocator`, right now we're storing it in a closure on `MaterialProperties`. TBD if this has bad performance characteristics. ## Benchmarks - `many_cubes`: `cargo run --example many_cubes --release --features=bevy/trace_tracy -- --vary-material-data-per-instance`:  - @DGriffin91's Caldera `cargo run --release --features=bevy/trace_tracy -- --random-materials`  - @DGriffin91's Caldera with 20 unique material types (i.e. `MaterialPlugin<M>`) and random materials per mesh `cargo run --release --features=bevy/trace_tracy -- --random-materials`  ### TODO - We almost certainly lost some parallelization from removing the type params that could be gained back from smarter iteration. - Test all the things that could have broken. - ~Fix meshlets~ ## Showcase See [the example](https://github.com/bevyengine/bevy/pull/19667/files#diff-9d768cfe1c3aa81eff365d250d3cbe5a63e8df63e81dd85f64c3c3cd993f6d94) for a custom material implemented without the use of the `Material` trait and thus `AsBindGroup`.  --------- Co-authored-by: IceSentry <IceSentry@users.noreply.github.com> Co-authored-by: IceSentry <c.giguere42@gmail.com>
130 lines
4.0 KiB
Rust
130 lines
4.0 KiB
Rust
#![expect(missing_docs, reason = "Not all docs are written yet, see #3492.")]
|
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
|
|
|
mod as_bind_group;
|
|
mod extract_component;
|
|
mod extract_resource;
|
|
|
|
use bevy_macro_utils::{derive_label, BevyManifest};
|
|
use proc_macro::TokenStream;
|
|
use quote::format_ident;
|
|
use syn::{parse_macro_input, DeriveInput};
|
|
|
|
pub(crate) fn bevy_render_path() -> syn::Path {
|
|
BevyManifest::shared().get_path("bevy_render")
|
|
}
|
|
|
|
#[proc_macro_derive(ExtractResource)]
|
|
pub fn derive_extract_resource(input: TokenStream) -> TokenStream {
|
|
extract_resource::derive_extract_resource(input)
|
|
}
|
|
|
|
/// Implements `ExtractComponent` trait for a component.
|
|
///
|
|
/// The component must implement [`Clone`].
|
|
/// The component will be extracted into the render world via cloning.
|
|
/// Note that this only enables extraction of the component, it does not execute the extraction.
|
|
/// See `ExtractComponentPlugin` to actually perform the extraction.
|
|
///
|
|
/// If you only want to extract a component conditionally, you may use the `extract_component_filter` attribute.
|
|
///
|
|
/// # Example
|
|
///
|
|
/// ```no_compile
|
|
/// use bevy_ecs::component::Component;
|
|
/// use bevy_render_macros::ExtractComponent;
|
|
///
|
|
/// #[derive(Component, Clone, ExtractComponent)]
|
|
/// #[extract_component_filter(With<Camera>)]
|
|
/// pub struct Foo {
|
|
/// pub should_foo: bool,
|
|
/// }
|
|
///
|
|
/// // Without a filter (unconditional).
|
|
/// #[derive(Component, Clone, ExtractComponent)]
|
|
/// pub struct Bar {
|
|
/// pub should_bar: bool,
|
|
/// }
|
|
/// ```
|
|
#[proc_macro_derive(ExtractComponent, attributes(extract_component_filter))]
|
|
pub fn derive_extract_component(input: TokenStream) -> TokenStream {
|
|
extract_component::derive_extract_component(input)
|
|
}
|
|
|
|
#[proc_macro_derive(
|
|
AsBindGroup,
|
|
attributes(
|
|
uniform,
|
|
storage_texture,
|
|
texture,
|
|
sampler,
|
|
bind_group_data,
|
|
storage,
|
|
bindless,
|
|
data
|
|
)
|
|
)]
|
|
pub fn derive_as_bind_group(input: TokenStream) -> TokenStream {
|
|
let input = parse_macro_input!(input as DeriveInput);
|
|
|
|
as_bind_group::derive_as_bind_group(input).unwrap_or_else(|err| err.to_compile_error().into())
|
|
}
|
|
|
|
/// Derive macro generating an impl of the trait `RenderLabel`.
|
|
///
|
|
/// This does not work for unions.
|
|
#[proc_macro_derive(RenderLabel)]
|
|
pub fn derive_render_label(input: TokenStream) -> TokenStream {
|
|
let input = parse_macro_input!(input as DeriveInput);
|
|
let mut trait_path = bevy_render_path();
|
|
trait_path
|
|
.segments
|
|
.push(format_ident!("render_graph").into());
|
|
trait_path
|
|
.segments
|
|
.push(format_ident!("RenderLabel").into());
|
|
derive_label(input, "RenderLabel", &trait_path)
|
|
}
|
|
|
|
/// Derive macro generating an impl of the trait `RenderSubGraph`.
|
|
///
|
|
/// This does not work for unions.
|
|
#[proc_macro_derive(RenderSubGraph)]
|
|
pub fn derive_render_sub_graph(input: TokenStream) -> TokenStream {
|
|
let input = parse_macro_input!(input as DeriveInput);
|
|
let mut trait_path = bevy_render_path();
|
|
trait_path
|
|
.segments
|
|
.push(format_ident!("render_graph").into());
|
|
trait_path
|
|
.segments
|
|
.push(format_ident!("RenderSubGraph").into());
|
|
derive_label(input, "RenderSubGraph", &trait_path)
|
|
}
|
|
|
|
#[proc_macro_derive(ShaderLabel)]
|
|
pub fn derive_shader_label(input: TokenStream) -> TokenStream {
|
|
let input = parse_macro_input!(input as DeriveInput);
|
|
let mut trait_path = bevy_render_path();
|
|
trait_path
|
|
.segments
|
|
.push(format_ident!("render_phase").into());
|
|
trait_path
|
|
.segments
|
|
.push(format_ident!("ShaderLabel").into());
|
|
derive_label(input, "ShaderLabel", &trait_path)
|
|
}
|
|
|
|
#[proc_macro_derive(DrawFunctionLabel)]
|
|
pub fn derive_draw_function_label(input: TokenStream) -> TokenStream {
|
|
let input = parse_macro_input!(input as DeriveInput);
|
|
let mut trait_path = bevy_render_path();
|
|
trait_path
|
|
.segments
|
|
.push(format_ident!("render_phase").into());
|
|
trait_path
|
|
.segments
|
|
.push(format_ident!("DrawFunctionLabel").into());
|
|
derive_label(input, "DrawFunctionLabel", &trait_path)
|
|
}
|