Skip to content

Commit ec165da

Browse files
Merge pull request #320 from yueshuangyan/master
feat(webgl): add adaptors
2 parents 30f51ef + 03ba24f commit ec165da

File tree

12 files changed

+200
-170
lines changed

12 files changed

+200
-170
lines changed

packages/paddlejs-backend-webgl/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@paddlejs/paddlejs-backend-webgl",
3-
"version": "1.1.9",
3+
"version": "1.1.10",
44
"description": "",
55
"main": "lib/index",
66
"scripts": {

packages/paddlejs-backend-webgl/src/backend.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ export default class WebGLBackend extends PaddlejsBackend {
6868
this.textureConf = GLTexture.getTextureConfig(gl);
6969

7070
// use 2048 as MAX_TEXTURE_SIZE in half float mode.
71-
this.MAX_TEXTURE_SIZE = env.get('webgl_force_half_float_texture') && this.textureConf.textureHalfFloat
72-
? 2048
73-
: env.get('MAX_TEXTURE_SIZE') || gl.getParameter(gl.MAX_TEXTURE_SIZE);
71+
this.MAX_TEXTURE_SIZE = env.get('MAX_TEXTURE_SIZE') || gl.getParameter(gl.MAX_TEXTURE_SIZE);
7472

7573
// 关闭相关功能
7674
gl.disable(gl.DEPTH_TEST);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { env } from '@paddlejs/paddlejs-core';
2+
3+
export function calMod() {
4+
return env.get('useModAdaptor')
5+
? `
6+
int calMod(int a, int b) {
7+
float modV = mod(float(a), float(b));
8+
if (modV == float(b)) {
9+
modV = 0.0;
10+
}
11+
return int(modV);
12+
}
13+
`
14+
: `
15+
int calMod(int a, int b) {
16+
return a - a / b * b;
17+
}
18+
`;
19+
}
20+
21+
export function calDivision() {
22+
return env.get('useDivisionAdaptor')
23+
? `
24+
int calDivision(int a, int b) {
25+
return int(float(a) / (float(b) - 0.0001));
26+
}
27+
`
28+
: `
29+
int calDivision(int a, int b) {
30+
return a / b;
31+
}
32+
`;
33+
}
34+

packages/paddlejs-backend-webgl/src/ops/atom/common_func_with_texture.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ function genOffsetYIfConditions(cut, height) {
4444
const curIf = cur > 0
4545
? idx === 0
4646
? `
47-
if ((float(a) / pieceW) >= float(${cur})) {
47+
if (float(a) / float(pieceW) >= float(${cur})) {
4848
offsetY = int(${cur}) * ${height};
4949
}
5050
`
5151
: `
52-
else if ((float(a) / pieceW) >= float(${cur})) {
52+
else if (float(a) / float(pieceW) >= float(${cur})) {
5353
offsetY = int(${cur}) * ${height};
5454
}
5555
`
@@ -66,17 +66,17 @@ function getValueFromTensorPosLimit(
6666
return `
6767
// 超限布局根据tensor坐标获取这个tensor位置的值
6868
float getValueFromTensorPos_${textureName}(int r, int g, int b, int a) {
69-
float limitCut = float(${limitCut});
70-
float pieceW = ceil(float(${width_shape}) / limitCut);
71-
int x = calMod(a, int(pieceW));
69+
int limitCut = ${limitCut};
70+
int pieceW = calCeil(${width_shape}, ${limitCut});
71+
int x = calMod(a, pieceW);
7272
int offsetY = 0;
7373
7474
${genOffsetYIfConditions(limitCut, height_shape)}
7575
7676
vec4 pixels = TEXTURE2D(texture_${textureName},
7777
vec2(
7878
(float(x * ${channel} + g) + 0.5) / float(${width_texture}),
79-
(float(r * int(limitCut) * ${height_shape} + b + offsetY) + 0.5) / float(${height_texture})
79+
(float(r * limitCut * ${height_shape} + b + offsetY) + 0.5) / float(${height_texture})
8080
)
8181
);
8282
return pixels.r;
@@ -173,15 +173,19 @@ export function getTensorPosFromArrayIndex(
173173
}
174174

175175
const shapeVec = `ivec${length_shape}(${numbers_shape.join(', ')})`;
176+
177+
let posStr = `pos[0] = n / ${numbers_shape[0]};`;
178+
for (let i = 1; i < length_shape; i++) {
179+
posStr += `
180+
n = calMod(n, ${numbers_shape[i - 1]});
181+
pos[${i}] = calDivision(n, ${numbers_shape[i]});
182+
`;
183+
}
176184
return `
177185
ivec${length_shape} shapeVec_${textureName} = ${shapeVec};
178186
ivec${length_shape} getTensorPosFromArrayIndex_${textureName}(int n) {
179187
ivec${length_shape} pos;
180-
pos[0] = n / shapeVec_${textureName}[0];
181-
for (int i = 1; i < ${length_shape}; i++) {
182-
n = calMod(n, shapeVec_${textureName}[i - 1]);
183-
pos[i] = n / shapeVec_${textureName}[i];
184-
}
188+
${posStr}
185189
return pos;
186190
}
187191
`;

packages/paddlejs-backend-webgl/src/ops/atom/prefix.ts

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,65 @@
55
import { env } from '@paddlejs/paddlejs-core';
66
import prefix_uint from './prefix_uint';
77
import prefix_half from './prefix_half';
8+
import { calMod, calDivision } from './common_func_adaptor';
89

9-
const prefixV1
10-
= ` #ifdef GL_FRAGMENT_PRECISION_HIGH
11-
precision highp float;
12-
precision highp int;
13-
#else
14-
precision highp float;
15-
precision highp int;
16-
#endif
17-
varying vec2 vCoord;
18-
varying vec4 outColor;
10+
function prefixV1() {
11+
return ` #ifdef GL_FRAGMENT_PRECISION_HIGH
12+
precision highp float;
13+
precision highp int;
14+
#else
15+
precision highp float;
16+
precision highp int;
17+
#endif
18+
varying vec2 vCoord;
19+
varying vec4 outColor;
20+
void setOutput(float result) {
21+
gl_FragColor.r = result;
22+
}
23+
void setPackedOutput(vec4 result) {
24+
gl_FragColor = result;
25+
}
26+
int calCeil(int a, int b) {
27+
return int(ceil(float(a) / float(b)));
28+
}
29+
${calMod()}
30+
${calDivision()}
31+
`;
32+
}
33+
function prefixV2() {
34+
return ` #version 300 es
35+
#ifdef GL_FRAGMENT_PRECISION_HIGH
36+
precision highp float;
37+
precision highp int;
38+
#else
39+
precision mediump float;
40+
precision mediump int;
41+
#endif
42+
43+
// 顶点shader透传的材质坐标
44+
in vec2 vCoord;
45+
out vec4 outColor;
1946
void setOutput(float result) {
20-
gl_FragColor.r = result;
47+
outColor.r = result;
2148
}
2249
void setPackedOutput(vec4 result) {
23-
gl_FragColor = result;
50+
outColor = result;
2451
}
25-
int calMod(int a, int b) {
26-
return a - a / b * b;
52+
int calCeil(int a, int b) {
53+
return int(ceil(float(a) / float(b)));
2754
}
28-
`;
29-
const prefixV2
30-
= ` #version 300 es
31-
#ifdef GL_FRAGMENT_PRECISION_HIGH
32-
precision highp float;
33-
precision highp int;
34-
#else
35-
precision mediump float;
36-
precision mediump int;
37-
#endif
38-
39-
// 顶点shader透传的材质坐标
40-
in vec2 vCoord;
41-
out vec4 outColor;
42-
void setOutput(float result) {
43-
outColor.r = result;
44-
}
45-
void setPackedOutput(vec4 result) {
46-
outColor = result;
47-
}
48-
int calMod(int a, int b) {
49-
return a - a / b * b;
50-
}
51-
`;
55+
${calMod()}
56+
${calDivision()}
57+
`;
58+
}
5259

53-
export default function genPrefixCode({ isFrameBufferSupportFloat, isFinalOp, isFloatTextureReadPixelsEnabled }) {
60+
export default function genPrefixCode({ frameBufferSupportFloat, isFinalOp, isFloatTextureReadPixelsEnabled }) {
5461
return env.get('webglVersion') === 2
55-
? prefixV2
56-
: isFrameBufferSupportFloat
57-
? prefixV1
62+
? prefixV2()
63+
: frameBufferSupportFloat
64+
? prefixV1()
5865
: isFinalOp && !isFloatTextureReadPixelsEnabled
59-
? prefix_uint
60-
: prefix_half;
66+
? prefix_uint()
67+
: prefix_half();
6168

6269
}

packages/paddlejs-backend-webgl/src/ops/atom/prefix_half.ts

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,42 @@
22
* @file prefix code of half float type
33
*/
44

5-
export default `
6-
#ifdef GL_FRAGMENT_PRECISION_HIGH
7-
precision highp float;
8-
precision highp int;
9-
#else
10-
precision highp float;
11-
precision highp int;
12-
#endif
13-
14-
#define isnan(value) isnan_custom(value)
15-
bool isnan_custom(float val) {
16-
return (val > 0. || val < 1. || val == 0.) ? false : true;
17-
}
18-
19-
varying vec2 vCoord;
20-
varying vec4 outColor;
21-
void setOutput(float result) {
22-
if(isnan(result)) {
23-
gl_FragColor.r = 0.0;
24-
}else {
25-
gl_FragColor.r = result;
26-
}
27-
}
28-
29-
void setPackedOutput(vec4 result) {
30-
gl_FragColor = result;
31-
}
32-
33-
int calMod(int a, int b) {
34-
return a - a / b * b;
35-
}
36-
`;
5+
import { calMod, calDivision } from './common_func_adaptor';
6+
7+
export default function () {
8+
return ` #ifdef GL_FRAGMENT_PRECISION_HIGH
9+
precision highp float;
10+
precision highp int;
11+
#else
12+
precision highp float;
13+
precision highp int;
14+
#endif
15+
16+
#define isnan(value) isnan_custom(value)
17+
bool isnan_custom(float val) {
18+
return (val > 0. || val < 1. || val == 0.) ? false : true;
19+
}
20+
21+
varying vec2 vCoord;
22+
varying vec4 outColor;
23+
void setOutput(float result) {
24+
if(isnan(result)) {
25+
gl_FragColor.r = 0.0;
26+
}else {
27+
gl_FragColor.r = result;
28+
}
29+
}
30+
31+
void setPackedOutput(vec4 result) {
32+
gl_FragColor = result;
33+
}
34+
35+
${calMod()}
36+
${calDivision()}
37+
38+
int calCeil(int a, int b) {
39+
return int(ceil(float(a) / float(b)));
40+
}
41+
`;
42+
}
3743

0 commit comments

Comments
 (0)