cancel
Showing results for 
Search instead for 
Did you mean: 

Archives Discussions

VincentSC
Adept II

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

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

// GL_RGBA8 => CL_RGBA, CL_UNORM_INT8, CL_BGRA, CL_UNORM_INT8

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);

clFlush(commandQueue);

glEnable(GL_TEXTURE_2D);

glBindTexture(GL_TEXTURE_2D, vto_id);

glBegin(GL_QUADS);

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);

glEnd();

glLoadIdentity();

glBindTexture(GL_TEXTURE_2D, 0);

glDisable(GL_TEXTURE_2D);

glutSwapBuffers();

// 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!

0 Likes
1 Solution
VincentSC
Adept II

Via Twitter I got and answer from Anteru:

Swizzle texture access


// (Not tried) I would assume this gets ignored by OpenCL but it does affect OpenGL state.

// This affects how the shader reads from the texture, but doesn't change the format itself.

::glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);

::glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);

::glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);

::glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);


Rex Guo came with this link: http://www.opengl.org/registry/specs/ARB/texture_swizzle.txt

View solution in original post

0 Likes
2 Replies
himanshu_gautam
Grandmaster

Hi

I am not sure how to change the image format form RGBA to BGRA using CLCreateImage/OpenCL functions.

Probably you can use the type cast only as you mentioned. 

If you find any solutions please share with us.


0 Likes
VincentSC
Adept II

Via Twitter I got and answer from Anteru:

Swizzle texture access


// (Not tried) I would assume this gets ignored by OpenCL but it does affect OpenGL state.

// This affects how the shader reads from the texture, but doesn't change the format itself.

::glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);

::glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);

::glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_BLUE);

::glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);


Rex Guo came with this link: http://www.opengl.org/registry/specs/ARB/texture_swizzle.txt

0 Likes