From 313619d8d524e1d56b5596646ee3ea89932a4b23 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Tue, 23 Sep 2025 15:35:26 +0100 Subject: [PATCH] Add progress output --- src/camera.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/camera.c b/src/camera.c index 999df26..d923870 100644 --- a/src/camera.c +++ b/src/camera.c @@ -5,6 +5,8 @@ #include #include +#include +#include #include #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); }