void tracing for spot lights

This commit is contained in:
zomseffen 2025-01-14 16:01:18 +01:00
parent dd568a75e3
commit 4c4b8288d5
5 changed files with 55 additions and 81 deletions

Binary file not shown.

View file

@ -109,9 +109,6 @@ uvec4 sample_color_from_scene_info(uint volume_start, uvec2 raster_pos, uint f)
uint back_color_size_u = scene_info.infos[array_descr_start + 10]; 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 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 top_size = top_color_size_u * top_color_size_v;
uint bottom_size = bottom_color_size_u * bottom_color_size_v; uint bottom_size = bottom_color_size_u * bottom_color_size_v;
uint left_size = left_color_size_u * left_color_size_v; uint left_size = left_color_size_u * left_color_size_v;
@ -142,28 +139,32 @@ void main() {
uint last = scene_info.infos[max_length]; uint last = scene_info.infos[max_length];
uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, fragRasterPos, facing); 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); vec4 orig_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 max_light_num = scene_info.infos[0];
uint light_num = 0; uint light_num = 0;
uint volume_index = fragVolumeStart; uint volume_index = fragVolumeStart;
uint volume_pos_x = scene_info.infos[volume_index + 0]; uint volume_pos_x = scene_info.infos[volume_index + 0];
uint volume_pos_y = scene_info.infos[volume_index + 1]; uint volume_pos_y = scene_info.infos[volume_index + 1];
uint volume_pos_z = scene_info.infos[volume_index + 2]; uint volume_pos_z = scene_info.infos[volume_index + 2];
uint light_index = scene_info.infos[volume_index + 6]; uint light_index = scene_info.infos[volume_index + 6];
vec3 light_direction = get_light_position(light_index) - origPosition; vec3 light_direction = get_light_position(light_index) - origPosition;
vec3 light_color = get_light_color(light_index); vec3 light_color = get_light_color(light_index);
bool x_pos = light_direction.x > 0.0; bool x_pos = light_direction.x > 0.0;
bool x_null = (light_direction.x == 0.0); bool x_null = (light_direction.x == 0.0);
bool y_pos = light_direction.y > 0.0; bool y_pos = light_direction.y > 0.0;
bool y_null = (light_direction.y == 0.0); bool y_null = (light_direction.y == 0.0);
bool z_pos = light_direction.z > 0.0; bool z_pos = light_direction.z > 0.0;
bool z_null = (light_direction.z == 0.0); bool z_null = (light_direction.z == 0.0);
vec3 color_sum = vec3(0.0, 0.0, 0.0) + (color_sample.xyz * 0.01); vec3 color_sum = vec3(0.0, 0.0, 0.0) + (orig_color_sample.xyz * 0.01);
uint max_iterations = 2; //max_light_num * 20; uint max_iterations = max_light_num * 20;
for (int i = 0; i < max_iterations; i++) { for (int i = 0; i < max_iterations; i++) {
float x_border = float(volume_pos_x + (scene_info.infos[volume_index + 3]) * uint(x_pos)) - 0.5; float x_border = float(volume_pos_x + (scene_info.infos[volume_index + 3]) * uint(x_pos)) - 0.5;
float y_border = float(volume_pos_y + (scene_info.infos[volume_index + 4]) * uint(y_pos)) - 0.5; float y_border = float(volume_pos_y + (scene_info.infos[volume_index + 4]) * uint(y_pos)) - 0.5;
@ -185,9 +186,9 @@ void main() {
z_factor = (z_border - origPosition.z) / light_direction.z; z_factor = (z_border - origPosition.z) / light_direction.z;
} }
if ((x_factor >= 0.999 && y_factor >= 0.999 && z_factor >= 0.999)) { if ((x_factor >= 1.0) && (y_factor >= 1.0) && (z_factor >= 1.0)) {
// no hit, add light color result // no hit, add light color result
color_sum += (color_sample.xyz * light_color) / (0.01 * light_direction.length() * light_direction.length() + 1.0); color_sum += (orig_color_sample.xyz * light_color) / ((0.01 * light_direction.length() * light_direction.length()) + 1.0);
needs_next_light = true; needs_next_light = true;
} else { } else {
// if there is a border hit before reaching the light // if there is a border hit before reaching the light
@ -203,8 +204,8 @@ void main() {
hit_facing = 2; hit_facing = 2;
} }
vec3 intersection_pos = origPosition + x_factor * light_direction; vec3 intersection_pos = origPosition + x_factor * light_direction;
u = uint(ceil(intersection_pos.y)) - volume_pos_y; u = uint(round(intersection_pos.y)) - volume_pos_y;
v = uint(ceil(intersection_pos.z)) - volume_pos_z; v = uint(round(intersection_pos.z)) - volume_pos_z;
} }
if (y_factor <= x_factor && y_factor <= z_factor) { if (y_factor <= x_factor && y_factor <= z_factor) {
@ -214,19 +215,19 @@ void main() {
hit_facing = 4; hit_facing = 4;
} }
vec3 intersection_pos = origPosition + y_factor * light_direction; vec3 intersection_pos = origPosition + y_factor * light_direction;
u = uint(ceil(intersection_pos.x)) - volume_pos_x; u = uint(round(intersection_pos.x)) - volume_pos_x;
v = uint(ceil(intersection_pos.z)) - volume_pos_z; v = uint(round(intersection_pos.z)) - volume_pos_z;
} }
if (z_factor <= x_factor && z_factor <= y_factor) { if (z_factor <= x_factor && z_factor <= y_factor) {
if (y_pos) { if (z_pos) {
hit_facing = 0; hit_facing = 0;
} else { } else {
hit_facing = 1; hit_facing = 1;
} }
vec3 intersection_pos = origPosition + z_factor * light_direction; vec3 intersection_pos = origPosition + z_factor * light_direction;
u = uint(ceil(intersection_pos.x)) - volume_pos_x; u = uint(round(intersection_pos.x)) - volume_pos_x;
v = uint(ceil(intersection_pos.y)) - volume_pos_y; v = uint(round(intersection_pos.y)) - volume_pos_y;
} }
uint next_neighbor = sample_neighbor_from_scene_info(volume_index, uvec2(u, v), hit_facing); uint next_neighbor = sample_neighbor_from_scene_info(volume_index, uvec2(u, v), hit_facing);
uvec4 color_sample = sample_color_from_scene_info(volume_index, uvec2(u, v), hit_facing); uvec4 color_sample = sample_color_from_scene_info(volume_index, uvec2(u, v), hit_facing);
@ -234,18 +235,15 @@ void main() {
if (color_sample == uvec4(0, 0, 0, 0)) { if (color_sample == uvec4(0, 0, 0, 0)) {
// not a color hit, so check neighbor // not a color hit, so check neighbor
if (next_neighbor != 0) { if (next_neighbor != 0) {
color_sum = vec3(1.0, 0.0, 0.0);
volume_index = next_neighbor; volume_index = next_neighbor;
uint volume_pos_x = scene_info.infos[volume_index + 0]; volume_pos_x = scene_info.infos[volume_index + 0];
uint volume_pos_y = scene_info.infos[volume_index + 1]; volume_pos_y = scene_info.infos[volume_index + 1];
uint volume_pos_z = scene_info.infos[volume_index + 2]; volume_pos_z = scene_info.infos[volume_index + 2];
} else { } else {
color_sum = vec3(0.0, 0.0, 1.0);
// neightbor miss, shouldn't happen with a light inside of a volume. Might happen with ambient light. For now move on to next light. // neightbor miss, shouldn't happen with a light inside of a volume. Might happen with ambient light. For now move on to next light.
needs_next_light = true; needs_next_light = true;
} }
} else { } else {
color_sum = vec3(1.0, 0.0, 1.0);
// color hit, move on to next light (may change once transparents are implemnted) // color hit, move on to next light (may change once transparents are implemnted)
needs_next_light = true; needs_next_light = true;
} }
@ -254,30 +252,5 @@ void main() {
break; break;
} }
} }
// todo light color memory index does not contain the expected values -> check
outColor = vec4(color_sum, 1.0); outColor = vec4(color_sum, 1.0);
/*if (scene_info.infos[1] == 16) {
outColor = vec4(0, 1, 0, 1);
}*/
/*if (fragVolumeStart == 1 && scene_info.infos[fragVolumeStart] == 16) {
outColor = vec4(0, 1, 0, 1);
}*/
/*if (facing == 0) {
outColor = vec4(1, 0, 0, 1);
} else if (facing == 1) {
outColor = vec4(0, 1, 1, 1);
} else if (facing == 2) {
outColor = vec4(0, 1, 0, 1);
} else if (facing == 3) {
outColor = vec4(1, 0, 1, 1);
} else if (facing == 4) {
outColor = vec4(0, 0, 1, 1);
} else if (facing == 5) {
outColor = vec4(1, 1, 0, 1);
}*/
} }

View file

@ -457,7 +457,7 @@ impl App {
vk::MemoryMapFlags::empty(), vk::MemoryMapFlags::empty(),
)?; )?;
memcpy(self.scene_handler.rt_memory.as_ptr(), memory.cast(), 2103); memcpy(self.scene_handler.rt_memory.as_ptr(), memory.cast(), self.scene_handler.rt_memory.len());
self.device.unmap_memory(self.data.storage_buffers_memory[image_index]); self.device.unmap_memory(self.data.storage_buffers_memory[image_index]);

View file

@ -168,10 +168,11 @@ impl EmptyVolume {
for x in 0..x_size+1 { for x in 0..x_size+1 {
for y in 0..y_size+1 { for y in 0..y_size+1 {
for z in 0..z_size+1 { for z in 0..z_size+1 {
neighbors.set_element(reference.clone(), reference.borrow().position.x + x, reference.borrow().position.y + y, reference.borrow().position.z + z);
// fill only the edges // fill only the edges
if x == 0 || x == x_size || y == 0 || y == y_size || z==0 || z == z_size { /*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) neighbors.set_element(reference.clone(), reference.borrow().position.x + x, reference.borrow().position.y + y, reference.borrow().position.z + z)
} }*/
} }
} }
} }
@ -724,7 +725,7 @@ impl EmptyVolume {
mem_index += 1; mem_index += 1;
v[mem_index] = self.position.y as u32; v[mem_index] = self.position.y as u32;
mem_index += 1; mem_index += 1;
v[mem_index] = self.position.y as u32; v[mem_index] = self.position.z as u32;
mem_index += 1; mem_index += 1;
//max sizes //max sizes
v[mem_index] = self.size_x as u32; v[mem_index] = self.size_x as u32;
@ -740,7 +741,7 @@ impl EmptyVolume {
mem_index += 1; mem_index += 1;
} }
//color/roughness buffer sizes, 2 values each //color/roughness buffer sizes, 2 values each
if self.color_top.len() > 0 { if self.color_top.len() > 1 {
v[mem_index] = self.size_x as u32; v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_y as u32; v[mem_index + 1] = self.size_y as u32;
} else { } else {
@ -749,7 +750,7 @@ impl EmptyVolume {
} }
mem_index += 2; mem_index += 2;
if self.color_bottom.len() > 0 { if self.color_bottom.len() > 1 {
v[mem_index] = self.size_x as u32; v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_y as u32; v[mem_index + 1] = self.size_y as u32;
} else { } else {
@ -758,7 +759,7 @@ impl EmptyVolume {
} }
mem_index += 2; mem_index += 2;
if self.color_left.len() > 0 { if self.color_left.len() > 1 {
v[mem_index] = self.size_y as u32; v[mem_index] = self.size_y as u32;
v[mem_index + 1] = self.size_z as u32; v[mem_index + 1] = self.size_z as u32;
} else { } else {
@ -767,7 +768,7 @@ impl EmptyVolume {
} }
mem_index += 2; mem_index += 2;
if self.color_right.len() > 0 { if self.color_right.len() > 1 {
v[mem_index] = self.size_y as u32; v[mem_index] = self.size_y as u32;
v[mem_index + 1] = self.size_z as u32; v[mem_index + 1] = self.size_z as u32;
} else { } else {
@ -776,7 +777,7 @@ impl EmptyVolume {
} }
mem_index += 2; mem_index += 2;
if self.color_front.len() > 0 { if self.color_front.len() > 1 {
v[mem_index] = self.size_x as u32; v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_z as u32; v[mem_index + 1] = self.size_z as u32;
} else { } else {
@ -785,7 +786,7 @@ impl EmptyVolume {
} }
mem_index += 2; mem_index += 2;
if self.color_back.len() > 0 { if self.color_back.len() > 1 {
v[mem_index] = self.size_x as u32; v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_z as u32; v[mem_index + 1] = self.size_z as u32;
} else { } else {
@ -794,7 +795,7 @@ impl EmptyVolume {
} }
mem_index += 2; mem_index += 2;
//neighbor buffer sizes, 2 values each //neighbor buffer sizes, 2 values each
if self.neighbor_top.len() > 0 { if self.neighbor_top.len() > 1 {
v[mem_index] = self.size_x as u32; v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_y as u32; v[mem_index + 1] = self.size_y as u32;
} else { } else {
@ -803,7 +804,7 @@ impl EmptyVolume {
} }
mem_index += 2; mem_index += 2;
if self.neighbor_bottom.len() > 0 { if self.neighbor_bottom.len() > 1 {
v[mem_index] = self.size_x as u32; v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_y as u32; v[mem_index + 1] = self.size_y as u32;
} else { } else {
@ -812,7 +813,7 @@ impl EmptyVolume {
} }
mem_index += 2; mem_index += 2;
if self.neighbor_left.len() > 0 { if self.neighbor_left.len() > 1 {
v[mem_index] = self.size_y as u32; v[mem_index] = self.size_y as u32;
v[mem_index + 1] = self.size_z as u32; v[mem_index + 1] = self.size_z as u32;
} else { } else {
@ -821,7 +822,7 @@ impl EmptyVolume {
} }
mem_index += 2; mem_index += 2;
if self.neighbor_right.len() > 0 { if self.neighbor_right.len() > 1 {
v[mem_index] = self.size_y as u32; v[mem_index] = self.size_y as u32;
v[mem_index + 1] = self.size_z as u32; v[mem_index + 1] = self.size_z as u32;
} else { } else {
@ -830,7 +831,7 @@ impl EmptyVolume {
} }
mem_index += 2; mem_index += 2;
if self.neighbor_front.len() > 0 { if self.neighbor_front.len() > 1 {
v[mem_index] = self.size_x as u32; v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_z as u32; v[mem_index + 1] = self.size_z as u32;
} else { } else {
@ -839,7 +840,7 @@ impl EmptyVolume {
} }
mem_index += 2; mem_index += 2;
if self.neighbor_back.len() > 0 { if self.neighbor_back.len() > 1 {
v[mem_index] = self.size_x as u32; v[mem_index] = self.size_x as u32;
v[mem_index + 1] = self.size_z as u32; v[mem_index + 1] = self.size_z as u32;
} else { } else {
@ -860,7 +861,7 @@ impl EmptyVolume {
} }
} }
else { else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]); v[mem_index] = u32::from_ne_bytes([0, 0, 0, 0]);
mem_index += 1; mem_index += 1;
} }
@ -874,7 +875,7 @@ impl EmptyVolume {
} }
} }
else { else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]); v[mem_index] = u32::from_ne_bytes([0, 0, 0, 0]);
mem_index += 1; mem_index += 1;
} }
@ -888,7 +889,7 @@ impl EmptyVolume {
} }
} }
else { else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]); v[mem_index] = u32::from_ne_bytes([0, 0, 0, 0]);
mem_index += 1; mem_index += 1;
} }
@ -902,7 +903,7 @@ impl EmptyVolume {
} }
} }
else { else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]); v[mem_index] = u32::from_ne_bytes([0, 0, 0, 0]);
mem_index += 1; mem_index += 1;
} }
@ -916,7 +917,7 @@ impl EmptyVolume {
} }
} }
else { else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]); v[mem_index] = u32::from_ne_bytes([0, 0, 0, 0]);
mem_index += 1; mem_index += 1;
} }
@ -930,14 +931,14 @@ impl EmptyVolume {
} }
} }
else { else {
v[mem_index] = u32::from_ne_bytes([255, 255, 255, 255]); v[mem_index] = u32::from_ne_bytes([0, 0, 0, 0]);
mem_index += 1; mem_index += 1;
} }
// neighbors // neighbors
if self.neighbor_top.len() > 0 { if self.neighbor_top.len() > 0 {
for value in &self.neighbor_top { for nvalue in &self.neighbor_top {
if let Some(reference) = value { if let Some(reference) = nvalue {
v[mem_index] = reference.borrow().memory_start as u32; v[mem_index] = reference.borrow().memory_start as u32;
} }
else { else {
@ -952,8 +953,8 @@ impl EmptyVolume {
} }
if self.neighbor_bottom.len() > 0 { if self.neighbor_bottom.len() > 0 {
for value in &self.neighbor_bottom { for nvalue in &self.neighbor_bottom {
if let Some(reference) = value { if let Some(reference) = nvalue {
v[mem_index] = reference.borrow().memory_start as u32; v[mem_index] = reference.borrow().memory_start as u32;
} }
else { else {
@ -968,8 +969,8 @@ impl EmptyVolume {
} }
if self.neighbor_left.len() > 0 { if self.neighbor_left.len() > 0 {
for value in &self.neighbor_left { for nvalue in &self.neighbor_left {
if let Some(reference) = value { if let Some(reference) = nvalue {
v[mem_index] = reference.borrow().memory_start as u32; v[mem_index] = reference.borrow().memory_start as u32;
} }
else { else {
@ -984,8 +985,8 @@ impl EmptyVolume {
} }
if self.neighbor_right.len() > 0 { if self.neighbor_right.len() > 0 {
for value in &self.neighbor_right { for nvalue in &self.neighbor_right {
if let Some(reference) = value { if let Some(reference) = nvalue {
v[mem_index] = reference.borrow().memory_start as u32; v[mem_index] = reference.borrow().memory_start as u32;
} }
else { else {
@ -1000,8 +1001,8 @@ impl EmptyVolume {
} }
if self.neighbor_front.len() > 0 { if self.neighbor_front.len() > 0 {
for value in &self.neighbor_front { for nvalue in &self.neighbor_front {
if let Some(reference) = value { if let Some(reference) = nvalue {
v[mem_index] = reference.borrow().memory_start as u32; v[mem_index] = reference.borrow().memory_start as u32;
} }
else { else {
@ -1016,8 +1017,8 @@ impl EmptyVolume {
} }
if self.neighbor_back.len() > 0 { if self.neighbor_back.len() > 0 {
for value in &self.neighbor_back { for nvalue in &self.neighbor_back {
if let Some(reference) = value { if let Some(reference) = nvalue {
v[mem_index] = reference.borrow().memory_start as u32; v[mem_index] = reference.borrow().memory_start as u32;
} }
else { else {

View file

@ -82,7 +82,7 @@ impl Scene {
let shade = (rng.gen_range(0..25) as f32) / 100.0; let shade = (rng.gen_range(0..25) as f32) / 100.0;
let cube = Cube { let cube = Cube {
pos: vec3(10.0, 10.0, 10.0), pos: vec3(10.0, 10.0, 10.0),
color: vec3(shade, 1.0, shade), color: vec3(1.0, 1.0, 0.0),
tex_coord: vec2(0.0, 0.0) tex_coord: vec2(0.0, 0.0)
}; };