Skip to content

Commit

Permalink
Merge pull request #5970 from inaridarkfox4231/Disable-unused-registers
Browse files Browse the repository at this point in the history
Disable unused registers to avoid environment-dependent vanishing bugs
  • Loading branch information
davepagurek committed Jan 26, 2023
2 parents 6420c4e + dfccc74 commit f970dd3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/webgl/p5.RenderBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/webgl/p5.Shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions test/unit/webgl/p5.RendererGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f970dd3

Please sign in to comment.