Sunday, February 13, 2011

How to get the GPU memory size and usage in OpenGL?

Tip - Use GL_NVX_gpu_memory_info for NVIDIA graphics cards and GL_ATI_meminfo for AMD/ATI cards.

Details - Few years ago, this kind of information wasn’t available to OpenGL developers but today, the two majors in OpenGL (I mean NVIDIA and AMD/ATI) have added some useful extensions to fetch graphics card memory size and other related data.

OpenGL Memory Size with NVIDIA
NVIDIA has recently published the specs of a new extension called GL_NVX_gpu_memory_info. Thanks to GL_NVX_gpu_memory_info you can retrieve the size of the total available GPU memory and the size of the current available GPU memory. It returns sizes in KB.

Here is a code snippet that shows how to use it.

#define GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX 0x9048
#define GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX 0x9049

GLint nTotalMemoryInKB = 0;
glGetIntegerv( GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX,
                       &nTotalMemoryInKB );

GLint nCurAvailMemoryInKB = 0;
glGetIntegerv( GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX,
                       &nCurAvailMemoryInKB );

GL_NVX_gpu_memory_info is available in NVIDIA display drivers since R196.xx.   

OpenGL Memory Size with AMD/ATI
AMD has released two extensions to deal with the size of video memory:
·         WGL_AMD_gpu_association
·         GL_ATI_meminfo

WGL_AMD_gpu_association is an extension developed for parallel rendering: you can create a different OpenGL context on each available GPU. This extension comes with hardware query functionalities. With WGL_AMD_gpu_association you can get the amount of graphics memory available for the GPU.

GL_ATI_meminfo is used when you need more detailed information about the available memory for VBO(vertex buffer object) or for your textures.

Here is a code snippet that shows how to use it.

GLuint uNoOfGPUs = wglGetGPUIDsAMD( 0, 0 );
GLuint* uGPUIDs = new GLuint[uNoOfGPUs];
wglGetGPUIDsAMD( uNoOfGPUs, uGPUIDs );

GLuint uTotalMemoryInMB = 0;
wglGetGPUInfoAMD( uGPUIDs[0],
                               WGL_GPU_RAM_AMD,
                               GL_UNSIGNED_INT,
                               sizeof( GLuint ),
                               &uTotalMemoryInMB );

GLint nCurAvailMemoryInKB = 0;
glGetIntegerv( GL_TEXTURE_FREE_MEMORY_ATI,
                       &nCurAvailMemoryInKB );

Reference –

Posted By : Sujith R Mohan

No comments:

Post a Comment