Skip to content

Commit 11b92b8

Browse files
committed
Supports new RenderTarget instances
1 parent f914a07 commit 11b92b8

1 file changed

Lines changed: 71 additions & 98 deletions

File tree

src/renderer/webgl/WebGLPipeline.js

Lines changed: 71 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var Class = require('../../utils/Class');
99
var DeepCopy = require('../../utils/object/DeepCopy');
1010
var GetFastValue = require('../../utils/object/GetFastValue');
1111
var Matrix4 = require('../../math/Matrix4');
12+
var RenderTarget = require('./RenderTarget');
1213
var Utils = require('./Utils');
1314
var WebGLShader = require('./WebGLShader');
1415

@@ -259,54 +260,30 @@ var WebGLPipeline = new Class({
259260
this.hasBooted = false;
260261

261262
/**
262-
* The WebGLFramebuffer this pipeline is targeting, if any.
263+
* An array of RenderTarget instances that belong to this pipeline.
263264
*
264-
* @name Phaser.Renderer.WebGL.WebGLPipeline#targetFramebuffer
265-
* @type {?WebGLFramebuffer}
265+
* @name Phaser.Renderer.WebGL.WebGLPipeline#renderTargets
266+
* @type {Phaser.Renderer.WebGL.RenderTarget[]}
266267
* @since 3.50.0
267268
*/
268-
this.targetFramebuffer = null;
269+
this.renderTargets = [];
269270

270271
/**
271-
* The WebGLTexture this pipeline is targeting, if any.
272+
* A reference to the currently bound Render Target instance from the `WebGLPipeline.renderTargets` array.
272273
*
273-
* @name Phaser.GameObjects.Shader#targetTexture
274-
* @type {?WebGLTexture}
274+
* @name Phaser.Renderer.WebGL.WebGLPipeline#currentRenderTarget
275+
* @type {Phaser.Renderer.WebGL.RenderTarget}
275276
* @since 3.50.0
276277
*/
277-
this.targetTexture = null;
278-
279-
/**
280-
* When using a targetTexture its dimensions are based on the scale of the WebGLRenderer.
281-
*
282-
* This value controls how much those dimensions are scaled.
283-
*
284-
* @name Phaser.GameObjects.Shader#targetScale
285-
* @type {number}
286-
* @since 3.50.0
287-
*/
288-
this.targetScale = GetFastValue(config, 'targetScale', 1);
289-
290-
/**
291-
* When using a targetTexture this controls if the target is
292-
* automatically cleared (via `gl.COLOR_BUFFER_BIT`) during the `postBind` method.
293-
*
294-
* If you need more control how, or if, the target is cleared, you can disable
295-
* this via the config, or even directly at runtime.
296-
*
297-
* @name Phaser.GameObjects.Shader#targetAutoClear
298-
* @type {boolean}
299-
* @since 3.50.0
300-
*/
301-
this.targetAutoClear = GetFastValue(config, 'targetAutoClear', true);
278+
this.currentRenderTarget;
302279

303280
/**
304281
* An array of all the WebGLShader instances that belong to this pipeline.
305282
*
306-
* All shaders must use the same attributes, as set by this pipeline, but can manage their own
307-
* uniforms.
283+
* Shaders manage their own attributes and uniforms, but share the same vertex data buffer,
284+
* which belongs to this pipeline.
308285
*
309-
* These are set in a call to the `setShadersFromConfig` method, which happens automatically,
286+
* Shaders are set in a call to the `setShadersFromConfig` method, which happens automatically,
310287
* but can also be called at any point in your game. See the method documentation for details.
311288
*
312289
* @name Phaser.Renderer.WebGL.WebGLPipeline#shaders
@@ -361,26 +338,56 @@ var WebGLPipeline = new Class({
361338
*/
362339
boot: function ()
363340
{
341+
var i;
364342
var gl = this.gl;
365343
var config = this.config;
344+
var renderer = this.renderer;
366345

367-
var target = GetFastValue(config, 'target', false);
346+
// Create the Render Targets
368347

369-
var renderer = this.renderer;
348+
var renderTargets = this.renderTargets;
370349

371-
var width = renderer.width * this.targetScale;
372-
var height = renderer.height * this.targetScale;
350+
var targets = GetFastValue(config, 'renderTarget', false);
373351

374-
if (target && width > 0 && height > 0)
352+
// If boolean, set to number = 1
353+
if (typeof(targets) === 'boolean' && targets)
375354
{
376-
this.targetTexture = renderer.createTextureFromSource(null, width, height, 0);
377-
this.targetFramebuffer = renderer.createFramebuffer(width, height, this.targetTexture, false);
355+
targets = 1;
378356
}
379357

358+
var width = renderer.width;
359+
var height = renderer.height;
360+
361+
if (typeof(targets) === 'number')
362+
{
363+
// Create this many default RTs
364+
for (i = 0; i < targets; i++)
365+
{
366+
renderTargets.push(new RenderTarget(this, width, height, 1, true));
367+
}
368+
}
369+
else if (Array.isArray(targets))
370+
{
371+
for (i = 0; i < targets.length; i++)
372+
{
373+
var scale = GetFastValue(targets[i], 'scale', 1);
374+
var autoClear = GetFastValue(targets[i], 'autoClear', 1);
375+
376+
renderTargets.push(new RenderTarget(this, width, height, scale, autoClear));
377+
}
378+
}
379+
380+
if (renderTargets.length)
381+
{
382+
// Default to the first one in the array
383+
this.currentRenderTarget = renderTargets[0];
384+
}
385+
386+
// Create the Shaders
387+
380388
this.setShadersFromConfig(config);
381389

382390
// Which shader has the largest vertex size?
383-
var i;
384391
var shaders = this.shaders;
385392
var vertexSize = 0;
386393
var vertexComponentCount = 0;
@@ -435,24 +442,6 @@ var WebGLPipeline = new Class({
435442
this.onBoot();
436443
},
437444

438-
clearTarget: function ()
439-
{
440-
var gl = this.gl;
441-
var renderer = this.renderer;
442-
var target = this.targetTexture;
443-
444-
if (target)
445-
{
446-
renderer.setFramebuffer(this.targetFramebuffer);
447-
448-
gl.clearColor(0, 0, 0, 0);
449-
450-
gl.clear(gl.COLOR_BUFFER_BIT);
451-
452-
renderer.setFramebuffer(null, false);
453-
}
454-
},
455-
456445
/**
457446
* This method is called once when this pipeline has finished being set-up
458447
* at the end of the boot process. By the time this method is called, all
@@ -708,26 +697,18 @@ var WebGLPipeline = new Class({
708697

709698
projectionMatrix.ortho(0, width, height, 0, -1000, 1000);
710699

711-
// Resize the target?
712-
var target = this.targetTexture;
713-
714-
if (target)
715-
{
716-
width *= this.targetScale;
717-
height *= this.targetScale;
718-
719-
var renderer = this.renderer;
700+
var i;
720701

721-
renderer.deleteFramebuffer(this.targetFramebuffer);
722-
renderer.deleteTexture(target);
702+
var targets = this.renderTargets;
723703

724-
this.targetTexture = renderer.createTextureFromSource(null, width, height, 0);
725-
this.targetFramebuffer = renderer.createFramebuffer(width, height, this.targetTexture, false);
704+
for (i = 0; i < targets.length; i++)
705+
{
706+
targets[i].resize(width, height);
726707
}
727708

728709
var shaders = this.shaders;
729710

730-
for (var i = 0; i < shaders.length; i++)
711+
for (i = 0; i < shaders.length; i++)
731712
{
732713
var shader = shaders[i];
733714

@@ -774,21 +755,9 @@ var WebGLPipeline = new Class({
774755
*/
775756
postBind: function (gameObject)
776757
{
777-
var renderer = this.renderer;
778-
var target = this.targetTexture;
779-
780-
if (target)
758+
if (this.currentRenderTarget)
781759
{
782-
renderer.pushFramebuffer(this.targetFramebuffer);
783-
784-
if (this.targetAutoClear)
785-
{
786-
var gl = this.gl;
787-
788-
gl.clearColor(0, 0, 0, 0);
789-
790-
gl.clear(gl.COLOR_BUFFER_BIT);
791-
}
760+
this.currentRenderTarget.bind();
792761
}
793762

794763
this.onPostBind(gameObject);
@@ -805,9 +774,9 @@ var WebGLPipeline = new Class({
805774
*/
806775
unbind: function ()
807776
{
808-
if (this.targetTexture)
777+
if (this.currentRenderTarget)
809778
{
810-
this.renderer.setFramebuffer(null);
779+
this.this.currentRenderTarget.unbind();
811780
}
812781
},
813782

@@ -865,19 +834,23 @@ var WebGLPipeline = new Class({
865834
*/
866835
postFlush: function (gameObject)
867836
{
868-
this.renderer.popFramebuffer();
837+
var target = this.currentRenderTarget;
838+
839+
if (target)
840+
{
841+
target.unbind();
842+
}
869843

870844
var wasBound = this.renderer.setVertexBuffer(this.vertexBuffer);
871845

872846
this.currentShader.bind(wasBound);
873847

874-
var texture = this.targetTexture;
875-
var width = texture.width;
876-
var height = texture.height;
877-
878-
this.drawFillRect(0, 0, width, height, 0x0, 1, texture, true);
848+
if (target)
849+
{
850+
target.draw();
879851

880-
this.flush(true);
852+
this.flush(true);
853+
}
881854

882855
this.onPostFlush(gameObject);
883856

0 commit comments

Comments
 (0)