0

I'm making app in OpenGL 2.0 and i have problem with very low FPS about ~20 frames per second. I made for every spite new object which draw with:

GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
     GLES20.glBindTexture(GLES20.GL_TEXTURE_2D,texture);

      int mPositionHandle = 
            GLES20.glGetAttribLocation(riGraphicTools.sp_Image, "vPosition");

      GLES20.glEnableVertexAttribArray(mPositionHandle);

      GLES20.glVertexAttribPointer(mPositionHandle, 3,
                                     GLES20.GL_FLOAT, false,
                                     0, vertexBuffer);

      int mTexCoordLoc = GLES20.glGetAttribLocation(riGraphicTools.sp_Image, 
                           "a_texCoord" );

      GLES20.glEnableVertexAttribArray ( mTexCoordLoc );

      GLES20.glVertexAttribPointer ( mTexCoordLoc, 2, GLES20.GL_FLOAT,
                    false,
                    0, uvBuffer);

      int mtrxhandle = GLES20.glGetUniformLocation(riGraphicTools.sp_Image, 
                         "uMVPMatrix");

      GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, m, 0);

      int mSamplerLoc = GLES20.glGetUniformLocation (riGraphicTools.sp_Image, 
                          "s_texture" );

      GLES20.glUniform1i ( mSamplerLoc, 0);

      GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length,
               GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

      GLES20.glDisableVertexAttribArray(mPositionHandle);
      GLES20.glDisableVertexAttribArray(mTexCoordLoc);
      GLES20.glFlush();
      GLES20.glFinish();

Now, i have only four sprites and fps about 20frames,but i'm going to add about 10-15 sprites.It will be buggy.

Filip
  • 15
  • 4

1 Answers1

2

There's a number of things that will probably help your performance. Starting with a couple of simple items:

  1. As @dari also pointed out, get rid of those glFlush() and glFinish() calls. You should very rarely need them. Excessive glFlush() calls are almost always harmful to performance, and glFinish() is really, really bad. In short, one of the main principles of graphics APIs like OpenGL is that they let the GPU work asynchronously from the CPU. glFinish() forces a full synchronization between CPU and GPU, instead of letting them work in parallel, which is highly undesirable.

  2. You should make all those glGetAttribLocation() and glGetUniformLocation() calls once after compiling the shader, and store away the locations, typically in member variables of your class. Then just use the values of those variables instead of making redundant calls every time you draw. For example, one time after you link the shader, you execute these statements, where mPositionLoc and mMatrixLoc are member variables:

    mPositionLoc = GLES20.glGetAttribLocation(riGraphicTools.sp_Image, "vPosition");
    mMatrixLoc = GLES20.glGetUniformLocation(riGraphicTools.sp_Image, "uMVPMatrix");
    

    Then when you draw, you use these variables for setting up your attributes and uniforms:

    GLES20.glEnableVertexAttribArray(mPositionLoc);
    GLES20.glVertexAttribPointer(mPositionLoc, 3,
                                 GLES20.GL_FLOAT, false,
                                 0, vertexBuffer);
    ...
    GLES20.glUniformMatrix4fv(mMatricLoc, 1, false, m, 0);
    

These two alone might already take you a big step forward. Beyond this, you should look into using Vertex Buffer Objects (VBOs). They allow you to store your vertex and index data in buffers, instead of passing them to glVertexAttribPointer() and glDrawElements() each time. This is particularly beneficial if the vertex data does not change frequently. You should be able to find plenty of tutorial and sample code for how to use them. They will use API calls like glGenBuffers(), glBindBuffer(), etc.

Taking it even further if this is not enough to meet your performance targets, you could consider storing the vertex data for all your sprites in a single buffer, instead of having a separate buffer for each. This way, you will not need as many glBindBuffer() can glVertexAttribPointer() calls.

Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
  • 2.Which part of code i must execute every time and which part i can execute only on start. – Filip Jul 19 '14 at 22:51
  • 1
    I added a couple of examples. – Reto Koradi Jul 19 '14 at 23:04
  • Ok i understand and have done step 1 and 2. Result is about 60FPS :D It's good. I can't VBOs implement because i'm changing vertex data frequently. [link](http://stackoverflow.com/questions/24844950/android-opengl2-0-intersection-between-two-textures) Can you help me with this? – Filip Jul 19 '14 at 23:08