Skip to content

Commit

Permalink
Simplify scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
Game4all committed Apr 30, 2023
1 parent 762e10e commit 3173286
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 118 deletions.
72 changes: 11 additions & 61 deletions src/voxel/world/chunks.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use bevy::{
math::IVec3,
prelude::{
Changed, Commands, CoreSet, DetectChanges, Entity, GlobalTransform, IntoSystemConfig,
IntoSystemSetConfig, IntoSystemSetConfigs, Plugin, Query, Res, ResMut, Resource, SystemSet,
With,
Changed, Commands, CoreSet, Entity, GlobalTransform, IntoSystemConfig, IntoSystemConfigs,
IntoSystemSetConfig, Plugin, Query, Res, ResMut, Resource, SystemSet, With,
},
utils::{HashMap, HashSet},
};
use float_ord::FloatOrd;

use super::{
meshing::queue_mesh_tasks, player::PlayerController, terrain::process_terrain_gen, Chunk,
ChunkShape, CHUNK_LENGTH,
};
use super::{player::PlayerController, Chunk, ChunkShape, CHUNK_LENGTH};
use crate::voxel::storage::ChunkMap;
use crate::voxel::Voxel;

Expand All @@ -33,14 +29,6 @@ fn update_player_pos(
}
}

/// Run criteria for the [`update_view_chunks`] system
fn update_view_chunks_criteria(
chunk_pos: Res<CurrentLocalPlayerChunk>,
view_distance: Res<ChunkLoadRadius>,
) -> bool {
chunk_pos.is_changed() || view_distance.is_changed()
}

/// Checks for the loaded chunks around the player and schedules loading of new chunks in sight
fn update_view_chunks(
player_pos: Res<CurrentLocalPlayerChunk>,
Expand Down Expand Up @@ -128,22 +116,7 @@ fn clear_dirty_chunks(mut dirty_chunks: ResMut<DirtyChunks>) {

/// Label for the stage housing the chunk loading systems.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash, SystemSet)]
#[system_set(base)]
pub struct ChunkLoadingStage;

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash, SystemSet)]
/// Labels for the systems added by [`VoxelWorldChunkingPlugin`]
pub enum ChunkLoadingSystem {
/// Updates the player current chunk.
/// The computed position is used for loading / meshing priority systems.
UpdatePlayerPos,
/// Runs chunk view distance calculations and queue events for chunk creations and deletions.
UpdateViewChunks,
/// Creates the voxel buffers to hold chunk data and attach them a chunk entity in the ECS world.
CreateChunks,
/// Clears the dirty chunks list.
ClearDirtyChunks,
}
pub struct ChunkLoadingSet;

/// Handles dynamically loading / unloading regions (aka chunks) of the world according to camera position.
pub struct VoxelWorldChunkingPlugin;
Expand Down Expand Up @@ -238,36 +211,13 @@ impl Plugin for VoxelWorldChunkingPlugin {
})
.init_resource::<ChunkCommandQueue>()
.init_resource::<DirtyChunks>()
.configure_set(
ChunkLoadingStage
.after(CoreSet::Update)
.before(CoreSet::Last),
)
.configure_set(ChunkLoadingSystem::ClearDirtyChunks.in_base_set(CoreSet::Last))
.configure_sets(
(
ChunkLoadingSystem::UpdatePlayerPos,
ChunkLoadingSystem::UpdateViewChunks,
ChunkLoadingSystem::CreateChunks,
)
.in_base_set(ChunkLoadingStage),
.configure_set(ChunkLoadingSet.in_base_set(CoreSet::Update))
.add_systems(
(update_player_pos, update_view_chunks, create_chunks)
.chain()
.in_set(ChunkLoadingSet),
)
.add_systems((
update_player_pos.in_set(ChunkLoadingSystem::UpdatePlayerPos),
update_view_chunks
.in_set(ChunkLoadingSystem::UpdateViewChunks)
.run_if(update_view_chunks_criteria)
.before(create_chunks)
.before(update_player_pos),
create_chunks
.in_set(ChunkLoadingSystem::CreateChunks)
.before(queue_mesh_tasks),
destroy_chunks
.in_base_set(CoreSet::Last)
.after(process_terrain_gen),
clear_dirty_chunks
.in_set(ChunkLoadingSystem::ClearDirtyChunks)
.before(process_terrain_gen),
));
.add_system(destroy_chunks.in_base_set(CoreSet::PostUpdate))
.add_system(clear_dirty_chunks.in_base_set(CoreSet::Last));
}
}
45 changes: 15 additions & 30 deletions src/voxel/world/meshing.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::cell::RefCell;

use super::{
chunks::{ChunkEntities, ChunkLoadingStage, DirtyChunks},
chunks::{ChunkEntities, ChunkLoadingSet, DirtyChunks},
terrain::TerrainGenSet,
Chunk, ChunkShape, Voxel, CHUNK_LENGTH,
};
use crate::voxel::{
Expand Down Expand Up @@ -39,7 +40,7 @@ static SHARED_MESH_BUFFERS: Lazy<ThreadLocal<RefCell<MeshBuffers<Voxel, ChunkSha
Lazy::new(ThreadLocal::default);

/// Queues meshing tasks for the chunks in need of a remesh.
pub fn queue_mesh_tasks(
fn queue_mesh_tasks(
mut commands: Commands,
dirty_chunks: Res<DirtyChunks>,
chunk_entities: Res<ChunkEntities>,
Expand Down Expand Up @@ -100,42 +101,26 @@ fn process_mesh_tasks(
});
}

/// A stage existing solely for enabling the use of change detection.
/// The set of systems which asynchronusly mesh the chunks.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash, SystemSet)]
pub struct ChunkMeshingPrepareStage;

// /// Label for the stage housing the chunk meshing systems.
// #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash, SystemSet)]
// pub struct ChunkMeshingStage;

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash, SystemSet)]
pub enum ChunkRenderingSystem {
/// Queues meshing tasks for the chunks in need of a remesh.
QueueMeshTasks,

/// Polls and process the generated chunk meshes.
ProcessMeshTasks,
}
pub struct ChunkMeshingSet;

/// Handles the meshing of the chunks.
pub struct VoxelWorldMeshingPlugin;

impl Plugin for VoxelWorldMeshingPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.configure_sets(
(
ChunkMeshingPrepareStage,
ChunkRenderingSystem::QueueMeshTasks,
ChunkRenderingSystem::ProcessMeshTasks,
)
.chain()
.in_base_set(ChunkLoadingStage),
app.configure_set(
ChunkMeshingSet
.in_base_set(CoreSet::Update)
.after(TerrainGenSet)
.after(ChunkLoadingSet),
)
.add_system(prepare_chunks.in_set(ChunkMeshingPrepareStage))
.add_systems((
queue_mesh_tasks.in_set(ChunkRenderingSystem::QueueMeshTasks),
process_mesh_tasks.in_set(ChunkRenderingSystem::ProcessMeshTasks),
));
.add_systems(
(prepare_chunks, queue_mesh_tasks, process_mesh_tasks)
.chain()
.in_set(ChunkMeshingSet),
);
}
}

Expand Down
41 changes: 14 additions & 27 deletions src/voxel/world/terrain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
chunks::{ChunkLoadingStage, DirtyChunks},
chunks::{ChunkLoadingSet, DirtyChunks},
Chunk, ChunkShape,
};
use crate::voxel::{
Expand All @@ -9,8 +9,8 @@ use crate::voxel::{
};
use bevy::{
prelude::{
Added, Commands, Component, Entity, IntoSystemConfig, IntoSystemSetConfig,
IntoSystemSetConfigs, Plugin, Query, ResMut, SystemSet,
Added, Commands, Component, CoreSet, Entity, IntoSystemConfigs, IntoSystemSetConfig,
Plugin, Query, ResMut, SystemSet,
},
tasks::{AsyncComputeTaskPool, Task},
};
Expand Down Expand Up @@ -61,35 +61,22 @@ pub fn process_terrain_gen(
/// Handles terrain generation.
pub struct VoxelWorldTerrainGenPlugin;

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash, SystemSet)]
/// Labels for the systems added by [`VoxelWorldTerrainGenPlugin`]
pub enum TerrainGenSystem {
/// Queues the terrain gen async tasks for the newly created chunks.
QueueTerrainGen,
/// Polls for finished gen tasks and put back the generated terrain into the voxel map
ProcessTerrainGen,
}

// we need to use a whole system stage for this in order to enable the usage of added component querries.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug, Hash, SystemSet)]
#[system_set(base)]
struct TerrainGenStage;
pub struct TerrainGenSet;

impl Plugin for VoxelWorldTerrainGenPlugin {
fn build(&self, app: &mut bevy::prelude::App) {
app.configure_set(TerrainGenStage.after(ChunkLoadingStage))
.configure_sets(
(
TerrainGenSystem::QueueTerrainGen,
TerrainGenSystem::ProcessTerrainGen,
)
.chain()
.in_base_set(TerrainGenStage),
)
.add_systems((
queue_terrain_gen.in_set(TerrainGenSystem::QueueTerrainGen),
process_terrain_gen.in_set(TerrainGenSystem::ProcessTerrainGen),
));
app.configure_set(
TerrainGenSet
.in_base_set(CoreSet::Update)
.after(ChunkLoadingSet),
)
.add_systems(
(queue_terrain_gen, process_terrain_gen)
.chain()
.in_set(TerrainGenSet),
);
}
}

Expand Down

0 comments on commit 3173286

Please sign in to comment.