boostorg / compute

A C++ GPU Computing Library for OpenCL
http://boostorg.github.io/compute/
Boost Software License 1.0
1.52k stars 333 forks source link

How can I return a custom struct from a BOOST_COMPUTE_FUNCTION #863

Open phillvancejr opened 3 years ago

phillvancejr commented 3 years ago

Lets say I have a pixel struct and want to loop through two vectors of pixels add the components of the two pixels together and then put them in another vector, how can I return a new struct from transform?

struct pixel {
  int r, g, b, a;
};

BOOST_COMPUTE_ADAPT_STRUCT(pixel, pixel, (r,g,b,a))

//main

vector<pixel> image1( size, pixel{0,0,0,1});
vector<pixel> image2( size, pixel{1,0,0,1});
vector<pixel> result( size, pixel{0,0,0,1});

// setup up device, context, queue and device vectors ...

BOOST_COMPUTE_FUNCTION(pixel, add_images, (pixel a, pixel b),
{
  pixel out;
  out.r = a.r + b.r;
  // etc....

  return out;
});

compute::transform(
  device_image1.begin(),
  device_image1.end(),
  device_image2.begin(),
  device_result.begin(),
  add_images,
  queue
);

// get results...

How do I allocate memory for the result pixel inside the Compute Function?