@thi.ng/shader-ast-optimize
    TypeScript icon, indicating that this package has built-in type declarations

    0.1.3 • Public • Published

    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:

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

    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

    Keywords

    Install

    npm i @thi.ng/shader-ast-optimize

    DownloadsWeekly Downloads

    44

    Version

    0.1.3

    License

    Apache-2.0

    Unpacked Size

    26.7 kB

    Total Files

    8

    Last publish

    Collaborators

    • thi.ng