UNPKG

@thi.ng/shader-ast-stdlib

Version:

Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast

40 lines (39 loc) 935 B
import { F } from "@thi.ng/shader-ast/api/types"; import { forLoop } from "@thi.ng/shader-ast/ast/controlflow"; import { defn, ret } from "@thi.ng/shader-ast/ast/function"; import { gensym } from "@thi.ng/shader-ast/ast/idgen"; import { FLOAT0, FLOAT05, float } from "@thi.ng/shader-ast/ast/lit"; import { add, addSelf, inc, lt, mul, mulSelf } from "@thi.ng/shader-ast/ast/ops"; import { sym } from "@thi.ng/shader-ast/ast/sym"; const additive = (type, fn, oct = 4, name = gensym("additive_")) => defn(F, name, [[type], [type], F], (pos, shift, decay) => { let n; let amp; return [ n = sym(FLOAT0), amp = sym(FLOAT05), forLoop( sym(FLOAT0), (i) => lt(i, float(oct)), inc, (i) => [ addSelf( n, mul(amp, fn(add(pos, mul(i, shift)))) ), mulSelf(amp, decay), mulSelf(pos, 2) ] ), ret(n) ]; }); export { additive };