aboutsummaryrefslogtreecommitdiff
path: root/azalea/benches/physics.rs
blob: 430809cc15670650d057886fcc8a9ff22e1c148d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::hint::black_box;

use azalea::{
    Vec3,
    pathfinder::simulation::{SimulatedPlayerBundle, SimulationSet},
};
use azalea_block::BlockState;
use azalea_core::position::{ChunkBlockPos, ChunkPos};
use azalea_physics::collision::BlockWithShape;
use azalea_registry::builtin::BlockKind;
use azalea_world::{Chunk, ChunkStorage, PartialChunkStorage};
use criterion::{BatchSize, Bencher, Criterion, criterion_group, criterion_main};

#[allow(dead_code)]
fn generate_world(partial_chunks: &mut PartialChunkStorage, size: u32) -> ChunkStorage {
    let size = size as i32;

    let mut chunks = ChunkStorage::default();
    for chunk_x in -size..size {
        for chunk_z in -size..size {
            let chunk_pos = ChunkPos::new(chunk_x, chunk_z);
            partial_chunks.set(&chunk_pos, Some(Chunk::default()), &mut chunks);
        }
    }

    for chunk_x in -size..size {
        for chunk_z in -size..size {
            let chunk_pos = ChunkPos::new(chunk_x, chunk_z);
            let chunk = chunks.get(&chunk_pos).unwrap();
            let mut chunk = chunk.write();
            for x in 0..16_u8 {
                for z in 0..16_u8 {
                    chunk.set_block_state(
                        &ChunkBlockPos::new(x, 1, z),
                        BlockKind::Stone.into(),
                        chunks.min_y(),
                    );
                }
            }
        }
    }

    // let mut start = BlockPos::new(-64, 4, -64);
    // // move start down until it's on a solid block
    // while chunks.get_block_state(&start).unwrap().is_air() {
    //     start = start.down(1);
    // }
    // start = start.up(1);

    // let mut end = BlockPos::new(63, 4, 63);
    // // move end down until it's on a solid block
    // while chunks.get_block_state(&end).unwrap().is_air() {
    //     end = end.down(1);
    // }
    // end = end.up(1);

    chunks
}

fn run_physics_benchmark(b: &mut Bencher<'_>) {
    let mut partial_chunks = PartialChunkStorage::new(32);

    let world = generate_world(&mut partial_chunks, 4);

    let mut simulation_set = SimulationSet::new(world);

    // let entity = simulation_set.spawn(SimulatedPlayerBundle::new(Vec3::new(0.0,
    // 4.0, 0.0))); for _ in 0..20 {
    //     simulation_set.tick();
    //     println!("tick over");
    // }
    // simulation_set.despawn(entity);
    // std::process::exit(0);

    b.iter(|| {
        let entity = simulation_set.spawn(SimulatedPlayerBundle::new(Vec3::new(0.5, 2.0, 0.5)));
        for _ in 0..200 {
            simulation_set.tick();
        }
        simulation_set.despawn(entity);
    })
}

fn bench_physics(c: &mut Criterion) {
    c.bench_function("physics", |b| {
        run_physics_benchmark(b);
    });

    c.bench_function("is_collision_shape_full", |b| {
        b.iter_batched(
            || {
                let mut blocks = Vec::new();
                for _ in 0..1024 {
                    let id = rand::random_range(0..=BlockState::MAX_STATE);
                    let block = BlockState::try_from(id).unwrap();
                    blocks.push(block);
                }
                blocks
            },
            |blocks| {
                for block in blocks {
                    black_box(block.is_collision_shape_full());
                    // black_box(block.collision_shape() == SHAPE1);
                }
            },
            BatchSize::LargeInput,
        );
    });
}

criterion_group!(benches, bench_physics);
criterion_main!(benches);