Skip to content

Instantly share code, notes, and snippets.

@mgsx-dev
Created September 25, 2019 14:45
Show Gist options
  • Save mgsx-dev/bcdf62afdaf7584fddc04d2e48e002f1 to your computer and use it in GitHub Desktop.
Save mgsx-dev/bcdf62afdaf7584fddc04d2e48e002f1 to your computer and use it in GitHub Desktop.
SpriteBatch VS PolygonSpriteBatch drawing quads benchmark
package com.mygdx;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.profiling.GLProfiler;
public class BatchBenchmark extends ApplicationAdapter
{
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.vSyncEnabled = false;
config.foregroundFPS = 0;
new LwjglApplication(new BatchBenchmark(), config);
}
private static final boolean BENCHMARK_POLY = true;
private FPSLogger fps = new FPSLogger();
private Batch batch;
private TextureRegion region;
private GLProfiler profiler;
private float time;
private int drawCalls;
private int frames;
@Override
public void create() {
profiler = new GLProfiler(Gdx.graphics);
profiler.enable();
if(BENCHMARK_POLY){
batch = new PolygonSpriteBatch(32767);
}else{
batch = new SpriteBatch(8191);
}
Texture texture = new Texture(new Pixmap(32, 32, Format.RGBA8888));
region = new TextureRegion(texture);
}
@Override
public void render() {
final int N = 8191 * 10;
batch.begin();
for(int i=0 ; i<N ; i++){
batch.draw(region, 0, 0);
}
batch.end();
fps.log();
float delta = Gdx.graphics.getDeltaTime();
time += delta;
if(time > 1){
System.out.println(drawCalls + " / " + frames);
time = 0;
drawCalls = 0;
frames = 0;
}
drawCalls += profiler.getDrawCalls();
frames++;
profiler.reset();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment