2015-01-28 3 views
0

Я пытаюсь создать простую программу, которая открывает openCL и openGL interop. В настоящее время нет opengl или opencl-кода, но программа не работает до создания контекста. Вот некоторый (скомпилируемый) код, который я пытаюсь запустить, и терпит неудачу так жалко.Не удалось создать контекст контекста OpenCL OpenGL

Здесь:

#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)

+0

Любое предположение о том, где он падает? – Dan

ответ

1

Я думаю, ваша ошибка может быть вызвана не финишным список свойств с 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 
}; 
Смежные вопросы