From c8d47800eb4d85a26216da3ff938d530be32dbf0 Mon Sep 17 00:00:00 2001 From: Anton Lydike Date: Fri, 24 Apr 2020 16:51:22 +0200 Subject: [PATCH] performance improvements --- README.md | 8 ++++++++ main.c | 8 ++++---- marcher.h | 13 ++++++++++--- src/scene.c | 21 ++++++++++++++++----- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 49f3274..fef9d19 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,12 @@ in fish: nix-shell --run fish # run gcc with flags gcc (pkg-config --cflags --libs gtk+-3.0 | string split " ") -lm -Wall -Wextra -o march.out main.c +``` + +## Running + +give it the number of threads you want to use + +```bash +./march.out ``` \ No newline at end of file diff --git a/main.c b/main.c index 3bc132e..632880b 100644 --- a/main.c +++ b/main.c @@ -122,9 +122,10 @@ int main(int argc, char* argv[]) { printf("threads: %d\n", threads); // create basic scene with up to 10 objects - Scene scene = scene_new(4000, 4000, 10); - scene.max_steps = 1000; - scene.threshold = 0.0001; + Scene scene = scene_new(1000, 1000, 10); + scene.perf_opts.max_steps = 1000; + scene.perf_opts.threshold = 0.0001; + scene.perf_opts.speed_cutoff = 10; scene.background = color_new(255,255,255); //scene_add_obj(&scene, circle_new(pt_new(SCENE_MOD / 2.0, SCENE_MOD/ 2.0, SCENE_MOD / 2.0), .2)); @@ -140,4 +141,3 @@ int main(int argc, char* argv[]) { return 0; } - diff --git a/marcher.h b/marcher.h index 69d906f..acc539a 100644 --- a/marcher.h +++ b/marcher.h @@ -62,15 +62,22 @@ typedef struct __myobject { struct __myscene* scene; } SceneObject; + +typedef struct __perfopts { + int speed_cutoff; + int max_steps; + double threshold; +} PerformanceOptimizations; + + typedef struct __myscene { unsigned int width; unsigned int height; SceneObject * objects; int object_count; int allocated_space; - // some other settings - int max_steps; - double threshold; + // performance opts + PerformanceOptimizations perf_opts; // colors etc Color background; } Scene; diff --git a/src/scene.c b/src/scene.c index 65e9190..34f9a90 100644 --- a/src/scene.c +++ b/src/scene.c @@ -15,12 +15,18 @@ Scene scene_new(unsigned int width, unsigned int height, int obj_count) { Scene scene; scene.height = height; scene.width = width; - scene.max_steps = 32; - scene.threshold = 0.02; scene.object_count = 0; scene.objects = malloc(obj_count * sizeof(SceneObject)); scene.allocated_space = obj_count; scene.background = color_new(0,0,0); + + PerformanceOptimizations perf_opts; + perf_opts.speed_cutoff = 0; + perf_opts.max_steps = 32; + perf_opts.threshold = 0.02; + + scene.perf_opts = perf_opts; + return scene; } @@ -71,8 +77,9 @@ Color march_ray(Point origin, Point direction, Scene* scene) { SceneObject* closest_obj = scene->objects; // get steps, threshold from scene - int steps = scene->max_steps; - double threshold = scene->threshold; + int steps = scene->perf_opts.max_steps; + double threshold = scene->perf_opts.threshold; + int speed_cutoff = scene->perf_opts.speed_cutoff; // as long as we did not max out steps, or got very close to an object while (steps > 0 && dist > threshold) { @@ -91,6 +98,10 @@ Color march_ray(Point origin, Point direction, Scene* scene) { } } + if (speed_cutoff > 0 && speed_cutoff < dist) { + break; + } + // write down our closest encounter if (dist < closest_encounter) closest_encounter = dist; @@ -107,7 +118,7 @@ Color march_ray(Point origin, Point direction, Scene* scene) { // a hit! #ifndef SCENE_NO_AM_OCC - double f = (steps / (double) scene->max_steps); + double f = (steps / (double) scene->perf_opts.max_steps); f = f * f * f * f; // to the fourth power to make smaller values bigger Color c = closest_obj->get_color(pos, direction, closest_obj);