summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.c7
-rw-r--r--src/modules/blinds/blinds.c23
-rw-r--r--src/modules/checkers/checkers.c25
-rw-r--r--src/modules/compose/compose.c43
-rw-r--r--src/modules/drizzle/drizzle.c24
-rw-r--r--src/modules/flui2d/flui2d.c25
-rw-r--r--src/modules/julia/julia.c35
-rw-r--r--src/modules/meta2d/meta2d.c36
-rw-r--r--src/modules/montage/montage.c61
-rw-r--r--src/modules/pixbounce/pixbounce.c20
-rw-r--r--src/modules/plasma/plasma.c25
-rw-r--r--src/modules/plato/plato.c33
-rw-r--r--src/modules/ray/ray.c29
-rw-r--r--src/modules/roto/roto.c25
-rw-r--r--src/modules/rtv/rtv.c38
-rw-r--r--src/modules/shapes/shapes.c39
-rw-r--r--src/modules/snow/snow.c29
-rw-r--r--src/modules/sparkler/sparkler.c20
-rw-r--r--src/modules/spiro/spiro.c29
-rw-r--r--src/modules/stars/stars.c43
-rw-r--r--src/modules/submit/submit.c32
-rw-r--r--src/modules/swab/swab.c28
-rw-r--r--src/modules/swarm/swarm.c29
-rw-r--r--src/modules/voronoi/voronoi.c20
-rw-r--r--src/til.c72
-rw-r--r--src/til.h35
-rw-r--r--src/til_threads.c6
-rw-r--r--src/til_threads.h2
28 files changed, 387 insertions, 446 deletions
diff --git a/src/main.c b/src/main.c
index 122fdea..6381bc5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -39,7 +39,7 @@ static til_fb_ops_t *fb_ops;
typedef struct rototiller_t {
const til_module_t *module;
- void *module_context;
+ til_module_context_t *module_context;
pthread_t thread;
til_fb_t *fb;
struct timeval start_tv;
@@ -226,7 +226,7 @@ static void * rototiller_thread(void *_rt)
gettimeofday(&now, NULL);
ticks = get_ticks(&rt->start_tv, &now, rt->ticks_offset);
- til_module_render(rt->module, rt->module_context, ticks, &page->fragment);
+ til_module_render(rt->module_context, ticks, &page->fragment);
til_fb_page_put(rt->fb, page);
}
@@ -282,6 +282,7 @@ int main(int argc, const char *argv[])
get_ticks(&rototiller.start_tv,
&rototiller.start_tv,
rototiller.ticks_offset),
+ 0,
setup.module_setup,
&rototiller.module_context)) < 0,
"unable to create module context: %s", strerror(-r));
@@ -299,7 +300,7 @@ int main(int argc, const char *argv[])
pthread_cancel(rototiller.thread);
pthread_join(rototiller.thread, NULL);
til_shutdown();
- til_module_destroy_context(rototiller.module, rototiller.module_context);
+ til_module_context_free(rototiller.module_context);
til_fb_free(rototiller.fb);
return EXIT_SUCCESS;
diff --git a/src/modules/blinds/blinds.c b/src/modules/blinds/blinds.c
index 8f32054..e717bb3 100644
--- a/src/modules/blinds/blinds.c
+++ b/src/modules/blinds/blinds.c
@@ -6,6 +6,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
/* Copyright (C) 2017-2022 Vito Caputo <vcaputo@pengaru.com> */
@@ -25,7 +26,8 @@ typedef struct blinds_setup_t {
} blinds_setup_t;
typedef struct blinds_context_t {
- blinds_setup_t setup;
+ til_module_context_t til_module_context;
+ blinds_setup_t setup;
} blinds_context_t;
@@ -35,28 +37,20 @@ static blinds_setup_t blinds_default_setup = {
};
-static void * blinds_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * blinds_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
blinds_context_t *ctxt;
if (!setup)
setup = &blinds_default_setup.til_setup;
- ctxt = calloc(1, sizeof(blinds_context_t));
+ ctxt = til_module_context_new(sizeof(blinds_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
ctxt->setup = *(blinds_setup_t *)setup;
- return ctxt;
-}
-
-
-static void blinds_destroy_context(void *context)
-{
- blinds_context_t *ctxt = context;
-
- free(ctxt);
+ return &ctxt->til_module_context;
}
@@ -89,9 +83,9 @@ static inline void draw_blind_vertical(til_fb_fragment_t *fragment, unsigned col
/* draw blinds over the fragment */
-static void blinds_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void blinds_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- blinds_context_t *ctxt = context;
+ blinds_context_t *ctxt = (blinds_context_t *)context;
static float rr;
@@ -194,7 +188,6 @@ static int blinds_setup(const til_settings_t *settings, til_setting_t **res_sett
til_module_t blinds_module = {
.create_context = blinds_create_context,
- .destroy_context = blinds_destroy_context,
.render_fragment = blinds_render_fragment,
.setup = blinds_setup,
.name = "blinds",
diff --git a/src/modules/checkers/checkers.c b/src/modules/checkers/checkers.c
index e911089..079df4d 100644
--- a/src/modules/checkers/checkers.c
+++ b/src/modules/checkers/checkers.c
@@ -20,6 +20,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#define CHECKERS_DEFAULT_SIZE 32
@@ -49,6 +50,7 @@ typedef struct checkers_setup_t {
} checkers_setup_t;
typedef struct checkers_context_t {
+ til_module_context_t til_module_context;
checkers_setup_t setup;
} checkers_context_t;
@@ -61,38 +63,32 @@ static checkers_setup_t checkers_default_setup = {
};
-static void * checkers_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * checkers_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
checkers_context_t *ctxt;
if (!setup)
setup = &checkers_default_setup.til_setup;
- ctxt = calloc(1, sizeof(checkers_context_t));
+ ctxt = til_module_context_new(sizeof(checkers_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
ctxt->setup = *(checkers_setup_t *)setup;
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void checkers_destroy_context(void *context)
+static int checkers_fragmenter(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
{
- free(context);
-}
-
-
-static int checkers_fragmenter(void *context, unsigned n_cpus, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
-{
- checkers_context_t *ctxt = context;
+ checkers_context_t *ctxt = (checkers_context_t *)context;
return til_fb_fragment_tile_single(fragment, ctxt->setup.size, number, res_fragment);
}
-static void checkers_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void checkers_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
*res_fragmenter = checkers_fragmenter;
}
@@ -109,9 +105,9 @@ static inline unsigned hash(unsigned x)
}
-static void checkers_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void checkers_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- checkers_context_t *ctxt = context;
+ checkers_context_t *ctxt = (checkers_context_t *)context;
int state;
switch (ctxt->setup.pattern) {
@@ -293,7 +289,6 @@ static int checkers_setup(const til_settings_t *settings, til_setting_t **res_se
til_module_t checkers_module = {
.create_context = checkers_create_context,
- .destroy_context = checkers_destroy_context,
.prepare_frame = checkers_prepare_frame,
.render_fragment = checkers_render_fragment,
.setup = checkers_setup,
diff --git a/src/modules/compose/compose.c b/src/modules/compose/compose.c
index dfe3794..7415150 100644
--- a/src/modules/compose/compose.c
+++ b/src/modules/compose/compose.c
@@ -4,6 +4,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_settings.h"
#include "til_util.h"
@@ -26,12 +27,12 @@
typedef struct compose_layer_t {
const til_module_t *module;
- void *module_ctxt;
+ til_module_context_t *module_ctxt;
char *settings;
} compose_layer_t;
typedef struct compose_context_t {
- unsigned n_cpus;
+ til_module_context_t til_module_context;
til_fb_fragment_t texture_fb;
compose_layer_t texture;
@@ -46,9 +47,9 @@ typedef struct compose_setup_t {
char *layers[];
} compose_setup_t;
-static void * compose_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup);
-static void compose_destroy_context(void *context);
-static void compose_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter);
+static til_module_context_t * compose_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup);
+static void compose_destroy_context(til_module_context_t *context);
+static void compose_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter);
static int compose_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);
static compose_setup_t compose_default_setup = {
@@ -66,7 +67,7 @@ til_module_t compose_module = {
};
-static void * compose_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * compose_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
compose_context_t *ctxt;
size_t n;
@@ -76,12 +77,10 @@ static void * compose_create_context(unsigned seed, unsigned ticks, unsigned n_c
for (n = 0; ((compose_setup_t *)setup)->layers[n]; n++);
- ctxt = calloc(1, sizeof(compose_context_t) + n * sizeof(compose_layer_t));
+ ctxt = til_module_context_new(sizeof(compose_context_t) + n * sizeof(compose_layer_t), seed, n_cpus);
if (!ctxt)
return NULL;
- ctxt->n_cpus = n_cpus;
-
for (size_t i = 0; i < n; i++) {
const til_module_t *layer_module;
til_setup_t *layer_setup = NULL;
@@ -90,7 +89,7 @@ static void * compose_create_context(unsigned seed, unsigned ticks, unsigned n_c
(void) til_module_randomize_setup(layer_module, &layer_setup, NULL);
ctxt->layers[i].module = layer_module;
- (void) til_module_create_context(layer_module, rand_r(&seed), ticks, layer_setup, &ctxt->layers[i].module_ctxt);
+ (void) til_module_create_context(layer_module, rand_r(&seed), ticks, 0, layer_setup, &ctxt->layers[i].module_ctxt);
til_setup_free(layer_setup);
ctxt->n_layers++;
@@ -102,23 +101,23 @@ static void * compose_create_context(unsigned seed, unsigned ticks, unsigned n_c
ctxt->texture.module = til_lookup_module(((compose_setup_t *)setup)->texture);
(void) til_module_randomize_setup(ctxt->texture.module, &texture_setup, NULL);
- (void) til_module_create_context(ctxt->texture.module, rand_r(&seed), ticks, texture_setup, &ctxt->texture.module_ctxt);
+ (void) til_module_create_context(ctxt->texture.module, rand_r(&seed), ticks, 0, texture_setup, &ctxt->texture.module_ctxt);
til_setup_free(texture_setup);
}
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void compose_destroy_context(void *context)
+static void compose_destroy_context(til_module_context_t *context)
{
- compose_context_t *ctxt = context;
+ compose_context_t *ctxt = (compose_context_t *)context;
for (int i = 0; i < ctxt->n_layers; i++)
- til_module_destroy_context(ctxt->layers[i].module, ctxt->layers[i].module_ctxt);
+ til_module_context_free(ctxt->layers[i].module_ctxt);
if (ctxt->texture.module)
- til_module_destroy_context(ctxt->texture.module, ctxt->texture.module_ctxt);
+ til_module_context_free(ctxt->texture.module_ctxt);
free(ctxt->texture_fb.buf);
@@ -126,9 +125,9 @@ static void compose_destroy_context(void *context)
}
-static void compose_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void compose_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- compose_context_t *ctxt = context;
+ compose_context_t *ctxt = (compose_context_t *)context;
if (ctxt->texture.module) {
if (!ctxt->texture_fb.buf ||
@@ -147,20 +146,20 @@ static void compose_prepare_frame(void *context, unsigned ticks, unsigned n_cpus
}
ctxt->texture_fb.cleared = 0;
- til_module_render(ctxt->texture.module, ctxt->texture.module_ctxt, ticks, &ctxt->texture_fb);
+ til_module_render(ctxt->texture.module_ctxt, ticks, &ctxt->texture_fb);
- til_module_render(ctxt->layers[0].module, ctxt->layers[0].module_ctxt, ticks, fragment);
+ til_module_render(ctxt->layers[0].module_ctxt, ticks, fragment);
for (size_t i = 1; i < ctxt->n_layers; i++) {
til_fb_fragment_t textured = *fragment;
textured.texture = &ctxt->texture_fb;
- til_module_render(ctxt->layers[i].module, ctxt->layers[i].module_ctxt, ticks, &textured);
+ til_module_render(ctxt->layers[i].module_ctxt, ticks, &textured);
}
} else {
for (size_t i = 0; i < ctxt->n_layers; i++)
- til_module_render(ctxt->layers[i].module, ctxt->layers[i].module_ctxt, ticks, fragment);
+ til_module_render(ctxt->layers[i].module_ctxt, ticks, fragment);
}
}
diff --git a/src/modules/drizzle/drizzle.c b/src/modules/drizzle/drizzle.c
index 21d4fbf..b23fba8 100644
--- a/src/modules/drizzle/drizzle.c
+++ b/src/modules/drizzle/drizzle.c
@@ -20,6 +20,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "puddle/puddle.h"
@@ -41,8 +42,9 @@ typedef struct drizzle_setup_t {
} drizzle_setup_t;
typedef struct drizzle_context_t {
- puddle_t *puddle;
- drizzle_setup_t setup;
+ til_module_context_t til_module_context;
+ puddle_t *puddle;
+ drizzle_setup_t setup;
} drizzle_context_t;
static drizzle_setup_t drizzle_default_setup = {
@@ -72,14 +74,14 @@ static inline uint32_t color_to_uint32(v3f_t color) {
}
-static void * drizzle_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * drizzle_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
drizzle_context_t *ctxt;
if (!setup)
setup = &drizzle_default_setup.til_setup;
- ctxt = calloc(1, sizeof(drizzle_context_t));
+ ctxt = til_module_context_new(sizeof(drizzle_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
@@ -91,22 +93,22 @@ static void * drizzle_create_context(unsigned seed, unsigned ticks, unsigned n_c
ctxt->setup = *(drizzle_setup_t *)setup;
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void drizzle_destroy_context(void *context)
+static void drizzle_destroy_context(til_module_context_t *context)
{
- drizzle_context_t *ctxt = context;
+ drizzle_context_t *ctxt = (drizzle_context_t *)context;
puddle_free(ctxt->puddle);
free(ctxt);
}
-static void drizzle_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void drizzle_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- drizzle_context_t *ctxt = context;
+ drizzle_context_t *ctxt = (drizzle_context_t *)context;
*res_fragmenter = til_fragmenter_slice_per_cpu;
@@ -128,9 +130,9 @@ static void drizzle_prepare_frame(void *context, unsigned ticks, unsigned n_cpus
}
-static void drizzle_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void drizzle_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- drizzle_context_t *ctxt = context;
+ drizzle_context_t *ctxt = (drizzle_context_t *)context;
float xf = 1.f / (float)fragment->frame_width;
float yf = 1.f / (float)fragment->frame_height;
v2f_t coord;
diff --git a/src/modules/flui2d/flui2d.c b/src/modules/flui2d/flui2d.c
index 4f87b54..df980ba 100644
--- a/src/modules/flui2d/flui2d.c
+++ b/src/modules/flui2d/flui2d.c
@@ -6,6 +6,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_settings.h"
@@ -186,6 +187,7 @@ typedef struct flui2d_setup_t {
} flui2d_setup_t;
typedef struct flui2d_context_t {
+ til_module_context_t til_module_context;
flui2d_t fluid;
flui2d_emitters_t emitters;
float clockstep;
@@ -243,7 +245,7 @@ static void gamma_init(float gamma)
}
-static void * flui2d_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * flui2d_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
static int initialized;
flui2d_context_t *ctxt;
@@ -251,7 +253,7 @@ static void * flui2d_create_context(unsigned seed, unsigned ticks, unsigned n_cp
if (!setup)
setup = &flui2d_default_setup.til_setup;
- ctxt = calloc(1, sizeof(flui2d_context_t));
+ ctxt = til_module_context_new(sizeof(flui2d_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
@@ -266,20 +268,14 @@ static void * flui2d_create_context(unsigned seed, unsigned ticks, unsigned n_cp
ctxt->emitters = ((flui2d_setup_t *)setup)->emitters;
ctxt->clockstep = ((flui2d_setup_t *)setup)->clockstep;
- return ctxt;
-}
-
-
-static void flui2d_destroy_context(void *context)
-{
- free(context);
+ return &ctxt->til_module_context;
}
/* Prepare a frame for concurrent drawing of fragment using multiple fragments */
-static void flui2d_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void flui2d_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- flui2d_context_t *ctxt = context;
+ flui2d_context_t *ctxt = (flui2d_context_t *)context;
float r = (ticks % (unsigned)(2 * M_PI * 1000)) * .001f;
*res_fragmenter = til_fragmenter_tile64;
@@ -336,9 +332,9 @@ static void flui2d_prepare_frame(void *context, unsigned ticks, unsigned n_cpus,
/* Draw a the flui2d densities */
-static void flui2d_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void flui2d_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- flui2d_context_t *ctxt = context;
+ flui2d_context_t *ctxt = (flui2d_context_t *)context;
for (int y = fragment->y; y < fragment->y + fragment->height; y++) {
int y0, y1;
@@ -542,11 +538,10 @@ static int flui2d_setup(const til_settings_t *settings, til_setting_t **res_sett
til_module_t flui2d_module = {
.create_context = flui2d_create_context,
- .destroy_context = flui2d_destroy_context,
.prepare_frame = flui2d_prepare_frame,
.render_fragment = flui2d_render_fragment,
+ .setup = flui2d_setup,
.name = "flui2d",
.description = "Fluid dynamics simulation in 2D (threaded (poorly))",
.author = "Vito Caputo <vcaputo@pengaru.com>",
- .setup = flui2d_setup,
};
diff --git a/src/modules/julia/julia.c b/src/modules/julia/julia.c
index d9cfdff..dd11f25 100644
--- a/src/modules/julia/julia.c
+++ b/src/modules/julia/julia.c
@@ -5,6 +5,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
/* Copyright (C) 2017 Vito Caputo <vcaputo@pengaru.com> */
@@ -13,12 +14,13 @@
/* TODO: explore using C99 complex.h and its types? */
typedef struct julia_context_t {
- float rr;
- float realscale;
- float imagscale;
- float creal;
- float cimag;
- float threshold;
+ til_module_context_t til_module_context;
+ float rr;
+ float realscale;
+ float imagscale;
+ float creal;
+ float cimag;
+ float threshold;
} julia_context_t;
static uint32_t colors[] = {
@@ -65,26 +67,20 @@ static uint32_t colors[] = {
};
-static void * julia_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * julia_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
julia_context_t *ctxt;
- ctxt = calloc(1, sizeof(julia_context_t));
+ ctxt = til_module_context_new(sizeof(julia_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
ctxt->rr = ((float)rand_r(&seed)) / (float)RAND_MAX * 100.f;
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void julia_destroy_context(void *context)
-{
- free(context);
-
-}
-
static inline unsigned julia_iter(float real, float imag, float creal, float cimag, unsigned max_iters, float threshold)
{
unsigned i;
@@ -110,9 +106,9 @@ static inline unsigned julia_iter(float real, float imag, float creal, float cim
/* Prepare a frame for concurrent drawing of fragment using multiple fragments */
-static void julia_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void julia_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- julia_context_t *ctxt = context;
+ julia_context_t *ctxt = (julia_context_t *)context;
*res_fragmenter = til_fragmenter_slice_per_cpu;
@@ -137,9 +133,9 @@ static void julia_prepare_frame(void *context, unsigned ticks, unsigned n_cpus,
/* Draw a morphing Julia set */
-static void julia_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void julia_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- julia_context_t *ctxt = context;
+ julia_context_t *ctxt = (julia_context_t *)context;
unsigned x, y;
unsigned width = fragment->width, height = fragment->height;
uint32_t *buf = fragment->buf;
@@ -160,7 +156,6 @@ static void julia_render_fragment(void *context, unsigned ticks, unsigned cpu, t
til_module_t julia_module = {
.create_context = julia_create_context,
- .destroy_context = julia_destroy_context,
.prepare_frame = julia_prepare_frame,
.render_fragment = julia_render_fragment,
.name = "julia",
diff --git a/src/modules/meta2d/meta2d.c b/src/modules/meta2d/meta2d.c
index d1063de..eb816d0 100644
--- a/src/modules/meta2d/meta2d.c
+++ b/src/modules/meta2d/meta2d.c
@@ -21,6 +21,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "din/din.h"
@@ -30,16 +31,17 @@
#define META2D_NUM_BALLS 10
typedef struct meta2d_ball_t {
- v2f_t position;
- float radius;
- v3f_t color;
+ v2f_t position;
+ float radius;
+ v3f_t color;
} meta2d_ball_t;
typedef struct meta2d_context_t {
- unsigned n;
- din_t *din_a, *din_b;
- float din_t;
- meta2d_ball_t balls[META2D_NUM_BALLS];
+ til_module_context_t til_module_context;
+ unsigned n;
+ din_t *din_a, *din_b;
+ float din_t;
+ meta2d_ball_t balls[META2D_NUM_BALLS];
} meta2d_context_t;
@@ -65,11 +67,13 @@ static inline uint32_t color_to_uint32(v3f_t color) {
}
-static void * meta2d_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * meta2d_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
meta2d_context_t *ctxt;
- ctxt = calloc(1, sizeof(meta2d_context_t));
+ ctxt = til_module_context_new(sizeof(meta2d_context_t), seed, n_cpus);
+ if (!ctxt)
+ return NULL;
/* perlin noise is used for some organic-ish random movement of the balls */
ctxt->din_a = din_new(10, 10, META2D_NUM_BALLS + 2);
@@ -84,13 +88,13 @@ static void * meta2d_create_context(unsigned seed, unsigned ticks, unsigned n_cp
v3f_rand(&ball->color, &(v3f_t){0.f, 0.f, 0.f}, &(v3f_t){1.f, 1.f, 1.f});
}
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void meta2d_destroy_context(void *context)
+static void meta2d_destroy_context(til_module_context_t *context)
{
- meta2d_context_t *ctxt = context;
+ meta2d_context_t *ctxt = (meta2d_context_t *)context;
din_free(ctxt->din_a);
din_free(ctxt->din_b);
@@ -98,9 +102,9 @@ static void meta2d_destroy_context(void *context)
}
-static void meta2d_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void meta2d_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- meta2d_context_t *ctxt = context;
+ meta2d_context_t *ctxt = (meta2d_context_t *)context;
*res_fragmenter = til_fragmenter_slice_per_cpu;
@@ -175,9 +179,9 @@ static void meta2d_prepare_frame(void *context, unsigned ticks, unsigned n_cpus,
}
-static void meta2d_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void meta2d_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- meta2d_context_t *ctxt = context;
+ meta2d_context_t *ctxt = (meta2d_context_t *)context;
float xf = 2.f / (float)fragment->frame_width;
float yf = 2.f / (float)fragment->frame_height;
v2f_t coord;
diff --git a/src/modules/montage/montage.c b/src/modules/montage/montage.c
index 1b79de5..443fcaa 100644
--- a/src/modules/montage/montage.c
+++ b/src/modules/montage/montage.c
@@ -4,21 +4,22 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_util.h"
/* Copyright (C) 2019 - Vito Caputo <vcaputo@pengaru.com> */
typedef struct montage_context_t {
+ til_module_context_t til_module_context;
const til_module_t **modules;
- void **contexts;
+ til_module_context_t **contexts;
size_t n_modules;
} montage_context_t;
-static void setup_next_module(montage_context_t *ctxt);
-static void * montage_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup);
-static void montage_destroy_context(void *context);
-static void montage_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter);
-static void montage_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment);
+static til_module_context_t * montage_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup);
+static void montage_destroy_context(til_module_context_t *context);
+static void montage_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter);
+static void montage_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment);
til_module_t montage_module = {
@@ -31,13 +32,13 @@ til_module_t montage_module = {
};
-static void * montage_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * montage_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
const til_module_t **modules, *rtv_module, *compose_module;
size_t n_modules;
montage_context_t *ctxt;
- ctxt = calloc(1, sizeof(montage_context_t));
+ ctxt = til_module_context_new(sizeof(montage_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
@@ -89,22 +90,22 @@ static void * montage_create_context(unsigned seed, unsigned ticks, unsigned n_c
(void) til_module_randomize_setup(module, &setup, NULL);
- if (module->create_context) /* FIXME errors */
- ctxt->contexts[i] = module->create_context(rand_r(&seed), ticks, 1, setup);
+ /* FIXME errors */
+ (void) til_module_create_context(module, rand_r(&seed), ticks, 1, setup, &ctxt->contexts[i]);
til_setup_free(setup);
}
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void montage_destroy_context(void *context)
+static void montage_destroy_context(til_module_context_t *context)
{
- montage_context_t *ctxt = context;
+ montage_context_t *ctxt = (montage_context_t *)context;
for (int i = 0; i < ctxt->n_modules; i++)
- til_module_destroy_context(ctxt->modules[i], ctxt->contexts[i]);
+ til_module_context_free(ctxt->contexts[i]);
free(ctxt->contexts);
free(ctxt->modules);
@@ -112,7 +113,6 @@ static void montage_destroy_context(void *context)
}
-
/* this is a hacked up derivative of til_fb_fragment_tile_single() */
static int montage_fragment_tile(const til_fb_fragment_t *fragment, unsigned tile_width, unsigned tile_height, unsigned number, til_fb_fragment_t *res_fragment)
{
@@ -164,9 +164,9 @@ static int montage_fragment_tile(const til_fb_fragment_t *fragment, unsigned til
* 1. it divides the frame into subfragments for threaded rendering
* 2. it determines which modules will be rendered where via fragment->number
*/
-static int montage_fragmenter(void *context, unsigned n_cpus, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
+static int montage_fragmenter(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
{
- montage_context_t *ctxt = context;
+ montage_context_t *ctxt = (montage_context_t *)context;
float root = sqrtf(ctxt->n_modules);
unsigned tile_width = fragment->frame_width / ceilf(root); /* screens are wide, always give excess to the width */
unsigned tile_height = fragment->frame_height / rintf(root); /* only give to the height when fraction is >= .5f */
@@ -183,18 +183,15 @@ static int montage_fragmenter(void *context, unsigned n_cpus, const til_fb_fragm
}
-
-static void montage_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void montage_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- montage_context_t *ctxt = context;
-
*res_fragmenter = montage_fragmenter;
}
-static void montage_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void montage_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- montage_context_t *ctxt = context;
+ montage_context_t *ctxt = (montage_context_t *)context;
const til_module_t *module = ctxt->modules[fragment->number];
if (fragment->number >= ctxt->n_modules) {
@@ -203,21 +200,5 @@ static void montage_render_fragment(void *context, unsigned ticks, unsigned cpu,
return;
}
- /* since we're *already* in a threaded render of tiles, no further
- * threading within the montage tiles is desirable, so the per-module
- * render is done explicitly serially here in an open-coded ad-hoc
- * fashion for now. FIXME TODO: move this into rototiller.c
- */
- if (module->prepare_frame) {
- til_fragmenter_t fragmenter;
- unsigned fragnum = 0;
- til_fb_fragment_t frag;
-
- module->prepare_frame(ctxt->contexts[fragment->number], ticks, 1, fragment, &fragmenter);
-
- while (fragmenter(ctxt->contexts[fragment->number], 1, fragment, fragnum++, &frag))
- module->render_fragment(ctxt->contexts[fragment->number], ticks, 0, &frag);
- } else if (module->render_fragment)
- module->render_fragment(ctxt->contexts[fragment->number], ticks, 0, fragment);
+ til_module_render(ctxt->contexts[fragment->number], ticks, fragment);
}
-
diff --git a/src/modules/pixbounce/pixbounce.c b/src/modules/pixbounce/pixbounce.c
index 99a8519..827615e 100644
--- a/src/modules/pixbounce/pixbounce.c
+++ b/src/modules/pixbounce/pixbounce.c
@@ -3,6 +3,7 @@
#include <unistd.h>
#include "til.h"
+#include "til_module_context.h"
#include "draw.h"
/* Copyright (C) 2018-22 Philip J. Freeman <elektron@halo.nu> */
@@ -225,13 +226,13 @@ pixbounce_pixmap_t pixbounce_pixmap[] = {
typedef struct pixbounce_context_t {
+ til_module_context_t til_module_context;
int x, y;
int x_dir, y_dir;
pixbounce_pixmap_t *pix;
uint32_t color;
float pixmap_size_factor;
int multiplier;
-
} pixbounce_context_t;
static uint32_t pick_color()
@@ -239,11 +240,11 @@ static uint32_t pick_color()
return makergb(rand()%256, rand()%256, rand()%256, 1);
}
-static void * pixbounce_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * pixbounce_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
pixbounce_context_t *ctxt;
- ctxt = malloc(sizeof(pixbounce_context_t));
+ ctxt = til_module_context_new(sizeof(pixbounce_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
@@ -256,18 +257,12 @@ static void * pixbounce_create_context(unsigned seed, unsigned ticks, unsigned n
ctxt->pixmap_size_factor = ((((pixbounce_setup_t *)setup)->pixmap_size)*55 + 22 )/ 100;
ctxt->multiplier = 1;
- return ctxt;
-}
-
-static void pixbounce_destroy_context(void *context)
-{
- pixbounce_context_t *ctxt = context;
- free(context);
+ return &ctxt->til_module_context;
}
-static void pixbounce_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void pixbounce_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- pixbounce_context_t *ctxt = context;
+ pixbounce_context_t *ctxt = (pixbounce_context_t *)context;
int width = fragment->width, height = fragment->height;
@@ -417,7 +412,6 @@ int pixbounce_setup(const til_settings_t *settings, til_setting_t **res_setting,
til_module_t pixbounce_module = {
.create_context = pixbounce_create_context,
- .destroy_context = pixbounce_destroy_context,
.render_fragment = pixbounce_render_fragment,
.setup = pixbounce_setup,
.name = "pixbounce",
diff --git a/src/modules/plasma/plasma.c b/src/modules/plasma/plasma.c
index dfcbd15..56f1176 100644
--- a/src/modules/plasma/plasma.c
+++ b/src/modules/plasma/plasma.c
@@ -5,6 +5,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
/* Copyright (C) 2017 Vito Caputo <vcaputo@pengaru.com> */
@@ -31,7 +32,8 @@ typedef struct color_t {
static int32_t costab[FIXED_TRIG_LUT_SIZE], sintab[FIXED_TRIG_LUT_SIZE];
typedef struct plasma_context_t {
- unsigned rr;
+ til_module_context_t til_module_context;
+ unsigned rr;
} plasma_context_t;
@@ -51,7 +53,7 @@ static void init_plasma(int32_t *costab, int32_t *sintab)
}
-static void * plasma_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * plasma_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
static int initialized;
plasma_context_t *ctxt;
@@ -62,26 +64,20 @@ static void * plasma_create_context(unsigned seed, unsigned ticks, unsigned n_cp
init_plasma(costab, sintab);
}
- ctxt = calloc(1, sizeof(plasma_context_t));
+ ctxt = til_module_context_new(sizeof(plasma_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
ctxt->rr = rand_r(&seed);
- return ctxt;
-}
-
-
-static void plasma_destroy_context(void *context)
-{
- free(context);
+ return &ctxt->til_module_context;
}
/* Prepare a frame for concurrent drawing of fragment using multiple fragments */
-static void plasma_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void plasma_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- plasma_context_t *ctxt = context;
+ plasma_context_t *ctxt = (plasma_context_t *)context;
*res_fragmenter = til_fragmenter_slice_per_cpu;
ctxt->rr += 3;
@@ -89,9 +85,9 @@ static void plasma_prepare_frame(void *context, unsigned ticks, unsigned n_cpus,
/* Draw a plasma effect */
-static void plasma_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void plasma_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- plasma_context_t *ctxt = context;
+ plasma_context_t *ctxt = (plasma_context_t *)context;
int xstep = PLASMA_WIDTH / fragment->frame_width;
int ystep = PLASMA_HEIGHT / fragment->frame_height;
unsigned width = fragment->width * xstep, height = fragment->height * ystep;
@@ -160,7 +156,6 @@ static void plasma_render_fragment(void *context, unsigned ticks, unsigned cpu,
til_module_t plasma_module = {
.create_context = plasma_create_context,
- .destroy_context = plasma_destroy_context,
.prepare_frame = plasma_prepare_frame,
.render_fragment = plasma_render_fragment,
.name = "plasma",
diff --git a/src/modules/plato/plato.c b/src/modules/plato/plato.c
index 2ca3478..c22a6c4 100644
--- a/src/modules/plato/plato.c
+++ b/src/modules/plato/plato.c
@@ -46,21 +46,23 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
typedef struct plato_context_t {
- float r;
+ til_module_context_t til_module_context;
+ float r;
} plato_context_t;
typedef struct v3f_t {
- float x, y, z;
+ float x, y, z;
} v3f_t;
typedef struct polyhedron_t {
- const char *name;
- unsigned edge_cnt, vertex_cnt;
- unsigned n_vertices; /* size of vertices[] which enumerates all vertices in face order */
- v3f_t vertices[];
+ const char *name;
+ unsigned edge_cnt, vertex_cnt;
+ unsigned n_vertices; /* size of vertices[] which enumerates all vertices in face order */
+ v3f_t vertices[];
} polyhedron_t;
@@ -608,29 +610,21 @@ static void draw_polyhedron(const polyhedron_t *polyhedron, m4f_t *transform, ti
}
-static void * plato_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * plato_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
plato_context_t *ctxt;
- ctxt = calloc(1, sizeof(plato_context_t));
+ ctxt = til_module_context_new(sizeof(plato_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void plato_destroy_context(void *context)
+static void plato_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- plato_context_t *ctxt = context;
-
- free(ctxt);
-}
-
-
-static void plato_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
-{
- plato_context_t *ctxt = context;
+ plato_context_t *ctxt = (plato_context_t *)context;
ctxt->r += .015f;
til_fb_fragment_clear(fragment);
@@ -666,7 +660,6 @@ static void plato_render_fragment(void *context, unsigned ticks, unsigned cpu, t
til_module_t plato_module = {
.create_context = plato_create_context,
- .destroy_context = plato_destroy_context,
.render_fragment = plato_render_fragment,
.name = "plato",
.description = "Platonic solids rendered in 3D",
diff --git a/src/modules/ray/ray.c b/src/modules/ray/ray.c
index 14ef45f..84e4eb2 100644
--- a/src/modules/ray/ray.c
+++ b/src/modules/ray/ray.c
@@ -4,6 +4,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_util.h"
#include "ray/ray_camera.h"
@@ -126,26 +127,27 @@ static float r;
typedef struct ray_context_t {
- ray_render_t *render;
+ til_module_context_t til_module_context;
+ ray_render_t *render;
} ray_context_t;
-static void * ray_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * ray_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
- return calloc(1, sizeof(ray_context_t));
-}
+ ray_context_t *ctxt;
+ ctxt = til_module_context_new(sizeof(ray_context_t), seed, n_cpus);
+ if (!ctxt)
+ return NULL;
-static void ray_destroy_context(void *context)
-{
- free(context);
+ return &ctxt->til_module_context;
}
/* prepare a frame for concurrent rendering */
-static void ray_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void ray_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- ray_context_t *ctxt = context;
+ ray_context_t *ctxt = (ray_context_t *)context;
*res_fragmenter = til_fragmenter_tile64;
#if 1
@@ -175,17 +177,17 @@ static void ray_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, ti
/* ray trace a simple scene into the fragment */
-static void ray_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void ray_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- ray_context_t *ctxt = context;
+ ray_context_t *ctxt = (ray_context_t *)context;
ray_render_trace_fragment(ctxt->render, fragment);
}
-static void ray_finish_frame(void *context, unsigned ticks, til_fb_fragment_t *fragment)
+static void ray_finish_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment)
{
- ray_context_t *ctxt = context;
+ ray_context_t *ctxt = (ray_context_t *)context;
ray_render_free(ctxt->render);
}
@@ -193,7 +195,6 @@ static void ray_finish_frame(void *context, unsigned ticks, til_fb_fragment_t *f
til_module_t ray_module = {
.create_context = ray_create_context,
- .destroy_context = ray_destroy_context,
.prepare_frame = ray_prepare_frame,
.render_fragment = ray_render_fragment,
.finish_frame = ray_finish_frame,
diff --git a/src/modules/roto/roto.c b/src/modules/roto/roto.c
index 8908bd2..cc69103 100644
--- a/src/modules/roto/roto.c
+++ b/src/modules/roto/roto.c
@@ -5,6 +5,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
/* Copyright (C) 2016 Vito Caputo <vcaputo@pengaru.com> */
@@ -24,31 +25,26 @@ typedef struct color_t {
} color_t;
typedef struct roto_context_t {
- unsigned r, rr;
+ til_module_context_t til_module_context;
+ unsigned r, rr;
} roto_context_t;
static int32_t costab[FIXED_TRIG_LUT_SIZE], sintab[FIXED_TRIG_LUT_SIZE];
static uint8_t texture[256][256];
static color_t palette[2];
-static void * roto_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * roto_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
roto_context_t *ctxt;
- ctxt = calloc(1, sizeof(roto_context_t));
+ ctxt = til_module_context_new(sizeof(roto_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
ctxt->r = rand_r(&seed);
ctxt->rr = rand_r(&seed);
- return ctxt;
-}
-
-
-static void roto_destroy_context(void *context)
-{
- free(context);
+ return &ctxt->til_module_context;
}
@@ -178,9 +174,9 @@ static void init_roto(uint8_t texture[256][256], int32_t *costab, int32_t *sinta
/* prepare a frame for concurrent rendering */
-static void roto_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void roto_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- roto_context_t *ctxt = context;
+ roto_context_t *ctxt = (roto_context_t *)context;
static int initialized;
if (!initialized) {
@@ -198,9 +194,9 @@ static void roto_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, t
/* Draw a rotating checkered 256x256 texture into fragment. */
-static void roto_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void roto_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- roto_context_t *ctxt = context;
+ roto_context_t *ctxt = (roto_context_t *)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;
int x, y, frame_width = fragment->frame_width, frame_height = fragment->frame_height;
uint32_t *buf = fragment->buf;
@@ -248,7 +244,6 @@ static void roto_render_fragment(void *context, unsigned ticks, unsigned cpu, ti
til_module_t roto_module = {
.create_context = roto_create_context,
- .destroy_context = roto_destroy_context,
.prepare_frame = roto_prepare_frame,
.render_fragment = roto_render_fragment,
.name = "roto",
diff --git a/src/modules/rtv/rtv.c b/src/modules/rtv/rtv.c
index d60a4cd..fe68903 100644
--- a/src/modules/rtv/rtv.c
+++ b/src/modules/rtv/rtv.c
@@ -3,6 +3,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_settings.h"
#include "til_util.h"
@@ -24,7 +25,7 @@
typedef struct rtv_channel_t {
const til_module_t *module;
- void *module_ctxt;
+ til_module_context_t *module_ctxt;
til_setup_t *module_setup;
time_t last_on_time, cumulative_time;
char *settings_as_arg;
@@ -33,6 +34,7 @@ typedef struct rtv_channel_t {
} rtv_channel_t;
typedef struct rtv_context_t {
+ til_module_context_t til_module_context;
time_t next_switch, next_hide_caption;
rtv_channel_t *channel, *last_channel;
txt_t *caption;
@@ -59,10 +61,10 @@ typedef struct rtv_setup_t {
} rtv_setup_t;
static void setup_next_channel(rtv_context_t *ctxt, unsigned ticks);
-static void * rtv_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup);
-static void rtv_destroy_context(void *context);
-static void rtv_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter);
-static void rtv_finish_frame(void *context, unsigned ticks, til_fb_fragment_t *fragment);
+static til_module_context_t * rtv_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup);
+static void rtv_destroy_context(til_module_context_t *context);
+static void rtv_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter);
+static void rtv_finish_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment);
static int rtv_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);
static rtv_setup_t rtv_default_setup = {
@@ -117,7 +119,7 @@ static void cleanup_channel(rtv_context_t *ctxt)
ctxt->channel->cumulative_time = 0;
- ctxt->channel->module_ctxt = til_module_destroy_context(ctxt->channel->module, ctxt->channel->module_ctxt);
+ ctxt->channel->module_ctxt = til_module_context_free(ctxt->channel->module_ctxt);
free(ctxt->channel->settings_as_arg);
ctxt->channel->settings_as_arg = NULL;
@@ -186,7 +188,7 @@ static void setup_next_channel(rtv_context_t *ctxt, unsigned ticks)
}
if (!ctxt->channel->module_ctxt)
- (void) til_module_create_context(ctxt->channel->module, rand(), ticks, ctxt->channel->module_setup, &ctxt->channel->module_ctxt);
+ (void) til_module_create_context(ctxt->channel->module, rand(), ticks, 0, ctxt->channel->module_setup, &ctxt->channel->module_ctxt);
ctxt->channel->last_on_time = now;
}
@@ -213,7 +215,7 @@ static int rtv_should_skip_module(const rtv_setup_t *setup, const til_module_t *
}
-static void * rtv_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * rtv_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
rtv_context_t *ctxt;
const til_module_t **modules;
@@ -230,7 +232,7 @@ static void * rtv_create_context(unsigned seed, unsigned ticks, unsigned n_cpus,
n_channels++;
}
- ctxt = calloc(1, sizeof(rtv_context_t) + n_channels * sizeof(rtv_channel_t));
+ ctxt = til_module_context_new(sizeof(rtv_context_t) + n_channels * sizeof(rtv_channel_t), seed, n_cpus);
if (!ctxt)
return NULL;
@@ -242,7 +244,7 @@ static void * rtv_create_context(unsigned seed, unsigned ticks, unsigned n_cpus,
ctxt->snow_channel.module = &rtv_none_module;
if (((rtv_setup_t *)setup)->snow_module) {
ctxt->snow_channel.module = til_lookup_module(((rtv_setup_t *)setup)->snow_module);
- (void) til_module_create_context(ctxt->snow_channel.module, rand_r(&seed), ticks, NULL, &ctxt->snow_channel.module_ctxt);
+ (void) til_module_create_context(ctxt->snow_channel.module, rand_r(&seed), ticks, 0, NULL, &ctxt->snow_channel.module_ctxt);
}
for (size_t i = 0; i < n_modules; i++) {
@@ -252,13 +254,13 @@ static void * rtv_create_context(unsigned seed, unsigned ticks, unsigned n_cpus,
setup_next_channel(ctxt, ticks);
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void rtv_destroy_context(void *context)
+static void rtv_destroy_context(til_module_context_t *context)
{
- rtv_context_t *ctxt = context;
+ rtv_context_t *ctxt = (rtv_context_t *)context;
/* TODO FIXME: cleanup better, snow module etc */
cleanup_channel(ctxt);
@@ -266,9 +268,9 @@ static void rtv_destroy_context(void *context)
}
-static void rtv_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void rtv_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- rtv_context_t *ctxt = context;
+ rtv_context_t *ctxt = (rtv_context_t *)context;
time_t now = time(NULL);
if (now >= ctxt->next_switch)
@@ -277,13 +279,13 @@ static void rtv_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, ti
if (now >= ctxt->next_hide_caption)
ctxt->caption = NULL;
- til_module_render(ctxt->channel->module, ctxt->channel->module_ctxt, ticks, fragment);
+ til_module_render(ctxt->channel->module_ctxt, ticks, fragment);
}
-static void rtv_finish_frame(void *context, unsigned ticks, til_fb_fragment_t *fragment)
+static void rtv_finish_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment)
{
- rtv_context_t *ctxt = context;
+ rtv_context_t *ctxt = (rtv_context_t *)context;
if (!ctxt->caption)
return;
diff --git a/src/modules/shapes/shapes.c b/src/modules/shapes/shapes.c
index a746f77..8a1da6e 100644
--- a/src/modules/shapes/shapes.c
+++ b/src/modules/shapes/shapes.c
@@ -57,6 +57,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#define SHAPES_DEFAULT_TYPE SHAPES_TYPE_PINWHEEL
#define SHAPES_DEFAULT_SCALE 1
@@ -76,18 +77,19 @@ typedef enum shapes_type_t {
} shapes_type_t;
typedef struct shapes_setup_t {
- til_setup_t til_setup;
- shapes_type_t type;
- float scale;
- float pinch;
- float pinch_spin;
- unsigned n_pinches;
- unsigned n_points;
- float spin;
+ til_setup_t til_setup;
+ shapes_type_t type;
+ float scale;
+ float pinch;
+ float pinch_spin;
+ unsigned n_pinches;
+ unsigned n_points;
+ float spin;
} shapes_setup_t;
typedef struct shapes_context_t {
- shapes_setup_t setup;
+ til_module_context_t til_module_context;
+ shapes_setup_t setup;
} shapes_context_t;
@@ -96,34 +98,26 @@ static shapes_setup_t shapes_default_setup = {
};
-static void * shapes_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * shapes_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
shapes_context_t *ctxt;
if (!setup)
setup = &shapes_default_setup.til_setup;
- ctxt = calloc(1, sizeof(shapes_context_t));
+ ctxt = til_module_context_new(sizeof(shapes_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
ctxt->setup = *(shapes_setup_t *)setup;
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void shapes_destroy_context(void *context)
+static void shapes_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- shapes_context_t *ctxt = context;
-
- free(ctxt);
-}
-
-
-static void shapes_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
-{
- shapes_context_t *ctxt = context;
+ shapes_context_t *ctxt = (shapes_context_t *)context;
unsigned size = MIN(fragment->width, fragment->height) * ctxt->setup.scale;
unsigned xoff = (fragment->width - size) >> 1;
unsigned yoff = (fragment->height - size) >> 1;
@@ -495,7 +489,6 @@ static int shapes_setup(const til_settings_t *settings, til_setting_t **res_sett
til_module_t shapes_module = {
.create_context = shapes_create_context,
- .destroy_context = shapes_destroy_context,
.render_fragment = shapes_render_fragment,
.setup = shapes_setup,
.name = "shapes",
diff --git a/src/modules/snow/snow.c b/src/modules/snow/snow.c
index bf0aee0..767459e 100644
--- a/src/modules/snow/snow.c
+++ b/src/modules/snow/snow.c
@@ -4,52 +4,48 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
/* Copyright (C) 2019 - Vito Caputo <vcaputo@pengaru.com> */
/* This implements white noise / snow just using rand() */
typedef union snow_seed_t {
- char __padding[256]; /* prevent seeds sharing a cache-line */
- unsigned seed;
+ char __padding[256]; /* prevent seeds sharing a cache-line */
+ unsigned seed;
} snow_seed_t;
typedef struct snow_context_t {
- unsigned unused;
- snow_seed_t seeds[];
+ til_module_context_t til_module_context;
+ unsigned unused;
+ snow_seed_t seeds[];
} snow_context_t;
-static void * snow_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * snow_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
snow_context_t *ctxt;
- ctxt = calloc(1, sizeof(snow_context_t) + n_cpus * sizeof(snow_seed_t));
+ ctxt = til_module_context_new(sizeof(snow_context_t) + n_cpus * sizeof(snow_seed_t), seed, n_cpus);
if (!ctxt)
return NULL;
for (unsigned i = 0; i < n_cpus; i++)
ctxt->seeds[i].seed = rand_r(&seed);
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void snow_destroy_context(void *context)
-{
- free(context);
-}
-
-
-static void snow_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void snow_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
*res_fragmenter = til_fragmenter_slice_per_cpu;
}
-static void snow_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void snow_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- snow_context_t *ctxt = context;
+ snow_context_t *ctxt = (snow_context_t *)context;
unsigned *seed = &ctxt->seeds[cpu].seed;
for (unsigned y = fragment->y; y < fragment->y + fragment->height; y++) {
@@ -68,7 +64,6 @@ static void snow_render_fragment(void *context, unsigned ticks, unsigned cpu, ti
til_module_t snow_module = {
.create_context = snow_create_context,
- .destroy_context = snow_destroy_context,
.prepare_frame = snow_prepare_frame,
.render_fragment = snow_render_fragment,
.name = "snow",
diff --git a/src/modules/sparkler/sparkler.c b/src/modules/sparkler/sparkler.c
index 10d8638..8c8348b 100644
--- a/src/modules/sparkler/sparkler.c
+++ b/src/modules/sparkler/sparkler.c
@@ -6,6 +6,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_util.h"
#include "particles.h"
@@ -25,6 +26,7 @@ typedef struct sparkler_setup_t {
} sparkler_setup_t;
typedef struct sparkler_context_t {
+ til_module_context_t til_module_context;
particles_t *particles;
sparkler_setup_t setup;
} sparkler_context_t;
@@ -33,14 +35,14 @@ extern particle_ops_t simple_ops;
static sparkler_setup_t sparkler_default_setup;
-static void * sparkler_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * sparkler_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
sparkler_context_t *ctxt;
if (!setup)
setup = &sparkler_default_setup.til_setup;
- ctxt = calloc(1, sizeof(sparkler_context_t));
+ ctxt = til_module_context_new(sizeof(sparkler_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
@@ -59,22 +61,22 @@ static void * sparkler_create_context(unsigned seed, unsigned ticks, unsigned n_
particles_add_particles(ctxt->particles, NULL, &simple_ops, INIT_PARTS);
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void sparkler_destroy_context(void *context)
+static void sparkler_destroy_context(til_module_context_t *context)
{
- sparkler_context_t *ctxt = context;
+ sparkler_context_t *ctxt = (sparkler_context_t *)context;
particles_free(ctxt->particles);
free(ctxt);
}
-static void sparkler_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void sparkler_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- sparkler_context_t *ctxt = context;
+ sparkler_context_t *ctxt = (sparkler_context_t *)context;
*res_fragmenter = til_fragmenter_slice_per_cpu;
@@ -88,9 +90,9 @@ static void sparkler_prepare_frame(void *context, unsigned ticks, unsigned n_cpu
/* Render a 3D particle system */
-static void sparkler_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void sparkler_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- sparkler_context_t *ctxt = context;
+ sparkler_context_t *ctxt = (sparkler_context_t *)context;
if (!ctxt->setup.show_bsp_matches)
til_fb_fragment_clear(fragment);
diff --git a/src/modules/spiro/spiro.c b/src/modules/spiro/spiro.c
index a2adb7b..39868f6 100644
--- a/src/modules/spiro/spiro.c
+++ b/src/modules/spiro/spiro.c
@@ -6,6 +6,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "draw.h"
@@ -22,19 +23,20 @@ Spirograph Emulator
*/
typedef struct spiro_context_t {
- float r;
- int r_dir;
- float p;
- int p_dir;
+ til_module_context_t til_module_context;
+ float r;
+ int r_dir;
+ float p;
+ int p_dir;
} spiro_context_t;
-static void * spiro_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * spiro_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
spiro_context_t *ctxt;
float z;
- ctxt = malloc(sizeof(spiro_context_t));
+ ctxt = til_module_context_new(sizeof(spiro_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
@@ -48,20 +50,12 @@ static void * spiro_create_context(unsigned seed, unsigned ticks, unsigned n_cpu
#ifdef DEBUG
printf("spiro: initial context: r=%f, dir=%i, p=%f, dir=%i\n", ctxt->r, ctxt->r_dir, ctxt->p, ctxt->p_dir);
#endif
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void spiro_destroy_context(void *context)
+static void spiro_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- spiro_context_t *ctxt = context;
-
- free(context);
-}
-
-
-static void spiro_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
-{
- spiro_context_t *ctxt = context;
+ spiro_context_t *ctxt = (spiro_context_t *)context;
int width = fragment->width, height = fragment->height;
@@ -145,7 +139,6 @@ static void spiro_render_fragment(void *context, unsigned ticks, unsigned cpu, t
til_module_t spiro_module = {
.create_context = spiro_create_context,
- .destroy_context = spiro_destroy_context,
.render_fragment = spiro_render_fragment,
.name = "spiro",
.description = "Spirograph emulator",
diff --git a/src/modules/stars/stars.c b/src/modules/stars/stars.c
index 5506559..31f4ead 100644
--- a/src/modules/stars/stars.c
+++ b/src/modules/stars/stars.c
@@ -10,6 +10,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_settings.h"
#include "draw.h"
@@ -18,26 +19,26 @@
#define DEFAULT_ROT_ADJ .00003
-struct points
-{
- float x, y, z;
- struct points *next;
+struct points {
+ float x, y, z;
+ struct points *next;
};
typedef struct stars_context_t {
- struct points* points;
- float rot_adj;
- float rot_rate;
- float rot_angle;
- float offset_x;
- float offset_y;
- float offset_angle;
- unsigned seed;
+ til_module_context_t til_module_context;
+ struct points* points;
+ float rot_adj;
+ float rot_rate;
+ float rot_angle;
+ float offset_x;
+ float offset_y;
+ float offset_angle;
+ unsigned seed;
} stars_context_t;
typedef struct stars_setup_t {
- til_setup_t til_setup;
- float rot_adj;
+ til_setup_t til_setup;
+ float rot_adj;
} stars_setup_t;
static stars_setup_t stars_default_setup = {
@@ -50,7 +51,7 @@ float get_random_unit_coord(unsigned *seed) {
}
-static void * stars_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * stars_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
stars_context_t *ctxt;
float z;
@@ -59,7 +60,7 @@ static void * stars_create_context(unsigned seed, unsigned ticks, unsigned n_cpu
if (!setup)
setup = &stars_default_setup.til_setup;
- ctxt = malloc(sizeof(stars_context_t));
+ ctxt = til_module_context_new(sizeof(stars_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
@@ -85,12 +86,12 @@ static void * stars_create_context(unsigned seed, unsigned ticks, unsigned n_cpu
ctxt->points = p_ptr;
}
}
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void stars_destroy_context(void *context)
+static void stars_destroy_context(til_module_context_t *context)
{
- stars_context_t *ctxt = context;
+ stars_context_t *ctxt = (stars_context_t *)context;
struct points* p_ptr;
struct points* last_ptr=NULL;
@@ -108,9 +109,9 @@ static void stars_destroy_context(void *context)
}
-static void stars_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void stars_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- stars_context_t *ctxt = context;
+ stars_context_t *ctxt = (stars_context_t *)context;
struct points* iterator;
struct points* tmp_ptr;
struct points* last_ptr=NULL;
diff --git a/src/modules/submit/submit.c b/src/modules/submit/submit.c
index fa7a95c..1573688 100644
--- a/src/modules/submit/submit.c
+++ b/src/modules/submit/submit.c
@@ -22,6 +22,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_settings.h"
#include "til_util.h"
@@ -49,12 +50,13 @@ static color_t colors[NUM_PLAYERS + 1] = {
typedef struct submit_context_t {
- grid_t *grid;
- grid_player_t *players[NUM_PLAYERS];
- uint32_t seq;
- uint32_t game_winner;
- unsigned bilerp:1;
- uint8_t cells[GRID_SIZE * GRID_SIZE];
+ til_module_context_t til_module_context;
+ grid_t *grid;
+ grid_player_t *players[NUM_PLAYERS];
+ uint32_t seq;
+ uint32_t game_winner;
+ unsigned bilerp:1;
+ uint8_t cells[GRID_SIZE * GRID_SIZE];
} submit_context_t;
typedef struct submit_setup_t {
@@ -265,36 +267,36 @@ static void setup_grid(submit_context_t *ctxt)
}
-static void * submit_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * submit_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
submit_context_t *ctxt;
if (!setup)
setup = &submit_default_setup.til_setup;
- ctxt = calloc(1, sizeof(submit_context_t));
+ ctxt = til_module_context_new(sizeof(submit_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
ctxt->bilerp = ((submit_setup_t *)setup)->bilerp;
setup_grid(ctxt);
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void submit_destroy_context(void *context)
+static void submit_destroy_context(til_module_context_t *context)
{
- submit_context_t *ctxt = context;
+ submit_context_t *ctxt = (submit_context_t *)context;
grid_free(ctxt->grid);
free(ctxt);
}
-static void submit_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void submit_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- submit_context_t *ctxt = context;
+ submit_context_t *ctxt = (submit_context_t *)context;
*res_fragmenter = til_fragmenter_tile64;
@@ -313,9 +315,9 @@ static void submit_prepare_frame(void *context, unsigned ticks, unsigned n_cpus,
}
-static void submit_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void submit_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- submit_context_t *ctxt = context;
+ submit_context_t *ctxt = (submit_context_t *)context;
if (!ctxt->bilerp)
draw_grid(ctxt, fragment);
diff --git a/src/modules/swab/swab.c b/src/modules/swab/swab.c
index e81fec9..8566fa3 100644
--- a/src/modules/swab/swab.c
+++ b/src/modules/swab/swab.c
@@ -23,22 +23,24 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_util.h"
#include "din/din.h"
typedef struct swab_context_t {
- din_t *din;
- float r;
+ til_module_context_t til_module_context;
+ din_t *din;
+ float r;
} swab_context_t;
typedef struct color_t {
- float r,g,b;
+ float r,g,b;
} color_t;
typedef struct v3f_t {
- float x, y, z;
+ float x, y, z;
} v3f_t;
@@ -64,11 +66,11 @@ static inline uint32_t color_to_uint32(color_t color) {
}
-static void * swab_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * swab_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
swab_context_t *ctxt;
- ctxt = calloc(1, sizeof(swab_context_t));
+ ctxt = til_module_context_new(sizeof(swab_context_t), seed, n_cpus);
if (!ctxt)
return NULL;
@@ -78,22 +80,22 @@ static void * swab_create_context(unsigned seed, unsigned ticks, unsigned n_cpus
return NULL;
}
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void swab_destroy_context(void *context)
+static void swab_destroy_context(til_module_context_t *context)
{
- swab_context_t *ctxt = context;
+ swab_context_t *ctxt = (swab_context_t *)context;
din_free(ctxt->din);
free(ctxt);
}
-static void swab_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void swab_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- swab_context_t *ctxt = context;
+ swab_context_t *ctxt = (swab_context_t *)context;
*res_fragmenter = til_fragmenter_tile64;
@@ -101,9 +103,9 @@ static void swab_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, t
}
-static void swab_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void swab_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- swab_context_t *ctxt = context;
+ swab_context_t *ctxt = (swab_context_t *)context;
float cos_r = cos(ctxt->r);
float sin_r = sin(ctxt->r);
float z1 = cos_r;
diff --git a/src/modules/swarm/swarm.c b/src/modules/swarm/swarm.c
index d45c926..14d82ed 100644
--- a/src/modules/swarm/swarm.c
+++ b/src/modules/swarm/swarm.c
@@ -27,6 +27,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
typedef struct v3f_t {
float x, y, z;
@@ -53,10 +54,11 @@ typedef struct swarm_setup_t {
} swarm_setup_t;
typedef struct swarm_context_t {
- v3f_t color;
- float ztweak;
- swarm_setup_t setup;
- boid_t boids[];
+ til_module_context_t til_module_context;
+ v3f_t color;
+ float ztweak;
+ swarm_setup_t setup;
+ boid_t boids[];
} swarm_context_t;
#define SWARM_SIZE (32 * 1024)
@@ -179,14 +181,14 @@ static inline uint32_t color_to_uint32(v3f_t color) {
}
-static void * swarm_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * swarm_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
swarm_context_t *ctxt;
if (!setup)
setup = &swarm_default_setup.til_setup;
- ctxt = calloc(1, sizeof(swarm_context_t) + sizeof(*(ctxt->boids)) * SWARM_SIZE);
+ ctxt = til_module_context_new(sizeof(swarm_context_t) + sizeof(*(ctxt->boids)) * SWARM_SIZE, seed, n_cpus);
if (!ctxt)
return NULL;
@@ -195,15 +197,7 @@ static void * swarm_create_context(unsigned seed, unsigned ticks, unsigned n_cpu
for (unsigned i = 0; i < SWARM_SIZE; i++)
boid_randomize(&ctxt->boids[i]);
- return ctxt;
-}
-
-
-static void swarm_destroy_context(void *context)
-{
- swarm_context_t *ctxt = context;
-
- free(ctxt);
+ return &ctxt->til_module_context;
}
@@ -407,9 +401,9 @@ static void swarm_draw_as_lines(swarm_context_t *ctxt, til_fb_fragment_t *fragme
}
-static void swarm_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void swarm_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- swarm_context_t *ctxt = context;
+ swarm_context_t *ctxt = (swarm_context_t *)context;
swarm_update(ctxt, ticks);
@@ -469,7 +463,6 @@ static int swarm_setup(const til_settings_t *settings, til_setting_t **res_setti
til_module_t swarm_module = {
.create_context = swarm_create_context,
- .destroy_context = swarm_destroy_context,
.render_fragment = swarm_render_fragment,
.setup = swarm_setup,
.name = "swarm",
diff --git a/src/modules/voronoi/voronoi.c b/src/modules/voronoi/voronoi.c
index adb180d..9365015 100644
--- a/src/modules/voronoi/voronoi.c
+++ b/src/modules/voronoi/voronoi.c
@@ -5,6 +5,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_util.h"
#include "v2f.h"
@@ -42,6 +43,7 @@ typedef struct voronoi_distances_t {
} voronoi_distances_t;
typedef struct voronoi_context_t {
+ til_module_context_t til_module_context;
unsigned seed;
voronoi_setup_t setup;
voronoi_distances_t distances;
@@ -78,14 +80,14 @@ static void voronoi_randomize(voronoi_context_t *ctxt)
}
-static void * voronoi_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
+static til_module_context_t * voronoi_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
voronoi_context_t *ctxt;
if (!setup)
setup = &voronoi_default_setup.til_setup;
- ctxt = calloc(1, sizeof(voronoi_context_t) + ((voronoi_setup_t *)setup)->n_cells * sizeof(voronoi_cell_t));
+ ctxt = til_module_context_new(sizeof(voronoi_context_t) + ((voronoi_setup_t *)setup)->n_cells * sizeof(voronoi_cell_t), seed, n_cpus);
if (!ctxt)
return NULL;
@@ -94,13 +96,13 @@ static void * voronoi_create_context(unsigned seed, unsigned ticks, unsigned n_c
voronoi_randomize(ctxt);
- return ctxt;
+ return &ctxt->til_module_context;
}
-static void voronoi_destroy_context(void *context)
+static void voronoi_destroy_context(til_module_context_t *context)
{
- voronoi_context_t *ctxt = context;
+ voronoi_context_t *ctxt = (voronoi_context_t *)context;
free(ctxt->distances.buf);
free(ctxt);
@@ -279,9 +281,9 @@ static void voronoi_sample_colors(voronoi_context_t *ctxt, til_fb_fragment_t *fr
}
-static void voronoi_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void voronoi_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
- voronoi_context_t *ctxt = context;
+ voronoi_context_t *ctxt = (voronoi_context_t *)context;
*res_fragmenter = til_fragmenter_tile64;
@@ -312,9 +314,9 @@ static void voronoi_prepare_frame(void *context, unsigned ticks, unsigned n_cpus
}
-static void voronoi_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void voronoi_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
- voronoi_context_t *ctxt = context;
+ voronoi_context_t *ctxt = (voronoi_context_t *)context;
for (int y = 0; y < fragment->height; y++) {
for (int x = 0; x < fragment->width; x++) {
diff --git a/src/til.c b/src/til.c
index 56944cb..ff7ce13 100644
--- a/src/til.c
+++ b/src/til.c
@@ -13,6 +13,7 @@
#include "til.h"
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_settings.h"
#include "til_threads.h"
#include "til_util.h"
@@ -105,13 +106,13 @@ void til_shutdown(void)
}
-static void _blank_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
+static void _blank_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
*res_fragmenter = til_fragmenter_slice_per_cpu;
}
-static void _blank_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
+static void _blank_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment)
{
til_fb_fragment_clear(fragment);
}
@@ -167,22 +168,35 @@ void til_get_modules(const til_module_t ***res_modules, size_t *res_n_modules)
}
-static void module_render_fragment(const til_module_t *module, void *context, til_threads_t *threads, unsigned ticks, til_fb_fragment_t *fragment)
+static void module_render_fragment(til_module_context_t *context, til_threads_t *threads, unsigned ticks, til_fb_fragment_t *fragment)
{
- assert(module);
+ const til_module_t *module;
+
+ assert(context);
+ assert(context->module);
assert(threads);
assert(fragment);
- if (module->prepare_frame) {
+ module = context->module;
+
+ if (context->n_cpus > 1 && module->prepare_frame) {
til_fragmenter_t fragmenter;
- module->prepare_frame(context, ticks, til_threads_num_threads(threads), fragment, &fragmenter);
+ module->prepare_frame(context, ticks, fragment, &fragmenter);
if (module->render_fragment) {
til_threads_frame_submit(threads, fragment, fragmenter, module->render_fragment, context, ticks);
til_threads_wait_idle(threads);
}
+ } else if (module->prepare_frame) {
+ til_fragmenter_t fragmenter;
+ unsigned fragnum = 0;
+ til_fb_fragment_t frag;
+ module->prepare_frame(context, ticks, fragment, &fragmenter);
+
+ while (fragmenter(context, fragment, fragnum++, &frag))
+ module->render_fragment(context, ticks, 0, &frag);
} else if (module->render_fragment)
module->render_fragment(context, ticks, 0, fragment);
@@ -196,45 +210,43 @@ static void module_render_fragment(const til_module_t *module, void *context, ti
/* This is a public interface to the threaded module rendering intended for use by
* modules that wish to get the output of other modules for their own use.
*/
-void til_module_render(const til_module_t *module, void *context, unsigned ticks, til_fb_fragment_t *fragment)
+void til_module_render(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment)
{
- module_render_fragment(module, context, til_threads, ticks, fragment);
+ module_render_fragment(context, til_threads, ticks, fragment);
}
-int til_module_create_context(const til_module_t *module, unsigned seed, unsigned ticks, til_setup_t *setup, void **res_context)
+/* if n_cpus == 0, it will be automatically set to n_threads.
+ * to explicitly set n_cpus, just pass the value. This is primarily intended for
+ * the purpose of explicitly constraining rendering parallelization to less than n_threads,
+ * if n_cpus is specified > n_threads it won't increase n_threads...
+ */
+int til_module_create_context(const til_module_t *module, unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup, til_module_context_t **res_context)
{
- void *context;
+ til_module_context_t *context;
assert(module);
assert(res_context);
+ if (!n_cpus)
+ n_cpus = til_threads_num_threads(til_threads);
+
if (!module->create_context)
- return 0;
+ context = til_module_context_new(sizeof(til_module_context_t), seed, n_cpus);
+ else
+ context = module->create_context(seed, ticks, n_cpus, setup);
- context = module->create_context(seed, ticks, til_threads_num_threads(til_threads), setup);
if (!context)
return -ENOMEM;
+ context->module = module;
+
*res_context = context;
return 0;
}
-void * til_module_destroy_context(const til_module_t *module, void *context)
-{
- assert(module);
-
- if (!module->destroy_context)
- return NULL;
-
- module->destroy_context(context);
-
- return NULL;
-}
-
-
/* select module if not yet selected, then setup the module. */
int til_module_setup(til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
@@ -336,15 +348,15 @@ int til_module_randomize_setup(const til_module_t *module, til_setup_t **res_set
}
-/* generic fragmenter using a horizontal slice per cpu according to n_cpus */
-int til_fragmenter_slice_per_cpu(void *context, unsigned n_cpus, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
+/* generic fragmenter using a horizontal slice per cpu according to context->n_cpus */
+int til_fragmenter_slice_per_cpu(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
{
- return til_fb_fragment_slice_single(fragment, n_cpus, number, res_fragment);
+ return til_fb_fragment_slice_single(fragment, context->n_cpus, number, res_fragment);
}
-/* generic fragmenter using 64x64 tiles, ignores n_cpus */
-int til_fragmenter_tile64(void *context, unsigned n_cpus, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
+/* generic fragmenter using 64x64 tiles */
+int til_fragmenter_tile64(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
{
return til_fb_fragment_tile_single(fragment, 64, number, res_fragment);
}
diff --git a/src/til.h b/src/til.h
index 91ed157..ab8f6b8 100644
--- a/src/til.h
+++ b/src/til.h
@@ -2,11 +2,12 @@
#define _TIL_H
#include "til_fb.h"
+#include "til_module_context.h"
#include "til_setup.h"
/* til_fragmenter produces fragments from an input fragment, num being the desired fragment for the current call.
* return value of 1 means a fragment has been produced, 0 means num is beyond the end of fragments. */
-typedef int (*til_fragmenter_t)(void *context, unsigned n_cpus, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment);
+typedef int (*til_fragmenter_t)(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment);
typedef struct til_settings_t settings;
typedef struct til_setting_desc_t til_setting_desc_t;
@@ -15,17 +16,17 @@ typedef struct til_knob_t til_knob_t;
#define TIL_MODULE_OVERLAYABLE 1u
typedef struct til_module_t {
- void * (*create_context)(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup);
- void (*destroy_context)(void *context);
- void (*prepare_frame)(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter);
- void (*render_fragment)(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment);
- void (*finish_frame)(void *context, unsigned ticks, til_fb_fragment_t *fragment);
- int (*setup)(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);
- size_t (*knobs)(void *context, til_knob_t **res_knobs);
- char *name;
- char *description;
- char *author;
- unsigned flags;
+ til_module_context_t * (*create_context)(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup);
+ void (*destroy_context)(til_module_context_t *context);
+ void (*prepare_frame)(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter);
+ void (*render_fragment)(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment);
+ void (*finish_frame)(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment);
+ int (*setup)(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);
+ size_t (*knobs)(til_module_context_t *context, til_knob_t **res_knobs);
+ char *name;
+ char *description;
+ char *author;
+ unsigned flags;
} til_module_t;
int til_init(void);
@@ -33,12 +34,12 @@ void til_quiesce(void);
void til_shutdown(void);
const til_module_t * til_lookup_module(const char *name);
void til_get_modules(const til_module_t ***res_modules, size_t *res_n_modules);
-void til_module_render(const til_module_t *module, void *context, unsigned ticks, til_fb_fragment_t *fragment);
-int til_module_create_context(const til_module_t *module, unsigned seed, unsigned ticks, til_setup_t *setup, void **res_context);
-void * til_module_destroy_context(const til_module_t *module, void *context);
+void til_module_render(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment);
+int til_module_create_context(const til_module_t *module, unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup, til_module_context_t **res_context);
+til_module_context_t * til_module_destroy_context(til_module_context_t *context);
int til_module_setup(til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);
int til_module_randomize_setup(const til_module_t *module, til_setup_t **res_setup, char **res_arg);
-int til_fragmenter_slice_per_cpu(void *context, unsigned n_cpus, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment);
-int til_fragmenter_tile64(void *context, unsigned n_cpus, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment);
+int til_fragmenter_slice_per_cpu(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment);
+int til_fragmenter_tile64(til_module_context_t *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment);
#endif
diff --git a/src/til_threads.c b/src/til_threads.c
index 9f5c191..a4d1709 100644
--- a/src/til_threads.c
+++ b/src/til_threads.c
@@ -22,7 +22,7 @@ typedef struct til_threads_t {
pthread_mutex_t frame_mutex;
pthread_cond_t frame_cond;
- void (*render_fragment_func)(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment);
+ void (*render_fragment_func)(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment);
void *context;
til_fb_fragment_t *fragment;
til_fragmenter_t fragmenter;
@@ -62,7 +62,7 @@ static void * thread_func(void *_thread)
frag_num = __sync_fetch_and_add(&threads->next_fragment, 1);
- if (!threads->fragmenter(threads->context, threads->n_threads, threads->fragment, frag_num, &fragment))
+ if (!threads->fragmenter(threads->context, threads->fragment, frag_num, &fragment))
break;
threads->render_fragment_func(threads->context, threads->ticks, thread->id, &fragment);
@@ -94,7 +94,7 @@ void til_threads_wait_idle(til_threads_t *threads)
/* submit a frame's fragments to the threads */
-void til_threads_frame_submit(til_threads_t *threads, til_fb_fragment_t *fragment, til_fragmenter_t fragmenter, void (*render_fragment_func)(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment), void *context, unsigned ticks)
+void til_threads_frame_submit(til_threads_t *threads, til_fb_fragment_t *fragment, til_fragmenter_t fragmenter, void (*render_fragment_func)(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment), til_module_context_t *context, unsigned ticks)
{
til_threads_wait_idle(threads); /* XXX: likely non-blocking; already happens pre page flip */
diff --git a/src/til_threads.h b/src/til_threads.h
index b55d7b8..6b854f0 100644
--- a/src/til_threads.h
+++ b/src/til_threads.h
@@ -7,7 +7,7 @@ typedef struct til_threads_t til_threads_t;
til_threads_t * til_threads_create();
void til_threads_destroy(til_threads_t *threads);
-void til_threads_frame_submit(til_threads_t *threads, til_fb_fragment_t *fragment, til_fragmenter_t fragmenter, void (*render_fragment_func)(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment), void *context, unsigned ticks);
+void til_threads_frame_submit(til_threads_t *threads, til_fb_fragment_t *fragment, til_fragmenter_t fragmenter, void (*render_fragment_func)(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment), til_module_context_t *context, unsigned ticks);
void til_threads_wait_idle(til_threads_t *threads);
unsigned til_threads_num_threads(til_threads_t *threads);
© All Rights Reserved