Skip to content

Commit 3f5ec1a

Browse files
committed
fix(webgl): close pack_output in webgl 1.0
1 parent 2b5661a commit 3f5ec1a

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,8 @@ export default class WebGLBackend extends PaddlejsBackend {
156156

157157
const pbo = this.createPBO();
158158
await this.createAndWaitForFence();
159-
const result = this.downloadFloat32TensorFromBuffer(pbo);
160-
161159
const shape = fetchInfo ? fetchInfo.shape : [];
162-
if (env.get('webgl_pack_output')) {
163-
return result.slice(0, getSizeFromShape(shape));
164-
}
165-
166-
return result;
160+
return this.downloadFloat32TensorFromBuffer(pbo, shape);
167161
}
168162

169163
createPBO() {
@@ -234,25 +228,23 @@ export default class WebGLBackend extends PaddlejsBackend {
234228
fn();
235229
}
236230

237-
downloadFloat32TensorFromBuffer(buffer) {
231+
downloadFloat32TensorFromBuffer(buffer, shape) {
238232
const size: number = 4 * this.width_texture_out * this.height_texture_out;
239233
if (this.glVersion === 2) {
240234
const gl2 = this.gl as WebGL2RenderingContext;
241235
const pixels = new Float32Array(size);
242236
gl2.bindBuffer(gl2.PIXEL_PACK_BUFFER, buffer);
243237
gl2.getBufferSubData(gl2.PIXEL_PACK_BUFFER, 0, pixels);
244238
gl2.bindBuffer(gl2.PIXEL_PACK_BUFFER, null);
245-
246239
const result: number[] = [];
247240
if (env.get('webgl_pack_output')) {
248-
return Array.from(pixels);
241+
return Array.from(pixels).slice(0, getSizeFromShape(shape));
249242
}
250243
for (let i = 0; i < this.width_texture_out * this.height_texture_out; i++) {
251244
result.push(pixels[4 * i]);
252245
}
253246
return result;
254247
}
255-
256248
const pixels = buffer;
257249
const result = [] as number[];
258250
for (let i = 0; i < this.width_texture_out * this.height_texture_out; i++) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,12 @@ export class GLTexture {
176176

177177
if (
178178
data instanceof Uint8Array
179-
|| data instanceof Uint8ClampedArray
180-
|| !(data instanceof Float32Array || data instanceof Array)) {
179+
|| data instanceof Uint8ClampedArray) {
181180
// case1: 输入为0~255之间的像素数据,类型为Uint8Array 或 Uint8ClampedArray
182181
// case2: 小程序环境,输入数据可能是HTMLImageElement、HTMLVideoElement、HTMLCanvasElement、小程序中图像的临时path string。
183182
texeltype = gl.UNSIGNED_BYTE;
183+
}
184+
else if (!(data instanceof Float32Array || data instanceof Array)) {
184185
gl.texImage2D(
185186
gl.TEXTURE_2D,
186187
0,

packages/paddlejs-core/src/transform/packOutOp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class PackOut extends Transformer {
1515
}
1616

1717
transform(...args: any) {
18-
if (!env.get('webgl_pack_output')) {
18+
if (!env.get('webgl_pack_output') || env.get('webglVersion') === 1) {
1919
return;
2020
}
2121
const [ops, vars] = args;

0 commit comments

Comments
 (0)