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
35namespace glu
36{
37
38class RenderContext;
39
40enum 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
58enum 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
67enum IndexType
68{
69	INDEXTYPE_UINT8,
70	INDEXTYPE_UINT16,
71	INDEXTYPE_UINT32,
72
73	INDEXTYPE_LAST
74};
75
76enum 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
93struct BindingPoint
94{
95	enum Type
96	{
97		TYPE_LOCATION = 0,			//!< Binding by numeric location.
98		TYPE_NAME,					//!< Binding by input name.
99
100		TYPE_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
107				BindingPoint (void)											: type(TYPE_LAST), location (0) {}
108	explicit	BindingPoint (int location_)								: type(TYPE_LOCATION), location(location_) {}
109	explicit	BindingPoint (const std::string& name_, int location_ = 0)	: type(TYPE_NAME), name(name_), location(location_) {}
110};
111
112struct 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
122	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
132	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
143struct VertexArrayBinding
144{
145	BindingPoint		binding;
146	VertexArrayPointer	pointer;
147
148	VertexArrayBinding (const BindingPoint& binding_, const VertexArrayPointer& pointer_)
149		: binding	(binding_)
150		, pointer	(pointer_)
151	{
152	}
153
154	VertexArrayBinding (void)
155	{
156	}
157} DE_WARN_UNUSED_TYPE;
158
159struct 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
166	PrimitiveList (PrimitiveType type_, int numElements_)
167		: type			(type_)
168		, numElements	(numElements_)
169		, indexType		(INDEXTYPE_LAST)
170		, indices		(0)
171	{
172	}
173
174	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
182	PrimitiveList (void)
183		: type			(PRIMITIVETYPE_LAST)
184		, numElements	(0)
185		, indexType		(INDEXTYPE_LAST)
186		, indices		(0)
187	{
188	}
189} DE_WARN_UNUSED_TYPE;
190
191class DrawUtilCallback
192{
193public:
194	virtual void	beforeDrawCall	(void) { }
195	virtual void	afterDrawCall	(void) { }
196};
197
198void	draw					(const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback = DE_NULL);
199
200void	drawFromUserPointers	(const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback = DE_NULL);
201void	drawFromBuffers			(const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback = DE_NULL);
202void	drawFromVAOBuffers		(const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback = DE_NULL);
203
204// Shorthands for PrimitiveList
205namespace pr
206{
207
208#define DECLARE_PR_CTOR(NAME, TYPE)										\
209inline PrimitiveList NAME (int numElements)								\
210{																		\
211	return PrimitiveList(TYPE, numElements);							\
212}																		\
213inline PrimitiveList NAME (int numElements, const deUint8* indices)		\
214{																		\
215	return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT8, indices);	\
216}																		\
217inline PrimitiveList NAME (int numElements, const deUint16* indices)	\
218{																		\
219	return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT16, indices);	\
220}																		\
221inline PrimitiveList NAME (int numElements, const deUint32* indices)	\
222{																		\
223	return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT32, indices);	\
224}																		\
225struct DeclarePRCtor##NAME##Unused_s { int unused; }
226
227DECLARE_PR_CTOR(Triangles,		PRIMITIVETYPE_TRIANGLES);
228DECLARE_PR_CTOR(TriangleStrip,	PRIMITIVETYPE_TRIANGLE_STRIP);
229DECLARE_PR_CTOR(TriangleFan,	PRIMITIVETYPE_TRIANGLE_FAN);
230
231DECLARE_PR_CTOR(Lines,			PRIMITIVETYPE_LINES);
232DECLARE_PR_CTOR(LineStrip,		PRIMITIVETYPE_LINE_STRIP);
233DECLARE_PR_CTOR(LineLineLoop,	PRIMITIVETYPE_LINE_LOOP);
234
235DECLARE_PR_CTOR(Points,			PRIMITIVETYPE_POINTS);
236
237DECLARE_PR_CTOR(Patches,		PRIMITIVETYPE_PATCHES);
238
239} // pr
240
241// Shorthands for VertexArrayBinding
242namespace va
243{
244
245#define DECLARE_VA_CTOR(NAME, DATATYPE, TYPE, CONVERT)																						\
246inline 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}																																			\
250inline 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}																																			\
254inline 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}																																			\
258struct DeclareVACtor##NAME##Unused_s { int unused; }
259
260// Integer types
261DECLARE_VA_CTOR(Uint8,			deUint8,		VTX_COMP_UNSIGNED_INT8,		VTX_COMP_CONVERT_NONE);
262DECLARE_VA_CTOR(Uint16,			deUint16,		VTX_COMP_UNSIGNED_INT16,	VTX_COMP_CONVERT_NONE);
263DECLARE_VA_CTOR(Uint32,			deUint32,		VTX_COMP_UNSIGNED_INT32,	VTX_COMP_CONVERT_NONE);
264DECLARE_VA_CTOR(Int8,			deInt8,			VTX_COMP_SIGNED_INT8,		VTX_COMP_CONVERT_NONE);
265DECLARE_VA_CTOR(Int16,			deInt16,		VTX_COMP_SIGNED_INT16,		VTX_COMP_CONVERT_NONE);
266DECLARE_VA_CTOR(Int32,			deInt32,		VTX_COMP_SIGNED_INT32,		VTX_COMP_CONVERT_NONE);
267
268// Normalized integers.
269DECLARE_VA_CTOR(Unorm8,			deUint8,		VTX_COMP_UNSIGNED_INT8,		VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
270DECLARE_VA_CTOR(Unorm16,		deUint16,		VTX_COMP_UNSIGNED_INT16,	VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
271DECLARE_VA_CTOR(Unorm32,		deUint32,		VTX_COMP_UNSIGNED_INT32,	VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
272DECLARE_VA_CTOR(Snorm8,			deInt8,			VTX_COMP_SIGNED_INT8,		VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
273DECLARE_VA_CTOR(Snorm16,		deInt16,		VTX_COMP_SIGNED_INT16,		VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
274DECLARE_VA_CTOR(Snorm32,		deInt32,		VTX_COMP_SIGNED_INT32,		VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT);
275
276// Integers converted to float.
277DECLARE_VA_CTOR(Uint8Float,		deUint8,		VTX_COMP_UNSIGNED_INT8,		VTX_COMP_CONVERT_CAST_TO_FLOAT);
278DECLARE_VA_CTOR(Uint16Float,	deUint16,		VTX_COMP_UNSIGNED_INT16,	VTX_COMP_CONVERT_CAST_TO_FLOAT);
279DECLARE_VA_CTOR(Uint32Float,	deUint32,		VTX_COMP_UNSIGNED_INT32,	VTX_COMP_CONVERT_CAST_TO_FLOAT);
280DECLARE_VA_CTOR(Int8Float,		deInt8,			VTX_COMP_SIGNED_INT8,		VTX_COMP_CONVERT_CAST_TO_FLOAT);
281DECLARE_VA_CTOR(Int16Float,		deInt16,		VTX_COMP_SIGNED_INT16,		VTX_COMP_CONVERT_CAST_TO_FLOAT);
282DECLARE_VA_CTOR(Int32Float,		deInt32,		VTX_COMP_SIGNED_INT32,		VTX_COMP_CONVERT_CAST_TO_FLOAT);
283
284// Special types.
285DECLARE_VA_CTOR(Fixed,			void,			VTX_COMP_FIXED,				VTX_COMP_CONVERT_NONE);
286DECLARE_VA_CTOR(Half,			void,			VTX_COMP_HALF_FLOAT,		VTX_COMP_CONVERT_NONE);
287DECLARE_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