opengl_renderbuffer.hpp 3.6 KB

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