diff --git a/shaders/compiled/frag_rt_quad.spv b/shaders/compiled/frag_rt_quad.spv index e586d97..15b4f81 100644 Binary files a/shaders/compiled/frag_rt_quad.spv and b/shaders/compiled/frag_rt_quad.spv differ diff --git a/shaders/rt_quad.frag b/shaders/rt_quad.frag index 7ffe0e8..495d707 100644 --- a/shaders/rt_quad.frag +++ b/shaders/rt_quad.frag @@ -18,11 +18,11 @@ uvec4 unpack_color(uint val) { uint val3 = (val << 16) >> 24; uint val4 = (val << 24) >> 24; - return uvec4(val1, val2, val3, val4); + return uvec4(val4, val3, val2, val1); } -uvec4 sample_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) { - uint array_descr_start = volume_start + 3; +uint sample_neighbor_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) { + uint array_descr_start = volume_start + 6 + scene_info.infos[0]; uint color_array_start = array_descr_start + 24; uint top_color_size_u = scene_info.infos[array_descr_start]; @@ -46,7 +46,7 @@ uvec4 sample_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) { uint top_neighbor_size_u = scene_info.infos[array_descr_start + 12]; uint top_neighbor_size_v = scene_info.infos[array_descr_start + 13]; - /*uint bottom_neighbor_size_u = scene_info.infos[array_descr_start + 14]; + uint bottom_neighbor_size_u = scene_info.infos[array_descr_start + 14]; uint bottom_neighbor_size_v = scene_info.infos[array_descr_start + 15]; uint left_neighbor_size_u = scene_info.infos[array_descr_start + 16]; @@ -59,7 +59,58 @@ uvec4 sample_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) { uint front_neighbor_size_v = scene_info.infos[array_descr_start + 21]; uint back_neighbor_size_u = scene_info.infos[array_descr_start + 22]; - uint back_neighbor_size_v = scene_info.infos[array_descr_start + 23];*/ + uint back_neighbor_size_v = scene_info.infos[array_descr_start + 23]; + + uint top_color_size = top_color_size_u * top_color_size_v; + uint bottom_color_size = bottom_color_size_u * bottom_color_size_v; + uint left_color_size = left_color_size_u * left_color_size_v; + uint right_color_size = right_color_size_u * right_color_size_v; + uint front_color_size = front_color_size_u * front_color_size_v; + uint back_color_size = back_color_size_u * back_color_size_v; + + uint color_array_end = color_array_start + top_color_size + bottom_color_size + left_color_size + right_color_size + front_color_size + back_color_size; + + uint top_neighbor_size = top_neighbor_size_u * top_neighbor_size_v; + uint bottom_neighbor_size = bottom_neighbor_size_u * bottom_neighbor_size_v; + uint left_neighbor_size = left_neighbor_size_u * left_neighbor_size_v; + uint right_neighbor_size = right_neighbor_size_u * right_neighbor_size_v; + uint front_neighbor_size = front_neighbor_size_u * front_neighbor_size_v; + uint back_neighbor_size = back_neighbor_size_u * back_neighbor_size_v; + + // maybe do an array solution for this as well + uint array_start = color_array_end + uint(f > 0) * top_neighbor_size + uint(f > 1) * bottom_neighbor_size + uint(f > 2) * left_neighbor_size + uint(f > 3) * right_neighbor_size + uint(f > 4) * front_neighbor_size; + uint us[6] = {top_neighbor_size_u, bottom_neighbor_size_u, left_neighbor_size_u, right_neighbor_size_u, front_neighbor_size_u, back_neighbor_size_u}; + uint vs[6] = {top_neighbor_size_v, bottom_neighbor_size_v, left_neighbor_size_v, right_neighbor_size_v, front_neighbor_size_v, back_neighbor_size_v}; + uint u_size = us[f]; + uint v_size = vs[f]; + uint value = scene_info.infos[array_start + raster_pos.x * v_size * uint(u_size > 1) + raster_pos.y * uint(v_size > 1)]; + return value; +} + +uvec4 sample_color_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) { + uint array_descr_start = volume_start + 6 + scene_info.infos[0]; + uint color_array_start = array_descr_start + 24; + + uint top_color_size_u = scene_info.infos[array_descr_start]; + uint top_color_size_v = scene_info.infos[array_descr_start + 1]; + + uint bottom_color_size_u = scene_info.infos[array_descr_start + 2]; + uint bottom_color_size_v = scene_info.infos[array_descr_start + 3]; + + uint left_color_size_u = scene_info.infos[array_descr_start + 4]; + uint left_color_size_v = scene_info.infos[array_descr_start + 5]; + + uint right_color_size_u = scene_info.infos[array_descr_start + 6]; + uint right_color_size_v = scene_info.infos[array_descr_start + 7]; + + uint front_color_size_u = scene_info.infos[array_descr_start + 8]; + uint front_color_size_v = scene_info.infos[array_descr_start + 9]; + + uint back_color_size_u = scene_info.infos[array_descr_start + 10]; + uint back_color_size_v = scene_info.infos[array_descr_start + 11]; + + uint top_neighbor_size_u = scene_info.infos[array_descr_start + 12]; + uint top_neighbor_size_v = scene_info.infos[array_descr_start + 13]; uint top_size = top_color_size_u * top_color_size_v; uint bottom_size = bottom_color_size_u * bottom_color_size_v; @@ -74,21 +125,64 @@ uvec4 sample_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) { uint vs[6] = {top_color_size_v, bottom_color_size_v, left_color_size_v, right_color_size_v, front_color_size_v, back_color_size_v}; uint u_size = us[f]; uint v_size = vs[f]; - uint value = scene_info.infos[array_start + raster_pos.x * v_size + raster_pos.y]; - - if (top_color_size_v == 0) { - return uvec4(255, 0, 0, 255); - } - + uint value = scene_info.infos[array_start + raster_pos.x * v_size * uint(u_size > 1) + raster_pos.y * uint(v_size > 1)]; return unpack_color(value); } +vec3 get_light_position(uint light_index) { + return vec3(float(scene_info.infos[light_index]), float(scene_info.infos[light_index + 1]), float(scene_info.infos[light_index + 2])); +} + +vec4 get_light_color(uint light_index) { + return vec4(float(scene_info.infos[light_index + 3]) / 255.0, float(scene_info.infos[light_index + 4]) / 255.0, float(scene_info.infos[light_index + 5]) / 255.0, 1.0); +} + void main() { uint max_length = scene_info.infos[0]; uint last = scene_info.infos[max_length]; - uvec4 color_roughness = sample_from_scene_info(fragVolumeStart, fragRasterPos, facing); - outColor = vec4(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0, 1); + uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, fragRasterPos, facing); + vec4 color_sample = vec4(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0, 1); + + uint max_light_num = scene_info.infos[0]; + uint light_num = 0; + uint volume_index = fragVolumeStart; + uint light_index = scene_info.infos[fragVolumeStart + 3]; + vec3 light_direction = get_light_position(light_index) - origPosition; + vec4 light_color = get_light_color(light_index); + + bool x_pos = light_direction.x > 0.0; + bool x_null = light_direction.x == 0.0; + bool y_pos = light_direction.y > 0.0; + bool y_null = light_direction.y == 0.0; + bool z_pos = light_direction.z > 0.0; + bool z_null = light_direction.z == 0.0; + + uint max_iterations = max_light_num * 20; + for (int i = 0; i < max_iterations; i++) { + float x_border = float(scene_info.infos[volume_index + 0] + scene_info.infos[volume_index + 3] * uint(x_pos)); + float y_border = float(scene_info.infos[volume_index + 1] + scene_info.infos[volume_index + 4] * uint(y_pos)); + float z_border = float(scene_info.infos[volume_index + 2] + scene_info.infos[volume_index + 5] * uint(z_pos)); + + // 2 is way behind the light position and should result in no collision being detected + float x_factor = 2.0; + float y_factor = 2.0; + float z_factor = 2.0; + if (!x_null) { + x_factor = (x_border - origPosition.x) / light_direction.x; + } + if (!y_null) { + float y_factor = (y_border - origPosition.y) / light_direction.y; + } + if (!z_null) { + float z_factor = (z_border - origPosition.z) / light_direction.z; + } + + + } + + + outColor = color_sample; /*if (scene_info.infos[1] == 16) { outColor = vec4(0, 1, 0, 1); diff --git a/src/app_data.rs b/src/app_data.rs index b30b6a1..9a6e5c6 100644 --- a/src/app_data.rs +++ b/src/app_data.rs @@ -60,4 +60,5 @@ pub struct AppData { pub topology: vk::PrimitiveTopology, pub scene_rt_memory_size: u64, + pub num_lights_per_volume: u32, } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8a8508c..b10bddb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -178,6 +178,7 @@ impl App { let entry = Entry::new(loader).map_err(|b| anyhow!("{}", b))?; let mut data = app_data::AppData::default(); data.use_geometry_shader = false; + data.num_lights_per_volume = 1; let mut scene_handler = scene::Scene::default(); //load_model::load_model(&mut data)?; diff --git a/src/scene/empty_volume.rs b/src/scene/empty_volume.rs index ad3d5e6..cbe73bb 100644 --- a/src/scene/empty_volume.rs +++ b/src/scene/empty_volume.rs @@ -10,6 +10,8 @@ use crate::primitives::cube::Cube; use crate::primitives::quad::Quad; use crate::scene::oct_tree::OctTree; +use super::light::PointLight; + pub struct EmptyVolume { pub memory_start: usize, @@ -47,7 +49,7 @@ impl EmptyVolume { 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] } - + // MARK: From Oct Tree pub fn from_oct_tree(tree: &OctTree) -> (Vec>>, OctTree>>) { // todo: ppotentially use a child exist check while going through the oct tree to find some obvios starting empty volumes. Will still need to check for possible expansions though let mut volumes: Vec>> = vec![]; @@ -80,6 +82,7 @@ impl EmptyVolume { } println!("new starting pos: {}, {}, {}", x_index, y_index, z_index); println!("start growing volume x"); + // MARK: Start new Volume let mut x_size = 0; let mut grow = true; while grow { @@ -160,7 +163,7 @@ impl EmptyVolume { neighbor_front: vec![], }; println!("adding neighbor references"); - //fill in info in the neighbor octtree + // MARK: 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 { @@ -200,7 +203,7 @@ impl EmptyVolume { let x_max_pos = reference.borrow().position.x + reference.borrow().size_x; let y_max_pos = reference.borrow().position.y + reference.borrow().size_y; let z_max_pos = reference.borrow().position.z + reference.borrow().size_z; - // bottom face of the volume + // MARK: bottom face of the volume let mut bottom_colors = vec![]; let mut bottom_roughness = vec![]; let mut bottom_elements_num = 0; @@ -226,7 +229,7 @@ impl EmptyVolume { reference.borrow_mut().color_bottom= vec![]; reference.borrow_mut().roughness_bottom= vec![]; } - // top face of the volume + // MARK: top face of the volume let mut top_colors = vec![]; let mut top_roughness = vec![]; let mut top_elements_num = 0; @@ -253,7 +256,7 @@ impl EmptyVolume { reference.borrow_mut().roughness_top= vec![]; } - // back face of the volume + // MARK: back face of the volume let mut back_colors = vec![]; let mut back_roughness = vec![]; let mut back_elements_num = 0; @@ -280,7 +283,7 @@ impl EmptyVolume { reference.borrow_mut().roughness_back= vec![]; } - // front face of the volume + // MARK: front face of the volume let mut front_colors = vec![]; let mut front_roughness = vec![]; let mut front_elements_num = 0; @@ -307,7 +310,7 @@ impl EmptyVolume { reference.borrow_mut().roughness_front= vec![]; } - // front face of the volume + // MARK: left face of the volume let mut left_colors = vec![]; let mut left_roughness = vec![]; let mut left_elements_num = 0; @@ -334,7 +337,7 @@ impl EmptyVolume { reference.borrow_mut().roughness_left= vec![]; } - // back face of the volume + // MARK: right face of the volume let mut right_colors = vec![]; let mut right_roughness = vec![]; let mut right_elements_num = 0; @@ -370,6 +373,7 @@ impl EmptyVolume { } } println!("add the neighbor linkage for all the volumes of the oct tree"); + // MARK: Neighbor Linkage for reference in volumes.iter_mut() { let x_min_pos; if reference.borrow().position.x == 0 { @@ -398,7 +402,7 @@ impl EmptyVolume { let x_max_pos = reference.borrow().position.x + reference.borrow().size_x; let y_max_pos = reference.borrow().position.y + reference.borrow().size_y; let z_max_pos = reference.borrow().position.z + reference.borrow().size_z; - // bottom face of the volume + // MARK: bottom face of the volume let mut bottom_neighbors = vec![]; let mut bottom_elements_num = 0; if z_min_pos != 0 { @@ -420,7 +424,7 @@ impl EmptyVolume { else { reference.borrow_mut().neighbor_bottom = vec![None]; } - // top face of the volume + // MARK: top face of the volume let mut top_neighbors = vec![]; let mut top_elements_num = 0; for x in 0..reference.borrow().size_x { @@ -441,7 +445,7 @@ impl EmptyVolume { reference.borrow_mut().neighbor_top = vec![None]; } - // back face of the volume + // MARK: back face of the volume let mut back_neighbors = vec![]; let mut back_elements_num = 0; for x in 0..reference.borrow().size_x { @@ -462,7 +466,7 @@ impl EmptyVolume { reference.borrow_mut().neighbor_back = vec![None]; } - // front face of the volume + // MARK: front face of the volume let mut front_neighbors = vec![]; let mut front_elements_num = 0; if y_min_pos != 0{ @@ -485,7 +489,7 @@ impl EmptyVolume { reference.borrow_mut().neighbor_front = vec![None]; } - // left face of the volume + // MARK: left face of the volume let mut left_neighbors = vec![]; let mut left_elements_num = 0; if x_min_pos != 0 { @@ -508,7 +512,7 @@ impl EmptyVolume { reference.borrow_mut().neighbor_left = vec![None]; } - // right face of the volume + // MARK: right face of the volume let mut right_neighbors = vec![]; let mut right_elements_num = 0; for y in 0..reference.borrow().size_y { @@ -532,7 +536,7 @@ impl EmptyVolume { println!("volume creation took {} s", start_time.elapsed().as_millis() as f32 / 1000.0); (volumes, neighbors) } - + // MARK: To Quads pub fn to_quads(&self) -> Vec { let mut quads = vec![]; let float_pos = Vector3 {x: self.position.x as f32, y: self.position.y as f32, z: self.position.z as f32}; @@ -686,10 +690,12 @@ impl EmptyVolume { } quads } - - pub fn get_buffer_mem_size(&self) -> u32 { + // MARK: Get Buffer Mem Size + pub fn get_buffer_mem_size(&self, light_number: u32) -> u32 { let mut mem_size: u32 = 0; + mem_size += 3; //pos mem_size += 3; //max sizes + mem_size += light_number; // light references mem_size += 12; //color/roughness buffer sizes, 2 values each mem_size += 12; //neighbor buffer sizes, 2 values each @@ -710,9 +716,16 @@ impl EmptyVolume { mem_size } - - pub fn insert_into_memory(&self, mut v: Vec) -> Vec { + // MARK: insert into Memory + pub fn insert_into_memory(&self, mut v: Vec, light_number: u32, lights: &Vec) -> Vec { let mut mem_index = self.memory_start; + //pos + v[mem_index] = self.position.x as u32; + mem_index += 1; + v[mem_index] = self.position.y as u32; + mem_index += 1; + v[mem_index] = self.position.y as u32; + mem_index += 1; //max sizes v[mem_index] = self.size_x as u32; mem_index += 1; @@ -720,6 +733,12 @@ impl EmptyVolume { mem_index += 1; v[mem_index] = self.size_z as u32; mem_index += 1; + //Todo: insert lights + let selected_lights = self.select_lights(lights, light_number); + for light in selected_lights { + v[mem_index] = light; + mem_index += 1; + } //color/roughness buffer sizes, 2 values each if self.color_top.len() > 0 { v[mem_index] = self.size_x as u32; @@ -1014,4 +1033,26 @@ impl EmptyVolume { println!("last memory index of volume was {}, equivalent to {}kB", mem_index, mem_index * 32 / 8 / 1024); v } + + pub fn select_lights(&self, lights: &Vec, light_number: u32) -> Vec { + let center = self.position + Vector3{x: self.size_x / 2, y: self.size_y / 2, z: self.size_z / 2}; + let mut weighted_indices = vec![]; + for light in lights { + let weight = light.weighted_distance(center); + weighted_indices.push((weight, light.memory_start)); + } + weighted_indices.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap()); + + let mut out_index = vec![]; + for index in 0..weighted_indices.len() { + out_index.push(weighted_indices[weighted_indices.len() - (index + 1)].1 as u32); + if out_index.len() == light_number as usize { + break; + } + } + while out_index.len() < light_number as usize { + out_index.push(0); + } + out_index + } } \ No newline at end of file diff --git a/src/scene/light.rs b/src/scene/light.rs new file mode 100644 index 0000000..a83b754 --- /dev/null +++ b/src/scene/light.rs @@ -0,0 +1,34 @@ +use cgmath::{InnerSpace, MetricSpace, Vector3}; + +use crate::vertex; + +#[derive(Clone, Debug, PartialEq)] +pub struct PointLight{ + pub pos: vertex::Vec3, + pub color: vertex::Vec3, + pub memory_start: usize, +} + +impl PointLight { + pub fn get_buffer_mem_size(&self) -> u32 { + 4 * 3 + 4 * 3 + } + + pub fn insert_into_memory(&self, mut v: Vec) -> Vec { + v[self.memory_start] = self.pos.x as u32; + v[self.memory_start + 1] = self.pos.y as u32; + v[self.memory_start + 2] = self.pos.z as u32; + + v[self.memory_start + 3] = self.color.x as u32; + v[self.memory_start + 4] = self.color.y as u32; + v[self.memory_start + 5] = self.color.z as u32; + v + } + + pub fn weighted_distance(&self, center: Vector3) -> f32 { + let distance = self.pos.distance(vertex::Vec3{x: center.x as f32, y: center.y as f32, z: center.z as f32}); + let light_intensity = self.color.magnitude(); + + light_intensity / distance.powf(2.0) + } +} \ No newline at end of file diff --git a/src/scene/mod.rs b/src/scene/mod.rs index 9103b37..7d710ff 100644 --- a/src/scene/mod.rs +++ b/src/scene/mod.rs @@ -1,5 +1,6 @@ mod oct_tree; mod empty_volume; +mod light; use anyhow::Ok; use vulkanalia::prelude::v1_0::*; @@ -10,6 +11,7 @@ use cgmath::{vec2, vec3}; use std::cell::RefCell; use std::rc::Rc; +use crate::app_data; use crate::app_data::AppData; use crate::buffer; use crate::vertex; @@ -17,6 +19,7 @@ use crate::primitives::cube::Cube; use crate::primitives::drawable::Drawable; use crate::scene::oct_tree::{OctTree, OctTreeIter, CHUNK_SIZE}; use crate::scene::empty_volume::EmptyVolume; +use crate::scene::light::PointLight; extern crate rand; use rand::Rng; @@ -50,6 +53,8 @@ pub struct Scene { pub index_buffer_memory_quad: vk::DeviceMemory, pub rt_memory: Vec, + + pub point_lights: Vec, } impl Scene { @@ -82,6 +87,8 @@ impl Scene { oct_tree.set_cube(cube.clone()); + self.point_lights.push(PointLight { pos: vec3(11.0, 11.0, 11.0), color: vec3(1.0, 1.0, 1.0), memory_start: 0 }); + let mut empty_volumes: Vec>>; (empty_volumes, _) = EmptyVolume::from_oct_tree(&oct_tree); println!("number of empty volumes is {}", empty_volumes.len()); @@ -112,9 +119,14 @@ impl Scene { } let mut memory_index = 1; // zero should be the location for the overall length (also will be the invalid memory allocation for pointing to a nonexistant neighbor) + for light in &mut self.point_lights { + light.memory_start = memory_index; + memory_index += light.get_buffer_mem_size() as usize; + } + for volume in &empty_volumes { volume.borrow_mut().memory_start = memory_index; - memory_index += volume.borrow().get_buffer_mem_size() as usize; + memory_index += volume.borrow().get_buffer_mem_size(data.num_lights_per_volume) as usize; } for volume in &empty_volumes { @@ -124,18 +136,15 @@ impl Scene { } } println!("Memory size is {} kB, max indes is {}", memory_index * 32 / 8 /1024 + 1, memory_index); - let mut volume_vec = vec![memory_index as u32; memory_index]; + let mut volume_vec = vec![data.num_lights_per_volume; memory_index]; for volume in &empty_volumes { - volume_vec = volume.borrow().insert_into_memory(volume_vec); + volume_vec = volume.borrow().insert_into_memory(volume_vec, data.num_lights_per_volume, &self.point_lights); + } + for light in &self.point_lights { + volume_vec = light.insert_into_memory(volume_vec); } println!("volume_vec print {:?}", volume_vec); - let mut empty_count = 0; - for element in &volume_vec { - if *element == memory_index as u32 { - empty_count += 1; - } - } - println!("empty elements count is {}", empty_count); + self.rt_memory = volume_vec; data.scene_rt_memory_size = (self.rt_memory.len() * 4) as u64; // size of the needed buffer size in bytes