From 526ff05ed26cc92fd44ce582b842b41f799a145f Mon Sep 17 00:00:00 2001 From: Piotrek Date: Fri, 23 Apr 2021 22:15:14 +0200 Subject: [PATCH] testing 32x32x32 nodes world --- Cargo.lock | 65 +++++++++++++++++++++++++++ Cargo.toml | 2 + src/renderer/buffers/brick_buffer.rs | 9 ++-- src/renderer/buffers/node_buffer.rs | 51 +++++++++++++++++---- src/shaders/bin/shader.frag.spv | Bin 10076 -> 10076 bytes src/shaders/raycasting.cginc | 4 +- 6 files changed, 117 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 00cdac7..cd981e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -620,6 +620,17 @@ dependencies = [ "byteorder", ] +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "wasi", +] + [[package]] name = "gfx-auxil" version = "0.8.0" @@ -1420,6 +1431,12 @@ dependencies = [ "miniz_oxide 0.3.7", ] +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + [[package]] name = "proc-macro-crate" version = "0.1.5" @@ -1459,6 +1476,46 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + [[package]] name = "range-alloc" version = "0.1.2" @@ -1762,6 +1819,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + [[package]] name = "wasm-bindgen" version = "0.2.69" @@ -1982,11 +2045,13 @@ version = "0.1.0" dependencies = [ "anyhow", "bytemuck", + "byteorder", "cgmath", "fs_extra", "futures", "glob", "image", + "rand", "shaderc", "wgpu", "winit", diff --git a/Cargo.toml b/Cargo.toml index 81fe1ef..0483473 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,8 +11,10 @@ winit = "0.24" wgpu = "0.7" futures = "0.3" bytemuck = { version = "1.5", features = [ "derive" ] } +byteorder = "1.4" image = "0.23.14" cgmath = "0.18" +rand = "0.8" [build-dependencies] anyhow = "1.0" diff --git a/src/renderer/buffers/brick_buffer.rs b/src/renderer/buffers/brick_buffer.rs index eec43b3..6241769 100644 --- a/src/renderer/buffers/brick_buffer.rs +++ b/src/renderer/buffers/brick_buffer.rs @@ -1,5 +1,6 @@ use wgpu::util::DeviceExt; use std::convert::TryInto; +use rand::prelude::*; const BRICK_SIZE : usize = 32; // 32x32x32 brick size const BRICK_NUM : usize = 10000; // x bricks in texture @@ -94,11 +95,13 @@ impl BrickBuffer { // Data let mut bricks = BrickData::new(); println!("Brick buffer size: {} MB", DATA_LEN as f32 / 1024.0 / 1024.0); + + let mut rng = rand::thread_rng(); for x in 0..32 { - for y in 0..16 { + for y in 0..32 { for z in 0..32 { - bricks.set(0, x, y, z, 1); - bricks.set(1, y, x, z, 1); + let value = if rng.gen::()> 0.99 { 1 } else { 0 }; + bricks.set(0, x, y, z, value); } } } diff --git a/src/renderer/buffers/node_buffer.rs b/src/renderer/buffers/node_buffer.rs index 7154530..97ce4e3 100644 --- a/src/renderer/buffers/node_buffer.rs +++ b/src/renderer/buffers/node_buffer.rs @@ -1,10 +1,31 @@ +use byteorder::{ByteOrder, LittleEndian}; +use rand::prelude::*; // 4x4x4 nodes -const WORLD_SIZE: u32 = 4; +const WORLD_SIZE: usize = 32; const NUM_NODES: usize = (WORLD_SIZE*WORLD_SIZE*WORLD_SIZE) as usize; +struct NodeData { + pub data: Box<[u32]> +} + +impl NodeData { + + pub fn new() -> Self { + let data = vec![0_u32; NUM_NODES].into_boxed_slice(); + Self { data } + } + + pub fn set(&mut self, x: usize, y: usize, z: usize, value: u32) + { + let idx = x + WORLD_SIZE * (y + WORLD_SIZE * z); + self.data[idx] = value; + } +} + + pub struct NodeBuffer { - nodes: [u32;NUM_NODES], + nodes: NodeData, texture: wgpu::Texture, size: wgpu::Extent3d, pub bind_layout: wgpu::BindGroupLayout, @@ -16,7 +37,7 @@ impl NodeBuffer { pub fn new(device: &wgpu::Device) -> Self { // Create texture - let size = wgpu::Extent3d { width: WORLD_SIZE, height: WORLD_SIZE, depth: WORLD_SIZE }; + let size = wgpu::Extent3d { width: WORLD_SIZE as u32, height: WORLD_SIZE as u32, depth: WORLD_SIZE as u32 }; let texture = device.create_texture( &wgpu::TextureDescriptor { size: size, @@ -69,10 +90,22 @@ impl NodeBuffer { ); // Data - let mut nodes : [u32; NUM_NODES] = [Default::default(); NUM_NODES]; - nodes[0] = 1; - nodes[1] = 2; - nodes[2] = 2; + let mut nodes = NodeData::new(); + + let mut rng = rand::thread_rng(); + for x in 0..WORLD_SIZE { + for y in 0..WORLD_SIZE { + for z in 0..WORLD_SIZE { + let value = if rng.gen::()> 0.5 { 1 } else { 0 }; + nodes.set(x, y, z, value); + } + } + } + // for i in 0..WORLD_SIZE { + // nodes.set(i, 0, 0, 1); + // nodes.set(0, i, 0, 1); + // nodes.set(0, 0, i, 1); + // } // Done Self { nodes, texture, size, bind_layout, bind_group } @@ -80,8 +113,8 @@ impl NodeBuffer { pub fn write(&mut self, queue: &wgpu::Queue) { // Convert 32 bit values to 8 bit - let bytes = bytemuck::bytes_of(&self.nodes); - println!("Node buffer size: {} kB", bytes.len() as f32 / 1024.0); + let mut bytes = [0_u8; NUM_NODES*4]; + LittleEndian::write_u32_into(&self.nodes.data, &mut bytes); // Write queue.write_texture( diff --git a/src/shaders/bin/shader.frag.spv b/src/shaders/bin/shader.frag.spv index 3e1ad77a868bb3fc910258241e3bda8f5c870a34..7245206a96bb1bdb092ee4d366cd21683b77ab3e 100644 GIT binary patch delta 1491 zcmY+D%}Z2K7{I=syTb9WAZrce)2|m;2uLd7t<5eBAZ1^|23wvFk}|%BH+Mi22h*`=Au;l&d9F_W*a#jeH+2ju=qjA;xH zC~R6Np-{c%oM|yB>9$}akxBN8%FA+6?`)m4Hc>CPeNBySiv1y>&Rf-3mEMxTZ>v>9 znrBx&_-zSX!!$WOtbyLh-U({ij>;}b@EoHaUZpRu#ZxIIwG>jb8f@MyHz$EXE>xr_~6K25vE>M zdSALN*y|n{=TfzZxvYu_X)adO=3+%{E>_g$VtIQ>`T5{g&-=p38)427*}by8ywDnn>kyBRcy4R zXN9TpQH}Y@287|B3v+L=ixO&48%*w+{J~&f+9reXzPi5g++CXv?&mJr_WJAGh!yp% zf5PVK7xEo8#r%G)CE>2%PT|^r?6hM)6>b$+-)tm)P8iPT&kLu6^TmGK9^BNoD<~H; z1v)&9L@f$Ov&t^(v*MG%MlmzRHCU3cF>DfxL;HB1t5wRfI+ip?QgNKplq=Y7@j+ zw+w<76+?^K^pDu0KOhLoYdQ7&KEDHBAM?)pKF@nSx6O&oiT9Ud*Amv4`G-rU$uyca z*^76}<8zNqfK!Kfs&s!Yz#YOfrKLwkAMqLC(%kgea*6zy$rP96xj_P~L@R{f>O-#&h3Eh53h^K>}_Q%eN}MuH6E+2*YuvBWi>Xj{G&*)LWOX zOSig%?&2huDlX=dDz->-v7(rZ6~$bvDCS~$yrTS^d)f1@a#U7$@exlQCWiBMPFhn53i;;ka_SeHqMShz%Yyew zr(`pb$ERhpfTv;8Gs48c`vE&;7Zu~B8pft=#TXI@@CCbM)AU6c)GZs1WS`h0oR;A8 zdA+h(;YOI(C!08UR?m6BVRII|+mQi*oQwq(=ji)obM)wKZFfQXti%{sFLVwK$RzxO zdhnhGWyA5-xuW2_gfzhW 32 || pos.y > 32 || pos.z > 32) { break; } + if(pos.x > 31 || pos.y > 31 || pos.z > 31) { break; } } return vec4(0.5,0.5,0.5, 0.0); @@ -88,7 +88,7 @@ vec3 castNodes(vec3 origin, vec3 dir) vec3 incAxis = vec3(0,0,0); // Iterate - for(int i=0;i<56;i++) + for(int i=0;i<110;i++) { // Get node uint node = texelFetch(nodesTexture, ivec3(pos), 0).r;