1 #ifndef _GLUDRAWUTIL_HPP
2 #define _GLUDRAWUTIL_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements Quality Program OpenGL 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 Draw call utilities.
24  *
25  * Draw call utilities provide an abstraction for commonly used draw calls.
26  * The objective of that abstraction is to allow moving data to buffers
27  * and state to VAOs automatically if target context doesn't support
28  * user pointers or default VAOs.
29  *//*--------------------------------------------------------------------*/
30 
31 #include "gluDefs.hpp"
32 
33 #include <string>
34 
35 namespace glu
36 {
37 
38 class RenderContext;
39 
40 enum VertexComponentType
41 {
42 	// Standard types: all conversion types apply.
43 	VTX_COMP_UNSIGNED_INT8		= 0,
44 	VTX_COMP_UNSIGNED_INT16,
45 	VTX_COMP_UNSIGNED_INT32,
46 	VTX_COMP_SIGNED_INT8,
47 	VTX_COMP_SIGNED_INT16,
48 	VTX_COMP_SIGNED_INT32,
49 
50 	// Special types: only CONVERT_NONE is allowed.
51 	VTX_COMP_FIXED,
52 	VTX_COMP_HALF_FLOAT,
53 	VTX_COMP_FLOAT,
54 
55 	VTX_COMP_TYPE_LAST
56 };
57 
58 enum VertexComponentConversion
59 {
60 	VTX_COMP_CONVERT_NONE = 0,				//!< No conversion: integer types, or floating-point values.
61 	VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT,	//!< Normalize integers to range [0,1] or [-1,1] depending on type.
62 	VTX_COMP_CONVERT_CAST_TO_FLOAT,			//!< Convert to floating-point directly.
63 
64 	VTX_COMP_CONVERT_LAST
65 };
66 
67 enum IndexType
68 {
69 	INDEXTYPE_UINT8,
70 	INDEXTYPE_UINT16,
71 	INDEXTYPE_UINT32,
72 
73 	INDEXTYPE_LAST
74 };
75 
76 enum PrimitiveType
77 {
78 	PRIMITIVETYPE_TRIANGLES = 0,
79 	PRIMITIVETYPE_TRIANGLE_STRIP,
80 	PRIMITIVETYPE_TRIANGLE_FAN,
81 
82 	PRIMITIVETYPE_LINES,
83 	PRIMITIVETYPE_LINE_STRIP,
84 	PRIMITIVETYPE_LINE_LOOP,
85 
86 	PRIMITIVETYPE_POINTS,
87 
88 	PRIMITIVETYPE_PATCHES,
89 
90 	PRIMITIVETYPE_LAST
91 };
92 
93 struct BindingPoint
94 {
95 	enum Type
96 	{
97 		BPTYPE_LOCATION = 0,			//!< Binding by numeric location.
98 		BPTYPE_NAME,					//!< Binding by input name.
99 
100 		BPTYPE_LAST
101 	};
102 
103 	Type			type;			//!< Binding type (name or location).
104 	std::string		name;			//!< Input name, or empty if is not binding by name.
105 	int				location;		//!< Input location, or offset to named location if binding by name.
106 
BindingPointglu::BindingPoint107 				BindingPoint (void)											: type(BPTYPE_LAST), location (0) {}
BindingPointglu::BindingPoint108 	explicit	BindingPoint (int location_)								: type(BPTYPE_LOCATION), location(location_) {}
BindingPointglu::BindingPoint109 	explicit	BindingPoint (const std::string& name_, int location_ = 0)	: type(BPTYPE_NAME), name(name_), location(location_) {}
110 };
111 
112 struct VertexArrayPointer
113 {
114 	VertexComponentType			componentType;	//!< Component type.
115 	VertexComponentConversion	convert;		//!< Component conversion type.
116 	int							numComponents;	//!< Number of components per element.
117 	int							numElements;	//!< Number of elements in total.
118 	int							stride;			//!< Element stride.
119 
120 	const void*					data;			//!< Data pointer.
121 
VertexArrayPointerglu::VertexArrayPointer122 	VertexArrayPointer (VertexComponentType componentType_, VertexComponentConversion convert_, int numComponents_, int numElements_, int stride_, const void* data_)
123 		: componentType	(componentType_)
124 		, convert		(convert_)
125 		, numComponents	(numComponents_)
126 		, numElements	(numElements_)
127 		, stride		(stride_)
128 		, data			(data_)
129 	{
130 	}
131 
VertexArrayPointerglu::VertexArrayPointer132 	VertexArrayPointer (void)
133 		: componentType	(VTX_COMP_TYPE_LAST)
134 		, convert		(VTX_COMP_CONVERT_LAST)
135 		, numComponents	(0)
136 		, numElements	(0)
137 		, stride		(0)
138 		, data			(0)
139 	{
140 	}
141 } DE_WARN_UNUSED_TYPE;
142 
143 struct VertexArrayBinding
144 {
145 	BindingPoint		binding;
146 	VertexArrayPointer	pointer;
147 
VertexArrayBindingglu::VertexArrayBinding148 	VertexArrayBinding (const BindingPoint& binding_, const VertexArrayPointer& pointer_)
149 		: binding	(binding_)
150 		, pointer	(pointer_)
151 	{
152 	}
153 
VertexArrayBindingglu::VertexArrayBinding154 	VertexArrayBinding (void)
155 	{
156 	}
157 } DE_WARN_UNUSED_TYPE;
158 
159 struct PrimitiveList
160 {
161 	PrimitiveType		type;			//!< Primitive type.
162 	int					numElements;	//!< Number of elements to be drawn.
163 	IndexType			indexType;		//!< Index type or INDEXTYPE_LAST if not used
164 	const void*			indices;		//!< Index list or DE_NULL if not used.
165 
PrimitiveListglu::PrimitiveList166 	PrimitiveList (PrimitiveType type_, int numElements_)
167 		: type			(type_)
168 		, numElements	(numElements_)
169 		, indexType		(INDEXTYPE_LAST)
170 		, indices		(0)
171 	{
172 	}
173 
PrimitiveListglu::PrimitiveList174 	PrimitiveList (PrimitiveType type_, int numElements_, IndexType indexType_, const void* indices_)
175 		: type			(type_)
176 		, numElements	(numElements_)
177 		, indexType		(indexType_)
178 		, indices		(indices_)
179 	{
180 	}
181 
PrimitiveListglu::PrimitiveList182 	PrimitiveList (void)
183 		: type			(PRIMITIVETYPE_LAST)
184 		, numElements	(0)
185 		, indexType		(INDEXTYPE_LAST)
186 		, indices		(0)
187 	{
188 	}
189 } DE_WARN_UNUSED_TYPE;
190 
191 class DrawUtilCallback
192 {
193 public:
beforeDrawCall(void)194 	virtual void	beforeDrawCall	(void) { }
afterDrawCall(void)195 	virtual void	afterDrawCall	(void) { }
196 };
197 
198 void	draw					(const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback = DE_NULL);
199 
200 void	drawFromUserPointers	(const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback = DE_NULL);
201 void	drawFromBuffers			(const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback = DE_NULL);
202 void	drawFromVAOBuffers		(const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback = DE_NULL);
203 
204 // Shorthands for PrimitiveList
205 namespace pr
206 {
207 
208 #define DECLARE_PR_CTOR(NAME, TYPE)										\
209 inline PrimitiveList NAME (int numElements)								\
210 {																		\
211 	return PrimitiveList(TYPE, numElements);							\
212 }																		\
213 inline PrimitiveList NAME (int numElements, const deUint8* indices)		\
214 {																		\
215 	return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT8, indices);	\
216 }																		\
217 inline PrimitiveList NAME (int numElements, const deUint16* indices)	\
218 {																		\
219 	return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT16, indices);	\
220 }																		\
221 inline PrimitiveList NAME (int numElements, const deUint32* indices)	\
222 {																		\
223 	return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT32, indices);	\
224 }																		\
225 struct DeclarePRCtor##NAME##Unused_s { int unused; }
226 
227 DECLARE_PR_CTOR(Triangles,		PRIMITIVETYPE_TRIANGLES);
228 DECLARE_PR_CTOR(TriangleStrip,	PRIMITIVETYPE_TRIANGLE_STRIP);
229 DECLARE_PR_CTOR(TriangleFan,	PRIMITIVETYPE_TRIANGLE_FAN);
230 
231 DECLARE_PR_CTOR(Lines,			PRIMITIVETYPE_LINES);
232 DECLARE_PR_CTOR(LineStrip,		PRIMITIVETYPE_LINE_STRIP);
233 DECLARE_PR_CTOR(LineLineLoop,	PRIMITIVETYPE_LINE_LOOP);
234 
235 DECLARE_PR_CTOR(Points,			PRIMITIVETYPE_POINTS);
236 
237 DECLARE_PR_CTOR(Patches,		PRIMITIVETYPE_PATCHES);
238 
239 } // pr
240 
241 // Shorthands for VertexArrayBinding
242 namespace va
243 {
244 
245 #define DECLARE_VA_CTOR(NAME, DATATYPE, TYPE, CONVERT)																						\
246 inline VertexArrayBinding NAME (const std::string& name, int offset, int numComponents, int numElements, int stride, const DATATYPE* data)	\
247 {																																			\
248 	return VertexArrayBinding(BindingPoint(name, offset), VertexArrayPointer(TYPE, CONVERT, numComponents, numElements, stride, data));		\
249 }																																			\
250 inline VertexArrayBinding NAME (const std::string& name, int numComponents, int numElements, int stride, const DATATYPE* data)				\
251 {																																			\
252 	return NAME(name, 0, numComponents, numElements, stride, data);																			\
253 }																																			\
254 inline VertexArrayBinding NAME (int location, int numComponents, int numElements, int stride, const DATATYPE* data)							\
255 {																																			\
256 	return VertexArrayBinding(BindingPoint(location), VertexArrayPointer(TYPE, CONVERT, numComponents, numElements, stride, data));			\
257 }																																			\
258 struct DeclareVACtor##NAME##Unused_s { int unused; }
259 
260 // Integer types
261 DECLARE_VA_CTOR(Uint8,			deUint8,		VTX_COMP_UNSIGNED_INT8,		VTX_COMP_CONVERT_NONE);
262 DECLARE_VA_CTOR(Uint16,			deUint16,		VTX_COMP_UNSIGNED_INT16,	VTX_COMP_CONVERT_NONE);
263 DECLARE_VA_CTOR(Uint32,			deUint32,		VTX_COMP_UNSIGNED_INT32,	VTX_COMP_CONVERT_NONE);
264 DECLARE_VA_CTOR(Int8,			deInt8,			VTX_COMP_SIGNED_INT8,		VTX_COMP_CONVERT_NONE);
265 DECLARE_VA_CTOR(Int16,			deInt16,		VTX_COMP_SIGNED_INT16,		VTX_COMP_CONVERT_NONE);
266 DECLARE_VA_CTOR(Int32,			deInt32,		VTX_COMP_SIGNED_INT32,		VTX_COMP_CONVERT_NONE);
267 
268 // Normalized integers.
269 DECLARE_VA_CTOR(Unorm8,			deUint8,		VTX_COMP_UNSIGNED_INT8,		VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
270 DECLARE_VA_CTOR(Unorm16,		deUint16,		VTX_COMP_UNSIGNED_INT16,	VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
271 DECLARE_VA_CTOR(Unorm32,		deUint32,		VTX_COMP_UNSIGNED_INT32,	VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
272 DECLARE_VA_CTOR(Snorm8,			deInt8,			VTX_COMP_SIGNED_INT8,		VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
273 DECLARE_VA_CTOR(Snorm16,		deInt16,		VTX_COMP_SIGNED_INT16,		VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
274 DECLARE_VA_CTOR(Snorm32,		deInt32,		VTX_COMP_SIGNED_INT32,		VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
275 
276 // Integers converted to float.
277 DECLARE_VA_CTOR(Uint8Float,		deUint8,		VTX_COMP_UNSIGNED_INT8,		VTX_COMP_CONVERT_CAST_TO_FLOAT);
278 DECLARE_VA_CTOR(Uint16Float,	deUint16,		VTX_COMP_UNSIGNED_INT16,	VTX_COMP_CONVERT_CAST_TO_FLOAT);
279 DECLARE_VA_CTOR(Uint32Float,	deUint32,		VTX_COMP_UNSIGNED_INT32,	VTX_COMP_CONVERT_CAST_TO_FLOAT);
280 DECLARE_VA_CTOR(Int8Float,		deInt8,			VTX_COMP_SIGNED_INT8,		VTX_COMP_CONVERT_CAST_TO_FLOAT);
281 DECLARE_VA_CTOR(Int16Float,		deInt16,		VTX_COMP_SIGNED_INT16,		VTX_COMP_CONVERT_CAST_TO_FLOAT);
282 DECLARE_VA_CTOR(Int32Float,		deInt32,		VTX_COMP_SIGNED_INT32,		VTX_COMP_CONVERT_CAST_TO_FLOAT);
283 
284 // Special types.
285 DECLARE_VA_CTOR(Fixed,			void,			VTX_COMP_FIXED,				VTX_COMP_CONVERT_NONE);
286 DECLARE_VA_CTOR(Half,			void,			VTX_COMP_HALF_FLOAT,		VTX_COMP_CONVERT_NONE);
287 DECLARE_VA_CTOR(Float,			float,			VTX_COMP_FLOAT,				VTX_COMP_CONVERT_NONE);
288 
289 #undef DECLARE_VA_CTOR
290 
291 } // va
292 
293 } // glu
294 
295 #endif // _GLUDRAWUTIL_HPP
296