From 15c0313cdc1d0ef3b7febfe1f748c60fca7c4c4b Mon Sep 17 00:00:00 2001 From: INARI_DARKFOX <39549290+inaridarkfox4231@users.noreply.github.com> Date: Mon, 23 Jan 2023 18:02:50 +0900 Subject: [PATCH 01/10] add Array for recording availability of registers Prepare an array to record the availability of registers to prevent the same register from being disabled more than once. --- src/webgl/p5.RendererGL.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index 09e994e175..a96365829d 100755 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -112,6 +112,8 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) { this._useLineColor = false; this._useVertexColor = false; + + this.registerEnabled = []; this._tint = [255, 255, 255, 255]; From b1690b3632497c1715fcca9993e5a9a01f529f50 Mon Sep 17 00:00:00 2001 From: INARI_DARKFOX <39549290+inaridarkfox4231@users.noreply.github.com> Date: Mon, 23 Jan 2023 18:06:52 +0900 Subject: [PATCH 02/10] Record register availability in enableAttrib() In the enableAttrib function, when enabling a register, tell the renderer that it is available by using the location number as an argument. --- src/webgl/p5.Shader.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index f67696a506..a8934ff7af 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -565,6 +565,8 @@ p5.Shader.prototype.enableAttrib = function( const gl = this._renderer.GL; if (!attr.enabled) { gl.enableVertexAttribArray(loc); + // Record register availability + this._renderer.registerEnabled[loc] = true; attr.enabled = true; } this._renderer.GL.vertexAttribPointer( From cbc1677dff9d328f6e11dc35ab51a5467f70726a Mon Sep 17 00:00:00 2001 From: INARI_DARKFOX <39549290+inaridarkfox4231@users.noreply.github.com> Date: Mon, 23 Jan 2023 18:12:54 +0900 Subject: [PATCH 03/10] Disable registers not used for drawing If the registers corresponding to the attributes that are not used for drawing are left open, the data still stored there will cause problems. So it asks the renderer if the register corresponding to it is open, and if it is, it closes it and notifies the renderer and shader of that state. --- src/webgl/p5.RenderBuffer.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/webgl/p5.RenderBuffer.js b/src/webgl/p5.RenderBuffer.js index a586fb0323..f3852863c8 100644 --- a/src/webgl/p5.RenderBuffer.js +++ b/src/webgl/p5.RenderBuffer.js @@ -58,6 +58,14 @@ p5.RenderBuffer.prototype._prepareBuffer = function(geometry, shader) { } // enable the attribute shader.enableAttrib(attr, this.size); + } else { + const loc = attr.location; + if (loc == -1 || !this._renderer.registerEnabled[loc]) { return; } + // Disable register corresponding to unused attribute + gl.disableVertexAttribArray(loc); + attr.enabled = false; + // Record register availability + this._renderer.registerEnabled[loc] = false; } }; From e8fea3ffac5cc9ee2c68987bb639f2c49cc9565c Mon Sep 17 00:00:00 2001 From: INARI_DARKFOX <39549290+inaridarkfox4231@users.noreply.github.com> Date: Mon, 23 Jan 2023 18:37:45 +0900 Subject: [PATCH 04/10] Use === instead of ==. Use === instead of ==. --- src/webgl/p5.RenderBuffer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webgl/p5.RenderBuffer.js b/src/webgl/p5.RenderBuffer.js index f3852863c8..b925574617 100644 --- a/src/webgl/p5.RenderBuffer.js +++ b/src/webgl/p5.RenderBuffer.js @@ -60,7 +60,7 @@ p5.RenderBuffer.prototype._prepareBuffer = function(geometry, shader) { shader.enableAttrib(attr, this.size); } else { const loc = attr.location; - if (loc == -1 || !this._renderer.registerEnabled[loc]) { return; } + if (loc === -1 || !this._renderer.registerEnabled[loc]) { return; } // Disable register corresponding to unused attribute gl.disableVertexAttribArray(loc); attr.enabled = false; From f56791771606ea99f9aadeab4c69f8f94eaf59b1 Mon Sep 17 00:00:00 2001 From: INARI_DARKFOX <39549290+inaridarkfox4231@users.noreply.github.com> Date: Mon, 23 Jan 2023 18:38:34 +0900 Subject: [PATCH 05/10] remove trailing space remove trailing space --- src/webgl/p5.RendererGL.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index a96365829d..986d39443d 100755 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -112,7 +112,7 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) { this._useLineColor = false; this._useVertexColor = false; - + this.registerEnabled = []; this._tint = [255, 255, 255, 255]; From 1c7cbbd0504228a20739acc7f492a118de69a59f Mon Sep 17 00:00:00 2001 From: INARI_DARKFOX <39549290+inaridarkfox4231@users.noreply.github.com> Date: Wed, 25 Jan 2023 10:03:01 +0900 Subject: [PATCH 06/10] Enable register even if it is disabled If the shader doesn't know that the register has been disabled by another shader, it can't enable it. Therefore, even if the corresponding shader does not know about it, if the register used by it is disabled, enable processing should be performed. --- src/webgl/p5.Shader.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index a8934ff7af..5159375986 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -561,9 +561,12 @@ p5.Shader.prototype.enableAttrib = function( ); } const loc = attr.location; + // Get register availability + const registerIsEnabled = this._renderer.registerEnabled[loc]; if (loc !== -1) { const gl = this._renderer.GL; - if (!attr.enabled) { + // Enable register even if it is disabled + if (!attr.enabled || !registerIsEnabled) { gl.enableVertexAttribArray(loc); // Record register availability this._renderer.registerEnabled[loc] = true; From 8eb10650d1b74955ca726a7703e808fc06be565a Mon Sep 17 00:00:00 2001 From: INARI_DARKFOX <39549290+inaridarkfox4231@users.noreply.github.com> Date: Thu, 26 Jan 2023 00:07:54 +0900 Subject: [PATCH 07/10] remove "enabled" property Since only the global array (named "registerEnabled") needs to know the enabled/disabled state of a register, we eliminate the "enabled" property of attribute. --- src/webgl/p5.Shader.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index 5159375986..0439a0e34e 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -561,16 +561,13 @@ p5.Shader.prototype.enableAttrib = function( ); } const loc = attr.location; - // Get register availability - const registerIsEnabled = this._renderer.registerEnabled[loc]; if (loc !== -1) { const gl = this._renderer.GL; // Enable register even if it is disabled - if (!attr.enabled || !registerIsEnabled) { + if (!this._renderer.registerEnabled[loc]) { gl.enableVertexAttribArray(loc); // Record register availability this._renderer.registerEnabled[loc] = true; - attr.enabled = true; } this._renderer.GL.vertexAttribPointer( loc, From 783e76580b27175d620cc6109a7b3825b01f133c Mon Sep 17 00:00:00 2001 From: INARI_DARKFOX <39549290+inaridarkfox4231@users.noreply.github.com> Date: Thu, 26 Jan 2023 00:09:31 +0900 Subject: [PATCH 08/10] Remove the enabled property of attribute. It is impossible for the shader itself to know when a register is available or not. I decided to leave that to the global array. So I'm going to remove that property. --- src/webgl/p5.RenderBuffer.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/webgl/p5.RenderBuffer.js b/src/webgl/p5.RenderBuffer.js index b925574617..84304fdd8c 100644 --- a/src/webgl/p5.RenderBuffer.js +++ b/src/webgl/p5.RenderBuffer.js @@ -63,7 +63,6 @@ p5.RenderBuffer.prototype._prepareBuffer = function(geometry, shader) { if (loc === -1 || !this._renderer.registerEnabled[loc]) { return; } // Disable register corresponding to unused attribute gl.disableVertexAttribArray(loc); - attr.enabled = false; // Record register availability this._renderer.registerEnabled[loc] = false; } From c1a9c692b36e97a106f906b76a00cb41b360a247 Mon Sep 17 00:00:00 2001 From: INARI_DARKFOX <39549290+inaridarkfox4231@users.noreply.github.com> Date: Thu, 26 Jan 2023 02:58:24 +0900 Subject: [PATCH 09/10] Register enable/disable flag test I decided to use a global flag to manage the enabled/disabled state of the registers used to store shader attributes. This value will be true if the geometry supplies a value, false if it does not. Therefore, I prepared a geometry with aTexCoord and a geometry without it, and tested whether the value was properly switched. --- test/unit/webgl/p5.RendererGL.js | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/unit/webgl/p5.RendererGL.js b/test/unit/webgl/p5.RendererGL.js index bd6b915a2e..9fbaf7af16 100644 --- a/test/unit/webgl/p5.RendererGL.js +++ b/test/unit/webgl/p5.RendererGL.js @@ -1392,6 +1392,44 @@ suite('p5.RendererGL', function() { }); }); + suite('Test for register availability', function() { + test('register enable/disable flag test', function(done) { + const renderer = myp5.createCanvas(16, 16, myp5.WEBGL); + + // geometry without aTexCoord. + const myGeom = new p5.Geometry(1, 1, function() { + this.gid = 'registerEnabledTest'; + this.vertices.push(createVector(-8, -8)); + this.vertices.push(createVector(8, -8)); + this.vertices.push(createVector(8, 8)); + this.vertices.push(createVector(-8, 8)); + this.faces.push([0, 1, 2]); + this.faces.push([0, 2, 3]); + this.computeNormals(); + }); + + myp5.fill(255); + myp5.directionalLight(255, 255, 255, 0, 0, -1); + + myp5.triangle(-8, -8, 8, -8, 8, 8); + + // get register location of + // lightingShader's aTexCoord attribute. + const attributes = renderer._curShader.attributes; + const loc = attributes.aTexCoord.location; + + assert.equal(renderer.registerEnabled[loc], true); + + myp5.model(myGeom); + assert.equal(renderer.registerEnabled[loc], false); + + myp5.triangle(-8, -8, 8, 8, -8, 8); + assert.equal(renderer.registerEnabled[loc], true); + + done(); + }); + }); + suite('setAttributes', function() { test('It leaves a reference to the correct canvas', function(done) { const renderer = myp5.createCanvas(10, 10, myp5.WEBGL); From dfccc747d54e1bc6feb7902e59fa3d369d190325 Mon Sep 17 00:00:00 2001 From: INARI_DARKFOX <39549290+inaridarkfox4231@users.noreply.github.com> Date: Thu, 26 Jan 2023 03:04:59 +0900 Subject: [PATCH 10/10] not "createVector" but "myp5.createVector". I accidentally wrote "myp5.createVector" as "createVector". Just a typo. Excuse me. --- test/unit/webgl/p5.RendererGL.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/unit/webgl/p5.RendererGL.js b/test/unit/webgl/p5.RendererGL.js index 9fbaf7af16..99bd247a65 100644 --- a/test/unit/webgl/p5.RendererGL.js +++ b/test/unit/webgl/p5.RendererGL.js @@ -1399,10 +1399,10 @@ suite('p5.RendererGL', function() { // geometry without aTexCoord. const myGeom = new p5.Geometry(1, 1, function() { this.gid = 'registerEnabledTest'; - this.vertices.push(createVector(-8, -8)); - this.vertices.push(createVector(8, -8)); - this.vertices.push(createVector(8, 8)); - this.vertices.push(createVector(-8, 8)); + this.vertices.push(myp5.createVector(-8, -8)); + this.vertices.push(myp5.createVector(8, -8)); + this.vertices.push(myp5.createVector(8, 8)); + this.vertices.push(myp5.createVector(-8, 8)); this.faces.push([0, 1, 2]); this.faces.push([0, 2, 3]); this.computeNormals();