supported_extensions) {
// Detect OpenGL version first
final String version = glGetString(GL_VERSION);
if ( version == null )
throw new IllegalStateException("glGetString(GL_VERSION) returned null - possibly caused by missing current context.");
final StringTokenizer version_tokenizer = new StringTokenizer(version, ". ");
final String major_string = version_tokenizer.nextToken();
final String minor_string = version_tokenizer.nextToken();
int majorVersion = 0;
int minorVersion = 0;
try {
majorVersion = Integer.parseInt(major_string);
minorVersion = Integer.parseInt(minor_string);
} catch (NumberFormatException e) {
LWJGLUtil.log("The major and/or minor OpenGL version is malformed: " + e.getMessage());
}
final int[][] GL_VERSIONS = {
{ 1, 2, 3, 4, 5 }, // OpenGL 1
{ 0, 1 }, // OpenGL 2
{ 0, 1, 2, 3 }, // OpenGL 3
{ 0, 1, 2, 3, 4, 5 }, // OpenGL 4
};
for ( int major = 1; major <= GL_VERSIONS.length; major++ ) {
int[] minors = GL_VERSIONS[major - 1];
for ( int minor : minors ) {
if ( major < majorVersion || (major == majorVersion && minor <= minorVersion) )
supported_extensions.add("OpenGL" + Integer.toString(major) + Integer.toString(minor));
}
}
int profileMask = 0;
if ( majorVersion < 3 ) {
// Parse EXTENSIONS string
final String extensions_string = glGetString(GL_EXTENSIONS);
if ( extensions_string == null )
throw new IllegalStateException("glGetString(GL_EXTENSIONS) returned null - is there a context current?");
final StringTokenizer tokenizer = new StringTokenizer(extensions_string);
while ( tokenizer.hasMoreTokens() )
supported_extensions.add(tokenizer.nextToken());
} else {
// Use forward compatible indexed EXTENSIONS
final int extensionCount = glGetInteger(GL_NUM_EXTENSIONS);
for ( int i = 0; i < extensionCount; i++ )
supported_extensions.add(glGetStringi(GL_EXTENSIONS, i));
// Get the context profile mask for versions >= 3.2
if ( 3 < majorVersion || 2 <= minorVersion ) {
Util.checkGLError(); // Make sure we have no errors up to this point
try {
profileMask = glGetInteger(GL_CONTEXT_PROFILE_MASK);
// Retrieving GL_CONTEXT_PROFILE_MASK may generate an INVALID_OPERATION error on certain implementations, ignore.
// Happens on pre10.1 ATI drivers, when ContextAttribs.withProfileCompatibility is not used
Util.checkGLError();
} catch (OpenGLException e) {
LWJGLUtil.log("Failed to retrieve CONTEXT_PROFILE_MASK");
}
}
}
return profileMask;
}
/**
* Helper method to ContextCapabilities. It will try to initialize the native stubs,
* and remove the given extension name from the extension set if the initialization fails.
*/
static void initNativeStubs(final Class> extension_class, Set supported_extensions, String ext_name) {
resetNativeStubs(extension_class);
if (supported_extensions.contains(ext_name)) {
try {
Method init_stubs_method = extension_class.getDeclaredMethod("initNativeStubs");
init_stubs_method.invoke(null);
} catch (Exception e) {
LWJGLUtil.logger().log(() -> "Failed to initialize extension " + extension_class + " - exception", e);
supported_extensions.remove(ext_name);
}
}
}
/**
* Makes a GL context the current LWJGL context by loading GL function pointers. The context must be current before a call to
* this method! Instead it simply ensures that the current context is reflected accurately by GLContext's extension caps and
* function pointers. Use useContext(null) when no context is active. If the context is the same as last time, then this is
* a no-op.
If the context has not been encountered before it will be fully initialized from scratch. Otherwise a cached set
* of caps and function pointers will be used.
The reference to the context is held in a weak reference; therefore if no
* strong reference exists to the GL context it will automatically be forgotten by the VM at an indeterminate point in the
* future, freeing up a little RAM.
*
* @param context The context object, which uniquely identifies a GL context. If context is null, the native stubs are
* unloaded.
*
* @throws LWJGLException if context non-null, and the gl library can't be loaded or the basic GL11 functions can't be loaded
*/
public static synchronized void useContext(Object context) throws LWJGLException {
useContext(context, false);
}
/**
* Makes a GL context the current LWJGL context by loading GL function pointers. The context must be current before a call to
* this method! Instead it simply ensures that the current context is reflected accurately by GLContext's extension caps and
* function pointers. Use useContext(null) when no context is active.
If the context is the same as last time, then this is
* a no-op.
If the context has not been encountered before it will be fully initialized from scratch. Otherwise a cached set
* of caps and function pointers will be used.
The reference to the context is held in a weak reference; therefore if no
* strong reference exists to the GL context it will automatically be forgotten by the VM at an indeterminate point in the
* future, freeing up a little RAM.
*
If forwardCompatible is true, function pointers of deprecated GL11-GL21 functionality will not be loaded. Calling a deprecated
* function using the specified context will result in an IllegalStateException
.
*
* @param context The context object, which uniquely identifies a GL context. If context is null, the native stubs are
* unloaded.
* @param forwardCompatible If the context is a forward compatible context (does not expose deprecated functionality, see XGL_ARB_create_context)
*
* @throws LWJGLException if context non-null, and the gl library can't be loaded or the basic GL11 functions can't be loaded
*/
public static synchronized void useContext(Object context, boolean forwardCompatible) throws LWJGLException {
if ( context == null ) {
ContextCapabilities.unloadAllStubs();
setCapabilities(null);
if ( did_auto_load )
unloadOpenGLLibrary();
return;
}
if ( gl_ref_count == 0 ) {
loadOpenGLLibrary();
did_auto_load = true;
}
try {
ContextCapabilities capabilities = capability_cache.get(context);
if ( capabilities == null ) {
/*
* The capabilities object registers itself as current. This behaviour is caused
* by a chicken-and-egg situation where the constructor needs to call GL functions
* as part of its capability discovery, but GL functions cannot be called before
* a capabilities object has been set.
*/
new ContextCapabilities(forwardCompatible);
capability_cache.put(context, getCapabilities());
} else
setCapabilities(capabilities);
} catch (LWJGLException e) {
if ( did_auto_load )
unloadOpenGLLibrary();
throw e;
}
}
/** If the OpenGL reference count is 0, the library is loaded. The reference count is then incremented. */
public static synchronized void loadOpenGLLibrary() throws LWJGLException {
if ( gl_ref_count == 0 )
nLoadOpenGLLibrary();
gl_ref_count++;
}
private static native void nLoadOpenGLLibrary() throws LWJGLException;
/** The OpenGL library reference count is decremented, and if it reaches 0, the library is unloaded. */
public static synchronized void unloadOpenGLLibrary() {
gl_ref_count--;
/*
* Unload the native OpenGL library unless we're on linux, since
* some drivers (NVIDIA proprietary) crash on exit when unloading the library.
*/
if ( gl_ref_count == 0 && LWJGLUtil.getPlatform() != LWJGLUtil.PLATFORM_LINUX )
nUnloadOpenGLLibrary();
}
private static native void nUnloadOpenGLLibrary();
/** Native method to clear native stub bindings */
static native void resetNativeStubs(Class clazz);
private static final class CapabilitiesCacheEntry {
Thread owner;
ContextCapabilities capabilities;
}
}