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_BUFFER_HPP
12 #define BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_BUFFER_HPP
13 
14 #include <boost/compute/buffer.hpp>
15 #include <boost/compute/interop/opengl/gl.hpp>
16 #include <boost/compute/interop/opengl/cl_gl.hpp>
17 
18 namespace boost {
19 namespace compute {
20 
21 /// \class opengl_buffer
22 ///
23 /// A OpenCL buffer for accessing an OpenGL memory object.
24 class opengl_buffer : public buffer
25 {
26 public:
27     /// Creates a null OpenGL buffer object.
opengl_buffer()28     opengl_buffer()
29         : buffer()
30     {
31     }
32 
33     /// Creates a new OpenGL buffer object for \p mem.
opengl_buffer(cl_mem mem,bool retain=true)34     explicit opengl_buffer(cl_mem mem, bool retain = true)
35         : buffer(mem, retain)
36     {
37     }
38 
39     /// Creates a new OpenGL buffer object in \p context for \p bufobj
40     /// with \p flags.
41     ///
42     /// \see_opencl_ref{clCreateFromGLBuffer}
opengl_buffer(const context & context,GLuint bufobj,cl_mem_flags flags=read_write)43     opengl_buffer(const context &context,
44                   GLuint bufobj,
45                   cl_mem_flags flags = read_write)
46     {
47         cl_int error = 0;
48         m_mem = clCreateFromGLBuffer(context, flags, bufobj, &error);
49         if(!m_mem){
50             BOOST_THROW_EXCEPTION(opencl_error(error));
51         }
52     }
53 
54     /// Creates a new OpenGL buffer object as a copy of \p other.
opengl_buffer(const opengl_buffer & other)55     opengl_buffer(const opengl_buffer &other)
56         : buffer(other)
57     {
58     }
59 
60     /// Copies the OpenGL buffer object from \p other.
operator =(const opengl_buffer & other)61     opengl_buffer& operator=(const opengl_buffer &other)
62     {
63         if(this != &other){
64             buffer::operator=(other);
65         }
66 
67         return *this;
68     }
69 
70     /// Destroys the OpenGL buffer object.
~opengl_buffer()71     ~opengl_buffer()
72     {
73     }
74 
75     /// Returns the OpenGL memory object ID.
76     ///
77     /// \see_opencl_ref{clGetGLObjectInfo}
get_opengl_object() const78     GLuint get_opengl_object() const
79     {
80         GLuint object = 0;
81         clGetGLObjectInfo(m_mem, 0, &object);
82         return object;
83     }
84 
85     /// Returns the OpenGL memory object type.
86     ///
87     /// \see_opencl_ref{clGetGLObjectInfo}
get_opengl_type() const88     cl_gl_object_type get_opengl_type() const
89     {
90         cl_gl_object_type type;
91         clGetGLObjectInfo(m_mem, &type, 0);
92         return type;
93     }
94 };
95 
96 namespace detail {
97 
98 // set_kernel_arg specialization for opengl_buffer
99 template<>
100 struct set_kernel_arg<opengl_buffer> : set_kernel_arg<memory_object> { };
101 
102 } // end detail namespace
103 } // end compute namespace
104 } // end boost namespace
105 
106 #endif // BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_BUFFER_HPP
107