@thi.ng/shader-ast-stdlib
Version:
Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast
60 lines (59 loc) • 2.06 kB
JavaScript
import { V4 } from "@thi.ng/shader-ast/api/types";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { FLOAT0, FLOAT1, vec4 } from "@thi.ng/shader-ast/ast/lit";
import { add, div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
import { $w, $xyz } from "@thi.ng/shader-ast/ast/swizzle";
import { clamp01 } from "../math/clamp.js";
const __coeff = (col, f) => f === FLOAT0 ? vec4() : f === FLOAT1 ? col : mul(col, f);
const porterDuff = (name, fa, fb) => defn(V4, name, [V4, V4], (a, b) => {
const sa = fa($w(a), $w(b));
const sb = fb($w(a), $w(b));
const src = __coeff(a, sa);
const dest = __coeff(b, sb);
const srcZero = sa === FLOAT0;
const destZero = sb === FLOAT0;
return [
ret(
srcZero && destZero ? vec4() : clamp01(srcZero ? dest : destZero ? src : add(src, dest))
)
];
});
const premultiplyAlpha = (col) => vec4(mul($xyz(col), $w(col)), $w(col));
const postmultiplyAlpha = (col) => vec4(div($xyz(col), $w(col)), $w(col));
const ZERO = () => FLOAT0;
const ONE = () => FLOAT1;
const A = (a) => a;
const B = (_, b) => b;
const ONE_MINUS_A = (a) => sub(FLOAT1, a);
const ONE_MINUS_B = (_, b) => sub(FLOAT1, b);
const blendSrcOver = porterDuff("blendSrcOver", ONE, ONE_MINUS_A);
const blendDestOver = porterDuff("blendDestOver", ONE_MINUS_B, ONE);
const blendSrcIn = porterDuff("blendSrcIn", B, ZERO);
const blendDestIn = porterDuff("blendDestIn", ZERO, A);
const blendSrcOut = porterDuff("blendSrcOut", ONE_MINUS_B, ZERO);
const blendDestOut = porterDuff("blendDestOut", ZERO, ONE_MINUS_A);
const blendSrcAtop = porterDuff("blendSrcAtop", B, ONE_MINUS_A);
const blendDestAtop = porterDuff("blendDestAtop", ONE_MINUS_B, A);
const blendXor = porterDuff("blendXor", ONE_MINUS_B, ONE_MINUS_A);
const blendPlus = porterDuff("blendPlus", ONE, ONE);
export {
A,
B,
ONE,
ONE_MINUS_A,
ONE_MINUS_B,
ZERO,
blendDestAtop,
blendDestIn,
blendDestOut,
blendDestOver,
blendPlus,
blendSrcAtop,
blendSrcIn,
blendSrcOut,
blendSrcOver,
blendXor,
porterDuff,
postmultiplyAlpha,
premultiplyAlpha
};