trying to free unused chunks
This commit is contained in:
+6
-7
@@ -126,13 +126,12 @@ impl Game {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Calculate if chunk is within render distance
|
// Calculate if chunk is within render distance
|
||||||
let ox = pos.x - chunk_pos.x;
|
let (ox, oy, oz) = (pos.x - chunk_pos.x, pos.y - chunk_pos.y, pos.z - chunk_pos.z);
|
||||||
let oy = pos.y - chunk_pos.y;
|
let within_distance = ox > -RENDER_DIST && ox < RENDER_DIST
|
||||||
let oz = pos.z - chunk_pos.z;
|
&& oy > -RENDER_DIST && oy < RENDER_DIST
|
||||||
let dist = RENDER_DIST;
|
&& oz > -RENDER_DIST && oz < RENDER_DIST;
|
||||||
let within_distance = ox > -dist && ox < dist && oy > -dist && oy < dist && oz > -dist && oz < dist;
|
|
||||||
|
|
||||||
// Check if chunk is within render distance
|
// Within render distance and waiting
|
||||||
if within_distance && chunk.state == ChunkState::Waiting {
|
if within_distance && chunk.state == ChunkState::Waiting {
|
||||||
// Write chunk. If succeded change its state to Visible, otherwise make it Ready again.
|
// Write chunk. If succeded change its state to Visible, otherwise make it Ready again.
|
||||||
if renderer.write_chunk(pos, chunk) {
|
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 {
|
if !within_distance && chunk.state == ChunkState::Visible {
|
||||||
// Set chunk to stale, a.k.a. was visible but is not anymore
|
// Set chunk to stale, a.k.a. was visible but is not anymore
|
||||||
chunk.state = ChunkState::Stale;
|
chunk.state = ChunkState::Stale;
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
use std::time::Instant;
|
|
||||||
use byteorder::{ByteOrder, LittleEndian};
|
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use cgmath::Vector3;
|
use cgmath::Vector3;
|
||||||
use crate::game::world::block::WorldBlock;
|
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 }
|
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 {
|
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);
|
||||||
|
|
||||||
@@ -100,22 +110,6 @@ impl Content {
|
|||||||
ContentStats { node_tex_size, block_tex_size, block_tex_used, block_count, block_freed, block_used }
|
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) {
|
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
|
||||||
@@ -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>) {
|
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);
|
self.nodes.shift(offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, queue: &wgpu::Queue) {
|
|
||||||
self.nodes.update(queue, &self.node_texture);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -57,7 +57,7 @@ impl Nodes {
|
|||||||
if self.shift == None {
|
if self.shift == None {
|
||||||
self.shift = Some(*offset);
|
self.shift = Some(*offset);
|
||||||
} else {
|
} 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 || {
|
thread::spawn(move || {
|
||||||
let source = source.read().unwrap();
|
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 mut target = vec![0_u32; NODE_TEX_SIZE_QB].into_boxed_slice();
|
||||||
let (ox, oy, oz) = (offset.x, offset.y, offset.z);
|
let (ox, oy, oz) = (offset.x, offset.y, offset.z);
|
||||||
|
|
||||||
@@ -97,7 +97,9 @@ impl Nodes {
|
|||||||
|
|
||||||
// Receive work
|
// Receive work
|
||||||
while let Ok(result) = self.channel.1.try_recv() {
|
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.buffer = Arc::new(RwLock::new(result.1));
|
||||||
self.shift = None;
|
self.shift = None;
|
||||||
self.dirty = true;
|
self.dirty = true;
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ pub trait RendererView {
|
|||||||
fn shift(&mut self, offset: Vector3<i32>);
|
fn shift(&mut self, offset: Vector3<i32>);
|
||||||
fn get_camera_transform(&mut self) -> &mut dyn CameraTransform;
|
fn get_camera_transform(&mut self) -> &mut dyn CameraTransform;
|
||||||
fn get_ui(&mut self) -> &mut UserInterface;
|
fn get_ui(&mut self) -> &mut UserInterface;
|
||||||
fn free_chunk(&mut self, chunk_pos: Vector3<i32>);
|
|
||||||
fn is_busy(&self) -> bool;
|
fn is_busy(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,19 +31,6 @@ impl RendererView for Renderer {
|
|||||||
true
|
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
|
* offset: By how much to shift the world, in blocks
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user