简体   繁体   中英

Can't create OpenCL context sharing with EGL/OpenGLES context

I'm trying to use OpenCL interop with OpenGL-ES through the use of the cl_khr_gl_sharing extension. But when I try to create an OpenCL context that would share with my OpenGL context, clCreateContext fails with an error -59 (CL_INVALID_OPERATION).

From the documentation ( OpenCL Docs ) the only possibility I see is that my "device [...] cannot support OpenCL objects which share the data store of an OpenGL object". I checked with clGetDeviceInfo that the OpenCL device I'm using does support the extension: it does. What I'm trying right now is to use an offscreen render, but I tried to use a PBuffer surface, I also tried an onscreen rendering, still the same error.

I'm using EGL on Ubuntu 18.04. Here is the info I get about my GPU when I inquire OpenGL and OpenCL:

OpenGL info ( glGetString ):

Opengl context: NVIDIA Corporation : GeForce GTX 750 Ti/PCIe/SSE2 : OpenGL ES 3.2 NVIDIA 435.21

OpenCL Info ( clGetPlatformInfo and clGetDeviceInfo ):

CL_PLATFORM_PROFILE FULL_PROFILE
CL_PLATFORM_VERSION OpenCL 1.2 CUDA 10.1.0
CL_PLATFORM_NAME NVIDIA CUDA
CL_PLATFORM_VENDOR NVIDIA Corporation
CL_DEVICE_NAME GeForce GTX 750 Ti
CL_DEVICE_PROFILE FULL_PROFILE
CL_DEVICE_VENDOR NVIDIA Corporation
CL_DEVICE_VERSION OpenCL 1.2 CUDA
CL_DRIVER_VERSION 435.21

Here is the simplified version of the code I'm trying to run:

int main()
{
    /// OpenGL Setup
    EGLDisplay egl_dpy = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    eglInitialize(egl_dpy, NULL, NULL);

    const EGLint config_attribs[] = {
            EGL_RED_SIZE, 8,
            EGL_GREEN_SIZE, 8,
            EGL_BLUE_SIZE, 8,
            EGL_ALPHA_SIZE, 8,
            EGL_DEPTH_SIZE, 16,
            EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
            EGL_NONE};

    EGLConfig cfg;
    EGLint count;
    eglChooseConfig(egl_dpy, config_attribs, &cfg, 1, &count);
    eglBindAPI(EGL_OPENGL_ES_API);

    static const EGLint attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE};
    EGLContext core_ctx = eglCreateContext(egl_dpy, cfg, EGL_NO_CONTEXT, attribs);

    eglMakeCurrent(egl_dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, core_ctx);


    /// OpenCL setup
    cl_platform_id platform_id = NULL;
    cl_uint ret_num_platforms;
    auto retPlat = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
    if (retPlat != CL_SUCCESS)
    {
        assert(0);
    }

    cl_device_id device_id = NULL;
    auto retDev = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
    if (retDev != CL_SUCCESS)
    {
        assert(0);
    }

    cl_context_properties props[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id, CL_GL_CONTEXT_KHR,
            (cl_context_properties)eglGetCurrentContext(), CL_EGL_DISPLAY_KHR,
            (cl_context_properties)eglGetCurrentDisplay(), 0};


    cl_int retContext;
    cl_context myContext = clCreateContext(props, 1, &device_id, NULL, NULL, &retContext);
    if (retContext != CL_SUCCESS)
    {
        std::cout << "clCreateContext " << retContext << "\n";
        assert(0);
    }
}

I found the problem. It seems that the Nvidia OpenCL driver does not support OpenGL sharing with EGL contexts. I was able to trace in the OpenCL library, and noticed a switch statement that was returning this particular error if the CL_EGL_DISPLAY_KHR property was used. If I use a GLX OpenGL context instead, everything runs fine. I don't know how they could document this fact better, but it would have saved me a week.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM