1 #ifndef _GLUOBJECTWRAPPER_HPP 2 #define _GLUOBJECTWRAPPER_HPP 3 /*------------------------------------------------------------------------- 4 * drawElements Quality Program OpenGL ES Utilities 5 * ------------------------------------------------ 6 * 7 * Copyright 2014 The Android Open Source Project 8 * 9 * Licensed under the Apache License, Version 2.0 (the "License"); 10 * you may not use this file except in compliance with the License. 11 * You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 * 21 *//*! 22 * \file 23 * \brief API object wrapper. 24 *//*--------------------------------------------------------------------*/ 25 26 #include "gluDefs.hpp" 27 #include "gluRenderContext.hpp" 28 #include "glwFunctions.hpp" 29 30 #include <vector> 31 32 namespace glu 33 { 34 35 /*--------------------------------------------------------------------*//*! 36 * \brief API object type. 37 * 38 * API object type is used to choose allocation and deallocation routines 39 * for objects. 40 *//*--------------------------------------------------------------------*/ 41 enum ObjectType 42 { 43 OBJECTTYPE_TEXTURE = 0, 44 OBJECTTYPE_BUFFER, 45 OBJECTTYPE_RENDERBUFFER, 46 OBJECTTYPE_FRAMEBUFFER, 47 OBJECTTYPE_TRANSFORM_FEEDBACK, 48 OBJECTTYPE_VERTEX_ARRAY, 49 OBJECTTYPE_QUERY, 50 OBJECTTYPE_SAMPLER, 51 52 OBJECTTYPE_LAST 53 }; 54 55 struct ObjectTraits 56 { 57 const char *name; 58 glw::glGenBuffersFunc glw::Functions::*genFunc; 59 glw::glDeleteBuffersFunc glw::Functions::*deleteFunc; 60 }; 61 62 const ObjectTraits &objectTraits(ObjectType type); 63 64 class ObjectWrapper 65 { 66 public: 67 ObjectWrapper(const glw::Functions &gl, const ObjectTraits &traits); 68 ObjectWrapper(const glw::Functions &gl, const ObjectTraits &traits, uint32_t object); 69 ~ObjectWrapper(void); 70 get(void) const71 inline uint32_t get(void) const 72 { 73 return m_object; 74 } operator *(void) const75 inline uint32_t operator*(void) const 76 { 77 return m_object; 78 } 79 80 protected: 81 const glw::Functions &m_gl; 82 const ObjectTraits &m_traits; 83 uint32_t m_object; 84 85 private: 86 ObjectWrapper(const ObjectWrapper &other); 87 ObjectWrapper &operator=(const ObjectWrapper &other); 88 } DE_WARN_UNUSED_TYPE; 89 90 /*--------------------------------------------------------------------*//*! 91 * \brief API object wrapper template. 92 *//*--------------------------------------------------------------------*/ 93 template <ObjectType Type> 94 class TypedObjectWrapper : public ObjectWrapper 95 { 96 public: TypedObjectWrapper(const glw::Functions & gl,uint32_t object)97 TypedObjectWrapper(const glw::Functions &gl, uint32_t object) : ObjectWrapper(gl, objectTraits(Type), object) 98 { 99 } TypedObjectWrapper(const RenderContext & context)100 explicit TypedObjectWrapper(const RenderContext &context) 101 : ObjectWrapper(context.getFunctions(), objectTraits(Type)) 102 { 103 } TypedObjectWrapper(const glw::Functions & gl)104 explicit TypedObjectWrapper(const glw::Functions &gl) : ObjectWrapper(gl, objectTraits(Type)) 105 { 106 } 107 } DE_WARN_UNUSED_TYPE; 108 109 /*--------------------------------------------------------------------*//*! 110 * \brief API object vector. 111 *//*--------------------------------------------------------------------*/ 112 class ObjectVector 113 { 114 public: 115 ObjectVector(const glw::Functions &gl, const ObjectTraits &traits, size_t numObjects = 0); 116 ~ObjectVector(void); 117 size(void) const118 size_t size(void) const 119 { 120 return m_objects.size(); 121 } 122 123 void resize(size_t newSize); 124 void clear(void); 125 empty(void) const126 bool empty(void) const 127 { 128 return m_objects.empty(); 129 } 130 get(size_t ndx) const131 uint32_t get(size_t ndx) const 132 { 133 return m_objects[ndx]; 134 } operator [](size_t ndx) const135 uint32_t operator[](size_t ndx) const 136 { 137 return get(ndx); 138 } 139 140 private: 141 ObjectVector(const ObjectVector &other); 142 ObjectVector &operator=(const ObjectVector &other); 143 144 const glw::Functions &m_gl; 145 const ObjectTraits &m_traits; 146 std::vector<uint32_t> m_objects; 147 } DE_WARN_UNUSED_TYPE; 148 149 template <ObjectType Type> 150 class TypedObjectVector : public ObjectVector 151 { 152 public: TypedObjectVector(const RenderContext & context,size_t numObjects=0)153 explicit TypedObjectVector(const RenderContext &context, size_t numObjects = 0) 154 : ObjectVector(context.getFunctions(), objectTraits(Type), numObjects) 155 { 156 } TypedObjectVector(const glw::Functions & gl,size_t numObjects=0)157 explicit TypedObjectVector(const glw::Functions &gl, size_t numObjects = 0) 158 : ObjectVector(gl, objectTraits(Type), numObjects) 159 { 160 } 161 }; 162 163 // Typedefs for simple wrappers without functionality. 164 165 typedef TypedObjectWrapper<OBJECTTYPE_TEXTURE> Texture; 166 typedef TypedObjectWrapper<OBJECTTYPE_BUFFER> Buffer; 167 typedef TypedObjectWrapper<OBJECTTYPE_RENDERBUFFER> Renderbuffer; 168 typedef TypedObjectWrapper<OBJECTTYPE_FRAMEBUFFER> Framebuffer; 169 typedef TypedObjectWrapper<OBJECTTYPE_TRANSFORM_FEEDBACK> TransformFeedback; 170 typedef TypedObjectWrapper<OBJECTTYPE_VERTEX_ARRAY> VertexArray; 171 typedef TypedObjectWrapper<OBJECTTYPE_QUERY> Query; 172 typedef TypedObjectWrapper<OBJECTTYPE_SAMPLER> Sampler; 173 174 typedef TypedObjectVector<OBJECTTYPE_TEXTURE> TextureVector; 175 typedef TypedObjectVector<OBJECTTYPE_BUFFER> BufferVector; 176 typedef TypedObjectVector<OBJECTTYPE_RENDERBUFFER> RenderbufferVector; 177 178 } // namespace glu 179 180 #endif // _GLUOBJECTWRAPPER_HPP 181