0.4.14 • Published 12 days ago

@thi.ng/shader-ast-optimize v0.4.14

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
12 days ago

shader-ast-optimize

npm version npm 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 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: 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

0.4.14

12 days ago

0.4.13

14 days ago

0.4.12

17 days ago

0.4.11

26 days ago

0.4.10

29 days ago

0.4.9

1 month ago

0.4.8

1 month ago

0.4.7

1 month ago

0.4.6

2 months ago

0.4.5

2 months ago

0.4.4

2 months ago

0.4.3

2 months ago

0.4.2

2 months ago

0.4.1

2 months ago

0.4.0

2 months ago

0.3.55

2 months ago

0.3.54

2 months ago

0.3.53

2 months ago

0.3.52

2 months ago

0.3.51

2 months ago

0.3.50

2 months ago

0.3.49

3 months ago

0.3.48

3 months ago

0.3.47

3 months ago

0.3.46

3 months ago

0.3.45

3 months ago

0.3.42

3 months ago

0.3.41

3 months ago

0.3.44

3 months ago

0.3.43

3 months ago

0.3.40

3 months ago

0.3.39

3 months ago

0.3.38

4 months ago

0.3.37

4 months ago

0.3.36

5 months ago

0.3.35

5 months ago

0.3.34

5 months ago

0.3.33

5 months ago

0.3.32

5 months ago

0.3.31

5 months ago

0.3.30

5 months ago

0.3.29

6 months ago

0.3.20

7 months ago

0.3.28

6 months ago

0.3.26

6 months ago

0.3.25

6 months ago

0.3.24

6 months ago

0.3.23

6 months ago

0.3.22

7 months ago

0.3.21

7 months ago

0.3.19

7 months ago

0.3.18

7 months ago

0.3.6

9 months ago

0.3.5

9 months ago

0.3.8

9 months ago

0.3.7

9 months ago

0.3.2

10 months ago

0.3.3

9 months ago

0.3.9

9 months ago

0.3.17

7 months ago

0.3.16

8 months ago

0.3.15

8 months ago

0.3.14

8 months ago

0.3.13

8 months ago

0.3.12

8 months ago

0.3.11

8 months ago

0.3.10

9 months ago

0.3.1

11 months ago

0.2.52

12 months ago

0.2.51

1 year ago

0.3.0

12 months ago

0.2.50

1 year ago

0.2.49

1 year ago

0.2.48

1 year ago

0.2.47

1 year ago

0.2.46

1 year ago

0.2.45

1 year ago

0.2.44

1 year ago

0.2.43

1 year ago

0.2.42

1 year ago

0.2.41

1 year ago

0.2.40

1 year ago

0.2.38

1 year ago

0.2.37

1 year ago

0.2.36

1 year ago

0.2.35

1 year ago

0.2.34

1 year ago

0.2.33

1 year ago

0.2.27

2 years ago

0.2.26

2 years ago

0.2.25

2 years ago

0.2.24

2 years ago

0.2.23

2 years ago

0.2.22

2 years ago

0.2.30

1 year ago

0.2.32

1 year ago

0.2.31

1 year ago

0.2.29

2 years ago

0.2.28

2 years ago

0.2.21

2 years ago

0.2.20

2 years ago

0.2.19

2 years ago

0.2.18

2 years ago

0.2.17

2 years ago

0.2.16

2 years ago

0.2.15

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.7

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.9

2 years ago

0.2.3

2 years ago

0.2.2

2 years ago

0.2.4

2 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago