[英]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
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.