Skip to content

Commit

Permalink
Less iterating
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac8668 committed Jan 20, 2024
1 parent 953d0ee commit 602c913
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 25 deletions.
40 changes: 34 additions & 6 deletions src/chunk_group.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::prelude::*;
use async_channel::Sender;
use itertools::Itertools;

pub type ChunkCenter<'a> = Option<&'a mut [Atom; CHUNK_LEN]>;
Expand Down Expand Up @@ -228,14 +229,24 @@ pub fn local_to_global(pos: (IVec2, i32)) -> IVec2 {
IVec2::new(global_x, global_y)
}

pub fn get_chunk_groups<'a>(
//This function gets the chunk groups and spawns a scope to update them
pub fn update_chunk_groups<'a>(
chunks: &'a mut HashMap<IVec2, Chunk>,
(x_toff, y_toff): (i32, i32),
dirty_rects: &HashMap<IVec2, URect>,
dirty_rects: &'a HashMap<IVec2, URect>,
manager_pos: IVec2,
) -> Vec<ChunkGroup<'a>> {
dt: u8,
senders: (
&'a Sender<DeferredDirtyRectUpdate>,
&'a Sender<DeferredDirtyRectUpdate>,
),
materials: &'a Materials,
scope: &Scope<'a, '_, ()>,
) {
puffin::profile_function!();

let (dirty_update_rect_send, dirty_render_rect_send) = senders;

let mut chunk_groups = vec![];
let mut indices = HashMap::new();

Expand Down Expand Up @@ -356,9 +367,26 @@ pub fn get_chunk_groups<'a>(

for (chunk_pos, chunk) in chunks.iter_mut() {
if let Some(i) = indices.get(chunk_pos) {
chunk_groups[*i].center = Some(&mut chunk.atoms);
let i = *i;
let chunk_group;
unsafe {
chunk_group = chunk_groups.as_mut_ptr().add(i).as_mut().unwrap();
chunk_group.center = Some(&mut chunk.atoms);
}
let rect = dirty_rects.get(chunk_pos).unwrap();

scope.spawn(async move {
update_chunks(
&mut UpdateChunksType {
group: chunk_group,
dirty_update_rect_send,
dirty_render_rect_send,
materials,
},
dt,
rect,
)
});
}
}

chunk_groups
}
22 changes: 5 additions & 17 deletions src/chunk_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,28 +292,16 @@ pub fn chunk_manager_update(
puffin::profile_scope!("Update step scope.");

compute_pool.scope(|scope| {
let chunk_groups = get_chunk_groups(
update_chunk_groups(
&mut chunk_manager.chunks,
(x_toff, y_toff),
dirty_rects,
manager_pos,
dt,
(dirty_update_rect_send, dirty_render_rect_send),
materials,
scope,
);

for chunk_group in chunk_groups {
let rect = dirty_rects.get(&chunk_group.center_pos).unwrap();
scope.spawn(async move {
update_chunks(
&mut UpdateChunksType {
group: chunk_group,
dirty_update_rect_send,
dirty_render_rect_send,
materials,
},
dt,
rect,
)
});
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn main() {
ParticlesPlugin,
MaterialsPlugin,
CameraPlugin,
PuffinPlugin,
//PuffinPlugin,
))
.add_systems(Startup, setup);

Expand Down
2 changes: 1 addition & 1 deletion src/manager_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use async_channel::Sender;
use crate::prelude::*;

pub struct UpdateChunksType<'a> {
pub group: ChunkGroup<'a>,
pub group: &'a mut ChunkGroup<'a>,
pub dirty_update_rect_send: &'a Sender<DeferredDirtyRectUpdate>,
pub dirty_render_rect_send: &'a Sender<DeferredDirtyRectUpdate>,
pub materials: &'a Materials,
Expand Down

0 comments on commit 602c913

Please sign in to comment.