• Advertisement
Sign in to follow this  

OpenGL Which per-frame operations are expensive in terms of performance in OpenGL?

This topic is 673 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok guys, I already made the camera and hero movement and I loaded one small map consisting of 100 cubes for my mmorpg. I made some ugly models using blender, and I made a function that reads the vertices from .obj file and stores everything in GLfloat array. The next very important thing I need to know is: What kinds of operations should I try to keep to a minimum in my game?
What I gathered from previous posts is:

 

1. Don't make separate VAO and VBOs for every object, because binding and redefining a thousand VBOs every frame takes time.

2. Don't make too much draw calls because that takes time, too.

3. Calculate projection*view*model matrix outside the vertex shader, so you don't calculate the same stuff over and over.

4. Use spheres for collision whenever you can, because it's the simplest possible bounding object.

 

If anything more comes to mind, write it here, please, even if it's not so beginner-friendly, because I can return to this post at some point in the future.

 

And one more thing: I have a question about that 1st principle I just mentioned. Now that I have 7 or 8 models, what I need is a good, logical, performance-wise way of storing them in memory and drawing them on screen. How should I know when is the right time to make another VAO or VBO. Basically I know that VAO is a list of attributes for a number of objects, and it consists of several VBOs, and one VBO can contain vertex coordinates or textures or normals or stuff like that. So, for example, if I have 7-8 models with their own vertex coordinates, should I keep all their vertex coordinates in only one VBO or should I do something else?

Edited by Heelp

Share this post


Link to post
Share on other sites
Advertisement

You are asking a problem that may not be necessary for many years to come for you. A lot of people ask this and I tell everyone, optimize when you need to. Hardware is extremely fast nowadays that this shouldn't be a concern. Focus on your game if that is what you are building. If you just want to build the best tech in the world, then that is a different story.

Share this post


Link to post
Share on other sites

This kind of thing is actually micro-optimizing with the kind of workload you're doing.

 

The worst operations for performance are and always will be: (1) reading anything back from the GPU, and (2) updating a resource that's in use.  Avoid those, and so long as the rest of your code isn't fighting the API you'll do OK.

Share this post


Link to post
Share on other sites

I'm directx not opengl but my understanding in regards to playing nice with the API they are similar.

 

1. Don't draw what you don't need to.  So frustum and if possible occlusion culling.

2. draw front to back to take advantage of hi-z and early-z.

3. sort by state to reduce state changes between draw calls and use texture atlus's to furture reduce state changes.

4. I agree with your 1,2, and 3 except I would say only make as draw calls as you need to and minimize draw calls using various techniques like instancing.

5. mhagain advice is good as well.

6. figure out the best way to upload uniforms to the gpu. (I remember a couple of threads in this forum where there were performance implications)

Share this post


Link to post
Share on other sites

1. Don't make separate VAO and VBOs for every object, because binding and redefining a thousand VBOs every frame takes time.
2. Don't make too much draw calls because that takes time, too.
3. Calculate projection*view*model matrix outside the vertex shader, so you don't calculate the same stuff over and over.

1. Yes, if you've got different VBO's per object, you'll have to make extra GL function calls to rebind VBO's inbetween rendering each object. Whether or not this is a problem depends on your game. If you're trying to draw 10's of thousands of unique objects, it's probably an issue. If you're trying to draw one thousand, not as much.

2. Basically, any GL function takes up CPU time. If you design your renderer so that you'll make the least GL calls, then you'll save CPU time. However, in order to do this, sometimes you have to do things that are inefficient for the GPU... Depending on your game, it may be more important to optimize for the CPU or optimize for the GPU.

3. As above, if your game has spare CPU time and is over budget on the GPU, then this could be a good idea. However, if the opposite is true (spare GPU time, over CPU budget), then it may be harmful :P

Also, if you calculate projection*view*model on the CPU, then you need to repeatedly set a uniform value for every single object in the scene. On the other hand, if you put projection*view in one UBO and model in (many) others, then you only need to set projection*view once per frame instead of once per object. If your scene is full of static objects (so model doesn't need to be set every frame), this could be a significant reduction in GL calls...

Share this post


Link to post
Share on other sites

1. Yes, if you've got different VBO's per object, you'll have to make extra GL function calls to rebind VBO's inbetween rendering each object. Whether or not this is a problem depends on your game. If you're trying to draw 10's of thousands of unique objects, it's probably an issue. If you're trying to draw one thousand, not as much.
2. Basically, any GL function takes up CPU time. If you design your renderer so that you'll make the least GL calls, then you'll save CPU time. However, in order to do this, sometimes you have to do things that are inefficient for the GPU... Depending on your game, it may be more important to optimize for the CPU or optimize for the GPU.
3. As above, if your game has spare CPU time and is over budget on the GPU, then this could be a good idea. However, if the opposite is true (spare GPU time, over CPU budget), then it may be harmful :P
Also, if you calculate projection*view*model on the CPU, then you need to repeatedly set a uniform value for every single object in the scene. On the other hand, if you put projection*view in one UBO and model in (many) others, then you only need to set projection*view once per frame instead of once per object. If your scene is full of static objects (so model doesn't need to be set every frame), this could be a significant reduction in GL calls...


These are great examples because they also illustrate some other factors to be aware (and beware) of.

 

The main thing that jumps out here is performance vs code complexity vs flexibility.  The example of putting all objects into a single VBO is a great use case for this.  On the one hand you get to reduce VBO binds, but on the other hand your model loading code becomes more complex because you've now got to record offsets for each object (as well as handle the case where a VBO you allocate up-front may not be large enough).  Then you lose the flexibility to load and unload models on the fly.

 

Depending on your program any or all of these may be a deal-breaker.  So don't let the quest for the absolute theoretical highest performance overrule other goals.

 

The example of matrices is another great one, this time illustrating how an optimization in one place can lead to loss of performance in another.  That's often the case with optimizations: you're making a trade-off and hoping that you come out with a net positive.  In this case setting projection * view one-time-only certainly looks attractive, but you end up trading that off against having to make an extra matrix multiplication per-vertex.  That's one of the reasons why we always say "benchmark", because unless you have measurements you'll never know if the work you did to optimize one area didn't come at a greater cost elsewhere.

Share this post


Link to post
Share on other sites
Also, if you calculate 

 

projection*view*model on the CPU, then you need to repeatedly set a uniform value for every single object in the scene. On the other hand, if you put projection*viewin one UBO and model in (many) others, then you only need to set projection*view once per frame instead of once per object. If your scene is full of static objects (so model doesn't need to be set every frame), this could be a significant reduction in GL calls...

 

 

Question. Im a stupid person, maybe I don't understand something, but follow my logic here. If I have 2 million vertices, then the vertex shader runs 2 million times. Why would I want to calculate projection*view 2 million times per frame, when I can do it only once per frame in the cpu? What tradeoff??? :blink: 

Edited by Heelp

Share this post


Link to post
Share on other sites

Question. Im a stupid person, maybe I don't understand something, but follow my logic here. If I have 2 million vertices, then the vertex shader runs 2 million times. Why would I want to calculate projection*view 2 million times per frame, when I can do it only once per frame in the cpu? What tradeoff??? :blink: 

 

 

 

Also, if you calculate projection*view*model on the CPU, then you need to repeatedly set a uniform value for every single object in the scene. On the other hand, if you put projection*view in one UBO and model in (many) others, then you only need to set projection*view once per frame instead of once per object. If your scene is full of static objects (so model doesn't need to be set every frame), this could be a significant reduction in GL calls...

 

He explains it with the above.

Also GPU's are ALU heavy and they can tear through calculations.

Share this post


Link to post
Share on other sites

Why do I need to repeatedly set a uniform value, uniform value is only set up once, and it works for all the vertices, that's why I had declared it to be uniform in the first place.

Share this post


Link to post
Share on other sites

Question. Im a stupid person, maybe I don't understand something, but follow my logic here. If I have 2 million vertices, then the vertex shader runs 2 million times. Why would I want to calculate projection*view 2 million times per frame, when I can do it only once per frame in the cpu? What tradeoff??? :blink:

The CPU and GPU are completely independent parallel processors running on their own timelines.
If game A takes 15ms of CPU time per frame and 4ms of GPU time per frame, then your optimization choices will be different to game B that takes 8ms of CPU time per frame and 30ms of GPU time per frame.
Whichever of the two processors is taking more time per frame, we say that that processor is bottlenecking the game.
Game A is bottlenecked by the CPU frametime. We can add another 11ms of per-frame work to the GPU and this game's framerate won't change at all!
Game B is bottlenecked by the GPU frametime. We can add another 22ms of per-frame work to the CPU and this game's framerate won't change at all!

When optimizing Game A, I'd try to reduce the CPU workload, even if that means increasing the GPU workload.

So, given the example you quoted:
#1
Once per object (CPU):
Update UBOs with every static object's world matrix:
Per frame per camera (CPU):
Update UBO with view*projection matrix:
Per vertex (GPU):
Calculate mul( view_projection, mul( world, position ) );

#2
Per frame per camera per object (CPU):
Update UBOs with world*view*projection matrix:
Per vertex (GPU):
Calculate mul( world_view_projection, position );

#1 has a reduced CPU workload, but increased GPU workload.
#2 has an increased CPU workload, but reduced GPU workload.

Yes, #2 is more efficient in general (assuming you've got a large number of vertices), because the per-vertex part happens a large number of times... but generalizations aren't useful when optimizing specific games.

Also, a "position * matrix" operation takes something like 4 clock cycles. A low-spec PC GPU has about 1280 "shader cores" running at 1GHz, so 2M vertices at 4 clock cycles each, divided by 1280 parallel operations is 6250 clock cycles, which takes 6.25 microseconds. So then you've got to measure the CPU overhead of performing the per-object world-view-proj calculations and UBO updates, and then weigh up whether 6?s of GPU time is more important than whatever the CPU time cost is.

Furthermore, GPU's are usually bottlenecked by memory access times. Often the GPU's ALU units are sitting idle, waiting for data to arrive from memory before they can compute anything. In many situations, you may be able to add extra ALU instructions to a shader without increasing it's runtime whatsoever! So in some cases, this extra matrix multiply could work out to be free.

Share this post


Link to post
Share on other sites

True story....Ok, I agree.  ^_^  Thanks for the long post.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
      Regards,
      LifeArtist
    • By QQemka
      Hello. I am coding a small thingy in my spare time. All i want to achieve is to load a heightmap (as the lowest possible walking terrain), some static meshes (elements of the environment) and a dynamic character (meaning i can move, collide with heightmap/static meshes and hold a varying item in a hand ). Got a bunch of questions, or rather problems i can't find solution to myself. Nearly all are deal with graphics/gpu, not the coding part. My c++ is on high enough level.
      Let's go:
      Heightmap - i obviously want it to be textured, size is hardcoded to 256x256 squares. I can't have one huge texture stretched over entire terrain cause every pixel would be enormous. Thats why i decided to use 2 specified textures. First will be a tileset consisting of 16 square tiles (u v range from 0 to 0.25 for first tile and so on) and second a 256x256 buffer with 0-15 value representing index of the tile from tileset for every heigtmap square. Problem is, how do i blend the edges nicely and make some computationally cheap changes so its not obvious there are only 16 tiles? Is it possible to generate such terrain with some existing program?
      Collisions - i want to use bounding sphere and aabb. But should i store them for a model or entity instance? Meaning i have 20 same trees spawned using the same tree model, but every entity got its own transformation (position, scale etc). Storing collision component per instance grats faster access + is precalculated and transformed (takes additional memory, but who cares?), so i stick with this, right? What should i do if object is dynamically rotated? The aabb is no longer aligned and calculating per vertex min/max everytime object rotates/scales is pretty expensive, right?
      Drawing aabb - problem similar to above (storing aabb data per instance or model). This time in my opinion per model is enough since every instance also does not have own vertex buffer but uses the shared one (so 20 trees share reference to one tree model). So rendering aabb is about taking the model's aabb, transforming with instance matrix and voila. What about aabb vertex buffer (this is more of a cosmetic question, just curious, bumped onto it in time of writing this). Is it better to make it as 8 points and index buffer (12 lines), or only 2 vertices with min/max x/y/z and having the shaders dynamically generate 6 other vertices and draw the box? Or maybe there should be just ONE 1x1x1 cube box template moved/scaled per entity?
      What if one model got a diffuse texture and a normal map, and other has only diffuse? Should i pass some bool flag to shader with that info, or just assume that my game supports only diffuse maps without fancy stuff?
      There were several more but i forgot/solved them at time of writing
      Thanks in advance
    • By RenanRR
      Hi All,
      I'm reading the tutorials from learnOpengl site (nice site) and I'm having a question on the camera (https://learnopengl.com/Getting-started/Camera).
      I always saw the camera being manipulated with the lookat, but in tutorial I saw the camera being changed through the MVP arrays, which do not seem to be camera, but rather the scene that changes:
      Vertex Shader:
      #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); TexCoord = vec2(aTexCoord.x, aTexCoord.y); } then, the matrix manipulated:
      ..... glm::mat4 projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); .... glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); ourShader.setMat4("view", view); .... model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); ourShader.setMat4("model", model);  
      So, some doubts:
      - Why use it like that?
      - Is it okay to manipulate the camera that way?
      -in this way, are not the vertex's positions that changes instead of the camera?
      - I need to pass MVP to all shaders of object in my scenes ?
       
      What it seems, is that the camera stands still and the scenery that changes...
      it's right?
       
       
      Thank you
       
    • By dpadam450
      Sampling a floating point texture where the alpha channel holds 4-bytes of packed data into the float. I don't know how to cast the raw memory to treat it as an integer so I can perform bit-shifting operations.

      int rgbValue = int(textureSample.w);//4 bytes of data packed as color
      // algorithm might not be correct and endianness might need switching.
      vec3 extractedData = vec3(  rgbValue & 0xFF000000,  (rgbValue << 8) & 0xFF000000, (rgbValue << 16) & 0xFF000000);
      extractedData /= 255.0f;
    • By Devashish Khandelwal
      While writing a simple renderer using OpenGL, I faced an issue with the glGetUniformLocation function. For some reason, the location is coming to be -1.
      Anyone has any idea .. what should I do?
  • Advertisement