trying to free unused chunks

This commit is contained in:
Piotrek
2021-05-20 15:13:10 +02:00
parent 7daec14ba7
commit dac7c52912
4 changed files with 43 additions and 54 deletions
+6 -7
View File
@@ -126,13 +126,12 @@ impl Game {
}
// Calculate if chunk is within render distance
let ox = pos.x - chunk_pos.x;
let oy = pos.y - chunk_pos.y;
let oz = pos.z - chunk_pos.z;
let dist = RENDER_DIST;
let within_distance = ox > -dist && ox < dist && oy > -dist && oy < dist && oz > -dist && oz < dist;
let (ox, oy, oz) = (pos.x - chunk_pos.x, pos.y - chunk_pos.y, pos.z - chunk_pos.z);
let within_distance = ox > -RENDER_DIST && ox < RENDER_DIST
&& oy > -RENDER_DIST && oy < RENDER_DIST
&& oz > -RENDER_DIST && oz < RENDER_DIST;
// Check if chunk is within render distance
// Within render distance and waiting
if within_distance && chunk.state == ChunkState::Waiting {
// Write chunk. If succeded change its state to Visible, otherwise make it Ready again.
if renderer.write_chunk(pos, chunk) {
@@ -142,7 +141,7 @@ impl Game {
}
}
// If not within render distance and set as visible
// Outside render distance and visible
if !within_distance && chunk.state == ChunkState::Visible {
// Set chunk to stale, a.k.a. was visible but is not anymore
chunk.state = ChunkState::Stale;
+32 -30
View File
@@ -1,5 +1,3 @@
use std::time::Instant;
use byteorder::{ByteOrder, LittleEndian};
use std::convert::TryInto;
use cgmath::Vector3;
use crate::game::world::block::WorldBlock;
@@ -86,6 +84,18 @@ impl Content {
Self { nodes, block_freed, node_texture, block_freeidx, block_texture, bind_layout, bind_group }
}
pub fn is_busy(&self) -> bool {
self.nodes.is_busy()
}
pub fn get_world_offset(&self) -> Vector3<i32> {
self.nodes.get_world_offset()
}
pub fn update(&mut self, queue: &wgpu::Queue) {
self.nodes.update(queue, &self.node_texture);
}
pub fn stats(&self) -> ContentStats {
const TO_MB: f64 = 1.0 / (1024.0 * 1024.0);
@@ -100,22 +110,6 @@ impl Content {
ContentStats { node_tex_size, block_tex_size, block_tex_used, block_count, block_freed, block_used }
}
pub fn free(&mut self, block_pos: Vector3<i32>) {
// 0,0 at node buffer center
let half = NODE_TEX_SIZE as i32 / 2;
let pos = block_pos + &Vector3{x: half, y: half, z: half};
// Get node
let index = (pos.x + NODE_TEX_SIZE as i32 * (pos.y + NODE_TEX_SIZE as i32 * pos.z)) as usize;
let value = self.nodes.get_node(index) as usize;
// Free it if used
if value != 0 {
self.block_freed.push(value);
}
}
pub fn write(&mut self, queue: &wgpu::Queue, block_pos: &Vector3<i32>, block: &WorldBlock) {
// 0,0 at node buffer center
@@ -171,19 +165,27 @@ impl Content {
);
}
pub fn is_busy(&self) -> bool {
self.nodes.is_busy()
}
pub fn get_world_offset(&self) -> Vector3<i32> {
self.nodes.get_world_offset()
}
pub fn shift(&mut self, offset: &Vector3<i32>) {
// Work in progress.
// Note: [z is swapped with x ], [ node buffer is not equal to render distance?]
// if offset.x > 0 {
// let size = NODE_TEX_SIZE as i32;
// for x in 0..128 {
// for y in 0..128 {
// for z in 0..32 {
// let index = (x + size * (y + size * z)) as usize;
// let value = self.nodes.get_node(index);
// if value != 0 { self.block_freed.push(value as usize); }
// }
// }
// }
// println!("Saved: {}", self.block_freed.len());
// }
self.nodes.shift(offset);
}
pub fn update(&mut self, queue: &wgpu::Queue) {
self.nodes.update(queue, &self.node_texture);
}
}
+5 -3
View File
@@ -57,7 +57,7 @@ impl Nodes {
if self.shift == None {
self.shift = Some(*offset);
} else {
println!("Tried to shift while another shifin was in progress");
println!("Tried to shift while another shifing was in progress. Check RendererView.is_busy() before shifting.");
}
}
@@ -74,7 +74,7 @@ impl Nodes {
thread::spawn(move || {
let source = source.read().unwrap();
let size = NODE_TEX_SIZE as i32;
let size = NODE_TEX_SIZE as i32;
let mut target = vec![0_u32; NODE_TEX_SIZE_QB].into_boxed_slice();
let (ox, oy, oz) = (offset.x, offset.y, offset.z);
@@ -97,7 +97,9 @@ impl Nodes {
// Receive work
while let Ok(result) = self.channel.1.try_recv() {
self.offset += result.0;
let moved_by = result.0;
self.offset += moved_by;
self.buffer = Arc::new(RwLock::new(result.1));
self.shift = None;
self.dirty = true;
-14
View File
@@ -9,7 +9,6 @@ pub trait RendererView {
fn shift(&mut self, offset: Vector3<i32>);
fn get_camera_transform(&mut self) -> &mut dyn CameraTransform;
fn get_ui(&mut self) -> &mut UserInterface;
fn free_chunk(&mut self, chunk_pos: Vector3<i32>);
fn is_busy(&self) -> bool;
}
@@ -32,19 +31,6 @@ impl RendererView for Renderer {
true
}
fn free_chunk(&mut self, chunk_pos: Vector3<i32>) {
// Free all blocks within chunk
let size = chunk::SIZE as i32;
let pos = chunk_pos * size;
for x in 0..size {
for y in 0..size {
for z in 0..size {
self.buffers.content.free(pos + Vector3{x, y, z});
}
}
}
}
/*
* offset: By how much to shift the world, in blocks
*/