1

I'm trying to get the total amount of GPU memory from my video card using native Qt's OpenGL, I have tried hundred of methods, but none do work. This is what I have at the moment:

  QOpenGLContext context;
  context.create();
  QOffscreenSurface surface;
  surface.setFormat(context.format());
  surface.create();
  QOpenGLFunctions func;
  context.makeCurrent(&surface);
  func.initializeOpenGLFunctions();
  GLint total_mem_kb = 0;
  func.glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX,&total_mem_kb);
  qDebug()<<total_mem_kb;

The problem is that the variable total_mem_kb is always 0, It does not get the value inside of glGetIntegerv. By running this code I get 0. What can be the problem? Can you please give me a hint?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Mr. Hello_world
  • 85
  • 2
  • 10
  • Possible duplicate of [Qt 5.5 and OpenGL: Retrieving device info](http://stackoverflow.com/questions/32386770/qt-5-5-and-opengl-retrieving-device-info) – vallentin Apr 07 '17 at 00:06
  • @Vallentin, I have seen that one, it's not the same. The glGetString works fine for me, but the code above does not work the way it should. – Mr. Hello_world Apr 07 '17 at 00:11
  • Could you be more specific with "not working". Note that `GL_NVX_gpu_memory_info` is an Nvidia extension. So if your graphics card is anything but Nvidia, the chances of supporting this extension are slim. – vallentin Apr 07 '17 at 00:13
  • @Vallentin, "not working" - does not init the variable total_mem_kb inside of the glGetIntegerv function. Nvidia Drivers are updated an hour ago, I just don't know why this is "not working" – Mr. Hello_world Apr 07 '17 at 00:17

2 Answers2

3

First an foremost check if the NVX_gpu_memory_info extension is supported.

Note that the extension requires OpenGL 2.0 at least.

GLint count;
glGetIntegerv(GL_NUM_EXTENSIONS, &count);

for (GLint i = 0; i < count; ++i)
{
    const char *extension = (const char*)glGetStringi(GL_EXTENSIONS, i);
    if (!strcmp(extension, "GL_NVX_gpu_memory_info"))
        printf("%d: %s\n", i, extension);
}

I know you just said that you have an Nvidia graphics card, but this doesn't by default guarantee support. Additionally if you have an integrated graphics card then make sure you are actually using your dedicated graphics card.

If you have an Nvidia GeForce graphics card, then then the following should result in something along the lines of "Nvidia" and "GeForce".

glGetString(GL_VENDOR);
glGetString(GL_RENDERER);

If it returns anything but "Nvidia" then you need to open your Nvidia Control Panel and set the preferred graphics card to your Nvidia graphics card.


After you've verified it being the Nvidia graphics card and that the extension is supported. Then you can try getting the total and current available memory:

GLint totalMemoryKb = 0;
glGetIntegerv(GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX, &totalMemoryKb);

GLint currentMemoryKb = 0;
glGetIntegerv(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &currentMemoryKb);

I would also like to point out that the NVX_gpu_memory_info extension defines it as:

GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX

and not

GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX

Note the MEMORY vs MEM difference.

So suspecting you've defined GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX yourself or leveraging something else that has defined it. That tells it could be wrongly defined or referring to something else.

vallentin
  • 23,478
  • 6
  • 59
  • 81
  • glGetString(GL_VENDOR) = the result is "Intel". Why it says Intel? – Mr. Hello_world Apr 07 '17 at 00:59
  • 2
    Then that tells that you are using your integrated graphics card and not your dedicated Nvidia graphics card. – vallentin Apr 07 '17 at 01:00
  • But how I can get the vide card info for Nvidia? I have already switched GPU, now Qt is being run on Nvidia card and not Intel, and the result is the same. – Mr. Hello_world Apr 07 '17 at 01:02
  • Was that a question? or did you just say `GL_VENDOR` is now saying "Nvidia" ? – vallentin Apr 07 '17 at 01:05
  • Sorry just a little bit stressed because spend lots of time and nothing is working. I switched qt to run on Nvidia, but it still says Intel and the RENDERER return an empty string. The question: How I can get the info about my Nvidia card? I thought that the functions above will return the info about my Nvidia card ignoring that the application is running on an Intel video card – Mr. Hello_world Apr 07 '17 at 01:09
  • 2
    No, each function call and extension is in relation to the GPU the context is working with. To make so that your Nvidia graphics card is chosen. Open your Nvidia Control Panel and set the preferred graphics card to your Nvidia graphics card. – vallentin Apr 07 '17 at 01:12
0

I use the following:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
LONG __stdcall glxGpuTotalMemory()
{
    GLint total_mem_kb = 0;
    glGetIntegerv(GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX, &total_mem_kb);

    if (total_mem_kb == 0 && wglGetGPUIDsAMD)
    {
        UINT n = wglGetGPUIDsAMD(0, 0);
        UINT *ids = new UINT[n];
        size_t total_mem_mb = 0;
        wglGetGPUIDsAMD(n, ids);
        wglGetGPUInfoAMD(ids[0], WGL_GPU_RAM_AMD, GL_UNSIGNED_INT, sizeof(size_t), &total_mem_mb);
        total_mem_kb = total_mem_mb * 1024;
    }

    return total_mem_kb;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////   
LONG __stdcall glxGpuAvailMemory()
{
    GLint cur_avail_mem_kb = 0;
    glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX, &cur_avail_mem_kb);

    if (cur_avail_mem_kb == 0 && wglGetGPUIDsAMD)
    {
        glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI, &cur_avail_mem_kb);
    }

    return cur_avail_mem_kb;
}
Elias
  • 452
  • 5
  • 11