Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Windows OpenGL Functions #271

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,389 changes: 695 additions & 694 deletions CHANGES.md

Large diffs are not rendered by default.

662 changes: 346 additions & 316 deletions contrib/platform/src/com/sun/jna/platform/win32/GDI32.java

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/OpenGL32.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* Copyright (c) 2011 Timothy Wall, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.win32;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HDC;
import com.sun.jna.platform.win32.WinOpenGL.HGLRC;
import com.sun.jna.win32.StdCallLibrary;

/**
* opengl32.dll Interface.
*/
public interface OpenGL32 extends StdCallLibrary {
OpenGL32 INSTANCE = (OpenGL32) Native.loadLibrary("opengl32", OpenGL32.class);

/**
* The glGetString function returns a string describing the current OpenGL connection.
*
* @param name
* One of the following symbolic constants.
* @return The glGetString function returns a pointer to a static string describing some aspect of the current OpenGL connection.
*/
public String glGetString(int name);

/**
* The wglCreateContext function creates a new OpenGL rendering context, which is suitable for drawing on the device
* referenced by hdc. The rendering context has the same pixel format as the device context.
*
* @param hdc
* Handle to a device context for which the function creates a suitable OpenGL rendering context.
* @return handle to an OpenGL rendering context
*/
public HGLRC wglCreateContext(HDC windowDC);

/**
* The wglGetCurrentContext function obtains a handle to the current OpenGL rendering context of the calling thread.
*
* @return If the calling thread has a current OpenGL rendering context, wglGetCurrentContext returns a
* handle to that rendering context. Otherwise, the return value is NULL.
*/
public HGLRC wglGetCurrentContext();

/**
* The wglMakeCurrent function makes a specified OpenGL rendering context the calling thread's current rendering
* context. All subsequent OpenGL calls made by the thread are drawn on the device identified by hdc.
*
* @param hdc
* Handle to a device context. Subsequent OpenGL calls made by the calling thread are drawn on the
* device identified by hdc.
* @param hglrc
* Handle to an OpenGL rendering context that the function sets as the calling thread's rendering context.
* @return true if successful
*/
public boolean wglMakeCurrent(HDC windowDC, HGLRC hglrc);

/**
* The wglDeleteContext function deletes a specified OpenGL rendering context.
*
* @param hglrc
* Handle to an OpenGL rendering context that the function will delete.
* @return true if successful
*/
public boolean wglDeleteContext(HGLRC hglrc);

/**
* The wglGetProcAddress function returns the address of an OpenGL extension function for use with the
* current OpenGL rendering context.
*
* @param lpszProc
* Points to a null-terminated string that is the name of the extension function.
* The name of the extension function must be identical to a corresponding function implemented by OpenGL.
* @return When the function succeeds, the return value is the address of the extension function.
*/
public Pointer wglGetProcAddress(String lpszProc);
}
81 changes: 81 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/OpenGL32Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* Copyright (c) 2011 Timothy Wall, All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package com.sun.jna.platform.win32;

import com.sun.jna.Function;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HDC;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinGDI.PIXELFORMATDESCRIPTOR;
import com.sun.jna.platform.win32.WinOpenGL.HGLRC;
import com.sun.jna.platform.win32.WinOpenGL.HGPUNVByReference;

/**
* opengl32 utility API.
*/
public abstract class OpenGL32Util {

/**
* Return a procedure function pointer
* @param procName the procedure name
* @return the function
*/
public static Function wglGetProcAddress(String procName) {
Pointer funcPointer = OpenGL32.INSTANCE.wglGetProcAddress("wglEnumGpusNV");
return (funcPointer == null) ? null : Function.getFunction(funcPointer);
}

/**
* Return a RAS connection by name
* @param connName the connection name
* @return the RAS connection structure
*/
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
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
HGPUNVByReference hGPU = new HGPUNVByReference();
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;
}
}
Loading