performance improvements

rework
Anton Lydike 5 years ago
parent 67b2c70f25
commit c8d47800eb

@ -10,3 +10,11 @@ nix-shell --run fish
# run gcc with flags # run gcc with flags
gcc (pkg-config --cflags --libs gtk+-3.0 | string split " ") -lm -Wall -Wextra -o march.out main.c 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 <num threads>
```

@ -122,9 +122,10 @@ int main(int argc, char* argv[]) {
printf("threads: %d\n", threads); printf("threads: %d\n", threads);
// create basic scene with up to 10 objects // create basic scene with up to 10 objects
Scene scene = scene_new(4000, 4000, 10); Scene scene = scene_new(1000, 1000, 10);
scene.max_steps = 1000; scene.perf_opts.max_steps = 1000;
scene.threshold = 0.0001; scene.perf_opts.threshold = 0.0001;
scene.perf_opts.speed_cutoff = 10;
scene.background = color_new(255,255,255); 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)); //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; return 0;
} }

@ -62,15 +62,22 @@ typedef struct __myobject {
struct __myscene* scene; struct __myscene* scene;
} SceneObject; } SceneObject;
typedef struct __perfopts {
int speed_cutoff;
int max_steps;
double threshold;
} PerformanceOptimizations;
typedef struct __myscene { typedef struct __myscene {
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
SceneObject * objects; SceneObject * objects;
int object_count; int object_count;
int allocated_space; int allocated_space;
// some other settings // performance opts
int max_steps; PerformanceOptimizations perf_opts;
double threshold;
// colors etc // colors etc
Color background; Color background;
} Scene; } Scene;

@ -15,12 +15,18 @@ Scene scene_new(unsigned int width, unsigned int height, int obj_count) {
Scene scene; Scene scene;
scene.height = height; scene.height = height;
scene.width = width; scene.width = width;
scene.max_steps = 32;
scene.threshold = 0.02;
scene.object_count = 0; scene.object_count = 0;
scene.objects = malloc(obj_count * sizeof(SceneObject)); scene.objects = malloc(obj_count * sizeof(SceneObject));
scene.allocated_space = obj_count; scene.allocated_space = obj_count;
scene.background = color_new(0,0,0); 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; return scene;
} }
@ -71,8 +77,9 @@ Color march_ray(Point origin, Point direction, Scene* scene) {
SceneObject* closest_obj = scene->objects; SceneObject* closest_obj = scene->objects;
// get steps, threshold from scene // get steps, threshold from scene
int steps = scene->max_steps; int steps = scene->perf_opts.max_steps;
double threshold = scene->threshold; 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 // as long as we did not max out steps, or got very close to an object
while (steps > 0 && dist > threshold) { 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 // write down our closest encounter
if (dist < closest_encounter) closest_encounter = dist; if (dist < closest_encounter) closest_encounter = dist;
@ -107,7 +118,7 @@ Color march_ray(Point origin, Point direction, Scene* scene) {
// a hit! // a hit!
#ifndef SCENE_NO_AM_OCC #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 f = f * f * f * f; // to the fourth power to make smaller values bigger
Color c = closest_obj->get_color(pos, direction, closest_obj); Color c = closest_obj->get_color(pos, direction, closest_obj);

Loading…
Cancel
Save