# @thi.ng/shader-ast-optimize

> Shader AST code/tree optimization passes/strategies

Latest version **1.0.77** (published 2026-09-01) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @thi.ng/shader-ast-optimize
pnpm add @thi.ng/shader-ast-optimize
yarn add @thi.ng/shader-ast-optimize
bun add @thi.ng/shader-ast-optimize
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.77 |
| Published | 2026-09-01 |
| First published | 2021-10-12 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 6 |
| Unpacked size | 29.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Karsten Schmidt |
| Maintainers | thi.ng |
| Keywords | ast, compiler, constant-folding, optimization, rewrite, shader, shader-ast, tree, typescript, webgl |

## Links

- npm: https://www.npmjs.com/package/@thi.ng/shader-ast-optimize
- Repository: https://codeberg.org/thi.ng/umbrella
- Homepage: https://thi.ng/shader-ast-optimize
- Funding: https://github.com/sponsors/postspectacular
- npm.io page: https://npm.io/package/@thi.ng/shader-ast-optimize

## Dependencies (6)

- [@thi.ng/api](https://npm.io/package/@thi.ng/api.md) ^8.12.28
- [@thi.ng/math](https://npm.io/package/@thi.ng/math.md) ^5.15.17
- [@thi.ng/errors](https://npm.io/package/@thi.ng/errors.md) ^2.6.17
- [@thi.ng/logger](https://npm.io/package/@thi.ng/logger.md) ^3.3.11
- [@thi.ng/defmulti](https://npm.io/package/@thi.ng/defmulti.md) ^3.0.105
- [@thi.ng/shader-ast](https://npm.io/package/@thi.ng/shader-ast.md) ^1.1.58

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.77 (latest) — 2026-09-01
- 1.0.76 — 2026-08-25
- 1.0.75 — 2026-07-21
- 1.0.74 — 2026-07-02
- 1.0.73 — 2026-06-05
- 1.0.72 — 2026-05-23
- 1.0.71 — 2026-05-20
- 1.0.69 — 2026-05-11
- 1.0.68 — 2026-05-08
- 1.0.66 — 2026-04-26
- 1.0.65 — 2026-04-18
- 1.0.64 — 2026-04-02
- 1.0.63 — 2026-03-19
- 1.0.62 — 2026-03-11
- 1.0.61 — 2026-03-11
- … 207 more at https://npm.io/package/@thi.ng/shader-ast-optimize/versions

## README

<!-- This file is generated - DO NOT EDIT! -->
<!-- Please see: https://codeberg.org/thi.ng/umbrella/src/branch/develop/CONTRIBUTING.md#changes-to-readme-files -->
# ![@thi.ng/shader-ast-optimize](https://codeberg.org/thi.ng/umbrella/media/branch/develop/assets/banners/thing-shader-ast-optimize.svg?98682bff)

[![npm version](https://img.shields.io/npm/v/@thi.ng/shader-ast-optimize.svg)](https://www.npmjs.com/package/@thi.ng/shader-ast-optimize)
![npm downloads](https://img.shields.io/npm/dm/@thi.ng/shader-ast-optimize.svg)
[![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)

> [!NOTE]

> This is one of 217 standalone projects. LLM-free, human-made and
> cared for software, maintained as part of the
> [@thi.ng/umbrella](https://codeberg.org/thi.ng/umbrella/) ecosystem and
> anti-framework.
>
> 🚀 Please help me to work full-time on these projects by [sponsoring
> me](https://codeberg.org/thi.ng/umbrella/src/branch/develop/CONTRIBUTING.md#donations).
> Thank you! ❤️

- [About](#about)
  - [defOptimized()](#defoptimized)
  - [Tree optimizations](#tree-optimizations)
    - [Constant folding](#constant-folding)
- [Status](#status)
- [Installation](#installation)
- [Dependencies](#dependencies)
- [Usage examples](#usage-examples)
- [API](#api)
- [Authors](#authors)
- [License](#license)

## About

Shader AST code/tree optimization passes/strategies. This is a support package for [@thi.ng/shader-ast](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/shader-ast).

### defOptimized()

The function
[`defOptimize()`](https://docs.thi.ng/umbrella/shader-ast-optimize/functions/defOptimized.html)
can be used as direct replacement for [thi.ng/shader-ast]()'s
[`defMain()`](https://docs.thi.ng/umbrella/shader-ast/functions/defMain.html) to
define automatically optimized shader `main()` functions.

### Tree optimizations

Currently, only the following operations are supported/considered:

#### Constant folding

- scalar math operators (incl. some vector versions)
- scalar math built-in functions (incl. some vector versions)
- scalar comparisons
- single component vector swizzling
- literal hoisting

(See tests for some more examples, non-exhaustive...)

**Note:** The static optimizer throws an error if it detects a division-by-zero...

```ts tangle:export/readme1.ts
import {
  add, defn, float, mul, neg, ret, scope, vec2, $x, $y
} from "@thi.ng/shader-ast";
import { targetGLSL } from "@thi.ng/shader-ast-glsl";
import { constantFolding } from "@thi.ng/shader-ast-optimize";

// function def
const foo = defn("float", "foo", ["float"], (x) => [
  ret(mul(x, add(neg(float(10)), 42)))
]);

const bar = vec2(100, 200);

// program def
const prog = scope([
  foo,
  foo(add(float(1), float(2))),
  foo(add($x(bar), $y(bar)))
], true);

// GLSL codegen
const glsl = targetGLSL();

// unoptimized AST as GLSL (see section above)
console.log(glsl(prog));
// #version 300 es
// float foo(in float _s0) {
// return (_s0 * ((-10.0) + 42.0));
// }
// foo((1.0 + 2.0));
// foo((vec2(100.0, 200.0).x + vec2(100.0, 200.0).y));

// with constant folding
console.log(glsl(constantFolding(prog)))
// #version 300 es
// float foo(in float _s0) {
// return (_s0 * 32.0);
// }
// foo(3.0);
// foo(300.0);

const expr = mul(float(4), $x(vec2(2)))

console.log(glsl(expr))
// (4.0 * vec2(2.0).x)

// optimize single expression
console.log(glsl(constantFolding(expr)))
// 8.0
```

## Status

**STABLE** - used in production

[Search or submit any issues for this package](https://codeberg.org/thi.ng/umbrella/issues?q=%5Bshader-ast-optimize%5D)

## Installation

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

ESM import:

```ts
import * as opt from "@thi.ng/shader-ast-optimize";
```

Browser ESM import:

```html
<script type="module" src="https://esm.run/@thi.ng/shader-ast-optimize"></script>
```

[JSDelivr documentation](https://www.jsdelivr.com/)

For Node.js REPL:

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

Package sizes (brotli'd, pre-treeshake): ESM: 1.27 KB

## Dependencies

- [@thi.ng/api](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/api)
- [@thi.ng/defmulti](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/defmulti)
- [@thi.ng/errors](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/errors)
- [@thi.ng/logger](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/logger)
- [@thi.ng/math](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/math)
- [@thi.ng/shader-ast](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/shader-ast)

Note: @thi.ng/api is in _most_ cases a type-only import (not used at runtime)

## Usage examples

One project in this repo's
[/examples](https://codeberg.org/thi.ng/umbrella/src/branch/develop/examples)
directory is using this package:

| Screenshot                                                                                                            | Description                                              | Live demo                                            | Source                                                                                    |
|:----------------------------------------------------------------------------------------------------------------------|:---------------------------------------------------------|:-----------------------------------------------------|:------------------------------------------------------------------------------------------|
| <img src="https://codeberg.org/thi.ng/umbrella/media/branch/develop/assets/examples/shader-ast-evo.jpg" width="240"/> | Evolutionary shader generation using genetic programming | [Demo](https://demo.thi.ng/umbrella/shader-ast-evo/) | [Source](https://codeberg.org/thi.ng/umbrella/src/branch/develop/examples/shader-ast-evo) |

## API

[Generated API docs](https://docs.thi.ng/umbrella/shader-ast-optimize/)

TODO

## Authors

- [Karsten Schmidt](https://thi.ng)

If this project contributes to an academic publication, please cite it as:

```bibtex
@misc{thing-shader-ast-optimize,
  title = "@thi.ng/shader-ast-optimize",
  author = "Karsten Schmidt",
  note = "https://thi.ng/shader-ast-optimize",
  year = 2019
}
```

## License

&copy; 2019 - 2026 Karsten Schmidt // Apache License 2.0

---
_Source: https://npm.io/package/@thi.ng/shader-ast-optimize · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
