Faster random implementation

This commit is contained in:
David 2021-08-29 16:17:13 +02:00
commit 742ef283e4
7 changed files with 49 additions and 10 deletions

3
.gitignore vendored
View file

@ -8,3 +8,6 @@ raytracer
# Actual output image # Actual output image
image.ppm image.ppm
# Profiler data
perf.*

View file

@ -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 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 g++ $(FLAGS) -I$(INCLUDE) $(LIBS) main.cpp -o raytracer
make debug:
image: raytracer image: raytracer
@./raytracer > image.ppm @./raytracer > image.ppm
@if [ $$TERM = "xterm-kitty" ]; then\ @if [ $$TERM = "xterm-kitty" ]; then\

View file

@ -34,8 +34,8 @@ bool hittable_list<T>::hit(const ray& r, float t_min, float t_max, hit_record& r
bool hit_anything = false; bool hit_anything = false;
float closest_so_far = t_max; float closest_so_far = t_max;
uint32_t s = objects.size();
for (uint32_t i = 0; i < objects.size(); ++i) for (uint32_t i = 0; i < s; ++i)
{ {
T *object = &objects[i]; T *object = &objects[i];
if (object->hit(r, t_min, closest_so_far, temp_rec)) if (object->hit(r, t_min, closest_so_far, temp_rec))

View file

@ -2,9 +2,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#define RMT_ENABLED 0
// Disable profiling
#define RMT_ENABLED 1
// Lib includes // Lib includes
#pragma GCC diagnostic push #pragma GCC diagnostic push
@ -21,9 +19,15 @@
#include "sphere.hpp" #include "sphere.hpp"
#include "camera.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); color ray_color(const ray& r, const hittable& world, int32_t depth);
float hit_sphere(const point3& center, float radius, const ray& r); float hit_sphere(const point3& center, float radius, const ray& r);
void print_timers();
hittable_list<sphere> random_scene(); hittable_list<sphere> random_scene();
hittable_list<sphere> random_scene() { hittable_list<sphere> random_scene() {
@ -188,10 +192,10 @@ int32_t main()
rmt_DestroyGlobalInstance(rmt); rmt_DestroyGlobalInstance(rmt);
} }
#ifdef DEBUG
debug_record debug_record_array[__COUNTER__]; debug_record debug_record_array[__COUNTER__];
void print_timers_()
void print_timers()
{ {
for (uint32_t i = 0; for (uint32_t i = 0;
i < sizeof(debug_record_array) / sizeof(debug_record_array[0]); i < sizeof(debug_record_array) / sizeof(debug_record_array[0]);
@ -211,3 +215,5 @@ void print_timers()
(double)record->cycles / CLOCKS_PER_SEC); (double)record->cycles / CLOCKS_PER_SEC);
} }
} }
#endif

15
random.h Normal file
View file

@ -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));
}

View file

@ -4,9 +4,16 @@
#include <math.h> #include <math.h>
#include <memory> #include <memory>
#include "timer.hpp" #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 */ /* Utility functions */
float degrees_to_radians(float d) inline float degrees_to_radians(float d)
{ {
return d * M_PI / 180; return d * M_PI / 180;
} }

View file

@ -1,11 +1,15 @@
#include <x86intrin.h> #include <x86intrin.h>
#include "rtweekend.hpp" #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 timed_block_##Number(__COUNTER__, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define TIMED_BLOCK_(number, ...) TIMED_BLOCK__(number, ##__VA_ARGS__) #define TIMED_BLOCK_(number, ...) TIMED_BLOCK__(number, ##__VA_ARGS__)
#define TIMED_BLOCK(...) TIMED_BLOCK_(__LINE__, ##__VA_ARGS__) #define TIMED_BLOCK(...) TIMED_BLOCK_(__LINE__, ##__VA_ARGS__)
#else
#define TIMED_BLOCK(...)
#endif
#ifdef DEBUG
struct debug_record struct debug_record
{ {
uint64_t cycles; uint64_t cycles;
@ -17,6 +21,7 @@ struct debug_record
}; };
extern debug_record debug_record_array[]; extern debug_record debug_record_array[];
struct timed_block { struct timed_block {
@ -37,3 +42,4 @@ struct timed_block {
record->cycles += __rdtsc(); record->cycles += __rdtsc();
} }
}; };
#endif