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
32namespace 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 *//*--------------------------------------------------------------------*/
41enum 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
55struct ObjectTraits
56{
57	const char*					name;
58	glw::glGenBuffersFunc		glw::Functions::*genFunc;
59	glw::glDeleteBuffersFunc	glw::Functions::*deleteFunc;
60};
61
62const ObjectTraits& objectTraits (ObjectType type);
63
64class ObjectWrapper
65{
66public:
67							ObjectWrapper		(const glw::Functions& gl, const ObjectTraits& traits);
68							ObjectWrapper		(const glw::Functions& gl, const ObjectTraits& traits, deUint32 object);
69							~ObjectWrapper		(void);
70
71	inline deUint32			get					(void) const { return m_object; }
72	inline deUint32			operator*			(void) const { return m_object; }
73
74protected:
75	const glw::Functions&	m_gl;
76	const ObjectTraits&		m_traits;
77	deUint32				m_object;
78
79private:
80							ObjectWrapper		(const ObjectWrapper& other);
81	ObjectWrapper&			operator=			(const ObjectWrapper& other);
82} DE_WARN_UNUSED_TYPE;
83
84/*--------------------------------------------------------------------*//*!
85 * \brief API object wrapper template.
86 *//*--------------------------------------------------------------------*/
87template<ObjectType Type> class TypedObjectWrapper : public ObjectWrapper
88{
89public:
90				TypedObjectWrapper (const glw::Functions& gl, deUint32 object) : ObjectWrapper(gl, objectTraits(Type), object) {}
91	explicit	TypedObjectWrapper (const RenderContext& context) : ObjectWrapper(context.getFunctions(), objectTraits(Type)) {}
92	explicit	TypedObjectWrapper (const glw::Functions& gl) : ObjectWrapper(gl, objectTraits(Type)) {}
93} DE_WARN_UNUSED_TYPE;
94
95/*--------------------------------------------------------------------*//*!
96 * \brief API object vector.
97 *//*--------------------------------------------------------------------*/
98class ObjectVector
99{
100public:
101										ObjectVector		(const glw::Functions& gl, const ObjectTraits& traits, size_t numObjects = 0);
102										~ObjectVector		(void);
103
104	size_t								size				(void) const		{ return m_objects.size();	}
105
106	void								resize				(size_t newSize);
107	void								clear				(void);
108
109	bool								empty				(void) const		{ return m_objects.empty();	}
110
111	deUint32							get					(size_t ndx) const	{ return m_objects[ndx];	}
112	deUint32							operator[]			(size_t ndx) const	{ return get(ndx);			}
113
114private:
115										ObjectVector		(const ObjectVector& other);
116	ObjectVector&						operator=			(const ObjectVector& other);
117
118	const glw::Functions&				m_gl;
119	const ObjectTraits&					m_traits;
120	std::vector<deUint32>				m_objects;
121} DE_WARN_UNUSED_TYPE;
122
123template<ObjectType Type> class TypedObjectVector : public ObjectVector
124{
125public:
126	explicit	TypedObjectVector (const RenderContext& context, size_t numObjects = 0) : ObjectVector(context.getFunctions(), objectTraits(Type), numObjects) {}
127	explicit	TypedObjectVector (const glw::Functions& gl, size_t numObjects = 0) : ObjectVector(gl, objectTraits(Type), numObjects) {}
128};
129
130// Typedefs for simple wrappers without functionality.
131
132typedef TypedObjectWrapper<OBJECTTYPE_TEXTURE>				Texture;
133typedef TypedObjectWrapper<OBJECTTYPE_BUFFER>				Buffer;
134typedef TypedObjectWrapper<OBJECTTYPE_RENDERBUFFER>			Renderbuffer;
135typedef TypedObjectWrapper<OBJECTTYPE_FRAMEBUFFER>			Framebuffer;
136typedef TypedObjectWrapper<OBJECTTYPE_TRANSFORM_FEEDBACK>	TransformFeedback;
137typedef TypedObjectWrapper<OBJECTTYPE_VERTEX_ARRAY>			VertexArray;
138typedef TypedObjectWrapper<OBJECTTYPE_QUERY>				Query;
139typedef TypedObjectWrapper<OBJECTTYPE_SAMPLER>				Sampler;
140
141typedef TypedObjectVector<OBJECTTYPE_TEXTURE>				TextureVector;
142typedef TypedObjectVector<OBJECTTYPE_BUFFER>				BufferVector;
143typedef TypedObjectVector<OBJECTTYPE_RENDERBUFFER>			RenderbufferVector;
144
145} // glu
146
147#endif // _GLUOBJECTWRAPPER_HPP
148