writing node buffer in chunks to allow >48 size

This commit is contained in:
Piotrek
2021-05-09 18:25:44 +02:00
parent 466f98a42c
commit 0e53810fbd
2 changed files with 37 additions and 27 deletions
+22 -12
View File
@@ -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());
}
}
}