OS X上的免费VRam

时间:2010-09-23 22:44:35

标签: macos gpu

有谁知道如何在os x上获得免费(!)vram?

我知道您可以查询注册表项:

typeCode = IORegistryEntrySearchCFProperty(dspPort,kIOServicePlane,CFSTR(kIOFBMemorySizeKey),
                                           kCFAllocatorDefault,
                                           kIORegistryIterateRecursively | kIORegistryIterateParents);

但是这将返回ALL vram,而不是free vram。在Windows下,您可以使用directshow

查询免费VRAM
mDDrawResult = DirectDrawCreate(NULL, &mDDraw, NULL);
mDDrawResult = mDDraw->QueryInterface(IID_IDirectDraw2, (LPVOID *)&mDDraw2);
DDSCAPS ddscaps;
DWORD   totalmem, freemem;
ddscaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
mDDrawResult = mDDraw2->GetAvailableVidMem(&ddscaps, &totalmem, &freemem);

丑陋,但它确实有效。有谁知道osx的方式?

最佳

温迪

1 个答案:

答案 0 :(得分:2)

回答自己,以便其他人可以使用它:


#include <IOKit/graphics/IOGraphicsLib.h>
size_t currentFreeVRAM()
{

    kern_return_t krc;  
    mach_port_t masterPort;
    krc = IOMasterPort(bootstrap_port, &masterPort);
    if (krc == KERN_SUCCESS) 
    {
        CFMutableDictionaryRef pattern = IOServiceMatching(kIOAcceleratorClassName);
        //CFShow(pattern);

        io_iterator_t deviceIterator;
        krc = IOServiceGetMatchingServices(masterPort, pattern, &deviceIterator);
        if (krc == KERN_SUCCESS) 
        {
            io_object_t object;
            while ((object = IOIteratorNext(deviceIterator))) 
            {
                CFMutableDictionaryRef properties = NULL;
                krc = IORegistryEntryCreateCFProperties(object, &properties, kCFAllocatorDefault, (IOOptionBits)0);
                if (krc == KERN_SUCCESS) 
                {
                    CFMutableDictionaryRef perf_properties = (CFMutableDictionaryRef) CFDictionaryGetValue( properties, CFSTR("PerformanceStatistics") );
                    //CFShow(perf_properties);

                    // look for a number of keys (this is mostly reverse engineering and best-guess effort)
                    const void* free_vram_number = CFDictionaryGetValue(perf_properties, CFSTR("vramFreeBytes"));
                    if (free_vram_number) 
                    {
                        ssize_t vramFreeBytes;
                        CFNumberGetValue( (CFNumberRef) free_vram_number, kCFNumberSInt64Type, &vramFreeBytes);
                        return vramFreeBytes;
                    }
                }
                if (properties) CFRelease(properties);
                IOObjectRelease(object);
            }           
            IOObjectRelease(deviceIterator);
        }
    }
    return 0; // when we come here, this is a fail 
}

  • 我有点惊讶这个查询花了将近3毫秒..
  • 请注意您的系统上可能有多个加速器(例如,macbook) 所以一定要为查询选择合适的一个