Sign in to follow this  
okonomiyaki

OpenGL VRAM with VBO's and VAR's (or D3D equivalent)

Recommended Posts

I'm reading up on exactly how the system and video card interacts in terms of memory. I'm trying to figure how to decide when to upload data to VRAM, how to do it, etc. I have a pretty good grasp on vertex buffer objects. I've never really worried about memory, but I want to start building a game that might need to manage it a little. Tell if this is a bad idea. With VBO's, you can figure out if you've ran out of memory by detecting the OUT_OF_MEMORY error from the BufferDataARB call. However, it seems risky to wait until you totally filled up the memory. Anyway, I guess you could detect that when you run out, delete the least recently used buffer, and try to upload the data again. But that seems inefficient and risky. You could get a kind of thrashing if you have a big buffer that is constantly deleted and reuploaded, which would make many other things reupload as well. That kind of takes that point out of the vertex buffer object, where you can upload data once and not worry about it again. What's a simple uploading/caching scheme? Do you usually use vertex arrays in combination with vertex buffer objects? What exactly happens in VRAM with vertex arrays? You upload the data once, and then is it cached at all? If so, how do you detect it, or does the system do that for you so that when you call a {Vertex}Pointer() function it finds the cached data? edit: I just realized that I'm talking about OpenGL structures. I hope this is still applicable in this forum. I guess I just wanted a general answer as to how you decide when/how to upload data to VRAM (all of it once at the beginning, if it all fits? how do you know if it fits?) [Edited by - okonomiyaki on July 7, 2004 11:47:59 AM]

Share this post


Link to post
Share on other sites
Hi...

Quote:

With VBO's, you can figure out if you've ran out of memory by detecting the OUT_OF_MEMORY error from the BufferDataARB call.


I'm not really sure that this is true. If BufferDataARB can't allocate the requested buffer on Video Memory, it will try AGP Memory and if it fails again it will try to allocate it on system memory. Don't take this too seriously though, because there is sometime since i read the specs. But i think this is the behavior for VBO at least.
With VAR it can be different because of the frequency and priority flags.

Any feedback on this?

Quote:

However, it seems risky to wait until you totally filled up the memory. Anyway, I guess you could detect that when you run out, delete the least recently used buffer, and try to upload the data again.


What happens if the 90% of the memory is allocated by textures, display lists, and other things that go to the Video/AGP memory? When you are at this situation, how can you be sure that deallocating the LRU vertex buffer will solve the problem?

What i thought for preventing situation like these, is precalculate the maximum texture/vbo/index etc. memory a certain level will use, and try to minimize the requirements "a priori" (e.g. compress textures to free some memory, use 16-bit indices where applicable, etc.). Or, if we are talking for really big levels (from the memory point of view), try to calculate the mean memory usage for some positions in it, and try to minimize this. This stands for situations where you stream data (almost) continuesly from the HD. Something like FarCry's terrain renderer (it keeps unloading and loading terrain chunks all the time, when you move fast of course (see editor)). BUT, what happens if you can't minimize memory requirements? Then you need somekind of memory manager for that. But, don't wait running out of memory to start unloading data. Because in OGL, i don't there is anyway for requesting availiable mem of any type (Vid/AGP).

Quote:

What's a simple uploading/caching scheme? Do you usually use vertex arrays in combination with vertex buffer objects? What exactly happens in VRAM with vertex arrays? You upload the data once, and then is it cached at all? If so, how do you detect it, or does the system do that for you so that when you call a {Vertex}Pointer() function it finds the cached data?


What do you mean by caching? If your data were uploaded to the Video RAM, then (i want to believe that) they will stay there as long I don't remove them. If you mean Post TnL caching it has nothing to do with where the data is stored. It works for system memory VAs or VBOs or VAR, as long as you are using indexed arrays.

About the uploading/caching scheme you said, sorry but i can't comment. I haven't experimented with anything like this before, so...

Hope i didn't confused you, or said anything that isn't true. If something isn't true, someone please correct me.

HellRaiZer

Share this post


Link to post
Share on other sites
Quote:
Original post by HellRaiZer
Hi...

Quote:

With VBO's, you can figure out if you've ran out of memory by detecting the OUT_OF_MEMORY error from the BufferDataARB call.


I'm not really sure that this is true. If BufferDataARB can't allocate the requested buffer on Video Memory, it will try AGP Memory and if it fails again it will try to allocate it on system memory. Don't take this too seriously though, because there is sometime since i read the specs. But i think this is the behavior for VBO at least.
With VAR it can be different because of the frequency and priority flags.

Any feedback on this?


"OUT_OF_MEMORY may be generated if the data store of a buffer object
cannot be allocated because the <size> argument of BufferDataARB is
too large."

Maybe I misinterpreted that. Now that I think about it, you're probably right. The system would probably fall back on some kind of paging scheme. Oh well, I never liked my idea in the first place anyway.

Quote:
Quote:

However, it seems risky to wait until you totally filled up the memory. Anyway, I guess you could detect that when you run out, delete the least recently used buffer, and try to upload the data again.


What happens if the 90% of the memory is allocated by textures, display lists, and other things that go to the Video/AGP memory? When you are at this situation, how can you be sure that deallocating the LRU vertex buffer will solve the problem?

What i thought for preventing situation like these, is precalculate the maximum texture/vbo/index etc. memory a certain level will use, and try to minimize the requirements "a priori" (e.g. compress textures to free some memory, use 16-bit indices where applicable, etc.). Or, if we are talking for really big levels (from the memory point of view), try to calculate the mean memory usage for some positions in it, and try to minimize this. This stands for situations where you stream data (almost) continuesly from the HD. Something like FarCry's terrain renderer (it keeps unloading and loading terrain chunks all the time, when you move fast of course (see editor)). BUT, what happens if you can't minimize memory requirements? Then you need somekind of memory manager for that. But, don't wait running out of memory to start unloading data. Because in OGL, i don't there is anyway for requesting availiable mem of any type (Vid/AGP).


Well, I meant that in a general sense. Free a texture, free a buffer, whatever is needed.
The more I think about it though, the more I'm inclined to forget about vertex arrays altogether and use VBO's for everything. OpenGL and drivers should do memory management for me with many small buffers. I shouldn't have to really worry about it.
Though, of course it's good to minimize memory requirements. But I should be able to upload everything to VRAM and let the drivers worry about the rest, right?

Quote:

What do you mean by caching?


I meant in the sense where you upload data one frame, and then come around the next frame, and the data is still cached in memory (VRAM). Yes, you are right that it should stay there. But somewhere later on you might delete the buffer for various reasons. That's kind of what I was asking, if a typical scheme consisted of managing buffers or anything uploaded to VRAM.


I was just searching around and I found some stuff in the Materials/Shaders thread. In the described effect system, they always check to see if the data is already "cached" (uploaded to the video card). If it's not, they reupload it. That's my question, why would it ever end up not being cached?

Share this post


Link to post
Share on other sites
The whole caching subject is down to the fact that you dont want to make lots of little VBOs or even one big VBO, because the driver cant manage them properly (ATI and NV both agree on this point).
Instead you can create a bunch of VBOs which are filled as need and the size taken in each one and what is allocated to it is tracked. Then, when you come to draw your model you see if its already inserted into a VBO, if it is then render away, if it isnt then upload the data to the VBO and then render.

For the most part, yes the driver will handle VBO memory management using VRAM, AGP and even System as needed, however you can help it by not allocating one huge buffer or loads and loads of little ones.

As to how big is too big and how small is too small, i asked that question in the OGL forum a while back and got some answers, although i dont recall atm what they were [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by _the_phantom_
For the most part, yes the driver will handle VBO memory management using VRAM, AGP and even System as needed, however you can help it by not allocating one huge buffer or loads and loads of little ones.


though that part is still more than confusing to me. as a simple question:

if i can easily allocate the required memory as chunks of system memory, shouldnt the driver be able to allocate the SAME memory as vbo? instead it just seems to fail when its out of video memory, forcing me to fall back to vertex arrays for the remaining few chunks (which will kill performance, because for a weird reason my indices are in video memory and _usually_ squeeze out a few percent more speed). i begin to wonder if there is an upper limit to how much memory can be managed by the driver for vbo (as a sum, not a big single chunk).

Share this post


Link to post
Share on other sites
Quote:

For the most part, yes the driver will handle VBO memory management using VRAM, AGP and even System as needed, however you can help it by not allocating one huge buffer or loads and loads of little ones.


ok, if this is true, I can understand it. It's better to handle yourself. But I assume it can kind of be a loose system, where it's a simple design that isn't bulletproof, because the drivers should be able to do some management.

Quote:

if i can easily allocate the required memory as chunks of system memory, shouldnt the driver be able to allocate the SAME memory as vbo? instead it just seems to fail when its out of video memory, forcing me to fall back to vertex arrays for the remaining few chunks (which will kill performance, because for a weird reason my indices are in video memory and _usually_ squeeze out a few percent more speed). i begin to wonder if there is an upper limit to how much memory can be managed by the driver for vbo (as a sum, not a big single chunk).


That's exactly what I'm trying to figure out. The specs don't really go into it. So you've encountered the OUT_OF_MEMORY error. I suppose there could be an upper limit to the amount of memory, though I don't see how this could be hardcoded with the variable amount of memory on different systems. Maybe it doesn't try to allocate any memory other than VRAM.

Share this post


Link to post
Share on other sites
In theory, yeah the driver should fall back from vram to agp and then system, however I guess that the implimentations still arent perfect.

The drivers do perform some management, but its hard for it to manage large cunks of ram allocated effectively and for small chunks the overhead of buffer switching and management out weights any gains you might have gotten anyway.

I'll have to do some experiments as to how much VBO space i can reserve etc at some point, if its silly for my setup then I guess its time to poke some driver dev people [smile]

Share this post


Link to post
Share on other sites
Quote:
Original post by _the_phantom_
I'll have to do some experiments as to how much VBO space i can reserve etc at some point, if its silly for my setup then I guess its time to poke some driver dev people [smile]


on my radeon about 17mb in a single chunk worked, 70mb didnt. if i allocate a bunch of chunks i can get 12 of these 17mb chunks (the remaining 4 that i would need fail). that would be about 200mb, but seeing all the textures, frame/depth/whatever buffers thats obviously where i run out of video memory. unfortunately it refuses to use anything else, even if i try a usage like stream_read.

Share this post


Link to post
Share on other sites
I guess I'll just reserve a certain amount of memory for vertices and all their attributes, and when rendering, use a kind of sliding slot system. I don't think it should be that hard to implement, and I guess it gives me more control on exactly what's going on, and lets me track what's being uploaded and when, etc.
Should be fun. Question is how much to reserve :D

edit: also, I'm think I just reserve that as one big buffer and then map into it? Seems wrong for some reason. I'll try it out.

Share this post


Link to post
Share on other sites
Quote:
Original post by _the_phantom_
yeah, I'd have thought that would fall back into AGP/system ram at that point as well... hmmm intresting...


especially since having to fall back to vertex arrays for the remaining data creates an ugly mess and the constant need to check if vbo needs to be disabled (not to mention that vbo index buffers dont play nice with va vertex data). i wouldnt mind if vbo could completely replace va and become a single solution, but with all the little issues they seem to be more complicated than one would have expected.

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
      628356
    • Total Posts
      2982253
  • 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