• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Conoktra

glDrawElements() terrible lag, glDrawArrays fine?

18 posts in this topic

Hello everyone :).

I am rendering a heightmapped terrain composed of 16384 quads for the map editor of the game Galactic Vice ([url="http://img10.imageshack.us/img10/2011/gvrainforest.jpg"]screenshot[/url]). 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.
0

Share this post


Link to post
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... [img]http://public.gamedev.net/public/style_emoticons/default/sad.gif[/img]

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.
0

Share this post


Link to post
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:
[code]
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);
[/code]

Here is different code that does work using glDrawArrays():
[code]
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);
[/code]
0

Share this post


Link to post
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().
0

Share this post


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

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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


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

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1309064130' post='4827782']
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... [img]http://public.gamedev.net/public/style_emoticons/default/sad.gif[/img]
[/quote]

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


0

Share this post


Link to post
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 [i]is[/i] 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.

[code]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);[/code]
0

Share this post


Link to post
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
0

Share this post


Link to post
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.
0

Share this post


Link to post
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 :).
0

Share this post


Link to post
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.
[/quote]

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 [i]that[/i] much. A drop to 1 FPS is certainly [i]not[/i] 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 [i]will[/i] 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.
-1

Share this post


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

Share this post


Link to post
Share on other sites
Ive been thinking perhaps it is a problem with GLEW or the actual radeon driver :(.
0

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites
[quote name='mhagain' timestamp='1309339260' post='4829006']
[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.
[/quote]

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 [i]that[/i] much. A drop to 1 FPS is certainly [i]not[/i] 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 [i]will[/i] 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.
-1

Share this post


Link to post
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0