tags:

views:

26

answers:

1

Hi,

does anyone know how to get the free(!) vram on os x?

I know that you can query for a registry entry:

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

but this will return ALL vram, not the free vram. Under windows you can query for free VRAM using directshow

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);

Ugly, but it works. Anyone knows the osx way?

Best Wendy

A: 

answering myself so others may use this:


#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 
}

  • i am somewhat surprised that this query takes almost 3 msec ..
  • be aware that there may be more than one accelerator on your system ( eg. macbook ) so be sure you select the proper one for the query
wendy_44