Skip to content

Commit 61e771e

Browse files
Merge pull request #16 from plotly/disableVertexAttribArray
Set the number of enabled vertex attributes upon a shader program switch
2 parents 064d91b + 9a1e1eb commit 61e771e

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

index.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var GLError = require("./lib/GLError")
1010
//Shader object
1111
function Shader(gl) {
1212
this.gl = gl
13+
this.gl.lastAttribCount = 0 // fixme where else should we store info, safe but not nice on the gl object
1314

1415
//Default initialize these to null
1516
this._vref =
@@ -29,10 +30,38 @@ proto.bind = function() {
2930
if(!this.program) {
3031
this._relink()
3132
}
33+
34+
// ensuring that we have the right number of enabled vertex attributes
35+
var i
36+
var newAttribCount = this.gl.getProgramParameter(this.program, this.gl.ACTIVE_ATTRIBUTES) // more robust approach
37+
//var newAttribCount = Object.keys(this.attributes).length // avoids the probably immaterial introspection slowdown
38+
var oldAttribCount = this.gl.lastAttribCount
39+
if(newAttribCount > oldAttribCount) {
40+
for(i = oldAttribCount; i < newAttribCount; i++) {
41+
this.gl.enableVertexAttribArray(i)
42+
}
43+
} else if(oldAttribCount > newAttribCount) {
44+
for(i = newAttribCount; i < oldAttribCount; i++) {
45+
this.gl.disableVertexAttribArray(i)
46+
}
47+
}
48+
49+
this.gl.lastAttribCount = newAttribCount
50+
3251
this.gl.useProgram(this.program)
3352
}
3453

3554
proto.dispose = function() {
55+
56+
// disabling vertex attributes so new shader starts with zero
57+
// and it's also useful if all shaders are disposed but the
58+
// gl context is reused for subsequent replotting
59+
var oldAttribCount = this.gl.lastAttribCount
60+
for (var i = 0; i < oldAttribCount; i++) {
61+
this.gl.disableVertexAttribArray(i)
62+
}
63+
this.gl.lastAttribCount = 0
64+
3665
if(this._fref) {
3766
this._fref.dispose()
3867
}
@@ -119,7 +148,8 @@ proto.update = function(
119148
var attributeUnpacked = []
120149
var attributeNames = []
121150
var attributeLocations = []
122-
for(var i=0; i<attributes.length; ++i) {
151+
var i
152+
for(i=0; i<attributes.length; ++i) {
123153
var attr = attributes[i]
124154
if(attr.type.indexOf('mat') >= 0) {
125155
var size = attr.type.charAt(attr.type.length-1)|0
@@ -159,7 +189,7 @@ proto.update = function(
159189

160190
//For all unspecified attributes, assign them lexicographically min attribute
161191
var curLocation = 0
162-
for(var i=0; i<attributeLocations.length; ++i) {
192+
for(i=0; i<attributeLocations.length; ++i) {
163193
if(attributeLocations[i] < 0) {
164194
while(attributeLocations.indexOf(curLocation) >= 0) {
165195
curLocation += 1

0 commit comments

Comments
 (0)