@@ -10,6 +10,7 @@ var GLError = require("./lib/GLError")
10
10
//Shader object
11
11
function Shader ( gl ) {
12
12
this . gl = gl
13
+ this . gl . lastAttribCount = 0 // fixme where else should we store info, safe but not nice on the gl object
13
14
14
15
//Default initialize these to null
15
16
this . _vref =
@@ -29,10 +30,38 @@ proto.bind = function() {
29
30
if ( ! this . program ) {
30
31
this . _relink ( )
31
32
}
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
+
32
51
this . gl . useProgram ( this . program )
33
52
}
34
53
35
54
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
+
36
65
if ( this . _fref ) {
37
66
this . _fref . dispose ( )
38
67
}
@@ -119,7 +148,8 @@ proto.update = function(
119
148
var attributeUnpacked = [ ]
120
149
var attributeNames = [ ]
121
150
var attributeLocations = [ ]
122
- for ( var i = 0 ; i < attributes . length ; ++ i ) {
151
+ var i
152
+ for ( i = 0 ; i < attributes . length ; ++ i ) {
123
153
var attr = attributes [ i ]
124
154
if ( attr . type . indexOf ( 'mat' ) >= 0 ) {
125
155
var size = attr . type . charAt ( attr . type . length - 1 ) | 0
@@ -159,7 +189,7 @@ proto.update = function(
159
189
160
190
//For all unspecified attributes, assign them lexicographically min attribute
161
191
var curLocation = 0
162
- for ( var i = 0 ; i < attributeLocations . length ; ++ i ) {
192
+ for ( i = 0 ; i < attributeLocations . length ; ++ i ) {
163
193
if ( attributeLocations [ i ] < 0 ) {
164
194
while ( attributeLocations . indexOf ( curLocation ) >= 0 ) {
165
195
curLocation += 1
0 commit comments