Add factor to adjust brightness of the distribution.

This commit is contained in:
hal8174 2024-06-22 15:41:40 +02:00
parent d53b39f932
commit 5958307f13
2 changed files with 34 additions and 1 deletions

View file

@ -6,6 +6,8 @@
#include "random_sampler.hpp" #include "random_sampler.hpp"
#include "random_sampler_wrapper.hpp" #include "random_sampler_wrapper.hpp"
#include "tasking/taskschedulerinternal.h" #include "tasking/taskschedulerinternal.h"
#include <atomic>
#include <mutex>
#include <vector> #include <vector>
ApplicationIntegrator::ApplicationIntegrator(int argc, char **argv, ApplicationIntegrator::ApplicationIntegrator(int argc, char **argv,
@ -47,6 +49,11 @@ inline float luminance(Vec3fa v) {
void ApplicationIntegrator::resetRender() { void ApplicationIntegrator::resetRender() {
Application::resetRender(); Application::resetRender();
accepted.store(0, std::memory_order_relaxed);
luminance_count.store(0, std::memory_order_relaxed);
std::lock_guard<std::mutex> guard(large_step_global_luminance_mutex);
large_step_global_luminance = 0.0;
if (bMetropolis) { if (bMetropolis) {
data.film.count = false; data.film.count = false;
} else { } else {
@ -174,15 +181,19 @@ void ApplicationIntegrator::mltRender(int *pixels, int width, int height,
// d = Distribution1D(float* bis_values, num_bins) // d = Distribution1D(float* bis_values, num_bins)
// float integral = d.funcInt; // float integral = d.funcInt;
// int index_of_the_sampled_bin = d.SampleDiscrete(rng.get1D()); // int index_of_the_sampled_bin = d.SampleDiscrete(rng.get1D());
parallel_for(size_t(0), size_t(num_chains), [&](const range<size_t> &range) { parallel_for(size_t(0), size_t(num_chains), [&](const range<size_t> &range) {
const int threadIndex = (int)TaskScheduler::threadIndex(); const int threadIndex = (int)TaskScheduler::threadIndex();
float large_luminance_outer_sum = 0.0;
for (size_t i = range.begin(); i < range.end(); i++) { for (size_t i = range.begin(); i < range.end(); i++) {
MLTRandomSampler sampler(small_step_size, large_step_probability); MLTRandomSampler sampler(small_step_size, large_step_probability);
sampler.init(data.frame_count * num_chains + i); sampler.init(data.frame_count * num_chains + i);
float last_l = 0.0; float last_l = 0.0;
float large_luminance_sum = 0.0;
for (size_t j = 0; j < chain_lengths; j++) { for (size_t j = 0; j < chain_lengths; j++) {
sampler.new_ray(RandomSampler_get1D(sampler.sampler) < large_step_probability); sampler.new_ray(RandomSampler_get1D(sampler.sampler) < large_step_probability);
@ -196,15 +207,32 @@ void ApplicationIntegrator::mltRender(int *pixels, int width, int height,
Vec3f f = renderPixel(x, y, camera, g_stats[threadIndex], sampler); Vec3f f = renderPixel(x, y, camera, g_stats[threadIndex], sampler);
float l = luminance(f); float l = luminance(f);
if (sampler.is_large_step()) {
large_luminance_sum += l;
luminance_count.fetch_add(1, std::memory_order_relaxed);
}
if ((last_l == 0.0 && l > 0.0) || (last_l > 0.0 && RandomSampler_get1D(sampler.sampler) < l / last_l)) { if ((last_l == 0.0 && l > 0.0) || (last_l > 0.0 && RandomSampler_get1D(sampler.sampler) < l / last_l)) {
data.film.addSplat(x_pixel, y_pixel, f / l); data.film.addSplat(x_pixel, y_pixel, f / l);
sampler.accept(); sampler.accept();
accepted.fetch_add(1, std::memory_order_relaxed);
} }
} }
large_luminance_outer_sum += large_luminance_sum;
} }
std::lock_guard<std::mutex> guard(large_step_global_luminance_mutex);
large_step_global_luminance += large_luminance_outer_sum;
}); });
std::lock_guard<std::mutex> guard(large_step_global_luminance_mutex);
data.film.scalar = (float)(width * height) * large_step_global_luminance / (accepted.load(std::memory_order_relaxed) * luminance_count.load(std::memory_order_relaxed));
printf("%f\n", data.film.scalar);
} }
void ApplicationIntegrator::mcRender(int *pixels, int width, int height, void ApplicationIntegrator::mcRender(int *pixels, int width, int height,
@ -218,6 +246,7 @@ void ApplicationIntegrator::mcRender(int *pixels, int width, int height,
renderTile((int)i, threadIndex, pixels, width, height, time, renderTile((int)i, threadIndex, pixels, width, height, time,
camera, numTilesX, numTilesY); camera, numTilesX, numTilesY);
}); });
} }
/* renders a single screen tile */ /* renders a single screen tile */

View file

@ -29,6 +29,10 @@ protected:
const unsigned int height, const float time, const ISPCCamera& camera, const int numTilesX, const unsigned int height, const float time, const ISPCCamera& camera, const int numTilesX,
const int numTilesY); const int numTilesY);
std::atomic<size_t> accepted = 0;
std::atomic<size_t> luminance_count = 0;
float large_step_global_luminance = 0.0;
std::mutex large_step_global_luminance_mutex;
int num_chains = 100; int num_chains = 100;
int chain_lengths = 100000; int chain_lengths = 100000;
float small_step_size = 0.01; float small_step_size = 0.01;