diff --git a/src/renderer/buffers/blocks.rs b/src/renderer/buffers/blocks.rs index fcca948..8b607c7 100644 --- a/src/renderer/buffers/blocks.rs +++ b/src/renderer/buffers/blocks.rs @@ -1,7 +1,5 @@ use std::convert::TryInto; - -use crate::renderer::buffers::{BLOCK_SIZE, BLOCK_NUM, BLOCK_LEN}; -const DATA_LEN : usize = BLOCK_LEN * BLOCK_NUM; +use crate::renderer::buffers::{BLOCK_SIZE, BLOCK_TEX_SIZE, BLOCK_TEX_LEN, BLOCK_LEN}; pub struct Blocks { @@ -20,7 +18,8 @@ impl Blocks { pub fn new(device: &wgpu::Device) -> Self { // Create texture - let size = wgpu::Extent3d { width: BLOCK_SIZE as u32, height: BLOCK_SIZE as u32, depth: (BLOCK_SIZE*BLOCK_NUM) as u32 }; + let BLOCK_TEX_DIM = BLOCK_TEX_SIZE * BLOCK_SIZE; + let size = wgpu::Extent3d { width: BLOCK_SIZE as u32, height: BLOCK_TEX_DIM as u32, depth: BLOCK_TEX_DIM as u32 }; let texture = device.create_texture( &wgpu::TextureDescriptor { size: size, @@ -73,9 +72,9 @@ impl Blocks { ); // Data - let mut blocks = vec![0_u8; DATA_LEN].into_boxed_slice(); + let mut blocks = vec![0_u8; BLOCK_TEX_LEN].into_boxed_slice(); blocks[0] = 1; - println!("block buffer size: {} MB", DATA_LEN as f32 / 1024.0 / 1024.0); + println!("block buffer size: {} MB", BLOCK_TEX_LEN as f32 / 1024.0 / 1024.0); // Done Self { blocks, texture, bind_layout, bind_group, free_block: 0 } @@ -90,7 +89,10 @@ impl Blocks { let block_offset = block*BLOCK_LEN; let block_bytes : [u8;BLOCK_LEN] = self.blocks[block_offset..block_offset+BLOCK_LEN].try_into().unwrap(); let block_size = wgpu::Extent3d{ width: BLOCK_SIZE as u32, height: BLOCK_SIZE as u32, depth: BLOCK_SIZE as u32 }; - let block_origin = wgpu::Origin3d{ x:0, y:0, z: (block*BLOCK_SIZE) as u32 }; + + let y = (block%BLOCK_TEX_SIZE * BLOCK_SIZE) as u32; + let z = (block/BLOCK_TEX_SIZE * BLOCK_SIZE) as u32; + let block_origin = wgpu::Origin3d{ x:0, y, z }; // Write queue.write_texture( diff --git a/src/renderer/buffers/mod.rs b/src/renderer/buffers/mod.rs index e5f426f..ffb0e63 100644 --- a/src/renderer/buffers/mod.rs +++ b/src/renderer/buffers/mod.rs @@ -1,12 +1,16 @@ // Consts -pub const WORLD_SIZE: usize = 32; -pub const NUM_NODES: usize = (WORLD_SIZE*WORLD_SIZE*WORLD_SIZE) as usize; +pub const BLOCK_SIZE : usize = 32; // 32x32x32 block size +pub const BLOCK_TEX_SIZE : usize = 64; // 64x64 blocks in texture +pub const NODE_TEX_SIZE: usize = 32; // 32x32x32 nodes in texture + + +pub const NODE_TEX_LEN: usize = NODE_TEX_SIZE*NODE_TEX_SIZE*NODE_TEX_SIZE; // Number of u32 in node texture +pub const BLOCK_LEN : usize = BLOCK_SIZE*BLOCK_SIZE*BLOCK_SIZE; // Number of u8 in one block +pub const BLOCK_TEX_LEN: usize = BLOCK_TEX_SIZE*BLOCK_TEX_SIZE * BLOCK_LEN; // Number of u8 in block texture + -pub const BLOCK_SIZE : usize = 32; // 32x32x32 brick size -pub const BLOCK_NUM : usize = 32768; // max bricks in texture -pub const BLOCK_LEN : usize = BLOCK_SIZE*BLOCK_SIZE*BLOCK_SIZE; // Import names diff --git a/src/renderer/buffers/nodes.rs b/src/renderer/buffers/nodes.rs index 0ab90a7..a668a1f 100644 --- a/src/renderer/buffers/nodes.rs +++ b/src/renderer/buffers/nodes.rs @@ -1,7 +1,7 @@ use byteorder::{ByteOrder, LittleEndian}; use rand::prelude::*; -use crate::renderer::buffers::{WORLD_SIZE, NUM_NODES}; +use crate::renderer::buffers::{NODE_TEX_SIZE, NODE_TEX_LEN}; pub struct Nodes { pub nodes: Box<[u32]>, @@ -16,7 +16,7 @@ impl Nodes { pub fn new(device: &wgpu::Device) -> Self { // Create texture - let size = wgpu::Extent3d { width: WORLD_SIZE as u32, height: WORLD_SIZE as u32, depth: WORLD_SIZE as u32 }; + let size = wgpu::Extent3d { width: NODE_TEX_SIZE as u32, height: NODE_TEX_SIZE as u32, depth: NODE_TEX_SIZE as u32 }; let texture = device.create_texture( &wgpu::TextureDescriptor { size: size, @@ -69,7 +69,7 @@ impl Nodes { ); // Data - let nodes = vec![0_u32; NUM_NODES].into_boxed_slice(); + let nodes = vec![0_u32; NODE_TEX_LEN].into_boxed_slice(); println!("Node buffer size: {} MB", nodes.len() as f32 * 4.0 / 1024.0 / 1024.0); // Done @@ -78,7 +78,7 @@ impl Nodes { pub fn write(&mut self, queue: &wgpu::Queue) { // Convert 32 bit values to 8 bit - let mut bytes = [0_u8; NUM_NODES*4]; + let mut bytes = [0_u8; NODE_TEX_LEN*4]; LittleEndian::write_u32_into(&self.nodes, &mut bytes); // Write diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 993c010..49b2112 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -7,7 +7,8 @@ use buffers::{Nodes, Blocks, Uniform, Raster, Materials}; mod passes; use passes::RaytracePass; -pub mod camera; use camera::Camera; +pub mod camera; +use camera::Camera; pub struct Renderer { @@ -124,7 +125,11 @@ impl Renderer { self.brick_buffer.write(&self.queue, *node); } - println!("Changed: {}", self.changed_bricks.len()); + let used = self.brick_buffer.next_block(false); + let total = buffers::BLOCK_TEX_SIZE * buffers::BLOCK_TEX_SIZE; + let changed = self.changed_bricks.len(); + println!("Used: {}/{} Changed: {}", used, total, changed); + self.changed_bricks.clear(); } @@ -192,7 +197,7 @@ impl Renderer { */ pub fn block_get_id(&mut self, pos: Point3, apply: bool) -> usize { // Get node - let node_idx = pos.x + buffers::WORLD_SIZE * (pos.y + buffers::WORLD_SIZE * pos.z); + let node_idx = pos.x + buffers::NODE_TEX_SIZE * (pos.y + buffers::NODE_TEX_SIZE * pos.z); let mut node_value = self.node_buffer.nodes[node_idx]; // Use new brick if neccesary diff --git a/src/shaders/bin/shader.frag.spv b/src/shaders/bin/shader.frag.spv index 4e09391..d305dd6 100644 Binary files a/src/shaders/bin/shader.frag.spv and b/src/shaders/bin/shader.frag.spv differ diff --git a/src/shaders/raycasting.cginc b/src/shaders/raycasting.cginc index a420473..f660a26 100644 --- a/src/shaders/raycasting.cginc +++ b/src/shaders/raycasting.cginc @@ -20,7 +20,11 @@ HitResult castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr) vec3 rayInv = 1.0/dir; vec3 raySign = sign(dir); vec3 dist = (pos-origin+0.5 + raySign*0.5) * rayInv; - ivec3 offset = ivec3(0, 0, (addr-1)*32); + + // Calculate offset + uint y = (addr-1) % BLOCK_TEX_SIZE * BLOCK_SIZE; + uint z = (addr-1) / BLOCK_TEX_SIZE * BLOCK_SIZE; + ivec3 offset = ivec3(0, y, z); for(int i=0;i<110;i++) { @@ -45,7 +49,7 @@ HitResult castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr) // Check for bounds if(pos.x < 0 || pos.y < 0 || pos.z < 0) { break; } - if(pos.x > 31 || pos.y > 31 || pos.z > 31) { break; } + if(pos.x >= BLOCK_SIZE || pos.y >= BLOCK_SIZE || pos.z >= BLOCK_SIZE) { break; } } return HitResult(0, vec3(0,0,0), vec3(0,0,0)); @@ -92,8 +96,8 @@ HitResult castNodes(Ray ray, uint maxSteps) pos += incAxis * raySign; // Outside bounds - //if(pos.x < 0 || pos.y < 0 || pos.z < 0) { break; } - // if(pos.x > 1 || pos.y > 1 || pos.z > 1) { break; } + if(pos.x < 0 || pos.y < 0 || pos.z < 0) { break; } + if(pos.x >= BLOCK_TEX_SIZE || pos.y > BLOCK_TEX_SIZE || pos.z >= NODE_TEX_SIZE) { break; } } // Not found diff --git a/src/shaders/shader.frag b/src/shaders/shader.frag index 4f575f5..a8cbf6d 100644 --- a/src/shaders/shader.frag +++ b/src/shaders/shader.frag @@ -24,9 +24,11 @@ layout(set=3, binding=0) uniform Materials { }; // Defines -// #define PI 3.141592 -// #define EPSILON 0.01 -// #define NEAR 0.01 +#define EPSILON 0.00000001 +#define NODE_TEX_SIZE 32 +#define BLOCK_TEX_SIZE 64 +#define BLOCK_SIZE 32 + // Include #include "structs.cginc" @@ -66,7 +68,7 @@ vec3 solve() if(primary.data > 0) { vec3 color = applyLighting(primary.pos, primaryRay.dir, primary.normal, primary.data); - vec3 hitPos = primary.pos-primaryRay.dir*0.00000001; + vec3 hitPos = primary.pos-primary.normal*EPSILON; // Shadow ray Ray shadowRay = Ray(hitPos, _SunDir);