Tracing and Performance improvements

tracing
chunk rebuild testing
speed up world gen by moving collider creation to thread pool
This commit is contained in:
2024-05-07 23:26:13 -04:00
parent 58a9f62dca
commit 4e9a35adc6
11 changed files with 206 additions and 46 deletions

91
Cargo.lock generated
View File

@@ -816,8 +816,10 @@ dependencies = [
"bevy_ecs",
"bevy_utils",
"console_error_panic_hook",
"tracing-error",
"tracing-log 0.1.4",
"tracing-subscriber",
"tracing-tracy",
"tracing-wasm",
]
@@ -964,6 +966,7 @@ dependencies = [
"ktx2",
"naga",
"naga_oil",
"profiling",
"ruzstd",
"serde",
"thiserror",
@@ -2097,6 +2100,20 @@ dependencies = [
"thread_local",
]
[[package]]
name = "generator"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "186014d53bc231d0090ef8d6f03e0920c54d85a5ed22f4f2f74315ec56cf83fb"
dependencies = [
"cc",
"cfg-if",
"libc",
"log",
"rustversion",
"windows 0.54.0",
]
[[package]]
name = "gethostname"
version = "0.4.3"
@@ -2660,6 +2677,19 @@ version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "loom"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "419e0dc8046cb947daa77eb95ae174acfbddb7673b4151f56d1eed8e93fbfaca"
dependencies = [
"cfg-if",
"generator",
"scoped-tls",
"tracing",
"tracing-subscriber",
]
[[package]]
name = "mach2"
version = "0.4.2"
@@ -3323,6 +3353,20 @@ name = "profiling"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43d84d1d7a6ac92673717f9f6d1518374ef257669c24ebc5ac25d5033828be58"
dependencies = [
"profiling-procmacros",
"tracing",
]
[[package]]
name = "profiling-procmacros"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8021cf59c8ec9c432cfc2526ac6b8aa508ecaf29cd415f271b8406c1b851c3fd"
dependencies = [
"quote",
"syn 2.0.55",
]
[[package]]
name = "quick-xml"
@@ -3609,6 +3653,12 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "rustversion"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "092474d1a01ea8278f69e6a358998405fae5b8b963ddaeb2b0b04a128bf1dfb0"
[[package]]
name = "ruzstd"
version = "0.5.0"
@@ -4021,6 +4071,16 @@ dependencies = [
"valuable",
]
[[package]]
name = "tracing-error"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e"
dependencies = [
"tracing",
"tracing-subscriber",
]
[[package]]
name = "tracing-log"
version = "0.1.4"
@@ -4061,6 +4121,17 @@ dependencies = [
"tracing-log 0.2.0",
]
[[package]]
name = "tracing-tracy"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6024d04f84a69fd0d1dc1eee3a2b070bd246530a0582f9982ae487cb6c703614"
dependencies = [
"tracing-core",
"tracing-subscriber",
"tracy-client",
]
[[package]]
name = "tracing-wasm"
version = "0.2.1"
@@ -4072,6 +4143,26 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "tracy-client"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59fb931a64ff88984f86d3e9bcd1ae8843aa7fe44dd0f8097527bc172351741d"
dependencies = [
"loom",
"once_cell",
"tracy-client-sys",
]
[[package]]
name = "tracy-client-sys"
version = "0.22.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d104d610dfa9dd154535102cc9c6164ae1fa37842bc2d9e83f9ac82b0ae0882"
dependencies = [
"cc",
]
[[package]]
name = "ttf-parser"
version = "0.20.0"

View File

@@ -13,3 +13,6 @@ serde_json = "1.0.115"
asset_loader = {path = "../asset_loader"}
rayon = "1.10.0"
bevy-inspector-egui = "0.24.0"
[features]
tracing = ["bevy/trace_tracy"]

View File

@@ -1,10 +1,13 @@
use bevy::prelude::*;
use crate::{hex_utils::*, prelude::*};
#[cfg(feature = "tracing")]
use bevy::log::*;
use bevy::prelude::*;
const CHUNK_TOTAL: usize = Chunk::SIZE * Chunk::SIZE;
pub fn generate_chunk_collider(chunk: &Chunk, map: &Map) -> (Vec<Vec3>, Vec<[u32; 3]>) {
#[cfg(feature = "tracing")]
let span = info_span!("generate_chunk_collider").entered();
let vertex_count: usize = CHUNK_TOTAL * 6;
let mut verts = Vec::with_capacity(vertex_count);
let mut indices = Vec::with_capacity(vertex_count);
@@ -28,12 +31,6 @@ fn create_tile_collider(pos: Vec3, verts: &mut Vec<Vec3>, indices: &mut Vec<[u32
let p = pos + HEX_CORNERS[i];
verts.push(p);
}
// for i in 0..3 {
// let off = i * 2;
// indices.push([off + idx, ((off + 1) % 6) + idx, ((off + 2) % 6) + idx]);
// }
// indices.push([idx, idx + 2, idx + 4]);
//Top Surfave
indices.push([idx, idx + 1, idx + 5]);

View File

@@ -120,6 +120,12 @@ impl HexCoord {
return true;
}
pub fn is_on_chunk_edge(&self) -> bool {
let offset = self.to_offset().rem_euclid(IVec2::splat(Chunk::SIZE as i32));
let e = (Chunk::SIZE - 1) as i32;
return offset.x == 0 || offset.y == 0 || offset.x == e || offset.y == e;
}
pub fn to_chunk_pos(&self) -> IVec2 {
let off = self.to_offset();

View File

@@ -144,6 +144,10 @@ pub mod prelude {
pub fn get_world_size(&self) -> Vec2 {
return Vec2::new(self.get_world_width(), self.get_world_height());
}
pub fn set_height(&mut self, pos: &HexCoord, height: f32) {
self.chunks[pos.to_chunk_index(self.width)].heights[pos.to_chunk_local_index()] = height;
}
}
pub const ATTRIBUTE_PACKED_VERTEX_DATA: MeshVertexAttribute =
MeshVertexAttribute::new("PackedVertexData", 988540817, VertexFormat::Uint32);

View File

@@ -3,6 +3,8 @@ use crate::hex_utils::HexCoord;
use crate::tile_manager::TileAsset;
use crate::tile_mapper::TileMapperAsset;
use crate::{hex_utils::offset3d_to_world, prelude::*};
#[cfg(feature = "tracing")]
use bevy::log::*;
use bevy::{
prelude::*,
render::{
@@ -18,6 +20,9 @@ pub fn generate_chunk_mesh(
tiles: &Res<Assets<TileAsset>>,
mappers: &Res<Assets<TileMapperAsset>>,
) -> Mesh {
#[cfg(feature = "tracing")]
let span = info_span!("generate_chunk_mesh").entered();
let vertex_count: usize = Chunk::SIZE * Chunk::SIZE * 6;
let mut verts = Vec::with_capacity(vertex_count);
let mut uvs = Vec::with_capacity(vertex_count);

View File

@@ -14,3 +14,7 @@ noise = "0.8.2"
world_generation ={path="../../engine/world_generation"}
bevy_rapier3d = { version = "0.25.0", features = [ "simd-stable", "debug-render-3d","parallel" ] }
rayon = "1.10.0"
[features]
tracing = ["bevy/trace_tracy", "world_generation/tracing"]

View File

@@ -22,7 +22,8 @@ pub struct ChunkRebuildPlugin;
impl Plugin for ChunkRebuildPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(ChunkRebuildQueue::default());
app.add_systems(PostUpdate, chunk_rebuilder);
app.init_resource::<PhosChunkRegistry>();
app.add_systems(PreUpdate, chunk_rebuilder);
}
}
@@ -58,17 +59,18 @@ fn chunk_rebuilder(
.map(|idx| {
let chunk = &heightmap.chunks[*idx];
let mesh = generate_chunk_mesh(chunk, &heightmap, cur_painter, &tile_assets, &tile_mappers);
let collision = generate_chunk_collider(chunk, &heightmap);
let (col_verts, col_indicies) = generate_chunk_collider(chunk, &heightmap);
let collider = Collider::trimesh_with_flags(col_verts, col_indicies, TriMeshFlags::MERGE_DUPLICATE_VERTICES);
return (
mesh,
collision,
collider,
offset_to_world(chunk.chunk_offset * Chunk::SIZE as i32, 0.),
hex_utils::offset_to_index(chunk.chunk_offset, heightmap.width),
);
})
.collect();
for (mesh, (col_verts, col_indicies), pos, index) in chunk_meshes {
for (mesh, collider, pos, index) in chunk_meshes {
let chunk = commands.spawn((
MaterialMeshBundle {
mesh: meshes.add(mesh),
@@ -82,7 +84,7 @@ fn chunk_rebuilder(
0.,
(Chunk::SIZE / 2) as f32 * 1.5,
)),
Collider::trimesh_with_flags(col_verts, col_indicies, TriMeshFlags::MERGE_DUPLICATE_VERTICES),
collider,
));
chunks.chunks[index] = chunk.id();
}

View File

@@ -1,6 +1,8 @@
use bevy::{asset::LoadState, log::tracing_subscriber::registry, pbr::ExtendedMaterial, prelude::*};
#[cfg(feature = "tracing")]
use bevy::log::*;
use bevy::{asset::LoadState, pbr::ExtendedMaterial, prelude::*};
use bevy_inspector_egui::quick::ResourceInspectorPlugin;
use bevy_rapier3d::geometry::{Collider, TriMeshFlags};
use bevy_rapier3d::geometry::Collider;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use world_generation::{
biome_painter::*,
@@ -20,7 +22,9 @@ use crate::{
utlis::render_distance_system::RenderDistanceVisibility,
};
use super::prelude::CurrentBiomePainter;
use super::{
chunk_rebuild::ChunkRebuildPlugin, prelude::CurrentBiomePainter, terraforming_test::TerraFormingTestPlugin,
};
pub struct MapInitPlugin;
@@ -29,6 +33,8 @@ impl Plugin for MapInitPlugin {
app.add_plugins((
ResourceInspectorPlugin::<PhosMap>::default(),
ResourceInspectorPlugin::<GenerationConfig>::default(),
ChunkRebuildPlugin,
TerraFormingTestPlugin,
));
app.add_systems(Startup, (load_textures, load_tiles));
@@ -212,11 +218,19 @@ fn spawn_map(
.chunks
.par_iter()
.map(|chunk: &Chunk| {
#[cfg(feature = "tracing")]
let _gen_mesh = info_span!("Generate Chunk").entered();
let mesh = generate_chunk_mesh(chunk, &heightmap, cur_painter, &tile_assets, &tile_mappers);
let collision = generate_chunk_collider(chunk, &heightmap);
let (col_verts, col_indicies) = generate_chunk_collider(chunk, &heightmap);
let collider: Collider;
{
#[cfg(feature = "tracing")]
let _collider_span = info_span!("Create Collider Trimesh").entered();
collider = Collider::trimesh(col_verts, col_indicies);
}
return (
mesh,
collision,
collider,
offset_to_world(chunk.chunk_offset * Chunk::SIZE as i32, 0.),
hex_utils::offset_to_index(chunk.chunk_offset, heightmap.width),
);
@@ -225,7 +239,16 @@ fn spawn_map(
let mut registry = PhosChunkRegistry::new(chunk_meshes.len());
for (mesh, (col_verts, col_indicies), pos, index) in chunk_meshes {
{
#[cfg(feature = "tracing")]
let _spawn_span = info_span!("Spawn Chunks").entered();
let visibility_offset = Vec3::new(
(Chunk::SIZE / 2) as f32 * SHORT_DIAGONAL,
0.,
(Chunk::SIZE / 2) as f32 * 1.5,
);
for (mesh, collider, pos, index) in chunk_meshes {
// let mesh_handle = meshes.a
let chunk = commands.spawn((
MaterialMeshBundle {
mesh: meshes.add(mesh),
@@ -234,15 +257,12 @@ fn spawn_map(
..default()
},
PhosChunk::new(index),
RenderDistanceVisibility::default().with_offset(Vec3::new(
(Chunk::SIZE / 2) as f32 * SHORT_DIAGONAL,
0.,
(Chunk::SIZE / 2) as f32 * 1.5,
)),
Collider::trimesh_with_flags(col_verts, col_indicies, TriMeshFlags::MERGE_DUPLICATE_VERTICES),
RenderDistanceVisibility::default().with_offset(visibility_offset),
collider,
));
registry.chunks.push(chunk.id());
}
}
commands.spawn((PbrBundle {
transform: Transform::from_translation(heightmap.get_center()),

View File

@@ -1,8 +1,8 @@
use bevy::prelude::*;
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_rapier3d::{pipeline::QueryFilter, plugin::RapierContext};
use world_generation::{hex_utils::HexCoord, prelude::Map};
use crate::camera_system::components::PhosCamera;
use crate::{camera_system::components::PhosCamera, prelude::PhosChunkRegistry};
use super::chunk_rebuild::ChunkRebuildQueue;
@@ -15,23 +15,52 @@ impl Plugin for TerraFormingTestPlugin {
}
fn deform(
cam: Query<&Transform, With<PhosCamera>>,
keyboard: Res<ButtonInput<KeyCode>>,
cam_query: Query<(&GlobalTransform, &Camera), With<PhosCamera>>,
window: Query<&Window, With<PrimaryWindow>>,
keyboard: Res<ButtonInput<MouseButton>>,
rapier_context: Res<RapierContext>,
mut heightmap: ResMut<Map>,
mut rebuild: ResMut<ChunkRebuildQueue>,
time: Res<Time>,
) {
if !keyboard.pressed(KeyCode::KeyF) {
if !keyboard.just_pressed(MouseButton::Left) {
return;
}
let cam_transform = cam.single();
let fwd: Vec3 = cam_transform.forward().into();
let win = window.single();
let (cam_transform, camera) = cam_query.single();
let Some(cursor_pos) = win.cursor_position() else {
return;
};
let collision = rapier_context.cast_ray(cam_transform.translation, fwd, 100., true, QueryFilter::only_fixed());
let Some(cam_ray) = camera.viewport_to_world(cam_transform, cursor_pos) else {
return;
};
if let Some((entity, dist)) = collision {
let contact_point = cam_transform.translation + (fwd * dist);
let collision = rapier_context.cast_ray(
cam_ray.origin,
cam_ray.direction.into(),
100.,
true,
QueryFilter::only_fixed(),
);
if let Some((_, dist)) = collision {
let contact_point = cam_ray.get_point(dist);
let contact_coord = HexCoord::from_world_pos(contact_point);
let cur_height = heightmap.sample_height(&contact_coord);
heightmap.set_height(&contact_coord, cur_height + 1.);
let cur_chunk = contact_coord.to_chunk_index(heightmap.width);
if contact_coord.is_on_chunk_edge() {
let neighbors = contact_coord.get_neighbors();
let mut other_chunks: Vec<_> = neighbors
.iter()
.map(|c| c.to_chunk_index(heightmap.width))
.filter(|c| c != &cur_chunk)
.collect();
rebuild.queue.append(&mut other_chunks);
}
rebuild.queue.push(cur_chunk);
}
}

View File

@@ -26,7 +26,6 @@ impl Plugin for PhosGamePlugin {
MapInitPlugin,
MaterialPlugin::<ExtendedMaterial<StandardMaterial, ChunkMaterial>>::default(),
RenderDistancePlugin,
ChunkRebuildPlugin,
));
//Systems - Startup