diff options
| author | user <user@clank> | 2026-08-16 15:58:11 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-08-16 15:58:11 +0200 |
| commit | 72ffc4cc5cc5f9b95d21a115e761de6a632d68a5 (patch) | |
| tree | 860336fd32f5aa62676eff0fcdbd5e72f214908a | |
| parent | sl0ppty: bits (diff) | |
| download | slosh-72ffc4cc5cc5f9b95d21a115e761de6a632d68a5.tar.gz slosh-72ffc4cc5cc5f9b95d21a115e761de6a632d68a5.tar.xz slosh-72ffc4cc5cc5f9b95d21a115e761de6a632d68a5.zip | |
sl0ppty: sin and cos
128 + sin(t / 4) / 2 a breathe that is actually a sine
abs(sin(x * 6 + t / 5)) a wave travelling along a frame
abs(sin(dist(x, y, curx, cury) * 20 - t / 4)) / 2 ripples from the cursor
Degrees in, -255..255 out. Two decisions, both for the same reason -- this is a
language people type into a config file, not one they read a manual for.
Degrees because that is what anybody guesses, and 255 because it is the scale
every strength here already uses: `128 + sin(t / 4) / 2` is a breathe centred
half way up with nothing to rescale, and `abs(sin(...))` is an amount as it
stands.
Every angle wraps, negative ones included. `t / 4` does not stop at 360 and an
expression that died at one turn would be no use for the one thing this is for.
A 91-entry table of round(255*sin) at one-degree steps, three quadrants by
symmetry, no interpolation -- the argument is an integer and there is nothing
between two degrees to interpolate. A table and not a polynomial because
`contrib/shadertoy.html` reimplements this language in JavaScript so the preview
can run in a browser, and `tests/test_shadertoy.py` compares the two on every
expression: identical data with identical reduction cannot drift the way two
approximations of one curve would. The JS carries the same 91 numbers.
Until now the only oscillator was `abs(t / 6 % 510 - 255)`, a triangle wave
written as arithmetic. It works and it looks like what it is -- a ramp with a
corner in it. A sine has no corner, which is the whole difference between a pane
that pulses and a pane that flickers.
Verified: 19 cases in `tests/expr_test.c` (the cardinal angles, quadrant
symmetry, wrapping past a turn and below zero, and the breathe idiom); the
cross-check now agrees with the compiler on 660 evaluations including generated
trig; the repl's completion drift test caught `sin`/`cos` missing from its list
before I did, which is what it is for. Then six chains in a real pane -- breathe,
travelling wave, clipped crests, plasma, rolling scanlines, cursor ripples --
each measured over eight distinct frames.
| -rw-r--r-- | config/config.kdl | 7 | ||||
| -rwxr-xr-x | contrib/shader-repl | 2 | ||||
| -rw-r--r-- | contrib/shadertoy.html | 28 | ||||
| -rw-r--r-- | docs/shaders.md | 1 | ||||
| -rw-r--r-- | src/expr.c | 41 | ||||
| -rw-r--r-- | tests/expr_test.c | 26 | ||||
| -rw-r--r-- | tests/test_shadertoy.py | 12 |
7 files changed, 114 insertions, 3 deletions
diff --git a/config/config.kdl b/config/config.kdl index d9e8c3c..6fc59d2 100644 --- a/config/config.kdl +++ b/config/config.kdl @@ -468,6 +468,13 @@ states { // functions min(a,b) max(a,b) abs(a) clamp(v,lo,hi) // dist(x1,y1,x2,y2) counts a row double, because a cell is // about twice as tall as it is wide +// sin(deg) cos(deg) degrees in, -255..255 out. Degrees because +// they are what you guess, and 255 because it +// is the strength scale everything else here +// uses -- so `128 + sin(t / 4) / 2` is a +// breathe with nothing to rescale. Any angle +// wraps, negative ones included, since `t / 4` +// does not stop at 360. // // There are no loops and no recursion, on purpose: a config cannot spin, and // there is nothing to sandbox. An expression that does not compile drops that diff --git a/contrib/shader-repl b/contrib/shader-repl index 537f3bb..f154bb8 100755 --- a/contrib/shader-repl +++ b/contrib/shader-repl @@ -65,7 +65,7 @@ PROPS = ["amount=", "color=", "channel=", "where=", "at=", "radius=", "band=", VALUES = ['"chrome"', '"content"', '"fg"', '"bg"', '"both"'] VARS = ["x", "y", "cols", "rows", "curx", "cury", "cursor", "focused", "t", "since"] -FUNCS = ["min(", "max(", "abs(", "clamp(", "dist("] +FUNCS = ["min(", "max(", "abs(", "clamp(", "dist(", "sin(", "cos("] HISTORY = os.path.join( os.environ.get("XDG_DATA_HOME") or os.path.expanduser("~/.local/share"), diff --git a/contrib/shadertoy.html b/contrib/shadertoy.html index a094e82..070f139 100644 --- a/contrib/shadertoy.html +++ b/contrib/shadertoy.html @@ -109,6 +109,8 @@ <code>& | ^ ~ << >></code> on the 32-bit value, binding tighter than comparison, so <code>x & 7 == 0</code> reads as it looks<br> <code>a ? b : c</code> <code>min max abs clamp dist</code><br> + <code>sin cos</code> take <em>degrees</em> and answer -255..255, so + <code>128 + sin(t / 4) / 2</code> is a breathe<br> <code>dist</code> counts a row double, because a cell is about twice as tall as it is wide. </p> @@ -145,7 +147,29 @@ const VARS = { since: { dep: DEP.TIME }, }; -const FNS = { min: 2, max: 2, abs: 1, clamp: 3, dist: 4 }; +const FNS = { min: 2, max: 2, abs: 1, clamp: 3, dist: 4, sin: 1, cos: 1 }; + /* Degrees in, -255..255 out. The same quarter-turn table src/expr.c carries and + the same reduction: identical data cannot drift the way two approximations of + one curve would, and a preview that disagrees with the compiler is worse than + no preview. */ + const SIN_Q = [ + 0, 4, 9, 13, 18, 22, 27, 31, 35, 40, 44, 49, 53, + 57, 62, 66, 70, 75, 79, 83, 87, 91, 96, 100, 104, 108, + 112, 116, 120, 124, 127, 131, 135, 139, 143, 146, 150, 153, 157, + 160, 164, 167, 171, 174, 177, 180, 183, 186, 190, 192, 195, 198, + 201, 204, 206, 209, 211, 214, 216, 219, 221, 223, 225, 227, 229, + 231, 233, 235, 236, 238, 240, 241, 243, 244, 245, 246, 247, 248, + 249, 250, 251, 252, 253, 253, 254, 254, 254, 255, 255, 255, 255 + ]; + const isin = (deg) => { + let d = deg % 360; + if (d < 0) d += 360; + if (d <= 90) return SIN_Q[d]; + if (d <= 180) return SIN_Q[180 - d]; + if (d <= 270) return -SIN_Q[d - 180]; + return -SIN_Q[360 - d]; + }; + function compile(src) { let i = 0, deps = 0; @@ -195,6 +219,8 @@ function compile(src) { case "min": return (e) => Math.min(a[0](e), a[1](e)); case "max": return (e) => Math.max(a[0](e), a[1](e)); case "abs": return (e) => Math.abs(a[0](e)); + case "sin": return (e) => isin(a[0](e)); + case "cos": return (e) => isin(a[0](e) + 90); case "clamp": return (e) => { const v = a[0](e), lo = a[1](e), hi = a[2](e); return v < lo ? lo : v > hi ? hi : v; }; /* A row counts double: a cell is about twice as tall as it is wide, and diff --git a/docs/shaders.md b/docs/shaders.md index aa332e5..716395a 100644 --- a/docs/shaders.md +++ b/docs/shaders.md @@ -65,6 +65,7 @@ shaders { | | `a ? b : c` — both sides are evaluated, then one is chosen | | functions | `min(a,b)` `max(a,b)` `abs(a)` `clamp(v,lo,hi)` | | | `dist(x1,y1,x2,y2)` — counts a row double, because a cell is about twice as tall as it is wide | +| | `sin(deg)` `cos(deg)` — **degrees** in, −255..255 out, so `128 + sin(t / 4) / 2` is a breathe | There are no loops and no recursion, on purpose: a config cannot spin and there is nothing to sandbox. An expression that does not compile drops that one shader @@ -19,7 +19,7 @@ enum { * contrib/shadertoy.html reimplements this language in JS and a test compares * the two, so `x << 33` has to mean the same thing in both. */ OP_BAND, OP_BXOR, OP_BOR, OP_BNOT, OP_SHL, OP_SHR, - OP_MIN, OP_MAX, OP_ABS, OP_CLAMP, OP_DIST, + OP_MIN, OP_MAX, OP_ABS, OP_CLAMP, OP_DIST, OP_SIN, OP_COS, OP_SELECT, /* cond a b -> a or b, both already evaluated */ }; @@ -190,6 +190,7 @@ static void parse_primary(parser_t *ps, expr_prog_t *pr) { } FNS[] = { {"min", 2, OP_MIN}, {"max", 2, OP_MAX}, {"abs", 1, OP_ABS}, {"clamp", 3, OP_CLAMP}, {"dist", 4, OP_DIST}, + {"sin", 1, OP_SIN}, {"cos", 1, OP_COS}, }; for (size_t i = 0; i < sizeof FNS / sizeof *FNS; i++) { if (strcmp(FNS[i].name, name) != 0) continue; @@ -376,6 +377,40 @@ static void parse_expr(parser_t *ps, expr_prog_t *pr) { emit(ps, pr, OP_SELECT, 0); } + +/* ---- trigonometry, in the only arithmetic this language has --------------- + * + * `sin(d)` and `cos(d)` take **degrees** and answer -255..255. Degrees because + * they are what a person writing a config guesses, and 255 because that is the + * strength scale everything else works in: `128 + sin(t / 4) / 2` is a breathe + * centred half way up, with nothing to rescale. + * + * A quarter turn of round(255*sin) at one-degree steps, with the other three + * quadrants by symmetry. No interpolation: the argument is an integer and there + * is nothing between two degrees to interpolate. A table rather than a + * polynomial for one reason -- contrib/shadertoy.html has to answer identically, + * and identical data with identical reduction cannot drift the way two + * approximations of the same curve do. + */ +static const int16_t SIN_Q[91] = { + 0, 4, 9, 13, 18, 22, 27, 31, 35, 40, 44, 49, 53, + 57, 62, 66, 70, 75, 79, 83, 87, 91, 96, 100, 104, 108, + 112, 116, 120, 124, 127, 131, 135, 139, 143, 146, 150, 153, 157, + 160, 164, 167, 171, 174, 177, 180, 183, 186, 190, 192, 195, 198, + 201, 204, 206, 209, 211, 214, 216, 219, 221, 223, 225, 227, 229, + 231, 233, 235, 236, 238, 240, 241, 243, 244, 245, 246, 247, 248, + 249, 250, 251, 252, 253, 253, 254, 254, 254, 255, 255, 255, 255, +}; + +static int32_t isin(int32_t deg) { + int32_t d = deg % 360; + if (d < 0) d += 360; + if (d <= 90) return SIN_Q[d]; + if (d <= 180) return SIN_Q[180 - d]; + if (d <= 270) return -SIN_Q[d - 180]; + return -SIN_Q[360 - d]; +} + /* ---- VM ----------------------------------------------------------------- */ static int vm_run(const expr_prog_t *pr, const expr_env_t *env) { @@ -408,6 +443,8 @@ static int vm_run(const expr_prog_t *pr, const expr_env_t *env) { case OP_NOT: if (sp) st[sp - 1] = !st[sp - 1]; break; case OP_BNOT: if (sp) st[sp - 1] = (int32_t)~(uint32_t)st[sp - 1]; break; case OP_ABS: if (sp) st[sp - 1] = st[sp - 1] < 0 ? -st[sp - 1] : st[sp - 1]; break; + case OP_SIN: if (sp) st[sp - 1] = isin(st[sp - 1]); break; + case OP_COS: if (sp) st[sp - 1] = isin(st[sp - 1] + 90); break; default: { int need = in->op == OP_CLAMP ? 3 : in->op == OP_DIST ? 4 : in->op == OP_SELECT ? 3 : 2; @@ -505,6 +542,8 @@ static bool stack_fits(const expr_prog_t *pr) { case OP_NEG: case OP_NOT: case OP_BNOT: + case OP_SIN: + case OP_COS: case OP_ABS: break; case OP_CLAMP: case OP_SELECT: sp -= 2; break; diff --git a/tests/expr_test.c b/tests/expr_test.c index abb3335..b84e355 100644 --- a/tests/expr_test.c +++ b/tests/expr_test.c @@ -106,6 +106,32 @@ int main(void) { eq("x ^ y", 7); eq("(x ^ y) & 1", 1); + printf("\n-- sin and cos: degrees in, -255..255 out\n"); + eq("sin(0)", 0); + eq("sin(30)", 127); /* round(255 * 0.5) is 127, not 128: 127.5 rounds down */ + eq("sin(90)", 255); + eq("sin(180)", 0); + eq("sin(270)", -255); + eq("cos(0)", 255); + eq("cos(90)", 0); + eq("cos(180)", -255); + /* Symmetry, so the four quadrants are one curve and not four. */ + eq("sin(45) - sin(135)", 0); + eq("sin(30) + sin(210)", 0); + eq("cos(60) - sin(30)", 0); + /* Any angle at all: wrapped, including negative ones, because `t / 4` does not + * stop at 360 and an expression that dies at one turn is no use for animation. */ + eq("sin(360)", 0); + eq("sin(450)", 255); + eq("sin(0 - 90)", -255); + eq("sin(0 - 450)", -255); + eq("sin(36000 + 90)", 255); + /* The shape a strength actually wants: a breathe centred half way up, which is + * why the range is the same 255 the amounts use. */ + eq("128 + sin(90) / 2", 255); + eq("128 + sin(270) / 2", 1); /* -255/2 truncates toward zero, so 128 - 127 */ + eq("abs(sin(270))", 255); + printf("\n-- variables\n"); eq("x", 3); eq("y", 4); diff --git a/tests/test_shadertoy.py b/tests/test_shadertoy.py index 62e205e..2f88225 100644 --- a/tests/test_shadertoy.py +++ b/tests/test_shadertoy.py @@ -114,6 +114,15 @@ EXPRS = [ "~x & 255", "t >> 6 & 15", "(x * 7 ^ y * 13) & 31", + # trig: degrees in, -255..255 out, from a table both sides share + "sin(0)", "sin(30)", "sin(45)", "sin(90)", "sin(180)", "sin(270)", + "sin(359)", "sin(360)", "sin(450)", "sin(0 - 30)", "sin(0 - 400)", + "cos(0)", "cos(90)", "cos(180)", "cos(270)", "cos(0 - 90)", + "sin(x * 40)", "cos(y * 37)", "sin(t / 4)", "sin(t) + cos(t)", + "128 + sin(t / 4) / 2", + "abs(sin(x * 9 + t / 6))", + "clamp(sin(t / 5) + 128, 0, 255)", + "sin(sin(t / 3))", ] @@ -129,6 +138,9 @@ def rand_exprs(n, seed=20260814): f"({a} {rng.choice(['+', '-', '*', '/', '%', '&', '|', '^'])} {b}) * 2", f"{a} {rng.choice(['<<', '>>'])} ({b} % 6)", f"({a} ^ {b}) & {c}", + f"sin({a} * 3 + {b})", + f"cos({a} - {b})", + f"128 + sin({a} * {rng.choice(['2', '5', '11'])}) / 2", f"clamp({a} * {rng.choice([2, 3, 9])} - {b}, 0, 255)", f"({a} {rng.choice(['<', '>', '==', '<=', '!='])} {b}) * {c}", f"min({a}, {b}) + max({b}, {c})", |
