Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization for SpriteBatch when running non VertexArray VertexDataModes. (GL30 default) #7346

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion gdx/src/com/badlogic/gdx/graphics/g2d/SpriteBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ public SpriteBatch (int size, ShaderProgram defaultShader) {
ownsShader = true;
} else
shader = defaultShader;

// Pre bind the mesh to force the upload of indices data.
if (vertexDataType != VertexDataType.VertexArray) {
mesh.bind(shader);
mesh.unbind(shader);
}
}

/** Returns a new instance of the default shader used by SpriteBatch for GL2 when no shader is specified. */
Expand Down Expand Up @@ -956,7 +962,7 @@ public void flush () {
lastTexture.bind();
Mesh mesh = this.mesh;
mesh.setVertices(vertices, 0, idx);
Buffer indicesBuffer = (Buffer)mesh.getIndicesBuffer(true);
Buffer indicesBuffer = (Buffer)mesh.getIndicesBuffer(false);
indicesBuffer.position(0);
indicesBuffer.limit(count);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

package com.badlogic.gdx.tests;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.WindowedMean;
import com.badlogic.gdx.tests.utils.GdxTest;
import com.badlogic.gdx.tests.utils.GdxTestConfig;
import com.badlogic.gdx.utils.StringBuilder;

@GdxTestConfig
public class SpriteBatchPerformanceTest extends GdxTest {

private Texture texture;
private SpriteBatch spriteBatch;
private WindowedMean counter = new WindowedMean(10000);
private StringBuilder stringBuilder = new StringBuilder();

private BitmapFont bitmapFont;

@Override
public void create () {
texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
spriteBatch = new SpriteBatch(8191);
bitmapFont = new BitmapFont();
}

@Override
public void render () {
Gdx.gl20.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
Gdx.gl20.glClearColor(0.2f, 0.2f, 0.2f, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

spriteBatch.begin();

// Accelerate the draws
for (int j = 0; j < 100; j++) {

// fill the batch
for (int i = 0; i < 8190; i++) {
spriteBatch.draw(texture, 0, 0, 1, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor thing but maybe instead of drawing a single point we can change it to something like 20px * 20px just for the user to see something is happening.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@obigu i think it would bias the test since it will overload fragment shader stage, considering there are about 819.000 drawings, it would have to render 20x20 pixels each, so ~327 Mega pixels which could be a lot compared to the actual amount (less than 1 Mega pixels). In this end it would defeat isolation of this test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we want to isolate a bit more, we could disable blending and use depth test (never pass) in order to fully bypass fragment stage.

}

long beforeFlush = System.nanoTime();

spriteBatch.flush();
Gdx.gl.glFlush();
long afterFlush = System.nanoTime();

counter.addValue(afterFlush - beforeFlush);

}

spriteBatch.end();

spriteBatch.begin();
stringBuilder.setLength(0);
stringBuilder.append("Mean Time ms: ");
stringBuilder.append(counter.getMean() / 1e6);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a conditional that shows "Please Wait..." on the text dependign if counter.hasEnoughData() returns true or not as it may take a while (over 5s on a Samsung S7) to populate depending on the device you run the test and I thought the test was not working.

bitmapFont.draw(spriteBatch, stringBuilder, 0, 200);
spriteBatch.end();
}

@Override
public void dispose () {
texture.dispose();
spriteBatch.dispose();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ public class GdxTests {
SortedSpriteTest.class,
SoundTest.class,
SpriteBatchRotationTest.class,
SpriteBatchPerformanceTest.class,
SpriteBatchShaderTest.class,
SpriteBatchTest.class,
SpriteCacheOffsetTest.class,
Expand Down