PDA

View Full Version : Does OpenGL/GLES has a default vertex attribute format?



samwyi
09-13-2017, 05:52 PM
I tried to find an answer from GL spec for the following question, but didn't. So, help needed.

Does OpenGL/GLES has a default vertex attribute format? If the app binds a vertex buffer, but not call glVertexAttribFormat or glVertexAttribPointer to set the format, how should the driver interpret the vertex data in the buffer? Or what type of GL error should the app get? I'm talking about GLES 3.0 and above.

Thanks.

mhagain
09-14-2017, 03:03 AM
Every piece of state in OpenGL has a default and all of these defaults are documented in the specification.

you have specifically mentioned ES 3.0 and above, so I'll cite from the ES 3.0.5 specification at https://www.khronos.org/registry/OpenGL/specs/es/3.0/es_spec_3.0.pdf

Section 2.8, Vertex Specification, states:
The initial values for all generic vertex attributes are (0.0, 0.0, 0.0, 1.0).
So unless you have otherwise specified a value for a vertex attribute, it will have a value of (0.0, 0.0, 0.0, 1.0).

Section 2.9, Vertex Arrays, states:
In the initial state, the boolean values are each false, the memory pointers are each NULL, the strides are each zero, the array types are each FLOAT, the integers representing values per element are each four, the normalized and pure integer flags are each false, and the divisors are each zero.

Table 6.2: Vertex Array Object State, lists the initial enable/disable state for each vertex attrib array false, and we can see that this value is FALSE for VERTEX_ATTRIB_ARRAY_ENABLED. It also states that the arrays are initially configured as though no buffer object were bound.

So to summarize:

All vertex attribute arrays are disabled.
Each is initialized to the equivalent of glVertexAttribPointer (n, 4, GL_FLOAT, GL_FALSE, 0, NULL);
Buffer object 0 is "bound" for the each.


What happens if you attempt to read from such an array? Because no buffer object is associated with the array it's equivalent to reading from a client-side pointer, and because the pointer is initially NULL, you can expect similar behaviour to what happens if you attempt to dereference a NULL pointer in the general case - or at least you would, if the array weren't initially disabled. I cannot find a reference in the spec to what actually happens, so we may assume that it's undefined behaviour.

Alfonse Reinheart
09-14-2017, 09:06 AM
If the app binds a vertex buffer, but not call glVertexAttribFormat or glVertexAttribPointer to set the format, how should the driver interpret the vertex data in the buffer?

What do you mean by "binds a vertex buffer"? You mention `glVertexAttribFormat`, so you probably have an understanding that `glBindBuffer(GL_ARRAY_BUFFER, ...)` does not associate a buffer object with a VAO. That only `glBindVertexBuffer` or `glVertexAttribPointer` following a `GL_ARRAY_BUFFER` binding will actually put the buffer object in the VAO.

Well obviously if you use `glVertexAttribPointer`, then you set both the buffer and the format, so it's impossible to set the buffer without setting the format.

But if you're using `glVertexAttribFormat` and its ilk (https://www.khronos.org/opengl/wiki/Separate_Attribute_Format), then the answer is simple: nothing.

The default values for the format (as mhagain correctly stated) are that all attributes are disabled. And the only attributes that get fetched at rendering time are the enabled ones. So, since all attributes are disabled, nothing will be fetched. And therefore, it doesn't matter what buffers are attached to the VAO.

It is perfectly legal to render without attribute arrays. Granted, with the exception of `gl_VertexID` and `gl_InstanceID`, all of your VS invocations will get the same value. But that doesn't mean you cannot manufacture vertex data from just those built-in attributes.

samwyi
09-14-2017, 04:13 PM
Section 2.9, Vertex Arrays, states:


In the initial state, the boolean values are each false, the memory pointers are each NULL, the strides are each zero, the array types are each FLOAT, the integers representing values per element are each four, the normalized and pure integer flags are each false, and the divisors are each zero.





This is the information I'm looking for. Thank you, mhagain.

samwyi
09-14-2017, 04:26 PM
What do you mean by "binds a vertex buffer"?


very helpful analysis of the problem, Alfonse. Sorry, my description of the usage case is not quite clear. I meant "binds a vertex buffer to a VAO". So the situation is that the VAO has a VBO bound, and the corresponding vertex attribute is also enabled. In this case, if the app does not specify the vertex format, what is the default value? Thanks for the help. Now I know the expected default value should be:

Section 2.9, Vertex Arrays, states:
In the initial state, the boolean values are each false, the memory pointers are each NULL, the strides are each zero, the array types are each FLOAT, the integers representing values per element are each four, the normalized and pure integer flags are each false, and the divisors are each zero.