PDA

View Full Version : OpenGL 2.1 VertexAttribDivisorARB problem



kingc8
03-25-2015, 05:11 AM
Hello,

I'm using VertexAttribDivisorARB for quad based sprite system.

I'm using glVertexAttribDivisorARB (VertAttribArray, 6) to have 1 generic vertex attribute distributed to 6 vertex shaders at-a-time.

My setup is using glDrawArrays twice, once for the "background", then it's Z-cleared and then a DrawArrays for the "foreground".

-Of course, I'm using glDrawArraysInstanced to achieve the instanced drawing.

However, I find that the second glDrawArraysInstanced seems to using the vertex attribute array from the start.

That is to say, I have an effective attrib array for 5000 unique quads, 2500 at the back and 2500 at the front.

But the glDrawArraysInstanced for the second drawing is somehow restarting the attrib array index. Why?

This is for OpenGL 2.1 only.

Thanks,

Cameron
:sorrow:

GClements
03-25-2015, 05:44 AM
Of course, I'm using glDrawArraysInstanced to achieve the instanced drawing.
glDrawArraysInstanced requires OpenGL 3.1. If you're using the ARB_draw_instanced extension, you should be using glDrawArraysInstancedARB.



However, I find that the second glDrawArraysInstanced seems to using the vertex attribute array from the start.

For which attributes? The per-instance attributes (non-zero divisor) or per-vertex attributes (zero divisor)?

The first parameter to glDrawArraysInstanced specifies the offset for per-vertex (non-instanced, zero divisor) attributes. Per-instance attributes are always read starting from zero.

If you want to apply an offset for instanced attributes, you need to either use glDrawArraysInstancedBaseInstance() (which requires OpenGL 4.2 or later), or use glVertexAttribPointer() to offset the start of the attribute array(s) for instanced attributes.

Dark Photon
03-25-2015, 06:01 AM
However, I find that the second glDrawArraysInstanced seems to using the vertex attribute array from the start. That is to say, I have an effective attrib array for 5000 unique quads, 2500 at the back and 2500 at the front. But the glDrawArraysInstanced for the second drawing is somehow restarting the attrib array index. Why?

I suspect you want to read up on ARB_base_instance (https://www.opengl.org/registry/specs/ARB/base_instance.txt) and look at the glDrawArraysInstancedBaseInstance API. Alternatively, respecify your vertex attribute pointer(s) between draw calls.

kingc8
03-30-2015, 12:22 PM
Perfect, yes, the vertexpointer offset fixes it just right. Thanks very much.

I'm going to abandon this approach as i need to have the quads load the unique properties from the Vbo. however instancing as far as i understand only allows me to use the same texture coords for each instanced draw call.

i really want the attribute divisor by itself to reduce memory bandwidth, but the divisor and the instancing come as a package.

i suppose i could bundle tex coords as a vertex attribute to get around the vbo instancing cloning tex coords.. but i would need to bundle quad dimensions to get the proper ratio.. yuck.

oh well, thanks, again!

Cameron

GClements
03-30-2015, 12:36 PM
i suppose i could bundle tex coords as a vertex attribute to get around the vbo instancing cloning tex coords.. but i would need to bundle quad dimensions to get the proper ratio.
Even so, supplying two <u,v> pairs per quad (origin and size) requires less memory than supplying four.

If you need unique attributes, you can use a texture or uniform array which is indexed based upon both gl_VertexID and gl_InstanceID. But texture/array lookups don't pipeline as well as attributes.