diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2022-05-21 23:27:02 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2022-05-21 23:27:02 -0700 |
commit | c1fed8c508dd89c86de7dd647de0d014c441344e (patch) | |
tree | fac4d327c18fe5a2a205a2f4b4ad7f8287ec3536 /src/modules | |
parent | e21cdb67718a5d203372fd0c425e8be3e1d273f3 (diff) |
modules/*: first stab at utilizing supplied seeds
This is a mostly mechanical change of using rand_r() in place of
rand(), using the provided seed as the seed state.
There's some outstanding rand()s outside of create_context()
which should probably get switched over, with the seed being
stowed in the context struct. I didn't bother going deeper on
this at the moment in the interests of getting to sleep soon.
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/compose/compose.c | 4 | ||||
-rw-r--r-- | src/modules/julia/julia.c | 2 | ||||
-rw-r--r-- | src/modules/meta2d/meta2d.c | 3 | ||||
-rw-r--r-- | src/modules/montage/montage.c | 2 | ||||
-rw-r--r-- | src/modules/plasma/plasma.c | 2 | ||||
-rw-r--r-- | src/modules/roto/roto.c | 4 | ||||
-rw-r--r-- | src/modules/rtv/rtv.c | 2 | ||||
-rw-r--r-- | src/modules/snow/snow.c | 8 | ||||
-rw-r--r-- | src/modules/spiro/spiro.c | 4 | ||||
-rw-r--r-- | src/modules/stars/stars.c | 18 | ||||
-rw-r--r-- | src/modules/voronoi/voronoi.c | 12 |
11 files changed, 33 insertions, 28 deletions
diff --git a/src/modules/compose/compose.c b/src/modules/compose/compose.c index b0f0104..f55e516 100644 --- a/src/modules/compose/compose.c +++ b/src/modules/compose/compose.c @@ -90,7 +90,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(), ticks, layer_setup, &ctxt->layers[i].module_ctxt); + (void) til_module_create_context(layer_module, rand_r(&seed), ticks, layer_setup, &ctxt->layers[i].module_ctxt); til_setup_free(layer_setup); ctxt->n_layers++; @@ -102,7 +102,7 @@ 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(), ticks, texture_setup, &ctxt->texture.module_ctxt); + (void) til_module_create_context(ctxt->texture.module, rand_r(&seed), ticks, texture_setup, &ctxt->texture.module_ctxt); til_setup_free(texture_setup); } diff --git a/src/modules/julia/julia.c b/src/modules/julia/julia.c index c24e40a..d9cfdff 100644 --- a/src/modules/julia/julia.c +++ b/src/modules/julia/julia.c @@ -73,7 +73,7 @@ static void * julia_create_context(unsigned seed, unsigned ticks, unsigned n_cpu if (!ctxt) return NULL; - ctxt->rr = ((float)rand()) / (float)RAND_MAX * 100.f; + ctxt->rr = ((float)rand_r(&seed)) / (float)RAND_MAX * 100.f; return ctxt; } diff --git a/src/modules/meta2d/meta2d.c b/src/modules/meta2d/meta2d.c index 8b87804..d1063de 100644 --- a/src/modules/meta2d/meta2d.c +++ b/src/modules/meta2d/meta2d.c @@ -78,8 +78,9 @@ static void * meta2d_create_context(unsigned seed, unsigned ticks, unsigned n_cp for (int i = 0; i < META2D_NUM_BALLS; i++) { meta2d_ball_t *ball = &ctxt->balls[i]; + /* TODO: add _r() variants of v[23]f_rand()? */ v2f_rand(&ball->position, &(v2f_t){-.7f, -.7f}, &(v2f_t){.7f, .7f}); - ball->radius = rand() / (float)RAND_MAX * .2f + .05f; + ball->radius = rand_r(&seed) / (float)RAND_MAX * .2f + .05f; v3f_rand(&ball->color, &(v3f_t){0.f, 0.f, 0.f}, &(v3f_t){1.f, 1.f, 1.f}); } diff --git a/src/modules/montage/montage.c b/src/modules/montage/montage.c index a7030ff..1b79de5 100644 --- a/src/modules/montage/montage.c +++ b/src/modules/montage/montage.c @@ -90,7 +90,7 @@ 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(), ticks, 1, setup); + ctxt->contexts[i] = module->create_context(rand_r(&seed), ticks, 1, setup); til_setup_free(setup); } diff --git a/src/modules/plasma/plasma.c b/src/modules/plasma/plasma.c index aca4e85..dfcbd15 100644 --- a/src/modules/plasma/plasma.c +++ b/src/modules/plasma/plasma.c @@ -66,7 +66,7 @@ static void * plasma_create_context(unsigned seed, unsigned ticks, unsigned n_cp if (!ctxt) return NULL; - ctxt->rr = rand(); + ctxt->rr = rand_r(&seed); return ctxt; } diff --git a/src/modules/roto/roto.c b/src/modules/roto/roto.c index 0969929..8908bd2 100644 --- a/src/modules/roto/roto.c +++ b/src/modules/roto/roto.c @@ -39,8 +39,8 @@ static void * roto_create_context(unsigned seed, unsigned ticks, unsigned n_cpus if (!ctxt) return NULL; - ctxt->r = rand(); - ctxt->rr = rand(); + ctxt->r = rand_r(&seed); + ctxt->rr = rand_r(&seed); return ctxt; } diff --git a/src/modules/rtv/rtv.c b/src/modules/rtv/rtv.c index 6cd9744..3e789ab 100644 --- a/src/modules/rtv/rtv.c +++ b/src/modules/rtv/rtv.c @@ -242,7 +242,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(), ticks, NULL, &ctxt->snow_channel.module_ctxt); + (void) til_module_create_context(ctxt->snow_channel.module, rand_r(&seed), ticks, NULL, &ctxt->snow_channel.module_ctxt); } for (size_t i = 0; i < n_modules; i++) { diff --git a/src/modules/snow/snow.c b/src/modules/snow/snow.c index 82ad847..bf0aee0 100644 --- a/src/modules/snow/snow.c +++ b/src/modules/snow/snow.c @@ -10,8 +10,8 @@ /* This implements white noise / snow just using rand() */ typedef union snow_seed_t { - char __padding[256]; /* prevent seeds sharing a cache-line */ - int seed; + char __padding[256]; /* prevent seeds sharing a cache-line */ + unsigned seed; } snow_seed_t; typedef struct snow_context_t { @@ -29,7 +29,7 @@ static void * snow_create_context(unsigned seed, unsigned ticks, unsigned n_cpus return NULL; for (unsigned i = 0; i < n_cpus; i++) - ctxt->seeds[i].seed = rand(); + ctxt->seeds[i].seed = rand_r(&seed); return ctxt; } @@ -50,7 +50,7 @@ static void snow_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, t static void snow_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment) { snow_context_t *ctxt = context; - int *seed = &ctxt->seeds[cpu].seed; + unsigned *seed = &ctxt->seeds[cpu].seed; for (unsigned y = fragment->y; y < fragment->y + fragment->height; y++) { for (unsigned x = fragment->x; x < fragment->x + fragment->width; x++) { diff --git a/src/modules/spiro/spiro.c b/src/modules/spiro/spiro.c index 07d9fbf..a2adb7b 100644 --- a/src/modules/spiro/spiro.c +++ b/src/modules/spiro/spiro.c @@ -38,12 +38,12 @@ static void * spiro_create_context(unsigned seed, unsigned ticks, unsigned n_cpu if (!ctxt) return NULL; - ctxt->r=.25f+(rand()/(float)RAND_MAX)*.5f; + ctxt->r=.25f+(rand_r(&seed)/(float)RAND_MAX)*.5f; if(ctxt->r>.5f) ctxt->r_dir=-1; else ctxt->r_dir=1; - ctxt->p=(rand()/(float)RAND_MAX)*ctxt->r; + ctxt->p=(rand_r(&seed)/(float)RAND_MAX)*ctxt->r; ctxt->p_dir=ctxt->r_dir*-1; #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); diff --git a/src/modules/stars/stars.c b/src/modules/stars/stars.c index 21ae7f5..5506559 100644 --- a/src/modules/stars/stars.c +++ b/src/modules/stars/stars.c @@ -32,6 +32,7 @@ typedef struct stars_context_t { float offset_x; float offset_y; float offset_angle; + unsigned seed; } stars_context_t; typedef struct stars_setup_t { @@ -44,8 +45,8 @@ static stars_setup_t stars_default_setup = { }; -float get_random_unit_coord() { - return (((float)rand()/(float)RAND_MAX)*2.0)-1.0; +float get_random_unit_coord(unsigned *seed) { + return (((float)rand_r(seed)/(float)RAND_MAX)*2.0)-1.0; } @@ -63,6 +64,7 @@ static void * stars_create_context(unsigned seed, unsigned ticks, unsigned n_cpu return NULL; ctxt->points = NULL; + ctxt->seed = seed; ctxt->rot_adj = ((stars_setup_t *)setup)->rot_adj; ctxt->rot_rate = 0.00; ctxt->rot_angle = 0; @@ -72,12 +74,12 @@ static void * stars_create_context(unsigned seed, unsigned ticks, unsigned n_cpu //add a bunch of points for(z=0.01; z<1; z=z+0.01) { - for(int i=0; i<rand()%16; i++){ + for(int i=0; i<rand_r(&ctxt->seed)%16; i++){ p_ptr = malloc(sizeof(struct points)); if (!p_ptr) return NULL; - p_ptr->x = get_random_unit_coord(); - p_ptr->y = get_random_unit_coord(); + p_ptr->x = get_random_unit_coord(&ctxt->seed); + p_ptr->y = get_random_unit_coord(&ctxt->seed); p_ptr->z = z; p_ptr->next = ctxt->points; ctxt->points = p_ptr; @@ -185,12 +187,12 @@ static void stars_render_fragment(void *context, unsigned ticks, unsigned cpu, t } // add stars at horizon - for(int i=0; i<rand()%16; i++){ + for(int i=0; i<rand_r(&ctxt->seed)%16; i++){ tmp_ptr = malloc(sizeof(struct points)); if (!tmp_ptr) break; - tmp_ptr->x = get_random_unit_coord(); - tmp_ptr->y = get_random_unit_coord(); + tmp_ptr->x = get_random_unit_coord(&ctxt->seed); + tmp_ptr->y = get_random_unit_coord(&ctxt->seed); tmp_ptr->z = 0.01; tmp_ptr->next = ctxt->points; ctxt->points = tmp_ptr; diff --git a/src/modules/voronoi/voronoi.c b/src/modules/voronoi/voronoi.c index 0a9ba70..adb180d 100644 --- a/src/modules/voronoi/voronoi.c +++ b/src/modules/voronoi/voronoi.c @@ -42,6 +42,7 @@ typedef struct voronoi_distances_t { } voronoi_distances_t; typedef struct voronoi_context_t { + unsigned seed; voronoi_setup_t setup; voronoi_distances_t distances; voronoi_cell_t cells[]; @@ -67,12 +68,12 @@ static void voronoi_randomize(voronoi_context_t *ctxt) for (size_t i = 0; i < ctxt->setup.n_cells; i++) { voronoi_cell_t *p = &ctxt->cells[i]; - p->origin.x = ((float)rand() * inv_rand_max) * 2.f - 1.f; - p->origin.y = ((float)rand() * 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; - p->color = ((uint32_t)(rand() % 256)) << 16; - p->color |= ((uint32_t)(rand() % 256)) << 8; - p->color |= ((uint32_t)(rand() % 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)); } } @@ -89,6 +90,7 @@ static void * voronoi_create_context(unsigned seed, unsigned ticks, unsigned n_c return NULL; ctxt->setup = *(voronoi_setup_t *)setup; + ctxt->seed = seed; voronoi_randomize(ctxt); |