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) 108 { 109 } BindingPointglu::BindingPoint110 explicit BindingPoint(int location_) : type(BPTYPE_LOCATION), location(location_) 111 { 112 } BindingPointglu::BindingPoint113 explicit BindingPoint(const std::string &name_, int location_ = 0) 114 : type(BPTYPE_NAME) 115 , name(name_) 116 , location(location_) 117 { 118 } 119 }; 120 121 struct VertexArrayPointer 122 { 123 VertexComponentType componentType; //!< Component type. 124 VertexComponentConversion convert; //!< Component conversion type. 125 int numComponents; //!< Number of components per element. 126 int numElements; //!< Number of elements in total. 127 int stride; //!< Element stride. 128 129 const void *data; //!< Data pointer. 130 VertexArrayPointerglu::VertexArrayPointer131 VertexArrayPointer(VertexComponentType componentType_, VertexComponentConversion convert_, int numComponents_, 132 int numElements_, int stride_, const void *data_) 133 : componentType(componentType_) 134 , convert(convert_) 135 , numComponents(numComponents_) 136 , numElements(numElements_) 137 , stride(stride_) 138 , data(data_) 139 { 140 } 141 VertexArrayPointerglu::VertexArrayPointer142 VertexArrayPointer(void) 143 : componentType(VTX_COMP_TYPE_LAST) 144 , convert(VTX_COMP_CONVERT_LAST) 145 , numComponents(0) 146 , numElements(0) 147 , stride(0) 148 , data(0) 149 { 150 } 151 } DE_WARN_UNUSED_TYPE; 152 153 struct VertexArrayBinding 154 { 155 BindingPoint binding; 156 VertexArrayPointer pointer; 157 VertexArrayBindingglu::VertexArrayBinding158 VertexArrayBinding(const BindingPoint &binding_, const VertexArrayPointer &pointer_) 159 : binding(binding_) 160 , pointer(pointer_) 161 { 162 } 163 VertexArrayBindingglu::VertexArrayBinding164 VertexArrayBinding(void) 165 { 166 } 167 } DE_WARN_UNUSED_TYPE; 168 169 struct PrimitiveList 170 { 171 PrimitiveType type; //!< Primitive type. 172 int numElements; //!< Number of elements to be drawn. 173 IndexType indexType; //!< Index type or INDEXTYPE_LAST if not used 174 const void *indices; //!< Index list or DE_NULL if not used. 175 PrimitiveListglu::PrimitiveList176 PrimitiveList(PrimitiveType type_, int numElements_) 177 : type(type_) 178 , numElements(numElements_) 179 , indexType(INDEXTYPE_LAST) 180 , indices(0) 181 { 182 } 183 PrimitiveListglu::PrimitiveList184 PrimitiveList(PrimitiveType type_, int numElements_, IndexType indexType_, const void *indices_) 185 : type(type_) 186 , numElements(numElements_) 187 , indexType(indexType_) 188 , indices(indices_) 189 { 190 } 191 PrimitiveListglu::PrimitiveList192 PrimitiveList(void) : type(PRIMITIVETYPE_LAST), numElements(0), indexType(INDEXTYPE_LAST), indices(0) 193 { 194 } 195 } DE_WARN_UNUSED_TYPE; 196 197 class DrawUtilCallback 198 { 199 public: beforeDrawCall(void)200 virtual void beforeDrawCall(void) 201 { 202 } afterDrawCall(void)203 virtual void afterDrawCall(void) 204 { 205 } 206 }; 207 208 void draw(const RenderContext &context, uint32_t program, int numVertexArrays, const VertexArrayBinding *vertexArrays, 209 const PrimitiveList &primitives, DrawUtilCallback *callback = DE_NULL); 210 211 void drawFromUserPointers(const RenderContext &context, uint32_t program, int numVertexArrays, 212 const VertexArrayBinding *vertexArrays, const PrimitiveList &primitives, 213 DrawUtilCallback *callback = DE_NULL); 214 void drawFromBuffers(const RenderContext &context, uint32_t program, int numVertexArrays, 215 const VertexArrayBinding *vertexArrays, const PrimitiveList &primitives, 216 DrawUtilCallback *callback = DE_NULL); 217 void drawFromVAOBuffers(const RenderContext &context, uint32_t program, int numVertexArrays, 218 const VertexArrayBinding *vertexArrays, const PrimitiveList &primitives, 219 DrawUtilCallback *callback = DE_NULL); 220 221 // Shorthands for PrimitiveList 222 namespace pr 223 { 224 225 #define DECLARE_PR_CTOR(NAME, TYPE) \ 226 inline PrimitiveList NAME(int numElements) \ 227 { \ 228 return PrimitiveList(TYPE, numElements); \ 229 } \ 230 inline PrimitiveList NAME(int numElements, const uint8_t *indices) \ 231 { \ 232 return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT8, indices); \ 233 } \ 234 inline PrimitiveList NAME(int numElements, const uint16_t *indices) \ 235 { \ 236 return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT16, indices); \ 237 } \ 238 inline PrimitiveList NAME(int numElements, const uint32_t *indices) \ 239 { \ 240 return PrimitiveList(TYPE, numElements, INDEXTYPE_UINT32, indices); \ 241 } \ 242 struct DeclarePRCtor##NAME##Unused_s \ 243 { \ 244 int unused; \ 245 } 246 247 DECLARE_PR_CTOR(Triangles, PRIMITIVETYPE_TRIANGLES); 248 DECLARE_PR_CTOR(TriangleStrip, PRIMITIVETYPE_TRIANGLE_STRIP); 249 DECLARE_PR_CTOR(TriangleFan, PRIMITIVETYPE_TRIANGLE_FAN); 250 251 DECLARE_PR_CTOR(Lines, PRIMITIVETYPE_LINES); 252 DECLARE_PR_CTOR(LineStrip, PRIMITIVETYPE_LINE_STRIP); 253 DECLARE_PR_CTOR(LineLineLoop, PRIMITIVETYPE_LINE_LOOP); 254 255 DECLARE_PR_CTOR(Points, PRIMITIVETYPE_POINTS); 256 257 DECLARE_PR_CTOR(Patches, PRIMITIVETYPE_PATCHES); 258 259 } // namespace pr 260 261 // Shorthands for VertexArrayBinding 262 namespace va 263 { 264 265 #define DECLARE_VA_CTOR(NAME, DATATYPE, TYPE, CONVERT) \ 266 inline VertexArrayBinding NAME(const std::string &name, int offset, int numComponents, int numElements, \ 267 int stride, const DATATYPE *data) \ 268 { \ 269 return VertexArrayBinding(BindingPoint(name, offset), \ 270 VertexArrayPointer(TYPE, CONVERT, numComponents, numElements, stride, data)); \ 271 } \ 272 inline VertexArrayBinding NAME(const std::string &name, int numComponents, int numElements, int stride, \ 273 const DATATYPE *data) \ 274 { \ 275 return NAME(name, 0, numComponents, numElements, stride, data); \ 276 } \ 277 inline VertexArrayBinding NAME(int location, int numComponents, int numElements, int stride, const DATATYPE *data) \ 278 { \ 279 return VertexArrayBinding(BindingPoint(location), \ 280 VertexArrayPointer(TYPE, CONVERT, numComponents, numElements, stride, data)); \ 281 } \ 282 struct DeclareVACtor##NAME##Unused_s \ 283 { \ 284 int unused; \ 285 } 286 287 // Integer types 288 DECLARE_VA_CTOR(Uint8, uint8_t, VTX_COMP_UNSIGNED_INT8, VTX_COMP_CONVERT_NONE); 289 DECLARE_VA_CTOR(Uint16, uint16_t, VTX_COMP_UNSIGNED_INT16, VTX_COMP_CONVERT_NONE); 290 DECLARE_VA_CTOR(Uint32, uint32_t, VTX_COMP_UNSIGNED_INT32, VTX_COMP_CONVERT_NONE); 291 DECLARE_VA_CTOR(Int8, int8_t, VTX_COMP_SIGNED_INT8, VTX_COMP_CONVERT_NONE); 292 DECLARE_VA_CTOR(Int16, int16_t, VTX_COMP_SIGNED_INT16, VTX_COMP_CONVERT_NONE); 293 DECLARE_VA_CTOR(Int32, int32_t, VTX_COMP_SIGNED_INT32, VTX_COMP_CONVERT_NONE); 294 295 // Normalized integers. 296 DECLARE_VA_CTOR(Unorm8, uint8_t, VTX_COMP_UNSIGNED_INT8, VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT); 297 DECLARE_VA_CTOR(Unorm16, uint16_t, VTX_COMP_UNSIGNED_INT16, VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT); 298 DECLARE_VA_CTOR(Unorm32, uint32_t, VTX_COMP_UNSIGNED_INT32, VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT); 299 DECLARE_VA_CTOR(Snorm8, int8_t, VTX_COMP_SIGNED_INT8, VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT); 300 DECLARE_VA_CTOR(Snorm16, int16_t, VTX_COMP_SIGNED_INT16, VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT); 301 DECLARE_VA_CTOR(Snorm32, int32_t, VTX_COMP_SIGNED_INT32, VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT); 302 303 // Integers converted to float. 304 DECLARE_VA_CTOR(Uint8Float, uint8_t, VTX_COMP_UNSIGNED_INT8, VTX_COMP_CONVERT_CAST_TO_FLOAT); 305 DECLARE_VA_CTOR(Uint16Float, uint16_t, VTX_COMP_UNSIGNED_INT16, VTX_COMP_CONVERT_CAST_TO_FLOAT); 306 DECLARE_VA_CTOR(Uint32Float, uint32_t, VTX_COMP_UNSIGNED_INT32, VTX_COMP_CONVERT_CAST_TO_FLOAT); 307 DECLARE_VA_CTOR(Int8Float, int8_t, VTX_COMP_SIGNED_INT8, VTX_COMP_CONVERT_CAST_TO_FLOAT); 308 DECLARE_VA_CTOR(Int16Float, int16_t, VTX_COMP_SIGNED_INT16, VTX_COMP_CONVERT_CAST_TO_FLOAT); 309 DECLARE_VA_CTOR(Int32Float, int32_t, VTX_COMP_SIGNED_INT32, VTX_COMP_CONVERT_CAST_TO_FLOAT); 310 311 // Special types. 312 DECLARE_VA_CTOR(Fixed, void, VTX_COMP_FIXED, VTX_COMP_CONVERT_NONE); 313 DECLARE_VA_CTOR(Half, void, VTX_COMP_HALF_FLOAT, VTX_COMP_CONVERT_NONE); 314 DECLARE_VA_CTOR(Float, float, VTX_COMP_FLOAT, VTX_COMP_CONVERT_NONE); 315 316 #undef DECLARE_VA_CTOR 317 318 } // namespace va 319 320 } // namespace glu 321 322 #endif // _GLUDRAWUTIL_HPP 323