@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/CHANGELOG.md +1 -1
- package/README.md +1 -1
- package/api.js +0 -1
- package/env/bvec.js +17 -12
- package/env/float.js +37 -34
- package/env/int.js +23 -20
- package/env/ivec2.js +26 -23
- package/env/ivec3.js +26 -23
- package/env/ivec4.js +26 -23
- package/env/mat2.js +23 -20
- package/env/mat3.js +23 -20
- package/env/mat4.js +23 -20
- package/env/uint.js +23 -20
- package/env/uvec2.js +26 -23
- package/env/uvec3.js +26 -23
- package/env/uvec4.js +26 -23
- package/env/vec2.js +65 -62
- package/env/vec3.js +66 -63
- package/env/vec4.js +65 -62
- package/env.js +114 -96
- package/package.json +14 -11
- package/pool.js +66 -67
- package/runtime.js +44 -93
- package/target.js +201 -213
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 {
|
|
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 {
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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.
|
|
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
|
|
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.
|
|
37
|
-
"@thi.ng/checks": "^3.4.
|
|
38
|
-
"@thi.ng/errors": "^2.4.
|
|
39
|
-
"@thi.ng/math": "^5.7.
|
|
40
|
-
"@thi.ng/matrices": "^2.2.
|
|
41
|
-
"@thi.ng/pixel": "^5.0.
|
|
42
|
-
"@thi.ng/shader-ast": "^0.12.
|
|
43
|
-
"@thi.ng/vectors": "^7.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": "
|
|
166
|
+
"gitHead": "22e36fa838e5431d40165384918b395603bbd92f\n"
|
|
164
167
|
}
|
package/pool.js
CHANGED
|
@@ -1,75 +1,74 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
*
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
22
|
+
}
|
|
23
|
+
return pixels;
|
|
44
24
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
};
|