From 3f4656939cc641708cb36b62d1add09b044bf25f Mon Sep 17 00:00:00 2001 From: zomseffen Date: Sat, 11 Jan 2025 14:12:24 +0100 Subject: [PATCH] halfway to raytracing --- shaders/compiled/frag_rt_quad.spv | Bin 7124 -> 11068 bytes shaders/rt_quad.frag | 120 ++++++++++++++++++++++++++---- src/app_data.rs | 1 + src/main.rs | 1 + src/scene/empty_volume.rs | 79 +++++++++++++++----- src/scene/light.rs | 34 +++++++++ src/scene/mod.rs | 29 +++++--- 7 files changed, 222 insertions(+), 42 deletions(-) create mode 100644 src/scene/light.rs diff --git a/shaders/compiled/frag_rt_quad.spv b/shaders/compiled/frag_rt_quad.spv index e586d97e2f74618e169422459d129632b29e3129..15b4f814b512aeb94222088c611fa16f462fe90c 100644 GIT binary patch literal 11068 zcmZvh2b`T%orW(mlSFzzniMA>f`AA}l@Cv3&ilUqa{4*9c)nt5yfKWu#|l zRKrU!%%SWyCJvZB>^P)W zC+%-Vy{cyzyiIE-@MO=(IL%gm-2)S2Bi)m|{d8eybYRT5RnvzbXLu)bvV*Z>!_y=E zbhc+=3X0y@noT{?Gda~i(LFvk*=}b?w1LV#tF=4zVE+_j8(cZ1%h1%&*l4>~#_xu` zZ5tZx>tBu1(b@xzejY|u@vb&MJbb@4e^vN_ZGJ?X&ukqD=$V-4S<~IuKiNC6p~H^j z)!3Fg$}( z+Wzioq$+OVpW0T%wivKtY-)-HXs512&ADu|wpCTm<=BS%2c|ZSYPYvyGd3GE=Su2{ zYCSe-uCk46&PJed&I{XQwArAxHJh{5aqFn`De8i{5g5g@U#Qa{yg<~&qR;wW1n{B9vSIb z-96ktIyl9G>a!c#FlP}L_>9(G@b(H%j7<-&9POW+taR_Hfr*~MWz~6JS{-Noy}#6# zROfkdeGWRRJ8ht+cW895-Oj%FZR|@&Yrh8He-l2Vbuhf%Y1NIB{vD5AjiK6$qthAN zN$Bl6A=-@AT(tWBX+}s;eVLApR`YWP`o@0GsP5@87Mqb)w^F-b z9j({lx4M0uRdMH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FxWw_b~Eb49qM_v7q+X2>RUEg_m_$uJE z?k2ce7Z!ClgQKqg$n6H}vu-D&Jbc%J)4IKIwH_?$_JO0W{>ZI@^;y?DNFKiDgVVYL zaJ3p1b?e}$t3PrF!TPM*&nOSy3&3gJt#Gw1Sk%1^9Ch_a?hC>CtUJUg58sQxY26#) zYByj}cN;kB>W|!;!1}D~-6{{?i@|B#m%!C_U{Uv_;HaxVa$g44XWi|L^6 zTb?>jb@fN?tHAoKyOU8KzE^{nB>Xkt<=BqqUHMwDG3w^;VpNO#*MZv;{(A6g zEb`v~Hb&k2-Hd9H|3pkDgxw+xKoPdhP?K zJ--ZBy8?@z_kh)lJ->qHd5fN31skg#J--IFuRhP$os4SH|Lfo-3I7JTJ>lO3uTA*3 z!0QwKZE*U2dtnV`4tv120eb_ra?Z{sXY@f5iU~ z+?ntnf%_BwW3caA%;zUydsx?6=9^=j^Etq1Ow|7=*!_v$u|ESlXZ48rIauBNgN*X< z{Q{ik-w(Hkdc^z^d_QLXAx3%leg$^VqW`bKlQG}-i1`gzJ@{|I)`@fRJFqe8aV~xj zwzj^g{RgnMJ!ettk6`u4`xDq)_4r=-Gk6(x0j584{{q(Mx(+kS!}nLP`@FY#kNpj- z=DHpv{vV8f9?DuWeSgQ){4FN-cijU`t{!#&1-6$xk1)!Y(qk;+gV=aX-F&&f3;z!) CgW2f- 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