Skip to content

Commit 58e4912

Browse files
committed
Moved uploadCanvasToGPU to WebGLRenderer
1 parent 0cdb150 commit 58e4912

3 files changed

Lines changed: 43 additions & 36 deletions

File tree

v3/src/gameobjects/text/static/Text.js

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ var Text = new Class({
6565
this.width = 1;
6666
this.height = 1;
6767

68-
this.gpuBuffer = null;
68+
this.canvasTexture = null;
6969
this.prevWidth = this.canvas.width;
7070
this.prevHeight = this.canvas.height;
7171

@@ -260,45 +260,19 @@ var Text = new Class({
260260

261261
if (this.state.game.config.renderType === Phaser.WEBGL)
262262
{
263-
this.uploadToGPU();
263+
this.canvasTexture = this.state.game.renderer.uploadCanvasToGPU(
264+
this.canvas,
265+
this.canvasTexture,
266+
!(this.prevWidth < canvas.width || this.prevHeight < canvas.height)
267+
);
268+
this.prevWidth = canvas.width;
269+
this.prevHeight = canvas.height;
264270
}
265271

266272
return this;
267273
},
268274

269-
uploadToGPU: function ()
270-
{
271-
var gl = this.state.game.renderer.gl;
272-
var currentTexture2D = this.state.game.renderer.currentTexture2D;
273-
var canvas = this.canvas;
274-
275-
if (this.gpuBuffer === null)
276-
{
277-
this.gpuBuffer = gl.createTexture();
278-
}
279-
280-
if (this.prevWidth < canvas.width || this.prevHeight < canvas.height)
281-
{
282-
/* New canvas is too big. We need to reallocate the texture */
283-
gl.bindTexture(gl.TEXTURE_2D, this.gpuBuffer);
284-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
285-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
286-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
287-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
288-
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
289-
this.prevWidth = canvas.width;
290-
this.prevHeight = canvas.height;
291-
}
292-
else
293-
{
294-
/* if the canvas is smaller we just update the resource */
295-
gl.bindTexture(gl.TEXTURE_2D, this.gpuBuffer);
296-
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
297-
}
298-
299-
/* we must rebind old texture */
300-
gl.bindTexture(gl.TEXTURE_2D, currentTexture2D);
301-
}
275+
302276
});
303277

304278
module.exports = Text;

v3/src/gameobjects/text/static/TextWebGLRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var TextWebGLRenderer = function (renderer, src, interpolationPercentage, camera
55
return;
66
}
77

8-
renderer.spriteBatch.addSpriteTexture(src, camera, src.gpuBuffer, src.prevWidth, src.prevHeight);
8+
renderer.spriteBatch.addSpriteTexture(src, camera, src.canvasTexture, src.prevWidth, src.prevHeight);
99
};
1010

1111
module.exports = TextWebGLRenderer;

v3/src/renderer/webgl/WebGLRenderer.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,39 @@ WebGLRenderer.prototype = {
343343
return batchInstance;
344344
}
345345
return null;
346+
},
347+
348+
uploadCanvasToGPU: function (srcCanvas, dstTexture, shouldUpdateResource)
349+
{
350+
var gl = this.gl;
351+
352+
if (!dstTexture)
353+
{
354+
/* only call this once */
355+
dstTexture = gl.createTexture();
356+
}
357+
358+
if (shouldUpdateResource)
359+
{
360+
/* Update resource */
361+
gl.bindTexture(gl.TEXTURE_2D, dstTexture);
362+
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, srcCanvas);
363+
}
364+
else
365+
{
366+
/* Allocate or Reallocate resource */
367+
gl.bindTexture(gl.TEXTURE_2D, dstTexture);
368+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, srcCanvas);
369+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
370+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
371+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
372+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
373+
}
374+
375+
/* we must rebind old texture */
376+
gl.bindTexture(gl.TEXTURE_2D, this.currentTexture2D);
377+
378+
return dstTexture;
346379
}
347380
};
348381

0 commit comments

Comments
 (0)