Commit 2e805556 authored by MURRAY David's avatar MURRAY David

added log for variance output

parent ae654756
Pipeline #106288 skipped with stage
......@@ -3,8 +3,9 @@
#extension GL_ARB_shader_storage_buffer_object : require
uniform sampler2D display_buffer;
uniform sampler2D position_buffer;
uniform sampler2D output_buffer;
uniform sampler2DArray alpha_buffer;
// uniform sampler2D rad_sqr_buffer;
uniform sampler2D variance_buffer;
uniform vec2 viewport;
......@@ -16,9 +17,10 @@ uniform ivec2 picked_pos;
uniform int frame;
uniform int log_variance;
#define RADIANCE 0
#define ALPHA 1
#define VARIANCE 2
#define RADIANCE_RAW 0
#define RADIANCE_ADJUSTED 1
#define ALPHA 2
#define VARIANCE 3
layout(std430, binding=0) buffer variance_picked
{
......@@ -32,7 +34,56 @@ float toRadiance(vec3 rgb)
return 0.2973769 * rgb.r + 0.6273491 * rgb.g + 0.0752741 * rgb.b;
}
float getRadiance(ivec2 centralPos)
vec3 getAvgVar(ivec2 centralPos)
{
vec3 sum = vec3(0.0);//texelFetch(variance_buffer, centralPos, 0).r;
ivec2 pos = centralPos;
float weight[] = {
0.00000067, 0.00002292, 0.00019117, 0.00038771, 0.00019117, 0.00002292, 0.00000067,
0.00002292, 0.00078633, 0.00655965, 0.01330373, 0.00655965, 0.00078633, 0.00002292,
0.00019117, 0.00655965, 0.05472157, 0.11098164, 0.05472157, 0.00655965, 0.00019117,
0.00038771, 0.01330373, 0.11098164, 0.22508352, 0.11098164, 0.01330373, 0.00038771,
0.00019117, 0.00655965, 0.05472157, 0.11098164, 0.05472157, 0.00655965, 0.00019117,
0.00002292, 0.00078633, 0.00655965, 0.01330373, 0.00655965, 0.00078633, 0.00002292,
0.00000067, 0.00002292, 0.00019117, 0.00038771, 0.00019117, 0.00002292, 0.00000067
};
int x[] = {
-3, -2, -1, 0, 1, 2, 3,
-3, -2, -1, 0, 1, 2, 3,
-3, -2, -1, 0, 1, 2, 3,
-3, -2, -1, 0, 1, 2, 3,
-3, -2, -1, 0, 1, 2, 3,
-3, -2, -1, 0, 1, 2, 3,
-3, -2, -1, 0, 1, 2, 3
};
int y[] = {
-3, -3, -3, -3, -3, -3, -3,
-2, -2, -2, -2, -2, -2, -2,
-1, -1, -1, -1, -1, -1, -1,
0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3
};
// for(int i = 0; i < 27; ++i)
// {
// float tmp_sqr = toRadiance(texelFetch(rad_sqr_buffer, centralPos + ivec2(x[i], y[i]), 0).rgb);
// float tmp_mean = toRadiance(texelFetch(output_buffer, centralPos + ivec2(x[i], y[i]), 0).rgb);
// float var = tmp_sqr - tmp_mean*tmp_mean;
// sum += weight[i] * var;
// }
for(int i = 0; i < 27; ++i)
{
sum += weight[i] * texelFetch(variance_buffer, centralPos + ivec2(x[i], y[i]), 0).rgb;
}
return sum;
}
float getAvgVarGray(ivec2 centralPos)
{
float sum = 0.0;//texelFetch(variance_buffer, centralPos, 0).r;
ivec2 pos = centralPos;
......@@ -66,8 +117,17 @@ float getRadiance(ivec2 centralPos)
3, 3, 3, 3, 3, 3, 3
};
// for(int i = 0; i < 27; ++i)
// {
// float tmp_sqr = toRadiance(texelFetch(rad_sqr_buffer, centralPos + ivec2(x[i], y[i]), 0).rgb);
// float tmp_mean = toRadiance(texelFetch(output_buffer, centralPos + ivec2(x[i], y[i]), 0).rgb);
// float var = tmp_sqr - tmp_mean*tmp_mean;
// sum += weight[i] * var;
// }
for(int i = 0; i < 27; ++i)
{
sum += weight[i] * toRadiance(texelFetch(variance_buffer, centralPos + ivec2(x[i], y[i]), 0).rgb);
}
return sum;
}
......@@ -75,12 +135,14 @@ float getRadiance(ivec2 centralPos)
void
main(void)
{
vec2 screen_coord = gl_FragCoord.xy/viewport;
vec4 tex = texture(display_buffer, screen_coord);
vec4 ret_color = tex;
vec2 screen_coord = gl_FragCoord.xy/viewport;
vec4 ret_color = vec4(0.0, 0.0, 0.0, 1.0);
if(visu_mode == ALPHA)
if(visu_mode == RADIANCE_RAW)
ret_color = texture(output_buffer, screen_coord);
else if(visu_mode == RADIANCE_ADJUSTED)
ret_color = texture(display_buffer, screen_coord);
else if(visu_mode == ALPHA)
{
// vec4 alpha_tex = texture(alpha_buffer, screen_coord);
......@@ -101,13 +163,18 @@ main(void)
}
else if(visu_mode == VARIANCE)
{
vec3 radMeanSqr = ret_color.rgb * ret_color.rgb;
vec3 radSqrMean = texture(variance_buffer, screen_coord).rgb;
ret_color.rgb = radSqrMean - radMeanSqr;
// vec3 radMeanSqr = ret_color.rgb * ret_color.rgb;
// vec3 radSqrMean = texture(rad_sqr_buffer, screen_coord).rgb;
// ret_color.rgb = radSqrMean - radMeanSqr;
ret_color.rgb = texture(variance_buffer, screen_coord).rgb;
// ret_color.rgb = getAvgVar(ivec2(gl_FragCoord.xy));
// ret_color = texture(variance_buffer, screen_coord);
// ret_color.rgb *= 100.0;
}
if(display_picked == 1)
{
vec2 screen_pick = picked_pos / viewport;
......@@ -118,7 +185,7 @@ main(void)
if(log_variance == 1)
if(is_same_x && is_same_y)
data[frame] = getRadiance(picked_pos);
data[frame] = getAvgVarGray(picked_pos);
}
out_color = ret_color;
......
......@@ -69,7 +69,7 @@ void ImGUI::run()
colors[ImGuiCol_Button] = ImVec4(0.45f*0.8, 0.55f*0.8, 0.60f*0.8, 1);
colors[ImGuiCol_ButtonHovered] = ImVec4(0.45f*0.6, 0.55f*0.6, 0.60f*0.6, 1);
colors[ImGuiCol_ButtonActive] = ImVec4(0.45f*0.9, 0.55f*0.9, 0.60f*0.9, 1);
colors[ImGuiCol_SliderGrab] = ImVec4(0.45f*0.8, 0.55f*0.8, 0.60f*0.8, 1);
colors[ImGuiCol_SliderGrab] = ImVec4(0.45f*0.7, 0.55f*0.7, 0.60f*0.7, 1);
colors[ImGuiCol_SliderGrabActive] = ImVec4(0.45f*0.6, 0.55f*0.6, 0.60f*0.6, 1);
colors[ImGuiCol_FrameBg] = ImVec4(0.45f*0.6, 0.55f*0.6, 0.60f*0.6, 1);
colors[ImGuiCol_PopupBg] = ImVec4(0.45f*0.6, 0.55f*0.6, 0.60f*0.6, 1);
......
......@@ -6,6 +6,9 @@
*
**/
#include "util/string_parsing.hpp"
#include "mrf_ImGui_shader.h"
#include "util/precision_timer.hpp"
using mrf::uint;
......@@ -18,7 +21,7 @@ using optix::float3;
ImGUI_shader::ImGUI_shader(mrf::gui::fb::Loger::LEVEL const & logging_level) :
ImGUI(logging_level), _fbo_ready(false), _quad_ready(false), _ssbo_ready(false)
{
_requestedMode = VisuMode::RADIANCE;
_requestedMode = VisuMode::RADIANCE_ADJUSTED;
_alpha_layer = 0;
_use_sample_balancing = true;
_reset_on_iteration = true;
......@@ -62,17 +65,21 @@ void ImGUI_shader::customUI(ImGUI::UI_MODE mode)
ImGui::SetNextTreeNodeOpen(true);
if (ImGui::CollapsingHeader("Variance Reduction"))
{
const char* items[] = { "Radiance", "Alpha", "Variance" };
ImGui::SetNextItemWidth(_ui_panel_width / 4.f);
ImGui::Combo("Visualization", &_requestedMode, items, IM_ARRAYSIZE(items));
const char* items[] = { "Radiance (adjusted)", "Radiance (raw)", "Alpha balance", "Variance" };
ImGui::SetNextItemWidth(_ui_panel_width / 2.f);
ImGui::Combo("", &_requestedMode, items, IM_ARRAYSIZE(items));
ImGui::SameLine();
if (ImGui::Button("Save buffer"))
{
if (_requestedMode == VisuMode::RADIANCE)
if (_requestedMode == VisuMode::RADIANCE_RAW)
_renderer->saveImage(_image_output_file.substr(0, _image_output_file.find(".exr")) + "_radiance_raw.exr", "output_buffer");
else if (_requestedMode == VisuMode::RADIANCE_ADJUSTED)
_renderer->saveImage(_image_output_file.substr(0, _image_output_file.find(".exr")) + "_radiance.exr", "display_buffer");
else
else if (_requestedMode == VisuMode::ALPHA)
_renderer->saveImage(_image_output_file.substr(0, _image_output_file.find(".exr")) + "_alpha.png", "alpha_buffer");
else if (_requestedMode == VisuMode::VARIANCE)
_renderer->saveImage(_image_output_file.substr(0, _image_output_file.find(".exr")) + "_variance.exr", "variance_buffer");
}
if (ImGui::Checkbox("Sample balancing", &_use_sample_balancing))
......@@ -144,7 +151,7 @@ bool ImGUI_shader::doRendering()
{
bool ret = GUI::doRendering();
_textureList[TextureName::POSITION].updateTextureFromBuffer(_renderer->getPositionBuffer());
//_textureList[TextureName::POSITION].updateTextureFromBuffer(_renderer->getPositionBuffer());
_textureList[TextureName::ALPHA].updateTextureFromBuffer(_renderer->getBuffer("alpha_buffer"));
_textureList[TextureName::VARIANCE].updateTextureFromBuffer(_renderer->getBuffer("variance_buffer"));
......@@ -179,11 +186,35 @@ void ImGUI_shader::display()
//Our rendering have been done, now ImGui will execute its drawing command (must be done in this order to ensure correct behavior of popups).
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
if (_log_variance && _frame == _variance_samples-1)
if (_log_variance)
{
saveVarianceAtPicked();
_log_variance = false;
_log_time = false;
if (_log_time)
_time[_frame] = _elapsed_time;
if (_frame == _variance_samples - 1)
{
saveVarianceAtPicked();
std::string filename, dir, config;
mrf::util::StringParsing::getDirectory(_image_output_file.c_str(), dir);
config = "samples" + std::to_string(_variance_samples);
if (_use_sample_balancing)
{
config = config + "_start" + std::to_string(_renderer->getStartAlpha());
config = config + "_" + std::to_string(_renderer->getSamplesAlpha()) + "x" + std::to_string(_renderer->getAlphaPasses());
if (_reset_on_iteration)
config = config + "_withReset";
}
else
config = config + "_noBalance";
filename = dir + "variance_" + config;
_renderer->saveImage(filename + ".png", "variance_buffer");
filename = dir + "radiance_" + config;
_renderer->saveImage(filename + ".exr", "output_buffer");
_log_variance = false;
_log_time = false;
}
}
_current_mode = UI_MODE::DISPLAY;
......@@ -198,7 +229,7 @@ void ImGUI_shader::render(Shader* prg, bool use_tex)
if (use_tex)
{
_textureList[TextureName::COLOR].activate(prg);
_textureList[TextureName::POSITION].activate(prg);
_textureList[TextureName::OUTPUT].activate(prg);
_textureList[TextureName::ALPHA].activate(prg);
_textureList[TextureName::VARIANCE].activate(prg);
}
......@@ -213,7 +244,7 @@ void ImGUI_shader::render(Shader* prg, bool use_tex)
if (use_tex)
{
_textureList[TextureName::COLOR].deactivate();
_textureList[TextureName::POSITION].deactivate();
_textureList[TextureName::OUTPUT].deactivate();
_textureList[TextureName::ALPHA].deactivate();
_textureList[TextureName::VARIANCE].deactivate();
}
......@@ -360,12 +391,12 @@ void ImGUI_shader::activateVarianceLog()
void ImGUI_shader::stopVarianceLog()
{
delete _variance;
delete _time;
_log_variance = false;
_log_time = false;
delete _variance;
delete _time;
initSSBO();
//initSSBO();
}
void ImGUI_shader::addVarianceItem(int iteration, float log_time)
......@@ -432,7 +463,11 @@ void ImGUI_shader::saveVarianceAtPicked()
std::ofstream file;
std::string filename;
filename = _image_output_file.substr(0, _image_output_file.size() - 4) + "variance.txt";
filename = _image_output_file.substr(0, _image_output_file.size() - 4) + "_variance_" + std::to_string(_variance_samples);
if(_use_sample_balancing)
filename = filename + "_start" + std::to_string(_renderer->getStartAlpha()) + ".txt";
else
filename = filename + "_noBalance.txt";
file.open(filename, std::ios::out);
for (int i = 0; i < _variance_samples; ++i)
......@@ -484,11 +519,12 @@ void ImGUI_shader::variancePopUp()
if (launch)
{
activateVarianceLog();
_render_on_pause = false;
_camera_changed = true;
}
else if (cancel)
_variance_samples = tmp;
_render_on_pause = false;
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
......
......@@ -63,10 +63,10 @@ private:
void variancePopUp();
enum VisuMode {
RADIANCE,
RADIANCE_ADJUSTED,
RADIANCE_RAW,
ALPHA,
BETA,
PATH
VARIANCE
};
int _alpha_layer;
......
......@@ -49,7 +49,7 @@ void GUI::initializeUI(mrf::rendering::OptixRenderer* renderer)
createContext(_width, _height);
_textureList[TextureName::COLOR] = Texture(_renderer->getDisplayBuffer(), "display_buffer");
_textureList[TextureName::OUTPUT] = Texture(_renderer->getOutputBuffer(), "display_buffer");
_textureList[TextureName::OUTPUT] = Texture(_renderer->getOutputBuffer(), "output_buffer");
}
void GUI::run()
......@@ -174,7 +174,11 @@ bool GUI::doRendering()
#ifndef MRF_RENDERING_MODE_SPECTRAL
//if (_renderer->getNumFrame() < 10000)
{
mrf::util::PrecisionTimer timer;
timer.start();
_renderer->_context->launch(0, _width, _height);
timer.stop();
_elapsed_time = timer.elapsed();
_renderer->incrementNumFrame();
}
#else//SPECTRAL MODE
......
......@@ -103,6 +103,7 @@ protected:
unsigned int _displayTexture;
int _frame;
float _elapsed_time;
enum TextureName {
COLOR,
......
......@@ -80,6 +80,7 @@ rtDeclareVariable(float3, bad_color,, );
rtBuffer<Metrics, 3> metrics_buffer;
rtBuffer<float4, 3> alpha_buffer;
rtBuffer<float4, 2> variance_buffer;
rtBuffer<float4, 2> rad_sqr_buffer;
rtDeclareVariable(int, alpha_samples,, );
rtDeclareVariable(int, alpha_passes,, );
rtDeclareVariable(float, start_alpha,, );
......@@ -136,7 +137,7 @@ RT_PROGRAM void pathtrace_camera()
// metrics_buffer[make_uint3(launch_index, i)] = tmp;
}
if(frame_number == 1)
variance_buffer[launch_index] = make_float4(0.f);
rad_sqr_buffer[launch_index] = make_float4(0.f);
float3 impactPos = make_float3(0.f, 0.f, 0.f);
for (; samples_per_pixel>0; --samples_per_pixel)
......@@ -238,17 +239,17 @@ RT_PROGRAM void pathtrace_camera()
seed = prd.seed;
Metrics all_metrics = metrics_buffer[make_uint3(launch_index, 0)];
// if(all_metrics.nb_samples < alpha_samples * alpha_passes)
if(frame_number < alpha_samples * alpha_passes)
if(all_metrics.nb_samples < alpha_samples * alpha_passes)
// if(frame_number < alpha_samples * alpha_passes)
{
int path_depth = 0;
if(!all_metrics.is_bg)
{
while(path_depth <= prd.depth && path_depth < max_path_length)
{
all_metrics = metrics_buffer[make_uint3(launch_index, path_depth)];
{
all_metrics = metrics_buffer[make_uint3(launch_index, path_depth)];
int nb_samples = all_metrics.nb_samples + 1;
metrics_buffer[make_uint3(launch_index, path_depth)].nb_samples = nb_samples;
// float3 alpha_comp1 = all_metrics.alpha_comp_num;
float toRad1 = toRadiance(all_metrics.alpha_comp_num);//(alpha_comp1.x*0.265f + 0.67f*alpha_comp1.y + 0.065f*alpha_comp1.z);
......@@ -291,8 +292,7 @@ RT_PROGRAM void pathtrace_camera()
}
++path_depth;
}
// else
// alpha_buffer[make_uint3(launch_index, path_depth)] = make_float4(0.f, 0.f, 0.f, -1.f);
}
}
}
......@@ -348,18 +348,20 @@ RT_PROGRAM void pathtrace_camera()
float3 xiSqr = pixel_color * pixel_color;
float3 xiSqrSum = xiSqr; //init to xiSqr, updated to sum if needed;
if (frame_number > 1)
int nb_samples = frame_number;//metrics_buffer[make_uint3(launch_index, 0)].nb_samples;
if (nb_samples > 1)
{
float a = 1.0f / (float)frame_number;
float a = 1.0f / (float)nb_samples;
float3 old_color = make_float3(output_buffer[launch_index]);
pixel_color = lerp( old_color, pixel_color, a ); //pixel_color is now equivalent to radiance_mean
xiSqrSum = make_float3(variance_buffer[launch_index]);// get sum of sqr xi up to n-1 (=mean of sqr)
xiSqrSum = make_float3(rad_sqr_buffer[launch_index]);// get sum of sqr xi up to n-1 (=mean of sqr)
xiSqrSum = lerp( xiSqrSum, xiSqr, a); //lerp to add the nth to the mean of sqr
}
output_buffer[launch_index] = make_float4(pixel_color,1.f);
variance_buffer[launch_index] = make_float4(xiSqrSum.x, xiSqrSum.y, xiSqrSum.z, 1.f);
rad_sqr_buffer[launch_index] = make_float4(xiSqrSum, 1.f);
variance_buffer[launch_index] = make_float4(xiSqrSum - pixel_color * pixel_color, 1.f);
//interactive rgb mode
#ifdef INTERACTIVE_MODE
......@@ -461,10 +463,17 @@ RT_PROGRAM void miss()
#endif
mult_color(temp_bg_color, current_prd.attenuation);
add_color(current_prd.result, temp_bg_color);
add_color(current_prd.result, temp_bg_color);
if(current_prd.depth == 0)
metrics_buffer[make_uint3(launch_index, 0)].is_bg = true;
metrics_buffer[make_uint3(launch_index, 0)].is_bg = true;
for(int i = 0; i < current_prd.depth; ++i)
{
uint3 index = make_uint3(launch_index, i);
metrics_buffer[index].alpha_comp_num = make_float3(0.f);
metrics_buffer[index].alpha_comp_den = make_float3(0.f);
}
current_prd.done = true;
}
......
......@@ -69,9 +69,9 @@ RT_PROGRAM void diffuseEmitter()
{
uint3 index = make_uint3(launch_index, i);
float3 factor;
factor.x = 1.f;//max(0.f,total_attenuation.x / metrics_buffer[index].attenuation.x);
factor.y = 1.f;//max(0.f,total_attenuation.y / metrics_buffer[index].attenuation.y);
factor.z = 1.f;//max(0.f,total_attenuation.z / metrics_buffer[index].attenuation.z);
factor.x = max(0.f,total_attenuation.x / metrics_buffer[index].attenuation.x);
factor.y = max(0.f,total_attenuation.y / metrics_buffer[index].attenuation.y);
factor.z = max(0.f,total_attenuation.z / metrics_buffer[index].attenuation.z);
metrics_buffer[index].alpha_comp_num *= factor*factor*emission_color*emission_color;
metrics_buffer[index].alpha_comp_den *= factor*factor*emission_color*emission_color;
}
......
......@@ -568,7 +568,7 @@ void OptixRenderer::createRenderContext(bool interactive_mode)
#endif
//do this afetr scene import
//do this after scene import
//to detect if we need envmap support or not
//_material_handler.createMaterials(_next_event_type,_cuda_compile_options);
......@@ -611,7 +611,7 @@ void OptixRenderer::createRenderTarget()
else
buffer = _context->createBuffer(RT_BUFFER_OUTPUT, RT_FORMAT_FLOAT, _render_width, _render_height);
#else
buffer = _context->createBuffer(RT_BUFFER_OUTPUT, RT_FORMAT_FLOAT4, _render_width, _render_height);
buffer = _context->createBuffer(RT_BUFFER_INPUT_OUTPUT, RT_FORMAT_FLOAT4, _render_width, _render_height);
#endif
_context["output_buffer"]->set(buffer);
......@@ -638,8 +638,11 @@ void OptixRenderer::createRenderTarget()
_alpha_buffer = _context->createBuffer(RT_BUFFER_INPUT_OUTPUT, RT_FORMAT_FLOAT4, _render_width, _render_height, (_max_path_length + 1));
_context["alpha_buffer"]->set(_alpha_buffer);
_variance_buffer = _context->createBuffer(RT_BUFFER_INPUT_OUTPUT, RT_FORMAT_FLOAT4, _render_width, _render_height);// , std::max(1, _max_path_length));
_variance_buffer = _context->createBuffer(RT_BUFFER_OUTPUT, RT_FORMAT_FLOAT4, _render_width, _render_height);// , std::max(1, _max_path_length));
_context["variance_buffer"]->set(_variance_buffer);
_rad_sqr_buffer = _context->createBuffer(RT_BUFFER_INPUT_OUTPUT, RT_FORMAT_FLOAT4, _render_width, _render_height);// , std::max(1, _max_path_length));
_context["rad_sqr_buffer"]->set(_rad_sqr_buffer);
}
void OptixRenderer::resizeRenderTarget(uint width, uint height)
......@@ -668,6 +671,7 @@ void OptixRenderer::resizeRenderTarget(uint width, uint height)
_context["metrics_buffer"]->getBuffer()->setSize(width, height, (_max_path_length + 1));
_context["alpha_buffer"]->getBuffer()->setSize(width, height, (_max_path_length + 1));
_context["rad_sqr_buffer"]->getBuffer()->setSize(width, height);
_context["variance_buffer"]->getBuffer()->setSize(width, height);
_context["cranley_patterson_buffer"]->getBuffer()->setSize(2*width*height);
......
......@@ -304,6 +304,7 @@ public:
bool _use_sample_balancing;
bool _reset_iteration;
optix::Buffer _alpha_buffer;
optix::Buffer _rad_sqr_buffer;
optix::Buffer _variance_buffer;
int _samples_alpha;
int _alpha_passes;
......
......@@ -76,6 +76,8 @@ inline void PrecisionTimer::reset()
inline float PrecisionTimer::elapsed()
{
if (_seconds_elapsed != 0)
return _seconds_elapsed;
auto temp = std::chrono::high_resolution_clock::now();
_seconds_elapsed = 1e-9 * std::chrono::duration_cast<std::chrono::nanoseconds>(temp - _start_time).count();
return static_cast<float>(_seconds_elapsed);
......
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