Faster random implementation
This commit is contained in:
parent
321c677da2
commit
742ef283e4
7 changed files with 49 additions and 10 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -8,3 +8,6 @@ raytracer
|
|||
|
||||
# Actual output image
|
||||
image.ppm
|
||||
|
||||
# Profiler data
|
||||
perf.*
|
||||
|
|
|
|||
2
Makefile
2
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\
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
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))
|
||||
|
|
|
|||
18
main.cpp
18
main.cpp
|
|
@ -2,9 +2,7 @@
|
|||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
// 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<sphere> random_scene();
|
||||
|
||||
hittable_list<sphere> 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
|
||||
|
|
|
|||
15
random.h
Normal file
15
random.h
Normal 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));
|
||||
}
|
||||
|
|
@ -4,9 +4,16 @@
|
|||
#include <math.h>
|
||||
#include <memory>
|
||||
#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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,15 @@
|
|||
#include <x86intrin.h>
|
||||
#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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue