bevy/assets/shaders
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
..
animate_shader.wgsl
array_texture.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
automatic_instancing.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
bindless_material.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
cubemap_unlit.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
custom_clustered_decal.wgsl
custom_gltf_2d.wgsl
custom_material_2d.wgsl
custom_material_import.wgsl
custom_material_screenspace_texture.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
custom_material.frag Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
custom_material.vert Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
custom_material.wesl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
custom_material.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
custom_phase_item.wgsl
custom_stencil.wgsl
custom_ui_material.wgsl
custom_vertex_attribute.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
extended_material_bindless.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
extended_material.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
fallback_image_test.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
game_of_life.wgsl Fix game_of_life shader relying on Naga bug (#18951) 2025-04-26 21:38:08 +00:00
gpu_readback.wgsl
instancing.wgsl
irradiance_volume_voxel_visualization.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
line_material.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
manual_material.wgsl Type erased materials (#19667) 2025-06-27 22:57:24 +00:00
post_processing.wgsl
shader_defs.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
show_prepass.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
specialized_mesh_pipeline.wgsl Fix non-crate typos (#18219) 2025-03-11 06:17:48 +00:00
storage_buffer.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
texture_binding_array.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00
tonemapping_test_patterns.wgsl Fix non-crate typos (#18219) 2025-03-11 06:17:48 +00:00
util.wesl Fix wesl in wasm and webgl2 (#18591) 2025-03-28 21:45:02 +00:00
water_material.wgsl Ugrade to wgpu version 25.0 (#19563) 2025-06-26 19:41:47 +00:00