@thi.ng/shader-ast-js 1.0.26 → 1.0.28

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/env.js CHANGED
@@ -5,7 +5,14 @@ import { ZERO3, ZERO4 } from "@thi.ng/vectors/api";
5
5
  import { fromBVec2, fromBVec3, fromBVec4 } from "@thi.ng/vectors/convert";
6
6
  import { setVN3, setVN4 } from "@thi.ng/vectors/setvn";
7
7
  import { setVV4 } from "@thi.ng/vectors/setvv";
8
- import { setSwizzle2, setSwizzle3, setSwizzle4, swizzle2, swizzle3, swizzle4, } from "@thi.ng/vectors/swizzle";
8
+ import {
9
+ setSwizzle2,
10
+ setSwizzle3,
11
+ setSwizzle4,
12
+ swizzle2,
13
+ swizzle3,
14
+ swizzle4
15
+ } from "@thi.ng/vectors/swizzle";
9
16
  import { BVEC2, BVEC3, BVEC4 } from "./env/bvec.js";
10
17
  import { FLOAT } from "./env/float.js";
11
18
  import { INT } from "./env/int.js";
@@ -22,7 +29,17 @@ import { UVEC4 } from "./env/uvec4.js";
22
29
  import { VEC2 } from "./env/vec2.js";
23
30
  import { VEC3 } from "./env/vec3.js";
24
31
  import { VEC4 } from "./env/vec4.js";
25
- import { POOL_IVEC2, POOL_IVEC3, POOL_IVEC4, POOL_UVEC2, POOL_UVEC3, POOL_UVEC4, POOL_VEC2, POOL_VEC3, POOL_VEC4, } from "./pool.js";
32
+ import {
33
+ POOL_IVEC2,
34
+ POOL_IVEC3,
35
+ POOL_IVEC4,
36
+ POOL_UVEC2,
37
+ POOL_UVEC3,
38
+ POOL_UVEC4,
39
+ POOL_VEC2,
40
+ POOL_VEC3,
41
+ POOL_VEC4
42
+ } from "./pool.js";
26
43
  const { next: $2, uniform: $n2 } = POOL_VEC2;
27
44
  const { next: $3, uniform: $n3 } = POOL_VEC3;
28
45
  const { next: $4, uniform: $n4 } = POOL_VEC4;
@@ -32,100 +49,101 @@ const { next: $i4, uniform: $ni4 } = POOL_IVEC4;
32
49
  const { next: $u2, uniform: $nu2 } = POOL_UVEC2;
33
50
  const { next: $u3, uniform: $nu3 } = POOL_UVEC3;
34
51
  const { next: $u4, uniform: $nu4 } = POOL_UVEC4;
35
- // TODO texture lookups
36
- // all texture fns currently return [0,0,0,0] or 0
37
52
  const SAMPLER_TODO = {
38
- texelFetch: () => ZERO4,
39
- texelFetchOffset: () => ZERO4,
40
- texture: () => ZERO4,
41
- texturen: () => 0,
42
- textureGrad: () => ZERO4,
43
- textureGradn: () => 0,
44
- textureLod: () => ZERO4,
45
- textureLodn: () => 0,
46
- textureOffset: () => ZERO4,
47
- textureOffsetn: () => 0,
48
- textureProj: () => ZERO4,
49
- textureProjn: () => 0,
50
- textureSize: () => ZERO3,
53
+ texelFetch: () => ZERO4,
54
+ texelFetchOffset: () => ZERO4,
55
+ texture: () => ZERO4,
56
+ texturen: () => 0,
57
+ textureGrad: () => ZERO4,
58
+ textureGradn: () => 0,
59
+ textureLod: () => ZERO4,
60
+ textureLodn: () => 0,
61
+ textureOffset: () => ZERO4,
62
+ textureOffsetn: () => 0,
63
+ textureProj: () => ZERO4,
64
+ textureProjn: () => 0,
65
+ textureSize: () => ZERO3
51
66
  };
52
- export const JS_DEFAULT_ENV = {
53
- vec2: VEC2(POOL_VEC2),
54
- vec2b: (v) => fromBVec2($2(), v),
55
- vec2i: identity,
56
- vec2n: $n2,
57
- vec2u: identity,
58
- vec3: VEC3(POOL_VEC3),
59
- vec3b: (v) => fromBVec3($3(), v),
60
- vec3i: identity,
61
- vec3n: $n3,
62
- vec3u: identity,
63
- vec3vn: (a, n) => setVN3($3(), a, n),
64
- vec4: VEC4(POOL_VEC4),
65
- vec4b: (v) => fromBVec4($4(), v),
66
- vec4i: identity,
67
- vec4n: $n4,
68
- vec4u: identity,
69
- vec4vn: (a, n) => setVN4($4(), a, n),
70
- vec4vnn: (a, z, w) => setVV4($4(), a, [z, w]),
71
- vec4vv: (a, b) => setVV4($4(), a, b),
72
- mat2n: (n) => mat22n($4(), n),
73
- mat2vv: (a, b) => mat22v($4(), a, b),
74
- mat3n: (n) => mat33n([], n),
75
- mat3vvv: (a, b, c) => mat33v([], a, b, c),
76
- mat4n: (n) => mat44n([], n),
77
- mat4vvvv: (a, b, c, d) => mat44v([], a, b, c, d),
78
- swizzle2: (a, b, c) => swizzle2($2(), a, b, c),
79
- swizzle3: (a, b, c, d) => swizzle3($3(), a, b, c, d),
80
- swizzle4: (a, b, c, d, e) => swizzle4($4(), a, b, c, d, e),
81
- set_swizzle2: setSwizzle2,
82
- set_swizzle3: setSwizzle3,
83
- set_swizzle4: setSwizzle4,
84
- float: FLOAT,
85
- int: INT,
86
- uint: UINT,
87
- bvec2: BVEC2,
88
- bvec2n: (n) => ((n = !!n), [n, n]),
89
- bvec3: BVEC3,
90
- bvec3n: (n) => ((n = !!n), [n, n, n]),
91
- bvec4: BVEC4,
92
- bvec4n: (n) => ((n = !!n), [n, n, n, n]),
93
- ivec2: IVEC2,
94
- ivec2b: (v) => fromBVec2($i2(), v),
95
- ivec2n: $ni2,
96
- ivec3: IVEC3,
97
- ivec3b: (v) => fromBVec3($i3(), v),
98
- ivec3n: $ni3,
99
- ivec4: IVEC4,
100
- ivec4b: (v) => fromBVec4($i4(), v),
101
- ivec4n: $ni4,
102
- uvec2: UVEC2,
103
- uvec2b: (v) => fromBVec2($u2(), v),
104
- uvec2n: $nu2,
105
- uvec3: UVEC3,
106
- uvec3b: (v) => fromBVec3($u3(), v),
107
- uvec3n: $nu3,
108
- uvec4: UVEC4,
109
- uvec4b: (v) => fromBVec4($u4(), v),
110
- uvec4n: $nu4,
111
- mat2: MAT2,
112
- mat3: MAT3,
113
- mat4: MAT4,
114
- sampler1D: SAMPLER_TODO,
115
- sampler2D: SAMPLER_TODO,
116
- sampler3D: SAMPLER_TODO,
117
- samplerCube: SAMPLER_TODO,
118
- sampler2DShadow: SAMPLER_TODO,
119
- samplerCubeShadow: SAMPLER_TODO,
120
- pools: {
121
- vec2: POOL_VEC2,
122
- vec3: POOL_VEC3,
123
- vec4: POOL_VEC4,
124
- ivec2: POOL_IVEC2,
125
- ivec3: POOL_IVEC3,
126
- ivec4: POOL_IVEC4,
127
- uvec2: POOL_UVEC2,
128
- uvec3: POOL_UVEC3,
129
- uvec4: POOL_UVEC4,
130
- },
67
+ const JS_DEFAULT_ENV = {
68
+ vec2: VEC2(POOL_VEC2),
69
+ vec2b: (v) => fromBVec2($2(), v),
70
+ vec2i: identity,
71
+ vec2n: $n2,
72
+ vec2u: identity,
73
+ vec3: VEC3(POOL_VEC3),
74
+ vec3b: (v) => fromBVec3($3(), v),
75
+ vec3i: identity,
76
+ vec3n: $n3,
77
+ vec3u: identity,
78
+ vec3vn: (a, n) => setVN3($3(), a, n),
79
+ vec4: VEC4(POOL_VEC4),
80
+ vec4b: (v) => fromBVec4($4(), v),
81
+ vec4i: identity,
82
+ vec4n: $n4,
83
+ vec4u: identity,
84
+ vec4vn: (a, n) => setVN4($4(), a, n),
85
+ vec4vnn: (a, z, w) => setVV4($4(), a, [z, w]),
86
+ vec4vv: (a, b) => setVV4($4(), a, b),
87
+ mat2n: (n) => mat22n($4(), n),
88
+ mat2vv: (a, b) => mat22v($4(), a, b),
89
+ mat3n: (n) => mat33n([], n),
90
+ mat3vvv: (a, b, c) => mat33v([], a, b, c),
91
+ mat4n: (n) => mat44n([], n),
92
+ mat4vvvv: (a, b, c, d) => mat44v([], a, b, c, d),
93
+ swizzle2: (a, b, c) => swizzle2($2(), a, b, c),
94
+ swizzle3: (a, b, c, d) => swizzle3($3(), a, b, c, d),
95
+ swizzle4: (a, b, c, d, e) => swizzle4($4(), a, b, c, d, e),
96
+ set_swizzle2: setSwizzle2,
97
+ set_swizzle3: setSwizzle3,
98
+ set_swizzle4: setSwizzle4,
99
+ float: FLOAT,
100
+ int: INT,
101
+ uint: UINT,
102
+ bvec2: BVEC2,
103
+ bvec2n: (n) => (n = !!n, [n, n]),
104
+ bvec3: BVEC3,
105
+ bvec3n: (n) => (n = !!n, [n, n, n]),
106
+ bvec4: BVEC4,
107
+ bvec4n: (n) => (n = !!n, [n, n, n, n]),
108
+ ivec2: IVEC2,
109
+ ivec2b: (v) => fromBVec2($i2(), v),
110
+ ivec2n: $ni2,
111
+ ivec3: IVEC3,
112
+ ivec3b: (v) => fromBVec3($i3(), v),
113
+ ivec3n: $ni3,
114
+ ivec4: IVEC4,
115
+ ivec4b: (v) => fromBVec4($i4(), v),
116
+ ivec4n: $ni4,
117
+ uvec2: UVEC2,
118
+ uvec2b: (v) => fromBVec2($u2(), v),
119
+ uvec2n: $nu2,
120
+ uvec3: UVEC3,
121
+ uvec3b: (v) => fromBVec3($u3(), v),
122
+ uvec3n: $nu3,
123
+ uvec4: UVEC4,
124
+ uvec4b: (v) => fromBVec4($u4(), v),
125
+ uvec4n: $nu4,
126
+ mat2: MAT2,
127
+ mat3: MAT3,
128
+ mat4: MAT4,
129
+ sampler1D: SAMPLER_TODO,
130
+ sampler2D: SAMPLER_TODO,
131
+ sampler3D: SAMPLER_TODO,
132
+ samplerCube: SAMPLER_TODO,
133
+ sampler2DShadow: SAMPLER_TODO,
134
+ samplerCubeShadow: SAMPLER_TODO,
135
+ pools: {
136
+ vec2: POOL_VEC2,
137
+ vec3: POOL_VEC3,
138
+ vec4: POOL_VEC4,
139
+ ivec2: POOL_IVEC2,
140
+ ivec3: POOL_IVEC3,
141
+ ivec4: POOL_IVEC4,
142
+ uvec2: POOL_UVEC2,
143
+ uvec3: POOL_UVEC3,
144
+ uvec4: POOL_UVEC4
145
+ }
146
+ };
147
+ export {
148
+ JS_DEFAULT_ENV
131
149
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/shader-ast-js",
3
- "version": "1.0.26",
3
+ "version": "1.0.28",
4
4
  "description": "Customizable JS codegen, compiler & runtime for @thi.ng/shader-ast",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -24,7 +24,9 @@
24
24
  "author": "Karsten Schmidt (https://thi.ng)",
25
25
  "license": "Apache-2.0",
26
26
  "scripts": {
27
- "build": "yarn clean && tsc --declaration",
27
+ "build": "yarn build:esbuild && yarn build:decl",
28
+ "build:decl": "tsc --declaration --emitDeclarationOnly",
29
+ "build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
28
30
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc env",
29
31
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
32
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
@@ -33,17 +35,18 @@
33
35
  "test": "bun test"
34
36
  },
35
37
  "dependencies": {
36
- "@thi.ng/api": "^8.9.11",
37
- "@thi.ng/checks": "^3.4.11",
38
- "@thi.ng/errors": "^2.4.5",
39
- "@thi.ng/math": "^5.7.6",
40
- "@thi.ng/matrices": "^2.2.12",
41
- "@thi.ng/pixel": "^5.0.3",
42
- "@thi.ng/shader-ast": "^0.12.84",
43
- "@thi.ng/vectors": "^7.8.8"
38
+ "@thi.ng/api": "^8.9.12",
39
+ "@thi.ng/checks": "^3.4.12",
40
+ "@thi.ng/errors": "^2.4.6",
41
+ "@thi.ng/math": "^5.7.7",
42
+ "@thi.ng/matrices": "^2.2.14",
43
+ "@thi.ng/pixel": "^5.0.5",
44
+ "@thi.ng/shader-ast": "^0.12.85",
45
+ "@thi.ng/vectors": "^7.8.10"
44
46
  },
45
47
  "devDependencies": {
46
48
  "@microsoft/api-extractor": "^7.38.3",
49
+ "esbuild": "^0.19.8",
47
50
  "rimraf": "^5.0.5",
48
51
  "tools": "^0.0.1",
49
52
  "typedoc": "^0.25.4",
@@ -160,5 +163,5 @@
160
163
  ],
161
164
  "year": 2019
162
165
  },
163
- "gitHead": "25f2ac8ff795a432a930119661b364d4d93b59a0\n"
166
+ "gitHead": "22e36fa838e5431d40165384918b395603bbd92f\n"
164
167
  }
package/pool.js CHANGED
@@ -1,75 +1,74 @@
1
- import { typedArray, } from "@thi.ng/api";
1
+ import {
2
+ typedArray
3
+ } from "@thi.ng/api";
2
4
  import { outOfBounds } from "@thi.ng/errors/out-of-bounds";
3
5
  import { set } from "@thi.ng/vectors/set";
4
6
  import { setC2, setC3, setC4 } from "@thi.ng/vectors/setc";
5
7
  import { setN, setN2, setN3, setN4 } from "@thi.ng/vectors/setn";
6
- /**
7
- * Manager for re-using pre-allocated memory for various vector ops. If the
8
- * compiled shader program includes a `main()` function, the pools will be reset
9
- * automatically when that `main` function executes. Otherwise, users **MUST**
10
- * call the {@link CompileResult.__reset} manually each time before invoking a
11
- * compiled entry point function.
12
- *
13
- * @remarks
14
- * Currently, the default capacity for each vector type is fixed at 2048 items.
15
- * That means a shader program can create up to this number of temporary objects
16
- * (per fragment/invocation). Should this number be insufficient, please submit
17
- * an issue and explain your use case. Thanks!
18
- */
19
- export class Pool {
20
- size;
21
- cap;
22
- mem;
23
- items;
24
- index = 0;
25
- next;
26
- from;
27
- uniform;
28
- constructor(type, size, cap) {
29
- this.size = size;
30
- this.cap = cap;
31
- this.mem = typedArray(type, cap * size);
32
- this.items = new Array(cap);
33
- for (let i = 0; i < cap; i++) {
34
- this.items[i] = this.mem.subarray(i * size, i * size + size);
35
- }
36
- const next = () => {
37
- if (this.index > this.items.length)
38
- outOfBounds(this.index);
39
- return this.items[this.index++];
40
- };
41
- let from;
42
- let uniform;
43
- switch (this.size) {
44
- case 2:
45
- from = (x, y) => setC2(next(), x, y);
46
- uniform = (n) => setN2(next(), n);
47
- case 3:
48
- from = (x, y, z) => setC3(next(), x, y, z);
49
- uniform = (n) => setN3(next(), n);
50
- case 4:
51
- from = (x, y, z, w) => setC4(next(), x, y, z, w);
52
- uniform = (n) => setN4(next(), n);
53
- default:
54
- from = (...args) => set(next(), args);
55
- uniform = (n) => setN(next(), n);
56
- }
57
- this.next = next;
58
- this.from = from;
59
- this.uniform = uniform;
8
+ class Pool {
9
+ constructor(type, size, cap) {
10
+ this.size = size;
11
+ this.cap = cap;
12
+ this.mem = typedArray(type, cap * size);
13
+ this.items = new Array(cap);
14
+ for (let i = 0; i < cap; i++) {
15
+ this.items[i] = this.mem.subarray(i * size, i * size + size);
60
16
  }
61
- reset() {
62
- this.index = 0;
63
- return this;
17
+ const next = () => {
18
+ if (this.index > this.items.length)
19
+ outOfBounds(this.index);
20
+ return this.items[this.index++];
21
+ };
22
+ let from;
23
+ let uniform;
24
+ switch (this.size) {
25
+ case 2:
26
+ from = (x, y) => setC2(next(), x, y);
27
+ uniform = (n) => setN2(next(), n);
28
+ case 3:
29
+ from = (x, y, z) => setC3(next(), x, y, z);
30
+ uniform = (n) => setN3(next(), n);
31
+ case 4:
32
+ from = (x, y, z, w) => setC4(next(), x, y, z, w);
33
+ uniform = (n) => setN4(next(), n);
34
+ default:
35
+ from = (...args) => set(next(), args);
36
+ uniform = (n) => setN(next(), n);
64
37
  }
38
+ this.next = next;
39
+ this.from = from;
40
+ this.uniform = uniform;
41
+ }
42
+ mem;
43
+ items;
44
+ index = 0;
45
+ next;
46
+ from;
47
+ uniform;
48
+ reset() {
49
+ this.index = 0;
50
+ return this;
51
+ }
65
52
  }
66
53
  const CAP = 2048;
67
- export const POOL_VEC2 = new Pool("f32", 2, CAP);
68
- export const POOL_VEC3 = new Pool("f32", 3, CAP);
69
- export const POOL_VEC4 = new Pool("f32", 4, CAP);
70
- export const POOL_IVEC2 = new Pool("i32", 2, CAP);
71
- export const POOL_IVEC3 = new Pool("i32", 3, CAP);
72
- export const POOL_IVEC4 = new Pool("i32", 4, CAP);
73
- export const POOL_UVEC2 = new Pool("u32", 2, CAP);
74
- export const POOL_UVEC3 = new Pool("u32", 3, CAP);
75
- export const POOL_UVEC4 = new Pool("u32", 4, CAP);
54
+ const POOL_VEC2 = new Pool("f32", 2, CAP);
55
+ const POOL_VEC3 = new Pool("f32", 3, CAP);
56
+ const POOL_VEC4 = new Pool("f32", 4, CAP);
57
+ const POOL_IVEC2 = new Pool("i32", 2, CAP);
58
+ const POOL_IVEC3 = new Pool("i32", 3, CAP);
59
+ const POOL_IVEC4 = new Pool("i32", 4, CAP);
60
+ const POOL_UVEC2 = new Pool("u32", 2, CAP);
61
+ const POOL_UVEC3 = new Pool("u32", 3, CAP);
62
+ const POOL_UVEC4 = new Pool("u32", 4, CAP);
63
+ export {
64
+ POOL_IVEC2,
65
+ POOL_IVEC3,
66
+ POOL_IVEC4,
67
+ POOL_UVEC2,
68
+ POOL_UVEC3,
69
+ POOL_UVEC4,
70
+ POOL_VEC2,
71
+ POOL_VEC3,
72
+ POOL_VEC4,
73
+ Pool
74
+ };
package/runtime.js CHANGED
@@ -1,99 +1,50 @@
1
1
  import { clamp, clamp01 } from "@thi.ng/math/interval";
2
2
  import { intBufferFromCanvas } from "@thi.ng/pixel/int";
3
- export const rgbaBgra8888 = (rgba) => ((clamp01(rgba[0]) * 255.5) << 0) |
4
- ((clamp01(rgba[1]) * 255.5) << 8) |
5
- ((clamp01(rgba[2]) * 255.5) << 16) |
6
- ((clamp01(rgba[3]) * 255.5) << 24);
7
- export const rgbaRgb565 = (rgba) => (((clamp01(rgba[0]) * 255.5) & 0xf8) << 8) |
8
- (((clamp01(rgba[1]) * 255.5) & 0xfc) << 3) |
9
- (((clamp01(rgba[2]) * 255.5) & 0xf8) >> 3);
10
- const clampCoord = (x, maxW, w) => w !== undefined ? Math.min(x + w, maxW) : maxW;
11
- /**
12
- * Low-level function used by {@link canvasRenderer} and {@link renderBuffer}.
13
- * Applies shader function `fn` to each pixel in the given region of the
14
- * `pixels` buffer (e.g. a `Uint32Array`). The render region is defined via
15
- * options. The top-left `x`, `y` coords and `w`, `h` dimensions. The remaining
16
- * parameters `bufW`, `bufH`, `bufOffsetX`, `bufOffsetY` and `imgH` are used to
17
- * define the actual location of the given buffer in the full image to be
18
- * computed and to support use cases where the target array only defines a
19
- * sub-region of the full image (e.g. when splitting rendering over multiple
20
- * workers, each with their own buffer).
21
- *
22
- * @param fn -
23
- * @param pixels -
24
- * @param opts
25
- */
26
- export const renderPixels = (fn, pixels, { x, y, w, h, bufW, bufH, offsetX, offsetY, imgH, fmt }) => {
27
- offsetX = offsetX || 0;
28
- offsetY = offsetY || 0;
29
- imgH = (imgH || bufH) - 1 - offsetY;
30
- x = clamp(x || 0, 0, bufW);
31
- y = clamp(y || 0, 0, bufH);
32
- const x2 = clampCoord(x, bufW, w);
33
- const y2 = clampCoord(y, bufH, h);
34
- const fragCoord = [];
35
- for (let yy = y; yy < y2; yy++) {
36
- fragCoord[1] = imgH - yy;
37
- let i = yy * bufW + x;
38
- for (let xx = x; xx < x2; xx++) {
39
- fragCoord[0] = xx + offsetX;
40
- pixels[i++] = fmt(fn(fragCoord));
41
- }
3
+ const rgbaBgra8888 = (rgba) => clamp01(rgba[0]) * 255.5 << 0 | clamp01(rgba[1]) * 255.5 << 8 | clamp01(rgba[2]) * 255.5 << 16 | clamp01(rgba[3]) * 255.5 << 24;
4
+ const rgbaRgb565 = (rgba) => (clamp01(rgba[0]) * 255.5 & 248) << 8 | (clamp01(rgba[1]) * 255.5 & 252) << 3 | (clamp01(rgba[2]) * 255.5 & 248) >> 3;
5
+ const clampCoord = (x, maxW, w) => w !== void 0 ? Math.min(x + w, maxW) : maxW;
6
+ const renderPixels = (fn, pixels, { x, y, w, h, bufW, bufH, offsetX, offsetY, imgH, fmt }) => {
7
+ offsetX = offsetX || 0;
8
+ offsetY = offsetY || 0;
9
+ imgH = (imgH || bufH) - 1 - offsetY;
10
+ x = clamp(x || 0, 0, bufW);
11
+ y = clamp(y || 0, 0, bufH);
12
+ const x2 = clampCoord(x, bufW, w);
13
+ const y2 = clampCoord(y, bufH, h);
14
+ const fragCoord = [];
15
+ for (let yy = y; yy < y2; yy++) {
16
+ fragCoord[1] = imgH - yy;
17
+ let i = yy * bufW + x;
18
+ for (let xx = x; xx < x2; xx++) {
19
+ fragCoord[0] = xx + offsetX;
20
+ pixels[i++] = fmt(fn(fragCoord));
42
21
  }
43
- return pixels;
22
+ }
23
+ return pixels;
44
24
  };
45
- /**
46
- * Takes a
47
- * [`IntBuffer`](https://docs.thi.ng/umbrella/pixel/classes/IntBuffer.html)
48
- * pixel buffer from thi.ng/pixel, and options to define a buffer-local render
49
- * region. Applies shader function `fn` to each pixel in that region (or the
50
- * full buffer by default).
51
- *
52
- * @remarks
53
- * In case the buffer only defines a sub-region of a larger image,
54
- * {@link RenderPixelOpts.offsetX}, {@link RenderPixelOpts.offsetY} and
55
- * {@link RenderPixelOpts.imgH} can be given to configure the location and full
56
- * image height.
57
- *
58
- * The default target pixel format is `ABGR8888` using {@link rgbaBgra8888} as
59
- * default {@link RenderPixelOpts.fmt} conversion.
60
- *
61
- * @param fn -
62
- * @param buf -
63
- * @param x -
64
- * @param y -
65
- * @param w -
66
- * @param h -
67
- * @param bufOffsetX -
68
- * @param bufOffsetY -
69
- * @param imgH -
70
- */
71
- export const renderBuffer = (fn, buf, opts) => {
72
- renderPixels(fn, buf.data, {
73
- fmt: rgbaBgra8888,
74
- bufW: buf.width,
75
- bufH: buf.height,
76
- x: 0,
77
- y: 0,
78
- ...opts,
79
- });
80
- return buf;
25
+ const renderBuffer = (fn, buf, opts) => {
26
+ renderPixels(fn, buf.data, {
27
+ fmt: rgbaBgra8888,
28
+ bufW: buf.width,
29
+ bufH: buf.height,
30
+ x: 0,
31
+ y: 0,
32
+ ...opts
33
+ });
34
+ return buf;
81
35
  };
82
- /**
83
- * Higher order function accepting an `HTMLCanvasElement` and returning a render
84
- * function which accepts the following parameters:
85
- *
86
- * - `fn` - shader function (compiled via `targetJS().compile(ast)`)
87
- * - `x`, `y`, `w`, `h` - optional args to define a sub-region to be updated
88
- * (default to full image update)
89
- *
90
- * @param canvas -
91
- */
92
- export const canvasRenderer = (canvas) => {
93
- const buf = intBufferFromCanvas(canvas);
94
- const data = new ImageData(canvas.width, canvas.height);
95
- return (fn, x = 0, y = 0, w = canvas.width, h = canvas.height) => {
96
- renderBuffer(fn, buf, { x, y, w, h });
97
- buf.blitCanvas(canvas, { data });
98
- };
36
+ const canvasRenderer = (canvas) => {
37
+ const buf = intBufferFromCanvas(canvas);
38
+ const data = new ImageData(canvas.width, canvas.height);
39
+ return (fn, x = 0, y = 0, w = canvas.width, h = canvas.height) => {
40
+ renderBuffer(fn, buf, { x, y, w, h });
41
+ buf.blitCanvas(canvas, { data });
42
+ };
43
+ };
44
+ export {
45
+ canvasRenderer,
46
+ renderBuffer,
47
+ renderPixels,
48
+ rgbaBgra8888,
49
+ rgbaRgb565
99
50
  };