Skip to content

Commit

Permalink
Add support for HotSpot Critical Natives. Close #175
Browse files Browse the repository at this point in the history
  • Loading branch information
Spasi committed Apr 24, 2016
1 parent 11d3a9e commit d06ec4a
Show file tree
Hide file tree
Showing 25 changed files with 540 additions and 107 deletions.
4 changes: 2 additions & 2 deletions modules/core/src/main/java/org/lwjgl/egl/EGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public static EGLCapabilities getCapabilities() {

private static EGLCapabilities createClientCapabilities() {
long QueryString = functionProvider.getFunctionAddress("eglQueryString");
long versionString = invokePIP(QueryString, EGL_NO_DISPLAY, EGL_VERSION);
long versionString = invokePP(QueryString, EGL_NO_DISPLAY, EGL_VERSION);

Set<String> ext = new HashSet<>(32);

Expand All @@ -158,7 +158,7 @@ private static EGLCapabilities createClientCapabilities() {
APIVersion version = apiParseVersion(memASCII(versionString), "EGL");

addEGLVersions(version.major, version.minor, ext);
addExtensions(memASCII(invokePIP(QueryString, EGL_NO_DISPLAY, EGL_EXTENSIONS)), ext);
addExtensions(memASCII(invokePP(QueryString, EGL_NO_DISPLAY, EGL_EXTENSIONS)), ext);
}

return new EGLCapabilities(functionProvider, ext);
Expand Down
4 changes: 2 additions & 2 deletions modules/core/src/main/java/org/lwjgl/openal/AL.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static ALCapabilities createCapabilities(ALCCapabilities alcCaps) {
if ( GetString == NULL || GetError == NULL || IsExtensionPresent == NULL )
throw new IllegalStateException("Core OpenAL functions could not be found. Make sure that the OpenAL library has been loaded correctly.");

long versionString = invokeIP(GetString, AL_VERSION);
long versionString = invokeP(GetString, AL_VERSION);
if ( versionString == NULL || callI(GetError) != AL_NO_ERROR )
throw new IllegalStateException("There is no OpenAL context current in the current thread or process.");

Expand All @@ -177,7 +177,7 @@ public static ALCapabilities createCapabilities(ALCCapabilities alcCaps) {
}

// Parse EXTENSIONS string
String extensionsString = memUTF8(checkPointer(invokeIP(GetString, AL_EXTENSIONS)));
String extensionsString = memUTF8(checkPointer(invokeP(GetString, AL_EXTENSIONS)));

/*
OpenALSoft: AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW
Expand Down
6 changes: 3 additions & 3 deletions modules/core/src/main/java/org/lwjgl/openal/ALC.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ public static ALCCapabilities createCapabilities(long device) {
try ( MemoryStack stack = stackPush() ) {
IntBuffer version = stack.mallocInt(1);

invokePIIPV(GetIntegerv, device, ALC_MAJOR_VERSION, 1, memAddress(version));
invokePPV(GetIntegerv, device, ALC_MAJOR_VERSION, 1, memAddress(version));
majorVersion = version.get(0);

invokePIIPV(GetIntegerv, device, ALC_MINOR_VERSION, 1, memAddress(version));
invokePPV(GetIntegerv, device, ALC_MINOR_VERSION, 1, memAddress(version));
minorVersion = version.get(0);
}

Expand All @@ -193,7 +193,7 @@ public static ALCCapabilities createCapabilities(long device) {
}

// Parse EXTENSIONS string
String extensionsString = memUTF8(checkPointer(invokePIP(GetString, device, ALC_EXTENSIONS)));
String extensionsString = memUTF8(checkPointer(invokePP(GetString, device, ALC_EXTENSIONS)));

StringTokenizer tokenizer = new StringTokenizer(extensionsString);
while ( tokenizer.hasMoreTokens() ) {
Expand Down
8 changes: 4 additions & 4 deletions modules/core/src/main/java/org/lwjgl/opencl/CL.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ private static class SharedLibraryCL extends SharedLibrary.Delegate implements F

try ( MemoryStack stack = stackPush() ) {
IntBuffer pi = stack.ints(0);
callIPPI(clGetPlatformIDs, 0, NULL, memAddress(pi));
callPPI(clGetPlatformIDs, 0, NULL, memAddress(pi));

int platforms = pi.get(0);

if ( platforms == 1 ) {
PointerBuffer pp = stack.pointers(0);

callIPPI(clGetPlatformIDs, 1, memAddress(pp), NULL);
callPPI(clGetPlatformIDs, 1, memAddress(pp), NULL);
long cl_platform_id = pp.get(0);
if ( supportsOpenCL12(stack, cl_platform_id) )
platform = cl_platform_id;
Expand All @@ -125,15 +125,15 @@ private boolean supportsOpenCL12(MemoryStack stack, long platform) {

PointerBuffer pp = stack.mallocPointer(1);

int errcode = callPIPPPI(clGetPlatformInfo, platform, CL_PLATFORM_VERSION, 0, NULL, memAddress(pp));
int errcode = callPPPPI(clGetPlatformInfo, platform, CL_PLATFORM_VERSION, 0L, NULL, memAddress(pp));
if ( errcode != CL_SUCCESS )
return false;

int bytes = (int)pp.get(0);

ByteBuffer version = stack.malloc(bytes);

errcode = callPIPPPI(clGetPlatformInfo, platform, CL_PLATFORM_VERSION, bytes, memAddress(version), NULL);
errcode = callPPPPI(clGetPlatformInfo, platform, CL_PLATFORM_VERSION, (long)bytes, memAddress(version), NULL);
if ( errcode != CL_SUCCESS )
return false;

Expand Down
20 changes: 10 additions & 10 deletions modules/core/src/main/java/org/lwjgl/opengl/GL.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,14 @@ public static GLCapabilities createCapabilities(boolean forwardCompatible) {
IntBuffer version = stack.ints(0);

// Try the 3.0+ version query first
callIPV(GetIntegerv, GL_MAJOR_VERSION, memAddress(version));
callPV(GetIntegerv, GL_MAJOR_VERSION, memAddress(version));
if ( callI(GetError) == GL_NO_ERROR && 3 <= (majorVersion = version.get(0)) ) {
// We're on an 3.0+ context.
callIPV(GetIntegerv, GL_MINOR_VERSION, memAddress(version));
callPV(GetIntegerv, GL_MINOR_VERSION, memAddress(version));
minorVersion = version.get(0);
} else {
// Fallback to the string query.
long versionString = callIP(GetString, GL_VERSION);
long versionString = callP(GetString, GL_VERSION);
if ( versionString == NULL || callI(GetError) != GL_NO_ERROR )
throw new IllegalStateException("There is no OpenGL context current in the current thread.");

Expand Down Expand Up @@ -378,7 +378,7 @@ public static GLCapabilities createCapabilities(boolean forwardCompatible) {

if ( majorVersion < 3 ) {
// Parse EXTENSIONS string
String extensionsString = memASCII(checkPointer(callIP(GetString, GL_EXTENSIONS)));
String extensionsString = memASCII(checkPointer(callP(GetString, GL_EXTENSIONS)));

StringTokenizer tokenizer = new StringTokenizer(extensionsString);
while ( tokenizer.hasMoreTokens() )
Expand All @@ -388,27 +388,27 @@ public static GLCapabilities createCapabilities(boolean forwardCompatible) {
try ( MemoryStack stack = stackPush() ) {
IntBuffer pi = stack.ints(0);

callIPV(GetIntegerv, GL_NUM_EXTENSIONS, memAddress(pi));
callPV(GetIntegerv, GL_NUM_EXTENSIONS, memAddress(pi));
int extensionCount = pi.get(0);

long GetStringi = apiGetFunctionAddress(functionProvider, "glGetStringi");
for ( int i = 0; i < extensionCount; i++ )
supportedExtensions.add(memASCII(callIIP(GetStringi, GL_EXTENSIONS, i)));
supportedExtensions.add(memASCII(callP(GetStringi, GL_EXTENSIONS, i)));

// In real drivers, we may encounter the following weird scenarios:
// - 3.1 context without GL_ARB_compatibility but with deprecated functionality exposed and working.
// - Core or forward-compatible context with GL_ARB_compatibility exposed, but not working when used.
// We ignore these and go by the spec.

// Force forwardCompatible to true if the context is a forward-compatible context.
callIPV(GetIntegerv, GL_CONTEXT_FLAGS, memAddress(pi));
callPV(GetIntegerv, GL_CONTEXT_FLAGS, memAddress(pi));
if ( (pi.get(0) & GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT) != 0 )
forwardCompatible = true;
else {
// Force forwardCompatible to true if the context is a core profile context.
if ( (3 < majorVersion || 1 <= minorVersion) ) { // OpenGL 3.1+
if ( 3 < majorVersion || 2 <= minorVersion ) { // OpenGL 3.2+
callIPV(GetIntegerv, GL_CONTEXT_PROFILE_MASK, memAddress(pi));
callPV(GetIntegerv, GL_CONTEXT_PROFILE_MASK, memAddress(pi));
if ( (pi.get(0) & GL_CONTEXT_CORE_PROFILE_BIT) != 0 )
forwardCompatible = true;
} else
Expand Down Expand Up @@ -583,10 +583,10 @@ public static GLXCapabilities createCapabilitiesGLX(long display, int screen) {

if ( screen == -1 ) {
long glXGetClientString = functionProvider.getFunctionAddress("glXGetClientString");
extensionsString = callPIP(glXGetClientString, display, GLX_EXTENSIONS);
extensionsString = callPP(glXGetClientString, display, GLX_EXTENSIONS);
} else {
long glXQueryExtensionsString = functionProvider.getFunctionAddress("glXQueryExtensionsString");
extensionsString = callPIP(glXQueryExtensionsString, display, screen);
extensionsString = callPP(glXQueryExtensionsString, display, screen);
}

StringTokenizer tokenizer = new StringTokenizer(memASCII(extensionsString));
Expand Down
12 changes: 6 additions & 6 deletions modules/core/src/main/java/org/lwjgl/opengles/GLES.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,14 @@ public static GLESCapabilities createCapabilities() {
IntBuffer pi = stack.ints(0);

// Try the 3.0+ version query first
invokeIPV(GetIntegerv, GL_MAJOR_VERSION, memAddress(pi));
invokePV(GetIntegerv, GL_MAJOR_VERSION, memAddress(pi));
if ( invokeI(GetError) == GL_NO_ERROR && 3 <= (majorVersion = pi.get(0)) ) {
// We're on an 3.0+ context.
invokeIPV(GetIntegerv, GL_MINOR_VERSION, memAddress(pi));
invokePV(GetIntegerv, GL_MINOR_VERSION, memAddress(pi));
minorVersion = pi.get(0);
} else {
// Fallback to the string query.
long versionString = invokeIP(GetString, GL_VERSION);
long versionString = invokeP(GetString, GL_VERSION);
if ( versionString == NULL || invokeI(GetError) != GL_NO_ERROR )
throw new IllegalStateException("There is no OpenGL ES context current in the current thread.");

Expand Down Expand Up @@ -255,7 +255,7 @@ public static GLESCapabilities createCapabilities() {

if ( majorVersion < 3 ) {
// Parse EXTENSIONS string
String extensionsString = memASCII(checkPointer(invokeIP(GetString, GL_EXTENSIONS)));
String extensionsString = memASCII(checkPointer(invokeP(GetString, GL_EXTENSIONS)));

StringTokenizer tokenizer = new StringTokenizer(extensionsString);
while ( tokenizer.hasMoreTokens() )
Expand All @@ -267,13 +267,13 @@ public static GLESCapabilities createCapabilities() {
try ( MemoryStack stack = stackPush() ) {
IntBuffer pi = stack.ints(0);

invokeIPV(GetIntegerv, GL_NUM_EXTENSIONS, memAddress(pi));
invokePV(GetIntegerv, GL_NUM_EXTENSIONS, memAddress(pi));
extensionCount = pi.get(0);
}

long GetStringi = apiGetFunctionAddress(functionProvider, "glGetStringi");
for ( int i = 0; i < extensionCount; i++ )
supportedExtensions.add(memASCII(checkPointer(callIIP(GetStringi, GL_EXTENSIONS, i))));
supportedExtensions.add(memASCII(checkPointer(callP(GetStringi, GL_EXTENSIONS, i))));
}

caps = new GLESCapabilities(getFunctionProvider(), supportedExtensions);
Expand Down
126 changes: 126 additions & 0 deletions modules/core/src/main/java/org/lwjgl/system/Checks.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ public static IntBuffer checkNT(IntBuffer buf) {
return buf;
}

/** Ensures that the specified array is null-terminated. */
public static int[] checkNT(int[] buf) {
checkBuffer(buf, 1);
if ( buf[buf.length - 1] != 0 )
throw new IllegalArgumentException("Missing null termination");

return buf;
}

/** Ensures that the specified IntBuffer is terminated with the specified terminator. */
public static IntBuffer checkNT(IntBuffer buf, int terminator) {
checkBuffer(buf, 1);
Expand All @@ -182,6 +191,15 @@ public static IntBuffer checkNT(IntBuffer buf, int terminator) {
return buf;
}

/** Ensures that the specified array is terminated with the specified terminator. */
public static int[] checkNT(int[] buf, int terminator) {
checkBuffer(buf, 1);
if ( buf[buf.length - 1] != terminator )
throw new IllegalArgumentException("Missing termination");

return buf;
}

/** Ensures that the specified LongBuffer is null-terminated. */
public static LongBuffer checkNT(LongBuffer buf) {
checkBuffer(buf, 1);
Expand All @@ -191,6 +209,15 @@ public static LongBuffer checkNT(LongBuffer buf) {
return buf;
}

/** Ensures that the specified array is null-terminated. */
public static long[] checkNT(long[] buf) {
checkBuffer(buf, 1);
if ( buf[buf.length - 1] != NULL )
throw new IllegalArgumentException("Missing null termination");

return buf;
}

/** Ensures that the specified FloatBuffer is null-terminated. */
public static FloatBuffer checkNT(FloatBuffer buf) {
checkBuffer(buf, 1);
Expand All @@ -200,6 +227,15 @@ public static FloatBuffer checkNT(FloatBuffer buf) {
return buf;
}

/** Ensures that the specified array is null-terminated. */
public static float[] checkNT(float[] buf) {
checkBuffer(buf, 1);
if ( buf[buf.length - 1] != 0.0f )
throw new IllegalArgumentException("Missing null termination");

return buf;
}

/** Ensures that the specified PointerBuffer is null-terminated. */
public static PointerBuffer checkNT(PointerBuffer buf) {
checkBuffer(buf, 1);
Expand All @@ -218,6 +254,76 @@ public static PointerBuffer checkNT(PointerBuffer buf, long terminator) {
return buf;
}

/**
* Helper method to ensure a array has enough capacity.
*
* @param buf the array to check
* @param size the minimum array capacity
*
* @throws IllegalArgumentException
*/
public static void checkBuffer(short[] buf, int size) {
if ( buf.length < size ) {
throwArraySizeException(buf, size);
}
}

/**
* Helper method to ensure a array has enough capacity.
*
* @param buf the array to check
* @param size the minimum array capacity
*
* @throws IllegalArgumentException
*/
public static void checkBuffer(int[] buf, int size) {
if ( buf.length < size ) {
throwArraySizeException(buf, size);
}
}

/**
* Helper method to ensure a array has enough capacity.
*
* @param buf the array to check
* @param size the minimum array capacity
*
* @throws IllegalArgumentException
*/
public static void checkBuffer(long[] buf, int size) {
if ( buf.length < size ) {
throwArraySizeException(buf, size);
}
}

/**
* Helper method to ensure a array has enough capacity.
*
* @param buf the array to check
* @param size the minimum array capacity
*
* @throws IllegalArgumentException
*/
public static void checkBuffer(float[] buf, int size) {
if ( buf.length < size ) {
throwArraySizeException(buf, size);
}
}

/**
* Helper method to ensure a array has enough capacity.
*
* @param buf the array to check
* @param size the minimum array capacity
*
* @throws IllegalArgumentException
*/
public static void checkBuffer(double[] buf, int size) {
if ( buf.length < size ) {
throwArraySizeException(buf, size);
}
}

/**
* Helper method to ensure a buffer has enough capacity.
*
Expand Down Expand Up @@ -313,6 +419,26 @@ private static void throwArraySizeException(Object[] array, int size) {
throw new IllegalArgumentException("Number of array elements is " + array.length + ", must be at least " + size);
}

private static void throwArraySizeException(short[] array, int size) {
throw new IllegalArgumentException("Number of array elements is " + array.length + ", must be at least " + size);
}

private static void throwArraySizeException(int[] array, int size) {
throw new IllegalArgumentException("Number of array elements is " + array.length + ", must be at least " + size);
}

private static void throwArraySizeException(long[] array, int size) {
throw new IllegalArgumentException("Number of array elements is " + array.length + ", must be at least " + size);
}

private static void throwArraySizeException(float[] array, int size) {
throw new IllegalArgumentException("Number of array elements is " + array.length + ", must be at least " + size);
}

private static void throwArraySizeException(double[] array, int size) {
throw new IllegalArgumentException("Number of array elements is " + array.length + ", must be at least " + size);
}

private static void throwBufferSizeGTException(Buffer buf, int size) {
throw new IllegalArgumentException("Number of remaining buffer elements is " + buf.remaining() + ", must be at most " + size + ".");
}
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/test/java/org/lwjgl/demo/glfw/Gears.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void init() {
long objc_msgSend = ObjCRuntime.getLibrary().getFunctionAddress("objc_msgSend");
long contentView = invokePPP(objc_msgSend, cocoaWindow, sel_getUid("contentView"));

invokePPZV(objc_msgSend, contentView, sel_getUid("setWantsBestResolutionOpenGLSurface:"), false);
invokePPV(objc_msgSend, contentView, sel_getUid("setWantsBestResolutionOpenGLSurface:"), false);

boolean bool = invokePPZ(objc_msgSend, contentView, sel_getUid("wantsBestResolutionOpenGLSurface"));
System.out.println("wantsBestResolutionOpenGLSurface = " + bool);
Expand Down
Loading

0 comments on commit d06ec4a

Please sign in to comment.