UNPKG

@thi.ng/shader-ast-stdlib

Version:

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

22 lines (21 loc) 748 B
import { F, V4 } from "@thi.ng/shader-ast/api/types"; import { defn, ret } from "@thi.ng/shader-ast/ast/function"; import { vec4 } from "@thi.ng/shader-ast/ast/lit"; import { mul, sub } from "@thi.ng/shader-ast/ast/ops"; import { $ } from "@thi.ng/shader-ast/ast/swizzle"; import { sym } from "@thi.ng/shader-ast/ast/sym"; import { dot, fract } from "@thi.ng/shader-ast/builtin/math"; const packFloat = defn(V4, null, [F], (x) => { let res; return [ res = sym(fract(mul(vec4(1, 255, 65025, 16581375), x))), ret(sub(res, mul($(res, "yzww"), vec4(1 / 255, 1 / 255, 1 / 255, 0)))) ]; }); const unpackFloat = defn(F, null, [V4], (v) => [ ret(dot(v, vec4(1, 1 / 255, 1 / 65025, 1 / 16581375))) ]); export { packFloat, unpackFloat };