@thi.ng/shader-ast-stdlib 0.15.0 → 0.16.1

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**: 2023-10-25T12:40:55Z
3
+ - **Last updated**: 2023-10-27T20:44:01Z
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,18 @@ 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.16.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-stdlib@0.16.1) (2023-10-27)
13
+
14
+ #### 🩹 Bug fixes
15
+
16
+ - fix pkg exports ([20d5d2d](https://github.com/thi-ng/umbrella/commit/20d5d2d))
17
+
18
+ ## [0.16.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-stdlib@0.16.0) (2023-10-27)
19
+
20
+ #### 🚀 Features
21
+
22
+ - add trunc(), modulo(), foldback01() ([d3ab3e6](https://github.com/thi-ng/umbrella/commit/d3ab3e6))
23
+
12
24
  ## [0.15.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-stdlib@0.15.0) (2023-10-25)
13
25
 
14
26
  #### 🚀 Features
package/README.md CHANGED
@@ -105,7 +105,7 @@ For Node.js REPL:
105
105
  const shaderAstStdlib = await import("@thi.ng/shader-ast-stdlib");
106
106
  ```
107
107
 
108
- Package sizes (brotli'd, pre-treeshake): ESM: 13.20 KB
108
+ Package sizes (brotli'd, pre-treeshake): ESM: 13.42 KB
109
109
 
110
110
  ## Dependencies
111
111
 
package/index.d.ts CHANGED
@@ -18,6 +18,7 @@ export * from "./math/cross2.js";
18
18
  export * from "./math/dist-chebyshev.js";
19
19
  export * from "./math/dist-manhattan.js";
20
20
  export * from "./math/fit.js";
21
+ export * from "./math/interval.js";
21
22
  export * from "./math/magsq.js";
22
23
  export * from "./math/maxcomp.js";
23
24
  export * from "./math/mincomp.js";
package/index.js CHANGED
@@ -18,6 +18,7 @@ export * from "./math/cross2.js";
18
18
  export * from "./math/dist-chebyshev.js";
19
19
  export * from "./math/dist-manhattan.js";
20
20
  export * from "./math/fit.js";
21
+ export * from "./math/interval.js";
21
22
  export * from "./math/magsq.js";
22
23
  export * from "./math/maxcomp.js";
23
24
  export * from "./math/mincomp.js";
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Predicate function to check if given point `p` is inside the circle defined
3
+ * by `pos` and `radius`.
4
+ */
5
+ export declare const isPointInCircle: import("@thi.ng/shader-ast").TaggedFn3<"vec2", "vec2", "float", "bool">;
6
+ /**
7
+ * Predicate function to check if given point `p` is inside the axis-aligned
8
+ * rect defined by `pos` and `size`. Branchless.
9
+ */
10
+ export declare const isPointInRect: import("@thi.ng/shader-ast").TaggedFn3<"vec2", "vec2", "vec2", "bool">;
11
+ //# sourceMappingURL=point.d.ts.map
package/isec/point.js ADDED
@@ -0,0 +1,23 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
2
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
3
+ import { bool } from "@thi.ng/shader-ast/ast/lit";
4
+ import { add } from "@thi.ng/shader-ast/ast/ops";
5
+ import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
6
+ import { sym } from "@thi.ng/shader-ast/ast/sym";
7
+ import { distance, min, step } from "@thi.ng/shader-ast/builtin/math";
8
+ /**
9
+ * Predicate function to check if given point `p` is inside the circle defined
10
+ * by `pos` and `radius`.
11
+ */
12
+ export const isPointInCircle = defn("bool", null, [V2, V2, F], (p, pos, radius) => [ret(bool(step(distance(p, pos), radius)))]);
13
+ /**
14
+ * Predicate function to check if given point `p` is inside the axis-aligned
15
+ * rect defined by `pos` and `size`. Branchless.
16
+ */
17
+ export const isPointInRect = defn("bool", null, [V2, V2, V2], (p, pos, size) => {
18
+ let q;
19
+ return [
20
+ (q = sym(min(step(pos, p), step(p, add(pos, size))))),
21
+ ret(bool(min($x(q), $y(q)))),
22
+ ];
23
+ });
package/math/fit.d.ts CHANGED
@@ -8,6 +8,17 @@ import type { PrimTerm, Term, TermType } from "@thi.ng/shader-ast";
8
8
  * @param b -
9
9
  */
10
10
  export declare const fitNorm1: import("@thi.ng/shader-ast").TaggedFn3<"float", "float", "float", "float">;
11
+ /**
12
+ * Similar to {@link fitNorm1} but also for vector types and without checking if
13
+ * `a == b`. Scales value `x` from closed interval [a,b] to closed [0,1]
14
+ * interval. No clamping performed.
15
+ *
16
+ * @param x
17
+ * @param a
18
+ * @param b
19
+ * @returns
20
+ */
21
+ export declare const fitNorm: <T extends PrimTerm>(x: T, a: T, b: T) => Term<TermType<T>>;
11
22
  /**
12
23
  * Fits value `x` from closed interval [a,b] to closed interval [c,d]. No
13
24
  * clamping performed.
@@ -19,16 +30,6 @@ export declare const fitNorm1: import("@thi.ng/shader-ast").TaggedFn3<"float", "
19
30
  * @param d -
20
31
  */
21
32
  export declare const fit: <T extends PrimTerm>(x: T, a: T, b: T, c: T, d: T) => Term<TermType<T>>;
22
- /**
23
- * Scales value `x` from closed interval [a,b] to closed [0,1] interval. No
24
- * clamping performed.
25
- *
26
- * @param x
27
- * @param a
28
- * @param b
29
- * @returns
30
- */
31
- export declare const fitNorm: <T extends PrimTerm>(x: T, a: T, b: T) => Term<TermType<T>>;
32
33
  /**
33
34
  * Same as {@link fit}, but first clamps `x` to closed [a,b] interval.
34
35
  *
package/math/fit.js CHANGED
@@ -16,6 +16,17 @@ import { clamp01 } from "./clamp.js";
16
16
  export const fitNorm1 = defn(F, "fitNorm1", [F, F, F], (x, a, b) => [
17
17
  ret(ternary(neq(a, b), div(sub(x, a), sub(b, a)), FLOAT0)),
18
18
  ]);
19
+ /**
20
+ * Similar to {@link fitNorm1} but also for vector types and without checking if
21
+ * `a == b`. Scales value `x` from closed interval [a,b] to closed [0,1]
22
+ * interval. No clamping performed.
23
+ *
24
+ * @param x
25
+ * @param a
26
+ * @param b
27
+ * @returns
28
+ */
29
+ export const fitNorm = (x, a, b) => div(sub(x, a), sub(b, a));
19
30
  /**
20
31
  * Fits value `x` from closed interval [a,b] to closed interval [c,d]. No
21
32
  * clamping performed.
@@ -27,16 +38,6 @@ export const fitNorm1 = defn(F, "fitNorm1", [F, F, F], (x, a, b) => [
27
38
  * @param d -
28
39
  */
29
40
  export const fit = (x, a, b, c, d) => mix(c, d, fitNorm(x, a, b));
30
- /**
31
- * Scales value `x` from closed interval [a,b] to closed [0,1] interval. No
32
- * clamping performed.
33
- *
34
- * @param x
35
- * @param a
36
- * @param b
37
- * @returns
38
- */
39
- export const fitNorm = (x, a, b) => div(sub(x, a), sub(b, a));
40
41
  /**
41
42
  * Same as {@link fit}, but first clamps `x` to closed [a,b] interval.
42
43
  *
@@ -0,0 +1,17 @@
1
+ /**
2
+ * GLSL 100 trunc() polyfill.
3
+ */
4
+ export declare const trunc: import("@thi.ng/shader-ast").TaggedFn1<"float", "float">;
5
+ /**
6
+ * Returns `x - y * trunc(x / y)`, i.e. essentially the same as JS `%` operator.
7
+ * Result will always have the sign of `x`.
8
+ */
9
+ export declare const modulo: import("@thi.ng/shader-ast").TaggedFn2<"float", "float", "float">;
10
+ /**
11
+ * Same as thi.ng/math foldback01(). Folds `x` into the closed [0..1] interval,
12
+ * using infinite internal reflection on either side of the interval.
13
+ *
14
+ * @param x
15
+ */
16
+ export declare const foldback01: import("@thi.ng/shader-ast").TaggedFn1<"float", "float">;
17
+ //# sourceMappingURL=interval.d.ts.map
@@ -0,0 +1,33 @@
1
+ import { F } from "@thi.ng/shader-ast/api/types";
2
+ import { ternary } from "@thi.ng/shader-ast/ast/controlflow";
3
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
4
+ import { FLOAT0, FLOAT1, FLOAT2 } from "@thi.ng/shader-ast/ast/lit";
5
+ import { div, lt, mul, sub } from "@thi.ng/shader-ast/ast/ops";
6
+ import { sym } from "@thi.ng/shader-ast/ast/sym";
7
+ import { abs, ceil, floor, mix, mod, step, } from "@thi.ng/shader-ast/builtin/math";
8
+ /**
9
+ * GLSL 100 trunc() polyfill.
10
+ */
11
+ export const trunc = defn(F, null, [F], (x) => [
12
+ ret(ternary(lt(x, FLOAT0), ceil(x), floor(x))),
13
+ ]);
14
+ /**
15
+ * Returns `x - y * trunc(x / y)`, i.e. essentially the same as JS `%` operator.
16
+ * Result will always have the sign of `x`.
17
+ */
18
+ export const modulo = defn(F, null, [F, F], (x, y) => [
19
+ ret(sub(x, mul(y, trunc(div(x, y))))),
20
+ ]);
21
+ /**
22
+ * Same as thi.ng/math foldback01(). Folds `x` into the closed [0..1] interval,
23
+ * using infinite internal reflection on either side of the interval.
24
+ *
25
+ * @param x
26
+ */
27
+ export const foldback01 = defn(F, null, [F], (x) => {
28
+ let y;
29
+ return [
30
+ (y = sym(mod(abs(x), FLOAT2))),
31
+ ret(mix(y, sub(FLOAT2, y), step(FLOAT1, y))),
32
+ ];
33
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/shader-ast-stdlib",
3
- "version": "0.15.0",
3
+ "version": "0.16.1",
4
4
  "description": "Function collection for modular GPGPU / shader programming with @thi.ng/shader-ast",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -38,7 +38,7 @@
38
38
  },
39
39
  "dependencies": {
40
40
  "@thi.ng/api": "^8.9.6",
41
- "@thi.ng/shader-ast": "^0.12.75"
41
+ "@thi.ng/shader-ast": "^0.12.76"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@microsoft/api-extractor": "^7.38.0",
@@ -86,6 +86,7 @@
86
86
  "./*.d.ts",
87
87
  "color",
88
88
  "fog",
89
+ "isec",
89
90
  "light",
90
91
  "math",
91
92
  "matrix",
@@ -93,7 +94,8 @@
93
94
  "raymarch",
94
95
  "screen",
95
96
  "sdf",
96
- "tex"
97
+ "tex",
98
+ "viz"
97
99
  ],
98
100
  "exports": {
99
101
  ".": {
@@ -162,6 +164,9 @@
162
164
  "./math/fit": {
163
165
  "default": "./math/fit.js"
164
166
  },
167
+ "./math/interval": {
168
+ "default": "./math/interval.js"
169
+ },
165
170
  "./math/magsq": {
166
171
  "default": "./math/magsq.js"
167
172
  },
@@ -349,5 +354,5 @@
349
354
  ],
350
355
  "year": 2019
351
356
  },
352
- "gitHead": "fc6e14052ed24bf9196896980ceb9f398b6ea6c1\n"
357
+ "gitHead": "f540c4a501157b764229035035c2a858bace2a61\n"
353
358
  }
@@ -0,0 +1,68 @@
1
+ import type { Func1, Func2, Vec2Term } from "@thi.ng/shader-ast";
2
+ export interface FnSampleOpts {
3
+ /**
4
+ * Pixel radius per axis. The function will be sampled at ± N around the
5
+ * given base fragment position.
6
+ *
7
+ * @defaultValue 1
8
+ */
9
+ radius: number;
10
+ /**
11
+ * Number of sub-fragment steps per axis. The function will be sampled at ±
12
+ * {@link FnSampleOpts.radius} around the given base fragment position.
13
+ *
14
+ * @defaultValue 4
15
+ */
16
+ steps: number;
17
+ /**
18
+ * If true, the {@link functionSampler} will return 1.0 for the entire area
19
+ * below the curve (i.e. turns a line plot into an area plot). By default
20
+ * only the fragments touching the curve will produce non-zero results.
21
+ *
22
+ * @defaultValue false
23
+ */
24
+ area: boolean;
25
+ /**
26
+ * Min X position (in function domain). The function will only be evaluated
27
+ * within the [min..max] domain.
28
+ *
29
+ * @defaultValue 0
30
+ */
31
+ min: number;
32
+ /**
33
+ * Max X position (in function domain). The function will only be evaluated
34
+ * within the [min..max] domain.
35
+ *
36
+ * @defaultValue 1
37
+ */
38
+ max: number;
39
+ }
40
+ /**
41
+ * Higher order function. Takes a unary 1D function `fn` to evaluate and plot
42
+ * within a given interval. Also takes a point mapping function `map` to
43
+ * transform fragment coordinates into the function domain. Returns a new
44
+ * specialized function, which then accepts 2x vec2 args: a fragment coordinate
45
+ * and overall viewport dimensions. It then transforms the point into the
46
+ * function domain and evaluates/samples the function `fn` in the sub-pixel
47
+ * region around the input position. Returns a coverage ratio of how many
48
+ * samples where on the curve.
49
+ *
50
+ * @remarks
51
+ * The function domain, sampling region & quality can be configured via options.
52
+ * See {@link FnSampleOpts} for details.
53
+ *
54
+ * See {@link functionDomainMapper} for a configurable point mapping function.
55
+ */
56
+ export declare const functionSampler: (fn: Func1<"float", "float">, map: Func2<"vec2", "vec2", "vec2">, opts?: Partial<FnSampleOpts>) => import("@thi.ng/shader-ast").TaggedFn2<"vec2", "vec2", "float">;
57
+ /**
58
+ * Higher order helper function for {@link functionSampler} to map fragment
59
+ * coordinates within a screen rect (defined by `pos` & `size` in UV space) into
60
+ * the domain of the sampled function interval.
61
+ *
62
+ * @param amin
63
+ * @param amax
64
+ * @param bmin
65
+ * @param bmax
66
+ */
67
+ export declare const functionDomainMapper: (amin: Vec2Term, amax: Vec2Term, bmin?: Vec2Term, bmax?: Vec2Term) => import("@thi.ng/shader-ast").TaggedFn2<"vec2", "vec2", "vec2">;
68
+ //# sourceMappingURL=function.d.ts.map
@@ -0,0 +1,77 @@
1
+ import { F, V2 } from "@thi.ng/shader-ast/api/types";
2
+ import { assign } from "@thi.ng/shader-ast/ast/assign";
3
+ import { forLoop, ifThen } from "@thi.ng/shader-ast/ast/controlflow";
4
+ import { defn, ret } from "@thi.ng/shader-ast/ast/function";
5
+ import { FLOAT0, FLOAT2, VEC2_0, VEC2_1, float, vec2, } from "@thi.ng/shader-ast/ast/lit";
6
+ import { add, div, gt, inc, lt, lte, or, sub, } from "@thi.ng/shader-ast/ast/ops";
7
+ import { $x, $y } from "@thi.ng/shader-ast/ast/swizzle";
8
+ import { sym } from "@thi.ng/shader-ast/ast/sym";
9
+ import { fit } from "../math/fit.js";
10
+ /**
11
+ * Higher order function. Takes a unary 1D function `fn` to evaluate and plot
12
+ * within a given interval. Also takes a point mapping function `map` to
13
+ * transform fragment coordinates into the function domain. Returns a new
14
+ * specialized function, which then accepts 2x vec2 args: a fragment coordinate
15
+ * and overall viewport dimensions. It then transforms the point into the
16
+ * function domain and evaluates/samples the function `fn` in the sub-pixel
17
+ * region around the input position. Returns a coverage ratio of how many
18
+ * samples where on the curve.
19
+ *
20
+ * @remarks
21
+ * The function domain, sampling region & quality can be configured via options.
22
+ * See {@link FnSampleOpts} for details.
23
+ *
24
+ * See {@link functionDomainMapper} for a configurable point mapping function.
25
+ */
26
+ export const functionSampler = (fn, map, opts) => {
27
+ const { min, max, radius, step, area } = {
28
+ min: 0,
29
+ max: 1,
30
+ radius: 1,
31
+ step: 4,
32
+ area: false,
33
+ ...opts,
34
+ };
35
+ return defn(F, undefined, [V2, V2], (frag, res) => {
36
+ let count, total;
37
+ let q;
38
+ const invStep = float((2 * radius) / step);
39
+ return [
40
+ (q = sym(map(frag, res))),
41
+ // bail out early if outside [min..max] interval
42
+ ifThen(or(lt($x(q), float(min)), gt($x(q), float(max))), [
43
+ ret(FLOAT0),
44
+ ]),
45
+ (count = sym(FLOAT0)),
46
+ (total = sym(FLOAT0)),
47
+ forLoop(sym(float(-radius)), (x) => lte(x, float(radius)), (x) => assign(x, add(x, invStep)), (x) => [
48
+ forLoop(sym(float(-radius)), (y) => lte(y, float(radius)), (y) => assign(y, add(y, invStep)), (y) => [
49
+ inc(total),
50
+ assign(q, map(add(frag, vec2(x, y)), res)),
51
+ ifThen(gt(fn($x(q)), $y(q)), [inc(count)]),
52
+ ]),
53
+ ]),
54
+ ...(!area
55
+ ? [
56
+ ifThen(gt(count, div(total, FLOAT2)), [
57
+ assign(count, sub(total, count)),
58
+ ]),
59
+ ]
60
+ : []),
61
+ ret(div(count, total)),
62
+ ];
63
+ });
64
+ };
65
+ /**
66
+ * Higher order helper function for {@link functionSampler} to map fragment
67
+ * coordinates within a screen rect (defined by `pos` & `size` in UV space) into
68
+ * the domain of the sampled function interval.
69
+ *
70
+ * @param amin
71
+ * @param amax
72
+ * @param bmin
73
+ * @param bmax
74
+ */
75
+ export const functionDomainMapper = (amin, amax, bmin = VEC2_0, bmax = VEC2_1) => defn(V2, undefined, [V2, V2], (p, res) => [
76
+ ret(fit(div(p, res), amin, amax, bmin, bmax)),
77
+ ]);