Extract handling of requested brickmap to method

This commit is contained in:
Jarrod Doyle 2024-03-23 11:20:31 +00:00
parent ba973a7306
commit 23ea18c3de
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 75 additions and 72 deletions

View File

@ -178,25 +178,35 @@ impl BrickmapManager {
} }
pub fn process_feedback_buffer(&mut self, context: &gfx::Context, world: &mut WorldManager) { pub fn process_feedback_buffer(&mut self, context: &gfx::Context, world: &mut WorldManager) {
// Get request count let data: Vec<u32> = self.feedback_result_buffer.get_mapped_range(context, 0..16);
let mut data: Vec<u32> = self.feedback_result_buffer.get_mapped_range(context, 0..16);
let request_count = data[1] as usize; let request_count = data[1] as usize;
// Getting the position data and resetting the request count is costly, so we
// only do it if there were actually any requests
if request_count > 0 { if request_count > 0 {
let range = 16..(16 + 16 * request_count as u64); // Reset the request count for next frame
data = self.feedback_result_buffer.get_mapped_range(context, range);
context context
.queue .queue
.write_buffer(&self.feedback_buffer, 4, &[0, 0, 0, 0]); .write_buffer(&self.feedback_buffer, 4, &[0, 0, 0, 0]);
let range = 16..(16 + 16 * request_count as u64);
let data = self.feedback_result_buffer.get_mapped_range(context, range);
for i in 0..request_count {
let request_data = &data[(i * 4)..(i * 4 + 3)];
self.handle_request(world, request_data);
}
} }
// Generate a sphere of voxels // TODO: Why do we call this here rather than doing it outside of here?
self.upload_unpack_buffers(context);
// TODO: This is inaccurate if we've looped
log::info!("Num loaded brickmaps: {}", self.brickmap_cache_idx);
}
fn handle_request(&mut self, world: &mut WorldManager, data: &[u32]) {
let grid_dims = self.state_uniform.brickgrid_dims; let grid_dims = self.state_uniform.brickgrid_dims;
for i in 0..request_count {
// Extract brickgrid position of the requested brickmap // Extract brickgrid position of the requested brickmap
let grid_pos = glam::uvec3(data[i * 4], data[i * 4 + 1], data[i * 4 + 2]); let grid_pos = glam::uvec3(data[0], data[1], data[2]);
let grid_idx = math::to_1d_index( let grid_idx = math::to_1d_index(
grid_pos, grid_pos,
glam::uvec3(grid_dims[0], grid_dims[1], grid_dims[2]), glam::uvec3(grid_dims[0], grid_dims[1], grid_dims[2]),
@ -211,7 +221,7 @@ impl BrickmapManager {
// 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); self.update_brickgrid_element(grid_idx, 0);
continue; return;
} }
// Update the brickgrid index // Update the brickgrid index
@ -260,13 +270,6 @@ impl BrickmapManager {
self.brickmap_cache_idx = (self.brickmap_cache_idx + 1) % self.brickmap_cache_map.len(); self.brickmap_cache_idx = (self.brickmap_cache_idx + 1) % self.brickmap_cache_map.len();
} }
// TODO: Why do we call this here rather than doing it outside of here?
self.upload_unpack_buffers(context);
// TODO: This is inaccurate if we've looped
log::info!("Num loaded brickmaps: {}", self.brickmap_cache_idx);
}
fn update_brickgrid_element(&mut self, index: usize, data: u32) { fn update_brickgrid_element(&mut self, index: usize, data: u32) {
// If we're updating a brickgrid element, we need to make sure to deallocate anything // If we're updating a brickgrid element, we need to make sure to deallocate anything
// that's already there. The shading table gets deallocated, and the brickmap cache entry // that's already there. The shading table gets deallocated, and the brickmap cache entry