我一直在尝试创建一个绑定到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:代码可以工作,但我不确定这是不是正确的方法。
苹果:我没有机器来测试它,但如果我也知道正确的参数,我会很感激。
发布于 2021-05-03 06:06:07
英特尔的Linux驱动程序不支持CL-GL互操作(它缺少cl_khr_gl_sharing扩展)。如果驱动程序支持CL-GL互操作,则代码片段应在Linux上工作
https://stackoverflow.com/questions/65222179
复制相似问题