From 6bfd66051632fdb8eca4103df2c3c67492d28af7 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sat, 23 Nov 2019 16:59:59 -0800 Subject: rototiller: pass cpu to .render_fragment() Mostly mechanical change, though threads.c needed some jiggering to make the logical cpu id available to the worker threads. Now render_fragment() can easily addresss per-cpu data created by create_context(). --- src/modules/flui2d/flui2d.c | 2 +- src/modules/julia/julia.c | 2 +- src/modules/pixbounce/pixbounce.c | 2 +- src/modules/plasma/plasma.c | 2 +- src/modules/ray/ray.c | 2 +- src/modules/roto/roto.c | 2 +- src/modules/snow/snow.c | 2 +- src/modules/sparkler/sparkler.c | 2 +- src/modules/stars/stars.c | 2 +- src/modules/submit/submit.c | 2 +- src/modules/swab/swab.c | 2 +- src/rototiller.c | 2 +- src/rototiller.h | 2 +- src/threads.c | 34 +++++++++++++++++++++++----------- src/threads.h | 3 +-- 15 files changed, 37 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/modules/flui2d/flui2d.c b/src/modules/flui2d/flui2d.c index 624202b..1464c7d 100644 --- a/src/modules/flui2d/flui2d.c +++ b/src/modules/flui2d/flui2d.c @@ -247,7 +247,7 @@ static void flui2d_prepare_frame(void *context, unsigned n_cpus, fb_fragment_t * /* Draw a the flui2d densities */ -static void flui2d_render_fragment(void *context, fb_fragment_t *fragment) +static void flui2d_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) { flui2d_context_t *ctxt = context; diff --git a/src/modules/julia/julia.c b/src/modules/julia/julia.c index 61ed102..ff44a70 100644 --- a/src/modules/julia/julia.c +++ b/src/modules/julia/julia.c @@ -139,7 +139,7 @@ static void julia_prepare_frame(void *context, unsigned n_cpus, fb_fragment_t *f /* Draw a morphing Julia set */ -static void julia_render_fragment(void *context, fb_fragment_t *fragment) +static void julia_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) { julia_context_t *ctxt = context; unsigned x, y; diff --git a/src/modules/pixbounce/pixbounce.c b/src/modules/pixbounce/pixbounce.c index 764f559..bfc7693 100644 --- a/src/modules/pixbounce/pixbounce.c +++ b/src/modules/pixbounce/pixbounce.c @@ -101,7 +101,7 @@ static int pick_pix(int num_pics, int last_pic) return pix_num; } -static void pixbounce_render_fragment(void *context, fb_fragment_t *fragment) +static void pixbounce_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) { static int initialized=0; static int x, y, multiplier; diff --git a/src/modules/plasma/plasma.c b/src/modules/plasma/plasma.c index 731838f..cf32a41 100644 --- a/src/modules/plasma/plasma.c +++ b/src/modules/plasma/plasma.c @@ -86,7 +86,7 @@ static void plasma_prepare_frame(void *context, unsigned n_cpus, fb_fragment_t * /* Draw a plasma effect */ -static void plasma_render_fragment(void *context, fb_fragment_t *fragment) +static void plasma_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) { plasma_context_t *ctxt = context; unsigned width = fragment->width, height = fragment->height; diff --git a/src/modules/ray/ray.c b/src/modules/ray/ray.c index 99e6609..ad710bc 100644 --- a/src/modules/ray/ray.c +++ b/src/modules/ray/ray.c @@ -179,7 +179,7 @@ static void ray_prepare_frame(void *context, unsigned n_cpus, fb_fragment_t *fra /* ray trace a simple scene into the fragment */ -static void ray_render_fragment(void *context, fb_fragment_t *fragment) +static void ray_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) { ray_context_t *ctxt = context; diff --git a/src/modules/roto/roto.c b/src/modules/roto/roto.c index 050b129..8e91c89 100644 --- a/src/modules/roto/roto.c +++ b/src/modules/roto/roto.c @@ -199,7 +199,7 @@ static void roto_prepare_frame(void *context, unsigned n_cpus, fb_fragment_t *fr /* Draw a rotating checkered 256x256 texture into fragment. */ -static void roto_render_fragment(void *context, fb_fragment_t *fragment) +static void roto_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) { roto_context_t *ctxt = context; int y_cos_r, y_sin_r, x_cos_r, x_sin_r, x_cos_r_init, x_sin_r_init, cos_r, sin_r; diff --git a/src/modules/snow/snow.c b/src/modules/snow/snow.c index f3c20d5..cf3ffa7 100644 --- a/src/modules/snow/snow.c +++ b/src/modules/snow/snow.c @@ -29,7 +29,7 @@ static void snow_prepare_frame(void *context, unsigned n_cpus, fb_fragment_t *fr } -static void snow_render_fragment(void *context, fb_fragment_t *fragment) +static void snow_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) { for (unsigned y = fragment->y; y < fragment->y + fragment->height; y++) { for (unsigned x = fragment->x; x < fragment->x + fragment->width; x++) { diff --git a/src/modules/sparkler/sparkler.c b/src/modules/sparkler/sparkler.c index 9462438..12138c9 100644 --- a/src/modules/sparkler/sparkler.c +++ b/src/modules/sparkler/sparkler.c @@ -80,7 +80,7 @@ static void sparkler_prepare_frame(void *context, unsigned ncpus, fb_fragment_t /* Render a 3D particle system */ -static void sparkler_render_fragment(void *context, fb_fragment_t *fragment) +static void sparkler_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) { sparkler_context_t *ctxt = context; diff --git a/src/modules/stars/stars.c b/src/modules/stars/stars.c index 7c2f5f2..63ff73c 100644 --- a/src/modules/stars/stars.c +++ b/src/modules/stars/stars.c @@ -13,7 +13,7 @@ /* Copyright (C) 2017 Philip J. Freeman */ -static void stars_render_fragment(void *context, fb_fragment_t *fragment) +static void stars_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) { static int initialized, z; static struct universe* u; diff --git a/src/modules/submit/submit.c b/src/modules/submit/submit.c index 340783c..254cc78 100644 --- a/src/modules/submit/submit.c +++ b/src/modules/submit/submit.c @@ -311,7 +311,7 @@ static void submit_prepare_frame(void *context, unsigned n_cpus, fb_fragment_t * } -static void submit_render_fragment(void *context, fb_fragment_t *fragment) +static void submit_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) { submit_context_t *ctxt = context; diff --git a/src/modules/swab/swab.c b/src/modules/swab/swab.c index aa53566..9c26da2 100644 --- a/src/modules/swab/swab.c +++ b/src/modules/swab/swab.c @@ -106,7 +106,7 @@ static void swab_prepare_frame(void *context, unsigned n_cpus, fb_fragment_t *fr } -static void swab_render_fragment(void *context, fb_fragment_t *fragment) +static void swab_render_fragment(void *context, unsigned cpu, fb_fragment_t *fragment) { swab_context_t *ctxt = context; float cos_r = cos(ctxt->r); diff --git a/src/rototiller.c b/src/rototiller.c index a5d0927..4d6bd79 100644 --- a/src/rototiller.c +++ b/src/rototiller.c @@ -107,7 +107,7 @@ static void module_render_fragment(const rototiller_module_t *module, void *cont } } else if (module->render_fragment) - module->render_fragment(context, fragment); + module->render_fragment(context, 0, fragment); if (module->finish_frame) module->finish_frame(context, fragment); diff --git a/src/rototiller.h b/src/rototiller.h index e60eece..a81c3e4 100644 --- a/src/rototiller.h +++ b/src/rototiller.h @@ -14,7 +14,7 @@ typedef struct rototiller_module_t { void * (*create_context)(unsigned n_cpus); void (*destroy_context)(void *context); void (*prepare_frame)(void *context, unsigned n_cpus, fb_fragment_t *fragment, rototiller_fragmenter_t *res_fragmenter); - void (*render_fragment)(void *context, fb_fragment_t *fragment); + void (*render_fragment)(void *context, unsigned cpu, fb_fragment_t *fragment); void (*finish_frame)(void *context, fb_fragment_t *fragment); char *name; char *description; diff --git a/src/threads.c b/src/threads.c index 77ff7d1..0a68861 100644 --- a/src/threads.c +++ b/src/threads.c @@ -7,6 +7,12 @@ #include "threads.h" #include "util.h" +typedef struct thread_t { + threads_t *threads; + pthread_t pthread; + unsigned id; +} thread_t; + typedef struct threads_t { unsigned n_threads; @@ -16,7 +22,7 @@ typedef struct threads_t { pthread_mutex_t frame_mutex; pthread_cond_t frame_cond; - void (*render_fragment_func)(void *context, fb_fragment_t *fragment); + void (*render_fragment_func)(void *context, unsigned cpu, fb_fragment_t *fragment); void *context; fb_fragment_t *fragment; rototiller_fragmenter_t fragmenter; @@ -24,14 +30,15 @@ typedef struct threads_t { unsigned next_fragment; unsigned frame_num; - pthread_t threads[]; + thread_t threads[]; } threads_t; /* render fragments using the supplied render function */ -static void * thread_func(void *_threads) +static void * thread_func(void *_thread) { - threads_t *threads = _threads; + thread_t *thread = _thread; + threads_t *threads = thread->threads; unsigned prev_frame_num = 0; pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); @@ -56,7 +63,7 @@ static void * thread_func(void *_threads) if (!threads->fragmenter(threads->context, threads->fragment, frag_num, &fragment)) break; - threads->render_fragment_func(threads->context, &fragment); + threads->render_fragment_func(threads->context, thread->id, &fragment); } /* report as idle */ @@ -83,7 +90,7 @@ void threads_wait_idle(threads_t *threads) /* submit a frame's fragments to the threads */ -void threads_frame_submit(threads_t *threads, fb_fragment_t *fragment, rototiller_fragmenter_t fragmenter, void (*render_fragment_func)(void *context, fb_fragment_t *fragment), void *context) +void threads_frame_submit(threads_t *threads, fb_fragment_t *fragment, rototiller_fragmenter_t fragmenter, void (*render_fragment_func)(void *context, unsigned cpu, fb_fragment_t *fragment), void *context) { threads_wait_idle(threads); /* XXX: likely non-blocking; already happens pre page flip */ @@ -105,7 +112,7 @@ threads_t * threads_create(void) unsigned i, num = get_ncpus(); threads_t *threads; - threads = calloc(1, sizeof(threads_t) + sizeof(pthread_t) * num); + threads = calloc(1, sizeof(threads_t) + sizeof(thread_t) * num); if (!threads) return NULL; @@ -117,8 +124,13 @@ threads_t * threads_create(void) pthread_mutex_init(&threads->frame_mutex, NULL); pthread_cond_init(&threads->frame_cond, NULL); - for (i = 0; i < num; i++) - pthread_create(&threads->threads[i], NULL, thread_func, threads); + for (i = 0; i < num; i++) { + thread_t *thread = &threads->threads[i]; + + thread->threads = threads; + thread->id = i; + pthread_create(&thread->pthread, NULL, thread_func, thread); + } return threads; } @@ -130,10 +142,10 @@ void threads_destroy(threads_t *threads) unsigned i; for (i = 0; i < threads->n_threads; i++) - pthread_cancel(threads->threads[i]); + pthread_cancel(threads->threads[i].pthread); for (i = 0; i < threads->n_threads; i++) - pthread_join(threads->threads[i], NULL); + pthread_join(threads->threads[i].pthread, NULL); pthread_mutex_destroy(&threads->idle_mutex); pthread_cond_destroy(&threads->idle_cond); diff --git a/src/threads.h b/src/threads.h index ea78c25..7080537 100644 --- a/src/threads.h +++ b/src/threads.h @@ -3,13 +3,12 @@ typedef struct fb_fragment_t fb_fragment_t; typedef struct rototiller_frame_t rototiller_frame_t; -typedef struct thread_t thread_t; typedef struct threads_t threads_t; threads_t * threads_create(); void threads_destroy(threads_t *threads); -void threads_frame_submit(threads_t *threads, fb_fragment_t *fragment, rototiller_fragmenter_t fragmenter, void (*render_fragment_func)(void *context, fb_fragment_t *fragment), void *context); +void threads_frame_submit(threads_t *threads, fb_fragment_t *fragment, rototiller_fragmenter_t fragmenter, void (*render_fragment_func)(void *context, unsigned cpu, fb_fragment_t *fragment), void *context); void threads_wait_idle(threads_t *threads); unsigned threads_num_threads(threads_t *threads); -- cgit v1.2.1