Skip to content

Commit 2f686ae

Browse files
committed
feat(texture): reallocate texture shape
1 parent a59a6a9 commit 2f686ae

File tree

3 files changed

+48
-57
lines changed

3 files changed

+48
-57
lines changed

packages/paddlejs-backend-webgl/src/ops/shader/custom/pack_out.ts

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,31 @@
33
*/
44

55
function mainFunc(
6-
{ origin },
6+
{ out },
77
{}
88
) {
9-
const width_texture_origin = origin.width_texture;
10-
const height_texture_origin = origin.height_texture;
119
return `
1210
13-
vec2 getOriginCoord(float x, float y) {
14-
if (x > float(${width_texture_origin})) {
15-
int num = int(x / float(${width_texture_origin}));
16-
x = mod(x, float(${width_texture_origin}));
17-
y = y + float(num);
18-
}
19-
return vec2(x, y);
20-
}
21-
22-
float getClipedCoordRed(vec2 xy) {
23-
return TEXTURE2D(
24-
texture_origin,
25-
vec2(float(xy.x / float(${width_texture_origin})), float(xy.y / float(${height_texture_origin})))
26-
).r;
27-
}
28-
2911
// start函数
3012
void main() {
31-
13+
ivec4 oPos = getOutputTensorPos();
3214
vec2 outCoord = vCoord.xy * _2d_shape_texture_out;
33-
vec4 out4;
34-
float x = floor(outCoord.x) * 4.0;
35-
float y = floor(outCoord.y) * 4.0 + 0.5;
36-
float x0 = x + 0.5;
37-
float x1 = x + 1.5;
38-
float x2 = x + 2.5;
39-
float x3 = x + 3.5;
15+
int index = int(outCoord.x) + int(outCoord.y) * int(${out.width_texture});
16+
17+
int first = index * 4;
18+
int sec = index * 4 + 1;
19+
int third = index * 4 + 2;
20+
int fourth = index * 4 + 3;
4021
41-
vec2 xy0 = getOriginCoord(x0, y);
42-
vec2 xy1 = getOriginCoord(x1, y);
43-
vec2 xy2 = getOriginCoord(x2, y);
44-
vec2 xy3 = getOriginCoord(x3, y);
22+
ivec4 rPos = getTensorPosFromArrayIndex_origin(first);
23+
ivec4 gPos = getTensorPosFromArrayIndex_origin(sec);
24+
ivec4 bPos = getTensorPosFromArrayIndex_origin(third);
25+
ivec4 aPos = getTensorPosFromArrayIndex_origin(fourth);
4526
46-
float r = getClipedCoordRed(xy0);
47-
float g = getClipedCoordRed(xy1);
48-
float b = getClipedCoordRed(xy2);
49-
float a = getClipedCoordRed(xy3);
27+
float r = getValueFromTensorPos_origin(rPos.r, rPos.g, rPos.b, rPos.a);
28+
float g = getValueFromTensorPos_origin(gPos.r, gPos.g, gPos.b, gPos.a);
29+
float b = getValueFromTensorPos_origin(bPos.r, bPos.g, bPos.b, bPos.a);
30+
float a = getValueFromTensorPos_origin(aPos.r, aPos.g, aPos.b, aPos.a);
5031
5132
setPackedOutput(vec4(r, g, b, a));
5233
}
@@ -55,6 +36,6 @@ function mainFunc(
5536
export default {
5637
mainFunc,
5738
textureFuncConf: {
58-
origin: ['getValueFromTensorPosPacking', 'getValueFromTensorPos']
39+
origin: ['getValueFromTensorPos', 'getTensorPosFromArrayIndex']
5940
}
6041
};

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,26 @@ function genIntDataCode(dataArr: number[], key) {
8181
return dataStr;
8282
}
8383

84+
function getSmallestDivisor(number, base) {
85+
let divisor = base;
86+
if (number % divisor === 0) {
87+
return divisor;
88+
}
89+
while (divisor < number) {
90+
if (number % divisor === 0) {
91+
break;
92+
}
93+
divisor++;
94+
}
95+
return divisor;
96+
}
97+
8498
export {
8599
nhwc2nchw,
86100
getSizeFromShape,
87101
reduceShape,
88102
genFpDataCode,
89103
genFpFloatArr,
90-
genIntDataCode
104+
genIntDataCode,
105+
getSmallestDivisor
91106
};

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

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { Tensor } from '../types';
77
import { env } from '@paddlejs/paddlejs-core';
88
import { WebGLContextAttributes, UniformType } from './webgl_types';
9+
import { getSmallestDivisor } from '../utils/dataProcess';
910

1011

1112
// 枚举类
@@ -85,10 +86,6 @@ export class GLHelper {
8586
return env.get('webglVersion');
8687
}
8788

88-
public static getWebglTextureLimitCut() {
89-
return 8;
90-
}
91-
9289
public static createCanvas() {
9390
return env.get('canvas') || (document && document.createElement('canvas'));
9491
}
@@ -410,29 +407,27 @@ export class GLHelper {
410407
const {
411408
shape = []
412409
} = tensor;
413-
const b = shape[0];
414-
const c = shape[1];
415-
const h = shape[2];
416-
const w = shape[3];
417-
let height = b * h;
418-
let width = c * w;
419-
420-
// trick TEXTURE_SIZE 超限问题,后续升级更优解
421-
if (height > GL_TEXTURE_MAX_SIZE || width > GL_TEXTURE_MAX_SIZE) {
422-
const textureCut = this.getWebglTextureLimitCut();
423-
env.get('debug') && console.error('大小超限', shape);
424-
height *= textureCut;
425-
width = c * (Math.ceil(w / textureCut));
426-
if (height > GL_TEXTURE_MAX_SIZE || width > GL_TEXTURE_MAX_SIZE) {
427-
const requested = `[${width}x${height}]`;
410+
const [smallest, sec, third, biggest] = [...shape].sort((prev, cur) => prev - cur);
411+
let texW = smallest * biggest;
412+
let texH = sec * third;
413+
414+
if (texW > GL_TEXTURE_MAX_SIZE || texH > GL_TEXTURE_MAX_SIZE) {
415+
const [smaller, bigger] = [texW, texH].sort((prev, cur) => prev - cur);
416+
const baseCut = Math.ceil(bigger / GL_TEXTURE_MAX_SIZE);
417+
const cut = getSmallestDivisor(bigger, baseCut);
418+
texW = smaller * cut;
419+
texH = Math.ceil(bigger / cut);
420+
env.get('debug') && console.error('大小超限', shape, [texH, texW]);
421+
if (texW > GL_TEXTURE_MAX_SIZE || texH > GL_TEXTURE_MAX_SIZE) {
422+
const requested = `[${texW}x${texH}]`;
428423
const max = `[${GL_TEXTURE_MAX_SIZE}x${GL_TEXTURE_MAX_SIZE}]`;
429424
throw new Error(
430425
'Requested texture size ' + requested
431426
+ ' greater than WebGL maximum on this browser / GPU ' + max + '.');
432427
}
433428
}
434429

435-
tensor.shape_texture = [height, width];
430+
tensor.shape_texture = [texH, texW];
436431
}
437432

438433
}

0 commit comments

Comments
 (0)