我正在尝试创建一个执行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)
答案 0 :(得分: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
};