UNPKG

@thi.ng/shader-ast

Version:

DSL to define shader code in TypeScript and cross-compile to GLSL, JS and other targets

126 lines (125 loc) 3.32 kB
import { F } from "../api/types.js"; import { assign } from "../ast/assign.js"; import { builtinCall } from "../ast/function.js"; import { matchingBoolType, matchingPrimFor } from "../ast/item.js"; const __primOp1 = (name) => (a) => builtinCall(name, a.type, a); const __primOp2 = (name) => (a, b) => builtinCall(name, a.type, a, b); const __primOp3 = (name) => (a, b, c) => builtinCall(name, a.type, a, b, c); const normalize = (v) => builtinCall("normalize", v.type, v); const normalizeSelf = (v) => assign(v, normalize(v)); const length = (v) => builtinCall("length", F, v); const distance = (a, b) => builtinCall("distance", F, a, b); const dot = (a, b) => builtinCall("dot", F, a, b); const cross = (a, b) => builtinCall("cross", a.type, a, b); const reflect = (i, n) => builtinCall("reflect", i.type, i, n); const refract = (i, n, ior) => builtinCall("refract", i.type, i, n, ior); const faceForward = (i, n, nref) => builtinCall("faceForward", i.type, i, n, nref); const min = __primOp2("min"); const max = __primOp2("max"); const clamp = __primOp3("clamp"); const minSelf = (a, b) => assign(a, min(a, b)); const maxSelf = (a, b) => assign(a, max(a, b)); const clampSelf = (a, b, c) => assign(a, clamp(a, b, c)); const step = __primOp2("step"); const smoothstep = __primOp3("smoothstep"); const radians = __primOp1("radians"); const degrees = __primOp1("degrees"); const cos = __primOp1("cos"); const cosh = __primOp1("cosh"); const sin = __primOp1("sin"); const sinh = __primOp1("sinh"); const tan = __primOp1("tan"); const tanh = __primOp1("tanh"); const acos = __primOp1("acos"); const acosh = __primOp1("acosh"); const asin = __primOp1("asin"); const asinh = __primOp1("asinh"); const atanh = __primOp1("atanh"); function atan(a, b) { const f = b ? builtinCall("atan", a.type, a, b) : builtinCall("atan", a.type, a); b && (f.info = "nn"); return f; } const pow = __primOp2("pow"); const exp = __primOp1("exp"); const log = __primOp1("log"); const exp2 = __primOp1("exp2"); const log2 = __primOp1("log2"); const sqrt = __primOp1("sqrt"); const inversesqrt = __primOp1("inversesqrt"); const abs = __primOp1("abs"); const sign = __primOp1("sign"); const floor = __primOp1("floor"); const ceil = __primOp1("ceil"); const fract = __primOp1("fract"); const powf = (x, y) => pow(x, matchingPrimFor(x, y)); function mod(a, b) { const f = builtinCall("mod", a.type, a, b); b.type === F && (f.info = "n"); return f; } const modf = (a, b) => builtinCall("modf", a.type, a, b); function mix(a, b, c) { const f = builtinCall("mix", a.type, a, b, c); c.type === F && (f.info = "n"); return f; } const matrixCompMult = (a, b) => builtinCall("matrixCompMult", a.type, a, b); function isnan(a) { return builtinCall("isnan", matchingBoolType(a), a); } function isinf(a) { return builtinCall("isinf", matchingBoolType(a), a); } export { abs, acos, acosh, asin, asinh, atan, atanh, ceil, clamp, clampSelf, cos, cosh, cross, degrees, distance, dot, exp, exp2, faceForward, floor, fract, inversesqrt, isinf, isnan, length, log, log2, matrixCompMult, max, maxSelf, min, minSelf, mix, mod, modf, normalize, normalizeSelf, pow, powf, radians, reflect, refract, sign, sin, sinh, smoothstep, sqrt, step, tan, tanh };