Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't achieve 60fps rendering simple quad, Android, Opengl ES 2.0

I'm working on a simple pong type game to get to grips with opengl and android, and seem to have hit a brick wall in terms of performance.

I've got my game logic on a separate thread, with draw commands sent to the gl thread through a blocking queue. The problem is that I'm stuck at around 40fps, and nothing I've tried seems to improve the framerate.

To keep things simple I set up opengl with:

GLES20.glDisable(GLES20.GL_CULL_FACE);
GLES20.glDisable(GLES20.GL_DEPTH_TEST);
GLES20.glDisable(GLES20.GL_BLEND);

Set up of the opengl program and drawing is handled by the following class:

class GLRectangle {
private final String vertexShaderCode =
        "precision lowp float;" +
        // This matrix member variable provides a hook to manipulate
        // the coordinates of the objects that use this vertex shader
        "uniform lowp mat4 uMVPMatrix;" +

        "attribute lowp vec4 vPosition;" +
        "void main() {" +
        // the matrix must be included as a modifier of gl_Position
        "  gl_Position = vPosition * uMVPMatrix;" +
        "}";

private final String fragmentShaderCode =
        "precision lowp float;" +
        "uniform lowp vec4 vColor;" +
        "void main() {" +
        "  gl_FragColor = vColor;" +
        "}";

protected static int mProgram = 0;

private static ShortBuffer drawListBuffer;
private static short drawOrder[] = { 0, 1, 2, 0, 2, 3};//, 4, 5, 6, 4, 6, 7 }; // order to draw vertices

// number of coordinates per vertex in this array
private static final int COORDS_PER_VERTEX = 3;
private static final int vertexStride = COORDS_PER_VERTEX * 4; // bytes per vertex



GLRectangle(){

    int vertexShader = GameRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = GameRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

    mProgram = GLES20.glCreateProgram();             // create empty OpenGL ES Program
    GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
    GLES20.glLinkProgram(mProgram);                  // creates OpenGL ES program executables

    // initialize byte buffer for the index list
    ByteBuffer dlb = ByteBuffer.allocateDirect(
    // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);  
}

protected static void Draw(Drawable dObj, float mvpMatrix[])
{   
    FloatBuffer vertexBuffer = dObj.vertexBuffer;

    GLES20.glUseProgram(mProgram);
    //GameRenderer.checkGlError("glUseProgram");

    // get handle to fragment shader's vColor member
    int mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
    //GameRenderer.checkGlError("glGetUniformLocation");

    // get handle to shape's transformation matrix
    int mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    //GameRenderer.checkGlError("glGetUniformLocation");

    // get handle to vertex shader's vPosition member
    int mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    //GameRenderer.checkGlError("glGetAttribLocation");

    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    //GameRenderer.checkGlError("glUniformMatrix4fv");

    // Set color for drawing the quad
    GLES20.glUniform4fv(mColorHandle, 1, dObj.color, 0);
    //GameRenderer.checkGlError("glUniform4fv");

    // Enable a handle to the square vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    //GameRenderer.checkGlError("glEnableVertexAttribArray");

     // Prepare the square coordinate data
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                                    GLES20.GL_FLOAT, false,
                                    vertexStride, vertexBuffer);
    //GameRenderer.checkGlError("glVertexAttribPointer");

    // Draw the square
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length,
                              GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
    //GameRenderer.checkGlError("glDrawElements");

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
    //GameRenderer.checkGlError("glDisableVertexAttribArray");
}
}

I've done plenty of profiling and googling, but cant find anything to make this work faster... I've included a screenshot of the DDMS output:

DDMS Output

To me, it looks like glClear is causeing GLThread to sleep for 23ms... though I doubt that's really the case.

I have absolutely no idea how I can make this more efficient, there's nothing fancy going on. In my quest for better rendering performance I have switched to the multi-threaded approach I described, turned off alpha blending and depth testing, changed to a batched drawing approach (not applicable for this simple example), and switched everything to lowp in the shaders.

Any assistance with getting this to 60fps would be greatly appreciated!

Bruce

edit Well talk about overthinking a problem. It turns out that I've had the powersaving mode switched on for the past week... it seems to lock rendering to 40fps.

like image 301
BruceJones Avatar asked Nov 11 '12 19:11

BruceJones


1 Answers

This behavior occurs when Power Saving mode is switched on, using a Galaxy S3.

It appears the power saving mode locks the framerate to 40fps. Switching it off easily achieved the desired 60fps.

like image 96
BruceJones Avatar answered Oct 01 '22 10:10

BruceJones