Question

I have an OpenCL buffer containing an 2D image. This image have stride bigger than its width. I need to make OpenCL image from this buffer. The problem is that function clEnqueueCopyImageToBuffer does not contain stride as an input parameter. Is it possible to make OpenCL image from OpenCL buffer(with stride bigger than width), with only one copying or faster? The one way to solve this problem is to write own kernel, but maybe there are much more neat solutions?

Was it helpful?

Solution

Unfortunately, there is no method in the OpenCL specification which allows you to directly create an image from a buffer when the buffer data has a stride not equal to the image width. The most efficient solution would probably be to write your own kernel to do this.

The simplest solution that doesn't involve writing your own kernel would be to copy one line at a time with clEnqueueCopyBufferToImage. If your image is big enough, it might be that the performance of this technique would be reasonably comparable to the hand-written kernel, but you would have to try it out to see.


I didn't include the clEnqueueCopyBufferRect approach in my original answer because my first instinct was that the extra copy would kill performance. However, the comments above got me thinking about it further, and I was interested enough to implement all three approaches to see what the performance was actually like.

performance results

As I suspected, the fastest approach was to implement a kernel to do this directly. However, copying the data over line-by-line was significantly slower than I had anticipated. Copying the buffer into an intermediate buffer with clEnqueueCopyBufferRect is actually a pretty good compromise of performance and simplicity, although is still a couple of times slower than the kernel implementation.

The source code for this little experiment can be found here. I was copying a 1020x1020 image with a stride of 1024, and the timings are averaged over 8 runs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top