@thi.ng/shader-ast-stdlib 0.12.28 → 0.13.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**: 2022-12-22T21:47:07Z
3
+ - **Last updated**: 2023-01-10T15:20:19Z
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,16 @@ 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.13.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-stdlib@0.13.0) (2023-01-10)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add pre/postmultiplyAlpha() fns ([367ebbd](https://github.com/thi-ng/umbrella/commit/367ebbd))
17
+
18
+ #### 🩹 Bug fixes
19
+
20
+ - fix porterDuff() ops ([f5bbcc8](https://github.com/thi-ng/umbrella/commit/f5bbcc8))
21
+
12
22
  ## [0.12.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/shader-ast-stdlib@0.12.0) (2022-05-07)
13
23
 
14
24
  #### 🚀 Features
package/README.md CHANGED
@@ -101,7 +101,7 @@ For Node.js REPL:
101
101
  const shaderAstStdlib = await import("@thi.ng/shader-ast-stdlib");
102
102
  ```
103
103
 
104
- Package sizes (brotli'd, pre-treeshake): ESM: 10.88 KB
104
+ Package sizes (brotli'd, pre-treeshake): ESM: 10.96 KB
105
105
 
106
106
  ## Dependencies
107
107
 
@@ -502,4 +502,4 @@ If this project contributes to an academic publication, please cite it as:
502
502
 
503
503
  ## License
504
504
 
505
- © 2019 - 2022 Karsten Schmidt // Apache License 2.0
505
+ © 2019 - 2023 Karsten Schmidt // Apache License 2.0
@@ -1,23 +1,30 @@
1
1
  import type { Fn2 } from "@thi.ng/api";
2
- import type { FloatTerm } from "@thi.ng/shader-ast";
2
+ import type { FloatTerm, Vec4Sym } from "@thi.ng/shader-ast";
3
3
  /**
4
4
  * Higher-order Porter-Duff alpha compositing operator. See
5
5
  * [thi.ng/porter-duff](https://thi.ng/porter-duff) for reference. Returns an
6
6
  * optimized AST function which accepts 2 RGBA colors (vec4) and returns blended
7
- * & clamped result (also a vec4). All built-in PD operators are defined via
8
- * this HOF.
7
+ * & clamped result (also a vec4).
9
8
  *
10
- * The two given JS functions are used to extract blending coefficients for
11
- * src/dest colors and are called with the alpha components of both colors.
9
+ * @remark
10
+ * All built-in PD operators are defined via this HOF. The two given coefficient
11
+ * functions are used to extract blending coefficients for src/dest colors and
12
+ * are called with the alpha components of both colors.
12
13
  *
13
14
  * Optimization only happens for cases where either `fa` and/or `fb` are
14
- * {@link ZERO}.
15
+ * {@link ZERO} or {@link ONE}.
16
+ *
17
+ * *IMPORTANT*: Both colors MUST be use pre-multiplied alpha for correct
18
+ * results. If needed, use {@link premultiplyAlpha} and
19
+ * {@link postmultiplyAlpha}.
15
20
  *
16
21
  * @param name - function name
17
22
  * @param fa - src coeff fn
18
23
  * @param fb - dest coeff fn
19
24
  */
20
25
  export declare const porterDuff: (name: string, fa: Fn2<FloatTerm, FloatTerm, FloatTerm>, fb: Fn2<FloatTerm, FloatTerm, FloatTerm>) => import("@thi.ng/shader-ast").TaggedFn2<"vec4", "vec4", "vec4">;
26
+ export declare const premultiplyAlpha: (col: Vec4Sym) => import("@thi.ng/shader-ast").Lit<"vec4">;
27
+ export declare const postmultiplyAlpha: (col: Vec4Sym) => import("@thi.ng/shader-ast").Lit<"vec4">;
21
28
  export declare const ZERO: () => import("@thi.ng/shader-ast").Lit<"float">;
22
29
  export declare const ONE: () => import("@thi.ng/shader-ast").Lit<"float">;
23
30
  export declare const A: (a: FloatTerm) => FloatTerm;
@@ -1,37 +1,47 @@
1
1
  import { defn, ret } from "@thi.ng/shader-ast/ast/function";
2
2
  import { FLOAT0, FLOAT1, vec4 } from "@thi.ng/shader-ast/ast/lit";
3
- import { add, mul, sub } from "@thi.ng/shader-ast/ast/ops";
4
- import { $w } from "@thi.ng/shader-ast/ast/swizzle";
3
+ import { add, div, mul, sub } from "@thi.ng/shader-ast/ast/ops";
4
+ import { $w, $xyz } from "@thi.ng/shader-ast/ast/swizzle";
5
5
  import { clamp01 } from "../math/clamp.js";
6
- const coeff = (f, a, b) => (f === ZERO ? FLOAT0 : f === ONE ? a : mul(a, f($w(a), $w(b))));
6
+ /** @internal */
7
+ const __coeff = (col, f) => f === FLOAT0 ? vec4() : f === FLOAT1 ? col : mul(col, f);
7
8
  /**
8
9
  * Higher-order Porter-Duff alpha compositing operator. See
9
10
  * [thi.ng/porter-duff](https://thi.ng/porter-duff) for reference. Returns an
10
11
  * optimized AST function which accepts 2 RGBA colors (vec4) and returns blended
11
- * & clamped result (also a vec4). All built-in PD operators are defined via
12
- * this HOF.
12
+ * & clamped result (also a vec4).
13
13
  *
14
- * The two given JS functions are used to extract blending coefficients for
15
- * src/dest colors and are called with the alpha components of both colors.
14
+ * @remark
15
+ * All built-in PD operators are defined via this HOF. The two given coefficient
16
+ * functions are used to extract blending coefficients for src/dest colors and
17
+ * are called with the alpha components of both colors.
16
18
  *
17
19
  * Optimization only happens for cases where either `fa` and/or `fb` are
18
- * {@link ZERO}.
20
+ * {@link ZERO} or {@link ONE}.
21
+ *
22
+ * *IMPORTANT*: Both colors MUST be use pre-multiplied alpha for correct
23
+ * results. If needed, use {@link premultiplyAlpha} and
24
+ * {@link postmultiplyAlpha}.
19
25
  *
20
26
  * @param name - function name
21
27
  * @param fa - src coeff fn
22
28
  * @param fb - dest coeff fn
23
29
  */
24
30
  export const porterDuff = (name, fa, fb) => defn("vec4", name, ["vec4", "vec4"], (a, b) => {
25
- const src = coeff(fa, a, b);
26
- const dest = coeff(fb, a, b);
27
- const srcZero = src === FLOAT0;
28
- const destZero = dest === FLOAT0;
31
+ const sa = fa($w(a), $w(b));
32
+ const sb = fb($w(a), $w(b));
33
+ const src = __coeff(a, sa);
34
+ const dest = __coeff(b, sb);
35
+ const srcZero = sa === FLOAT0;
36
+ const destZero = sb === FLOAT0;
29
37
  return [
30
38
  ret(srcZero && destZero
31
39
  ? vec4()
32
40
  : clamp01(srcZero ? dest : destZero ? src : add(src, dest))),
33
41
  ];
34
42
  });
43
+ export const premultiplyAlpha = (col) => vec4(mul($xyz(col), $w(col)), $w(col));
44
+ export const postmultiplyAlpha = (col) => vec4(div($xyz(col), $w(col)), $w(col));
35
45
  // coefficient functions
36
46
  export const ZERO = () => FLOAT0;
37
47
  export const ONE = () => FLOAT1;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/shader-ast-stdlib",
3
- "version": "0.12.28",
3
+ "version": "0.13.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",
@@ -34,12 +34,12 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.6.2",
38
- "@thi.ng/shader-ast": "^0.12.35"
37
+ "@thi.ng/api": "^8.6.3",
38
+ "@thi.ng/shader-ast": "^0.12.37"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@microsoft/api-extractor": "^7.33.7",
42
- "@thi.ng/testament": "^0.3.8",
42
+ "@thi.ng/testament": "^0.3.9",
43
43
  "rimraf": "^3.0.2",
44
44
  "tools": "^0.0.1",
45
45
  "typedoc": "^0.23.22",
@@ -330,5 +330,5 @@
330
330
  ],
331
331
  "year": 2019
332
332
  },
333
- "gitHead": "bc6f7f5e2765bb96fe64db804eaf4b2443b47fc6\n"
333
+ "gitHead": "3f0b3e2a7c82aefc7e46fb4338369836b5e1b8cf\n"
334
334
  }