aboutsummaryrefslogtreecommitdiffstats
path: root/src/shader.c
diff options
context:
space:
mode:
authoruser <user@clank>2026-08-14 22:39:18 +0200
committeruser <user@clank>2026-08-14 22:39:18 +0200
commitc0b88416fc9543fa348b63be07905f7a33fdbcc3 (patch)
treef669fdf1066b2ffeed7d61baae572f4834b7ce62 /src/shader.c
parentsl0ppty: a tab is something you can name and move (diff)
downloadslosh-c0b88416fc9543fa348b63be07905f7a33fdbcc3.tar.gz
slosh-c0b88416fc9543fa348b63be07905f7a33fdbcc3.tar.xz
slosh-c0b88416fc9543fa348b63be07905f7a33fdbcc3.zip
sl0ppty: a shader's strength can be an expression
Adding an effect meant writing C -- in the binary, or since D15 in a shared library. Both are a lot of ceremony for "dim every other row". So: `amount` may be an expression, compiled to bytecode when the config is read and evaluated per cell. shaders { dim amount="(y % 2) * 40" // scanlines dim amount="(x > cols - 10) * 120" // a right margin tint amount="255 - dist(x, y, curx, cury) * 12" color="#ff5fd7" } The scope is the argument, and that is the whole design. Every built-in here is already colour_op(amount) with the amount a pure function of position -- vignette, zebra, ruler, margin and spotlight differ only in that function -- so the part worth handing to a config is the *number*, not the colour maths. Three things follow at once: the mixing stays in C where it is fast and correct; an expression cannot produce an invalid cell, because it produces one byte; and that byte is cacheable. The cache is what makes an interpreter affordable, and the numbers are why this exists at all. Measured on a 200x50 pane, one pass: native vignette 6.1 ns/cell constant expression, folded 5.6 ns/cell positional expression, mapped 5.2 ns/cell animated (reads the clock) 35.0 ns/cell A shader you wrote in your config costs what a compiled one does. A program that does not read the clock is evaluated once per cell into a byte map and reused until something it reads changes; one that does read the clock is interpreted per cell, per frame, which is the honest price of animation and is opt-in. (The earlier measurement of 56ns for interpretation was per evaluation -- correct, and the reason the map exists rather than an argument against the language.) What a program reads is derived by the compiler rather than declared, and that dependency set *is* the cache key. A stale map is therefore unrepresentable rather than merely unlikely -- the same rule the layout follows, arrived at from the other direction. A unit test caught the first violation of it before anything else did: `cols` and `rows` carried no dependency flag, on the theory that the key always contains the rect, and so a program reading only the size looked constant and was folded at compile time, where every size is zero. The language is deliberately total. No loops, no recursion, no jumps: even `?:` compiles to evaluating both sides and selecting, because nothing in it has an effect. Evaluation is O(program length), so a config cannot spin, and there is nothing to sandbox. Division by zero is 0 rather than a trap: an expression that is wrong should look wrong, not take the session with it. One that does not compile drops that one shader with a warning -- not the config, and not the half-strength version of what you asked for, which is what the number path did with garbage and is now also fixed. shader_t gained the compiled program, so the plugin ABI goes to 2. A plugin built against 1 is refused with "shader ABI 1, we speak 2" and needs nothing but a rebuild -- the check earning its keep the first time it was asked to.
Diffstat (limited to 'src/shader.c')
-rw-r--r--src/shader.c47
1 files changed, 45 insertions, 2 deletions
diff --git a/src/shader.c b/src/shader.c
index 1d36891..b1bb1a1 100644
--- a/src/shader.c
+++ b/src/shader.c
@@ -9,6 +9,10 @@
#include <stdlib.h>
#include <string.h>
+#include "expr.h"
+
+static int clamp255(int v) { return v < 0 ? 0 : v > 255 ? 255 : v; }
+
/* Integer maths throughout: this runs per cell per pass per frame, and a float
* here would buy nothing a byte channel can tell the difference between. */
static uint8_t mix8(uint8_t from, uint8_t to, uint8_t amount) {
@@ -182,7 +186,7 @@ bool shader_make_p(shader_t *out, const char *kind, color_t color,
if (!out || !kind) return false;
const shader_def_t *d = find_kind(kind);
if (!d) return false;
- *out = (shader_t){d->name, d->fn, color, amount, param};
+ *out = (shader_t){d->name, d->fn, NULL, color, amount, param};
return true;
}
@@ -340,6 +344,38 @@ void shade_apply(screen_t *s, const shader_t *shaders, size_t n, uint16_t x0,
const shader_t *sh = &shaders[i];
if (!sh->kind || !sh->fn) continue;
+ /* An expression's amount is computed here, not inside the shader, so no
+ * shader — built in or loaded — has to know that its strength might be a
+ * function of position. `local` carries the per-cell value; it is copied
+ * once per pass rather than per cell, because copying a shader_t for every
+ * cell would cost more than evaluating the expression. */
+ shader_t local = *sh;
+ const uint8_t *map = NULL;
+ expr_env_t env = {0};
+ bool per_cell = false;
+
+ if (sh->amount_expr) {
+ env.cols = w;
+ env.rows = h;
+ env.curx = ctx.has_cursor ? ctx.cursor_x : 0;
+ env.cury = ctx.has_cursor ? ctx.cursor_y : 0;
+ env.cursor = ctx.has_cursor;
+ env.focused = ctx.focused;
+ env.t = ctx.now_ms;
+ if (expr_deps(sh->amount_expr) == 0) {
+ local.amount = (uint8_t)clamp255(expr_constant(sh->amount_expr));
+ } else {
+ uint8_t *m = NULL;
+ /* The map is the whole reason an interpreter is affordable: a program
+ * that does not read the clock is evaluated once per cell and then
+ * reused for as long as nothing it reads has changed. One that does
+ * read the clock is evaluated per cell, per frame, which is what
+ * asking for animation costs. */
+ if (expr_amount_map(sh->amount_expr, &env, &m)) map = m;
+ else per_cell = true;
+ }
+ }
+
for (uint16_t y = 0; y < h; y++) {
cell_t *row = &s->cur[(size_t)(y0 + y) * s->cols + x0];
ctx.y = y;
@@ -356,7 +392,14 @@ void shade_apply(screen_t *s, const shader_t *shaders, size_t n, uint16_t x0,
if (!c->bg.set) c->bg = ctx.default_bg;
ctx.x = x;
- sh->fn(sh, &ctx, c);
+ if (map) {
+ local.amount = map[(size_t)y * w + x];
+ } else if (per_cell) {
+ env.x = x;
+ env.y = y;
+ local.amount = (uint8_t)clamp255(expr_eval(sh->amount_expr, &env));
+ }
+ local.fn(&local, &ctx, c);
}
}
}