readme and first steps for storage buffer raytracing

This commit is contained in:
zomseffen 2024-10-18 17:15:33 +02:00
parent 912659bb52
commit 534f1a109a
7 changed files with 424 additions and 44 deletions

View file

@ -17,9 +17,9 @@ use crate::primitives::drawable::Drawable;
extern crate rand;
use rand::Rng;
const CHUNK_SIZE_EXPONENT: u32 = 10;
const CHUNK_SIZE_EXPONENT: u32 = 9;
const CHUNK_SIZE: usize = (2 as usize).pow(CHUNK_SIZE_EXPONENT);
const MAX_TREE_DEPTH: usize = 8;
const MAX_TREE_DEPTH: usize = 7;
const MIN_CHUNK_SIZE: usize = CHUNK_SIZE / (2 as usize).pow(MAX_TREE_DEPTH as u32);
#[derive(Clone, Debug, Default)]
@ -45,10 +45,10 @@ pub struct Scene {
impl Scene {
pub unsafe fn prepare_data(&mut self, instance: &vulkanalia::Instance, device: &vulkanalia::Device, data: &AppData) -> Result<()> {
let mut rng = rand::thread_rng();
let grid_size = 50; //CHUNK_SIZE as i32;
let grid_size = 512; //CHUNK_SIZE as i32;
// todo store the chunks somewhere (or only use them as intermediary for neighbouthood calculation idc)
let mut oct_tree = OctTree::create(CHUNK_SIZE)?;
let mut oct_tree: OctTree<Cube> = OctTree::create(CHUNK_SIZE)?;
//todo use the 14 vertice box method. Not using geometry shaders seems to be faster... make this a setting?
// have cube elements with a method asking for vertices, while giving a primitive type -> method for preferred primitive type as well as one collecting all primitives
@ -56,7 +56,7 @@ impl Scene {
for y_index in 0..grid_size {
let shade = (rng.gen_range(0..25) as f32) / 100.0;
let cube = Cube {
pos: vec3(x_index as f32, y_index as f32, 0.0),
pos: vec3(x_index as f32, y_index as f32, 5.0),
color: vec3(shade, 1.0, shade),
tex_coord: vec2(0.0, 0.0)
};
@ -65,6 +65,9 @@ impl Scene {
}
}
let mut test = EmptyVolume::from_oct_tree(&oct_tree);
println!("number of empty volumes is {}", test.len());
let oct_tree_iter = OctTreeIter::create(&oct_tree)?;
for item in oct_tree_iter {
let sized_index = self.sized_vertices.len();
@ -120,7 +123,7 @@ impl Scene {
#[derive(Clone, Debug)]
#[allow(non_snake_case)]
struct OctTree {
struct OctTree<T> {
pub child_XYZ: Option<Rc<RefCell<Self>>>,
pub child_xYZ: Option<Rc<RefCell<Self>>>,
pub child_xyZ: Option<Rc<RefCell<Self>>>,
@ -130,15 +133,27 @@ struct OctTree {
pub child_xyz: Option<Rc<RefCell<Self>>>,
pub child_Xyz: Option<Rc<RefCell<Self>>>,
pub blocks: Vec<Option<Cube>>,
pub blocks: Vec<Option<T>>,
size: usize,
}
#[warn(non_snake_case)]
impl OctTree {
impl OctTree<Cube> {
pub fn set_cube(&mut self, cube: Cube) {
let x = cube.pos.x as usize;
let y = cube.pos.y as usize;
let z = cube.pos.z as usize;
assert!(x < self.size, "x value out of range!");
assert!(y < self.size, "y value out of range!");
assert!(z < self.size, "z value out of range!");
self.set_element_internal(cube, x, y, z);
}
}
impl<T: Clone> OctTree<T> {
pub fn create(size: usize) -> Result<Self> {
let mut blocks: Vec<Option<Cube>> = vec![];
let mut blocks: Vec<Option<T>> = vec![];
if size == MIN_CHUNK_SIZE {
for _ in 0..MIN_CHUNK_SIZE {
for _ in 0..MIN_CHUNK_SIZE {
@ -165,17 +180,14 @@ impl OctTree {
})
}
pub fn set_cube(&mut self, cube: Cube) {
let x = cube.pos.x as usize;
let y = cube.pos.y as usize;
let z = cube.pos.z as usize;
pub fn set_element(&mut self, element: T, x: usize, y: usize, z: usize) {
assert!(x < self.size, "x value out of range!");
assert!(y < self.size, "y value out of range!");
assert!(z < self.size, "z value out of range!");
self.set_cube_internal(cube, x, y, z);
self.set_element_internal(element, x, y, z);
}
fn set_cube_internal(&mut self, cube: Cube, x: usize, y: usize, z: usize) {
fn set_element_internal(&mut self, element: T, x: usize, y: usize, z: usize) {
if self.size > MIN_CHUNK_SIZE {
let mid_point = self.size / 2;
if x >= mid_point {
@ -183,11 +195,11 @@ impl OctTree {
if z >= mid_point {
match &self.child_XYZ {
Some(child) => {
child.borrow_mut().set_cube_internal(cube, x - mid_point, y - mid_point, z - mid_point);
child.borrow_mut().set_element_internal(element, x - mid_point, y - mid_point, z - mid_point);
},
None => {
let mut child = OctTree::create(self.size / 2).unwrap();
child.set_cube_internal(cube, x - mid_point, y - mid_point, z - mid_point);
child.set_element_internal(element, x - mid_point, y - mid_point, z - mid_point);
self.child_XYZ = Some(Rc::new(RefCell::new(child)));
}
}
@ -195,11 +207,11 @@ impl OctTree {
else {
match &self.child_XYz {
Some(child) => {
child.borrow_mut().set_cube_internal(cube, x - mid_point, y - mid_point, z);
child.borrow_mut().set_element_internal(element, x - mid_point, y - mid_point, z);
},
None => {
let mut child = OctTree::create(self.size / 2).unwrap();
child.set_cube_internal(cube, x - mid_point, y - mid_point, z);
child.set_element_internal(element, x - mid_point, y - mid_point, z);
self.child_XYz = Some(Rc::new(RefCell::new(child)));
}
}
@ -209,11 +221,11 @@ impl OctTree {
if z >= mid_point {
match &self.child_XyZ {
Some(child) => {
child.borrow_mut().set_cube_internal(cube, x - mid_point, y, z - mid_point);
child.borrow_mut().set_element_internal(element, x - mid_point, y, z - mid_point);
},
None => {
let mut child = OctTree::create(self.size / 2).unwrap();
child.set_cube_internal(cube, x - mid_point, y, z - mid_point);
child.set_element_internal(element, x - mid_point, y, z - mid_point);
self.child_XyZ = Some(Rc::new(RefCell::new(child)));
}
}
@ -221,11 +233,11 @@ impl OctTree {
else {
match &self.child_Xyz {
Some(child) => {
child.borrow_mut().set_cube_internal(cube, x - mid_point, y, z);
child.borrow_mut().set_element_internal(element, x - mid_point, y, z);
},
None => {
let mut child = OctTree::create(self.size / 2).unwrap();
child.set_cube_internal(cube, x - mid_point, y, z);
child.set_element_internal(element, x - mid_point, y, z);
self.child_Xyz = Some(Rc::new(RefCell::new(child)));
}
}
@ -237,11 +249,11 @@ impl OctTree {
if z >= mid_point {
match &self.child_xYZ {
Some(child) => {
child.borrow_mut().set_cube_internal(cube, x, y - mid_point, z - mid_point);
child.borrow_mut().set_element_internal(element, x, y - mid_point, z - mid_point);
},
None => {
let mut child = OctTree::create(self.size / 2).unwrap();
child.set_cube_internal(cube, x, y - mid_point, z - mid_point);
child.set_element_internal(element, x, y - mid_point, z - mid_point);
self.child_xYZ = Some(Rc::new(RefCell::new(child)));
}
}
@ -249,11 +261,11 @@ impl OctTree {
else {
match &self.child_xYz {
Some(child) => {
child.borrow_mut().set_cube_internal(cube, x, y - mid_point, z);
child.borrow_mut().set_element_internal(element, x, y - mid_point, z);
},
None => {
let mut child = OctTree::create(self.size / 2).unwrap();
child.set_cube_internal(cube, x, y - mid_point, z);
child.set_element_internal(element, x, y - mid_point, z);
self.child_xYz = Some(Rc::new(RefCell::new(child)));
}
}
@ -263,11 +275,11 @@ impl OctTree {
if z >= mid_point {
match &self.child_xyZ {
Some(child) => {
child.borrow_mut().set_cube_internal(cube, x, y, z - mid_point);
child.borrow_mut().set_element_internal(element, x, y, z - mid_point);
},
None => {
let mut child = OctTree::create(self.size / 2).unwrap();
child.set_cube_internal(cube, x, y, z - mid_point);
child.set_element_internal(element, x, y, z - mid_point);
self.child_xyZ = Some(Rc::new(RefCell::new(child)));
}
}
@ -275,11 +287,11 @@ impl OctTree {
else {
match &self.child_xyz {
Some(child) => {
child.borrow_mut().set_cube_internal(cube, x, y, z);
child.borrow_mut().set_element_internal(element, x, y, z);
},
None => {
let mut child = OctTree::create(self.size / 2).unwrap();
child.set_cube_internal(cube, x, y, z);
child.set_element_internal(element, x, y, z);
self.child_xyz = Some(Rc::new(RefCell::new(child)));
}
}
@ -288,7 +300,7 @@ impl OctTree {
}
}
else {
self.blocks[z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE + y * MIN_CHUNK_SIZE + x] = Some(cube);
self.blocks[z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE + y * MIN_CHUNK_SIZE + x] = Some(element);
}
}
@ -385,18 +397,107 @@ impl OctTree {
self.blocks[z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE + y * MIN_CHUNK_SIZE + x] = None;
}
}
pub fn get_element(&self, x: usize, y: usize, z: usize) -> Option<T> {
if x >= self.size || y >= self.size || z >= self.size {
return None
}
if self.size > MIN_CHUNK_SIZE {
let mid_point = self.size / 2;
if x >= mid_point {
if y >= mid_point {
if z >= mid_point {
match &self.child_XYZ {
Some(child) => {
child.borrow().get_element(x - mid_point, y - mid_point, z - mid_point)
},
None => None
}
}
else {
match &self.child_XYz {
Some(child) => {
child.borrow().get_element( x - mid_point, y - mid_point, z)
},
None => None
}
}
}
else {
if z >= mid_point {
match &self.child_XyZ {
Some(child) => {
child.borrow().get_element(x - mid_point, y, z - mid_point)
},
None => None
}
}
else {
match &self.child_Xyz {
Some(child) => {
child.borrow().get_element(x - mid_point, y, z)
},
None => None
}
}
}
}
else {
if y >= mid_point {
if z >= mid_point {
match &self.child_xYZ {
Some(child) => {
child.borrow().get_element(x, y - mid_point, z - mid_point)
},
None => None
}
}
else {
match &self.child_xYz {
Some(child) => {
child.borrow().get_element(x, y - mid_point, z)
},
None => None
}
}
}
else {
if z >= mid_point {
match &self.child_xyZ {
Some(child) => {
child.borrow().get_element(x, y, z - mid_point)
},
None => None
}
}
else {
match &self.child_xyz {
Some(child) => {
child.borrow().get_element(x, y, z)
},
None => None
}
}
}
}
}
else {
self.blocks[z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE + y * MIN_CHUNK_SIZE + x].clone()
}
}
}
struct OctTreeIter<'a> {
iter_x: usize,
iter_y: usize,
iter_z: usize,
todo: Vec<Rc<RefCell<OctTree>>>,
chunk: &'a OctTree
todo: Vec<Rc<RefCell<OctTree<Cube>>>>,
chunk: &'a OctTree<Cube>
}
impl<'a> OctTreeIter<'a> {
pub fn create(chunk: &'a OctTree) -> Result<Self> {
pub fn create(chunk: &'a OctTree<Cube>) -> Result<Self> {
let mut out = Self {
iter_x: 0,
iter_y: 0,
@ -408,7 +509,7 @@ impl<'a> OctTreeIter<'a> {
Ok(out)
}
fn add_todo(&mut self, oct_tree: &OctTree) {
fn add_todo(&mut self, oct_tree: &OctTree<Cube>) {
match &oct_tree.child_XYZ {
Some(child) => {
self.todo.push(child.clone());
@ -500,4 +601,207 @@ impl<'a> Iterator for OctTreeIter<'a> {
self.add_todo(&self.chunk);
None
}
}
}
struct EmptyVolume {
pub memory_start: usize,
pub size_x: usize,
pub size_y: usize,
pub size_z: usize,
pub position: Vector3<usize>,
pub color_front: Vec<Vector3<u8>>,
pub color_back: Vec<Vector3<u8>>,
pub color_top: Vec<Vector3<u8>>,
pub color_bottom: Vec<Vector3<u8>>,
pub color_left: Vec<Vector3<u8>>,
pub color_right: Vec<Vector3<u8>>,
pub roughness_front: Vec<Vector3<u8>>,
pub roughness_back: Vec<Vector3<u8>>,
pub roughness_top: Vec<Vector3<u8>>,
pub roughness_bottom: Vec<Vector3<u8>>,
pub roughness_left: Vec<Vector3<u8>>,
pub roughness_right: Vec<Vector3<u8>>,
pub neighbor_front: Vec<Rc<RefCell<Self>>>,
pub neighbor_back: Vec<Rc<RefCell<Self>>>,
pub neighbor_top: Vec<Rc<RefCell<Self>>>,
pub neighbor_bottom: Vec<Rc<RefCell<Self>>>,
pub neighbor_left: Vec<Rc<RefCell<Self>>>,
pub neighbor_right: Vec<Rc<RefCell<Self>>>,
}
impl EmptyVolume {
pub fn contains(&self, pos: &Vector3<usize>) -> bool {
self.position[0] + self.size_x > pos[0] && pos[0] >= self.position[0] &&
self.position[1] + self.size_y > pos[1] && pos[1] >= self.position[1] &&
self.position[2] + self.size_z > pos[2] && pos[2] >= self.position[2]
}
pub fn from_oct_tree(tree: &OctTree<Cube>) -> Vec<Rc<RefCell<EmptyVolume>>> {
let mut volumes: Vec<Rc<RefCell<EmptyVolume>>> = vec![];
let mut neighbors: OctTree<Rc<RefCell<EmptyVolume>>> = OctTree::create(tree.size).unwrap();
// iterate over all block positions in the oct tree
for x_index in 0..tree.size {
for y_index in 0..tree.size {
for z_index in 0..tree.size {
// check if there is a block at that position
let query_result = tree.get_element(x_index, y_index, z_index);
match query_result {
Some(cube) => {
//if so do nothing
},
None => {
//if not check that it is not already inside of a volume
let mut contained = false;
for volume in &volumes {
if volume.borrow().contains(&Vector3{x: x_index, y: y_index, z: z_index}) {
contained = true;
}
}
if contained {
// abort if it is already covered
continue;
}
println!("new starting pos: {}, {}, {}", x_index, y_index, z_index);
println!("start growing volume x");
let mut x_size = 0;
let mut grow = true;
while grow {
grow = tree.get_element(x_index + x_size + 1, y_index, z_index).is_none() && neighbors.get_element(x_index + x_size + 1, y_index, z_index).is_none();
grow &= (x_index + x_size + 1) < tree.size;
if grow {
x_size += 1;
}
}
println!("start growing volume y");
let mut y_size = 0;
grow = true;
while grow {
grow &= (y_index + y_size + 1) < tree.size;
if grow {
for x in 0..x_size {
grow &= tree.get_element(x_index + x, y_index + y_size + 1, z_index).is_none() &&
neighbors.get_element(x_index + x, y_index + y_size + 1, z_index).is_none();
if !grow {
break;
}
}
}
if grow {
y_size += 1;
}
}
println!("start growing volume z");
let mut z_size = 0;
grow = true;
while grow {
grow &= (z_index + z_size + 1) < tree.size;
if grow {
for x in 0..x_size {
for y in 0..y_size {
grow &= tree.get_element(x_index + x, y_index + y, z_index + z_size + 1).is_none() &&
neighbors.get_element(x_index + x, y_index + y, z_index + z_size + 1).is_none();
if !grow {
break;
}
}
if !grow {
break;
}
}
}
if grow {
z_size += 1;
}
}
println!("final size: {}, {}, {}", x_size+1, y_size+1, z_size+1);
// create new empty volume
let new_volume = EmptyVolume {
memory_start: 0,
size_x: x_size + 1,
size_y: y_size + 1,
size_z: z_size + 1,
position: Vector3{x: x_index, y: y_index, z: z_index},
color_front: vec![],
color_back: vec![],
color_top: vec![],
color_bottom: vec![],
color_left: vec![],
color_right: vec![],
roughness_front: vec![],
roughness_back: vec![],
roughness_top: vec![],
roughness_bottom: vec![],
roughness_left: vec![],
roughness_right: vec![],
neighbor_front: vec![],
neighbor_back: vec![],
neighbor_top: vec![],
neighbor_bottom: vec![],
neighbor_left: vec![],
neighbor_right: vec![],
};
println!("adding neighbor references");
//fill in info in the neighbor octtree
let reference = Rc::new(RefCell::new(new_volume));
for x in 0..x_size+1 {
for y in 0..y_size+1 {
for z in 0..z_size+1 {
if x == 0 || x == x_size || y == 0 || y == y_size || z==0 || z == z_size {
neighbors.set_element(reference.clone(), reference.borrow().position.x + x, reference.borrow().position.y + y, reference.borrow().position.z + z)
}
}
}
}
println!("new volume done");
//push to the list
volumes.push(reference);
}
}
}
}
}
volumes
}
}
#[cfg(test)]
mod test {
use cgmath::Vector2;
use super::*;
#[test]
fn test_oct_tree(){
let mut test_tree: OctTree<Cube> = OctTree::create(512).unwrap();
let test_cube = Cube{color: Vector3 { x: 1.0, y: 0.0, z: 0.0 }, pos: Vector3 { x: 5.0, y: 2.0, z: 10.0 }, tex_coord: Vector2{x: 0.0, y: 0.0}};
test_tree.set_cube(test_cube.clone());
let cube_result = test_tree.get_element(5, 2, 10).unwrap();
let cube_result2 = test_tree.get_element(300, 2, 10);
assert_eq!(test_cube, cube_result);
assert_eq!(cube_result2, None);
let test_iter = OctTreeIter::create(&test_tree).unwrap();
let mut count = 0;
for result in test_iter {
if let Some(_) = result {
count += 1;
}
}
assert_eq!(count, 1);
}
}