1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
|
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "til.h"
#include "til_fb.h"
#include "til_module_context.h"
#include "til_settings.h"
#include "til_stream.h"
#include "til_tap.h"
#include "til_util.h"
/* Copyright (C) 2023 - Vito Caputo <vcaputo@pengaru.com> */
/* This implements a rudimentary mixing module for things like fades */
/*
* TODO:
* - make interlace line granularity configurable instead of always 1 pixel
*/
typedef enum mixer_style_t {
MIXER_STYLE_BLEND,
MIXER_STYLE_FLICKER,
MIXER_STYLE_INTERLACE,
MIXER_STYLE_PAINTROLLER,
} mixer_style_t;
typedef enum mixer_orientation_t {
MIXER_ORIENTATION_HORIZONTAL,
MIXER_ORIENTATION_VERTICAL,
} mixer_orientation_t;
typedef struct mixer_input_t {
til_module_context_t *module_ctxt;
/* XXX: it's expected that inputs will get more settable attributes to stick in here */
} mixer_input_t;
typedef struct mixer_seed_t {
char __padding[256]; /* prevent seeds sharing a cache line */
unsigned state;
} mixer_seed_t;
typedef struct mixer_context_t {
til_module_context_t til_module_context;
struct {
til_tap_t T;
} taps;
struct {
float T;
} vars;
float *T;
mixer_input_t inputs[2];
til_fb_fragment_t *snapshots[2];
mixer_seed_t seeds[];
} mixer_context_t;
typedef struct mixer_setup_input_t {
til_setup_t *setup;
} mixer_setup_input_t;
typedef struct mixer_setup_t {
til_setup_t til_setup;
mixer_style_t style;
mixer_setup_input_t inputs[2];
mixer_orientation_t orientation;
unsigned n_passes;
} mixer_setup_t;
#define MIXER_DEFAULT_STYLE MIXER_STYLE_BLEND
#define MIXER_DEFAULT_PASSES 8
#define MIXER_DEFAULT_ORIENTATION MIXER_ORIENTATION_VERTICAL
static void mixer_update_taps(mixer_context_t *ctxt, til_stream_t *stream, unsigned ticks)
{
if (!til_stream_tap_context(stream, &ctxt->til_module_context, NULL, &ctxt->taps.T))
*ctxt->T = cosf(til_ticks_to_rads(ticks)) * .5f + .5f;
else /* we're not driving the tap, so let's update our local copy just once */
ctxt->vars.T = *ctxt->T; /* FIXME: taps need synchronization/thread-safe details fleshed out / atomics */
}
static til_module_context_t * mixer_create_context(const til_module_t *module, til_stream_t *stream, unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup)
{
mixer_setup_t *s = (mixer_setup_t *)setup;
mixer_context_t *ctxt;
int r;
assert(setup);
ctxt = til_module_context_new(module, sizeof(mixer_context_t) * (sizeof(mixer_seed_t) * n_cpus), stream, seed, ticks, n_cpus, setup);
if (!ctxt)
return NULL;
for (size_t i = 0; i < nelems(s->inputs); i++) {
const til_module_t *input_module;
input_module = ((mixer_setup_t *)setup)->inputs[i].setup->creator;
r = til_module_create_context(input_module, stream, rand_r(&seed), ticks, n_cpus, s->inputs[i].setup, &ctxt->inputs[i].module_ctxt);
if (r < 0)
return til_module_context_free(&ctxt->til_module_context);
}
ctxt->taps.T = til_tap_init_float(ctxt, &ctxt->T, 1, &ctxt->vars.T, "T");
mixer_update_taps(ctxt, stream, ticks);
return &ctxt->til_module_context;
}
static void mixer_destroy_context(til_module_context_t *context)
{
mixer_context_t *ctxt = (mixer_context_t *)context;
for (size_t i = 0; i < nelems(ctxt->inputs); i++)
til_module_context_free(ctxt->inputs[i].module_ctxt);
free(context);
}
static inline float randf(unsigned *seed)
{
return 1.f / ((float)RAND_MAX) * rand_r(seed);
}
static void mixer_prepare_frame(til_module_context_t *context, til_stream_t *stream, unsigned ticks, til_fb_fragment_t **fragment_ptr, til_frame_plan_t *res_frame_plan)
{
mixer_context_t *ctxt = (mixer_context_t *)context;
til_fb_fragment_t *fragment = *fragment_ptr;
size_t i = 0;
*res_frame_plan = (til_frame_plan_t){ .fragmenter = til_fragmenter_slice_per_cpu_x16 };
mixer_update_taps(ctxt, stream, ticks);
switch (((mixer_setup_t *)context->setup)->style) {
case MIXER_STYLE_FLICKER:
if (randf(&context->seed) < ctxt->vars.T)
i = 1;
else
i = 0;
til_module_render(ctxt->inputs[i].module_ctxt, stream, ticks, &fragment);
break;
case MIXER_STYLE_INTERLACE:
for (int i = 0; i < context->n_cpus; i++)
ctxt->seeds[i].state = rand_r(&context->seed);
/* fallthrough */
case MIXER_STYLE_PAINTROLLER: {
float T = ctxt->vars.T;
/* INTERLACE and PAINTROLLER progressively overlay b_module output atop a_module,
* so we can render b_module into the fragment first. Only when (T < 1) do we
* have to snapshot that then render a_module into the fragment, then the snapshot
* of b_module's output can be copied from to overlay the progression.
*/
if (T > .001f) {
til_module_render(ctxt->inputs[1].module_ctxt, stream, ticks, &fragment);
if (T < .999f)
ctxt->snapshots[1] = til_fb_fragment_snapshot(&fragment, 0);
}
if (T < .999f)
til_module_render(ctxt->inputs[0].module_ctxt, stream, ticks, &fragment);
break;
}
case MIXER_STYLE_BLEND: {
float T = ctxt->vars.T;
/* BLEND needs *both* contexts rendered and snapshotted for blending,
* except when at the start/end points for T. It's the most costly
* style to perform.
*/
if (T < .999f) {
til_module_render(ctxt->inputs[0].module_ctxt, stream, ticks, &fragment);
if (T > 0.001f)
ctxt->snapshots[0] = til_fb_fragment_snapshot(&fragment, 0);
}
if (T > 0.001f) {
til_module_render(ctxt->inputs[1].module_ctxt, stream, ticks, &fragment);
if (T < .999f)
ctxt->snapshots[1] = til_fb_fragment_snapshot(&fragment, 0);
}
break;
}
default:
assert(0);
}
*fragment_ptr = fragment;
}
/* derived from modules/drizzle pixel_mult_scalar(), there's definitely room for optimizations */
static inline uint32_t pixels_lerp(uint32_t a_pixel, uint32_t b_pixel, float one_sub_T, float T)
{
uint32_t pixel;
float a, b;
/* r */
a = ((uint8_t)(a_pixel >> 16));
a *= one_sub_T;
b = ((uint8_t)(b_pixel >> 16));
b *= T;
pixel = (((uint32_t)(a+b)) << 16);
/* g */
a = ((uint8_t)(a_pixel >> 8));
a *= one_sub_T;
b = ((uint8_t)(b_pixel >> 8));
b *= T;
pixel |= (((uint32_t)(a+b)) << 8);
/* b */
a = ((uint8_t)a_pixel);
a *= one_sub_T;
b = ((uint8_t)b_pixel);
b *= T;
pixel |= ((uint32_t)(a+b));
return pixel;
}
static void mixer_render_fragment(til_module_context_t *context, til_stream_t *stream, unsigned ticks, unsigned cpu, til_fb_fragment_t **fragment_ptr)
{
mixer_context_t *ctxt = (mixer_context_t *)context;
til_fb_fragment_t *fragment = *fragment_ptr;
switch (((mixer_setup_t *)context->setup)->style) {
case MIXER_STYLE_FLICKER:
/* handled in prepare_frame() */
break;
case MIXER_STYLE_BLEND: {
uint32_t *dest = fragment->buf;
til_fb_fragment_t *snapshot_a, *snapshot_b;
uint32_t *a, *b;
float T = ctxt->vars.T;
float one_sub_T = 1.f - T;
if (T <= 0.001f || T >= .999f)
break;
assert(ctxt->snapshots[0]);
assert(ctxt->snapshots[1]);
snapshot_a = ctxt->snapshots[0];
snapshot_b = ctxt->snapshots[1];
a = snapshot_a->buf + (fragment->y - snapshot_a->y) * snapshot_a->pitch + (fragment->x - snapshot_a->x);
b = snapshot_b->buf + (fragment->y - snapshot_b->y) * snapshot_b->pitch + (fragment->x - snapshot_b->x);
/* for the tweens, we already have snapshots sitting in ctxt->snapshots[],
* which we now interpolate the pixels out of in parallel
*/
for (unsigned y = 0, h = fragment->height, w = fragment->width; y < h; y++) {
unsigned x = 0;
/* go four-wide if there's enough, note even without SSE this is a bit quicker a la unrolled loop */
if ((w & ~3U)) {
for (; x < (w & ~3U); x += 4) {
/* TODO: explore adding a SIMD/SSE implementation, this is an ideal application for it */
*dest = pixels_lerp(*a, *b, one_sub_T, T);
dest++;
a++;
b++;
*dest = pixels_lerp(*a, *b, one_sub_T, T);
dest++;
a++;
b++;
*dest = pixels_lerp(*a, *b, one_sub_T, T);
dest++;
a++;
b++;
*dest = pixels_lerp(*a, *b, one_sub_T, T);
dest++;
a++;
b++;
}
}
/* pick up any tail pixels */
for (; x < w; a++, b++, dest++, x++)
*dest = pixels_lerp(*a, *b, one_sub_T, T);
a += snapshot_a->pitch - w; /* things are a little awkward because we're fragmenting a threaded render within what was snapshotted */
b += snapshot_b->pitch - w;
dest += fragment->stride;
}
break;
}
case MIXER_STYLE_INTERLACE: {
til_fb_fragment_t *snapshot_b;
float T = ctxt->vars.T;
if (T <= 0.001f || T >= .999f)
break;
assert(ctxt->snapshots[1]);
snapshot_b = ctxt->snapshots[1];
for (unsigned y = 0; y < fragment->height; y++) {
float r = randf(&ctxt->seeds[cpu].state);
if (r < T)
til_fb_fragment_copy(fragment, 0, fragment->x, fragment->y + y, fragment->width, 1, snapshot_b);
}
break;
}
case MIXER_STYLE_PAINTROLLER: {
mixer_orientation_t orientation = ((mixer_setup_t *)context->setup)->orientation;
unsigned n_passes = ((mixer_setup_t *)context->setup)->n_passes;
til_fb_fragment_t *snapshot_b;
float T = ctxt->vars.T;
float div = 1.f / (float)n_passes;
unsigned iwhole = T * n_passes;
float frac = T * n_passes - iwhole;
if (T <= 0.001f || T >= .999f)
break;
assert(ctxt->snapshots[1]);
snapshot_b = ctxt->snapshots[1];
/* There are two rects to compute:
* 1. the whole "rolled" area already transitioned
* 2. the in-progress fractional area being rolled
*
* The simple thing to do is just compute those two in two steps,
* and clip their rects to the fragment rect and copy b->fragment
* clipped by the result, for each step. til_fb_fragment_copy()
* should clip to the dest fragment for us, so this is rather
* trivial.
*/
switch (orientation) {
case MIXER_ORIENTATION_HORIZONTAL: {
float row_h = ((float)fragment->frame_height * div);
unsigned whole_w = fragment->frame_width;
unsigned whole_h = ceilf(row_h * (float)iwhole);
unsigned frac_w = ((float)fragment->frame_width * frac);
unsigned frac_h = row_h;
til_fb_fragment_copy(fragment, 0, 0, 0, whole_w, whole_h, snapshot_b);
til_fb_fragment_copy(fragment, 0, 0, whole_h, frac_w, frac_h, snapshot_b);
break;
}
case MIXER_ORIENTATION_VERTICAL: {
float col_w = ((float)fragment->frame_width * div);
unsigned whole_w = ceilf(col_w * (float)iwhole);
unsigned whole_h = fragment->frame_height;
unsigned frac_w = col_w;
unsigned frac_h = ((float)fragment->frame_height * frac);
til_fb_fragment_copy(fragment, 0, 0, 0, whole_w, whole_h, snapshot_b);
til_fb_fragment_copy(fragment, 0, whole_w, 0, frac_w, frac_h, snapshot_b);
break;
}
default:
assert(0);
}
/* progressively transition from a->b via incremental striping */
break;
}
default:
assert(0);
}
*fragment_ptr = fragment;
}
static int mixer_finish_frame(til_module_context_t *context, til_stream_t *stream, unsigned int ticks, til_fb_fragment_t **fragment_ptr)
{
mixer_context_t *ctxt = (mixer_context_t *)context;
for (int i = 0; i < 2; i++) {
if (!ctxt->snapshots[i])
continue;
ctxt->snapshots[i] = til_fb_fragment_reclaim(ctxt->snapshots[i]);
}
return 0;
}
static char * mixer_random_module_setting(unsigned seed)
{
const char *candidates[] = {
"blinds",
"checkers",
"drizzle",
"julia",
"meta2d",
"moire",
"pixbounce",
"plasma",
"plato",
"roto",
"shapes",
"snow",
"sparkler",
"spiro",
"stars",
"submit",
"swab",
"swarm",
"voronoi",
};
return strdup(candidates[rand() % nelems(candidates)]);
}
static void mixer_setup_free(til_setup_t *setup)
{
mixer_setup_t *s = (mixer_setup_t *)setup;
for (size_t i = 0; i < nelems(s->inputs); i++)
til_setup_free(s->inputs[i].setup);
free(setup);
}
static int mixer_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup);
til_module_t mixer_module = {
.create_context = mixer_create_context,
.destroy_context = mixer_destroy_context,
.prepare_frame = mixer_prepare_frame,
.render_fragment = mixer_render_fragment,
.finish_frame = mixer_finish_frame,
.name = "mixer",
.description = "Module blender (threaded)",
.setup = mixer_setup,
};
static int mixer_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup)
{
const char *input_names[2] = { "First module to mix", "Second module to mix" };
const char *input_keys[2] = { "a_module", "b_module" };
const char *input_module_name_names[2] = { "First module's name", "Second module's name" };
const char *input_preferred[2] = { "blank", "compose" };
const char *exclusions[] = { "none", "mixer", NULL };
const char *style_values[] = {
"blend",
"flicker",
"interlace",
"paintroller",
NULL
};
const char *passes_values[] = {
"2",
"4",
"6",
"8",
"10",
"12",
"16",
"18",
"20",
NULL
};
const char *orientation_values[] = {
"horizontal",
"vertical",
NULL
};
til_setting_t *style;
til_setting_t *passes;
til_setting_t *orientation;
const til_settings_t *inputs_settings[2];
til_setting_t *inputs[2];
int r;
r = til_settings_get_and_describe_setting(settings,
&(til_setting_spec_t){
.name = "Mixer blend style",
.key = "style",
.values = style_values,
.preferred = style_values[MIXER_DEFAULT_STYLE],
.annotations = NULL
},
&style,
res_setting,
res_desc);
if (r)
return r;
if (!strcasecmp(style->value, style_values[MIXER_STYLE_PAINTROLLER])) {
r = til_settings_get_and_describe_setting(settings,
&(til_setting_spec_t){
.name = "Mixer paint roller orientation",
.key = "orientation",
.values = orientation_values,
.preferred = orientation_values[MIXER_DEFAULT_ORIENTATION],
.annotations = NULL
},
&orientation,
res_setting,
res_desc);
if (r)
return r;
r = til_settings_get_and_describe_setting(settings,
&(til_setting_spec_t){
.name = "Mixer paint roller passes",
.key = "passes",
.values = passes_values,
.preferred = TIL_SETTINGS_STR(MIXER_DEFAULT_PASSES),
.annotations = NULL
},
&passes,
res_setting,
res_desc);
if (r)
return r;
}
for (int i = 0; i < 2; i++) {
r = til_settings_get_and_describe_setting(settings,
&(til_setting_spec_t){
.name = input_names[i],
.key = input_keys[i],
.preferred = input_preferred[i],
.as_nested_settings = 1,
.random = mixer_random_module_setting,
},
&inputs[i],
res_setting,
res_desc);
if (r)
return r;
inputs_settings[i] = (*res_setting)->value_as_nested_settings;
assert(inputs_settings[i]);
r = til_module_setup_full(inputs_settings[i],
res_setting,
res_desc,
NULL, /* XXX: no res_setup, defer finalizing */
input_module_name_names[i],
input_preferred[i],
(TIL_MODULE_EXPERIMENTAL | TIL_MODULE_HERMETIC | TIL_MODULE_AUDIO_ONLY),
exclusions);
if (r)
return r;
}
if (res_setup) {
mixer_setup_t *setup;
unsigned i;
setup = til_setup_new(settings, sizeof(*setup), mixer_setup_free, &mixer_module);
if (!setup)
return -ENOMEM;
r = til_value_to_pos(style_values, style->value, (unsigned *)&setup->style);
if (r < 0)
return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, style, res_setting, -EINVAL);
if (setup->style == MIXER_STYLE_PAINTROLLER) {
if (sscanf(passes->value, "%u", &setup->n_passes) != 1)
return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, passes, res_setting, -EINVAL);
r = til_value_to_pos(orientation_values, orientation->value, (unsigned *)&setup->orientation);
if (r < 0)
return til_setup_free_with_failed_setting_ret_err(&setup->til_setup, orientation, res_setting, -EINVAL);
}
for (i = 0; i < 2; i++) {
r = til_module_setup_full(inputs_settings[i],
res_setting,
res_desc,
&setup->inputs[i].setup, /* finalize! */
input_module_name_names[i],
input_preferred[i],
(TIL_MODULE_EXPERIMENTAL | TIL_MODULE_HERMETIC | TIL_MODULE_AUDIO_ONLY),
exclusions);
if (r < 0)
return til_setup_free_with_ret_err(&setup->til_setup, r);
assert(r == 0);
}
*res_setup = &setup->til_setup;
}
return 0;
}
|