Files
Rostyslav Lesovyi 9fd2637846 Add tools to avoid unnecessary AssetEvent::Modified events that lead to rendering performance costs (#16751) (#22460)
# Objective

- Fixes #16751

## Solution

- `Assets::get_mut` now returns a wrapper `AssetMut` type instead of
`&mut impl Asset`.
- `AssetMut` implements `Deref` and `DerefMut`.
- `DerefMut` marks assets as changed.
- when dropped `AssetMut` will add `AssetEvent::Modified` event to a
queue only in case asset was marked as changed.

## Testing

- Did you test these changes? If so, how?
  - No unit tests were added, change is pretty straightforward.
  - Test project: https://github.com/MatrixDev/bevy-feature-16751-test.
    - With change: ~100 fps.
    - Without change: ~15 fps.
- Are there any parts that need more testing?
- I don't really see how this can break anything or add a measurable
overhead.
- `AssetEvent::Modified` will now be sent after the asset was modified
instead of before. It should not affect anything but still worth noting.
- How can other people (reviewers) test your changes? Is there anything
specific they need to know?
- Have a big amount of entities that constantly update their materials.
- Properties of those materials should be animated in a stepped maned
(like changing color every 0.1 seconds).
  - Update material only if value has actually changed:
```rust
if material.base_color != new_color {
    material.base_color = new_color;
}
```
- If relevant, what platforms did you test these changes on, and are
there any important ones you can't test?
  - tested on macos (Mackbook M1)
  - not a platform-specific issue

PS: This is my first PR, so please don’t judge.
2026-02-10 18:39:37 +00:00

112 lines
3.2 KiB
Rust

//! This example demonstrates how to use a storage buffer with `AsBindGroup` in a custom material.
use bevy::{
mesh::MeshTag,
prelude::*,
reflect::TypePath,
render::{render_resource::AsBindGroup, storage::ShaderBuffer},
shader::ShaderRef,
};
const SHADER_ASSET_PATH: &str = "shaders/storage_buffer.wgsl";
fn main() {
App::new()
.add_plugins((DefaultPlugins, MaterialPlugin::<CustomMaterial>::default()))
.add_systems(Startup, setup)
.add_systems(Update, update)
.run();
}
/// set up a simple 3D scene
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut buffers: ResMut<Assets<ShaderBuffer>>,
mut materials: ResMut<Assets<CustomMaterial>>,
) {
// Example data for the storage buffer
let color_data: Vec<[f32; 4]> = vec![
[1.0, 0.0, 0.0, 1.0],
[0.0, 1.0, 0.0, 1.0],
[0.0, 0.0, 1.0, 1.0],
[1.0, 1.0, 0.0, 1.0],
[0.0, 1.0, 1.0, 1.0],
];
let colors = buffers.add(ShaderBuffer::from(color_data));
let mesh_handle = meshes.add(Cuboid::from_size(Vec3::splat(0.3)));
// Create the custom material with the storage buffer
let material_handle = materials.add(CustomMaterial {
colors: colors.clone(),
});
commands.insert_resource(CustomMaterialHandle(material_handle.clone()));
// Spawn cubes with the custom material
let mut current_color_id: u32 = 0;
for i in -6..=6 {
for j in -3..=3 {
commands.spawn((
Mesh3d(mesh_handle.clone()),
MeshMaterial3d(material_handle.clone()),
MeshTag(current_color_id % 5),
Transform::from_xyz(i as f32, j as f32, 0.0),
));
current_color_id += 1;
}
}
// Camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 0.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
));
}
// Update the material color by time
fn update(
time: Res<Time>,
material_handles: Res<CustomMaterialHandle>,
mut materials: ResMut<Assets<CustomMaterial>>,
mut buffers: ResMut<Assets<ShaderBuffer>>,
) {
let material = materials.get_mut(&material_handles.0).unwrap();
let mut buffer = buffers.get_mut(&material.colors).unwrap();
buffer.set_data(
(0..5)
.map(|i| {
let t = time.elapsed_secs() * 5.0;
[
ops::sin(t + i as f32) / 2.0 + 0.5,
ops::sin(t + i as f32 + 2.0) / 2.0 + 0.5,
ops::sin(t + i as f32 + 4.0) / 2.0 + 0.5,
1.0,
]
})
.collect::<Vec<[f32; 4]>>(),
);
}
// Holds handles to the custom materials
#[derive(Resource)]
struct CustomMaterialHandle(Handle<CustomMaterial>);
// This struct defines the data that will be passed to your shader
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
struct CustomMaterial {
#[storage(0, read_only)]
colors: Handle<ShaderBuffer>,
}
impl Material for CustomMaterial {
fn vertex_shader() -> ShaderRef {
SHADER_ASSET_PATH.into()
}
fn fragment_shader() -> ShaderRef {
SHADER_ASSET_PATH.into()
}
}