Skip to content

Commit e032be2

Browse files
Merge pull request #218 from JingyuanZhang/master
feat(core&webgl): update cacheTextures structure to accelerate preheat process
2 parents 569b69d + 0ca0fba commit e032be2

File tree

4 files changed

+17
-20
lines changed

4 files changed

+17
-20
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export default class WebGLBackend extends PaddlejsBackend {
2222
pbo?: WebGLBuffer | null;
2323
vertexBuffer?: WebGLBuffer | null;
2424
textureConf: TextureConfig | null;
25-
nextTexture?: WebGLTexture | null;
2625
currentTexture?: WebGLTexture | null;
2726
textureBufferIndex?: number;
2827
cacheTextures: object;
@@ -308,41 +307,38 @@ export default class WebGLBackend extends PaddlejsBackend {
308307
if (!loc) {
309308
return;
310309
}
311-
that.initTexture(textureIndex, item, iLayer, isRendered, isPacked, modelName);
310+
that.initTexture(textureIndex, item, isPacked);
312311
gl.uniform1i(loc, textureIndex++);
313312
});
314313
// gl.clearColor(.0, .0, .0, 1);
315314
// gl.clear(gl.COLOR_BUFFER_BIT);
316315
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
317316
}
318317

319-
initTexture(index, item, iLayer, isRendered, isPacked, modelName) {
318+
initTexture(index, item, isPacked) {
320319
const gl = this.gl;
321320
const textureConf = this.textureConf as TextureConfig;
322321
const tensorName = item.opts.type;
323-
const prefix = modelName + '_';
324322
let texture;
325323

326-
if (!item.data) {
327-
texture = this.texturesMap[item.opts.type];
324+
if (!item.persistable) {
325+
texture = this.texturesMap[tensorName];
328326
}
329327
else {
330-
// texture = gl.createTexture();
331-
if (isRendered && item.persistable) {
332-
const tData = this.cacheTextures[prefix + iLayer];
333-
texture = tData['texture_' + tensorName];
328+
this.cacheTextures = this.cacheTextures || {};
329+
if (this.cacheTextures[tensorName]) {
330+
texture = this.cacheTextures[tensorName];
334331
}
335332
else {
336333
texture = gl.createTexture();
337-
this.cacheTextures[prefix + iLayer] = this.cacheTextures[prefix + iLayer] || {};
338-
this.cacheTextures[prefix + iLayer]['texture_' + tensorName] = texture;
334+
this.cacheTextures[tensorName] = texture;
339335
}
340336
}
341337

342338
gl.activeTexture(gl[`TEXTURE${index}`]);
343339
gl.bindTexture(gl.TEXTURE_2D, texture);
344340

345-
if (item.data && (!isRendered || !item.persistable)) {
341+
if (item.data) {
346342
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
347343
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
348344
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
@@ -395,6 +391,7 @@ export default class WebGLBackend extends PaddlejsBackend {
395391
gl.FLOAT,
396392
temp);
397393
}
394+
item.data = null;
398395
}
399396
}
400397

packages/paddlejs-core/__tests__/spec/mediaProcessor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('test mediaProcessor with scale and targetSize', () => {
4747

4848
test('test api process', () => {
4949
const data = processror.process(img, mediaParams);
50-
expect(data).toEqual([{ data: [], shape: [1, 3, 224, 224], name: 'image', persistable: false }]);
50+
expect(data).toEqual([{ data: [], shape: [1, 3, 224, 224], name: 'image', persistable: true }]);
5151
});
5252
});
5353

packages/paddlejs-core/src/mediaProcessor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export default class MediaProcessor {
6969
data: data,
7070
shape: opt.shape || opt.targetShape,
7171
name: 'image',
72-
persistable: false
72+
persistable: true
7373
}] as InputFeed[];
7474
}
7575

@@ -86,7 +86,7 @@ export default class MediaProcessor {
8686
data,
8787
shape: [1, 4, opt.targetShape[2], opt.targetShape[3]],
8888
name: 'image',
89-
persistable: false
89+
persistable: true
9090
}] as InputFeed[];
9191
}
9292

@@ -95,7 +95,7 @@ export default class MediaProcessor {
9595
data,
9696
shape: opt.targetShape || opt.shape,
9797
name: 'image',
98-
persistable: false
98+
persistable: true
9999
}] as InputFeed[];
100100
}
101101

packages/paddlejs-core/src/runner.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export default class Runner {
138138
data: new Float32Array(data as number[]),
139139
shape: shape || [1, fc, fh, fw],
140140
name: 'image',
141-
persistable: false
141+
persistable: true
142142
}
143143
];
144144
}
@@ -151,7 +151,7 @@ export default class Runner {
151151
data: new Float32Array(inputData),
152152
shape: shape || [1, fc, height || fh, width || fw],
153153
name: 'image',
154-
persistable: false
154+
persistable: true
155155
}
156156
];
157157
}
@@ -193,7 +193,7 @@ export default class Runner {
193193
data: new Float32Array(fc * fh * fw).fill(1.0),
194194
name: 'image',
195195
shape: [1, fc, fh, fw],
196-
persistable: false
196+
persistable: true
197197
};
198198
}
199199

0 commit comments

Comments
 (0)