Add progress output

This commit is contained in:
Camden Dixie O'Brien
2025-09-23 15:35:26 +01:00
parent c12ae7b4a5
commit 313619d8d5

View File

@@ -5,6 +5,8 @@
#include <float.h>
#include <math.h>
#include <stdatomic.h>
#include <stdio.h>
#include <threads.h>
#define MAX_ITER 10
@@ -24,6 +26,7 @@ typedef struct {
pix_t *pixels;
rng_t rng;
unsigned start_y, row_count, scene_count;
atomic_uint *progress;
} work_slice_t;
static const vec3_t lightblue = { 0.4, 0.6, 1.0 };
@@ -104,6 +107,8 @@ static int render_thread(void *arg)
setpix(colour, slice->pixels + (w * y + x));
}
atomic_fetch_add(slice->progress, 1);
}
return 0;
@@ -143,8 +148,11 @@ void camera_render(
img_out->w = camera->img_width;
img_out->h = camera->img_height;
const unsigned rows_per_thread = img_out->h / NTHREADS;
const unsigned rem_rows = img_out->h % NTHREADS;
const unsigned rows = camera->img_height;
const unsigned rows_per_thread = rows / NTHREADS;
const unsigned rem_rows = rows % NTHREADS;
atomic_uint progress = 0;
thrd_t threads[NTHREADS];
work_slice_t slices[NTHREADS];
@@ -160,9 +168,20 @@ void camera_render(
if (rem_rows != 0 && i == NTHREADS - 1)
slices[i].row_count += rem_rows;
slices[i].progress = &progress;
thrd_create(threads + i, render_thread, slices + i);
}
const unsigned digits = (unsigned)floor(log(rows) / log(10)) + 1;
unsigned done;
do {
thrd_sleep(&(struct timespec) { .tv_nsec = 50'000'000 }, nullptr);
done = atomic_load(&progress);
fprintf(stderr, "\r[%*d/%d]", digits, done, rows);
fflush(stderr);
} while (done < rows);
for (unsigned i = 0; i < NTHREADS; ++i)
thrd_join(threads[i], 0);
}