@thi.ng/shader-ast-stdlib
Version:
Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast
81 lines (80 loc) • 2.08 kB
JavaScript
import { S2D, V2, V4 } from "@thi.ng/shader-ast/api/types";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { vec2 } from "@thi.ng/shader-ast/ast/lit";
import { add, addSelf, div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
import { sym } from "@thi.ng/shader-ast/ast/sym";
import { texture } from "@thi.ng/shader-ast/builtin/texture";
const __singlePass = (col, tex, uv, off, k) => addSelf(
col,
mul(add(texture(tex, add(uv, off)), texture(tex, sub(uv, off))), k)
);
const blur5 = defn(
V4,
"blur5",
[S2D, V2, V2, V2],
(tex, res, uv, dir) => {
let col;
let off;
const k1 = 0.29411764705882354;
const k2 = 0.35294117647058826;
return [
off = sym(div(mul(vec2(1 + 1 / 3), dir), res)),
col = sym(mul(texture(tex, uv), k1)),
__singlePass(col, tex, uv, off, k2),
ret(col)
];
}
);
const blur9 = defn(
V4,
"blur9",
[S2D, V2, V2, V2],
(tex, res, uv, dir) => {
let col;
let off;
let off2;
let delta;
const k1 = 0.3162162162;
const k2 = 0.0702702703;
return [
delta = sym(div(dir, res)),
off = sym(mul(delta, 1.3846153846)),
off2 = sym(mul(delta, 3.2307692308)),
col = sym(mul(texture(tex, uv), 0.227027027)),
__singlePass(col, tex, uv, off, k1),
__singlePass(col, tex, uv, off2, k2),
ret(col)
];
}
);
const blur13 = defn(
V4,
"blur13",
[S2D, V2, V2, V2],
(tex, res, uv, dir) => {
let col;
let off;
let off2;
let off3;
let delta;
const k1 = 0.2969069646728344;
const k2 = 0.09447039785044732;
const k3 = 0.010381362401148057;
return [
delta = sym(div(dir, res)),
off = sym(mul(delta, 1.411764705882353)),
off2 = sym(mul(delta, 3.2941176470588234)),
off3 = sym(mul(delta, 5.176470588235294)),
col = sym(mul(texture(tex, uv), 0.1964825501511404)),
__singlePass(col, tex, uv, off, k1),
__singlePass(col, tex, uv, off2, k2),
__singlePass(col, tex, uv, off3, k3),
ret(col)
];
}
);
export {
blur13,
blur5,
blur9
};