UNPKG

@thi.ng/shader-ast-stdlib

Version:

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

127 lines (126 loc) 3.34 kB
import { F, V2, V3, V4 } from "@thi.ng/shader-ast/api/types"; import { defn, ret } from "@thi.ng/shader-ast/ast/function"; import { mat2, vec2, vec3, vec4 } from "@thi.ng/shader-ast/ast/lit"; import { add, addSelf, mul, mulSelf } from "@thi.ng/shader-ast/ast/ops"; import { $, $x, $y, $z } from "@thi.ng/shader-ast/ast/swizzle"; import { sym } from "@thi.ng/shader-ast/ast/sym"; import { dot, fract, sin } from "@thi.ng/shader-ast/builtin/math"; const hash2 = defn(V2, "hash2", [V2], (p) => [ ret(fract(mul(sin(mul(p, mat2(127.1, 311.7, 269.5, 183.3))), 43758.5453))) ]); const hash3 = defn(V3, "hash3", [V2], (p) => [ ret( fract( mul( sin( vec3( dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)), dot(p, vec2(419.2, 371.9)) ) ), 43758.5453 ) ) ) ]); const H = vec3(0.1031, 0.103, 0.0973); const H4 = vec4(0.1031, 0.103, 0.0973, 0.1099); const hash11 = defn(F, "hash11", [F], (p) => { let x; return [ x = sym(fract(mul(p, 0.1031))), mulSelf(x, add(x, 19.19)), mulSelf(x, add(x, x)), ret(fract(x)) ]; }); const __hash1 = (V) => defn(F, null, [V], (p) => { let x; return [ x = sym(fract(mul($(p, "xyx"), 0.1031))), addSelf(x, dot(x, add($(x, "yzx"), 19.19))), ret(fract(mul(add($x(x), $y(x)), $z(x)))) ]; }); const hash12 = __hash1(V2); const hash13 = __hash1(V3); const hash21 = defn(V2, "hash21", [F], (p) => { let x; return [ x = sym(fract(mul(vec3(p), H))), addSelf(x, dot(x, add($(x, "yzx"), 19.19))), ret(fract(mul(add($(x, "xx"), $(x, "yz")), $(x, "zy")))) ]; }); const hash22 = defn(V2, "hash22", [V2], (p) => { let x; return [ x = sym(fract(mul($(p, "xyx"), H))), addSelf(x, dot(x, add($(x, "yzx"), 19.19))), ret(fract(mul(add($(x, "xx"), $(x, "yz")), $(x, "zy")))) ]; }); const hash23 = defn(V2, "hash23", [V3], (p) => { let x; return [ x = sym(fract(mul(p, H))), addSelf(x, dot(x, add($(x, "yzx"), 19.19))), ret(fract(mul(add($(x, "xx"), $(x, "yz")), $(x, "zy")))) ]; }); const __hash3 = (I) => defn(V3, null, [I], (p) => { let x; return [ x = sym(fract(mul(p, H))), addSelf(x, dot(x, add($(x, "yzx"), 19.19))), ret(fract(mul(add($(x, "xxy"), $(x, "yzz")), $(x, "zyx")))) ]; }); const hash31 = __hash3(F); const hash32 = defn(V3, "hash32", [V2], (p) => { let x; return [ x = sym(fract(mul($(p, "xyx"), H))), addSelf(x, dot(x, add($(x, "yzx"), 19.19))), ret(fract(mul(add($(x, "xxy"), $(x, "yzz")), $(x, "zyx")))) ]; }); const hash33 = __hash3(V3); const __hash4 = (I) => defn(V4, null, [I], (p) => { let x; return [ x = sym(fract(mul(p, H4))), addSelf(x, dot(x, add($(x, "wzxy"), 19.19))), ret(fract(mul(add($(x, "xxyz"), $(x, "yzzw")), $(x, "zywx")))) ]; }); const __hash4a = (I) => defn(V4, null, [I], (p) => { let x; return [ x = sym(fract(mul($(p, "xyxy"), H4))), addSelf(x, dot(x, add($(x, "wzxy"), 19.19))), ret(fract(mul(add($(x, "xxyz"), $(x, "yzzw")), $(x, "zywx")))) ]; }); const hash41 = __hash4(F); const hash42 = __hash4a(V2); const hash43 = __hash4a(V3); const hash44 = __hash4(V4); export { hash11, hash12, hash13, hash2, hash21, hash22, hash23, hash3, hash31, hash32, hash33, hash41, hash42, hash43, hash44 };