Compare commits

..

2 Commits

Author SHA1 Message Date
Jarrod Doyle a6d89fb2e5
Remove unused code 2024-03-25 16:32:23 +00:00
Jarrod Doyle 0200d876b1
Tidy up brickmap request handling 2024-03-25 16:29:58 +00:00
3 changed files with 59 additions and 78 deletions

View File

@ -1,9 +1,6 @@
use std::collections::HashSet; use std::collections::HashSet;
use crate::{ use crate::gfx::{BulkBufferBuilder, Context};
gfx::{BulkBufferBuilder, Context},
math,
};
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum BrickgridFlag { pub enum BrickgridFlag {
@ -78,9 +75,15 @@ impl Brickgrid {
} }
} }
/// Panics if position maps to out of range index pub fn get_buffer(&self) -> &wgpu::Buffer {
// pub fn set(&mut self, pos: glam::UVec3, value: BrickgridElement) -> BrickgridElement { &self.buffer
// let index = math::to_1d_index(pos, self.dimensions); }
pub fn get_upload_buffer(&self) -> &wgpu::Buffer {
&self.upload_buffer
}
/// Panics if index out of range
pub fn set(&mut self, index: usize, value: BrickgridElement) -> BrickgridElement { pub fn set(&mut self, index: usize, value: BrickgridElement) -> BrickgridElement {
let current = self.data[index]; let current = self.data[index];
self.data[index] = value; self.data[index] = value;
@ -88,9 +91,7 @@ impl Brickgrid {
current current
} }
/// Panics if position maps to out of range index /// Panics if index out of range
// pub fn get(&mut self, pos: glam::UVec3) -> BrickgridElement {
// let index = math::to_1d_index(pos, self.dimensions);
pub fn get(&mut self, index: usize) -> BrickgridElement { pub fn get(&mut self, index: usize) -> BrickgridElement {
self.data[index] self.data[index]
} }
@ -128,12 +129,4 @@ impl Brickgrid {
); );
} }
} }
pub fn get_buffer(&self) -> &wgpu::Buffer {
&self.buffer
}
pub fn get_upload_buffer(&self) -> &wgpu::Buffer {
&self.upload_buffer
}
} }

View File

@ -6,7 +6,7 @@ use crate::{
use super::{ use super::{
brickgrid::{Brickgrid, BrickgridElement, BrickgridFlag}, brickgrid::{Brickgrid, BrickgridElement, BrickgridFlag},
brickmap_cache::{BrickmapCache, BrickmapCacheEntry}, brickmap_cache::BrickmapCache,
shading_table::ShadingTableAllocator, shading_table::ShadingTableAllocator,
}; };
@ -166,13 +166,53 @@ impl BrickmapManager {
let grid_pos = grid_pos.as_ivec3(); let grid_pos = grid_pos.as_ivec3();
let (bitmask_data, albedo_data) = super::util::cull_interior_voxels(world, grid_pos); let (bitmask_data, albedo_data) = super::util::cull_interior_voxels(world, grid_pos);
// If there's no voxel colour data post-culling it means the brickmap is let mut brickgrid_element = BrickgridElement::default();
// empty. We don't need to upload it, just mark the relevant brickgrid entry.
if albedo_data.is_empty() { // We have voxel data so we have a brickmap to upload
if let Some(entry) = self.update_brickgrid_element(grid_idx, 0) { if !albedo_data.is_empty() {
// TODO: We need to actually remove the cache entry lmao let shading_idx = self
// The brickgrid element had a brickmap entry so we need to unload it's .shading_table_allocator
// shading data .try_alloc(albedo_data.len() as u32)
.unwrap() as usize;
if let Some(entry) = self.brickmap_cache.add_entry(
grid_idx,
shading_idx as u32,
bitmask_data,
albedo_data,
) {
// An entry got removed so we need to deallocate it's shading table elements
// and mark the relevant brickgrid as unloaded
if let Err(e) = self
.shading_table_allocator
.try_dealloc(entry.shading_table_offset)
{
log::warn!("{}", e)
}
self.brickgrid.set(
entry.grid_idx,
BrickgridElement::new(0, BrickgridFlag::Unloaded),
);
}
brickgrid_element =
BrickgridElement::new(self.brickmap_cache.index, BrickgridFlag::Loaded);
}
let old = self.brickgrid.set(grid_idx, brickgrid_element);
if old.get_flag() == BrickgridFlag::Loaded {
// The brickgrid element was previously loaded so we need to unload any of
// the data that was associated with it
if let Some(entry) = self.brickmap_cache.remove_entry(old.get_pointer()) {
if entry.grid_idx != grid_idx {
log::error!(
"Mismatch between brickgrid index and brickmap grid index: {} vs {}",
grid_idx,
entry.grid_idx
);
}
// We need to deallocate the removed entries shading table elements
if let Err(e) = self if let Err(e) = self
.shading_table_allocator .shading_table_allocator
.try_dealloc(entry.shading_table_offset) .try_dealloc(entry.shading_table_offset)
@ -180,53 +220,7 @@ impl BrickmapManager {
log::warn!("{}", e) log::warn!("{}", e)
} }
} }
return;
} }
// Update the shading table
let shading_idx = self
.shading_table_allocator
.try_alloc(albedo_data.len() as u32)
.unwrap() as usize;
if let Some(entry) =
self.brickmap_cache
.add_entry(grid_idx, shading_idx as u32, bitmask_data, albedo_data)
{
self.update_brickgrid_element(entry.grid_idx, 1);
}
// Update the brickgrid index
if let Some(old_entry) = self.update_brickgrid_element(
grid_idx,
super::util::to_brickgrid_element(
self.brickmap_cache.index as u32,
BrickgridFlag::Loaded,
),
) {
// TODO: We need to actually remove the cache entry lmao
// 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)
}
}
}
fn update_brickgrid_element(&mut self, index: usize, data: u32) -> Option<BrickmapCacheEntry> {
let mut brickmap_cache_entry = None;
let current = self.brickgrid.get(index);
if current.get_flag() == BrickgridFlag::Loaded {
let cache_index = current.get_pointer();
brickmap_cache_entry = self.brickmap_cache.get_entry(cache_index);
}
// We're safe to overwrite the CPU brickgrid and mark for GPU upload now
self.brickgrid.set(index, BrickgridElement(data));
brickmap_cache_entry
} }
fn upload_unpack_buffers(&mut self, context: &gfx::Context) { fn upload_unpack_buffers(&mut self, context: &gfx::Context) {

View File

@ -1,7 +1,5 @@
use crate::voxel::world::{Voxel, WorldManager}; use crate::voxel::world::{Voxel, WorldManager};
use super::brickgrid::BrickgridFlag;
pub fn cull_interior_voxels( pub fn cull_interior_voxels(
world: &mut WorldManager, world: &mut WorldManager,
grid_pos: glam::IVec3, grid_pos: glam::IVec3,
@ -103,10 +101,6 @@ pub fn cull_interior_voxels(
(bitmask_data, albedo_data) (bitmask_data, albedo_data)
} }
pub fn to_brickgrid_element(brickmap_cache_idx: u32, flags: BrickgridFlag) -> u32 {
(brickmap_cache_idx << 8) + flags as u32
}
pub fn grid_pos_to_world_pos( pub fn grid_pos_to_world_pos(
world: &mut WorldManager, world: &mut WorldManager,
grid_pos: glam::IVec3, grid_pos: glam::IVec3,