From f80e5a94d603a0f36eb665f1c78cde01142c6c2a Mon Sep 17 00:00:00 2001 From: Ioannis Tsakpinis Date: Fri, 1 Oct 2010 22:20:14 +0000 Subject: [PATCH] OpenCL bug fixes on MacOS. Improved 64bit pointer detection. --- .../org/lwjgl/DefaultSysImplementation.java | 1 + src/java/org/lwjgl/Sys.java | 9 ++--- src/java/org/lwjgl/SysImplementation.java | 5 +++ src/java/org/lwjgl/opencl/APIUtil.java | 8 ++-- src/java/org/lwjgl/opencl/CallbackUtil.java | 6 ++- .../org/lwjgl/test/opencl/HelloOpenCL.java | 40 ++++++++++--------- src/native/common/common_tools.c | 4 ++ 7 files changed, 45 insertions(+), 28 deletions(-) diff --git a/src/java/org/lwjgl/DefaultSysImplementation.java b/src/java/org/lwjgl/DefaultSysImplementation.java index 265b0e13..808de5dd 100644 --- a/src/java/org/lwjgl/DefaultSysImplementation.java +++ b/src/java/org/lwjgl/DefaultSysImplementation.java @@ -40,6 +40,7 @@ package org.lwjgl; */ abstract class DefaultSysImplementation implements SysImplementation { public native int getJNIVersion(); + public native int getPointerSize(); public native void setDebug(boolean debug); public long getTimerResolution() { diff --git a/src/java/org/lwjgl/Sys.java b/src/java/org/lwjgl/Sys.java index 0fb85dcc..ee80988e 100644 --- a/src/java/org/lwjgl/Sys.java +++ b/src/java/org/lwjgl/Sys.java @@ -77,15 +77,13 @@ public final class Sys { }); } - private static boolean loadLibrary(final String lib_name) { + private static void loadLibrary(final String lib_name) { try { doLoadLibrary(lib_name); - return false; } catch (UnsatisfiedLinkError e) { if (implementation.has64Bit()) { try { doLoadLibrary(lib_name + POSTFIX64BIT); - return true; } catch (UnsatisfiedLinkError e2) { LWJGLUtil.log("Failed to load 64 bit library: " + e2.getMessage()); } @@ -97,13 +95,14 @@ public final class Sys { static { implementation = createImplementation(); - is64Bit = loadLibrary(JNI_LIBRARY_NAME); + loadLibrary(JNI_LIBRARY_NAME); + is64Bit = implementation.getPointerSize() == 8; int native_jni_version = implementation.getJNIVersion(); int required_version = implementation.getRequiredJNIVersion(); if (native_jni_version != required_version) throw new LinkageError("Version mismatch: jar version is '" + required_version + - "', native libary version is '" + native_jni_version + "'"); + "', native library version is '" + native_jni_version + "'"); implementation.setDebug(LWJGLUtil.DEBUG); } diff --git a/src/java/org/lwjgl/SysImplementation.java b/src/java/org/lwjgl/SysImplementation.java index ab255947..c866fbc7 100644 --- a/src/java/org/lwjgl/SysImplementation.java +++ b/src/java/org/lwjgl/SysImplementation.java @@ -51,6 +51,11 @@ interface SysImplementation { */ int getJNIVersion(); + /** + * Returns the platform's pointer size in bytes + */ + int getPointerSize(); + void setDebug(boolean debug); /** diff --git a/src/java/org/lwjgl/opencl/APIUtil.java b/src/java/org/lwjgl/opencl/APIUtil.java index 0c05645a..1c31b491 100644 --- a/src/java/org/lwjgl/opencl/APIUtil.java +++ b/src/java/org/lwjgl/opencl/APIUtil.java @@ -430,9 +430,11 @@ final class APIUtil { static Set getExtensions(final String extensionList) { final Set extensions = new HashSet(); - final StringTokenizer tokenizer = new StringTokenizer(extensionList); - while ( tokenizer.hasMoreTokens() ) - extensions.add(tokenizer.nextToken()); + if ( extensionList != null ) { + final StringTokenizer tokenizer = new StringTokenizer(extensionList); + while ( tokenizer.hasMoreTokens() ) + extensions.add(tokenizer.nextToken()); + } return extensions; } diff --git a/src/java/org/lwjgl/opencl/CallbackUtil.java b/src/java/org/lwjgl/opencl/CallbackUtil.java index f83798fd..862a24b4 100644 --- a/src/java/org/lwjgl/opencl/CallbackUtil.java +++ b/src/java/org/lwjgl/opencl/CallbackUtil.java @@ -34,6 +34,8 @@ package org.lwjgl.opencl; import java.util.HashMap; import java.util.Map; +import static org.lwjgl.opencl.CL10.*; + /** * Utility class that handles OpenCL API callbacks. * @@ -79,7 +81,7 @@ final class CallbackUtil { * @param user_data the GlobalRef memory address */ static void checkCallback(final int errcode, final long user_data) { - if ( errcode != 0x0 && user_data != 0 ) + if ( errcode != CL_SUCCESS && user_data != 0 ) deleteGlobalRef(user_data); } @@ -107,7 +109,7 @@ final class CallbackUtil { * @param user_data the global reference pointer */ static void registerCallback(final CLContext context, final long user_data) { - if ( context.getPointer() == 0 ) { + if ( context.getPointerUnsafe() == 0 ) { if ( user_data != 0 ) deleteGlobalRef(user_data); return; diff --git a/src/java/org/lwjgl/test/opencl/HelloOpenCL.java b/src/java/org/lwjgl/test/opencl/HelloOpenCL.java index 485cc8b0..bf2eeb7b 100644 --- a/src/java/org/lwjgl/test/opencl/HelloOpenCL.java +++ b/src/java/org/lwjgl/test/opencl/HelloOpenCL.java @@ -33,6 +33,7 @@ package org.lwjgl.test.opencl; import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; +import org.lwjgl.LWJGLUtil; import org.lwjgl.PointerBuffer; import org.lwjgl.opencl.*; import org.lwjgl.opencl.api.CLBufferRegion; @@ -49,7 +50,7 @@ public class HelloOpenCL { public HelloOpenCL() { } - protected void execute() { + protected static void execute() { try { CL.create(); @@ -135,28 +136,31 @@ public class HelloOpenCL { clRetainMemObject(buffer); - final long exec_caps = device.getInfoLong(CL_DEVICE_EXECUTION_CAPABILITIES); - if ( (exec_caps & CL_EXEC_NATIVE_KERNEL) == CL_EXEC_NATIVE_KERNEL ) { - System.out.println("-TRYING TO EXEC NATIVE KERNEL-"); - final CLCommandQueue queue = clCreateCommandQueue(context, device, 0, null); + if ( LWJGLUtil.getPlatform() != LWJGLUtil.PLATFORM_MACOSX ) { + // TODO: Native kernels crash on MacOSX, disable this until we can debug properly. + final long exec_caps = device.getInfoLong(CL_DEVICE_EXECUTION_CAPABILITIES); + if ( (exec_caps & CL_EXEC_NATIVE_KERNEL) == CL_EXEC_NATIVE_KERNEL ) { + System.out.println("-TRYING TO EXEC NATIVE KERNEL-"); + final CLCommandQueue queue = clCreateCommandQueue(context, device, 0, null); - clEnqueueNativeKernel(queue, new CLNativeKernel() { - protected void execute(final ByteBuffer[] memobjs) { - if ( memobjs == null ) - System.out.println("OK, it's null"); - else { - System.out.println("memobjs = " + memobjs.length); - for ( int k = 0; k < memobjs.length; k++ ) { - System.out.println("memobjs[" + k + "].remaining() = " + memobjs[k].remaining()); - for ( int l = memobjs[k].position(); l < memobjs[k].limit(); l++ ) { - memobjs[k].put(l, (byte)l); + clEnqueueNativeKernel(queue, new CLNativeKernel() { + protected void execute(final ByteBuffer[] memobjs) { + if ( memobjs == null ) + System.out.println("OK, it's null"); + else { + System.out.println("memobjs = " + memobjs.length); + for ( int k = 0; k < memobjs.length; k++ ) { + System.out.println("memobjs[" + k + "].remaining() = " + memobjs[k].remaining()); + for ( int l = memobjs[k].position(); l < memobjs[k].limit(); l++ ) { + memobjs[k].put(l, (byte)l); + } } } } - } - }, new CLMem[] { buffer }, new long[] { 128 }, null, null); + }, new CLMem[] { buffer }, new long[] { 128 }, null, null); - clFinish(queue); + clFinish(queue); + } } clReleaseMemObject(buffer); diff --git a/src/native/common/common_tools.c b/src/native/common/common_tools.c index 7f17f972..4461ce9c 100644 --- a/src/native/common/common_tools.c +++ b/src/native/common/common_tools.c @@ -61,6 +61,10 @@ void putAttrib(attrib_list_t *list, int attrib) { list->current_index++; } +JNIEXPORT jint JNICALL Java_org_lwjgl_DefaultSysImplementation_getPointerSize(JNIEnv *env, jclass clazz) { + return (jint)sizeof(void *); +} + JNIEXPORT void JNICALL Java_org_lwjgl_DefaultSysImplementation_setDebug (JNIEnv *env, jobject ignored, jboolean enable) { debug = enable == JNI_TRUE ? true : false;