changed brick texture from being 32x32x32 1D to 32x32x32 2D (smaller indexes)
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
|
use crate::renderer::buffers::{BLOCK_SIZE, BLOCK_TEX_SIZE, BLOCK_TEX_LEN, BLOCK_LEN};
|
||||||
use crate::renderer::buffers::{BLOCK_SIZE, BLOCK_NUM, BLOCK_LEN};
|
|
||||||
const DATA_LEN : usize = BLOCK_LEN * BLOCK_NUM;
|
|
||||||
|
|
||||||
|
|
||||||
pub struct Blocks {
|
pub struct Blocks {
|
||||||
@@ -20,7 +18,8 @@ impl Blocks {
|
|||||||
pub fn new(device: &wgpu::Device) -> Self {
|
pub fn new(device: &wgpu::Device) -> Self {
|
||||||
|
|
||||||
// Create texture
|
// 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(
|
let texture = device.create_texture(
|
||||||
&wgpu::TextureDescriptor {
|
&wgpu::TextureDescriptor {
|
||||||
size: size,
|
size: size,
|
||||||
@@ -73,9 +72,9 @@ impl Blocks {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Data
|
// 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;
|
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
|
// Done
|
||||||
Self { blocks, texture, bind_layout, bind_group, free_block: 0 }
|
Self { blocks, texture, bind_layout, bind_group, free_block: 0 }
|
||||||
@@ -90,7 +89,10 @@ impl Blocks {
|
|||||||
let block_offset = block*BLOCK_LEN;
|
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_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_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
|
// Write
|
||||||
queue.write_texture(
|
queue.write_texture(
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
|
|
||||||
// Consts
|
// Consts
|
||||||
|
|
||||||
pub const WORLD_SIZE: usize = 32;
|
pub const BLOCK_SIZE : usize = 32; // 32x32x32 block size
|
||||||
pub const NUM_NODES: usize = (WORLD_SIZE*WORLD_SIZE*WORLD_SIZE) as usize;
|
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
|
// Import names
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use byteorder::{ByteOrder, LittleEndian};
|
use byteorder::{ByteOrder, LittleEndian};
|
||||||
use rand::prelude::*;
|
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 struct Nodes {
|
||||||
pub nodes: Box<[u32]>,
|
pub nodes: Box<[u32]>,
|
||||||
@@ -16,7 +16,7 @@ impl Nodes {
|
|||||||
pub fn new(device: &wgpu::Device) -> Self {
|
pub fn new(device: &wgpu::Device) -> Self {
|
||||||
|
|
||||||
// Create texture
|
// 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(
|
let texture = device.create_texture(
|
||||||
&wgpu::TextureDescriptor {
|
&wgpu::TextureDescriptor {
|
||||||
size: size,
|
size: size,
|
||||||
@@ -69,7 +69,7 @@ impl Nodes {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Data
|
// 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);
|
println!("Node buffer size: {} MB", nodes.len() as f32 * 4.0 / 1024.0 / 1024.0);
|
||||||
|
|
||||||
// Done
|
// Done
|
||||||
@@ -78,7 +78,7 @@ impl Nodes {
|
|||||||
|
|
||||||
pub fn write(&mut self, queue: &wgpu::Queue) {
|
pub fn write(&mut self, queue: &wgpu::Queue) {
|
||||||
// Convert 32 bit values to 8 bit
|
// 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);
|
LittleEndian::write_u32_into(&self.nodes, &mut bytes);
|
||||||
|
|
||||||
// Write
|
// Write
|
||||||
|
|||||||
+8
-3
@@ -7,7 +7,8 @@ use buffers::{Nodes, Blocks, Uniform, Raster, Materials};
|
|||||||
mod passes;
|
mod passes;
|
||||||
use passes::RaytracePass;
|
use passes::RaytracePass;
|
||||||
|
|
||||||
pub mod camera; use camera::Camera;
|
pub mod camera;
|
||||||
|
use camera::Camera;
|
||||||
|
|
||||||
|
|
||||||
pub struct Renderer {
|
pub struct Renderer {
|
||||||
@@ -124,7 +125,11 @@ impl Renderer {
|
|||||||
self.brick_buffer.write(&self.queue, *node);
|
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();
|
self.changed_bricks.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +197,7 @@ impl Renderer {
|
|||||||
*/
|
*/
|
||||||
pub fn block_get_id(&mut self, pos: Point3<usize>, apply: bool) -> usize {
|
pub fn block_get_id(&mut self, pos: Point3<usize>, apply: bool) -> usize {
|
||||||
// Get node
|
// 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];
|
let mut node_value = self.node_buffer.nodes[node_idx];
|
||||||
|
|
||||||
// Use new brick if neccesary
|
// Use new brick if neccesary
|
||||||
|
|||||||
Binary file not shown.
@@ -20,7 +20,11 @@ HitResult castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
|
|||||||
vec3 rayInv = 1.0/dir;
|
vec3 rayInv = 1.0/dir;
|
||||||
vec3 raySign = sign(dir);
|
vec3 raySign = sign(dir);
|
||||||
vec3 dist = (pos-origin+0.5 + raySign*0.5) * rayInv;
|
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++)
|
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
|
// Check for bounds
|
||||||
if(pos.x < 0 || pos.y < 0 || pos.z < 0) { break; }
|
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));
|
return HitResult(0, vec3(0,0,0), vec3(0,0,0));
|
||||||
@@ -92,8 +96,8 @@ HitResult castNodes(Ray ray, uint maxSteps)
|
|||||||
pos += incAxis * raySign;
|
pos += incAxis * raySign;
|
||||||
|
|
||||||
// Outside bounds
|
// Outside bounds
|
||||||
//if(pos.x < 0 || pos.y < 0 || pos.z < 0) { break; }
|
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 >= BLOCK_TEX_SIZE || pos.y > BLOCK_TEX_SIZE || pos.z >= NODE_TEX_SIZE) { break; }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Not found
|
// Not found
|
||||||
|
|||||||
@@ -24,9 +24,11 @@ layout(set=3, binding=0) uniform Materials {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Defines
|
// Defines
|
||||||
// #define PI 3.141592
|
#define EPSILON 0.00000001
|
||||||
// #define EPSILON 0.01
|
#define NODE_TEX_SIZE 32
|
||||||
// #define NEAR 0.01
|
#define BLOCK_TEX_SIZE 64
|
||||||
|
#define BLOCK_SIZE 32
|
||||||
|
|
||||||
|
|
||||||
// Include
|
// Include
|
||||||
#include "structs.cginc"
|
#include "structs.cginc"
|
||||||
@@ -66,7 +68,7 @@ vec3 solve()
|
|||||||
if(primary.data > 0)
|
if(primary.data > 0)
|
||||||
{
|
{
|
||||||
vec3 color = applyLighting(primary.pos, primaryRay.dir, primary.normal, primary.data);
|
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
|
// Shadow ray
|
||||||
Ray shadowRay = Ray(hitPos, _SunDir);
|
Ray shadowRay = Ray(hitPos, _SunDir);
|
||||||
|
|||||||
Reference in New Issue
Block a user