From 742ef283e413f1546f96513904623165e4a72e97 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 29 Aug 2021 16:17:13 +0200 Subject: [PATCH] Faster random implementation --- .gitignore | 3 +++ Makefile | 2 ++ hittable_list.hpp | 4 ++-- main.cpp | 18 ++++++++++++------ random.h | 15 +++++++++++++++ rtweekend.hpp | 9 ++++++++- timer.hpp | 8 +++++++- 7 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 random.h diff --git a/.gitignore b/.gitignore index 7f42aa0..80c9739 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ raytracer # Actual output image image.ppm + +# Profiler data +perf.* diff --git a/Makefile b/Makefile index 5f5fdf7..6ff5af9 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,8 @@ FLAGS=-Ofast -march=native -g -Wall -Wextra -Wpedantic raytracer: camera.hpp color.hpp hittable.hpp hittable_list.hpp main.cpp material.hpp ray.hpp rtweekend.hpp sphere.hpp vec3.hpp $(INCLUDE)/Remotery.c $(INCLUDE)/Remotery.h g++ $(FLAGS) -I$(INCLUDE) $(LIBS) main.cpp -o raytracer +make debug: + image: raytracer @./raytracer > image.ppm @if [ $$TERM = "xterm-kitty" ]; then\ diff --git a/hittable_list.hpp b/hittable_list.hpp index a9c2dcd..9c3366e 100644 --- a/hittable_list.hpp +++ b/hittable_list.hpp @@ -34,8 +34,8 @@ bool hittable_list::hit(const ray& r, float t_min, float t_max, hit_record& r bool hit_anything = false; float closest_so_far = t_max; - - for (uint32_t i = 0; i < objects.size(); ++i) + uint32_t s = objects.size(); + for (uint32_t i = 0; i < s; ++i) { T *object = &objects[i]; if (object->hit(r, t_min, closest_so_far, temp_rec)) diff --git a/main.cpp b/main.cpp index 8d5c035..4bb6f64 100644 --- a/main.cpp +++ b/main.cpp @@ -2,9 +2,7 @@ #include #include - -// Disable profiling -#define RMT_ENABLED 1 +#define RMT_ENABLED 0 // Lib includes #pragma GCC diagnostic push @@ -21,9 +19,15 @@ #include "sphere.hpp" #include "camera.hpp" +#ifdef DEBUG +#define print_timers() print_timers_() +#else +#define print_timers() +#endif + color ray_color(const ray& r, const hittable& world, int32_t depth); float hit_sphere(const point3& center, float radius, const ray& r); -void print_timers(); + hittable_list random_scene(); hittable_list random_scene() { @@ -188,10 +192,10 @@ int32_t main() rmt_DestroyGlobalInstance(rmt); } +#ifdef DEBUG debug_record debug_record_array[__COUNTER__]; - -void print_timers() +void print_timers_() { for (uint32_t i = 0; i < sizeof(debug_record_array) / sizeof(debug_record_array[0]); @@ -211,3 +215,5 @@ void print_timers() (double)record->cycles / CLOCKS_PER_SEC); } } + +#endif diff --git a/random.h b/random.h new file mode 100644 index 0000000..b5f66e7 --- /dev/null +++ b/random.h @@ -0,0 +1,15 @@ +// *Really* minimal PCG32 code / (c) 2014 M.E. O'Neill / pcg-random.org +// Licensed under Apache License 2.0 (NO WARRANTY, etc. see website) + +typedef struct { uint64_t state; uint64_t inc; } pcg32_random_t; + +uint32_t pcg32_random_r(pcg32_random_t* rng) +{ + uint64_t oldstate = rng->state; + // Advance internal state + rng->state = oldstate * 6364136223846793005ULL + (rng->inc|1); + // Calculate output function (XSH RR), uses old state for max ILP + uint32_t xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u; + uint32_t rot = oldstate >> 59u; + return (xorshifted >> rot) | (xorshifted << ((-rot) & 31)); +} diff --git a/rtweekend.hpp b/rtweekend.hpp index 410329b..13a9781 100644 --- a/rtweekend.hpp +++ b/rtweekend.hpp @@ -4,9 +4,16 @@ #include #include #include "timer.hpp" +#include "random.h" + +#define FAST_RANDOM 1 +#ifdef FAST_RANDOM +pcg32_random_t pcg = { 0x853c49e6748fea9bULL, 0xda3e39cb94b95bdbULL }; +#define rand() pcg32_random_r(&pcg) +#endif /* Utility functions */ -float degrees_to_radians(float d) +inline float degrees_to_radians(float d) { return d * M_PI / 180; } diff --git a/timer.hpp b/timer.hpp index ed537cb..6a2c689 100644 --- a/timer.hpp +++ b/timer.hpp @@ -1,11 +1,15 @@ #include #include "rtweekend.hpp" - +#ifdef DEBUG #define TIMED_BLOCK__(number, ...) timed_block timed_block_##Number(__COUNTER__, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__) #define TIMED_BLOCK_(number, ...) TIMED_BLOCK__(number, ##__VA_ARGS__) #define TIMED_BLOCK(...) TIMED_BLOCK_(__LINE__, ##__VA_ARGS__) +#else +#define TIMED_BLOCK(...) +#endif +#ifdef DEBUG struct debug_record { uint64_t cycles; @@ -17,6 +21,7 @@ struct debug_record }; + extern debug_record debug_record_array[]; struct timed_block { @@ -37,3 +42,4 @@ struct timed_block { record->cycles += __rdtsc(); } }; +#endif