【发布时间】:2020-12-09 17:46:33
【问题描述】:
我一直在尝试创建一个绑定到 OpenGL 上下文的 OpenCL 上下文。我没有找到KHRGLSharing.clGetGLContextInfoKHR 方法查询可用设备所需的properties 参数的适当值。 OpenGL 上下文是使用 GLFW 创建的,我要使用的窗口是当前上下文(使用 glfwMakeContextCurrent 设置)
以下代码片段显示了我到目前为止的想法:
public static List<Long> queryDevicesForPlatform(long platform) {
stack.push(); // MemoryStack defined elsewhere
//Create properties
//Problematic piece of code
long[] properties = switch (Platform.get()) {
//These parameters let the JVM crash when clGetGLContextInfoKHR is called
case LINUX -> new long[]{
CL_CONTEXT_PLATFORM, platform,
CL_GL_CONTEXT_KHR, GLX14.glXGetCurrentContext(),
CL_GLX_DISPLAY_KHR, GLX14.glXGetCurrentDisplay(),
0
};
//Not yet tested
case MACOSX -> new long[]{
CL_CONTEXT_PLATFORM, platform,
CL_CGL_SHAREGROUP_KHR, CGL.CGLGetShareGroup(CGL.CGLGetCurrentContext()),
0
};
//This one works
case WINDOWS -> new long[]{
CL_CONTEXT_PLATFORM, platform,
CL_GL_CONTEXT_KHR, glfwGetCurrentContext(),
CL_WGL_HDC_KHR, wglGetCurrentDC(),
0
};
};
//Copy properties to a buffer
ByteBuffer byteProp = stack.malloc(properties.length * Long.BYTES);
byteProp.asLongBuffer().put(properties);
ByteBuffer bytes = stack.malloc(Long.BYTES);
//JVM crashes here
int error = KHRGLSharing.clGetGLContextInfoKHR(PointerBuffer.create(byteProp),
CL_DEVICES_FOR_GL_CONTEXT_KHR, (ByteBuffer) null, PointerBuffer.create(bytes));
assert error == CL22.CL_SUCCESS: error;
ByteBuffer value = stack.malloc((int) bytes.asLongBuffer().get(0));
error = KHRGLSharing.clGetGLContextInfoKHR(PointerBuffer.create(byteProp), CL_DEVICES_FOR_GL_CONTEXT_KHR, value, null);
assert error == CL22.CL_SUCCESS: error;
LongBuffer devices = value.asLongBuffer();
ArrayList<Long> ret = new ArrayList<>();
while(devices.hasRemaining()) {
ret.add(devices.get());
}
stack.pop();
return ret;
}
Linux:我不知道为CL_CONTEXT_PLATFORM、CL_GL_CONTEXT_KHR 和CL_GLX_DISPLAY_KHR 传递什么值。当前使用 SIGSEGV 使 JVM 崩溃。
Windows:代码有效,但我不确定这是否是正确的方法。
Apple:我没有机器可以对此进行测试,但如果我也知道那里的正确参数,我将不胜感激。
【问题讨论】: