bug fixes, faster node texture shift, loading +z not working (0,0,2 chunk loading only one line)

This commit is contained in:
Piotrek
2021-05-15 17:07:20 +02:00
parent 00c518b9c1
commit 16edbfea42
5 changed files with 59 additions and 54 deletions
+18 -21
View File
@@ -7,6 +7,7 @@ use winit::event::Event;
use player::Player; use player::Player;
use world::World; use world::World;
const RENDER_DIST: i32 = 1;
pub enum GameState { pub enum GameState {
Paused, Paused,
@@ -34,9 +35,9 @@ impl Game {
}; };
// Preload chunks around player // Preload chunks around player
for x in -2..2 { for x in -RENDER_DIST..RENDER_DIST+1 {
for z in -2..2 { for z in -RENDER_DIST..RENDER_DIST+1 {
let pos = Vector3{x, y: 00, z}; let pos = Vector3{x, y: 0, z};
if instance.world.load_chunk(pos) { if instance.world.load_chunk(pos) {
let chunk = instance.world.get_chunk(&pos).unwrap(); let chunk = instance.world.get_chunk(&pos).unwrap();
chunk.write_to(renderer, pos * world::chunk::SIZE as i32); chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
@@ -81,36 +82,32 @@ impl Game {
if self.prev_chunk_pos == None || self.prev_chunk_pos != Some(chunk_pos) { if self.prev_chunk_pos == None || self.prev_chunk_pos != Some(chunk_pos) {
renderer.get_ui().set_text("World", 2, format!("Chunk: {}, {}, {}", chunk_pos.x, chunk_pos.y, chunk_pos.z)); renderer.get_ui().set_text("World", 2, format!("Chunk: {}, {}, {}", chunk_pos.x, chunk_pos.y, chunk_pos.z));
// Shift world
let dir = chunk_pos - self.prev_chunk_pos.unwrap_or(chunk_pos); //TODO: clamp to -1,1 let dir = chunk_pos - self.prev_chunk_pos.unwrap_or(chunk_pos); //TODO: clamp to -1,1
if dir.x != 0 || dir.y != 0 || dir.z != 0 {
// Shift world
renderer.shift(dir * world::chunk::SIZE as i32); renderer.shift(dir * world::chunk::SIZE as i32);
// Load chunks at the edge // Load chunks at the edge
let center = chunk_pos.clone() + 2*dir; let center = chunk_pos.clone() + RENDER_DIST*dir;
let mask = Vector3{x: dir.x.abs(), y: dir.y.abs(), z: dir.z.abs()}; let mask = Vector3{x: dir.x.abs(), y: dir.y.abs(), z: dir.z.abs()};
println!("Load at {:?}", center); println!("Load at {:?}", center);
for a in -2..3 { let pos = center; //+ Vector3{x: mask.z*a, y: 0, z: mask.x*a};
let pos = center + Vector3{x: mask.z*a, y: 0, z: mask.x*a};
self.world.load_chunk(pos); self.world.load_chunk(pos);
let chunk = self.world.get_chunk(&pos).unwrap(); let chunk = self.world.get_chunk(&pos).unwrap();
chunk.write_to(renderer, pos * world::chunk::SIZE as i32); chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
// for a in -RENDER_DIST..RENDER_DIST+1 {
// let pos = center; //+ Vector3{x: mask.z*a, y: 0, z: mask.x*a};
// self.world.load_chunk(pos);
// let chunk = self.world.get_chunk(&pos).unwrap();
// chunk.write_to(renderer, pos * world::chunk::SIZE as i32);
// }
} }
//println!("Writing chunk to {:?}", pos * world::chunk::SIZE as i32);
// for i in -2..2 {
// }
// if self.world.load_chunk(chunk_pos) {
// let chunk = self.world.get_chunk(&chunk_pos).unwrap();
// chunk.write_to(renderer, chunk_pos * world::chunk::SIZE as i32);
// println!("Writing chunk to {:?}", chunk_pos * world::chunk::SIZE as i32);
// }
self.prev_chunk_pos = Some(chunk_pos); self.prev_chunk_pos = Some(chunk_pos);
} }
} }
+26 -24
View File
@@ -2,12 +2,13 @@ use std::time::Instant;
use byteorder::{ByteOrder, LittleEndian}; use byteorder::{ByteOrder, LittleEndian};
use std::convert::TryInto; use std::convert::TryInto;
use cgmath::Vector3; use cgmath::Vector3;
use std::cmp::{min, max};
use crate::game::world::block::WorldBlock; use crate::game::world::block::WorldBlock;
//note: remember to update in main.frag //note: remember to update in main.frag
pub const BLOCK_SIZE : usize = 32; // 32x32x32 block size pub const BLOCK_SIZE : usize = 32; // 32x32x32 block size
pub const BLOCK_TEX_SIZE : usize = 40; // number of blocks in texture (32^3 = 1GB, 40^3 = 2GB) pub const BLOCK_TEX_SIZE : usize = 40; // number of blocks in texture (32^3 = 1GB, 40^3 = 2GB)
pub const NODE_TEX_SIZE: usize = 128; // 32x32x32 nodes in texture pub const NODE_TEX_SIZE: usize = 64; // 32x32x32 nodes in texture
const BLOCK_SIZE_QB: usize = BLOCK_SIZE*BLOCK_SIZE*BLOCK_SIZE; const BLOCK_SIZE_QB: usize = BLOCK_SIZE*BLOCK_SIZE*BLOCK_SIZE;
const BLOCK_TEX_SIZE_SQ: usize = BLOCK_TEX_SIZE*BLOCK_TEX_SIZE; const BLOCK_TEX_SIZE_SQ: usize = BLOCK_TEX_SIZE*BLOCK_TEX_SIZE;
@@ -20,6 +21,7 @@ pub struct ContentStats {
pub block_tex_size: f64, // MB pub block_tex_size: f64, // MB
pub block_tex_used: f64, // MB pub block_tex_used: f64, // MB
pub block_count: u64, // amount pub block_count: u64, // amount
pub block_freed: u64, // amount
pub block_used: u64, // amount pub block_used: u64, // amount
} }
@@ -27,6 +29,7 @@ pub struct Content {
node_buffer: Box<[u32]>, node_buffer: Box<[u32]>,
node_texture: wgpu::Texture, node_texture: wgpu::Texture,
node_dirty: bool, node_dirty: bool,
block_freed: Vec<u32>,
block_freeidx: usize, block_freeidx: usize,
block_texture: wgpu::Texture, block_texture: wgpu::Texture,
pub bind_layout: wgpu::BindGroupLayout, pub bind_layout: wgpu::BindGroupLayout,
@@ -85,26 +88,29 @@ impl Content {
// Done // Done
let block_freeidx = 0; let block_freeidx = 0;
Self { node_buffer, node_dirty: false, node_texture, block_freeidx, block_texture, bind_layout, bind_group } let node_dirty = false;
let block_freed = Vec::new();
Self { node_buffer, node_dirty, block_freed, node_texture, block_freeidx, block_texture, bind_layout, bind_group }
} }
pub fn stats(&self) -> ContentStats { pub fn stats(&self) -> ContentStats {
const TO_MB: f64 = 1.0 / (1024.0 * 1024.0); const TO_MB: f64 = 1.0 / (1024.0 * 1024.0);
let block_used = self.block_freeidx as u64; let block_used = self.block_freeidx as u64;
let block_freed = self.block_freed.len() as u64;
let block_count = BLOCK_TEX_SIZE_QB as u64; let block_count = BLOCK_TEX_SIZE_QB as u64;
let node_tex_size = (NODE_TEX_SIZE_QB*4) as f64 * TO_MB; let node_tex_size = (NODE_TEX_SIZE_QB*4) as f64 * TO_MB;
let block_tex_used = (block_used * BLOCK_SIZE_QB as u64) as f64 * TO_MB; let block_tex_used = (block_used * BLOCK_SIZE_QB as u64) as f64 * TO_MB;
let block_tex_size = (BLOCK_TEX_SIZE_QB*BLOCK_SIZE_QB) as f64 * TO_MB; let block_tex_size = (BLOCK_TEX_SIZE_QB*BLOCK_SIZE_QB) as f64 * TO_MB;
ContentStats { node_tex_size, block_tex_size, block_tex_used, block_count, block_used } ContentStats { node_tex_size, block_tex_size, block_tex_used, block_count, block_freed, block_used }
} }
pub fn write(&mut self, queue: &wgpu::Queue, block_pos: &Vector3<i32>, block: &WorldBlock) { pub fn write(&mut self, queue: &wgpu::Queue, block_pos: &Vector3<i32>, block: &WorldBlock) {
// 0,0 at node buffer center // 0,0 at node buffer center
let half = (NODE_TEX_SIZE / 2); let half = NODE_TEX_SIZE / 2;
let block_pos = block_pos + &Vector3{x: half as i32, y: half as i32, z: half as i32}; let block_pos = block_pos + &Vector3{x: half as i32, y: half as i32, z: half as i32};
// Get node // Get node
@@ -113,11 +119,16 @@ impl Content {
// Allocate new block // Allocate new block
if value == 0 { if value == 0 {
// Reuse freed if available, allocate new if not
if let Some(idx) = self.block_freed.pop() {
value = idx as usize;
} else {
value = self.block_freeidx + 1; value = self.block_freeidx + 1;
}
// Assign
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);
// Mark node buffer as dirty // Mark node buffer as dirty
self.node_dirty = true; self.node_dirty = true;
} }
@@ -133,8 +144,6 @@ impl Content {
let block_size = wgpu::Extent3d{ width: BLOCK_SIZE as u32, height: BLOCK_SIZE as u32, depth: BLOCK_SIZE as u32 }; let block_size = wgpu::Extent3d{ width: BLOCK_SIZE as u32, height: BLOCK_SIZE as u32, depth: BLOCK_SIZE as u32 };
let block_bytes : [u8;BLOCK_SIZE*BLOCK_SIZE*BLOCK_SIZE] = block.materials[..].try_into().unwrap(); let block_bytes : [u8;BLOCK_SIZE*BLOCK_SIZE*BLOCK_SIZE] = block.materials[..].try_into().unwrap();
//println!("Y: {} Z: {}", y, z);
// Write block // Write block
queue.write_texture( queue.write_texture(
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,
@@ -145,25 +154,18 @@ impl Content {
pub fn shift(&mut self, offset: &Vector3<i32>) { pub fn shift(&mut self, offset: &Vector3<i32>) {
let timer = Instant::now(); let timer = Instant::now();
let mut result = vec![0_u32; self.node_buffer.len()].into_boxed_slice(); let mut result = vec![0_u32; self.node_buffer.len()].into_boxed_slice();
let ox = offset.x; let (ox, oy, oz) = (offset.x, offset.y, offset.z);
let oy = offset.y;
let oz = offset.z;
let size = NODE_TEX_SIZE as i32; let size = NODE_TEX_SIZE as i32;
for x0 in 0..size { let a = max(0, -oz);
let x1 = x0+ox; let b = min(size-oz, size);
if x1 < 0 || x1 >= size { continue; } println!("from {} to {}", a, b);
for y0 in 0..size { for x in max(0, -ox)..min(size-ox, size) {
let y1 = y0+oy; for y in max(0, -oy)..min(size-oy, size) {
if y1 < 0 || y1 >= size { continue; } for z in max(0, -oz)..min(size-oz, size) {
let idx0 = x + size * (y + size * z);
for z0 in 0..size { let idx1 = (x+ox) + size * ((y+oy) + size * (z+oz));
let z1 = z0+oz;
if z1 < 0 || z1 >= size { continue; }
let idx0 = x0 + size * (y0 + size * z0);
let idx1 = x1 + size * (y1 + size * z1);
result[idx1 as usize] = self.node_buffer[idx0 as usize]; result[idx1 as usize] = self.node_buffer[idx0 as usize];
} }
} }
+7 -1
View File
@@ -150,7 +150,13 @@ impl Renderer {
// Stats // Stats
let content_stats = self.buffers.content.stats(); let content_stats = self.buffers.content.stats();
self.ui.set_text("Renderer", 0, format!("Nodes: {}MB", content_stats.node_tex_size)); self.ui.set_text("Renderer", 0, format!("Nodes: {}MB", content_stats.node_tex_size));
self.ui.set_text("Renderer", 1, format!("Blocks: {} / {} MB ({}/{})", content_stats.block_tex_used, content_stats.block_tex_size, content_stats.block_used, content_stats.block_count)); self.ui.set_text("Renderer", 1, format!("Blocks: {} / {} MB ({} -{} / {})",
content_stats.block_tex_used,
content_stats.block_tex_size,
content_stats.block_used,
content_stats.block_freed,
content_stats.block_count
));
// Return ok // Return ok
Ok(()) Ok(())
+1 -1
View File
@@ -22,7 +22,7 @@ impl RendererView for Renderer {
fn shift(&mut self, offset: Vector3<i32>) { fn shift(&mut self, offset: Vector3<i32>) {
self.buffers.content.shift(&-offset); self.buffers.content.shift(&-offset);
self.world_offset -= offset; self.world_offset -= offset;
//println!("World shifted by {:?}. World offset is now {:?}", offset, self.world_offset); println!("World shifted by {:?}. World offset is now {:?}", offset, self.world_offset);
} }
fn get_camera_transform(&mut self) -> &mut dyn CameraTransform { fn get_camera_transform(&mut self) -> &mut dyn CameraTransform {
+1 -1
View File
@@ -21,7 +21,7 @@ layout(set=2, binding=0) uniform Materials { Material _Materials[256]; };
// Defines // Defines
#define EPSILON 0.00000001 #define EPSILON 0.00000001
#define SCALE 1.0 #define SCALE 1.0
#define NODE_TEX_SIZE 128 #define NODE_TEX_SIZE 64
#define BLOCK_TEX_SIZE 40 #define BLOCK_TEX_SIZE 40
#define BLOCK_SIZE 32 #define BLOCK_SIZE 32