首页 > 解决方案 > CL GL Interop:CL 上下文创建参数

问题描述

我一直在尝试创建一个绑定到 OpenGL 上下文的 OpenCL 上下文。我未能找到查询可用设备的方法properties所需的参数的适当值。KHRGLSharing.clGetGLContextInfoKHROpenGL 上下文是使用 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_KHRCL_GLX_DISPLAY_KHR。当前使 JVM 崩溃并带有SIGSEGV.

Windows:代码有效,但我不确定这是否是正确的方法。

Apple:我没有机器可以对此进行测试,但如果我也知道那里的正确参数,我将不胜感激。

标签: openglopencllwjglglfw

解决方案


英特尔的 Linux 驱动程序不支持 CL-GL 互操作(它缺少cl_khr_gl_sharing扩展)。如果驱动程序支持 CL-GL 互操作,则代码片段应该可以在 Linux 上运行


推荐阅读