26
\$\begingroup\$

What tips or tricks do you have when it comes to making the OpenGL more efficient?

\$\endgroup\$

closed as too broad by bummzack, MichaelHouse Aug 4 '13 at 23:55

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

16
\$\begingroup\$

Use VBO rather than glStart()/glEnd(). This saves cycles in transferring the data to the GPU's memory.

\$\endgroup\$
  • 7
    \$\begingroup\$ More than that, glBegin and glEnd were deprecated in later revisions and were completely removed (and therefore will not work) in versions 3.x and above. If you request an OpenGL 3.x context you will be unable to use them completely. \$\endgroup\$ – Slurps Mad Rips Jul 22 '10 at 6:59
  • \$\begingroup\$ DirectX actually forces this upon you, it doesn't have an equivalent of glStart()/glEnd(). \$\endgroup\$ – knight666 Jul 22 '10 at 7:18
  • \$\begingroup\$ @SAHChandler I didn't know that. I didn't realized it was deprecated. \$\endgroup\$ – MrValdez Jul 22 '10 at 7:43
  • 3
    \$\begingroup\$ @SAHChandler: That sucks! I learned all my drawing with glBegin() and glEnd(). Noooo!!! \$\endgroup\$ – thyrgle Jul 23 '10 at 1:21
8
\$\begingroup\$

The standard tip of profile, profile, profile applies just as well here, although the tools are slightly different.

gDEBugger is great at helping you see exactly what's going on under the hood. It's expensive, though.

\$\endgroup\$
  • 2
    \$\begingroup\$ Just wanted to add that it is now free. \$\endgroup\$ – DMan Dec 19 '10 at 18:06
8
\$\begingroup\$

These are from Apple's documentation references on OpenGL but every little bit helps right? ;)

OpenGL Performance Optimization : The Basics
Optimizing OpenGL Data Throughput on OS X

(This last one is a sample application)
Vertex Optimization

There are also a very large number of videos available on the subject (Also from Apple) that talk about OpenGL extensions. Some of them are OS X specific, but a few others are more generalized and could most likely be applied to Windows and Linux.

You can find the videos here. You do require iTunes and an Apple account to view them however (but they are free! :D)

\$\endgroup\$
7
\$\begingroup\$

Well, it really, really depends on what you're planning to do, but here are the basics:

  • Your CPU is very precious while your GPU is a hungry beast. Offset as much as you can to the GPU. Nowadays you can do this with excellent libraries like OpenCL, CUDA and Thrust.

  • Do your lighting in a shader. Not only will this help retain your sanity, but it allows you to very cool things like toon shaders and deferred lighting.

  • Particles suck. Even when using OpenGL point sprites.

\$\endgroup\$
6
\$\begingroup\$

Minimize your state changes. On a lot of platforms, each state change potentially flushes the pipeline. It's much better to batch similar items together and change state once.

\$\endgroup\$
4
\$\begingroup\$
glEnable(GL_CULL_FACES);

Simple one liner that applies back face culling. This is excellent for trimming down polygon count. Also look into Frustum/Occlusion culling for reducing poly counts.

Another thing to look into is SIMD for your vectors and quaternions (mostly used in camera rotations). If you don't want to mess with ASM then check out libSIMD which is a great library for handling SIMD in C.

\$\endgroup\$
4
\$\begingroup\$

Don't use glGet* when you can access local variables, e.g.:

  • Don't query textures size from OpenGL, store them on loading in variables
  • Don't query line widths / points sizes from OpenGL when you can store the changes in your application

These calls are really slow

Move unused stuff outside of glBegin - glEnd clause (be it glColor or glNormal or anything else) if it does not changes inside. OpenGL is a state machine, it uses the last value.

\$\endgroup\$
3
\$\begingroup\$

Minimize OpenGL commands is the key. So you must batch all drawing commands. For 2d games, it is a good idea to use Altas Texture and draw all sprites sharing the same texture in one opengl command.

\$\endgroup\$
  • \$\begingroup\$ Be careful. Less and bigger batches are useful, but depending on the target platform "too big" batches will decrease performance. \$\endgroup\$ – Andreas Jul 22 '10 at 14:37
3
\$\begingroup\$

Batch, batch, batch!! (Ab)use the GPU to render as much as possible in each draw call. Games are mostly limited by the amount of state changes, not so much by the amount of triangles.

\$\endgroup\$