summaryrefslogtreecommitdiff
path: root/src/modules/snow/snow.c
blob: cd364a0cb8c7be16d799daada798b967f700e7fc (plain)
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
#include <stdint.h>
#include <stdlib.h>
#include <time.h>

#include "til.h"
#include "til_fb.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 */
	int	seed;
} snow_seed_t;

typedef struct snow_context_t {
	unsigned	n_cpus;
	snow_seed_t	seeds[];
} snow_context_t;


static void * snow_create_context(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));
	if (!ctxt)
		return NULL;

	for (unsigned i = 0; i < n_cpus; i++)
		ctxt->seeds[i].seed = rand();

	ctxt->n_cpus = n_cpus;

	return ctxt;
}


static void snow_destroy_context(void *context)
{
	free(context);
}


static int snow_fragmenter(void *context, const til_fb_fragment_t *fragment, unsigned number, til_fb_fragment_t *res_fragment)
{
	snow_context_t	*ctxt = context;

	return til_fb_fragment_slice_single(fragment, ctxt->n_cpus, number, res_fragment);
}


static void snow_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter)
{
	*res_fragmenter = snow_fragmenter;
}


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;

	for (unsigned y = fragment->y; y < fragment->y + fragment->height; y++) {
		for (unsigned x = fragment->x; x < fragment->x + fragment->width; x++) {
#ifdef __WIN32__
			uint32_t	pixel = rand();
#else
			uint32_t	pixel = rand_r(seed) % 256;
#endif

			til_fb_fragment_put_pixel_unchecked(fragment, x, y, pixel << 16 | pixel << 8 | pixel);
		}
	}
}


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",
	.description = "TV snow / white noise (threaded)",
	.author = "Vito Caputo <vcaputo@pengaru.com>",
};
© All Rights Reserved