bevy/crates/bevy_render/src
charlotte 🌸 e6ba9a6d18
Type erased materials (#19667)
# 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`:
![Screenshot 2025-06-26
235426](https://github.com/user-attachments/assets/10a0ee29-9932-4f91-ab43-33518b117ac5)

- @DGriffin91's Caldera
`cargo run --release --features=bevy/trace_tracy -- --random-materials`

![image](https://github.com/user-attachments/assets/ef91ba6a-8e88-4922-a73f-acb0af5b0dbc)


- @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`
![Screenshot 2025-06-27
000425](https://github.com/user-attachments/assets/9561388b-881d-46cf-8c3d-b15b3e9aedc7)


### 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`.


![image](https://github.com/user-attachments/assets/e3fcca7c-e04e-4a4e-9d89-39d697a9e3b8)

---------

Co-authored-by: IceSentry <IceSentry@users.noreply.github.com>
Co-authored-by: IceSentry <c.giguere42@gmail.com>
2025-06-27 22:57:24 +00:00
..
batching clippy: expect large variants and other small fixes (#19222) 2025-05-15 22:29:59 +00:00
camera Resolution override (#19817) 2025-06-27 16:30:54 +00:00
diagnostic Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
experimental Remove Shader weak_handles from bevy_render. (#19362) 2025-05-26 20:20:25 +00:00
mesh Allow images to be resized on the GPU without losing data (#19462) 2025-06-24 06:22:50 +00:00
primitives reexport entity set collections in entity module (#18413) 2025-03-30 03:51:14 +00:00
render_graph Let query items borrow from query state to avoid needing to clone (#15396) 2025-06-16 21:05:41 +00:00
render_phase Type erased materials (#19667) 2025-06-27 22:57:24 +00:00
render_resource Type erased materials (#19667) 2025-06-27 22:57:24 +00:00
renderer Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
texture use wgpu TextureDataOrder (#19829) 2025-06-27 06:57:29 +00:00
view Add UVec to_extents helper method (#19807) 2025-06-26 20:53:49 +00:00
alpha.rs bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bindless.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
color_operations.wgsl
erased_render_asset.rs Type erased materials (#19667) 2025-06-27 22:57:24 +00:00
extract_component.rs Let query items borrow from query state to avoid needing to clone (#15396) 2025-06-16 21:05:41 +00:00
extract_instances.rs Let query items borrow from query state to avoid needing to clone (#15396) 2025-06-16 21:05:41 +00:00
extract_param.rs Stop storing access for all systems (#19477) 2025-06-13 17:56:09 +00:00
extract_resource.rs Fix "Unrecognized Option" error when using Criterion-specific arguments in benchmarks (#17222) 2025-01-08 00:09:31 +00:00
globals.rs Remove Shader weak_handles from bevy_render. (#19362) 2025-05-26 20:20:25 +00:00
globals.wgsl
gpu_component_array_buffer.rs Adopt consistent FooSystems naming convention for system sets (#18900) 2025-05-06 15:18:03 +00:00
gpu_readback.rs Event Split: Event, EntityEvent, and BufferedEvent (#19647) 2025-06-15 16:46:34 +00:00
lib.rs Type erased materials (#19667) 2025-06-27 22:57:24 +00:00
maths.wgsl Atmosphere LUT parameterization improvements (#17555) 2025-02-03 21:52:11 +00:00
pipelined_rendering.rs Adopt consistent FooSystems naming convention for system sets (#18900) 2025-05-06 15:18:03 +00:00
render_asset.rs Type erased materials (#19667) 2025-06-27 22:57:24 +00:00
settings.rs Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
storage.rs Allow images to be resized on the GPU without losing data (#19462) 2025-06-24 06:22:50 +00:00
sync_component.rs Refactored ComponentHook Parameters into HookContext (#17503) 2025-01-23 02:45:24 +00:00
sync_world.rs Remove 's lifetime from WorldQuery::Fetch (#19720) 2025-06-19 00:58:21 +00:00