diff --git a/src/webgl/p5.RenderBuffer.js b/src/webgl/p5.RenderBuffer.js index a586fb0323..84304fdd8c 100644 --- a/src/webgl/p5.RenderBuffer.js +++ b/src/webgl/p5.RenderBuffer.js @@ -58,6 +58,13 @@ 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); + // Record register availability + this._renderer.registerEnabled[loc] = false; } }; diff --git a/src/webgl/p5.RendererGL.js b/src/webgl/p5.RendererGL.js index f105c2c1b2..98a5dfa848 100644 --- a/src/webgl/p5.RendererGL.js +++ b/src/webgl/p5.RendererGL.js @@ -136,6 +136,8 @@ p5.RendererGL = function(elt, pInst, isMainCanvas, attr) { this._useLineColor = false; this._useVertexColor = false; + this.registerEnabled = []; + this._tint = [255, 255, 255, 255]; // lightFalloff variables diff --git a/src/webgl/p5.Shader.js b/src/webgl/p5.Shader.js index f67696a506..0439a0e34e 100644 --- a/src/webgl/p5.Shader.js +++ b/src/webgl/p5.Shader.js @@ -563,9 +563,11 @@ p5.Shader.prototype.enableAttrib = function( const loc = attr.location; if (loc !== -1) { const gl = this._renderer.GL; - if (!attr.enabled) { + // Enable register even if it is disabled + if (!this._renderer.registerEnabled[loc]) { gl.enableVertexAttribArray(loc); - attr.enabled = true; + // Record register availability + this._renderer.registerEnabled[loc] = true; } this._renderer.GL.vertexAttribPointer( loc, diff --git a/test/unit/webgl/p5.RendererGL.js b/test/unit/webgl/p5.RendererGL.js index fbcade3c31..8c2ebc29f8 100644 --- a/test/unit/webgl/p5.RendererGL.js +++ b/test/unit/webgl/p5.RendererGL.js @@ -1399,6 +1399,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(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(); + }); + + 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);