23

When developing an OpenGL program, is there a way to poll from the system to find out just how many megabytes are available to store textures, etc?

Or is the standard approach these days just allocate memory and forget about everything?

2 Answers 2

55

Although the official stance remains "you don't need to know, you don't want to know, and it would not help you anyway", luckily at least two IHVs have shown a little more insight lately and offer extensions to query that information:

One nice thing about these extensions is that they have a least common denominator which is just what most people need, and you don't need to query extension support or do anything special, as they both work via glGetIntegerv.

In the easiest case, you can just initialize an array of 4 integers to zero (or some minimum default value that you'll assume in case the extensions don't work), then you call glGetIntegerv twice (with GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX and TEXTURE_FREE_MEMORY_ATI, respectively), and finally call glGetError to clear the error state. glGetIntegerv does not modify the pointed-to memory if it fails, nor does it crash or any other bad thing -- it merely sets the error state to GL_INVALID_ENUM.

Both extensions return a value in the first array position, the ATI one returns some values in the other 3 too.

n.b.: glAreTexturesResident has not been supported for almost a decade on mainstream hardware, in the same manner as texture priorities. The common mantra is that the driver writer knows much better than you anyway.

5
  • I'm not using JOGL, so I wouldn't know, but there is at least one plausible explanation in the documentation for the GL2 and GL3 interfaces: "supports most of it's [sic] extensions defined at the time of this specification". That means September 2004 and August 2008, respectively, which excludes the above two extensions (March and December 2009). Thus, if you use one of these two interfaces, that would be a plausible explanation (another one being that your graphics driver really just doesn't support the extensions).
    – Damon
    Commented Dec 17, 2012 at 14:03
  • 3
    Anyway, you don't really need "support" for the extensions. As stated above, they use glGetIntegerv -- just use the constants (which are in the spec files). It will work, or it will not work. Choose a reasonable default value, and that's as good as you can get.
    – Damon
    Commented Dec 17, 2012 at 17:23
  • Yep, you were right, it worked by just passing the hex values.. thanks Damon, +1
    – elect
    Commented Dec 18, 2012 at 8:58
  • 2
    It has been a long time, but it is still working on an nVidia Titan currently. Very useful, thanks! Commented Jun 22, 2016 at 15:15
  • For Mesa @ GLX there's also GLX_MESA_query_renderer.
    – Ruslan
    Commented Dec 21, 2019 at 14:37
13

OpenGL doesn't give you this information. And frankly: There's only little benefit, simply because today we have multitasking operating systems. The OpenGL driver is responsible for swapping in texture data to/from system memory, if there's demand for it.

What OpenGL can do for you, is tell, if the textures you've uploaded are still resident in fast memory. The function is called "glAreTexturesResident". You can use this to gradually upload stuff to the GPU until you've filled up the GPU's memory. But keep in mind that you're not the only user of the GPU.

1
  • 14
    Just adding it here so that others visiting here would know: glAreTexturesResident is deprecated in the core profile, thus it isn't prudent to use them in a modern OpenGL program.
    – legends2k
    Commented Dec 24, 2013 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.