Skip to content

Commit

Permalink
Switch to IPQ
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshiew committed Dec 28, 2024
1 parent 22c0a08 commit 7683589
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 20 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/plugins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ tracing = { workspace = true }
ahash = { workspace = true }
bevy_framepace = "0.18.0"
linearize = { workspace = true }
indexed_priority_queue = "0.3.0"
2 changes: 1 addition & 1 deletion crates/plugins/src/scene/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn process_load_chunk_ops(
) {
let scene_zoom_level = scene_zoom.zoom_level.into();
for _ in 0..CHUNK_OP_RATE {
let Some(cpos) = load_ops.pop_front() else {
let Some(cpos) = load_ops.pop() else {
return;
};

Expand Down
23 changes: 16 additions & 7 deletions crates/plugins/src/scene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy::prelude::*;
use infinigen_common::chunks::CHUNK_SIZE_F32;
use infinigen_common::view;
use infinigen_common::world::{ChunkPosition, WorldPosition};
use utils::Queue;
use utils::{ChunkPriority, LoadQueue};

use crate::settings::{Config, DEFAULT_HORIZONTAL_VIEW_DISTANCE, DEFAULT_VERTICAL_VIEW_DISTANCE};
use crate::AppState;
Expand All @@ -19,7 +19,7 @@ pub struct LoadedChunk {
pub cpos: ChunkPosition,
}

pub type LoadChunkRequests = Queue<ChunkPosition>;
pub type LoadChunkRequests = LoadQueue;

#[derive(Debug, Default, Resource)]
pub struct SceneCamera {
Expand Down Expand Up @@ -139,20 +139,24 @@ pub fn handle_update_scene_view(
#[derive(Event)]
pub struct UpdateSceneEvent;

#[allow(clippy::too_many_arguments)]
pub fn check_if_should_update_scene(
mut commands: Commands,
mut scene_camera: ResMut<SceneCamera>,
camera: Single<&Transform, With<Camera>>,
mut load_ops: ResMut<LoadChunkRequests>,
mut reload_evs: EventReader<ReloadAllChunksEvent>,
mut refresh_evs: EventReader<RefreshChunkOpsQueueEvent>,
mut update_scene_evs: EventWriter<UpdateSceneEvent>,
loaded: Query<Entity, With<LoadedChunk>>,
) {
let mut should_update = false;
if refresh_evs.read().next().is_some() {
load_ops.clear();
should_update = true;
}
if reload_evs.read().next().is_some() {
load_ops.clear();
tracing::info!("Reloading all chunks");
for loaded_chunk in loaded.iter() {
commands.entity(loaded_chunk).despawn_recursive();
Expand Down Expand Up @@ -191,7 +195,6 @@ pub fn update_scene(
return;
}
tracing::trace!("Updating scene");
load_ops.clear();

let (camera, projection) = camera.single();
let current_cpos: ChunkPosition = WorldPosition {
Expand All @@ -210,7 +213,8 @@ pub fn update_scene(
let near = projection.near;
let far = projection.far;

let already_loaded_or_loading: AHashSet<_> = loaded.iter().map(|l| l.cpos).collect();
let already_loaded: AHashSet<_> = loaded.iter().map(|l| l.cpos).collect();
tracing::debug!(loaded = ?already_loaded.len(), "Chunks already loaded");

let (to_load, to_unload) = view::compute_chunks_delta(
current_cpos,
Expand All @@ -231,11 +235,16 @@ pub fn update_scene(
fov,
near,
far,
&already_loaded_or_loading,
&already_loaded,
);
tracing::debug!(load = ?to_load.len(), unload = ?to_unload.len(), "Chunks to load/unload");

for cpos in to_load {
load_ops.push_back(cpos);
for (i, cpos) in to_load.into_iter().enumerate() {
let priority = ChunkPriority { priority: i };
load_ops.push(cpos, priority);
}
for cpos in to_unload.iter() {
load_ops.remove(*cpos);
}

unload_evs.send_batch(to_unload.into_iter().map(UnloadChunkOpEvent));
Expand Down
48 changes: 36 additions & 12 deletions crates/plugins/src/scene/utils.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
//! Converts from our native mesh types to Bevy meshes
use std::collections::VecDeque;
use std::cmp::Ordering;

use bevy::prelude::*;
use bevy::render::mesh::{Indices, VertexAttributeValues};
use bevy::render::render_asset::RenderAssetUsages;
use bevy::render::render_resource::PrimitiveTopology;
use indexed_priority_queue::DefaultMapIPQ;
use infinigen_common::chunks::Array3Chunk;
use infinigen_common::mesh::faces::{prepare_padded_chunk, BlockVisibilityChecker};
use infinigen_common::mesh::shapes::ChunkFace;
use infinigen_common::mesh::textures::BlockAppearances;
use infinigen_common::mesh::{mesh_chunk_greedy_quads, mesh_chunk_visible_block_faces, MeshInfo};
use infinigen_common::world::Direction;
use infinigen_common::world::{ChunkPosition, Direction};
use linearize::StaticCopyMap;

pub fn to_bevy_mesh(
Expand Down Expand Up @@ -70,25 +71,48 @@ pub fn bevy_mesh_greedy_quads(
mesh.map(to_bevy_mesh)
}

#[derive(Resource, Default)]
pub struct Queue<T> {
inner: VecDeque<T>,
#[derive(Default, Eq, PartialEq, Clone, Copy)]
pub struct ChunkPriority {
pub priority: usize,
}

impl<T> Queue<T> {
pub fn len(&self) -> usize {
self.inner.len()
impl PartialOrd for ChunkPriority {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for ChunkPriority {
fn cmp(&self, other: &Self) -> Ordering {
self.priority.partial_cmp(&other.priority).unwrap()
}
}

#[derive(Default, Resource)]
pub struct LoadQueue {
inner: DefaultMapIPQ<ChunkPosition, ChunkPriority>,
}

pub fn push_back(&mut self, item: T) {
self.inner.push_back(item);
impl LoadQueue {
pub fn push(&mut self, pos: ChunkPosition, priority: ChunkPriority) {
self.inner.push(pos, priority);
}

pub fn pop_front(&mut self) -> Option<T> {
self.inner.pop_front()
pub fn remove(&mut self, pos: ChunkPosition) {
if self.inner.contains(pos) {
self.inner.remove(pos);
}
}

pub fn clear(&mut self) {
self.inner.clear();
}

pub fn pop(&mut self) -> Option<ChunkPosition> {
self.inner.pop()
}

pub fn len(&self) -> usize {
self.inner.len()
}
}

0 comments on commit 7683589

Please sign in to comment.