-->

FBO offscreen rendering slow

2019-04-15 03:07发布

问题:

I want to use offscreen rendering using openGL es 2.0 and GLSL shader
I setup FBO and it seems working except there are two problems

1) The program goes about 30 fps but all the sudden, it drops to 20fps and then come back to 30 fps again, then few secs later, drops to 20fps again then come back to 30fps and so on...(keeps going on like that) and I don't know what cause this random pause/delay
2) When I close the application, it does not respond for awhile. In other words, it seems like there are some data to clean up or some delay...So when I close the program, it does not do so until few seconds later.

Am I doing something wrong? Is there anyway I can fix these?
Here is my code:

My init function:

glGenFramebuffers(1,&frameBuf);
glGenRenderbuffers(1,&renderBuf);
glBindRenderbuffer(GL_RENDERBUFFER,renderBuf);
glRenderbufferStorage(GL_RENDERBUFFER,GL_RGBA4,640,480);
glBindRenderbuffer(GL_RENDERBUFFER,0);
glBindFramebuffer(GL_FRAMEBUFFER,frameBuf);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_RENDERBUFFER,renderBuf);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
    printf("GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT\n");
else
    printf("COMPLETE\n");

glBindFramebuffer(GL_FRAMEBUFFER,0);

The my draw function:

glBindFramebuffer(GL_FRAMEBUFFER, frameBuf);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

GLfloat vertices1[] = {
    -1.0,-1.0,-1.0, 1.0,-1.0,-1.0, -1.0,1.0,-1.0,
    1.0, 1.0,-1.0, -1.0,1.0,-1.0, 1.0,-1.0,-1.0
};
glVertexAttribPointer(vertexAttr1, 3, GL_FLOAT, GL_FALSE, 0, vertices1);
glEnableVertexAttribArray(vertexAttr1);
GLfloat texCoord[] = {
    0.0f,0.0f, 1.0f,0.0f, 0.0f,1.0f,
    1.0f,1.0f, 0.0f,1.0f, 1.0f,0.0f
};

glVertexAttribPointer(texCoordAttr1, 2, GL_FLOAT, GL_FALSE, 0, texCoord);
glEnableVertexAttribArray(texCoordAttr1);

glDrawArrays(GL_TRIANGLES, 0, 6);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

回答1:

Here are a few things that may help with performance:

  • glClearColor() only needs to be called once in the init function.
  • Create the GLFloat arrays only once in the init function, store them in VBOs and use those to render. This avoids having to send all your vertex data down the bus to the GPU every frame.
  • Move the glEnableVertexAttribArray() calls to init, no need to keep on re-enabling them every frame.
  • In this case, you don't need to ever unbind the framebuffer. That also gets rid of the glBindFramebuffer() call at the beginning of draw.
  • Generate a VAO in init and use that to replace most of what remains of the draw method.

Also it would be nice if we could see your shaders. A lot of embedded systems aren't very fast with fragment shaders, your shaders could be the issue here.

Oh and when are you calling glUseProgram()?