Compare commits

..

9 commits

Author SHA1 Message Date
zomseffen
17e21fe8e1 switched to sending rotation matrix for components 2025-08-04 10:41:07 +02:00
zomseffen
c02f07b684 start of hierarchy model, fixes volumetrics memory clean up when resending with different sizes 2025-08-01 13:16:48 +02:00
zomseffen
fbe684c1f3 some transparent stacking via opacity 2025-07-23 17:46:15 +02:00
zomseffen
0d1b94c551 reuse logic for full transparency check of volumes 2025-07-21 14:54:34 +02:00
zomseffen
76539a7721 transparency first check 2025-07-11 14:30:53 +02:00
zomseffen
0bb7659fdc fixes border transparent bug 2025-07-08 18:11:31 +02:00
zomseffen
9c0cdc8aa9 transparent volumetrics preparations 2025-07-07 15:49:41 +02:00
zomseffen
b18bde6f9f code clean up 2025-07-02 16:20:19 +02:00
zomseffen
31b61be788 Merge commit '81413a1f0b' 2025-07-02 16:12:47 +02:00
33 changed files with 1012 additions and 478 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
/target
src/test_oct_shader.rs
oct_tree_dump.json

28
Cargo.lock generated
View file

@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
version = 4
[[package]]
name = "ab_glyph"
@ -236,6 +236,7 @@ checksum = "1a98d30140e3296250832bbaaff83b27dcd6fa3cc70fb6f1f3e5c9c0023b5317"
dependencies = [
"approx",
"num-traits",
"serde",
]
[[package]]
@ -533,6 +534,12 @@ dependencies = [
"windows-sys 0.59.0",
]
[[package]]
name = "itoa"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]]
name = "jni"
version = "0.21.1"
@ -1009,6 +1016,12 @@ version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
[[package]]
name = "ryu"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
[[package]]
name = "same-file"
version = "1.0.6"
@ -1057,6 +1070,18 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.141"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30b9eff21ebe718216c6ec64e1d9ac57087aad11efc64e32002bce4a0d4c03d3"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]
[[package]]
name = "shlex"
version = "1.3.0"
@ -1268,6 +1293,7 @@ dependencies = [
"rand",
"rustc-hash",
"serde",
"serde_json",
"thiserror",
"tobj",
"vulkanalia",

View file

@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
anyhow = "1"
log = "0.4"
cgmath = "0.18"
cgmath = { version = "0.18", features = ["serde"] }
png = "0.17"
pretty_env_logger = "0.5"
thiserror = "1"
@ -16,5 +16,6 @@ tobj = { version = "3", features = ["log"] }
vulkanalia = { version = "=0.23.0", features = ["libloading", "provisional", "window"] }
winit = "0.29"
serde = { version = "1.0", features = ["derive"] }
serde_json = "*"
rand = "0.8"
rustc-hash = "*"

View file

@ -3,7 +3,7 @@ use std::io::{self, Write};
use std::path::Path;
use std::fs::File;
use std::io::{BufReader, BufRead, Error};
use std::io::{BufReader, BufRead};
fn insert_place_holders(path: &str) {
let input = File::open(path).unwrap();
@ -41,6 +41,7 @@ fn main() {
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_visible.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");
@ -61,6 +62,7 @@ fn main() {
std::fs::remove_file("shaders/compiled/vert_rt_quad.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/frag_rt_quad.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/rt_compute_rasterize.spv").unwrap_or(());
std::fs::remove_file("shaders/compiled/rt_compute_visible.spv").unwrap_or(());
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(());

View file

@ -13,6 +13,7 @@ C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_quad.vert -o shaders/compiled/ve
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_quad.frag -o shaders/compiled/frag_rt_quad.spv
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_rasterize.comp -o shaders/compiled/rt_compute_rasterize.spv
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/rt_compute_visible.comp -o shaders/compiled/rt_compute_visible.spv
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

View file

@ -14,6 +14,7 @@ glslc shaders/rt_quad.vert -o shaders/compiled/vert_rt_quad.spv
glslc shaders/rt_quad.frag -o shaders/compiled/frag_rt_quad.spv
glslc shaders/rt_compute_rasterize.comp -o shaders/compiled/rt_compute_rasterize.spv
glslc shaders/rt_compute_visible.comp -o shaders/compiled/rt_compute_visible.spv
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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -67,159 +67,260 @@ vec3 unpack_color(uint val) {
return vec3(val4 / 255.0, val3 / 255.0, val2 / 255.0);
}
void add_cube(uint cube_num, float scale, vec3 pos, uint color, uint start_volume) {
bool is_side_visible(uint x, uint y, uint z, uint compound_grid_size, uint input_offset, int dx, int dy, int dz) {
return (
(x == 0 && dx < 0) || (x == compound_grid_size - 1 && dx > 0) ||
(y == 0 && dy < 0) || (y == compound_grid_size - 1 && dy > 0) ||
(z == 0 && dz < 0) || (z == compound_grid_size - 1 && dz > 0)
) ||
(transparent_grid[input_offset + (x + dx) * compound_grid_size * compound_grid_size + (y + dy) * compound_grid_size + (z + dz)] &&
(!transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] ||
transparent_grid[input_offset + (x + dx) * compound_grid_size * compound_grid_size + (y + dy) * compound_grid_size + (z + dz)] && grid_in[input_offset + (x + dx) * compound_grid_size * compound_grid_size + (y + dy) * compound_grid_size + (z + dz)] != grid_in[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z])
);
}
uint vertex_size = 7;
void add_cube(uint cube_num, float scale, vec3 pos, uint color, uint start_volume, bool transparent, uvec3 grid_pos, uint compound_grid_size, uint input_offset) {
// add node info for the cube
//vertice 0
vertices[(cube_num * 8 + 0) * 6 + 0] = pos.x - 0.5 * scale;
vertices[(cube_num * 8 + 0) * 6 + 1] = pos.y + 0.5 * scale;
vertices[(cube_num * 8 + 0) * 6 + 2] = pos.z + 0.5 * scale;
vertices[(cube_num * 8 + 0) * vertex_size + 0] = pos.x - 0.5 * scale;
vertices[(cube_num * 8 + 0) * vertex_size + 1] = pos.y + 0.5 * scale;
vertices[(cube_num * 8 + 0) * vertex_size + 2] = pos.z + 0.5 * scale;
//volume
vertices[(cube_num * 8 + 0) * 6 + 3] = uintBitsToFloat(start_volume);
vertices[(cube_num * 8 + 0) * vertex_size + 3] = uintBitsToFloat(start_volume);
//color
vertices[(cube_num * 8 + 0) * 6 + 4] = uintBitsToFloat(color);
vertices[(cube_num * 8 + 0) * vertex_size + 4] = uintBitsToFloat(color);
//facing
vertices[(cube_num * 8 + 0) * 6 + 5] = uintBitsToFloat(6);
vertices[(cube_num * 8 + 0) * vertex_size + 5] = uintBitsToFloat(6);
// flags, first bit is transparency
vertices[(cube_num * 8 + 0) * vertex_size + 6] = 1 * uint(transparent);
//vertice 1
vertices[(cube_num * 8 + 1) * 6 + 0] = pos.x + 0.5 * scale;
vertices[(cube_num * 8 + 1) * 6 + 1] = pos.y + 0.5 * scale;
vertices[(cube_num * 8 + 1) * 6 + 2] = pos.z + 0.5 * scale;
vertices[(cube_num * 8 + 1) * vertex_size + 0] = pos.x + 0.5 * scale;
vertices[(cube_num * 8 + 1) * vertex_size + 1] = pos.y + 0.5 * scale;
vertices[(cube_num * 8 + 1) * vertex_size + 2] = pos.z + 0.5 * scale;
//volume
vertices[(cube_num * 8 + 1) * 6 + 3] = uintBitsToFloat(start_volume);
vertices[(cube_num * 8 + 1) * vertex_size + 3] = uintBitsToFloat(start_volume);
//color
vertices[(cube_num * 8 + 1) * 6 + 4] = uintBitsToFloat(color);
vertices[(cube_num * 8 + 1) * vertex_size + 4] = uintBitsToFloat(color);
//facing
vertices[(cube_num * 8 + 1) * 6 + 5] = uintBitsToFloat(4);
vertices[(cube_num * 8 + 1) * vertex_size + 5] = uintBitsToFloat(4);
// flags, first bit is transparency
vertices[(cube_num * 8 + 1) * vertex_size + 6] = 1 * uint(transparent);
//vertice 2
vertices[(cube_num * 8 + 2) * 6 + 0] = pos.x - 0.5 * scale;
vertices[(cube_num * 8 + 2) * 6 + 1] = pos.y - 0.5 * scale;
vertices[(cube_num * 8 + 2) * 6 + 2] = pos.z + 0.5 * scale;
vertices[(cube_num * 8 + 2) * vertex_size + 0] = pos.x - 0.5 * scale;
vertices[(cube_num * 8 + 2) * vertex_size + 1] = pos.y - 0.5 * scale;
vertices[(cube_num * 8 + 2) * vertex_size + 2] = pos.z + 0.5 * scale;
//volume
vertices[(cube_num * 8 + 2) * 6 + 3] = uintBitsToFloat(start_volume);
vertices[(cube_num * 8 + 2) * vertex_size + 3] = uintBitsToFloat(start_volume);
//color
vertices[(cube_num * 8 + 2) * 6 + 4] = uintBitsToFloat(color);
vertices[(cube_num * 8 + 2) * vertex_size + 4] = uintBitsToFloat(color);
//facing
vertices[(cube_num * 8 + 2) * 6 + 5] = uintBitsToFloat(3);
vertices[(cube_num * 8 + 2) * vertex_size + 5] = uintBitsToFloat(3);
// flags, first bit is transparency
vertices[(cube_num * 8 + 2) * vertex_size + 6] = 1 * uint(transparent);
//vertice 3
vertices[(cube_num * 8 + 3) * 6 + 0] = pos.x + 0.5 * scale;
vertices[(cube_num * 8 + 3) * 6 + 1] = pos.y - 0.5 * scale;
vertices[(cube_num * 8 + 3) * 6 + 2] = pos.z + 0.5 * scale;
vertices[(cube_num * 8 + 3) * vertex_size + 0] = pos.x + 0.5 * scale;
vertices[(cube_num * 8 + 3) * vertex_size + 1] = pos.y - 0.5 * scale;
vertices[(cube_num * 8 + 3) * vertex_size + 2] = pos.z + 0.5 * scale;
//volume
vertices[(cube_num * 8 + 3) * 6 + 3] = uintBitsToFloat(start_volume);
vertices[(cube_num * 8 + 3) * vertex_size + 3] = uintBitsToFloat(start_volume);
//color
vertices[(cube_num * 8 + 3) * 6 + 4] = uintBitsToFloat(color);
vertices[(cube_num * 8 + 3) * vertex_size + 4] = uintBitsToFloat(color);
//facing
vertices[(cube_num * 8 + 3) * 6 + 5] = uintBitsToFloat(1);
vertices[(cube_num * 8 + 3) * vertex_size + 5] = uintBitsToFloat(1);
// flags, first bit is transparency
vertices[(cube_num * 8 + 3) * vertex_size + 6] = 1 * uint(transparent);
//vertice 4
vertices[(cube_num * 8 + 4) * 6 + 0] = pos.x - 0.5 * scale;
vertices[(cube_num * 8 + 4) * 6 + 1] = pos.y + 0.5 * scale;
vertices[(cube_num * 8 + 4) * 6 + 2] = pos.z - 0.5 * scale;
vertices[(cube_num * 8 + 4) * vertex_size + 0] = pos.x - 0.5 * scale;
vertices[(cube_num * 8 + 4) * vertex_size + 1] = pos.y + 0.5 * scale;
vertices[(cube_num * 8 + 4) * vertex_size + 2] = pos.z - 0.5 * scale;
//volume
vertices[(cube_num * 8 + 4) * 6 + 3] = uintBitsToFloat(start_volume);
vertices[(cube_num * 8 + 4) * vertex_size + 3] = uintBitsToFloat(start_volume);
//color
vertices[(cube_num * 8 + 4) * 6 + 4] = uintBitsToFloat(color);
vertices[(cube_num * 8 + 4) * vertex_size + 4] = uintBitsToFloat(color);
//facing
vertices[(cube_num * 8 + 4) * 6 + 5] = uintBitsToFloat(0);
vertices[(cube_num * 8 + 4) * vertex_size + 5] = uintBitsToFloat(0);
// flags, first bit is transparency
vertices[(cube_num * 8 + 4) * vertex_size + 6] = 1 * uint(transparent);
//vertice 5
vertices[(cube_num * 8 + 5) * 6 + 0] = pos.x + 0.5 * scale;
vertices[(cube_num * 8 + 5) * 6 + 1] = pos.y + 0.5 * scale;
vertices[(cube_num * 8 + 5) * 6 + 2] = pos.z - 0.5 * scale;
vertices[(cube_num * 8 + 5) * vertex_size + 0] = pos.x + 0.5 * scale;
vertices[(cube_num * 8 + 5) * vertex_size + 1] = pos.y + 0.5 * scale;
vertices[(cube_num * 8 + 5) * vertex_size + 2] = pos.z - 0.5 * scale;
//volume
vertices[(cube_num * 8 + 5) * 6 + 3] = uintBitsToFloat(start_volume);
vertices[(cube_num * 8 + 5) * vertex_size + 3] = uintBitsToFloat(start_volume);
//color
vertices[(cube_num * 8 + 5) * 6 + 4] = uintBitsToFloat(color);
vertices[(cube_num * 8 + 5) * vertex_size + 4] = uintBitsToFloat(color);
//facing
vertices[(cube_num * 8 + 5) * 6 + 5] = uintBitsToFloat(2);
vertices[(cube_num * 8 + 5) * vertex_size + 5] = uintBitsToFloat(2);
// flags, first bit is transparency
vertices[(cube_num * 8 + 5) * vertex_size + 6] = 1 * uint(transparent);
//vertice 6
vertices[(cube_num * 8 + 6) * 6 + 0] = pos.x - 0.5 * scale;
vertices[(cube_num * 8 + 6) * 6 + 1] = pos.y - 0.5 * scale;
vertices[(cube_num * 8 + 6) * 6 + 2] = pos.z - 0.5 * scale;
vertices[(cube_num * 8 + 6) * vertex_size + 0] = pos.x - 0.5 * scale;
vertices[(cube_num * 8 + 6) * vertex_size + 1] = pos.y - 0.5 * scale;
vertices[(cube_num * 8 + 6) * vertex_size + 2] = pos.z - 0.5 * scale;
//volume
vertices[(cube_num * 8 + 6) * 6 + 3] = uintBitsToFloat(start_volume);
vertices[(cube_num * 8 + 6) * vertex_size + 3] = uintBitsToFloat(start_volume);
//color
vertices[(cube_num * 8 + 6) * 6 + 4] = uintBitsToFloat(color);
vertices[(cube_num * 8 + 6) * vertex_size + 4] = uintBitsToFloat(color);
//facing
vertices[(cube_num * 8 + 6) * 6 + 5] = uintBitsToFloat(5);
vertices[(cube_num * 8 + 6) * vertex_size + 5] = uintBitsToFloat(5);
// flags, first bit is transparency
vertices[(cube_num * 8 + 6) * vertex_size + 6] = 1 * uint(transparent);
//vertice 7
vertices[(cube_num * 8 + 7) * 6 + 0] = pos.x + 0.5 * scale;
vertices[(cube_num * 8 + 7) * 6 + 1] = pos.y - 0.5 * scale;
vertices[(cube_num * 8 + 7) * 6 + 2] = pos.z - 0.5 * scale;
vertices[(cube_num * 8 + 7) * vertex_size + 0] = pos.x + 0.5 * scale;
vertices[(cube_num * 8 + 7) * vertex_size + 1] = pos.y - 0.5 * scale;
vertices[(cube_num * 8 + 7) * vertex_size + 2] = pos.z - 0.5 * scale;
//volume
vertices[(cube_num * 8 + 7) * 6 + 3] = uintBitsToFloat(start_volume);
vertices[(cube_num * 8 + 7) * vertex_size + 3] = uintBitsToFloat(start_volume);
//color
vertices[(cube_num * 8 + 7) * 6 + 4] = uintBitsToFloat(color);
vertices[(cube_num * 8 + 7) * vertex_size + 4] = uintBitsToFloat(color);
//facing
vertices[(cube_num * 8 + 7) * 6 + 5] = uintBitsToFloat(6);
vertices[(cube_num * 8 + 7) * vertex_size + 5] = uintBitsToFloat(6);
// flags, first bit is transparency
vertices[(cube_num * 8 + 7) * vertex_size + 6] = 1 * uint(transparent);
//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;
if (is_side_visible(grid_pos.x, grid_pos.y, grid_pos.z, compound_grid_size, input_offset, 0, 0, 1)) {
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;
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;
} else {
indices[cube_num * 36 + 0] = 0;
indices[cube_num * 36 + 1] = 0;
indices[cube_num * 36 + 2] = 0;
indices[cube_num * 36 + 3] = 0;
indices[cube_num * 36 + 4] = 0;
indices[cube_num * 36 + 5] = 0;
}
//bottom
indices[cube_num * 36 + 6] = cube_num * 8 + 4;
indices[cube_num * 36 + 7] = cube_num * 8 + 7;
indices[cube_num * 36 + 8] = cube_num * 8 + 6;
if (is_side_visible(grid_pos.x, grid_pos.y, grid_pos.z, compound_grid_size, input_offset, 0, 0, -1)) {
indices[cube_num * 36 + 6] = cube_num * 8 + 4;
indices[cube_num * 36 + 7] = cube_num * 8 + 7;
indices[cube_num * 36 + 8] = cube_num * 8 + 6;
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;
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;
} else {
indices[cube_num * 36 + 6] = 0;
indices[cube_num * 36 + 7] = 0;
indices[cube_num * 36 + 8] = 0;
indices[cube_num * 36 + 9] = 0;
indices[cube_num * 36 + 10] = 0;
indices[cube_num * 36 + 11] = 0;
}
//left
indices[cube_num * 36 + 12] = cube_num * 8 + 2;
indices[cube_num * 36 + 13] = cube_num * 8 + 0;
indices[cube_num * 36 + 14] = cube_num * 8 + 4;
if (is_side_visible(grid_pos.x, grid_pos.y, grid_pos.z, compound_grid_size, input_offset, -1, 0, 0)) {
indices[cube_num * 36 + 12] = cube_num * 8 + 2;
indices[cube_num * 36 + 13] = cube_num * 8 + 0;
indices[cube_num * 36 + 14] = cube_num * 8 + 4;
indices[cube_num * 36 + 15] = cube_num * 8 + 2;
indices[cube_num * 36 + 16] = cube_num * 8 + 4;
indices[cube_num * 36 + 17] = cube_num * 8 + 6;
indices[cube_num * 36 + 15] = cube_num * 8 + 2;
indices[cube_num * 36 + 16] = cube_num * 8 + 4;
indices[cube_num * 36 + 17] = cube_num * 8 + 6;
} else {
indices[cube_num * 36 + 12] = 0;
indices[cube_num * 36 + 13] = 0;
indices[cube_num * 36 + 14] = 0;
indices[cube_num * 36 + 15] = 0;
indices[cube_num * 36 + 16] = 0;
indices[cube_num * 36 + 17] = 0;
}
//right
indices[cube_num * 36 + 18] = cube_num * 8 + 5;
indices[cube_num * 36 + 19] = cube_num * 8 + 1;
indices[cube_num * 36 + 20] = cube_num * 8 + 3;
if (is_side_visible(grid_pos.x, grid_pos.y, grid_pos.z, compound_grid_size, input_offset, 1, 0, 0)) {
indices[cube_num * 36 + 18] = cube_num * 8 + 5;
indices[cube_num * 36 + 19] = cube_num * 8 + 1;
indices[cube_num * 36 + 20] = cube_num * 8 + 3;
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;
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;
} else {
indices[cube_num * 36 + 18] = 0;
indices[cube_num * 36 + 19] = 0;
indices[cube_num * 36 + 20] = 0;
indices[cube_num * 36 + 21] = 0;
indices[cube_num * 36 + 22] = 0;
indices[cube_num * 36 + 23] = 0;
}
//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;
if (is_side_visible(grid_pos.x, grid_pos.y, grid_pos.z, compound_grid_size, input_offset, 0, -1, 0)) {
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 + 6;
indices[cube_num * 36 + 28] = cube_num * 8 + 7;
indices[cube_num * 36 + 29] = cube_num * 8 + 3;
indices[cube_num * 36 + 27] = cube_num * 8 + 6;
indices[cube_num * 36 + 28] = cube_num * 8 + 7;
indices[cube_num * 36 + 29] = cube_num * 8 + 3;
} else {
indices[cube_num * 36 + 24] = 0;
indices[cube_num * 36 + 25] = 0;
indices[cube_num * 36 + 26] = 0;
indices[cube_num * 36 + 27] = 0;
indices[cube_num * 36 + 28] = 0;
indices[cube_num * 36 + 29] = 0;
}
//far
indices[cube_num * 36 + 30] = cube_num * 8 + 1;
indices[cube_num * 36 + 31] = cube_num * 8 + 4;
indices[cube_num * 36 + 32] = cube_num * 8 + 0;
if (is_side_visible(grid_pos.x, grid_pos.y, grid_pos.z, compound_grid_size, input_offset, 0, 1, 0)) {
indices[cube_num * 36 + 30] = cube_num * 8 + 1;
indices[cube_num * 36 + 31] = cube_num * 8 + 4;
indices[cube_num * 36 + 32] = cube_num * 8 + 0;
indices[cube_num * 36 + 33] = cube_num * 8 + 1;
indices[cube_num * 36 + 34] = cube_num * 8 + 5;
indices[cube_num * 36 + 35] = cube_num * 8 + 4;
indices[cube_num * 36 + 33] = cube_num * 8 + 1;
indices[cube_num * 36 + 34] = cube_num * 8 + 5;
indices[cube_num * 36 + 35] = cube_num * 8 + 4;
} else {
indices[cube_num * 36 + 30] = 0;
indices[cube_num * 36 + 31] = 0;
indices[cube_num * 36 + 32] = 0;
indices[cube_num * 36 + 33] = 0;
indices[cube_num * 36 + 34] = 0;
indices[cube_num * 36 + 35] = 0;
}
}
void clear_cube(uint cube_num) {
for (int idx=0; idx < 36; idx++) {
indices[cube_num * 36 + idx] = 0;
}
for (int cube=0; cube<8; cube++) {
for (int attr=0; attr<vertex_size; attr++) {
vertices[(cube_num * 8 + cube) * vertex_size + attr] = 0;
}
}
}
uint cohort_index_from_pos(uint x, uint y, uint z, uint block_size, uint compound_size) {
@ -227,16 +328,6 @@ uint cohort_index_from_pos(uint x, uint y, uint z, uint block_size, uint compoun
return (z / block_size) * (steps*steps) + (y / block_size) * steps + (x / block_size);
}
bool is_visible(uint x, uint y, uint z, uint compound_grid_size, uint input_offset) {
return x == 0 || x == compound_grid_size - 1 || y == 0 || y == compound_grid_size -1 || z == 0 || z == compound_grid_size - 1 ||
transparent_grid[input_offset + (x - 1) * compound_grid_size * compound_grid_size + y * compound_grid_size + z] ||
transparent_grid[input_offset + (x + 1) * compound_grid_size * compound_grid_size + y * compound_grid_size + z] ||
transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + z] ||
transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + (y + 1) * compound_grid_size + z] ||
transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + (z - 1)] ||
transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + (z + 1)];
}
uint find_volume(uint overlapping_volume_num, uint volume_index_offset, vec3 volume_check_pos) {
uint volume_start = 0;
for(int i=0; i < overlapping_volume_num; i++) {
@ -376,53 +467,45 @@ void main() {
vec3 check_pos = compound_pos + vec3(float(x) * compound_scale, float(y) * compound_scale, float(z) * compound_scale) + mid_offset;
uint overlapping_volume_num = compounds[compound_start + 9];
uint volume_index_offset = compound_start + 10 + compounds[compound_start + 3] + compounds[compound_start + 4];
uint volume_index_offset = compound_start + 11 + compounds[compound_start + 3] + compounds[compound_start + 4];
//todo potentially it is needed to find volume start for each pos
uint volume_start = find_volume(overlapping_volume_num, volume_index_offset, check_pos - vec3(0.0, 0.0, 0.0) * compound_scale);
if (out_memory[output_offset + cohort_start + cohort_index * 9 + 1] != 0 && is_visible(x - 1, y - 1, z - 1, compound_grid_size, input_offset)) {
//uint volume_start = find_volume(overlapping_volume_num, volume_index_offset, check_pos - vec3(1.0, 1.0, 1.0) * compound_scale);
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, out_memory[output_offset + cohort_start + cohort_index * 9 + 1], volume_start);
}
if (out_memory[output_offset + cohort_start + cohort_index * 9 + 2] != 0 && is_visible(x - 0, y - 1, z - 1, compound_grid_size, input_offset)) {
//uint volume_start = find_volume(overlapping_volume_num, volume_index_offset, check_pos - vec3(0.0, 1.0, 1.0) * compound_scale);
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, out_memory[output_offset + cohort_start + cohort_index * 9 + 2], volume_start);
}
if (out_memory[output_offset + cohort_start + cohort_index * 9 + 3] != 0 && is_visible(x - 1, y - 0, z - 1, compound_grid_size, input_offset)) {
//uint volume_start = find_volume(overlapping_volume_num, volume_index_offset, check_pos - vec3(1.0, 0.0, 1.0) * compound_scale);
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, out_memory[output_offset + cohort_start + cohort_index * 9 + 3], volume_start);
}
if (out_memory[output_offset + cohort_start + cohort_index * 9 + 4] != 0 && is_visible(x - 0, y - 0, z - 1, compound_grid_size, input_offset)) {
//uint volume_start = find_volume(overlapping_volume_num, volume_index_offset, check_pos - vec3(0.0, 0.0, 1.0) * compound_scale);
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, out_memory[output_offset + cohort_start + cohort_index * 9 + 4], volume_start);
}
if (out_memory[output_offset + cohort_start + cohort_index * 9 + 5] != 0 && is_visible(x - 1, y - 1, z - 0, compound_grid_size, input_offset)) {
//uint volume_start = find_volume(overlapping_volume_num, volume_index_offset, check_pos - vec3(1.0, 1.0, 0.0) * compound_scale);
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, out_memory[output_offset + cohort_start + cohort_index * 9 + 5], volume_start);
}
if (out_memory[output_offset + cohort_start + cohort_index * 9 + 6] != 0 && is_visible(x - 0, y - 1, z - 0, compound_grid_size, input_offset)) {
//uint volume_start = find_volume(overlapping_volume_num, volume_index_offset, check_pos - vec3(0.0, 1.0, 0.0) * compound_scale);
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, out_memory[output_offset + cohort_start + cohort_index * 9 + 6], volume_start);
}
if (out_memory[output_offset + cohort_start + cohort_index * 9 + 7] != 0 && is_visible(x - 1, y - 0, z - 0, compound_grid_size, input_offset)) {
//uint volume_start = find_volume(overlapping_volume_num, volume_index_offset, check_pos - vec3(1.0, 0.0, 0.0) * compound_scale);
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, out_memory[output_offset + cohort_start + cohort_index * 9 + 7], volume_start);
}
if (out_memory[output_offset + cohort_start + cohort_index * 9 + 8] != 0 && is_visible(x - 0, y - 0, z - 0, compound_grid_size, input_offset)) {
//uint volume_start = find_volume(overlapping_volume_num, volume_index_offset, check_pos - vec3(0.0, 0.0, 0.0) * compound_scale);
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, out_memory[output_offset + cohort_start + cohort_index * 9 + 8], volume_start);
uint child_index = 1;
for (int dz=1; dz>=0; dz--) {
for (int dy=1; dy>=0; dy--) {
for (int dx=1; dx>=0; dx--) {
uint cube_num = input_offset + (z - dz) * compound_grid_size * compound_grid_size + (y - dy) * compound_grid_size + (x - dx);
if (out_memory[output_offset + cohort_start + cohort_index * 9 + child_index] != 0) {
add_cube(
cube_num,
compound_scale,
check_pos - vec3(dx, dy, dz) * compound_scale, out_memory[output_offset + cohort_start + cohort_index * 9 + child_index],
volume_start,
transparent_grid[input_offset + (x - dx) * compound_grid_size * compound_grid_size + (y - dy) * compound_grid_size + (z - dz)],
uvec3(x - dx, y - dy, z - dz), compound_grid_size, input_offset);
} else {
clear_cube(cube_num);
}
child_index++;
}
}
}
}
} 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;
for (int i=0; i < 9; i++) {
out_memory[output_offset + cohort_start + cohort_index * 9 + i] = 0;
}
if (size == 2) {
for (int dx=0; dx<=1; dx++) {
for (int dy=0; dy<=1; dy++) {
for (int dz=0; dz<=1; dz++) {
uint cube_num = input_offset + (z - dz) * compound_grid_size * compound_grid_size + (y - dy) * compound_grid_size + (x - dx);
clear_cube(cube_num);
}
}
}
}
}
}

View file

@ -13,7 +13,7 @@ layout(binding = 3) readonly buffer CompoundBuffer {
uint compounds[];
};
layout(binding = 4) buffer SceneInfoBuffer2 {
layout(binding = 8) buffer SceneInfoBuffer2 {
uint grid[];
};
@ -200,32 +200,23 @@ void main() {
bool transparent = false;
//handle included shapes
for (uint o=0; o < compounds[compound_start + 3]; o++) {
uint component_index = compounds[compound_start + 10 + o];
uint component_index = compounds[compound_start + 11 + o];
uint component_type = compounds[component_index];
vec3 component_pos = vec3(uintBitsToFloat(compounds[component_index + 1]), uintBitsToFloat(compounds[component_index + 2]), uintBitsToFloat(compounds[component_index + 3]));
vec3 component_rot = vec3(uintBitsToFloat(compounds[component_index + 4]), uintBitsToFloat(compounds[component_index + 5]), uintBitsToFloat(compounds[component_index + 6]));
mat3 component_rot_mat = mat3(
vec3(1.0, 0.0, 0.0),
vec3(0.0, cos(component_rot.x), sin(component_rot.x)),
vec3(0.0, -sin(component_rot.x), cos(component_rot.x))
) * mat3(
vec3(cos(component_rot.y), 0.0, sin(component_rot.y)),
vec3(0.0, 1.0, 0.0),
vec3(-sin(component_rot.y), 0.0, cos(component_rot.y))
) * mat3(
vec3(cos(component_rot.z), sin(component_rot.z), 0.0),
vec3(-sin(component_rot.z), cos(component_rot.y), 0.0),
vec3(0.0, 0.0, 1.0)
vec3(uintBitsToFloat(compounds[component_index + 4]), uintBitsToFloat(compounds[component_index + 5]), uintBitsToFloat(compounds[component_index + 6])),
vec3(uintBitsToFloat(compounds[component_index + 7]), uintBitsToFloat(compounds[component_index + 8]), uintBitsToFloat(compounds[component_index + 9])),
vec3(uintBitsToFloat(compounds[component_index + 10]), uintBitsToFloat(compounds[component_index + 11]), uintBitsToFloat(compounds[component_index + 12]))
);
color_int = compounds[component_index + 7];
color_int = compounds[component_index + 13];
uvec4 component_color = unpack_color(color_int);
transparent = compounds[component_index + 8] != 0;
transparent = compounds[component_index + 14] != 0;
if (component_type == 0) {
// handle sphere
float radius = uintBitsToFloat(compounds[component_index + 9]);
float radius = uintBitsToFloat(compounds[component_index + 15]);
render = length(component_pos - check_pos) <= radius;
if (render) {
@ -237,9 +228,9 @@ void main() {
if (component_type == 1) {
// handle cone
float radius1 = uintBitsToFloat(compounds[component_index + 9]);
float radius2 = uintBitsToFloat(compounds[component_index + 10]);
vec3 direction = component_rot_mat * vec3(uintBitsToFloat(compounds[component_index + 11]), uintBitsToFloat(compounds[component_index + 12]), uintBitsToFloat(compounds[component_index + 13]));
float radius1 = uintBitsToFloat(compounds[component_index + 15]);
float radius2 = uintBitsToFloat(compounds[component_index + 16]);
vec3 direction = component_rot_mat * vec3(uintBitsToFloat(compounds[component_index + 17]), uintBitsToFloat(compounds[component_index + 18]), uintBitsToFloat(compounds[component_index + 19]));
vec3 diff = check_pos - component_pos;
float factor = dot(direction, diff) / dot(direction, direction);
@ -257,7 +248,7 @@ void main() {
if (component_type == 2) {
// handle cone
vec3 size = vec3(uintBitsToFloat(compounds[component_index + 9]), uintBitsToFloat(compounds[component_index + 10]), uintBitsToFloat(compounds[component_index + 11]));
vec3 size = vec3(uintBitsToFloat(compounds[component_index + 15]), uintBitsToFloat(compounds[component_index + 16]), uintBitsToFloat(compounds[component_index + 17]));
vec3 direction1 = component_rot_mat * vec3(size.x, 0.0, 0.0) / 2.0;
vec3 direction2 = component_rot_mat * vec3(0.0, size.y, 0.0) / 2.0;
vec3 direction3 = component_rot_mat * vec3(0.0, 0.0, size.z) / 2.0;
@ -278,28 +269,19 @@ void main() {
}
//handle excluded shapes
for (uint o=0; o < compounds[compound_start + 4]; o++) {
uint component_index = compounds[compound_start + 10 + compounds[compound_start + 3] + o];
uint component_index = compounds[compound_start + 11 + compounds[compound_start + 3] + o];
uint component_type = compounds[component_index];
vec3 component_pos = vec3(uintBitsToFloat(compounds[component_index + 1]), uintBitsToFloat(compounds[component_index + 2]), uintBitsToFloat(compounds[component_index + 3]));
vec3 component_rot = vec3(uintBitsToFloat(compounds[component_index + 4]), uintBitsToFloat(compounds[component_index + 5]), uintBitsToFloat(compounds[component_index + 6]));
mat3 component_rot_mat = mat3(
vec3(1.0, 0.0, 0.0),
vec3(0.0, cos(component_rot.x), sin(component_rot.x)),
vec3(0.0, -sin(component_rot.x), cos(component_rot.x))
) * mat3(
vec3(cos(component_rot.y), 0.0, sin(component_rot.y)),
vec3(0.0, 1.0, 0.0),
vec3(-sin(component_rot.y), 0.0, cos(component_rot.y))
) * mat3(
vec3(cos(component_rot.z), sin(component_rot.z), 0.0),
vec3(-sin(component_rot.z), cos(component_rot.y), 0.0),
vec3(0.0, 0.0, 1.0)
vec3(uintBitsToFloat(compounds[component_index + 4]), uintBitsToFloat(compounds[component_index + 5]), uintBitsToFloat(compounds[component_index + 6])),
vec3(uintBitsToFloat(compounds[component_index + 7]), uintBitsToFloat(compounds[component_index + 8]), uintBitsToFloat(compounds[component_index + 9])),
vec3(uintBitsToFloat(compounds[component_index + 10]), uintBitsToFloat(compounds[component_index + 11]), uintBitsToFloat(compounds[component_index + 12]))
);
uvec4 color = unpack_color(compounds[component_index + 7]);
uvec4 color = unpack_color(compounds[component_index + 13]);
if (component_type == 0) {
// handle sphere
float radius = uintBitsToFloat(compounds[component_index + 9]);
float radius = uintBitsToFloat(compounds[component_index + 15]);
render = render && !(length(component_pos - check_pos) <= radius);
if (!render) {
@ -310,9 +292,9 @@ void main() {
if (component_type == 1) {
// handle cone
float radius1 = uintBitsToFloat(compounds[component_index + 9]);
float radius2 = uintBitsToFloat(compounds[component_index + 10]);
vec3 direction = component_rot_mat * vec3(uintBitsToFloat(compounds[component_index + 11]), uintBitsToFloat(compounds[component_index + 12]), uintBitsToFloat(compounds[component_index + 13]));
float radius1 = uintBitsToFloat(compounds[component_index + 15]);
float radius2 = uintBitsToFloat(compounds[component_index + 16]);
vec3 direction = component_rot_mat * vec3(uintBitsToFloat(compounds[component_index + 17]), uintBitsToFloat(compounds[component_index + 18]), uintBitsToFloat(compounds[component_index + 19]));
vec3 diff = check_pos - component_pos;
float factor = dot(direction, diff) / dot(direction, direction);
@ -329,7 +311,7 @@ void main() {
if (component_type == 2) {
// handle cone
vec3 size = vec3(uintBitsToFloat(compounds[component_index + 9]), uintBitsToFloat(compounds[component_index + 10]), uintBitsToFloat(compounds[component_index + 11]));
vec3 size = vec3(uintBitsToFloat(compounds[component_index + 15]), uintBitsToFloat(compounds[component_index + 16]), uintBitsToFloat(compounds[component_index + 17]));
vec3 direction1 = component_rot_mat * vec3(size.x, 0.0, 0.0) / 2.0;
vec3 direction2 = component_rot_mat * vec3(0.0, size.y, 0.0) / 2.0;
vec3 direction3 = component_rot_mat * vec3(0.0, 0.0, size.z) / 2.0;

View file

@ -0,0 +1,65 @@
#version 450
layout(binding = 3) readonly buffer CompoundBuffer {
uint compounds[];
};
layout(binding = 4) buffer SceneInfoBuffer2 {
uint grid_out[];
};
layout(binding = 8) readonly buffer ColorBuffer {
uint grid_in[];
};
layout(binding = 9) buffer transparencies {
bool transparent_grid[];
};
layout (local_size_x = 16, local_size_y = 1, local_size_z = 1) in;
bool is_visible(uint x, uint y, uint z, uint compound_grid_size, uint input_offset) {
return (x == 0 || x == compound_grid_size - 1 || y == 0 || y == compound_grid_size -1 || z == 0 || z == compound_grid_size - 1) ||
((transparent_grid[input_offset + (x - 1) * compound_grid_size * compound_grid_size + y * compound_grid_size + z] ||
transparent_grid[input_offset + (x + 1) * compound_grid_size * compound_grid_size + y * compound_grid_size + z] ||
transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + z] ||
transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + (y + 1) * compound_grid_size + z] ||
transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + (z - 1)] ||
transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + (z + 1)]) &&
(!transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] ||
(transparent_grid[input_offset + (x + 1) * compound_grid_size * compound_grid_size + y * compound_grid_size + z] && grid_in[input_offset + (x + 1) * compound_grid_size * compound_grid_size + y * compound_grid_size + z] != grid_in[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]) ||
(transparent_grid[input_offset + (x - 1) * compound_grid_size * compound_grid_size + y * compound_grid_size + z] && grid_in[input_offset + (x - 1) * compound_grid_size * compound_grid_size + y * compound_grid_size + z] != grid_in[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]) ||
(transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + (y + 1) * compound_grid_size + z] && grid_in[input_offset + x * compound_grid_size * compound_grid_size + (y + 1) * compound_grid_size + z] != grid_in[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]) ||
(transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + z] && grid_in[input_offset + x * compound_grid_size * compound_grid_size + (y - 1) * compound_grid_size + z] != grid_in[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]) ||
(transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + (z + 1)] && grid_in[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + (z + 1)] != grid_in[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z]) ||
(transparent_grid[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + (z - 1)] && grid_in[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + (z - 1)] != grid_in[input_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z])
));
}
void main() {
uint index = gl_GlobalInvocationID.x;
uint output_offset = 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];
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 y = index % compound_grid_size;
uint z = (index - y) / compound_grid_size;
// iterate upwards along the x axis
uint sum = 0;
for (uint x=0; x < compound_grid_size; x++) {
if (is_visible(x, y, z, compound_grid_size, output_offset)) {
grid_out[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = grid_in[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z];
} else {
grid_out[output_offset + x * compound_grid_size * compound_grid_size + y * compound_grid_size + z] = 0;
}
}
}

View file

@ -22,6 +22,10 @@ layout(binding = 3) readonly buffer CompoundBuffer {
uint compounds[];
};
layout(binding = 9) buffer transparencies {
bool transparent_grid[];
};
layout(binding = 10) readonly buffer OctTreeMemory {
uint oct_tree_mem[];
};
@ -40,11 +44,10 @@ 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;
uint val1 = bitfieldExtract(val, 24, 8);
uint val2 = bitfieldExtract(val, 16, 8);
uint val3 = bitfieldExtract(val, 8, 8);
uint val4 = bitfieldExtract(val, 0, 8);
return uvec4(val4, val3, val2, val1);
}
@ -465,6 +468,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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;
uint transparency_offset = compounds[compound_start + 10];
// invalidate the min found
hit_factors[min_index] = max_factor;
@ -474,18 +478,16 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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 = 70;
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 ++;
uvec4 last_transp_col = uvec4(0, 0, 0, 0);
for (uint check_it=0; check_it < 80; check_it++) {
// failsafe to get out in case of an accuracy issue
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));
@ -516,17 +518,40 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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;
uvec4 unpacked_color = unpack_color(oct_tree_mem[oct_tree_address + child_index]);
uvec3 check_grid_pos = grid_pos + parent_child_vec(1, child_index);
bool is_transparent = transparent_grid[transparency_offset + check_grid_pos.x * compound_grid_size * compound_grid_size + check_grid_pos.y * compound_grid_size + check_grid_pos.z];
//is_transparent = false;
if (!is_transparent) {
result.has_hit = true;
result.end_color = unpacked_color;
break;
} else {
if (last_transp_col != unpacked_color) {
color_mul_transparent = result.color_mul;
float opacity = 1.0 - float(unpacked_color.w) / 255.0;
result.color_mul = result.color_mul * vec3(float(unpacked_color.x) / 255.0, float(unpacked_color.y) / 255.0, float(unpacked_color.z) / 255.0) * opacity;
end_color_transparent = unpacked_color;
end_pos_transparent = intersection_pos;
end_facing_transparent = uint(is_x_hit) * (2 + uint(x_pos)) + uint(is_y_hit) * (4 + uint(y_pos)) + uint(is_z_hit && !z_pos);
if (max(result.color_mul.x, max(result.color_mul.y, result.color_mul.z)) < 0.1) {
break;
}
}
// since we have hollow areas we do not want to double up on the transp color. Will not work as intended with multiple layers of transparent colors
last_transp_col = unpacked_color;
}
}
} 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) {
uint next_child = oct_tree_mem[oct_tree_address + child_index];
if (oct_tree_mem[next_child] != 0) {
// change base address and position to child
current_size /= 2;
oct_tree_address = x;
oct_tree_address = next_child;
grid_pos += parent_child_vec(current_size, child_index);
for (int i=0; i < 8; i++) {
children_open[i] = true;
@ -589,8 +614,6 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
// 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;
}
}
@ -680,7 +703,8 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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);
float opacity = 1.0 - float(color_sample.w) / 255.0;
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) * opacity;
result.has_transparent_hit = true;
result.end_volume = volume_index;
result.end_direction = direction;
@ -855,5 +879,40 @@ vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4
}
}
return color_sum;
}
vec3 start_tracking(bool is_transparent, uint fragVolumeStart, uvec4 color_roughness, vec3 start_pos, uint 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;
if (is_transparent) {
vec3 color_direct = diffuse_tracing(fragVolumeStart, color_roughness, start_pos, facing);
Tracing t = trace_ray(fragVolumeStart, ubo.camera_pos, start_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);
color_seen_through = add_reflection(t.end_direction, t.end_facing, t.end_volume, t.end_pos, t.end_color, color_seen_through);
color_seen_through = color_seen_through * t.color_mul;
}
else {
// Todo: hit sky box
color_seen_through = vec3(0.0, 0.0, 0.0);
}
color_direct = add_reflection(normalize(start_pos - ubo.camera_pos), facing, fragVolumeStart, start_pos, color_roughness, color_direct);
color_sum = opacity * color_direct + color_seen_through;
//color_sum = color_seen_through;
}
else {
color_sum = diffuse_tracing(fragVolumeStart, color_roughness, start_pos, facing);
//color_sum = add_reflection(normalize(start_pos - ubo.camera_pos), facing, fragVolumeStart, start_pos, color_roughness, color_sum);
}
return color_sum;
}

View file

@ -33,6 +33,10 @@ layout(binding = 3) readonly buffer CompoundBuffer {
uint compounds[];
};
layout(binding = 9) buffer transparencies {
bool transparent_grid[];
};
layout(binding = 10) readonly buffer OctTreeMemory {
uint oct_tree_mem[];
};
@ -51,11 +55,10 @@ 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;
uint val1 = bitfieldExtract(val, 24, 8);
uint val2 = bitfieldExtract(val, 16, 8);
uint val3 = bitfieldExtract(val, 8, 8);
uint val4 = bitfieldExtract(val, 0, 8);
return uvec4(val4, val3, val2, val1);
}
@ -476,6 +479,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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;
uint transparency_offset = compounds[compound_start + 10];
// invalidate the min found
hit_factors[min_index] = max_factor;
@ -484,22 +488,17 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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;
/*if (oct_tree_address == 337042) {
result.has_hit = true;
}*/
// iterate through the oct_tree
uint check_it = 0;
uint max_check_it = 80;
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 ++;
uvec4 last_transp_col = uvec4(0, 0, 0, 0);
for (uint check_it=0; check_it < 80; check_it++) {
// failsafe to get out in case of an accuracy issue
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));
@ -530,17 +529,40 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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;
uvec4 unpacked_color = unpack_color(oct_tree_mem[oct_tree_address + child_index]);
uvec3 check_grid_pos = grid_pos + parent_child_vec(1, child_index);
bool is_transparent = transparent_grid[transparency_offset + check_grid_pos.x * compound_grid_size * compound_grid_size + check_grid_pos.y * compound_grid_size + check_grid_pos.z];
//is_transparent = false;
if (!is_transparent) {
result.has_hit = true;
result.end_color = unpacked_color;
break;
} else {
if (last_transp_col != unpacked_color) {
color_mul_transparent = result.color_mul;
float opacity = 1.0 - float(unpacked_color.w) / 255.0;
result.color_mul = result.color_mul * vec3(float(unpacked_color.x) / 255.0, float(unpacked_color.y) / 255.0, float(unpacked_color.z) / 255.0) * opacity;
end_color_transparent = unpacked_color;
end_pos_transparent = intersection_pos;
end_facing_transparent = uint(is_x_hit) * (2 + uint(x_pos)) + uint(is_y_hit) * (4 + uint(y_pos)) + uint(is_z_hit && !z_pos);
if (max(result.color_mul.x, max(result.color_mul.y, result.color_mul.z)) < 0.1) {
break;
}
}
// since we have hollow areas we do not want to double up on the transp color. Will not work as intended with multiple layers of transparent colors
last_transp_col = unpacked_color;
}
}
} 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) {
uint next_child = oct_tree_mem[oct_tree_address + child_index];
if (oct_tree_mem[next_child] != 0) {
// change base address and position to child
current_size /= 2;
oct_tree_address = x;
oct_tree_address = next_child;
grid_pos += parent_child_vec(current_size, child_index);
for (int i=0; i < 8; i++) {
children_open[i] = true;
@ -603,8 +625,6 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
// 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;
}
}
@ -694,7 +714,8 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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);
float opacity = 1.0 - float(color_sample.w) / 255.0;
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) * opacity;
result.has_transparent_hit = true;
result.end_volume = volume_index;
result.end_direction = direction;
@ -872,41 +893,49 @@ vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4
return color_sum;
}
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 start_tracking(bool is_transparent, uint fragVolumeStart, uvec4 color_roughness, vec3 start_pos, uint 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;
if (is_transparent) {
vec3 color_direct = diffuse_tracing(fragVolumeStart, color_roughness, start_pos, facing);
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);
Tracing t = trace_ray(fragVolumeStart, ubo.camera_pos, start_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);
color_seen_through = diffuse_tracing(t.end_volume, t.end_color, t.end_pos, t.end_facing);
color_seen_through = add_reflection(t.end_direction, t.end_facing, t.end_volume, t.end_pos, t.end_color, color_seen_through);
color_seen_through = color_seen_through * t.color_mul;
}
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_direct = add_reflection(normalize(start_pos - ubo.camera_pos), facing, fragVolumeStart, start_pos, color_roughness, color_direct);
color_sum = opacity * color_direct + color_seen_through;
//color_sum = color_seen_through;
}
else {
color_sum = diffuse_tracing(fragVolumeStart, color_roughness, clamped_pos, facing);
color_sum = diffuse_tracing(fragVolumeStart, color_roughness, start_pos, facing);
//color_sum = add_reflection(normalize(clamped_pos - ubo.camera_pos), facing, fragVolumeStart, clamped_pos, color_roughness, color_sum);
//color_sum = add_reflection(normalize(start_pos - ubo.camera_pos), facing, fragVolumeStart, start_pos, color_roughness, color_sum);
}
return color_sum;
}
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);
uint orig_neighbor = sample_neighbor_from_scene_info(fragVolumeStart, clamped_raster_pos, facing);
vec3 color_sum = start_tracking(orig_neighbor != 0, fragVolumeStart, color_roughness, clamped_pos, facing);
outColor = vec4(color_sum, 1.0);
}

View file

@ -15,37 +15,10 @@ 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);
}
vec3 color_sum = start_tracking(orig_neighbor != 0, fragVolumeStart, color_roughness, clamped_pos, facing);
outColor = vec4(color_sum, 1.0);
}

View file

@ -4,6 +4,7 @@ layout(location = 0) flat in uint volume_start;
layout(location = 1) flat in uint color_roughness;
layout(location = 2) flat in uint facing;
layout(location = 3) in vec3 origPosition;
layout(location = 4) flat in uint flags;
layout(location = 0) out vec4 outColor;
@ -31,6 +32,10 @@ layout(binding = 3) readonly buffer CompoundBuffer {
uint compounds[];
};
layout(binding = 9) buffer transparencies {
bool transparent_grid[];
};
layout(binding = 10) readonly buffer OctTreeMemory {
uint oct_tree_mem[];
};
@ -49,11 +54,10 @@ 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;
uint val1 = bitfieldExtract(val, 24, 8);
uint val2 = bitfieldExtract(val, 16, 8);
uint val3 = bitfieldExtract(val, 8, 8);
uint val4 = bitfieldExtract(val, 0, 8);
return uvec4(val4, val3, val2, val1);
}
@ -474,6 +478,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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;
uint transparency_offset = compounds[compound_start + 10];
// invalidate the min found
hit_factors[min_index] = max_factor;
@ -482,22 +487,17 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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;
/*if (oct_tree_address == 337042) {
result.has_hit = true;
}*/
// iterate through the oct_tree
uint check_it = 0;
uint max_check_it = 80;
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 ++;
uvec4 last_transp_col = uvec4(0, 0, 0, 0);
for (uint check_it=0; check_it < 80; check_it++) {
// failsafe to get out in case of an accuracy issue
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));
@ -528,17 +528,40 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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;
uvec4 unpacked_color = unpack_color(oct_tree_mem[oct_tree_address + child_index]);
uvec3 check_grid_pos = grid_pos + parent_child_vec(1, child_index);
bool is_transparent = transparent_grid[transparency_offset + check_grid_pos.x * compound_grid_size * compound_grid_size + check_grid_pos.y * compound_grid_size + check_grid_pos.z];
//is_transparent = false;
if (!is_transparent) {
result.has_hit = true;
result.end_color = unpacked_color;
break;
} else {
if (last_transp_col != unpacked_color) {
color_mul_transparent = result.color_mul;
float opacity = 1.0 - float(unpacked_color.w) / 255.0;
result.color_mul = result.color_mul * vec3(float(unpacked_color.x) / 255.0, float(unpacked_color.y) / 255.0, float(unpacked_color.z) / 255.0) * opacity;
end_color_transparent = unpacked_color;
end_pos_transparent = intersection_pos;
end_facing_transparent = uint(is_x_hit) * (2 + uint(x_pos)) + uint(is_y_hit) * (4 + uint(y_pos)) + uint(is_z_hit && !z_pos);
if (max(result.color_mul.x, max(result.color_mul.y, result.color_mul.z)) < 0.1) {
break;
}
}
// since we have hollow areas we do not want to double up on the transp color. Will not work as intended with multiple layers of transparent colors
last_transp_col = unpacked_color;
}
}
} 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) {
uint next_child = oct_tree_mem[oct_tree_address + child_index];
if (oct_tree_mem[next_child] != 0) {
// change base address and position to child
current_size /= 2;
oct_tree_address = x;
oct_tree_address = next_child;
grid_pos += parent_child_vec(current_size, child_index);
for (int i=0; i < 8; i++) {
children_open[i] = true;
@ -601,8 +624,6 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
// 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;
}
}
@ -692,7 +713,8 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
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);
float opacity = 1.0 - float(color_sample.w) / 255.0;
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) * opacity;
result.has_transparent_hit = true;
result.end_volume = volume_index;
result.end_direction = direction;
@ -870,30 +892,60 @@ vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4
return color_sum;
}
vec3 start_tracking(bool is_transparent, uint fragVolumeStart, uvec4 color_roughness, vec3 start_pos, uint 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;
if (is_transparent) {
vec3 color_direct = diffuse_tracing(fragVolumeStart, color_roughness, start_pos, facing);
Tracing t = trace_ray(fragVolumeStart, ubo.camera_pos, start_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);
color_seen_through = add_reflection(t.end_direction, t.end_facing, t.end_volume, t.end_pos, t.end_color, color_seen_through);
color_seen_through = color_seen_through * t.color_mul;
}
else {
// Todo: hit sky box
color_seen_through = vec3(0.0, 0.0, 0.0);
}
color_direct = add_reflection(normalize(start_pos - ubo.camera_pos), facing, fragVolumeStart, start_pos, color_roughness, color_direct);
color_sum = opacity * color_direct + color_seen_through;
//color_sum = color_seen_through;
}
else {
color_sum = diffuse_tracing(fragVolumeStart, color_roughness, start_pos, facing);
//color_sum = add_reflection(normalize(start_pos - ubo.camera_pos), facing, fragVolumeStart, start_pos, color_roughness, color_sum);
}
return color_sum;
}
struct Flags {
bool transparent;
};
Flags unpack_flags(uint packed_flags) {
// left most 8 bits first
Flags unpacked_flags;
unpacked_flags.transparent = bool(bitfieldExtract(packed_flags, 24, 1));
return unpacked_flags;
}
void main() {
uvec4 unpacked = unpack_color(color_roughness);
vec3 fragColor = vec3(float(unpacked.x) / 255.0, float(unpacked.y) / 255.0, float(unpacked.z) / 255.0);
uint test_facing = facing;
//365905
/*if (volume_start == 158451) {
outColor = vec4(1.0, 0.0, 0.0, 1);
Flags unpacked_flags = unpack_flags(flags);
if (volume_start != 0) {
vec3 color_sum = start_tracking(unpacked_flags.transparent, volume_start, unpacked, origPosition, facing);
outColor = vec4(color_sum, 1.0);
}
if (volume_start == 172317) {
outColor = vec4(0.0, 1.0, 0.0, 1);
}*/
vec3 color_sum = diffuse_tracing(volume_start, unpacked, origPosition, test_facing);
outColor = vec4(color_sum, 1.0);
if (facing == 6) {
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
if (volume_start == 0) {
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
//outColor = vec4(fragColor, 1.0);
}

View file

@ -14,11 +14,13 @@ layout(location = 0) in vec3 inPosition;
layout(location = 1) in uint volume_start;
layout(location = 2) in uint color_roughness;
layout(location = 3) in uint facing;
layout(location = 4) in uint flags;
layout(location = 0) flat out uint out_volume_start;
layout(location = 1) flat out uint out_color_roughness;
layout(location = 2) flat out uint out_facing;
layout(location = 3) out vec3 origPosition;
layout(location = 4) flat out uint out_flags;
void main() {
gl_Position = ubo.proj * ubo.view * ubo.geom_rot * ubo.model * vec4(inPosition, 1.0);
@ -27,4 +29,5 @@ void main() {
out_color_roughness = color_roughness;
out_facing = facing;
origPosition = inPosition;
out_flags = flags;
}

View file

@ -4,35 +4,31 @@ layout(location = 0) flat in uint volume_start;
layout(location = 1) flat in uint color_roughness;
layout(location = 2) flat in uint facing;
layout(location = 3) in vec3 origPosition;
layout(location = 4) flat in uint flags;
layout(location = 0) out vec4 outColor;
#include rt_lib.frag
struct Flags {
bool transparent;
};
Flags unpack_flags(uint packed_flags) {
// left most 8 bits first
Flags unpacked_flags;
unpacked_flags.transparent = bool(bitfieldExtract(packed_flags, 24, 1));
return unpacked_flags;
}
void main() {
uvec4 unpacked = unpack_color(color_roughness);
vec3 fragColor = vec3(float(unpacked.x) / 255.0, float(unpacked.y) / 255.0, float(unpacked.z) / 255.0);
uint test_facing = facing;
//365905
/*if (volume_start == 158451) {
outColor = vec4(1.0, 0.0, 0.0, 1);
Flags unpacked_flags = unpack_flags(flags);
if (volume_start != 0) {
vec3 color_sum = start_tracking(unpacked_flags.transparent, volume_start, unpacked, origPosition, facing);
outColor = vec4(color_sum, 1.0);
}
if (volume_start == 172317) {
outColor = vec4(0.0, 1.0, 0.0, 1);
}*/
vec3 color_sum = diffuse_tracing(volume_start, unpacked, origPosition, test_facing);
outColor = vec4(color_sum, 1.0);
if (facing == 6) {
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
if (volume_start == 0) {
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
//outColor = vec4(fragColor, 1.0);
}

View file

@ -29,6 +29,7 @@ pub struct AppData {
pub pipeline_compute_grow_two: vk::Pipeline,
pub pipeline_compute_grow_three: vk::Pipeline,
pub pipeline_compute_mempos: vk::Pipeline,
pub pipeline_compute_visible: vk::Pipeline,
pub framebuffers: Vec<vk::Framebuffer>,
pub command_pool: vk::CommandPool,

View file

@ -10,7 +10,6 @@ pub type Mat4 = cgmath::Matrix4<f32>;
use crate::app_data;
use crate::command_buffer;
use crate::primitives;
use crate::vertex;
use crate::vertex::VertexContainer;
use crate::scene;
@ -532,8 +531,9 @@ pub unsafe fn update_compute_storage_buffer(
Ok(())
}
/*
// used to look into the oct tree buffer. Needs to have it marked as transfer src to avoid validation error
use std::fs::OpenOptions;
pub unsafe fn show_oct_tree(
instance: &Instance,
device: &Device,
@ -568,7 +568,9 @@ pub unsafe fn show_oct_tree(
//337042
//674083 max pos for two volumetrics
//337042 for one
let mut line = 0;
let file_ref: std::fs::File = OpenOptions::new().create(true).write(true).truncate(true).open("oct_tree_dump.json")?;
serde_json::to_writer(&file_ref, &test_vec)?;
/*let mut line = 0;
for val in &test_vec {
/*if *val != 0 {
println!("{}: {}", line, val);
@ -583,7 +585,7 @@ pub unsafe fn show_oct_tree(
}
line += 1;
}
println!("max pos {}", data.compute_task_oct_tree_size);
println!("max pos {}", data.compute_task_oct_tree_size);*/
device.unmap_memory(staging_buffer_memory);
@ -591,7 +593,7 @@ pub unsafe fn show_oct_tree(
device.free_memory(staging_buffer_memory, None);
Ok(())
}*/
}/**/
pub unsafe fn create_descriptor_pool(device: &Device, data: &mut app_data::AppData) -> Result<()> {
let ubo_size = vk::DescriptorPoolSize::builder()

View file

@ -60,7 +60,7 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa
// define the compute load before going into the render pass
if scene_handler.volumetrics.len() != 0 {
device.cmd_bind_pipeline(
*command_buffer, vk::PipelineBindPoint::COMPUTE, data.pipeline_compute_rasterize); //todo build own pipeline
*command_buffer, vk::PipelineBindPoint::COMPUTE, data.pipeline_compute_rasterize);
device.cmd_bind_descriptor_sets(
*command_buffer,
@ -87,7 +87,7 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa
// compute storage barrier
let buffer_memory_barrier_color = vk::BufferMemoryBarrier::builder()
.buffer(data.compute_out_storage_buffers_color[i])
.buffer(data.compute_out_storage_buffers_size_three[i])
.src_access_mask(vk::AccessFlags::SHADER_WRITE)
.dst_access_mask(vk::AccessFlags::SHADER_READ)
.size(vk::WHOLE_SIZE as u64)
@ -107,6 +107,40 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa
&[] as &[vk::MemoryBarrier],
&[buffer_memory_barrier_color, buffer_memory_barrier_transparent],
&[] as &[vk::ImageMemoryBarrier]);
// prune for visibility
device.cmd_bind_pipeline(
*command_buffer, vk::PipelineBindPoint::COMPUTE, data.pipeline_compute_visible);
device.cmd_bind_descriptor_sets(
*command_buffer,
vk::PipelineBindPoint::COMPUTE,
data.pipeline_layout,
0,
&[data.descriptor_sets[i]],
&[]);
device.cmd_dispatch(*command_buffer, (data.compute_task_one_size as f64 / 16.0).ceil() as u32, 1, 1);
let buffer_memory_barrier_color = vk::BufferMemoryBarrier::builder()
.buffer(data.compute_out_storage_buffers_color[i])
.src_access_mask(vk::AccessFlags::SHADER_WRITE)
.dst_access_mask(vk::AccessFlags::SHADER_READ)
.size(vk::WHOLE_SIZE as u64)
.build();
let buffer_memory_barrier_threed = 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();
device.cmd_pipeline_barrier(*command_buffer,
vk::PipelineStageFlags::COMPUTE_SHADER,
vk::PipelineStageFlags::COMPUTE_SHADER,
vk::DependencyFlags::DEVICE_GROUP,
&[] as &[vk::MemoryBarrier],
&[buffer_memory_barrier_color, buffer_memory_barrier_threed],
&[] as &[vk::ImageMemoryBarrier]);
// grow x axis
device.cmd_bind_pipeline(
*command_buffer, vk::PipelineBindPoint::COMPUTE, data.pipeline_compute_grow_one);

View file

@ -20,9 +20,9 @@ use vulkanalia::prelude::v1_0::*;
use vulkanalia::Version;
use vulkanalia::bytecode::Bytecode;
use core::f32;
use std::collections::HashSet;
use std::ffi::CStr;
use std::time::{Duration, SystemTime};
use std::os::raw::c_void;
// extension imports
@ -30,7 +30,7 @@ use vulkanalia::vk::ExtDebugUtilsExtension;
use vulkanalia::vk::KhrSurfaceExtension;
use vulkanalia::vk::KhrSwapchainExtension;
use cgmath::Matrix;
use cgmath::{Matrix, Vector3};
use std::mem::size_of;
use std::ptr::copy_nonoverlapping as memcpy;
@ -50,6 +50,8 @@ pub mod load_model;
pub mod scene;
pub mod primitives;
//mod test_oct_shader;
const PORTABILITY_MACOS_VERSION: Version = Version::new(1, 3, 216);
const VALIDATION_ENABLED: bool =
cfg!(debug_assertions);
@ -64,8 +66,10 @@ const DEVICE_EXTENSIONS: &[vk::ExtensionName] = &[
const MAX_FRAMES_IN_FLIGHT: usize = 30;
fn main() -> Result<()> {
//test_oct_shader::test()?;
//Ok(())
pretty_env_logger::init();
// Window
let event_loop = EventLoop::new()?;
@ -150,19 +154,24 @@ fn main() -> Result<()> {
app.synchronized = 0;
println!("Set diffuse tracing raster size to {}", app.scene_handler.rt_memory[2]);
}
if event.logical_key == "r" && event.state == ElementState::Pressed && event.repeat == false {
app.angle += (1.0 / 360.0) * f32::consts::PI;
}
if event.logical_key == NamedKey::Escape {
elwt.exit();
unsafe { app.device.device_wait_idle().unwrap(); }
unsafe { app.destroy(); }
}
/*if event.logical_key == "p" && event.state == ElementState::Pressed && event.repeat == false {
// print part of the oct tree buffer
if event.logical_key == "p" && event.state == ElementState::Pressed && event.repeat == false {
// print part of the oct tree buffer function need to be uncommented
println!("Angle is {}", app.angle);
unsafe {
buffer::show_oct_tree(&app.instance, &app.device, &app.data, 0, &app.scene_handler).expect("failed to show oct tree");
}
}*/
}
},
_ => {}
}
@ -193,6 +202,7 @@ struct App {
show_frame_rate: bool,
synchronized: usize,
appstart: Instant,
angle: f32,
}
impl App {
@ -258,9 +268,11 @@ impl App {
show_frame_rate: false,
synchronized: 0,
appstart: Instant::now(),
angle: 0.0,
})
}
/// MARK: Render
/// Renders a frame for our Vulkan app.
unsafe fn render(&mut self, window: &Window) -> Result<()> {
let start_time = Instant::now();
@ -268,6 +280,24 @@ impl App {
self.device.wait_for_fences(&[in_flight_fence], true, u64::MAX)?;
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));
if self.scene_handler.volumetrics[2].borrow().included_shapes[0].borrow().get_rot() != cgmath::Matrix3::from_angle_x(cgmath::Rad(self.angle)) {
self.scene_handler.volumetrics[2].borrow_mut().included_shapes[0].borrow_mut().set_rot(cgmath::Matrix3::from_angle_x(cgmath::Rad(self.angle)));
self.scene_handler.volumetrics[2].borrow_mut().excluded_shapes[0].borrow_mut().set_rot(cgmath::Matrix3::from_angle_x(cgmath::Rad(self.angle)));
}
if self.scene_handler.is_dirty() {
self.synchronized = 0;
let mem_resize = self.scene_handler.update_memory(&mut self.data, true);
if mem_resize {
// we need bigger buffers and therefore new descriptors
println!("We need to recreate the swap chain, because of buffer resizing");
return self.recreate_swapchain(window);
}
}
let result = self.device.acquire_next_image_khr(
self.data.swapchain,
u64::MAX,
@ -289,14 +319,8 @@ impl App {
self.data.images_in_flight[image_index] = in_flight_fence;
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.synchronized = 0;
if self.synchronized < MAX_FRAMES_IN_FLIGHT {
if self.scene_handler.is_dirty() {
self.scene_handler.update_memory(&mut self.data, true)
}
buffer::update_render_storage_buffer(&self.instance, &self.device, &self.data, image_index, &self.scene_handler)?;
buffer::update_compute_storage_buffer(&self.instance, &self.device, &self.data, image_index, &self.scene_handler)?;
self.synchronized += 1
@ -492,6 +516,7 @@ impl App {
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_mempos, None);
self.device.destroy_pipeline(self.data.pipeline_compute_visible, None);
self.device.destroy_pipeline_layout(self.data.pipeline_layout, None);
self.device.destroy_render_pass(self.data.render_pass, None);
@ -897,7 +922,7 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
.vertex_binding_descriptions(binding_descriptions_quad)
.vertex_attribute_descriptions(&attribute_descriptions_quad);
// set up compute shader
// set up compute shaders
// load the byte data
let compute_bytes = include_bytes!("../shaders/compiled/rt_compute_rasterize.spv");
// create the shaders
@ -908,6 +933,16 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
.module(compute_shader_module_rasterize)
.name(b"main\0");
// load the byte data
let compute_bytes = include_bytes!("../shaders/compiled/rt_compute_visible.spv");
// create the shaders
let compute_shader_module_visible = create_shader_module(device, &compute_bytes[..])?;
//create the shader stage for the compute shader
let compute_stage_visible = vk::PipelineShaderStageCreateInfo::builder()
.stage(vk::ShaderStageFlags::COMPUTE)
.module(compute_shader_module_visible)
.name(b"main\0");
// load the byte data
let compute_bytes = include_bytes!("../shaders/compiled/rt_compute_grow_one.spv");
// create the shaders
@ -1111,6 +1146,10 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
.stage(compute_stage_rasterize)
.layout(data.pipeline_layout);
let info_compute_visible = vk::ComputePipelineCreateInfo::builder()
.stage(compute_stage_visible)
.layout(data.pipeline_layout);
let info_compute_grow_one = vk::ComputePipelineCreateInfo::builder()
.stage(compute_stage_grow_one)
.layout(data.pipeline_layout);
@ -1127,7 +1166,7 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
.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_mempos], 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, info_compute_visible], None)?.0;
data.pipeline_cube = pipelines[0];
data.pipeline_cuboid = pipelines[1];
@ -1139,6 +1178,7 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
data.pipeline_compute_grow_two = compute_pipelines[2];
data.pipeline_compute_grow_three = compute_pipelines[3];
data.pipeline_compute_mempos = compute_pipelines[4];
data.pipeline_compute_visible = compute_pipelines[5];
device.destroy_shader_module(vert_shader_module_cube, None);
device.destroy_shader_module(geo_shader_module_cube, None);
@ -1159,6 +1199,7 @@ unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Resu
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_mempos, None);
device.destroy_shader_module(compute_shader_module_visible, None);
Ok(())
}

View file

@ -1,6 +1,6 @@
use cgmath::{ElementWise, InnerSpace, Vector3};
use std::cell::{RefCell, Ref};
use std::cell::RefCell;
use std::rc::Rc;
use std::time::Instant;
@ -11,8 +11,6 @@ use crate::primitives::quad::Quad;
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;
@ -995,24 +993,24 @@ impl EmptyVolume {
pub fn select_lights(&self, lights: LightsIter, light_number: u32, min_light_weight: f32) -> Vec<u32> {
let mut weighted_indices = vec![];
for light in lights {
let mut has_hitable_side = false;
//let mut has_hitable_side = false;
if self.color_bottom.len() > 0 {
let center = self.real_position + Vector3{x: self.size_x as f32 * 0.5, y: self.size_y as f32 * 0.5, z: self.size_z as f32 * 0.0 - 0.5} * self.scale;
let normal = Vector3 {x: 0.0, y: 0.0, z: 1.0};
let dir = light.borrow().get_direction(center);
if normal.dot(dir) < 0.0 {
/*if normal.dot(dir) < 0.0 {
has_hitable_side = true;
}
}*/
}
if self.color_top.len() > 0 {
let center = self.real_position + Vector3{x: self.size_x as f32 * 0.5, y: self.size_y as f32 * 0.5, z: self.size_z as f32 * 1.0 - 0.5} * self.scale;
let normal = Vector3 {x: 0.0, y: 0.0, z: -1.0};
let dir = light.borrow().get_direction(center);
if normal.dot(dir) < 0.0 {
/*if normal.dot(dir) < 0.0 {
has_hitable_side = true;
}
}*/
}
if self.color_front.len() > 0 {
@ -1020,9 +1018,9 @@ impl EmptyVolume {
let normal = Vector3 {x: 0.0, y: 1.0, z: 0.0};
let dir = light.borrow().get_direction(center);
if normal.dot(dir) < 0.0 {
/*if normal.dot(dir) < 0.0 {
has_hitable_side = true;
}
}*/
}
if self.color_back.len() > 0 {
@ -1030,9 +1028,9 @@ impl EmptyVolume {
let normal = Vector3 {x: 0.0, y: -1.0, z: 0.0};
let dir = light.borrow().get_direction(center);
if normal.dot(dir) < 0.0 {
/*if normal.dot(dir) < 0.0 {
has_hitable_side = true;
}
}*/
}
if self.color_left.len() > 0 {
@ -1040,9 +1038,9 @@ impl EmptyVolume {
let normal = Vector3 {x: 1.0, y: 0.0, z: 0.0};
let dir = light.borrow().get_direction(center);
if normal.dot(dir) < 0.0 {
/*if normal.dot(dir) < 0.0 {
has_hitable_side = true;
}
}*/
}
if self.color_right.len() > 0 {
@ -1050,9 +1048,9 @@ impl EmptyVolume {
let normal = Vector3 {x: -1.0, y: 0.0, z: 0.0};
let dir = light.borrow().get_direction(center);
if normal.dot(dir) < 0.0 {
/*if normal.dot(dir) < 0.0 {
has_hitable_side = true;
}
}*/
}
/*if !has_hitable_side {
@ -1106,8 +1104,8 @@ impl EmptyVolume {
}
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;
let first_start;
let second_start;
let step_one;
let step_two;
@ -1168,7 +1166,7 @@ impl EmptyVolume {
if !done_volumes.contains(&volume) {
let mask = Vector3 {x: 1, y: 1, z: 1} - (step_one + step_two);
let negated_mask = (step_one + step_two);
let negated_mask = step_one + step_two;
let volume_start_first = negated_mask.mul_element_wise(volume.borrow().grid_position) + first_pos.mul_element_wise(mask);
let volume_start_second = negated_mask.mul_element_wise(volume.borrow().grid_position) + second_pos.mul_element_wise(mask);
@ -1338,7 +1336,7 @@ impl Memorizable for EmptyVolume {
// compound references
let mut count = 0;
for compound in &self.compositions {
println!("adds compound reference: {}", compound.borrow().get_memory_start() as u32);
//println!("adds compound reference: {}", compound.borrow().get_memory_start() as u32);
v[mem_index] = compound.borrow().get_memory_start() as u32;
mem_index += 1;
count += 1;
@ -1643,7 +1641,7 @@ impl Memorizable for EmptyVolume {
}
else {
v[mem_index] = 0;
mem_index += 1;
//mem_index += 1;
}
self.dirty = false;

View file

@ -11,15 +11,15 @@ use rand::Rng;
use anyhow::{Ok, Result};
use cgmath::{vec2, vec3, Vector3, Point3};
use cgmath::{vec2, vec3, Matrix3, Point3, SquareMatrix, Vector3};
use std::cell::RefCell;
use std::rc::Rc;
use super::light::{DirectionalLight, PointLight};
use super::light::PointLight;
pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Point3<f32>)> {
pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<Point3<f32>> {
let mut rng = rand::thread_rng();
let grid_size = CHUNK_SIZE as i32;
@ -60,7 +60,7 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi
color: vec3(0.9, 0.9, 0.9),
tex_coord: vec2(0.0, 0.0),
transparent: true,
roughness: 32,
roughness: 0,
};
oct_tree1.set_cube(cube.clone());
@ -69,7 +69,7 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi
color: vec3(0.9, 0.9, 0.9),
tex_coord: vec2(0.0, 0.0),
transparent: true,
roughness: 32,
roughness: 0,
};
oct_tree1.set_cube(cube.clone());
@ -122,26 +122,26 @@ 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(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))));
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 }, Matrix3::identity(), 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 }, Matrix3::identity(), 2.5, Vector3 { x: 255, y: 128, z: 0 }, 128, 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 }, Matrix3::identity(), 1.5, Vector3 { x: 0, y: 255, z: 0 }, 64, false))));
scene.volumetrics.push(Rc::new(RefCell::new(comp)));
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))));
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 }, Matrix3::identity(), 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 }, Matrix3::identity(), 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)));
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)));
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 }, Matrix3::identity(), 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 }, Matrix3::identity(), 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)));
Ok((cgmath::point3(5.0, 5.0, 10.0)))
Ok(cgmath::point3(11.0 + grid_size as f32, 11.0 + grid_size as f32, 10.0))
}
pub fn generate_test_scene2(scene: &mut Scene, data: &mut AppData, chunk_num_x: usize, chunk_num_y: usize, chunk_num_z: usize, num_gaussians: usize) -> Result<(Point3<f32>)> {
pub fn generate_test_scene2(scene: &mut Scene, data: &mut AppData, chunk_num_x: usize, chunk_num_y: usize, chunk_num_z: usize, num_gaussians: usize) -> Result<Point3<f32>> {
let mut rng = rand::thread_rng();
let scale = 1.0;

View file

@ -0,0 +1,73 @@
use std::{cell::RefCell, rc::Rc};
use cgmath::{Matrix3, Vector3};
use crate::scene::volumetrics::{Volumetrics, ShapeComposition};
#[derive(Debug, Clone)]
pub struct HierarchyModel {
volume: Rc<RefCell<dyn Volumetrics>>,
include: bool,
rotation: Matrix3<f32>,
translation: Vector3<f32>,
children: Vec<Rc<RefCell<Self>>>,
dirty: bool, // denotes if any rotation or translation was touched since the last memory insert, which does not need a new memory pos calculation
child_dirty: bool, // denotes if anything in the child structure changed, which will need a new memory pos calculation. child_dirty will always need the more throughout update, hence a check assessing the update need of the structure can early break on disvoering this, as everything dirty forces to update child_dirty should update as well.
}
impl HierarchyModel {
pub fn new(volume: Rc<RefCell<dyn Volumetrics>>, include: bool, rotation: Matrix3<f32>, translation: Vector3<f32>) -> Self {
Self { volume, include, rotation, translation, children: vec![], dirty: true, child_dirty: false }
}
pub fn add_child(&mut self, mut child: Self) {
self.children.push(Rc::new(RefCell::new(child)));
self.child_dirty = true;
}
pub fn get_child(&self, index: usize) -> Rc<RefCell<Self>> {
self.children[index].clone()
}
pub fn remove_child(&mut self, index: usize) {
self.children.remove(index);
self.child_dirty = true;
}
pub fn update_rotation(&mut self, rotation: Matrix3<f32>) {
self.rotation = rotation;
self.dirty = true;
}
pub fn update_translation(&mut self, translation: Vector3<f32>) {
self.translation = translation;
self.dirty = true;
}
pub fn check_dirty_state(&self) -> (bool, bool) {
if self.children.len() == 0 || self.child_dirty {
return (self.dirty, self.child_dirty)
} else {
let mut dirty = self.dirty;
let mut child_dirty = self.child_dirty;
for child in &self.children {
let result = child.borrow().check_dirty_state();
dirty = dirty || result.0;
child_dirty = child_dirty || result.1;
if child_dirty {
break;
}
}
(dirty, child_dirty)
}
}
pub fn to_shape_composition(&self, start_pos: Vector3<f32>, size: u32) -> ShapeComposition {
let mut comp = ShapeComposition::new(size);
comp
}
}

View file

@ -1,5 +1,4 @@
use cgmath::AbsDiffEq;
use cgmath::{InnerSpace, MetricSpace, Vector3};
use cgmath::{InnerSpace, Vector3};
use crate::vertex;
use super::memorizable::Memorizable;
@ -115,7 +114,7 @@ impl Light for PointLight {
new_diff.z += diff_high.z;
}
if ((new_diff.x < new_diff.y || new_diff.y == 0.0) && (new_diff.x < new_diff.z || new_diff.z == 0.0) && new_diff.x != 0.0) {
if (new_diff.x < new_diff.y || new_diff.y == 0.0) && (new_diff.x < new_diff.z || new_diff.z == 0.0) && new_diff.x != 0.0 {
offset_vec.x -= new_diff.x;
} else {
if (new_diff.y < new_diff.z || new_diff.z == 0.0) && new_diff.y != 0.0 {
@ -223,10 +222,10 @@ mod test {
let mem_size = p.get_buffer_mem_size(&data);
assert!(mem_size == 7);
let mut memory = vec![0; 7];
assert!(!p.dirty);
p.insert_into_memory(memory, &data, &scene);
let memory = vec![0; 7];
assert!(p.dirty);
p.insert_into_memory(memory, &data, &scene);
assert!(!p.dirty);
}
#[test]
@ -239,7 +238,7 @@ mod test {
let mem_size = p.get_buffer_mem_size(&data);
assert!(mem_size == 7);
let mut memory = vec![0; 6];
let memory = vec![0; 6];
assert!(!p.dirty);
p.insert_into_memory(memory, &data, &scene);
assert!(p.dirty);
@ -254,10 +253,10 @@ mod test {
let mem_size = p.get_buffer_mem_size(&data);
assert!(mem_size == 7);
let mut memory = vec![0; 7];
assert!(!p.dirty);
p.insert_into_memory(memory, &data, &scene);
let memory = vec![0; 7];
assert!(p.dirty);
p.insert_into_memory(memory, &data, &scene);
assert!(!p.dirty);
}
#[test]
@ -270,7 +269,7 @@ mod test {
let mem_size = p.get_buffer_mem_size(&data);
assert!(mem_size == 7);
let mut memory = vec![0; 6];
let memory = vec![0; 6];
assert!(!p.dirty);
p.insert_into_memory(memory, &data, &scene);
assert!(p.dirty);

View file

@ -1,4 +1,3 @@
use super::light::LightSource;
use super::AppData;
use super::Scene;

View file

@ -4,13 +4,14 @@ mod light;
mod memorizable;
pub mod generators;
pub mod volumetrics;
pub mod hierarchy_model;
use anyhow::Ok;
use light::{DirectionalLight, LightSource, PointLight};
use vulkanalia::prelude::v1_0::*;
use anyhow::Result;
use cgmath::{vec2, vec3, Vector3};
use cgmath::Vector3;
use std::cell::RefCell;
use std::rc::Rc;
@ -18,15 +19,14 @@ use std::rc::Rc;
use crate::scene::memorizable::{Memorizable, CompoundMemorizable};
use crate::app_data::AppData;
use crate::buffer;
use crate::primitives::rec_cuboid::Cuboid;
use crate::vertex;
use crate::primitives::cube::Cube;
use crate::primitives::drawable::Drawable;
use crate::scene::oct_tree::{OctTree, OctTreeIter, CHUNK_SIZE};
use crate::scene::oct_tree::{OctTree, CHUNK_SIZE};
use crate::scene::empty_volume::EmptyVolume;
use crate::scene::hierarchy_model::HierarchyModel;
extern crate rand;
use rand::Rng;
#[repr(C)]
#[derive(Clone, Debug, Default)]
@ -181,10 +181,16 @@ impl Scene {
return true
}
}
for shape_comps in &self.volumetrics {
if shape_comps.borrow().is_dirty() {
return true
}
}
return false
}
pub fn update_memory(&mut self, data: &mut AppData, reuse_memory: bool) {
pub fn update_memory(&mut self, data: &mut AppData, reuse_memory: bool) -> 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 = 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)
@ -215,6 +221,7 @@ impl Scene {
let mut compute_task_one_out_size = 0;
let mut target_index = 1;
let mut node_count = 0;
let mut transparrent_index = 0;
for compound in &self.volumetrics {
// calculate the memory starts in the volumetrics memory
compound.borrow_mut().set_memory_start(compound_data_len);
@ -222,6 +229,9 @@ impl Scene {
// calculate the memory pos in the output memory
compound.borrow_mut().target_memory_start = target_index;
target_index += compound.borrow().get_target_buffer_mem_size();
// calculate the memory start for the transparency array
compound.borrow_mut().transparency_index = transparrent_index;
transparrent_index += compound.borrow().size.pow(3);
// calculate overall task sizes and oct tree node count
node_count += compound.borrow().get_num_nodes();
compute_task_one_size += compound.borrow().size.pow(2) as usize;
@ -230,15 +240,30 @@ impl Scene {
let mut volume_vec;
let needs_overwrite;
if !reuse_memory || memory_index != self.rt_memory.len() {
let volumetrics_needs_overwrite;
if !reuse_memory || memory_index > self.rt_memory.len() {
volume_vec = vec![data.num_lights_per_volume; memory_index];
volumetrics_memory = vec![compound_data_len as u32; compound_data_len];
needs_overwrite = true;
} else {
needs_overwrite = false;
volume_vec = self.rt_memory.clone();
volumetrics_memory = self.volumetrics_memory.clone();
}
if !reuse_memory || compound_data_len > self.volumetrics_memory.len() || compound_data_len <= self.volumetrics_memory.len() / 2 {
volumetrics_memory = vec![compound_data_len as u32; compound_data_len];
volumetrics_needs_overwrite = true;
println!("recreated volumetrics memory");
} else {
volumetrics_needs_overwrite = false;
volumetrics_memory = self.volumetrics_memory.clone();
println!("reused volumetrics memory");
}
volumetrics_memory[0] = compound_data_len as u32;
for i in compound_data_len..self.volumetrics_memory.len() {
volumetrics_memory[i] = 0;
}
volume_vec[0] = data.num_lights_per_volume;
volume_vec[1] = data.max_iterations_per_light;
volume_vec[2] = data.diffuse_raster_steps;
@ -254,7 +279,7 @@ impl Scene {
}
for compound in &self.volumetrics {
if needs_overwrite || compound.borrow().is_dirty() {
if volumetrics_needs_overwrite || compound.borrow().is_dirty() {
volumetrics_memory = compound.borrow_mut().insert_into_memory(volumetrics_memory, data, &self);
}
}
@ -269,6 +294,9 @@ impl Scene {
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;
//return wheter we need resized buffers
needs_overwrite || volumetrics_needs_overwrite
}
pub unsafe fn destroy(&mut self, device: &vulkanalia::Device) {

View file

@ -1,30 +1,35 @@
use crate::app_data::AppData;
use super::{Scene, memorizable::CompoundMemorizable};
use cgmath::{InnerSpace, Vector3};
use winit::dpi::Size;
use cgmath::{InnerSpace, Vector3, Matrix3};
use super::memorizable::Memorizable;
use std::cell::RefCell;
use std::{cell::RefCell};
use std::rc::Rc;
use serde::{Deserialize, Serialize};
use serde_json::{Serializer};
pub trait Volumetrics: Memorizable {
fn get_pos(&self) -> Vector3<f32>;
fn set_pos(&mut self, p: Vector3<f32>);
fn get_rot(&self) -> Vector3<f32>;
fn set_rot(&mut self, p: Vector3<f32>);
fn get_rot(&self) -> Matrix3<f32>;
fn set_rot(&mut self, p: Matrix3<f32>);
fn get_bbox(&self) -> (Vector3<f32>,Vector3<f32>);
fn is_transparent(&self) -> bool;
fn set_transparency(&mut self, transparent: bool);
fn serialize_wrapper(&self, serializer: &mut Serializer<std::fs::File>) -> Result<(), serde_json::error::Error>;
}
enum ShapeTypes {
#[derive(Clone, Debug, PartialEq)]
pub enum ShapeTypes {
SPHERE,
CONE,
CUBOID,
CUBOID
}
@ -41,11 +46,12 @@ pub struct ShapeComposition {
pub bbox_high: Vector3<f32>,
pub overlapping_volumes: Vec<u32>,
pub scale: f32,
pub transparency_index: u32,
}
impl ShapeComposition {
pub fn new(size: u32) -> Self {
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 }, scale: 0.0, overlapping_volumes: vec![] }
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 }, scale: 0.0, overlapping_volumes: vec![], transparency_index: 0 }
}
pub fn prepare_for_mem_insertion(&mut self) {
@ -100,8 +106,8 @@ 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, target address, num_volumes, included_address, excluded_address
1 + 1 + 1 + 1 + 1 + 3 + 1 + 1 + self.included_shapes.len() as u32 + self.excluded_shapes.len() as u32 + self.overlapping_volumes.len() as u32
//size, scale, memory_end, num_included, num_excluded, pos, target address, num_volumes, transparent start, included_address, excluded_address, overlapping volumes
1 + 1 + 1 + 1 + 1 + 3 + 1 + 1 + 1 + self.included_shapes.len() as u32 + self.excluded_shapes.len() as u32 + self.overlapping_volumes.len() as u32
}
fn get_prev_buffer_mem_size(&self) -> u32 {
@ -129,7 +135,7 @@ impl Memorizable for ShapeComposition {
let mut shapes_memory = 0;
let shapes_index = self.memory_start + self.get_buffer_mem_size(data) as usize;
let mut element_offset = 10;
let mut element_offset = 11;
for volumetric in &self.included_shapes {
volumetric.borrow_mut().set_memory_start(shapes_index + shapes_memory);
shapes_memory += volumetric.borrow().get_buffer_mem_size(data) as usize;
@ -152,7 +158,6 @@ impl Memorizable for ShapeComposition {
element_offset += 1;
}
println!("Compound links to volumes:");
for volume_start in &self.overlapping_volumes {
v[self.memory_start + element_offset] = volume_start.clone();
element_offset += 1;
@ -167,6 +172,7 @@ impl Memorizable for ShapeComposition {
v[self.memory_start + 7] = u32::from_ne_bytes(self.bbox_low.z.to_ne_bytes());
v[self.memory_start + 8] = self.target_memory_start as u32;
v[self.memory_start + 9] = self.overlapping_volumes.len() as u32;
v[self.memory_start + 10] = self.transparency_index; // transparency mem start
self.prev_memory_size = self.get_compound_buffer_mem_size(data);
self.dirty = false;
@ -188,10 +194,10 @@ impl PartialEq for ShapeComposition {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Sphere {
pos: Vector3<f32>,
rot: Vector3<f32>, // rotation will only matter once textures are implemented, till then it is a holdover
rot: Matrix3<f32>, // rotation will only matter once textures are implemented, till then it is a holdover
color: Vector3<u8>, // color, either as pure color or texture modifier
transparent: bool,
radius: f32,
@ -203,7 +209,7 @@ pub struct Sphere {
impl Sphere {
pub fn new(
pos: Vector3<f32>,
rot: Vector3<f32>,
rot: Matrix3<f32>,
radius: f32,
color: Vector3<u8>,
roughness: u8,
@ -215,12 +221,12 @@ impl Sphere {
impl Memorizable for Sphere {
fn get_buffer_mem_size(&self, data: &AppData) -> u32 {
// type, pos, rot, (color + roughness), transparent, radius
1 + 3 + 3 + 1 + 1 + 1
1 + 3 + 9 + 1 + 1 + 1
}
fn get_prev_buffer_mem_size(&self) -> u32 {
// constant memory size
1 + 3 + 3 + 1 + 1 + 1
1 + 3 + 9 + 1 + 1 + 1
}
fn is_dirty(&self) -> bool {
self.dirty
@ -231,15 +237,21 @@ impl Memorizable for Sphere {
v[self.memory_start + 2] = u32::from_ne_bytes(self.pos.y.to_ne_bytes());
v[self.memory_start + 3] = u32::from_ne_bytes(self.pos.z.to_ne_bytes());
v[self.memory_start + 4] = u32::from_ne_bytes(self.rot.x.to_ne_bytes());
v[self.memory_start + 5] = u32::from_ne_bytes(self.rot.y.to_ne_bytes());
v[self.memory_start + 6] = u32::from_ne_bytes(self.rot.z.to_ne_bytes());
v[self.memory_start + 4] = u32::from_ne_bytes(self.rot[0][0].to_ne_bytes());
v[self.memory_start + 5] = u32::from_ne_bytes(self.rot[0][1].to_ne_bytes());
v[self.memory_start + 6] = u32::from_ne_bytes(self.rot[0][2].to_ne_bytes());
v[self.memory_start + 7] = u32::from_ne_bytes(self.rot[1][0].to_ne_bytes());
v[self.memory_start + 8] = u32::from_ne_bytes(self.rot[1][1].to_ne_bytes());
v[self.memory_start + 9] = u32::from_ne_bytes(self.rot[1][2].to_ne_bytes());
v[self.memory_start + 10] = u32::from_ne_bytes(self.rot[2][0].to_ne_bytes());
v[self.memory_start + 11] = u32::from_ne_bytes(self.rot[2][1].to_ne_bytes());
v[self.memory_start + 12] = u32::from_ne_bytes(self.rot[2][2].to_ne_bytes());
v[self.memory_start + 7] = u32::from_ne_bytes([self.color.x, self.color.y, self.color.z, self.roughness]);
v[self.memory_start + 13] = u32::from_ne_bytes([self.color.x, self.color.y, self.color.z, self.roughness]);
v[self.memory_start + 8] = self.transparent as u32;
v[self.memory_start + 14] = self.transparent as u32;
v[self.memory_start + 9] = u32::from_ne_bytes(self.radius.to_ne_bytes());
v[self.memory_start + 15] = u32::from_ne_bytes(self.radius.to_ne_bytes());
self.dirty = false;
@ -264,11 +276,11 @@ impl Volumetrics for Sphere {
self.pos = p;
}
fn get_rot(&self) -> Vector3<f32> {
fn get_rot(&self) -> Matrix3<f32> {
self.rot
}
fn set_rot(&mut self, p: Vector3<f32>) {
fn set_rot(&mut self, p: Matrix3<f32>) {
self.dirty = true;
self.rot = p;
}
@ -285,13 +297,17 @@ impl Volumetrics for Sphere {
fn set_transparency(&mut self, transparent: bool) {
self.transparent = transparent;
}
fn serialize_wrapper(&self, serializer: &mut Serializer<std::fs::File>) -> Result<(), serde_json::error::Error> {
self.serialize(serializer)
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Cone {
pos: Vector3<f32>,
rot: Vector3<f32>,
rot: Matrix3<f32>,
color: Vector3<u8>, // color, either as pure color or texture modifier
transparent: bool,
radius1: f32,
@ -305,7 +321,7 @@ pub struct Cone {
impl Cone {
pub fn new(
pos: Vector3<f32>,
rot: Vector3<f32>,
rot: Matrix3<f32>,
radius1: f32,
radius2: f32,
direction: Vector3<f32>,
@ -319,12 +335,12 @@ impl Cone {
impl Memorizable for Cone {
fn get_buffer_mem_size(&self, data: &AppData) -> u32 {
// type, pos, rot, (color + roughness), transparent, radius1, radius2, direction
1 + 3 + 3 + 1 + 1 + 1 + 1 + 3
1 + 3 + 9 + 1 + 1 + 1 + 1 + 3
}
fn get_prev_buffer_mem_size(&self) -> u32 {
// constant memory size
1 + 3 + 3 + 1 + 1 + 1 + 1 + 3
1 + 3 + 9 + 1 + 1 + 1 + 1 + 3
}
fn is_dirty(&self) -> bool {
self.dirty
@ -335,20 +351,26 @@ impl Memorizable for Cone {
v[self.memory_start + 2] = u32::from_ne_bytes(self.pos.y.to_ne_bytes());
v[self.memory_start + 3] = u32::from_ne_bytes(self.pos.z.to_ne_bytes());
v[self.memory_start + 4] = u32::from_ne_bytes(self.rot.x.to_ne_bytes());
v[self.memory_start + 5] = u32::from_ne_bytes(self.rot.y.to_ne_bytes());
v[self.memory_start + 6] = u32::from_ne_bytes(self.rot.z.to_ne_bytes());
v[self.memory_start + 4] = u32::from_ne_bytes(self.rot[0][0].to_ne_bytes());
v[self.memory_start + 5] = u32::from_ne_bytes(self.rot[0][1].to_ne_bytes());
v[self.memory_start + 6] = u32::from_ne_bytes(self.rot[0][2].to_ne_bytes());
v[self.memory_start + 7] = u32::from_ne_bytes(self.rot[1][0].to_ne_bytes());
v[self.memory_start + 8] = u32::from_ne_bytes(self.rot[1][1].to_ne_bytes());
v[self.memory_start + 9] = u32::from_ne_bytes(self.rot[1][2].to_ne_bytes());
v[self.memory_start + 10] = u32::from_ne_bytes(self.rot[2][0].to_ne_bytes());
v[self.memory_start + 11] = u32::from_ne_bytes(self.rot[2][1].to_ne_bytes());
v[self.memory_start + 12] = u32::from_ne_bytes(self.rot[2][2].to_ne_bytes());
v[self.memory_start + 7] = u32::from_ne_bytes([self.color.x, self.color.y, self.color.z, self.roughness]);
v[self.memory_start + 13] = u32::from_ne_bytes([self.color.x, self.color.y, self.color.z, self.roughness]);
v[self.memory_start + 8] = self.transparent as u32;
v[self.memory_start + 14] = self.transparent as u32;
v[self.memory_start + 9] = u32::from_ne_bytes(self.radius1.to_ne_bytes());
v[self.memory_start + 10] = u32::from_ne_bytes(self.radius2.to_ne_bytes());
v[self.memory_start + 15] = u32::from_ne_bytes(self.radius1.to_ne_bytes());
v[self.memory_start + 16] = u32::from_ne_bytes(self.radius2.to_ne_bytes());
v[self.memory_start + 11] = u32::from_ne_bytes(self.direction.x.to_ne_bytes());
v[self.memory_start + 12] = u32::from_ne_bytes(self.direction.y.to_ne_bytes());
v[self.memory_start + 13] = u32::from_ne_bytes(self.direction.z.to_ne_bytes());
v[self.memory_start + 17] = u32::from_ne_bytes(self.direction.x.to_ne_bytes());
v[self.memory_start + 18] = u32::from_ne_bytes(self.direction.y.to_ne_bytes());
v[self.memory_start + 19] = u32::from_ne_bytes(self.direction.z.to_ne_bytes());
self.dirty = false;
@ -373,18 +395,17 @@ impl Volumetrics for Cone {
self.pos = p;
}
fn get_rot(&self) -> Vector3<f32> {
fn get_rot(&self) -> Matrix3<f32> {
self.rot
}
fn set_rot(&mut self, p: Vector3<f32>) {
fn set_rot(&mut self, p: Matrix3<f32>) {
self.dirty = true;
self.rot = p;
}
fn get_bbox(&self) -> (Vector3<f32>, Vector3<f32>) {
let rot = cgmath::Matrix3::from_angle_x(cgmath::Rad(self.rot.x)) * cgmath::Matrix3::from_angle_y(cgmath::Rad(self.rot.y)) * cgmath::Matrix3::from_angle_z(cgmath::Rad(self.rot.z));
let dir = rot * self.direction;
let dir = self.rot * self.direction;
let vec_one;
let vec_two;
if dir.x != 0.0 || dir.z != 0.0 {
@ -413,13 +434,17 @@ impl Volumetrics for Cone {
fn set_transparency(&mut self, transparent: bool) {
self.transparent = transparent;
}
fn serialize_wrapper(&self, serializer: &mut Serializer<std::fs::File>) -> Result<(), serde_json::error::Error> {
self.serialize(serializer)
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Rect {
pos: Vector3<f32>,
rot: Vector3<f32>,
rot: Matrix3<f32>,
color: Vector3<u8>, // color, either as pure color or texture modifier
transparent: bool,
size: Vector3<f32>,
@ -431,7 +456,7 @@ pub struct Rect {
impl Rect {
pub fn new(
pos: Vector3<f32>,
rot: Vector3<f32>,
rot: Matrix3<f32>,
size: Vector3<f32>,
color: Vector3<u8>,
roughness: u8,
@ -443,12 +468,12 @@ impl Rect {
impl Memorizable for Rect {
fn get_buffer_mem_size(&self, data: &AppData) -> u32 {
// type, pos, rot, (color + roughness), transparent, radius1, radius2, direction
1 + 3 + 3 + 1 + 1 + 3
1 + 3 + 9 + 1 + 1 + 3
}
fn get_prev_buffer_mem_size(&self) -> u32 {
// constant memory size
1 + 3 + 3 + 1 + 1 + 3
1 + 3 + 9 + 1 + 1 + 3
}
fn is_dirty(&self) -> bool {
self.dirty
@ -459,17 +484,23 @@ impl Memorizable for Rect {
v[self.memory_start + 2] = u32::from_ne_bytes(self.pos.y.to_ne_bytes());
v[self.memory_start + 3] = u32::from_ne_bytes(self.pos.z.to_ne_bytes());
v[self.memory_start + 4] = u32::from_ne_bytes(self.rot.x.to_ne_bytes());
v[self.memory_start + 5] = u32::from_ne_bytes(self.rot.y.to_ne_bytes());
v[self.memory_start + 6] = u32::from_ne_bytes(self.rot.z.to_ne_bytes());
v[self.memory_start + 4] = u32::from_ne_bytes(self.rot[0][0].to_ne_bytes());
v[self.memory_start + 5] = u32::from_ne_bytes(self.rot[0][1].to_ne_bytes());
v[self.memory_start + 6] = u32::from_ne_bytes(self.rot[0][2].to_ne_bytes());
v[self.memory_start + 7] = u32::from_ne_bytes(self.rot[1][0].to_ne_bytes());
v[self.memory_start + 8] = u32::from_ne_bytes(self.rot[1][1].to_ne_bytes());
v[self.memory_start + 9] = u32::from_ne_bytes(self.rot[1][2].to_ne_bytes());
v[self.memory_start + 10] = u32::from_ne_bytes(self.rot[2][0].to_ne_bytes());
v[self.memory_start + 11] = u32::from_ne_bytes(self.rot[2][1].to_ne_bytes());
v[self.memory_start + 12] = u32::from_ne_bytes(self.rot[2][2].to_ne_bytes());
v[self.memory_start + 7] = u32::from_ne_bytes([self.color.x, self.color.y, self.color.z, self.roughness]);
v[self.memory_start + 13] = u32::from_ne_bytes([self.color.x, self.color.y, self.color.z, self.roughness]);
v[self.memory_start + 8] = self.transparent as u32;
v[self.memory_start + 14] = self.transparent as u32;
v[self.memory_start + 9] = u32::from_ne_bytes(self.size.x.to_ne_bytes());
v[self.memory_start + 10] = u32::from_ne_bytes(self.size.y.to_ne_bytes());
v[self.memory_start + 11] = u32::from_ne_bytes(self.size.z.to_ne_bytes());
v[self.memory_start + 15] = u32::from_ne_bytes(self.size.x.to_ne_bytes());
v[self.memory_start + 16] = u32::from_ne_bytes(self.size.y.to_ne_bytes());
v[self.memory_start + 17] = u32::from_ne_bytes(self.size.z.to_ne_bytes());
self.dirty = false;
@ -494,20 +525,19 @@ impl Volumetrics for Rect {
self.pos = p;
}
fn get_rot(&self) -> Vector3<f32> {
fn get_rot(&self) -> Matrix3<f32> {
self.rot
}
fn set_rot(&mut self, p: Vector3<f32>) {
fn set_rot(&mut self, p: Matrix3<f32>) {
self.dirty = true;
self.rot = p;
}
fn get_bbox(&self) -> (Vector3<f32>, Vector3<f32>) {
let rot = cgmath::Matrix3::from_angle_x(cgmath::Rad(self.rot.x)) * cgmath::Matrix3::from_angle_y(cgmath::Rad(self.rot.y)) * cgmath::Matrix3::from_angle_z(cgmath::Rad(self.rot.z));
let dir1 = rot * Vector3 { x: self.size.x / 2.0, y: 0.0, z: 0.0 };
let dir2 = rot * Vector3 { x: 0.0, y: self.size.y / 2.0, z: 0.0 };
let dir3 = rot * Vector3 { x: 0.0, y: 0.0, z: self.size.z / 2.0 };
let dir1 = self.rot * Vector3 { x: self.size.x / 2.0, y: 0.0, z: 0.0 };
let dir2 = self.rot * Vector3 { x: 0.0, y: self.size.y / 2.0, z: 0.0 };
let dir3 = self.rot * Vector3 { x: 0.0, y: 0.0, z: self.size.z / 2.0 };
let pos1 = self.pos - dir1 - dir2 - dir3;
let pos2 = self.pos - dir1 - dir2 + dir3;
@ -539,4 +569,50 @@ impl Volumetrics for Rect {
fn set_transparency(&mut self, transparent: bool) {
self.transparent = transparent;
}
}
fn serialize_wrapper(&self, serializer: &mut Serializer<std::fs::File>) -> Result<(), serde_json::error::Error> {
self.serialize(serializer)
}
}
#[cfg(test)]
mod test {
use cgmath::SquareMatrix;
use super::*;
use std::fs::OpenOptions;
#[test]
fn test_serialization_rect() {
// we generally expect serde to do it's job, so there will only be one tested struct
let rect: Rect = Rect::new(Vector3 { x: 1.0, y: 2.0, z: 3.0 }, Matrix3::identity(), Vector3 { x: 1.0, y: 3.0, z: 1.0 }, Vector3 { x: 255, y: 128, z: 0 }, 64, false);
let file_ref: std::fs::File = OpenOptions::new().create(true).write(true).truncate(true).open("rect_dump.json").expect("Could not open file for writing!");
serde_json::to_writer(&file_ref, &rect).expect("Serialization failed");
let file_ref = OpenOptions::new().read(true).create(false).write(false).truncate(false).open("rect_dump.json").expect("Could not open file for reading!");
let result: Rect = serde_json::from_reader(&file_ref).expect("Could not unserialize json");
assert!(rect == result, "Deserialized rect did not match!");
std::fs::remove_file("rect_dump.json").expect("Could not remove file!");
}
#[test]
fn test_serialization_wrapper_rect() {
let rect: Rect = Rect::new(Vector3 { x: 1.0, y: 2.0, z: 3.0 }, Matrix3::identity(), Vector3 { x: 1.0, y: 3.0, z: 1.0 }, Vector3 { x: 255, y: 128, z: 0 }, 64, false);
let file_ref: std::fs::File = OpenOptions::new().create(true).write(true).truncate(true).open("rect_manual_dump.json").expect("Could not open file for writing!");
let mut serializer = serde_json::Serializer::new(file_ref);
rect.serialize_wrapper(&mut serializer).expect("Serializer did not work!");
let file_ref = OpenOptions::new().read(true).create(false).write(false).truncate(false).open("rect_manual_dump.json").expect("Could not open file for reading!");
let result: Rect = serde_json::from_reader(&file_ref).expect("Could not unserialize json");
assert!(rect == result, "Deserialized rect did not match!");
std::fs::remove_file("rect_manual_dump.json").expect("Could not remove file!");
}
}

View file

@ -275,14 +275,15 @@ pub struct RTVolVertex {
pub volume_start: u32,
pub color_roughness: u32,
facing: Facing,
pub transparent: bool,
}
impl RTVolVertex {
pub const fn new(pos: Vec3, volume_start: u32, color_roughness: u32, facing: Facing) -> Self {
Self { pos, volume_start, color_roughness, facing }
pub const fn new(pos: Vec3, volume_start: u32, color_roughness: u32, facing: Facing, transparent: bool) -> Self {
Self { pos, volume_start, color_roughness, facing, transparent }
}
}
impl VertexContainer<4> for RTVolVertex {
impl VertexContainer<5> for RTVolVertex {
fn binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::builder()
.binding(0)
@ -291,7 +292,7 @@ impl VertexContainer<4> for RTVolVertex {
.build()
}
fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 4] {
fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 5] {
let pos = vk::VertexInputAttributeDescription::builder()
.binding(0)
.location(0)
@ -320,7 +321,14 @@ impl VertexContainer<4> for RTVolVertex {
.offset((size_of::<Vec3>() + size_of::<u32>() * 2) as u32)
.build();
[pos, volume_start, color_roughness, facing]
let flags = vk::VertexInputAttributeDescription::builder()
.binding(0)
.location(4)
.format(vk::Format::R32_UINT)
.offset((size_of::<Vec3>() + size_of::<u32>() * 3) as u32)
.build();
[pos, volume_start, color_roughness, facing, flags]
}
}