diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/com/mbien/opencl/CLCommandQueue.java | 10 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLContext.java | 36 | ||||
-rw-r--r-- | src/com/mbien/opencl/CLGLContext.java | 83 |
3 files changed, 106 insertions, 23 deletions
diff --git a/src/com/mbien/opencl/CLCommandQueue.java b/src/com/mbien/opencl/CLCommandQueue.java index d488d1c4..737e8c81 100644 --- a/src/com/mbien/opencl/CLCommandQueue.java +++ b/src/com/mbien/opencl/CLCommandQueue.java @@ -154,6 +154,16 @@ public class CLCommandQueue implements CLResource { localWorkSize ==0 ? null : new long[] {localWorkSize } ); } + public CLCommandQueue put2DRangeKernel(CLKernel kernel, long globalWorkOffsetX, long globalWorkOffsetY, + long globalWorkSizeX, long globalWorkSizeY, + long localWorkSizeX, long localWorkSizeY) { + return this.putNDRangeKernel( + kernel, 2, + globalWorkOffsetX==0 && globalWorkOffsetY==0 ? null : new long[] {globalWorkOffsetX, globalWorkOffsetY}, + globalWorkSizeX ==0 && globalWorkSizeY ==0 ? null : new long[] {globalWorkSizeX, globalWorkSizeY }, + localWorkSizeX ==0 && localWorkSizeY ==0 ? null : new long[] {localWorkSizeX, localWorkSizeY } ); + } + public CLCommandQueue putNDRangeKernel(CLKernel kernel, int workDimension, long[] globalWorkOffset, long[] globalWorkSize, long[] localWorkSize) { int ret = cl.clEnqueueNDRangeKernel( diff --git a/src/com/mbien/opencl/CLContext.java b/src/com/mbien/opencl/CLContext.java index 04df962b..75e4d35a 100644 --- a/src/com/mbien/opencl/CLContext.java +++ b/src/com/mbien/opencl/CLContext.java @@ -29,19 +29,19 @@ import static com.sun.gluegen.runtime.BufferFactory.*; * specified in the context. * @author Michael Bien */ -public final class CLContext implements CLResource { +public class CLContext implements CLResource { final CL cl; public final long ID; - private CLDevice[] devices; + protected CLDevice[] devices; - private final List<CLProgram> programs; - private final List<CLBuffer<? extends Buffer>> buffers; - private final Map<CLDevice, List<CLCommandQueue>> queuesMap; + protected final List<CLProgram> programs; + protected final List<CLBuffer<? extends Buffer>> buffers; + protected final Map<CLDevice, List<CLCommandQueue>> queuesMap; - private CLContext(long contextID) { + protected CLContext(long contextID) { this.cl = CLPlatform.getLowLevelBinding(); this.ID = contextID; this.programs = new ArrayList<CLProgram>(); @@ -75,7 +75,7 @@ public final class CLContext implements CLResource { * The platform to be used is implementation dependent. */ public static final CLContext create() { - return createContextFromType(null, CL.CL_DEVICE_TYPE_ALL); + return new CLContext(createContextFromType(null, CL.CL_DEVICE_TYPE_ALL)); } /** @@ -121,7 +121,7 @@ public final class CLContext implements CLResource { properties.rewind(); } - return createContextFromType(properties, type); + return new CLContext(createContextFromType(properties, type)); } /** @@ -143,27 +143,27 @@ public final class CLContext implements CLResource { properties.rewind(); } - return createContext(properties, deviceIDs); + return new CLContext(createContext(properties, deviceIDs)); } - private static final CLContext createContextFromType(LongBuffer properties, long deviceType) { + protected static final long createContextFromType(LongBuffer properties, long deviceType) { IntBuffer status = IntBuffer.allocate(1); long context = CLPlatform.getLowLevelBinding().clCreateContextFromType(properties, deviceType, null, null, status); checkForError(status.get(), "can not create CL context"); - return new CLContext(context); + return context; } - private static final CLContext createContext(LongBuffer properties, long[] devices) { + protected static final long createContext(LongBuffer properties, long[] devices) { IntBuffer status = IntBuffer.allocate(1); long context = CLPlatform.getLowLevelBinding().clCreateContext(properties, devices, null, null, status); checkForError(status.get(), "can not create CL context"); - return new CLContext(context); + return context; } /** @@ -263,16 +263,6 @@ public final class CLContext implements CLResource { return buffer; } - public final <B extends Buffer> CLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, Mem... flags) { - return createFromGLBuffer(directBuffer, glBuffer, Mem.flagsToInt(flags)); - } - - public final <B extends Buffer> CLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, int flags) { - CLBuffer<B> buffer = new CLBuffer<B>(this, directBuffer, glBuffer, flags); - buffers.add(buffer); - return buffer; - } - CLCommandQueue createCommandQueue(CLDevice device, long properties) { CLCommandQueue queue = new CLCommandQueue(this, device, properties); diff --git a/src/com/mbien/opencl/CLGLContext.java b/src/com/mbien/opencl/CLGLContext.java new file mode 100644 index 00000000..9699c1ba --- /dev/null +++ b/src/com/mbien/opencl/CLGLContext.java @@ -0,0 +1,83 @@ +package com.mbien.opencl; + +import java.nio.Buffer; +import com.mbien.opencl.CLBuffer.Mem; +import com.sun.opengl.impl.GLContextImpl; +import com.sun.opengl.impl.macosx.cgl.MacOSXCGLContext; +import com.sun.opengl.impl.windows.wgl.WindowsWGLContext; +import com.sun.opengl.impl.x11.glx.X11GLXContext; +import com.sun.opengl.impl.x11.glx.X11GLXGraphicsConfiguration; +import java.nio.LongBuffer; +import javax.media.nativewindow.DefaultGraphicsConfiguration; +import javax.media.opengl.GLContext; + +/** + * + * @author Michael Bien + */ +public final class CLGLContext extends CLContext { + + final long glContextID; + + private CLGLContext(long clContextID, long glContextID) { + super(clContextID); + this.glContextID = glContextID; + } + + public static CLGLContext create(GLContext glContext) { + + long glID = glContext.getContext(); + +//UNIX +//cl_context_properties props[] = { +// CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), +// CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(), +// CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0}; + +//WIN32 +//cl_context_properties props[] = { +// CL_GL_CONTEXT_KHR, (cl_context_properties)TODO0, +// CL_WGL_HDC_KHR, (cl_context_properties)TODO 0, +// CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0}; + +//MACOSX +//cl_context_properties props[] = { +// CL_CGL_SHAREGROUP_KHR, (cl_context_properties)TODO 0, +// CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0}; + + GLContextImpl ctxImpl = (GLContextImpl)glContext; + + DefaultGraphicsConfiguration config = (DefaultGraphicsConfiguration)ctxImpl.getDrawableImpl() + .getNativeWindow().getGraphicsConfiguration().getNativeGraphicsConfiguration(); + + LongBuffer properties = LongBuffer.allocate(5); + if(glContext instanceof X11GLXContext) { + long handle = config.getScreen().getDevice().getHandle(); + properties.put(CLGLI.CL_GL_CONTEXT_KHR).put(glID) + .put(CLGLI.CL_GLX_DISPLAY_KHR).put(handle); + }else if(glContext instanceof WindowsWGLContext) { + // TODO test on windows + throw new RuntimeException("cl-gl interoperability on windows not yet implemented"); + }else if(glContext instanceof MacOSXCGLContext) { + // TODO test on mac + throw new RuntimeException("cl-gl interoperability on mac not yet implemented"); + } + + properties.put(0).rewind(); // 0 terminated array + + long clID = createContextFromType(properties, CL.CL_DEVICE_TYPE_ALL); + + return new CLGLContext(clID, glID); + } + + + public final <B extends Buffer> CLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, Mem... flags) { + return createFromGLBuffer(directBuffer, glBuffer, Mem.flagsToInt(flags)); + } + + public final <B extends Buffer> CLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, int flags) { + CLBuffer<B> buffer = new CLBuffer<B>(this, directBuffer, glBuffer, flags); + buffers.add(buffer); + return buffer; + } +} |