diff --git a/build.rs b/build.rs index 3a366b4..67aaf3a 100644 --- a/build.rs +++ b/build.rs @@ -18,7 +18,7 @@ fn main() { 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"); std::fs::remove_file("shaders/compiled/geo_cube.spv").unwrap_or(()); std::fs::remove_file("shaders/compiled/frag_cube.spv").unwrap_or(()); @@ -32,7 +32,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/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..23863ab 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..625eb4b 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..5d56e66 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..891a76f 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..fc8e3b0 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..27e27eb 100644 --- a/shaders/rt_compute_grow_one.comp +++ b/shaders/rt_compute_grow_one.comp @@ -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..2127584 100644 --- a/shaders/rt_compute_grow_three.comp +++ b/shaders/rt_compute_grow_three.comp @@ -37,7 +37,7 @@ void main() { 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; + 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..0631b0c 100644 --- a/shaders/rt_compute_grow_two.comp +++ b/shaders/rt_compute_grow_two.comp @@ -37,7 +37,7 @@ void main() { 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; + 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..25c5a63 --- /dev/null +++ b/shaders/rt_compute_mempos.comp @@ -0,0 +1,349 @@ +#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; + +} + +void main() { + uint index = gl_GlobalInvocationID.x; + uint output_offset = 1; + uint input_offset = 0; + uint compound_start = 0; + + uint nodes = num_nodes(compounds[compound_start]); + // iterate over the compounds and find the work index inside of it + while (index > nodes) { + output_offset += nodes * 9; + 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]); + } + + 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 parent = parent_start + ((cohort_index - cohort_index % 8) / 8) * 9; + uint start = cohort_start + 9 * cohort_index; + 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 = (cohort_index % steps) * size; + uint y = (((cohort_index - (cohort_index % steps)) % (steps * steps)) / (steps)) * size; + uint z = (((cohort_index - (cohort_index % (steps * steps)))) / (steps * steps)) * size; + + // 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 + x = x + (size - 1); + y = y + (size - 1); + z = z + (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 * uint(size != 64); + + if (size > 2) { + // add child node reference + uint cohort_end = cohort_start + 9 * add_size; + out_memory[output_offset + cohort_start + cohort_index * 9 + 1] = cohort_end + cohort_index * 9 + 9 * 0; // xyz + out_memory[output_offset + cohort_start + cohort_index * 9 + 2] = cohort_end + cohort_index * 9 + 9 * 1; // Xyz + out_memory[output_offset + cohort_start + cohort_index * 9 + 3] = cohort_end + cohort_index * 9 + 9 * 2; // xYz + out_memory[output_offset + cohort_start + cohort_index * 9 + 4] = cohort_end + cohort_index * 9 + 9 * 3; // XYz + out_memory[output_offset + cohort_start + cohort_index * 9 + 5] = cohort_end + cohort_index * 9 + 9 * 4; // xyZ + out_memory[output_offset + cohort_start + cohort_index * 9 + 6] = cohort_end + cohort_index * 9 + 9 * 5; // XyZ + out_memory[output_offset + cohort_start + cohort_index * 9 + 7] = cohort_end + cohort_index * 9 + 9 * 6; // xYZ + out_memory[output_offset + cohort_start + cohort_index * 9 + 8] = cohort_end + cohort_index * 9 + 9 * 7; // 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..67912b2 100644 --- a/shaders/rt_compute_rasterize.comp +++ b/shaders/rt_compute_rasterize.comp @@ -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/src/app_data.rs b/src/app_data.rs index 0dafc31..bfacaf4 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,6 +95,8 @@ 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 min_light_weight: f32, diff --git a/src/buffer.rs b/src/buffer.rs index 8374162..c2b7572 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -206,7 +206,7 @@ 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) @@ -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..7b6289b 100644 --- a/src/command_buffer.rs +++ b/src/command_buffer.rs @@ -156,6 +156,13 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa &[]); device.cmd_dispatch(*command_buffer, (data.compute_task_one_size 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 +176,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 @@ -185,6 +192,13 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa &[]); device.cmd_dispatch(*command_buffer, (data.compute_task_one_size 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 +212,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 +227,17 @@ 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 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.render_storage_buffers[i]) + .buffer(data.compute_out_storage_buffers_size_two[i]) .src_access_mask(vk::AccessFlags::SHADER_WRITE) .dst_access_mask(vk::AccessFlags::SHADER_READ) .size(vk::WHOLE_SIZE as u64) @@ -224,10 +245,10 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa device.cmd_pipeline_barrier(*command_buffer, vk::PipelineStageFlags::COMPUTE_SHADER, - vk::PipelineStageFlags::FRAGMENT_SHADER, + 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]); } // start render pass diff --git a/src/main.rs b/src/main.rs index 1b2714e..d5905b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -441,13 +441,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 +481,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 +904,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 +1069,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 +1083,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 +1100,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/generators.rs b/src/scene/generators.rs index 3b7f372..c628790 100644 --- a/src/scene/generators.rs +++ b/src/scene/generators.rs @@ -127,12 +127,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..924bed0 100644 --- a/src/scene/mod.rs +++ b/src/scene/mod.rs @@ -227,7 +227,12 @@ impl Scene { 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; @@ -238,6 +243,8 @@ impl Scene { 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..8797cc3 100644 --- a/src/scene/volumetrics/mod.rs +++ b/src/scene/volumetrics/mod.rs @@ -31,6 +31,7 @@ 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>>>, @@ -40,7 +41,7 @@ pub struct ShapeComposition { 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 } } } @@ -142,6 +143,25 @@ 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>,