Sorting out the scissor stack

This commit is contained in:
Richard Davey 2018-07-26 17:50:07 +01:00
parent a3c4c60519
commit 1777e16227

View file

@ -302,7 +302,8 @@ var WebGLRenderer = new Class({
* @type {Uint32Array} * @type {Uint32Array}
* @since 3.0.0 * @since 3.0.0
*/ */
this.currentScissor = new Uint32Array([ 0, 0, this.width, this.height ]); // this.currentScissor = new Uint32Array([ 0, 0, this.width, this.height ]);
this.currentScissor = null;
/** /**
* Index to the scissor stack top * Index to the scissor stack top
@ -312,7 +313,7 @@ var WebGLRenderer = new Class({
* @default 0 * @default 0
* @since 3.0.0 * @since 3.0.0
*/ */
this.currentScissorIdx = 0; // this.currentScissorIdx = 0;
/** /**
* Stack of scissor data * Stack of scissor data
@ -321,7 +322,8 @@ var WebGLRenderer = new Class({
* @type {Uint32Array} * @type {Uint32Array}
* @since 3.0.0 * @since 3.0.0
*/ */
this.scissorStack = new Uint32Array(4 * 1000); // this.scissorStack = new Uint32Array(4 * 1000);
this.scissorStack = [];
// Setup context lost and restore event listeners // Setup context lost and restore event listeners
@ -447,7 +449,7 @@ var WebGLRenderer = new Class({
{ {
this.contextLost = true; this.contextLost = true;
throw new Error('This browser does not support WebGL. Try using the Canvas pipeline.'); throw new Error('WebGL unsupported');
} }
this.gl = gl; this.gl = gl;
@ -515,6 +517,9 @@ var WebGLRenderer = new Class({
this.addPipeline('Light2D', new ForwardDiffuseLightPipeline({ game: this.game, renderer: this })); this.addPipeline('Light2D', new ForwardDiffuseLightPipeline({ game: this.game, renderer: this }));
this.setBlendMode(CONST.BlendModes.NORMAL); this.setBlendMode(CONST.BlendModes.NORMAL);
// this.pushScissor(0, 0, this.width, this.height);
this.resize(this.width, this.height); this.resize(this.width, this.height);
this.game.events.once('ready', this.boot, this); this.game.events.once('ready', this.boot, this);
@ -580,7 +585,10 @@ var WebGLRenderer = new Class({
pipelines[pipelineName].resize(width, height, resolution); pipelines[pipelineName].resize(width, height, resolution);
} }
this.currentScissor.set([ 0, 0, this.width, this.height ]); // if (this.currentScissor)
// {
// this.currentScissor = [ 0, 0, this.width, this.height ];
// }
this.drawingBufferHeight = gl.drawingBufferHeight; this.drawingBufferHeight = gl.drawingBufferHeight;
@ -757,49 +765,29 @@ var WebGLRenderer = new Class({
* *
* @method Phaser.Renderer.WebGL.WebGLRenderer#setScissor * @method Phaser.Renderer.WebGL.WebGLRenderer#setScissor
* @since 3.0.0 * @since 3.0.0
*
* @param {integer} x - [description]
* @param {integer} y - [description]
* @param {integer} w - [description]
* @param {integer} h - [description]
*
* @return {Phaser.Renderer.WebGL.WebGLRenderer} [description]
*/ */
setScissor: function (x, y, w, h) setScissor: function ()
{ {
var gl = this.gl; var gl = this.gl;
var currentScissor = this.currentScissor;
var enabled = (x === 0 && y === 0 && w === this.width && h === this.height && w >= 0 && h >= 0);
// TODO: If new scissor is same as old scissor, skip setting it again var current = this.currentScissor;
// TODO: If scissor is viewport size, skip setting altogether
if (currentScissor[0] !== x || var x = current[0];
currentScissor[1] !== y || var y = current[1];
currentScissor[2] !== w || var w = current[2];
currentScissor[3] !== h) var h = current[3];
// https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/scissor
var box = gl.getParameter(gl.SCISSOR_BOX);
if (box && (box[0] !== x || box[1] !== y || box[2] !== w || box[3] !== h))
{ {
this.flush(); this.flush();
gl.enable(gl.SCISSOR_TEST);
gl.scissor(x, (this.drawingBufferHeight - y - h), w, h);
} }
currentScissor[0] = x;
currentScissor[1] = y;
currentScissor[2] = w;
currentScissor[3] = h;
this.currentScissorEnabled = enabled;
if (enabled)
{
gl.disable(gl.SCISSOR_TEST);
return this;
}
gl.enable(gl.SCISSOR_TEST);
gl.scissor(x, (this.drawingBufferHeight - y - h), w, h);
return this;
}, },
/** /**
@ -818,18 +806,17 @@ var WebGLRenderer = new Class({
pushScissor: function (x, y, w, h) pushScissor: function (x, y, w, h)
{ {
var scissorStack = this.scissorStack; var scissorStack = this.scissorStack;
var stackIndex = this.currentScissorIdx; var stackLength = scissorStack.length;
var currentScissor = this.currentScissor;
scissorStack[stackIndex + 0] = currentScissor[0]; var scissor = [ x, y, w, h ];
scissorStack[stackIndex + 1] = currentScissor[1];
scissorStack[stackIndex + 2] = currentScissor[2]; scissorStack.push(scissor);
scissorStack[stackIndex + 3] = currentScissor[3];
this.currentScissorIdx += 4; this.currentScissor = scissor;
this.setScissor(x, y, w, h);
return this; this.setScissor();
return scissor;
}, },
/** /**
@ -837,23 +824,14 @@ var WebGLRenderer = new Class({
* *
* @method Phaser.Renderer.WebGL.WebGLRenderer#popScissor * @method Phaser.Renderer.WebGL.WebGLRenderer#popScissor
* @since 3.0.0 * @since 3.0.0
*
* @return {Phaser.Renderer.WebGL.WebGLRenderer} [description]
*/ */
popScissor: function () popScissor: function ()
{ {
var scissorStack = this.scissorStack; var scissorStack = this.scissorStack;
var stackIndex = this.currentScissorIdx - 4;
var x = scissorStack[stackIndex + 0]; this.currentScissor = scissorStack.pop();
var y = scissorStack[stackIndex + 1];
var w = scissorStack[stackIndex + 2];
var h = scissorStack[stackIndex + 3];
this.currentScissorIdx = stackIndex; this.setScissor();
this.setScissor(x, y, w, h);
return this;
}, },
/** /**
@ -1562,6 +1540,9 @@ var WebGLRenderer = new Class({
pipelines[key].onPreRender(); pipelines[key].onPreRender();
} }
this.scissorStack = [];
this.currentScissor = null;
this.setPipeline(this.pipelines.TextureTintPipeline); this.setPipeline(this.pipelines.TextureTintPipeline);
}, },