opengl和opencl共享时CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR
文章目录
- 前提条件
- 原因
- 实际情况
- 解决方案
- 修改后
- 参考代码
在使用opengl的上下文创建opencl的context时出现CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR这个错误,错误码-1000.查询错误码表示的意义,找到下面的链接:
https://registry.khronos.org/OpenCL/sdk/3.0/docs/man/html/clGetGLContextInfoKHR.html
前提条件
首先查询扩展是否支持cl_khr_gl_sharing, 如果device不支持的话opencl和opengl共享上下文不用考虑了。
原因
CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR if a context was specified for an OpenGL or OpenGL ES implementation using the EGL, GLX, or WGL binding APIs, as described for clCreateContext; and any of the following conditions hold:
The specified display and context properties do not identify a valid OpenGL or OpenGL ES context.
The specified context does not support buffer and renderbuffer objects.
The specified context is not compatible with the OpenCL context being created (for example, it exists in a physically distinct address space, such as another hardware device; or it does not support sharing data with OpenCL due to implementation restrictions).
CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR if a share group was specified for a CGL-based OpenGL implementation by setting the property CL_CGL_SHAREGROUP_KHR, and the specified share group does not identify a valid CGL share group object.
总结一下就是:
CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR
错误会在以下情况下发生:
-
当使用EGL、GLX或WGL绑定API为OpenGL或OpenGL ES实现指定上下文时,如果以下任一条件成立:
- 指定的显示和上下文属性不能识别一个有效的OpenGL或OpenGL ES上下文。
- 指定的上下文不支持缓冲区和渲染缓冲区对象。
- 指定的上下文与正在创建的OpenCL上下文不兼容(例如,它存在于一个物理上不同的地址空间,如另一个硬件设备;或者由于实现限制,它不支持与OpenCL共享数据)。
-
如果为基于CGL的OpenGL实现指定了一个共享组,通过设置属性
CL_CGL_SHAREGROUP_KHR
,并且指定的共享组不能识别一个有效的CGL共享组对象。
实际情况
输出opencl和opengl的设备显示如下的信息:
platforms size : 1
Using platform: NVIDIA CUDA
devices size: 1
Using device: Quadro M6000 24GB
Max Texture Size: 16384
Max Vertex Attribs: 16
Vendor: Intel
Renderer: Mesa Intel(R) UHD Graphics 770 (ADL-S GT1)
Version: 4.6 (Compatibility Profile) Mesa 23.2.1-1ubuntu3.1~22.04.2
Shading Language Version: 4.60
GLX_RENDER_TYPE: 32788
GLX_FBCONFIG_ID: 832
GLX_SCREEN: 0
CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR
CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR
my_context: 0
Error: No devices found in the context.
从这里看就是opencl使用nvidia gpu和opengl使用intel gpu, 不在同一个物理设备上,不支持上下文共享。over!
解决方案
设置opencl和opengl使用相同的设备,这里改为都使用nvidia的gpu.
在terminal的窗口中输入以下指令可以使opengl使用nvidia驱动中的版本
export __NV_PRIME_RENDER_OFFLOAD=1
export __VK_LAYER_NV_optimus=NVIDIA_only
export __GLX_VENDOR_LIBRARY_NAME=nvidia
这些指令只对当前terminal有效,如果需要永久设置,在~/.bashrc中添加上面的变量即可。
修改后
platforms size : 1
Using platform: NVIDIA CUDA
devices size: 1
Using device: Quadro M6000 24GB
Max Texture Size: 16384
Max Vertex Attribs: 16
Vendor: NVIDIA Corporation
Renderer: Quadro M6000 24GB/PCIe/SSE2
Version: 4.6.0 NVIDIA 535.183.01
Shading Language Version: 4.60 NVIDIA
GLX_RENDER_TYPE: 32788
GLX_FBCONFIG_ID: 304
GLX_SCREEN: 0
CL_SUCCESS
CL_SUCCESS
my_context: 0x5b789cecc5f0
Context created successfully with device: Quadro M6000 24GB
可以看到shading language 变为nvidia的。完美!
参考代码
#include <iostream>
#include <vector>
#include <CL/cl2.hpp>
#include <CL/cl_gl.h>
#include <CL/cl_ext.h>
#include <CL/Utils/Utils.hpp>
#include <CL/Utils/Error.hpp>
// #include <opencl.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <GL/glx.h>
#include <X11/Xlib.h>#include "opengl_debug.h"void printCLError(cl_int error) {switch (error) {case CL_SUCCESS: std::cout << "CL_SUCCESS" << std::endl; break;case CL_DEVICE_NOT_FOUND: std::cout << "CL_DEVICE_NOT_FOUND" << std::endl; break;case CL_DEVICE_NOT_AVAILABLE: std::cout << "CL_DEVICE_NOT_AVAILABLE" << std::endl; break;case CL_COMPILER_NOT_AVAILABLE: std::cout << "CL_COMPILER_NOT_AVAILABLE" << std::endl; break;case CL_MEM_OBJECT_ALLOCATION_FAILURE: std::cout << "CL_MEM_OBJECT_ALLOCATION_FAILURE" << std::endl; break;case CL_OUT_OF_RESOURCES: std::cout << "CL_OUT_OF_RESOURCES" << std::endl; break;case CL_OUT_OF_HOST_MEMORY: std::cout << "CL_OUT_OF_HOST_MEMORY" << std::endl; break;case CL_PROFILING_INFO_NOT_AVAILABLE: std::cout << "CL_PROFILING_INFO_NOT_AVAILABLE" << std::endl; break;case CL_MEM_COPY_OVERLAP: std::cout << "CL_MEM_COPY_OVERLAP" << std::endl; break;case CL_IMAGE_FORMAT_MISMATCH: std::cout << "CL_IMAGE_FORMAT_MISMATCH" << std::endl; break;case CL_IMAGE_FORMAT_NOT_SUPPORTED: std::cout << "CL_IMAGE_FORMAT_NOT_SUPPORTED" << std::endl; break;case CL_BUILD_PROGRAM_FAILURE: std::cout << "CL_BUILD_PROGRAM_FAILURE" << std::endl; break;case CL_MAP_FAILURE: std::cout << "CL_MAP_FAILURE" << std::endl; break;case CL_MISALIGNED_SUB_BUFFER_OFFSET: std::cout << "CL_MISALIGNED_SUB_BUFFER_OFFSET" << std::endl; break;case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST: std::cout << "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST" << std::endl; break;case CL_COMPILE_PROGRAM_FAILURE: std::cout << "CL_COMPILE_PROGRAM_FAILURE" << std::endl; break;case CL_LINKER_NOT_AVAILABLE: std::cout << "CL_LINKER_NOT_AVAILABLE" << std::endl; break;case CL_LINK_PROGRAM_FAILURE: std::cout << "CL_LINK_PROGRAM_FAILURE" << std::endl; break;case CL_DEVICE_PARTITION_FAILED: std::cout << "CL_DEVICE_PARTITION_FAILED" << std::endl; break;case CL_KERNEL_ARG_INFO_NOT_AVAILABLE: std::cout << "CL_KERNEL_ARG_INFO_NOT_AVAILABLE" << std::endl; break;case CL_INVALID_VALUE: std::cout << "CL_INVALID_VALUE" << std::endl; break;case CL_INVALID_DEVICE_TYPE: std::cout << "CL_INVALID_DEVICE_TYPE" << std::endl; break;case CL_INVALID_PLATFORM: std::cout << "CL_INVALID_PLATFORM" << std::endl; break;case CL_INVALID_DEVICE: std::cout << "CL_INVALID_DEVICE" << std::endl; break;case CL_INVALID_CONTEXT: std::cout << "CL_INVALID_CONTEXT" << std::endl; break;case CL_INVALID_QUEUE_PROPERTIES: std::cout << "CL_INVALID_QUEUE_PROPERTIES" << std::endl; break;case CL_INVALID_COMMAND_QUEUE: std::cout << "CL_INVALID_COMMAND_QUEUE" << std::endl; break;case CL_INVALID_HOST_PTR: std::cout << "CL_INVALID_HOST_PTR" << std::endl; break;case CL_INVALID_MEM_OBJECT: std::cout << "CL_INVALID_MEM_OBJECT" << std::endl; break;case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: std::cout << "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR" << std::endl; break;case CL_INVALID_IMAGE_SIZE: std::cout << "CL_INVALID_IMAGE_SIZE" << std::endl; break;case CL_INVALID_SAMPLER: std::cout << "CL_INVALID_SAMPLER" << std::endl; break;case CL_INVALID_BINARY: std::cout << "CL_INVALID_BINARY" << std::endl; break;case CL_INVALID_BUILD_OPTIONS: std::cout << "CL_INVALID_BUILD_OPTIONS" << std::endl; break;case CL_INVALID_PROGRAM: std::cout << "CL_INVALID_PROGRAM" << std::endl; break;case CL_INVALID_PROGRAM_EXECUTABLE: std::cout << "CL_INVALID_PROGRAM_EXECUTABLE" << std::endl; break;case CL_INVALID_KERNEL_NAME: std::cout << "CL_INVALID_KERNEL_NAME" << std::endl; break;case CL_INVALID_KERNEL_DEFINITION: std::cout << "CL_INVALID_KERNEL_DEFINITION" << std::endl; break;case CL_INVALID_KERNEL: std::cout << "CL_INVALID_KERNEL" << std::endl; break;case CL_INVALID_ARG_INDEX: std::cout << "CL_INVALID_ARG_INDEX" << std::endl; break;case CL_INVALID_ARG_VALUE: std::cout << "CL_INVALID_ARG_VALUE" << std::endl; break;case CL_INVALID_ARG_SIZE: std::cout << "CL_INVALID_ARG_SIZE" << std::endl; break;case CL_INVALID_KERNEL_ARGS: std::cout << "CL_INVALID_KERNEL_ARGS" << std::endl; break;case CL_INVALID_WORK_DIMENSION: std::cout << "CL_INVALID_WORK_DIMENSION" << std::endl; break;case CL_INVALID_WORK_GROUP_SIZE: std::cout << "CL_INVALID_WORK_GROUP_SIZE" << std::endl; break;case CL_INVALID_WORK_ITEM_SIZE: std::cout << "CL_INVALID_WORK_ITEM_SIZE" << std::endl; break;case CL_INVALID_GLOBAL_OFFSET: std::cout << "CL_INVALID_GLOBAL_OFFSET" << std::endl; break;case CL_INVALID_EVENT_WAIT_LIST: std::cout << "CL_INVALID_EVENT_WAIT_LIST" << std::endl; break;case CL_INVALID_EVENT: std::cout << "CL_INVALID_EVENT" << std::endl; break;case CL_INVALID_OPERATION: std::cout << "CL_INVALID_OPERATION" << std::endl; break;case CL_INVALID_GL_OBJECT: std::cout << "CL_INVALID_GL_OBJECT" << std::endl; break;case CL_INVALID_BUFFER_SIZE: std::cout << "CL_INVALID_BUFFER_SIZE" << std::endl; break;case CL_INVALID_MIP_LEVEL: std::cout << "CL_INVALID_MIP_LEVEL" << std::endl; break;case CL_INVALID_GLOBAL_WORK_SIZE: std::cout << "CL_INVALID_GLOBAL_WORK_SIZE" << std::endl; break;case CL_INVALID_PROPERTY: std::cout << "CL_INVALID_PROPERTY" << std::endl; break;case CL_INVALID_IMAGE_DESCRIPTOR: std::cout << "CL_INVALID_IMAGE_DESCRIPTOR" << std::endl; break;case CL_INVALID_COMPILER_OPTIONS: std::cout << "CL_INVALID_COMPILER_OPTIONS" << std::endl; break;case CL_INVALID_LINKER_OPTIONS: std::cout << "CL_INVALID_LINKER_OPTIONS" << std::endl; break;case CL_INVALID_DEVICE_PARTITION_COUNT: std::cout << "CL_INVALID_DEVICE_PARTITION_COUNT" << std::endl; break;case CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR: std::cout << "CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR" << std::endl; break;default: std::cout << "Unknown OpenCL error: " << error << std::endl; break;}
}void printPlatformInfo(const cl::Platform& platform) {std::string platformName;platform.getInfo(CL_PLATFORM_NAME, &platformName);std::cout << "Platform: " << platformName << std::endl;std::string platformVendor;platform.getInfo(CL_PLATFORM_VENDOR, &platformVendor);std::cout << "Vendor: " << platformVendor << std::endl;std::string platformVersion;platform.getInfo(CL_PLATFORM_VERSION, &platformVersion);std::cout << "Version: " << platformVersion << std::endl;std::string platformProfile;platform.getInfo(CL_PLATFORM_PROFILE, &platformProfile);std::cout << "Profile: " << platformProfile << std::endl;std::string platformExtensions;platform.getInfo(CL_PLATFORM_EXTENSIONS, &platformExtensions);std::cout << "Extensions: " << platformExtensions << std::endl;
}// 打印OpenGL状态信息
void printOpenGLState() {GLint maxTextureSize;glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);std::cout << "Max Texture Size: " << maxTextureSize << std::endl;GLint maxVertexAttribs;glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs);std::cout << "Max Vertex Attribs: " << maxVertexAttribs << std::endl;const GLubyte* vendor = glGetString(GL_VENDOR);const GLubyte* renderer = glGetString(GL_RENDERER);const GLubyte* version = glGetString(GL_VERSION);const GLubyte* shadingLanguageVersion = glGetString(GL_SHADING_LANGUAGE_VERSION);std::cout << "Vendor: " << vendor << std::endl;std::cout << "Renderer: " << renderer << std::endl;std::cout << "Version: " << version << std::endl;std::cout << "Shading Language Version: " << shadingLanguageVersion << std::endl;}void printDeviceInfo(const cl::Device& device) {std::string deviceName;device.getInfo(CL_DEVICE_NAME, &deviceName);std::cout << " Device: " << deviceName << std::endl;std::string deviceVendor;device.getInfo(CL_DEVICE_VENDOR, &deviceVendor);std::cout << " Vendor: " << deviceVendor << std::endl;std::string deviceVersion;device.getInfo(CL_DEVICE_VERSION, &deviceVersion);std::cout << " Version: " << deviceVersion << std::endl;cl_uint computeUnits;device.getInfo(CL_DEVICE_MAX_COMPUTE_UNITS, &computeUnits);std::cout << " Compute Units: " << computeUnits << std::endl;cl_ulong globalMemSize;device.getInfo(CL_DEVICE_GLOBAL_MEM_SIZE, &globalMemSize);std::cout << " Global Memory Size: " << globalMemSize / (1024 * 1024) << " MB" << std::endl;cl_ulong localMemSize;device.getInfo(CL_DEVICE_LOCAL_MEM_SIZE, &localMemSize);std::cout << " Local Memory Size: " << localMemSize / 1024 << " KB" << std::endl;std::string deviceExtensions;device.getInfo(CL_DEVICE_EXTENSIONS, &deviceExtensions);std::cout << " Extensions: " << deviceExtensions << std::endl;
}bool checkInteropSupport(const cl::Device& device) {std::string extensions;device.getInfo(CL_DEVICE_EXTENSIONS, &extensions);return extensions.find("cl_khr_gl_sharing") != std::string::npos;
}// 检查EGL错误
void checkEGLError(const char* msg) {EGLint error = eglGetError();if (error != EGL_SUCCESS) {std::cout << msg << ": EGL error " << std::hex << error << std::endl;}
}void CL_CALLBACK pfn_notify(const char *errinfo, const void *private_info, size_t cb, void *user_data) {fprintf(stderr, "OpenCL Error (via pfn_notify): %s\n", errinfo);
}void createOpenCLContextFromEGL() {try {// 获取所有平台std::vector<cl::Platform> platforms;cl::Platform::get(&platforms);if (platforms.empty()) {std::cout << "No OpenCL platforms found." << std::endl;return;}for (const auto& platform : platforms) {printPlatformInfo(platform);}// 选择第一个平台cl::Platform platform = platforms[0];std::cout << "Using platform: " << platform.getInfo<CL_PLATFORM_NAME>() << std::endl;// 获取平台上的所有设备std::vector<cl::Device> devices;platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);for(const auto& device : devices) {printDeviceInfo(device);}if (devices.empty()) {std::cout << "No devices found on platform: " << platform.getInfo<CL_PLATFORM_NAME>() << std::endl;return;}// 选择第一个设备cl::Device device = devices[0];std::cout << "Using device: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;if(!checkInteropSupport(device)){std::cout << "Device does not support OpenCL-OpenGL interop" << std::endl;}// 获取当前的EGL显示和上下文EGLDisplay eglDisplay = eglGetCurrentDisplay();EGLContext eglContext = eglGetCurrentContext();if (eglDisplay == EGL_NO_DISPLAY || eglContext == EGL_NO_CONTEXT) {std::cout << "Failed to get current EGL display or context" << std::endl;return;}// 配置上下文属性cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platform(),CL_GL_CONTEXT_KHR, (cl_context_properties)eglContext,CL_EGL_DISPLAY_KHR, (cl_context_properties)eglDisplay,CL_NONE};// 创建上下文int error_code;cl::Context context(CL_DEVICE_TYPE_GPU, properties,nullptr,&error_code);printCLError(error_code);// 获取设备列表std::vector<cl::Device> contextDevices = context.getInfo<CL_CONTEXT_DEVICES>();if (contextDevices.empty()) {std::cout << "Error: No devices found in the context." << std::endl;return;}std::cout << "Context created successfully with device: " << contextDevices[0].getInfo<CL_DEVICE_NAME>() << std::endl;// } catch (const cl::Error& e) {// std::cout << "OpenCL Error: " << e.what() << " (" << e.err() << ")" << std::endl;} catch (const std::exception& e) {std::cout << "Standard Exception: " << e.what() << std::endl;} catch (...) {std::cout << "Unknown Exception occurred." << std::endl;}
}int DemoOfEGLContext()
{GL_CHECK_ERROR();// 初始化EGLEGLDisplay eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);if (eglDisplay == EGL_NO_DISPLAY) {std::cout << "Failed to get EGL display" << std::endl;return -1;}GL_CHECK_ERROR();if (!eglInitialize(eglDisplay, nullptr, nullptr)) {std::cout << "Failed to initialize EGL" << std::endl;return -1;}GL_CHECK_ERROR();// 配置EGL属性const EGLint configAttribs[] = {EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,EGL_NONE};GL_CHECK_ERROR();EGLConfig config;EGLint numConfigs;if (!eglChooseConfig(eglDisplay, configAttribs, &config, 1, &numConfigs) || numConfigs < 1) {std::cout << "Failed to choose EGL config" << std::endl;return -1;}// 创建EGL上下文const EGLint contextAttribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2,EGL_NONE};GL_CHECK_ERROR();EGLContext eglContext = eglCreateContext(eglDisplay, config, EGL_NO_CONTEXT, contextAttribs);if (eglContext == EGL_NO_CONTEXT) {std::cout << "Failed to create EGL context" << std::endl;return -1;}// 配置Pbuffer属性const EGLint pbufferAttribs[] = {EGL_WIDTH, 800,EGL_HEIGHT, 600,EGL_NONE,};GL_CHECK_ERROR();// 这里需要替换为你实际的本地窗口句柄EGLSurface eglSurface = eglCreatePbufferSurface(eglDisplay, config, pbufferAttribs);if (eglSurface == EGL_NO_SURFACE) {std::cout << "Failed to create EGL Pbuffer surface" << std::endl;return -1;}// 绑定上下文if (!eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) {std::cout << "Failed to make EGL context current" << std::endl;return -1;}// 调用创建OpenCL上下文的函数createOpenCLContextFromEGL();// 主循环// ...GL_CHECK_ERROR();// 终止EGLeglDestroySurface(eglDisplay, eglSurface);eglDestroyContext(eglDisplay, eglContext);eglTerminate(eglDisplay);
}void DemoOfGLXContext()
{
// 获取所有平台std::vector<cl::Platform> platforms;cl::Platform::get(&platforms);if (platforms.empty()) {std::cout << "No OpenCL platforms found." << std::endl;return;}GL_CHECK_ERROR();// 选择第一个平台cl::Platform platform = platforms[0];std::cout << "platforms size : " << platforms.size() << std::endl;std::cout << "Using platform: " << platform.getInfo<CL_PLATFORM_NAME>() << std::endl;// 获取平台上的所有设备std::vector<cl::Device> devices;platform.getDevices(CL_DEVICE_TYPE_GPU, &devices);std::cout << "devices size: " << devices.size() << std::endl;if (devices.empty()) {std::cout << "No devices found on platform: " << platform.getInfo<CL_PLATFORM_NAME>() << std::endl;return;}GL_CHECK_ERROR();// 选择第一个设备cl::Device device = devices[0];std::cout << "Using device: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;// 检查设备是否支持OpenCL-OpenGL互操作性if (!checkInteropSupport(device)) {std::cout << "Device does not support OpenCL-OpenGL interop" << std::endl;return;}GL_CHECK_ERROR();// 获取当前的GLX显示Display* display = XOpenDisplay(nullptr);if (!display) {std::cout << "Failed to open X display" << std::endl;return;}GL_CHECK_ERROR();int screen = DefaultScreen(display);// 设置FBConfig属性int fbAttribs[] = {GLX_RENDER_TYPE, GLX_RGBA_BIT,GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,GLX_RED_SIZE, 8,GLX_GREEN_SIZE, 8,GLX_BLUE_SIZE, 8,GLX_ALPHA_SIZE, 8,GLX_DEPTH_SIZE, 24,None};GL_CHECK_ERROR();// 获取FBConfigint numConfigs;GLXFBConfig* fbConfigs = glXChooseFBConfig(display, screen, fbAttribs, &numConfigs);if (!fbConfigs || numConfigs == 0) {std::cout << "Failed to get FBConfig" << std::endl;return;}GLXFBConfig fbConfig = fbConfigs[0];GL_CHECK_ERROR();// 创建OpenGL上下文GLXContext glxContext = glXCreateNewContext(display, fbConfig, GLX_RGBA_TYPE, nullptr, GL_TRUE);if (!glxContext) {std::cout << "Failed to create GLX context" << std::endl;return;}GL_CHECK_ERROR();// 创建Pbuffer属性int pbufferAttribs[] = {GLX_PBUFFER_WIDTH, 800,GLX_PBUFFER_HEIGHT, 600,None};// 创建PbufferGLXPbuffer pbuffer = glXCreatePbuffer(display, fbConfig, pbufferAttribs);if (!pbuffer) {std::cout << "Failed to create Pbuffer" << std::endl;return;}GL_CHECK_ERROR();// 绑定Pbuffer上下文if (!glXMakeContextCurrent(display, pbuffer, pbuffer, glxContext)) {std::cout << "Failed to make GLX context current" << std::endl;return;}GL_CHECK_ERROR();printOpenGLState();int value;glXQueryContext(display, glxContext, GLX_RENDER_TYPE, &value);std::cout << "GLX_RENDER_TYPE: " << value << std::endl;glXQueryContext(display, glxContext, GLX_FBCONFIG_ID, &value);std::cout << "GLX_FBCONFIG_ID: " << value << std::endl;glXQueryContext(display, glxContext, GLX_SCREEN, &value);std::cout << "GLX_SCREEN: " << value << std::endl;// 配置上下文属性cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platform(),CL_GL_CONTEXT_KHR, (cl_context_properties)glxContext,CL_GLX_DISPLAY_KHR, (cl_context_properties)display,CL_NONE};GL_CHECK_ERROR();// 创建OpenCL上下文int error_code;cl::Context context(CL_DEVICE_TYPE_GPU, properties, nullptr, nullptr, &error_code);printCLError(error_code);cl::Context my_context(devices, properties, pfn_notify, nullptr, &error_code);printCLError(error_code);// cl::Context my_context = clCreateContext(properties,1, devices2, &pfn_notify, nullptr, &error_code);std::cout << "my_context: " << my_context() << std::endl;// 获取设备列表std::vector<cl::Device> contextDevices = context.getInfo<CL_CONTEXT_DEVICES>();if (contextDevices.empty()) {std::cout << "Error: No devices found in the context." << std::endl;return;}GL_CHECK_ERROR();std::cout << "Context created successfully with device: " << contextDevices[0].getInfo<CL_DEVICE_NAME>() << std::endl;// 清理glXDestroyPbuffer(display, pbuffer);glXDestroyContext(display, glxContext);XCloseDisplay(display);
}int main() {// DemoOfEGLContext();DemoOfGLXContext();return 0;
}