OpenCL OpenGL 互操作上下文创建失败

OpenCL OpenGL interop context creation failing

我正在尝试创建一个执行 openCL 和 openGL 互操作的简单程序。目前没有 opengl 或 opencl 代码,但程序在创建上下文之前失败。这是我正在尝试的一些(可编译的)代码 运行 但失败得很惨。

这里是:

#include <GL/glew.h>
#include <GLFW/glfw3.h>

#include <CL/cl.hpp>

#include <iostream>
#include <fstream>

cl::Platform getBestPlatform()
{

    std::vector<cl::Platform> platforms;
    std::vector<cl::Device> devices;

    cl::Platform ret;

    cl::Platform::get(&platforms);



    cl_int fastestNum = 0;

    for (auto& p : platforms)
    {
        p.getDevices(CL_DEVICE_TYPE_ALL, &devices);

        for (auto& d : devices)
        {
            cl_int speed;
            d.getInfo(CL_DEVICE_MAX_COMPUTE_UNITS, &speed);

            if (speed > fastestNum)
            {
                fastestNum = speed;
                ret = p;
            }
        }
    }
    return ret;
}

int main()
{
    if (!glfwInit())
    {
        std::cout << "Failed to init GLFW" << std::endl;

        return -1;
    }



    // set AA
    glfwWindowHint(GLFW_SAMPLES, 1);

    // set GL version
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    // set profile to core profile
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // set the window to non-resizable
    glfwWindowHint(GLFW_RESIZABLE, false);

    GLFWwindow* window = glfwCreateWindow(800, 600, "OpenCL OpenGL", NULL, NULL);


    // exit if the window wasn't initialized correctly
    if (!window)
    {
        fprintf(stderr, "Window failed to create");
        glfwTerminate();
        return -1;
    }


    // make context current
    glfwMakeContextCurrent(window);

    // use newer GL
    glewExperimental = GL_TRUE;

    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);




    if (glewInit() != GLEW_OK)
    {
        std::cout << "Failed to init GLEW. err code: " << glewInit() << std::endl;
        glfwTerminate();
        return -1;
    }

    // init cl
    cl::Platform platform = getBestPlatform();

    std::cout << "Using Platform: " << platform.getInfo<CL_PLATFORM_NAME>() << std::endl;

    std::vector<cl::Device> devices;
    platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);

    std::cout << "Using Device: " << devices[0].getInfo<CL_DEVICE_NAME>() << std::endl;


    cl_context_properties context_properties[] =
    {
        CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(),
        CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(),
        CL_CONTEXT_PLATFORM, (cl_context_properties)platform()
    };

    cl_int err = CL_SUCCESS;
    cl::Context context(devices, context_properties, NULL, NULL, &err);

    if (err != CL_SUCCESS){
        std::cout << "Error creating context" << "\t" << err << "\n";
        exit(-1);
    }

    do
    {



        glfwPollEvents();
        glfwSwapBuffers(window);

    } while (!glfwWindowShouldClose(window));

}

代码的第一部分只是openGL上下文创建的东西,但第二部分是需要注意的地方。

对不起,我忘了包括这个,但是调用了上下文创建之后的退出,因为上下文创建的错误代码是 -30 (CL_INVALID_VALUE)

我认为您的错误可能是由于没有用 NULL 完成属性列表。否则它将尝试获取下一个完全未知值的参数,因此会出现 CL_INVALID_VALUE 错误。

cl_context_properties context_properties[] =
{
    CL_GL_CONTEXT_KHR, (cl_context_properties)wglGetCurrentContext(),
    CL_WGL_HDC_KHR, (cl_context_properties)wglGetCurrentDC(),
    CL_CONTEXT_PLATFORM, (cl_context_properties)platform(), 
    NULL
};