diff --git a/build.rs b/build.rs index 67aaf3a..4a669e3 100644 --- a/build.rs +++ b/build.rs @@ -2,6 +2,28 @@ use std::process::Command; use std::io::{self, Write}; use std::path::Path; +use std::fs::File; +use std::io::{BufReader, BufRead, Error}; + +fn insert_place_holders(path: &str) { + let input = File::open(path).unwrap(); + let mut output = File::create(path.replace("_placeholder", "")).unwrap(); + let buffered = BufReader::new(input); + + for line in buffered.lines() { + let line_str = line.unwrap(); + if line_str.contains("#include ") { + let replacer = File::open(format!("shaders/{}", line_str.clone().split_off(9))).expect(format!("could not find the lib file shaders/{}", line_str.clone().split_off(9)).as_str()); + let replacement_buffered = BufReader::new(replacer); + for replacement_line in replacement_buffered.lines() { + write!(output, "{}\n", replacement_line.unwrap()).expect("could not write"); + } + } else { + write!(output, "{}\n", line_str).expect("could not write"); + } + } +} + fn main() { println!("cargo::rerun-if-changed=shaders/cube.frag"); println!("cargo::rerun-if-changed=shaders/cube.geom"); @@ -12,7 +34,8 @@ fn main() { println!("cargo::rerun-if-changed=shaders/cuboid.vert"); println!("cargo::rerun-if-changed=shaders/rt_quad.vert"); - println!("cargo::rerun-if-changed=shaders/rt_quad.frag"); + println!("cargo::rerun-if-changed=shaders/rt_lib.frag"); + println!("cargo::rerun-if-changed=shaders/rt_quad_placeholder.frag"); println!("cargo::rerun-if-changed=shaders/rt_compute_rasterize.comp"); println!("cargo::rerun-if-changed=shaders/rt_compute_grow_one.comp"); @@ -20,6 +43,9 @@ fn main() { println!("cargo::rerun-if-changed=shaders/rt_compute_grow_three.comp"); println!("cargo::rerun-if-changed=shaders/rt_compute_mempos.comp"); + // replace placeholders + insert_place_holders("shaders/rt_quad_placeholder.frag"); + std::fs::remove_file("shaders/compiled/geo_cube.spv").unwrap_or(()); std::fs::remove_file("shaders/compiled/frag_cube.spv").unwrap_or(()); std::fs::remove_file("shaders/compiled/vert_cube.spv").unwrap_or(()); diff --git a/shaders/compiled/frag_rt_quad.spv b/shaders/compiled/frag_rt_quad.spv index ff99d3f..2cb5167 100644 Binary files a/shaders/compiled/frag_rt_quad.spv and b/shaders/compiled/frag_rt_quad.spv differ diff --git a/shaders/rt_lib.frag b/shaders/rt_lib.frag new file mode 100644 index 0000000..bc293db --- /dev/null +++ b/shaders/rt_lib.frag @@ -0,0 +1,794 @@ +layout(binding = 0) uniform UniformBufferObject { + mat4 model; + mat4 geom_rot; + mat4 view; + mat4 proj; + vec3 camera_pos; + bool[16] use_geom_shader; +} ubo; + +// 0 - location for the maximum number of lights referenced per chunk (also will be the invalid memory allocation for pointing to a nonexistant neighbor) +// 1 - location for the max iterations per light +// 2 - diffuse raster samples (2*n + 1) * (2*n + 1) so as to always have at least the central fragment covered +// 3 - diffuse raster size (float, needs to be decoded) +// 4 - max recursive rays +// 5 - diffuse rays per hit +// 6 - maximum number of compounds per light +layout(binding = 2) readonly buffer SceneInfoBuffer{ + uint infos[]; +} scene_info; + +layout(binding = 3) readonly buffer CompoundBuffer { + uint compounds[]; +}; + +layout(binding = 10) readonly buffer OctTreeMemory { + uint oct_tree_mem[]; +}; + +uint max_num_lights = scene_info.infos[0]; +uint max_iterations_per_light = scene_info.infos[1]; +// diffuse raytracing using a quadratic raster of rays +int half_diffuse_raster_steps = int(scene_info.infos[2]); +float raster_distance = uintBitsToFloat(scene_info.infos[3]); +int raster_points = (2 * half_diffuse_raster_steps + 1) * (2 * half_diffuse_raster_steps + 1); +float pos_infinity = uintBitsToFloat(0x7F800000); +// set limit for maximal iterations +uint max_iterations = max_num_lights * max_iterations_per_light * raster_points; +uint iteration_num = 0; +uint max_num_compounds = scene_info.infos[6]; + +uvec4 unpack_color(uint val) { + // left most 8 bits first + uint val1 = (val >> 24); + uint val2 = (val << 8) >> 24; + uint val3 = (val << 16) >> 24; + uint val4 = (val << 24) >> 24; + + return uvec4(val4, val3, val2, val1); +} + +uint array_descr_offset = 6 + max_num_lights + max_num_compounds; +uint color_array_offset = 24 + 1; + +uint sample_neighbor_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) { + uint array_descr_start = volume_start + array_descr_offset; + uint color_array_start = array_descr_start + color_array_offset; + + 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 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]; + uint left_neighbor_size_v = scene_info.infos[array_descr_start + 17]; + + uint right_neighbor_size_u = scene_info.infos[array_descr_start + 18]; + uint right_neighbor_size_v = scene_info.infos[array_descr_start + 19]; + + uint front_neighbor_size_u = scene_info.infos[array_descr_start + 20]; + 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 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; +} + +uint sample_neighbor_from_scene_info(uint volume_start, vec2 raster_pos, uint f) { + return sample_neighbor_from_scene_info(volume_start, uvec2(uint(floor(raster_pos.x)), uint(floor(raster_pos.y))), f); +} + +uvec4 sample_color_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) { + uint array_descr_start = volume_start + array_descr_offset; + uint color_array_start = array_descr_start + color_array_offset; + + 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_size = top_color_size_u * top_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 right_size = right_color_size_u * right_color_size_v; + uint front_size = front_color_size_u * front_color_size_v; + uint back_size = back_color_size_u * back_color_size_v; + + // maybe do an array solution for this as well + uint array_start = color_array_start + uint(f > 0) * top_size + uint(f > 1) * bottom_size + uint(f > 2) * left_size + uint(f > 3) * right_size + uint(f > 4) * front_size; + uint us[6] = {top_color_size_u, bottom_color_size_u, left_color_size_u, right_color_size_u, front_color_size_u, back_color_size_u}; + 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 + clamp(raster_pos.x, 0, u_size) * v_size * uint(u_size > 1) + clamp(raster_pos.y, 0, v_size) * uint(v_size > 1)]; + return unpack_color(value); +} + +uvec4 sample_color_from_scene_info(uint volume_start, vec2 raster_pos, uint f) { + return sample_color_from_scene_info(volume_start, uvec2(uint(floor(raster_pos.x)), uint(floor(raster_pos.y))), f); +} + +vec3 get_light_position(uint light_index) { + return vec3(uintBitsToFloat(scene_info.infos[light_index + 1]), uintBitsToFloat(scene_info.infos[light_index + 2]), uintBitsToFloat(scene_info.infos[light_index + 3])); +} + +vec3 get_light_color(uint light_index) { + return vec3(float(scene_info.infos[light_index + 4]) / 255.0, float(scene_info.infos[light_index + 5]) / 255.0, float(scene_info.infos[light_index + 6]) / 255.0); +} + +vec3 normal_for_facing(uint facing) { + if (facing == 0) { + return vec3(0.0, 0.0, -1.0); + } + if (facing == 1) { + return vec3(0.0, 0.0, 1.0); + } + if (facing == 2) { + return vec3(1.0, 0.0, 0.0); + } + if (facing == 3) { + return vec3(-1.0, 0.0, 0.0); + } + if (facing == 4) { + return vec3(0.0, 1.0, 0.0); + } + if (facing == 5) { + return vec3(0.0, -1.0, 0.0); + } + + return vec3(0.0, 0.0, 0.0); +} + +vec3 reflect_vector(vec3 direction, uint facing) { + vec3 normal = normal_for_facing(facing); + return direction - 2.0 * dot(direction, normal) * normal; +} + +uvec3 parent_child_vec(uint child_size, uint child_index) { + if (child_index == 1) { + return uvec3(0, 0, 0); + } + if (child_index == 2) { + return uvec3(child_size, 0, 0); + } + if (child_index == 3) { + return uvec3(0, child_size, 0); + } + if (child_index == 4) { + return uvec3(child_size, child_size, 0); + } + if (child_index == 5) { + return uvec3(0, 0, child_size); + } + if (child_index == 6) { + return uvec3(child_size, 0, child_size); + } + if (child_index == 7) { + return uvec3(0, child_size, child_size); + } + if (child_index == 8) { + return uvec3(child_size, child_size, child_size); + } + return uvec3(0, 0, 0); +} + +uint next_oct_tree_child(vec3 mid_point, vec3 check_pos, bool child_open[8]) { + if (check_pos.x <= mid_point.x && check_pos.y <= mid_point.y && check_pos.z <= mid_point.z && child_open[0]) { + return 1; + } + if (check_pos.x >= mid_point.x && check_pos.y <= mid_point.y && check_pos.z <= mid_point.z && child_open[1]) { + return 2; + } + if (check_pos.x <= mid_point.x && check_pos.y >= mid_point.y && check_pos.z <= mid_point.z && child_open[2]) { + return 3; + } + if (check_pos.x >= mid_point.x && check_pos.y >= mid_point.y && check_pos.z <= mid_point.z && child_open[3]) { + return 4; + } + if (check_pos.x <= mid_point.x && check_pos.y <= mid_point.y && check_pos.z >= mid_point.z && child_open[4]) { + return 5; + } + if (check_pos.x >= mid_point.x && check_pos.y <= mid_point.y && check_pos.z >= mid_point.z && child_open[5]) { + return 6; + } + if (check_pos.x <= mid_point.x && check_pos.y >= mid_point.y && check_pos.z >= mid_point.z && child_open[6]) { + return 7; + } + if (check_pos.x >= mid_point.x && check_pos.y >= mid_point.y && check_pos.z >= mid_point.z && child_open[7]) { + return 8; + } + + return 0; // return to parent +} + +struct Tracing { + vec3 end_pos; + uvec4 end_color; + uint end_volume; + uint end_facing; + float end_factor; + uint end_cycle; + bool has_hit; + vec3 color_mul; + uvec2 end_raster; + + vec3 end_direction; + bool has_transparent_hit; +}; + +Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, float start_max_factor, bool allow_reflect) { + vec3 direction = start_direction; + float max_factor = start_max_factor; + vec3 pos = starting_pos; + // setup volume info + uint volume_index = volume_start; + float volume_scale = uintBitsToFloat(scene_info.infos[volume_index + array_descr_offset + color_array_offset - 1]); + float volume_pos_x = uintBitsToFloat(scene_info.infos[volume_index + 0]); + float volume_pos_y = uintBitsToFloat(scene_info.infos[volume_index + 1]); + float volume_pos_z = uintBitsToFloat(scene_info.infos[volume_index + 2]); + + bool x_pos = direction.x > 0.0; + bool x_null = (direction.x == 0.0); + + bool y_pos = direction.y > 0.0; + bool y_null = (direction.y == 0.0); + + bool z_pos = direction.z > 0.0; + bool z_null = (direction.z == 0.0); + + // default is max factor, that way we avoid collision when going parallel to an axis. The other directions will score a hit + float x_factor = max_factor; + float y_factor = max_factor; + float z_factor = max_factor; + + Tracing result; + result.has_hit = false; + result.has_transparent_hit = false; + result.color_mul = vec3(1.0, 1.0, 1.0); + + // intermediate storage for transparent hit values + vec3 end_pos_transparent; + uvec4 end_color_transparent; + uint end_volume_transparent; + uint end_facing_transparent; + uvec2 end_raster_transparent; + vec3 color_mul_transparent; + + uint next_volumetric_index = 0; + uint[5] done_volumetrics; + for (int i=0; i < 5; i++) { + done_volumetrics[i] = 0; + } + + while (iteration_num < max_iterations) { + iteration_num ++; + + uint compound_num = 0; + while (scene_info.infos[volume_index + 6 + max_num_lights + compound_num] != 0 && compound_num < max_num_compounds && iteration_num < max_iterations && !result.has_hit) { + uint compound_start = scene_info.infos[volume_index + 6 + max_num_lights + compound_num]; + + bool already_checked = false; + for (int i=0; i < 5; i++) { + if (compound_start == done_volumetrics[i]) { + already_checked = true; + break; + } + } + if (already_checked) { + compound_num += 1; + continue; + } + done_volumetrics[next_volumetric_index] = compound_start; + next_volumetric_index = (next_volumetric_index + 1) % 5; + + //iteration_num ++; + uint oct_tree_index = compounds[compound_start + 8]; + uint compound_grid_size = compounds[compound_start]; + float compound_scale = uintBitsToFloat(compounds[compound_start + 1]); + vec3 compound_pos = vec3(uintBitsToFloat(compounds[compound_start + 5]), uintBitsToFloat(compounds[compound_start + 6]), uintBitsToFloat(compounds[compound_start + 7])); + // check if we hit the volume + float x_border = compound_pos.x + float((compound_grid_size) * uint(!x_pos)) * compound_scale; + float y_border = compound_pos.y + float((compound_grid_size) * uint(!y_pos)) * compound_scale; + float z_border = compound_pos.z + float((compound_grid_size) * uint(!z_pos)) * compound_scale; + + // go over the borders by this amount + float overstep = 0.00001 / length(direction); + + if (!x_null) { + x_factor = (x_border - pos.x) / direction.x; + } else { + x_factor = max_factor; + } + if (!y_null) { + y_factor = (y_border - pos.y) / direction.y; + } else { + y_factor = max_factor; + } + if (!z_null) { + z_factor = (z_border - pos.z) / direction.z; + } else { + z_factor = max_factor; + } + x_factor += overstep; + y_factor += overstep; + z_factor += overstep; + + vec3 intersection_pos = pos; + bool is_x_hit = false; + bool is_y_hit = false; + bool is_z_hit = false; + bool hit_inside = false; + if ((compound_pos.x <= intersection_pos.x && intersection_pos.x <= compound_pos.x + float(compound_grid_size) * compound_scale) && + (compound_pos.y <= intersection_pos.y && intersection_pos.y <= compound_pos.y + float(compound_grid_size) * compound_scale) && + (compound_pos.z <= intersection_pos.z && intersection_pos.z <= compound_pos.z + float(compound_grid_size) * compound_scale)){ + //hit_inside = true; + } else { + vec3 intersection_pos_x = pos + x_factor * direction; + vec3 intersection_pos_y = pos + y_factor * direction; + vec3 intersection_pos_z = pos + z_factor * direction; + if ((compound_pos.x <= intersection_pos_x.x && intersection_pos_x.x <= compound_pos.x + float(compound_grid_size) * compound_scale) && + (compound_pos.y <= intersection_pos_x.y && intersection_pos_x.y <= compound_pos.y + float(compound_grid_size) * compound_scale) && + (compound_pos.z <= intersection_pos_x.z && intersection_pos_x.z <= compound_pos.z + float(compound_grid_size) * compound_scale) && x_factor > 0.0 && x_factor <= max_factor) { + hit_inside = true; + is_x_hit = true; + intersection_pos = intersection_pos_x; + } + + if ((compound_pos.x <= intersection_pos_y.x && intersection_pos_y.x <= compound_pos.x + float(compound_grid_size) * compound_scale) && + (compound_pos.y <= intersection_pos_y.y && intersection_pos_y.y <= compound_pos.y + float(compound_grid_size) * compound_scale) && + (compound_pos.z <= intersection_pos_y.z && intersection_pos_y.z <= compound_pos.z + float(compound_grid_size) * compound_scale) && y_factor > 0.0 && y_factor <= max_factor && (y_factor < x_factor || !is_x_hit)) { + hit_inside = true; + is_y_hit = true; + intersection_pos = intersection_pos_y; + } + + if ((compound_pos.x <= intersection_pos_z.x && intersection_pos_z.x <= compound_pos.x + float(compound_grid_size) * compound_scale) && + (compound_pos.y <= intersection_pos_z.y && intersection_pos_z.y <= compound_pos.y + float(compound_grid_size) * compound_scale) && + (compound_pos.z <= intersection_pos_z.z && intersection_pos_z.z <= compound_pos.z + float(compound_grid_size) * compound_scale) && z_factor > 0.0 && z_factor <= max_factor && (z_factor < x_factor || !is_x_hit) && (z_factor < y_factor || !is_y_hit)) { + hit_inside = true; + is_z_hit = true; + intersection_pos = intersection_pos_z; + } + } + + // check that either the hit is in range or we are inside of the compound from the start + if (hit_inside) { + vec3 oct_tree_pos = vec3(compound_pos); + uint current_size = compound_grid_size; + vec3 mid_point = oct_tree_pos + float(current_size / 2) * vec3(compound_scale, compound_scale, compound_scale); + bool children_open[8] = {true, true, true, true, true, true, true, true}; + uint oct_tree_address = oct_tree_index; + // iterate through the oct_tree + uint check_it = 0; + uint max_check_it = 60; + uint prev_child = 0; + uint prev_prev_child = 0; + + uvec3 grid_pos = uvec3(0, 0, 0); + uvec3 parent_pos = uvec3(0, 0, 0); + + bool has_moved = false; + while (!result.has_hit && check_it < max_check_it) { + // failsafe to get out in case has_moved runs into an accuracy issue + check_it ++; + oct_tree_pos = vec3(grid_pos) * compound_scale + compound_pos; + mid_point = oct_tree_pos + (float(current_size / 2) * vec3(compound_scale, compound_scale, compound_scale)); + + uint child_index = next_oct_tree_child(mid_point, intersection_pos, children_open); + if (child_index == 0) { + // go up to parent + // if parent is 0 abort, as we have reached the root node again and try to exit it + if (oct_tree_mem[oct_tree_address] == 0) { + break; + } + for (int i=0; i < 8; i++) { + children_open[i] = true; + } + uint parent_index = oct_tree_mem[oct_tree_address]; + // check which child we came from + child_index = 1 * uint(oct_tree_address == oct_tree_mem[parent_index + 1]) + 2 * uint(oct_tree_address == oct_tree_mem[parent_index + 2]) + 3 * uint(oct_tree_address == oct_tree_mem[parent_index + 3]) + 4 * uint(oct_tree_address == oct_tree_mem[parent_index + 4]) + 5 * uint(oct_tree_address == oct_tree_mem[parent_index + 5]) + 6 * uint(oct_tree_address == oct_tree_mem[parent_index + 6]) + 7 * uint(oct_tree_address == oct_tree_mem[parent_index + 7]) + 8 * uint(oct_tree_address == oct_tree_mem[parent_index + 8]); + // mark as done to avoid reinvestigating, since intersection_pos is on its edge + children_open[child_index - 1] = false; + prev_prev_child = prev_child; + prev_child = oct_tree_address; + + uvec3 back_vec = parent_child_vec(current_size, child_index); + grid_pos -= parent_child_vec(current_size, child_index); + current_size *= 2; + oct_tree_address = parent_index; + } else { + // go down into child + if (current_size == 2) { + // check block if hit break + if (oct_tree_mem[oct_tree_address + child_index] != 0) { + result.has_hit = true; + result.end_color = unpack_color(oct_tree_mem[oct_tree_address + child_index]); + break; + } + } else { + // check if the child has content, else skip to next child of current parent + uint x = oct_tree_mem[oct_tree_address + child_index]; + if (oct_tree_mem[x] != 0) { + // change base address and position to child + current_size /= 2; + oct_tree_address = x; + grid_pos += parent_child_vec(current_size, child_index); + for (int i=0; i < 8; i++) { + children_open[i] = true; + } + continue; + } + } + children_open[child_index - 1] = false; + + // we did not go deeper or had a hit, so intersection pos needs to be updated + // new intersection pos calc + vec3 offset = vec3(parent_child_vec(current_size / 2, child_index)) * compound_scale; + vec3 low = oct_tree_pos + offset; + float x_border = low.x + float((compound_scale * current_size / 2) * uint(x_pos)); + float y_border = low.y + float((compound_scale * current_size / 2) * uint(y_pos)); + float z_border = low.z + float((compound_scale * current_size / 2) * uint(z_pos)); + + if (!x_null) { + x_factor = (x_border - pos.x) / direction.x; + if (x_factor <= 0.0) { + x_factor = max_factor; + } + } else { + x_factor = max_factor; + } + if (!y_null) { + y_factor = (y_border - pos.y) / direction.y; + if (y_factor <= 0.0) { + y_factor = max_factor; + } + } else { + y_factor = max_factor; + } + if (!z_null) { + z_factor = (z_border - pos.z) / direction.z; + if (z_factor <= 0.0) { + z_factor = max_factor; + } + } else { + z_factor = max_factor; + } + float smallest_factor = min(min(x_factor, y_factor), z_factor); + + if (x_factor == smallest_factor) { + is_x_hit = true; + is_y_hit = false; + is_z_hit = false; + } + if (y_factor == smallest_factor) { + is_x_hit = false; + is_y_hit = true; + is_z_hit = false; + } + if (z_factor == smallest_factor) { + is_x_hit = false; + is_y_hit = false; + is_z_hit = true; + } + + // move a bit further to fully enter the next quadrant + smallest_factor += overstep; + + //has_moved = length(intersection_pos - (pos + smallest_factor * direction)) >= 0.00001; + has_moved = intersection_pos != (pos + smallest_factor * direction); + intersection_pos = pos + smallest_factor * direction; + } + } + + uint hit_facing = uint(is_x_hit) * (2 + uint(x_pos)) + uint(is_y_hit) * (4 + uint(y_pos)) + uint(is_z_hit && !z_pos); + //result.has_hit = true; + result.end_pos = intersection_pos; + result.end_facing = hit_facing; + result.end_volume = volume_index; + result.end_direction = direction; + } + + compound_num += 1; + } + + if (result.has_hit) { + break; + } + + float x_border = volume_pos_x + float((scene_info.infos[volume_index + 3]) * uint(x_pos)) * volume_scale - 0.5 * volume_scale; + float y_border = volume_pos_y + float((scene_info.infos[volume_index + 4]) * uint(y_pos)) * volume_scale - 0.5 * volume_scale; + float z_border = volume_pos_z + float((scene_info.infos[volume_index + 5]) * uint(z_pos)) * volume_scale - 0.5 * volume_scale; + + bool needs_next_light = false; + + if (!x_null) { + x_factor = (x_border - pos.x) / direction.x; + } else { + x_factor = max_factor; + } + if (!y_null) { + y_factor = (y_border - pos.y) / direction.y; + } else { + y_factor = max_factor; + } + if (!z_null) { + z_factor = (z_border - pos.z) / direction.z; + } else { + z_factor = max_factor; + } + + if ((x_factor >= max_factor) && (y_factor >= max_factor) && (z_factor >= max_factor)) { + // no hit, finish tracking + break; + } else { + // if there is a border hit before reaching the end + // change to the relevant next volume + // Todo: look into removing ifs from this + uint hit_facing = 0; + uint u = 0; + uint v = 0; + + bool is_x_smallest = x_factor < y_factor && x_factor < z_factor; + bool is_y_smallest = y_factor < x_factor && y_factor < z_factor; + bool is_z_smallest = z_factor <= x_factor && z_factor <= y_factor; + + hit_facing = uint(is_x_smallest) * (2 + uint(x_pos)) + uint(is_y_smallest) * (4 + uint(y_pos)) + uint(is_z_smallest && !z_pos); + float smallest_factor = min(min(x_factor, y_factor), z_factor); // maybe use multiplication instead? + vec3 intersection_pos = pos + smallest_factor * direction; + u = uint(is_x_smallest) * (uint(round((intersection_pos.y - volume_pos_y) / volume_scale))) + + uint(is_y_smallest || is_z_smallest) * (uint(round((intersection_pos.x - volume_pos_x) / volume_scale))); + v = uint(is_x_smallest || is_y_smallest) * (uint(round((intersection_pos.z - volume_pos_z) / volume_scale))) + + uint(is_z_smallest) * (uint(round((intersection_pos.y - volume_pos_y) / volume_scale))); + + 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); + + if (color_sample.xyz == uvec3(0, 0, 0)) { + // not a color hit, so check neighbor + if (next_neighbor != 0) { + volume_index = next_neighbor; + volume_scale = uintBitsToFloat(scene_info.infos[volume_index + array_descr_offset + color_array_offset - 1]); + volume_pos_x = uintBitsToFloat(scene_info.infos[volume_index + 0]); + volume_pos_y = uintBitsToFloat(scene_info.infos[volume_index + 1]); + volume_pos_z = uintBitsToFloat(scene_info.infos[volume_index + 2]); + } else { + // neighbor miss + end_color_transparent = uvec4(255, 0, 0, 255); + result.end_color = uvec4(255, 0, 0, 255); + break; + } + } else { + if (next_neighbor != 0) { + // transparent hit, move on but change the color + end_volume_transparent = volume_index; + color_mul_transparent = result.color_mul; + + volume_index = next_neighbor; + volume_scale = uintBitsToFloat(scene_info.infos[volume_index + array_descr_offset + color_array_offset - 1]); + volume_pos_x = uintBitsToFloat(scene_info.infos[volume_index + 0]); + volume_pos_y = uintBitsToFloat(scene_info.infos[volume_index + 1]); + volume_pos_z = uintBitsToFloat(scene_info.infos[volume_index + 2]); + result.color_mul = result.color_mul * vec3(float(color_sample.x) / 255.0, float(color_sample.y) / 255.0, float(color_sample.z) / 255.0); + result.has_transparent_hit = true; + result.end_volume = volume_index; + result.end_direction = direction; + + end_color_transparent = color_sample; + end_raster_transparent = uvec2(u, v); + end_pos_transparent = intersection_pos; + end_facing_transparent = hit_facing; + + // stop iterating if there is barely anything left to see + if (max(result.color_mul.x, max(result.color_mul.y, result.color_mul.z)) < 0.1) { + break; + } + } else { + // color hit, either reflect or move on + result.end_pos = intersection_pos; + result.end_facing = hit_facing; + result.end_color = color_sample; + result.end_raster = uvec2(u, v); + result.has_hit = true; + result.end_volume = volume_index; + result.end_direction = direction; + + float reflectivity = 1.0 - float(color_sample.w) / 255.0; + vec3 refltective_color_mul = result.color_mul * vec3(float(color_sample.x) / 255.0, float(color_sample.y) / 255.0, float(color_sample.z) / 255.0); + vec3 visibility_after_reflection = refltective_color_mul * reflectivity; + //break; + //max(visibility_after_reflection.x, max(visibility_after_reflection.y, visibility_after_reflection.z)) >= 0.1 && + if (max(visibility_after_reflection.x, max(visibility_after_reflection.y, visibility_after_reflection.z)) >= 0.1 && allow_reflect) { + // do reflect + direction = reflect_vector(direction, hit_facing); + pos = intersection_pos; + //max_factor -= smallest_factor; + + x_pos = direction.x > 0.0; + x_null = (direction.x == 0.0); + + y_pos = direction.y > 0.0; + y_null = (direction.y == 0.0); + + z_pos = direction.z > 0.0; + z_null = (direction.z == 0.0); + } else { + break; + } + } + } + } + } + + result.end_factor = min(min(x_factor, y_factor), z_factor); + result.end_cycle = iteration_num; + + // in case we have a transparent hit but no hit afterwards + if (!result.has_hit && result.has_transparent_hit) { + // did we stop because nothing could be seen through the object? + if (max(result.color_mul.x, max(result.color_mul.y, result.color_mul.z)) < 0.1) { + // if so count it as a hit and recover the pre transparent color multiplier + result.has_hit = true; + result.color_mul = color_mul_transparent; + } + result.end_pos = end_pos_transparent; + result.end_color = end_color_transparent; + result.end_volume = end_volume_transparent; + result.end_facing = end_facing_transparent; + result.end_raster = end_raster_transparent; + } + + return result; +} + +vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sample, vec3 normal) { + uint light_num = 0; + + // initialize color + vec3 color_sum = vec3(0.0, 0.0, 0.0);// + (orig_color_sample.xyz * 0.005); + + while (iteration_num < max_iterations) { + // setup light info + uint light_index = scene_info.infos[volume_start + 6 + light_num]; + if (light_index == 0) { + // abort if there is no new light + break; + } + vec3 light_direction; + float max_factor; + if (scene_info.infos[light_index] == 0) { + //point light + light_direction = get_light_position(light_index) - starting_pos; + max_factor = 1.0; + } else if (scene_info.infos[light_index] == 1) { + // directional light + light_direction = -normalize(get_light_position(light_index)); + max_factor = pos_infinity; + } + vec3 light_color = get_light_color(light_index); + + Tracing result = trace_ray(volume_start, starting_pos, light_direction, max_factor, false); + // add result, if there is a hit the null vector will be added + color_sum += float(!result.has_hit) * result.color_mul * max(dot(normal, normalize(light_direction)), 0.0) * (orig_color_sample.xyz * light_color) / (length(light_direction) * length(light_direction)); + + light_num += 1; + if (light_num >= max_num_lights) { + break; + } + } + + return color_sum; +} + +vec3 diffuse_tracing(uint volume_start, uvec4 color_roughness, vec3 pos, uint f) { + vec4 orig_color_sample = vec4(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0, 1); + vec3 normal = normal_for_facing(f); + + vec3 color_sum = vec3(0.0, 0.0, 0.0); + for (int u_offset = -half_diffuse_raster_steps; u_offset <= half_diffuse_raster_steps; u_offset++) { + for (int v_offset = -half_diffuse_raster_steps; v_offset <= half_diffuse_raster_steps; v_offset++) { + float x_offset = raster_distance * float(u_offset) * float(f == 0 || f == 1 || f == 4 || f == 5); + float y_offset = raster_distance * float(u_offset) * float(f == 2 || f == 3); + y_offset += raster_distance * float(v_offset) * float(f == 0 || f == 1); + float z_offset = raster_distance * float(v_offset) * float(f == 4 || f == 5 || f == 2 || f == 3); + + vec3 offset = vec3(x_offset, y_offset, z_offset); + + color_sum += get_lighting_color(volume_start, pos + offset, orig_color_sample, normal) / float(raster_points); + } + } + + return color_sum; +} + +vec3 clamp_to_volume(uint volume_start, vec3 position) { + float volume_pos_x = uintBitsToFloat(scene_info.infos[volume_start + 0]); + float volume_pos_y = uintBitsToFloat(scene_info.infos[volume_start + 1]); + float volume_pos_z = uintBitsToFloat(scene_info.infos[volume_start + 2]); + float volume_scale = uintBitsToFloat(scene_info.infos[volume_start + array_descr_offset + color_array_offset - 1]); + + float high_x_border = volume_pos_x + float(scene_info.infos[volume_start + 3]) * volume_scale - 0.501 * volume_scale; + float high_y_border = volume_pos_y + float(scene_info.infos[volume_start + 4]) * volume_scale - 0.501 * volume_scale; + float high_z_border = volume_pos_z + float(scene_info.infos[volume_start + 5]) * volume_scale - 0.501 * volume_scale; + + float low_x_border = float(volume_pos_x) - 0.501 * volume_scale; + float low_y_border = float(volume_pos_y) - 0.501 * volume_scale; + float low_z_border = float(volume_pos_z) - 0.501 * volume_scale; + + return vec3(min(max(position.x, low_x_border), high_x_border), min(max(position.y, low_y_border), high_y_border), min(max(position.z, low_z_border), high_z_border)); +} + +vec2 clamp_to_quad(vec2 raster_pos, uvec2 min_raster_pos, uvec2 max_raster_pos) { + return vec2(max(min_raster_pos.x, min(max_raster_pos.x - 1, raster_pos.x)), max(min_raster_pos.y, min(max_raster_pos.y - 1, raster_pos.y))); +} + +vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4 color_sample, vec3 color_sum) { + float reflectivity = 1.0 - float(color_sample.w) / 255.0; + + if (reflectivity > 0.01) { + vec3 orig_color_sample = vec3(float(color_sample.x) / 255.0, float(color_sample.y) / 255.0, float(color_sample.z) / 255.0); + vec3 reflection_direction = reflect_vector(view_vector, f); + Tracing reflection_tracing = trace_ray(volume_start, pos, reflection_direction, pos_infinity, true); + if (reflection_tracing.has_hit || reflection_tracing.has_transparent_hit) { + vec3 color_from_reflection = diffuse_tracing(reflection_tracing.end_volume, reflection_tracing.end_color, reflection_tracing.end_pos, reflection_tracing.end_facing) * orig_color_sample; + color_sum = color_sum * (1.0 - reflectivity) + color_from_reflection * reflectivity; + } + } + + return color_sum; +} \ No newline at end of file diff --git a/shaders/rt_quad.frag b/shaders/rt_quad.frag index c59b4e4..d5f4a7b 100644 --- a/shaders/rt_quad.frag +++ b/shaders/rt_quad.frag @@ -321,13 +321,34 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl uvec2 end_raster_transparent; vec3 color_mul_transparent; + uint next_volumetric_index = 0; + uint[5] done_volumetrics; + for (int i=0; i < 5; i++) { + done_volumetrics[i] = 0; + } + while (iteration_num < max_iterations) { iteration_num ++; uint compound_num = 0; while (scene_info.infos[volume_index + 6 + max_num_lights + compound_num] != 0 && compound_num < max_num_compounds && iteration_num < max_iterations && !result.has_hit) { - //iteration_num ++; uint compound_start = scene_info.infos[volume_index + 6 + max_num_lights + compound_num]; + + bool already_checked = false; + for (int i=0; i < 5; i++) { + if (compound_start == done_volumetrics[i]) { + already_checked = true; + break; + } + } + if (already_checked) { + compound_num += 1; + continue; + } + done_volumetrics[next_volumetric_index] = compound_start; + next_volumetric_index = (next_volumetric_index + 1) % 5; + + //iteration_num ++; uint oct_tree_index = compounds[compound_start + 8]; uint compound_grid_size = compounds[compound_start]; float compound_scale = uintBitsToFloat(compounds[compound_start + 1]); @@ -820,4 +841,4 @@ void main() { } outColor = vec4(color_sum, 1.0); -} \ No newline at end of file +} diff --git a/shaders/rt_quad_placeholder.frag b/shaders/rt_quad_placeholder.frag new file mode 100644 index 0000000..0f69b04 --- /dev/null +++ b/shaders/rt_quad_placeholder.frag @@ -0,0 +1,51 @@ +#version 450 + +layout(location = 0) in vec2 fragRasterPos; +layout(location = 1) flat in uint fragVolumeStart; +layout(location = 2) in vec3 origPosition; +layout(location = 3) flat in uint facing; +layout(location = 4) flat in uvec2 minRasterPos; +layout(location = 5) flat in uvec2 maxRasterPos; + +layout(location = 0) out vec4 outColor; + +#include rt_lib.frag + +void main() { + vec3 clamped_pos = clamp_to_volume(fragVolumeStart, origPosition); + vec2 clamped_raster_pos = clamp_to_quad(fragRasterPos, minRasterPos, maxRasterPos); + uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, clamped_raster_pos, facing); + vec3 orig_color_sample = vec3(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0); + vec3 color_sum; + + uint orig_neighbor = sample_neighbor_from_scene_info(fragVolumeStart, clamped_raster_pos, facing); + if (orig_neighbor != 0) { + vec3 color_direct = diffuse_tracing(fragVolumeStart, color_roughness, clamped_pos, facing); + + Tracing t = trace_ray(fragVolumeStart, ubo.camera_pos, clamped_pos - ubo.camera_pos, pos_infinity, false); + float opacity = float(color_roughness.w) / 255.0; + vec3 color_seen_through; + if (t.has_hit) { + //color_seen_through = vec3(float(t.end_color.x) / 255.0, float(t.end_color.y) / 255.0, float(t.end_color.z) / 255.0); + + color_seen_through = diffuse_tracing(t.end_volume, t.end_color, t.end_pos, t.end_facing) * orig_color_sample * t.color_mul; + color_seen_through = add_reflection(t.end_direction, t.end_facing, t.end_volume, t.end_pos, t.end_color, color_seen_through); + } + else { + // Todo: hit sky box + color_seen_through = vec3(0.0, 0.0, 0.0); + } + + color_direct = add_reflection(normalize(clamped_pos - ubo.camera_pos), facing, fragVolumeStart, clamped_pos, color_roughness, color_direct); + color_sum = opacity * color_direct + (1.0 - opacity) * color_seen_through; + + //color_sum = color_seen_through; + } + else { + color_sum = diffuse_tracing(fragVolumeStart, color_roughness, clamped_pos, facing); + + color_sum = add_reflection(normalize(clamped_pos - ubo.camera_pos), facing, fragVolumeStart, clamped_pos, color_roughness, color_sum); + } + + outColor = vec4(color_sum, 1.0); +} \ No newline at end of file