This project is part of the @thi.ng/umbrella monorepo.
Shader AST code optimization passes/strategies. This is a support package for @thi.ng/shader-ast.
Currently, only the following operations are supported / considered:
- scalar math operators
- scalar math built-in functions
- single component vector swizzling
- literal hoisting
import { $x, $y, add, defn, float, mul, neg, ret } from "@thi.ng/shader-ast";
import { targetGLSL } from "@thi.ng/shader-ast-glsl";
import { constantFolding } from "@thi.ng/shader-ast-optimize";
const foo = defn("float", "foo", ["float"], (x) => [
ret(mul(x, add(neg(float(10)), float(42))))
]);
const bar = vec2(100, 200);
const prog = scope([
foo,
foo(add(float(1), float(2))),
foo(add($x(bar), $y(bar)))
], true);
const glsl = targetGLSL();
// unoptimized AST as GLSL (see section above)
glsl(prog);
// float foo(in float _sym0) {
// return (_sym0 * (-10.0 + 42.0));
// };
// foo((1.0 + 2.0));
// foo((vec2(100.0, 200.0).x + vec2(100.0, 200.0).y));
// same tree after constant folding optimizations
glsl(constantFolding(prog))
// float foo(in float _sym0) {
// return (_sym0 * 32.0);
// };
// foo(3.0);
// foo(300.0);
const expr = mul(float(4), $x(vec2(2)))
glsl(expr)
// (4.0 * vec2(2.0).x)
glsl(constantFolding(expr))
// 8.0
STABLE - used in production
Search or submit any issues for this package
yarn add @thi.ng/shader-ast-optimize
ES module import:
<script type="module" src="https://cdn.skypack.dev/@thi.ng/shader-ast-optimize"></script>
For Node.js REPL:
# with flag only for < v16
node --experimental-repl-await
> const shaderAstOptimize = await import("@thi.ng/shader-ast-optimize");
Package sizes (gzipped, pre-treeshake): ESM: 999 bytes
Several demos in this repo's /examples directory are using this package.
A selection:
Screenshot | Description | Live demo | Source |
---|---|---|---|
Evolutionary shader generation using genetic programming | Demo | Source |
TODO
Karsten Schmidt
If this project contributes to an academic publication, please cite it as:
@misc{thing-shader-ast-optimize,
title = "@thi.ng/shader-ast-optimize",
author = "Karsten Schmidt",
note = "https://thi.ng/shader-ast-optimize",
year = 2019
}
© 2019 - 2021 Karsten Schmidt // Apache Software License 2.0