Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:01

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_HPP
0012 #define BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_HPP
0013 
0014 #include <boost/compute/image/image_object.hpp>
0015 #include <boost/compute/interop/opengl/gl.hpp>
0016 #include <boost/compute/interop/opengl/cl_gl.hpp>
0017 #include <boost/compute/detail/get_object_info.hpp>
0018 #include <boost/compute/type_traits/type_name.hpp>
0019 #include <boost/compute/utility/extents.hpp>
0020 
0021 namespace boost {
0022 namespace compute {
0023 
0024 /// \class opengl_texture
0025 ///
0026 /// A OpenCL image2d for accessing an OpenGL texture object.
0027 class opengl_texture : public image_object
0028 {
0029 public:
0030     /// Creates a null OpenGL texture object.
0031     opengl_texture()
0032         : image_object()
0033     {
0034     }
0035 
0036     /// Creates a new OpenGL texture object for \p mem.
0037     explicit opengl_texture(cl_mem mem, bool retain = true)
0038         : image_object(mem, retain)
0039     {
0040     }
0041 
0042     /// Creates a new OpenGL texture object in \p context for \p texture
0043     /// with \p flags.
0044     ///
0045     /// \see_opencl_ref{clCreateFromGLTexture}
0046     opengl_texture(const context &context,
0047                    GLenum texture_target,
0048                    GLint miplevel,
0049                    GLuint texture,
0050                    cl_mem_flags flags = read_write)
0051     {
0052         cl_int error = 0;
0053 
0054         #ifdef BOOST_COMPUTE_CL_VERSION_1_2
0055         m_mem = clCreateFromGLTexture(context,
0056                                       flags,
0057                                       texture_target,
0058                                       miplevel,
0059                                       texture,
0060                                       &error);
0061         #else
0062         m_mem = clCreateFromGLTexture2D(context,
0063                                         flags,
0064                                         texture_target,
0065                                         miplevel,
0066                                         texture,
0067                                         &error);
0068         #endif
0069 
0070         if(!m_mem){
0071             BOOST_THROW_EXCEPTION(opencl_error(error));
0072         }
0073     }
0074 
0075     /// Creates a new OpenGL texture object as a copy of \p other.
0076     opengl_texture(const opengl_texture &other)
0077         : image_object(other)
0078     {
0079     }
0080 
0081     /// Copies the OpenGL texture object from \p other.
0082     opengl_texture& operator=(const opengl_texture &other)
0083     {
0084         if(this != &other){
0085             image_object::operator=(other);
0086         }
0087 
0088         return *this;
0089     }
0090 
0091     /// Destroys the texture object.
0092     ~opengl_texture()
0093     {
0094     }
0095 
0096     /// Returns the size (width, height) of the texture.
0097     extents<2> size() const
0098     {
0099         extents<2> size;
0100         size[0] = get_image_info<size_t>(CL_IMAGE_WIDTH);
0101         size[1] = get_image_info<size_t>(CL_IMAGE_HEIGHT);
0102         return size;
0103     }
0104 
0105     /// Returns the origin of the texture (\c 0, \c 0).
0106     extents<2> origin() const
0107     {
0108         return extents<2>();
0109     }
0110 
0111     /// Returns information about the texture.
0112     ///
0113     /// \see_opencl_ref{clGetGLTextureInfo}
0114     template<class T>
0115     T get_texture_info(cl_gl_texture_info info) const
0116     {
0117         return detail::get_object_info<T>(clGetGLTextureInfo, m_mem, info);
0118     }
0119 };
0120 
0121 namespace detail {
0122 
0123 // set_kernel_arg() specialization for opengl_texture
0124 template<>
0125 struct set_kernel_arg<opengl_texture> : public set_kernel_arg<image_object> { };
0126 
0127 } // end detail namespace
0128 } // end compute namespace
0129 } // end boost namespace
0130 
0131 BOOST_COMPUTE_TYPE_NAME(boost::compute::opengl_texture, image2d_t)
0132 
0133 #endif // BOOST_COMPUTE_INTEROP_OPENGL_OPENGL_TEXTURE_HPP