Per-thread RNGs and RNG bugfix

This commit is contained in:
David 2021-08-30 18:34:14 +02:00
commit 52806e4457
6 changed files with 70 additions and 39 deletions

View file

@ -2,6 +2,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <getopt.h>
#include <time.h>
#define RMT_ENABLED 0
@ -117,7 +118,7 @@ hittable_list<sphere> random_scene() {
}
template<typename T>
color ray_color(const ray& r, hittable_list<T>& world, int32_t depth)
color ray_color(const ray& r, hittable_list<T>& world, int32_t depth, int32_t thread_id)
{
rmt_ScopedCPUSample(Scatter, RMTSF_Aggregate | RMTSF_Recursive);
if (depth <= 0)
@ -131,11 +132,11 @@ color ray_color(const ray& r, hittable_list<T>& world, int32_t depth)
ray scattered;
color attenuation;
rmt_BeginCPUSample(Scatter, RMTSF_Aggregate);
bool visible = rec.mat_ptr->scatter(r, rec, attenuation, scattered);
bool visible = rec.mat_ptr->scatter(r, rec, attenuation, scattered, thread_id);
rmt_EndCPUSample();
if (visible)
{
return attenuation * ray_color(scattered, world, depth-1);
return attenuation * ray_color(scattered, world, depth-1, thread_id);
}
else
{
@ -163,6 +164,9 @@ float hit_sphere(const point3& center, float radius, const ray& r)
int32_t main(int argc, char *argv[])
{
/* Argument parsing */
int32_t c;
bool using_default_output = true;
@ -225,6 +229,30 @@ int32_t main(int argc, char *argv[])
//indicators::show_console_cursor(false);
// Get the number of logical CPUs
int32_t ncores = sysconf(_SC_NPROCESSORS_ONLN);
// Initialize and seed the random number generators
pcg_table = (pcg32_random_t *) malloc(sizeof(pcg32_random_t) * ncores);
for (int32_t i = 0; i < ncores; ++i)
{
struct timespec ts;
if (timespec_get(&ts, TIME_UTC))
{
// Use higher quality seed
uint64_t seed = (uint64_t)(ts.tv_nsec ^ ts.tv_sec);
pcg_table[i] = { seed, seed };
}
else
{
// Error, use default seed
pcg_table[i] = default_pcg;
}
}
// Image
aspect_ratio = 3.0 / 2.0;
image_width = 1200;
@ -257,11 +285,11 @@ int32_t main(int argc, char *argv[])
// Render
fprintf(output_file_handle, "P3\n%d %d\n255\n", image_width, image_height);
int32_t ncores = sysconf(_SC_NPROCESSORS_ONLN); // Get the number of logical CPUs
std::vector<pthread_t> threads;
std::vector<thread_args> args;
threads.reserve(ncores);
args.reserve(ncores);
args.reserve(ncores);
std::vector<indicators::BlockProgressBar*> bar_memory;
bar_memory.reserve(ncores);
@ -381,10 +409,10 @@ void *raytrace_lines(void *arg)
color pixel_color = color(0,0,0);
for (int32_t s = 0; s < samples_per_pixel; ++s)
{
float u = ((i + random_float()) / (image_width-1));
float v = ((j + random_float()) / (image_height-1));
ray r = global_camera->get_ray(u,v);
pixel_color += ray_color(r, world, max_depth);
float u = ((i + random_float(thread_id)) / (image_width-1));
float v = ((j + random_float(thread_id)) / (image_height-1));
ray r = global_camera->get_ray(u,v, thread_id);
pixel_color += ray_color(r, world, max_depth, thread_id);
}
int32_t index = j * image_width + i;
image[index] = pixel_color;