|
| 1 | +use core::hint::black_box; |
| 2 | + |
| 3 | +use criterion::{criterion_group, Criterion}; |
| 4 | +use rand::random; |
| 5 | +use std::time::{Duration, Instant}; |
| 6 | + |
| 7 | +use bevy_render::{ |
| 8 | + mesh::{Indices, Mesh, PrimitiveTopology}, |
| 9 | + render_asset::RenderAssetUsages, |
| 10 | +}; |
| 11 | + |
| 12 | +const GRID_SIZE: usize = 256; |
| 13 | + |
| 14 | +fn compute_normals(c: &mut Criterion) { |
| 15 | + let indices = Indices::U32( |
| 16 | + (0..GRID_SIZE - 1) |
| 17 | + .flat_map(|i| std::iter::repeat(i).zip(0..GRID_SIZE - 1)) |
| 18 | + .flat_map(|(i, j)| { |
| 19 | + let tl = ((GRID_SIZE * j) + i) as u32; |
| 20 | + let tr = tl + 1; |
| 21 | + let bl = ((GRID_SIZE * (j + 1)) + i) as u32; |
| 22 | + let br = bl + 1; |
| 23 | + [tl, bl, tr, tr, bl, br] |
| 24 | + }) |
| 25 | + .collect(), |
| 26 | + ); |
| 27 | + |
| 28 | + let new_mesh = || { |
| 29 | + let positions = (0..GRID_SIZE) |
| 30 | + .flat_map(|i| std::iter::repeat(i).zip(0..GRID_SIZE)) |
| 31 | + .map(|(i, j)| [i as f32, j as f32, random::<f32>()]) |
| 32 | + .collect::<Vec<_>>(); |
| 33 | + Mesh::new( |
| 34 | + PrimitiveTopology::TriangleList, |
| 35 | + RenderAssetUsages::MAIN_WORLD, |
| 36 | + ) |
| 37 | + .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions) |
| 38 | + .with_inserted_indices(indices.clone()) |
| 39 | + }; |
| 40 | + |
| 41 | + c.bench_function("smooth_normals", |b| { |
| 42 | + b.iter_custom(|iters| { |
| 43 | + let mut total = Duration::default(); |
| 44 | + for _ in 0..iters { |
| 45 | + let mut mesh = new_mesh(); |
| 46 | + black_box(mesh.attribute(Mesh::ATTRIBUTE_NORMAL)); |
| 47 | + let start = Instant::now(); |
| 48 | + mesh.compute_smooth_normals(); |
| 49 | + let end = Instant::now(); |
| 50 | + black_box(mesh.attribute(Mesh::ATTRIBUTE_NORMAL)); |
| 51 | + total += end.duration_since(start); |
| 52 | + } |
| 53 | + total |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + c.bench_function("face_weighted_normals", |b| { |
| 58 | + b.iter_custom(|iters| { |
| 59 | + let mut total = Duration::default(); |
| 60 | + for _ in 0..iters { |
| 61 | + let mut mesh = new_mesh(); |
| 62 | + black_box(mesh.attribute(Mesh::ATTRIBUTE_NORMAL)); |
| 63 | + let start = Instant::now(); |
| 64 | + mesh.compute_smooth_normals(); |
| 65 | + let end = Instant::now(); |
| 66 | + black_box(mesh.attribute(Mesh::ATTRIBUTE_NORMAL)); |
| 67 | + total += end.duration_since(start); |
| 68 | + } |
| 69 | + total |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + let new_mesh = || { |
| 74 | + new_mesh() |
| 75 | + .with_duplicated_vertices() |
| 76 | + .with_computed_flat_normals() |
| 77 | + }; |
| 78 | + |
| 79 | + c.bench_function("flat_normals", |b| { |
| 80 | + b.iter_custom(|iters| { |
| 81 | + let mut total = Duration::default(); |
| 82 | + for _ in 0..iters { |
| 83 | + let mut mesh = new_mesh(); |
| 84 | + black_box(mesh.attribute(Mesh::ATTRIBUTE_NORMAL)); |
| 85 | + let start = Instant::now(); |
| 86 | + mesh.compute_flat_normals(); |
| 87 | + let end = Instant::now(); |
| 88 | + black_box(mesh.attribute(Mesh::ATTRIBUTE_NORMAL)); |
| 89 | + total += end.duration_since(start); |
| 90 | + } |
| 91 | + total |
| 92 | + }); |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +criterion_group!(benches, compute_normals); |
0 commit comments