NULL image returned by clCreateFromGLTexture2D?

The docs lead me to believe that if a NULL image is returned, a non-success error should be returned as well… but I’m getting CL_SUCCESS and a NULL cl_mem return value from clCreateFromGLTexture2D!


#include <CL/cl.h>
#include <SDL2/SDL.h>
#include <GL/gl.h>
#include <stdio.h>

static int WINDOW_WIDTH = 800;
static int WINDOW_HEIGHT = 600;

int main() {
  cl_int err;

  cl_platform_id platform;
  if (clGetPlatformIDs(1, &platform, NULL) != CL_SUCCESS) {
    fprintf(stderr, "error getting platforms
");
    return 1;
  }

  cl_device_id device;
  if(clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, &device, NULL) != CL_SUCCESS) {
    fprintf(stderr, "error getting devices
");
    return 1;
  }

  cl_context context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);

  SDL_Window* w = SDL_CreateWindow("Oh hello, mandelbrot", 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0); 
  SDL_GL_CreateContext(w);

  GLint tex;
  glGenTextures(1, &tex);
  glBindTexture(GL_TEXTURE_2D, tex);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexImage2D(
    GL_TEXTURE_2D,
    0,  
    GL_RGBA,
    WINDOW_WIDTH,
    WINDOW_HEIGHT,
    0,  
    GL_RGBA,
    GL_FLOAT,
    NULL
  );

  if(glGetError() != GL_NO_ERROR) {
    fprintf(stderr, "OpenGL error
");
    return 2;
  }

  cl_mem img = clCreateFromGLTexture2D(
    context,
    CL_MEM_WRITE_ONLY,
    GL_TEXTURE_2D,
    0,
    tex,
    &err
  );

  if(err != CL_SUCCESS) {
    fprintf(stderr, "OpenCL error creating image: %d
", err);
    return 1;
  } else if (img == NULL) {
    fprintf(stderr, "NULL image
");
    return 1;
  }

  return 0;
}

Any ideas?

Thanks!

(Sorry for the double-post - I can’t find a way to edit)

If it helps, I’m on 64-bit Arch Linux with mesa drivers and intel-opencl-sdk/intel-opencl-runtime installed. I wouldn’t be surprised if it’s an issue with the OpenCL or OpenGL implementation - I’ve had some problems before.

Ironic edit: Well this post is editable… I’m sure I’ll figure out the criteria eventually.

Sounds like a driver bug.

Hi BenFoppa,

Did you check that the OpenCL device you working on supports OpenCL-OpenGL sharing?
It makes sense to check that before creating OpenCL context. You can check that by doing something like this:


    size_t requiredSize = 0;
    char* extensions = NULL;
    char* isSupported = NULL;
    err = clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, 0, NULL, &requiredSize);
    if (CL_SUCCESS != err)
    {
        printf("clGetDeviceInfo failed!
");
        return 1;
    }

    extensions = (char*) malloc(requiredSize);
    err = clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, requiredSize, extensions, NULL);
    if (CL_SUCCESS != err)
    {
        printf("clGetDeviceInfo failed!
");
        free(extensions);
        return 1;
    }

    isSupported = strstr(extensions, "cl_khr_gl_sharing");
    free(extensions);

    if (NULL == isSupported)
    {
        printf("This OpenCL device doesn't support OpenCL-OpenGL sharing
");
        return 1;
    }

[QUOTE=nmeraey;30714]Hi BenFoppa,

Did you check that the OpenCL device you working on supports OpenCL-OpenGL sharing?
It makes sense to check that before creating OpenCL context. You can check that by doing something like this:


    size_t requiredSize = 0;
    char* extensions = NULL;
    char* isSupported = NULL;
    err = clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, 0, NULL, &requiredSize);
    if (CL_SUCCESS != err)
    {
        printf("clGetDeviceInfo failed!
");
        return 1;
    }

    extensions = (char*) malloc(requiredSize);
    err = clGetDeviceInfo(device, CL_DEVICE_EXTENSIONS, requiredSize, extensions, NULL);
    if (CL_SUCCESS != err)
    {
        printf("clGetDeviceInfo failed!
");
        free(extensions);
        return 1;
    }

    isSupported = strstr(extensions, "cl_khr_gl_sharing");
    free(extensions);

    if (NULL == isSupported)
    {
        printf("This OpenCL device doesn't support OpenCL-OpenGL sharing
");
        return 1;
    }

[/QUOTE]

Wow, yup, that’s the issue! Thanks!