diff --git a/src/renderer/buffers/content.rs b/src/renderer/buffers/content.rs index 032fd52..9182be8 100644 --- a/src/renderer/buffers/content.rs +++ b/src/renderer/buffers/content.rs @@ -11,6 +11,7 @@ pub const NODE_TEX_SIZE: usize = 48; // 32x32x32 nodes in texture pub struct Content { node_buffer: Box<[u32]>, node_texture: wgpu::Texture, + node_dirty: bool, block_freeidx: usize, block_texture: wgpu::Texture, pub bind_layout: wgpu::BindGroupLayout, @@ -68,7 +69,7 @@ impl Content { // Done 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)] @@ -84,9 +85,10 @@ impl Content { self.node_buffer[index] = value as u32; self.block_freeidx = value; //println!("Alloc: {}", value); - } - //println!("Index: {} Value: {}", index, value); + // Mark node buffer as dirty + self.node_dirty = true; + } // Calculate position in block texture let y = ((value-1) % BLOCK_TEX_SIZE * BLOCK_SIZE) as u32; @@ -102,16 +104,22 @@ impl Content { 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 ); + } - // 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 mut node_bytes = [0_u8; NODE_TEX_SIZE*NODE_TEX_SIZE*NODE_TEX_SIZE*4]; - LittleEndian::write_u32_into(&self.node_buffer, &mut node_bytes); + pub fn update(&mut self, queue: &wgpu::Queue) { + if self.node_dirty { + self.node_dirty = false; - // Write nodes (TODO: do it max once per frame, and only if smth changed) - queue.write_texture( - wgpu::TextureCopyView { texture: &self.node_texture, mip_level: 0, origin: wgpu::Origin3d::ZERO }, &node_bytes, - wgpu::TextureDataLayout { offset: 0, bytes_per_row: 4*node_size.width, rows_per_image: node_size.height }, node_size - ); + // 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 mut node_bytes = [0_u8; NODE_TEX_SIZE*NODE_TEX_SIZE*NODE_TEX_SIZE*4]; + LittleEndian::write_u32_into(&self.node_buffer, &mut node_bytes); + + // Write nodes (TODO: do it max once per frame, and only if smth changed) + queue.write_texture( + wgpu::TextureCopyView { texture: &self.node_texture, mip_level: 0, origin: wgpu::Origin3d::ZERO }, &node_bytes, + wgpu::TextureDataLayout { offset: 0, bytes_per_row: 4*node_size.width, rows_per_image: node_size.height }, node_size + ); + } } } \ No newline at end of file diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 13e473c..4b8a37f 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -117,6 +117,9 @@ impl Renderer { 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])); + // Update content + self.buffers.content.update(&self.queue); + // Get next frame to render to let frame = self.swapchain.get_current_frame()?.output;