# glDrawElements() terrible lag, glDrawArrays fine?

This topic is 2393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone .

I am rendering a heightmapped terrain composed of 16384 quads for the map editor of the game Galactic Vice (screenshot). When I manually compile the quads and render using glDrawArrays() I get upwards of 200 FPS on a Radeon HD 5650. But, when the equivalent mesh is indexed and rendered using glDrawElements() it drops to less-than a frame a second.

A Radeon 5650 should be able to hand so few quads with no problem... and it does so long as I don't use glDrawElements(). However, for the implementation of my map editor using indexed vertices makes life a lot easier (updating the terrain mesh is much easier).

Has anywhere ever encountered an issue like this before? Is there any way to use glDrawArrays() without causing such horrible lag? Perhaps there is a better performing way to render meshes that use indexed elements?

Thanks beforehand.

##### Share on other sites
Whenever your OpenGL performance suddenly drops to less than a frame per second, it's usually because the driver/hardware doesn't support something that you're doing, and has silently switched over to rendering via the CPU instead of the GPU...

Do you mean that you're using the GL_QUADS primitive type? If so, you should really be using GL_TRIANGLE_STRIP or GL_TRIANGLES instead.

What kind of index buffer are you using? What's the format (type) of the indices? If using int's, try using short's instead.

##### Share on other sites
I have tried rendering with both GL_QUADS and GL_TRIANGLES for the primitive type, and GL_UNSIGNED_INT and GL_UNSIGNED_SHORT for the indices--all with the same result. Its not in a VBO either. Enabling/disabling texcoord arrays makes no difference (the only other thing being used).

The lagging render code goes as follows:
 vector<vec3> Verts; // stores the base-mesh vector<u32> Quads; // stores in indices glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, &Verts[0]); glDrawElements(GL_QUADS, Quads.size(), GL_UNSIGNED_INT, &Quads[0]); //glDrawElements(GL_QUADS, Quads.size(), GL_UNSIGNED_SHORT, &Quads[0]); //glDrawElements(GL_TRIANGLES, Tris.size(), GL_UNSIGNED_INT, &Tris[0]); //glDrawElements(GL_TRIANGLES, Tris.size(), GL_UNSIGNED_SHORT, &Tris[0]); glDisableClientState(GL_VERTEX_ARRAY); 

Here is different code that does work using glDrawArrays():
 vector<vec3> Verts; // stores the base-mesh vector<u32> Quads; // stores in indices vector<vector3df> mesh; // will store the re-ordered vertices mesh.resize(Quads.size()); u32 i = 0; for(; i < Quads.size(); ++i) { mesh = Verts[Quads]; } glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, &mesh[0]); glDrawArrays(GL_QUADS, 0, mesh.size()); glDisableClientState(GL_VERTEX_ARRAY); 

##### Share on other sites
I suspect index buffer for some issues (running out of the buffer, making irregular triangles or so). Make sure that OpenGL doesn't retrieve any error. From my experience, glDrawElements() can be even significantly faster that glDrawArrays().

##### Share on other sites
Just added this chunk before and after rendering code, and confirms OpenGL is not generating any errors:
 GLenum error = GL_NO_ERROR; while((error = glGetError()) != GL_NO_ERROR) { logPrintf("OpenGL Error: %s\n", gluErrorString(error)); } 

I also know it is not reading past the range of the buffers (plugged it into valgrind, a memory debugger). This is so baffling--it should not be doing this!

Also, when rendering the same exact buffer with GL_POINTS it is fast, or rendering with "glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)" is also fast. Its only when the polygon gets filled that it is horribly slow.

##### Share on other sites

It uses glDrawRangeElements() but you can change it to glDrawElements() if you want.

##### Share on other sites
glDrawRangeElements() renders even worse results. However, I think I have found a solution. Ill let everyone know how it goes.

##### Share on other sites
In that case, you have a unique machine.

##### Share on other sites

Whenever your OpenGL performance suddenly drops to less than a frame per second, it's usually because the driver/hardware doesn't support something that you're doing, and has silently switched over to rendering via the CPU instead of the GPU...

Or the code is doing something horrible bad^^, which is a lot more common in my experience.

##### Share on other sites
Ok, no luck--a quadtree accelerator just slowed things down further because it made multiple calls to glDrawElements (and/or glDrawRangeElements). Profiling the code confirms that the lag is caused by glDrawElements. Oddly enough it is faster to re-order the vertices myself (using the same indices) and render with glDrawArrays than it is to use glDrawElements.

vector<vec3> Verts; // stores the base-mesh vector<u32> Quads; // stores the indices for the quads vector<vector3df> mesh; // will store the re-ordered vertices mesh.resize(Quads.size()); u32 i = 0; for(; i < Quads.size(); ++i) { mesh = Verts[Quads]; } glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, &mesh[0]); glDrawArrays(GL_QUADS, 0, mesh.size()); glDisableClientState(GL_VERTEX_ARRAY);

##### Share on other sites
in your case using a index array should be slower then just using the vertex array, cause the little speedup by the indices is very small to the performance overhead produced by the additional data upload.

Did u try using Buffer Objects ?

the performance shouldn't drop to 1fps of course, but this could perhaps fix some hidden bugs

##### Share on other sites
The performance problem is due to the fact that you are not using vertex buffer objects (VBOs). Without VBOs, the driver has to copy all of your vertex data into the command buffer when you call glDrawArrays() or glDrawElements() before those functions can return. In today's multithreaded drivers, that can get very expensive, especially because putting all that data in the command buffer can cause a lot of flushes to happen, which cause the driver threads to have to synchronize with each other. The performance is worse with glDrawElements() because the driver is actually de-indexing your vertex data.

For any kind of reasonable performance, you absolutely must put the vertex data and index data in VBOs. Then, the glDrawArrays() and glDrawElements() functions can return immediately, no data has to be copied by the driver, and no thread synchronization has to occur in the driver.

##### Share on other sites
Interesting, and it definitely makes sense. I was hoping to avoid VBO's for the fact that as the terrain mesh is modified I have to constantly re-upload the data anyway (at least this way I don't have to constantly re-upload index data =D). I will give it a try and report back.

Thanks for everyone's help thus far .

##### Share on other sites

The performance problem is due to the fact that you are not using vertex buffer objects (VBOs). Without VBOs, the driver has to copy all of your vertex data into the command buffer when you call glDrawArrays() or glDrawElements() before those functions can return. In today's multithreaded drivers, that can get very expensive, especially because putting all that data in the command buffer can cause a lot of flushes to happen, which cause the driver threads to have to synchronize with each other. The performance is worse with glDrawElements() because the driver is actually de-indexing your vertex data.

For any kind of reasonable performance, you absolutely must put the vertex data and index data in VBOs. Then, the glDrawArrays() and glDrawElements() functions can return immediately, no data has to be copied by the driver, and no thread synchronization has to occur in the driver.

I don't think that's quite true. There are plenty of practical real-world examples that don't use VBOs but which can still get reasonable performance despite that (even on today's multithreaded hardware), and - in fact - if you're using a dynamic VBO that you must refill every time you draw from it, then client-side arrays are most likely going to be the fast path anyway. Plus, while glDrawElements certainly has some additional overhead, it's nowhere near that much. A drop to 1 FPS is certainly not symptomatic of not using VBOs. (Interesting to note that if you do use VBOs but don't have a good understanding of how the hardware and CPU/GPU synchronisation works then you will lose performance with them when compared to client-side arrays, typically from multiple GPU-stalls per frame.)

There's something else wrong here. I wonder what would happen if the OP split his drawing into two batches of 8k quads each.

##### Share on other sites
That's why I gave him that link. It just draws 2 triangles and his performance is even worst.

##### Share on other sites
Ive been thinking perhaps it is a problem with GLEW or the actual radeon driver .

##### Share on other sites
I had sometimes the same thoughts, but only in very rare cases it is a problem of the driver.

Just test your program on different machines(send it to friends for example) and see if they have the same problem.

##### Share on other sites

[quote name='Eric Lengyel' timestamp='1309300638' post='4828857']
The performance problem is due to the fact that you are not using vertex buffer objects (VBOs). Without VBOs, the driver has to copy all of your vertex data into the command buffer when you call glDrawArrays() or glDrawElements() before those functions can return. In today's multithreaded drivers, that can get very expensive, especially because putting all that data in the command buffer can cause a lot of flushes to happen, which cause the driver threads to have to synchronize with each other. The performance is worse with glDrawElements() because the driver is actually de-indexing your vertex data.

For any kind of reasonable performance, you absolutely must put the vertex data and index data in VBOs. Then, the glDrawArrays() and glDrawElements() functions can return immediately, no data has to be copied by the driver, and no thread synchronization has to occur in the driver.

I don't think that's quite true. There are plenty of practical real-world examples that don't use VBOs but which can still get reasonable performance despite that (even on today's multithreaded hardware), and - in fact - if you're using a dynamic VBO that you must refill every time you draw from it, then client-side arrays are most likely going to be the fast path anyway. Plus, while glDrawElements certainly has some additional overhead, it's nowhere near that much. A drop to 1 FPS is certainly not symptomatic of not using VBOs. (Interesting to note that if you do use VBOs but don't have a good understanding of how the hardware and CPU/GPU synchronisation works then you will lose performance with them when compared to client-side arrays, typically from multiple GPU-stalls per frame.)

There's something else wrong here. I wonder what would happen if the OP split his drawing into two batches of 8k quads each.
[/quote]

I didn't see the mention of 1 fps in the first post before. Yeah, there is something else going on. However, everything I said is valid. I don't mean that not using VBOs will perform terribly, but you should always be able to beat client-side arrays by using VBOs, even for dynamic vertex data that changes every frame. Client-side arrays are never faster unless you're talking about a very small number of vertices. In either case (dynamic VBO or client-side arrays), the driver has to copy the vertex data to an internal buffer, and the hardware has to read that data through a DMA. The difference is that by using a VBO, you don't risk filling up the command buffer and causing a flush that could stall the CPU, and you don't pay for the CPU copy if the vertex data doesn't change. And don't forget that the driver has to de-index the vertex data if you use client-side arrays with glDrawElements(). There's no way that's ever going to be faster than using VBOs.

##### Share on other sites
Alright, so I finally got home and tested the same code on a GeForce GTX 460, and it gets about 500 FPS--which is even more baffling. I am leaning more and more towards a driver bug.