JOCL v2.6.0-rc-20250722
JOCL, OpenCL® API Binding for Java™ (public API).
CLGLContext.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opencl.gl;
30
31import java.nio.Buffer;
32
33import com.jogamp.opengl.GLContext;
34
35import jogamp.opengl.GLContextImpl;
36import jogamp.opengl.egl.EGLContext;
37import jogamp.opengl.macosx.cgl.CGL;
38import jogamp.opengl.macosx.cgl.MacOSXCGLContext;
39import jogamp.opengl.windows.wgl.WindowsWGLContext;
40import jogamp.opengl.x11.glx.X11GLXContext;
41
42import com.jogamp.common.nio.PointerBuffer;
43import com.jogamp.opencl.CLContext;
44import com.jogamp.opencl.CLDevice;
45import com.jogamp.opencl.CLMemory.Mem;
46import com.jogamp.opencl.CLPlatform;
47import com.jogamp.opencl.llb.CL;
48
49/**
50 * OpenCL Context supporting JOGL-JOCL interoperablity.
51 * @author Michael Bien, et al.
52 */
53public final class CLGLContext extends CLContext {
54
55 final long glID;
56 private final GLContext glContext;
57
58 private CLGLContext(final CLPlatform platform, final GLContext glContext, final long clContextID, final long glContextID, final ErrorDispatcher dispatcher) {
59 super(platform, clContextID, dispatcher);
60 this.glID = glContextID;
61 this.glContext = glContext;
62 }
63
64 /**
65 * Creates a shared context on all available devices (CL_DEVICE_TYPE_ALL).
66 * @see GLContext#makeCurrent()
67 */
68 public static CLGLContext create(final GLContext glContext) {
69 return create(glContext, (CLPlatform)null, CLDevice.Type.ALL);
70 }
71
72 /**
73 * Creates a shared context on the specified platform on all available devices (CL_DEVICE_TYPE_ALL).
74 * @see GLContext#makeCurrent()
75 */
76 public static CLGLContext create(final GLContext glContext, final CLPlatform platform) {
77 return create(glContext, platform, CLDevice.Type.ALL);
78 }
79
80 /**
81 * Creates a shared context on the specified platform and with the specified
82 * device types.
83 * @see GLContext#makeCurrent()
84 */
85 public static CLGLContext create(final GLContext glContext, final CLDevice.Type... deviceTypes) {
86 return create(glContext, null, deviceTypes);
87 }
88
89 /**
90 * Creates a shared context on the specified platform and with the specified
91 * device types.
92 * @see GLContext#makeCurrent()
93 */
94 public static CLGLContext create(final GLContext glContext, CLPlatform platform, final CLDevice.Type... deviceTypes) {
95
96 if(platform == null) {
98 }
99
100 final long[] glID = new long[1];
101 final PointerBuffer properties = setupContextProperties(platform, glContext, glID);
102 final ErrorDispatcher dispatcher = createErrorHandler();
103 final long clID = createContextFromType(platform, dispatcher, properties, toDeviceBitmap(deviceTypes));
104
105 return new CLGLContext(platform, glContext, clID, glID[0], dispatcher);
106
107 }
108
109 /**
110 * Creates a shared context on the specified platform and with the specified
111 * devices.
112 * @see GLContext#makeCurrent()
113 */
114 public static CLGLContext create(final GLContext glContext, final CLDevice... devices) {
115
116 if(devices == null) {
117 throw new IllegalArgumentException("no devices specified");
118 }else if(devices[0] == null) {
119 throw new IllegalArgumentException("first device was null");
120 }
121
123
124 final long[] glID = new long[1];
125 final PointerBuffer properties = setupContextProperties(platform, glContext, glID);
126 final ErrorDispatcher dispatcher = createErrorHandler();
127 final long clID = createContext(platform, dispatcher, properties, devices);
128
129 final CLGLContext context = new CLGLContext(platform, glContext, clID, glID[0], dispatcher);
130 if(devices != null) {
131 for (int i = 0; i < devices.length; i++) {
133 }
134 }
135 return context;
136 }
137
138
139 private static PointerBuffer setupContextProperties(final CLPlatform platform, final GLContext glContext, final long[] glID) {
140
141 if(platform == null) {
142 throw new RuntimeException("no OpenCL installation found");
143 }
144 if(glContext == null) {
145 throw new IllegalArgumentException("GLContext was null.");
146 }
147
148 // context must be current
149 if(!glContext.isCurrent()) {
150 throw new IllegalArgumentException("OpenGL context is not current,\n"+
151 " creating a OpenCL context for context sharing is not allowed in this situation.");
152 }
153
154 final GLContextImpl ctxImpl = (GLContextImpl)glContext;
155 glID[0] = glContext.getHandle();
156
157 PointerBuffer properties;
158 if(glContext instanceof X11GLXContext) {
159// spec: "When the GLX binding API is supported, the attribute
160// CL_GL_CONTEXT_KHR should be set to a GLXContext handle to an
161// OpenGL context, and the attribute CL_GLX_DISPLAY_KHR should be
162// set to the Display handle of the X Window System display used to
163// create the OpenGL context."
164 properties = PointerBuffer.allocateDirect(7);
165 final long displayHandle = ctxImpl.getDrawableImpl().getNativeSurface().getDisplayHandle();
166 properties.put(CL.CL_GL_CONTEXT_KHR).put(glID[0])
167 .put(CL.CL_GLX_DISPLAY_KHR).put(displayHandle)
168 .put(CL.CL_CONTEXT_PLATFORM).put(platform.ID);
169 }else if(glContext instanceof WindowsWGLContext) {
170// spec: "When the WGL binding API is supported, the attribute
171// CL_GL_CONTEXT_KHR should be set to an HGLRC handle to an OpenGL
172// context, and the attribute CL_WGL_HDC_KHR should be set to the
173// HDC handle of the display used to create the OpenGL context."
174 properties = PointerBuffer.allocateDirect(7);
175 final long surfaceHandle = ctxImpl.getDrawableImpl().getNativeSurface().getSurfaceHandle();
176 properties.put(CL.CL_GL_CONTEXT_KHR).put(glID[0])
177 .put(CL.CL_WGL_HDC_KHR).put(surfaceHandle)
178 .put(CL.CL_CONTEXT_PLATFORM).put(platform.ID);
179 }else if(glContext instanceof MacOSXCGLContext) {
180// spec: "When the CGL binding API is supported, the attribute
181// CL_CGL_SHAREGROUP_KHR should be set to a CGLShareGroup handle to
182// a CGL share group object."
183 /**
184 * FIXME: For all Mac OSX Versions ???
185 * CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE used to specify the GL sharing group ID
186 * on Mac OSX 10.8.4 works.
187 * Using the std. CL_CGL_SHAREGROUP_KHR on Mac OSX 10.8.4 causes the context creation
188 * to throw a CL_INVALID_VALUE error.
189 */
190 final long cgl = CGL.getCGLContext(glID[0]);
191 final long group = CGL.CGLGetShareGroup(cgl);
192 properties = PointerBuffer.allocateDirect(5);
193 properties.put(CL.CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE).put(group)
194 .put(CL.CL_CONTEXT_PLATFORM).put(platform.ID);
195 }else if(glContext instanceof EGLContext) {
196// TODO test EGL
197// spec: "When the EGL binding API is supported, the attribute
198// CL_GL_CONTEXT_KHR should be set to an EGLContext handle to an
199// OpenGL ES or OpenGL context, and the attribute
200// CL_EGL_DISPLAY_KHR should be set to the EGLDisplay handle of the
201// display used to create the OpenGL ES or OpenGL context."
202 properties = PointerBuffer.allocateDirect(7);
203 final long displayHandle = ctxImpl.getDrawableImpl().getNativeSurface().getDisplayHandle();
204 properties.put(CL.CL_GL_CONTEXT_KHR).put(glID[0])
205 .put(CL.CL_EGL_DISPLAY_KHR).put(displayHandle)
206 .put(CL.CL_CONTEXT_PLATFORM).put(platform.ID);
207 }else{
208 throw new RuntimeException("unsupported GLContext: "+glContext);
209 }
210
211 return properties.put(0).rewind(); // 0 terminated array
212 }
213
214 // Buffers
215 /**
216 * Creates a CLGLBuffer for memory sharing with the specified OpenGL buffer.
217 * @param glBuffer The OpenGL buffer handle like a vertex buffer or pixel buffer object.
218 * @param glBufferSize The size of the OpenGL buffer in bytes
219 * @param flags optional flags.
220 */
221 public final CLGLBuffer<?> createFromGLBuffer(final int glBuffer, final long glBufferSize, final Mem... flags) {
222 return createFromGLBuffer(null, glBuffer, glBufferSize, Mem.flagsToInt(flags));
223 }
224
225 /**
226 * Creates a CLGLBuffer for memory sharing with the specified OpenGL buffer.
227 * @param glBuffer The OpenGL buffer handle like a vertex buffer or pixel buffer object.
228 * @param glBufferSize The size of the OpenGL buffer in bytes
229 * @param flags optional flags.
230 */
231 public final CLGLBuffer<?> createFromGLBuffer(final int glBuffer, final long glBufferSize, final int flags) {
232 return createFromGLBuffer(null, glBuffer, glBufferSize, flags);
233 }
234
235 /**
236 * Creates a CLGLBuffer for memory sharing with the specified OpenGL buffer.
237 * @param directBuffer A direct allocated NIO buffer for data transfers between java and OpenCL.
238 * @param glBuffer The OpenGL buffer handle like a vertex buffer or pixel buffer object.
239 * @param glBufferSize The size of the OpenGL buffer in bytes
240 * @param flags optional flags.
241 */
242 public final <B extends Buffer> CLGLBuffer<B> createFromGLBuffer(final B directBuffer, final int glBuffer, final long glBufferSize, final Mem... flags) {
243 return createFromGLBuffer(directBuffer, glBuffer, glBufferSize, Mem.flagsToInt(flags));
244 }
245
246 /**
247 * Creates a CLGLBuffer for memory sharing with the specified OpenGL buffer.
248 * @param directBuffer A direct allocated NIO buffer for data transfers between java and OpenCL.
249 * @param glBuffer The OpenGL buffer handle like a vertex buffer or pixel buffer object.
250 * @param glBufferSize The size of the OpenGL buffer in bytes
251 * @param flags optional flags.
252 */
253 public final <B extends Buffer> CLGLBuffer<B> createFromGLBuffer(final B directBuffer, final int glBuffer, final long glBufferSize, final int flags) {
254 final CLGLBuffer<B> buffer = CLGLBuffer.create(this, directBuffer, glBufferSize, flags, glBuffer);
255 memoryObjects.add(buffer);
256 return buffer;
257 }
258
259 // Renderbuffers
260 public final CLGLImage2d<?> createFromGLRenderbuffer(final int glBuffer, final Mem... flags) {
261 return createFromGLRenderbuffer(null, glBuffer, Mem.flagsToInt(flags));
262 }
263
264 public final CLGLImage2d<?> createFromGLRenderbuffer(final int glBuffer, final int flags) {
265 return createFromGLRenderbuffer(null, glBuffer, flags);
266 }
267
268 public final <B extends Buffer> CLGLImage2d<B> createFromGLRenderbuffer(final B directBuffer, final int glBuffer, final Mem... flags) {
269 return createFromGLRenderbuffer(directBuffer, glBuffer, Mem.flagsToInt(flags));
270 }
271
272 public final <B extends Buffer> CLGLImage2d<B> createFromGLRenderbuffer(final B directBuffer, final int glBuffer, final int flags) {
273 final CLGLImage2d<B> buffer = CLGLImage2d.createFromGLRenderbuffer(this, directBuffer, flags, glBuffer);
274 memoryObjects.add(buffer);
275 return buffer;
276 }
277
278 //2d Textures
279 public final CLGLTexture2d<?> createFromGLTexture2d(final int target, final int texture, final int mipmap, final Mem... flags) {
280 return createFromGLTexture2d(null, target, texture, mipmap, Mem.flagsToInt(flags));
281 }
282
283 public final CLGLTexture2d<?> createFromGLTexture2d(final int target, final int texture, final int mipmap, final int flags) {
284 return createFromGLTexture2d(null, target, texture, mipmap, flags);
285 }
286
287 public final <B extends Buffer> CLGLTexture2d<B> createFromGLTexture2d(final B directBuffer, final int target, final int texture, final int mipmap, final Mem... flags) {
288 return createFromGLTexture2d(directBuffer, target, texture, mipmap, Mem.flagsToInt(flags));
289 }
290
291 public final <B extends Buffer> CLGLTexture2d<B> createFromGLTexture2d(final B directBuffer, final int target, final int texture, final int mipmap, final int flags) {
292 final CLGLTexture2d<B> buffer = CLGLTexture2d.createFromGLTexture2d(this, directBuffer, target, texture, mipmap, flags);
293 memoryObjects.add(buffer);
294 return buffer;
295 }
296
297 //3d Textures
298 public final CLGLTexture3d<?> createFromGLTexture3d(final int target, final int texture, final int mipmap, final Mem... flags) {
299 return createFromGLTexture3d(null, target, texture, mipmap, Mem.flagsToInt(flags));
300 }
301
302 public final CLGLTexture3d<?> createFromGLTexture3d(final int target, final int texture, final int mipmap, final int flags) {
303 return createFromGLTexture3d(null, target, texture, mipmap, flags);
304 }
305
306 public final <B extends Buffer> CLGLTexture3d<B> createFromGLTexture3d(final B directBuffer, final int target, final int texture, final int mipmap, final Mem... flags) {
307 return createFromGLTexture3d(directBuffer, target, texture, mipmap, Mem.flagsToInt(flags));
308 }
309
310 public final <B extends Buffer> CLGLTexture3d<B> createFromGLTexture3d(final B directBuffer, final int target, final int texture, final int mipmap, final int flags) {
311 final CLGLTexture3d<B> buffer = CLGLTexture3d.createFromGLTexture3d(this, directBuffer, flags, target, mipmap, texture);
312 memoryObjects.add(buffer);
313 return buffer;
314 }
315
316 /**
317 * Return the low level OpenCL interface with OpenGL interoperability.
318 */
319 @Override
320 public CL getCL() {
321 return super.getCL();
322 }
323
324 /**
325 * Returns the OpenGL context this context was shared with.
326 */
327 public GLContext getGLContext() {
328 return glContext;
329 }
330
331 @Override
333 return this;
334 }
335
336}
CLContext is responsible for managing objects such as command-queues, memory, program and kernel obje...
Definition: CLContext.java:79
void overrideContext(final CLDevice device)
Definition: CLContext.java:515
static long createContextFromType(final CLPlatform platform, final CLErrorHandler handler, final PointerBuffer properties, final long deviceType)
Definition: CLContext.java:199
static CLContext create()
Creates a context on all available devices (CL_DEVICE_TYPE_ALL).
Definition: CLContext.java:139
final Set< CLMemory<? extends Buffer > > memoryObjects
Definition: CLContext.java:85
static long createContext(final CLPlatform platform, final CLErrorHandler handler, final PointerBuffer properties, final CLDevice... devices)
Definition: CLContext.java:209
static long toDeviceBitmap(final Type[] deviceTypes)
Definition: CLContext.java:659
static ErrorDispatcher createErrorHandler()
Definition: CLContext.java:704
final CLPlatform platform
Definition: CLContext.java:89
This object represents an OpenCL device.
Definition: CLDevice.java:53
CLPlatform getPlatform()
Returns the platform for this OpenCL object.
Definition: CLDevice.java:102
CLPlatfrorm representing a OpenCL implementation (e.g.
Definition: CLPlatform.java:99
final long ID
OpenCL platform id for this platform.
static CLPlatform getDefault()
Returns the default OpenCL platform or null when no platform found.
Shared buffer between OpenGL and OpenCL contexts.
Definition: CLGLBuffer.java:45
OpenCL Context supporting JOGL-JOCL interoperablity.
final< B extends Buffer > CLGLBuffer< B > createFromGLBuffer(final B directBuffer, final int glBuffer, final long glBufferSize, final int flags)
Creates a CLGLBuffer for memory sharing with the specified OpenGL buffer.
final CLGLTexture2d<?> createFromGLTexture2d(final int target, final int texture, final int mipmap, final int flags)
final< B extends Buffer > CLGLTexture2d< B > createFromGLTexture2d(final B directBuffer, final int target, final int texture, final int mipmap, final int flags)
static CLGLContext create(final GLContext glContext, CLPlatform platform, final CLDevice.Type... deviceTypes)
Creates a shared context on the specified platform and with the specified device types.
final< B extends Buffer > CLGLBuffer< B > createFromGLBuffer(final B directBuffer, final int glBuffer, final long glBufferSize, final Mem... flags)
Creates a CLGLBuffer for memory sharing with the specified OpenGL buffer.
final CLGLBuffer<?> createFromGLBuffer(final int glBuffer, final long glBufferSize, final Mem... flags)
Creates a CLGLBuffer for memory sharing with the specified OpenGL buffer.
CL getCL()
Return the low level OpenCL interface with OpenGL interoperability.
final CLGLImage2d<?> createFromGLRenderbuffer(final int glBuffer, final Mem... flags)
final CLGLBuffer<?> createFromGLBuffer(final int glBuffer, final long glBufferSize, final int flags)
Creates a CLGLBuffer for memory sharing with the specified OpenGL buffer.
final< B extends Buffer > CLGLTexture3d< B > createFromGLTexture3d(final B directBuffer, final int target, final int texture, final int mipmap, final Mem... flags)
final< B extends Buffer > CLGLImage2d< B > createFromGLRenderbuffer(final B directBuffer, final int glBuffer, final int flags)
final CLGLTexture2d<?> createFromGLTexture2d(final int target, final int texture, final int mipmap, final Mem... flags)
final CLGLTexture3d<?> createFromGLTexture3d(final int target, final int texture, final int mipmap, final int flags)
final CLGLTexture3d<?> createFromGLTexture3d(final int target, final int texture, final int mipmap, final Mem... flags)
GLContext getGLContext()
Returns the OpenGL context this context was shared with.
final< B extends Buffer > CLGLImage2d< B > createFromGLRenderbuffer(final B directBuffer, final int glBuffer, final Mem... flags)
final< B extends Buffer > CLGLTexture2d< B > createFromGLTexture2d(final B directBuffer, final int target, final int texture, final int mipmap, final Mem... flags)
static CLGLContext create(final GLContext glContext, final CLDevice... devices)
Creates a shared context on the specified platform and with the specified devices.
static CLGLContext create(final GLContext glContext)
Creates a shared context on all available devices (CL_DEVICE_TYPE_ALL).
CLGLContext getContext()
Returns the context for this OpenCL object.
static CLGLContext create(final GLContext glContext, final CLDevice.Type... deviceTypes)
Creates a shared context on the specified platform and with the specified device types.
static CLGLContext create(final GLContext glContext, final CLPlatform platform)
Creates a shared context on the specified platform on all available devices (CL_DEVICE_TYPE_ALL).
final CLGLImage2d<?> createFromGLRenderbuffer(final int glBuffer, final int flags)
final< B extends Buffer > CLGLTexture3d< B > createFromGLTexture3d(final B directBuffer, final int target, final int texture, final int mipmap, final int flags)
2D OpenCL image representing an OpenGL renderbuffer.
2D OpenCL image representing an 2D OpenGL texture.
3D OpenCL image representing an 3D OpenGL texture.
Enumeration for the type of a device.
Definition: CLDevice.java:798
Memory settings for configuring CLMemory.
Definition: CLMemory.java:289
static int flagsToInt(final Mem[] flags)
Definition: CLMemory.java:382
Java bindings to OpenCL, the Open Computing Language.
Definition: CL.java:26