opengl_texture.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_TEXTURE_HPP
  11. #define BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_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/detail/get_object_info.hpp>
  16. #include <boost/compute/type_traits/type_name.hpp>
  17. #include <boost/compute/utility/extents.hpp>
  18. namespace boost {
  19. namespace compute {
  20. /// \class opengl_texture
  21. ///
  22. /// A OpenCL image2d for accessing an OpenGL texture object.
  23. class opengl_texture : public image_object
  24. {
  25. public:
  26. /// Creates a null OpenGL texture object.
  27. opengl_texture()
  28. : image_object()
  29. {
  30. }
  31. /// Creates a new OpenGL texture object for \p mem.
  32. explicit opengl_texture(cl_mem mem, bool retain = true)
  33. : image_object(mem, retain)
  34. {
  35. }
  36. /// Creates a new OpenGL texture object in \p context for \p texture
  37. /// with \p flags.
  38. ///
  39. /// \see_opencl_ref{clCreateFromGLTexture}
  40. opengl_texture(const context &context,
  41. GLenum texture_target,
  42. GLint miplevel,
  43. GLuint texture,
  44. cl_mem_flags flags = read_write)
  45. {
  46. cl_int error = 0;
  47. #ifdef BOOST_COMPUTE_CL_VERSION_1_2
  48. m_mem = clCreateFromGLTexture(context,
  49. flags,
  50. texture_target,
  51. miplevel,
  52. texture,
  53. &error);
  54. #else
  55. m_mem = clCreateFromGLTexture2D(context,
  56. flags,
  57. texture_target,
  58. miplevel,
  59. texture,
  60. &error);
  61. #endif
  62. if(!m_mem){
  63. BOOST_THROW_EXCEPTION(opencl_error(error));
  64. }
  65. }
  66. /// Creates a new OpenGL texture object as a copy of \p other.
  67. opengl_texture(const opengl_texture &other)
  68. : image_object(other)
  69. {
  70. }
  71. /// Copies the OpenGL texture object from \p other.
  72. opengl_texture& operator=(const opengl_texture &other)
  73. {
  74. if(this != &other){
  75. image_object::operator=(other);
  76. }
  77. return *this;
  78. }
  79. /// Destroys the texture object.
  80. ~opengl_texture()
  81. {
  82. }
  83. /// Returns the size (width, height) of the texture.
  84. extents<2> size() const
  85. {
  86. extents<2> size;
  87. size[0] = get_image_info<size_t>(CL_IMAGE_WIDTH);
  88. size[1] = get_image_info<size_t>(CL_IMAGE_HEIGHT);
  89. return size;
  90. }
  91. /// Returns the origin of the texture (\c 0, \c 0).
  92. extents<2> origin() const
  93. {
  94. return extents<2>();
  95. }
  96. /// Returns information about the texture.
  97. ///
  98. /// \see_opencl_ref{clGetGLTextureInfo}
  99. template<class T>
  100. T get_texture_info(cl_gl_texture_info info) const
  101. {
  102. return detail::get_object_info<T>(clGetGLTextureInfo, m_mem, info);
  103. }
  104. };
  105. namespace detail {
  106. // set_kernel_arg() specialization for opengl_texture
  107. template<>
  108. struct set_kernel_arg<opengl_texture> : public set_kernel_arg<image_object> { };
  109. } // end detail namespace
  110. } // end compute namespace
  111. } // end boost namespace
  112. BOOST_COMPUTE_TYPE_NAME(boost::compute::opengl_texture, image2d_t)
  113. #endif // BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_HPP