More timing functions

This commit is contained in:
David 2021-08-28 01:04:31 +02:00
commit 321c677da2
12 changed files with 184 additions and 129 deletions

View file

@ -3,6 +3,9 @@
#include <stdlib.h>
// Disable profiling
#define RMT_ENABLED 1
// Lib includes
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
@ -18,23 +21,23 @@
#include "sphere.hpp"
#include "camera.hpp"
color ray_color(const ray& r, const hittable& world, int32_t depth);
double hit_sphere(const point3& center, double radius, const ray& r);
hittable_list random_scene();
float hit_sphere(const point3& center, float radius, const ray& r);
void print_timers();
hittable_list<sphere> random_scene();
hittable_list random_scene() {
hittable_list world;
hittable_list<sphere> random_scene() {
hittable_list<sphere> world;
auto ground_material = make_shared<lambertian>(color(0.5, 0.5, 0.5));
world.add(make_shared<sphere>(point3(0,-1000,0), 1000, ground_material));
world.add(sphere(point3(0,-1000,0), 1000, ground_material));
for (int32_t a = -11; a < 11; a++)
{
for (int32_t b = -11; b < 11; b++)
{
double choose_mat = random_double();
point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());
float choose_mat = random_float();
point3 center(a + 0.9*random_float(), 0.2, b + 0.9*random_float());
if ((center - point3(4, 0.2, 0)).length() > 0.9)
{
@ -44,39 +47,40 @@ hittable_list random_scene() {
// diffuse
color albedo = color::random() * color::random();
sphere_material = make_shared<lambertian>(albedo);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
world.add(sphere(center, 0.2, sphere_material));
}
else if (choose_mat < 0.95)
{
// metal
color albedo = color::random(0.5, 1);
double fuzz = random_double(0, 0.5);
float fuzz = random_float(0, 0.5);
sphere_material = make_shared<metal>(albedo, fuzz);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
world.add(sphere(center, 0.2, sphere_material));
}
else
{
// glass
sphere_material = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
world.add(sphere(center, 0.2, sphere_material));
}
}
}
}
auto material1 = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(point3(0, 1, 0), 1.0, material1));
world.add(sphere(point3(0, 1, 0), 1.0, material1));
auto material2 = make_shared<lambertian>(color(0.4, 0.2, 0.1));
world.add(make_shared<sphere>(point3(-4, 1, 0), 1.0, material2));
world.add(sphere(point3(-4, 1, 0), 1.0, material2));
auto material3 = make_shared<metal>(color(0.7, 0.6, 0.5), 0.0);
world.add(make_shared<sphere>(point3(4, 1, 0), 1.0, material3));
world.add(sphere(point3(4, 1, 0), 1.0, material3));
return world;
}
color ray_color(const ray& r, const hittable& world, int32_t depth)
template<typename T>
color ray_color(const ray& r, hittable_list<T>& world, int32_t depth)
{
rmt_ScopedCPUSample(Scatter, RMTSF_Aggregate | RMTSF_Recursive);
if (depth <= 0)
@ -102,17 +106,17 @@ color ray_color(const ray& r, const hittable& world, int32_t depth)
}
}
vec3 unit_direction = normalize(r.direction);
double t = 0.5 * (unit_direction.y + 1.0);
float t = 0.5 * (unit_direction.y + 1.0);
return (1-t) * color(1,1,1) + t*color(0.5,0.7,1.0);
}
double hit_sphere(const point3& center, double radius, const ray& r)
float hit_sphere(const point3& center, float radius, const ray& r)
{
vec3 oc = r.origin - center;
double a = r.direction.length_squared();
double half_b = dot(oc, r.direction);
double c = oc.length_squared() - radius*radius;
double discriminant = half_b*half_b - a*c;
float a = r.direction.length_squared();
float half_b = dot(oc, r.direction);
float c = oc.length_squared() - radius*radius;
float discriminant = half_b*half_b - a*c;
if (discriminant < 0)
return -1;
@ -130,7 +134,7 @@ int32_t main()
// Image
const double aspect_ratio = 3.0 / 2.0;
const float aspect_ratio = 3.0 / 2.0;
const int32_t image_width = 1200;
const int32_t image_height = (int32_t) (image_width / aspect_ratio);
int32_t samples_per_pixel = 500;
@ -141,17 +145,15 @@ int32_t main()
samples_per_pixel = strtol(getenv("SPP"), NULL, 10);
}
// World
hittable_list world = random_scene();
hittable_list<sphere> world = random_scene();
// Camera
point3 lookfrom(13,2,3);
point3 lookat(0,0,0);
vec3 vup(0,1,0);
double dist_to_focus = 10.0;
double aperture = 0.1;
float dist_to_focus = 10.0;
float aperture = 0.1;
camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
@ -163,6 +165,7 @@ int32_t main()
{
rmt_ScopedCPUSample(OuterLoop, RMTSF_Aggregate);
fprintf(stderr, "\rScanlines remaining: %d ", j);
print_timers();
fflush(stderr);
for (int32_t i = 0; i < image_width; ++i)
{
@ -171,8 +174,8 @@ int32_t main()
for (int32_t s = 0; s < samples_per_pixel; ++s)
{
double u = ((i + random_double()) / (image_width-1));
double v = ((j + random_double()) / (image_height-1));
float u = ((i + random_float()) / (image_width-1));
float v = ((j + random_float()) / (image_height-1));
ray r = cam.get_ray(u,v);
pixel_color += ray_color(r, world, max_depth);
}
@ -184,3 +187,27 @@ int32_t main()
fprintf(stderr, "\nDone\n");
rmt_DestroyGlobalInstance(rmt);
}
debug_record debug_record_array[__COUNTER__];
void print_timers()
{
for (uint32_t i = 0;
i < sizeof(debug_record_array) / sizeof(debug_record_array[0]);
++i)
{
debug_record *record = &debug_record_array[i];
fprintf(stderr,
"%d: %s:%s:%d; "
"Cycles = %ld; "
"Hit count %ld; "
"Cycles/hit %f; "
"Time %f",
i, record->filename, record->function_name, record->line_number,
record->cycles,
record->hit_count,
(double)record->cycles / record->hit_count,
(double)record->cycles / CLOCKS_PER_SEC);
}
}