Options
All
  • Public
  • Public/Protected
  • All
Menu

@thi.ng/shader-ast-optimize

shader-ast-optimize

npm versionnpm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

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 NodeJS (v14.6+):

node --experimental-specifier-resolution=node --experimental-repl-await

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

Package sizes (gzipped, pre-treeshake): ESM: 1.00 KB

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Evolutionary shader generation using genetic programmingDemoSource

API

Generated API docs

TODO

Authors

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
}

License

© 2019 - 2021 Karsten Schmidt // Apache Software License 2.0

Generated using TypeDoc