Reputation: 2261
I am trying to create a simple program that does openCL and openGL interop. There is currently no opengl or opencl code, but the program fails before the context is created. Here is some (compileable) code that is what I am trying to run and fails so miserably.
Here it is:
#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));
}
The first bit of code is just openGL context creation stuff, but the second part is what to pay attention to.
Sorry I forgot to include this, but the exit that is after the context creation is called because the error code from context creation is -30 (CL_INVALID_VALUE)
Upvotes: 0
Views: 811
Reputation: 8410
I think your error may be due to not finishing the properties list with a NULL. Otherwise it will try to get the next parameter which is a completely unknown value, therefore a CL_INVALID_VALUE error.
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
};
Upvotes: 1