openGL获取NVIDIA显卡和AMD显卡的可用内存和总内存

windows和linux下通过openGL获取NVIDIA和AMD显卡的可用内存和总内存,使用到了glew库https://github.com/nigels-com/glew

代码如下
#ifdef _WIN32
#include 
#include 
#include 
#include   
#elif  defined(__x86_64__) || defined(__ppc64__)
#include 
#include 
#endif

//NVIDIA 可用内存
glGetIntegerv(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX,¤tlyAvailableMemoryInKb);

//NVIDIA 总共内存
glGetIntegerv(GL_GPU_MEMORY_INFO_TOTAL_AVAILABLE_MEMORY_NVX,&totalAvailableMemoryInKb);

//AMD 可用内存
glGetIntegerv(GL_TEXTURE_FREE_MEMORY_ATI,&nCurAvailMemoryInKB);

//AMD 总共内存(需要平台判定下)
#ifdef _WIN32
//windwos下的
        typedef UINT(WINAPI * PFNWGLGETGPUIDSAMDPROC) (UINT maxCount, UINT* ids);
        typedef INT(WINAPI * PFNWGLGETGPUINFOAMDPROC) (UINT id, INT property, GLenum dataType, UINT size, void* data);
        PFNWGLGETGPUIDSAMDPROC wglGetGPUIDsAMD = (PFNWGLGETGPUIDSAMDPROC)wglGetProcAddress("wglGetGPUIDsAMD");
        PFNWGLGETGPUINFOAMDPROC wglGetGPUInfoAMD = (PFNWGLGETGPUINFOAMDPROC)wglGetProcAddress("wglGetGPUInfoAMD");
        if (!wglGetGPUIDsAMD || !wglGetGPUInfoAMD)
        {
            return 0;
        }
        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);
        return uTotalMemoryInMB * 1024;
#else
//linux下的
        GLuint uNoOfGPUs = glXGetGPUIDsAMD(0, 0);
        GLuint* uGPUIDs = new GLuint[uNoOfGPUs];
        glXGetGPUIDsAMD(uNoOfGPUs, uGPUIDs);
        GLuint uTotalMemoryInMB = 0;
        glXGetGPUInfoAMD(uGPUIDs[0],
            GLX_GPU_RAM_AMD,
            GL_UNSIGNED_INT,
            sizeof(GLuint),
            &uTotalMemoryInMB);
        return uTotalMemoryInMB * 1024;
#endif

你可能感兴趣的:(openGL获取NVIDIA显卡和AMD显卡的可用内存和总内存)