Commit c4940732 authored by Johannes Braun's avatar Johannes Braun
Browse files

Made BVH classes more abstract

parent 3b2d65e7
<linespace version="1.0">
<item name="subdivisions" value="30"/>
<item name="subdivisions" value="10"/>
<item name="radial-subdivision" value="50"/>
<item name="generator" value="gpu"/>
<item name="generator" value="cpu"/>
</linespace>
......@@ -7,5 +7,5 @@
<color value="#0277BD"/>
<!--<texture src="/textures/LS_Field.png" scale="100"/>-->
</item>
<item name="duration" value="300"/>
</splash>
\ No newline at end of file
<item name="duration" value="500"/>
</splash>
#ifndef __LINESPACE_GLH
#define __LINESPACE_GLH
#extension GL_ARB_shading_language_include : require
#extension GL_ARB_compute_variable_group_size : require
#extension GL_NV_gpu_shader5 : require
#extension GL_NV_shader_buffer_load : require
layout(local_size_variable) in;
#include <raytracer/basic_structs.glsl>
#include <raytracer/utilities.glsl>
#define FACE_BIAS 0.003
#define T_MIN_BIAS 0.0001
struct Voxel
{
Bounds bounds;
UIntRange children;
uint containment_flags;
uint depth;
};
uniform struct
{
uint node_count;
uint max_depth;
Triangle* triangles;
Int32Range* candidates;
Voxel* voxels;
} u_ntree;
uniform struct
{
uint resolution;
uint resolution_pow2;
uint resolution_6xpow2;
uint entries_per_node;
//equals lines buffer item count (which is approx. line count/32)
uint entry_count;
//Buffer
uint32_t* lines;
} u_linespace;
const int c_correllating_faces[3][2] = {
{1, 2}, {0, 2}, {0, 1}
};
bool getLineSpaceEntry(int nodeIndex, int start_face, int start_dim_a, int start_dim_b, int end_face, int end_dim_a, int end_dim_b) // faces in [0..5]
{
int lineIndex = (start_face * int(u_linespace.resolution_pow2) + start_dim_b * int(u_linespace.resolution) + start_dim_a) * int(u_linespace.resolution_6xpow2) +
(end_face * int(u_linespace.resolution_pow2) + end_dim_b * int(u_linespace.resolution) + end_dim_a);
return (u_linespace.lines[nodeIndex * int(u_linespace.entries_per_node) + ((lineIndex) >> 5)] & (1 << (lineIndex & 31))) != 0;
}
bool faceIsPlausible(const in float axis_value)
{
return abs(axis_value) < FACE_BIAS;
}
struct StackElement
{
vec3 delta;
float t_min;
vec3 t_next;
float t_max;
ivec3 current_index;
int node_index;
int start_face_index;
int end_face_index;
};
bool lspace_intersectsAny(const in Ray ray, const in float max_t, inout uint voxel_index, inout vec4 debug_color)
{
//TODO: FIX!
//Why? Somewhere in here there is a HUGE buffer overflow. At least I think so. It may also be some kind of indetermined loop or something of that I can't think right now.
return false;
Voxel root = u_ntree.voxels[0];
StackElement stack[5];
//Initialize delta
vec3 step = sign(ray.direction.xyz);
stack[0].delta = step / ray.direction.xyz * bounds__size(root.bounds).xyz;
//Initialize t_min and t_max
vec3 t_max = (root.bounds.max.xyz - ray.origin.xyz) / ray.direction.xyz;
vec3 t_min = (root.bounds.min.xyz - ray.origin.xyz) / ray.direction.xyz;
stack[0].t_min = compMax(min(t_min, t_max));
stack[0].t_max = min(compMin(max(t_min, t_max)), max_t);
if (stack[0].t_max < stack[0].t_min || stack[0].t_max < 0) return false;
stack[0].t_min = max(stack[0].t_min, 0);
const ivec3 max_index = ivec3(u_linespace.resolution - 1);
vec3 start_position = ((ray.origin.xyz + stack[0].t_min*ray.direction.xyz) - root.bounds.min.xyz) / (bounds__size(root.bounds).xyz / u_linespace.resolution);
stack[0].current_index = max(min(ivec3(start_position), max_index), 0);
stack[0].delta /= vec3(u_linespace.resolution);
vec3 next_factor = start_position - vec3(stack[0].current_index);
next_factor.x = mix(next_factor.x, 1.0f - next_factor.x, floor((sign(step.x) + 1) / 2));
next_factor.y = mix(next_factor.y, 1.0f - next_factor.y, floor((sign(step.y) + 1) / 2));
next_factor.z = mix(next_factor.z, 1.0f - next_factor.z, floor((sign(step.z) + 1) / 2));
stack[0].t_next = next_factor * stack[0].delta + stack[0].t_min;
stack[0].node_index = 0;
stack[0].start_face_index = -1;
for (int i = 0; i < 3; ++i)
{
//Replaces the following if-statement
//if (faceIsPlausible(start_position[i]) || faceIsPlausible(start_position[i] - u_linespace.resolution)) stack[0].sFaceIndex = i;
stack[0].start_face_index = max(stack[0].start_face_index + 1, (i + 1) * int(faceIsPlausible(start_position[i]) || faceIsPlausible(start_position[i] - u_linespace.resolution))) - 1;
}
//Now, the start face index is -1, if the ray begins inside the voxel, otherwise it is between 0 and 2
//Temporary variables used when traversing.
//Start
ivec3 start_index;
int start_dimension_a;
int start_dimenstion_b;
int start_face;
//End
ivec3 end_index;
int end_dimension_a;
int end_dimension_b;
int end_face;
int current_stack_depth = 0;
while(current_stack_depth >= 0)
{
if (stack[current_stack_depth].t_min + T_MIN_BIAS >= stack[current_stack_depth].t_max)
{
--current_stack_depth;
continue;
}
StackElement current_item = stack[current_stack_depth];
int last_stack_depth = current_stack_depth;
Voxel current_voxel = u_ntree.voxels[stack[last_stack_depth].node_index];
bool has_children = current_voxel.children.min != 0;
int next_node_index = int(current_voxel.children.min)
+ stack[last_stack_depth].current_index.z * int(u_linespace.resolution_pow2)
+ stack[last_stack_depth].current_index.y * int(u_linespace.resolution)
+ stack[last_stack_depth].current_index.x;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// DEBUG
/*if (has_children && next_node_index >= u_ntree.node_count)
return false;*/
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
stack[last_stack_depth].end_face_index = max(0,
int(stack[last_stack_depth].t_next.y < stack[last_stack_depth].t_next.x && stack[last_stack_depth].t_next.y < stack[last_stack_depth].t_next.z));
stack[last_stack_depth].end_face_index = max(stack[last_stack_depth].end_face_index,
2*int(stack[last_stack_depth].t_next.y < stack[last_stack_depth].t_next.x && stack[last_stack_depth].t_next.y < stack[last_stack_depth].t_next.z));
if(!has_children)
{
//return true;
// What?
//if (stack[tempStackDepth].tMin > (currNode.childrenSize.x*2.0f)) return true;
//Traverse candidates if needed.
}
else
{
bool continue_traversal = false;
if (stack[last_stack_depth].start_face_index == -1)
{
//Does start inside the voxel
continue_traversal = true;
}
else
{
Voxel next_voxel = u_ntree.voxels[next_node_index];
vec3 next_start_position = ((ray.origin.xyz + stack[last_stack_depth].t_min * ray.direction.xyz) - next_voxel.bounds.min.xyz) / (bounds__size(next_voxel.bounds).xyz / u_linespace.resolution);
start_index = max(min(ivec3(next_start_position), max_index), 0);
end_index = max(min(
ivec3(((ray.origin.xyz + stack[last_stack_depth].t_next[stack[last_stack_depth].end_face_index] * ray.direction.xyz) - next_voxel.bounds.min.xyz) / (bounds__size(next_voxel.bounds).xyz / u_linespace.resolution)),
max_index), 0);
start_dimension_a = start_index[c_correllating_faces[stack[last_stack_depth].start_face_index][0]];
start_dimenstion_b = start_index[c_correllating_faces[stack[last_stack_depth].start_face_index][1]];
end_dimension_a = end_index[c_correllating_faces[stack[last_stack_depth].end_face_index][0]];
end_dimension_b = end_index[c_correllating_faces[stack[last_stack_depth].end_face_index][1]];
start_face = stack[last_stack_depth].start_face_index + int(ray.direction[stack[last_stack_depth].start_face_index] < 0.0f) * 3;
end_face = stack[last_stack_depth].end_face_index + int(ray.direction[stack[last_stack_depth].end_face_index] >= 0.0f) * 3;
if (getLineSpaceEntry(next_node_index, start_face, start_dimension_a, start_dimenstion_b, end_face, end_dimension_a, end_dimension_b))
{
if ((stack[last_stack_depth].t_min > 0.1f) && ((next_voxel.depth + 1) >= u_ntree.max_depth)) {
voxel_index = uint(next_node_index);
return true;
}
continue_traversal = true;
}
}
if (continue_traversal)
{
++current_stack_depth;
Voxel next_voxel = u_ntree.voxels[next_node_index];
start_position = ((ray.origin.xyz + stack[last_stack_depth].t_min * ray.direction.xyz) - next_voxel.bounds.min.xyz) / (bounds__size(next_voxel.bounds).xyz / (bounds__size(next_voxel.bounds).xyz / u_linespace.resolution));
stack[current_stack_depth].current_index = max(min(ivec3(start_position), max_index), 0);
stack[current_stack_depth].delta = stack[last_stack_depth].delta / u_linespace.resolution;
next_factor = start_position - vec3(stack[current_stack_depth].current_index);
next_factor.x = mix(next_factor.x, 1.0f - next_factor.x, floor((sign(step.x) + 1) / 2));
next_factor.y = mix(next_factor.y, 1.0f - next_factor.y, floor((sign(step.y) + 1) / 2));
next_factor.z = mix(next_factor.z, 1.0f - next_factor.z, floor((sign(step.z) + 1) / 2));
stack[current_stack_depth].t_next = next_factor * stack[current_stack_depth].delta + stack[last_stack_depth].t_min;
stack[current_stack_depth].node_index = next_node_index;
stack[current_stack_depth].t_min = stack[last_stack_depth].t_min;
stack[current_stack_depth].t_max = stack[last_stack_depth].t_next[stack[last_stack_depth].end_face_index];
stack[current_stack_depth].start_face_index = -1;
for (int i = 0; i < 3; ++i)
{
//Replaces the following if-statement
//if (faceIsPlausible(start_position[i]) || faceIsPlausible(start_position[i] - u_linespace.resolution)) stack[0].sFaceIndex = i;
stack[current_stack_depth].start_face_index = max(stack[current_stack_depth].start_face_index + 1,
(i + 1) * int(faceIsPlausible(start_position[i]) || faceIsPlausible(start_position[i] - u_linespace.resolution))) - 1;
}
}
/*if (current_stack_depth >= 2) {
debug_color = vec4(stack[current_stack_depth].delta, 1);
return true;
}*/
}
int e_idx = stack[last_stack_depth].end_face_index;
stack[last_stack_depth].t_min = stack[last_stack_depth].t_next[e_idx];
stack[last_stack_depth].t_next[e_idx] += stack[last_stack_depth].delta[e_idx];
stack[last_stack_depth].start_face_index = e_idx;
stack[last_stack_depth].current_index[e_idx] += int(step[e_idx]);
}
return false;
}
#endif //!__LINESPACE_GLH
#version 430
in vec4 color;
layout(location=0) out vec4 out_color;
void main()
{
out_color = color;
}
\ No newline at end of file
#version 430
#extension GL_ARB_shading_language_include : require
#include <raytracer/std.glsl>
#include <raytracer/basic_structs.glsl>
#include <raytracer/buffers.glsl>
#include <raytracer/utilities.glsl>
struct Patch
{
uint face;
uint padding;
uvec2 index;
};
struct Line
{
//vec4 center_begin;
//vec4 center_end;
Hit nearest;
Hit farthest;
};
STD_BUFFER line_buffer
{
Line b_lines[];
};
layout(points) in;
layout(line_strip, max_vertices = 2) out;
uniform mat4 u_view_projection;
uniform uint u_mesh;
uniform uint u_triangle_count;
uniform bool u_full_ls;
out vec4 color;
void main()
{
int tbi_id = int(gl_in[0].gl_Position.x);
Line line = b_lines[tbi_id];
vec4 last_position;
/*if (u_full_ls) {
gl_Position = u_view_projection * line.center_begin;
color = vec4(0, 1, 0, 1);
EmitVertex();
gl_Position = u_view_projection * line.center_end;
color = vec4(1, 0, 0, 1);
EmitVertex();
EndPrimitive();
}
else {*/
if (line.nearest.barycentric.x != -1 && line.farthest.barycentric.x != -1) {
Mesh mesh = b_meshes[u_mesh];
Triangle triangle = mesh.triangles[line.nearest.triangle];
Vertex v1 = mesh.vertices[triangle.indices[1]];
Vertex v2 = mesh.vertices[triangle.indices[2]];
Vertex v3 = mesh.vertices[triangle.indices[0]];
vec4 position = hit__getFromBarycentric(line.nearest.barycentric, triangle, position);
gl_Position = u_view_projection * position;
last_position = position;
color = vec4(0, 1, 0, 1);
EmitVertex();
mesh = b_meshes[u_mesh];
triangle = mesh.triangles[line.farthest.triangle];
v1 = mesh.vertices[triangle.indices[1]];
v2 = mesh.vertices[triangle.indices[2]];
v3 = mesh.vertices[triangle.indices[0]];
position = hit__getFromBarycentric(line.farthest.barycentric, triangle, position);
if (position == last_position)
{
position += vec4(vec3(1e-2), 0);
}
gl_Position = u_view_projection * position;
color = vec4(1, 0, 0, 1);
EmitVertex();
}
EndPrimitive();
//}
}
#version 430
void main()
{
gl_Position = vec4(gl_VertexID, 0, 0, 1);
}
\ No newline at end of file
#version 430
in vec2 pass_texcoord;
uniform sampler2D u_grid_texture;
layout(location = 0) out vec4 out_final_color;
void main()
{
out_final_color = vec4(1, 1, 1, texture(u_grid_texture, pass_texcoord).a);
/*if (out_final_color.a < 0.9f)
discard;*/
}
\ No newline at end of file
#version 430
layout(location = 0) in vec4 in_position;
layout(location = 2) in vec2 in_texcoord;
uniform mat4 u_model_view_projection;
out vec2 pass_texcoord;
void main()
{
gl_Position = u_model_view_projection * in_position;
pass_texcoord = in_texcoord;
}
\ No newline at end of file
......@@ -2,7 +2,7 @@
#extension GL_ARB_bindless_texture : require
in vec2 out_texcoord;
//in vec2 out_texcoord;
uniform sampler2D screen_texture;
uniform vec2 u_inverse_resolution;
......@@ -39,18 +39,11 @@ void main()
{
ivec2 pos = ivec2(gl_FragCoord.xy);
vec2 resolution = 1/u_inverse_resolution;
vec2 center = resolution / 2;
vec2 texture_half_size = screen_texture.textureSize(0) / 2;
vec2 texel_position = vec2(pos) - center + texture_half_size*u_scale;
/*if(any(texel_position.lessThan(vec2(0, 0))) || any(texel_position.greaterThan(2*texture_half_size)))
{
discard;
}*/
//vec2 texture_coordinate = vec2(pos) / (u_scale * 2 * texture_half_size);
vec2 texel_position = vec2(pos) - center + texture_half_size*u_scale;
vec4 final_background = vec4(1);
......@@ -60,13 +53,13 @@ void main()
final_background *= mix(vec4(1), u_background_color, bool(u_background_configuration & bg_color) ? 1 : 0);
final_background *= mix(vec4(1), grad_vertical, bool(u_background_configuration & bg_gradient_vertical) ? 1 : 0);
final_background *= mix(vec4(1), grad_horizontal, bool(u_background_configuration & bg_gradient_horizontal) ? 1 : 0);
if (bool(u_background_configuration & bg_texture)) {
final_background *= texture(u_background_texture, vec2(pos)*u_background_texture_scale);
}
out_final_color = texelFetch(screen_texture, ivec2(texel_position/u_scale), 0);
out_final_color.a *= u_alpha;
out_final_color = vec4(mix(final_background.rgb, out_final_color.rgb, out_final_color.a), 1);
}
\ No newline at end of file
}
......@@ -7,7 +7,7 @@
#include <screenshader/gbuffer/gbuffer_lights.glsl>
#include <screenshader/utilities.glsl>
in vec2 out_texcoord;
//in vec2 out_texcoord;
uniform samplerCube environment;
uniform vec3 camera_position;
......
......@@ -2,8 +2,6 @@
#extension GL_ARB_bindless_texture : require
in vec2 out_texcoord;
uniform sampler2DMS screen_texture;
uniform vec2 u_inverse_resolution;
uniform uint u_sample_count;
......@@ -20,4 +18,4 @@ void main()
}
out_final_color /= u_sample_count;
//out_final_color = texture(screen_texture, out_texcoord);
}
\ No newline at end of file
}
#version 430
#extension GL_ARB_shading_language_include : require
layout(points) in;
layout(triangle_strip, max_vertices = 3) out;
void main()
{
gl_Position = vec4(-1, -1, 0.0, 1.0 );
EmitVertex();
gl_Position = vec4(3, -1, 0.0, 1.0 );
EmitVertex();
gl_Position = vec4(-1, 3, 0.0, 1.0 );
EmitVertex();
EndPrimitive();
}
......@@ -2,10 +2,10 @@
layout(location=0) in vec2 in_position;
out vec2 out_texcoord;
//out vec2 out_texcoord;
void main()
{
out_texcoord = in_position;
gl_Position = vec4((in_position -0.5)*2, 0, 1);
}
\ No newline at end of file
//out_texcoord = in_position;
gl_Position = vec4(0, 0, 0, 1);
}
......@@ -2,8 +2,6 @@
#extension GL_ARB_bindless_texture : require
in vec2 out_texcoord;
uniform sampler2D screen_texture;
uniform vec2 u_inverse_resolution;
......@@ -11,5 +9,6 @@ layout(location = 0) out vec4 out_final_color;
void main()
{
out_final_color = texture(screen_texture, out_texcoord);
}
\ No newline at end of file
ivec2 pos = ivec2(gl_FragCoord.xy);
out_final_color = texelFetch(screen_texture, pos, 0);
}
......@@ -15,7 +15,6 @@ float schlickFresnel(const in vec3 view, const in vec3 normal, const in float io
return R0 + pow(1.0 - dot(view, normal), 5.0) * (1.0 - R0);
}
float gauss[5][5] = {
{ 0.003765, 0.015019, 0.023792, 0.015019, 0.003765 },
{ 0.015019, 0.059912, 0.094907, 0.059912, 0.015019 },
......
#version 430
#extension GL_ARB_shading_language_include : require
#include <simple/simple.glsl>
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
in VertexData pass_vertex[];
out VertexData out_vertex;
out flat int out_primitive;
void main()
{
out_primitive = gl_PrimitiveIDIn;
gl_PrimitiveID = gl_PrimitiveIDIn;
out_vertex = pass_vertex[0];
out_vertex.barycentric = vec3(1, 0, 0);
gl_Position = gl_in[0].gl_Position;
EmitVertex();
out_vertex = pass_vertex[1];
out_vertex.barycentric = vec3(0, 1, 0);
gl_Position = gl_in[1].gl_Position;
EmitVertex();
out_vertex = pass_vertex[2];
out_vertex.barycentric = vec3(0, 0, 1);
gl_Position = gl_in[2].gl_Position;
EmitVertex();
EndPrimitive();
}
#version 430
in vec4 color;
layout(location=0) out vec4 out_color;