Listing supported extensions

Started by oparisy, April 26, 2014, 09:53:00

Previous topic - Next topic

oparisy

Hi,

probably a FAQ, but I could not find any informations about this: is there a way to enumerate all ARB extensions supported by the system running LWJGL?

As I understand it, the usual way to check for extensions is to test flags exposed through GLContext.getCapabilities().

Nice when a given extension is tested for, but daunting if I need to test for all flags :)

Any hint?

Regards, Olivier.

Cornix

I once wrote this class for my own personal use, you might find it useful for your needs as well:
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.lwjgl.opengl.*;

public class GLVersion {
	
	private static GLVersion instance;
	
	public static int getMajorVersion() {
		return get().majorVersion;
	}
	
	public static int getMinorVersion() {
		return get().minorVersion;
	}
	
	public static double getVersion() {
		return get().version;
	}
	
	public static String getVersionString() {
		return get().versionString;
	}
	
	public static boolean isExtensionSupported(GLExtension extension) {
		return isExtensionSupported(extension.asString());
	}
	
	public static boolean isExtensionSupported(String extension) {
		return get().extensionMap.containsKey(extension);
	}
	
	public static Collection<String> getSupportedExtensions() {
		return get().extensionMap.keySet();
	}
	
	private static GLVersion get() {
		if (instance == null) {
			instance = new GLVersion();
		}
		return instance;
	}
	
	private final String versionString;
	private final int majorVersion;
	private final int minorVersion;
	private final double version;
	private final Map<String, Boolean> extensionMap;
	
	private GLVersion() {
		versionString = GL11.glGetString(GL11.GL_VERSION);
		
		int majorVersionIndex = versionString.indexOf('.');
		int minorVersionIndex = majorVersionIndex + 1;
		while (minorVersionIndex < versionString.length() && Character.isDigit(minorVersionIndex)) {
			minorVersionIndex++;
		}
		minorVersionIndex++;
		
		majorVersion = Integer.parseInt(versionString.substring(0, majorVersionIndex));
		minorVersion = Integer.parseInt(versionString.substring(majorVersionIndex + 1, minorVersionIndex));
		version = Double.parseDouble(versionString.substring(0, minorVersionIndex));
		
		String[] supportedExtensions;
		if (majorVersion >= 3) {
			int numExtensions = GL11.glGetInteger(GL30.GL_NUM_EXTENSIONS);
			supportedExtensions = new String[numExtensions];
			for (int i = 0; i < numExtensions; i++) {
				supportedExtensions[i] = GL30.glGetStringi(GL11.GL_EXTENSIONS, i);
			}
		} else {
			String extensionsAsString = GL11.glGetString(GL11.GL_EXTENSIONS);
			supportedExtensions = extensionsAsString.split(" ");
		}
		
		extensionMap = new HashMap<>();
		for (String extension : supportedExtensions) {
			extensionMap.put(extension, Boolean.TRUE);
		}
	}
	
}

It offers static methods to check for the major / minor version, and to check what kinds of extensions are supported.
I also used an enum GLExtensions which would contain several extensions I often used to make it more safe against typos.

Credits are not needed.

oparisy

OK, so I need GL11.GL_EXTENSIONS, which is easier to use since OpenGL 3 due to GL30.GL_NUM_EXTENSIONS availability.

Thanks for sharing your code, this is much appreciated.

Regards.