clCreateFromGLTexture -> clSetKernelArg -> CL_INVALID_MEM_OBJECT

Hi,

I am having difficulties passing a Texture from OpenGL to OpenCL and back.
When I’m setting the kernel argument to be the cl_mem corresponding to the OpenGL Texture I get the Error:

CL_INVALID_MEM_OBJECT

but I can’t figure out why. The doc is not very specific: “an argument declared to be a memory object when the specified arg_value is not a valid memory object”

So my cl_mem is not accepted as an 2D image object.
I’m guessing i created it poorly?

Here are the important snippets of my code:


//helper class
class FrameBuffer{
public:
    GLuint glTextureID; //ID to point to corresponding GL Texture
    cl_mem clTextureMem; //CL_mem pointer to openCL Memory
};

//creating the Framebuffer:
frameBufferTest = *new FrameBuffer();
//generate the texture ID
glGenTextures(1, &frameBufferTest.glTextureID);
//binnding the texture
glBindTexture(GL_TEXTURE_2D, frameBufferTest.glTextureID);
//regular sampler params
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//specify texture dimensions, format etc
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_FLOAT, 0);
    
frameBufferTest.clTextureMem = clCreateFromGLTexture(context, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0,frameBufferTest.glTextureID,NULL);
CheckError(error,  __LINE__);



//Setting the kernel Arguments:

glFinish();
clEnqueueAcquireGLObjects(cq, 1,  &frameBufferTest.clTextureMem, 0, 0, NULL);
error = clSetKernelArg(kernels[kernelId], argIdx, sizeof(frameBufferTest.clTextureMem), &frameBufferTest.clTextureMem);//Here i get the Error
CheckError(error,  memId);


Maybe you can spot what i did wrong?
Thanks for reading,

Cery

frameBufferTest.clTextureMem = clCreateFromGLTexture(context, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0,frameBufferTest.glTextureID,NULL);
CheckError(error, LINE);

Well thats not very smart to forget setting the error parameter thus loosing important error information.
The real error is: CL_INVALID_GL_OBJECT

Now i can search for the error way better.

When you create a texture with glTexImage2D() and a null data pointer, the texture is incomplete and clCreateFromGLTexture() fails with CL_INVALID_GL_OBJECT.
You have to create it with a non-null data pointer, or fill it with glClearTexSubImage(), or any other method to specify a pixel content.

Thanks utnapishtim,
I’ve tried what you suggested by simply using a char array for testing, but i get a similar result.

unsigned char * tmp_data = new unsigned char [width*height*4];
    for(int i = 0; i < width*height*4;i++){
        tmp_data[i] = 255;
    }

    glGenTextures(1, &fbcurrTex);

    glBindTexture(GL_TEXTURE_2D, fbcurrTex);
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tmp_data);
    
    CheckError(glGetError(),  __LINE__);
    glBindTexture(GL_TEXTURE_2D, 0);
    
    fbcurrTexCL = clCreateFromGLTexture(context, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0,fbcurrTex,&error);

But even if I’m not doing the OpenGL to OpenCL thing I get a black Texture which should be white?
I’m pretty sure I messed up the OpenGL side of things. :S

I tried the glClearTexSubImage() way with no success.

Thanks again,

Cery

Try with GL_RGBA8 as internal format instead of GL_RGBA.

unfortunately that does not do the trick either. :???:

And in your initial case, use an internal format GL_RGBA32F instead of GL_RGBA8 for a data type GL_FLOAT.