updating node buffer max once per frame

This commit is contained in:
Piotrek
2021-05-09 18:15:30 +02:00
parent bf14d7c578
commit 466f98a42c
2 changed files with 23 additions and 12 deletions
+11 -3
View File
@@ -11,6 +11,7 @@ pub const NODE_TEX_SIZE: usize = 48; // 32x32x32 nodes in texture
pub struct Content { pub struct Content {
node_buffer: Box<[u32]>, node_buffer: Box<[u32]>,
node_texture: wgpu::Texture, node_texture: wgpu::Texture,
node_dirty: bool,
block_freeidx: usize, block_freeidx: usize,
block_texture: wgpu::Texture, block_texture: wgpu::Texture,
pub bind_layout: wgpu::BindGroupLayout, pub bind_layout: wgpu::BindGroupLayout,
@@ -68,7 +69,7 @@ impl Content {
// Done // Done
let block_freeidx = 0; let block_freeidx = 0;
Self { node_buffer, node_texture, block_freeidx, block_texture, bind_layout, bind_group } Self { node_buffer, node_dirty: false, node_texture, block_freeidx, block_texture, bind_layout, bind_group }
} }
#[allow(unused)] #[allow(unused)]
@@ -84,9 +85,10 @@ impl Content {
self.node_buffer[index] = value as u32; self.node_buffer[index] = value as u32;
self.block_freeidx = value; self.block_freeidx = value;
//println!("Alloc: {}", value); //println!("Alloc: {}", value);
}
//println!("Index: {} Value: {}", index, value); // Mark node buffer as dirty
self.node_dirty = true;
}
// Calculate position in block texture // Calculate position in block texture
let y = ((value-1) % BLOCK_TEX_SIZE * BLOCK_SIZE) as u32; let y = ((value-1) % BLOCK_TEX_SIZE * BLOCK_SIZE) as u32;
@@ -102,6 +104,11 @@ impl Content {
wgpu::TextureCopyView { texture: &self.block_texture, mip_level: 0, origin: block_origin }, &block_bytes, wgpu::TextureCopyView { texture: &self.block_texture, mip_level: 0, origin: block_origin }, &block_bytes,
wgpu::TextureDataLayout { offset: 0, bytes_per_row: block_size.width, rows_per_image: block_size.height }, block_size wgpu::TextureDataLayout { offset: 0, bytes_per_row: block_size.width, rows_per_image: block_size.height }, block_size
); );
}
pub fn update(&mut self, queue: &wgpu::Queue) {
if self.node_dirty {
self.node_dirty = false;
// Convert u32 node buffer to u8 // Convert u32 node buffer to u8
let node_size = wgpu::Extent3d { width: NODE_TEX_SIZE as u32, height: NODE_TEX_SIZE as u32, depth: NODE_TEX_SIZE as u32 }; let node_size = wgpu::Extent3d { width: NODE_TEX_SIZE as u32, height: NODE_TEX_SIZE as u32, depth: NODE_TEX_SIZE as u32 };
@@ -114,4 +121,5 @@ impl Content {
wgpu::TextureDataLayout { offset: 0, bytes_per_row: 4*node_size.width, rows_per_image: node_size.height }, node_size wgpu::TextureDataLayout { offset: 0, bytes_per_row: 4*node_size.width, rows_per_image: node_size.height }, node_size
); );
} }
}
} }
+3
View File
@@ -117,6 +117,9 @@ impl Renderer {
self.buffers.uniforms.values.update(&self.camera, self.start.elapsed().as_secs_f32()); self.buffers.uniforms.values.update(&self.camera, self.start.elapsed().as_secs_f32());
self.queue.write_buffer(&self.buffers.uniforms.buffer, 0, bytemuck::cast_slice(&[self.buffers.uniforms.values])); self.queue.write_buffer(&self.buffers.uniforms.buffer, 0, bytemuck::cast_slice(&[self.buffers.uniforms.values]));
// Update content
self.buffers.content.update(&self.queue);
// Get next frame to render to // Get next frame to render to
let frame = self.swapchain.get_current_frame()?.output; let frame = self.swapchain.get_current_frame()?.output;