Commit 88ba58f9 authored by Johannes Braun's avatar Johannes Braun
Browse files

Fixed a bug where the light distance is not being set properly (aka at all)

parent 4f275612
......@@ -76,11 +76,6 @@ bool traverseBVH(const in Mesh mesh, const in Ray local_ray, const in bool use_f
t_min = t;
hit_triangle = true;
if (use_first)
{
return true;
}
nearest.barycentric = vec2(u, v);
nearest.triangle = i;
nearest.mesh = mesh.id;
......@@ -94,6 +89,11 @@ bool traverseBVH(const in Mesh mesh, const in Ray local_ray, const in bool use_f
farthest.mesh = mesh.id;
}
if (use_first && hit_triangle)
{
return true;
}
}
}
......
......@@ -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();
bool intersects = traces_data[id].ray.nearestIntersection(traces_data[id].hit, true);
traces_data[id].ray.nearestIntersection(traces_data[id].hit, true);
}
......@@ -24,6 +24,8 @@ uniform Environment u_environment;
uniform int random_seed;
subroutine uniform lightSample u_light_sample[4];
const int num_effects = 4;
//Global properties for the pathtracer itself
uniform struct
{
......@@ -31,7 +33,7 @@ uniform struct
uint current_sample;
float clamp_direct;
float clamp_indirect;
ivec4 bounce_thresholds;
int bounce_thresholds[num_effects];
} u_render_config;
//Global properties for all mesh-local Line Spaces.
......@@ -40,7 +42,7 @@ uniform struct
float accuracy_quality;
float shadow_quality;
float distance_threshold;
ivec4 bounce_thresholds;
int bounce_thresholds[num_effects];
} u_linespace_properties;
struct LightData
......@@ -66,7 +68,7 @@ struct Bounce
float path_distance;
float pdf_accumulation;
ivec4 bounce_amount;
int bounce_amount[num_effects];
int count;
int bsdf_id;
};
......@@ -77,11 +79,40 @@ struct Bounce
////
//////////////////////////////////////////////////////////////////////////
int iArray(const in int array[num_effects], int index)
{
for(int i=0; i<4; ++i)
{
if(i==index)
{
return array[i];
}
}
}
int iArrayComp(const in int array1[num_effects], const in int array2[num_effects], int index)
{
for(int i=0; i<4; ++i)
{
if(i==index)
{
return array1[i] < array2[i] ? 1 : (array1[i] > array2[i] ? -1 : 0);
}
}
}
LightData sampleLight(const in Light light, const in Vertex vertex, const in vec2 random_sample)
{
LightData result;
result.sample_succeeded = u_light_sample[light.type % 4](light, vertex.position.xyz, result.point_on_light, result.light_color, random_sample);
for(int i=0; i<4; ++i)
{
if(i==light.type%4)
{
result.sample_succeeded = u_light_sample[i](light, vertex.position.xyz, result.point_on_light, result.light_color, random_sample);
}
}
vec3 non_normal_direction = result.point_on_light - vertex.position.xyz;
result.testing_distance = length(non_normal_direction);
result.shadow_test.direction = normalize(non_normal_direction);
result.shadow_test.origin = vertex.position.xyz + 1e-2f * vertex.normal.xyz;
return result;
......@@ -90,14 +121,14 @@ LightData sampleLight(const in Light light, const in Vertex vertex, const in vec
bool shouldUseBvh(const in Bounce bounce)
{
return (bounce.pdf_accumulation > (1-u_linespace_properties.accuracy_quality))
&& (bounce.bounce_amount[bounce.bsdf_id] < u_linespace_properties.bounce_thresholds[bounce.bsdf_id])
&& iArrayComp(bounce.bounce_amount, u_linespace_properties.bounce_thresholds, bounce.bsdf_id) == 1
&& bounce.path_distance < u_linespace_properties.distance_threshold;
}
bool shouldUseBvhShadow(const in Bounce bounce)
{
return (bounce.pdf_accumulation > (1-u_linespace_properties.shadow_quality))
&& (bounce.bounce_amount[bounce.bsdf_id] < u_linespace_properties.bounce_thresholds[bounce.bsdf_id])
&& iArrayComp(bounce.bounce_amount, u_linespace_properties.bounce_thresholds, bounce.bsdf_id) == 1
&& bounce.path_distance < u_linespace_properties.distance_threshold;
}
......@@ -136,7 +167,17 @@ bool shade(inout Bounce bounce)
//int someID = img_dimen % 2;//clamp(int(bsdf_result.bsdf_id), 0, 1);
bounce.bounce_amount[bsdf_result.bsdf_id] += 1;
bool exceeds_bounces = false;
for(int i=0; i<4; ++i)
{
if(i==bsdf_result.bsdf_id)
{
bounce.bounce_amount[i] += 1;
exceeds_bounces = bounce.bounce_amount[i] > u_render_config.bounce_thresholds[i];
break;
}
}
bounce.bsdf_id = int(bsdf_result.bsdf_id);
bounce.pdf_accumulation *= bsdf_result.probability_density;
bounce.ray = bsdf_result.generated_ray;
......@@ -147,7 +188,7 @@ bool shade(inout Bounce bounce)
return bounce.hit.invalidate();
}
if(bounce.bounce_amount[bsdf_result.bsdf_id] > u_render_config.bounce_thresholds[bsdf_result.bsdf_id]
if(exceeds_bounces
|| bsdf_result.radiance == vec3(0)
|| bounce.pdf_accumulation <= 1e-5f)
{
......@@ -160,6 +201,7 @@ bool shade(inout Bounce bounce)
//And sample it.
LightData data = light.sampleLight(vertex, random_sample);
if (data.sample_succeeded && !data.shadow_test.intersectsAny(data.testing_distance, shouldUseBvhShadow(bounce)))
{
vec3 lighting = data.light_color * bsdf_result.evaluation * bounce.radiance * max(dot(vertex.normal.xyz, normalize(data.shadow_test.direction)), 0.f);
......@@ -180,7 +222,7 @@ void trace(inout Bounce bounce)
void main()
{
int id = u_render_target.imageSize().x * int(gl_GlobalInvocationID.y) + int(gl_GlobalInvocationID.x);
int id = 1440 * int(gl_GlobalInvocationID.y) + int(gl_GlobalInvocationID.x);
//Discard pixel if it's not visible to avoid artifacts
ivec2 target_size = u_render_target.imageSize();
......@@ -189,14 +231,19 @@ void main()
// Get the trace generated by the external primary-ray-generator and initialize the Bounce struct.
Trace current = traces_data[id];
Bounce bounce;
bounce.hit = current.hit;
bounce.ray = current.ray;
ivec2 pixel = ivec2(bounce.ray.px, bounce.ray.py);
//u_render_target.imageStore(pixel, vec4(bounce.hit.barycentric, 0, 1));
//return;
//Threshold properties
bounce.path_distance = 0;
bounce.pdf_accumulation = 1.f;
bounce.bounce_amount = ivec4(0);
bounce.bounce_amount = int[4](0,0,0,0);
bounce.color = vec3(0);
bounce.radiance = vec3(1);
......@@ -208,7 +255,6 @@ void main()
bounce.trace();
}
ivec2 pixel = ivec2(bounce.ray.px, bounce.ray.py);
vec4 color = u_render_target.imageLoad(pixel);
// Divide color storage by sample count to retrieve the final color.
......
......@@ -35,7 +35,7 @@ struct Hit
/*
@bytesize 16
*/
// struct TraceProperties
// struct traceproperties
// {
// float accuracy_importance;
// float shadow_importance;
......@@ -53,7 +53,7 @@ struct Trace
Hit hit;
//Additional properties for BVH-LS-Hybrid
//TraceProperties properties;
// TraceProperties properties;
};
......
......@@ -146,6 +146,7 @@ void loadScene(const fs::path& path, float scale)
// Reload Pathtracer if used.
if (pathtracer && pathtracer->collector())
pathtracer->reload(core::Context::current().graph());
ImGui::ClearSelectableTreeData("tree_scene_graph");
}
void drawSceneWindow()
......
......@@ -116,31 +116,31 @@ namespace glare::core
m_graph_root->attach(default_cam_node);
gl::debugMessageCallback([&](gl::DebugSource source, gl::DebugType type, unsigned id, gl::DebugSeverity severity, std::string message) {
if (severity == gl::DebugSeverity::eHigh) {
switch (type)
{
case gl::DebugType::eError:
Log_Error << message;
break;
case gl::DebugType::eUndefinedBehavior:
case gl::DebugType::eDeprecatedBehavior:
case gl::DebugType::ePerformance:
case gl::DebugType::ePortability:
Log_Warn << message;
break;
case gl::DebugType::eMarker:
Log_Hint << message;
break;
case gl::DebugType::eOther:
Log_Debug << message;
break;
default:
break;
}
switch (type)
{
case gl::DebugType::eError:
Log_Error << message;
break;
case gl::DebugType::eUndefinedBehavior:
case gl::DebugType::eDeprecatedBehavior:
case gl::DebugType::ePerformance:
case gl::DebugType::ePortability:
Log_Warn << message;
break;
case gl::DebugType::eMarker:
Log_Hint << message;
break;
case gl::DebugType::eOther:
Log_Debug << message;
break;
default:
break;
}
});
gl::debugMessageControl(gl::DebugSeverity::eNotification, false);
gl::debugMessageControl(gl::DebugSeverity::eLow, false);
gl::setEnabled(gl::EnableParameter::eMultisample, true);
ImGui::glfw3::init(m_window.glfw(), false);
......
......@@ -76,8 +76,12 @@ namespace glare::raytrace
m_render_shader->uniform("u_render_config.clamp_direct", m_clamp_direct);
m_render_shader->uniform("u_render_config.clamp_indirect", m_clamp_indirect);
m_render_shader->uniform("u_render_config.bounce_thresholds", glm::ivec4(m_bounce_thresholds[0], m_bounce_thresholds[1], m_bounce_thresholds[2], m_bounce_thresholds[3]));
m_render_shader->uniform("u_linespace_properties.bounce_thresholds", glm::ivec4(m_linespace_bounces[0], m_linespace_bounces[1], m_linespace_bounces[2], m_linespace_bounces[3]));
for (int i = 0; i < 4; ++i)
{
const std::string idx = "[" + std::to_string(i) + "]";
m_render_shader->uniform("u_render_config.bounce_thresholds" + idx, m_bounce_thresholds[i]);
m_render_shader->uniform("u_linespace_properties.bounce_thresholds" + idx, m_linespace_bounces[i]);
}
m_render_shader->uniform("u_linespace_properties.accuracy_quality", m_linespace_accuracy);
m_render_shader->uniform("u_linespace_properties.shadow_quality", m_linespace_shadow);
......@@ -95,7 +99,7 @@ namespace glare::raytrace
m_width = width;
m_height = height;
m_trace_buffer.reserve<Trace>(width * height, gl::BufferUsage::eDynamicCopy);
m_trace_buffer.reserve<Trace>(2000*2000, gl::BufferUsage::eDynamicCopy);
m_render_target = std::make_unique<core::Texture>(core::Image<float>({ static_cast<int>(width), static_cast<int>(height) }, 4));
......
......@@ -12,13 +12,14 @@ namespace glare::raytrace
Ray ray;
Hit hit;
struct Properties
/*struct Properties
{
float accuracy_importance;
float shadow_importance;
glm::uint bounce_amount;
float travelled_distance;
} properties;
} properties;*/
};
enum class RayGenType
......
......@@ -25,6 +25,26 @@ namespace gl
glDebugMessageCallback(baseCallback, &debug_callback);
}
void debugMessageControl(DebugSource source, bool enabled)
{
glDebugMessageControl(static_cast<GLenum>(source), GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, enabled);
}
void debugMessageControl(DebugType type, bool enabled)
{
glDebugMessageControl(GL_DONT_CARE, static_cast<GLenum>(type), GL_DONT_CARE, 0, nullptr, enabled);
}
void debugMessageControl(DebugSeverity severity, bool enabled)
{
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, static_cast<GLenum>(severity), 0, nullptr, enabled);
}
void debugMessageControl(std::vector<unsigned> ids, bool enabled)
{
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, static_cast<int>(ids.size()), ids.data(), enabled);
}
void clear(ClearBufferBits bits)
{
glClear(static_cast<GLbitfield>(bits));
......
......@@ -911,6 +911,10 @@ namespace gl
using debug_callback_t = std::function<void(DebugSource source, DebugType type, unsigned id, DebugSeverity severity, std::string message)>;
void debugMessageCallback(debug_callback_t callback);
void debugMessageControl(DebugSource source, bool enabled);
void debugMessageControl(DebugType type, bool enabled);
void debugMessageControl(DebugSeverity severity, bool enabled);
void debugMessageControl(std::vector<unsigned> ids, bool enabled);
void clear(ClearBufferBits bits);
void clearColor(float r, float g, float b, float a);
......
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