• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #ifndef BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_HPP
12 #define BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_HPP
13 
14 #include <boost/compute/image/image_object.hpp>
15 #include <boost/compute/interop/opengl/gl.hpp>
16 #include <boost/compute/interop/opengl/cl_gl.hpp>
17 #include <boost/compute/detail/get_object_info.hpp>
18 #include <boost/compute/type_traits/type_name.hpp>
19 #include <boost/compute/utility/extents.hpp>
20 
21 namespace boost {
22 namespace compute {
23 
24 /// \class opengl_texture
25 ///
26 /// A OpenCL image2d for accessing an OpenGL texture object.
27 class opengl_texture : public image_object
28 {
29 public:
30     /// Creates a null OpenGL texture object.
opengl_texture()31     opengl_texture()
32         : image_object()
33     {
34     }
35 
36     /// Creates a new OpenGL texture object for \p mem.
opengl_texture(cl_mem mem,bool retain=true)37     explicit opengl_texture(cl_mem mem, bool retain = true)
38         : image_object(mem, retain)
39     {
40     }
41 
42     /// Creates a new OpenGL texture object in \p context for \p texture
43     /// with \p flags.
44     ///
45     /// \see_opencl_ref{clCreateFromGLTexture}
opengl_texture(const context & context,GLenum texture_target,GLint miplevel,GLuint texture,cl_mem_flags flags=read_write)46     opengl_texture(const context &context,
47                    GLenum texture_target,
48                    GLint miplevel,
49                    GLuint texture,
50                    cl_mem_flags flags = read_write)
51     {
52         cl_int error = 0;
53 
54         #ifdef BOOST_COMPUTE_CL_VERSION_1_2
55         m_mem = clCreateFromGLTexture(context,
56                                       flags,
57                                       texture_target,
58                                       miplevel,
59                                       texture,
60                                       &error);
61         #else
62         m_mem = clCreateFromGLTexture2D(context,
63                                         flags,
64                                         texture_target,
65                                         miplevel,
66                                         texture,
67                                         &error);
68         #endif
69 
70         if(!m_mem){
71             BOOST_THROW_EXCEPTION(opencl_error(error));
72         }
73     }
74 
75     /// Creates a new OpenGL texture object as a copy of \p other.
opengl_texture(const opengl_texture & other)76     opengl_texture(const opengl_texture &other)
77         : image_object(other)
78     {
79     }
80 
81     /// Copies the OpenGL texture object from \p other.
operator =(const opengl_texture & other)82     opengl_texture& operator=(const opengl_texture &other)
83     {
84         if(this != &other){
85             image_object::operator=(other);
86         }
87 
88         return *this;
89     }
90 
91     /// Destroys the texture object.
~opengl_texture()92     ~opengl_texture()
93     {
94     }
95 
96     /// Returns the size (width, height) of the texture.
size() const97     extents<2> size() const
98     {
99         extents<2> size;
100         size[0] = get_image_info<size_t>(CL_IMAGE_WIDTH);
101         size[1] = get_image_info<size_t>(CL_IMAGE_HEIGHT);
102         return size;
103     }
104 
105     /// Returns the origin of the texture (\c 0, \c 0).
origin() const106     extents<2> origin() const
107     {
108         return extents<2>();
109     }
110 
111     /// Returns information about the texture.
112     ///
113     /// \see_opencl_ref{clGetGLTextureInfo}
114     template<class T>
get_texture_info(cl_gl_texture_info info) const115     T get_texture_info(cl_gl_texture_info info) const
116     {
117         return detail::get_object_info<T>(clGetGLTextureInfo, m_mem, info);
118     }
119 };
120 
121 namespace detail {
122 
123 // set_kernel_arg() specialization for opengl_texture
124 template<>
125 struct set_kernel_arg<opengl_texture> : public set_kernel_arg<image_object> { };
126 
127 } // end detail namespace
128 } // end compute namespace
129 } // end boost namespace
130 
131 BOOST_COMPUTE_TYPE_NAME(boost::compute::opengl_texture, image2d_t)
132 
133 #endif // BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_HPP
134