Skip to content

Commit 6a1700a

Browse files
authored
Merge pull request #431 from JingyuanZhang/master
fix: fix glsl calculate float(index) error in texture boundary
2 parents 13c6d04 + 2d7df17 commit 6a1700a

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,48 @@
33
* @author yueshuangyan
44
*/
55

6+
// 根据tensor坐标获取对应纹理位置的pixel.r,返回值为 R 通道值
67
export function getValueFromTensorPos(
78
textureName: string,
89
{ width_shape, height_shape, channel, width_texture, height_texture }
910
) {
1011
const chw = width_shape * height_shape * channel;
1112
const hw = width_shape * height_shape;
1213
return `
13-
// 根据tensor坐标获取这个tensor位置的值
1414
float getValueFromTensorPos_${textureName}(int n, int c, int h, int w) {
1515
int index = n * ${chw} + c * ${hw} + h * ${width_shape} + w;
16-
int pos_w = int(mod(float(index), float(${width_texture})));
16+
// 0.01 hack: 在 PC/WISE 机器上,出现某个值(比如 index 为 3520) float(index) 和 float(3520) 返回值不同的情况,目前 +0.01 hack
17+
int pos_w = int(mod(float(index) + 0.01, float(${width_texture})));
1718
int pos_h = index / int(${width_texture});
1819
vec4 pixels = TEXTURE2D(texture_${textureName},
1920
vec2(
2021
(float(pos_w) + 0.5) / float(${width_texture}),
2122
(float(pos_h) + 0.5) / float(${height_texture})
2223
)
2324
);
24-
// 只用了r通道
2525
return pixels.r;
2626
}`;
2727
}
2828

29+
// 根据tensor坐标获取对应纹理位置的pixel,返回值为四通道值 RGBA
2930
export function getValueFromTensorPosPacking(
3031
textureName: string,
3132
{ channel, height_shape, width_texture, height_texture, width_shape }
3233
) {
3334
const chw = width_shape * height_shape * channel;
3435
const hw = width_shape * height_shape;
3536
return `
36-
// 根据tensor坐标获取这个tensor位置的值
3737
vec4 getValueFromTensorPosPacking_${textureName}(int n, int c, int h, int w) {
3838
int index = n * ${chw} + c * ${hw} + h * ${width_shape} + w;
39-
int pos_w = int(mod(float(index), float(${width_texture})));
39+
// 0.01 hack: 在 PC/WISE 设备上,出现某个值(比如 index 为 3520) float(index) 和 float(3520) 返回值不同的情况,目前 +0.01 hack
40+
int pos_w = int(mod(float(index) + 0.01, float(${width_texture})));
4041
int pos_h = index / int(${width_texture});
4142
vec4 pixels = TEXTURE2D(texture_${textureName},
4243
vec2(
4344
(float(pos_w) + 0.5) / float(${width_texture}),
4445
(float(pos_h) + 0.5) / float(${height_texture})
4546
)
4647
);
47-
// 只用了r通道
4848
return pixels;
4949
}`;
5050
}

packages/paddlejs-backend-webgl/src/ops/shader/conv2d.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,22 @@ function mainFunc(
1313
fuse_relu,
1414
filter_nearest_vec4,
1515
filter_remainder_vec4,
16-
act_type
16+
act_type,
17+
padding_algorithm = ''
1718
}
1819
) {
1920
const [stride_v = 1, stride_h = 1] = strides;
20-
const [padTop = 0, padLeft = 0] = paddings;
21+
let [padTop = 0, padLeft = 0] = paddings;
2122
const [dilation_v = 1, dilation_h = 1] = dilations;
2223

24+
function calcPadding() {
25+
if (padding_algorithm === 'SAME'
26+
&& ((Math.ceil((origin.width_shape - filter.width_shape) / stride_v) + 1) !== out.width_shape)) {
27+
padTop = 1;
28+
padLeft = 1;
29+
}
30+
}
31+
calcPadding();
2332
return `
2433
// start函数
2534
void main(void) {

0 commit comments

Comments
 (0)