Skip to content

Latest commit

 

History

History

shader-ast-optimize

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

@thi.ng/shader-ast-optimize

npm version npm downloads Mastodon Follow

This project is part of the @thi.ng/umbrella monorepo and anti-framework.

About

Shader AST code optimization passes/strategies. This is a support package for @thi.ng/shader-ast.

Tree optimizations

Currently, only the following operations are supported / considered:

Constant folding

  • 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

Status

STABLE - used in production

Search or submit any issues for this package

Installation

yarn add @thi.ng/shader-ast-optimize

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/shader-ast-optimize"></script>

Skypack documentation

For Node.js REPL:

const shaderAstOptimize = await import("@thi.ng/shader-ast-optimize");

Package sizes (brotli'd, pre-treeshake): ESM: 1014 bytes

Dependencies

Usage examples

One project in this repo's /examples directory is using this package:

Screenshot Description Live demo Source
Evolutionary shader generation using genetic programming Demo Source

API

Generated API docs

TODO

Authors

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
}

License

© 2019 - 2023 Karsten Schmidt // Apache License 2.0