some code cleaning
This commit is contained in:
@@ -1,66 +1,102 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::cmp::{min, max};
|
||||
use std::time::Instant;
|
||||
use cgmath::Vector3;
|
||||
use std::thread;
|
||||
use byteorder::{LittleEndian, ByteOrder};
|
||||
|
||||
pub const NODE_TEX_SIZE: usize = 128; // 32x32x32 nodes in texture
|
||||
pub const NODE_TEX_SIZE_SQ: usize = NODE_TEX_SIZE*NODE_TEX_SIZE;
|
||||
pub const NODE_TEX_SIZE_QB: usize = NODE_TEX_SIZE*NODE_TEX_SIZE*NODE_TEX_SIZE;
|
||||
|
||||
struct ShiftResult {
|
||||
offset: Vector3<i32>,
|
||||
active_buffer: usize
|
||||
}
|
||||
|
||||
pub struct Nodes {
|
||||
node_buffer: [Box<[u32]>; 2],
|
||||
// Realtime variables
|
||||
node_buffer: Arc<Mutex<[Box<[u32]>; 2]>>,
|
||||
world_offset: Vector3<i32>,
|
||||
active_buffer: usize,
|
||||
modified: bool,
|
||||
dirty: bool,
|
||||
// Threaded work
|
||||
busy: i32,
|
||||
}
|
||||
|
||||
impl Nodes {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
node_buffer: [vec![0_u32; NODE_TEX_SIZE_QB].into_boxed_slice(), vec![0_u32; NODE_TEX_SIZE_QB].into_boxed_slice()],
|
||||
// Realtime variables
|
||||
node_buffer: Arc::new(Mutex::new([vec![0_u32; NODE_TEX_SIZE_QB].into_boxed_slice(), vec![0_u32; NODE_TEX_SIZE_QB].into_boxed_slice()])),
|
||||
world_offset: Vector3{x:0,y:0,z:0},
|
||||
active_buffer: 0,
|
||||
modified: false,
|
||||
dirty: false,
|
||||
// Threaded work
|
||||
busy: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_world_offset(&self) -> Vector3<i32> {
|
||||
self.world_offset
|
||||
}
|
||||
|
||||
pub fn get_node(&self, index: usize) -> u32 {
|
||||
self.node_buffer[self.active_buffer][index]
|
||||
self.node_buffer.lock().unwrap()[self.active_buffer][index]
|
||||
}
|
||||
|
||||
pub fn set_node(&mut self, index: usize, value: u32) {
|
||||
self.node_buffer[self.active_buffer][index] = value;
|
||||
self.modified = true;
|
||||
self.node_buffer.lock().unwrap()[self.active_buffer][index] = value;
|
||||
self.dirty = true;
|
||||
}
|
||||
|
||||
pub fn shift(&mut self, offset: &Vector3<i32>) -> bool {
|
||||
pub fn shift(&mut self, offset: &Vector3<i32>) {
|
||||
|
||||
let a = if self.active_buffer == 0 { 0 } else { 1 };
|
||||
let b = if self.active_buffer == 0 { 1 } else { 0 };
|
||||
|
||||
let (ox, oy, oz) = (offset.x, offset.y, offset.z);
|
||||
let size = NODE_TEX_SIZE as i32;
|
||||
let node_buffer = Arc::clone(&self.node_buffer);
|
||||
|
||||
self.busy += 1;
|
||||
|
||||
for i in self.node_buffer[b].iter_mut() {
|
||||
*i = 0;
|
||||
}
|
||||
|
||||
for x in max(0, -ox)..min(size-ox, size) {
|
||||
for y in max(0, -oy)..min(size-oy, size) {
|
||||
for z in max(0, -oz)..min(size-oz, size) {
|
||||
let idx0 = x + size * (y + size * z);
|
||||
let idx1 = (x+ox) + size * ((y+oy) + size * (z+oz));
|
||||
self.node_buffer[b][idx1 as usize] = self.node_buffer[a][idx0 as usize];
|
||||
}
|
||||
}
|
||||
}
|
||||
let t = thread::spawn(move || {
|
||||
// Prepare variables
|
||||
let mut node_buffer = node_buffer.lock().unwrap();
|
||||
let size = NODE_TEX_SIZE as i32;
|
||||
|
||||
// Clear target buffer
|
||||
for i in node_buffer[b].iter_mut() {
|
||||
*i = 0;
|
||||
}
|
||||
|
||||
// Copy slice of source buffer into target buffer
|
||||
for x in max(0, -ox)..min(size-ox, size) {
|
||||
for y in max(0, -oy)..min(size-oy, size) {
|
||||
for z in max(0, -oz)..min(size-oz, size) {
|
||||
let idx0 = x + size * (y + size * z);
|
||||
let idx1 = (x+ox) + size * ((y+oy) + size * (z+oz));
|
||||
node_buffer[b][idx1 as usize] = node_buffer[a][idx0 as usize];
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// DEBUG
|
||||
t.join().unwrap();
|
||||
|
||||
self.world_offset += *offset;
|
||||
self.active_buffer = b;
|
||||
self.modified = true;
|
||||
true
|
||||
self.dirty = true;
|
||||
self.busy -= 1;
|
||||
}
|
||||
|
||||
pub fn update(&mut self, queue: &wgpu::Queue, texture: &wgpu::Texture) {
|
||||
if self.modified {
|
||||
self.modified = false;
|
||||
|
||||
|
||||
if self.dirty {
|
||||
self.dirty = false;
|
||||
|
||||
// Write all nodes to gpu
|
||||
for z in 0..NODE_TEX_SIZE {
|
||||
@@ -70,7 +106,7 @@ impl Nodes {
|
||||
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];
|
||||
let offset = NODE_TEX_SIZE_SQ*z;
|
||||
LittleEndian::write_u32_into(&self.node_buffer[self.active_buffer][offset..offset+NODE_TEX_SIZE_SQ], &mut node_bytes);
|
||||
LittleEndian::write_u32_into(&self.node_buffer.lock().unwrap()[self.active_buffer][offset..offset+NODE_TEX_SIZE_SQ], &mut node_bytes);
|
||||
|
||||
// Write nodes
|
||||
queue.write_texture(
|
||||
|
||||
Reference in New Issue
Block a user