I'm developing an Android app using OpenGL ES 2. The problem I am encountering is that the glClear()
function is taking so long to process that the game appears jittery as frames are delayed.
The output of a run of the program with timing probes shows that while setting up all vertices and images from the atlas only takes less than 1 millisecond, glClear()
takes between 10 and 20 milliseconds. In fact, the clearing often takes up to 95% of the total rendering time.
My code is based upon common tutorials, and the Render
function is this:
private void Render(float[] m, short[] indices) {
Log.d("time", "--START RENDER--");
// get handle to vertex shader's vPosition member
int mPositionHandle = GLES20.glGetAttribLocation(riGraphicTools.sp_Image, "vPosition");
// Enable generic vertex attribute array
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(mPositionHandle, 3,
GLES20.GL_FLOAT, true,
0, vertexBuffer);
// Get handle to texture coordinates location
int mTexCoordLoc = GLES20.glGetAttribLocation(riGraphicTools.sp_Image, "a_texCoord" );
// Enable generic vertex attribute array
GLES20.glEnableVertexAttribArray ( mTexCoordLoc );
// Prepare the texturecoordinates
GLES20.glVertexAttribPointer ( mTexCoordLoc, 2, GLES20.GL_FLOAT,
false,
0, uvBuffer);
// Get handle to shape's transformation matrix
int mtrxhandle = GLES20.glGetUniformLocation(riGraphicTools.sp_Image, "uMVPMatrix");
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, m, 0);
// Get handle to textures locations
int mSamplerLoc = GLES20.glGetUniformLocation (riGraphicTools.sp_Image, "s_texture" );
// Set the sampler texture unit to 0, where we have saved the texture.
GLES20.glUniform1i ( mSamplerLoc, 0);
long clearTime = System.nanoTime();
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
Log.d("time", "Clear time is " + (System.nanoTime() - clearTime));
// Draw the triangles
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length,
GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
GLES20.glDisableVertexAttribArray(mTexCoordLoc);
Log.d("time", "--END RENDER--");
}
I have tried moving the png atlas to /drawable-nodpi
but it had no effect.
I have tried using the glFlush()
and glFinish()
functions as well.
Interestingly, if I do not call glClear()
then it must automatically be called. This is because the total rendering time is still as high as when it was called, and there is no remnants of the previous frame onscreen. Only the first call to glClear()
is time-consuming. If it is called again, the subsequent calls are only 1 or 2 milliseconds.
I have also tried different combinations of parameters (such as GLES20.GL_DEPTH_BUFFER_BIT
), and using glClearColor()
. The clear time is still high.
Thank you in advance.