Image2d or Buffer?

Hello everyone

I’m fairly new to opencl and i mainly want to use it for image processing.
So my question is, whether it is generally better to use Image2D instead of a regular uchar Buffer? Especially considering that my kernels will only be dealing with image sizes of lets say 640x480 pixels.

For example, should this:

__kernel void convert2gray(__read_only image2d_t src, __write_only image2d_t dst)
{...}

usually be faster than this:

__kernel void convert2gray(__global unsigned char* src, __global unsigned char* dst)
{...}

?

I hope you understand, what i’m aiming at.

Thanks for any reply in advance!

In my experience, if you’re dealing with bytes - just use images. Use the UNORM_INT8 type and do all your maths using floats. For specific algorithms on some hardware you may get better performance with arrays, but to start with just sticking to images makes things easier and they work well for many algorithms.

If you’re using floats with fixed addressing (i.e. linear access, no interpolation) and need to do several iterations on read/write data i’ve found arrays can be a fair bit faster (at least on the hardware i’ve used), so as always - it depends on your algorithms and what you’re doing.

Alright, I see. Thanks for the answer!

So if I understand you correctly, using images could make things a little easier (more handy) . But, does using them ever speed things up by any occasion? Or in other words, could using arrays instead of images slow things down?

Right now it seems that using images will in most cases not slow things down but will not speed them up either. So i might aswell use arrays for everything. Is that correct?

It totally depends on what you’re doing. They will slow things down sometimes - although how much overhead they have depends on the hardware & driver too.

Images are good if you need:

  • interpolation sampling (effectively free)
  • non-linear access (e.g. affine transformations, or y-order 2d)
  • automatic data conversion e.g. from ubyte to float and back again (effectively free)
  • 2d-spatial coherency

Images have a different in-memory layout which is 2d-oriented. Arrays are slow for accessing bytes or non-linear access.

You seem set on arrays, just do what you want really.