2D openGL engine - flickering & performance

Started by
5 comments, last by StratBoy61 13 years, 7 months ago
Hi there,

I am writing a 2D engine, and I thought that I had got the right idea to use nothing but quads to draw sprites on.
The thing is that I am experiencing two behaviors that got me decided to post this message on gamedev :

- First, I can see some flickering, generally located on the last sprite drawn. I must say that I am using the OpenGL ES 2.0 reduced set, so all the drawings are made using glDrawElements() on a single quad. I am animating the sprite by changing the texture's coordinate in the vertex shader (I am using an atlas to store all the sprites' poses and I simply loop through them). Oddly enough, when I animate the sprites, I can see some of them flickering --usually the last drawn but not always, whereas everything is drawn smoothly when I do not animate. The only difference between *animating* and not animating is dynamically changing the texture's coordinate in the vertex shader...

- Second, I was surprised to see that my frame rate goes down to 30FPS when I display 3,000 sprites. I assume that it is due to the number of call to glDrawElements(), because I can draw way more than 3,000 x 2 triangles in my 3D engine before seeing the FPS go down. But does it make sense that only 3,000 calls to glDrawElements() degrade so much the performances ?

Could someone please tell me whether what I am doing is the right thing to do or not ? Does someone know where the flickering could be coming from ?
I just noticed that if I turn GL_DEPTH_TEST on, the flickering tends to disappear !?!!
Thank you in advance !
Cheers,
stratboy61
Advertisement
3000 calls to glDrawElements is a LOT. It should be about 1/10 of this, maximum, so what you need to do is start grouping quads with common state and drawing multiple quads in one call. There are plenty of other posts about this, and information is very widespread on the internet, so do some searching and you'll find what you need.

If you're using GL_QUADS as your mode and GL_UNSIGNED_INT as your type you're not doing yourself any favours either. Switch to GL_TRIANGLES because it's native to the GPU so there's less possibility of it going through any software emulation steps first, and switch to GL_UNSIGNED_SHORT because it's more widely supported in hardware.

Should this be moved to the OpenGL forums?

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Quote:Original post by mhagain
If you're using GL_QUADS as your mode and GL_UNSIGNED_INT as your type you're not doing yourself any favours either. Switch to GL_TRIANGLES because it's native to the GPU so there's less possibility of it going through any software emulation steps first, and switch to GL_UNSIGNED_SHORT because it's more widely supported in hardware.

Thank you mhagain for your answer.

I am already using GL_TRIANGLE_STRIP with GL_UNSIGNED_SHORT ; I used the word "quad" to somehow emphasize the technique I used to display sprites.

Okay, I will try to reduce the calls to glDrawElements ; I am glad to have a feedback about what is considered too much ;-)

Any hint about the flickering ? I added glFlush and glFinish, but it does not change anything.
Regards,
stratboy61
How large are the sprites on the screen?
The slowdown could also be caused by fillrate, which you could test for example by making each sprite just 1x1 pixels and see if you get better performance.
The sprites are 96x96 pixels.
I do not have any performance issue so far (I am not displaying as many sprites as 3,000), but I am troubled with this flickering effect. It really looks like a vertical sync problem, as it is occasional. This is why it does not really make sense... Also, I do not understand why the flickering occurs only when the sprite is animated ; if I keep the same code, but rather than looping through the animations, I just keep on displaying say, the first pose, the flickering is no longer visible.
Thanks for the hint though, I am going to test with sprites of other sizes.
Regards,
stratboy61
Have you tried stepping through the animation very slowly to see exactly what the flickering is? Possibly you're generating invalid texture coordinates for one of the frames of animation, so that nothing is drawn, causing the sprite to "flicker" off and on very briefly.
Hello raigan,

Thank you for the tip but it is the first thing that I checked :-)
I am going to try using alpha blending, because so far I have used the discard method in the fragment shader to manage transparency. I will also try to display more sprites at once, by using a VBO containing several sprites and update their textures coordinates and positions. I do not know if it is the best thing to do for each frame though, but I will try it out.
Regards,
stratboy61

This topic is closed to new replies.

Advertisement