2
0
mirror of https://github.com/boostorg/compute.git synced 2026-02-01 08:22:17 +00:00

Add interop support

This adds interoperability support between Boost.Compute and various
other C/C++ libraries (Eigen, OpenCV, OpenGL, Qt and VTK). This eases
development for users using external libraries with Boost.Compute.
This commit is contained in:
Kyle Lutz
2014-01-06 23:35:38 -08:00
parent b47e74df6f
commit aad03486d9
38 changed files with 2143 additions and 83 deletions

View File

@@ -40,8 +40,4 @@
#include <boost/compute/cl_ext.hpp>
#endif
#ifdef BOOST_COMPUTE_HAVE_GL
#include <boost/compute/cl_gl.hpp>
#endif
#endif // BOOST_COMPUTE_HPP

View File

@@ -19,10 +19,6 @@
#include <boost/compute/memory_object.hpp>
#include <boost/compute/detail/get_object_info.hpp>
#ifdef BOOST_COMPUTE_HAVE_GL
#include <boost/compute/cl_gl.hpp>
#endif
namespace boost {
namespace compute {
@@ -123,23 +119,6 @@ public:
return get_memory_info<T>(info);
}
#ifdef BOOST_COMPUTE_HAVE_GL
static buffer from_gl_buffer(const context &context,
GLuint bufobj,
cl_mem_flags flags = read_write)
{
cl_int error = 0;
cl_mem mem = clCreateFromGLBuffer(context, flags, bufobj, &error);
if(!mem){
BOOST_THROW_EXCEPTION(runtime_exception(error));
}
buffer buf(mem);
clReleaseMemObject(mem);
return buf;
}
#endif // BOOST_COMPUTE_HAVE_GL
private:
BOOST_COPYABLE_AND_MOVABLE(buffer)
};

View File

@@ -28,10 +28,6 @@
#include <boost/compute/detail/get_object_info.hpp>
#include <boost/compute/detail/assert_cl_success.hpp>
#ifdef BOOST_COMPUTE_HAVE_GL
#include <boost/compute/cl_gl.hpp>
#endif
namespace boost {
namespace compute {
@@ -1117,54 +1113,6 @@ public:
return m_queue;
}
#ifdef BOOST_COMPUTE_HAVE_GL
void enqueue_acquire_gl_objects(size_t num_objects,
const cl_mem *mem_objects)
{
BOOST_ASSERT(m_queue != 0);
cl_int ret = clEnqueueAcquireGLObjects(m_queue,
num_objects,
mem_objects,
0,
0,
0);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(runtime_exception(ret));
}
}
void enqueue_acquire_gl_buffer(const buffer &buffer)
{
BOOST_ASSERT(buffer.get_context() == this->get_context());
enqueue_acquire_gl_objects(1, &buffer.get());
}
void enqueue_release_gl_objects(size_t num_objects,
const cl_mem *mem_objects)
{
BOOST_ASSERT(m_queue != 0);
cl_int ret = clEnqueueReleaseGLObjects(m_queue,
num_objects,
mem_objects,
0,
0,
0);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(runtime_exception(ret));
}
}
void enqueue_release_gl_buffer(const buffer &buffer)
{
BOOST_ASSERT(buffer.get_context() == this->get_context());
enqueue_release_gl_objects(1, &buffer.get());
}
#endif // BOOST_COMPUTE_HAVE_GL
private:
BOOST_COPYABLE_AND_MOVABLE(command_queue)

View File

@@ -0,0 +1,16 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_EIGEN_HPP
#define BOOST_COMPUTE_INTEROP_EIGEN_HPP
#include <boost/compute/interop/eigen/core.hpp>
#endif // BOOST_COMPUTE_INTEROP_EIGEN_HPP

View File

@@ -0,0 +1,72 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_EIGEN_EIGEN_HPP
#define BOOST_COMPUTE_INTEROP_EIGEN_EIGEN_HPP
#include <Eigen/Core>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/algorithm/copy_n.hpp>
#include <boost/compute/iterator/buffer_iterator.hpp>
#include <boost/compute/type_traits/type_name.hpp>
namespace boost {
namespace compute {
/// Copies \p matrix to \p buffer.
template<class Derived>
inline void eigen_copy_matrix_to_buffer(const Eigen::PlainObjectBase<Derived> &matrix,
buffer_iterator<typename Derived::Scalar> buffer,
command_queue &queue = system::default_queue())
{
::boost::compute::copy_n(matrix.data(), matrix.size(), buffer, queue);
}
/// Copies \p buffer to \p matrix.
template<class Derived>
inline void eigen_copy_buffer_to_matrix(const buffer_iterator<typename Derived::Scalar> buffer,
Eigen::PlainObjectBase<Derived> &matrix,
command_queue &queue = system::default_queue())
{
::boost::compute::copy_n(buffer, matrix.size(), matrix.data(), queue);
}
/// Converts an \c Eigen::Matrix4f to a \c float16_.
inline float16_ eigen_matrix4f_to_float16(const Eigen::Matrix4f &matrix)
{
float16_ result;
std::memcpy(&result, matrix.data(), 16 * sizeof(float));
return result;
}
/// Converts an \c Eigen::Matrix4d to a \c double16_.
inline double16_ eigen_matrix4d_to_double16(const Eigen::Matrix4d &matrix)
{
double16_ result;
std::memcpy(&result, matrix.data(), 16 * sizeof(double));
return result;
}
} // end compute namespace
} // end boost namespace
BOOST_COMPUTE_TYPE_NAME(Eigen::Vector2i, int2);
BOOST_COMPUTE_TYPE_NAME(Eigen::Vector4i, int4);
BOOST_COMPUTE_TYPE_NAME(Eigen::Vector2f, float2);
BOOST_COMPUTE_TYPE_NAME(Eigen::Vector4f, float4);
BOOST_COMPUTE_TYPE_NAME(Eigen::Matrix2f, float8);
BOOST_COMPUTE_TYPE_NAME(Eigen::Matrix4f, float16);
BOOST_COMPUTE_TYPE_NAME(Eigen::Vector2d, double2);
BOOST_COMPUTE_TYPE_NAME(Eigen::Vector4d, double4);
BOOST_COMPUTE_TYPE_NAME(Eigen::Matrix2d, double8);
BOOST_COMPUTE_TYPE_NAME(Eigen::Matrix4d, double16);
#endif // BOOST_COMPUTE_INTEROP_EIGEN_EIGEN_HPP

View File

@@ -0,0 +1,17 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_OPENCV_HPP
#define BOOST_COMPUTE_INTEROP_OPENCV_HPP
#include <boost/compute/interop/opencv/core.hpp>
#include <boost/compute/interop/opencv/highgui.hpp>
#endif // BOOST_COMPUTE_INTEROP_OPENCV_HPP

View File

@@ -0,0 +1,119 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_OPENCV_CORE_HPP
#define BOOST_COMPUTE_INTEROP_OPENCV_CORE_HPP
#include <opencv2/core/core.hpp>
#include <boost/compute/image_format.hpp>
#include <boost/compute/algorithm/copy_n.hpp>
#include <boost/compute/iterator/buffer_iterator.hpp>
namespace boost {
namespace compute {
template<class T>
inline void opencv_copy_mat_to_buffer(const cv::Mat &mat,
buffer_iterator<T> buffer,
command_queue &queue = system::default_queue())
{
BOOST_ASSERT(mat.isContinuous());
::boost::compute::copy_n(
reinterpret_cast<T *>(mat.data), mat.rows * mat.cols, buffer, queue
);
}
template<class T>
inline void opencv_copy_buffer_to_mat(const buffer_iterator<T> buffer,
cv::Mat &mat,
command_queue &queue = system::default_queue())
{
BOOST_ASSERT(mat.isContinuous());
::boost::compute::copy_n(
buffer, mat.cols * mat.rows, reinterpret_cast<T *>(mat.data), queue
);
}
inline void opencv_copy_mat_to_image(const cv::Mat &mat,
image2d &image,
command_queue &queue = system::default_queue())
{
BOOST_ASSERT(mat.data != 0);
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);
}
inline void opencv_copy_image_to_mat(const image2d &image,
cv::Mat &mat,
command_queue &queue = system::default_queue())
{
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);
}
inline image_format opencv_get_mat_image_format(const cv::Mat &mat)
{
switch(mat.type()){
case CV_8UC4:
return image_format(CL_BGRA, CL_UNORM_INT8);
case CV_16UC4:
return image_format(CL_BGRA, CL_UNORM_INT16);
case CV_32F:
return image_format(CL_INTENSITY, CL_FLOAT);
default:
return image_format();
}
}
inline cv::Mat opencv_create_mat_with_image2d(const image2d &image,
command_queue &queue = system::default_queue())
{
BOOST_ASSERT(image.get_context() == queue.get_context());
cv::Mat mat(image.height(), image.width(), CV_8UC4);
opencv_copy_image_to_mat(image, mat, queue);
return mat;
}
inline image2d opencv_create_image2d_with_mat(const cv::Mat &mat,
cl_mem_flags flags,
command_queue &queue = system::default_queue())
{
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
);
opencv_copy_mat_to_image(mat, image, queue);
return image;
}
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_INTEROP_OPENCV_CORE_HPP

View File

@@ -0,0 +1,34 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_OPENCV_HIGHGUI_HPP
#define BOOST_COMPUTE_INTEROP_OPENCV_HIGHGUI_HPP
#include <opencv2/highgui/highgui.hpp>
#include <boost/compute/image2d.hpp>
#include <boost/compute/interop/opencv/core.hpp>
namespace boost {
namespace compute {
inline void opencv_imshow(const std::string &winname,
const image2d &image,
command_queue &queue = system::default_queue())
{
const cv::Mat mat = opencv_create_mat_with_image2d(image, queue);
cv::imshow(winname, mat);
}
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_INTEROP_OPENCV_HIGHGUI_HPP

View File

@@ -0,0 +1,18 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_OPENGL_HPP
#define BOOST_COMPUTE_INTEROP_OPENGL_HPP
#include <boost/compute/interop/opengl/acquire.hpp>
#include <boost/compute/interop/opengl/opengl_buffer.hpp>
#include <boost/compute/interop/opengl/opengl_texture.hpp>
#endif // BOOST_COMPUTE_INTEROP_OPENGL_HPP

View File

@@ -0,0 +1,76 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_OPENGL_ACQUIRE_HPP
#define BOOST_COMPUTE_INTEROP_OPENGL_ACQUIRE_HPP
#include <boost/compute/command_queue.hpp>
#include <boost/compute/interop/opengl/cl_gl.hpp>
#include <boost/compute/interop/opengl/opengl_buffer.hpp>
namespace boost {
namespace compute {
/// Enqueues a command to acquire the specified OpenGL memory objects.
///
/// \see_opencl_ref{clEnqueueAcquireGLObjects}
inline void opengl_enqueue_acquire_gl_objects(size_t num_objects,
const cl_mem *mem_objects,
command_queue &queue)
{
cl_int ret = clEnqueueAcquireGLObjects(queue.get(),
num_objects,
mem_objects,
0,
0,
0);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(runtime_exception(ret));
}
}
/// Enqueues a command to release the specified OpenGL memory objects.
///
/// \see_opencl_ref{clEnqueueReleaseGLObjects}
void opengl_enqueue_release_gl_objects(size_t num_objects,
const cl_mem *mem_objects,
command_queue &queue)
{
cl_int ret = clEnqueueReleaseGLObjects(queue.get(),
num_objects,
mem_objects,
0,
0,
0);
if(ret != CL_SUCCESS){
BOOST_THROW_EXCEPTION(runtime_exception(ret));
}
}
inline void opengl_enqueue_acquire_buffer(const opengl_buffer &buffer,
command_queue &queue)
{
BOOST_ASSERT(buffer.get_context() == queue.get_context());
opengl_enqueue_acquire_gl_objects(1, &buffer.get(), queue);
}
inline void opengl_enqueue_release_buffer(const opengl_buffer &buffer,
command_queue &queue)
{
BOOST_ASSERT(buffer.get_context() == queue.get_context());
opengl_enqueue_release_gl_objects(1, &buffer.get(), queue);
}
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_INTEROP_OPENGL_ACQUIRE_HPP

View File

@@ -1,5 +1,5 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
// Copyright (c) 2013-2014 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,15 +8,13 @@
// See http://kylelutz.github.com/compute for more information.
//---------------------------------------------------------------------------//
#ifndef BOOST_COMPUTE_CL_GL_HPP
#define BOOST_COMPUTE_CL_GL_HPP
#ifndef BOOST_COMPUTE_INTEROP_OPENGL_CL_GL_HPP
#define BOOST_COMPUTE_INTEROP_OPENGL_CL_GL_HPP
#if defined(__APPLE__)
#include <OpenGL/gl.h>
#include <OpenCL/cl_gl.h>
#else
#include <GL/gl.h>
#include <CL/cl_gl.h>
#endif
#endif // BOOST_COMPUTE_CL_GL_HPP
#endif // BOOST_COMPUTE_INTEROP_OPENGL_CL_GL_HPP

View File

@@ -0,0 +1,20 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_OPENGL_GL_HPP
#define BOOST_COMPUTE_INTEROP_OPENGL_GL_HPP
#if defined(__APPLE__)
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
#endif // BOOST_COMPUTE_INTEROP_OPENGL_GL_HPP

View File

@@ -0,0 +1,93 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_OPENGL_OPENGL_BUFFER_HPP
#define BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_BUFFER_HPP
#include <boost/compute/buffer.hpp>
#include <boost/compute/interop/opengl/gl.hpp>
#include <boost/compute/interop/opengl/cl_gl.hpp>
namespace boost {
namespace compute {
/// \class opengl_buffer
///
/// A OpenCL buffer for accessing an OpenGL memory object.
class opengl_buffer : public buffer
{
public:
/// Creates a null OpenGL buffer object.
opengl_buffer()
: buffer()
{
}
/// Creates a new OpenGL buffer object for \p mem.
explicit opengl_buffer(cl_mem mem, bool retain = true)
: buffer(mem, retain)
{
}
/// Creates a new OpenGL buffer object in \p context for \p bufobj
/// with \p flags.
opengl_buffer(const context &context,
GLuint bufobj,
cl_mem_flags flags = read_write)
{
cl_int error = 0;
m_mem = clCreateFromGLBuffer(context, flags, bufobj, &error);
if(!m_mem){
BOOST_THROW_EXCEPTION(runtime_exception(error));
}
}
/// Creates a new OpenGL buffer object as a copy of \p other.
opengl_buffer(const opengl_buffer &other)
: buffer(other)
{
}
/// Copies the OpenGL buffer object from \p other.
opengl_buffer& operator=(const opengl_buffer &other)
{
if(this != &other){
buffer::operator=(other);
}
return *this;
}
/// Destroys the OpenGL buffer object.
~opengl_buffer()
{
}
/// Returns the OpenGL memory object ID.
GLuint get_opengl_object() const
{
GLuint object = 0;
clGetGLObjectInfo(m_mem, 0, &object);
return object;
}
/// Returns the OpenGL memory object type.
cl_gl_object_type get_opengl_type() const
{
cl_gl_object_type type;
clGetGLObjectInfo(m_mem, &type, 0);
return type;
}
};
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_BUFFER_HPP

View File

@@ -0,0 +1,92 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_OPENGL_OPENGL_TEXTURE_HPP
#define BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_HPP
#include <boost/compute/memory_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>
namespace boost {
namespace compute {
class opengl_texture : public memory_object
{
public:
opengl_texture()
: memory_object()
{
}
explicit opengl_texture(cl_mem mem, bool retain = true)
: memory_object(mem, retain)
{
}
opengl_texture(const context &context,
GLenum texture_target,
GLint miplevel,
GLuint texture,
cl_mem_flags flags = read_write)
{
cl_int error = 0;
#ifdef CL_VERSION_1_2
m_mem = clCreateFromGLTexture(context,
flags,
texture_target,
miplevel,
texture,
&error);
#else
m_mem = clCreateFromGLTexture2D(context,
flags,
texture_target,
miplevel,
texture,
&error);
#endif
if(!m_mem){
BOOST_THROW_EXCEPTION(runtime_exception(error));
}
}
opengl_texture(const opengl_texture &other)
: memory_object(other)
{
}
opengl_texture& operator=(const opengl_texture &other)
{
if(this != &other){
memory_object::operator=(other);
}
return *this;
}
~opengl_texture()
{
}
template<class T>
T get_texture_info(cl_gl_texture_info info) const
{
return detail::get_object_info<T>(clGetGLTextureInfo, m_mem, info);
}
};
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_HPP

View File

@@ -0,0 +1,17 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_QT_HPP
#define BOOST_COMPUTE_INTEROP_QT_HPP
#include <boost/compute/interop/qt/qtcore.hpp>
#include <boost/compute/interop/qt/qtgui.hpp>
#endif // BOOST_COMPUTE_INTEROP_QT_HPP

View File

@@ -0,0 +1,70 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_QT_QIMAGE_HPP
#define BOOST_COMPUTE_INTEROP_QT_QIMAGE_HPP
#include <boost/compute/image2d.hpp>
#include <boost/compute/image_format.hpp>
#include <boost/compute/command_queue.hpp>
#include <QImage>
namespace boost {
namespace compute {
inline image_format qt_qimage_format_to_image_format(const QImage::Format &format)
{
if(format == QImage::Format_RGB32){
return image_format(image_format::bgra, image_format::unorm_int8);
}
return image_format();
}
inline QImage::Format qt_image_format_to_qimage_format(const image_format &format)
{
if(format == image_format(image_format::bgra, image_format::unorm_int8)){
return QImage::Format_RGB32;
}
return QImage::Format_Invalid;
}
inline image_format qt_qimage_get_format(const QImage &image)
{
return qt_qimage_format_to_image_format(image.format());
}
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());
}
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());
}
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_INTEROP_QT_QIMAGE_HPP

View File

@@ -0,0 +1,20 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_QT_QPOINT_HPP
#define BOOST_COMPUTE_INTEROP_QT_QPOINT_HPP
#include <QPoint>
#include <boost/compute/type_traits/type_name.hpp>
BOOST_COMPUTE_TYPE_NAME(QPoint, "int2")
#endif // BOOST_COMPUTE_INTEROP_QT_QPOINT_HPP

View File

@@ -0,0 +1,20 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_QT_QPOINTF_HPP
#define BOOST_COMPUTE_INTEROP_QT_QPOINTF_HPP
#include <QPointF>
#include <boost/compute/type_traits/type_name.hpp>
BOOST_COMPUTE_TYPE_NAME(QPointF, "float2")
#endif // BOOST_COMPUTE_INTEROP_QT_QPOINTF_HPP

View File

@@ -0,0 +1,18 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_QT_QTCORE_HPP
#define BOOST_COMPUTE_INTEROP_QT_QTCORE_HPP
#include <boost/compute/interop/qt/qpoint.hpp>
#include <boost/compute/interop/qt/qpointf.hpp>
#include <boost/compute/interop/qt/qvector.hpp>
#endif // BOOST_COMPUTE_INTEROP_QT_QTCORE_HPP

View File

@@ -0,0 +1,16 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_QT_QTGUI_HPP
#define BOOST_COMPUTE_INTEROP_QT_QTGUI_HPP
#include <boost/compute/interop/qt/qimage.hpp>
#endif // BOOST_COMPUTE_INTEROP_QT_QTGUI_HPP

View File

@@ -0,0 +1,48 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_QT_QVECTOR_HPP
#define BOOST_COMPUTE_INTEROP_QT_QVECTOR_HPP
#include <boost/compute/detail/is_contiguous_iterator.hpp>
#include <QVector>
namespace boost {
namespace compute {
namespace detail {
template<class Iterator>
struct _is_contiguous_iterator<
Iterator,
typename boost::enable_if<
typename boost::is_same<
Iterator,
typename QVector<typename Iterator::value_type>::iterator
>::type
>::type
> : public boost::true_type {};
template<class Iterator>
struct _is_contiguous_iterator<
Iterator,
typename boost::enable_if<
typename boost::is_same<
Iterator,
typename QVector<typename Iterator::value_type>::const_iterator
>::type
>::type
> : public boost::true_type {};
} // end detail namespace
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_INTEROP_QT_QVECTOR_HPP

View File

@@ -0,0 +1,19 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_VTK_HPP
#define BOOST_COMPUTE_INTEROP_VTK_HPP
#include <boost/compute/interop/vtk/bounds.hpp>
#include <boost/compute/interop/vtk/data_array.hpp>
#include <boost/compute/interop/vtk/matrix4x4.hpp>
#include <boost/compute/interop/vtk/points.hpp>
#endif // BOOST_COMPUTE_INTEROP_VTK_HPP

View File

@@ -0,0 +1,59 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_VTK_BOUNDS_HPP
#define BOOST_COMPUTE_INTEROP_VTK_BOUNDS_HPP
#include <vector>
#include <iterator>
#include <boost/compute/system.hpp>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/algorithm/copy_n.hpp>
#include <boost/compute/algorithm/reduce.hpp>
#include <boost/compute/container/array.hpp>
namespace boost {
namespace compute {
/// Calculates the bounds for the points in the range [\p first, \p last) and
/// stores the result in \p bounds.
///
/// For example, this can be used to implement the GetBounds() method for a
/// vtkMapper subclass.
template<class PointIterator>
inline void vtk_compute_bounds(PointIterator first,
PointIterator last,
double bounds[6],
command_queue &queue = system::default_queue())
{
typedef typename std::iterator_traits<PointIterator>::value_type T;
const context &context = queue.get_context();
// compute min and max point
array<T, 2> extrema(context);
reduce(first, last, extrema.begin() + 0, min<T>(), queue);
reduce(first, last, extrema.begin() + 1, max<T>(), queue);
// copy results to host buffer
std::vector<T> buffer(2);
copy_n(extrema.begin(), 2, buffer.begin(), queue);
// copy to vtk-style bounds
bounds[0] = buffer[0][0]; bounds[1] = buffer[1][0];
bounds[2] = buffer[0][1]; bounds[3] = buffer[1][1];
bounds[4] = buffer[0][2]; bounds[5] = buffer[1][2];
}
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_INTEROP_VTK_BOUNDS_HPP

View File

@@ -0,0 +1,65 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_VTK_DATA_ARRAY_HPP
#define BOOST_COMPUTE_INTEROP_VTK_DATA_ARRAY_HPP
#include <vtkDataArray.h>
#include <vtkDataArrayTemplate.h>
#include <boost/compute/system.hpp>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/algorithm/copy.hpp>
#include <boost/compute/algorithm/copy_n.hpp>
#include <boost/compute/iterator/buffer_iterator.hpp>
namespace boost {
namespace compute {
/// Copies the values in \p data to \p buffer.
template<class T>
inline void vtk_copy_data_array_to_buffer(const vtkDataArray *data,
buffer_iterator<T> buffer,
command_queue &queue = system::default_queue());
/// \internal_
template<class T>
inline void vtk_copy_data_array_to_buffer(const vtkDataArrayTemplate<T> *data,
buffer_iterator<T> buffer,
command_queue &queue = system::default_queue())
{
vtkDataArrayTemplate<T> *data_ = const_cast<vtkDataArrayTemplate<T> *>(data);
const T *data_ptr = static_cast<const T *>(data_->GetVoidPointer(0));
size_t data_size = data_->GetNumberOfComponents() * data_->GetNumberOfTuples();
::boost::compute::copy_n(data_ptr, data_size, buffer, queue);
}
/// Copies the values in the range [\p first, \p last) to \p data.
template<class T>
inline void vtk_copy_buffer_to_data_array(buffer_iterator<T> first,
buffer_iterator<T> last,
vtkDataArray *data,
command_queue &queue = system::default_queue());
/// \internal_
template<class T>
inline void vtk_copy_buffer_to_data_array(buffer_iterator<T> first,
buffer_iterator<T> last,
vtkDataArrayTemplate<T> *data,
command_queue &queue = system::default_queue())
{
T *data_ptr = static_cast<T *>(data->GetVoidPointer(0));
::boost::compute::copy(first, last, data_ptr, queue);
}
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_INTEROP_VTK_DATA_ARRAY_HPP

View File

@@ -0,0 +1,46 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_VTK_MATRIX4X4_HPP
#define BOOST_COMPUTE_INTEROP_VTK_MATRIX4X4_HPP
#include <vtkMatrix4x4.h>
#include <boost/compute/types/builtin.hpp>
namespace boost {
namespace compute {
/// Converts a \c vtkMatrix4x4 to a \c float16_.
inline float16_ vtk_matrix4x4_to_float16(const vtkMatrix4x4 *matrix)
{
float16_ result;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
result[i*4+j] = matrix->GetElement(i, j);
}
}
return result;
}
/// Converts a \c vtkMatrix4x4 to a \c double16_;
inline double16_ vtk_matrix4x4_to_double16(const vtkMatrix4x4 *matrix)
{
double16_ result;
std::memcpy(&result, matrix->Element, 16 * sizeof(double));
return result;
}
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_INTEROP_VTK_MATRIX4X4_HPP

View File

@@ -0,0 +1,55 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2013-2014 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_INTEROP_VTK_POINTS_HPP
#define BOOST_COMPUTE_INTEROP_VTK_POINTS_HPP
#include <vector>
#include <vtkPoints.h>
#include <boost/compute/system.hpp>
#include <boost/compute/command_queue.hpp>
#include <boost/compute/algorithm/copy.hpp>
#include <boost/compute/iterator/buffer_iterator.hpp>
namespace boost {
namespace compute {
/// Copies \p points to \p buffer.
///
/// For example, to copy from a \c vtkPoints object to a \c vector<float4_>:
/// \code
/// vtkPoints *points = ...
/// vector<float4_> vector(points->GetNumberOfPoints(), context);
/// vtk_copy_points_to_buffer(points, vector.begin(), queue);
/// \endcode
template<class PointType>
inline void vtk_copy_points_to_buffer(const vtkPoints *points,
buffer_iterator<PointType> buffer,
command_queue &queue = system::default_queue())
{
vtkPoints *points_ = const_cast<vtkPoints *>(points);
// copy points to aligned buffer
std::vector<PointType> tmp(points_->GetNumberOfPoints());
for(vtkIdType i = 0; i < points_->GetNumberOfPoints(); i++){
double *p = points_->GetPoint(i);
tmp[i] = PointType(p[0], p[1], p[2], 1);
}
// copy data to device
copy(tmp.begin(), tmp.end(), buffer, queue);
}
} // end compute namespace
} // end boost namespace
#endif // BOOST_COMPUTE_INTEROP_VTK_POINTS_HPP