1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
package com.mbien.opencl;
import java.nio.Buffer;
import com.mbien.opencl.CLMemory.Mem;
import com.sun.opengl.impl.GLContextImpl;
import com.sun.opengl.impl.macosx.cgl.MacOSXCGLContext;
import com.sun.opengl.impl.windows.wgl.WindowsWGLContext;
import com.sun.opengl.impl.x11.glx.X11GLXContext;
import java.nio.LongBuffer;
import javax.media.nativewindow.DefaultGraphicsConfiguration;
import javax.media.opengl.GLContext;
import static com.mbien.opencl.CLGLI.*;
/**
*
* @author Michael Bien
*/
public final class CLGLContext extends CLContext {
final long glContextID;
private CLGLContext(long clContextID, long glContextID) {
super(clContextID);
this.glContextID = glContextID;
}
public static CLGLContext create(GLContext glContext) {
//UNIX
//cl_context_properties props[] = {
// CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(),
// CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(),
// CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0};
//WIN32
//cl_context_properties props[] = {
// CL_GL_CONTEXT_KHR, (cl_context_properties)TODO0,
// CL_WGL_HDC_KHR, (cl_context_properties)TODO 0,
// CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0};
//MACOSX
//cl_context_properties props[] = {
// CL_CGL_SHAREGROUP_KHR, (cl_context_properties)TODO 0,
// CL_CONTEXT_PLATFORM, (cl_context_properties)cpPlatform, 0};
long glID = 0;
GLContextImpl ctxImpl = (GLContextImpl)glContext;
DefaultGraphicsConfiguration config = (DefaultGraphicsConfiguration)ctxImpl.getDrawableImpl()
.getNativeWindow().getGraphicsConfiguration().getNativeGraphicsConfiguration();
LongBuffer properties = LongBuffer.allocate(5);
if(glContext instanceof X11GLXContext) {
long handle = config.getScreen().getDevice().getHandle();
glID = ((X11GLXContext)glContext).getContext();
properties.put(CLGLI.CL_GL_CONTEXT_KHR).put(glID)
.put(CLGLI.CL_GLX_DISPLAY_KHR).put(handle);
}else if(glContext instanceof WindowsWGLContext) {
// TODO test on windows
throw new RuntimeException("cl-gl interoperability on windows not yet implemented");
}else if(glContext instanceof MacOSXCGLContext) {
// TODO test on mac
throw new RuntimeException("cl-gl interoperability on mac not yet implemented");
}else{
throw new RuntimeException("unsupported GLContext: "+glContext);
}
properties.put(0).rewind(); // 0 terminated array
long clID = createContextFromType(properties, CL_DEVICE_TYPE_ALL);
return new CLGLContext(clID, glID);
}
public final <B extends Buffer> CLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, Mem... flags) {
return createFromGLBuffer(directBuffer, glBuffer, Mem.flagsToInt(flags));
}
public final <B extends Buffer> CLBuffer<B> createFromGLBuffer(B directBuffer, int glBuffer, int flags) {
CLBuffer<B> buffer = CLBuffer.create(this, directBuffer, flags, glBuffer);
memoryObjects.add(buffer);
return buffer;
}
// TODO move somewhere else
public GLObjectType getGLObjectType(CLBuffer<?> buffer) {
int[] array = new int[1];
int ret = ((CLGLI)cl).clGetGLObjectInfo(buffer.ID, array, 0, null, 0);
CLException.checkForError(ret, "error while asking for gl object info");
return GLObjectType.valueOf(array[0]);
}
public int getGLObjectID(CLBuffer<?> buffer) {
int[] array = new int[1];
int ret = ((CLGLI)cl).clGetGLObjectInfo(buffer.ID, null, 0, array, 0);
CLException.checkForError(ret, "error while asking for gl object info");
return array[0];
}
public enum GLObjectType {
GL_OBJECT_BUFFER(CL_GL_OBJECT_BUFFER),
GL_OBJECT_TEXTURE2D(CL_GL_OBJECT_TEXTURE2D),
GL_OBJECT_TEXTURE3D(CL_GL_OBJECT_TEXTURE3D),
GL_OBJECT_RENDERBUFFER(CL_GL_OBJECT_RENDERBUFFER);
public final int TYPE;
private GLObjectType(int type) {
this.TYPE = type;
}
public static GLObjectType valueOf(int type) {
if(type == CL_GL_OBJECT_BUFFER)
return GL_OBJECT_BUFFER;
else if(type == CL_GL_OBJECT_TEXTURE2D)
return GL_OBJECT_TEXTURE2D;
else if(type == CL_GL_OBJECT_TEXTURE3D)
return GL_OBJECT_TEXTURE3D;
else if(type == CL_GL_OBJECT_RENDERBUFFER)
return GL_OBJECT_RENDERBUFFER;
return null;
}
}
}
|