Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add many_materials stress test #17346

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2866,6 +2866,17 @@ description = "Displays many Text2d! Used for performance testing."
category = "Stress Tests"
wasm = true

[[example]]
name = "many_materials"
path = "examples/stress_tests/many_materials.rs"
doc-scrape-examples = true

[package.metadata.example.many_materials]
name = "Many Animated Materials"
description = "Benchmark to test rendering many animated materials"
category = "Stress Tests"
wasm = true

[[example]]
name = "transform_hierarchy"
path = "examples/stress_tests/transform_hierarchy.rs"
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ Example | Description
[Many Lights](../examples/stress_tests/many_lights.rs) | Simple benchmark to test rendering many point lights. Run with `WGPU_SETTINGS_PRIO=webgl2` to restrict to uniform buffers and max 256 lights
[Many Sprites](../examples/stress_tests/many_sprites.rs) | Displays many sprites in a grid arrangement! Used for performance testing. Use `--colored` to enable color tinted sprites.
[Many Text2d](../examples/stress_tests/many_text2d.rs) | Displays many Text2d! Used for performance testing.
[Many Materials](../examples/stress_tests/many_materials.rs) | Benchmark to test rendering many animated materials
[Text Pipeline](../examples/stress_tests/text_pipeline.rs) | Text Pipeline benchmark
[Transform Hierarchy](../examples/stress_tests/transform_hierarchy.rs) | Various test cases for hierarchy and transform propagation performance

Expand Down
107 changes: 107 additions & 0 deletions examples/stress_tests/many_materials.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
//! Benchmark to test rendering many animated materials
use argh::FromArgs;
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
prelude::*,
window::{PresentMode, WindowPlugin, WindowResolution},
};
use std::f32::consts::PI;

#[derive(FromArgs, Resource)]
/// Command-line arguments for the `many_materials` stress test.
struct Args {
/// the size of the grid of materials to render (n x n)
#[argh(option, short = 'n', default = "10")]
grid_size: usize,
}

fn main() {
// `from_env` panics on the web
#[cfg(not(target_arch = "wasm32"))]
let args: Args = argh::from_env();
#[cfg(target_arch = "wasm32")]
let args = Args::from_args(&[], &[]).unwrap();

App::new()
.add_plugins((
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
resolution: WindowResolution::new(1920.0, 1080.0)
.with_scale_factor_override(1.0),
title: "many_materials".into(),
present_mode: PresentMode::AutoNoVsync,
..default()
}),
..default()
}),
FrameTimeDiagnosticsPlugin::default(),
LogDiagnosticsPlugin::default(),
))
.insert_resource(args)
.add_systems(Startup, setup)
.add_systems(Update, animate_materials)
.run();
}

fn setup(
mut commands: Commands,
args: Res<Args>,
mesh_assets: ResMut<Assets<Mesh>>,
material_assets: ResMut<Assets<StandardMaterial>>,
) {
let args = args.into_inner();
let material_assets = material_assets.into_inner();
let mesh_assets = mesh_assets.into_inner();
let n = args.grid_size;

// Camera
commands.spawn((
Camera3d::default(),
Transform::from_xyz(n as f32 + 1.0, 1.0, n as f32 + 1.0)
.looking_at(Vec3::new(0.0, -0.5, 0.0), Vec3::Y),
));
CrazyRoka marked this conversation as resolved.
Show resolved Hide resolved

// Plane
commands.spawn((
Mesh3d(mesh_assets.add(Plane3d::default().mesh().size(50.0, 50.0))),
MeshMaterial3d(material_assets.add(Color::linear_rgb(0.3, 0.5, 0.3))),
));
CrazyRoka marked this conversation as resolved.
Show resolved Hide resolved

// Light
commands.spawn((
Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, 1.0, -PI / 4.)),
DirectionalLight {
illuminance: 3000.0,
shadows_enabled: true,
..default()
},
));

// Cubes
for x in 0..n {
for z in 0..n {
commands.spawn((
Mesh3d(mesh_assets.add(Cuboid::from_size(Vec3::ONE))),
MeshMaterial3d(material_assets.add(Color::WHITE)),
Transform::from_translation(Vec3::new(x as f32, 0.0, z as f32)),
));
}
}
CrazyRoka marked this conversation as resolved.
Show resolved Hide resolved
}

fn animate_materials(
material_handles: Query<&MeshMaterial3d<StandardMaterial>>,
time: Res<Time>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
for (i, material_handle) in material_handles.iter().enumerate() {
if let Some(material) = materials.get_mut(material_handle) {
let color = Color::hsl(
((i as f32 * 2.345 + time.elapsed_secs()) * 100.0) % 360.0,
1.0,
0.5,
);
material.base_color = color;
}
}
}
Loading