Jump to content
  • Advertisement
Sign in to follow this  
slicksk8te

OpenGL Modern OpenGL Transforms, HOW?

This topic is 916 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

I have been scouring all the documentation, forums, and general internet searches for this question and I have found a bunch of half answers and I hope someone can help.

 

Now to the question.

 

Lets say I have 1000 objects that use the same mesh, shader, texture, etc. but they all have separate transforms (locations, rotations, scale).

What is the best way to render this without tanking the frame time?

 

I am weary of instancing because there may be many different meshes in the future with different textures/state.

 

The slow way that I know of that is trivial to implement is to sort the geometry by state and make a draw call for each one and passing the transform matrix in through a uniform matrix. This does not work because DrawArrays is extremely slow and completely CPU bound. I have implemented this and know that this seriously destroys my frame time even with a couple of hundred draw calls.

 

I would like to use a single VAO for each object type, but how do you deal with transform data? The solution I have usually seen involves transforming the vertices on the CPU and then updating the VBO before drawing. It seems rather wasteful to do this on the CPU and resubmit the data because the GPU is sooooo much better at doing transforms in the vertex shader. This also may make the program CPU bound yet again.

 

Most explanations I have seen on how to do this always focus on rendering a few object per frame not hundreds let alone thousands. I absolutely know this is possible because i have seen many games pull off hundreds of objects. I know if there is a static environment then it is pretty easy to render because there should only be one transform for the whole environment if done right. That is pre-calculating the model space of a chunk or environment and then just moving the whole chunk or environment. The problem is dynamic objects. I only need a few hundred to about a thousand. I don't imagine that it can be this difficult.

 

I feel like I am missing something important and hope that i can get some information on how most games do it.

 

I have done a large amount of profiling and all roads seem to lead to reducing the number of draw calls, but I am having trouble determining how to do that with so many independent meshes with different transformation matrices.

 

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

Instancing.

 

OK, I know that you said this:

 

 

I am weary of instancing because there may be many different meshes in the future with different textures/state.

 

However, even if you do have different textures and state in the future, that's no reason to make your performance suffer now.  An instancing system can be coded to deal with this; even if you have, say 100 different combinations, that's still 100 draw calls versus 1000.

Share this post


Link to post
Share on other sites

You could load up your model and indices into a VBO/IBO and then setup your VAO + shader with them.  You would then create a uniform buffer object that holds the transform matrices  as an array for all your entities and update that every frame.  Then render all the models at once using either instancing or multidraw rendering.  In your shader use the draw_ID to index into the transform matrix array to get that particular model's transform.

 

If you have multiple models, as long as they use the same shader and have the same vertex format, you could store them all into a single VBO/IBO/VAO and use mulitidraw with different offsets.  You would need to look at putting all the textures in a texture array or using direct state access for the texture data.

 

Instead of a uniform buffer, you could use a shader storage buffer if you have a lot of data to store per instance (transform, material ids, etc), then index into it using the draw_ID.  If you go the SSBO route, you may want to look at triple buffering it and persistently mapping it so that you don't stall out rendering trying to update it.

 

cheers,

 

Bob

Share this post


Link to post
Share on other sites
Thanks for the help. I have tried instancing and it does mostly work for what I'm doing.

Don't UBOs have a relatively low limit for max size?

I'm guess I'm still unsure how AAA games are able to have so many meshes rendered along with things like particle effects

What strategies are employed to reduce the number of draw calls and be able to maintain independent object transformation?

I'm sure instancing is used for particle effects but I'm not sure how this is done in practice because there can be many emitters that (i.e. many explosions) die after some time. Would the buffer that contains the transformations have to be scraped and refreshed every time a particle died?

I'm pretty good at getting an implementation together once I understand the strategies but I'm having trouble finding best practices on this stuff. There are many beginner tutorials our there but I can't seem to get information on how AAA does these things in practice.

Share this post


Link to post
Share on other sites

AAA engines don't necessarily use the most modern strategies out there, but just something that works well enough for the target platform(s) + the game they're running.

 

Typically the most amount of geometry comes from static world geometry, which can be batched together (offline, not runtime) into bigger chunks to reduce draw calls.

 

Something like trees and foliage would just be rendered instanced. When far away, sprite impostors can be used for even lower performance impact.

 

One particle effect with all its particles is usually one draw call, and its vertex buffer is typically completely rewritten each frame when it's visible and updating. Most of the math can be done in vertex shader so that the CPU-side calculations for the update stay light.

 

On modern desktops you can get quite high with the draw call count (a few thousand should be no problem) but on high resolutions and less beefy GPU's the shading would easily become the bottleneck instead.

 

One thing to consider is that AAA games on Windows usually use D3D, which can have better optimized drivers. If you have bad luck with OpenGL drivers (Intel?) and an older card the driver may be doing work on CPU which should really be done on GPU. Using VBO's and a good driver, over a thousand draw calls should not be a problem on either D3D or OpenGL.

 

Share this post


Link to post
Share on other sites

Uniform buffers are much smaller than SSBO's.  My graphics card says it supports a max of 65536 bytes in the buffer (64k).  This is enough for 1024 transform matrix's (mat4).  If you need more, or want to triple buffer so you can update without stalling rendering, then you should look at using SSBO's.  SSBO's will be at least be 16MB in size.  

 

Cheers, 

 

Bob

Share this post


Link to post
Share on other sites


I'm guess I'm still unsure how AAA games are able to have so many meshes rendered along with things like particle effects

What strategies are employed to reduce the number of draw calls and be able to maintain independent object transformation?

 

Many mega-particles solutions involve a single draw of a mesh that you can imagine as, f.e., 200 quads floating over each other with small constant gaps between them along an object space axis direction - this means that value of quad on that axis provides a special state to every single quad that you can functionalize in vertex function (create many animated  supper glows, etc.)

 

Many particle-rich effects are a function of time and a single value in vertex, not a per particle transformation individual setters.

 

Now instancing drawings by transformations as well, is not usually a thing that will benefit most of the scenarios, even with army of reproduced meshes- since:

- draw calls that differ only by a uniform changing state in between them are nearly as fast as a single draw (they do run in parallel when issued, doing same amount of vertex function jobs).

- you will loose ability to sort geometry rendering from front to back, frustum culling, fill rate savings, in trivial ways

- you will introduce the second per instance vertex buffer, which you will need to update if still wanting the optimization techniques, and complicate vertex function feed (at each vertex)

- Remember the first point of nearly no benefit as opposed to the separate draw calls issued after each other to gpu.

 

Unless GPU vendors totally revolve GPU architecture, you may consider a whole pile of modern routines in graphic libraries as pure theoretical benefits, doing only harm when introduced today, even when speaking solely of performance only for that matter.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!