From: Not Zed Date: Thu, 30 Jan 2020 03:31:08 +0000 (+1030) Subject: All clobjects now track the platform for easy lookup. X-Git-Url: https://code.zedzone.au/cvs?a=commitdiff_plain;h=b95c4cddb0dcb36e6e071a5fc5a0aaf840376f99;p=zcl All clobjects now track the platform for easy lookup. Changed the extension design to fit with a binding generator and moving the call details to the relevent object. --- diff --git a/src/notzed.zcl/classes/api/Native.java b/src/notzed.zcl/classes/api/Native.java index 0516199..a2d2b0e 100644 --- a/src/notzed.zcl/classes/api/Native.java +++ b/src/notzed.zcl/classes/api/Native.java @@ -325,6 +325,10 @@ public class Native { return null; } + public static MethodHandle downcallHandle(Function find, String name, String signature) { + return downcallHandle(find.apply(name), signature); + } + public static MethodHandle downcallHandle(MemoryAddress addr, String signature) { Signature sig = Signature.parse(signature); int n = sig.classes.length; diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CL.java b/src/notzed.zcl/classes/au/notzed/zcl/CL.java index 50f9ee6..816b3db 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CL.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CL.java @@ -112,7 +112,7 @@ public interface CL { public final static int CL_PLATFORM_EXTENSIONS = 0x0904; // 2.1 public final static int CL_PLATFORM_HOST_TIMER_RESOLUTION = 0x0905; - + /* cl_device_type - bitfield */ public final static long CL_DEVICE_TYPE_DEFAULT = (1 << 0); public final static long CL_DEVICE_TYPE_CPU = (1 << 1); @@ -306,7 +306,7 @@ public interface CL { public final static long CL_MEM_USE_HOST_PTR = (1 << 3); public final static long CL_MEM_ALLOC_HOST_PTR = (1 << 4); public final static long CL_MEM_COPY_HOST_PTR = (1 << 5); -// reserved (1 << 6) +// reserved (1 << 6) public final static long CL_MEM_HOST_WRITE_ONLY = (1 << 7); public final static long CL_MEM_HOST_READ_ONLY = (1 << 8); public final static long CL_MEM_HOST_NO_ACCESS = (1 << 9); @@ -598,4 +598,41 @@ public interface CL { public final static int CL_PROFILING_COMMAND_END = 0x1283; public final static int CL_PROFILING_COMMAND_COMPLETE = 0x1284; + /** + * *************************************************************************** + */ + + // cl_khr_gl_event extension + public static final int CL_COMMAND_GL_FENCE_SYNC_OBJECT_KHR = 0x200D; + + // cl_khr_gl_sharing extension + + /* cl_gl_object_type = 0x2000 - 0x200F enum values are currently taken */ + public static final int CL_GL_OBJECT_BUFFER = 0x2000; + public static final int CL_GL_OBJECT_TEXTURE2D = 0x2001; + public static final int CL_GL_OBJECT_TEXTURE3D = 0x2002; + public static final int CL_GL_OBJECT_RENDERBUFFER = 0x2003; + public static final int CL_GL_OBJECT_TEXTURE2D_ARRAY = 0x200E; + public static final int CL_GL_OBJECT_TEXTURE1D = 0x200F; + public static final int CL_GL_OBJECT_TEXTURE1D_ARRAY = 0x2010; + public static final int CL_GL_OBJECT_TEXTURE_BUFFER = 0x2011; + + /* cl_gl_texture_info */ + public static final int CL_GL_TEXTURE_TARGET = 0x2004; + public static final int CL_GL_MIPMAP_LEVEL = 0x2005; + public static final int CL_GL_NUM_SAMPLES = 0x2012; + + /* Additional Error Codes */ + public static final int CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR = -1000; + + /* cl_gl_context_info */ + public static final int CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR = 0x2006; + public static final int CL_DEVICES_FOR_GL_CONTEXT_KHR = 0x2007; + + /* Additional cl_context_properties */ + public static final int CL_GL_CONTEXT_KHR = 0x2008; + public static final int CL_EGL_DISPLAY_KHR = 0x2009; + public static final int CL_GLX_DISPLAY_KHR = 0x200A; + public static final int CL_WGL_HDC_KHR = 0x200B; + public static final int CL_CGL_SHAREGROUP_KHR = 0x200C; } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLBuffer.java b/src/notzed.zcl/classes/au/notzed/zcl/CLBuffer.java index a24c794..bd580d5 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLBuffer.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLBuffer.java @@ -16,7 +16,10 @@ */ package au.notzed.zcl; +import static au.notzed.zcl.CL.*; +import static au.notzed.zcl.CLLib.*; import jdk.incubator.foreign.*; +import api.*; import java.nio.ByteBuffer; @@ -25,12 +28,12 @@ import java.nio.ByteBuffer; */ public class CLBuffer extends CLMemory { - public CLBuffer(MemoryAddress p) { - this(p, null); + public CLBuffer(MemoryAddress p, CLPlatform platform) { + super(p, platform, null); } - CLBuffer(MemoryAddress p, MemorySegment seg) { - super(p, seg); + CLBuffer(MemoryAddress p, CLPlatform platform, MemorySegment seg) { + super(p, platform, seg); } static void release(MemoryAddress p) { @@ -50,17 +53,36 @@ public class CLBuffer extends CLMemory { } /** - * Call clCreateSubBuffer. + * Call clCreateSubBuffer with a type CL_BUFFER_CREATE_TYPE_REGION * * @param flags * @param info Define the properties for the sub-bufer. - * @return A newly created buffer.

- * It must be released when no longer needed. + * @return A newly created buffer. * @see #REGION * @throws CLException * @since OpenCL 1.1 */ - public native CLBuffer createSubBuffer(long flags, CLBufferInfo info) throws CLException; + public CLBuffer createSubBuffer(long flags, CLBufferInfo info) throws CLException { + requireAPIVersion(CLPlatform.VERSION_1_1); + + try (Allocator frame = Memory.stack()) { + MemoryAddress pres = frame.alloca(8); + MemoryAddress pinfo = info.toNative(frame); + MemoryAddress b; + int res; + + b = clCreateSubBuffer(addr(), flags, CL_BUFFER_CREATE_TYPE_REGION, pinfo, pres); + res = getInt(pres); + if (res != 0) + throw new CLRuntimeException(res); + + return Native.resolve(b, (c) -> new CLBuffer(c, getObjectPlatform())); + } catch (RuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); + } + } /* Functional Interface Constructors. diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLBufferInfo.java b/src/notzed.zcl/classes/au/notzed/zcl/CLBufferInfo.java index fa97c8a..82a4764 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLBufferInfo.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLBufferInfo.java @@ -16,10 +16,14 @@ */ package au.notzed.zcl; +import api.Allocator; +import api.Native; +import jdk.incubator.foreign.MemoryAddress; + /** * Parameters for Buffer.createSubBuffer() *

- * Specific types are defined on CLBuffer. + * Specific types are defined on CLBuffer, e.g. CLBuffer.REGION. * */ public abstract class CLBufferInfo { @@ -34,5 +38,15 @@ public abstract class CLBufferInfo { this.size = size; } + MemoryAddress toNative(Allocator frame) { + MemoryAddress addr = frame.alloca(2*8); // FIXME: size_t + + Native.setLong(addr, origin); + Native.setLong(addr, 1, size); + + return addr; + } } + + abstract MemoryAddress toNative(Allocator frame); } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java b/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java index 73e7ae3..531282c 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLCommandQueue.java @@ -18,7 +18,6 @@ package au.notzed.zcl; import static au.notzed.zcl.CL.*; import static au.notzed.zcl.CLLib.*; -import au.notzed.zcl.internal.EventInfo; import jdk.incubator.foreign.*; import api.Native; import api.Allocator; @@ -31,7 +30,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.BufferOverflowException; import java.nio.BufferUnderflowException; -import au.notzed.zcl.khr.*; import java.lang.invoke.MethodHandle; import java.util.function.Function; @@ -53,14 +51,10 @@ import java.util.function.Function; * For the offer operations which take arrays all offsets and sizes are in * primitive element units and not bytes. */ -public class CLCommandQueue extends CLExtendable { +public class CLCommandQueue extends CLObject { - public CLCommandQueue(MemoryAddress p) { - super(p); - } - - static CLCommandQueue create(MemoryAddress p) { - return Native.resolve(p, CLCommandQueue::new); + public CLCommandQueue(MemoryAddress p, CLPlatform platform) { + super(p, platform); } private static void release(MemoryAddress p) { @@ -1527,7 +1521,6 @@ public class CLCommandQueue extends CLExtendable { * @throws CLException * x-deprecated as of OpenCL 2.0 */ - //@Deprecated public void enqueueTask(CLKernel kernel, CLEventList waiters, CLEventList events) throws CLException { @@ -1872,11 +1865,11 @@ public class CLCommandQueue extends CLExtendable { } public CLContext getContext() { - return getInfoAny(CL_QUEUE_CONTEXT, clGetCommandQueueInfo, CLContext::new); + return getInfoAny(CL_QUEUE_CONTEXT, clGetCommandQueueInfo, (x) -> new CLContext(x, getObjectPlatform())); } public CLDevice getDevice() { - return getInfoAny(CL_QUEUE_DEVICE, clGetCommandQueueInfo, CLDevice::new); + return getInfoAny(CL_QUEUE_DEVICE, clGetCommandQueueInfo, (x) -> new CLDevice(x, getObjectPlatform())); } public long getProperties() { @@ -1891,37 +1884,65 @@ public class CLCommandQueue extends CLExtendable { return getInfoInt(CL_QUEUE_SIZE); } - @Override - protected CLPlatform initPlatform() { - return getDevice().platform; - } + /* ********************************************************************** */ - protected GLSharing getGLSharing() { - return getExtension(CLPlatform.cl_khr_gl_sharing, (p) -> { - if (getDevice().hasDeviceExtension(GLSharing.NAME)) - return new GLSharing(p); - else - throw new UnsupportedOperationException(); - }); + protected GLext getGLext() { + return getObjectPlatform() + .getExtension(CLPlatform.cl_gl_ext, (p) + -> new GLext(p::clGetExtensionFunctionAddressForPlatform)); } - /* - Experimental: Alternative interface to extensions. + /* ********************************************************************** + * cl_khr_sharing extension */ public void enqueueAcquireGLObjects( CLMemory[] mem_objects, - CLEventList waiters, - CLEventList events) { - getGLSharing().enqueueAcquireGLObjects(this, mem_objects, waiters, events); + CLEventList wait, + CLEventList event) { + GLext gl = getGLext(); + try (Allocator frame = Memory.stack()) { + EventInfo info = new EventInfo(frame, wait, event); + MemoryAddress cmem_objects = Native.toAddrV(frame, mem_objects); + int res; + + res = gl.clEnqueueAcquireGLObjects(addr(), mem_objects.length, cmem_objects, + info.nwait, info.wait, info.event); + if (res != 0) + throw new CLRuntimeException(res); + + info.post(event); + } catch (RuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); + } } public void enqueueReleaseGLObjects( CLMemory[] mem_objects, - CLEventList waiters, - CLEventList events) { - getGLSharing().enqueueReleaseGLObjects(this, mem_objects, waiters, events); + CLEventList wait, + CLEventList event) { + GLext gl = getGLext(); + try (Allocator frame = Memory.stack()) { + EventInfo info = new EventInfo(frame, wait, event); + MemoryAddress cmem_objects = Native.toAddrV(frame, mem_objects); + int res; + + res = gl.clEnqueueReleaseGLObjects(addr(), mem_objects.length, cmem_objects, + info.nwait, info.wait, info.event); + if (res != 0) + throw new CLRuntimeException(res); + + info.post(event); + } catch (RuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); + } } + /* ********************************************************************** */ + /** * Invoke task.queue for this queue with no event lists. * @@ -1959,20 +1980,4 @@ public class CLCommandQueue extends CLExtendable { task.enqueue(this, wait, event); return this; } - - /** - * Create a null task. - *

- * This enqueues a marker. - * - * @return - * @deprecated use CLTask.ofNOP() instead - */ - @Deprecated - public static CLTask ofNOP() { - return (CLCommandQueue q, CLEventList wait, CLEventList event) -> { - q.enqueueMarkerWithWaitList(wait, event); - }; - } - } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java b/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java index bd5bc02..2913512 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLContext.java @@ -30,32 +30,33 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; -import au.notzed.zcl.khr.*; /** * Interface for cl_context */ -public class CLContext extends CLExtendable { +public class CLContext extends CLObject { /** * If a notify callback is supplied to createContext() then this is used to track the reference. */ final Callback notify; - public CLContext(MemoryAddress p) { - super(p); + /** + * platform must not be null. + */ + public CLContext(MemoryAddress p, CLPlatform platform) { + super(p, platform); this.notify = null; } - CLContext(MemoryAddress p, Callback notify) { - super(p); + /** + * platform must not be null. + */ + CLContext(MemoryAddress p, CLPlatform platform, Callback notify) { + super(p, platform); this.notify = notify; } - static CLContext create(MemoryAddress p) { - return Native.resolve(p, CLContext::new); - } - private static void release(MemoryAddress p) { try { clReleaseContext(p); @@ -129,7 +130,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLRuntimeException(res); - return Native.resolve(cl, CLContext::new); + return Native.resolve(cl, (c) -> new CLContext(c, devices[0].getObjectPlatform())); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -143,6 +144,23 @@ public class CLContext extends CLExtendable { }); } + /** + * Directly lookup platform for a context where we have no other context to find it + */ + static CLPlatform findPlatform(MemoryAddress ccl) { + try (Allocator a = Memory.stack()) { + MemorySegment devices = getInfoAny(ccl, CL_CONTEXT_DEVICES, clGetContextInfo, a); + MemoryAddress dev0 = getAddr(devices.baseAddress()); + MemoryAddress plat = getAddr(getInfo(dev0, CL_DEVICE_PLATFORM, clGetDeviceInfo, a, 8)); + + return Native.resolve(plat, CLPlatform::new); + } catch (RuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); + } + } + /** * Calls clCreateContextFromType. * @@ -154,7 +172,6 @@ public class CLContext extends CLExtendable { * @throws CLRuntimeException */ public static CLContext createContextFromType(CLContextProperty[] properties, long device_type, CLContextNotify notify) throws CLRuntimeException { - try (Allocator frame = Memory.stack()) { MemoryAddress pprops = CLProperty.toNative(frame, properties); MemoryAddress pres = frame.alloca(8); @@ -168,7 +185,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLRuntimeException(res); - return Native.resolve(cl, (p) -> new CLContext(p, call)); + return Native.resolve(cl, (p) -> new CLContext(p, findPlatform(p), call)); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -198,7 +215,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLRuntimeException(res); - return Native.resolve(q, CLCommandQueue::new); + return Native.resolve(q, (c) -> new CLCommandQueue(c, getObjectPlatform())); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -231,7 +248,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLRuntimeException(res); - return Native.resolve(q, CLCommandQueue::new); + return Native.resolve(q, (c) -> new CLCommandQueue(c, getObjectPlatform())); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -304,9 +321,9 @@ public class CLContext extends CLExtendable { throw new CLRuntimeException(res); if (hostseg != null && (flags & CL_MEM_USE_HOST_PTR) != 0) - return resolve(pbuffer, (x) -> new CLBuffer(x, hostseg)); + return resolve(pbuffer, (x) -> new CLBuffer(x, getObjectPlatform(), hostseg)); else - return resolve(pbuffer, CLBuffer::new); + return resolve(pbuffer, (x) -> new CLBuffer(x, getObjectPlatform())); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -468,9 +485,9 @@ public class CLContext extends CLExtendable { throw new CLRuntimeException(res); if (hostseg != null && (flags & CL_MEM_USE_HOST_PTR) != 0) - return resolve(ci, (x) -> new CLImage(x, hostseg)); + return resolve(ci, (x) -> new CLImage(x, getObjectPlatform(), hostseg)); else - return resolve(ci, CLImage::new); + return resolve(ci, (x) -> new CLImage(x, getObjectPlatform())); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -555,7 +572,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLRuntimeException(res); - return resolve(cp, CLPipe::new); + return resolve(cp, (x) -> new CLPipe(x, getObjectPlatform())); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -651,7 +668,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLRuntimeException(res); - return resolve(cs, CLSampler::new); + return resolve(cs, (x) -> new CLSampler(x, getObjectPlatform())); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -682,7 +699,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLRuntimeException(res); - return resolve(cs, CLSampler::new); + return resolve(cs, (x) -> new CLSampler(x, getObjectPlatform())); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -741,7 +758,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLException(res); - return resolve(cp, CLProgram::new); + return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform())); } catch (CLException | RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -796,7 +813,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLException(res); - return resolve(cp, CLProgram::new); + return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform())); } catch (CLException | RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -848,7 +865,7 @@ public class CLContext extends CLExtendable { status[i] = getInt(cstatus, i); } - return resolve(cp, CLProgram::new); + return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform())); } catch (CLException | RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -882,7 +899,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLException(res); - return resolve(cp, CLProgram::new); + return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform())); } catch (CLException | RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -907,7 +924,7 @@ public class CLContext extends CLExtendable { requireAPIVersion(CLPlatform.VERSION_1_2); try (Allocator frame = Memory.stack(); - Callback> cnotify = CLNotify.call(notify, CLProgram::new)) { + Callback> cnotify = CLNotify.call(notify, (x) -> new CLProgram(x, getObjectPlatform()))) { MemoryAddress cdevs = toAddrV(frame, devices); MemoryAddress coptions = toByteV(frame, options); MemoryAddress cprogs = toAddrV(frame, programs); @@ -921,7 +938,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLException(res); - return resolve(cp, CLProgram::new); + return resolve(cp, (x) -> new CLProgram(x, getObjectPlatform())); } catch (CLException | RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -951,7 +968,7 @@ public class CLContext extends CLExtendable { if (res != 0) throw new CLException(res); - return resolve(ce, CLEvent::new); + return resolve(ce, (x) -> new CLEvent(x, getObjectPlatform())); } catch (CLException | RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -974,7 +991,7 @@ public class CLContext extends CLExtendable { * @return List of devices. */ public CLDevice[] getDevices() { - return getInfoAnyV(CL_CONTEXT_DEVICES, clGetContextInfo, CLDevice::new, CLDevice[]::new); + return getInfoAnyV(CL_CONTEXT_DEVICES, clGetContextInfo, (x) -> new CLDevice(x, getObjectPlatform()), CLDevice[]::new); } /** @@ -986,66 +1003,157 @@ public class CLContext extends CLExtendable { return getInfoPropertyV(CL_CONTEXT_PROPERTIES, clGetContextInfo, CLContextProperty.TagValue::new, CLContextProperty[]::new); } - @Override - protected CLPlatform initPlatform() { - return getDevices()[0].platform; + /* ********************************************************************** */ + + protected GLext getGLext() { + return getObjectPlatform() + .getExtension(CLPlatform.cl_gl_ext, (p) + -> new GLext(p::clGetExtensionFunctionAddressForPlatform)); } - /* - Experimental: extension interface mechanism + /* ********************************************************************** + * cl_khr_sharing extension */ - protected GLSharing getGLSharing() { - return getExtension(CLPlatform.cl_khr_gl_sharing, (p) -> { - if (getDevices()[0].hasDeviceExtension(GLSharing.NAME)) - return new GLSharing(p); - else - throw new UnsupportedOperationException(); - }); + public static CLContextProperty GL_CONTEXT_KHR(long id) { + return new CLContextProperty.TagValue(CL_GL_CONTEXT_KHR, id); } - protected GLEvent getGLEvent() { - return getExtension(CLPlatform.cl_khr_gl_sharing, (p) -> { - if (getDevices()[0].hasDeviceExtension(GLEvent.NAME)) - return new GLEvent(p); - else - throw new UnsupportedOperationException(); - }); + public static CLContextProperty EGL_DISPLAY_KHR(long id) { + return new CLContextProperty.TagValue(CL_EGL_DISPLAY_KHR, id); + } + + public static CLContextProperty GLX_DISPLAY_KHR(long id) { + return new CLContextProperty.TagValue(CL_GLX_DISPLAY_KHR, id); + } + + public static CLContextProperty WGL_HDC_KHR(long id) { + return new CLContextProperty.TagValue(CL_WGL_HDC_KHR, id); + } + + public static CLContextProperty CGL_SHAREGROUP_KHR(long id) { + return new CLContextProperty.TagValue(CL_CGL_SHAREGROUP_KHR, id); } /** * @since cl_khr_gl_sharing extension */ public CLBuffer createFromGLBuffer( - long flags, - int bufobj) { - return getGLSharing().createFromGLBuffer(this, flags, bufobj); + long flags, + int bufobj) { + GLext gl = getGLext(); + try (Allocator frame = Memory.stack()) { + MemoryAddress cres = frame.alloca(8); + MemoryAddress ce; + int res; + + ce = gl.clCreateFromGLBuffer(addr(), flags, bufobj, cres); + res = Native.getInt(cres); + if (res != 0) + throw new CLRuntimeException(res); + return Native.resolve(ce, (b) -> new CLBuffer(b, getObjectPlatform())); + } catch (RuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); + } } /** * @since cl_khr_gl_sharing extension */ public CLImage createFromGLTexture( - long flags /* flags */, - int target /* target */, - int miplevel /* miplevel */, - int texture /* texture */) { - return getGLSharing().createFromGLTexture(this, flags, target, miplevel, texture); + long flags, + int target, + int miplevel, + int texture) { + GLext gl = getGLext(); + try (Allocator frame = Memory.stack()) { + MemoryAddress cres = frame.alloca(8); + MemoryAddress ce; + int res; + + ce = gl.clCreateFromGLTexture(addr(), flags, target, miplevel, texture, cres); + res = Native.getInt(cres); + if (res != 0) + throw new CLRuntimeException(res); + return Native.resolve(ce, (x) -> new CLImage(x, getObjectPlatform())); + } catch (RuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); + } } /** * @since cl_khr_gl_sharing extension */ - public CLImage createFromGLRenderbuffer( - long flags /* flags */, - int renderbuffer /* renderbuffer */) { - return getGLSharing().createFromGLRenderbuffer(this, flags, renderbuffer); - } + public CLImage createFromGLRenderbuffer( + long flags, + int renderbuffer) { + GLext gl = getGLext(); + try (Allocator frame = Memory.stack()) { + MemoryAddress cres = frame.alloca(8); + MemoryAddress ce; + int res; + + ce = gl.clCreateFromGLRenderbuffer(addr(), flags, renderbuffer, cres); + res = Native.getInt(cres); + if (res != 0) + throw new CLRuntimeException(res); + return Native.resolve(ce, (x) -> new CLImage(x, getObjectPlatform())); + } catch (RuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); + } + } + + /* + native T getGLContextInfoKHRAny( + CLContextProperty[] properties + int ctype, + int param_name) throws CLRuntimeException; + + native T getGLContextInfoKHRAnyV( + CLContextProperty[] properties + int ctype, + int param_name) throws CLRuntimeException; + */ + /* + public CLDevice getCurrendDeviceForGLConextKHR(CLContextProperty[] properties) throws CLRuntimeException { + return getGLContextInfoKHRAny(properties, CLObject.CTYPE_DEVICE, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR); + } + + public CLDevice[] getDevicesForGLConextKHR(CLContextProperty[] properties) throws CLRuntimeException { + return getGLContextInfoKHRAnyV(properties, CLObject.CTYPE_DEVICE, CL_DEVICES_FOR_GL_CONTEXT_KHR); + }*/ + + + /* ********************************************************************** + * cl_khr_gl_event extension + */ /** * @since cl_khr_gl_event extension */ - public CLEvent createEventFromGLsync(MemoryAddress glsync) { - return getGLEvent().clCreateEventFromGLsync(this, glsync); + public CLEvent clCreateEventFromGLsyncKHR(MemoryAddress glsync) { + GLext gl = getGLext(); + try (Allocator frame = Memory.stack()) { + MemoryAddress cret = frame.alloca(8); + MemoryAddress ce; + int res; + + ce = gl.clCreateEventFromGLsyncKHR(addr(), glsync, cret); + res = Native.getInt(cret); + if (res != 0) + throw new CLRuntimeException(res); + + return Native.resolve(ce, CLEvent::new); + } catch (RuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); + } } } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLDevice.java b/src/notzed.zcl/classes/au/notzed/zcl/CLDevice.java index c8fbd00..420c0ea 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLDevice.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLDevice.java @@ -25,10 +25,10 @@ import java.util.stream.Stream; /** * Interface for cl_device_id. */ -public class CLDevice extends CLExtendable { +public class CLDevice extends CLObject { - public CLDevice(MemoryAddress p) { - super(p); + public CLDevice(MemoryAddress p, CLPlatform platform) { + super(p, platform); } private static void release(MemoryAddress p) { @@ -48,11 +48,6 @@ public class CLDevice extends CLExtendable { return clGetDeviceInfo; } - @Override - protected CLPlatform initPlatform() { - return getPlatform(); - } - /** * Gets device view of device and host clock. * @@ -387,7 +382,7 @@ public class CLDevice extends CLExtendable { public boolean hasDeviceExtension(String name) { return Stream.of(getDeviceExtensions().split(" ")).anyMatch(name::equals); } - + public long getPrintfBufferSize() { return getInfoSizeT(CL_DEVICE_PRINTF_BUFFER_SIZE); } @@ -397,7 +392,7 @@ public class CLDevice extends CLExtendable { } public CLDevice getParentDevice() { - return getInfoAny(CL_DEVICE_PARENT_DEVICE, clGetDeviceInfo, CLDevice::new); + return getInfoAny(CL_DEVICE_PARENT_DEVICE, clGetDeviceInfo, (x) -> new CLDevice(x, getObjectPlatform())); } public int getPartitionMaxSubDevices() { diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLEvent.java b/src/notzed.zcl/classes/au/notzed/zcl/CLEvent.java index 201c35c..a91d7c3 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLEvent.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLEvent.java @@ -28,25 +28,18 @@ import java.util.ArrayList; */ public class CLEvent extends CLObject { - /** - * Cached and used for api version check - */ - final int apiVersion; - /** * This is used to retain a reference for any callback set. * There may be multiple. */ ArrayList> callbacks; - public CLEvent(MemoryAddress p) { - super(p); - - apiVersion = getContext().getAPIVersion(); + public CLEvent(MemoryAddress p, CLPlatform platform) { + super(p, platform); } - static CLEvent create(MemoryAddress p) { - return Native.resolve(p, CLEvent::new); + public CLEvent(MemoryAddress p) { + super(p); } private static void release(MemoryAddress p) { @@ -70,6 +63,29 @@ public class CLEvent extends CLObject { return clGetEventInfo; } + @Override + CLPlatform getObjectPlatform() { + if (platform == null) + platform = findPlatform(addr()); + return platform; + } + + /** + * Internal get platform. + * This goes via the context - every one must have a context? + */ + static CLPlatform findPlatform(MemoryAddress cevent) { + try (Allocator a = Memory.stack()) { + MemoryAddress ccl = getAddr(getInfo(cevent, CL_EVENT_CONTEXT, clGetEventInfo, a, 8)); + + return CLContext.findPlatform(ccl); + } catch (RuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); + } + } + /** * Call clSetUserEventStatus(this, status). *

@@ -80,7 +96,7 @@ public class CLEvent extends CLObject { * @since OpenCL 1.1 */ public void setUserEventStatus(int status) throws CLRuntimeException { - CLPlatform.requireAPIVersion(apiVersion, CLPlatform.VERSION_1_1); + requireAPIVersion(CLPlatform.VERSION_1_1); try { int res = clSetUserEventStatus(addr(), status); @@ -104,7 +120,7 @@ public class CLEvent extends CLObject { * @since OpenCL 1.1 */ public void setEventCallback(int type, CLEventNotify notify) throws CLRuntimeException { - CLPlatform.requireAPIVersion(apiVersion, CLPlatform.VERSION_1_1); + requireAPIVersion(CLPlatform.VERSION_1_1); Callback callback = CLEventNotify.call(notify); @@ -132,7 +148,7 @@ public class CLEvent extends CLObject { * @return */ public CLCommandQueue getCommandQueue() { - return getInfoAny(CL_EVENT_COMMAND_QUEUE, clGetEventInfo, CLCommandQueue::new); + return getInfoAny(CL_EVENT_COMMAND_QUEUE, clGetEventInfo, (x) -> new CLCommandQueue(x, getObjectPlatform())); } /** @@ -143,7 +159,7 @@ public class CLEvent extends CLObject { * @return */ public CLContext getContext() { - return getInfoAny(CL_EVENT_CONTEXT, clGetEventInfo, CLContext::new); + return getInfoAny(CL_EVENT_CONTEXT, clGetEventInfo, (x) -> new CLContext(x, getObjectPlatform())); } /** diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLExtendable.java b/src/notzed.zcl/classes/au/notzed/zcl/CLExtendable.java deleted file mode 100644 index 213f5fe..0000000 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLExtendable.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (C) 2015 notzed - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package au.notzed.zcl; - -import jdk.incubator.foreign.MemoryAddress; -import java.util.function.Function; - -/** - * Extendable object. These keep track of the platform and api revision to be - * able to lookup extension pointers efficiently. - */ -public abstract class CLExtendable extends CLObject { - - protected final CLPlatform platform; - /** - * Copy of platform.apiVersion, cached for faster lookup - */ - final int apiVersion; - - public CLExtendable(MemoryAddress p) { - super(p); - - platform = initPlatform(); - apiVersion = platform.apiVersion; - } - - /** - * Retrieve the platform. This should not cache the lookup. It cannot return - * null by definition. This should not be called by any implementing class. - * - * TODO: this is expensive to call every object instance the time, find a better way. - * Probably pass platform or parent in, with ability to look it up (on demand) if null. - * TODO: move the whole class to CLObject? - * @return - */ - protected abstract CLPlatform initPlatform(); - - public CLPlatform getPlatform() { - return platform; - } - - public int getAPIVersion() { - return apiVersion; - } - - public void requireAPIVersion(int version) throws UnsupportedOperationException { - CLPlatform.requireAPIVersion(apiVersion, version); - } - - public boolean haveAPIVersion(int version) { - return apiVersion >= version; - } - - /** - * Retrieve an extension interface for this object.Used by implementors of - * CLExtenable. - * - * @param - * @param id The extension id code on CLPlatform. - * @param create constructor method. This should perform - * extension availability checks. It is only called once per - * platform. - * @return - */ - protected T getExtension(int id, Function create) { - return platform.getExtension(id, create); - } -} diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLImage.java b/src/notzed.zcl/classes/au/notzed/zcl/CLImage.java index b255444..d9ff52d 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLImage.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLImage.java @@ -29,12 +29,12 @@ import java.lang.invoke.MethodHandle; */ public class CLImage extends CLMemory { - public CLImage(MemoryAddress p) { - this(p, null); + public CLImage(MemoryAddress p, CLPlatform platform) { + super(p, platform, null); } - public CLImage(MemoryAddress p, MemorySegment seg) { - super(p, seg); + public CLImage(MemoryAddress p, CLPlatform platform, MemorySegment seg) { + super(p, platform, seg); } static void release(MemoryAddress p) { @@ -79,7 +79,7 @@ public class CLImage extends CLMemory { } public CLBuffer getBuffer() { - return getInfoAny(CL_IMAGE_BUFFER, clGetImageInfo, CLBuffer::new); + return getInfoAny(CL_IMAGE_BUFFER, clGetImageInfo, (x) -> new CLBuffer(x, getObjectPlatform())); } public int getNumMipLevels() { diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLImageFormat.java b/src/notzed.zcl/classes/au/notzed/zcl/CLImageFormat.java index f419bc6..4503305 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLImageFormat.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLImageFormat.java @@ -96,7 +96,7 @@ public class CLImageFormat { * This is just hand-rolled for now. I'm not really sure how to approach it * since these are just going to be used temporarily */ - static MemoryAddress toNative(Allocator frame, CLImageFormat fmt) { + static MemoryAddress toNative(Allocator frame, CLImageFormat fmt) { MemoryAddress addr = frame.alloca(2*4); Native.setInt(addr, fmt.channelOrder); diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLKernel.java b/src/notzed.zcl/classes/au/notzed/zcl/CLKernel.java index 5565491..103e1db 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLKernel.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLKernel.java @@ -27,22 +27,11 @@ import java.lang.invoke.MethodHandle; /** * Interface for cl_kernel. - *

- * Note although this has api versioning it does not extend CLExtendable for - * efficiency reasons. There is therefore no run-time checking of api. */ public class CLKernel extends CLObject { - int apiVersion; - - CLKernel(MemoryAddress p) { - super(p); - - apiVersion = getContext().getAPIVersion(); - } - - static CLKernel create(MemoryAddress p) { - return Native.resolve(p, CLKernel::new); + CLKernel(MemoryAddress p, CLPlatform platform) { + super(p, platform); } private static void release(MemoryAddress p) { @@ -65,17 +54,17 @@ public class CLKernel extends CLObject { * @since OpenCL 2.1 */ public CLKernel cloneKernel() throws CLRuntimeException { - CLPlatform.requireAPIVersion(apiVersion, CLPlatform.VERSION_2_1); - + requireAPIVersion(CLPlatform.VERSION_2_1); + try (Allocator a = Memory.stack()) { MemoryAddress cres = a.alloca(8); MemoryAddress ck = clCloneKernel(addr(), cres); int res = getInt(cres); - + if (res != 0) throw new CLRuntimeException(res); - return Native.resolve(ck, CLKernel::new); + return Native.resolve(ck, (x) -> new CLKernel(x, getObjectPlatform())); } catch (CLRuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -392,11 +381,11 @@ public class CLKernel extends CLObject { } public CLContext getContext() { - return getInfoAny(CL_KERNEL_CONTEXT, clGetKernelInfo, CLContext::new); + return getInfoAny(CL_KERNEL_CONTEXT, clGetKernelInfo, (x) -> new CLContext(x, getObjectPlatform())); } public CLProgram getProgram() { - return getInfoAny(CL_KERNEL_PROGRAM, clGetKernelInfo, CLProgram::new); + return getInfoAny(CL_KERNEL_PROGRAM, clGetKernelInfo, (x) -> new CLProgram(x, getObjectPlatform())); } public String getAttributes() { diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java b/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java index f1151f0..a4ddbdc 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLMemory.java @@ -18,7 +18,6 @@ package au.notzed.zcl; import static au.notzed.zcl.CL.*; import static au.notzed.zcl.CLLib.*; -import au.notzed.zcl.khr.*; import jdk.incubator.foreign.*; import api.Native; import api.Callback; @@ -64,7 +63,7 @@ import java.nio.ByteOrder; * performance penalty over simply passing the byte offset as with the array * methods. It may change (again) in the future? */ -public abstract class CLMemory extends CLExtendable { +public abstract class CLMemory extends CLObject { /** * If use USE_HOST_PTR was used then this keeps track of the @@ -78,8 +77,8 @@ public abstract class CLMemory extends CLExtendable { */ Callback> destroyCallback; - CLMemory(MemoryAddress p, MemorySegment seg) { - super(p); + CLMemory(MemoryAddress p, CLPlatform plat, MemorySegment seg) { + super(p, plat); this.seg = seg; } @@ -89,13 +88,15 @@ public abstract class CLMemory extends CLExtendable { return clGetMemObjectInfo; } - public static CLMemory create(MemoryAddress p) { + /** + * This is required to support polymorphic query functions + * that are only given a 'mem object'. + */ + @Deprecated + private static CLMemory create(MemoryAddress p, CLPlatform plat) { if (p.offset() == 0) return null; - // This is basically a workaround so that setMemObjectDestructorCallback passes - // the right type without changing the api or using reflection. - // Probably better solutions. try (Allocator a = Memory.stack()) { MemoryAddress addr = getInfo(p, CL_MEM_TYPE, clGetMemObjectInfo, a, 4); @@ -103,16 +104,16 @@ public abstract class CLMemory extends CLExtendable { switch (type) { case CL_MEM_OBJECT_BUFFER: - return Native.resolve(p, CLBuffer::new); + return Native.resolve(p, (x) -> new CLBuffer(x, plat)); case CL_MEM_OBJECT_IMAGE2D: case CL_MEM_OBJECT_IMAGE3D: case CL_MEM_OBJECT_IMAGE2D_ARRAY: case CL_MEM_OBJECT_IMAGE1D: case CL_MEM_OBJECT_IMAGE1D_ARRAY: case CL_MEM_OBJECT_IMAGE1D_BUFFER: - return Native.resolve(p, CLImage::new); + return Native.resolve(p, (x) -> new CLImage(x, plat)); case CL_MEM_OBJECT_PIPE: - return Native.resolve(p, CLPipe::new); + return Native.resolve(p, (x) -> new CLPipe(x, plat)); default: throw new UnsupportedOperationException(); } @@ -127,11 +128,6 @@ public abstract class CLMemory extends CLExtendable { } } - @Override - protected CLPlatform initPlatform() { - return getContext().getDevices()[0].getPlatform(); - } - /** * If CL_MEM_USE_HOST_PTR was used at creation then this must * be invoked to avoid a memory leak. It also must be invoked @@ -155,12 +151,11 @@ public abstract class CLMemory extends CLExtendable { * @since OpenCL 1.1 */ public void setMemObjectDestructorCallback(CLNotify notify) throws CLException, UnsupportedOperationException { - //a bit costly perhaps - getContext().requireAPIVersion(CLPlatform.VERSION_1_1); + requireAPIVersion(CLPlatform.VERSION_1_1); Native.release(destroyCallback); if (notify != null) { - destroyCallback = CLNotify.call(notify, CLMemory::create); + destroyCallback = CLNotify.call(notify, (x) -> this); try { int res = clSetMemObjectDestructorCallback(addr(), destroyCallback.addr(), MemoryAddress.NULL); @@ -221,16 +216,16 @@ public abstract class CLMemory extends CLExtendable { * @return An interface to the context this memory was created on. */ public CLContext getContext() { - return getInfoAny(CL_MEM_CONTEXT, clGetMemObjectInfo, CLContext::new); + return getInfoAny(CL_MEM_CONTEXT, clGetMemObjectInfo, (x) -> new CLContext(x, getObjectPlatform())); } /** - * Get CL_MEM_ASSOCIATED_MEMOBJECT for a sub-buffer. + * Get CL_MEM_ASSOCIATED_MEMOBJECT for a sub-buffer or image. * * @return */ - public CLBuffer getAssociatedMemObject() { - return getInfoAny(CL_MEM_ASSOCIATED_MEMOBJECT, clGetMemObjectInfo, CLBuffer::new); + public CLMemory getAssociatedMemObject() { + return getInfoAny(CL_MEM_ASSOCIATED_MEMOBJECT, clGetMemObjectInfo, (x) -> create(x, getObjectPlatform())); } /** @@ -242,43 +237,81 @@ public abstract class CLMemory extends CLExtendable { return getInfoSizeT(CL_MEM_OFFSET); } - protected GLSharing getGLSharing() { - return getExtension(CLPlatform.cl_khr_gl_sharing, (p) -> { - if (getContext().getDevices()[0].hasDeviceExtension(GLSharing.NAME)) - return new GLSharing(p); - else - throw new UnsupportedOperationException(); - }); + /* ********************************************************************** */ + + protected GLext getGLext() { + return getObjectPlatform() + .getExtension(CLPlatform.cl_gl_ext, (p) + -> new GLext(p::clGetExtensionFunctionAddressForPlatform)); + } + + public static class GLObjectInfo { + + public int gl_object_type; + public int gl_object_name; + + public GLObjectInfo() { + } + + public GLObjectInfo(int gl_object_type, int gl_object_name) { + this.gl_object_type = gl_object_type; + this.gl_object_name = gl_object_name; + } + } + + /** + * @since cl_khr_gl_sharing + */ + public GLObjectInfo getGLObjectInfo() { + try (Allocator frame = Memory.stack()) { + MemoryAddress ctype = frame.alloca(8); + MemoryAddress cname = frame.alloca(8); + MemoryAddress ce; + int res; + + res = getGLext().clGetGLObjectInfo(addr(), ctype, cname); + if (res != 0) + throw new CLRuntimeException(res); + return new GLObjectInfo(Native.getInt(ctype), Native.getInt(cname)); + } catch (RuntimeException | Error t) { + throw t; + } catch (Throwable t) { + throw new RuntimeException(t); + } } /** * @since cl_khr_gl_sharing */ - public GLSharing.GLObjectInfo getGLObjectInfo() { - return getGLSharing().getGLObjectInfo(this); + public int getGLTextureInfoInt(int param) { + try (Allocator frame = Memory.stack()) { + return Native.getInt(getInfo(addr(), CL_GL_TEXTURE_TARGET, getGLext().clGetGLTextureInfo, frame, 4)); + } } /** * @since cl_khr_gl_sharing */ public int getGLTextureTarget() { - return getGLSharing().getGLTextureTarget(this); + return getGLTextureInfoInt(CL_GL_TEXTURE_TARGET); } /** * @since cl_khr_gl_sharing */ - public int getGLMIPMapLevelTextureTarget() { - return getGLSharing().getGLMIPMapLevel(this); + public int getGLMIPMapLevel() { + return getGLTextureInfoInt(CL_GL_MIPMAP_LEVEL); } /** * @since cl_khr_gl_sharing */ public int getGLNumSamples() { - return getGLSharing().getGLNumSamples(this); + return getGLTextureInfoInt(CL_GL_NUM_SAMPLES); } + /* ********************************************************************** */ + /** * Allocates a buffer suitable for opencl use - sets the byte order. * diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java b/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java index 6619c11..a037012 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLObject.java @@ -45,6 +45,33 @@ import api.Memory; */ public abstract class CLObject extends Native { + /** + * Cache the platform this object belongs to, for api checking. + * Classes can use getObjectPlatform to look it up. If + * they need to support null platform at createion (e.g. clevent) + * they must override it. + */ + protected CLPlatform platform; + + /** + * Instantiate a new CLObject. + * + * @param p address of object. May be MemoryAddress.NULL if required. + * @param platform This must not be null. + */ + protected CLObject(MemoryAddress p, CLPlatform platform) { + super(p); + + if (platform == null) + throw new NullPointerException(); + + this.platform = platform; + } + + /** + * If this constructor is called then getObjectPlatform() must + * be overridden. + */ protected CLObject(MemoryAddress p) { super(p); } @@ -60,10 +87,22 @@ public abstract class CLObject extends Native { release(o); } + CLPlatform getObjectPlatform() { + return platform; + } + + protected void requireAPIVersion(int required) throws UnsupportedOperationException { + getObjectPlatform().requireAPIVersion(required); + } + + protected boolean haveAPIVersion(int desired) { + return getObjectPlatform().haveAPIVersion(desired); + } + + // some are static for access before object instantiation + // new 5-param version - // this one is static so it can be accessed at creation time - // public so extensions can see it, move to internal package - public static MemoryAddress getInfo(MemoryAddress self, int id, MethodHandle getInfo, Allocator frame, long size) throws CLRuntimeException { + static MemoryAddress getInfo(MemoryAddress self, int id, MethodHandle getInfo, Allocator frame, long size) throws CLRuntimeException { try { MemoryAddress addr = frame.alloca(size); int res; @@ -109,19 +148,19 @@ public abstract class CLObject extends Native { } // new 5-param version for get any - protected MemorySegment getInfoAny(int id, MethodHandle getInfo, Allocator a) throws CLRuntimeException { + static MemorySegment getInfoAny(MemoryAddress addr, int id, MethodHandle getInfo, Allocator a) throws CLRuntimeException { try { MemoryAddress sizep = a.alloca(8); MemorySegment valp; long size; int res; - res = (int)getInfo.invokeExact(addr(), id, 0L, MemoryAddress.NULL, sizep); + res = (int)getInfo.invokeExact(addr, id, 0L, MemoryAddress.NULL, sizep); size = getLong(sizep); valp = a.allocs(size); - res = (int)getInfo.invokeExact(addr(), id, size, valp.baseAddress(), sizep); + res = (int)getInfo.invokeExact(addr, id, size, valp.baseAddress(), sizep); if (res != 0) throw new CLRuntimeException(res); @@ -136,31 +175,31 @@ public abstract class CLObject extends Native { protected byte[] getInfoByteV(int id, MethodHandle getInfo) { try (Allocator a = Memory.stack()) { - return getInfoAny(id, getInfo, a).toByteArray(); + return getInfoAny(addr(), id, getInfo, a).toByteArray(); } } protected String getInfoString(int id, MethodHandle getInfo) { try (Allocator a = Memory.stack()) { - return infoToString(getInfoAny(id, getInfo, a)); + return infoToString(getInfoAny(addr(), id, getInfo, a)); } } protected T[] getInfoPropertyV(int id, MethodHandle getInfo, BiFunction create, IntFunction createArray) { try (Allocator a = Memory.stack()) { - return CLProperty.fromNative(getInfoAny(id, getInfo, a), create, createArray); + return CLProperty.fromNative(getInfoAny(addr(), id, getInfo, a), create, createArray); } } protected long[] getInfoLongV(int id, MethodHandle getInfo) { try (Allocator a = Memory.stack()) { - return Native.toLongV(getInfoAny(id, getInfo, a)); + return Native.toLongV(getInfoAny(addr(), id, getInfo, a)); } } protected T[] getInfoAnyV(int id, MethodHandle getInfo, Function create, IntFunction createArray) { try (Allocator a = Memory.stack()) { - return Native.toObjectV(getInfoAny(id, getInfo, a), create, createArray); + return Native.toObjectV(getInfoAny(addr(), id, getInfo, a), create, createArray); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLPipe.java b/src/notzed.zcl/classes/au/notzed/zcl/CLPipe.java index 5f2c613..58f6f93 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLPipe.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLPipe.java @@ -28,12 +28,12 @@ import java.lang.invoke.MethodHandle; */ public class CLPipe extends CLMemory { - public CLPipe(MemoryAddress p) { - this(p, null); + public CLPipe(MemoryAddress p, CLPlatform platform) { + super(p, platform, null); } - public CLPipe(MemoryAddress p, MemorySegment seg) { - super(p, seg); + public CLPipe(MemoryAddress p, CLPlatform platform, MemorySegment seg) { + super(p, platform, seg); } @Override diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLPlatform.java b/src/notzed.zcl/classes/au/notzed/zcl/CLPlatform.java index 64e6971..e6530ad 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLPlatform.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLPlatform.java @@ -70,11 +70,19 @@ public class CLPlatform extends CLObject { return clGetPlatformInfo; } - public static void requireAPIVersion(int apiVersion, int required) throws UnsupportedOperationException { + CLPlatform getObjectPlatform() { + return this; + } + + public void requireAPIVersion(int required) throws UnsupportedOperationException { if (apiVersion < required) throw new UnsupportedOperationException("Requires version " + ((apiVersion >> 8) & 0xff) + "." + (apiVersion & 0xff)); } + public boolean haveAPIVersion(int desired) { + return apiVersion >= desired; + } + /** * Calls clGetPlatformIDs. * @@ -132,7 +140,7 @@ public class CLPlatform extends CLObject { res = (int)clGetDeviceIDs.invokeExact(addr(), type, len, list, lenp); - return toObjectV(list, new CLDevice[len], CLDevice::new); + return toObjectV(list, new CLDevice[len], (d) -> new CLDevice(d, this)); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -276,20 +284,14 @@ public class CLPlatform extends CLObject { } /** - * GL sharing extension id. + * The GL extensions */ - public static final int cl_khr_gl_sharing = 0; - /** - * GL event extension id - */ - public static final int cl_khr_gl_event = 1; + static final int cl_gl_ext = 0; /** * Tracks extension per platform. - *

- * Extensions are bound to platforms. */ - private final CLExtension[] extension = new CLExtension[2]; + private final Object[] extension = new Object[2]; /** * Retrieve an extension by extension id. @@ -303,22 +305,12 @@ public class CLPlatform extends CLObject { * TODO: pass the class, constructor.invoke? id = getfield()? */ @SuppressWarnings("unchecked") - T getExtension(int id, Function create) { + T getExtension(int id, Function create) { synchronized (extension) { T x = (T)extension[id]; - if (x == null) { - // or constructor.invoke - switch (id) { - case cl_khr_gl_sharing: - x = create.apply(this); - break; - case cl_khr_gl_event: - x = create.apply(this); - break; - } - extension[id] = x; - } + if (x == null) + extension[id] = x = create.apply(this); return x; } diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLProgram.java b/src/notzed.zcl/classes/au/notzed/zcl/CLProgram.java index ef63a40..a5dc91f 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLProgram.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLProgram.java @@ -31,12 +31,8 @@ import java.util.stream.LongStream; */ public class CLProgram extends CLObject { - CLProgram(MemoryAddress p) { - super(p); - } - - public static CLProgram create(MemoryAddress p) { - return Native.resolve(p, CLProgram::new); + CLProgram(MemoryAddress p, CLPlatform platform) { + super(p, platform); } private static void release(MemoryAddress p) { @@ -62,7 +58,7 @@ public class CLProgram extends CLObject { */ public void buildProgram(CLDevice[] devices, String options, CLNotify notify) throws CLException { try (Allocator frame = Memory.stack(); - Callback> call = CLNotify.call(notify, CLProgram::new)) { + Callback> call = CLNotify.call(notify, (x) -> new CLProgram(x, getObjectPlatform()))) { MemoryAddress pdevs = toAddrV(frame, devices); MemoryAddress poptions = toByteV(frame, options); int res; @@ -111,6 +107,9 @@ public class CLProgram extends CLObject { */ public void compileProgram(CLDevice[] devices, String options, CLProgram[] headers, String[] header_names, CLNotify notify) throws CLException, UnsupportedOperationException { int nheaders = 0; + + requireAPIVersion(CLPlatform.VERSION_1_2); + if (headers != null && header_names != null) { if (headers.length != header_names.length) throw new IllegalArgumentException(); @@ -120,7 +119,7 @@ public class CLProgram extends CLObject { } try (Allocator frame = Memory.stack(); - Callback> call = CLNotify.call(notify, CLProgram::new)) { + Callback> call = CLNotify.call(notify, (x) -> new CLProgram(x, getObjectPlatform()))) { MemoryAddress cdevs = toAddrV(frame, devices); MemoryAddress coptions = toByteV(frame, options); MemoryAddress cheaders = toAddrV(frame, headers); @@ -158,7 +157,7 @@ public class CLProgram extends CLObject { res = getInt(pres); if (res != 0) throw new CLException(res); - return resolve(ck, CLKernel::new); + return resolve(ck, (x) -> new CLKernel(x, getObjectPlatform())); } catch (CLException | RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -185,7 +184,7 @@ public class CLProgram extends CLObject { throw new CLRuntimeException(); size = getInt(csize); - return Native.toObjectV(ckern, new CLKernel[size], CLKernel::new); + return Native.toObjectV(ckern, new CLKernel[size], (x) -> new CLKernel(x, getObjectPlatform())); } catch (RuntimeException | Error t) { throw t; } catch (Throwable t) { @@ -194,7 +193,7 @@ public class CLProgram extends CLObject { } public CLContext getContext() { - return getInfoAny(CL_PROGRAM_CONTEXT, clGetProgramInfo, CLContext::new); + return getInfoAny(CL_PROGRAM_CONTEXT, clGetProgramInfo, (x) -> new CLContext(x, getObjectPlatform())); } public int getNumDevices() { @@ -202,7 +201,7 @@ public class CLProgram extends CLObject { } public CLDevice[] getDevices() { - return getInfoAnyV(CL_PROGRAM_DEVICES, clGetProgramInfo, CLDevice::new, CLDevice[]::new); + return getInfoAnyV(CL_PROGRAM_DEVICES, clGetProgramInfo, (x) -> new CLDevice(x, getObjectPlatform()), CLDevice[]::new); } public String getSource() { diff --git a/src/notzed.zcl/classes/au/notzed/zcl/CLSampler.java b/src/notzed.zcl/classes/au/notzed/zcl/CLSampler.java index 736f9d1..7bb6a2c 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/CLSampler.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/CLSampler.java @@ -26,8 +26,8 @@ import java.lang.invoke.MethodHandle; */ public class CLSampler extends CLObject { - public CLSampler(MemoryAddress p) { - super(p); + public CLSampler(MemoryAddress p, CLPlatform platform) { + super(p, platform); } @Override @@ -56,7 +56,7 @@ public class CLSampler extends CLObject { } public CLContext getContext() { - return getInfoAny(CL_SAMPLER_CONTEXT, clGetSamplerInfo, CLContext::new); + return getInfoAny(CL_SAMPLER_CONTEXT, clGetSamplerInfo, (x) -> new CLContext(x, getObjectPlatform())); } public boolean getNormalisedCoords() { diff --git a/src/notzed.zcl/classes/au/notzed/zcl/internal/EventInfo.java b/src/notzed.zcl/classes/au/notzed/zcl/EventInfo.java similarity index 96% rename from src/notzed.zcl/classes/au/notzed/zcl/internal/EventInfo.java rename to src/notzed.zcl/classes/au/notzed/zcl/EventInfo.java index 53236bb..e6c68bf 100644 --- a/src/notzed.zcl/classes/au/notzed/zcl/internal/EventInfo.java +++ b/src/notzed.zcl/classes/au/notzed/zcl/EventInfo.java @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -package au.notzed.zcl.internal; +package au.notzed.zcl; import jdk.incubator.foreign.*; import au.notzed.zcl.CLEventList; @@ -29,7 +29,7 @@ import api.Allocator; * structure. If the command succeeds, then call post(). * */ -public class EventInfo { +class EventInfo { public final int nwait; public final MemoryAddress wait; public final MemoryAddress event; diff --git a/src/notzed.zcl/classes/au/notzed/zcl/khr/GLEvent.java b/src/notzed.zcl/classes/au/notzed/zcl/khr/GLEvent.java deleted file mode 100644 index 3fa07ce..0000000 --- a/src/notzed.zcl/classes/au/notzed/zcl/khr/GLEvent.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (C) 2015 notzed - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package au.notzed.zcl.khr; - -import au.notzed.zcl.CLContext; -import au.notzed.zcl.CLEvent; -import au.notzed.zcl.CLExtension; -import au.notzed.zcl.CLPlatform; -import au.notzed.zcl.CLRuntimeException; -import au.notzed.zcl.CLext; - -import jdk.incubator.foreign.MemoryAddress; -import java.lang.invoke.MethodHandle; -import java.util.function.Function; -import api.*; - -/** - * cl_khr_gl_event extension interface. - */ -public class GLEvent implements CLExtension { - - private final CLPlatform plat; - private final MethodHandle clCreateEventFromGLsyncKHR; - - public GLEvent(CLPlatform plat) { - Function find = plat::clGetExtensionFunctionAddressForPlatform; - - this.clCreateEventFromGLsyncKHR = CLext.clCreateEventFromGLsyncKHR(find); - this.plat = plat; - } - - @Override - public String getName() { - return NAME; - } - - public final static int ID = CLPlatform.cl_khr_gl_event; - public final static String NAME = "cl_khr_gl_event"; - - public static final int CL_COMMAND_GL_FENCE_SYNC_OBJECT_KHR = 0x200D; - - public CLEvent clCreateEventFromGLsync(CLContext ctx, MemoryAddress glsync) { - try (Allocator frame = Memory.stack()) { - MemoryAddress cret = frame.alloca(8); - MemoryAddress ce; - int res; - - ce = (MemoryAddress)clCreateEventFromGLsyncKHR.invokeExact(ctx.addr(), glsync, cret); - res = Native.getInt(cret); - if (res != 0) - throw new CLRuntimeException(res); - - return Native.resolve(ce, CLEvent::new); - } catch (RuntimeException | Error t) { - throw t; - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - -} diff --git a/src/notzed.zcl/classes/au/notzed/zcl/khr/GLSharing.java b/src/notzed.zcl/classes/au/notzed/zcl/khr/GLSharing.java deleted file mode 100644 index 4f98358..0000000 --- a/src/notzed.zcl/classes/au/notzed/zcl/khr/GLSharing.java +++ /dev/null @@ -1,302 +0,0 @@ -/* - * Copyright (C) 2015 notzed - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package au.notzed.zcl.khr; - -import static au.notzed.zcl.CLext.*; -import au.notzed.zcl.CLBuffer; -import au.notzed.zcl.CLCommandQueue; -import au.notzed.zcl.CLContext; -import au.notzed.zcl.CLContextProperty; -import au.notzed.zcl.CLEventList; -import au.notzed.zcl.CLExtension; -import au.notzed.zcl.CLImage; -import au.notzed.zcl.CLMemory; -import au.notzed.zcl.CLPlatform; -import au.notzed.zcl.CLObject; -import au.notzed.zcl.CLRuntimeException; -import au.notzed.zcl.internal.EventInfo; - -import jdk.incubator.foreign.MemoryAddress; -import java.lang.invoke.MethodHandle; -import java.util.function.Function; -import api.*; - -/** - * cl_khr_gl_sharing extension interface. - */ -public class GLSharing implements CLExtension { - private final CLPlatform plat; - - private final MethodHandle clCreateFromGLBuffer; - private final MethodHandle clCreateFromGLTexture; - private final MethodHandle clCreateFromGLRenderbuffer; - private final MethodHandle clEnqueueAcquireGLObjects; - private final MethodHandle clEnqueueReleaseGLObjects; - private final MethodHandle clGetGLContextInfoKHR; - private final MethodHandle clGetGLObjectInfo; - private final MethodHandle clGetGLTextureInfo; - - public GLSharing(CLPlatform plat) { - Function find = plat::clGetExtensionFunctionAddressForPlatform; - - this.clCreateFromGLBuffer = clCreateFromGLBuffer(find); - this.clCreateFromGLTexture = clCreateFromGLTexture(find); - this.clCreateFromGLRenderbuffer = clCreateFromGLRenderbuffer(find); - this.clEnqueueAcquireGLObjects = clEnqueueAcquireGLObjects(find); - this.clEnqueueReleaseGLObjects = clEnqueueReleaseGLObjects(find); - this.clGetGLContextInfoKHR = clGetGLContextInfoKHR(find); - this.clGetGLObjectInfo = clGetGLObjectInfo(find); - this.clGetGLTextureInfo = clGetGLTextureInfo(find); - this.plat = plat; - } - - @Override - public String getName() { - return NAME; - } - - public final static int ID = CLPlatform.cl_khr_gl_sharing; - public final static String NAME = "cl_khr_gl_sharing"; - - /* cl_gl_object_type = 0x2000 - 0x200F enum values are currently taken */ - public static final int CL_GL_OBJECT_BUFFER = 0x2000; - public static final int CL_GL_OBJECT_TEXTURE2D = 0x2001; - public static final int CL_GL_OBJECT_TEXTURE3D = 0x2002; - public static final int CL_GL_OBJECT_RENDERBUFFER = 0x2003; - public static final int CL_GL_OBJECT_TEXTURE2D_ARRAY = 0x200E; - public static final int CL_GL_OBJECT_TEXTURE1D = 0x200F; - public static final int CL_GL_OBJECT_TEXTURE1D_ARRAY = 0x2010; - public static final int CL_GL_OBJECT_TEXTURE_BUFFER = 0x2011; - - /* cl_gl_texture_info */ - public static final int CL_GL_TEXTURE_TARGET = 0x2004; - public static final int CL_GL_MIPMAP_LEVEL = 0x2005; - public static final int CL_GL_NUM_SAMPLES = 0x2012; - - /* Additional Error Codes */ - public static final int CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR = -1000; - - /* cl_gl_context_info */ - public static final int CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR = 0x2006; - public static final int CL_DEVICES_FOR_GL_CONTEXT_KHR = 0x2007; - - /* Additional cl_context_properties */ - public static final int CL_GL_CONTEXT_KHR = 0x2008; - public static final int CL_EGL_DISPLAY_KHR = 0x2009; - public static final int CL_GLX_DISPLAY_KHR = 0x200A; - public static final int CL_WGL_HDC_KHR = 0x200B; - public static final int CL_CGL_SHAREGROUP_KHR = 0x200C; - - public static CLContextProperty GL_CONTEXT_KHR(long id) { - return new CLContextProperty.TagValue(CL_GL_CONTEXT_KHR, id); - } - - public static CLContextProperty EGL_DISPLAY_KHR(long id) { - return new CLContextProperty.TagValue(CL_EGL_DISPLAY_KHR, id); - } - - public static CLContextProperty GLX_DISPLAY_KHR(long id) { - return new CLContextProperty.TagValue(CL_GLX_DISPLAY_KHR, id); - } - - public static CLContextProperty WGL_HDC_KHR(long id) { - return new CLContextProperty.TagValue(CL_WGL_HDC_KHR, id); - } - - public static CLContextProperty CGL_SHAREGROUP_KHR(long id) { - return new CLContextProperty.TagValue(CL_CGL_SHAREGROUP_KHR, id); - } - - public CLBuffer createFromGLBuffer(CLContext ctx, - long flags, - int bufobj) { - try (Allocator frame = Memory.stack()) { - MemoryAddress cres = frame.alloca(8); - MemoryAddress ce; - int res; - - ce = (MemoryAddress)clCreateFromGLBuffer.invokeExact(ctx.addr(), flags, bufobj, cres); - res = Native.getInt(cres); - if (res != 0) - throw new CLRuntimeException(res); - return Native.resolve(ce, CLBuffer::new); - } catch (RuntimeException | Error t) { - throw t; - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - - public CLImage createFromGLTexture(CLContext ctx, - long flags /* flags */, - int target /* target */, - int miplevel /* miplevel */, - int texture /* texture */) { - try (Allocator frame = Memory.stack()) { - MemoryAddress cres = frame.alloca(8); - MemoryAddress ce; - int res; - - ce = (MemoryAddress)clCreateFromGLTexture.invokeExact(ctx.addr(), flags, target, miplevel, texture, cres); - res = Native.getInt(cres); - if (res != 0) - throw new CLRuntimeException(res); - return Native.resolve(ce, CLImage::new); - } catch (RuntimeException | Error t) { - throw t; - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - - public CLImage createFromGLRenderbuffer(CLContext ctx /* context */, - long flags /* flags */, - int renderbuffer /* renderbuffer */) { - try (Allocator frame = Memory.stack()) { - MemoryAddress cres = frame.alloca(8); - MemoryAddress ce; - int res; - - ce = (MemoryAddress)clCreateFromGLRenderbuffer.invokeExact(ctx.addr(), flags, renderbuffer, cres); - res = Native.getInt(cres); - if (res != 0) - throw new CLRuntimeException(res); - return Native.resolve(ce, CLImage::new); - } catch (RuntimeException | Error t) { - throw t; - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - - public static class GLObjectInfo { - - public int gl_object_type; - public int gl_object_name; - - public GLObjectInfo() { - } - - public GLObjectInfo(int gl_object_type, int gl_object_name) { - this.gl_object_type = gl_object_type; - this.gl_object_name = gl_object_name; - } - } - - public GLObjectInfo getGLObjectInfo(CLMemory mem) { - try (Allocator frame = Memory.stack()) { - MemoryAddress ctype = frame.alloca(8); - MemoryAddress cname = frame.alloca(8); - MemoryAddress ce; - int res; - - res = (int)clGetGLObjectInfo.invokeExact(mem.addr(), ctype, cname); - if (res != 0) - throw new CLRuntimeException(res); - return new GLObjectInfo(Native.getInt(ctype), Native.getInt(cname)); - } catch (RuntimeException | Error t) { - throw t; - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - - public int getGLTextureInfoInt(CLMemory mem, int param) { - try (Allocator frame = Memory.stack()) { - return Native.getInt(CLObject.getInfo(mem.addr(), CL_GL_TEXTURE_TARGET, clGetGLTextureInfo, frame, 4)); - } - } - - public int getGLTextureTarget(CLMemory mem) { - return getGLTextureInfoInt(mem, CL_GL_TEXTURE_TARGET); - } - - public int getGLMIPMapLevel(CLMemory mem) { - return getGLTextureInfoInt(mem, CL_GL_MIPMAP_LEVEL); - } - - public int getGLNumSamples(CLMemory mem) { - return getGLTextureInfoInt(mem, CL_GL_NUM_SAMPLES); - } - - public void enqueueAcquireGLObjects( - CLCommandQueue queue /* command_queue */, - CLMemory[] mem_objects /* mem_objects */, - CLEventList wait, - CLEventList event) { - try (Allocator frame = Memory.stack()) { - EventInfo info = new EventInfo(frame, wait, event); - MemoryAddress cmem_objects = Native.toAddrV(frame, mem_objects); - int res; - - res = (int)clEnqueueAcquireGLObjects.invokeExact(queue.addr(), mem_objects.length, cmem_objects, - info.nwait, info.wait, info.event); - if (res != 0) - throw new CLRuntimeException(res); - - info.post(event); - } catch (RuntimeException | Error t) { - throw t; - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - - public void enqueueReleaseGLObjects( - CLCommandQueue queue /* command_queue */, - CLMemory[] mem_objects /* mem_objects */, - CLEventList wait, - CLEventList event) { - try (Allocator frame = Memory.stack()) { - EventInfo info = new EventInfo(frame, wait, event); - MemoryAddress cmem_objects = Native.toAddrV(frame, mem_objects); - int res; - - res = (int)clEnqueueReleaseGLObjects.invokeExact(queue.addr(), mem_objects.length, cmem_objects, - info.nwait, info.wait, info.event); - if (res != 0) - throw new CLRuntimeException(res); - - info.post(event); - } catch (RuntimeException | Error t) { - throw t; - } catch (Throwable t) { - throw new RuntimeException(t); - } - } - - /* - native T getGLContextInfoKHRAny( - CLContextProperty[] properties - int ctype, - int param_name) throws CLRuntimeException; - - native T getGLContextInfoKHRAnyV( - CLContextProperty[] properties - int ctype, - int param_name) throws CLRuntimeException; - */ - /* - public CLDevice getCurrendDeviceForGLConextKHR(CLContextProperty[] properties) throws CLRuntimeException { - return getGLContextInfoKHRAny(properties, CLObject.CTYPE_DEVICE, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR); - } - - public CLDevice[] getDevicesForGLConextKHR(CLContextProperty[] properties) throws CLRuntimeException { - return getGLContextInfoKHRAnyV(properties, CLObject.CTYPE_DEVICE, CL_DEVICES_FOR_GL_CONTEXT_KHR); - }*/ - -} diff --git a/src/notzed.zcl/classes/module-info.java b/src/notzed.zcl/classes/module-info.java index 4f375e6..355b012 100644 --- a/src/notzed.zcl/classes/module-info.java +++ b/src/notzed.zcl/classes/module-info.java @@ -24,7 +24,6 @@ module notzed.zcl { requires transitive jdk.incubator.foreign; exports au.notzed.zcl; - exports au.notzed.zcl.khr; - + exports api to notzed.zcl.demo; } diff --git a/src/notzed.zcl/gen/gen.make b/src/notzed.zcl/gen/gen.make index 8a62902..7900143 100644 --- a/src/notzed.zcl/gen/gen.make +++ b/src/notzed.zcl/gen/gen.make @@ -17,13 +17,13 @@ notzed.zcl_generated = \ notzed.zcl_JAVA_GENERATED = \ $(notzed.zcl_generated) \ - au/notzed/zcl/CLext.java + au/notzed/zcl/GLext.java $(notzed.zcl_genjavadir)/au/notzed/zcl/CL.java: src/notzed.zcl/include/CL/cl.h $(export_defines) @install -d $(@D) perl $(export_defines) $< > $@ || rm -f $@ -$(addprefix $(notzed.zcl_genjavadir)/,$(notzed.zcl_generated)): $(generate_api) $(opencl_pm) +$(addprefix $(notzed.zcl_genjavadir)/,$(notzed.zcl_generated)): $(generate_api) $(opencl_pm) src/notzed.zcl/gen/gen.make perl $(generate_api) \ -d $(notzed.zcl_genjavadir) \ -t au.notzed.zcl \ @@ -39,14 +39,14 @@ $(addprefix $(notzed.zcl_genjavadir)/,$(notzed.zcl_generated)): $(generate_api) -r _cl_image_desc=CLImageDesc \ -r _cl_image_format=CLImageFormat \ --raw-calls \ - -c CLLib -lOpenCL --func-file src/notzed.zcl/gen/opencl.txt \ + -c CLLib -lOpenCL -e '^$$' --func-file src/notzed.zcl/gen/opencl.txt \ ./$(opencl_pm) -$(addprefix $(notzed.zcl_genjavadir)/,au/notzed/zcl/CLext.java): $(generate_api) $(opencl_pm) +$(addprefix $(notzed.zcl_genjavadir)/,au/notzed/zcl/GLext.java): $(generate_api) $(opencl_pm) src/notzed.zcl/gen/gen.make perl $(generate_api) \ -d $(notzed.zcl_genjavadir) \ -t au.notzed.zcl \ --raw-calls \ - -c CLext --func-file src/notzed.zcl/gen/opencl-ext.txt \ + -c GLext --func-file src/notzed.zcl/gen/opencl-glext.txt \ --no-types \ ./$(opencl_pm) diff --git a/src/notzed.zcl/gen/generate-api b/src/notzed.zcl/gen/generate-api index 07e397f..f713506 100755 --- a/src/notzed.zcl/gen/generate-api +++ b/src/notzed.zcl/gen/generate-api @@ -967,65 +967,75 @@ END } } - if (@libs) { - # function handles - for $k (sort(findDefinition(\%data, 'func', @match))) { - my %func = %{$data{$k}}; - my @params = @{$func{arguments}}; - my $signature = funcSignature(\%func); - my $name = ($func{name}); + # function handles + my $callstatic = (@libs) ? "static " : ""; - print $dst "\tpublic final static MethodHandle $name;\n"; - } + for $k (sort(findDefinition(\%data, 'func', @match))) { + my %func = %{$data{$k}}; + my @params = @{$func{arguments}}; + my $signature = funcSignature(\%func); + my $name = ($func{name}); - # function handle init - print $dst "\tstatic {\n"; - print $dst "\t\tLibraryLookup[] libs = Native.loadLibraries(libraries);\n"; + print $dst "\tpublic final $callstatic MethodHandle $name;\n"; + } + # function handle init + { + if (@libs) { + # static function handle init + print $dst "\tstatic {\n"; + print $dst "\t\tLibraryLookup[] ctx = Native.loadLibraries(libraries);\n"; + } else { + # address factory handle init + print $dst "\tpublic $class{name}(Function ctx) {\n"; + } for $k (sort(findDefinition(\%data, 'func', @match))) { my %func = %{$data{$k}}; my @params = @{$func{arguments}}; my $signature = funcSignature(\%func); my $name = ($func{name}); - print $dst "\t\t$name = Native.downcallHandle(libs, \"$name\", \"$signature\");\n"; + print $dst "\t\t$name = Native.downcallHandle(ctx, \"$name\", \"$signature\");\n"; } print $dst "\t}\n"; + } - # function handle invocation - for $k (sort(findDefinition(\%data, 'func', @match))) { - my %func = %{$data{$k}}; - my @params = @{$func{arguments}}; - my $signature = funcSignature(\%func); - my $name = ($func{name}); - my %res = %{$func{result}}; - my $result = typeToRaw(\%res); + # function handle invocation + for $k (sort(findDefinition(\%data, 'func', @match))) { + my %func = %{$data{$k}}; + my @params = @{$func{arguments}}; + my $signature = funcSignature(\%func); + my $name = ($func{name}); + my %res = %{$func{result}}; + my $result = typeToRaw(\%res); - print $dst "\tpublic static $result $name("; + print $dst "\tpublic $callstatic $result $name("; - for $pi (@params) { - my %param = %{$pi}; - my $type = typeToRaw($pi); + for $pi (@params) { + my %param = %{$pi}; + my $type = typeToRaw($pi); - print $dst "$type $param{name}"; - print $dst ", " if ($pi != $params[$#params]); - } + print $dst "$type $param{name}"; + print $dst ", " if ($pi != $params[$#params]); + } - print $dst ") throws Throwable {\n"; - if ($result ne "void") { - print $dst "return ($result)"; - } - print $dst "$name.invokeExact("; - for $pi (@params) { - my %param = %{$pi}; + print $dst ") throws Throwable {\n"; + print $dst "\t\t"; + if ($result ne "void") { + print $dst "return ($result)"; + } + print $dst "$name.invokeExact("; + for $pi (@params) { + my %param = %{$pi}; - print $dst "$param{name}"; - print $dst ", " if ($pi != $params[$#params]); - } - print $dst ");\n"; - print $dst "\t}\n\n"; + print $dst "$param{name}"; + print $dst ", " if ($pi != $params[$#params]); } - } else { + print $dst ");\n"; + print $dst "\t}\n\n"; + } + + if (0) { # function handle factories for $k (sort(findDefinition(\%data, 'func', @match))) { my %func = %{$data{$k}}; diff --git a/src/notzed.zcl/gen/opencl-ext.txt b/src/notzed.zcl/gen/opencl-glext.txt similarity index 100% rename from src/notzed.zcl/gen/opencl-ext.txt rename to src/notzed.zcl/gen/opencl-glext.txt