Sign in to follow this  
RippeR37

OpenGL Managing scene's (level's) geometry - how to do if efficiently

Recommended Posts

Hey, im working on engine for future game and i stumbled on a problem. I'm not sure how to efficiently manage (mostly store and send to GPU) data of my level geometry.

Game will be with Top-Down camera (3D), so visibile area will be reduced, also most of objects will be pretty low-poly since nobody needs extra sharp(/smooth) models if u barely can see some details on it. ;) Problem is that i'm not sure how to store it, (update if needed* - though geometry is static, just what is visible) and send to GPU.
I've come up with some ideas:

1. Most basic - load everything right in one VBO (as models are very low-poly it won't have too much data as you might think?).
I think that though vertex shadders will work on every vertex, fragment shaders will work only on visible fragments and "discard" others automatic, right?

+ simple to write
+ dont have to bind multiple VAOs/VBOs/textures every frame
+ don't have to send new data every frame to GPU (using CPU time) and compute it!
- lot of data is on VBO (though size won't change during game) and will go through Vertex Shader (i think not by F.S. - right?)
- it seem's not so optimized?
- everything must be in one texture atlas which limits possibilities

2. Load every distinct object at startup of level, and then every frame create new VBO with new mesh, that would be made from objects that are currently visible at scene (modified with transformation matrix so that it all works ok).

+ it sounds as its an optimization ;p (but after some thought i'm not so sure)
+ i manage what i send to draw, so i could do some work (like LOD, though not in that kind of game, becouse all models are usally at same distance from camera...) so it's not actually "pro" per se...
+ i bind one VAO/VBO after creating that whole scene's mesh and dont change it for frame.
+ only visible vertexes (objects, somevertex might be little out of course) are sent to GPU
- loooooot of work on CPU, which may result in: GPU waits for CPU data ..... CPU waits for rendering by GPU ... and again GPU waits...
- looks much more complicated to build efficiently (at least as much as can)
- everything visible must be in one texture atlas...

3. Split level on smaller areas like 2D-tiles, each tile contains every object's data that is (at least partial) in this tile. Just not too small tiles, so that per visible scene there would be 3x3 or 4x4 tiles max. (or even 2x2?).

+ sound pretty fast (only needed data, not many VAO/VBO binds per frame, VAO/VBO would be already set-up at loading-time so no computing time needed per frame, only draw call).
+ somehow more flexible with textures, becouse i need current texture only for small tile which can be only couple meshes.
- seems hard to code it efficient, some problems (look below)
- some meshe's that would be on more than one tile would have to be doubled or splitted (not easy job i guess to do it right and fast! doubled seems better somehow)
- lot of pre-processing so as long as #1 pre-processing or even longer.

4. Render each object separate. If it's visible multiple times, render it using some kind of mechanism to do it efficiently like instancing (or something else? suggestons? i dont know instancing yet, but i heard of it).

+ normally it may be common way to render scenes (when meshes have many triangles its ok, becouse switching vbo doesnt cost so much then)
+ easiest to implement
+ easy to manage scene
+ easy to create a scene (just add description of mesh - pos/rotate/scale and which object it is just to add it).
+ i could create VAOs/VBOs earlier so no computing time needed on CPU side every frame
+ only visible models would be rendered
- lot of switches between every objects (maybe not that much, but when meshes are low-poly, i guess switching would take long time compared to rendering)
- it doesn't seems too optimized.

As i said, mostly i'd like to balance between complexity and performance, since i'm not working on some top-level, AAA project, just smaller project that may be very playable, may not ;)

PS. Could someone write in some order or with "weights" how much OpenGL actions cost (about)? Like: single draw call, bind VBO, bind VAO, bind texture, etc... so i'd knew what to watch for :)

Share this post


Link to post
Share on other sites
fragment shaders will work only on visible fragments and "discard" others automatic, right?

Not necessarily, deferred rendering is meant to address this issue.  Some other behind the scenes optimizations require some thought on your part.  Depth sorting for one...

 

If you load 1 big VBO then you can still access them individually by using the models indices.  I would not do this.  Accessing a component still requires additional draw calls if you want to change anything.  If you combine this with the headache and nuisance of adding and subtracting and hopping through indices then it may not be worth the extra few percentage points of performance increase.  Some implementations may slow down, who knows?  Messing up the index management can crash a computer. You won't know all your particular  issues until you try what you've built on every single machine that you can get your hands on.

 

Even most of those silly little handheld phones can handle at least several dozen VBO draw calls before they start complaining too much, some of them will do 100 or more per frame, easily.  This is combined with changing shaders and textures for many of those models.

 

Depending on your setup:

(i)You can use a distance check to disable whatever is off screen.  if(modelPosX > -5.0 && modelPosX < 5.0){displayArea_1();} helps a lot to manage top view and side view games.  Also if you isolate collision + animation within these screen-sized chunks then you can get really carried away for every discrete area.

(ii)If, when you change rooms it's only vertically or horizontally, then you will never have to display more than two areas at a time which gives you lots of flexibility in how many draw calls, texture and shader swaps can take place. 

 

You could pack all the floor tiles for a room into one VBO, I think this would be practical and easy and safe.  Same for trees, rocks, bushes.   Now for the tiles, rocks, trees, and bushes you would have only 4 VBO's to switch between.  Even a wrist watch running Java could handle that much.

 

Pay attention to optimization but don't become so hung up on it that you set yourself behind several years worrying about it.  Stability is far more important.  Finishing something is also good.

 

p.s. You mentioned something about creating the required VBO's every frame.  I would say, avoid that.  Even changing them per-frame can stall things, I imagine creating them per-frame will be far worse. My imagination can't be stretched far enough for me to come up with a good reason to do this.

Edited by Josh Petrie

Share this post


Link to post
Share on other sites
You can use a distance check to disable whatever is off screen.  if(modelPosX > -5.0 && modelPosX < 5.0){displayArea_1();} helps a lot to manage top view and side view games.  Also if you isolate collision + animation within these screen-sized chunks then you can get really carried away for every discrete area.

But then i guess u mean that every mesh has its own VBO, right?
 

 

when you change rooms it's only vertically or horizontally, then you will never have to display more than two areas at a time which gives you lots of flexibility in how many draw calls, texture and shader swaps can take place.

I plan for it to be outdoor top-down shooter. Something similiar to Left 4 Dead, but top-down, now for PC and maybe in the future for Android(/iOS). Test pic (a lot to do yet): http://i.stack.imgur.com/XC0ga.png S(it had some debugging on so actually rendering time is not even half now)

I thought about making group of objects but it doesnt help me too much, if i put - in example - all trees in one VBO...
 

 

p.s. You mentioned something about creating the required VBO's every frame.  I would say, avoid that.  Even changing them per-frame can stall things, I imagine creating them per-frame will be far worse. My imagination can't be stretched far enough for me to come up with a good reason to do this.

I guess i'll have to update my shadow-map generation so that i'm not creating new texture every frame (for each light) but use same texture and just update it... Luckily its only couple lines...

In the end i still dont know how should i keep it. Group it by position in bigger VBOs and use only 2-4 VBOs per frame looks best, right? But the question is - should i keep it in VRAM or RAM and just send it if i need it? 2nd option would allow me to create very big maps since it holds very little data in vram (and ram is usally much bigger), but seems slower...

Btw. as i asked before - how slow is binding VAOs, binding VBOs, binding Texture, drawing 100 triangles, drawing 100k triangles, drawing 500k triangles, turning of depth test, something else that could be important here too, etc. ? I'd like to know what should i avoid and how hard should i optimize things to use as few as possible. 

Edited by RippeR37

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628400
    • Total Posts
      2982450
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
  • Popular Now