Fix incorrect brickmap unloading (I think)

This commit is contained in:
Jarrod Doyle 2024-03-23 15:19:11 +00:00
parent e6d73686b4
commit 5acea1192c
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 30 additions and 24 deletions

View File

@ -6,7 +6,10 @@ use crate::{
voxel::world::WorldManager, voxel::world::WorldManager,
}; };
use super::{brickmap_cache::BrickmapCache, shading_table::ShadingTableAllocator}; use super::{
brickmap_cache::{BrickmapCache, BrickmapCacheEntry},
shading_table::ShadingTableAllocator,
};
#[repr(C)] #[repr(C)]
#[derive(Debug, Default, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] #[derive(Debug, Default, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
@ -207,7 +210,16 @@ impl BrickmapManager {
// If there's no voxel colour data post-culling it means the brickmap is // If there's no voxel colour data post-culling it means the brickmap is
// empty. We don't need to upload it, just mark the relevant brickgrid entry. // empty. We don't need to upload it, just mark the relevant brickgrid entry.
if albedo_data.is_empty() { if albedo_data.is_empty() {
self.update_brickgrid_element(grid_idx, 0); if let Some(entry) = self.update_brickgrid_element(grid_idx, 0) {
// The brickgrid element had a brickmap entry so we need to unload it's
// shading data
if let Err(e) = self
.shading_table_allocator
.try_dealloc(entry.shading_table_offset)
{
log::warn!("{}", e)
}
}
return; return;
} }
@ -218,20 +230,26 @@ impl BrickmapManager {
.unwrap() as usize; .unwrap() as usize;
if let Some(entry) = self.brickmap_cache.add_entry(grid_idx, shading_idx as u32) { if let Some(entry) = self.brickmap_cache.add_entry(grid_idx, shading_idx as u32) {
// TODO: Currently this is broken!!
// We removed the entry, then when updating the brickgrid we go and remove the NEW entry!!!
// Need to think about this
self.update_brickgrid_element(entry.grid_idx, 1); self.update_brickgrid_element(entry.grid_idx, 1);
} }
// Update the brickgrid index // Update the brickgrid index
self.update_brickgrid_element( if let Some(old_entry) = self.update_brickgrid_element(
grid_idx, grid_idx,
super::util::to_brickgrid_element( super::util::to_brickgrid_element(
self.brickmap_cache.index as u32, self.brickmap_cache.index as u32,
BrickgridFlag::Loaded, BrickgridFlag::Loaded,
), ),
); ) {
// The brickgrid element had a brickmap entry so we need to unload it's
// shading data
if let Err(e) = self
.shading_table_allocator
.try_dealloc(old_entry.shading_table_offset)
{
log::warn!("{}", e)
}
}
// Update the brickmap // Update the brickmap
let brickmap = Brickmap { let brickmap = Brickmap {
@ -253,29 +271,17 @@ impl BrickmapManager {
self.brickmap_staged.push(staged_brickmap); self.brickmap_staged.push(staged_brickmap);
} }
fn update_brickgrid_element(&mut self, index: usize, data: u32) { fn update_brickgrid_element(&mut self, index: usize, data: u32) -> Option<BrickmapCacheEntry> {
// If we're updating a brickgrid element, we need to make sure to deallocate anything let mut brickmap_cache_entry = None;
// that's already there. The shading table gets deallocated, and the brickmap cache entry
// is marked as None.
if (self.brickgrid[index] & 0xF) == 4 { if (self.brickgrid[index] & 0xF) == 4 {
let brickmap_idx = (self.brickgrid[index] >> 8) as usize; let cache_index = (self.brickgrid[index] >> 8) as usize;
match self.brickmap_cache.remove_entry(brickmap_idx) { brickmap_cache_entry = self.brickmap_cache.get_entry(cache_index);
Some(entry) => {
match self
.shading_table_allocator
.try_dealloc(entry.shading_table_offset)
{
Ok(_) => (),
Err(e) => log::warn!("{}", e),
}
}
None => log::warn!("Expected brickmap cache entry, found None!"),
}
} }
// We're safe to overwrite the CPU brickgrid and mark for GPU upload now // We're safe to overwrite the CPU brickgrid and mark for GPU upload now
self.brickgrid[index] = data; self.brickgrid[index] = data;
self.brickgrid_staged.insert(index); self.brickgrid_staged.insert(index);
brickmap_cache_entry
} }
// TODO: Tidy this up more // TODO: Tidy this up more