2
0
mirror of https://github.com/boostorg/compute.git synced 2026-02-21 02:52:14 +00:00

Refactor image classes

This commit is contained in:
Kyle Lutz
2015-02-07 14:50:27 -08:00
parent 6e38dafbd5
commit ca0bc4b7d3
37 changed files with 1703 additions and 1496 deletions

View File

@@ -22,10 +22,7 @@
#include <boost/compute/context.hpp>
#include <boost/compute/device.hpp>
#include <boost/compute/functional.hpp>
#include <boost/compute/image2d.hpp>
#include <boost/compute/image3d.hpp>
#include <boost/compute/image_format.hpp>
#include <boost/compute/image_sampler.hpp>
#include <boost/compute/image.hpp>
#include <boost/compute/iterator.hpp>
#include <boost/compute/kernel.hpp>
#include <boost/compute/lambda.hpp>

View File

@@ -8,10 +8,11 @@
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_COMMAND_QUEUE_H
#define BOOST_COMPUTE_COMMAND_QUEUE_H
#ifndef BOOST_COMPUTE_COMMAND_QUEUE_HPP
#define BOOST_COMPUTE_COMMAND_QUEUE_HPP
#include <cstddef>
#include <algorithm>
#include <boost/assert.hpp>
@@ -21,9 +22,11 @@
#include <boost/compute/device.hpp>
#include <boost/compute/kernel.hpp>
#include <boost/compute/context.hpp>
#include <boost/compute/image2d.hpp>
#include <boost/compute/image3d.hpp>
#include <boost/compute/exception.hpp>
#include <boost/compute/image/image1d.hpp>
#include <boost/compute/image/image2d.hpp>
#include <boost/compute/image/image3d.hpp>
#include <boost/compute/image/image_object.hpp>
#include <boost/compute/utility/wait_list.hpp>
#include <boost/compute/detail/get_object_info.hpp>
#include <boost/compute/detail/assert_cl_success.hpp>
@@ -675,51 +678,17 @@ public:
/// Enqueues a command to read data from \p image to host memory.
///
/// \see_opencl_ref{clEnqueueReadImage}
void enqueue_read_image(const image2d &image,
const size_t origin[2],
const size_t region[2],
size_t row_pitch,
void *host_ptr,
const wait_list &events = wait_list())
event enqueue_read_image(const image_object& image,
const size_t *origin,
const size_t *region,
size_t row_pitch,
size_t slice_pitch,
void *host_ptr,
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(image.get_context() == this->get_context());
const size_t origin3[3] = { origin[0], origin[1], size_t(0) };
const size_t region3[3] = { region[0], region[1], size_t(1) };
cl_int ret = clEnqueueReadImage(
m_queue,
image.get(),
CL_TRUE,
origin3,
region3,
row_pitch,
0,
host_ptr,
events.size(),
events.get_event_ptr(),
0
);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(opencl_error(ret));
}
}
/// Enqueues a command to read data from \p image to host memory.
///
/// \see_opencl_ref{clEnqueueReadImage}
void enqueue_read_image(const image3d &image,
const size_t origin[3],
const size_t region[3],
size_t row_pitch,
size_t slice_pitch,
void *host_ptr,
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(image.get_context() == this->get_context());
event event_;
cl_int ret = clEnqueueReadImage(
m_queue,
@@ -732,62 +701,53 @@ public:
host_ptr,
events.size(),
events.get_event_ptr(),
0
&event_.get()
);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(opencl_error(ret));
}
return event_;
}
/// Enqueues a command to write data from host memory to \p image.
///
/// \see_opencl_ref{clEnqueueWriteImage}
void enqueue_write_image(const image2d &image,
const size_t origin[2],
const size_t region[2],
size_t input_row_pitch,
const void *host_ptr,
/// \overload
template<size_t N>
event enqueue_read_image(const image_object& image,
const extents<N> origin,
const extents<N> region,
void *host_ptr,
size_t row_pitch = 0,
size_t slice_pitch = 0,
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(image.get_context() == this->get_context());
const size_t origin3[3] = { origin[0], origin[1], size_t(0) };
const size_t region3[3] = { region[0], region[1], size_t(1) };
size_t origin3[3] = { 0, 0, 0 };
size_t region3[3] = { 1, 1, 1 };
cl_int ret = clEnqueueWriteImage(
m_queue,
image.get(),
CL_TRUE,
origin3,
region3,
input_row_pitch,
0,
host_ptr,
events.size(),
events.get_event_ptr(),
0
std::copy(origin.data(), origin.data() + N, origin3);
std::copy(region.data(), region.data() + N, region3);
return enqueue_read_image(
image, origin3, region3, row_pitch, slice_pitch, host_ptr, events
);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(opencl_error(ret));
}
}
/// Enqueues a command to write data from host memory to \p image.
///
/// \see_opencl_ref{clEnqueueWriteImage}
void enqueue_write_image(const image3d &image,
const size_t origin[3],
const size_t region[3],
size_t input_row_pitch,
size_t input_slice_pitch,
const void *host_ptr,
const wait_list &events = wait_list())
event enqueue_write_image(image_object& image,
const size_t *origin,
const size_t *region,
const void *host_ptr,
size_t input_row_pitch = 0,
size_t input_slice_pitch = 0,
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(image.get_context() == this->get_context());
event event_;
cl_int ret = clEnqueueWriteImage(
m_queue,
@@ -800,45 +760,6 @@ public:
host_ptr,
events.size(),
events.get_event_ptr(),
0
);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(opencl_error(ret));
}
}
/// Enqueues a command to copy data from \p src_image to \p dst_image.
///
/// \see_opencl_ref{clEnqueueCopyImage}
event enqueue_copy_image(const image2d &src_image,
const image2d &dst_image,
const size_t src_origin[2],
const size_t dst_origin[2],
const size_t region[2],
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(src_image.get_context() == this->get_context());
BOOST_ASSERT(dst_image.get_context() == this->get_context());
BOOST_ASSERT_MSG(src_image.get_format() == dst_image.get_format(),
"Source and destination image formats must match.");
const size_t src_origin3[3] = { src_origin[0], src_origin[1], size_t(0) };
const size_t dst_origin3[3] = { dst_origin[0], dst_origin[1], size_t(0) };
const size_t region3[3] = { region[0], region[1], size_t(1) };
event event_;
cl_int ret = clEnqueueCopyImage(
m_queue,
src_image.get(),
dst_image.get(),
src_origin3,
dst_origin3,
region3,
events.size(),
events.get_event_ptr(),
&event_.get()
);
@@ -849,101 +770,40 @@ public:
return event_;
}
/// Enqueues a command to copy data from \p src_image to \p dst_image.
///
/// \see_opencl_ref{clEnqueueCopyImage}
event enqueue_copy_image(const image2d &src_image,
const image3d &dst_image,
const size_t src_origin[2],
const size_t dst_origin[3],
const size_t region[2],
const wait_list &events = wait_list())
/// \overload
template<size_t N>
event enqueue_write_image(image_object& image,
const extents<N> origin,
const extents<N> region,
const void *host_ptr,
const size_t input_row_pitch = 0,
const size_t input_slice_pitch = 0,
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(src_image.get_context() == this->get_context());
BOOST_ASSERT(dst_image.get_context() == this->get_context());
BOOST_ASSERT_MSG(src_image.get_format() == dst_image.get_format(),
"Source and destination image formats must match.");
BOOST_ASSERT(image.get_context() == this->get_context());
const size_t src_origin3[3] = { src_origin[0], src_origin[1], size_t(0) };
const size_t region3[3] = { region[0], region[1], size_t(1) };
size_t origin3[3] = { 0, 0, 0 };
size_t region3[3] = { 1, 1, 1 };
event event_;
std::copy(origin.data(), origin.data() + N, origin3);
std::copy(region.data(), region.data() + N, region3);
cl_int ret = clEnqueueCopyImage(
m_queue,
src_image.get(),
dst_image.get(),
src_origin3,
dst_origin,
region3,
events.size(),
events.get_event_ptr(),
&event_.get()
return enqueue_write_image(
image, origin3, region3, host_ptr, input_row_pitch, input_slice_pitch, events
);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(opencl_error(ret));
}
return event_;
}
/// Enqueues a command to copy data from \p src_image to \p dst_image.
///
/// \see_opencl_ref{clEnqueueCopyImage}
event enqueue_copy_image(const image3d &src_image,
const image2d &dst_image,
const size_t src_origin[3],
const size_t dst_origin[2],
const size_t region[2],
event enqueue_copy_image(const image_object& src_image,
image_object& dst_image,
const size_t *src_origin,
const size_t *dst_origin,
const size_t *region,
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(src_image.get_context() == this->get_context());
BOOST_ASSERT(dst_image.get_context() == this->get_context());
BOOST_ASSERT_MSG(src_image.get_format() == dst_image.get_format(),
"Source and destination image formats must match.");
const size_t dst_origin3[3] = { dst_origin[0], dst_origin[1], size_t(0) };
const size_t region3[3] = { region[0], region[1], size_t(1) };
event event_;
cl_int ret = clEnqueueCopyImage(
m_queue,
src_image.get(),
dst_image.get(),
src_origin,
dst_origin3,
region3,
events.size(),
events.get_event_ptr(),
&event_.get()
);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(opencl_error(ret));
}
return event_;
}
/// Enqueues a command to copy data from \p src_image to \p dst_image.
///
/// \see_opencl_ref{clEnqueueCopyImage}
event enqueue_copy_image(const image3d &src_image,
const image3d &dst_image,
const size_t src_origin[3],
const size_t dst_origin[3],
const size_t region[3],
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(src_image.get_context() == this->get_context());
BOOST_ASSERT(dst_image.get_context() == this->get_context());
BOOST_ASSERT_MSG(src_image.get_format() == dst_image.get_format(),
"Source and destination image formats must match.");
event event_;
@@ -966,57 +826,44 @@ public:
return event_;
}
/// Enqueues a command to copy data from \p src_image to \p dst_buffer.
///
/// \see_opencl_ref{clEnqueueCopyImageToBuffer}
event enqueue_copy_image_to_buffer(const image2d &src_image,
const buffer &dst_buffer,
const size_t src_origin[2],
const size_t region[2],
size_t dst_offset,
const wait_list &events = wait_list())
/// \overload
template<size_t N>
event enqueue_copy_image(const image_object& src_image,
image_object& dst_image,
const extents<N> src_origin,
const extents<N> dst_origin,
const extents<N> region,
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(src_image.get_context() == this->get_context());
BOOST_ASSERT(dst_buffer.get_context() == this->get_context());
BOOST_ASSERT(dst_image.get_context() == this->get_context());
BOOST_ASSERT_MSG(src_image.format() == dst_image.format(),
"Source and destination image formats must match.");
const size_t src_origin3[3] = { src_origin[0], src_origin[1], size_t(0) };
const size_t region3[3] = { region[0], region[1], size_t(1) };
size_t src_origin3[3] = { 0, 0, 0 };
size_t dst_origin3[3] = { 0, 0, 0 };
size_t region3[3] = { 1, 1, 1 };
event event_;
std::copy(src_origin.data(), src_origin.data() + N, src_origin3);
std::copy(dst_origin.data(), dst_origin.data() + N, dst_origin3);
std::copy(region.data(), region.data() + N, region3);
cl_int ret = clEnqueueCopyImageToBuffer(
m_queue,
src_image.get(),
dst_buffer.get(),
src_origin3,
region3,
dst_offset,
events.size(),
events.get_event_ptr(),
&event_.get()
return enqueue_copy_image(
src_image, dst_image, src_origin3, dst_origin3, region3, events
);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(opencl_error(ret));
}
return event_;
}
/// Enqueues a command to copy data from \p src_image to \p dst_buffer.
///
/// \see_opencl_ref{clEnqueueCopyImageToBuffer}
event enqueue_copy_image_to_buffer(const image3d &src_image,
const buffer &dst_buffer,
const size_t src_origin[3],
const size_t region[3],
event enqueue_copy_image_to_buffer(const image_object& src_image,
memory_object& dst_buffer,
const size_t *src_origin,
const size_t *region,
size_t dst_offset,
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(src_image.get_context() == this->get_context());
BOOST_ASSERT(dst_buffer.get_context() == this->get_context());
event event_;
@@ -1042,54 +889,14 @@ public:
/// Enqueues a command to copy data from \p src_buffer to \p dst_image.
///
/// \see_opencl_ref{clEnqueueCopyBufferToImage}
event enqueue_copy_buffer_to_image(const buffer &src_buffer,
const image2d &dst_image,
event enqueue_copy_buffer_to_image(const memory_object& src_buffer,
image_object& dst_image,
size_t src_offset,
const size_t dst_origin[3],
const size_t region[3],
const size_t *dst_origin,
const size_t *region,
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(src_buffer.get_context() == this->get_context());
BOOST_ASSERT(dst_image.get_context() == this->get_context());
const size_t dst_origin3[3] = { dst_origin[0], dst_origin[1], size_t(0) };
const size_t region3[3] = { region[0], region[1], size_t(1) };
event event_;
cl_int ret = clEnqueueCopyBufferToImage(
m_queue,
src_buffer.get(),
dst_image.get(),
src_offset,
dst_origin3,
region3,
events.size(),
events.get_event_ptr(),
&event_.get()
);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(opencl_error(ret));
}
return event_;
}
/// Enqueues a command to copy data from \p src_buffer to \p dst_image.
///
/// \see_opencl_ref{clEnqueueCopyBufferToImage}
event enqueue_copy_buffer_to_image(const buffer &src_buffer,
const image3d &dst_image,
size_t src_offset,
const size_t dst_origin[3],
const size_t region[3],
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(src_buffer.get_context() == this->get_context());
BOOST_ASSERT(dst_image.get_context() == this->get_context());
event event_;
@@ -1118,51 +925,13 @@ public:
/// \see_opencl_ref{clEnqueueFillImage}
///
/// \opencl_version_warning{1,2}
event enqueue_fill_image(const image2d &image,
event enqueue_fill_image(image_object& image,
const void *fill_color,
const size_t origin[2],
const size_t region[2],
const size_t *origin,
const size_t *region,
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(image.get_context() == this->get_context());
const size_t origin3[3] = { origin[0], origin[1], size_t(0) };
const size_t region3[3] = { region[0], region[1], size_t(1) };
event event_;
cl_int ret = clEnqueueFillImage(
m_queue,
image.get(),
fill_color,
origin3,
region3,
events.size(),
events.get_event_ptr(),
&event_.get()
);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(opencl_error(ret));
}
return event_;
}
/// Enqueues a command to fill \p image with \p fill_color.
///
/// \see_opencl_ref{clEnqueueFillImage}
///
/// \opencl_version_warning{1,2}
event enqueue_fill_image(const image3d &image,
const void *fill_color,
const size_t origin[3],
const size_t region[3],
const wait_list &events = wait_list())
{
BOOST_ASSERT(m_queue != 0);
BOOST_ASSERT(image.get_context() == this->get_context());
event event_;
@@ -1184,6 +953,27 @@ public:
return event_;
}
/// \overload
template<size_t N>
event enqueue_fill_image(image_object& image,
const void *fill_color,
const extents<N> origin,
const extents<N> region,
const wait_list &events = wait_list())
{
BOOST_ASSERT(image.get_context() == this->get_context());
size_t origin3[3] = { 0, 0, 0 };
size_t region3[3] = { 1, 1, 1 };
std::copy(origin.data(), origin.data() + N, origin3);
std::copy(region.data(), region.data() + N, region3);
return enqueue_fill_image(
image, fill_color, origin3, region3, events
);
}
/// Enqueues a command to migrate \p mem_objects.
///
/// \see_opencl_ref{clEnqueueMigrateMemObjects}
@@ -1659,16 +1449,35 @@ inline buffer buffer::clone(command_queue &queue) const
return copy;
}
inline image1d image1d::clone(command_queue &queue) const
{
image1d copy(
get_context(), width(), format(), get_memory_flags()
);
queue.enqueue_copy_image(*this, copy, origin(), copy.origin(), size());
return copy;
}
inline image2d image2d::clone(command_queue &queue) const
{
image2d copy(
get_context(), get_memory_flags(), get_format(), width(), height(), 0, 0
get_context(), width(), height(), format(), get_memory_flags()
);
size_t origin[2] = { 0, 0 };
size_t region[2] = { this->width(), this->height() };
queue.enqueue_copy_image(*this, copy, origin(), copy.origin(), size());
queue.enqueue_copy_image(*this, copy, origin, origin, region);
return copy;
}
inline image3d image3d::clone(command_queue &queue) const
{
image3d copy(
get_context(), width(), height(), depth(), format(), get_memory_flags()
);
queue.enqueue_copy_image(*this, copy, origin(), copy.origin(), size());
return copy;
}
@@ -1684,4 +1493,4 @@ BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(command_queue,
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_COMMAND_QUEUE_H
#endif // BOOST_COMPUTE_COMMAND_QUEUE_HPP

View File

@@ -21,10 +21,6 @@
#include <boost/compute/context.hpp>
#include <boost/compute/device.hpp>
#include <boost/compute/event.hpp>
#include <boost/compute/image2d.hpp>
#include <boost/compute/image3d.hpp>
#include <boost/compute/image_format.hpp>
#include <boost/compute/image_sampler.hpp>
#include <boost/compute/kernel.hpp>
#include <boost/compute/memory_object.hpp>
#include <boost/compute/platform.hpp>

View File

@@ -26,14 +26,13 @@
#include <boost/preprocessor/repetition.hpp>
#include <boost/compute/kernel.hpp>
#include <boost/compute/image2d.hpp>
#include <boost/compute/image3d.hpp>
#include <boost/compute/closure.hpp>
#include <boost/compute/function.hpp>
#include <boost/compute/functional.hpp>
#include <boost/compute/type_traits.hpp>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/image_sampler.hpp>
#include <boost/compute/image/image2d.hpp>
#include <boost/compute/image/image_sampler.hpp>
#include <boost/compute/memory_object.hpp>
#include <boost/compute/detail/device_ptr.hpp>
#include <boost/compute/detail/sha1.hpp>

View File

@@ -0,0 +1,25 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_IMAGE_HPP
#define BOOST_COMPUTE_IMAGE_HPP
/// \file
///
/// Meta-header to include all Boost.Compute image headers.
#include <boost/compute/image/image1d.hpp>
#include <boost/compute/image/image2d.hpp>
#include <boost/compute/image/image3d.hpp>
#include <boost/compute/image/image_format.hpp>
#include <boost/compute/image/image_object.hpp>
#include <boost/compute/image/image_sampler.hpp>
#endif // BOOST_COMPUTE_IMAGE_HPP

View File

@@ -0,0 +1,204 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_IMAGE_IMAGE1D_HPP
#define BOOST_COMPUTE_IMAGE_IMAGE1D_HPP
#include <boost/throw_exception.hpp>
#include <boost/compute/config.hpp>
#include <boost/compute/exception/opencl_error.hpp>
#include <boost/compute/image/image_format.hpp>
#include <boost/compute/image/image_object.hpp>
#include <boost/compute/type_traits/type_name.hpp>
#include <boost/compute/utility/extents.hpp>
namespace boost {
namespace compute {
// forward declarations
class command_queue;
/// \class image1d
/// \brief An OpenCL 1D image object
///
/// \opencl_version_warning{1,2}
///
/// \see image_format, image2d
class image1d : public image_object
{
public:
/// Creates a null image1d object.
image1d()
: image_object()
{
}
/// Creates a new image1d object.
///
/// \see_opencl_ref{clCreateImage}
image1d(const context &context,
size_t image_width,
const image_format &format,
cl_mem_flags flags = read_write,
void *host_ptr = 0)
{
#ifdef CL_VERSION_1_2
cl_image_desc desc;
desc.image_type = CL_MEM_OBJECT_IMAGE1D;
desc.image_width = image_width;
desc.image_height = 1;
desc.image_depth = 1;
desc.image_array_size = 0;
desc.image_row_pitch = 0;
desc.image_slice_pitch = 0;
desc.num_mip_levels = 0;
desc.num_samples = 0;
#ifdef CL_VERSION_2_0
desc.mem_object = 0;
#else
desc.buffer = 0;
#endif
cl_int error = 0;
m_mem = clCreateImage(
context, flags, format.get_format_ptr(), &desc, host_ptr, &error
);
if(!m_mem){
BOOST_THROW_EXCEPTION(opencl_error(error));
}
#else
// image1d objects are only supported in OpenCL 1.2 and later
BOOST_THROW_EXCEPTION(opencl_error(CL_IMAGE_FORMAT_NOT_SUPPORTED));
#endif
}
/// Creates a new image1d as a copy of \p other.
image1d(const image1d &other)
: image_object(other)
{
}
/// Copies the image1d from \p other.
image1d& operator=(const image1d &other)
{
image_object::operator=(other);
return *this;
}
#ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Move-constructs a new image object from \p other.
image1d(image1d&& other) BOOST_NOEXCEPT
: image_object(std::move(other))
{
}
/// Move-assigns the image from \p other to \c *this.
image1d& operator=(image1d&& other) BOOST_NOEXCEPT
{
image_object::operator=(std::move(other));
return *this;
}
#endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Destroys the image1d object.
~image1d()
{
}
/// Returns the size (width) of the image.
extents<1> size() const
{
extents<1> size;
size[0] = get_info<size_t>(CL_IMAGE_WIDTH);
return size;
}
/// Returns the origin of the image (\c 0).
extents<1> origin() const
{
return extents<1>();
}
/// Returns information about the image.
///
/// \see_opencl_ref{clGetImageInfo}
template<class T>
T get_info(cl_image_info info) const
{
return get_image_info<T>(info);
}
/// \overload
template<int Enum>
typename detail::get_object_info_type<image1d, Enum>::type
get_info() const;
/// Returns the supported image formats for the context.
///
/// \see_opencl_ref{clGetSupportedImageFormats}
static std::vector<image_format>
get_supported_formats(const context &context, cl_mem_flags flags = read_write)
{
#ifdef CL_VERSION_1_2
return image_object::get_supported_formats(context, CL_MEM_OBJECT_IMAGE1D, flags);
#else
return std::vector<image_format>();
#endif
}
/// Returns \c true if \p format is a supported 1D image format for
/// \p context.
static bool is_supported_format(const image_format &format,
const context &context,
cl_mem_flags flags = read_write)
{
#ifdef CL_VERSION_1_2
return image_object::is_supported_format(
format, context, CL_MEM_OBJECT_IMAGE1D, flags
);
#else
return false;
#endif
}
/// Creates a new image with a copy of the data in \c *this. Uses \p queue
/// to perform the copy operation.
image1d clone(command_queue &queue) const;
};
/// \internal_ define get_info() specializations for image1d
BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(image1d,
((cl_image_format, CL_IMAGE_FORMAT))
((size_t, CL_IMAGE_ELEMENT_SIZE))
((size_t, CL_IMAGE_ROW_PITCH))
((size_t, CL_IMAGE_SLICE_PITCH))
((size_t, CL_IMAGE_WIDTH))
((size_t, CL_IMAGE_HEIGHT))
((size_t, CL_IMAGE_DEPTH))
)
namespace detail {
// set_kernel_arg() specialization for image1d
template<>
struct set_kernel_arg<image1d> : public set_kernel_arg<image_object> { };
} // end detail namespace
} // end compute namespace
} // end boost namespace
BOOST_COMPUTE_TYPE_NAME(boost::compute::image1d, image1d_t)
#endif // BOOST_COMPUTE_IMAGE_IMAGE1D_HPP

View File

@@ -0,0 +1,262 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_IMAGE_IMAGE2D_HPP
#define BOOST_COMPUTE_IMAGE_IMAGE2D_HPP
#include <boost/throw_exception.hpp>
#include <boost/compute/config.hpp>
#include <boost/compute/context.hpp>
#include <boost/compute/exception/opencl_error.hpp>
#include <boost/compute/image/image_format.hpp>
#include <boost/compute/image/image_object.hpp>
#include <boost/compute/detail/get_object_info.hpp>
#include <boost/compute/type_traits/type_name.hpp>
#include <boost/compute/utility/extents.hpp>
namespace boost {
namespace compute {
// forward declarations
class command_queue;
/// \class image2d
/// \brief An OpenCL 2D image object
///
/// For example, to create a 640x480 8-bit RGBA image:
///
/// \snippet test/test_image2d.cpp create_image
///
/// \see image_format, image3d
class image2d : public image_object
{
public:
/// Creates a null image2d object.
image2d()
: image_object()
{
}
/// Creates a new image2d object.
///
/// \see_opencl_ref{clCreateImage}
image2d(const context &context,
size_t image_width,
size_t image_height,
const image_format &format,
cl_mem_flags flags = read_write,
void *host_ptr = 0,
size_t image_row_pitch = 0)
{
cl_int error = 0;
#ifdef CL_VERSION_1_2
cl_image_desc desc;
desc.image_type = CL_MEM_OBJECT_IMAGE2D;
desc.image_width = image_width;
desc.image_height = image_height;
desc.image_depth = 1;
desc.image_array_size = 0;
desc.image_row_pitch = image_row_pitch;
desc.image_slice_pitch = 0;
desc.num_mip_levels = 0;
desc.num_samples = 0;
#ifdef CL_VERSION_2_0
desc.mem_object = 0;
#else
desc.buffer = 0;
#endif
m_mem = clCreateImage(context,
flags,
format.get_format_ptr(),
&desc,
host_ptr,
&error);
#else
m_mem = clCreateImage2D(context,
flags,
format.get_format_ptr(),
image_width,
image_height,
image_row_pitch,
host_ptr,
&error);
#endif
if(!m_mem){
BOOST_THROW_EXCEPTION(opencl_error(error));
}
}
/// \internal_ (deprecated)
image2d(const context &context,
cl_mem_flags flags,
const image_format &format,
size_t image_width,
size_t image_height,
size_t image_row_pitch = 0,
void *host_ptr = 0)
{
cl_int error = 0;
#ifdef CL_VERSION_1_2
cl_image_desc desc;
desc.image_type = CL_MEM_OBJECT_IMAGE2D;
desc.image_width = image_width;
desc.image_height = image_height;
desc.image_depth = 1;
desc.image_array_size = 0;
desc.image_row_pitch = image_row_pitch;
desc.image_slice_pitch = 0;
desc.num_mip_levels = 0;
desc.num_samples = 0;
#ifdef CL_VERSION_2_0
desc.mem_object = 0;
#else
desc.buffer = 0;
#endif
m_mem = clCreateImage(context,
flags,
format.get_format_ptr(),
&desc,
host_ptr,
&error);
#else
m_mem = clCreateImage2D(context,
flags,
format.get_format_ptr(),
image_width,
image_height,
image_row_pitch,
host_ptr,
&error);
#endif
if(!m_mem){
BOOST_THROW_EXCEPTION(opencl_error(error));
}
}
/// Creates a new image2d as a copy of \p other.
image2d(const image2d &other)
: image_object(other)
{
}
/// Copies the image2d from \p other.
image2d& operator=(const image2d &other)
{
image_object::operator=(other);
return *this;
}
#ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Move-constructs a new image object from \p other.
image2d(image2d&& other) BOOST_NOEXCEPT
: image_object(std::move(other))
{
}
/// Move-assigns the image from \p other to \c *this.
image2d& operator=(image2d&& other) BOOST_NOEXCEPT
{
image_object::operator=(std::move(other));
return *this;
}
#endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Destroys the image2d object.
~image2d()
{
}
/// Returns the size (width, height) of the image.
extents<2> size() const
{
extents<2> size;
size[0] = get_info<size_t>(CL_IMAGE_WIDTH);
size[1] = get_info<size_t>(CL_IMAGE_HEIGHT);
return size;
}
/// Returns the origin of the image (\c 0, \c 0).
extents<2> origin() const
{
return extents<2>();
}
/// Returns information about the image.
///
/// \see_opencl_ref{clGetImageInfo}
template<class T>
T get_info(cl_image_info info) const
{
return detail::get_object_info<T>(clGetImageInfo, m_mem, info);
}
/// \overload
template<int Enum>
typename detail::get_object_info_type<image2d, Enum>::type
get_info() const;
/// Returns the supported image formats for the context.
///
/// \see_opencl_ref{clGetSupportedImageFormats}
static std::vector<image_format>
get_supported_formats(const context &context, cl_mem_flags flags = read_write)
{
return image_object::get_supported_formats(context, CL_MEM_OBJECT_IMAGE2D, flags);
}
/// Returns \c true if \p format is a supported 2D image format for
/// \p context.
static bool is_supported_format(const image_format &format,
const context &context,
cl_mem_flags flags = read_write)
{
return image_object::is_supported_format(
format, context, CL_MEM_OBJECT_IMAGE2D, flags
);
}
/// Creates a new image with a copy of the data in \c *this. Uses \p queue
/// to perform the copy operation.
image2d clone(command_queue &queue) const;
};
/// \internal_ define get_info() specializations for image2d
BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(image2d,
((cl_image_format, CL_IMAGE_FORMAT))
((size_t, CL_IMAGE_ELEMENT_SIZE))
((size_t, CL_IMAGE_ROW_PITCH))
((size_t, CL_IMAGE_SLICE_PITCH))
((size_t, CL_IMAGE_WIDTH))
((size_t, CL_IMAGE_HEIGHT))
((size_t, CL_IMAGE_DEPTH))
)
namespace detail {
// set_kernel_arg() specialization for image2d
template<>
struct set_kernel_arg<image2d> : public set_kernel_arg<image_object> { };
} // end detail namespace
} // end compute namespace
} // end boost namespace
BOOST_COMPUTE_TYPE_NAME(boost::compute::image2d, image2d_t)
#endif // BOOST_COMPUTE_IMAGE_IMAGE2D_HPP

View File

@@ -0,0 +1,262 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_IMAGE_IMAGE3D_HPP
#define BOOST_COMPUTE_IMAGE_IMAGE3D_HPP
#include <boost/throw_exception.hpp>
#include <boost/compute/detail/get_object_info.hpp>
#include <boost/compute/exception/opencl_error.hpp>
#include <boost/compute/image/image_format.hpp>
#include <boost/compute/image/image_object.hpp>
#include <boost/compute/type_traits/type_name.hpp>
#include <boost/compute/utility/extents.hpp>
namespace boost {
namespace compute {
/// \class image3d
/// \brief An OpenCL 3D image object
///
/// \see image_format, image2d
class image3d : public image_object
{
public:
/// Creates a null image3d object.
image3d()
: image_object()
{
}
/// Creates a new image3d object.
///
/// \see_opencl_ref{clCreateImage}
image3d(const context &context,
size_t image_width,
size_t image_height,
size_t image_depth,
const image_format &format,
cl_mem_flags flags = read_write,
void *host_ptr = 0,
size_t image_row_pitch = 0,
size_t image_slice_pitch = 0)
{
cl_int error = 0;
#ifdef CL_VERSION_1_2
cl_image_desc desc;
desc.image_type = CL_MEM_OBJECT_IMAGE3D;
desc.image_width = image_width;
desc.image_height = image_height;
desc.image_depth = image_depth;
desc.image_array_size = 0;
desc.image_row_pitch = image_row_pitch;
desc.image_slice_pitch = image_slice_pitch;
desc.num_mip_levels = 0;
desc.num_samples = 0;
#ifdef CL_VERSION_2_0
desc.mem_object = 0;
#else
desc.buffer = 0;
#endif
m_mem = clCreateImage(context,
flags,
format.get_format_ptr(),
&desc,
host_ptr,
&error);
#else
m_mem = clCreateImage3D(context,
flags,
format.get_format_ptr(),
image_width,
image_height,
image_depth,
image_row_pitch,
image_slice_pitch,
host_ptr,
&error);
#endif
if(!m_mem){
BOOST_THROW_EXCEPTION(opencl_error(error));
}
}
/// \internal_ (deprecated)
image3d(const context &context,
cl_mem_flags flags,
const image_format &format,
size_t image_width,
size_t image_height,
size_t image_depth,
size_t image_row_pitch,
size_t image_slice_pitch = 0,
void *host_ptr = 0)
{
cl_int error = 0;
#ifdef CL_VERSION_1_2
cl_image_desc desc;
desc.image_type = CL_MEM_OBJECT_IMAGE3D;
desc.image_width = image_width;
desc.image_height = image_height;
desc.image_depth = image_depth;
desc.image_array_size = 0;
desc.image_row_pitch = image_row_pitch;
desc.image_slice_pitch = image_slice_pitch;
desc.num_mip_levels = 0;
desc.num_samples = 0;
#ifdef CL_VERSION_2_0
desc.mem_object = 0;
#else
desc.buffer = 0;
#endif
m_mem = clCreateImage(context,
flags,
format.get_format_ptr(),
&desc,
host_ptr,
&error);
#else
m_mem = clCreateImage3D(context,
flags,
format.get_format_ptr(),
image_width,
image_height,
image_depth,
image_row_pitch,
image_slice_pitch,
host_ptr,
&error);
#endif
if(!m_mem){
BOOST_THROW_EXCEPTION(opencl_error(error));
}
}
/// Creates a new image3d as a copy of \p other.
image3d(const image3d &other)
: image_object(other)
{
}
/// Copies the image3d from \p other.
image3d& operator=(const image3d &other)
{
image_object::operator=(other);
return *this;
}
#ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Move-constructs a new image object from \p other.
image3d(image3d&& other) BOOST_NOEXCEPT
: image_object(std::move(other))
{
}
/// Move-assigns the image from \p other to \c *this.
image3d& operator=(image3d&& other) BOOST_NOEXCEPT
{
image_object::operator=(std::move(other));
return *this;
}
#endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Destroys the image3d object.
~image3d()
{
}
/// Returns the size (width, height, depth) of the image.
extents<3> size() const
{
extents<3> size;
size[0] = get_info<size_t>(CL_IMAGE_WIDTH);
size[1] = get_info<size_t>(CL_IMAGE_HEIGHT);
size[2] = get_info<size_t>(CL_IMAGE_DEPTH);
return size;
}
/// Returns the origin of the image (\c 0, \c 0, \c 0).
extents<3> origin() const
{
return extents<3>();
}
/// Returns information about the image.
///
/// \see_opencl_ref{clGetImageInfo}
template<class T>
T get_info(cl_image_info info) const
{
return detail::get_object_info<T>(clGetImageInfo, m_mem, info);
}
/// \overload
template<int Enum>
typename detail::get_object_info_type<image3d, Enum>::type
get_info() const;
/// Returns the supported 3D image formats for the context.
///
/// \see_opencl_ref{clGetSupportedImageFormats}
static std::vector<image_format>
get_supported_formats(const context &context, cl_mem_flags flags = read_write)
{
return image_object::get_supported_formats(context, CL_MEM_OBJECT_IMAGE3D, flags);
}
/// Returns \c true if \p format is a supported 3D image format for
/// \p context.
static bool is_supported_format(const image_format &format,
const context &context,
cl_mem_flags flags = read_write)
{
return image_object::is_supported_format(
format, context, CL_MEM_OBJECT_IMAGE3D, flags
);
}
/// Creates a new image with a copy of the data in \c *this. Uses \p queue
/// to perform the copy operation.
image3d clone(command_queue &queue) const;
};
/// \internal_ define get_info() specializations for image3d
BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(image3d,
((cl_image_format, CL_IMAGE_FORMAT))
((size_t, CL_IMAGE_ELEMENT_SIZE))
((size_t, CL_IMAGE_ROW_PITCH))
((size_t, CL_IMAGE_SLICE_PITCH))
((size_t, CL_IMAGE_WIDTH))
((size_t, CL_IMAGE_HEIGHT))
((size_t, CL_IMAGE_DEPTH))
)
namespace detail {
// set_kernel_arg() specialization for image3d
template<>
struct set_kernel_arg<image3d> : public set_kernel_arg<image_object> { };
} // end detail namespace
} // end compute namespace
} // end boost namespace
BOOST_COMPUTE_TYPE_NAME(boost::compute::image3d, image3d_t)
#endif // BOOST_COMPUTE_IMAGE_IMAGE3D_HPP

View File

@@ -0,0 +1,135 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_IMAGE_IMAGE_FORMAT_HPP
#define BOOST_COMPUTE_IMAGE_IMAGE_FORMAT_HPP
#include <boost/compute/cl.hpp>
namespace boost {
namespace compute {
/// \class image_format
/// \brief A OpenCL image format
///
/// For example, to create a format for a 8-bit RGBA image:
/// \code
/// boost::compute::image_format rgba8(CL_RGBA, CL_UNSIGNED_INT8);
/// \endcode
///
/// After being constructed, image_format objects are usually passed to the
/// constructor of the various image classes (e.g. \ref image2d, \ref image3d)
/// to create an image object on a compute device.
///
/// Image formats supported by a context can be queried with the static
/// get_supported_formats() in each image class. For example:
/// \code
/// std::vector<image_format> formats = image2d::get_supported_formats(ctx);
/// \endcode
///
/// \see image2d
class image_format
{
public:
enum channel_order {
r = CL_R,
a = CL_A,
intensity = CL_INTENSITY,
luminance = CL_LUMINANCE,
rg = CL_RG,
ra = CL_RA,
rgb = CL_RGB,
rgba = CL_RGBA,
argb = CL_ARGB,
bgra = CL_BGRA
};
enum channel_data_type {
snorm_int8 = CL_SNORM_INT8,
snorm_int16 = CL_SNORM_INT16,
unorm_int8 = CL_UNORM_INT8,
unorm_int16 = CL_UNORM_INT16,
unorm_short_565 = CL_UNORM_SHORT_565,
unorm_short_555 = CL_UNORM_SHORT_555,
unorm_int_101010 = CL_UNORM_INT_101010,
signed_int8 = CL_SIGNED_INT8,
signed_int16 = CL_SIGNED_INT16,
signed_int32 = CL_SIGNED_INT32,
unsigned_int8 = CL_UNSIGNED_INT8,
unsigned_int16 = CL_UNSIGNED_INT16,
unsigned_int32 = CL_UNSIGNED_INT32,
float16 = CL_HALF_FLOAT,
float32 = CL_FLOAT
};
/// Creates a new image format object with \p order and \p type.
explicit image_format(cl_channel_order order, cl_channel_type type)
{
m_format.image_channel_order = order;
m_format.image_channel_data_type = type;
}
/// Creates a new image format object from \p format.
explicit image_format(const cl_image_format &format)
{
m_format.image_channel_order = format.image_channel_order;
m_format.image_channel_data_type = format.image_channel_data_type;
}
/// Creates a new image format object as a copy of \p other.
image_format(const image_format &other)
: m_format(other.m_format)
{
}
/// Copies the format from \p other to \c *this.
image_format& operator=(const image_format &other)
{
if(this != &other){
m_format = other.m_format;
}
return *this;
}
/// Destroys the image format object.
~image_format()
{
}
/// Returns a pointer to the \c cl_image_format object.
const cl_image_format* get_format_ptr() const
{
return &m_format;
}
/// Returns \c true if \c *this is the same as \p other.
bool operator==(const image_format &other) const
{
return m_format.image_channel_order ==
other.m_format.image_channel_order &&
m_format.image_channel_data_type ==
other.m_format.image_channel_data_type;
}
/// Returns \c true if \c *this is not the same as \p other.
bool operator!=(const image_format &other) const
{
return !(*this == other);
}
private:
cl_image_format m_format;
};
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_IMAGE_IMAGE_FORMAT_HPP

View File

@@ -0,0 +1,170 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_IMAGE_IMAGE_OBJECT_HPP
#define BOOST_COMPUTE_IMAGE_IMAGE_OBJECT_HPP
#include <algorithm>
#include <vector>
#include <boost/compute/config.hpp>
#include <boost/compute/memory_object.hpp>
#include <boost/compute/detail/get_object_info.hpp>
#include <boost/compute/image/image_format.hpp>
namespace boost {
namespace compute {
/// \class image_object
/// \brief Base-class for image objects.
///
/// The image_object class is the base-class for image objects on compute
/// devices.
///
/// \see image1d, image2d, image3d
class image_object : public memory_object
{
public:
image_object()
: memory_object()
{
}
explicit image_object(cl_mem mem, bool retain = true)
: memory_object(mem, retain)
{
}
image_object(const image_object &other)
: memory_object(other)
{
}
image_object& operator=(const image_object &other)
{
if(this != &other){
memory_object::operator=(other);
}
return *this;
}
#ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
image_object(image_object&& other) BOOST_NOEXCEPT
: memory_object(std::move(other))
{
}
/// \internal_
image_object& operator=(image_object&& other) BOOST_NOEXCEPT
{
memory_object::operator=(std::move(other));
return *this;
}
#endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Destroys the image object.
~image_object()
{
}
/// Returns information about the image object.
///
/// \see_opencl_ref{clGetImageInfo}
template<class T>
T get_image_info(cl_mem_info info) const
{
return detail::get_object_info<T>(clGetImageInfo, m_mem, info);
}
/// Returns the format for the image.
image_format format() const
{
return image_format(get_image_info<cl_image_format>(CL_IMAGE_FORMAT));
}
/// \internal_ (deprecated)
image_format get_format() const
{
return format();
}
/// Returns the width of the image.
size_t width() const
{
return get_image_info<size_t>(CL_IMAGE_WIDTH);
}
/// Returns the height of the image.
///
/// For 1D images, this function will return \c 1.
size_t height() const
{
return get_image_info<size_t>(CL_IMAGE_HEIGHT);
}
/// Returns the depth of the image.
///
/// For 1D and 2D images, this function will return \c 1.
size_t depth() const
{
return get_image_info<size_t>(CL_IMAGE_DEPTH);
}
/// Returns the supported image formats for the \p type in \p context.
///
/// \see_opencl_ref{clGetSupportedImageFormats}
static std::vector<image_format>
get_supported_formats(const context &context,
cl_mem_object_type type,
cl_mem_flags flags = read_write)
{
cl_uint count = 0;
clGetSupportedImageFormats(context, flags, type, 0, 0, &count);
std::vector<cl_image_format> cl_formats(count);
clGetSupportedImageFormats(context, flags, type, count, &cl_formats[0], 0);
std::vector<image_format> formats;
formats.reserve(count);
for(cl_uint i = 0; i < count; i++){
formats.push_back(image_format(cl_formats[i]));
}
return formats;
}
/// Returns \c true if \p format is a supported image format for
/// \p type in \p context with \p flags.
static bool is_supported_format(const image_format &format,
const context &context,
cl_mem_object_type type,
cl_mem_flags flags = read_write)
{
const std::vector<image_format> formats =
get_supported_formats(context, type, flags);
return std::find(formats.begin(), formats.end(), format) != formats.end();
}
};
namespace detail {
// set_kernel_arg() specialization for image_object
template<>
struct set_kernel_arg<image_object> : public set_kernel_arg<memory_object> { };
} // end detail namespace
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_IMAGE_IMAGE_OBJECT_HPP

View File

@@ -0,0 +1,221 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_IMAGE_IMAGE_SAMPLER_HPP
#define BOOST_COMPUTE_IMAGE_IMAGE_SAMPLER_HPP
#include <boost/throw_exception.hpp>
#include <boost/compute/config.hpp>
#include <boost/compute/context.hpp>
#include <boost/compute/kernel.hpp>
#include <boost/compute/detail/get_object_info.hpp>
#include <boost/compute/detail/assert_cl_success.hpp>
#include <boost/compute/exception/opencl_error.hpp>
#include <boost/compute/type_traits/type_name.hpp>
namespace boost {
namespace compute {
/// \class image_sampler
/// \brief An OpenCL image sampler object
///
/// \see image2d, image_format
class image_sampler
{
public:
enum addressing_mode {
none = CL_ADDRESS_NONE,
clamp_to_edge = CL_ADDRESS_CLAMP_TO_EDGE,
clamp = CL_ADDRESS_CLAMP,
repeat = CL_ADDRESS_REPEAT
};
enum filter_mode {
nearest = CL_FILTER_NEAREST,
linear = CL_FILTER_LINEAR
};
image_sampler()
: m_sampler(0)
{
}
image_sampler(const context &context,
bool normalized_coords,
cl_addressing_mode addressing_mode,
cl_filter_mode filter_mode)
{
cl_int error = 0;
#ifdef CL_VERSION_2_0
std::vector<cl_sampler_properties> sampler_properties;
sampler_properties.push_back(CL_SAMPLER_NORMALIZED_COORDS);
sampler_properties.push_back(cl_sampler_properties(normalized_coords));
sampler_properties.push_back(CL_SAMPLER_ADDRESSING_MODE);
sampler_properties.push_back(cl_sampler_properties(addressing_mode));
sampler_properties.push_back(CL_SAMPLER_FILTER_MODE);
sampler_properties.push_back(cl_sampler_properties(filter_mode));
sampler_properties.push_back(cl_sampler_properties(0));
m_sampler = clCreateSamplerWithProperties(
context, &sampler_properties[0], &error
);
#else
m_sampler = clCreateSampler(
context, normalized_coords, addressing_mode, filter_mode, &error
);
#endif
if(!m_sampler){
BOOST_THROW_EXCEPTION(opencl_error(error));
}
}
explicit image_sampler(cl_sampler sampler, bool retain = true)
: m_sampler(sampler)
{
if(m_sampler && retain){
clRetainSampler(m_sampler);
}
}
/// Creates a new image sampler object as a copy of \p other.
image_sampler(const image_sampler &other)
: m_sampler(other.m_sampler)
{
if(m_sampler){
clRetainSampler(m_sampler);
}
}
/// Copies the image sampler object from \p other to \c *this.
image_sampler& operator=(const image_sampler &other)
{
if(this != &other){
if(m_sampler){
clReleaseSampler(m_sampler);
}
m_sampler = other.m_sampler;
if(m_sampler){
clRetainSampler(m_sampler);
}
}
return *this;
}
#ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
image_sampler(image_sampler&& other) BOOST_NOEXCEPT
: m_sampler(other.m_sampler)
{
other.m_sampler = 0;
}
image_sampler& operator=(image_sampler&& other) BOOST_NOEXCEPT
{
if(m_sampler){
clReleaseSampler(m_sampler);
}
m_sampler = other.m_sampler;
other.m_sampler = 0;
return *this;
}
#endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Destroys the image sampler object.
~image_sampler()
{
if(m_sampler){
BOOST_COMPUTE_ASSERT_CL_SUCCESS(
clReleaseSampler(m_sampler)
);
}
}
/// Returns the underlying \c cl_sampler object.
cl_sampler& get() const
{
return const_cast<cl_sampler &>(m_sampler);
}
/// Returns the context for the image sampler object.
context get_context() const
{
return context(get_info<cl_context>(CL_SAMPLER_CONTEXT));
}
/// Returns information about the sampler.
///
/// \see_opencl_ref{clGetSamplerInfo}
template<class T>
T get_info(cl_sampler_info info) const
{
return detail::get_object_info<T>(clGetSamplerInfo, m_sampler, info);
}
/// \overload
template<int Enum>
typename detail::get_object_info_type<image_sampler, Enum>::type
get_info() const;
/// Returns \c true if the sampler is the same at \p other.
bool operator==(const image_sampler &other) const
{
return m_sampler == other.m_sampler;
}
/// Returns \c true if the sampler is different from \p other.
bool operator!=(const image_sampler &other) const
{
return m_sampler != other.m_sampler;
}
operator cl_sampler() const
{
return m_sampler;
}
private:
cl_sampler m_sampler;
};
/// \internal_ define get_info() specializations for image_sampler
BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(image_sampler,
((cl_uint, CL_SAMPLER_REFERENCE_COUNT))
((cl_context, CL_SAMPLER_CONTEXT))
((cl_addressing_mode, CL_SAMPLER_ADDRESSING_MODE))
((cl_filter_mode, CL_SAMPLER_FILTER_MODE))
((bool, CL_SAMPLER_NORMALIZED_COORDS))
)
namespace detail {
// set_kernel_arg specialization for image samplers
template<>
struct set_kernel_arg<image_sampler>
{
void operator()(kernel &kernel_, size_t index, const image_sampler &sampler)
{
kernel_.set_arg(index, sampler.get());
}
};
} // end detail namespace
} // end compute namespace
} // end boost namespace
BOOST_COMPUTE_TYPE_NAME(boost::compute::image_sampler, sampler_t)
#endif // BOOST_COMPUTE_IMAGE_IMAGE_SAMPLER_HPP

View File

@@ -1,5 +1,5 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
@@ -8,216 +8,5 @@
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_IMAGE2D_HPP
#define BOOST_COMPUTE_IMAGE2D_HPP
#include <vector>
#include <boost/throw_exception.hpp>
#include <boost/compute/config.hpp>
#include <boost/compute/context.hpp>
#include <boost/compute/exception.hpp>
#include <boost/compute/image_format.hpp>
#include <boost/compute/memory_object.hpp>
#include <boost/compute/detail/get_object_info.hpp>
#include <boost/compute/type_traits/type_name.hpp>
#include <boost/compute/utility/extents.hpp>
namespace boost {
namespace compute {
// forward declarations
class command_queue;
class image2d : public memory_object
{
public:
/// Creates a null image2d object.
image2d()
: memory_object()
{
}
/// Creates a new image2d object.
///
/// \see_opencl_ref{clCreateImage}
image2d(const context &context,
cl_mem_flags flags,
const image_format &format,
size_t image_width,
size_t image_height,
size_t image_row_pitch = 0,
void *host_ptr = 0)
{
cl_int error = 0;
#ifdef CL_VERSION_1_2
cl_image_desc desc;
desc.image_type = CL_MEM_OBJECT_IMAGE2D;
desc.image_width = image_width;
desc.image_height = image_height;
desc.image_depth = 1;
desc.image_array_size = 0;
desc.image_row_pitch = image_row_pitch;
desc.image_slice_pitch = 0;
desc.num_mip_levels = 0;
desc.num_samples = 0;
#ifdef CL_VERSION_2_0
desc.mem_object = 0;
#else
desc.buffer = 0;
#endif
m_mem = clCreateImage(context,
flags,
format.get_format_ptr(),
&desc,
host_ptr,
&error);
#else
m_mem = clCreateImage2D(context,
flags,
format.get_format_ptr(),
image_width,
image_height,
image_row_pitch,
host_ptr,
&error);
#endif
if(!m_mem){
BOOST_THROW_EXCEPTION(opencl_error(error));
}
}
/// Creates a new image2d as a copy of \p other.
image2d(const image2d &other)
: memory_object(other)
{
}
/// Copies the image2d from \p other.
image2d& operator=(const image2d &other)
{
memory_object::operator=(other);
return *this;
}
#ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Move-constructs a new image object from \p other.
image2d(image2d&& other) BOOST_NOEXCEPT
: memory_object(std::move(other))
{
}
/// Move-assigns the image from \p other to \c *this.
image2d& operator=(image2d&& other) BOOST_NOEXCEPT
{
memory_object::operator=(std::move(other));
return *this;
}
#endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Destroys the image2d object.
~image2d()
{
}
/// Returns the format for the image.
image_format get_format() const
{
return image_format(get_info<cl_image_format>(CL_IMAGE_FORMAT));
}
/// Returns the height of the image.
size_t height() const
{
return get_info<size_t>(CL_IMAGE_HEIGHT);
}
/// Returns the width of the image.
size_t width() const
{
return get_info<size_t>(CL_IMAGE_WIDTH);
}
/// Returns the size (width, height) of the image.
extents<2> size() const
{
extents<2> size;
size[0] = get_info<size_t>(CL_IMAGE_WIDTH);
size[1] = get_info<size_t>(CL_IMAGE_HEIGHT);
return size;
}
/// Returns information about the image.
///
/// \see_opencl_ref{clGetImageInfo}
template<class T>
T get_info(cl_image_info info) const
{
return detail::get_object_info<T>(clGetImageInfo, m_mem, info);
}
size_t get_pixel_count() const
{
return height() * width();
}
/// Returns the supported image formats for the context.
///
/// \see_opencl_ref{clGetSupportedImageFormats}
static std::vector<image_format> get_supported_formats(const context &context,
cl_mem_flags flags)
{
cl_uint count = 0;
clGetSupportedImageFormats(context,
flags,
CL_MEM_OBJECT_IMAGE2D,
0,
0,
&count);
std::vector<cl_image_format> cl_formats(count);
clGetSupportedImageFormats(context,
flags,
CL_MEM_OBJECT_IMAGE2D,
count,
&cl_formats[0],
0);
std::vector<image_format> formats;
for(size_t i = 0; i < count; i++){
formats.push_back(image_format(cl_formats[i]));
}
return formats;
}
/// Creates a new image with a copy of the data in \c *this. Uses \p queue
/// to perform the copy operation.
image2d clone(command_queue &queue) const;
};
namespace detail {
// set_kernel_arg specialization for image2d
template<>
struct set_kernel_arg<image2d>
{
void operator()(kernel &kernel_, size_t index, const image2d &image)
{
kernel_.set_arg(index, image.get());
}
};
} // end detail namespace
} // end compute namespace
} // end boost namespace
BOOST_COMPUTE_TYPE_NAME(boost::compute::image2d, image2d_t)
#endif // BOOST_COMPUTE_IMAGE2D_HPP
// deprecated, use <boost/compute/image/image2d.hpp> instead
#include <boost/compute/image/image2d.hpp>

View File

@@ -1,5 +1,5 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
@@ -8,206 +8,5 @@
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_IMAGE3D_HPP
#define BOOST_COMPUTE_IMAGE3D_HPP
#include <vector>
#include <boost/throw_exception.hpp>
#include <boost/compute/cl.hpp>
#include <boost/compute/context.hpp>
#include <boost/compute/exception.hpp>
#include <boost/compute/image_format.hpp>
#include <boost/compute/memory_object.hpp>
#include <boost/compute/detail/get_object_info.hpp>
#include <boost/compute/type_traits/type_name.hpp>
#include <boost/compute/utility/extents.hpp>
namespace boost {
namespace compute {
class image3d : public memory_object
{
public:
/// Creates a null image3d object.
image3d()
: memory_object()
{
}
/// Creates a new image3d object.
///
/// \see_opencl_ref{clCreateImage}
image3d(const context &context,
cl_mem_flags flags,
const image_format &format,
size_t image_width,
size_t image_height,
size_t image_depth,
size_t image_row_pitch,
size_t image_slice_pitch = 0,
void *host_ptr = 0)
{
cl_int error = 0;
#ifdef CL_VERSION_1_2
cl_image_desc desc;
desc.image_type = CL_MEM_OBJECT_IMAGE3D;
desc.image_width = image_width;
desc.image_height = image_height;
desc.image_depth = image_depth;
desc.image_array_size = 0;
desc.image_row_pitch = image_row_pitch;
desc.image_slice_pitch = image_slice_pitch;
desc.num_mip_levels = 0;
desc.num_samples = 0;
#ifdef CL_VERSION_2_0
desc.mem_object = 0;
#else
desc.buffer = 0;
#endif
m_mem = clCreateImage(context,
flags,
format.get_format_ptr(),
&desc,
host_ptr,
&error);
#else
m_mem = clCreateImage3D(context,
flags,
format.get_format_ptr(),
image_width,
image_height,
image_depth,
image_row_pitch,
image_slice_pitch,
host_ptr,
&error);
#endif
if(!m_mem){
BOOST_THROW_EXCEPTION(opencl_error(error));
}
}
/// Creates a new image3d as a copy of \p other.
image3d(const image3d &other)
: memory_object(other)
{
}
/// Copies the image3d from \p other.
image3d& operator=(const image3d &other)
{
memory_object::operator=(other);
return *this;
}
#ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Move-constructs a new image object from \p other.
image3d(image3d&& other) BOOST_NOEXCEPT
: memory_object(std::move(other))
{
}
/// Move-assigns the image from \p other to \c *this.
image3d& operator=(image3d&& other) BOOST_NOEXCEPT
{
memory_object::operator=(std::move(other));
return *this;
}
#endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
/// Destroys the image3d object.
~image3d()
{
}
/// Returns the format for the image.
image_format get_format() const
{
return image_format(get_info<cl_image_format>(CL_IMAGE_FORMAT));
}
/// Returns information about the image.
///
/// \see_opencl_ref{clGetImageInfo}
template<class T>
T get_info(cl_image_info info) const
{
return detail::get_object_info<T>(clGetImageInfo, m_mem, info);
}
/// Returns the size (width, height, depth) of the image.
extents<3> size() const
{
extents<3> size;
size[0] = get_info<size_t>(CL_IMAGE_WIDTH);
size[1] = get_info<size_t>(CL_IMAGE_HEIGHT);
size[2] = get_info<size_t>(CL_IMAGE_DEPTH);
return size;
}
size_t get_pixel_count() const
{
size_t height = get_info<size_t>(CL_IMAGE_HEIGHT);
size_t width = get_info<size_t>(CL_IMAGE_WIDTH);
size_t depth = get_info<size_t>(CL_IMAGE_DEPTH);
return height * width * depth;
}
/// Returns the supported image formats for the context.
///
/// \see_opencl_ref{clGetSupportedImageFormats}
static std::vector<image_format> get_supported_formats(const context &context,
cl_mem_flags flags)
{
cl_uint count = 0;
clGetSupportedImageFormats(context,
flags,
CL_MEM_OBJECT_IMAGE3D,
0,
0,
&count);
std::vector<cl_image_format> cl_formats(count);
clGetSupportedImageFormats(context,
flags,
CL_MEM_OBJECT_IMAGE3D,
count,
&cl_formats[0],
0);
std::vector<image_format> formats;
for(size_t i = 0; i < count; i++){
formats.push_back(image_format(cl_formats[i]));
}
return formats;
}
};
namespace detail {
// set_kernel_arg specialization for image3d
template<>
struct set_kernel_arg<image3d>
{
void operator()(kernel &kernel_, size_t index, const image3d &image)
{
kernel_.set_arg(index, image.get());
}
};
} // end detail namespace
} // end compute namespace
} // end boost namespace
BOOST_COMPUTE_TYPE_NAME(boost::compute::image3d, image3d_t)
#endif // BOOST_COMPUTE_IMAGE3D_HPP
// deprecated, use <boost/compute/image/image3d.hpp> instead
#include <boost/compute/image/image3d.hpp>

View File

@@ -1,5 +1,5 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
@@ -8,105 +8,5 @@
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_IMAGE_FORMAT_HPP
#define BOOST_COMPUTE_IMAGE_FORMAT_HPP
#include <boost/compute/cl.hpp>
namespace boost {
namespace compute {
class image_format
{
public:
enum channel_order {
r = CL_R,
a = CL_A,
intensity = CL_INTENSITY,
luminance = CL_LUMINANCE,
rg = CL_RG,
ra = CL_RA,
rgb = CL_RGB,
rgba = CL_RGBA,
argb = CL_ARGB,
bgra = CL_BGRA
};
enum channel_data_type {
snorm_int8 = CL_SNORM_INT8,
snorm_int16 = CL_SNORM_INT16,
unorm_int8 = CL_UNORM_INT8,
unorm_int16 = CL_UNORM_INT16,
unorm_short_565 = CL_UNORM_SHORT_565,
unorm_short_555 = CL_UNORM_SHORT_555,
unorm_int_101010 = CL_UNORM_INT_101010,
signed_int8 = CL_SIGNED_INT8,
signed_int16 = CL_SIGNED_INT16,
signed_int32 = CL_SIGNED_INT32,
unsigned_int8 = CL_UNSIGNED_INT8,
unsigned_int16 = CL_UNSIGNED_INT16,
unsigned_int32 = CL_UNSIGNED_INT32,
float16 = CL_HALF_FLOAT,
float32 = CL_FLOAT
};
image_format()
{
}
image_format(cl_channel_order order, cl_channel_type type)
{
m_format.image_channel_order = order;
m_format.image_channel_data_type = type;
}
explicit image_format(const cl_image_format &format)
{
m_format.image_channel_order = format.image_channel_order;
m_format.image_channel_data_type = format.image_channel_data_type;
}
image_format(const image_format &other)
: m_format(other.m_format)
{
}
image_format& operator=(const image_format &other)
{
if(this != &other){
m_format = other.m_format;
}
return *this;
}
~image_format()
{
}
const cl_image_format* get_format_ptr() const
{
return &m_format;
}
bool operator==(const image_format &other) const
{
return m_format.image_channel_order ==
other.m_format.image_channel_order &&
m_format.image_channel_data_type ==
other.m_format.image_channel_data_type;
}
bool operator!=(const image_format &other) const
{
return !(*this == other);
}
private:
cl_image_format m_format;
};
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_IMAGE_FORMAT_HPP
// deprecated, use <boost/compute/image/image_format.hpp> instead
#include <boost/compute/image/image_format.hpp>

View File

@@ -1,5 +1,5 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
// Copyright (c) 2013-2015 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
@@ -8,202 +8,5 @@
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_IMAGE_SAMPLER_HPP
#define BOOST_COMPUTE_IMAGE_SAMPLER_HPP
#include <boost/throw_exception.hpp>
#include <boost/compute/config.hpp>
#include <boost/compute/context.hpp>
#include <boost/compute/exception.hpp>
#include <boost/compute/kernel.hpp>
#include <boost/compute/type_traits/type_name.hpp>
#include <boost/compute/detail/get_object_info.hpp>
#include <boost/compute/detail/assert_cl_success.hpp>
namespace boost {
namespace compute {
class image_sampler
{
public:
enum addressing_mode {
none = CL_ADDRESS_NONE,
clamp_to_edge = CL_ADDRESS_CLAMP_TO_EDGE,
clamp = CL_ADDRESS_CLAMP,
repeat = CL_ADDRESS_REPEAT
};
enum filter_mode {
nearest = CL_FILTER_NEAREST,
linear = CL_FILTER_LINEAR
};
image_sampler()
: m_sampler(0)
{
}
image_sampler(const context &context,
bool normalized_coords,
cl_addressing_mode addressing_mode,
cl_filter_mode filter_mode)
{
cl_int error = 0;
#ifdef CL_VERSION_2_0
std::vector<cl_sampler_properties> sampler_properties;
sampler_properties.push_back(CL_SAMPLER_NORMALIZED_COORDS);
sampler_properties.push_back(cl_sampler_properties(normalized_coords));
sampler_properties.push_back(CL_SAMPLER_ADDRESSING_MODE);
sampler_properties.push_back(cl_sampler_properties(addressing_mode));
sampler_properties.push_back(CL_SAMPLER_FILTER_MODE);
sampler_properties.push_back(cl_sampler_properties(filter_mode));
sampler_properties.push_back(cl_sampler_properties(0));
m_sampler = clCreateSamplerWithProperties(
context, &sampler_properties[0], &error
);
#else
m_sampler = clCreateSampler(
context, normalized_coords, addressing_mode, filter_mode, &error
);
#endif
if(!m_sampler){
BOOST_THROW_EXCEPTION(opencl_error(error));
}
}
explicit image_sampler(cl_sampler sampler, bool retain = true)
: m_sampler(sampler)
{
if(m_sampler && retain){
clRetainSampler(m_sampler);
}
}
image_sampler(const image_sampler &other)
: m_sampler(other.m_sampler)
{
if(m_sampler){
clRetainSampler(m_sampler);
}
}
image_sampler& operator=(const image_sampler &other)
{
if(this != &other){
if(m_sampler){
clReleaseSampler(m_sampler);
}
m_sampler = other.m_sampler;
if(m_sampler){
clRetainSampler(m_sampler);
}
}
return *this;
}
#ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
image_sampler(image_sampler&& other) BOOST_NOEXCEPT
: m_sampler(other.m_sampler)
{
other.m_sampler = 0;
}
image_sampler& operator=(image_sampler&& other) BOOST_NOEXCEPT
{
if(m_sampler){
clReleaseSampler(m_sampler);
}
m_sampler = other.m_sampler;
other.m_sampler = 0;
return *this;
}
#endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
~image_sampler()
{
if(m_sampler){
BOOST_COMPUTE_ASSERT_CL_SUCCESS(
clReleaseSampler(m_sampler)
);
}
}
cl_sampler& get() const
{
return const_cast<cl_sampler &>(m_sampler);
}
context get_context() const
{
return context(get_info<cl_context>(CL_SAMPLER_CONTEXT));
}
template<class T>
T get_info(cl_sampler_info info) const
{
return detail::get_object_info<T>(clGetSamplerInfo, m_sampler, info);
}
/// \overload
template<int Enum>
typename detail::get_object_info_type<image_sampler, Enum>::type
get_info() const;
/// Returns \c true if the sampler is the same at \p other.
bool operator==(const image_sampler &other) const
{
return m_sampler == other.m_sampler;
}
/// Returns \c true if the sampler is different from \p other.
bool operator!=(const image_sampler &other) const
{
return m_sampler != other.m_sampler;
}
operator cl_sampler() const
{
return m_sampler;
}
private:
cl_sampler m_sampler;
};
/// \internal_ define get_info() specializations for image_sampler
BOOST_COMPUTE_DETAIL_DEFINE_GET_INFO_SPECIALIZATIONS(image_sampler,
((cl_uint, CL_SAMPLER_REFERENCE_COUNT))
((cl_context, CL_SAMPLER_CONTEXT))
((cl_addressing_mode, CL_SAMPLER_ADDRESSING_MODE))
((cl_filter_mode, CL_SAMPLER_FILTER_MODE))
((bool, CL_SAMPLER_NORMALIZED_COORDS))
)
namespace detail {
// set_kernel_arg specialization for image samplers
template<>
struct set_kernel_arg<image_sampler>
{
void operator()(kernel &kernel_, size_t index, const image_sampler &sampler)
{
kernel_.set_arg(index, sampler.get());
}
};
} // end detail namespace
} // end compute namespace
} // end boost namespace
BOOST_COMPUTE_TYPE_NAME(boost::compute::image_sampler, sampler_t)
#endif // BOOST_COMPUTE_IMAGE_SAMPLER_HPP
// deprecated, use <boost/compute/image/image_sampler.hpp> instead
#include <boost/compute/image/image_sampler.hpp>

View File

@@ -13,8 +13,12 @@
#include <opencv2/core/core.hpp>
#include <boost/compute/image_format.hpp>
#include <boost/throw_exception.hpp>
#include <boost/compute/algorithm/copy_n.hpp>
#include <boost/compute/exception/opencl_error.hpp>
#include <boost/compute/image/image2d.hpp>
#include <boost/compute/image/image_format.hpp>
#include <boost/compute/iterator/buffer_iterator.hpp>
namespace boost {
@@ -52,10 +56,7 @@ inline void opencv_copy_mat_to_image(const cv::Mat &mat,
BOOST_ASSERT(mat.isContinuous());
BOOST_ASSERT(image.get_context() == queue.get_context());
size_t origin[2] = { 0, 0 };
size_t region[2] = { image.width(), image.height() };
queue.enqueue_write_image(image, origin, region, 0, mat.data);
queue.enqueue_write_image(image, image.origin(), image.size(), mat.data);
}
inline void opencv_copy_image_to_mat(const image2d &image,
@@ -65,10 +66,7 @@ inline void opencv_copy_image_to_mat(const image2d &image,
BOOST_ASSERT(mat.isContinuous());
BOOST_ASSERT(image.get_context() == queue.get_context());
size_t origin[2] = { 0, 0 };
size_t region[2] = { image.width(), image.height() };
queue.enqueue_read_image(image, origin, region, 0, mat.data);
queue.enqueue_read_image(image, image.origin(), image.size(), mat.data);
}
inline image_format opencv_get_mat_image_format(const cv::Mat &mat)
@@ -84,9 +82,9 @@ inline image_format opencv_get_mat_image_format(const cv::Mat &mat)
return image_format(CL_RGBA, CL_FLOAT);
case CV_8UC1:
return image_format(CL_INTENSITY, CL_UNORM_INT8);
default:
return image_format();
}
BOOST_THROW_EXCEPTION(opencl_error(CL_IMAGE_FORMAT_NOT_SUPPORTED));
}
inline cv::Mat opencv_create_mat_with_image2d(const image2d &image,
@@ -130,9 +128,7 @@ inline image2d opencv_create_image2d_with_mat(const cv::Mat &mat,
const context &context = queue.get_context();
const image_format format = opencv_get_mat_image_format(mat);
image2d image(
context, flags, format, mat.cols, mat.rows
);
image2d image(context, mat.cols, mat.rows, format, flags);
opencv_copy_mat_to_image(mat, image, queue);

View File

@@ -13,7 +13,6 @@
#include <opencv2/highgui/highgui.hpp>
#include <boost/compute/image2d.hpp>
#include <boost/compute/interop/opencv/core.hpp>
namespace boost {

View File

@@ -97,13 +97,7 @@ namespace detail {
// set_kernel_arg specialization for opengl_buffer
template<>
struct set_kernel_arg<opengl_buffer>
{
void operator()(kernel &kernel_, size_t index, const opengl_buffer &buffer_)
{
kernel_.set_arg(index, buffer_.get());
}
};
struct set_kernel_arg<opengl_buffer> : set_kernel_arg<memory_object> { };
} // end detail namespace
} // end compute namespace

View File

@@ -11,9 +11,10 @@
#ifndef BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_RENDERBUFFER_HPP
#define BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_RENDERBUFFER_HPP
#include <boost/compute/memory_object.hpp>
#include <boost/compute/image/image_object.hpp>
#include <boost/compute/interop/opengl/gl.hpp>
#include <boost/compute/interop/opengl/cl_gl.hpp>
#include <boost/compute/utility/extents.hpp>
namespace boost {
namespace compute {
@@ -21,18 +22,18 @@ namespace compute {
/// \class opengl_renderbuffer
///
/// A OpenCL buffer for accessing an OpenGL renderbuffer object.
class opengl_renderbuffer : public memory_object
class opengl_renderbuffer : public image_object
{
public:
/// Creates a null OpenGL renderbuffer object.
opengl_renderbuffer()
: memory_object()
: image_object()
{
}
/// Creates a new OpenGL renderbuffer object for \p mem.
explicit opengl_renderbuffer(cl_mem mem, bool retain = true)
: memory_object(mem, retain)
: image_object(mem, retain)
{
}
@@ -57,7 +58,7 @@ public:
/// Creates a new OpenGL renderbuffer object as a copy of \p other.
opengl_renderbuffer(const opengl_renderbuffer &other)
: memory_object(other)
: image_object(other)
{
}
@@ -65,7 +66,7 @@ public:
opengl_renderbuffer& operator=(const opengl_renderbuffer &other)
{
if(this != &other){
memory_object::operator=(other);
image_object::operator=(other);
}
return *this;
@@ -76,6 +77,21 @@ public:
{
}
/// Returns the size (width, height) of the renderbuffer.
extents<2> size() const
{
extents<2> size;
size[0] = get_image_info<size_t>(CL_IMAGE_WIDTH);
size[1] = get_image_info<size_t>(CL_IMAGE_HEIGHT);
return size;
}
/// Returns the origin of the renderbuffer (\c 0, \c 0).
extents<2> origin() const
{
return extents<2>();
}
/// Returns the OpenGL memory object ID.
///
/// \see_opencl_ref{clGetGLObjectInfo}
@@ -99,15 +115,9 @@ public:
namespace detail {
// set_kernel_arg specialization for opengl_renderbuffer
// set_kernel_arg() specialization for opengl_renderbuffer
template<>
struct set_kernel_arg<opengl_renderbuffer>
{
void operator()(kernel &kernel_, size_t index, const opengl_renderbuffer &buffer_)
{
kernel_.set_arg(index, buffer_.get());
}
};
struct set_kernel_arg<opengl_renderbuffer> : public set_kernel_arg<image_object> { };
} // end detail namespace
} // end compute namespace

View File

@@ -11,10 +11,11 @@
#ifndef BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_HPP
#define BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_HPP
#include <boost/compute/memory_object.hpp>
#include <boost/compute/image/image_object.hpp>
#include <boost/compute/interop/opengl/gl.hpp>
#include <boost/compute/interop/opengl/cl_gl.hpp>
#include <boost/compute/detail/get_object_info.hpp>
#include <boost/compute/utility/extents.hpp>
namespace boost {
namespace compute {
@@ -22,18 +23,18 @@ namespace compute {
/// \class opengl_texture
///
/// A OpenCL image2d for accessing an OpenGL texture object.
class opengl_texture : public memory_object
class opengl_texture : public image_object
{
public:
/// Creates a null OpenGL texture object.
opengl_texture()
: memory_object()
: image_object()
{
}
/// Creates a new OpenGL texture object for \p mem.
explicit opengl_texture(cl_mem mem, bool retain = true)
: memory_object(mem, retain)
: image_object(mem, retain)
{
}
@@ -72,7 +73,7 @@ public:
/// Creates a new OpenGL texture object as a copy of \p other.
opengl_texture(const opengl_texture &other)
: memory_object(other)
: image_object(other)
{
}
@@ -80,7 +81,7 @@ public:
opengl_texture& operator=(const opengl_texture &other)
{
if(this != &other){
memory_object::operator=(other);
image_object::operator=(other);
}
return *this;
@@ -91,6 +92,21 @@ public:
{
}
/// Returns the size (width, height) of the texture.
extents<2> size() const
{
extents<2> size;
size[0] = get_image_info<size_t>(CL_IMAGE_WIDTH);
size[1] = get_image_info<size_t>(CL_IMAGE_HEIGHT);
return size;
}
/// Returns the origin of the texture (\c 0, \c 0).
extents<2> origin() const
{
return extents<2>();
}
/// Returns information about the texture.
///
/// \see_opencl_ref{clGetGLTextureInfo}
@@ -103,15 +119,9 @@ public:
namespace detail {
// set_kernel_arg specialization for opengl_texture
// set_kernel_arg() specialization for opengl_texture
template<>
struct set_kernel_arg<opengl_texture>
{
void operator()(kernel &kernel_, size_t index, const opengl_texture &texture)
{
kernel_.set_arg(index, texture.get());
}
};
struct set_kernel_arg<opengl_texture> : public set_kernel_arg<image_object> { };
} // end detail namespace
} // end compute namespace

View File

@@ -11,9 +11,13 @@
#ifndef BOOST_COMPUTE_INTEROP_QT_QIMAGE_HPP
#define BOOST_COMPUTE_INTEROP_QT_QIMAGE_HPP
#include <boost/compute/image2d.hpp>
#include <boost/compute/image_format.hpp>
#include <boost/throw_exception.hpp>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/exception/opencl_error.hpp>
#include <boost/compute/image/image2d.hpp>
#include <boost/compute/image/image_format.hpp>
#include <boost/compute/utility/dim.hpp>
#include <QImage>
@@ -26,7 +30,7 @@ inline image_format qt_qimage_format_to_image_format(const QImage::Format &forma
return image_format(image_format::bgra, image_format::unorm_int8);
}
return image_format();
BOOST_THROW_EXCEPTION(opencl_error(CL_IMAGE_FORMAT_NOT_SUPPORTED));
}
inline QImage::Format qt_image_format_to_qimage_format(const image_format &format)
@@ -47,21 +51,16 @@ inline void qt_copy_qimage_to_image2d(const QImage &qimage,
image2d &image,
command_queue &queue)
{
size_t origin[] = { 0, 0 };
size_t region[] = { image.width(), image.height() };
queue.enqueue_write_image(image, origin, region, 0, qimage.constBits());
queue.enqueue_write_image(image, image.origin(), image.size(), qimage.constBits());
}
inline void qt_copy_image2d_to_qimage(const image2d &image,
QImage &qimage,
command_queue &queue)
{
size_t origin[] = { 0, 0 };
size_t region[] = { static_cast<size_t>(qimage.width()),
static_cast<size_t>(qimage.height()) };
queue.enqueue_read_image(image, origin, region, 0, qimage.bits());
queue.enqueue_read_image(
image, dim(0, 0), dim(qimage.width(), qimage.height()), qimage.bits()
);
}
} // end compute namespace

View File

@@ -1,205 +0,0 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
//
// Distributed under the Boost Software License, Version 1.0
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_ITERATOR_DETAIL_PIXEL_INPUT_ITERATOR_HPP
#define BOOST_COMPUTE_ITERATOR_DETAIL_PIXEL_INPUT_ITERATOR_HPP
#include <cstddef>
#include <iterator>
#include <boost/config.hpp>
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/compute/image2d.hpp>
#include <boost/compute/detail/meta_kernel.hpp>
#include <boost/compute/image_sampler.hpp>
#include <boost/compute/type_traits/is_device_iterator.hpp>
#include <boost/compute/type_traits/make_vector_type.hpp>
namespace boost {
namespace compute {
namespace detail {
// forward declaration for pixel_input_iterator<T>
template<class T> class pixel_input_iterator;
// helper class which defines the iterator_facade super-class
// type for pixel_input_iterator<T>
template<class T>
class pixel_input_iterator_base
{
public:
typedef ::boost::iterator_facade<
pixel_input_iterator<T>,
typename ::boost::compute::make_vector_type<T, 4>::type,
::std::random_access_iterator_tag
> type;
};
template<class T, class IndexExpr>
struct pixel_input_iterator_index_expr
{
typedef typename make_vector_type<T, 4>::type result_type;
pixel_input_iterator_index_expr(const image2d &image, const IndexExpr &expr)
: m_image(image),
m_expr(expr)
{
}
const image2d &m_image;
IndexExpr m_expr;
};
template<class T, class IndexExpr>
inline meta_kernel& operator<<(meta_kernel &kernel,
const pixel_input_iterator_index_expr<T, IndexExpr> &expr)
{
std::string image =
kernel.get_image_identifier("__read_only", expr.m_image);
std::string sampler =
kernel.get_sampler_identifier(false,
image_sampler::none,
image_sampler::nearest);
std::string read_suffix;
if(boost::is_floating_point<T>::value){
read_suffix = "f";
}
else if(boost::is_unsigned<T>::value){
read_suffix = "ui";
}
else {
read_suffix = "i";
}
return kernel << "read_image" << read_suffix << "("
<< image << ","
<< sampler << ","
<< "(int2)("
<< expr.m_expr << " % get_image_width(" << image << "), "
<< expr.m_expr << " / get_image_width(" << image << ")))";
}
template<class T>
class pixel_input_iterator : public pixel_input_iterator_base<T>::type
{
public:
typedef typename pixel_input_iterator_base<T>::type super_type;
typedef typename super_type::reference reference;
typedef typename super_type::difference_type difference_type;
pixel_input_iterator()
: m_image(0),
m_index(0)
{
}
pixel_input_iterator(const image2d &image, size_t index)
: m_image(&image),
m_index(index)
{
}
pixel_input_iterator(const pixel_input_iterator<T> &other)
: m_image(other.m_image),
m_index(other.m_index)
{
}
pixel_input_iterator<T>& operator=(const pixel_input_iterator<T> &other)
{
if(this != &other){
m_image = other.m_image;
m_index = other.m_index;
}
return *this;
}
~pixel_input_iterator()
{
}
const image2d& get_image() const
{
return *m_image;
}
size_t get_index() const
{
return m_index;
}
template<class Expr>
pixel_input_iterator_index_expr<T, Expr>
operator[](const Expr &expr) const
{
return pixel_input_iterator_index_expr<T, Expr>(*m_image, expr);
}
private:
friend class ::boost::iterator_core_access;
reference dereference() const
{
return T(0);
}
bool equal(const pixel_input_iterator<T> &other) const
{
return m_image == other.m_image && m_index == other.m_index;
}
void increment()
{
m_index++;
}
void decrement()
{
m_index--;
}
void advance(difference_type n)
{
m_index = static_cast<size_t>(static_cast<difference_type>(m_index) + n);
}
difference_type distance_to(const pixel_input_iterator<T> &other) const
{
return static_cast<difference_type>(other.m_index - m_index);
}
private:
const image2d *m_image;
size_t m_index;
};
template<class T>
inline pixel_input_iterator<T>
make_pixel_input_iterator(const image2d &image, size_t index = 0)
{
return pixel_input_iterator<T>(image, index);
}
} // end detail namespace
// is_device_iterator specialization for pixel_input_iterator
template<class T>
struct is_device_iterator<detail::pixel_input_iterator<T> > : boost::true_type {};
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_ITERATOR_DETAIL_PIXEL_INPUT_ITERATOR_HPP