VoxelEngine2/shaders/rt_quad.frag
2025-01-13 16:18:05 +01:00

283 lines
No EOL
13 KiB
GLSL

#version 450
layout(location = 0) flat in uvec2 fragRasterPos;
layout(location = 1) flat in uint fragVolumeStart;
layout(location = 2) in vec3 origPosition;
layout(location = 3) flat in uint facing;
layout(location = 0) out vec4 outColor;
layout(binding = 2) buffer SceneInfoBuffer{
uint infos[];
} scene_info;
uvec4 unpack_color(uint val) {
// left most 8 bits first
uint val1 = (val >> 24);
uint val2 = (val << 8) >> 24;
uint val3 = (val << 16) >> 24;
uint val4 = (val << 24) >> 24;
return uvec4(val4, val3, val2, val1);
}
uint sample_neighbor_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) {
uint array_descr_start = volume_start + 6 + scene_info.infos[0];
uint color_array_start = array_descr_start + 24;
uint top_color_size_u = scene_info.infos[array_descr_start];
uint top_color_size_v = scene_info.infos[array_descr_start + 1];
uint bottom_color_size_u = scene_info.infos[array_descr_start + 2];
uint bottom_color_size_v = scene_info.infos[array_descr_start + 3];
uint left_color_size_u = scene_info.infos[array_descr_start + 4];
uint left_color_size_v = scene_info.infos[array_descr_start + 5];
uint right_color_size_u = scene_info.infos[array_descr_start + 6];
uint right_color_size_v = scene_info.infos[array_descr_start + 7];
uint front_color_size_u = scene_info.infos[array_descr_start + 8];
uint front_color_size_v = scene_info.infos[array_descr_start + 9];
uint back_color_size_u = scene_info.infos[array_descr_start + 10];
uint back_color_size_v = scene_info.infos[array_descr_start + 11];
uint top_neighbor_size_u = scene_info.infos[array_descr_start + 12];
uint top_neighbor_size_v = scene_info.infos[array_descr_start + 13];
uint bottom_neighbor_size_u = scene_info.infos[array_descr_start + 14];
uint bottom_neighbor_size_v = scene_info.infos[array_descr_start + 15];
uint left_neighbor_size_u = scene_info.infos[array_descr_start + 16];
uint left_neighbor_size_v = scene_info.infos[array_descr_start + 17];
uint right_neighbor_size_u = scene_info.infos[array_descr_start + 18];
uint right_neighbor_size_v = scene_info.infos[array_descr_start + 19];
uint front_neighbor_size_u = scene_info.infos[array_descr_start + 20];
uint front_neighbor_size_v = scene_info.infos[array_descr_start + 21];
uint back_neighbor_size_u = scene_info.infos[array_descr_start + 22];
uint back_neighbor_size_v = scene_info.infos[array_descr_start + 23];
uint top_color_size = top_color_size_u * top_color_size_v;
uint bottom_color_size = bottom_color_size_u * bottom_color_size_v;
uint left_color_size = left_color_size_u * left_color_size_v;
uint right_color_size = right_color_size_u * right_color_size_v;
uint front_color_size = front_color_size_u * front_color_size_v;
uint back_color_size = back_color_size_u * back_color_size_v;
uint color_array_end = color_array_start + top_color_size + bottom_color_size + left_color_size + right_color_size + front_color_size + back_color_size;
uint top_neighbor_size = top_neighbor_size_u * top_neighbor_size_v;
uint bottom_neighbor_size = bottom_neighbor_size_u * bottom_neighbor_size_v;
uint left_neighbor_size = left_neighbor_size_u * left_neighbor_size_v;
uint right_neighbor_size = right_neighbor_size_u * right_neighbor_size_v;
uint front_neighbor_size = front_neighbor_size_u * front_neighbor_size_v;
uint back_neighbor_size = back_neighbor_size_u * back_neighbor_size_v;
// maybe do an array solution for this as well
uint array_start = color_array_end + uint(f > 0) * top_neighbor_size + uint(f > 1) * bottom_neighbor_size + uint(f > 2) * left_neighbor_size + uint(f > 3) * right_neighbor_size + uint(f > 4) * front_neighbor_size;
uint us[6] = {top_neighbor_size_u, bottom_neighbor_size_u, left_neighbor_size_u, right_neighbor_size_u, front_neighbor_size_u, back_neighbor_size_u};
uint vs[6] = {top_neighbor_size_v, bottom_neighbor_size_v, left_neighbor_size_v, right_neighbor_size_v, front_neighbor_size_v, back_neighbor_size_v};
uint u_size = us[f];
uint v_size = vs[f];
uint value = scene_info.infos[array_start + raster_pos.x * v_size * uint(u_size > 1) + raster_pos.y * uint(v_size > 1)];
return value;
}
uvec4 sample_color_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) {
uint array_descr_start = volume_start + 6 + scene_info.infos[0];
uint color_array_start = array_descr_start + 24;
uint top_color_size_u = scene_info.infos[array_descr_start];
uint top_color_size_v = scene_info.infos[array_descr_start + 1];
uint bottom_color_size_u = scene_info.infos[array_descr_start + 2];
uint bottom_color_size_v = scene_info.infos[array_descr_start + 3];
uint left_color_size_u = scene_info.infos[array_descr_start + 4];
uint left_color_size_v = scene_info.infos[array_descr_start + 5];
uint right_color_size_u = scene_info.infos[array_descr_start + 6];
uint right_color_size_v = scene_info.infos[array_descr_start + 7];
uint front_color_size_u = scene_info.infos[array_descr_start + 8];
uint front_color_size_v = scene_info.infos[array_descr_start + 9];
uint back_color_size_u = scene_info.infos[array_descr_start + 10];
uint back_color_size_v = scene_info.infos[array_descr_start + 11];
uint top_neighbor_size_u = scene_info.infos[array_descr_start + 12];
uint top_neighbor_size_v = scene_info.infos[array_descr_start + 13];
uint top_size = top_color_size_u * top_color_size_v;
uint bottom_size = bottom_color_size_u * bottom_color_size_v;
uint left_size = left_color_size_u * left_color_size_v;
uint right_size = right_color_size_u * right_color_size_v;
uint front_size = front_color_size_u * front_color_size_v;
uint back_size = back_color_size_u * back_color_size_v;
// maybe do an array solution for this as well
uint array_start = color_array_start + uint(f > 0) * top_size + uint(f > 1) * bottom_size + uint(f > 2) * left_size + uint(f > 3) * right_size + uint(f > 4) * front_size;
uint us[6] = {top_color_size_u, bottom_color_size_u, left_color_size_u, right_color_size_u, front_color_size_u, back_color_size_u};
uint vs[6] = {top_color_size_v, bottom_color_size_v, left_color_size_v, right_color_size_v, front_color_size_v, back_color_size_v};
uint u_size = us[f];
uint v_size = vs[f];
uint value = scene_info.infos[array_start + raster_pos.x * v_size * uint(u_size > 1) + raster_pos.y * uint(v_size > 1)];
return unpack_color(value);
}
vec3 get_light_position(uint light_index) {
return vec3(float(scene_info.infos[light_index]), float(scene_info.infos[light_index + 1]), float(scene_info.infos[light_index + 2]));
}
vec3 get_light_color(uint light_index) {
return vec3(float(scene_info.infos[light_index + 3]) / 255.0, float(scene_info.infos[light_index + 4]) / 255.0, float(scene_info.infos[light_index + 5]) / 255.0);
}
void main() {
uint max_length = scene_info.infos[0];
uint last = scene_info.infos[max_length];
uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, fragRasterPos, facing);
vec4 color_sample = vec4(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0, 1);
uint max_light_num = scene_info.infos[0];
uint light_num = 0;
uint volume_index = fragVolumeStart;
uint volume_pos_x = scene_info.infos[volume_index + 0];
uint volume_pos_y = scene_info.infos[volume_index + 1];
uint volume_pos_z = scene_info.infos[volume_index + 2];
uint light_index = scene_info.infos[volume_index + 6];
vec3 light_direction = get_light_position(light_index) - origPosition;
vec3 light_color = get_light_color(light_index);
bool x_pos = light_direction.x > 0.0;
bool x_null = (light_direction.x == 0.0);
bool y_pos = light_direction.y > 0.0;
bool y_null = (light_direction.y == 0.0);
bool z_pos = light_direction.z > 0.0;
bool z_null = (light_direction.z == 0.0);
vec3 color_sum = vec3(0.0, 0.0, 0.0) + (color_sample.xyz * 0.01);
uint max_iterations = 2; //max_light_num * 20;
for (int i = 0; i < max_iterations; i++) {
float x_border = float(volume_pos_x + (scene_info.infos[volume_index + 3]) * uint(x_pos)) - 0.5;
float y_border = float(volume_pos_y + (scene_info.infos[volume_index + 4]) * uint(y_pos)) - 0.5;
float z_border = float(volume_pos_z + (scene_info.infos[volume_index + 5]) * uint(z_pos)) - 0.5;
bool needs_next_light = false;
// 2 is way behind the light position and should result in no collision being detected
float x_factor = 2.0;
float y_factor = 2.0;
float z_factor = 2.0;
if (!x_null) {
x_factor = (x_border - origPosition.x) / light_direction.x;
}
if (!y_null) {
y_factor = (y_border - origPosition.y) / light_direction.y;
}
if (!z_null) {
z_factor = (z_border - origPosition.z) / light_direction.z;
}
if ((x_factor >= 0.999 && y_factor >= 0.999 && z_factor >= 0.999)) {
// no hit, add light color result
color_sum += (color_sample.xyz * light_color) / (0.01 * light_direction.length() * light_direction.length() + 1.0);
needs_next_light = true;
} else {
// if there is a border hit before reaching the light
// change to the relevant next volume
// Todo: look into removing ifs from this
uint hit_facing = 0;
uint u = 0;
uint v = 0;
if (x_factor <= y_factor && x_factor <= z_factor) {
if (x_pos) {
hit_facing = 3;
} else {
hit_facing = 2;
}
vec3 intersection_pos = origPosition + x_factor * light_direction;
u = uint(ceil(intersection_pos.y)) - volume_pos_y;
v = uint(ceil(intersection_pos.z)) - volume_pos_z;
}
if (y_factor <= x_factor && y_factor <= z_factor) {
if (y_pos) {
hit_facing = 5;
} else {
hit_facing = 4;
}
vec3 intersection_pos = origPosition + y_factor * light_direction;
u = uint(ceil(intersection_pos.x)) - volume_pos_x;
v = uint(ceil(intersection_pos.z)) - volume_pos_z;
}
if (z_factor <= x_factor && z_factor <= y_factor) {
if (y_pos) {
hit_facing = 0;
} else {
hit_facing = 1;
}
vec3 intersection_pos = origPosition + z_factor * light_direction;
u = uint(ceil(intersection_pos.x)) - volume_pos_x;
v = uint(ceil(intersection_pos.y)) - volume_pos_y;
}
uint next_neighbor = sample_neighbor_from_scene_info(volume_index, uvec2(u, v), hit_facing);
uvec4 color_sample = sample_color_from_scene_info(volume_index, uvec2(u, v), hit_facing);
if (color_sample == uvec4(0, 0, 0, 0)) {
// not a color hit, so check neighbor
if (next_neighbor != 0) {
color_sum = vec3(1.0, 0.0, 0.0);
volume_index = next_neighbor;
uint volume_pos_x = scene_info.infos[volume_index + 0];
uint volume_pos_y = scene_info.infos[volume_index + 1];
uint volume_pos_z = scene_info.infos[volume_index + 2];
} else {
color_sum = vec3(0.0, 0.0, 1.0);
// neightbor miss, shouldn't happen with a light inside of a volume. Might happen with ambient light. For now move on to next light.
needs_next_light = true;
}
} else {
color_sum = vec3(1.0, 0.0, 1.0);
// color hit, move on to next light (may change once transparents are implemnted)
needs_next_light = true;
}
}
if (needs_next_light) {
break;
}
}
// todo light color memory index does not contain the expected values -> check
outColor = vec4(color_sum, 1.0);
/*if (scene_info.infos[1] == 16) {
outColor = vec4(0, 1, 0, 1);
}*/
/*if (fragVolumeStart == 1 && scene_info.infos[fragVolumeStart] == 16) {
outColor = vec4(0, 1, 0, 1);
}*/
/*if (facing == 0) {
outColor = vec4(1, 0, 0, 1);
} else if (facing == 1) {
outColor = vec4(0, 1, 1, 1);
} else if (facing == 2) {
outColor = vec4(0, 1, 0, 1);
} else if (facing == 3) {
outColor = vec4(1, 0, 1, 1);
} else if (facing == 4) {
outColor = vec4(0, 0, 1, 1);
} else if (facing == 5) {
outColor = vec4(1, 1, 0, 1);
}*/
}