avoiding copying node buffer

This commit is contained in:
Piotrek
2021-05-19 14:01:40 +02:00
parent 3718c2145e
commit 15e40a941c
3 changed files with 12 additions and 10 deletions
+1 -1
View File
@@ -87,7 +87,7 @@ impl Game {
// Get positions
let cam_pos = camera.get_position();
let cam_pos = Vector3{x: cam_pos.x as i32, y: cam_pos.y as i32, z: cam_pos.z as i32};
let cam_pos = Vector3{x: cam_pos.x as i32, y: 0/*cam_pos.y as i32*/, z: cam_pos.z as i32};
let chunk_pos = cam_pos / world::chunk::SIZE as i32;
// why was this there? lol
//if cam_pos.x < 0 { chunk_pos.x -= 1; }
+2 -2
View File
@@ -94,9 +94,9 @@ fn main() {
delta_timer = std::time::Instant::now();
// Redraw if running
if let GameState::Running = game.get_state() {
//if let GameState::Running = game.get_state() {
window.request_redraw();
}
//}
}
// Draw
Event::RedrawRequested(_) => {
+9 -7
View File
@@ -4,7 +4,7 @@ use std::cmp::{min, max};
use std::time::Instant;
use cgmath::Vector3;
use std::thread;
use std::sync::{mpsc, mpsc::Receiver, mpsc::Sender};
use std::sync::{RwLock, mpsc, mpsc::Receiver, mpsc::Sender};
use byteorder::{LittleEndian, ByteOrder};
pub const NODE_TEX_SIZE: usize = 128; // 32x32x32 nodes in texture
@@ -14,7 +14,7 @@ pub const NODE_TEX_SIZE_QB: usize = NODE_TEX_SIZE*NODE_TEX_SIZE*NODE_TEX_SIZE;
type NodeBuffer = (Vector3<i32>, Box<[u32]>);
pub struct Nodes {
buffer: Box<[u32]>,
buffer: Arc<RwLock<Box<[u32]>>>,
offset: Vector3<i32>,
dirty: bool,
shift: Option<Vector3<i32>>,
@@ -26,7 +26,7 @@ impl Nodes {
pub fn new() -> Self {
Self {
// Present
buffer: vec![0_u32; NODE_TEX_SIZE_QB].into_boxed_slice(),
buffer: Arc::new(RwLock::new(vec![0_u32; NODE_TEX_SIZE_QB].into_boxed_slice())),
offset: Vector3{x:0,y:0,z:0},
dirty: false,
@@ -41,7 +41,7 @@ impl Nodes {
}
pub fn get_node(&self, index: usize) -> u32 {
self.buffer[index]
self.buffer.read().unwrap()[index]
}
pub fn is_busy(&self) -> bool {
@@ -49,7 +49,7 @@ impl Nodes {
}
pub fn set_node(&mut self, index: usize, value: u32) {
self.buffer[index] = value;
self.buffer.write().unwrap()[index] = value;
self.dirty = true;
}
@@ -72,6 +72,8 @@ impl Nodes {
// Spawn worker
thread::spawn(move || {
let source = source.read().unwrap();
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);
@@ -96,7 +98,7 @@ impl Nodes {
// Receive work
while let Ok(result) = self.channel.1.try_recv() {
self.offset += result.0;
self.buffer = result.1;
self.buffer = Arc::new(RwLock::new(result.1));
self.shift = None;
self.dirty = true;
self.working = false;
@@ -113,7 +115,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.buffer[offset..offset+NODE_TEX_SIZE_SQ], &mut node_bytes);
LittleEndian::write_u32_into(&self.buffer.read().unwrap()[offset..offset+NODE_TEX_SIZE_SQ], &mut node_bytes);
// Write nodes
queue.write_texture(