From 6b8790a22c38e0d5419eb5a4dac5e30f684ae473 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 1 Apr 2022 17:47:19 -0700 Subject: modules/*: instantiate and use setups Now modules allocate and return an opaque setup pointer in res_setup when they implement a setup method. Defaults are utilized when ${module}_create_context() receives a NULL setup. The default setup used in this case should match the defaults/preferred values emitted by the module's setup method. But performing setup should always be optional, so a NULL setup provided to create_context() is to be expected. No cleanup of these setup instances is currently performed, so it's a small memory leak for now. Since these are opaque and may contain nested references to other allocations, simply using free() somewhere in the frontend is insufficient. There will probably need to be something like a til_module_t.setup_free() method added in the future which modules may assign libc's free() to when appropriate, or their own more elaborate version. Lifecycle for the settings is very simple; the setup method returns an instance, the caller is expected to free it when no longer needed (once free is implemented). The create_context consumer of a given setup must make its own copy of the settings if necessary, and may not keep a reference - it must assume the setup will be freed immediately after create_context() returns. This enables the ability to reuse a setup instance across multiple create_context() calls if desired, one can imagine something like running the same module with the same settings multiple times across multiple displays for instance. If the module has significant entropy the output will differ despite being configured identically... With this commit one may change settings for any of the modules *while* the modules are actively rendering a given context, and the settings should *not* be visible. They should only affect the context they're supplied to. --- src/modules/sparkler/sparkler.c | 73 +++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 24 deletions(-) (limited to 'src/modules/sparkler') diff --git a/src/modules/sparkler/sparkler.c b/src/modules/sparkler/sparkler.c index 51184b1..272c0fd 100644 --- a/src/modules/sparkler/sparkler.c +++ b/src/modules/sparkler/sparkler.c @@ -16,20 +16,31 @@ #define INIT_PARTS 100 +typedef struct sparkler_setup_t { + unsigned show_bsp_leafs:1; + unsigned show_bsp_matches:1; + unsigned show_bsp_matches_affected_only:1; + unsigned show_bsp_leafs_min_depth; +} sparkler_setup_t; + typedef struct sparkler_context_t { - particles_t *particles; - unsigned n_cpus; + particles_t *particles; + unsigned n_cpus; + sparkler_setup_t setup; } sparkler_context_t; extern particle_ops_t simple_ops; -static particles_conf_t sparkler_conf; +static sparkler_setup_t sparkler_default_setup; static void * sparkler_create_context(unsigned ticks, unsigned num_cpus, void *setup) { static int initialized; sparkler_context_t *ctxt; + if (!setup) + setup = &sparkler_default_setup; + if (!initialized) { srand(time(NULL) + getpid()); initialized = 1; @@ -39,7 +50,14 @@ static void * sparkler_create_context(unsigned ticks, unsigned num_cpus, void *s if (!ctxt) return NULL; - ctxt->particles = particles_new(&sparkler_conf); + ctxt->setup = *(sparkler_setup_t *)setup; + + ctxt->particles = particles_new(&(particles_conf_t){ + .show_bsp_leafs = ((sparkler_setup_t *)setup)->show_bsp_leafs, + .show_bsp_matches = ((sparkler_setup_t *)setup)->show_bsp_matches, + .show_bsp_leafs_min_depth = ((sparkler_setup_t *)setup)->show_bsp_leafs_min_depth, + .show_bsp_matches_affected_only = ((sparkler_setup_t *)setup)->show_bsp_matches_affected_only, + }); if (!ctxt->particles) { free(ctxt); return NULL; @@ -74,7 +92,7 @@ static void sparkler_prepare_frame(void *context, unsigned ticks, unsigned ncpus *res_fragmenter = sparkler_fragmenter; ctxt->n_cpus = ncpus; - if (sparkler_conf.show_bsp_matches) + if (ctxt->setup.show_bsp_matches) til_fb_fragment_zero(fragment); particles_sim(ctxt->particles, fragment); @@ -88,7 +106,7 @@ static void sparkler_render_fragment(void *context, unsigned ticks, unsigned cpu { sparkler_context_t *ctxt = context; - if (!sparkler_conf.show_bsp_matches) + if (!ctxt->setup.show_bsp_matches) til_fb_fragment_zero(fragment); particles_draw(ctxt->particles, fragment); @@ -99,7 +117,9 @@ static void sparkler_render_fragment(void *context, unsigned ticks, unsigned cpu static int sparkler_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, void **res_setup) { const char *show_bsp_leafs; + const char *show_bsp_leafs_min_depth; const char *show_bsp_matches; + const char *show_bsp_matches_affected_only; const char *values[] = { "off", "on", @@ -131,9 +151,6 @@ static int sparkler_setup(const til_settings_t *settings, til_setting_t **res_se "10", NULL }; - const char *show_bsp_leafs_min_depth; - - sparkler_conf.show_bsp_leafs = 1; r = til_settings_get_and_describe_value(settings, &(til_setting_desc_t){ @@ -147,10 +164,6 @@ static int sparkler_setup(const til_settings_t *settings, til_setting_t **res_se res_desc); if (r) return r; - - sscanf(show_bsp_leafs_min_depth, "%u", &sparkler_conf.show_bsp_leafs_min_depth); - } else { - sparkler_conf.show_bsp_leafs = 0; } r = til_settings_get_and_describe_value(settings, @@ -166,14 +179,7 @@ static int sparkler_setup(const til_settings_t *settings, til_setting_t **res_se if (r) return r; - if (!strcasecmp(show_bsp_matches, "on")) - sparkler_conf.show_bsp_matches = 1; - else - sparkler_conf.show_bsp_matches = 0; - if (!strcasecmp(show_bsp_matches, "on")) { - const char *show_bsp_matches_affected_only; - r = til_settings_get_and_describe_value(settings, &(til_setting_desc_t){ .name = "Show only narrow-phase affected match results", @@ -187,10 +193,29 @@ static int sparkler_setup(const til_settings_t *settings, til_setting_t **res_se if (r) return r; - if (!strcasecmp(show_bsp_matches_affected_only, "on")) - sparkler_conf.show_bsp_matches_affected_only = 1; - else - sparkler_conf.show_bsp_matches_affected_only = 0; + } + + if (res_setup) { + sparkler_setup_t *setup; + + setup = calloc(1, sizeof(*setup)); + if (!setup) + return -ENOMEM; + + if (!strcasecmp(show_bsp_leafs, "on")) { + setup->show_bsp_leafs = 1; + + sscanf(show_bsp_leafs_min_depth, "%u", &setup->show_bsp_leafs_min_depth); + } + + if (!strcasecmp(show_bsp_matches, "on")) { + setup->show_bsp_matches = 1; + + if (!strcasecmp(show_bsp_matches_affected_only, "on")) + setup->show_bsp_matches_affected_only = 1; + } + + *res_setup = setup; } return 0; -- cgit v1.2.3