1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL (ES) Module
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 Uniform block case.
22 *//*--------------------------------------------------------------------*/
24 #include "glsUniformBlockCase.hpp"
25 #include "gluRenderContext.hpp"
26 #include "gluShaderProgram.hpp"
27 #include "gluPixelTransfer.hpp"
28 #include "gluContextInfo.hpp"
29 #include "gluRenderContext.hpp"
30 #include "gluDrawUtil.hpp"
31 #include "glwFunctions.hpp"
32 #include "glwEnums.hpp"
33 #include "tcuTestLog.hpp"
34 #include "tcuSurface.hpp"
35 #include "tcuRenderTarget.hpp"
36 #include "deRandom.hpp"
37 #include "deStringUtil.hpp"
56 static bool isSupportedGLSLVersion (glu::GLSLVersion version)
58 return version >= (glslVersionIsES(version) ? glu::GLSL_VERSION_300_ES : glu::GLSL_VERSION_330);
61 struct PrecisionFlagsFmt
64 PrecisionFlagsFmt (deUint32 flags_) : flags(flags_) {}
67 std::ostream& operator<< (std::ostream& str, const PrecisionFlagsFmt& fmt)
70 DE_ASSERT(dePop32(fmt.flags & (PRECISION_LOW|PRECISION_MEDIUM|PRECISION_HIGH)) <= 1);
71 str << (fmt.flags & PRECISION_LOW ? "lowp" :
72 fmt.flags & PRECISION_MEDIUM ? "mediump" :
73 fmt.flags & PRECISION_HIGH ? "highp" : "");
80 LayoutFlagsFmt (deUint32 flags_) : flags(flags_) {}
83 std::ostream& operator<< (std::ostream& str, const LayoutFlagsFmt& fmt)
91 { LAYOUT_SHARED, "shared" },
92 { LAYOUT_PACKED, "packed" },
93 { LAYOUT_STD140, "std140" },
94 { LAYOUT_ROW_MAJOR, "row_major" },
95 { LAYOUT_COLUMN_MAJOR, "column_major" }
98 deUint32 remBits = fmt.flags;
99 for (int descNdx = 0; descNdx < DE_LENGTH_OF_ARRAY(bitDesc); descNdx++)
101 if (remBits & bitDesc[descNdx].bit)
103 if (remBits != fmt.flags)
105 str << bitDesc[descNdx].token;
106 remBits &= ~bitDesc[descNdx].bit;
109 DE_ASSERT(remBits == 0);
113 // VarType implementation.
115 VarType::VarType (void)
121 VarType::VarType (const VarType& other)
128 VarType::VarType (glu::DataType basicType, deUint32 flags)
129 : m_type (TYPE_BASIC)
132 m_data.basicType = basicType;
135 VarType::VarType (const VarType& elementType, int arraySize)
136 : m_type (TYPE_ARRAY)
139 m_data.array.size = arraySize;
140 m_data.array.elementType = new VarType(elementType);
143 VarType::VarType (const StructType* structPtr)
144 : m_type (TYPE_STRUCT)
147 m_data.structPtr = structPtr;
150 VarType::~VarType (void)
152 if (m_type == TYPE_ARRAY)
153 delete m_data.array.elementType;
156 VarType& VarType::operator= (const VarType& other)
159 return *this; // Self-assignment.
161 if (m_type == TYPE_ARRAY)
162 delete m_data.array.elementType;
164 m_type = other.m_type;
165 m_flags = other.m_flags;
168 if (m_type == TYPE_ARRAY)
170 m_data.array.elementType = new VarType(*other.m_data.array.elementType);
171 m_data.array.size = other.m_data.array.size;
174 m_data = other.m_data;
179 // StructType implementation.
181 void StructType::addMember (const char* name, const VarType& type, deUint32 flags)
183 m_members.push_back(StructMember(name, type, flags));
186 // Uniform implementation.
188 Uniform::Uniform (const char* name, const VarType& type, deUint32 flags)
195 // UniformBlock implementation.
197 UniformBlock::UniformBlock (const char* blockName)
198 : m_blockName (blockName)
204 struct BlockLayoutEntry
206 BlockLayoutEntry (void)
213 std::vector<int> activeUniformIndices;
216 std::ostream& operator<< (std::ostream& stream, const BlockLayoutEntry& entry)
218 stream << entry.name << " { name = " << entry.name
219 << ", size = " << entry.size
220 << ", activeUniformIndices = [";
222 for (vector<int>::const_iterator i = entry.activeUniformIndices.begin(); i != entry.activeUniformIndices.end(); i++)
224 if (i != entry.activeUniformIndices.begin())
233 struct UniformLayoutEntry
235 UniformLayoutEntry (void)
236 : type (glu::TYPE_LAST)
256 std::ostream& operator<< (std::ostream& stream, const UniformLayoutEntry& entry)
258 stream << entry.name << " { type = " << glu::getDataTypeName(entry.type)
259 << ", size = " << entry.size
260 << ", blockNdx = " << entry.blockNdx
261 << ", offset = " << entry.offset
262 << ", arrayStride = " << entry.arrayStride
263 << ", matrixStride = " << entry.matrixStride
264 << ", isRowMajor = " << (entry.isRowMajor ? "true" : "false")
272 std::vector<BlockLayoutEntry> blocks;
273 std::vector<UniformLayoutEntry> uniforms;
275 int getUniformIndex (const char* name) const;
276 int getBlockIndex (const char* name) const;
279 // \todo [2012-01-24 pyry] Speed up lookups using hash.
281 int UniformLayout::getUniformIndex (const char* name) const
283 for (int ndx = 0; ndx < (int)uniforms.size(); ndx++)
285 if (uniforms[ndx].name == name)
291 int UniformLayout::getBlockIndex (const char* name) const
293 for (int ndx = 0; ndx < (int)blocks.size(); ndx++)
295 if (blocks[ndx].name == name)
301 // ShaderInterface implementation.
303 ShaderInterface::ShaderInterface (void)
307 ShaderInterface::~ShaderInterface (void)
309 for (std::vector<StructType*>::iterator i = m_structs.begin(); i != m_structs.end(); i++)
312 for (std::vector<UniformBlock*>::iterator i = m_uniformBlocks.begin(); i != m_uniformBlocks.end(); i++)
316 StructType& ShaderInterface::allocStruct (const char* name)
318 m_structs.reserve(m_structs.size()+1);
319 m_structs.push_back(new StructType(name));
320 return *m_structs.back();
323 struct StructNameEquals
327 StructNameEquals (const char* name_) : name(name_) {}
329 bool operator() (const StructType* type) const
331 return type->getTypeName() && name == type->getTypeName();
335 const StructType* ShaderInterface::findStruct (const char* name) const
337 std::vector<StructType*>::const_iterator pos = std::find_if(m_structs.begin(), m_structs.end(), StructNameEquals(name));
338 return pos != m_structs.end() ? *pos : DE_NULL;
341 void ShaderInterface::getNamedStructs (std::vector<const StructType*>& structs) const
343 for (std::vector<StructType*>::const_iterator i = m_structs.begin(); i != m_structs.end(); i++)
345 if ((*i)->getTypeName() != DE_NULL)
346 structs.push_back(*i);
350 UniformBlock& ShaderInterface::allocBlock (const char* name)
352 m_uniformBlocks.reserve(m_uniformBlocks.size()+1);
353 m_uniformBlocks.push_back(new UniformBlock(name));
354 return *m_uniformBlocks.back();
357 namespace // Utilities
360 // Layout computation.
362 int getDataTypeByteSize (glu::DataType type)
364 return glu::getDataTypeScalarSize(type)*(int)sizeof(deUint32);
367 int getDataTypeByteAlignment (glu::DataType type)
371 case glu::TYPE_FLOAT:
374 case glu::TYPE_BOOL: return 1*(int)sizeof(deUint32);
376 case glu::TYPE_FLOAT_VEC2:
377 case glu::TYPE_INT_VEC2:
378 case glu::TYPE_UINT_VEC2:
379 case glu::TYPE_BOOL_VEC2: return 2*(int)sizeof(deUint32);
381 case glu::TYPE_FLOAT_VEC3:
382 case glu::TYPE_INT_VEC3:
383 case glu::TYPE_UINT_VEC3:
384 case glu::TYPE_BOOL_VEC3: // Fall-through to vec4
386 case glu::TYPE_FLOAT_VEC4:
387 case glu::TYPE_INT_VEC4:
388 case glu::TYPE_UINT_VEC4:
389 case glu::TYPE_BOOL_VEC4: return 4*(int)sizeof(deUint32);
397 int getDataTypeArrayStride (glu::DataType type)
399 DE_ASSERT(!glu::isDataTypeMatrix(type));
401 const int baseStride = getDataTypeByteSize(type);
402 const int vec4Alignment = (int)sizeof(deUint32)*4;
404 DE_ASSERT(baseStride <= vec4Alignment);
405 return de::max(baseStride, vec4Alignment); // Really? See rule 4.
408 static inline int deRoundUp32 (int a, int b)
411 return d*b == a ? a : (d+1)*b;
414 int computeStd140BaseAlignment (const VarType& type)
416 const int vec4Alignment = (int)sizeof(deUint32)*4;
418 if (type.isBasicType())
420 glu::DataType basicType = type.getBasicType();
422 if (glu::isDataTypeMatrix(basicType))
424 bool isRowMajor = !!(type.getFlags() & LAYOUT_ROW_MAJOR);
425 int vecSize = isRowMajor ? glu::getDataTypeMatrixNumColumns(basicType)
426 : glu::getDataTypeMatrixNumRows(basicType);
428 return getDataTypeArrayStride(glu::getDataTypeFloatVec(vecSize));
431 return getDataTypeByteAlignment(basicType);
433 else if (type.isArrayType())
435 int elemAlignment = computeStd140BaseAlignment(type.getElementType());
437 // Round up to alignment of vec4
438 return deRoundUp32(elemAlignment, vec4Alignment);
442 DE_ASSERT(type.isStructType());
444 int maxBaseAlignment = 0;
446 for (StructType::ConstIterator memberIter = type.getStruct().begin(); memberIter != type.getStruct().end(); memberIter++)
447 maxBaseAlignment = de::max(maxBaseAlignment, computeStd140BaseAlignment(memberIter->getType()));
449 return deRoundUp32(maxBaseAlignment, vec4Alignment);
453 inline deUint32 mergeLayoutFlags (deUint32 prevFlags, deUint32 newFlags)
455 const deUint32 packingMask = LAYOUT_PACKED|LAYOUT_SHARED|LAYOUT_STD140;
456 const deUint32 matrixMask = LAYOUT_ROW_MAJOR|LAYOUT_COLUMN_MAJOR;
458 deUint32 mergedFlags = 0;
460 mergedFlags |= ((newFlags & packingMask) ? newFlags : prevFlags) & packingMask;
461 mergedFlags |= ((newFlags & matrixMask) ? newFlags : prevFlags) & matrixMask;
466 void computeStd140Layout (UniformLayout& layout, int& curOffset, int curBlockNdx, const std::string& curPrefix, const VarType& type, deUint32 layoutFlags)
468 int baseAlignment = computeStd140BaseAlignment(type);
470 curOffset = deAlign32(curOffset, baseAlignment);
472 if (type.isBasicType())
474 glu::DataType basicType = type.getBasicType();
475 UniformLayoutEntry entry;
477 entry.name = curPrefix;
478 entry.type = basicType;
480 entry.arrayStride = 0;
481 entry.matrixStride = 0;
482 entry.blockNdx = curBlockNdx;
484 if (glu::isDataTypeMatrix(basicType))
486 // Array of vectors as specified in rules 5 & 7.
487 bool isRowMajor = !!(layoutFlags & LAYOUT_ROW_MAJOR);
488 int vecSize = isRowMajor ? glu::getDataTypeMatrixNumColumns(basicType)
489 : glu::getDataTypeMatrixNumRows(basicType);
490 int numVecs = isRowMajor ? glu::getDataTypeMatrixNumRows(basicType)
491 : glu::getDataTypeMatrixNumColumns(basicType);
492 int stride = getDataTypeArrayStride(glu::getDataTypeFloatVec(vecSize));
494 entry.offset = curOffset;
495 entry.matrixStride = stride;
496 entry.isRowMajor = isRowMajor;
498 curOffset += numVecs*stride;
503 entry.offset = curOffset;
505 curOffset += getDataTypeByteSize(basicType);
508 layout.uniforms.push_back(entry);
510 else if (type.isArrayType())
512 const VarType& elemType = type.getElementType();
514 if (elemType.isBasicType() && !glu::isDataTypeMatrix(elemType.getBasicType()))
516 // Array of scalars or vectors.
517 glu::DataType elemBasicType = elemType.getBasicType();
518 UniformLayoutEntry entry;
519 int stride = getDataTypeArrayStride(elemBasicType);
521 entry.name = curPrefix + "[0]"; // Array uniforms are always postfixed with [0]
522 entry.type = elemBasicType;
523 entry.blockNdx = curBlockNdx;
524 entry.offset = curOffset;
525 entry.size = type.getArraySize();
526 entry.arrayStride = stride;
527 entry.matrixStride = 0;
529 curOffset += stride*type.getArraySize();
531 layout.uniforms.push_back(entry);
533 else if (elemType.isBasicType() && glu::isDataTypeMatrix(elemType.getBasicType()))
535 // Array of matrices.
536 glu::DataType elemBasicType = elemType.getBasicType();
537 bool isRowMajor = !!(layoutFlags & LAYOUT_ROW_MAJOR);
538 int vecSize = isRowMajor ? glu::getDataTypeMatrixNumColumns(elemBasicType)
539 : glu::getDataTypeMatrixNumRows(elemBasicType);
540 int numVecs = isRowMajor ? glu::getDataTypeMatrixNumRows(elemBasicType)
541 : glu::getDataTypeMatrixNumColumns(elemBasicType);
542 int stride = getDataTypeArrayStride(glu::getDataTypeFloatVec(vecSize));
543 UniformLayoutEntry entry;
545 entry.name = curPrefix + "[0]"; // Array uniforms are always postfixed with [0]
546 entry.type = elemBasicType;
547 entry.blockNdx = curBlockNdx;
548 entry.offset = curOffset;
549 entry.size = type.getArraySize();
550 entry.arrayStride = stride*numVecs;
551 entry.matrixStride = stride;
552 entry.isRowMajor = isRowMajor;
554 curOffset += numVecs*type.getArraySize()*stride;
556 layout.uniforms.push_back(entry);
560 DE_ASSERT(elemType.isStructType() || elemType.isArrayType());
562 for (int elemNdx = 0; elemNdx < type.getArraySize(); elemNdx++)
563 computeStd140Layout(layout, curOffset, curBlockNdx, curPrefix + "[" + de::toString(elemNdx) + "]", type.getElementType(), layoutFlags);
568 DE_ASSERT(type.isStructType());
570 for (StructType::ConstIterator memberIter = type.getStruct().begin(); memberIter != type.getStruct().end(); memberIter++)
571 computeStd140Layout(layout, curOffset, curBlockNdx, curPrefix + "." + memberIter->getName(), memberIter->getType(), layoutFlags);
573 curOffset = deAlign32(curOffset, baseAlignment);
577 void computeStd140Layout (UniformLayout& layout, const ShaderInterface& interface)
579 // \todo [2012-01-23 pyry] Uniforms in default block.
581 int numUniformBlocks = interface.getNumUniformBlocks();
583 for (int blockNdx = 0; blockNdx < numUniformBlocks; blockNdx++)
585 const UniformBlock& block = interface.getUniformBlock(blockNdx);
586 bool hasInstanceName = block.getInstanceName() != DE_NULL;
587 std::string blockPrefix = hasInstanceName ? (std::string(block.getBlockName()) + ".") : std::string("");
589 int activeBlockNdx = (int)layout.blocks.size();
590 int firstUniformNdx = (int)layout.uniforms.size();
592 for (UniformBlock::ConstIterator uniformIter = block.begin(); uniformIter != block.end(); uniformIter++)
594 const Uniform& uniform = *uniformIter;
595 computeStd140Layout(layout, curOffset, activeBlockNdx, blockPrefix + uniform.getName(), uniform.getType(), mergeLayoutFlags(block.getFlags(), uniform.getFlags()));
598 int uniformIndicesEnd = (int)layout.uniforms.size();
599 int blockSize = curOffset;
600 int numInstances = block.isArray() ? block.getArraySize() : 1;
602 // Create block layout entries for each instance.
603 for (int instanceNdx = 0; instanceNdx < numInstances; instanceNdx++)
605 // Allocate entry for instance.
606 layout.blocks.push_back(BlockLayoutEntry());
607 BlockLayoutEntry& blockEntry = layout.blocks.back();
609 blockEntry.name = block.getBlockName();
610 blockEntry.size = blockSize;
612 // Compute active uniform set for block.
613 for (int uniformNdx = firstUniformNdx; uniformNdx < uniformIndicesEnd; uniformNdx++)
614 blockEntry.activeUniformIndices.push_back(uniformNdx);
617 blockEntry.name += "[" + de::toString(instanceNdx) + "]";
624 void generateValue (const UniformLayoutEntry& entry, void* basePtr, de::Random& rnd)
626 glu::DataType scalarType = glu::getDataTypeScalarType(entry.type);
627 int scalarSize = glu::getDataTypeScalarSize(entry.type);
628 bool isMatrix = glu::isDataTypeMatrix(entry.type);
629 int numVecs = isMatrix ? (entry.isRowMajor ? glu::getDataTypeMatrixNumRows(entry.type) : glu::getDataTypeMatrixNumColumns(entry.type)) : 1;
630 int vecSize = scalarSize / numVecs;
631 bool isArray = entry.size > 1;
632 const int compSize = sizeof(deUint32);
634 DE_ASSERT(scalarSize%numVecs == 0);
636 for (int elemNdx = 0; elemNdx < entry.size; elemNdx++)
638 deUint8* elemPtr = (deUint8*)basePtr + entry.offset + (isArray ? elemNdx*entry.arrayStride : 0);
640 for (int vecNdx = 0; vecNdx < numVecs; vecNdx++)
642 deUint8* vecPtr = elemPtr + (isMatrix ? vecNdx*entry.matrixStride : 0);
644 for (int compNdx = 0; compNdx < vecSize; compNdx++)
646 deUint8* compPtr = vecPtr + compSize*compNdx;
650 case glu::TYPE_FLOAT: *((float*)compPtr) = (float)rnd.getInt(-9, 9); break;
651 case glu::TYPE_INT: *((int*)compPtr) = rnd.getInt(-9, 9); break;
652 case glu::TYPE_UINT: *((deUint32*)compPtr) = (deUint32)rnd.getInt(0, 9); break;
653 // \note Random bit pattern is used for true values. Spec states that all non-zero values are
654 // interpreted as true but some implementations fail this.
655 case glu::TYPE_BOOL: *((deUint32*)compPtr) = rnd.getBool() ? rnd.getUint32()|1u : 0u; break;
664 void generateValues (const UniformLayout& layout, const std::map<int, void*>& blockPointers, deUint32 seed)
666 de::Random rnd (seed);
667 int numBlocks = (int)layout.blocks.size();
669 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
671 void* basePtr = blockPointers.find(blockNdx)->second;
672 int numEntries = (int)layout.blocks[blockNdx].activeUniformIndices.size();
674 for (int entryNdx = 0; entryNdx < numEntries; entryNdx++)
676 const UniformLayoutEntry& entry = layout.uniforms[layout.blocks[blockNdx].activeUniformIndices[entryNdx]];
677 generateValue(entry, basePtr, rnd);
684 const char* getCompareFuncForType (glu::DataType type)
688 case glu::TYPE_FLOAT: return "mediump float compare_float (highp float a, highp float b) { return abs(a - b) < 0.05 ? 1.0 : 0.0; }\n";
689 case glu::TYPE_FLOAT_VEC2: return "mediump float compare_vec2 (highp vec2 a, highp vec2 b) { return compare_float(a.x, b.x)*compare_float(a.y, b.y); }\n";
690 case glu::TYPE_FLOAT_VEC3: return "mediump float compare_vec3 (highp vec3 a, highp vec3 b) { return compare_float(a.x, b.x)*compare_float(a.y, b.y)*compare_float(a.z, b.z); }\n";
691 case glu::TYPE_FLOAT_VEC4: return "mediump float compare_vec4 (highp vec4 a, highp vec4 b) { return compare_float(a.x, b.x)*compare_float(a.y, b.y)*compare_float(a.z, b.z)*compare_float(a.w, b.w); }\n";
692 case glu::TYPE_FLOAT_MAT2: return "mediump float compare_mat2 (highp mat2 a, highp mat2 b) { return compare_vec2(a[0], b[0])*compare_vec2(a[1], b[1]); }\n";
693 case glu::TYPE_FLOAT_MAT2X3: return "mediump float compare_mat2x3 (highp mat2x3 a, highp mat2x3 b){ return compare_vec3(a[0], b[0])*compare_vec3(a[1], b[1]); }\n";
694 case glu::TYPE_FLOAT_MAT2X4: return "mediump float compare_mat2x4 (highp mat2x4 a, highp mat2x4 b){ return compare_vec4(a[0], b[0])*compare_vec4(a[1], b[1]); }\n";
695 case glu::TYPE_FLOAT_MAT3X2: return "mediump float compare_mat3x2 (highp mat3x2 a, highp mat3x2 b){ return compare_vec2(a[0], b[0])*compare_vec2(a[1], b[1])*compare_vec2(a[2], b[2]); }\n";
696 case glu::TYPE_FLOAT_MAT3: return "mediump float compare_mat3 (highp mat3 a, highp mat3 b) { return compare_vec3(a[0], b[0])*compare_vec3(a[1], b[1])*compare_vec3(a[2], b[2]); }\n";
697 case glu::TYPE_FLOAT_MAT3X4: return "mediump float compare_mat3x4 (highp mat3x4 a, highp mat3x4 b){ return compare_vec4(a[0], b[0])*compare_vec4(a[1], b[1])*compare_vec4(a[2], b[2]); }\n";
698 case glu::TYPE_FLOAT_MAT4X2: return "mediump float compare_mat4x2 (highp mat4x2 a, highp mat4x2 b){ return compare_vec2(a[0], b[0])*compare_vec2(a[1], b[1])*compare_vec2(a[2], b[2])*compare_vec2(a[3], b[3]); }\n";
699 case glu::TYPE_FLOAT_MAT4X3: return "mediump float compare_mat4x3 (highp mat4x3 a, highp mat4x3 b){ return compare_vec3(a[0], b[0])*compare_vec3(a[1], b[1])*compare_vec3(a[2], b[2])*compare_vec3(a[3], b[3]); }\n";
700 case glu::TYPE_FLOAT_MAT4: return "mediump float compare_mat4 (highp mat4 a, highp mat4 b) { return compare_vec4(a[0], b[0])*compare_vec4(a[1], b[1])*compare_vec4(a[2], b[2])*compare_vec4(a[3], b[3]); }\n";
701 case glu::TYPE_INT: return "mediump float compare_int (highp int a, highp int b) { return a == b ? 1.0 : 0.0; }\n";
702 case glu::TYPE_INT_VEC2: return "mediump float compare_ivec2 (highp ivec2 a, highp ivec2 b) { return a == b ? 1.0 : 0.0; }\n";
703 case glu::TYPE_INT_VEC3: return "mediump float compare_ivec3 (highp ivec3 a, highp ivec3 b) { return a == b ? 1.0 : 0.0; }\n";
704 case glu::TYPE_INT_VEC4: return "mediump float compare_ivec4 (highp ivec4 a, highp ivec4 b) { return a == b ? 1.0 : 0.0; }\n";
705 case glu::TYPE_UINT: return "mediump float compare_uint (highp uint a, highp uint b) { return a == b ? 1.0 : 0.0; }\n";
706 case glu::TYPE_UINT_VEC2: return "mediump float compare_uvec2 (highp uvec2 a, highp uvec2 b) { return a == b ? 1.0 : 0.0; }\n";
707 case glu::TYPE_UINT_VEC3: return "mediump float compare_uvec3 (highp uvec3 a, highp uvec3 b) { return a == b ? 1.0 : 0.0; }\n";
708 case glu::TYPE_UINT_VEC4: return "mediump float compare_uvec4 (highp uvec4 a, highp uvec4 b) { return a == b ? 1.0 : 0.0; }\n";
709 case glu::TYPE_BOOL: return "mediump float compare_bool (bool a, bool b) { return a == b ? 1.0 : 0.0; }\n";
710 case glu::TYPE_BOOL_VEC2: return "mediump float compare_bvec2 (bvec2 a, bvec2 b) { return a == b ? 1.0 : 0.0; }\n";
711 case glu::TYPE_BOOL_VEC3: return "mediump float compare_bvec3 (bvec3 a, bvec3 b) { return a == b ? 1.0 : 0.0; }\n";
712 case glu::TYPE_BOOL_VEC4: return "mediump float compare_bvec4 (bvec4 a, bvec4 b) { return a == b ? 1.0 : 0.0; }\n";
719 void getCompareDependencies (std::set<glu::DataType>& compareFuncs, glu::DataType basicType)
723 case glu::TYPE_FLOAT_VEC2:
724 case glu::TYPE_FLOAT_VEC3:
725 case glu::TYPE_FLOAT_VEC4:
726 compareFuncs.insert(glu::TYPE_FLOAT);
727 compareFuncs.insert(basicType);
730 case glu::TYPE_FLOAT_MAT2:
731 case glu::TYPE_FLOAT_MAT2X3:
732 case glu::TYPE_FLOAT_MAT2X4:
733 case glu::TYPE_FLOAT_MAT3X2:
734 case glu::TYPE_FLOAT_MAT3:
735 case glu::TYPE_FLOAT_MAT3X4:
736 case glu::TYPE_FLOAT_MAT4X2:
737 case glu::TYPE_FLOAT_MAT4X3:
738 case glu::TYPE_FLOAT_MAT4:
739 compareFuncs.insert(glu::TYPE_FLOAT);
740 compareFuncs.insert(glu::getDataTypeFloatVec(glu::getDataTypeMatrixNumRows(basicType)));
741 compareFuncs.insert(basicType);
745 compareFuncs.insert(basicType);
750 void collectUniqueBasicTypes (std::set<glu::DataType>& basicTypes, const VarType& type)
752 if (type.isStructType())
754 for (StructType::ConstIterator iter = type.getStruct().begin(); iter != type.getStruct().end(); ++iter)
755 collectUniqueBasicTypes(basicTypes, iter->getType());
757 else if (type.isArrayType())
758 collectUniqueBasicTypes(basicTypes, type.getElementType());
761 DE_ASSERT(type.isBasicType());
762 basicTypes.insert(type.getBasicType());
766 void collectUniqueBasicTypes (std::set<glu::DataType>& basicTypes, const UniformBlock& uniformBlock)
768 for (UniformBlock::ConstIterator iter = uniformBlock.begin(); iter != uniformBlock.end(); ++iter)
769 collectUniqueBasicTypes(basicTypes, iter->getType());
772 void collectUniqueBasicTypes (std::set<glu::DataType>& basicTypes, const ShaderInterface& interface)
774 for (int ndx = 0; ndx < interface.getNumUniformBlocks(); ++ndx)
775 collectUniqueBasicTypes(basicTypes, interface.getUniformBlock(ndx));
778 void generateCompareFuncs (std::ostream& str, const ShaderInterface& interface)
780 std::set<glu::DataType> types;
781 std::set<glu::DataType> compareFuncs;
783 // Collect unique basic types
784 collectUniqueBasicTypes(types, interface);
786 // Set of compare functions required
787 for (std::set<glu::DataType>::const_iterator iter = types.begin(); iter != types.end(); ++iter)
789 getCompareDependencies(compareFuncs, *iter);
792 for (int type = 0; type < glu::TYPE_LAST; ++type)
794 if (compareFuncs.find(glu::DataType(type)) != compareFuncs.end())
795 str << getCompareFuncForType(glu::DataType(type));
802 Indent (int level_) : level(level_) {}
805 std::ostream& operator<< (std::ostream& str, const Indent& indent)
807 for (int i = 0; i < indent.level; i++)
812 void generateDeclaration (std::ostringstream& src, const VarType& type, const char* name, int indentLevel, deUint32 unusedHints);
813 void generateDeclaration (std::ostringstream& src, const Uniform& uniform, int indentLevel);
814 void generateDeclaration (std::ostringstream& src, const StructType& structType, int indentLevel);
816 void generateLocalDeclaration (std::ostringstream& src, const StructType& structType, int indentLevel);
817 void generateFullDeclaration (std::ostringstream& src, const StructType& structType, int indentLevel);
819 void generateDeclaration (std::ostringstream& src, const StructType& structType, int indentLevel)
821 DE_ASSERT(structType.getTypeName() != DE_NULL);
822 generateFullDeclaration(src, structType, indentLevel);
826 void generateFullDeclaration (std::ostringstream& src, const StructType& structType, int indentLevel)
829 if (structType.getTypeName())
830 src << " " << structType.getTypeName();
831 src << "\n" << Indent(indentLevel) << "{\n";
833 for (StructType::ConstIterator memberIter = structType.begin(); memberIter != structType.end(); memberIter++)
835 src << Indent(indentLevel+1);
836 generateDeclaration(src, memberIter->getType(), memberIter->getName(), indentLevel+1, memberIter->getFlags() & UNUSED_BOTH);
839 src << Indent(indentLevel) << "}";
842 void generateLocalDeclaration (std::ostringstream& src, const StructType& structType, int indentLevel)
844 if (structType.getTypeName() == DE_NULL)
845 generateFullDeclaration(src, structType, indentLevel);
847 src << structType.getTypeName();
850 void generateDeclaration (std::ostringstream& src, const VarType& type, const char* name, int indentLevel, deUint32 unusedHints)
852 deUint32 flags = type.getFlags();
854 if ((flags & LAYOUT_MASK) != 0)
855 src << "layout(" << LayoutFlagsFmt(flags & LAYOUT_MASK) << ") ";
857 if ((flags & PRECISION_MASK) != 0)
858 src << PrecisionFlagsFmt(flags & PRECISION_MASK) << " ";
860 if (type.isBasicType())
861 src << glu::getDataTypeName(type.getBasicType()) << " " << name;
862 else if (type.isArrayType())
864 std::vector<int> arraySizes;
865 const VarType* curType = &type;
866 while (curType->isArrayType())
868 arraySizes.push_back(curType->getArraySize());
869 curType = &curType->getElementType();
872 if (curType->isBasicType())
874 if ((curType->getFlags() & PRECISION_MASK) != 0)
875 src << PrecisionFlagsFmt(curType->getFlags() & PRECISION_MASK) << " ";
876 src << glu::getDataTypeName(curType->getBasicType());
880 DE_ASSERT(curType->isStructType());
881 generateLocalDeclaration(src, curType->getStruct(), indentLevel+1);
886 for (std::vector<int>::const_iterator sizeIter = arraySizes.begin(); sizeIter != arraySizes.end(); sizeIter++)
887 src << "[" << *sizeIter << "]";
891 generateLocalDeclaration(src, type.getStruct(), indentLevel+1);
897 // Print out unused hints.
898 if (unusedHints != 0)
899 src << " // unused in " << (unusedHints == UNUSED_BOTH ? "both shaders" :
900 unusedHints == UNUSED_VERTEX ? "vertex shader" :
901 unusedHints == UNUSED_FRAGMENT ? "fragment shader" : "???");
906 void generateDeclaration (std::ostringstream& src, const Uniform& uniform, int indentLevel)
908 if ((uniform.getFlags() & LAYOUT_MASK) != 0)
909 src << "layout(" << LayoutFlagsFmt(uniform.getFlags() & LAYOUT_MASK) << ") ";
911 generateDeclaration(src, uniform.getType(), uniform.getName(), indentLevel, uniform.getFlags() & UNUSED_BOTH);
914 void generateDeclaration (std::ostringstream& src, const UniformBlock& block)
916 if ((block.getFlags() & LAYOUT_MASK) != 0)
917 src << "layout(" << LayoutFlagsFmt(block.getFlags() & LAYOUT_MASK) << ") ";
919 src << "uniform " << block.getBlockName();
922 for (UniformBlock::ConstIterator uniformIter = block.begin(); uniformIter != block.end(); uniformIter++)
925 generateDeclaration(src, *uniformIter, 1 /* indent level */);
930 if (block.getInstanceName() != DE_NULL)
932 src << " " << block.getInstanceName();
934 src << "[" << block.getArraySize() << "]";
937 DE_ASSERT(!block.isArray());
942 void generateValueSrc (std::ostringstream& src, const UniformLayoutEntry& entry, const void* basePtr, int elementNdx)
944 glu::DataType scalarType = glu::getDataTypeScalarType(entry.type);
945 int scalarSize = glu::getDataTypeScalarSize(entry.type);
946 bool isArray = entry.size > 1;
947 const deUint8* elemPtr = (const deUint8*)basePtr + entry.offset + (isArray ? elementNdx*entry.arrayStride : 0);
948 const int compSize = sizeof(deUint32);
951 src << glu::getDataTypeName(entry.type) << "(";
953 if (glu::isDataTypeMatrix(entry.type))
955 int numRows = glu::getDataTypeMatrixNumRows(entry.type);
956 int numCols = glu::getDataTypeMatrixNumColumns(entry.type);
958 DE_ASSERT(scalarType == glu::TYPE_FLOAT);
960 // Constructed in column-wise order.
961 for (int colNdx = 0; colNdx < numCols; colNdx++)
963 for (int rowNdx = 0; rowNdx < numRows; rowNdx++)
965 const deUint8* compPtr = elemPtr + (entry.isRowMajor ? rowNdx*entry.matrixStride + colNdx*compSize
966 : colNdx*entry.matrixStride + rowNdx*compSize);
968 if (colNdx > 0 || rowNdx > 0)
971 src << de::floatToString(*((const float*)compPtr), 1);
977 for (int scalarNdx = 0; scalarNdx < scalarSize; scalarNdx++)
979 const deUint8* compPtr = elemPtr + scalarNdx*compSize;
986 case glu::TYPE_FLOAT: src << de::floatToString(*((const float*)compPtr), 1); break;
987 case glu::TYPE_INT: src << *((const int*)compPtr); break;
988 case glu::TYPE_UINT: src << *((const deUint32*)compPtr) << "u"; break;
989 case glu::TYPE_BOOL: src << (*((const deUint32*)compPtr) != 0u ? "true" : "false"); break;
1000 void generateCompareSrc (std::ostringstream& src, const char* resultVar, const VarType& type, const char* srcName, const char* apiName, const UniformLayout& layout, const void* basePtr, deUint32 unusedMask)
1002 if (type.isBasicType() || (type.isArrayType() && type.getElementType().isBasicType()))
1004 // Basic type or array of basic types.
1005 bool isArray = type.isArrayType();
1006 glu::DataType elementType = isArray ? type.getElementType().getBasicType() : type.getBasicType();
1007 const char* typeName = glu::getDataTypeName(elementType);
1008 std::string fullApiName = string(apiName) + (isArray ? "[0]" : ""); // Arrays are always postfixed with [0]
1009 int uniformNdx = layout.getUniformIndex(fullApiName.c_str());
1010 const UniformLayoutEntry& entry = layout.uniforms[uniformNdx];
1014 for (int elemNdx = 0; elemNdx < type.getArraySize(); elemNdx++)
1016 src << "\tresult *= compare_" << typeName << "(" << srcName << "[" << elemNdx << "], ";
1017 generateValueSrc(src, entry, basePtr, elemNdx);
1023 src << "\tresult *= compare_" << typeName << "(" << srcName << ", ";
1024 generateValueSrc(src, entry, basePtr, 0);
1028 else if (type.isArrayType())
1030 const VarType& elementType = type.getElementType();
1032 for (int elementNdx = 0; elementNdx < type.getArraySize(); elementNdx++)
1034 std::string op = string("[") + de::toString(elementNdx) + "]";
1035 generateCompareSrc(src, resultVar, elementType, (string(srcName) + op).c_str(), (string(apiName) + op).c_str(), layout, basePtr, unusedMask);
1040 DE_ASSERT(type.isStructType());
1042 for (StructType::ConstIterator memberIter = type.getStruct().begin(); memberIter != type.getStruct().end(); memberIter++)
1044 if (memberIter->getFlags() & unusedMask)
1045 continue; // Skip member.
1047 string op = string(".") + memberIter->getName();
1048 generateCompareSrc(src, resultVar, memberIter->getType(), (string(srcName) + op).c_str(), (string(apiName) + op).c_str(), layout, basePtr, unusedMask);
1053 void generateCompareSrc (std::ostringstream& src, const char* resultVar, const ShaderInterface& interface, const UniformLayout& layout, const std::map<int, void*>& blockPointers, bool isVertex)
1055 deUint32 unusedMask = isVertex ? UNUSED_VERTEX : UNUSED_FRAGMENT;
1057 for (int blockNdx = 0; blockNdx < interface.getNumUniformBlocks(); blockNdx++)
1059 const UniformBlock& block = interface.getUniformBlock(blockNdx);
1061 if ((block.getFlags() & (isVertex ? DECLARE_VERTEX : DECLARE_FRAGMENT)) == 0)
1064 bool hasInstanceName = block.getInstanceName() != DE_NULL;
1065 bool isArray = block.isArray();
1066 int numInstances = isArray ? block.getArraySize() : 1;
1067 std::string apiPrefix = hasInstanceName ? string(block.getBlockName()) + "." : string("");
1069 DE_ASSERT(!isArray || hasInstanceName);
1071 for (int instanceNdx = 0; instanceNdx < numInstances; instanceNdx++)
1073 std::string instancePostfix = isArray ? string("[") + de::toString(instanceNdx) + "]" : string("");
1074 std::string blockInstanceName = block.getBlockName() + instancePostfix;
1075 std::string srcPrefix = hasInstanceName ? string(block.getInstanceName()) + instancePostfix + "." : string("");
1076 int activeBlockNdx = layout.getBlockIndex(blockInstanceName.c_str());
1077 void* basePtr = blockPointers.find(activeBlockNdx)->second;
1079 for (UniformBlock::ConstIterator uniformIter = block.begin(); uniformIter != block.end(); uniformIter++)
1081 const Uniform& uniform = *uniformIter;
1083 if (uniform.getFlags() & unusedMask)
1084 continue; // Don't read from that uniform.
1086 generateCompareSrc(src, resultVar, uniform.getType(), (srcPrefix + uniform.getName()).c_str(), (apiPrefix + uniform.getName()).c_str(), layout, basePtr, unusedMask);
1092 void generateVertexShader (std::ostringstream& src, glu::GLSLVersion glslVersion, const ShaderInterface& interface, const UniformLayout& layout, const std::map<int, void*>& blockPointers)
1094 DE_ASSERT(isSupportedGLSLVersion(glslVersion));
1096 src << glu::getGLSLVersionDeclaration(glslVersion) << "\n";
1097 src << "in highp vec4 a_position;\n";
1098 src << "out mediump float v_vtxResult;\n";
1101 std::vector<const StructType*> namedStructs;
1102 interface.getNamedStructs(namedStructs);
1103 for (std::vector<const StructType*>::const_iterator structIter = namedStructs.begin(); structIter != namedStructs.end(); structIter++)
1104 generateDeclaration(src, **structIter, 0);
1106 for (int blockNdx = 0; blockNdx < interface.getNumUniformBlocks(); blockNdx++)
1108 const UniformBlock& block = interface.getUniformBlock(blockNdx);
1109 if (block.getFlags() & DECLARE_VERTEX)
1110 generateDeclaration(src, block);
1113 // Comparison utilities.
1115 generateCompareFuncs(src, interface);
1118 "void main (void)\n"
1120 " gl_Position = a_position;\n"
1121 " mediump float result = 1.0;\n";
1124 generateCompareSrc(src, "result", interface, layout, blockPointers, true);
1126 src << " v_vtxResult = result;\n"
1130 void generateFragmentShader (std::ostringstream& src, glu::GLSLVersion glslVersion, const ShaderInterface& interface, const UniformLayout& layout, const std::map<int, void*>& blockPointers)
1132 DE_ASSERT(isSupportedGLSLVersion(glslVersion));
1134 src << glu::getGLSLVersionDeclaration(glslVersion) << "\n";
1135 src << "in mediump float v_vtxResult;\n";
1136 src << "layout(location = 0) out mediump vec4 dEQP_FragColor;\n";
1139 std::vector<const StructType*> namedStructs;
1140 interface.getNamedStructs(namedStructs);
1141 for (std::vector<const StructType*>::const_iterator structIter = namedStructs.begin(); structIter != namedStructs.end(); structIter++)
1142 generateDeclaration(src, **structIter, 0);
1144 for (int blockNdx = 0; blockNdx < interface.getNumUniformBlocks(); blockNdx++)
1146 const UniformBlock& block = interface.getUniformBlock(blockNdx);
1147 if (block.getFlags() & DECLARE_FRAGMENT)
1148 generateDeclaration(src, block);
1151 // Comparison utilities.
1153 generateCompareFuncs(src, interface);
1156 "void main (void)\n"
1158 " mediump float result = 1.0;\n";
1161 generateCompareSrc(src, "result", interface, layout, blockPointers, false);
1163 src << " dEQP_FragColor = vec4(1.0, v_vtxResult, result, 1.0);\n"
1167 void getGLUniformLayout (const glw::Functions& gl, UniformLayout& layout, deUint32 program)
1169 int numActiveUniforms = 0;
1170 int numActiveBlocks = 0;
1172 gl.getProgramiv(program, GL_ACTIVE_UNIFORMS, &numActiveUniforms);
1173 gl.getProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &numActiveBlocks);
1175 GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to get number of uniforms and uniform blocks");
1178 layout.blocks.resize(numActiveBlocks);
1179 for (int blockNdx = 0; blockNdx < numActiveBlocks; blockNdx++)
1181 BlockLayoutEntry& entry = layout.blocks[blockNdx];
1184 int numBlockUniforms;
1186 gl.getActiveUniformBlockiv(program, (deUint32)blockNdx, GL_UNIFORM_BLOCK_DATA_SIZE, &size);
1187 gl.getActiveUniformBlockiv(program, (deUint32)blockNdx, GL_UNIFORM_BLOCK_NAME_LENGTH, &nameLen);
1188 gl.getActiveUniformBlockiv(program, (deUint32)blockNdx, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &numBlockUniforms);
1190 GLU_EXPECT_NO_ERROR(gl.getError(), "Uniform block query failed");
1192 // \note Some implementations incorrectly return 0 as name length even though the length should include null terminator.
1193 std::vector<char> nameBuf(nameLen > 0 ? nameLen : 1);
1194 gl.getActiveUniformBlockName(program, (deUint32)blockNdx, (glw::GLsizei)nameBuf.size(), DE_NULL, &nameBuf[0]);
1196 entry.name = std::string(&nameBuf[0]);
1198 entry.activeUniformIndices.resize(numBlockUniforms);
1200 if (numBlockUniforms > 0)
1201 gl.getActiveUniformBlockiv(program, (deUint32)blockNdx, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &entry.activeUniformIndices[0]);
1203 GLU_EXPECT_NO_ERROR(gl.getError(), "Uniform block query failed");
1206 if (numActiveUniforms > 0)
1209 std::vector<deUint32> uniformIndices(numActiveUniforms);
1210 for (int i = 0; i < numActiveUniforms; i++)
1211 uniformIndices[i] = (deUint32)i;
1213 std::vector<int> types (numActiveUniforms);
1214 std::vector<int> sizes (numActiveUniforms);
1215 std::vector<int> nameLengths (numActiveUniforms);
1216 std::vector<int> blockIndices (numActiveUniforms);
1217 std::vector<int> offsets (numActiveUniforms);
1218 std::vector<int> arrayStrides (numActiveUniforms);
1219 std::vector<int> matrixStrides (numActiveUniforms);
1220 std::vector<int> rowMajorFlags (numActiveUniforms);
1223 gl.getActiveUniformsiv(program, (glw::GLsizei)uniformIndices.size(), &uniformIndices[0], GL_UNIFORM_TYPE, &types[0]);
1224 gl.getActiveUniformsiv(program, (glw::GLsizei)uniformIndices.size(), &uniformIndices[0], GL_UNIFORM_SIZE, &sizes[0]);
1225 gl.getActiveUniformsiv(program, (glw::GLsizei)uniformIndices.size(), &uniformIndices[0], GL_UNIFORM_NAME_LENGTH, &nameLengths[0]);
1226 gl.getActiveUniformsiv(program, (glw::GLsizei)uniformIndices.size(), &uniformIndices[0], GL_UNIFORM_BLOCK_INDEX, &blockIndices[0]);
1227 gl.getActiveUniformsiv(program, (glw::GLsizei)uniformIndices.size(), &uniformIndices[0], GL_UNIFORM_OFFSET, &offsets[0]);
1228 gl.getActiveUniformsiv(program, (glw::GLsizei)uniformIndices.size(), &uniformIndices[0], GL_UNIFORM_ARRAY_STRIDE, &arrayStrides[0]);
1229 gl.getActiveUniformsiv(program, (glw::GLsizei)uniformIndices.size(), &uniformIndices[0], GL_UNIFORM_MATRIX_STRIDE, &matrixStrides[0]);
1230 gl.getActiveUniformsiv(program, (glw::GLsizei)uniformIndices.size(), &uniformIndices[0], GL_UNIFORM_IS_ROW_MAJOR, &rowMajorFlags[0]);
1232 GLU_EXPECT_NO_ERROR(gl.getError(), "Active uniform query failed");
1234 // Translate to LayoutEntries
1235 layout.uniforms.resize(numActiveUniforms);
1236 for (int uniformNdx = 0; uniformNdx < numActiveUniforms; uniformNdx++)
1238 UniformLayoutEntry& entry = layout.uniforms[uniformNdx];
1239 std::vector<char> nameBuf (nameLengths[uniformNdx]);
1240 glw::GLsizei nameLen = 0;
1242 deUint32 type = GL_NONE;
1244 gl.getActiveUniform(program, (deUint32)uniformNdx, (glw::GLsizei)nameBuf.size(), &nameLen, &size, &type, &nameBuf[0]);
1246 GLU_EXPECT_NO_ERROR(gl.getError(), "Uniform name query failed");
1248 // \note glGetActiveUniform() returns length without \0 and glGetActiveUniformsiv() with \0
1249 if (nameLen+1 != nameLengths[uniformNdx] ||
1250 size != sizes[uniformNdx] ||
1251 type != (deUint32)types[uniformNdx])
1252 TCU_FAIL("Values returned by glGetActiveUniform() don't match with values queried with glGetActiveUniformsiv().");
1254 entry.name = std::string(&nameBuf[0]);
1255 entry.type = glu::getDataTypeFromGLType(types[uniformNdx]);
1256 entry.size = sizes[uniformNdx];
1257 entry.blockNdx = blockIndices[uniformNdx];
1258 entry.offset = offsets[uniformNdx];
1259 entry.arrayStride = arrayStrides[uniformNdx];
1260 entry.matrixStride = matrixStrides[uniformNdx];
1261 entry.isRowMajor = rowMajorFlags[uniformNdx] != GL_FALSE;
1266 void copyUniformData (const UniformLayoutEntry& dstEntry, void* dstBlockPtr, const UniformLayoutEntry& srcEntry, const void* srcBlockPtr)
1268 deUint8* dstBasePtr = (deUint8*)dstBlockPtr + dstEntry.offset;
1269 const deUint8* srcBasePtr = (const deUint8*)srcBlockPtr + srcEntry.offset;
1271 DE_ASSERT(dstEntry.size <= srcEntry.size);
1272 DE_ASSERT(dstEntry.type == srcEntry.type);
1274 int scalarSize = glu::getDataTypeScalarSize(dstEntry.type);
1275 bool isMatrix = glu::isDataTypeMatrix(dstEntry.type);
1276 const int compSize = sizeof(deUint32);
1278 for (int elementNdx = 0; elementNdx < dstEntry.size; elementNdx++)
1280 deUint8* dstElemPtr = dstBasePtr + elementNdx*dstEntry.arrayStride;
1281 const deUint8* srcElemPtr = srcBasePtr + elementNdx*srcEntry.arrayStride;
1285 int numRows = glu::getDataTypeMatrixNumRows(dstEntry.type);
1286 int numCols = glu::getDataTypeMatrixNumColumns(dstEntry.type);
1288 for (int colNdx = 0; colNdx < numCols; colNdx++)
1290 for (int rowNdx = 0; rowNdx < numRows; rowNdx++)
1292 deUint8* dstCompPtr = dstElemPtr + (dstEntry.isRowMajor ? rowNdx*dstEntry.matrixStride + colNdx*compSize
1293 : colNdx*dstEntry.matrixStride + rowNdx*compSize);
1294 const deUint8* srcCompPtr = srcElemPtr + (srcEntry.isRowMajor ? rowNdx*srcEntry.matrixStride + colNdx*compSize
1295 : colNdx*srcEntry.matrixStride + rowNdx*compSize);
1296 deMemcpy(dstCompPtr, srcCompPtr, compSize);
1301 deMemcpy(dstElemPtr, srcElemPtr, scalarSize*compSize);
1305 void copyUniformData (const UniformLayout& dstLayout, const std::map<int, void*>& dstBlockPointers, const UniformLayout& srcLayout, const std::map<int, void*>& srcBlockPointers)
1307 // \note Src layout is used as reference in case of activeUniforms happens to be incorrect in dstLayout blocks.
1308 int numBlocks = (int)srcLayout.blocks.size();
1310 for (int srcBlockNdx = 0; srcBlockNdx < numBlocks; srcBlockNdx++)
1312 const BlockLayoutEntry& srcBlock = srcLayout.blocks[srcBlockNdx];
1313 const void* srcBlockPtr = srcBlockPointers.find(srcBlockNdx)->second;
1314 int dstBlockNdx = dstLayout.getBlockIndex(srcBlock.name.c_str());
1315 void* dstBlockPtr = dstBlockNdx >= 0 ? dstBlockPointers.find(dstBlockNdx)->second : DE_NULL;
1317 if (dstBlockNdx < 0)
1320 for (vector<int>::const_iterator srcUniformNdxIter = srcBlock.activeUniformIndices.begin(); srcUniformNdxIter != srcBlock.activeUniformIndices.end(); srcUniformNdxIter++)
1322 const UniformLayoutEntry& srcEntry = srcLayout.uniforms[*srcUniformNdxIter];
1323 int dstUniformNdx = dstLayout.getUniformIndex(srcEntry.name.c_str());
1325 if (dstUniformNdx < 0)
1328 copyUniformData(dstLayout.uniforms[dstUniformNdx], dstBlockPtr, srcEntry, srcBlockPtr);
1333 } // anonymous (utilities)
1335 class UniformBufferManager
1338 UniformBufferManager (const glu::RenderContext& renderCtx);
1339 ~UniformBufferManager (void);
1341 deUint32 allocBuffer (void);
1344 UniformBufferManager (const UniformBufferManager& other);
1345 UniformBufferManager& operator= (const UniformBufferManager& other);
1347 const glu::RenderContext& m_renderCtx;
1348 std::vector<deUint32> m_buffers;
1351 UniformBufferManager::UniformBufferManager (const glu::RenderContext& renderCtx)
1352 : m_renderCtx(renderCtx)
1356 UniformBufferManager::~UniformBufferManager (void)
1358 if (!m_buffers.empty())
1359 m_renderCtx.getFunctions().deleteBuffers((glw::GLsizei)m_buffers.size(), &m_buffers[0]);
1362 deUint32 UniformBufferManager::allocBuffer (void)
1366 m_buffers.reserve(m_buffers.size()+1);
1367 m_renderCtx.getFunctions().genBuffers(1, &buf);
1368 GLU_EXPECT_NO_ERROR(m_renderCtx.getFunctions().getError(), "Failed to allocate uniform buffer");
1369 m_buffers.push_back(buf);
1378 // UniformBlockCase.
1380 UniformBlockCase::UniformBlockCase (tcu::TestContext& testCtx, glu::RenderContext& renderCtx, const char* name, const char* description, glu::GLSLVersion glslVersion, BufferMode bufferMode)
1381 : TestCase (testCtx, name, description)
1382 , m_renderCtx (renderCtx)
1383 , m_glslVersion (glslVersion)
1384 , m_bufferMode (bufferMode)
1386 TCU_CHECK_INTERNAL(isSupportedGLSLVersion(glslVersion));
1389 UniformBlockCase::~UniformBlockCase (void)
1393 UniformBlockCase::IterateResult UniformBlockCase::iterate (void)
1395 TestLog& log = m_testCtx.getLog();
1396 const glw::Functions& gl = m_renderCtx.getFunctions();
1397 UniformLayout refLayout; //!< std140 layout.
1398 vector<deUint8> data; //!< Data.
1399 map<int, void*> blockPointers; //!< Reference block pointers.
1401 // Initialize result to pass.
1402 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, "Pass");
1404 // Compute reference layout.
1405 computeStd140Layout(refLayout, m_interface);
1407 // Assign storage for reference values.
1410 for (vector<BlockLayoutEntry>::const_iterator blockIter = refLayout.blocks.begin(); blockIter != refLayout.blocks.end(); blockIter++)
1411 totalSize += blockIter->size;
1412 data.resize(totalSize);
1414 // Pointers for each block.
1416 for (int blockNdx = 0; blockNdx < (int)refLayout.blocks.size(); blockNdx++)
1418 blockPointers[blockNdx] = &data[0] + curOffset;
1419 curOffset += refLayout.blocks[blockNdx].size;
1424 generateValues(refLayout, blockPointers, 1 /* seed */);
1426 // Generate shaders and build program.
1427 std::ostringstream vtxSrc;
1428 std::ostringstream fragSrc;
1430 generateVertexShader(vtxSrc, m_glslVersion, m_interface, refLayout, blockPointers);
1431 generateFragmentShader(fragSrc, m_glslVersion, m_interface, refLayout, blockPointers);
1433 glu::ShaderProgram program(m_renderCtx, glu::makeVtxFragSources(vtxSrc.str(), fragSrc.str()));
1436 if (!program.isOk())
1439 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Compile failed");
1443 // Query layout from GL.
1444 UniformLayout glLayout;
1445 getGLUniformLayout(gl, glLayout, program.getProgram());
1447 // Print layout to log.
1448 log << TestLog::Section("ActiveUniformBlocks", "Active Uniform Blocks");
1449 for (int blockNdx = 0; blockNdx < (int)glLayout.blocks.size(); blockNdx++)
1450 log << TestLog::Message << blockNdx << ": " << glLayout.blocks[blockNdx] << TestLog::EndMessage;
1451 log << TestLog::EndSection;
1453 log << TestLog::Section("ActiveUniforms", "Active Uniforms");
1454 for (int uniformNdx = 0; uniformNdx < (int)glLayout.uniforms.size(); uniformNdx++)
1455 log << TestLog::Message << uniformNdx << ": " << glLayout.uniforms[uniformNdx] << TestLog::EndMessage;
1456 log << TestLog::EndSection;
1458 // Check that we can even try rendering with given layout.
1459 if (!checkLayoutIndices(glLayout) || !checkLayoutBounds(glLayout) || !compareTypes(refLayout, glLayout))
1461 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Invalid layout");
1462 return STOP; // It is not safe to use the given layout.
1465 // Verify all std140 blocks.
1466 if (!compareStd140Blocks(refLayout, glLayout))
1467 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Invalid std140 layout");
1469 // Verify all shared blocks - all uniforms should be active, and certain properties match.
1470 if (!compareSharedBlocks(refLayout, glLayout))
1471 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Invalid shared layout");
1473 // Check consistency with index queries
1474 if (!checkIndexQueries(program.getProgram(), glLayout))
1475 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Inconsintent block index query results");
1478 gl.useProgram(program.getProgram());
1480 // Assign binding points to all active uniform blocks.
1481 for (int blockNdx = 0; blockNdx < (int)glLayout.blocks.size(); blockNdx++)
1483 deUint32 binding = (deUint32)blockNdx; // \todo [2012-01-25 pyry] Randomize order?
1484 gl.uniformBlockBinding(program.getProgram(), (deUint32)blockNdx, binding);
1487 GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to set uniform block bindings");
1489 // Allocate buffers, write data and bind to targets.
1490 UniformBufferManager bufferManager(m_renderCtx);
1491 if (m_bufferMode == BUFFERMODE_PER_BLOCK)
1493 int numBlocks = (int)glLayout.blocks.size();
1494 vector<vector<deUint8> > glData (numBlocks);
1495 map<int, void*> glBlockPointers;
1497 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
1499 glData[blockNdx].resize(glLayout.blocks[blockNdx].size);
1500 glBlockPointers[blockNdx] = &glData[blockNdx][0];
1503 copyUniformData(glLayout, glBlockPointers, refLayout, blockPointers);
1505 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
1507 deUint32 buffer = bufferManager.allocBuffer();
1508 deUint32 binding = (deUint32)blockNdx;
1510 gl.bindBuffer(GL_UNIFORM_BUFFER, buffer);
1511 gl.bufferData(GL_UNIFORM_BUFFER, (glw::GLsizeiptr)glData[blockNdx].size(), &glData[blockNdx][0], GL_STATIC_DRAW);
1512 GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to upload uniform buffer data");
1514 gl.bindBufferBase(GL_UNIFORM_BUFFER, binding, buffer);
1515 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindBufferBase(GL_UNIFORM_BUFFER) failed");
1520 DE_ASSERT(m_bufferMode == BUFFERMODE_SINGLE);
1524 int numBlocks = (int)glLayout.blocks.size();
1525 int bindingAlignment = 0;
1526 map<int, int> glBlockOffsets;
1528 gl.getIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &bindingAlignment);
1530 // Compute total size and offsets.
1532 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
1534 if (bindingAlignment > 0)
1535 curOffset = deRoundUp32(curOffset, bindingAlignment);
1536 glBlockOffsets[blockNdx] = curOffset;
1537 curOffset += glLayout.blocks[blockNdx].size;
1539 totalSize = curOffset;
1541 // Assign block pointers.
1542 vector<deUint8> glData(totalSize);
1543 map<int, void*> glBlockPointers;
1545 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
1546 glBlockPointers[blockNdx] = &glData[glBlockOffsets[blockNdx]];
1548 // Copy to gl format.
1549 copyUniformData(glLayout, glBlockPointers, refLayout, blockPointers);
1551 // Allocate buffer and upload data.
1552 deUint32 buffer = bufferManager.allocBuffer();
1553 gl.bindBuffer(GL_UNIFORM_BUFFER, buffer);
1554 if (!glData.empty())
1555 gl.bufferData(GL_UNIFORM_BUFFER, (glw::GLsizeiptr)glData.size(), &glData[0], GL_STATIC_DRAW);
1557 GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to upload uniform buffer data");
1559 // Bind ranges to binding points.
1560 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
1562 deUint32 binding = (deUint32)blockNdx;
1563 gl.bindBufferRange(GL_UNIFORM_BUFFER, binding, buffer, (glw::GLintptr)glBlockOffsets[blockNdx], (glw::GLsizeiptr)glLayout.blocks[blockNdx].size);
1564 GLU_EXPECT_NO_ERROR(gl.getError(), "glBindBufferRange(GL_UNIFORM_BUFFER) failed");
1568 bool renderOk = render(program.getProgram());
1570 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Image compare failed");
1575 bool UniformBlockCase::compareStd140Blocks (const UniformLayout& refLayout, const UniformLayout& cmpLayout) const
1577 TestLog& log = m_testCtx.getLog();
1579 int numBlocks = m_interface.getNumUniformBlocks();
1581 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
1583 const UniformBlock& block = m_interface.getUniformBlock(blockNdx);
1584 bool isArray = block.isArray();
1585 std::string instanceName = string(block.getBlockName()) + (isArray ? "[0]" : "");
1586 int refBlockNdx = refLayout.getBlockIndex(instanceName.c_str());
1587 int cmpBlockNdx = cmpLayout.getBlockIndex(instanceName.c_str());
1588 bool isUsed = (block.getFlags() & (DECLARE_VERTEX|DECLARE_FRAGMENT)) != 0;
1590 if ((block.getFlags() & LAYOUT_STD140) == 0)
1591 continue; // Not std140 layout.
1593 DE_ASSERT(refBlockNdx >= 0);
1595 if (cmpBlockNdx < 0)
1597 // Not found, should it?
1600 log << TestLog::Message << "Error: Uniform block '" << instanceName << "' not found" << TestLog::EndMessage;
1604 continue; // Skip block.
1607 const BlockLayoutEntry& refBlockLayout = refLayout.blocks[refBlockNdx];
1608 const BlockLayoutEntry& cmpBlockLayout = cmpLayout.blocks[cmpBlockNdx];
1610 // \todo [2012-01-24 pyry] Verify that activeUniformIndices is correct.
1611 // \todo [2012-01-24 pyry] Verify all instances.
1612 if (refBlockLayout.activeUniformIndices.size() != cmpBlockLayout.activeUniformIndices.size())
1614 log << TestLog::Message << "Error: Number of active uniforms differ in block '" << instanceName
1615 << "' (expected " << refBlockLayout.activeUniformIndices.size()
1616 << ", got " << cmpBlockLayout.activeUniformIndices.size()
1617 << ")" << TestLog::EndMessage;
1621 for (vector<int>::const_iterator ndxIter = refBlockLayout.activeUniformIndices.begin(); ndxIter != refBlockLayout.activeUniformIndices.end(); ndxIter++)
1623 const UniformLayoutEntry& refEntry = refLayout.uniforms[*ndxIter];
1624 int cmpEntryNdx = cmpLayout.getUniformIndex(refEntry.name.c_str());
1626 if (cmpEntryNdx < 0)
1628 log << TestLog::Message << "Error: Uniform '" << refEntry.name << "' not found" << TestLog::EndMessage;
1633 const UniformLayoutEntry& cmpEntry = cmpLayout.uniforms[cmpEntryNdx];
1635 if (refEntry.type != cmpEntry.type ||
1636 refEntry.size != cmpEntry.size ||
1637 refEntry.offset != cmpEntry.offset ||
1638 refEntry.arrayStride != cmpEntry.arrayStride ||
1639 refEntry.matrixStride != cmpEntry.matrixStride ||
1640 refEntry.isRowMajor != cmpEntry.isRowMajor)
1642 log << TestLog::Message << "Error: Layout mismatch in '" << refEntry.name << "':\n"
1643 << " expected: type = " << glu::getDataTypeName(refEntry.type) << ", size = " << refEntry.size << ", offset = " << refEntry.offset << ", array stride = "<< refEntry.arrayStride << ", matrix stride = " << refEntry.matrixStride << ", row major = " << (refEntry.isRowMajor ? "true" : "false") << "\n"
1644 << " got: type = " << glu::getDataTypeName(cmpEntry.type) << ", size = " << cmpEntry.size << ", offset = " << cmpEntry.offset << ", array stride = "<< cmpEntry.arrayStride << ", matrix stride = " << cmpEntry.matrixStride << ", row major = " << (cmpEntry.isRowMajor ? "true" : "false")
1645 << TestLog::EndMessage;
1654 bool UniformBlockCase::compareSharedBlocks (const UniformLayout& refLayout, const UniformLayout& cmpLayout) const
1656 TestLog& log = m_testCtx.getLog();
1658 int numBlocks = m_interface.getNumUniformBlocks();
1660 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
1662 const UniformBlock& block = m_interface.getUniformBlock(blockNdx);
1663 bool isArray = block.isArray();
1664 std::string instanceName = string(block.getBlockName()) + (isArray ? "[0]" : "");
1665 int refBlockNdx = refLayout.getBlockIndex(instanceName.c_str());
1666 int cmpBlockNdx = cmpLayout.getBlockIndex(instanceName.c_str());
1667 bool isUsed = (block.getFlags() & (DECLARE_VERTEX|DECLARE_FRAGMENT)) != 0;
1669 if ((block.getFlags() & LAYOUT_SHARED) == 0)
1670 continue; // Not shared layout.
1672 DE_ASSERT(refBlockNdx >= 0);
1674 if (cmpBlockNdx < 0)
1676 // Not found, should it?
1679 log << TestLog::Message << "Error: Uniform block '" << instanceName << "' not found" << TestLog::EndMessage;
1683 continue; // Skip block.
1686 const BlockLayoutEntry& refBlockLayout = refLayout.blocks[refBlockNdx];
1687 const BlockLayoutEntry& cmpBlockLayout = cmpLayout.blocks[cmpBlockNdx];
1689 if (refBlockLayout.activeUniformIndices.size() != cmpBlockLayout.activeUniformIndices.size())
1691 log << TestLog::Message << "Error: Number of active uniforms differ in block '" << instanceName
1692 << "' (expected " << refBlockLayout.activeUniformIndices.size()
1693 << ", got " << cmpBlockLayout.activeUniformIndices.size()
1694 << ")" << TestLog::EndMessage;
1698 for (vector<int>::const_iterator ndxIter = refBlockLayout.activeUniformIndices.begin(); ndxIter != refBlockLayout.activeUniformIndices.end(); ndxIter++)
1700 const UniformLayoutEntry& refEntry = refLayout.uniforms[*ndxIter];
1701 int cmpEntryNdx = cmpLayout.getUniformIndex(refEntry.name.c_str());
1703 if (cmpEntryNdx < 0)
1705 log << TestLog::Message << "Error: Uniform '" << refEntry.name << "' not found" << TestLog::EndMessage;
1710 const UniformLayoutEntry& cmpEntry = cmpLayout.uniforms[cmpEntryNdx];
1712 if (refEntry.type != cmpEntry.type ||
1713 refEntry.size != cmpEntry.size ||
1714 refEntry.isRowMajor != cmpEntry.isRowMajor)
1716 log << TestLog::Message << "Error: Layout mismatch in '" << refEntry.name << "':\n"
1717 << " expected: type = " << glu::getDataTypeName(refEntry.type) << ", size = " << refEntry.size << ", row major = " << (refEntry.isRowMajor ? "true" : "false") << "\n"
1718 << " got: type = " << glu::getDataTypeName(cmpEntry.type) << ", size = " << cmpEntry.size << ", row major = " << (cmpEntry.isRowMajor ? "true" : "false")
1719 << TestLog::EndMessage;
1728 bool UniformBlockCase::compareTypes (const UniformLayout& refLayout, const UniformLayout& cmpLayout) const
1730 TestLog& log = m_testCtx.getLog();
1732 int numBlocks = m_interface.getNumUniformBlocks();
1734 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
1736 const UniformBlock& block = m_interface.getUniformBlock(blockNdx);
1737 bool isArray = block.isArray();
1738 int numInstances = isArray ? block.getArraySize() : 1;
1740 for (int instanceNdx = 0; instanceNdx < numInstances; instanceNdx++)
1742 std::ostringstream instanceName;
1744 instanceName << block.getBlockName();
1746 instanceName << "[" << instanceNdx << "]";
1748 int cmpBlockNdx = cmpLayout.getBlockIndex(instanceName.str().c_str());
1750 if (cmpBlockNdx < 0)
1753 const BlockLayoutEntry& cmpBlockLayout = cmpLayout.blocks[cmpBlockNdx];
1755 for (vector<int>::const_iterator ndxIter = cmpBlockLayout.activeUniformIndices.begin(); ndxIter != cmpBlockLayout.activeUniformIndices.end(); ndxIter++)
1757 const UniformLayoutEntry& cmpEntry = cmpLayout.uniforms[*ndxIter];
1758 int refEntryNdx = refLayout.getUniformIndex(cmpEntry.name.c_str());
1760 if (refEntryNdx < 0)
1762 log << TestLog::Message << "Error: Uniform '" << cmpEntry.name << "' not found in reference layout" << TestLog::EndMessage;
1767 const UniformLayoutEntry& refEntry = refLayout.uniforms[refEntryNdx];
1769 // \todo [2012-11-26 pyry] Should we check other properties as well?
1770 if (refEntry.type != cmpEntry.type)
1772 log << TestLog::Message << "Error: Uniform type mismatch in '" << refEntry.name << "':\n"
1773 << " expected: " << glu::getDataTypeName(refEntry.type) << "\n"
1774 << " got: " << glu::getDataTypeName(cmpEntry.type)
1775 << TestLog::EndMessage;
1785 bool UniformBlockCase::checkLayoutIndices (const UniformLayout& layout) const
1787 TestLog& log = m_testCtx.getLog();
1788 int numUniforms = (int)layout.uniforms.size();
1789 int numBlocks = (int)layout.blocks.size();
1792 // Check uniform block indices.
1793 for (int uniformNdx = 0; uniformNdx < numUniforms; uniformNdx++)
1795 const UniformLayoutEntry& uniform = layout.uniforms[uniformNdx];
1797 if (uniform.blockNdx < 0 || !deInBounds32(uniform.blockNdx, 0, numBlocks))
1799 log << TestLog::Message << "Error: Invalid block index in uniform '" << uniform.name << "'" << TestLog::EndMessage;
1804 // Check active uniforms.
1805 for (int blockNdx = 0; blockNdx < numBlocks; blockNdx++)
1807 const BlockLayoutEntry& block = layout.blocks[blockNdx];
1809 for (vector<int>::const_iterator uniformIter = block.activeUniformIndices.begin(); uniformIter != block.activeUniformIndices.end(); uniformIter++)
1811 if (!deInBounds32(*uniformIter, 0, numUniforms))
1813 log << TestLog::Message << "Error: Invalid active uniform index " << *uniformIter << " in block '" << block.name << "'" << TestLog::EndMessage;
1822 bool UniformBlockCase::checkLayoutBounds (const UniformLayout& layout) const
1824 TestLog& log = m_testCtx.getLog();
1825 int numUniforms = (int)layout.uniforms.size();
1828 for (int uniformNdx = 0; uniformNdx < numUniforms; uniformNdx++)
1830 const UniformLayoutEntry& uniform = layout.uniforms[uniformNdx];
1832 if (uniform.blockNdx < 0)
1835 const BlockLayoutEntry& block = layout.blocks[uniform.blockNdx];
1836 bool isMatrix = glu::isDataTypeMatrix(uniform.type);
1837 int numVecs = isMatrix ? (uniform.isRowMajor ? glu::getDataTypeMatrixNumRows(uniform.type) : glu::getDataTypeMatrixNumColumns(uniform.type)) : 1;
1838 int numComps = isMatrix ? (uniform.isRowMajor ? glu::getDataTypeMatrixNumColumns(uniform.type) : glu::getDataTypeMatrixNumRows(uniform.type)) : glu::getDataTypeScalarSize(uniform.type);
1839 int numElements = uniform.size;
1840 const int compSize = sizeof(deUint32);
1841 int vecSize = numComps*compSize;
1846 // For negative strides.
1847 minOffset = de::min(minOffset, (numVecs-1)*uniform.matrixStride);
1848 minOffset = de::min(minOffset, (numElements-1)*uniform.arrayStride);
1849 minOffset = de::min(minOffset, (numElements-1)*uniform.arrayStride + (numVecs-1)*uniform.matrixStride);
1851 maxOffset = de::max(maxOffset, vecSize);
1852 maxOffset = de::max(maxOffset, (numVecs-1)*uniform.matrixStride + vecSize);
1853 maxOffset = de::max(maxOffset, (numElements-1)*uniform.arrayStride + vecSize);
1854 maxOffset = de::max(maxOffset, (numElements-1)*uniform.arrayStride + (numVecs-1)*uniform.matrixStride + vecSize);
1856 if (uniform.offset+minOffset < 0 || uniform.offset+maxOffset > block.size)
1858 log << TestLog::Message << "Error: Uniform '" << uniform.name << "' out of block bounds" << TestLog::EndMessage;
1866 bool UniformBlockCase::checkIndexQueries (deUint32 program, const UniformLayout& layout) const
1868 tcu::TestLog& log = m_testCtx.getLog();
1869 const glw::Functions& gl = m_renderCtx.getFunctions();
1872 // \note Spec mandates that uniform blocks are assigned consecutive locations from 0
1873 // to ACTIVE_UNIFORM_BLOCKS. BlockLayoutEntries are stored in that order in UniformLayout.
1874 for (int blockNdx = 0; blockNdx < (int)layout.blocks.size(); blockNdx++)
1876 const BlockLayoutEntry& block = layout.blocks[blockNdx];
1877 const int queriedNdx = gl.getUniformBlockIndex(program, block.name.c_str());
1879 if (queriedNdx != blockNdx)
1881 log << TestLog::Message << "ERROR: glGetUniformBlockIndex(" << block.name << ") returned " << queriedNdx << ", expected " << blockNdx << "!" << TestLog::EndMessage;
1885 GLU_EXPECT_NO_ERROR(gl.getError(), "glGetUniformBlockIndex()");
1891 bool UniformBlockCase::render (deUint32 program) const
1893 tcu::TestLog& log = m_testCtx.getLog();
1894 const glw::Functions& gl = m_renderCtx.getFunctions();
1895 de::Random rnd (deStringHash(getName()));
1896 const tcu::RenderTarget& renderTarget = m_renderCtx.getRenderTarget();
1897 const int viewportW = de::min(renderTarget.getWidth(), 128);
1898 const int viewportH = de::min(renderTarget.getHeight(), 128);
1899 const int viewportX = rnd.getInt(0, renderTarget.getWidth() - viewportW);
1900 const int viewportY = rnd.getInt(0, renderTarget.getHeight() - viewportH);
1902 gl.clearColor(0.125f, 0.25f, 0.5f, 1.0f);
1903 gl.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
1907 const float position[] =
1909 -1.0f, -1.0f, 0.0f, 1.0f,
1910 -1.0f, +1.0f, 0.0f, 1.0f,
1911 +1.0f, -1.0f, 0.0f, 1.0f,
1912 +1.0f, +1.0f, 0.0f, 1.0f
1914 const deUint16 indices[] = { 0, 1, 2, 2, 1, 3 };
1916 gl.viewport(viewportX, viewportY, viewportW, viewportH);
1918 glu::VertexArrayBinding posArray = glu::va::Float("a_position", 4, 4, 0, &position[0]);
1919 glu::draw(m_renderCtx, program, 1, &posArray,
1920 glu::pr::Triangles(DE_LENGTH_OF_ARRAY(indices), &indices[0]));
1921 GLU_EXPECT_NO_ERROR(gl.getError(), "Draw failed");
1924 // Verify that all pixels are white.
1926 tcu::Surface pixels (viewportW, viewportH);
1927 int numFailedPixels = 0;
1929 glu::readPixels(m_renderCtx, viewportX, viewportY, pixels.getAccess());
1930 GLU_EXPECT_NO_ERROR(gl.getError(), "Reading pixels failed");
1932 for (int y = 0; y < pixels.getHeight(); y++)
1934 for (int x = 0; x < pixels.getWidth(); x++)
1936 if (pixels.getPixel(x, y) != tcu::RGBA::white())
1937 numFailedPixels += 1;
1941 if (numFailedPixels > 0)
1943 log << TestLog::Image("Image", "Rendered image", pixels);
1944 log << TestLog::Message << "Image comparison failed, got " << numFailedPixels << " non-white pixels" << TestLog::EndMessage;
1947 return numFailedPixels == 0;