From 350f89859501fbe759634e77733ecba2c97fd06d Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Thu, 14 Nov 2019 23:06:52 -0800 Subject: snow: add a simple tv snow / white noise module I wanted to add some noise to the rtv module and figured why not just add a snow module and make rtv pass through it briefly when switching modules. It's not interesting by itself, but as more composite/meta modules like rtv get made it might be handy beyond rtv. --- configure.ac | 1 + src/Makefile.am | 2 +- src/modules/Makefile.am | 2 +- src/modules/snow/Makefile.am | 3 +++ src/modules/snow/snow.c | 51 ++++++++++++++++++++++++++++++++++++++++++++ src/rototiller.c | 2 ++ 6 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 src/modules/snow/Makefile.am create mode 100644 src/modules/snow/snow.c diff --git a/configure.ac b/configure.ac index d9232b8..c1f1c6d 100644 --- a/configure.ac +++ b/configure.ac @@ -45,6 +45,7 @@ AC_CONFIG_FILES([ src/modules/ray/Makefile src/modules/roto/Makefile src/modules/rtv/Makefile + src/modules/snow/Makefile src/modules/sparkler/Makefile src/modules/stars/Makefile src/modules/submit/Makefile diff --git a/src/Makefile.am b/src/Makefile.am index f6b4b92..74e12c2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,4 +4,4 @@ rototiller_SOURCES = fb.c fb.h fps.c fps.h rototiller.c rototiller.h sdl_fb.c se if ENABLE_DRM rototiller_SOURCES += drm_fb.c endif -rototiller_LDADD = modules/flui2d/libflui2d.a modules/julia/libjulia.a modules/pixbounce/libpixbounce.a modules/plasma/libplasma.a modules/ray/libray.a modules/roto/libroto.a modules/rtv/librtv.a modules/sparkler/libsparkler.a modules/stars/libstars.a modules/submit/libsubmit.a libs/grid/libgrid.a libs/ray/libray.a -lm +rototiller_LDADD = modules/flui2d/libflui2d.a modules/julia/libjulia.a modules/pixbounce/libpixbounce.a modules/plasma/libplasma.a modules/ray/libray.a modules/roto/libroto.a modules/rtv/librtv.a modules/snow/libsnow.a modules/sparkler/libsparkler.a modules/stars/libstars.a modules/submit/libsubmit.a libs/grid/libgrid.a libs/ray/libray.a -lm diff --git a/src/modules/Makefile.am b/src/modules/Makefile.am index fa4e852..fa913a0 100644 --- a/src/modules/Makefile.am +++ b/src/modules/Makefile.am @@ -1 +1 @@ -SUBDIRS = flui2d julia pixbounce plasma ray roto rtv sparkler stars submit +SUBDIRS = flui2d julia pixbounce plasma ray roto rtv snow sparkler stars submit diff --git a/src/modules/snow/Makefile.am b/src/modules/snow/Makefile.am new file mode 100644 index 0000000..73f767d --- /dev/null +++ b/src/modules/snow/Makefile.am @@ -0,0 +1,3 @@ +noinst_LIBRARIES = libsnow.a +libsnow_a_SOURCES = snow.c +libsnow_a_CPPFLAGS = -I@top_srcdir@/src diff --git a/src/modules/snow/snow.c b/src/modules/snow/snow.c new file mode 100644 index 0000000..f3c20d5 --- /dev/null +++ b/src/modules/snow/snow.c @@ -0,0 +1,51 @@ +#include +#include +#include + +#include "fb.h" +#include "rototiller.h" + +/* Copyright (C) 2019 - Vito Caputo */ + +/* This implements white noise / snow just using rand() */ + +/* To avoid the contention around rand() I'm just using rand_r() + * in an entirely racy fashion with a single seed from the threaded + * render_fragment(). It should really be per-cpu but the module + * api doesn't currently send down a cpu identifier to the render + * function, so TODO in the future add that. + */ +static int snow_seed; + +static int snow_fragmenter(void *context, const fb_fragment_t *fragment, unsigned num, fb_fragment_t *res_fragment) +{ + return fb_fragment_slice_single(fragment, 32, num, res_fragment); +} + + +static void snow_prepare_frame(void *context, unsigned n_cpus, fb_fragment_t *fragment, rototiller_fragmenter_t *res_fragmenter) +{ + *res_fragmenter = snow_fragmenter; +} + + +static void snow_render_fragment(void *context, fb_fragment_t *fragment) +{ + for (unsigned y = fragment->y; y < fragment->y + fragment->height; y++) { + for (unsigned x = fragment->x; x < fragment->x + fragment->width; x++) { + uint32_t pixel = rand_r(&snow_seed) % 256; + + fb_fragment_put_pixel_unchecked(fragment, x, y, pixel << 16 | pixel << 8 | pixel); + } + } +} + + +rototiller_module_t snow_module = { + .prepare_frame = snow_prepare_frame, + .render_fragment = snow_render_fragment, + .name = "snow", + .description = "TV snow / white noise", + .author = "Vito Caputo ", + .license = "GPLv2", +}; diff --git a/src/rototiller.c b/src/rototiller.c index 51704a5..c8532af 100644 --- a/src/rototiller.c +++ b/src/rototiller.c @@ -38,6 +38,7 @@ extern rototiller_module_t plasma_module; extern rototiller_module_t roto_module; extern rototiller_module_t ray_module; extern rototiller_module_t rtv_module; +extern rototiller_module_t snow_module; extern rototiller_module_t sparkler_module; extern rototiller_module_t stars_module; extern rototiller_module_t submit_module; @@ -53,6 +54,7 @@ static const rototiller_module_t *modules[] = { &flui2d_module, &pixbounce_module, &rtv_module, + &snow_module, }; typedef struct rototiller_t { -- cgit v1.2.1