1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 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 Shader variable type.
22 *//*--------------------------------------------------------------------*/
24 #include "gluVarType.hpp"
25 #include "deStringUtil.hpp"
30 VarType::VarType (void)
35 VarType::VarType (const VarType& other)
41 VarType::VarType (DataType basicType, Precision precision)
44 m_data.basic.type = basicType;
45 m_data.basic.precision = precision;
48 VarType::VarType (const VarType& elementType, int arraySize)
51 DE_ASSERT(arraySize >= 0 || arraySize == UNSIZED_ARRAY);
52 m_data.array.size = arraySize;
53 m_data.array.elementType = new VarType(elementType);
56 VarType::VarType (const StructType* structPtr)
59 m_data.structPtr = structPtr;
62 VarType::~VarType (void)
64 if (m_type == TYPE_ARRAY)
65 delete m_data.array.elementType;
68 VarType& VarType::operator= (const VarType& other)
71 return *this; // Self-assignment.
73 if (m_type == TYPE_ARRAY)
74 delete m_data.array.elementType;
76 m_type = other.m_type;
79 if (m_type == TYPE_ARRAY)
81 m_data.array.elementType = new VarType(*other.m_data.array.elementType);
82 m_data.array.size = other.m_data.array.size;
85 m_data = other.m_data;
90 int VarType::getScalarSize (void) const
94 case TYPE_BASIC: return glu::getDataTypeScalarSize(m_data.basic.type);
95 case TYPE_ARRAY: return m_data.array.elementType->getScalarSize()*m_data.array.size;
100 for (StructType::ConstIterator iter = m_data.structPtr->begin(); iter != m_data.structPtr->end(); iter++)
101 size += iter->getType().getScalarSize();
111 bool VarType::operator== (const VarType& other) const
113 if (m_type != other.m_type)
119 return m_data.basic.type == other.m_data.basic.type &&
120 m_data.basic.precision == other.m_data.basic.precision;
123 return *m_data.array.elementType == *other.m_data.array.elementType &&
124 m_data.array.size == other.m_data.array.size;
127 return m_data.structPtr == other.m_data.structPtr;
135 bool VarType::operator!= (const VarType& other) const
137 return !(*this == other);
140 // StructMember implementation
142 bool StructMember::operator== (const StructMember& other) const
144 return (m_name == other.m_name) && (m_type == other.m_type);
147 bool StructMember::operator!= (const StructMember& other) const
149 return !(*this == other);
152 // StructType implementation.
154 void StructType::addMember (const char* name, const VarType& type)
156 m_members.push_back(StructMember(name, type));
159 bool StructType::operator== (const StructType& other) const
161 return (m_typeName == other.m_typeName) && (m_members == other.m_members);
164 bool StructType::operator!= (const StructType& other) const
166 return !(*this == other);
169 const char* getStorageName (Storage storage)
171 static const char* const s_names[] = { "in", "out", "const", "uniform", "buffer", "patch in", "patch out" };
173 return de::getSizedArrayElement<STORAGE_LAST>(s_names, storage);
176 const char* getInterpolationName (Interpolation interpolation)
178 static const char* const s_names[] = { "smooth", "flat", "centroid" };
180 return de::getSizedArrayElement<INTERPOLATION_LAST>(s_names, interpolation);
183 const char* getFormatLayoutName (FormatLayout layout)
185 static const char* s_names[] =
187 "rgba32f", // FORMATLAYOUT_RGBA32F
188 "rgba16f", // FORMATLAYOUT_RGBA16F
189 "r32f", // FORMATLAYOUT_R32F
190 "rgba8", // FORMATLAYOUT_RGBA8
191 "rgba8_snorm", // FORMATLAYOUT_RGBA8_SNORM
192 "rgba32i", // FORMATLAYOUT_RGBA32I
193 "rgba16i", // FORMATLAYOUT_RGBA16I
194 "rgba8i", // FORMATLAYOUT_RGBA8I
195 "r32i", // FORMATLAYOUT_R32I
196 "rgba32ui", // FORMATLAYOUT_RGBA32UI
197 "rgba16ui", // FORMATLAYOUT_RGBA16UI
198 "rgba8ui", // FORMATLAYOUT_RGBA8UI
199 "r32ui", // FORMATLAYOUT_R32UI
202 return de::getSizedArrayElement<FORMATLAYOUT_LAST>(s_names, layout);
205 const char* getMemoryAccessQualifierName (MemoryAccessQualifier qualifier)
209 case MEMORYACCESSQUALIFIER_COHERENT_BIT: return "coherent";
210 case MEMORYACCESSQUALIFIER_VOLATILE_BIT: return "volatile";
211 case MEMORYACCESSQUALIFIER_RESTRICT_BIT: return "restrict";
212 case MEMORYACCESSQUALIFIER_READONLY_BIT: return "readonly";
213 case MEMORYACCESSQUALIFIER_WRITEONLY_BIT: return "writeonly";
220 const char* getMatrixOrderName (MatrixOrder qualifier)
222 static const char* s_names[] =
224 "column_major", // MATRIXORDER_COLUMN_MAJOR
225 "row_major", // MATRIXORDER_ROW_MAJOR
228 return de::getSizedArrayElement<MATRIXORDER_LAST>(s_names, qualifier);
231 // Layout Implementation
233 Layout::Layout (int location_, int binding_, int offset_, FormatLayout format_, MatrixOrder matrixOrder_)
234 : location (location_)
238 , matrixOrder (matrixOrder_)
242 bool Layout::operator== (const Layout& other) const
244 return location == other.location &&
245 binding == other.binding &&
246 offset == other.offset &&
247 format == other.format &&
248 matrixOrder == other.matrixOrder;
251 bool Layout::operator!= (const Layout& other) const
253 return !(*this == other);
256 // VariableDeclaration Implementation
258 VariableDeclaration::VariableDeclaration (const VarType& varType_, const std::string& name_, Storage storage_, Interpolation interpolation_, const Layout& layout_, deUint32 memoryAccessQualifierBits_)
260 , interpolation (interpolation_)
263 , memoryAccessQualifierBits (memoryAccessQualifierBits_)
268 bool VariableDeclaration::operator== (const VariableDeclaration& other) const
270 return layout == other.layout &&
271 interpolation == other.interpolation &&
272 storage == other.storage &&
273 varType == other.varType &&
274 memoryAccessQualifierBits == other.memoryAccessQualifierBits &&
278 bool VariableDeclaration::operator!= (const VariableDeclaration& other) const
280 return !(*this == other);
283 // InterfaceBlock Implementation
285 InterfaceBlock::InterfaceBlock (void)
287 , storage (glu::STORAGE_LAST)
288 , memoryAccessQualifierFlags (0)
292 // Declaration utilties.
294 std::ostream& operator<< (std::ostream& str, const Layout& layout)
296 std::vector<std::string> layoutDeclarationList;
298 if (layout.location != -1)
299 layoutDeclarationList.push_back("location=" + de::toString(layout.location));
301 if (layout.binding != -1)
302 layoutDeclarationList.push_back("binding=" + de::toString(layout.binding));
304 if (layout.offset != -1)
305 layoutDeclarationList.push_back("offset=" + de::toString(layout.offset));
307 if (layout.format != FORMATLAYOUT_LAST)
308 layoutDeclarationList.push_back(getFormatLayoutName(layout.format));
310 if (layout.matrixOrder != MATRIXORDER_LAST)
311 layoutDeclarationList.push_back(getMatrixOrderName(layout.matrixOrder));
313 if (!layoutDeclarationList.empty())
315 str << "layout(" << layoutDeclarationList[0];
317 for (int layoutNdx = 1; layoutNdx < (int)layoutDeclarationList.size(); ++layoutNdx)
318 str << ", " << layoutDeclarationList[layoutNdx];
326 std::ostream& operator<< (std::ostream& str, const VariableDeclaration& decl)
328 if (decl.layout != Layout())
329 str << decl.layout << " ";
331 for (int bitNdx = 0; (1 << bitNdx) & MEMORYACCESSQUALIFIER_MASK; ++bitNdx)
332 if (decl.memoryAccessQualifierBits & (1 << bitNdx))
333 str << getMemoryAccessQualifierName((glu::MemoryAccessQualifier)(1 << bitNdx)) << " ";
335 if (decl.interpolation != INTERPOLATION_LAST)
336 str << getInterpolationName(decl.interpolation) << " ";
338 if (decl.storage != STORAGE_LAST)
339 str << getStorageName(decl.storage) << " ";
341 str << declare(decl.varType, decl.name);
349 std::ostream& operator<< (std::ostream& str, const Indent& indent)
351 for (int i = 0; i < indent.level; i++)
356 std::ostream& operator<< (std::ostream& str, const DeclareVariable& decl)
358 const VarType& type = decl.varType;
359 const VarType* curType = &type;
360 std::vector<int> arraySizes;
363 while (curType->isArrayType())
365 arraySizes.push_back(curType->getArraySize());
366 curType = &curType->getElementType();
369 if (curType->isBasicType())
371 if (curType->getPrecision() != PRECISION_LAST)
372 str << glu::getPrecisionName(curType->getPrecision()) << " ";
373 str << glu::getDataTypeName(curType->getBasicType());
375 else if (curType->isStructType())
377 const StructType* structPtr = curType->getStructPtr();
379 if (structPtr->hasTypeName())
380 str << structPtr->getTypeName();
382 str << declare(structPtr, decl.indentLevel); // Generate inline declaration.
387 str << " " << decl.name;
389 // Print array sizes.
390 for (std::vector<int>::const_iterator sizeIter = arraySizes.begin(); sizeIter != arraySizes.end(); sizeIter++)
392 const int arrSize = *sizeIter;
393 if (arrSize == VarType::UNSIZED_ARRAY)
396 str << "[" << arrSize << "]";
402 std::ostream& operator<< (std::ostream& str, const DeclareStructTypePtr& decl)
406 // Type name is optional.
407 if (decl.structPtr->hasTypeName())
408 str << " " << decl.structPtr->getTypeName();
410 str << "\n" << indent(decl.indentLevel) << "{\n";
412 for (StructType::ConstIterator memberIter = decl.structPtr->begin(); memberIter != decl.structPtr->end(); memberIter++)
414 str << indent(decl.indentLevel+1);
415 str << declare(memberIter->getType(), memberIter->getName(), decl.indentLevel+1) << ";\n";
418 str << indent(decl.indentLevel) << "}";
423 std::ostream& operator<< (std::ostream& str, const DeclareStructType& decl)
425 return str << declare(&decl.structType, decl.indentLevel);