performance improvements

rework
Anton Lydike 5 years ago
parent 67b2c70f25
commit c8d47800eb

@ -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 <num threads>
```

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

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

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

Loading…
Cancel
Save