|
| 1 | +/* Copyright (c) 2011 Timothy Wall, All Rights Reserved |
| 2 | + * |
| 3 | + * This library is free software; you can redistribute it and/or |
| 4 | + * modify it under the terms of the GNU Lesser General Public |
| 5 | + * License as published by the Free Software Foundation; either |
| 6 | + * version 2.1 of the License, or (at your option) any later version. |
| 7 | + * |
| 8 | + * This library is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | + * Lesser General Public License for more details. |
| 12 | + */ |
| 13 | +package com.sun.jna.platform.win32; |
| 14 | + |
| 15 | +import com.sun.jna.Function; |
| 16 | +import com.sun.jna.Pointer; |
| 17 | +import com.sun.jna.platform.win32.WinDef.HDC; |
| 18 | +import com.sun.jna.platform.win32.WinDef.HGLRCByReference; |
| 19 | +import com.sun.jna.platform.win32.WinDef.HWND; |
| 20 | +import com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR; |
| 21 | + |
| 22 | +/** |
| 23 | + * opengl32 utility API. |
| 24 | + */ |
| 25 | +public abstract class OpenGL32Util { |
| 26 | + |
| 27 | + /** |
| 28 | + * Return a procedure function pointer |
| 29 | + * @param procName the procedure name |
| 30 | + * @return the function |
| 31 | + */ |
| 32 | + public static Function wglGetProcAddress(String procName) { |
| 33 | + Pointer funcPointer = OpenGL32.INSTANCE.wglGetProcAddress("wglEnumGpusNV"); |
| 34 | + return (funcPointer == null) ? null : Function.getFunction(funcPointer); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Return a RAS connection by name |
| 39 | + * @param connName the connection name |
| 40 | + * @return the RAS connection structure |
| 41 | + */ |
| 42 | + public static int countGpusNV() { |
| 43 | + // create a dummy window |
| 44 | + HWND hWnd = User32Util.createWindow("Message", null, 0, 0, 0, 0, 0, null, null, null, null); |
| 45 | + HDC hdc = User32.INSTANCE.GetDC(hWnd); |
| 46 | + |
| 47 | + // set a compatible pixel format |
| 48 | + PIXELFORMATDESCRIPTOR.ByReference pfd = new PIXELFORMATDESCRIPTOR.ByReference(); |
| 49 | + pfd.nVersion = 1; |
| 50 | + pfd.dwFlags = WinGDI.PFD_DRAW_TO_WINDOW | WinGDI.PFD_SUPPORT_OPENGL | WinGDI.PFD_DOUBLEBUFFER; |
| 51 | + pfd.iPixelType = WinGDI.PFD_TYPE_RGBA; |
| 52 | + pfd.cColorBits = 24; |
| 53 | + pfd.cDepthBits = 16; |
| 54 | + pfd.iLayerType = WinGDI.PFD_MAIN_PLANE; |
| 55 | + GDI32.INSTANCE.SetPixelFormat(hdc, GDI32.INSTANCE.ChoosePixelFormat(hdc, pfd), pfd); |
| 56 | + |
| 57 | + // create the OpenGL context to get function address |
| 58 | + WinDef.HGLRC hGLRC = OpenGL32.INSTANCE.wglCreateContext(hdc); |
| 59 | + OpenGL32.INSTANCE.wglMakeCurrent(hdc, hGLRC); |
| 60 | + Pointer funcPointer = OpenGL32.INSTANCE.wglGetProcAddress("wglEnumGpusNV"); |
| 61 | + Function fncEnumGpusNV = (funcPointer == null) ? null : Function.getFunction(funcPointer); |
| 62 | + OpenGL32.INSTANCE.wglDeleteContext(hGLRC); |
| 63 | + |
| 64 | + // destroy the window |
| 65 | + User32.INSTANCE.ReleaseDC(hWnd, hdc); |
| 66 | + User32Util.destroyWindow(hWnd); |
| 67 | + |
| 68 | + // abort if the nVidia extensions are not present |
| 69 | + if (fncEnumGpusNV == null) return 0; |
| 70 | + |
| 71 | + // enumerate nVidia adapters |
| 72 | + HGLRCByReference hGPU = new HGLRCByReference(); |
| 73 | + for (int i = 0; i < 16; i++) { |
| 74 | + Boolean ok = (Boolean) fncEnumGpusNV.invoke(Boolean.class, new Object[] { Integer.valueOf(i), hGPU, }); |
| 75 | + if (!ok.booleanValue()) return i; |
| 76 | + } |
| 77 | + |
| 78 | + return 0; |
| 79 | + } |
| 80 | +} |
0 commit comments