@thi.ng/shader-ast-js 0.7.67 → 1.0.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/env/vec3.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import type { JSBuiltinsVec3 } from "../api.js";
2
- export declare const VEC3: JSBuiltinsVec3;
2
+ import { Pool } from "../pool.js";
3
+ export declare const VEC3: ({ next, uniform }: Pool) => JSBuiltinsVec3;
3
4
  //# sourceMappingURL=vec3.d.ts.map
package/env/vec3.js CHANGED
@@ -51,67 +51,68 @@ import { step3 } from "@thi.ng/vectors/step";
51
51
  import { sub3 } from "@thi.ng/vectors/sub";
52
52
  import { subN3 } from "@thi.ng/vectors/subn";
53
53
  import { tan3 } from "@thi.ng/vectors/tan";
54
- export const VEC3 = {
55
- abs: (a) => abs3([], a),
56
- acos: (a) => acos3([], a),
57
- add: (a, b) => add3([], a, b),
58
- addnv: (a, b) => addN3([], b, a),
59
- addvn: (a, b) => addN3([], a, b),
60
- asin: (a) => asin3([], a),
61
- atan: (a) => atan3([], a),
62
- atannn: (a, b) => atan_23([], a, b),
63
- ceil: (a) => ceil3([], a),
64
- clamp: (x, a, b) => clamp3([], x, a, b),
65
- cos: (a) => cos3([], a),
66
- cross: (a, b) => cross3([], a, b),
67
- dec: (a) => subN3([], a, 1),
68
- degrees: (a) => degrees3([], a),
54
+ import { Pool } from "../pool.js";
55
+ export const VEC3 = ({ next, uniform }) => ({
56
+ abs: (a) => abs3(next(), a),
57
+ acos: (a) => acos3(next(), a),
58
+ add: (a, b) => add3(next(), a, b),
59
+ addnv: (a, b) => addN3(next(), b, a),
60
+ addvn: (a, b) => addN3(next(), a, b),
61
+ asin: (a) => asin3(next(), a),
62
+ atan: (a) => atan3(next(), a),
63
+ atannn: (a, b) => atan_23(next(), a, b),
64
+ ceil: (a) => ceil3(next(), a),
65
+ clamp: (x, a, b) => clamp3(next(), x, a, b),
66
+ cos: (a) => cos3(next(), a),
67
+ cross: (a, b) => cross3(next(), a, b),
68
+ dec: (a) => subN3(next(), a, 1),
69
+ degrees: (a) => degrees3(next(), a),
69
70
  dFdx: () => ZERO3,
70
71
  dFdy: () => ZERO3,
71
72
  distance: dist,
72
- div: (a, b) => div3([], a, b),
73
- divnv: (a, b) => div3(null, [a, a, a], b),
74
- divvn: (a, b) => divN3([], a, b),
73
+ div: (a, b) => div3(next(), a, b),
74
+ divnv: (a, b) => div3(null, uniform(a), b),
75
+ divvn: (a, b) => divN3(next(), a, b),
75
76
  dot: (a, b) => dot3(a, b),
76
- exp: (a) => exp3([], a),
77
- exp2: (a) => exp_23([], a),
78
- faceForward: (a, b, c) => faceForward([], a, b, c),
79
- floor: (a) => floor3([], a),
80
- fract: (a) => fract3([], a),
77
+ exp: (a) => exp3(next(), a),
78
+ exp2: (a) => exp_23(next(), a),
79
+ faceForward: (a, b, c) => faceForward(next(), a, b, c),
80
+ floor: (a) => floor3(next(), a),
81
+ fract: (a) => fract3(next(), a),
81
82
  fwidth: () => ZERO3,
82
- inc: (a) => addN3([], a, 1),
83
- inversesqrt: (a) => invSqrt3([], a),
83
+ inc: (a) => addN3(next(), a, 1),
84
+ inversesqrt: (a) => invSqrt3(next(), a),
84
85
  length: mag,
85
- log: (a) => log3([], a),
86
- log2: (a) => log_23([], a),
87
- max: (a, b) => max3([], a, b),
88
- min: (a, b) => min3([], a, b),
89
- mix: (a, b, t) => mix3([], a, b, t),
90
- mixn: (a, b, t) => mixN3([], a, b, t),
91
- mod: (a, b) => mod3([], a, b),
92
- modn: (a, b) => modN3([], a, b),
93
- mul: (a, b) => mul3([], a, b),
94
- mulnv: (a, b) => mulN3([], b, a),
95
- mulvn: (a, b) => mulN3([], a, b),
96
- normalize: (a) => normalize3([], a),
97
- pow: (a, b) => pow3([], a, b),
98
- radians: (a) => radians3([], a),
99
- reflect: (a, b) => reflect([], a, b),
100
- refract: (a, b, c) => refract([], a, b, c),
101
- sign: (a) => sign3([], a),
102
- sin: (a) => sin3([], a),
103
- smoothstep: (a, b, t) => smoothStep3([], a, b, t),
104
- sqrt: (a) => sqrt3([], a),
105
- step: (a, b) => step3([], a, b),
106
- sub: (a, b) => sub3([], a, b),
107
- sub1: (a) => neg([], a),
108
- subnv: (a, b) => sub3(null, [a, a, a], b),
109
- subvn: (a, b) => subN3([], a, b),
110
- tan: (a) => tan3([], a),
86
+ log: (a) => log3(next(), a),
87
+ log2: (a) => log_23(next(), a),
88
+ max: (a, b) => max3(next(), a, b),
89
+ min: (a, b) => min3(next(), a, b),
90
+ mix: (a, b, t) => mix3(next(), a, b, t),
91
+ mixn: (a, b, t) => mixN3(next(), a, b, t),
92
+ mod: (a, b) => mod3(next(), a, b),
93
+ modn: (a, b) => modN3(next(), a, b),
94
+ mul: (a, b) => mul3(next(), a, b),
95
+ mulnv: (a, b) => mulN3(next(), b, a),
96
+ mulvn: (a, b) => mulN3(next(), a, b),
97
+ normalize: (a) => normalize3(next(), a),
98
+ pow: (a, b) => pow3(next(), a, b),
99
+ radians: (a) => radians3(next(), a),
100
+ reflect: (a, b) => reflect(next(), a, b),
101
+ refract: (a, b, c) => refract(next(), a, b, c),
102
+ sign: (a) => sign3(next(), a),
103
+ sin: (a) => sin3(next(), a),
104
+ smoothstep: (a, b, t) => smoothStep3(next(), a, b, t),
105
+ sqrt: (a) => sqrt3(next(), a),
106
+ step: (a, b) => step3(next(), a, b),
107
+ sub: (a, b) => sub3(next(), a, b),
108
+ sub1: (a) => neg(next(), a),
109
+ subnv: (a, b) => sub3(null, uniform(a), b),
110
+ subvn: (a, b) => subN3(next(), a, b),
111
+ tan: (a) => tan3(next(), a),
111
112
  equal: (a, b) => eq3([], a, b),
112
113
  notEqual: (a, b) => neq3([], a, b),
113
114
  greaterThan: (a, b) => gt3([], a, b),
114
115
  lessThan: (a, b) => lt3([], a, b),
115
116
  greaterThanEqual: (a, b) => gte3([], a, b),
116
117
  lessThanEqual: (a, b) => lte3([], a, b),
117
- };
118
+ });
package/env/vec4.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import type { JSBuiltinsVec } from "../api.js";
2
- export declare const VEC4: JSBuiltinsVec;
2
+ import { Pool } from "../pool.js";
3
+ export declare const VEC4: ({ next, uniform }: Pool) => JSBuiltinsVec;
3
4
  //# sourceMappingURL=vec4.d.ts.map
package/env/vec4.js CHANGED
@@ -50,66 +50,67 @@ import { step4 } from "@thi.ng/vectors/step";
50
50
  import { sub4 } from "@thi.ng/vectors/sub";
51
51
  import { subN4 } from "@thi.ng/vectors/subn";
52
52
  import { tan4 } from "@thi.ng/vectors/tan";
53
- export const VEC4 = {
54
- abs: (a) => abs4([], a),
55
- acos: (a) => acos4([], a),
56
- add: (a, b) => add4([], a, b),
57
- addnv: (a, b) => addN4([], b, a),
58
- addvn: (a, b) => addN4([], a, b),
59
- asin: (a) => asin4([], a),
60
- atan: (a) => atan4([], a),
61
- atannn: (a, b) => atan_24([], a, b),
62
- ceil: (a) => ceil4([], a),
63
- clamp: (x, a, b) => clamp4([], x, a, b),
64
- cos: (a) => cos4([], a),
65
- dec: (a) => subN4([], a, 1),
66
- degrees: (a) => degrees4([], a),
53
+ import { Pool } from "../pool.js";
54
+ export const VEC4 = ({ next, uniform }) => ({
55
+ abs: (a) => abs4(next(), a),
56
+ acos: (a) => acos4(next(), a),
57
+ add: (a, b) => add4(next(), a, b),
58
+ addnv: (a, b) => addN4(next(), b, a),
59
+ addvn: (a, b) => addN4(next(), a, b),
60
+ asin: (a) => asin4(next(), a),
61
+ atan: (a) => atan4(next(), a),
62
+ atannn: (a, b) => atan_24(next(), a, b),
63
+ ceil: (a) => ceil4(next(), a),
64
+ clamp: (x, a, b) => clamp4(next(), x, a, b),
65
+ cos: (a) => cos4(next(), a),
66
+ dec: (a) => subN4(next(), a, 1),
67
+ degrees: (a) => degrees4(next(), a),
67
68
  dFdx: () => ZERO4,
68
69
  dFdy: () => ZERO4,
69
70
  distance: dist,
70
- div: (a, b) => div4([], a, b),
71
- divnv: (a, b) => div4(null, [a, a, a, a], b),
72
- divvn: (a, b) => divN4([], a, b),
71
+ div: (a, b) => div4(next(), a, b),
72
+ divnv: (a, b) => div4(null, uniform(a), b),
73
+ divvn: (a, b) => divN4(next(), a, b),
73
74
  dot: (a, b) => dot4(a, b),
74
- exp: (a) => exp4([], a),
75
- exp2: (a) => exp_24([], a),
76
- faceForward: (a, b, c) => faceForward([], a, b, c),
77
- floor: (a) => floor4([], a),
78
- fract: (a) => fract4([], a),
75
+ exp: (a) => exp4(next(), a),
76
+ exp2: (a) => exp_24(next(), a),
77
+ faceForward: (a, b, c) => faceForward(next(), a, b, c),
78
+ floor: (a) => floor4(next(), a),
79
+ fract: (a) => fract4(next(), a),
79
80
  fwidth: () => ZERO4,
80
- inc: (a) => addN4([], a, 1),
81
- inversesqrt: (a) => invSqrt4([], a),
81
+ inc: (a) => addN4(next(), a, 1),
82
+ inversesqrt: (a) => invSqrt4(next(), a),
82
83
  length: mag,
83
- log: (a) => log4([], a),
84
- log2: (a) => log_24([], a),
85
- max: (a, b) => max4([], a, b),
86
- min: (a, b) => min4([], a, b),
87
- mix: (a, b, t) => mix4([], a, b, t),
88
- mixn: (a, b, t) => mixN4([], a, b, t),
89
- mod: (a, b) => mod4([], a, b),
90
- modn: (a, b) => modN4([], a, b),
91
- mul: (a, b) => mul4([], a, b),
92
- mulnv: (a, b) => mulN4([], b, a),
93
- mulvn: (a, b) => mulN4([], a, b),
94
- normalize: (a) => normalize4([], a),
95
- pow: (a, b) => pow4([], a, b),
96
- radians: (a) => radians4([], a),
97
- reflect: (a, b) => reflect([], a, b),
98
- refract: (a, b, c) => refract([], a, b, c),
99
- sign: (a) => sign4([], a),
100
- sin: (a) => sin4([], a),
101
- smoothstep: (a, b, t) => smoothStep4([], a, b, t),
102
- sqrt: (a) => sqrt4([], a),
103
- step: (a, b) => step4([], a, b),
104
- sub: (a, b) => sub4([], a, b),
105
- sub1: (a) => neg([], a),
106
- subnv: (a, b) => sub4(null, [a, a, a, a], b),
107
- subvn: (a, b) => subN4([], a, b),
108
- tan: (a) => tan4([], a),
84
+ log: (a) => log4(next(), a),
85
+ log2: (a) => log_24(next(), a),
86
+ max: (a, b) => max4(next(), a, b),
87
+ min: (a, b) => min4(next(), a, b),
88
+ mix: (a, b, t) => mix4(next(), a, b, t),
89
+ mixn: (a, b, t) => mixN4(next(), a, b, t),
90
+ mod: (a, b) => mod4(next(), a, b),
91
+ modn: (a, b) => modN4(next(), a, b),
92
+ mul: (a, b) => mul4(next(), a, b),
93
+ mulnv: (a, b) => mulN4(next(), b, a),
94
+ mulvn: (a, b) => mulN4(next(), a, b),
95
+ normalize: (a) => normalize4(next(), a),
96
+ pow: (a, b) => pow4(next(), a, b),
97
+ radians: (a) => radians4(next(), a),
98
+ reflect: (a, b) => reflect(next(), a, b),
99
+ refract: (a, b, c) => refract(next(), a, b, c),
100
+ sign: (a) => sign4(next(), a),
101
+ sin: (a) => sin4(next(), a),
102
+ smoothstep: (a, b, t) => smoothStep4(next(), a, b, t),
103
+ sqrt: (a) => sqrt4(next(), a),
104
+ step: (a, b) => step4(next(), a, b),
105
+ sub: (a, b) => sub4(next(), a, b),
106
+ sub1: (a) => neg(next(), a),
107
+ subnv: (a, b) => sub4(null, uniform(a), b),
108
+ subvn: (a, b) => subN4(next(), a, b),
109
+ tan: (a) => tan4(next(), a),
109
110
  equal: (a, b) => eq4([], a, b),
110
111
  notEqual: (a, b) => neq4([], a, b),
111
112
  greaterThan: (a, b) => gt4([], a, b),
112
113
  lessThan: (a, b) => lt4([], a, b),
113
114
  greaterThanEqual: (a, b) => gte4([], a, b),
114
115
  lessThanEqual: (a, b) => lte4([], a, b),
115
- };
116
+ });
package/env.js CHANGED
@@ -22,6 +22,16 @@ import { UVEC4 } from "./env/uvec4.js";
22
22
  import { VEC2 } from "./env/vec2.js";
23
23
  import { VEC3 } from "./env/vec3.js";
24
24
  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";
26
+ const { next: $2, uniform: $n2 } = POOL_VEC2;
27
+ const { next: $3, uniform: $n3 } = POOL_VEC3;
28
+ const { next: $4, uniform: $n4 } = POOL_VEC4;
29
+ const { next: $i2, uniform: $ni2 } = POOL_IVEC2;
30
+ const { next: $i3, uniform: $ni3 } = POOL_IVEC3;
31
+ const { next: $i4, uniform: $ni4 } = POOL_IVEC4;
32
+ const { next: $u2, uniform: $nu2 } = POOL_UVEC2;
33
+ const { next: $u3, uniform: $nu3 } = POOL_UVEC3;
34
+ const { next: $u4, uniform: $nu4 } = POOL_UVEC4;
25
35
  // TODO texture lookups
26
36
  // all texture fns currently return [0,0,0,0] or 0
27
37
  const SAMPLER_TODO = {
@@ -40,34 +50,34 @@ const SAMPLER_TODO = {
40
50
  textureSize: () => ZERO3,
41
51
  };
42
52
  export const JS_DEFAULT_ENV = {
43
- vec2: VEC2,
44
- vec2b: (v) => fromBVec2([], v),
53
+ vec2: VEC2(POOL_VEC2),
54
+ vec2b: (v) => fromBVec2($2(), v),
45
55
  vec2i: identity,
46
- vec2n: (n) => [n, n],
56
+ vec2n: $n2,
47
57
  vec2u: identity,
48
- vec3: VEC3,
49
- vec3b: (v) => fromBVec3([], v),
58
+ vec3: VEC3(POOL_VEC3),
59
+ vec3b: (v) => fromBVec3($3(), v),
50
60
  vec3i: identity,
51
- vec3n: (n) => [n, n, n],
61
+ vec3n: $n3,
52
62
  vec3u: identity,
53
- vec3vn: (a, n) => setVN3([], a, n),
54
- vec4: VEC4,
55
- vec4b: (v) => fromBVec4([], v),
63
+ vec3vn: (a, n) => setVN3($3(), a, n),
64
+ vec4: VEC4(POOL_VEC4),
65
+ vec4b: (v) => fromBVec4($4(), v),
56
66
  vec4i: identity,
57
- vec4n: (n) => [n, n, n, n],
67
+ vec4n: $n4,
58
68
  vec4u: identity,
59
- vec4vn: (a, n) => setVN4([], a, n),
60
- vec4vnn: (a, z, w) => setVV4([], a, [z, w]),
61
- vec4vv: (a, b) => setVV4([], a, b),
62
- mat2n: (n) => mat22n([], n),
63
- mat2vv: (a, b) => mat22v([], a, b),
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),
64
74
  mat3n: (n) => mat33n([], n),
65
75
  mat3vvv: (a, b, c) => mat33v([], a, b, c),
66
76
  mat4n: (n) => mat44n([], n),
67
77
  mat4vvvv: (a, b, c, d) => mat44v([], a, b, c, d),
68
- swizzle2: (a, b, c) => swizzle2([], a, b, c),
69
- swizzle3: (a, b, c, d) => swizzle3([], a, b, c, d),
70
- swizzle4: (a, b, c, d, e) => swizzle4([], a, b, c, d, e),
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),
71
81
  set_swizzle2: setSwizzle2,
72
82
  set_swizzle3: setSwizzle3,
73
83
  set_swizzle4: setSwizzle4,
@@ -81,23 +91,23 @@ export const JS_DEFAULT_ENV = {
81
91
  bvec4: BVEC4,
82
92
  bvec4n: (n) => ((n = !!n), [n, n, n, n]),
83
93
  ivec2: IVEC2,
84
- ivec2b: (v) => fromBVec2([], v),
85
- ivec2n: (n) => [n, n],
94
+ ivec2b: (v) => fromBVec2($i2(), v),
95
+ ivec2n: $ni2,
86
96
  ivec3: IVEC3,
87
- ivec3b: (v) => fromBVec3([], v),
88
- ivec3n: (n) => [n, n, n],
97
+ ivec3b: (v) => fromBVec3($i3(), v),
98
+ ivec3n: $ni3,
89
99
  ivec4: IVEC4,
90
- ivec4b: (v) => fromBVec4([], v),
91
- ivec4n: (n) => [n, n, n, n],
100
+ ivec4b: (v) => fromBVec4($i4(), v),
101
+ ivec4n: $ni4,
92
102
  uvec2: UVEC2,
93
- uvec2b: (v) => fromBVec2([], v),
94
- uvec2n: (n) => [n, n],
103
+ uvec2b: (v) => fromBVec2($u2(), v),
104
+ uvec2n: $nu2,
95
105
  uvec3: UVEC3,
96
- uvec3b: (v) => fromBVec3([], v),
97
- uvec3n: (n) => [n, n, n],
106
+ uvec3b: (v) => fromBVec3($u3(), v),
107
+ uvec3n: $nu3,
98
108
  uvec4: UVEC4,
99
- uvec4b: (v) => fromBVec4([], v),
100
- uvec4n: (n) => [n, n, n, n],
109
+ uvec4b: (v) => fromBVec4($u4(), v),
110
+ uvec4n: $nu4,
101
111
  mat2: MAT2,
102
112
  mat3: MAT3,
103
113
  mat4: MAT4,
@@ -107,4 +117,15 @@ export const JS_DEFAULT_ENV = {
107
117
  samplerCube: SAMPLER_TODO,
108
118
  sampler2DShadow: SAMPLER_TODO,
109
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
+ },
110
131
  };
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./api.js";
2
2
  export * from "./env.js";
3
+ export * from "./pool.js";
3
4
  export * from "./runtime.js";
4
5
  export * from "./target.js";
5
6
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./api.js";
2
2
  export * from "./env.js";
3
+ export * from "./pool.js";
3
4
  export * from "./runtime.js";
4
5
  export * from "./target.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/shader-ast-js",
3
- "version": "0.7.67",
3
+ "version": "1.0.0",
4
4
  "description": "Customizable JS codegen, compiler & runtime for @thi.ng/shader-ast",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -38,10 +38,10 @@
38
38
  "@thi.ng/checks": "^3.4.3",
39
39
  "@thi.ng/errors": "^2.3.3",
40
40
  "@thi.ng/math": "^5.5.4",
41
- "@thi.ng/matrices": "^2.1.62",
42
- "@thi.ng/pixel": "^4.2.12",
43
- "@thi.ng/shader-ast": "^0.12.58",
44
- "@thi.ng/vectors": "^7.7.5"
41
+ "@thi.ng/matrices": "^2.1.64",
42
+ "@thi.ng/pixel": "^4.2.14",
43
+ "@thi.ng/shader-ast": "^0.12.60",
44
+ "@thi.ng/vectors": "^7.7.7"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@microsoft/api-extractor": "^7.36.4",
@@ -132,6 +132,9 @@
132
132
  "./env": {
133
133
  "default": "./env.js"
134
134
  },
135
+ "./pool": {
136
+ "default": "./pool.js"
137
+ },
135
138
  "./runtime": {
136
139
  "default": "./runtime.js"
137
140
  },
@@ -147,5 +150,5 @@
147
150
  ],
148
151
  "year": 2019
149
152
  },
150
- "gitHead": "399f8c53d66b9b763d16c21bb59f145df5f59514\n"
153
+ "gitHead": "4e8f1abeeba7b919e069954eada917ecd2e9a8e9\n"
151
154
  }
package/pool.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ import { type IReset, type Type, type TypedArray } from "@thi.ng/api";
2
+ import type { Vec } from "@thi.ng/vectors";
3
+ /**
4
+ * Manager for re-using pre-allocated memory for various vector ops. If the
5
+ * compiled shader program includes a `main()` function, the pools will be reset
6
+ * automatically when that `main` function executes. Otherwise, users **MUST**
7
+ * call the {@link CompileResult.__reset} manually each time before invoking a
8
+ * compiled entry point function.
9
+ *
10
+ * @remarks
11
+ * Currently, the default capacity for each vector type is fixed at 2048 items.
12
+ * That means a shader program can create up to this number of temporary objects
13
+ * (per fragment/invocation). Should this number be insufficient, please submit
14
+ * an issue and explain your use case. Thanks!
15
+ */
16
+ export declare class Pool implements IReset {
17
+ protected size: number;
18
+ protected cap: number;
19
+ mem: TypedArray;
20
+ items: Vec[];
21
+ index: number;
22
+ next: () => Vec;
23
+ from: (...args: number[]) => Vec;
24
+ uniform: (n: number) => Vec;
25
+ constructor(type: Type, size: number, cap: number);
26
+ reset(): this;
27
+ }
28
+ export declare const POOL_VEC2: Pool;
29
+ export declare const POOL_VEC3: Pool;
30
+ export declare const POOL_VEC4: Pool;
31
+ export declare const POOL_IVEC2: Pool;
32
+ export declare const POOL_IVEC3: Pool;
33
+ export declare const POOL_IVEC4: Pool;
34
+ export declare const POOL_UVEC2: Pool;
35
+ export declare const POOL_UVEC3: Pool;
36
+ export declare const POOL_UVEC4: Pool;
37
+ //# sourceMappingURL=pool.d.ts.map
package/pool.js ADDED
@@ -0,0 +1,68 @@
1
+ import { typedArray, } from "@thi.ng/api";
2
+ import { outOfBounds } from "@thi.ng/errors/out-of-bounds";
3
+ import { set } from "@thi.ng/vectors/set";
4
+ import { setC2, setC3, setC4 } from "@thi.ng/vectors/setc";
5
+ 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
+ constructor(type, size, cap) {
21
+ this.size = size;
22
+ this.cap = cap;
23
+ this.index = 0;
24
+ this.mem = typedArray(type, cap * size);
25
+ this.items = new Array(cap);
26
+ for (let i = 0; i < cap; i++) {
27
+ this.items[i] = this.mem.subarray(i * size, i * size + size);
28
+ }
29
+ const next = () => {
30
+ if (this.index > this.items.length)
31
+ outOfBounds(this.index);
32
+ return this.items[this.index++];
33
+ };
34
+ let from;
35
+ let uniform;
36
+ switch (this.size) {
37
+ case 2:
38
+ from = (x, y) => setC2(next(), x, y);
39
+ uniform = (n) => setN2(next(), n);
40
+ case 3:
41
+ from = (x, y, z) => setC3(next(), x, y, z);
42
+ uniform = (n) => setN3(next(), n);
43
+ case 4:
44
+ from = (x, y, z, w) => setC4(next(), x, y, z, w);
45
+ uniform = (n) => setN4(next(), n);
46
+ default:
47
+ from = (...args) => set(next(), args);
48
+ uniform = (n) => setN(next(), n);
49
+ }
50
+ this.next = next;
51
+ this.from = from;
52
+ this.uniform = uniform;
53
+ }
54
+ reset() {
55
+ this.index = 0;
56
+ return this;
57
+ }
58
+ }
59
+ const CAP = 2048;
60
+ export const POOL_VEC2 = new Pool("f32", 2, CAP);
61
+ export const POOL_VEC3 = new Pool("f32", 3, CAP);
62
+ export const POOL_VEC4 = new Pool("f32", 4, CAP);
63
+ export const POOL_IVEC2 = new Pool("i32", 2, CAP);
64
+ export const POOL_IVEC3 = new Pool("i32", 3, CAP);
65
+ export const POOL_IVEC4 = new Pool("i32", 4, CAP);
66
+ export const POOL_UVEC2 = new Pool("u32", 2, CAP);
67
+ export const POOL_UVEC3 = new Pool("u32", 3, CAP);
68
+ export const POOL_UVEC4 = new Pool("u32", 4, CAP);
package/runtime.d.ts CHANGED
@@ -1,41 +1,54 @@
1
- import type { Fn } from "@thi.ng/api";
1
+ import type { Fn, UIntArray } from "@thi.ng/api";
2
2
  import { type IntBuffer } from "@thi.ng/pixel/int";
3
3
  import type { ReadonlyVec, Vec } from "@thi.ng/vectors";
4
+ export interface RenderPixelOpts {
5
+ x?: number;
6
+ y?: number;
7
+ w?: number;
8
+ h?: number;
9
+ bufW: number;
10
+ bufH: number;
11
+ offsetX?: number;
12
+ offsetY?: number;
13
+ imgH?: number;
14
+ /**
15
+ * Format conversion from float RGBA to packed integer.
16
+ */
17
+ fmt: Fn<ReadonlyVec, number>;
18
+ }
19
+ export declare const rgbaBgra8888: (rgba: ReadonlyVec) => number;
20
+ export declare const rgbaRgb565: (rgba: ReadonlyVec) => number;
4
21
  /**
5
22
  * Low-level function used by {@link canvasRenderer} and {@link renderBuffer}.
6
- * Applies shader function `fn` to each pixel in the given region of the `u32`
7
- * raw ABGR buffer (a `Uint32Array`). The region is defined by the top-left `x`,
8
- * `y` coords and `w`, `h` dimensions. The remaining parameters `bufW`, `bufH`,
9
- * `bufOffsetX`, `bufOffsetY` and `imgH` are used to define the actual location
10
- * of the given buffer in the full image to be computed and to support use cases
11
- * where the target array only defines a sub-region of the full image (e.g. when
12
- * splitting rendering over multiple workers, each with their own buffer).
23
+ * Applies shader function `fn` to each pixel in the given region of the
24
+ * `pixels` buffer (e.g. a `Uint32Array`). The render region is defined via
25
+ * options. The top-left `x`, `y` coords and `w`, `h` dimensions. The remaining
26
+ * parameters `bufW`, `bufH`, `bufOffsetX`, `bufOffsetY` and `imgH` are used to
27
+ * define the actual location of the given buffer in the full image to be
28
+ * computed and to support use cases where the target array only defines a
29
+ * sub-region of the full image (e.g. when splitting rendering over multiple
30
+ * workers, each with their own buffer).
13
31
  *
14
32
  * @param fn -
15
- * @param u32 -
16
- * @param bufW -
17
- * @param bufH -
18
- * @param x -
19
- * @param y -
20
- * @param w -
21
- * @param h -
22
- * @param bufOffsetX -
23
- * @param bufOffsetY -
24
- * @param imgH -
33
+ * @param pixels -
34
+ * @param opts
25
35
  */
26
- export declare const renderPixels: (fn: Fn<ReadonlyVec, Vec>, u32: Uint32Array, bufW: number, bufH: number, x: number, y: number, w?: number, h?: number, bufOffsetX?: number, bufOffsetY?: number, imgH?: number) => Uint32Array;
36
+ export declare const renderPixels: (fn: Fn<ReadonlyVec, Vec>, pixels: UIntArray, { x, y, w, h, bufW, bufH, offsetX, offsetY, imgH, fmt }: RenderPixelOpts) => UIntArray;
27
37
  /**
28
38
  * Takes a
29
39
  * [`IntBuffer`](https://docs.thi.ng/umbrella/pixel/classes/IntBuffer.html)
30
- * pixel buffer from thi.ng/pixel w/
31
- * [`ABGR8888`](https://docs.thi.ng/umbrella/pixel/variables/ABGR8888.html)
32
- * format, an optional buffer local region defined by `x`, `y`, `w`, `h` and
33
- * applies shader function `fn` to each pixel in that region (or full buffer by
34
- * default).
40
+ * pixel buffer from thi.ng/pixel, and options to define a buffer-local render
41
+ * region. Applies shader function `fn` to each pixel in that region (or the
42
+ * full buffer by default).
43
+ *
44
+ * @remarks
45
+ * In case the buffer only defines a sub-region of a larger image,
46
+ * {@link RenderPixelOpts.offsetX}, {@link RenderPixelOpts.offsetY} and
47
+ * {@link RenderPixelOpts.imgH} can be given to configure the location and full
48
+ * image height.
35
49
  *
36
- * In case the buffer only defines a sub-region of a larger image, `bufOffsetX`,
37
- * `bufOffsetY` and `imgH` can be given to configure the location and full image
38
- * height.
50
+ * The default target pixel format is `ABGR8888` using {@link rgbaBgra8888} as
51
+ * default {@link RenderPixelOpts.fmt} conversion.
39
52
  *
40
53
  * @param fn -
41
54
  * @param buf -
@@ -47,7 +60,7 @@ export declare const renderPixels: (fn: Fn<ReadonlyVec, Vec>, u32: Uint32Array,
47
60
  * @param bufOffsetY -
48
61
  * @param imgH -
49
62
  */
50
- export declare const renderBuffer: (fn: Fn<ReadonlyVec, Vec>, buf: IntBuffer, x?: number, y?: number, w?: number, h?: number, bufOffsetX?: number, bufOffsetY?: number, imgH?: number) => IntBuffer;
63
+ export declare const renderBuffer: (fn: Fn<ReadonlyVec, Vec>, buf: IntBuffer, opts?: Partial<RenderPixelOpts>) => IntBuffer;
51
64
  /**
52
65
  * Higher order function accepting an `HTMLCanvasElement` and returning a render
53
66
  * function which accepts the following parameters: