Commit 2cb137cd authored by Johannes Braun's avatar Johannes Braun
Browse files

Finished UniformGrid LS traversal.

parent e4a9ccd1
......@@ -4,7 +4,7 @@ Size=353,64
Collapsed=0
[Scene]
Pos=14,79
Pos=10,88
Size=352,837
Collapsed=0
......
<linespace version="1.0">
<item name="subdivisions" value="12"/>
<item name="subdivisions" value="10"/>
<item name="grid-subdivision" value="4"/>
<item name="radial-subdivision" value="50"/>
<item name="generator" value="gpu"/>
</linespace>
......@@ -25,6 +25,29 @@ bool intersectsAny(const in Ray ray, const in float max_distance, bool force_bvh
return traverseObjects(ray, true, max_distance, unused_hit, t_min, force_bvh);
}
int smallestAxis(const in vec3 vector)
{
if(vector.x < vector.y)
{
if(vector.x < vector.z)
{
return 0;
}
else
{
return 2;
}
}
else if(vector.y < vector.z)
{
return 1;
}
else
{
return 2;
}
}
bool traverseObjects(const in Ray ray, const in bool use_first, const in float max_distance, inout Hit hit, inout float t_min, bool force_bvh = false)
{
if(!use_global_bvh)
......@@ -48,90 +71,49 @@ bool traverseObjects(const in Ray ray, const in bool use_first, const in float m
else
{
Ray local_ray = ray.makeRelative(mesh);
/*
float local_tmin, local_tmax;
Bounds mesh_bounds = mesh.nodes[0].bounds;
if(local_ray.intersectsBounds(mesh_bounds, local_tmin, local_tmax, t_min))
if(local_ray.intersectsBounds(mesh_bounds, current_t))
{
vec3 start_position_on_box = local_ray.origin + local_tmin * local_ray.direction;
vec3 start_position_relative = start_position_on_box - mesh_bounds.min.xyz;
start_position_relative /= mesh.grid_resolution;
ivec3 start_voxel = clamp(ivec3(start_position_relative), ivec3(0), mesh.grid_resolution-1);
vec3 voxel_size = mesh_bounds.size().xyz / vec3(mesh.grid_resolution);
vec3 end_position_on_box = local_ray.origin + local_tmax * local_ray.direction;
vec3 end_position_relative = end_position_on_box - mesh_bounds.min.xyz;
end_position_relative /= mesh.grid_resolution;
ivec3 end_voxel = clamp(ivec3(end_position_relative), ivec3(0), mesh.grid_resolution-1);
vec3 voxel_size = mesh_bounds / vec3(mesh.grid_resolution);
ivec3 direction_sign = sign(local_ray.direction);
float delta = direction_sign / local_ray.direction * voxel_size;
vec3 tnext = offset * delta;
vec3 step = sign(local_ray.direction);
vec3 delta = step / local_ray.direction * voxel_size;
vec3 position = (local_ray.origin - mesh_bounds.min.xyz) / voxel_size;
ivec3 index = clamp(ivec3(position), ivec3(0), mesh.grid_resolution-1);
ivec3 totalStepSign = ivec3(greaterThan(step, ivec3(0)));
ivec3 offset = ivec3(0);
for(; start_voxel != end_voxel; start_voxel = clamp(start_voxel + offset, ivec3(0), mesh.grid_resolution-1))
//bool x;
//int a = x + (1-2*x)*blub;
// x true -> x = 1, a = 1 - blub;
// x false -> x = 0, a = 0+0*b
vec3 tnext = (totalStepSign + (1-2*totalStepSign) * (position - index)) * delta;
float temp_t_min = 0.f;
for(;;)
{
vec3 voxel_direction = normalize(vec3(end_voxel - start_voxel));
vec3 difference = abs(voxel_direction - local_ray.direction);
int axis = 0;
for(int ax=1; ax<3; ++ax)
{
if(difference[axis] < difference[ax])
axis = ax;
}
offset = ivec3(0);
offset[axis] = 1;
if(voxel_direction[axis] < 0)
offset[axis] = -1;
int ls_index = start_voxel.x + start_voxel.y * mesh.grid_resolution.y + start_voxel.z * mesh.grid_resolution.y * mesh.grid_resolution.z;
Hit bla;
int ls_index = index.x + index.y * mesh.grid_resolution.x + index.z * mesh.grid_resolution.y * mesh.grid_resolution.z;
int axis = smallestAxis(tnext);
float mint = t_min;
if(mesh.traverseLineSpace(0, local_ray, use_first, current_t, bla, mint) && mint < t_min){
hit = bla;
Hit temp_hit;
if(mesh.traverseLineSpace(ls_index, local_ray, use_first, current_t, temp_hit, mint)){
hit = temp_hit;
t_min = mint;
hit_triangle = true;
if (use_first)
{
return true;
}
if (use_first) return true;
break;
}
start_voxel = vec3(start_voxel) + offset * direction_sign;
}
*/
int ls_index = 0;//start_voxel.x + start_voxel.y * mesh.grid_resolution.y + start_voxel.z * mesh.grid_resolution.y * mesh.grid_resolution.z;
Hit bla;
float mint = t_min;
if(mesh.traverseLineSpace(ls_index, local_ray, use_first, current_t, bla, mint) && mint < t_min){
hit = bla;
t_min = mint;
hit_triangle = true;
if (use_first)
temp_t_min = tnext[axis];
index[axis] += int( step[axis] );
if ((index[axis] < 0) || (index[axis] >= mesh.grid_resolution[axis]))
{
return true;
break;
}
tnext[axis] += delta[axis];
}
//}
// for(int ls = 0; ls < mesh.num_linespaces; ++ls)
// {
// Hit bla;
// float mint = t_min;
// if(mesh.traverseLineSpace(ls, local_ray, use_first, current_t, bla, mint) && mint < t_min){
// hit = bla;
// t_min = mint;
// hit_triangle = true;
// if (use_first)
// {
// return true;
// }
// }
// }
}
}
}
......@@ -213,18 +195,48 @@ bool traverseObjects(const in Ray ray, const in bool use_first, const in float m
}
else
{
for(int ls = 0; ls < mesh.num_linespaces; ++ls)
Ray local_ray = ray.makeRelative(mesh);
Bounds mesh_bounds = mesh.nodes[0].bounds;
if(local_ray.intersectsBounds(mesh_bounds, current_t))
{
Hit bla;
float mint = t_min;
if(mesh.traverseLineSpace(ls, ray.makeRelative(mesh), use_first, current_t, bla, mint) && mint < t_min){
hit = bla;
t_min = mint;
hit_triangle = true;
if (use_first)
vec3 voxel_size = mesh_bounds.size().xyz / vec3(mesh.grid_resolution);
vec3 step = sign(local_ray.direction);
vec3 delta = step / local_ray.direction * voxel_size;
vec3 position = (local_ray.origin - mesh_bounds.min.xyz) / voxel_size;
ivec3 index = clamp(ivec3(position), ivec3(0), mesh.grid_resolution-1);
ivec3 totalStepSign = ivec3(greaterThan(step, ivec3(0)));
//bool x;
//int a = x + (1-2*x)*blub;
// x true -> x = 1, a = 1 - blub;
// x false -> x = 0, a = 0+0*b
vec3 tnext = (totalStepSign + (1-2*totalStepSign) * (position - index)) * delta;
float temp_t_min = 0.f;
for(;;)
{
int ls_index = index.x + index.y * mesh.grid_resolution.x + index.z * mesh.grid_resolution.y * mesh.grid_resolution.z;
int axis = smallestAxis(tnext);
float mint = t_min;
Hit temp_hit;
if(mesh.traverseLineSpace(ls_index, local_ray, use_first, current_t, temp_hit, mint)){
hit = temp_hit;
t_min = mint;
hit_triangle = true;
if (use_first) return true;
break;
}
temp_t_min = tnext[axis];
index[axis] += int( step[axis] );
if ((index[axis] < 0) || (index[axis] >= mesh.grid_resolution[axis]))
{
return true;
break;
}
tnext[axis] += delta[axis];
}
}
}
......
......@@ -31,5 +31,5 @@ void main()
vec2 random = settings.u_random_subpixel ? vec2(0, 0) : rand2D(random_seed + id, target_size.x*target_size.y);
traces_data[id].ray = u_camera.getRayFromPixel(vec2(gl_GlobalInvocationID.xy), random, vec2(target_size));
traces_data[id].hit.invalidate();
traces_data[id].ray.nearestIntersection(traces_data[id].hit, false);
traces_data[id].ray.nearestIntersection(traces_data[id].hit, true);
}
......@@ -43,7 +43,7 @@ namespace glare::raytrace
void Linespace::generate(const LocalCollector &collector, int uniform_grid_subdivision, int uniform_grid_index, int max_subdivisions, const Platform generator)
{
Log_Info << "Building Line Space for collector id " << collector.id() << "...";
//Log_Info << "Building Line Space for collector id " << collector.id() << "...";
core::ClockGL::instance().start();
m_bounds = collector.bounds();
auto grid = m_bounds.computeSubdivision(uniform_grid_subdivision);
......@@ -84,12 +84,15 @@ namespace glare::raytrace
const unsigned line_count = lineCount();
m_line_buffer.makeResident(gl::Access::eReadOnly);
Log_Info << "[Linespace Data] Generation Time: " << core::ClockGL::instance().end() / 1000000.f << "ms";
Log_Info << "[Linespace Data] Lines: " << line_count;
Log_Info << "[Linespace Data] Memory: " << (line_count * sizeof(Line)) << " Bytes";
Log_Info << "[Linespace Data] Patch Subdivisions: " << m_subdivision.resolution.x << ", " << m_subdivision.resolution.y << ", " << m_subdivision.resolution.z;
Log_Info << "[Linespace Data] Patch Sizes: x=" << m_subdivision.size.x << ", y=" << m_subdivision.size.y << ", z=" << m_subdivision.size.z;
Log_Info << "[Linespace Data] Object Bounds: " << collector.bounds();
if constexpr(false)
{
Log_Info << "[Linespace Data] Generation Time: " << core::ClockGL::instance().end() / 1000000.f << "ms";
Log_Info << "[Linespace Data] Lines: " << line_count;
Log_Info << "[Linespace Data] Memory: " << (line_count * sizeof(Line)) << " Bytes";
Log_Info << "[Linespace Data] Patch Subdivisions: " << m_subdivision.resolution.x << ", " << m_subdivision.resolution.y << ", " << m_subdivision.resolution.z;
Log_Info << "[Linespace Data] Patch Sizes: x=" << m_subdivision.size.x << ", y=" << m_subdivision.size.y << ", z=" << m_subdivision.size.z;
Log_Info << "[Linespace Data] Object Bounds: " << collector.bounds();
}
}
void Linespace::generateCPU(const LocalCollector &collector)
......@@ -102,7 +105,7 @@ namespace glare::raytrace
const Face begin_face = configuration.first;
const Face end_face = configuration.second;
Log_Info << "Working on lines between Faces " << static_cast<unsigned>(begin_face) << " and " << static_cast<unsigned>(end_face) << "...";
//Log_Info << "Working on lines between Faces " << static_cast<unsigned>(begin_face) << " and " << static_cast<unsigned>(end_face) << "...";
//For readability, I'll add those variables here.
const unsigned begin_count = m_subdivision.resolution[FaceAxises::get(begin_face).horizontal] * m_subdivision.resolution[FaceAxises::get(begin_face).vertical];
......@@ -182,7 +185,7 @@ namespace glare::raytrace
const Face begin_face = configuration.first;
const Face end_face = configuration.second;
Log_Debug << "Working on lines between Faces " << static_cast<unsigned>(begin_face) << " and " << static_cast<unsigned>(end_face) << "...";
//Log_Debug << "Working on lines between Faces " << static_cast<unsigned>(begin_face) << " and " << static_cast<unsigned>(end_face) << "...";
//For readability, I'll add those variables here.
const unsigned begin_count = patchCount(begin_face);
......
......@@ -57,21 +57,22 @@ namespace glare::raytrace
m_vertex_buffer->makeResident(gl::Access::eReadOnly);
m_triangle_buffer->makeResident(gl::Access::eReadOnly);
int subdiv_grid = 1;
auto subdiv = m_bounds.computeSubdivision(subdiv_grid);
m_grid_resolution = glm::ivec3(subdiv.resolution);
int count = m_grid_resolution.x * m_grid_resolution.y * m_grid_resolution.z;
pugi::xml_document doc;
doc.load_file(files::asset("preferences/linespace_default.xml").c_str());
const auto& linespace_tag = doc.child("linespace");
const int subdiv_grid = linespace_tag.find_child_by_attribute("item", "name", "grid-subdivisions").attribute("value").as_int(16);
const int max_subdivisions = linespace_tag.find_child_by_attribute("item", "name", "subdivisions").attribute("value").as_int(16);
const std::string gen = linespace_tag.find_child_by_attribute("item", "name", "generator").attribute("value").as_string("cpu");
const Platform generator = gen == "gpu" ? Platform::eGPU : Platform::eCPU;
auto subdiv = m_bounds.computeSubdivision(subdiv_grid);
m_grid_resolution = glm::ivec3(subdiv.resolution);
int count = m_grid_resolution.x * m_grid_resolution.y * m_grid_resolution.z;
m_linespaces.resize(count);
for (int i = 0; i < count; ++i)
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment