diff --git a/build.rs b/build.rs index 3a366b4..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,13 +34,17 @@ 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"); println!("cargo::rerun-if-changed=shaders/rt_compute_grow_two.comp"); println!("cargo::rerun-if-changed=shaders/rt_compute_grow_three.comp"); - println!("cargo::rerun-if-changed=shaders/rt_compute_combine.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(()); @@ -32,7 +58,7 @@ fn main() { std::fs::remove_file("shaders/compiled/rt_compute_grow_one.spv").unwrap_or(()); std::fs::remove_file("shaders/compiled/rt_compute_grow_two.spv").unwrap_or(()); std::fs::remove_file("shaders/compiled/rt_compute_grow_three.spv").unwrap_or(()); - std::fs::remove_file("shaders/compiled/rt_compute_combine.spv").unwrap_or(()); + std::fs::remove_file("shaders/compiled/rt_compute_mempos.spv").unwrap_or(()); if std::env::consts::OS == "windows" { let mut command = Command::new("./shaders/compile.bat"); diff --git a/shaders/compile.bat b/shaders/compile.bat index bb4fe6a..7717e6c 100644 --- a/shaders/compile.bat +++ b/shaders/compile.bat @@ -13,4 +13,4 @@ C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_rasterize.comp -o shader C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_grow_one.comp -o shaders/compiled/rt_compute_grow_one.spv C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_grow_two.comp -o shaders/compiled/rt_compute_grow_two.spv C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_grow_three.comp -o shaders/compiled/rt_compute_grow_three.spv -C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_combine.comp -o shaders/compiled/rt_compute_combine.spv \ No newline at end of file +C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_mempos.comp -o shaders/compiled/rt_compute_mempos.spv \ No newline at end of file diff --git a/shaders/compile.sh b/shaders/compile.sh index 88be158..9507c17 100755 --- a/shaders/compile.sh +++ b/shaders/compile.sh @@ -14,4 +14,4 @@ glslc shaders/rt_compute_rasterize.comp -o shaders/compiled/rt_compute_rasterize glslc shaders/rt_compute_grow_one.comp -o shaders/compiled/rt_compute_grow_one.spv glslc shaders/rt_compute_grow_two.comp -o shaders/compiled/rt_compute_grow_two.spv glslc shaders/rt_compute_grow_three.comp -o shaders/compiled/rt_compute_grow_three.spv -glslc shaders/rt_compute_combine.comp -o shaders/compiled/rt_compute_combine.spv \ No newline at end of file +glslc shaders/rt_compute_mempos.comp -o shaders/compiled/rt_compute_mempos.spv \ No newline at end of file diff --git a/shaders/compiled/frag_rt_quad.spv b/shaders/compiled/frag_rt_quad.spv index 5523307..ed483a0 100644 Binary files a/shaders/compiled/frag_rt_quad.spv and b/shaders/compiled/frag_rt_quad.spv differ diff --git a/shaders/compiled/rt_compute_combine.spv b/shaders/compiled/rt_compute_combine.spv deleted file mode 100644 index 61fd2ee..0000000 Binary files a/shaders/compiled/rt_compute_combine.spv and /dev/null differ diff --git a/shaders/compiled/rt_compute_grow_one.spv b/shaders/compiled/rt_compute_grow_one.spv index 82ea0be..ee4c26c 100644 Binary files a/shaders/compiled/rt_compute_grow_one.spv and b/shaders/compiled/rt_compute_grow_one.spv differ diff --git a/shaders/compiled/rt_compute_grow_three.spv b/shaders/compiled/rt_compute_grow_three.spv index 1a326ca..b3a00fd 100644 Binary files a/shaders/compiled/rt_compute_grow_three.spv and b/shaders/compiled/rt_compute_grow_three.spv differ diff --git a/shaders/compiled/rt_compute_grow_two.spv b/shaders/compiled/rt_compute_grow_two.spv index 136cf1c..08a3187 100644 Binary files a/shaders/compiled/rt_compute_grow_two.spv and b/shaders/compiled/rt_compute_grow_two.spv differ diff --git a/shaders/compiled/rt_compute_mempos.spv b/shaders/compiled/rt_compute_mempos.spv new file mode 100644 index 0000000..e7775cd Binary files /dev/null and b/shaders/compiled/rt_compute_mempos.spv differ diff --git a/shaders/compiled/rt_compute_rasterize.spv b/shaders/compiled/rt_compute_rasterize.spv index 9ad3d50..be99608 100644 Binary files a/shaders/compiled/rt_compute_rasterize.spv and b/shaders/compiled/rt_compute_rasterize.spv differ diff --git a/shaders/rt_compute_combine.comp b/shaders/rt_compute_combine.comp deleted file mode 100644 index c70c19e..0000000 --- a/shaders/rt_compute_combine.comp +++ /dev/null @@ -1,51 +0,0 @@ -#version 450 - -layout(binding = 0) uniform UniformBufferObject { - mat4 model; - mat4 geom_rot; - mat4 view; - mat4 proj; - vec3 camera_pos; - bool[16] use_geom_shader; -} ubo; - -layout(binding = 3) readonly buffer CompoundBuffer { - uint compounds[]; -}; - -layout(binding = 4) readonly buffer ColorBuffer { - uint grid_in[]; -}; - -layout(binding = 9) readonly buffer TransparentBuffer { - bool transparent_grid[]; -}; - -layout(binding = 8) buffer SizeBuffer3D { - uint grid_size_in[]; -}; - -layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in; - -void main() { - uint index = gl_GlobalInvocationID.x; - uint output_offset = 0; - uint compound_start = 0; - // iterate over the compounds and find the work index inside of it - while (index > compounds[compound_start] * compounds[compound_start]) { - output_offset += compounds[compound_start] * compounds[compound_start] * compounds[compound_start] * 2; - index -= compounds[compound_start] * compounds[compound_start]; - compound_start = compounds[compound_start + 2]; - } - // grid pos in the task - uint compound_grid_size = compounds[compound_start]; - float compound_scale = uintBitsToFloat(compounds[compound_start + 1]); - vec3 mid_offset = vec3(compound_scale * 0.5, compound_scale * 0.5, compound_scale * 0.5); - uint x = index % compound_grid_size; - uint y = ((index) % (compound_grid_size * compound_grid_size) - x) / (compound_grid_size); - uint z = (index - x - y * compound_grid_size) / (compound_grid_size * compound_grid_size); - - uint size_x = grid_size_in[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 3]; - uint size_y = grid_size_in[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 3 + 1]; - uint size_z = grid_size_in[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 3 + 2]; -} \ No newline at end of file diff --git a/shaders/rt_compute_grow_one.comp b/shaders/rt_compute_grow_one.comp index f4161f4..6fc2845 100644 --- a/shaders/rt_compute_grow_one.comp +++ b/shaders/rt_compute_grow_one.comp @@ -30,7 +30,7 @@ layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in; void main() { uint index = gl_GlobalInvocationID.x; uint output_offset = 0; - uint compound_start = 0; + uint compound_start = 1; // iterate over the compounds and find the work index inside of it while (index > compounds[compound_start] * compounds[compound_start]) { output_offset += compounds[compound_start] * compounds[compound_start] * compounds[compound_start]; @@ -43,40 +43,11 @@ void main() { vec3 mid_offset = vec3(compound_scale * 0.5, compound_scale * 0.5, compound_scale * 0.5); uint y = index % compound_grid_size; uint z = (index - y) / compound_grid_size; - vec3 compound_pos = vec3(uintBitsToFloat(compounds[compound_start + 5]), uintBitsToFloat(compounds[compound_start + 6]), uintBitsToFloat(compounds[compound_start + 7])); // iterate upwards along the x axis - bool seen_empty = false; - uint start = 0; - uint last_col = 0; + uint sum = 0; for (uint x=0; x < compound_grid_size; x++) { uint color_val = grid_in[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]; - bool transparent = transparent_grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]; - grid_out[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = 0; - // check if we need to stop a volume - if (color_val != 0 && !transparent) { - // check if we are in a volume right now - if (seen_empty) { - // close the current volume - grid_out[output_offset + start * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = x - start; - seen_empty = false; - last_col = 0; - } - } else { - // check if transparency changed - if (seen_empty && transparent && last_col != color_val) { - // if we switch colors close the current volume and prepare for a new one - grid_out[output_offset + start * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = x - start; - seen_empty = false; - } - // start a new volume if we are not in one right now - if (!seen_empty) { - seen_empty = true; - start = x; - last_col = color_val; - } - } - } - if (seen_empty) { - grid_out[output_offset + start * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = compound_grid_size - start; + sum += uint(color_val != 0); + grid_out[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = sum; } } \ No newline at end of file diff --git a/shaders/rt_compute_grow_three.comp b/shaders/rt_compute_grow_three.comp index 0460631..652a4cf 100644 --- a/shaders/rt_compute_grow_three.comp +++ b/shaders/rt_compute_grow_three.comp @@ -32,12 +32,12 @@ layout(binding = 8) buffer SizeBuffer3D { layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in; void main() { - uint index = gl_GlobalInvocationID.x; + uint index = gl_GlobalInvocationID.x * 2 + 1; uint output_offset = 0; - uint compound_start = 0; + uint compound_start = 1; // iterate over the compounds and find the work index inside of it while (index > compounds[compound_start] * compounds[compound_start]) { - output_offset += compounds[compound_start] * compounds[compound_start] * compounds[compound_start] * 2; + output_offset += compounds[compound_start] * compounds[compound_start] * compounds[compound_start]; index -= compounds[compound_start] * compounds[compound_start]; compound_start = compounds[compound_start + 2]; } @@ -47,55 +47,10 @@ void main() { vec3 mid_offset = vec3(compound_scale * 0.5, compound_scale * 0.5, compound_scale * 0.5); uint x = index % compound_grid_size; uint y = (index - x) / compound_grid_size; - vec3 compound_pos = vec3(uintBitsToFloat(compounds[compound_start + 5]), uintBitsToFloat(compounds[compound_start + 6]), uintBitsToFloat(compounds[compound_start + 7])); - // iterate upwards along the x axis - bool seen_empty = false; - uint start = 0; - uint start_x_size = 0; - uint start_y_size = 0; - uint last_col = 0; + // iterate upwards along the z axis + uint sum = 0; for (uint z=0; z < compound_grid_size; z++) { - uint color_val = grid_in[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]; - bool transparent = transparent_grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]; - uint current_x_size = grid_size_in[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 2]; - uint current_y_size = grid_size_in[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 2 + 1]; - - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 3] = 0; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 3 + 1] = 0; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 3 + 2] = 0; - // check if we need to stop a volume - if (color_val != 0 && !transparent) { - // check if we are in a volume right now - if (seen_empty) { - // close the current volume - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3] = start_x_size; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 1] = start_y_size; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 2] = z - start; - seen_empty = false; - last_col = 0; - } - } else { - // check if transparency changed - if (seen_empty && ((transparent && last_col != color_val) || (start_x_size != current_x_size) || (start_y_size != current_y_size))) { - // if we switch colors or size close the current volume and prepare for a new one - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3] = start_x_size; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 1] = start_y_size; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 2] = z - start; - seen_empty = false; - } - // start a new volume if we are not in one right now - if (!seen_empty && current_x_size != 0 && current_y_size != 0) { - seen_empty = true; - start = z; - start_x_size = current_x_size; - start_y_size = current_y_size; - last_col = color_val; - } - } - } - if (seen_empty) { - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3] = start_x_size; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 1] = start_y_size; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + start) * 3 + 2] = compound_grid_size - start; + sum += grid_size_in[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]; + grid_out[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = sum; } } \ No newline at end of file diff --git a/shaders/rt_compute_grow_two.comp b/shaders/rt_compute_grow_two.comp index c2ec2f6..12acbb4 100644 --- a/shaders/rt_compute_grow_two.comp +++ b/shaders/rt_compute_grow_two.comp @@ -32,12 +32,12 @@ layout(binding = 8) readonly buffer SizeBuffer3D { layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in; void main() { - uint index = gl_GlobalInvocationID.x; + uint index = gl_GlobalInvocationID.x * 2 + 1; uint output_offset = 0; - uint compound_start = 0; + uint compound_start = 1; // iterate over the compounds and find the work index inside of it while (index > compounds[compound_start] * compounds[compound_start]) { - output_offset += compounds[compound_start] * compounds[compound_start] * compounds[compound_start] * 2; + output_offset += compounds[compound_start] * compounds[compound_start] * compounds[compound_start]; index -= compounds[compound_start] * compounds[compound_start]; compound_start = compounds[compound_start + 2]; } @@ -47,48 +47,10 @@ void main() { vec3 mid_offset = vec3(compound_scale * 0.5, compound_scale * 0.5, compound_scale * 0.5); uint x = index % compound_grid_size; uint z = (index - x) / compound_grid_size; - vec3 compound_pos = vec3(uintBitsToFloat(compounds[compound_start + 5]), uintBitsToFloat(compounds[compound_start + 6]), uintBitsToFloat(compounds[compound_start + 7])); - // iterate upwards along the x axis - bool seen_empty = false; - uint start = 0; - uint start_x_size = 0; - uint last_col = 0; + // iterate upwards along the y axis + uint sum = 0; for (uint y=0; y < compound_grid_size; y++) { - uint color_val = grid_in[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]; - bool transparent = transparent_grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]; - uint current_x_size = grid_size_in[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]; - - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 2] = 0; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + y * compound_grid_size + z) * 2 + 1] = 0; - // check if we need to stop a volume - if (color_val != 0 && !transparent) { - // check if we are in a volume right now - if (seen_empty) { - // close the current volume - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2] = start_x_size; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2 + 1] = y - start; - seen_empty = false; - last_col = 0; - } - } else { - // check if transparency changed - if (seen_empty && ((transparent && last_col != color_val) || (start_x_size != current_x_size))) { - // if we switch colors or size close the current volume and prepare for a new one - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2] = start_x_size; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2 + 1] = y - start; - seen_empty = false; - } - // start a new volume if we are not in one right now - if (!seen_empty && current_x_size != 0) { - seen_empty = true; - start = y; - start_x_size = current_x_size; - last_col = color_val; - } - } - } - if (seen_empty) { - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2] = start_x_size; - grid_out[output_offset + (x * compound_grid_size * compound_grid_size + start * compound_grid_size + z) * 2 + 1] = compound_grid_size - start; + sum += grid_size_in[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]; + grid_out[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = sum; } } \ No newline at end of file diff --git a/shaders/rt_compute_mempos.comp b/shaders/rt_compute_mempos.comp new file mode 100644 index 0000000..831a827 --- /dev/null +++ b/shaders/rt_compute_mempos.comp @@ -0,0 +1,365 @@ +#version 450 + +layout(binding = 0) uniform UniformBufferObject { + mat4 model; + mat4 geom_rot; + mat4 view; + mat4 proj; + vec3 camera_pos; + bool[16] use_geom_shader; +} ubo; + +layout(binding = 3) readonly buffer CompoundBuffer { + uint compounds[]; +}; + +layout(binding = 4) readonly buffer ColorBuffer { + uint grid_in[]; +}; + +layout(binding = 9) readonly buffer TransparentBuffer { + bool transparent_grid[]; +}; + +layout(binding = 8) readonly buffer SizeBuffer3D { + uint grid_size_in[]; +}; + +layout(binding = 10) buffer OutMemory { + uint out_memory[]; +}; + +layout(binding = 2) readonly buffer SceneInfoBuffer{ + uint infos[]; +} scene_info; + +uint max_num_lights = scene_info.infos[0]; + +layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in; + +uint num_nodes(uint size) { + uint nodes = 0; + uint add_size = 1; + while (size >= 2) { + nodes += add_size; + add_size = add_size * 8; + size = size / 2; + } + + return nodes; +} + +layout(binding = 5) buffer SizedVertices { + float vertices[]; +}; + +layout(binding = 6) buffer Indices { + uint indices[]; +}; + +vec3 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 vec3(val4 / 255.0, val3 / 255.0, val2 / 255.0); +} + +void add_cube(uint cube_num, float scale, vec3 pos, vec3 color) { + // add node info for the cube + //vertice 0 + vertices[(cube_num * 8 + 0) * 11 + 0] = pos.x - 0.5 * scale; + vertices[(cube_num * 8 + 0) * 11 + 1] = pos.y + 0.5 * scale; + vertices[(cube_num * 8 + 0) * 11 + 2] = pos.z + 0.5 * scale; + + vertices[(cube_num * 8 + 0) * 11 + 3] = color.x; + vertices[(cube_num * 8 + 0) * 11 + 4] = color.y; + vertices[(cube_num * 8 + 0) * 11 + 5] = color.z; + + //vertice 1 + vertices[(cube_num * 8 + 1) * 11 + 0] = pos.x + 0.5 * scale; + vertices[(cube_num * 8 + 1) * 11 + 1] = pos.y + 0.5 * scale; + vertices[(cube_num * 8 + 1) * 11 + 2] = pos.z + 0.5 * scale; + + vertices[(cube_num * 8 + 1) * 11 + 3] = color.x; + vertices[(cube_num * 8 + 1) * 11 + 4] = color.y; + vertices[(cube_num * 8 + 1) * 11 + 5] = color.z; + + //vertice 2 + vertices[(cube_num * 8 + 2) * 11 + 0] = pos.x - 0.5 * scale; + vertices[(cube_num * 8 + 2) * 11 + 1] = pos.y - 0.5 * scale; + vertices[(cube_num * 8 + 2) * 11 + 2] = pos.z + 0.5 * scale; + + vertices[(cube_num * 8 + 2) * 11 + 3] = color.x; + vertices[(cube_num * 8 + 2) * 11 + 4] = color.y; + vertices[(cube_num * 8 + 2) * 11 + 5] = color.z; + + //vertice 3 + vertices[(cube_num * 8 + 3) * 11 + 0] = pos.x + 0.5 * scale; + vertices[(cube_num * 8 + 3) * 11 + 1] = pos.y - 0.5 * scale; + vertices[(cube_num * 8 + 3) * 11 + 2] = pos.z + 0.5 * scale; + + vertices[(cube_num * 8 + 3) * 11 + 3] = color.x; + vertices[(cube_num * 8 + 3) * 11 + 4] = color.y; + vertices[(cube_num * 8 + 3) * 11 + 5] = color.z; + + //vertice 4 + vertices[(cube_num * 8 + 4) * 11 + 0] = pos.x - 0.5 * scale; + vertices[(cube_num * 8 + 4) * 11 + 1] = pos.y + 0.5 * scale; + vertices[(cube_num * 8 + 4) * 11 + 2] = pos.z - 0.5 * scale; + + vertices[(cube_num * 8 + 4) * 11 + 3] = color.x; + vertices[(cube_num * 8 + 4) * 11 + 4] = color.y; + vertices[(cube_num * 8 + 4) * 11 + 5] = color.z; + + //vertice 5 + vertices[(cube_num * 8 + 5) * 11 + 0] = pos.x + 0.5 * scale; + vertices[(cube_num * 8 + 5) * 11 + 1] = pos.y + 0.5 * scale; + vertices[(cube_num * 8 + 5) * 11 + 2] = pos.z - 0.5 * scale; + + vertices[(cube_num * 8 + 5) * 11 + 3] = color.x; + vertices[(cube_num * 8 + 5) * 11 + 4] = color.y; + vertices[(cube_num * 8 + 5) * 11 + 5] = color.z; + + //vertice 6 + vertices[(cube_num * 8 + 6) * 11 + 0] = pos.x - 0.5 * scale; + vertices[(cube_num * 8 + 6) * 11 + 1] = pos.y - 0.5 * scale; + vertices[(cube_num * 8 + 6) * 11 + 2] = pos.z - 0.5 * scale; + + vertices[(cube_num * 8 + 6) * 11 + 3] = color.x; + vertices[(cube_num * 8 + 6) * 11 + 4] = color.y; + vertices[(cube_num * 8 + 6) * 11 + 5] = color.z; + + //vertice 7 + vertices[(cube_num * 8 + 7) * 11 + 0] = pos.x + 0.5 * scale; + vertices[(cube_num * 8 + 7) * 11 + 1] = pos.y - 0.5 * scale; + vertices[(cube_num * 8 + 7) * 11 + 2] = pos.z - 0.5 * scale; + + vertices[(cube_num * 8 + 7) * 11 + 3] = color.x; + vertices[(cube_num * 8 + 7) * 11 + 4] = color.y; + vertices[(cube_num * 8 + 7) * 11 + 5] = color.z; + + //add indices for the cube + //top + indices[cube_num * 36 + 0] = cube_num * 8 + 3; + indices[cube_num * 36 + 1] = cube_num * 8 + 0; + indices[cube_num * 36 + 2] = cube_num * 8 + 2; + + indices[cube_num * 36 + 3] = cube_num * 8 + 3; + indices[cube_num * 36 + 4] = cube_num * 8 + 1; + indices[cube_num * 36 + 5] = cube_num * 8 + 0; + + //bottom + indices[cube_num * 36 + 6] = cube_num * 8 + 6; + indices[cube_num * 36 + 7] = cube_num * 8 + 4; + indices[cube_num * 36 + 8] = cube_num * 8 + 7; + + indices[cube_num * 36 + 9] = cube_num * 8 + 4; + indices[cube_num * 36 + 10] = cube_num * 8 + 5; + indices[cube_num * 36 + 11] = cube_num * 8 + 7; + + //left + indices[cube_num * 36 + 12] = cube_num * 8 + 0; + indices[cube_num * 36 + 13] = cube_num * 8 + 4; + indices[cube_num * 36 + 14] = cube_num * 8 + 2; + + indices[cube_num * 36 + 15] = cube_num * 8 + 6; + indices[cube_num * 36 + 16] = cube_num * 8 + 2; + indices[cube_num * 36 + 17] = cube_num * 8 + 4; + + //right + indices[cube_num * 36 + 18] = cube_num * 8 + 1; + indices[cube_num * 36 + 19] = cube_num * 8 + 3; + indices[cube_num * 36 + 20] = cube_num * 8 + 5; + + indices[cube_num * 36 + 21] = cube_num * 8 + 5; + indices[cube_num * 36 + 22] = cube_num * 8 + 3; + indices[cube_num * 36 + 23] = cube_num * 8 + 7; + + //near + indices[cube_num * 36 + 24] = cube_num * 8 + 6; + indices[cube_num * 36 + 25] = cube_num * 8 + 3; + indices[cube_num * 36 + 26] = cube_num * 8 + 2; + + indices[cube_num * 36 + 27] = cube_num * 8 + 3; + indices[cube_num * 36 + 28] = cube_num * 8 + 6; + indices[cube_num * 36 + 29] = cube_num * 8 + 7; + + //far + indices[cube_num * 36 + 30] = cube_num * 8 + 0; + indices[cube_num * 36 + 31] = cube_num * 8 + 1; + indices[cube_num * 36 + 32] = cube_num * 8 + 4; + + indices[cube_num * 36 + 33] = cube_num * 8 + 5; + indices[cube_num * 36 + 34] = cube_num * 8 + 4; + indices[cube_num * 36 + 35] = cube_num * 8 + 1; + +} + +uint cohort_index_from_pos(uint x, uint y, uint z, uint block_size, uint compound_size) { + uint steps = compound_size / block_size; + return (z / block_size) * (steps*steps) + (y / block_size) * steps + (x / block_size); +} + +void main() { + uint index = gl_GlobalInvocationID.x; + uint output_offset = 1; + uint input_offset = 0; + uint compound_start = 1; + + uint nodes = num_nodes(compounds[compound_start]); + // iterate over the compounds and find the work index inside of it + while (index > nodes) { + input_offset += compounds[compound_start] * compounds[compound_start] * compounds[compound_start]; + index -= nodes; + compound_start = compounds[compound_start + 2]; + nodes = num_nodes(compounds[compound_start]); + } + + output_offset = compounds[compound_start + 8]; + + uint compound_grid_size = compounds[compound_start]; + uint parent_start = 0; + uint cohort_start = 0; + uint cohort_index = index; + uint size = compounds[compound_start]; + nodes = 0; + uint add_size = 1; + while (cohort_index >= add_size) { + nodes += add_size; + cohort_index -= add_size; + parent_start = cohort_start; + cohort_start = nodes * 9; + add_size *= 8; + size = size / 2; + } + + uint steps = compounds[compound_start] / size; + + float compound_scale = uintBitsToFloat(compounds[compound_start + 1]); + vec3 mid_offset = vec3(compound_scale * 0.5, compound_scale * 0.5, compound_scale * 0.5); + + uint x_no_offset = (cohort_index % steps) * size; + uint y_no_offset = (((cohort_index - (cohort_index % steps)) % (steps * steps)) / (steps)) * size; + uint z_no_offset = (((cohort_index - (cohort_index % (steps * steps)))) / (steps * steps)) * size; + + uint parent_size = size * 2; + uint parent_steps = compounds[compound_start] / parent_size; + uint x_parent = uint(floor(float(x_no_offset) / float(parent_size))) * parent_size; + uint y_parent = uint(floor(float(y_no_offset) / float(parent_size))) * parent_size; + uint z_parent = uint(floor(float(z_no_offset) / float(parent_size))) * parent_size; + + uint parent = output_offset + parent_start + cohort_index_from_pos(x_parent, y_parent, z_parent, parent_size, compound_grid_size) * 9;; + if (size == compounds[compound_start]) { + parent = 0; + } + + // plus one size offset, since we want to place the nodes at the far end. This aligns with the iteration directions in the previous shaders + uint x = x_no_offset + (size - 1); + uint y = y_no_offset + (size - 1); + uint z = z_no_offset + (size - 1); + + // sum of all elements with coordinates lower than x, y, z + uint contained_entries = grid_size_in[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]; + if (z > size) { + // remove contained from z neighbor + contained_entries = contained_entries - grid_size_in[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z - size]; + } + + if (y > size) { + if (z > size) { + // add back the section we will remove twice + contained_entries = contained_entries + int(grid_size_in[input_offset + x * compound_grid_size * compound_grid_size + (y - size) * compound_grid_size + z - size]); + } + // remove contained from y neighbor + contained_entries = contained_entries - int(grid_size_in[input_offset + x * compound_grid_size * compound_grid_size + (y - size) * compound_grid_size + z]); + } + + if (x > size) { + if (z > size) { + // add the portion already removed through the z neighbor + contained_entries = contained_entries + grid_size_in[input_offset + (x - size) * compound_grid_size * compound_grid_size + y * compound_grid_size + z - size]; + } + + if (y > size) { + // add the portion already removed by the y neighbor + contained_entries = contained_entries + grid_size_in[input_offset + (x - size) * compound_grid_size * compound_grid_size + (y - size) * compound_grid_size + z]; + + if (z > size) { + // remove the portion already added through the z neighbor + contained_entries = contained_entries - grid_size_in[input_offset + (x - size) * compound_grid_size * compound_grid_size + (y - size) * compound_grid_size + z - size]; + } + } + + // remove contained from x neighbor + contained_entries = contained_entries - grid_size_in[input_offset + (x - size) * compound_grid_size * compound_grid_size + y * compound_grid_size + z]; + } + + if (contained_entries > 0) { + out_memory[output_offset + cohort_start + cohort_index * 9 + 0] = parent; + + if (size > 2) { + // add child node reference + uint child_size = size / 2; + uint cohort_end = cohort_start + 9 * add_size; + out_memory[output_offset + cohort_start + cohort_index * 9 + 1] = output_offset + cohort_end + cohort_index_from_pos(x_no_offset, y_no_offset, z_no_offset, child_size, compound_grid_size) * 9; // xyz + out_memory[output_offset + cohort_start + cohort_index * 9 + 2] = output_offset + cohort_end + cohort_index_from_pos(x_no_offset + child_size, y_no_offset, z_no_offset, child_size, compound_grid_size) * 9; // Xyz + out_memory[output_offset + cohort_start + cohort_index * 9 + 3] = output_offset + cohort_end + cohort_index_from_pos(x_no_offset, y_no_offset + child_size, z_no_offset, child_size, compound_grid_size) * 9; // xYz + out_memory[output_offset + cohort_start + cohort_index * 9 + 4] = output_offset + cohort_end + cohort_index_from_pos(x_no_offset + child_size, y_no_offset + child_size, z_no_offset, child_size, compound_grid_size) * 9; // XYz + out_memory[output_offset + cohort_start + cohort_index * 9 + 5] = output_offset + cohort_end + cohort_index_from_pos(x_no_offset, y_no_offset, z_no_offset + child_size, child_size, compound_grid_size) * 9; // xyZ + out_memory[output_offset + cohort_start + cohort_index * 9 + 6] = output_offset + cohort_end + cohort_index_from_pos(x_no_offset + child_size, y_no_offset, z_no_offset + child_size, child_size, compound_grid_size) * 9; // XyZ + out_memory[output_offset + cohort_start + cohort_index * 9 + 7] = output_offset + cohort_end + cohort_index_from_pos(x_no_offset, y_no_offset + child_size, z_no_offset + child_size, child_size, compound_grid_size) * 9; // xYZ + out_memory[output_offset + cohort_start + cohort_index * 9 + 8] = output_offset + cohort_end + cohort_index_from_pos(x_no_offset + child_size, y_no_offset + child_size, z_no_offset + child_size, child_size, compound_grid_size) * 9; // XYZ + + } else { + // copy color values and add cubes to rendering + out_memory[output_offset + cohort_start + cohort_index * 9 + 1] = grid_in[input_offset + (x - 1) * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + (z - 1)]; // xyz + out_memory[output_offset + cohort_start + cohort_index * 9 + 2] = grid_in[input_offset + (x - 0) * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + (z - 1)]; // Xyz + out_memory[output_offset + cohort_start + cohort_index * 9 + 3] = grid_in[input_offset + (x - 1) * compound_grid_size * compound_grid_size + (y - 0) * compound_grid_size + (z - 1)]; // xYz + out_memory[output_offset + cohort_start + cohort_index * 9 + 4] = grid_in[input_offset + (x - 0) * compound_grid_size * compound_grid_size + (y - 0) * compound_grid_size + (z - 1)]; // XYz + out_memory[output_offset + cohort_start + cohort_index * 9 + 5] = grid_in[input_offset + (x - 1) * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + (z - 0)]; // xyZ + out_memory[output_offset + cohort_start + cohort_index * 9 + 6] = grid_in[input_offset + (x - 0) * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + (z - 0)]; // XyZ + out_memory[output_offset + cohort_start + cohort_index * 9 + 7] = grid_in[input_offset + (x - 1) * compound_grid_size * compound_grid_size + (y - 0) * compound_grid_size + (z - 0)]; // xYZ + out_memory[output_offset + cohort_start + cohort_index * 9 + 8] = grid_in[input_offset + (x - 0) * compound_grid_size * compound_grid_size + (y - 0) * compound_grid_size + (z - 0)]; // XYZ + + vec3 compound_pos = vec3(uintBitsToFloat(compounds[compound_start + 5]), uintBitsToFloat(compounds[compound_start + 6]), uintBitsToFloat(compounds[compound_start + 7])); + vec3 check_pos = compound_pos + vec3(float(x) * compound_scale, float(y) * compound_scale, float(z) * compound_scale) + mid_offset; + if (out_memory[output_offset + cohort_start + cohort_index * 9 + 1] != 0) { + add_cube(input_offset + (z - 1) * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + (x - 1), compound_scale, check_pos - vec3(1.0, 1.0, 1.0) * compound_scale, unpack_color(out_memory[output_offset + cohort_start + cohort_index * 9 + 1])); + } + if (out_memory[output_offset + cohort_start + cohort_index * 9 + 2] != 0) { + add_cube(input_offset + (z - 1) * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + (x - 0), compound_scale, check_pos - vec3(0.0, 1.0, 1.0) * compound_scale, unpack_color(out_memory[output_offset + cohort_start + cohort_index * 9 + 2])); + } + if (out_memory[output_offset + cohort_start + cohort_index * 9 + 3] != 0) { + add_cube(input_offset + (z - 1) * compound_grid_size * compound_grid_size + (y - 0) * compound_grid_size + (x - 1), compound_scale, check_pos - vec3(1.0, 0.0, 1.0) * compound_scale, unpack_color(out_memory[output_offset + cohort_start + cohort_index * 9 + 3])); + } + if (out_memory[output_offset + cohort_start + cohort_index * 9 + 4] != 0) { + add_cube(input_offset + (z - 1) * compound_grid_size * compound_grid_size + (y - 0) * compound_grid_size + (x - 0), compound_scale, check_pos - vec3(0.0, 0.0, 1.0) * compound_scale, unpack_color(out_memory[output_offset + cohort_start + cohort_index * 9 + 4])); + } + if (out_memory[output_offset + cohort_start + cohort_index * 9 + 5] != 0) { + add_cube(input_offset + (z - 0) * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + (x - 1), compound_scale, check_pos - vec3(1.0, 1.0, 0.0) * compound_scale, unpack_color(out_memory[output_offset + cohort_start + cohort_index * 9 + 5])); + } + if (out_memory[output_offset + cohort_start + cohort_index * 9 + 6] != 0) { + add_cube(input_offset + (z - 0) * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + (x - 0), compound_scale, check_pos - vec3(0.0, 1.0, 0.0) * compound_scale, unpack_color(out_memory[output_offset + cohort_start + cohort_index * 9 + 6])); + } + if (out_memory[output_offset + cohort_start + cohort_index * 9 + 7] != 0) { + add_cube(input_offset + (z - 0) * compound_grid_size * compound_grid_size + (y - 0) * compound_grid_size + (x - 1), compound_scale, check_pos - vec3(1.0, 0.0, 0.0) * compound_scale, unpack_color(out_memory[output_offset + cohort_start + cohort_index * 9 + 7])); + } + if (out_memory[output_offset + cohort_start + cohort_index * 9 + 8] != 0) { + add_cube(input_offset + (z - 0) * compound_grid_size * compound_grid_size + (y - 0) * compound_grid_size + (x - 0), compound_scale, check_pos - vec3(0.0, 0.0, 0.0) * compound_scale, unpack_color(out_memory[output_offset + cohort_start + cohort_index * 9 + 8])); + } + } + } else { + out_memory[output_offset + cohort_start + cohort_index * 9 + 0] = 0; + out_memory[output_offset + cohort_start + cohort_index * 9 + 1] = 0; + out_memory[output_offset + cohort_start + cohort_index * 9 + 2] = 0; + out_memory[output_offset + cohort_start + cohort_index * 9 + 3] = 0; + out_memory[output_offset + cohort_start + cohort_index * 9 + 4] = 0; + out_memory[output_offset + cohort_start + cohort_index * 9 + 5] = 0; + out_memory[output_offset + cohort_start + cohort_index * 9 + 6] = 0; + out_memory[output_offset + cohort_start + cohort_index * 9 + 7] = 0; + out_memory[output_offset + cohort_start + cohort_index * 9 + 8] = 0; + } +} \ No newline at end of file diff --git a/shaders/rt_compute_rasterize.comp b/shaders/rt_compute_rasterize.comp index 8603805..836a54c 100644 --- a/shaders/rt_compute_rasterize.comp +++ b/shaders/rt_compute_rasterize.comp @@ -9,7 +9,7 @@ layout(binding = 0) uniform UniformBufferObject { bool[16] use_geom_shader; } ubo; -layout(binding = 3) readonly buffer SceneInfoBuffer { +layout(binding = 3) readonly buffer CompoundBuffer { uint compounds[]; }; @@ -175,7 +175,7 @@ void add_cube(uint cube_num, float scale, vec3 pos, vec3 color) { void main() { uint index = gl_GlobalInvocationID.x; uint output_offset = 0; - uint compound_start = 0; + uint compound_start = 1; // iterate over the compounds and find the work index inside of it while (index > compounds[compound_start] * compounds[compound_start]) { output_offset += compounds[compound_start] * compounds[compound_start] * compounds[compound_start]; @@ -349,7 +349,7 @@ void main() { if (render) { grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = color_int; transparent_grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = transparent; - add_cube(output_offset + index * compound_grid_size + z, compound_scale, check_pos, color); + //add_cube(output_offset + index * compound_grid_size + z, compound_scale, check_pos, color); } else { grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = 0; transparent_grid[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = false; diff --git a/shaders/rt_lib.frag b/shaders/rt_lib.frag new file mode 100644 index 0000000..e2acfd1 --- /dev/null +++ b/shaders/rt_lib.frag @@ -0,0 +1,855 @@ +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; +const uint absolute_max_compounds = 10; +uint max_num_compounds = min(scene_info.infos[6], absolute_max_compounds); + +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[absolute_max_compounds] done_volumetrics; + for (int i=0; i < max_num_compounds; i++) { + done_volumetrics[i] = 0; + } + + uint[absolute_max_compounds] compound_starts; + float[absolute_max_compounds] hit_factors; + bool[absolute_max_compounds] is_x_hits; + bool[absolute_max_compounds] is_y_hits; + bool[absolute_max_compounds] is_z_hits; + bool[absolute_max_compounds] hits_inside; + + while (iteration_num < max_iterations) { + iteration_num ++; + + for (int i=0; i < max_num_compounds; i++) { + compound_starts[i] = 0; + hit_factors[i] = 0.0; + is_x_hits[i] = false; + is_y_hits[i] = false; + is_z_hits[i] = false; + hits_inside[i] = false; + } + + uint compound_num = 0; + // go over the borders by this amount + float overstep = 0.00001 / length(direction); + uint hits = 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 < max_num_compounds; i++) { + if (compound_start == done_volumetrics[i]) { + already_checked = true; + break; + } + } + if (already_checked) { + compound_num += 1; + continue; + } + + //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; + + 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 + 10.0 * overstep * direction; + bool is_x_hit = false; + bool is_y_hit = false; + bool is_z_hit = false; + bool hit_inside = false; + float hit_factor; + // check that either the hit is in range or we are inside of the compound from the start + 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; + hit_factor = 10.0 * overstep; + } 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; + hit_factor = x_factor; + } + + 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; + hit_factor = y_factor; + } + + 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; + hit_factor = z_factor; + } + } + + compound_starts[hits] = compound_start; + hit_factors[hits] = hit_factor; + is_x_hits[hits] = is_x_hit; + is_y_hits[hits] = is_y_hit; + is_z_hits[hits] = is_z_hit; + hits_inside[hits] = hit_inside; + hits += 1 * uint(hit_inside); + + done_volumetrics[next_volumetric_index] = compound_start; + next_volumetric_index = (next_volumetric_index + 1) % max_num_compounds; + + compound_num += 1; + } + + for (int i =0; i < hits; i++) { + if (result.has_hit) { + break; + } + // find encounters in order + float min_factor = max_factor; + uint min_index = 0; + for (int j = 0; j < hits; j++) { + if (hit_factors[j] < min_factor) { + min_factor = hit_factors[j]; + min_index = j; + } + } + // set up the compound + uint compound_start = compound_starts[min_index]; + bool is_x_hit = is_x_hits[min_index]; + bool is_y_hit = is_y_hits[min_index]; + bool is_z_hit = is_z_hits[min_index]; + 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])); + vec3 intersection_pos = pos + hit_factors[min_index] * direction; + // invalidate the min found + hit_factors[min_index] = max_factor; + + 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; + } + + 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); + + // clear volumetrics for reevaluation + for (int i=0; i < max_num_compounds; i++) { + done_volumetrics[i] = 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 378ccc8..5fad61a 100644 --- a/shaders/rt_quad.frag +++ b/shaders/rt_quad.frag @@ -24,13 +24,19 @@ layout(binding = 0) uniform UniformBufferObject { // 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 = 4) buffer SceneInfoBuffer2 { - uint infos[]; -} scene_info2; +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 @@ -41,6 +47,8 @@ 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; +const uint absolute_max_compounds = 10; +uint max_num_compounds = min(scene_info.infos[6], absolute_max_compounds); uvec4 unpack_color(uint val) { // left most 8 bits first @@ -52,7 +60,7 @@ uvec4 unpack_color(uint val) { return uvec4(val4, val3, val2, val1); } -uint array_descr_offset = 6 + max_num_lights; +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) { @@ -204,6 +212,63 @@ vec3 reflect_vector(vec3 direction, uint 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; @@ -257,8 +322,303 @@ 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[absolute_max_compounds] done_volumetrics; + for (int i=0; i < max_num_compounds; i++) { + done_volumetrics[i] = 0; + } + + uint[absolute_max_compounds] compound_starts; + float[absolute_max_compounds] hit_factors; + bool[absolute_max_compounds] is_x_hits; + bool[absolute_max_compounds] is_y_hits; + bool[absolute_max_compounds] is_z_hits; + bool[absolute_max_compounds] hits_inside; + while (iteration_num < max_iterations) { iteration_num ++; + + for (int i=0; i < max_num_compounds; i++) { + compound_starts[i] = 0; + hit_factors[i] = 0.0; + is_x_hits[i] = false; + is_y_hits[i] = false; + is_z_hits[i] = false; + hits_inside[i] = false; + } + + uint compound_num = 0; + // go over the borders by this amount + float overstep = 0.00001 / length(direction); + uint hits = 0; + // todo needs depth ordering of volumetrics inside of the volume + 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 < max_num_compounds; i++) { + if (compound_start == done_volumetrics[i]) { + already_checked = true; + break; + } + } + if (already_checked) { + compound_num += 1; + continue; + } + + //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; + + 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 + 10.0 * overstep * direction; + bool is_x_hit = false; + bool is_y_hit = false; + bool is_z_hit = false; + bool hit_inside = false; + float hit_factor; + // check that either the hit is in range or we are inside of the compound from the start + 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; + hit_factor = 10.0 * overstep; + } 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; + hit_factor = x_factor; + } + + 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; + hit_factor = y_factor; + } + + 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; + hit_factor = z_factor; + } + } + + compound_starts[hits] = compound_start; + hit_factors[hits] = hit_factor; + is_x_hits[hits] = is_x_hit; + is_y_hits[hits] = is_y_hit; + is_z_hits[hits] = is_z_hit; + hits_inside[hits] = hit_inside; + hits += 1 * uint(hit_inside); + + done_volumetrics[next_volumetric_index] = compound_start; + next_volumetric_index = (next_volumetric_index + 1) % max_num_compounds; + + compound_num += 1; + } + + for (int i =0; i < hits; i++) { + if (result.has_hit) { + break; + } + // find encounters in order + float min_factor = max_factor; + uint min_index = 0; + for (int j = 0; j < hits; j++) { + if (hit_factors[j] < min_factor) { + min_factor = hit_factors[j]; + min_index = j; + } + } + // set up the compound + uint compound_start = compound_starts[min_index]; + bool is_x_hit = is_x_hits[min_index]; + bool is_y_hit = is_y_hits[min_index]; + bool is_z_hit = is_z_hits[min_index]; + 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])); + vec3 intersection_pos = pos + hit_factors[min_index] * direction; + // invalidate the min found + hit_factors[min_index] = max_factor; + + 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; + } + + 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; @@ -267,12 +627,18 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl 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)) { @@ -369,6 +735,11 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl z_pos = direction.z > 0.0; z_null = (direction.z == 0.0); + + // clear volumetrics for reevaluation + for (int i=0; i < max_num_compounds; i++) { + done_volumetrics[i] = 0; + } } else { break; } @@ -437,8 +808,7 @@ vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sa return color_sum; } -vec3 diffuse_tracing(uint volume_start, uvec2 raster_pos, vec3 pos, uint f) { - uvec4 color_roughness = sample_color_from_scene_info(volume_start, raster_pos, f); +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); @@ -459,10 +829,6 @@ vec3 diffuse_tracing(uint volume_start, uvec2 raster_pos, vec3 pos, uint f) { return color_sum; } -vec3 diffuse_tracing(uint volume_start, vec2 raster_pos, vec3 pos, uint f) { - return diffuse_tracing(volume_start, uvec2(uint(floor(raster_pos.x)), uint(floor(raster_pos.y))), pos, f); -} - 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]); @@ -492,7 +858,7 @@ vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4 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_raster, reflection_tracing.end_pos, reflection_tracing.end_facing) * orig_color_sample; + 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; } } @@ -509,13 +875,15 @@ void main() { uint orig_neighbor = sample_neighbor_from_scene_info(fragVolumeStart, clamped_raster_pos, facing); if (orig_neighbor != 0) { - vec3 color_direct = diffuse_tracing(fragVolumeStart, clamped_raster_pos, clamped_pos, facing); + 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 = diffuse_tracing(t.end_volume, t.end_raster, t.end_pos, t.end_facing) * orig_color_sample * t.color_mul; + //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 { @@ -525,12 +893,14 @@ void main() { 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, clamped_raster_pos, clamped_pos, facing); + 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 +} 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 diff --git a/src/app_data.rs b/src/app_data.rs index 0dafc31..2bfad28 100644 --- a/src/app_data.rs +++ b/src/app_data.rs @@ -27,7 +27,7 @@ pub struct AppData { pub pipeline_compute_grow_one: vk::Pipeline, pub pipeline_compute_grow_two: vk::Pipeline, pub pipeline_compute_grow_three: vk::Pipeline, - pub pipeline_compute_combine: vk::Pipeline, + pub pipeline_compute_mempos: vk::Pipeline, pub framebuffers: Vec<vk::Framebuffer>, pub command_pool: vk::CommandPool, @@ -60,6 +60,9 @@ pub struct AppData { pub compute_out_storage_buffers_size_three: Vec<vk::Buffer>, pub compute_out_storage_buffers_memory_size_three: Vec<vk::DeviceMemory>, + pub compute_out_storage_buffers_oct_tree: Vec<vk::Buffer>, + pub compute_out_storage_buffers_memory_oct_tree: Vec<vk::DeviceMemory>, + pub compute_out_cuboid_buffers: Vec<vk::Buffer>, pub compute_out_cuboid_buffers_memory: Vec<vk::DeviceMemory>, @@ -92,8 +95,11 @@ pub struct AppData { pub compute_task_one_size: usize, pub compute_task_one_out_buffer_size: u64, pub compute_task_one_out_size: u64, + pub compute_task_oct_tree_size: u64, + pub compute_task_oct_tree_nodes: u64, // values passed to shader pub num_lights_per_volume: u32, + pub num_compound_per_volume: u32, pub min_light_weight: f32, pub max_iterations_per_light: u32, pub diffuse_raster_steps: u32, diff --git a/src/buffer.rs b/src/buffer.rs index 8374162..9326b71 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -206,13 +206,13 @@ pub unsafe fn create_descriptor_set_layout( .binding(2) .descriptor_type(vk::DescriptorType::STORAGE_BUFFER) .descriptor_count(1) - .stage_flags(vk::ShaderStageFlags::FRAGMENT); + .stage_flags(vk::ShaderStageFlags::FRAGMENT | vk::ShaderStageFlags::COMPUTE); let storage_binding_compute_in = vk::DescriptorSetLayoutBinding::builder() .binding(3) .descriptor_type(vk::DescriptorType::STORAGE_BUFFER) .descriptor_count(1) - .stage_flags(vk::ShaderStageFlags::COMPUTE); + .stage_flags(vk::ShaderStageFlags::COMPUTE | vk::ShaderStageFlags::FRAGMENT); let storage_binding_compute_out_color = vk::DescriptorSetLayoutBinding::builder() .binding(4) @@ -250,7 +250,13 @@ pub unsafe fn create_descriptor_set_layout( .descriptor_count(1) .stage_flags(vk::ShaderStageFlags::FRAGMENT | vk::ShaderStageFlags::COMPUTE); - let bindings = &[ubo_binding, sampler_binding, storage_binding_render, storage_binding_compute_in, storage_binding_compute_out_color, storage_binding_compute_cuboid_out, storage_binding_compute_cuboid_index_out, storage_binding_compute_out_size_two, storage_binding_compute_out_size_three, storage_binding_compute_out_size_transparent]; + let storage_binding_compute_out_oct_tree = vk::DescriptorSetLayoutBinding::builder() + .binding(10) + .descriptor_type(vk::DescriptorType::STORAGE_BUFFER) + .descriptor_count(1) + .stage_flags(vk::ShaderStageFlags::FRAGMENT | vk::ShaderStageFlags::COMPUTE); + + let bindings = &[ubo_binding, sampler_binding, storage_binding_render, storage_binding_compute_in, storage_binding_compute_out_color, storage_binding_compute_cuboid_out, storage_binding_compute_cuboid_index_out, storage_binding_compute_out_size_two, storage_binding_compute_out_size_three, storage_binding_compute_out_size_transparent, storage_binding_compute_out_oct_tree]; let info = vk::DescriptorSetLayoutCreateInfo::builder() .bindings(bindings); @@ -311,6 +317,9 @@ pub unsafe fn create_storage_buffers( data.compute_out_cuboid_index_buffers.clear(); data.compute_out_cuboid_index_buffers_memory.clear(); + data.compute_out_storage_buffers_oct_tree.clear(); + data.compute_out_storage_buffers_memory_oct_tree.clear(); + for _ in 0..data.swapchain_images.len() { let (storage_buffer, storage_buffer_memory) = create_buffer( instance, @@ -363,7 +372,7 @@ pub unsafe fn create_storage_buffers( instance, device, data, - (size_of::<u32>() * 2) as u64 * data.compute_task_one_out_buffer_size.max(1), + (size_of::<u32>()) as u64 * data.compute_task_one_out_buffer_size.max(1), vk::BufferUsageFlags::STORAGE_BUFFER, vk::MemoryPropertyFlags::DEVICE_LOCAL, )?; @@ -375,7 +384,7 @@ pub unsafe fn create_storage_buffers( instance, device, data, - (size_of::<u32>() * 3) as u64 * data.compute_task_one_out_buffer_size.max(1), + (size_of::<u32>()) as u64 * data.compute_task_one_out_buffer_size.max(1), vk::BufferUsageFlags::STORAGE_BUFFER, vk::MemoryPropertyFlags::DEVICE_LOCAL, )?; @@ -383,6 +392,18 @@ pub unsafe fn create_storage_buffers( data.compute_out_storage_buffers_size_three.push(storage_buffer); data.compute_out_storage_buffers_memory_size_three.push(storage_buffer_memory); + let (storage_buffer, storage_buffer_memory) = create_buffer( + instance, + device, + data, + (size_of::<u32>()) as u64 * data.compute_task_oct_tree_size.max(1), + vk::BufferUsageFlags::STORAGE_BUFFER, + vk::MemoryPropertyFlags::DEVICE_LOCAL, + )?; + + data.compute_out_storage_buffers_oct_tree.push(storage_buffer); + data.compute_out_storage_buffers_memory_oct_tree.push(storage_buffer_memory); + let (storage_buffer, storage_buffer_memory) = create_buffer( instance, device, @@ -551,8 +572,12 @@ pub unsafe fn create_descriptor_pool(device: &Device, data: &mut app_data::AppDa let compute_out_storage_transparent_size = vk::DescriptorPoolSize::builder() .type_(vk::DescriptorType::STORAGE_BUFFER) .descriptor_count(data.swapchain_images.len() as u32); + + let compute_out_storage_oct_tree = vk::DescriptorPoolSize::builder() + .type_(vk::DescriptorType::STORAGE_BUFFER) + .descriptor_count(data.swapchain_images.len() as u32); - let pool_sizes = &[ubo_size, sampler_size, render_storage_size, compute_in_storage_size, compute_out_storage_color_size, compute_out_cuboid_size, compute_out_cuboid_index_size, compute_out_storage_size_two_size, compute_out_storage_size_three_size, compute_out_storage_transparent_size]; + let pool_sizes = &[ubo_size, sampler_size, render_storage_size, compute_in_storage_size, compute_out_storage_color_size, compute_out_cuboid_size, compute_out_cuboid_index_size, compute_out_storage_size_two_size, compute_out_storage_size_three_size, compute_out_storage_transparent_size, compute_out_storage_oct_tree]; let info = vk::DescriptorPoolCreateInfo::builder() .pool_sizes(pool_sizes) .max_sets(data.swapchain_images.len() as u32); @@ -625,7 +650,7 @@ pub unsafe fn create_descriptor_sets(device: &Device, data: &mut app_data::AppDa let info = vk::DescriptorBufferInfo::builder() .buffer(data.compute_out_storage_buffers_color[i]) .offset(0) - .range((size_of::<u32>() * 3) as u64 * data.compute_task_one_out_buffer_size.max(1)); + .range((size_of::<u32>()) as u64 * data.compute_task_one_out_buffer_size.max(1)); let storage_info = &[info]; let storage_write_compute_out_color = vk::WriteDescriptorSet::builder() @@ -638,7 +663,7 @@ pub unsafe fn create_descriptor_sets(device: &Device, data: &mut app_data::AppDa let info = vk::DescriptorBufferInfo::builder() .buffer(data.compute_out_storage_buffers_size_two[i]) .offset(0) - .range((size_of::<u32>() * 2) as u64 * data.compute_task_one_out_buffer_size.max(1)); + .range((size_of::<u32>()) as u64 * data.compute_task_one_out_buffer_size.max(1)); let storage_info = &[info]; let storage_write_compute_out_size_two = vk::WriteDescriptorSet::builder() @@ -651,7 +676,7 @@ pub unsafe fn create_descriptor_sets(device: &Device, data: &mut app_data::AppDa let info = vk::DescriptorBufferInfo::builder() .buffer(data.compute_out_storage_buffers_size_three[i]) .offset(0) - .range((size_of::<u32>() * 3) as u64 * data.compute_task_one_out_buffer_size.max(1)); + .range((size_of::<u32>()) as u64 * data.compute_task_one_out_buffer_size.max(1)); let storage_info = &[info]; let storage_write_compute_out_size_three = vk::WriteDescriptorSet::builder() @@ -664,7 +689,7 @@ pub unsafe fn create_descriptor_sets(device: &Device, data: &mut app_data::AppDa let info = vk::DescriptorBufferInfo::builder() .buffer(data.compute_out_storage_buffers_transparent[i]) .offset(0) - .range((size_of::<bool>() * 3) as u64 * data.compute_task_one_out_buffer_size.max(1)); + .range((size_of::<bool>()) as u64 * data.compute_task_one_out_buffer_size.max(1)); let storage_info = &[info]; let storage_write_compute_out_transparent = vk::WriteDescriptorSet::builder() @@ -674,6 +699,19 @@ pub unsafe fn create_descriptor_sets(device: &Device, data: &mut app_data::AppDa .descriptor_type(vk::DescriptorType::STORAGE_BUFFER) .buffer_info(storage_info); + let info = vk::DescriptorBufferInfo::builder() + .buffer(data.compute_out_storage_buffers_oct_tree[i]) + .offset(0) + .range((size_of::<u32>()) as u64 * data.compute_task_oct_tree_size.max(1)); + let storage_info = &[info]; + + let storage_write_compute_out_oct_tree = vk::WriteDescriptorSet::builder() + .dst_set(data.descriptor_sets[i]) + .dst_binding(10) + .dst_array_element(0) + .descriptor_type(vk::DescriptorType::STORAGE_BUFFER) + .buffer_info(storage_info); + let info = vk::DescriptorBufferInfo::builder() .buffer(data.compute_out_cuboid_buffers[i]) .offset(0) @@ -702,7 +740,7 @@ pub unsafe fn create_descriptor_sets(device: &Device, data: &mut app_data::AppDa device.update_descriptor_sets( - &[ubo_write, sampler_write, storage_write_render, storage_write_compute_in, storage_write_compute_out_color, storage_write_compute_cuboid_out, storage_write_compute_cuboid_index_out, storage_write_compute_out_size_two, storage_write_compute_out_size_three, storage_write_compute_out_transparent], + &[ubo_write, sampler_write, storage_write_render, storage_write_compute_in, storage_write_compute_out_color, storage_write_compute_cuboid_out, storage_write_compute_cuboid_index_out, storage_write_compute_out_size_two, storage_write_compute_out_size_three, storage_write_compute_out_transparent, storage_write_compute_out_oct_tree], &[] as &[vk::CopyDescriptorSet], ); } diff --git a/src/command_buffer.rs b/src/command_buffer.rs index e477391..ae41ae1 100644 --- a/src/command_buffer.rs +++ b/src/command_buffer.rs @@ -85,14 +85,6 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa .size(vk::WHOLE_SIZE as u64) .build(); - device.cmd_pipeline_barrier(*command_buffer, - vk::PipelineStageFlags::COMPUTE_SHADER, - vk::PipelineStageFlags::VERTEX_INPUT, - vk::DependencyFlags::DEVICE_GROUP, - &[] as &[vk::MemoryBarrier], - &[buffer_memory_barrier_index, buffer_memory_barrier_vertex], - &[] as &[vk::ImageMemoryBarrier]); - // compute storage barrier let buffer_memory_barrier_color = vk::BufferMemoryBarrier::builder() .buffer(data.compute_out_storage_buffers_color[i]) @@ -155,7 +147,14 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa &[data.descriptor_sets[i]], &[]); - device.cmd_dispatch(*command_buffer, (data.compute_task_one_size as f64 / 16.0).ceil() as u32, 1, 1); + device.cmd_dispatch(*command_buffer, ((data.compute_task_one_size / 2) as f64 / 16.0).ceil() as u32, 1, 1); + + let buffer_memory_barrier_in = vk::BufferMemoryBarrier::builder() + .buffer(data.compute_out_storage_buffers_size_three[i]) + .src_access_mask(vk::AccessFlags::SHADER_READ) + .dst_access_mask(vk::AccessFlags::SHADER_WRITE) + .size(vk::WHOLE_SIZE as u64) + .build(); let buffer_memory_barrier_out = vk::BufferMemoryBarrier::builder() .buffer(data.compute_out_storage_buffers_size_two[i]) @@ -169,7 +168,7 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa vk::PipelineStageFlags::COMPUTE_SHADER, vk::DependencyFlags::DEVICE_GROUP, &[] as &[vk::MemoryBarrier], - &[buffer_memory_barrier_out], + &[buffer_memory_barrier_in, buffer_memory_barrier_out], &[] as &[vk::ImageMemoryBarrier]); // grow z axis @@ -184,7 +183,14 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa &[data.descriptor_sets[i]], &[]); - device.cmd_dispatch(*command_buffer, (data.compute_task_one_size as f64 / 16.0).ceil() as u32, 1, 1); + device.cmd_dispatch(*command_buffer, ((data.compute_task_one_size / 2) as f64 / 16.0).ceil() as u32, 1, 1); + + let buffer_memory_barrier_in = vk::BufferMemoryBarrier::builder() + .buffer(data.compute_out_storage_buffers_size_two[i]) + .src_access_mask(vk::AccessFlags::SHADER_READ) + .dst_access_mask(vk::AccessFlags::SHADER_WRITE) + .size(vk::WHOLE_SIZE as u64) + .build(); let buffer_memory_barrier_out = vk::BufferMemoryBarrier::builder() .buffer(data.compute_out_storage_buffers_size_three[i]) @@ -198,12 +204,12 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa vk::PipelineStageFlags::COMPUTE_SHADER, vk::DependencyFlags::DEVICE_GROUP, &[] as &[vk::MemoryBarrier], - &[buffer_memory_barrier_out], + &[buffer_memory_barrier_in, buffer_memory_barrier_out], &[] as &[vk::ImageMemoryBarrier]); - // combine element + // calculate mem size device.cmd_bind_pipeline( - *command_buffer, vk::PipelineBindPoint::COMPUTE, data.pipeline_compute_combine); + *command_buffer, vk::PipelineBindPoint::COMPUTE, data.pipeline_compute_mempos); device.cmd_bind_descriptor_sets( *command_buffer, @@ -213,10 +219,10 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa &[data.descriptor_sets[i]], &[]); - device.cmd_dispatch(*command_buffer, (data.compute_task_one_size as f64 / 16.0).ceil() as u32, 1, 1); + device.cmd_dispatch(*command_buffer, (data.compute_task_oct_tree_nodes as f64 / 16.0).ceil() as u32, 1, 1); let buffer_memory_barrier_out = vk::BufferMemoryBarrier::builder() - .buffer(data.render_storage_buffers[i]) + .buffer(data.compute_out_storage_buffers_oct_tree[i]) .src_access_mask(vk::AccessFlags::SHADER_WRITE) .dst_access_mask(vk::AccessFlags::SHADER_READ) .size(vk::WHOLE_SIZE as u64) @@ -229,6 +235,14 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa &[] as &[vk::MemoryBarrier], &[buffer_memory_barrier_out], &[] as &[vk::ImageMemoryBarrier]); + + device.cmd_pipeline_barrier(*command_buffer, + vk::PipelineStageFlags::COMPUTE_SHADER, + vk::PipelineStageFlags::VERTEX_INPUT, + vk::DependencyFlags::DEVICE_GROUP, + &[] as &[vk::MemoryBarrier], + &[buffer_memory_barrier_index, buffer_memory_barrier_vertex], + &[] as &[vk::ImageMemoryBarrier]); } // start render pass let clear_values = &[color_clear_value, depth_clear_value]; diff --git a/src/main.rs b/src/main.rs index 1b2714e..9c525e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -195,6 +195,7 @@ impl App { let mut data = app_data::AppData::default(); data.use_geometry_shader = false; data.num_lights_per_volume = 5; + data.num_compound_per_volume = 5; data.min_light_weight = 0.0001; data.max_iterations_per_light = 20; data.diffuse_raster_steps = 0; @@ -281,7 +282,7 @@ impl App { self.update_uniform_buffer(image_index)?; let time = self.appstart.elapsed().as_secs_f32() / 1.0; - self.scene_handler.point_lights[0].borrow_mut().set_pos(cgmath::vec3((10.0 + 64.0) as f32 + time.sin() * 2.0, (10.0 + 64.0) as f32 + time.cos() * 2.0, 11.0)); + //self.scene_handler.point_lights[0].borrow_mut().set_pos(cgmath::vec3((10.0 + 64.0) as f32 + time.sin() * 2.0, (10.0 + 64.0) as f32 + time.cos() * 2.0, 11.0)); self.synchronized = 0; if self.synchronized < MAX_FRAMES_IN_FLIGHT { @@ -441,13 +442,20 @@ impl App { .iter() .for_each(|m| self.device.free_memory(*m, None)); - self.data.compute_out_storage_buffers_size_three + self.data.compute_out_storage_buffers_size_three .iter() .for_each(|b| self.device.destroy_buffer(*b, None)); self.data.compute_out_storage_buffers_memory_size_three .iter() .for_each(|m| self.device.free_memory(*m, None)); + self.data.compute_out_storage_buffers_oct_tree + .iter() + .for_each(|b| self.device.destroy_buffer(*b, None)); + self.data.compute_out_storage_buffers_memory_oct_tree + .iter() + .for_each(|m| self.device.free_memory(*m, None)); + self.data.compute_out_cuboid_buffers .iter() .for_each(|b| self.device.destroy_buffer(*b, None)); @@ -474,7 +482,7 @@ impl App { self.device.destroy_pipeline(self.data.pipeline_compute_grow_one, None); self.device.destroy_pipeline(self.data.pipeline_compute_grow_two, None); self.device.destroy_pipeline(self.data.pipeline_compute_grow_three, None); - self.device.destroy_pipeline(self.data.pipeline_compute_combine, None); + self.device.destroy_pipeline(self.data.pipeline_compute_mempos, None); self.device.destroy_pipeline_layout(self.data.pipeline_layout, None); self.device.destroy_render_pass(self.data.render_pass, None); @@ -897,13 +905,13 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu .name(b"main\0"); // load the byte data - let compute_bytes = include_bytes!("../shaders/compiled/rt_compute_combine.spv"); + let compute_bytes = include_bytes!("../shaders/compiled/rt_compute_mempos.spv"); // create the shaders - let compute_shader_module_combine = create_shader_module(device, &compute_bytes[..])?; + let compute_shader_module_mempos = create_shader_module(device, &compute_bytes[..])?; //create the shader stage for the compute shader - let compute_stage_combine = vk::PipelineShaderStageCreateInfo::builder() + let compute_stage_mempos = vk::PipelineShaderStageCreateInfo::builder() .stage(vk::ShaderStageFlags::COMPUTE) - .module(compute_shader_module_combine) + .module(compute_shader_module_mempos) .name(b"main\0"); // define input assembly and object type. This is altered when using geometry shader @@ -1062,11 +1070,11 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu .stage(compute_stage_grow_three) .layout(data.pipeline_layout); - let info_compute_combine = vk::ComputePipelineCreateInfo::builder() - .stage(compute_stage_combine) + let info_compute_mempos = vk::ComputePipelineCreateInfo::builder() + .stage(compute_stage_mempos) .layout(data.pipeline_layout); - let compute_pipelines = device.create_compute_pipelines(vk::PipelineCache::null(), &[info_compute_rasterize, info_compute_grow_one, info_compute_grow_two, info_compute_grow_three, info_compute_combine], None)?.0; + let compute_pipelines = device.create_compute_pipelines(vk::PipelineCache::null(), &[info_compute_rasterize, info_compute_grow_one, info_compute_grow_two, info_compute_grow_three, info_compute_mempos], None)?.0; data.pipeline_cube = pipelines[0]; data.pipeline_cuboid = pipelines[1]; @@ -1076,7 +1084,7 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu data.pipeline_compute_grow_one = compute_pipelines[1]; data.pipeline_compute_grow_two = compute_pipelines[2]; data.pipeline_compute_grow_three = compute_pipelines[3]; - data.pipeline_compute_combine = compute_pipelines[4]; + data.pipeline_compute_mempos = compute_pipelines[4]; device.destroy_shader_module(vert_shader_module_cube, None); device.destroy_shader_module(geo_shader_module_cube, None); @@ -1093,7 +1101,7 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu device.destroy_shader_module(compute_shader_module_grow_one, None); device.destroy_shader_module(compute_shader_module_grow_two, None); device.destroy_shader_module(compute_shader_module_grow_three, None); - device.destroy_shader_module(compute_shader_module_combine, None); + device.destroy_shader_module(compute_shader_module_mempos, None); Ok(()) } diff --git a/src/scene/empty_volume.rs b/src/scene/empty_volume.rs index 36e3a1f..00ee843 100644 --- a/src/scene/empty_volume.rs +++ b/src/scene/empty_volume.rs @@ -13,6 +13,7 @@ use crate::scene::oct_tree::OctTree; use super::memorizable::Memorizable; use super::light::LightSource; use super::light::PointLight; +use super::volumetrics::ShapeComposition; use super::AppData; use super::LightsIter; use super::Scene; @@ -1063,10 +1064,10 @@ impl EmptyVolume { 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; } + out_index.push(weighted_indices[weighted_indices.len() - (index + 1)].1 as u32); } while out_index.len() < light_number as usize { out_index.push(0); @@ -1074,6 +1075,31 @@ impl EmptyVolume { out_index } + pub fn select_compounds(&self, compounds: &Vec<Rc<RefCell<ShapeComposition>>>, compound_number: u32) -> Vec<u32> { + let mut weighted_indices = vec![]; + for compound in compounds { + let bbox_low = compound.borrow().bbox_low; + let bbox_high = compound.borrow().bbox_high; + let diag = bbox_high - bbox_low; + if (self.real_position.x < bbox_high.x || self.real_position.y < bbox_high.y || self.real_position.z < bbox_high.z) && (bbox_low.x < self.real_position.x + self.size_x as f32 || bbox_low.y < self.real_position.y + self.size_y as f32 || bbox_low.z < self.real_position.z + self.size_z as f32) { + let le = diag.dot(diag); + weighted_indices.push((le, compound.borrow().get_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() { + if out_index.len() == compound_number as usize { + break; + } + out_index.push(weighted_indices[weighted_indices.len() - (index + 1)].1 as u32); + } + while out_index.len() < compound_number as usize { + out_index.push(0); + } + out_index + } + pub fn combine_results(first: &Rc<RefCell<OctTree<Cube>>>,first_neighbors: &Rc<OctTree<Rc<RefCell<EmptyVolume>>>>, second: &Rc<RefCell<OctTree<Cube>>>, second_neighbors: &Rc<OctTree<Rc<RefCell<EmptyVolume>>>>, facing: vertex::Facing) { let mut first_start; let mut second_start; @@ -1261,6 +1287,7 @@ impl Memorizable for EmptyVolume { mem_size += 12; //color/roughness buffer sizes, 2 values each mem_size += 12; //neighbor buffer sizes, 2 values each mem_size += 1; //scale of the volume, 1 float + mem_size += data.num_compound_per_volume; // compound references // this covers full color and roughness mem_size += (self.color_top.len() as u32).max(1); @@ -1296,12 +1323,20 @@ impl Memorizable for EmptyVolume { mem_index += 1; v[mem_index] = self.size_z as u32; mem_index += 1; - //Todo: insert lights + //insert lights let selected_lights = self.select_lights(scene.get_light_iter(), data.num_lights_per_volume, data.min_light_weight); for light in selected_lights { v[mem_index] = light; mem_index += 1; } + + // compound references + let selected_compounds = self.select_compounds(&scene.volumetrics, data.num_compound_per_volume); + for compound in selected_compounds { + v[mem_index] = compound; + mem_index += 1; + } + //color/roughness buffer sizes, 2 values each if self.color_top.len() > 1 { v[mem_index] = self.size_x as u32; diff --git a/src/scene/generators.rs b/src/scene/generators.rs index 3b7f372..eca7594 100644 --- a/src/scene/generators.rs +++ b/src/scene/generators.rs @@ -57,7 +57,7 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi let shade = (rng.gen_range(0..25) as f32) / 100.0; let cube = Cube { pos: vec3(10.0, 10.0, 10.0), - color: vec3(0.0, 0.0, 0.9), + color: vec3(0.9, 0.9, 0.9), tex_coord: vec2(0.0, 0.0), transparent: true, roughness: 32, @@ -66,7 +66,7 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi let cube = Cube { pos: vec3(10.0, 10.0, 9.0), - color: vec3(0.0, 0.0, 0.9), + color: vec3(0.9, 0.9, 0.9), tex_coord: vec2(0.0, 0.0), transparent: true, roughness: 32, @@ -93,8 +93,8 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi oct_tree2.set_cube(cube.clone()); scene.point_lights.push(Rc::new(RefCell::new(PointLight::init(vec3(11.0 + grid_size as f32, 11.0 + grid_size as f32, 11.0) * scale, vec3(2.0, 2.0, 2.0))))); - scene.point_lights.push(Rc::new(RefCell::new(PointLight::init(vec3(9.0 + grid_size as f32, 9.0 + grid_size as f32, 11.0) * scale, vec3(0.5, 0.5, 0.5))))); - scene.directional_lights.push(Rc::new(RefCell::new(DirectionalLight::init(vec3(1.0, 1.0, -1.0), vec3(0.1, 0.1, 0.1))))); + //scene.point_lights.push(Rc::new(RefCell::new(PointLight::init(vec3(9.0 + grid_size as f32, 9.0 + grid_size as f32, 11.0) * scale, vec3(0.5, 0.5, 0.5))))); + //scene.directional_lights.push(Rc::new(RefCell::new(DirectionalLight::init(vec3(1.0, 1.0, -1.0), vec3(0.1, 0.1, 0.1))))); let cube = Cuboid { pos: vec3(11.0 + grid_size as f32, 11.0 + grid_size as f32, 11.0) * scale, @@ -112,13 +112,16 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi size: Vector3 {x: 0.5, y: 0.5, z: 0.5} * scale }; let index = scene.sized_vertices.len(); - cube.draw(&data.topology, index, scene); + //cube.draw(&data.topology, index, scene); let tree_ref_one = Rc::new(RefCell::new(oct_tree1.clone())); let tree_ref_two = Rc::new(RefCell::new(oct_tree2.clone())); - scene.oct_trees = vec![vec![vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_one.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()]], vec![vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_one.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()]]]; + //scene.oct_trees = vec![vec![vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_one.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()]], vec![vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_one.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()]]]; + scene.oct_trees = vec![vec![vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_one.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()]]]; let mut comp = ShapeComposition::new(64); + //comp.included_shapes.push(Rc::new(RefCell::new(Rect::new(Vector3 { x: 5.0 + grid_size as f32, y: 5.0 + grid_size as f32, z: 10.0 }, Vector3 { x: 0.0, y: 0.0, z: 0.0 }, Vector3 { x: 5.0, y: 5.0, z: 5.0 },Vector3 { x: 0, y: 0, z: 255 }, 64, false)))); + comp.included_shapes.push(Rc::new(RefCell::new(Sphere::new(Vector3 { x: 5.0 + grid_size as f32, y: 5.0 + grid_size as f32, z: 10.0 }, Vector3 { x: 0.0, y: 0.0, z: 0.0 }, 2.0, Vector3 { x: 0, y: 255, z: 0 }, 64, false)))); comp.included_shapes.push(Rc::new(RefCell::new(Sphere::new(Vector3 { x: 5.0 + grid_size as f32, y: 5.0 + grid_size as f32, z: 10.0 }, Vector3 { x: 0.0, y: 0.0, z: 0.0 }, 2.5, Vector3 { x: 255, y: 0, z: 0 }, 64, false)))); comp.excluded_shapes.push(Rc::new(RefCell::new(Sphere::new(Vector3 { x: 5.0 + grid_size as f32, y: 5.0 + grid_size as f32, z: 11.5 }, Vector3 { x: 0.0, y: 0.0, z: 0.0 }, 1.5, Vector3 { x: 0, y: 255, z: 0 }, 64, false)))); @@ -127,12 +130,12 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi let mut comp = ShapeComposition::new(64); comp.included_shapes.push(Rc::new(RefCell::new(Cone::new(Vector3 { x: 20.0 + grid_size as f32, y: 5.0 + grid_size as f32, z: 10.0 }, Vector3 { x: 0.0, y: 0.0, z: 0.0 }, 0.0, 2.5, Vector3 { x: 0.0, y: 10.0, z: 0.0 },Vector3 { x: 0, y: 255, z: 0 }, 64, false)))); comp.excluded_shapes.push(Rc::new(RefCell::new(Cone::new(Vector3 { x: 20.0 + grid_size as f32, y: 5.0 + grid_size as f32, z: 10.0 }, Vector3 { x: 0.0, y: 0.0, z: 0.0 }, 0.0, 1.5, Vector3 { x: 0.0, y: 10.0, z: 0.0 },Vector3 { x: 0, y: 255, z: 0 }, 64, false)))); - scene.volumetrics.push(Rc::new(RefCell::new(comp))); + //scene.volumetrics.push(Rc::new(RefCell::new(comp))); let mut comp = ShapeComposition::new(64); comp.included_shapes.push(Rc::new(RefCell::new(Rect::new(Vector3 { x: -5.0 + grid_size as f32, y: 5.0 + grid_size as f32, z: 10.0 }, Vector3 { x: 0.0, y: 0.0, z: 0.0 }, Vector3 { x: 5.0, y: 10.0, z: 2.0 },Vector3 { x: 0, y: 0, z: 255 }, 64, false)))); comp.excluded_shapes.push(Rc::new(RefCell::new(Rect::new(Vector3 { x: -5.0 + grid_size as f32, y: 5.0 + grid_size as f32, z: 10.0 }, Vector3 { x: 0.0, y: 0.0, z: 0.0 }, Vector3 { x: 3.0, y: 8.0, z: 2.0 },Vector3 { x: 0, y: 0, z: 255 }, 64, false)))); - scene.volumetrics.push(Rc::new(RefCell::new(comp))); + //scene.volumetrics.push(Rc::new(RefCell::new(comp))); Ok((cgmath::point3(5.0, 5.0, 10.0))) } diff --git a/src/scene/mod.rs b/src/scene/mod.rs index 6fc3b0c..395271d 100644 --- a/src/scene/mod.rs +++ b/src/scene/mod.rs @@ -181,18 +181,39 @@ impl Scene { pub fn update_memory(&mut self, data: &mut AppData, reuse_memory: bool) { // reuse_memory controls whether a fresh data vector is created or the existing one is used if it is the right size - let mut memory_index = 6; + let mut memory_index = 7; // 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 // 4 - max recursive rays // 5 - diffuse rays per hit + // 6 - maximum number of compounds per light for memorizable in &self.memorizables { memorizable.borrow_mut().set_memory_start(memory_index); memory_index += memorizable.borrow_mut().get_buffer_mem_size(data) as usize; } + let mut compound_data_len = 1; + for compound in &self.volumetrics { + compound.borrow_mut().set_memory_start(compound_data_len); + compound_data_len += compound.borrow().get_compound_buffer_mem_size(data) as usize; + } + let mut volumetrics_memory = vec![compound_data_len as u32; compound_data_len]; + + let mut compute_task_one_size = 0; + let mut compute_task_one_out_size = 0; + let mut target_index = 1; + let mut node_count = 0; + for compound in &self.volumetrics { + compound.borrow_mut().target_memory_start = target_index; + target_index += compound.borrow().get_target_buffer_mem_size(); + node_count += compound.borrow().get_num_nodes(); + volumetrics_memory = compound.borrow_mut().insert_into_memory(volumetrics_memory, data, &self); + compute_task_one_size += compound.borrow().size.pow(2) as usize; + compute_task_one_out_size += compound.borrow().size.pow(3) as usize; + } + //println!("Memory size is {} kB, max indes is {}", memory_index * 32 / 8 /1024 + 1, memory_index); let mut volume_vec; let needs_overwrite; @@ -203,11 +224,13 @@ impl Scene { needs_overwrite = false; volume_vec = self.rt_memory.clone(); } + volume_vec[0] = data.num_lights_per_volume; volume_vec[1] = data.max_iterations_per_light; volume_vec[2] = data.diffuse_raster_steps; volume_vec[3] = u32::from_ne_bytes(data.diffuse_raster_size.to_ne_bytes()); volume_vec[4] = data.max_recursive_rays; volume_vec[5] = data.diffuse_rays_per_hit; + volume_vec[6] = data.num_compound_per_volume; for memorizable in &self.memorizables { if needs_overwrite || memorizable.borrow().is_dirty() { @@ -218,26 +241,13 @@ impl Scene { 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 - let mut data_len = 0; - for compound in &self.volumetrics { - compound.borrow_mut().set_memory_start(data_len); - data_len += compound.borrow().get_compound_buffer_mem_size(data) as usize; - } - let mut volumetrics_memory = vec![0; data_len]; - - let mut compute_task_one_size = 0; - let mut compute_task_one_out_size = 0; - for compound in &self.volumetrics { - volumetrics_memory = compound.borrow_mut().insert_into_memory(volumetrics_memory, data, &self); - compute_task_one_size += compound.borrow().size.pow(2) as usize; - compute_task_one_out_size += compound.borrow().size.pow(3) as usize; - } - self.volumetrics_memory = volumetrics_memory; data.scene_rt_volumetric_size = (self.volumetrics_memory.len() * 4) as u64; // size of the needed buffer size in bytes data.compute_task_one_size = compute_task_one_size; data.compute_task_one_out_buffer_size = (compute_task_one_out_size * 4) as u64; data.compute_task_one_out_size = compute_task_one_out_size as u64; + data.compute_task_oct_tree_size = target_index as u64; + data.compute_task_oct_tree_nodes = (node_count) as u64; } pub unsafe fn destroy(&mut self, device: &vulkanalia::Device) { diff --git a/src/scene/volumetrics/mod.rs b/src/scene/volumetrics/mod.rs index 8e7ade9..cf72f67 100644 --- a/src/scene/volumetrics/mod.rs +++ b/src/scene/volumetrics/mod.rs @@ -31,16 +31,19 @@ enum ShapeTypes { #[derive(Clone, Debug)] pub struct ShapeComposition { memory_start: usize, + pub target_memory_start: u32, prev_memory_size: u32, pub size: u32, pub included_shapes: Vec<Rc<RefCell<dyn Volumetrics>>>, pub excluded_shapes: Vec<Rc<RefCell<dyn Volumetrics>>>, dirty: bool, + pub bbox_low: Vector3<f32>, + pub bbox_high: Vector3<f32>, } impl ShapeComposition { pub fn new(size: u32) -> Self { - Self { memory_start: 0, prev_memory_size: 0, size: size, included_shapes: vec![], excluded_shapes: vec![], dirty: true } + Self { memory_start: 0, target_memory_start: 0, prev_memory_size: 0, size: size, included_shapes: vec![], excluded_shapes: vec![], dirty: true, bbox_low: Vector3 { x: 0.0, y: 0.0, z: 0.0 }, bbox_high: Vector3 { x: 0.0, y: 0.0, z: 0.0 } } } } @@ -60,7 +63,7 @@ impl CompoundMemorizable for ShapeComposition { impl Memorizable for ShapeComposition { fn get_buffer_mem_size(&self, data: &AppData) -> u32 { - //size, scale, memory_end, num_included, num_excluded, pos, wrapping address, included_address, excluded_address + //size, scale, memory_end, num_included, num_excluded, pos, target address, included_address, excluded_address 1 + 1 + 1 + 1 + 1 + 3 + 1 + self.included_shapes.len() as u32 + self.excluded_shapes.len() as u32 } @@ -117,6 +120,8 @@ impl Memorizable for ShapeComposition { } let bbox_high_pos_ind = bbox_high - bbox_low; + self.bbox_low = bbox_low; + self.bbox_high = bbox_high; let scale = bbox_high_pos_ind.x.max(bbox_high_pos_ind.y.max(bbox_high_pos_ind.z)) / (self.size as f32); v[self.memory_start + 1] = u32::from_ne_bytes(scale.to_ne_bytes()); @@ -126,7 +131,7 @@ impl Memorizable for ShapeComposition { v[self.memory_start + 5] = u32::from_ne_bytes(bbox_low.x.to_ne_bytes()); v[self.memory_start + 6] = u32::from_ne_bytes(bbox_low.y.to_ne_bytes()); v[self.memory_start + 7] = u32::from_ne_bytes(bbox_low.z.to_ne_bytes()); - v[self.memory_start + 8] = 0; //TODO add wrapping reference + v[self.memory_start + 8] = self.target_memory_start as u32; self.prev_memory_size = self.get_compound_buffer_mem_size(data); self.dirty = false; @@ -142,6 +147,24 @@ impl Memorizable for ShapeComposition { } } +impl ShapeComposition { + pub fn get_num_nodes(&self) -> u32 { + let mut nodes = 0; + let mut add_size = 1; + let mut size = self.size; + while size >= 2 { + nodes += add_size; + add_size *= 8; + size /= 2; + } + nodes + } + + pub fn get_target_buffer_mem_size(&self) -> u32 { + self.get_num_nodes() * 9 + } +} + #[derive(Clone, Debug, PartialEq)] pub struct Sphere { pos: Vector3<f32>,