How To Use The LibGDX GLProfiler

skaterdad

Mike

Posted on February 26, 2020

How To Use The LibGDX GLProfiler

When optimizing a LibGDX game's performance, sometimes you need to look under the hood. Let's add the GLProfiler to a Screen and see how (in)efficiently we're rendering.

Why?

In OpenGL, binding textures on the GPU can be an expensive operation. For each texture switch, there is a draw call. By minimizing these calls, you can improve rendering performance.

Thankfully, LibGDX has a built-in class to help us inspect the OpenGL calls: GLProfiler (Docs | Wiki).

Profiler Code

Here's the code needed to set up the GLProfiler in a theoretical "GameScreen" and read the number of draw calls and texture bindings.

  import com.badlogic.gdx.graphics.profiling.GLProfiler;

  public class GameScreen implements Screen {
    // Add this class member
    private GLProfiler profiler;

    public GameScreen(...) {
      // Your setup code

      // create & enable the profiler
      profiler = new GLProfiler(Gdx.graphics);
      profiler.enable();
    }

    @Override
    public void render(float delta) {
      // reset on each frame
      profiler.reset();

      // Do all your rendering here
      // ...

      // Check the profiler data.
      // You can view in debugger, log it, etc...
      float drawCalls = profiler.getDrawCalls();
      float textureBinds = profiler.getTextureBindings();

      // also handy
      float fps = Gdx.graphics.getFramesPerSecond();
    }

  }

What should you look for?

Many factors can result in extra draw calls & texture bindings.

  • Maybe you aren't using TextureAtlases?
  • Maybe you are, but there are more than needed?
  • Maybe you're rendering things in whatever order was convenient 6 years ago while your baby was sleeping?
  • Are you using Scene2D, and did not consider the textures when adding Actors to the Stage?
  • Scene2D actors (like TextButton) with BitmapFonts that are not packed with the other UI graphics.
  • Gremlins

The LibGDX Wiki page on SpriteBatch, TextureRegions, and Sprites is a great resource to learn more.

In general, use fewer texture files, and try to render your objects in texture-order as much as possible.

Case study coming soon

I recently went through a profiling & optimization effort for Santa Skate. Before release, I had not profiled it once! Performance was still okay, but my engineering brain was not satisfied knowing there were still slowdowns. In the coming days (weeks), I plan on writing another post explaining how I tamed the texture binds related to Scene2D widgets and the game world.

💖 💪 🙅 🚩
skaterdad
Mike

Posted on February 26, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How To Use The LibGDX GLProfiler
gamedev How To Use The LibGDX GLProfiler

February 26, 2020