tangrams / tangram

WebGL map rendering engine for creative cartography
https://tangram.city
MIT License
2.22k stars 290 forks source link

Ensure static GL attributes are enabled outside VAOs #689

Closed bcamper closed 5 years ago

bcamper commented 5 years ago

Several features make use of static GL vertex attributes, e.g. set with vertexAttrib4fv() etc. methods, unlike dynamic per-vertex "array" attributes set with enableVertexAttribArray() and vertexAttribPointer() methods).

These fixed vertex attribute calls were being made along with the rest of the Vertex Array Object initialization. However, it turns out that this state is NOT part of the VAO state. From the OpenGL VAO docs:

A vertex shader can read an attribute that is not currently enabled (via glEnableVertexAttribArray). The value that it gets is defined by special context state, which is not part of the VAO.

This discrepancy likely hasn't been frequently observed because we have only used static attribute values of zero -- perhaps while the spec indicates the values are undefined, most implementations set them to zero.

However, with the upcoming addition of a static attribute with a non-zero value (separate branch), this bug (with the attribute flipping between different values, unexpectedly) was observed in Chrome (though not in Firefox or Safari; perhaps they store the fixed attribute value as part of the VAO even if it's not to spec?).

And, to confirm the validity of the fix, it does appear to resolve an intermittent issue we had seen with line offsets (which make use of static attributes) in Edge on Win10.

With this change, any static attributes are re-set whenever a given VertexLayout is enabled for rendering, after any corresponding VAO is bound.