(Modern) Opengl Rendering Too Slow

May 27, 2018 at 11:57pm
I don’t know how to make opengl go any faster. In my ENTIRE project, i have only ONE Vertex Array Object, ONE Vertex Buffer Object, ONE Index Buffer Object, and only ONE Shader.


However, i am calling glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, nullptr) 100 times in a loop, and that drops my FPS down A LOT, Please help me, i am developing a 2D game, but because of this problem, i cant move forward.

Side note: I only made a VBO VAO IBO once in my program (meaning that its not in a loop). If i render only 4, 6, 10 etc, it still drops my FPS. I need help. If someone really needs my code, i will give it.
May 28, 2018 at 9:12am
What is a lot? If you go from doing nothing to doing something there will obviously be a huge relative difference.
May 28, 2018 at 10:25am
However, i am calling glDrawElements() 100 times in a loop

Why? Why do you draw same(?) triangles 100 times?

You can
either pack triangles to array(s) and draw them all with one call
or you really cannot avoid the dynamic changes and multiple calls.
May 28, 2018 at 12:46pm
How is this possible, can you give me an example? To pack all triangles to an array?
May 28, 2018 at 4:22pm
Is the behavior even defined if you pass in a nullptr...? 100 calls of 2x triangles could be improved, but it shouldn't be that detrimental.

i render only 4, 6, 10 etc, it still drops my FPS.

Drops your FPS from what to what? You're making us completely guess. Also, it's probably more accurate to measure your render time instead of the overall frame rate.

Are you re-allocating the triangles/buffers each time in the loop? What is your shader program doing? We need more information.
Last edited on May 28, 2018 at 4:23pm
May 28, 2018 at 8:36pm
Thanks for your reply sir, as I mentioned in my question, no, i am NOT allocating buffers each frame, I only sent the data to OpenGL once, in my loop I am ONLY rendering the trianges and using matrices to transform my geometery. And yes, my FPS is 1600+ if I render nothing, but if I render more, let’s say for example, 100 x 100 rectangles, it drops my FPS to around 10 or 20.
Last edited on May 28, 2018 at 8:39pm
May 28, 2018 at 8:37pm
Oh, and as you mentioned, i will try putting a nullptr to see how it effects my FPS, :)


Any further help will be appreciated :-)
May 29, 2018 at 8:30am
in my loop I am ONLY rendering the trianges and using matrices to transform my geometery.

You mean, to make one frame you essentially do:
LOOP k = 1..n
  APPLY transform k
  DRAW one triangle

Do all these n transforms change between each frame?
Even if yes, you can't precompute n triangles on CPU?
If you could, then drawing of one frame would simplify to:
DRAW triangles

May 29, 2018 at 9:15am
Yes, each draw call is like this:

void Renderer::push(const Rectangle& rectangle) {

rectangles.push_back(rectangle);

}

void Renderer::drawRectangles(){

for(auto& rect : Rectangles) {

//load uniforms, apply colors, transformations through shaders, etc.

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, nullptr);

}
}
May 29, 2018 at 10:11am
Once again, what prevents:
1
2
3
4
5
void Renderer::drawRectangles(){
  //load uniforms, apply colors, transformations through shaders, etc.

  glDrawElements(GL_TRIANGLES, 6 * rectangles.size(), GL_UNSIGNED_SHORT, nullptr);
} 
Last edited on May 29, 2018 at 8:37pm
May 29, 2018 at 12:36pm
Sorry, but I didn't get your question, prevents from what?
That is a simple draw call, that is being called 100 x 100 times.
May 29, 2018 at 8:37pm
You have:
1
2
3
4
5
6
7
void Renderer::drawRectangles(){
  for(auto& rect : rectangles) {
    //load uniforms, apply colors, transformations through shaders, etc.

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, nullptr);
  }
}

Could you have:
1
2
3
4
5
void Renderer::drawRectangles(){
  //load uniforms, apply colors, transformations through shaders, etc.

  glDrawElements(GL_TRIANGLES, 6 * rectangles.size(), GL_UNSIGNED_SHORT, nullptr);
}



You say that drawRectangles() is called 10'000 times. Does that mean that in your version the
//load uniforms, apply colors, transformations through shaders, etc.
is called rectangles.size()*10'000 times?
Last edited on May 29, 2018 at 8:38pm
Topic archived. No new replies allowed.