1 #ifndef _GLUDRAWUTIL_HPP
2 #define _GLUDRAWUTIL_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program OpenGL Utilities
5 * ---------------------------------------------
7 * Copyright 2014 The Android Open Source Project
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * \brief Draw call utilities.
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 *//*--------------------------------------------------------------------*/
31 #include "gluDefs.hpp"
40 enum VertexComponentType
42 // Standard types: all conversion types apply.
43 VTX_COMP_UNSIGNED_INT8 = 0,
44 VTX_COMP_UNSIGNED_INT16,
45 VTX_COMP_UNSIGNED_INT32,
47 VTX_COMP_SIGNED_INT16,
48 VTX_COMP_SIGNED_INT32,
50 // Special types: only CONVERT_NONE is allowed.
58 enum VertexComponentConversion
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.
78 PRIMITIVETYPE_TRIANGLES = 0,
79 PRIMITIVETYPE_TRIANGLE_STRIP,
80 PRIMITIVETYPE_TRIANGLE_FAN,
83 PRIMITIVETYPE_LINE_STRIP,
84 PRIMITIVETYPE_LINE_LOOP,
88 PRIMITIVETYPE_PATCHES,
97 TYPE_LOCATION = 0, //!< Binding by numeric location.
98 TYPE_NAME, //!< Binding by input name.
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.
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_) {}
112 struct VertexArrayPointer
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.
120 const void* data; //!< Data pointer.
122 VertexArrayPointer (VertexComponentType componentType_, VertexComponentConversion convert_, int numComponents_, int numElements_, int stride_, const void* data_)
123 : componentType (componentType_)
125 , numComponents (numComponents_)
126 , numElements (numElements_)
132 VertexArrayPointer (void)
133 : componentType (VTX_COMP_TYPE_LAST)
134 , convert (VTX_COMP_CONVERT_LAST)
141 } DE_WARN_UNUSED_TYPE;
143 struct VertexArrayBinding
145 BindingPoint binding;
146 VertexArrayPointer pointer;
148 VertexArrayBinding (const BindingPoint& binding_, const VertexArrayPointer& pointer_)
154 VertexArrayBinding (void)
157 } DE_WARN_UNUSED_TYPE;
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.
166 PrimitiveList (PrimitiveType type_, int numElements_)
168 , numElements (numElements_)
169 , indexType (INDEXTYPE_LAST)
174 PrimitiveList (PrimitiveType type_, int numElements_, IndexType indexType_, const void* indices_)
176 , numElements (numElements_)
177 , indexType (indexType_)
183 : type (PRIMITIVETYPE_LAST)
185 , indexType (INDEXTYPE_LAST)
189 } DE_WARN_UNUSED_TYPE;
191 class DrawUtilCallback
194 virtual void beforeDrawCall (void) { }
195 virtual void afterDrawCall (void) { }
198 void draw (const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback = DE_NULL);
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);
204 // Shorthands for PrimitiveList
208 #define DECLARE_PR_CTOR(NAME, TYPE) \
209 inline PrimitiveList NAME (int numElements) \
211 return PrimitiveList(TYPE, numElements); \
213 inline PrimitiveList NAME (int numElements, const deUint8* indices) \
215 return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT8, indices); \
217 inline PrimitiveList NAME (int numElements, const deUint16* indices) \
219 return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT16, indices); \
221 inline PrimitiveList NAME (int numElements, const deUint32* indices) \
223 return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT32, indices); \
225 struct DeclarePRCtor##NAME##Unused_s { int unused; }
227 DECLARE_PR_CTOR(Triangles, PRIMITIVETYPE_TRIANGLES);
228 DECLARE_PR_CTOR(TriangleStrip, PRIMITIVETYPE_TRIANGLE_STRIP);
229 DECLARE_PR_CTOR(TriangleFan, PRIMITIVETYPE_TRIANGLE_FAN);
231 DECLARE_PR_CTOR(Lines, PRIMITIVETYPE_LINES);
232 DECLARE_PR_CTOR(LineStrip, PRIMITIVETYPE_LINE_STRIP);
233 DECLARE_PR_CTOR(LineLineLoop, PRIMITIVETYPE_LINE_LOOP);
235 DECLARE_PR_CTOR(Points, PRIMITIVETYPE_POINTS);
237 DECLARE_PR_CTOR(Patches, PRIMITIVETYPE_PATCHES);
241 // Shorthands for VertexArrayBinding
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) \
248 return VertexArrayBinding(BindingPoint(name, offset), VertexArrayPointer(TYPE, CONVERT, numComponents, numElements, stride, data)); \
250 inline VertexArrayBinding NAME (const std::string& name, int numComponents, int numElements, int stride, const DATATYPE* data) \
252 return NAME(name, 0, numComponents, numElements, stride, data); \
254 inline VertexArrayBinding NAME (int location, int numComponents, int numElements, int stride, const DATATYPE* data) \
256 return VertexArrayBinding(BindingPoint(location), VertexArrayPointer(TYPE, CONVERT, numComponents, numElements, stride, data)); \
258 struct DeclareVACtor##NAME##Unused_s { int unused; }
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);
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);
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);
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);
289 #undef DECLARE_VA_CTOR
295 #endif // _GLUDRAWUTIL_HPP