Commit 4f275612 authored by Johannes Braun's avatar Johannes Braun
Browse files

Replaced packed uint32 for effects by an ivec4

parent df9f43a3
......@@ -31,7 +31,7 @@ uniform struct
uint current_sample;
float clamp_direct;
float clamp_indirect;
uint bounce_thresholds;
ivec4 bounce_thresholds;
} u_render_config;
//Global properties for all mesh-local Line Spaces.
......@@ -39,8 +39,8 @@ uniform struct
{
float accuracy_quality;
float shadow_quality;
uint bounce_thresholds;
float distance_threshold;
ivec4 bounce_thresholds;
} u_linespace_properties;
struct LightData
......@@ -64,24 +64,13 @@ struct Bounce
//Threshold properties
float path_distance;
float accuracy_importance;
float shadow_importance;
float pdf_accumulation;
uint bounce_amount;
ivec4 bounce_amount;
int count;
int bsdf_id;
};
uint getPart(uint base, uint index)
{
return (base >> (8 * index)) & 0xFF;
}
void addToPart(inout uint base, uint index, uint amount)
{
base += (amount & 0xFF) << (8 * index);
}
//////////////////////////////////////////////////////////////////////////
////
//// Utility Functions
......@@ -100,8 +89,15 @@ LightData sampleLight(const in Light light, const in Vertex vertex, const in vec
bool shouldUseBvh(const in Bounce bounce)
{
return (bounce.shadow_importance > (1-u_linespace_properties.shadow_quality))
&& (bounce.bounce_amount.getPart(bounce.bsdf_id) < u_linespace_properties.bounce_thresholds.getPart(bounce.bsdf_id))
return (bounce.pdf_accumulation > (1-u_linespace_properties.accuracy_quality))
&& (bounce.bounce_amount[bounce.bsdf_id] < u_linespace_properties.bounce_thresholds[bounce.bsdf_id])
&& 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])
&& bounce.path_distance < u_linespace_properties.distance_threshold;
}
......@@ -124,30 +120,37 @@ bool shade(inout Bounce bounce)
}
//Get Hit data
Mesh mesh = meshes_data[bounce.hit.mesh];
Vertex vertex = mesh.getVertex(bounce.hit);
Material material = materials_data[mesh.material];
bounce.path_distance += distance(bounce.ray.origin, vertex.position.xyz);
const Mesh mesh = meshes_data[bounce.hit.mesh];
const Vertex vertex = mesh.getVertex(bounce.hit);
const Material material = materials_data[mesh.material];
// generate a random sample on each bounce so there won't be any notifiable patterns
const vec2 base_random = vec2(bounce.ray.px, bounce.ray.py) - ivec2(floor(bounce.ray.px), floor(bounce.ray.py));
const ivec2 img_size = u_render_target.imageSize();
const int img_dimen = img_size.x * img_size.y;
vec2 random_sample = rand2D((random_seed + int(gl_GlobalInvocationID.y * uint(img_size.x) + gl_GlobalInvocationID.x))%img_dimen, img_size.x * img_size.y);
const vec2 random_sample = rand2D((random_seed + int(gl_GlobalInvocationID.y * uint(img_size.x) + gl_GlobalInvocationID.x))%img_dimen, img_size.x * img_size.y);
BSDFResult bsdf_result = material.computeBSDF(random_sample, vertex, bounce.ray);
bounce.path_distance += distance(bounce.ray.origin, vertex.position.xyz);
//int someID = img_dimen % 2;//clamp(int(bsdf_result.bsdf_id), 0, 1);
bounce.bounce_amount[bsdf_result.bsdf_id] += 1;
bounce.bsdf_id = int(bsdf_result.bsdf_id);
bounce.bounce_amount.addToPart(bsdf_result.bsdf_id, 1);
if(bounce.bounce_amount.getPart(bsdf_result.bsdf_id) > u_render_config.bounce_thresholds.getPart(bsdf_result.bsdf_id))
bounce.pdf_accumulation *= bsdf_result.probability_density;
bounce.ray = bsdf_result.generated_ray;
if(bsdf_result.bsdf_id == eEmit)
{
bounce.color += clamp(bounce.radiance * bsdf_result.radiance, 0, clamp_color_max);
return bounce.hit.invalidate();
}
if(bsdf_result.bsdf_id == eEmit)
if(bounce.bounce_amount[bsdf_result.bsdf_id] > u_render_config.bounce_thresholds[bsdf_result.bsdf_id]
|| bsdf_result.radiance == vec3(0)
|| bounce.pdf_accumulation <= 1e-5f)
{
bounce.color += clamp(bounce.radiance * bsdf_result.radiance, 0, clamp_color_max);
return bounce.hit.invalidate();
}
......@@ -157,22 +160,13 @@ 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, shouldUseBvh(bounce)))
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);
bounce.color += clamp(lighting, 0, clamp_color_max);
}
}
//Quit if bounces won't be of any use due to low contributions
if (bsdf_result.radiance == vec3(0) || bsdf_result.probability_density <= 1e-5f)
{
return bounce.hit.invalidate();
}
bounce.accuracy_importance *= bsdf_result.probability_density;
bounce.shadow_importance *= bsdf_result.probability_density;
bounce.ray = bsdf_result.generated_ray;
bounce.radiance *= clamp(bsdf_result.radiance * angle(bsdf_result.generated_ray, vertex) / bsdf_result.probability_density, 0, 1);
return true;
}
......@@ -201,9 +195,8 @@ void main()
//Threshold properties
bounce.path_distance = 0;
bounce.accuracy_importance = 1.f;
bounce.shadow_importance = 1.f;
bounce.bounce_amount = 0;
bounce.pdf_accumulation = 1.f;
bounce.bounce_amount = ivec4(0);
bounce.color = vec3(0);
bounce.radiance = vec3(1);
......
......@@ -20,7 +20,7 @@ namespace glare::raytrace
Pathtracer::Pathtracer(std::shared_ptr<SceneCollector> collector, RayGenType ray_generator)
{
Log_Hint << "Pathtracing Shader will now be compiled. This might take a couple of seconds.";
if(!m_render_shader) m_render_shader = std::make_unique<core::Program>(files::shader("/pathtracer/pathtracer.comp"));
if (!m_render_shader) m_render_shader = std::make_unique<core::Program>(files::shader("/pathtracer/pathtracer.comp"));
m_ray_generator = ray_generator;
core::Context::current().messages()->addObserver(this, tags::scene);
......@@ -50,7 +50,7 @@ namespace glare::raytrace
m_bounce_thresholds[Effect::eReflection] = static_cast<uint8_t>(bounces_child.find_child_by_attribute("item", "name", "reflection").attribute("value").as_uint(4));
m_bounce_thresholds[Effect::eTransmission] = static_cast<uint8_t>(bounces_child.find_child_by_attribute("item", "name", "transmission").attribute("value").as_uint(4));
m_bounce_thresholds[Effect::eEmission] = static_cast<uint8_t>(bounces_child.find_child_by_attribute("item", "name", "emission").attribute("value").as_uint(8));
//ls thresholds
const auto ls_thresholds_child = pt_child.find_child_by_attribute("group", "name", "ls-thresholds");
m_linespace_bounces[Effect::eDiffusion] = static_cast<uint8_t>(ls_thresholds_child.find_child_by_attribute("item", "name", "diffusion").attribute("value").as_uint(2));
......@@ -75,12 +75,13 @@ namespace glare::raytrace
m_render_shader->uniform("u_render_config.num_bounces", m_bounce_count);
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", uint32_t(m_bounce_thresholds));
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]));
m_render_shader->uniform("u_linespace_properties.accuracy_quality", m_linespace_accuracy);
m_render_shader->uniform("u_linespace_properties.shadow_quality", m_linespace_shadow);
m_render_shader->uniform("u_linespace_properties.distance_threshold", m_linespace_distance);
m_render_shader->uniform("u_linespace_properties.bounce_thresholds", uint32_t(m_linespace_bounces));
m_render_shader->bindSubroutine(gl::ShaderType::eCompute, "u_light_sample[0]", "samplePoint");
m_render_shader->bindSubroutine(gl::ShaderType::eCompute, "u_light_sample[1]", "sampleSpot");
......@@ -159,19 +160,24 @@ namespace glare::raytrace
}
}
Pathtracer::EffectArray::operator unsigned()
int& Pathtracer::EffectArray::operator[](Effect effect)
{
return *reinterpret_cast<uint32_t*>(m_data.data());
return m_data[static_cast<size_t>(effect)];
}
uint8_t& Pathtracer::EffectArray::operator[](Effect effect)
const int& Pathtracer::EffectArray::operator[](Effect effect) const
{
return m_data[static_cast<size_t>(effect)];
}
const uint8_t& Pathtracer::EffectArray::operator[](Effect effect) const
int& Pathtracer::EffectArray::operator[](int effect)
{
return m_data[static_cast<size_t>(effect)];
return m_data[effect];
}
const int& Pathtracer::EffectArray::operator[](int effect) const
{
return m_data[effect];
}
Pathtracer::LogTime::LogTime(std::string label, bool enabled)
......@@ -183,7 +189,7 @@ namespace glare::raytrace
Pathtracer::LogTime::~LogTime()
{
if (m_enabled)
Log_Hint << "Log Time for label=\"" << m_label << "\": " << core::ClockGL::instance().end() << " ns";
Log_Hint << "Log Time for label=\"" << m_label << "\": " << core::ClockGL::instance().end() << " ns";
}
void Pathtracer::initialize(std::shared_ptr<SceneCollector> collector)
......
......@@ -20,9 +20,9 @@ namespace glare::raytrace
enum class Effect
{
eDiffusion = 0,
eReflection = 1,
eTransmission = 2,
eEmission = 3
eReflection,
eTransmission,
eEmission
};
class Pathtracer : public msg::Observer
......@@ -93,12 +93,13 @@ namespace glare::raytrace
struct EffectArray
{
template<typename... Args> EffectArray(Args&&... args);
explicit operator uint32_t();
uint8_t& operator[](Effect effect);
const uint8_t& operator[](Effect effect) const;
int& operator[](Effect effect);
const int& operator[](Effect effect) const;
int& operator[](int effect);
const int& operator[](int effect) const;
private:
std::array<uint8_t, 4> m_data;
std::array<int, 4> m_data;
};
unsigned m_bounce_count = 16;
......
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