@thi.ng/shader-ast-stdlib
Version:
Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast
36 lines (35 loc) • 832 B
JavaScript
import { I, V2 } from "@thi.ng/shader-ast/api/types";
import { defn, ret } from "@thi.ng/shader-ast/ast/function";
import { float, int, vec2 } from "@thi.ng/shader-ast/ast/lit";
import { add, div, modi, mul } from "@thi.ng/shader-ast/ast/ops";
import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
const indexToUV = defn(
V2,
"indexToUV",
[[I, "i", { prec: "highp" }], ["ivec2"]],
(i, size) => [
ret(
vec2(
div(float(modi(i, $x(size))), float($x(size))),
div(float(div(i, $x(size))), float($y(size)))
)
)
]
);
const uvToIndex = defn(
I,
"uvToIndex",
[V2, [I, "width", { prec: "highp" }]],
(uv, width) => [
ret(
add(
int(mul($x(uv), float(width))),
int(mul($y(uv), float(mul(width, width))))
)
)
]
);
export {
indexToUV,
uvToIndex
};