Search in sources :

Example 1 with HGLRCByReference

use of com.sun.jna.platform.win32.WinDef.HGLRCByReference in project jna by java-native-access.

the class OpenGL32Util method countGpusNV.

/**
     * Count GPUs
     * @return the number of available GPUs
     */
public static int countGpusNV() {
    // create a dummy window
    HWND hWnd = User32Util.createWindow("Message", null, 0, 0, 0, 0, 0, null, null, null, null);
    HDC hdc = User32.INSTANCE.GetDC(hWnd);
    // set a compatible pixel format
    PIXELFORMATDESCRIPTOR.ByReference pfd = new PIXELFORMATDESCRIPTOR.ByReference();
    pfd.nVersion = 1;
    pfd.dwFlags = WinGDI.PFD_DRAW_TO_WINDOW | WinGDI.PFD_SUPPORT_OPENGL | WinGDI.PFD_DOUBLEBUFFER;
    pfd.iPixelType = WinGDI.PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = WinGDI.PFD_MAIN_PLANE;
    GDI32.INSTANCE.SetPixelFormat(hdc, GDI32.INSTANCE.ChoosePixelFormat(hdc, pfd), pfd);
    // create the OpenGL context to get function address
    WinDef.HGLRC hGLRC = OpenGL32.INSTANCE.wglCreateContext(hdc);
    OpenGL32.INSTANCE.wglMakeCurrent(hdc, hGLRC);
    Pointer funcPointer = OpenGL32.INSTANCE.wglGetProcAddress("wglEnumGpusNV");
    Function fncEnumGpusNV = (funcPointer == null) ? null : Function.getFunction(funcPointer);
    OpenGL32.INSTANCE.wglDeleteContext(hGLRC);
    // destroy the window
    User32.INSTANCE.ReleaseDC(hWnd, hdc);
    User32Util.destroyWindow(hWnd);
    // abort if the nVidia extensions are not present
    if (fncEnumGpusNV == null)
        return 0;
    // enumerate nVidia adapters
    HGLRCByReference hGPU = new HGLRCByReference();
    for (int i = 0; i < 16; i++) {
        Boolean ok = (Boolean) fncEnumGpusNV.invoke(Boolean.class, new Object[] { Integer.valueOf(i), hGPU });
        if (!ok.booleanValue())
            return i;
    }
    return 0;
}
Also used : HDC(com.sun.jna.platform.win32.WinDef.HDC) HWND(com.sun.jna.platform.win32.WinDef.HWND) PIXELFORMATDESCRIPTOR(com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR) Pointer(com.sun.jna.Pointer) HGLRCByReference(com.sun.jna.platform.win32.WinDef.HGLRCByReference) Function(com.sun.jna.Function) HGLRCByReference(com.sun.jna.platform.win32.WinDef.HGLRCByReference)

Aggregations

Function (com.sun.jna.Function)1 Pointer (com.sun.jna.Pointer)1 HDC (com.sun.jna.platform.win32.WinDef.HDC)1 HGLRCByReference (com.sun.jna.platform.win32.WinDef.HGLRCByReference)1 HWND (com.sun.jna.platform.win32.WinDef.HWND)1 PIXELFORMATDESCRIPTOR (com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR)1