Jump to content

  • Log In with Google      Sign In   
  • Create Account

glDrawElements() terrible lag, glDrawArrays fine?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
18 replies to this topic

#1 Conoktra   Members   -  Reputation: 140

Like
0Likes
Like

Posted 25 June 2011 - 10:36 PM

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.

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 30385

Like
0Likes
Like

Posted 25 June 2011 - 10:55 PM

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... Posted Image

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.

#3 Conoktra   Members   -  Reputation: 140

Like
0Likes
Like

Posted 26 June 2011 - 12:29 AM

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[i] = Verts[Quads[i]];
}

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &mesh[0]);

glDrawArrays(GL_QUADS, 0, mesh.size());

glDisableClientState(GL_VERTEX_ARRAY);


#4 Aks9   Members   -  Reputation: 861

Like
0Likes
Like

Posted 26 June 2011 - 03:43 AM

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().

#5 Conoktra   Members   -  Reputation: 140

Like
0Likes
Like

Posted 26 June 2011 - 08:23 AM

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.

#6 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 26 June 2011 - 02:21 PM

Try the code at http://www.opengl.org/wiki/Tutorial1:_Rendering_shapes_with_glDrawRangeElements,_VAO,_VBO,_shaders_%28C%2B%2B_/_freeGLUT%29

It uses glDrawRangeElements() but you can change it to glDrawElements() if you want.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#7 Conoktra   Members   -  Reputation: 140

Like
0Likes
Like

Posted 27 June 2011 - 11:50 PM

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

#8 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 28 June 2011 - 08:10 AM

In that case, you have a unique machine.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#9 Danny02   Members   -  Reputation: 275

Like
0Likes
Like

Posted 28 June 2011 - 10:22 AM

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... Posted Image


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




#10 Conoktra   Members   -  Reputation: 140

Like
0Likes
Like

Posted 28 June 2011 - 11:28 AM

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[i] = Verts[Quads[i]];
}

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &mesh[0]);

glDrawArrays(GL_QUADS, 0, mesh.size());

glDisableClientState(GL_VERTEX_ARRAY);


#11 Danny02   Members   -  Reputation: 275

Like
0Likes
Like

Posted 28 June 2011 - 12:57 PM

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

#12 Eric Lengyel   Crossbones+   -  Reputation: 2325

Like
-1Likes
Like

Posted 28 June 2011 - 04:37 PM

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.

#13 Conoktra   Members   -  Reputation: 140

Like
0Likes
Like

Posted 28 June 2011 - 07:27 PM

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 :).

#14 mhagain   Crossbones+   -  Reputation: 7978

Like
-1Likes
Like

Posted 29 June 2011 - 03:21 AM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#15 V-man   Members   -  Reputation: 805

Like
0Likes
Like

Posted 29 June 2011 - 04:02 AM

That's why I gave him that link. It just draws 2 triangles and his performance is even worst.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#16 Conoktra   Members   -  Reputation: 140

Like
0Likes
Like

Posted 30 June 2011 - 12:09 AM

Ive been thinking perhaps it is a problem with GLEW or the actual radeon driver :(.

#17 Danny02   Members   -  Reputation: 275

Like
0Likes
Like

Posted 30 June 2011 - 02:58 AM

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.

#18 Eric Lengyel   Crossbones+   -  Reputation: 2325

Like
-1Likes
Like

Posted 02 July 2011 - 12:16 AM


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.


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.

#19 Conoktra   Members   -  Reputation: 140

Like
0Likes
Like

Posted 04 July 2011 - 01:40 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS