gpu / JOCL

Java bindings for OpenCL
http://www.jocl.org
Other
183 stars 33 forks source link

cl_APPLE_gl_sharing bindings? #35

Closed LlemonDuck closed 3 years ago

LlemonDuck commented 3 years ago

In order to get a compute context from for CL-GL memory sharing on apple devices, they have their own flow

Relevant Apple Tech Note Listings 4 and 5

In particular, the components that I found necessary were the context property flag CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE = 268435456, and the method clGetGLContextInfoAPPLE along with its own parameter values. With just this one method, the rest of the typical CL-GL init flow can be used.

Are there any existing bindings for these/plans to add them?

gpu commented 3 years ago

I wasn't ware of Apple doing their own thing there, or what the reasoning and processes are that led to this function existing in the first place (I mean, clGetGLContextInfoKHR should have been enough to "emulate" the functionality that they needed).

I can have a look to see how this could sensibly be added.

But a (possibly important) a note: I had a short look at the linked issue, and it appears to be about "the other JOCL" - are you sure that wasn't supposed to be asked in https://github.com/JogAmp/jocl ?

LlemonDuck commented 3 years ago

Thanks for the response. That other thread has been a bit of a shitshow.

I know for sure I'm working with your JOCL lib, I find it much nicer than JogAmp's.

As of now, I'd prefer not to switch over to jogamp, but I think they do already have the binding, so it may be necessary if we can't get it added here.

I forked this project to try and add the binding myself, but I have to admit I'm not great with JNI and progress has been slow.

gpu commented 3 years ago

Adding the function itself should probably be relatively simple, following the pattern of the existing functions. (But admittedly: The last update for JOCL that I did on this level has been a while, so I'll also have to re-orient myself a bit). A question on top of that it whether I would add it directly in the CL class, because it is a vendor-specific function. I'd consider adding it in some CLApple class or so, throwing an exception if it is called on non-apple platforms, but I'll have to think about the best solution first. I'll try to allocate some time for that durint the weekend.

LlemonDuck commented 3 years ago

Thank you, that would be immensely helpful. In case you don't have offhand access to a mac, here's the relevant section of the header file (cl_gl_ext.h in /Library/Developer/CommandLineTools/SDKs/MacOSX11.1.sdk/System/Library/Frameworks/OpenCL.framework/Versions/A/Headers).

It seems that these three constants + the one method are the only portions of the file unique to cl_APPLE_gl_sharing and the rest is standard cl_khr_gl_sharing.

/* Context GL sharing
 *
 * Please check for the "cl_APPLE_gl_sharing" extension using clGetDeviceInfo(CL_DEVICE_EXTENSIONS)
 * before using these extensions.

 * Apple extension for creating a CL context from a CGL share group
 *
 * This enumerated value can be specified as part of the <properties> argument passed to clCreateContext 
 * to allow OpenCL compliant devices in an existing CGL share group to be used as the devices in 
 * the newly created CL context. GL objects that were allocated in the given CGL share group can 
 * now be shared between CL and GL.
 *
 * If the <num_devices> and <devices> argument values to clCreateContext are 0 and NULL respectively,
 * all CL compliant devices in the CGL share group will be used to create the context.
 * Additional CL devices can also be specified using the <num_devices> and <devices> arguments.
 * These, however, cannot be GPU devices. On Mac OS X, you can add the CPU to the list of CL devices
 * (in addition to the CL compliant devices in the CGL share group) used to create the CL context. 
 * Note that if a CPU device is specified, the CGL share group must also include the GL float renderer; 
 * Otherwise CL_INVALID_DEVICE will be returned.
 *
 * NOTE:  Make sure that appropriate cl_gl.h header file is included separately
 */
#define CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE        0x10000000 /* Introduced in Mac OS X 10.6 */

/* Apple extension for retrieving OpenGL context information for a CL context 
 * created via the above method.
 *
 * Provides a query mechanism to retrieve OpenGL context specific information 
 * from an OpenCL context to help identify device specific mappings and usage.
 * 
 * For example, one possible usage would be to allow the client to map a CGL 
 * virtual screen index to an appropriate CL device id to insure that the 
 * rendering device and the compute device are the same, thus guaranteeing 
 * any shared OpenGL memory that is attached o a CL memory object remains 
 * resident on the active device.
 */

cl_int  clGetGLContextInfoAPPLE ( cl_context __nonnull /* context */,
                                  void * __nonnull /* platform_gl_ctx */,
                                  cl_gl_platform_info /* param_name */,
                                  size_t /* param_value_size */,
                                  void *  __nullable /* param_value */,
                                  size_t * __nullable /* param_value_size_ret */)  CL_EXT_SUFFIX__VERSION_1_0 CL_DEPRECATED(10.6, 10.14);  

/* The list of supported param_name values and the information returned in param_value by 
 * clGetContextInfo are listed below:

 * Returns a cl_device_id for the CL device associated with the virtual screen for 
 * the given CGL context.  Return type: cl_device_id
 */
#define CL_CGL_DEVICE_FOR_CURRENT_VIRTUAL_SCREEN_APPLE      0x10000002 /* Introduced in Mac OS X 10.6 */

/* Returns an array of cl_device_ids for the CL device(s) corresponding to 
 * the virtual screen(s) for the given CGL context.   Return type: cl_device_id[]
 */
#define CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE  0x10000003 /* Introduced in Mac OS X 10.6 */

/* Error code returned by clGetGLContextInfoAPPLE if an invalid platform_gl_ctx is provided           */
#define CL_INVALID_GL_CONTEXT_APPLE                         -1000      /* Introduced in Mac OS X 10.6 */
gpu commented 3 years ago

Thanks again for your contribution!

The updated version is available under

<dependency>
    <groupId>org.jocl</groupId>
    <artifactId>jocl</artifactId>
    <version>2.0.3</version>
</dependency>

If you encounter any problems with that, just let me know.