From fead6a99d3fe4be0c1b654ed3f8fac10f5aff204 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sun, 28 May 2023 00:01:15 -0700 Subject: modules/voronoi: make cell color randomization conditional When voronoi is overlayed the colors get sampled, so randomizing them is pointless work every frame. --- src/modules/voronoi/voronoi.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/src/modules/voronoi/voronoi.c b/src/modules/voronoi/voronoi.c index 44e3f7c..5b86746 100644 --- a/src/modules/voronoi/voronoi.c +++ b/src/modules/voronoi/voronoi.c @@ -56,19 +56,33 @@ typedef struct voronoi_context_t { #define VORONOI_DEFAULT_RANDOMIZE 0 -static void voronoi_randomize(voronoi_context_t *ctxt) +/* TODO: stuff like this makes me think there needs to be support for threaded prepare_frame(), + * since this could just have per-cpu lists of cells and per-cpu rand_r seeds which could make + * a significant difference for large numbers of cells. + */ +static void voronoi_randomize(voronoi_context_t *ctxt, int do_colors) { float inv_rand_max= 1.f / (float)RAND_MAX; - for (size_t i = 0; i < ctxt->setup->n_cells; i++) { - voronoi_cell_t *p = &ctxt->cells[i]; + if (!do_colors) { + /* we can skip setting the colors when overlayed since they get sampled */ + for (size_t i = 0; i < ctxt->setup->n_cells; i++) { + voronoi_cell_t *p = &ctxt->cells[i]; - p->origin.x = ((float)rand_r(&ctxt->seed) * inv_rand_max) * 2.f - 1.f; - p->origin.y = ((float)rand_r(&ctxt->seed) * inv_rand_max) * 2.f - 1.f; + p->origin.x = ((float)rand_r(&ctxt->seed) * inv_rand_max) * 2.f - 1.f; + p->origin.y = ((float)rand_r(&ctxt->seed) * inv_rand_max) * 2.f - 1.f; + } + } else { + for (size_t i = 0; i < ctxt->setup->n_cells; i++) { + voronoi_cell_t *p = &ctxt->cells[i]; + + p->origin.x = ((float)rand_r(&ctxt->seed) * inv_rand_max) * 2.f - 1.f; + p->origin.y = ((float)rand_r(&ctxt->seed) * inv_rand_max) * 2.f - 1.f; - p->color = ((uint32_t)(rand_r(&ctxt->seed) % 256)) << 16; - p->color |= ((uint32_t)(rand_r(&ctxt->seed) % 256)) << 8; - p->color |= ((uint32_t)(rand_r(&ctxt->seed) % 256)); + p->color = ((uint32_t)(rand_r(&ctxt->seed) % 256)) << 16; + p->color |= ((uint32_t)(rand_r(&ctxt->seed) % 256)) << 8; + p->color |= ((uint32_t)(rand_r(&ctxt->seed) % 256)); + } } ctxt->distances.recalc_needed = 1; @@ -86,7 +100,7 @@ static til_module_context_t * voronoi_create_context(const til_module_t *module, ctxt->setup = (voronoi_setup_t *)setup; ctxt->seed = seed; - voronoi_randomize(ctxt); + voronoi_randomize(ctxt, 1); return &ctxt->til_module_context; } @@ -300,7 +314,7 @@ static void voronoi_prepare_frame(til_module_context_t *context, til_stream_t *s } if (ctxt->setup->randomize) - voronoi_randomize(ctxt); + voronoi_randomize(ctxt, !fragment->cleared); /* if the fragment comes in already cleared/initialized, use it for the colors, producing a mosaic */ if (fragment->cleared) -- cgit v1.2.3