From 0e53810fbd38aaa6b81d85cd2684f50d755c64c4 Mon Sep 17 00:00:00 2001 From: Piotrek Date: Sun, 9 May 2021 18:25:44 +0200 Subject: [PATCH] writing node buffer in chunks to allow >48 size --- src/app/mod.rs | 30 ++++++++++++++--------------- src/renderer/buffers/content.rs | 34 +++++++++++++++++++++------------ 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/src/app/mod.rs b/src/app/mod.rs index 4711ee8..b5d4a9f 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -28,26 +28,26 @@ impl App { }; - // let timer = Instant::now(); - // for x in 0..16 { - // for z in 0..16 { - // let pos = &Point3{x,y:0,z}; - // let block = app.world.gen_block(pos); - // content.write(pos, block); - // } - // } - // println!("Generated in {}", timer.elapsed().as_secs_f32()); + let timer = Instant::now(); + for x in 0..32 { + for z in 0..32 { + let pos = &Point3{x,y:0,z}; + let block = app.world.gen_block(pos); + content.write(pos, block); + } + } + println!("Generated in {}", timer.elapsed().as_secs_f32()); // let timer = Instant::now(); // app.world.save(); // println!("Saved in {}", timer.elapsed().as_secs_f32()); - let timer = Instant::now(); - app.world.load(); - for (pos, block) in app.world.all_blocks() { - content.write(&pos, block); - } - println!("Loaded in {}", timer.elapsed().as_secs_f32()); + // let timer = Instant::now(); + // app.world.load(); + // for (pos, block) in app.world.all_blocks() { + // content.write(&pos, block); + // } + // println!("Loaded in {}", timer.elapsed().as_secs_f32()); println!("App initialized"); app diff --git a/src/renderer/buffers/content.rs b/src/renderer/buffers/content.rs index 9182be8..d4e94d7 100644 --- a/src/renderer/buffers/content.rs +++ b/src/renderer/buffers/content.rs @@ -1,3 +1,4 @@ +use std::time::Instant; use byteorder::{ByteOrder, LittleEndian}; use std::convert::TryInto; use cgmath::Point3; @@ -5,8 +6,8 @@ use crate::app::world::block::WorldBlock; pub const BLOCK_SIZE : usize = 32; // 32x32x32 block size pub const BLOCK_TEX_SIZE : usize = 64; // 64x64 blocks in texture -pub const NODE_TEX_SIZE: usize = 48; // 32x32x32 nodes in texture - +pub const NODE_TEX_SIZE: usize = 128; // 32x32x32 nodes in texture +const NODE_TEX_SIZE_SQ: usize = NODE_TEX_SIZE*NODE_TEX_SIZE; pub struct Content { node_buffer: Box<[u32]>, @@ -106,20 +107,29 @@ impl Content { ); } + fn write_nodes(&mut self, queue: &wgpu::Queue, z: usize) { + // Convert u32 node buffer to u8 + let node_origin = wgpu::Origin3d{ x:0, y: 0, z: z as u32 }; + let node_size = wgpu::Extent3d { width: NODE_TEX_SIZE as u32, height: NODE_TEX_SIZE as u32, depth: 1 }; + let mut node_bytes = [0_u8; NODE_TEX_SIZE_SQ*4]; + LittleEndian::write_u32_into(&self.node_buffer[NODE_TEX_SIZE_SQ*z..NODE_TEX_SIZE_SQ*(z+1)], &mut node_bytes); + + // Write nodes + queue.write_texture( + wgpu::TextureCopyView { texture: &self.node_texture, mip_level: 0, origin: node_origin }, &node_bytes, + wgpu::TextureDataLayout { offset: 0, bytes_per_row: 4*node_size.width, rows_per_image: node_size.height }, node_size + ); + } + pub fn update(&mut self, queue: &wgpu::Queue) { if self.node_dirty { self.node_dirty = false; - // 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 - ); + let timer = Instant::now(); + for z in 0..NODE_TEX_SIZE { + self.write_nodes(queue, z); + } + println!("Write nodes: {}", timer.elapsed().as_secs_f32()); } } } \ No newline at end of file