AnsweredAssumed Answered

OpenGL/OpenCL: working with BMP-files (BGRA) and showing image (RGBA)

Question asked by VincentSC on Aug 20, 2013
Latest reply on Jan 15, 2014 by VincentSC

In some code I need to read in a BMP-file, show it via OpenGL, interact with OpenCL via the keyboard and write the resulting image to disk. My goal is to avoid conversions.


According to the interwebs the idea is to change GL_RGBA to GL_BGRA in clCreateImage2D. To my first surprise this change had no influence at all. What I understood later this is because OpenCL handles the data-upload (data in clCreateImage2D is NULL).

My question is how I can show the image in OpenGL as BGRA, so I don't need to convert the image. A possible solution is to use "(float4)(outColor.z, outColor.y, outColor.x, 1.0f)" instead of "(float4)(outColor.x, outColor.y, outColor.z, 1.0f)" in the last call of the kernel, but I'd like to know if it is possible without such hack.


Below are the important parts - it's quite basic CL/GL, except I'm working with images instead of buffers.


// Creating the image in OpenCL which will be the input for the kernel.

cl_image_format clImageFormat;

clImageFormat.image_channel_order = CL_BGRA;

clImageFormat.image_channel_data_type = CL_UNORM_INT8;

ImageIn = clCreateImage2D(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, &clImageFormat, width, height, 0, buffer, &errNum);


// Creating the GL-texture


glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image_width, image_height, 0, GL_BGRA /* or GL_RGBA */, GL_UNSIGNED_BYTE, NULL);


// Link ImageOut to the VTO texture, vto_id

imageOut = clCreateFromGLTexture2D(context, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, vto_id, &errNum);


// showing the image when the OpenCL kernel is finished.

errNum = clEnqueueReleaseGLObjects(commandQueue, 1, &imageObjects[2], 0, 0, 0);




glBindTexture(GL_TEXTURE_2D, vto_id);


glTexCoord2i(0, 1);

glVertex3f(-1.0f, 1.0f, 0.0f);

glTexCoord2i(1, 1);

glVertex3f( 1.0f, 1.0f, 0.0f);

glTexCoord2i(1, 0);

glVertex3f( 1.0f,-1.0f, 0.0f);

glTexCoord2i(0, 0);

glVertex3f(-1.0f,-1.0f, 0.0f);



glBindTexture(GL_TEXTURE_2D, 0);




// Reading back the image into a buffer - needing BGRA.

errNum = clEnqueueReadImage(commandQueue, imageOut, CL_TRUE, origin, region, 0, 0, buffer, 0, NULL, NULL);



Thanks for your time!