Is it important to call glDisableVertexAttribArray ()?

I do not quite understand the scope of vertex attribute arrays. I have several different shader programs with different number of vertex attributes. Are glEnableVertexAttribArray calls local to the shader program or global?

I currently enable vertex attribute arrays when creating a shader program and never disable them, and everything seems to work, but it looks like I might need to enable / disable them right before / after draw calls, is there any effect on this ?

(I'm in WebGL since this happens, so we are really talking about gl.enableVertexAttribArray and gl.disableVertexAttribArray . I also want to note that the orange OpenGL Shading Language book is pretty uninformative about these calls.)

+42
opengl-es webgl vertex-shader
Sep 14 '12 at 15:47
source share
3 answers

The state in which Vertex attribute arrays are allowed can be bound to a vertex array object (VAO) or global.

If you use VAO, you should not disable attribute arrays as they are encapsulated in VAO.

However, for a globally permitted array of attribute attributes, you must disable them, because if they are left on, OpenGL will try to read from arrays that may be associated with an invalid pointer, which could lead to your program crashing if you point to the client's address space or raise an error OpenGL if it indicates the limits of a valid Vertex buffer object.

+31
Sep 14 '12 at 15:56
source share

WebGL is not the same as OpenGL.

In an enabled WebGL array, it is allowed explicitly allowed as long as there is a buffer attached to the attribute, and (a) if it was used large enough to satisfy the drawing call, or (b) it was not used.

Unlike OpenGL ES 2.0, WebGL does not allow arrays on the client side.

Evidence:

 var gl = document.querySelector("canvas").getContext("webgl"); var m4 = twgl.m4; var programInfo2Attribs = twgl.createProgramInfo(gl, ["vs-uses-2-attributes", "fs"]); var programInfo1Attrib = twgl.createProgramInfo(gl, ["vs-uses-1-attribute", "fs"]); var arrays2Attribs = { position: [ -1, -1, 0, 1, -1, 0, -1, 1, 0, ], color: [ 1,0,0,1, 1,1,0,1, 0,1,0,1, ], }; var arrays1Attrib = { position: [ -1, -1, 0, 1, -1, 0, -1, 1, 0, -1, 1, 0, 1, -1, 0, 1, 1, 0, ], }; var bufferInfo2Attribs = twgl.createBufferInfoFromArrays(gl, arrays2Attribs); var bufferInfo1Attrib = twgl.createBufferInfoFromArrays(gl, arrays1Attrib); var uniforms = { u_matrix: m4.scale(m4.translation([-0.5, 0, 0]), [0.25, 0.5, 0.5]), }; gl.useProgram(programInfo2Attribs.program); twgl.setBuffersAndAttributes(gl, programInfo2Attribs, bufferInfo2Attribs); twgl.setUniforms(programInfo2Attribs, uniforms); twgl.drawBufferInfo(gl, gl.TRIANGLES, bufferInfo2Attribs); uniforms.u_matrix = m4.scale(m4.translation([0.5, 0, 0]), [0.25, 0.5, 0.5]); gl.useProgram(programInfo1Attrib.program); twgl.setBuffersAndAttributes(gl, programInfo1Attrib, bufferInfo1Attrib); twgl.setUniforms(programInfo1Attrib, uniforms); twgl.drawBufferInfo(gl, gl.TRIANGLES, bufferInfo1Attrib); 
 canvas { border: 1px solid black; } 
 <script src="https://twgljs.org/dist/twgl-full.min.js"></script> <script id="vs-uses-2-attributes" type="not-js"> attribute vec4 position; attribute vec4 color; varying vec4 v_color; uniform mat4 u_matrix; void main() { gl_Position = u_matrix * position; v_color = color; } </script> <script id="vs-uses-1-attribute" type="not-js"> attribute vec4 position; varying vec4 v_color; uniform mat4 u_matrix; void main() { gl_Position = u_matrix * position; v_color = vec4(0,1,0,1); } </script> <script id="fs" type="not-js"> precision mediump float; varying vec4 v_color; void main() { gl_FragColor = v_color; } </script> <p> 1st it draws a triangle (3 vertices, 2 attributes)<br/> 2nd it draws a quad (6 vertices, 1 attribute)<br/> It does NOT called gl.disableVertexAttrib so on the second draw call one of the attributes is still enabled. It is pointing to a buffer with only 3 vertices in it even though 6 vertices will be drawn. There are no errors. </p> <canvas></canvas> 
+12
Sep 15 '12 at 9:22
source share

For webGL, I am going to use yes , it is important to call gl.disableVertexAttribArray.

Chrome gave me this warning:

 WebGL: INVALID_OPERATION: drawElements: attribs not setup correctly 

This happened when the program changed to one, using less than the maximum number of attributes. Obviously, the solution was to turn off unused attributes before drawing.

If all your programs use the same number of attributes, you can leave with a call to gl.enableVertexAttribArray once during initialization. Otherwise, you will have to manage them when changing programs.

+4
Nov 13 '13 at 6:55
source share



All Articles