opengl_buffer.hpp 2.8 KB

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