@thi.ng/shader-ast-stdlib 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2024-03-06T08:50:42Z
3
+ - **Last updated**: 2024-03-07T20:40:47Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,14 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [0.18.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-stdlib@0.18.0) (2024-03-07)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add packFloat()/unpackFloat() ([23a6d7f](https://github.com/thi-ng/umbrella/commit/23a6d7f))
17
+ - add packedNormal2(), refactor normal2() ([6f6dc1d](https://github.com/thi-ng/umbrella/commit/6f6dc1d))
18
+ - add branches() n-ary helper fn & docs ([74f32ba](https://github.com/thi-ng/umbrella/commit/74f32ba))
19
+
12
20
  ## [0.17.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-stdlib@0.17.0) (2024-03-06)
13
21
 
14
22
  #### 🚀 Features
package/README.md CHANGED
@@ -109,7 +109,7 @@ For Node.js REPL:
109
109
  const shaderAstStdlib = await import("@thi.ng/shader-ast-stdlib");
110
110
  ```
111
111
 
112
- Package sizes (brotli'd, pre-treeshake): ESM: 13.67 KB
112
+ Package sizes (brotli'd, pre-treeshake): ESM: 13.93 KB
113
113
 
114
114
  ## Dependencies
115
115
 
@@ -408,6 +408,7 @@ Related:
408
408
  - `minComp2` / `minComp3` / `minComp4`
409
409
  - `perpendicularCCW` / `perpendicularCW`
410
410
  - `orthogonal3`
411
+ - `packFloat` / `unpackFloat`
411
412
  - `polar2` / `polar3`
412
413
  - `sincos` / `cossin`
413
414
  - `smootherStep` / `smootherStep2` / `smootherStep3` / `smootherStep4`
@@ -528,7 +529,7 @@ Related:
528
529
 
529
530
  - `indexToCoord` / `coordToIndex`
530
531
  - `indexToUV` / `uvToIndex`
531
- - `normal2`
532
+ - `normal2` / `packedNormal2`
532
533
  - `readIndex1` / `readIndex2` / `readIndex3` / `readIndex4`
533
534
 
534
535
  ### Visualization
@@ -0,0 +1,53 @@
1
+ import type { FloatTerm, Term, Type } from "@thi.ng/shader-ast";
2
+ /**
3
+ * Utility function to create an n-ary conditional based on given float value
4
+ * `x` in [0,1] range and any number of branch terms (at least 1). Returns a
5
+ * nested ternary expression.
6
+ *
7
+ * @remarks
8
+ * This function is useful for A/B testing of multiple versions of a shader,
9
+ * e.g. the popular technique of using columns based on the `gl_FragCoord.x` and
10
+ * dynamically computed column thresholds (defined by the number of given branch
11
+ * terms).
12
+ *
13
+ * @example
14
+ * ```ts tangle:../../export/branches.ts
15
+ * import { float, sym, vec3 } from "@thi.ng/shader-ast";
16
+ * import { branches } from "@thi.ng/shader-ast-stdlib";
17
+ * import { targetGLSL } from "@thi.ng/shader-ast-glsl";
18
+ *
19
+ * // dummy "position" symbol (would be gl_FragCoord.x or similar)
20
+ * const pos = sym(float(0.11));
21
+ *
22
+ * // serialize expression to GLSL for better legibility
23
+ * console.log(
24
+ * targetGLSL()(
25
+ * branches(
26
+ * pos,
27
+ * // branch terms/options...
28
+ * // branch thresholds are automatically computed
29
+ * // making it trivial to add/remove terms
30
+ * vec3(1,0,0),
31
+ * vec3(1,1,0),
32
+ * vec3(0,1,0),
33
+ * vec3(0,0,1),
34
+ * )
35
+ * )
36
+ * );
37
+ *
38
+ * // re-formatted result:
39
+ * // ((_sf5 < 0.25)
40
+ * // ? vec3(1.0, 0.0, 0.0)
41
+ * // : ((_sf5 < 0.5)
42
+ * // ? vec3(1.0, 1.0, 0.0)
43
+ * // : ((_sf5 < 0.75)
44
+ * // ? vec3(0.0, 1.0, 0.0)
45
+ * // : vec3(0.0, 0.0, 1.0))))
46
+ * ```
47
+ *
48
+ * @param x
49
+ * @param a
50
+ * @param terms
51
+ */
52
+ export declare const branches: <T extends Type>(x: FloatTerm, a: Term<T>, ...terms: Term<T>[]) => Term<T>;
53
+ //# sourceMappingURL=branches.d.ts.map
@@ -0,0 +1,16 @@
1
+ import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
2
+ import { float } from "@thi.ng/shader-ast/ast/lit";
3
+ import { lt } from "@thi.ng/shader-ast/ast/ops";
4
+ const branches = (x, a, ...terms) => {
5
+ const n = terms.length + 1;
6
+ if (n < 2)
7
+ return a;
8
+ const delta = 1 / n;
9
+ terms = [a, ...terms];
10
+ return terms.reduceRight(
11
+ (acc, t, i) => ternary(lt(x, float((i + 1) * delta)), t, acc)
12
+ );
13
+ };
14
+ export {
15
+ branches
16
+ };
package/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from "./color/linear-srgb.js";
6
6
  export * from "./color/luminance.js";
7
7
  export * from "./color/porter-duff.js";
8
8
  export * from "./color/rgbe.js";
9
+ export * from "./controlflow/branches.js";
9
10
  export * from "./fog/exp.js";
10
11
  export * from "./fog/exp2.js";
11
12
  export * from "./fog/linear.js";
@@ -27,6 +28,7 @@ export * from "./math/mix-cubic.js";
27
28
  export * from "./math/mix-quadratic.js";
28
29
  export * from "./math/orthogonal.js";
29
30
  export * from "./math/osc.js";
31
+ export * from "./math/pack-float.js";
30
32
  export * from "./math/polar.js";
31
33
  export * from "./math/sincos.js";
32
34
  export * from "./math/smoother-step.js";
package/index.js CHANGED
@@ -6,6 +6,7 @@ export * from "./color/linear-srgb.js";
6
6
  export * from "./color/luminance.js";
7
7
  export * from "./color/porter-duff.js";
8
8
  export * from "./color/rgbe.js";
9
+ export * from "./controlflow/branches.js";
9
10
  export * from "./fog/exp.js";
10
11
  export * from "./fog/exp2.js";
11
12
  export * from "./fog/linear.js";
@@ -27,6 +28,7 @@ export * from "./math/mix-cubic.js";
27
28
  export * from "./math/mix-quadratic.js";
28
29
  export * from "./math/orthogonal.js";
29
30
  export * from "./math/osc.js";
31
+ export * from "./math/pack-float.js";
30
32
  export * from "./math/polar.js";
31
33
  export * from "./math/sincos.js";
32
34
  export * from "./math/smoother-step.js";
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Encodes a float value in [0,1) interval to a vec4 suitable for a 8bit/channel
3
+ * render texture. Use {@link unpackFloat} for reverse op.
4
+ *
5
+ * @remarks
6
+ * Reference:
7
+ * https://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
8
+ */
9
+ export declare const packFloat: import("@thi.ng/shader-ast").TaggedFn1<"float", "vec4">;
10
+ /**
11
+ * Reverse op of {@link packFloat}.
12
+ *
13
+ * @remarks
14
+ * Also see {@link packedNormal2} for related functionality.
15
+ *
16
+ * Reference:
17
+ * https://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
18
+ */
19
+ export declare const unpackFloat: import("@thi.ng/shader-ast").TaggedFn1<"vec4", "float">;
20
+ //# sourceMappingURL=pack-float.d.ts.map
@@ -0,0 +1,21 @@
1
+ import { F, V4 } from "@thi.ng/shader-ast/api/types";
2
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
+ import { vec4 } from "@thi.ng/shader-ast/ast/lit";
4
+ import { mul, sub } from "@thi.ng/shader-ast/ast/ops";
5
+ import { $ } from "@thi.ng/shader-ast/ast/swizzle";
6
+ import { sym } from "@thi.ng/shader-ast/ast/sym";
7
+ import { dot, fract } from "@thi.ng/shader-ast/builtin/math";
8
+ const packFloat = defn(V4, null, [F], (x) => {
9
+ let res;
10
+ return [
11
+ res = sym(fract(mul(vec4(1, 255, 65025, 16581375), x))),
12
+ ret(sub(res, mul($(res, "yzww"), vec4(1 / 255, 1 / 255, 1 / 255, 0))))
13
+ ];
14
+ });
15
+ const unpackFloat = defn(F, null, [V4], (v) => [
16
+ ret(dot(v, vec4(1, 1 / 255, 1 / 65025, 1 / 16581375)))
17
+ ]);
18
+ export {
19
+ packFloat,
20
+ unpackFloat
21
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/shader-ast-stdlib",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -35,11 +35,12 @@
35
35
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
36
36
  "doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
37
37
  "pub": "yarn npm publish --access public",
38
- "test": "bun test"
38
+ "test": "bun test",
39
+ "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
39
40
  },
40
41
  "dependencies": {
41
- "@thi.ng/api": "^8.9.27",
42
- "@thi.ng/shader-ast": "^0.14.0"
42
+ "@thi.ng/api": "^8.9.28",
43
+ "@thi.ng/shader-ast": "^0.15.0"
43
44
  },
44
45
  "devDependencies": {
45
46
  "@microsoft/api-extractor": "^7.40.1",
@@ -85,6 +86,7 @@
85
86
  "./*.js",
86
87
  "./*.d.ts",
87
88
  "color",
89
+ "controlflow",
88
90
  "fog",
89
91
  "isec",
90
92
  "light",
@@ -125,6 +127,9 @@
125
127
  "./color/rgbe": {
126
128
  "default": "./color/rgbe.js"
127
129
  },
130
+ "./controlflow/branches": {
131
+ "default": "./controlflow/branches.js"
132
+ },
128
133
  "./fog/exp": {
129
134
  "default": "./fog/exp.js"
130
135
  },
@@ -191,6 +196,9 @@
191
196
  "./math/osc": {
192
197
  "default": "./math/osc.js"
193
198
  },
199
+ "./math/pack-float": {
200
+ "default": "./math/pack-float.js"
201
+ },
194
202
  "./math/polar": {
195
203
  "default": "./math/polar.js"
196
204
  },
@@ -363,5 +371,5 @@
363
371
  ],
364
372
  "year": 2019
365
373
  },
366
- "gitHead": "f6bb0e172c5dcb574b961f5155a50040d5569685\n"
374
+ "gitHead": "a421058a65ba76608d94129ac29451bfedaf201c\n"
367
375
  }
package/tex/normal.d.ts CHANGED
@@ -15,4 +15,17 @@
15
15
  * @param eps
16
16
  */
17
17
  export declare const normal2: import("@thi.ng/shader-ast").TaggedFn5<"sampler2D", "vec2", "vec2", "float", "float", "vec3">;
18
+ /**
19
+ * Principally the same as {@link normal2}, but using a RGBA 8bit/channel
20
+ * texture storing float values encoded using {@link packFloat}. WHen
21
+ * `packedNormal2()` then samples this textures, each value will be
22
+ * automatically decoded using {@link unpackFloat}.
23
+ *
24
+ * @param tex
25
+ * @param uv
26
+ * @param step
27
+ * @param z
28
+ * @param eps
29
+ */
30
+ export declare const packedNormal2: import("@thi.ng/shader-ast").TaggedFn5<"sampler2D", "vec2", "vec2", "float", "float", "vec3">;
18
31
  //# sourceMappingURL=normal.d.ts.map
package/tex/normal.js CHANGED
@@ -2,32 +2,34 @@ import { F, S2D, V2, V3 } from "@thi.ng/shader-ast/api/types";
2
2
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
3
  import { vec2, vec3 } from "@thi.ng/shader-ast/ast/lit";
4
4
  import { add, sub } from "@thi.ng/shader-ast/ast/ops";
5
- import { sym } from "@thi.ng/shader-ast/ast/sym";
6
5
  import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
6
+ import { sym } from "@thi.ng/shader-ast/ast/sym";
7
7
  import { normalize } from "@thi.ng/shader-ast/builtin/math";
8
8
  import { texture } from "@thi.ng/shader-ast/builtin/texture";
9
- const sample1 = (tex, uv, unit) => sub($x(texture(tex, sub(uv, unit))), $x(texture(tex, add(uv, unit))));
10
- const normal2 = defn(
11
- V3,
12
- null,
13
- [S2D, V2, V2, F, F],
14
- (tex, uv, step, z, eps) => {
15
- let sx;
16
- let sy;
17
- return [
18
- sx = sym(vec2($x(step), 0)),
19
- sy = sym(vec2(0, $y(step))),
20
- ret(
21
- normalize(
22
- add(
23
- vec3(sample1(tex, uv, sx), sample1(tex, uv, sy), z),
24
- eps
25
- )
9
+ import { unpackFloat } from "../math/pack-float.js";
10
+ const __defNormal2 = (decode) => defn(V3, null, [S2D, V2, V2, F, F], (tex, uv, step, z, eps) => {
11
+ let sx;
12
+ let sy;
13
+ const sample1 = (tex2, uv2, unit) => sub(
14
+ decode(texture(tex2, sub(uv2, unit))),
15
+ decode(texture(tex2, add(uv2, unit)))
16
+ );
17
+ return [
18
+ sx = sym(vec2($x(step), 0)),
19
+ sy = sym(vec2(0, $y(step))),
20
+ ret(
21
+ normalize(
22
+ add(
23
+ vec3(sample1(tex, uv, sx), sample1(tex, uv, sy), z),
24
+ eps
26
25
  )
27
26
  )
28
- ];
29
- }
30
- );
27
+ )
28
+ ];
29
+ });
30
+ const normal2 = __defNormal2($x);
31
+ const packedNormal2 = __defNormal2(unpackFloat);
31
32
  export {
32
- normal2
33
+ normal2,
34
+ packedNormal2
33
35
  };