@thi.ng/shader-ast-stdlib
Version:
Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast
33 lines (32 loc) • 843 B
JavaScript
import { F } from "@thi.ng/shader-ast/api/types";
import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { FLOAT0, FLOAT1, FLOAT2 } from "@thi.ng/shader-ast/ast/lit";
import { div, lt, mul, sub } from "@thi.ng/shader-ast/ast/ops";
import { sym } from "@thi.ng/shader-ast/ast/sym";
import {
abs,
ceil,
floor,
mix,
mod,
step
} from "@thi.ng/shader-ast/builtin/math";
const trunc = defn(F, null, [F], (x) => [
ret(ternary(lt(x, FLOAT0), ceil(x), floor(x)))
]);
const modulo = defn(F, null, [F, F], (x, y) => [
ret(sub(x, mul(y, trunc(div(x, y)))))
]);
const foldback01 = defn(F, null, [F], (x) => {
let y;
return [
y = sym(mod(abs(x), FLOAT2)),
ret(mix(y, sub(FLOAT2, y), step(FLOAT1, y)))
];
});
export {
foldback01,
modulo,
trunc
};