UNPKG

@thi.ng/shader-ast-stdlib

Version:

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

81 lines (80 loc) 2.03 kB
import { F, M3, V2, V3 } 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 { indexMat } from "@thi.ng/shader-ast/ast/indexed"; import { FLOAT0, FLOAT05, FLOAT1, VEC3_0, VEC3_1, float, vec3 } from "@thi.ng/shader-ast/ast/lit"; import { lt, madd, mul, reciprocal, sub } from "@thi.ng/shader-ast/ast/ops"; import { $x, $y, $z } from "@thi.ng/shader-ast/ast/swizzle"; import { max, min, pow } from "@thi.ng/shader-ast/builtin/math"; import { fit01, fitClamped } from "../math/fit.js"; const midLevelGamma = defn(F, "midLevelGamma", [F], (mid) => [ ret( reciprocal( ternary( lt(mid, FLOAT05), min(float(9.99), madd(float(9), sub(1, mul(mid, 2)), 1)), max(float(0.01), sub(1, sub(mul(mid, 2), 1))) ) ) ) ]); const midLevelGammaRGB = (mid) => vec3( midLevelGamma($x(mid)), midLevelGamma($y(mid)), midLevelGamma($z(mid)) ); const levelAdjustGamma = defn( F, "levelAdjustGamma", [F, F, V2, V2], (x, gamma, input, output) => [ ret( fit01( pow(fitClamped(x, $x(input), $y(input), FLOAT0, FLOAT1), gamma), $x(output), $y(output) ) ) ] ); const levelAdjustGammaRGB = defn( V3, "levelAdjustGammaRGB", [V3, V3, M3, M3], (x, gamma, input, output) => [ ret( fit01( pow( fitClamped( x, indexMat(input, 0), indexMat(input, 1), VEC3_0, VEC3_1 ), gamma ), indexMat(output, 0), indexMat(output, 1) ) ) ] ); const levelAdjustMid = (x, mid, input, output) => levelAdjustGamma(x, midLevelGamma(mid), input, output); const levelAdjustMidRGB = (x, mid, input, output) => levelAdjustGammaRGB(x, midLevelGammaRGB(mid), input, output); export { levelAdjustGamma, levelAdjustGammaRGB, levelAdjustMid, levelAdjustMidRGB, midLevelGamma, midLevelGammaRGB };