1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL Utilities
3 * ---------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Draw call utilities.
22 *//*--------------------------------------------------------------------*/
24 #include "gluDrawUtil.hpp"
25 #include "gluRenderContext.hpp"
26 #include "gluObjectWrapper.hpp"
27 #include "glwFunctions.hpp"
28 #include "glwEnums.hpp"
41 struct VertexAttributeDescriptor
44 VertexComponentType componentType;
45 VertexComponentConversion convert;
48 int stride; //!< Stride or 0 if using default stride.
49 const void* pointer; //!< Pointer or offset.
51 VertexAttributeDescriptor (int location_,
52 VertexComponentType componentType_,
53 VertexComponentConversion convert_,
58 : location (location_)
59 , componentType (componentType_)
61 , numComponents (numComponents_)
62 , numElements (numElements_)
68 VertexAttributeDescriptor (void)
70 , componentType (VTX_COMP_TYPE_LAST)
71 , convert (VTX_COMP_CONVERT_LAST)
80 struct VertexBufferLayout
83 std::vector<VertexAttributeDescriptor> attributes;
85 VertexBufferLayout (int size_ = 0)
91 struct VertexBufferDescriptor
94 std::vector<VertexAttributeDescriptor> attributes;
96 VertexBufferDescriptor (deUint32 buffer_ = 0)
102 class VertexBuffer : public Buffer
107 TYPE_PLANAR = 0, //!< Data for each vertex array resides in a separate contiguous block in buffer.
108 TYPE_STRIDED, //!< Vertex arrays are interleaved.
113 VertexBuffer (const RenderContext& context, int numBindings, const VertexArrayBinding* bindings, Type type = TYPE_PLANAR);
114 ~VertexBuffer (void);
116 const VertexBufferDescriptor& getDescriptor (void) const { return m_layout; }
119 VertexBuffer (const VertexBuffer& other);
120 VertexBuffer& operator= (const VertexBuffer& other);
122 VertexBufferDescriptor m_layout;
125 class IndexBuffer : public Buffer
128 IndexBuffer (const RenderContext& context, IndexType indexType, int numIndices, const void* indices);
132 IndexBuffer (const IndexBuffer& other);
133 IndexBuffer& operator= (const IndexBuffer& other);
136 static deUint32 getVtxCompGLType (VertexComponentType type)
140 case VTX_COMP_UNSIGNED_INT8: return GL_UNSIGNED_BYTE;
141 case VTX_COMP_UNSIGNED_INT16: return GL_UNSIGNED_SHORT;
142 case VTX_COMP_UNSIGNED_INT32: return GL_UNSIGNED_INT;
143 case VTX_COMP_SIGNED_INT8: return GL_BYTE;
144 case VTX_COMP_SIGNED_INT16: return GL_SHORT;
145 case VTX_COMP_SIGNED_INT32: return GL_INT;
146 case VTX_COMP_FIXED: return GL_FIXED;
147 case VTX_COMP_HALF_FLOAT: return GL_HALF_FLOAT;
148 case VTX_COMP_FLOAT: return GL_FLOAT;
155 static int getVtxCompSize (VertexComponentType type)
159 case VTX_COMP_UNSIGNED_INT8: return 1;
160 case VTX_COMP_UNSIGNED_INT16: return 2;
161 case VTX_COMP_UNSIGNED_INT32: return 4;
162 case VTX_COMP_SIGNED_INT8: return 1;
163 case VTX_COMP_SIGNED_INT16: return 2;
164 case VTX_COMP_SIGNED_INT32: return 4;
165 case VTX_COMP_FIXED: return 4;
166 case VTX_COMP_HALF_FLOAT: return 2;
167 case VTX_COMP_FLOAT: return 4;
174 static deUint32 getIndexGLType (IndexType type)
178 case INDEXTYPE_UINT8: return GL_UNSIGNED_BYTE;
179 case INDEXTYPE_UINT16: return GL_UNSIGNED_SHORT;
180 case INDEXTYPE_UINT32: return GL_UNSIGNED_INT;
187 static int getIndexSize (IndexType type)
191 case INDEXTYPE_UINT8: return 1;
192 case INDEXTYPE_UINT16: return 2;
193 case INDEXTYPE_UINT32: return 4;
200 static deUint32 getPrimitiveGLType (PrimitiveType type)
204 case PRIMITIVETYPE_TRIANGLES: return GL_TRIANGLES;
205 case PRIMITIVETYPE_TRIANGLE_STRIP: return GL_TRIANGLE_STRIP;
206 case PRIMITIVETYPE_TRIANGLE_FAN: return GL_TRIANGLE_FAN;
207 case PRIMITIVETYPE_LINES: return GL_LINES;
208 case PRIMITIVETYPE_LINE_STRIP: return GL_LINE_STRIP;
209 case PRIMITIVETYPE_LINE_LOOP: return GL_LINE_LOOP;
210 case PRIMITIVETYPE_POINTS: return GL_POINTS;
211 case PRIMITIVETYPE_PATCHES: return GL_PATCHES;
218 //! Lower named bindings to locations and eliminate bindings that are not used by program.
219 template<typename InputIter, typename OutputIter>
220 static OutputIter namedBindingsToProgramLocations (const glw::Functions& gl, deUint32 program, InputIter first, InputIter end, OutputIter out)
222 for (InputIter cur = first; cur != end; ++cur)
224 const BindingPoint& binding = cur->binding;
225 if (binding.type == BindingPoint::TYPE_NAME)
227 DE_ASSERT(binding.location >= 0);
228 int location = gl.getAttribLocation(program, binding.name.c_str());
231 // Add binding.location as an offset to accommodate matrices.
232 *out = VertexArrayBinding(BindingPoint(location + binding.location), cur->pointer);
246 static deUint32 getMinimumAlignment (const VertexArrayPointer& pointer)
248 // \todo [2013-05-07 pyry] What is the actual min?
250 return (deUint32)sizeof(float);
253 template<typename BindingIter>
254 static bool areVertexArrayLocationsValid (BindingIter first, BindingIter end)
256 std::set<int> usedLocations;
257 for (BindingIter cur = first; cur != end; ++cur)
259 const BindingPoint& binding = cur->binding;
261 if (binding.type != BindingPoint::TYPE_LOCATION)
264 if (usedLocations.find(binding.location) != usedLocations.end())
267 usedLocations.insert(binding.location);
273 // \todo [2013-05-08 pyry] Buffer upload should try to match pointers to reduce dataset size.
275 static void appendAttributeNonStrided (VertexBufferLayout& layout, const VertexArrayBinding& va)
277 const int offset = deAlign32(layout.size, getMinimumAlignment(va.pointer));
278 const int elementSize = getVtxCompSize(va.pointer.componentType)*va.pointer.numComponents;
279 const int size = elementSize*va.pointer.numElements;
281 // Must be assigned to location at this point.
282 DE_ASSERT(va.binding.type == BindingPoint::TYPE_LOCATION);
284 layout.attributes.push_back(VertexAttributeDescriptor(va.binding.location,
285 va.pointer.componentType,
287 va.pointer.numComponents,
288 va.pointer.numElements,
290 (const void*)(deUintptr)offset));
291 layout.size = offset+size;
294 template<typename BindingIter>
295 static void computeNonStridedBufferLayout (VertexBufferLayout& layout, BindingIter first, BindingIter end)
297 for (BindingIter iter = first; iter != end; ++iter)
298 appendAttributeNonStrided(layout, *iter);
301 static void copyToLayout (void* dstBasePtr, const VertexAttributeDescriptor& dstVA, const VertexArrayPointer& srcPtr)
303 DE_ASSERT(dstVA.componentType == srcPtr.componentType &&
304 dstVA.numComponents == srcPtr.numComponents &&
305 dstVA.numElements == srcPtr.numElements);
307 const int elementSize = getVtxCompSize(dstVA.componentType)*dstVA.numComponents;
308 const bool srcHasCustomStride = srcPtr.stride != 0 && srcPtr.stride != elementSize;
309 const bool dstHasCustomStride = dstVA.stride != 0 && dstVA.stride != elementSize;
311 if (srcHasCustomStride || dstHasCustomStride)
313 const int dstStride = dstVA.stride != 0 ? dstVA.stride : elementSize;
314 const int srcStride = srcPtr.stride != 0 ? srcPtr.stride : elementSize;
316 for (int ndx = 0; ndx < dstVA.numElements; ndx++)
317 deMemcpy((deUint8*)dstBasePtr + (deUintptr)dstVA.pointer + ndx*dstStride, (const deUint8*)srcPtr.data + ndx*srcStride, elementSize);
320 deMemcpy((deUint8*)dstBasePtr + (deUintptr)dstVA.pointer, srcPtr.data, elementSize*dstVA.numElements);
323 void uploadBufferData (const glw::Functions& gl, deUint32 buffer, deUint32 usage, const VertexBufferLayout& layout, const VertexArrayPointer* srcArrays)
325 // Create temporary data buffer for upload.
326 std::vector<deUint8> localBuf(layout.size);
328 for (int attrNdx = 0; attrNdx < (int)layout.attributes.size(); ++attrNdx)
329 copyToLayout(&localBuf[0], layout.attributes[attrNdx], srcArrays[attrNdx]);
331 gl.bindBuffer(GL_ARRAY_BUFFER, buffer);
332 gl.bufferData(GL_ARRAY_BUFFER, (int)localBuf.size(), &localBuf[0], usage);
333 gl.bindBuffer(GL_ARRAY_BUFFER, 0);
334 GLU_EXPECT_NO_ERROR(gl.getError(), "Uploading buffer data failed");
339 VertexBuffer::VertexBuffer (const RenderContext& context, int numBindings, const VertexArrayBinding* bindings, Type type)
342 const glw::Functions& gl = context.getFunctions();
343 const deUint32 usage = GL_STATIC_DRAW;
344 VertexBufferLayout layout;
346 if (!areVertexArrayLocationsValid(bindings, bindings+numBindings))
347 throw tcu::TestError("Invalid vertex array locations");
349 if (type == TYPE_PLANAR)
350 computeNonStridedBufferLayout(layout, bindings, bindings+numBindings);
352 throw tcu::InternalError("Strided layout is not yet supported");
354 std::vector<VertexArrayPointer> srcPtrs(numBindings);
355 for (int ndx = 0; ndx < numBindings; ndx++)
356 srcPtrs[ndx] = bindings[ndx].pointer;
358 DE_ASSERT(srcPtrs.size() == layout.attributes.size());
359 if (!srcPtrs.empty())
360 uploadBufferData(gl, m_object, usage, layout, &srcPtrs[0]);
362 // Construct descriptor.
363 m_layout.buffer = m_object;
364 m_layout.attributes = layout.attributes;
367 VertexBuffer::~VertexBuffer (void)
373 IndexBuffer::IndexBuffer (const RenderContext& context, IndexType indexType, int numIndices, const void* indices)
376 const glw::Functions& gl = context.getFunctions();
377 const deUint32 usage = GL_STATIC_DRAW;
379 gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_object);
380 gl.bufferData(GL_ELEMENT_ARRAY_BUFFER, numIndices*getIndexSize(indexType), indices, usage);
381 gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
382 GLU_EXPECT_NO_ERROR(gl.getError(), "Uploading index data failed");
385 IndexBuffer::~IndexBuffer (void)
389 static inline VertexAttributeDescriptor getUserPointerDescriptor (const VertexArrayBinding& vertexArray)
391 DE_ASSERT(vertexArray.binding.type == BindingPoint::TYPE_LOCATION);
393 return VertexAttributeDescriptor(vertexArray.binding.location,
394 vertexArray.pointer.componentType,
395 vertexArray.pointer.convert,
396 vertexArray.pointer.numComponents,
397 vertexArray.pointer.numElements,
398 vertexArray.pointer.stride,
399 vertexArray.pointer.data);
402 //! Setup VA according to allocation spec. Assumes that other state (VAO binding, buffer) is set already.
403 static void setVertexAttribPointer (const glw::Functions& gl, const VertexAttributeDescriptor& va)
405 const bool isIntType = de::inRange<int>(va.componentType, VTX_COMP_UNSIGNED_INT8, VTX_COMP_SIGNED_INT32);
406 const bool isSpecialType = de::inRange<int>(va.componentType, VTX_COMP_FIXED, VTX_COMP_FLOAT);
407 const deUint32 compTypeGL = getVtxCompGLType(va.componentType);
409 DE_ASSERT(isIntType != isSpecialType); // Must be either int or special type.
410 DE_ASSERT(isIntType || va.convert == VTX_COMP_CONVERT_NONE); // Conversion allowed only for special types.
411 DE_UNREF(isSpecialType);
413 gl.enableVertexAttribArray(va.location);
415 if (isIntType && va.convert == VTX_COMP_CONVERT_NONE)
416 gl.vertexAttribIPointer(va.location, va.numComponents, compTypeGL, va.stride, va.pointer);
418 gl.vertexAttribPointer(va.location, va.numComponents, compTypeGL, va.convert == VTX_COMP_CONVERT_NORMALIZE_TO_FLOAT ? GL_TRUE : GL_FALSE, va.stride, va.pointer);
421 //! Setup vertex buffer and attributes.
422 static void setVertexBufferAttributes (const glw::Functions& gl, const VertexBufferDescriptor& buffer)
424 gl.bindBuffer(GL_ARRAY_BUFFER, buffer.buffer);
426 for (std::vector<VertexAttributeDescriptor>::const_iterator vaIter = buffer.attributes.begin(); vaIter != buffer.attributes.end(); ++vaIter)
427 setVertexAttribPointer(gl, *vaIter);
429 gl.bindBuffer(GL_ARRAY_BUFFER, 0);
432 static void disableVertexArrays (const glw::Functions& gl, const std::vector<VertexArrayBinding>& bindings)
434 for (std::vector<VertexArrayBinding>::const_iterator vaIter = bindings.begin(); vaIter != bindings.end(); ++vaIter)
436 DE_ASSERT(vaIter->binding.type == BindingPoint::TYPE_LOCATION);
437 gl.disableVertexAttribArray(vaIter->binding.location);
441 #if defined(DE_DEBUG)
442 static bool isProgramActive (const RenderContext& context, deUint32 program)
444 // \todo [2013-05-08 pyry] Is this query broken?
445 /* deUint32 activeProgram = 0;
446 context.getFunctions().getIntegerv(GL_ACTIVE_PROGRAM, (int*)&activeProgram);
447 GLU_EXPECT_NO_ERROR(context.getFunctions().getError(), "oh");
448 return activeProgram == program;*/
454 static bool isDrawCallValid (int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives)
456 if (numVertexArrays < 0)
459 if ((primitives.indexType == INDEXTYPE_LAST) != (primitives.indices == 0))
462 if (primitives.numElements < 0)
465 if (!primitives.indices)
467 for (int ndx = 0; ndx < numVertexArrays; ndx++)
469 if (primitives.numElements > vertexArrays[ndx].pointer.numElements)
473 // \todo [2013-05-08 pyry] We could walk whole index array and determine index range
479 static inline void drawNonIndexed (const glw::Functions& gl, PrimitiveType type, int numElements)
481 deUint32 mode = getPrimitiveGLType(type);
482 gl.drawArrays(mode, 0, numElements);
485 static inline void drawIndexed (const glw::Functions& gl, PrimitiveType type, int numElements, IndexType indexType, const void* indexPtr)
487 deUint32 mode = getPrimitiveGLType(type);
488 deUint32 indexGLType = getIndexGLType(indexType);
490 gl.drawElements(mode, numElements, indexGLType, indexPtr);
495 void drawFromUserPointers (const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback)
497 const glw::Functions& gl = context.getFunctions();
498 std::vector<VertexArrayBinding> bindingsWithLocations;
500 DE_ASSERT(isDrawCallValid(numVertexArrays, vertexArrays, primitives));
501 DE_ASSERT(isProgramActive(context, program));
503 // Lower bindings to locations.
504 namedBindingsToProgramLocations(gl, program, vertexArrays, vertexArrays+numVertexArrays, std::inserter(bindingsWithLocations, bindingsWithLocations.begin()));
506 TCU_CHECK(areVertexArrayLocationsValid(bindingsWithLocations.begin(), bindingsWithLocations.end()));
509 for (std::vector<VertexArrayBinding>::const_iterator vaIter = bindingsWithLocations.begin(); vaIter != bindingsWithLocations.end(); ++vaIter)
510 setVertexAttribPointer(gl, getUserPointerDescriptor(*vaIter));
513 callback->beforeDrawCall();
515 if (primitives.indices)
516 drawIndexed(gl, primitives.type, primitives.numElements, primitives.indexType, primitives.indices);
518 drawNonIndexed(gl, primitives.type, primitives.numElements);
521 callback->afterDrawCall();
523 // Disable attribute arrays or otherwise someone later on might get crash thanks to invalid pointers.
524 disableVertexArrays(gl, bindingsWithLocations);
527 void drawFromBuffers (const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback)
529 const glw::Functions& gl = context.getFunctions();
530 std::vector<VertexArrayBinding> bindingsWithLocations;
532 DE_ASSERT(isDrawCallValid(numVertexArrays, vertexArrays, primitives));
533 DE_ASSERT(isProgramActive(context, program));
535 // Lower bindings to locations.
536 namedBindingsToProgramLocations(gl, program, vertexArrays, vertexArrays+numVertexArrays, std::inserter(bindingsWithLocations, bindingsWithLocations.begin()));
538 TCU_CHECK(areVertexArrayLocationsValid(bindingsWithLocations.begin(), bindingsWithLocations.end()));
540 // Create buffers for duration of draw call.
542 VertexBuffer vertexBuffer (context, (int)bindingsWithLocations.size(), (bindingsWithLocations.empty()) ? (DE_NULL) : (&bindingsWithLocations[0]));
545 setVertexBufferAttributes(gl, vertexBuffer.getDescriptor());
547 if (primitives.indices)
549 IndexBuffer indexBuffer(context, primitives.indexType, primitives.numElements, primitives.indices);
551 gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, *indexBuffer);
554 callback->beforeDrawCall();
556 drawIndexed(gl, primitives.type, primitives.numElements, primitives.indexType, 0);
559 callback->afterDrawCall();
561 gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
566 callback->beforeDrawCall();
568 drawNonIndexed(gl, primitives.type, primitives.numElements);
571 callback->afterDrawCall();
575 // Disable attribute arrays or otherwise someone later on might get crash thanks to invalid pointers.
576 for (std::vector<VertexArrayBinding>::const_iterator vaIter = bindingsWithLocations.begin(); vaIter != bindingsWithLocations.end(); ++vaIter)
577 gl.disableVertexAttribArray(vaIter->binding.location);
580 void drawFromVAOBuffers (const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback)
582 const glw::Functions& gl = context.getFunctions();
583 VertexArray vao (context);
585 gl.bindVertexArray(*vao);
586 drawFromBuffers(context, program, numVertexArrays, vertexArrays, primitives, callback);
587 gl.bindVertexArray(0);
590 void draw (const RenderContext& context, deUint32 program, int numVertexArrays, const VertexArrayBinding* vertexArrays, const PrimitiveList& primitives, DrawUtilCallback* callback)
592 const glu::ContextType ctxType = context.getType();
594 if (isContextTypeGLCore(ctxType) || contextSupports(ctxType, ApiType::es(3,1)))
595 drawFromVAOBuffers(context, program, numVertexArrays, vertexArrays, primitives, callback);
598 DE_ASSERT(isContextTypeES(ctxType));
599 drawFromUserPointers(context, program, numVertexArrays, vertexArrays, primitives, callback);