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"
26 #include "deArrayUtil.hpp"
31 VarType::VarType (void)
36 VarType::VarType (const VarType& other)
42 VarType::VarType (DataType basicType, Precision precision)
45 m_data.basic.type = basicType;
46 m_data.basic.precision = precision;
49 VarType::VarType (const VarType& elementType, int arraySize)
52 DE_ASSERT(arraySize >= 0 || arraySize == UNSIZED_ARRAY);
53 m_data.array.size = arraySize;
54 m_data.array.elementType = new VarType(elementType);
57 VarType::VarType (const StructType* structPtr)
60 m_data.structPtr = structPtr;
63 VarType::~VarType (void)
65 if (m_type == TYPE_ARRAY)
66 delete m_data.array.elementType;
69 VarType& VarType::operator= (const VarType& other)
72 return *this; // Self-assignment.
74 if (m_type == TYPE_ARRAY)
75 delete m_data.array.elementType;
77 m_type = other.m_type;
80 if (m_type == TYPE_ARRAY)
82 m_data.array.elementType = new VarType(*other.m_data.array.elementType);
83 m_data.array.size = other.m_data.array.size;
86 m_data = other.m_data;
91 int VarType::getScalarSize (void) const
95 case TYPE_BASIC: return glu::getDataTypeScalarSize(m_data.basic.type);
96 case TYPE_ARRAY: return m_data.array.elementType->getScalarSize()*m_data.array.size;
101 for (StructType::ConstIterator iter = m_data.structPtr->begin(); iter != m_data.structPtr->end(); iter++)
102 size += iter->getType().getScalarSize();
112 bool VarType::operator== (const VarType& other) const
114 if (m_type != other.m_type)
120 return m_data.basic.type == other.m_data.basic.type &&
121 m_data.basic.precision == other.m_data.basic.precision;
124 return *m_data.array.elementType == *other.m_data.array.elementType &&
125 m_data.array.size == other.m_data.array.size;
128 return m_data.structPtr == other.m_data.structPtr;
136 bool VarType::operator!= (const VarType& other) const
138 return !(*this == other);
141 // StructMember implementation
143 bool StructMember::operator== (const StructMember& other) const
145 return (m_name == other.m_name) && (m_type == other.m_type);
148 bool StructMember::operator!= (const StructMember& other) const
150 return !(*this == other);
153 // StructType implementation.
155 void StructType::addMember (const char* name, const VarType& type)
157 m_members.push_back(StructMember(name, type));
160 bool StructType::operator== (const StructType& other) const
162 return (m_typeName == other.m_typeName) && (m_members == other.m_members);
165 bool StructType::operator!= (const StructType& other) const
167 return !(*this == other);
170 const char* getStorageName (Storage storage)
172 static const char* const s_names[] = { "in", "out", "const", "uniform", "buffer", "patch in", "patch out" };
174 return de::getSizedArrayElement<STORAGE_LAST>(s_names, storage);
177 const char* getInterpolationName (Interpolation interpolation)
179 static const char* const s_names[] = { "smooth", "flat", "centroid" };
181 return de::getSizedArrayElement<INTERPOLATION_LAST>(s_names, interpolation);
184 const char* getFormatLayoutName (FormatLayout layout)
186 static const char* s_names[] =
188 "rgba32f", // FORMATLAYOUT_RGBA32F
189 "rgba16f", // FORMATLAYOUT_RGBA16F
190 "r32f", // FORMATLAYOUT_R32F
191 "rgba8", // FORMATLAYOUT_RGBA8
192 "rgba8_snorm", // FORMATLAYOUT_RGBA8_SNORM
193 "rgba32i", // FORMATLAYOUT_RGBA32I
194 "rgba16i", // FORMATLAYOUT_RGBA16I
195 "rgba8i", // FORMATLAYOUT_RGBA8I
196 "r32i", // FORMATLAYOUT_R32I
197 "rgba32ui", // FORMATLAYOUT_RGBA32UI
198 "rgba16ui", // FORMATLAYOUT_RGBA16UI
199 "rgba8ui", // FORMATLAYOUT_RGBA8UI
200 "r32ui", // FORMATLAYOUT_R32UI
203 return de::getSizedArrayElement<FORMATLAYOUT_LAST>(s_names, layout);
206 const char* getMemoryAccessQualifierName (MemoryAccessQualifier qualifier)
210 case MEMORYACCESSQUALIFIER_COHERENT_BIT: return "coherent";
211 case MEMORYACCESSQUALIFIER_VOLATILE_BIT: return "volatile";
212 case MEMORYACCESSQUALIFIER_RESTRICT_BIT: return "restrict";
213 case MEMORYACCESSQUALIFIER_READONLY_BIT: return "readonly";
214 case MEMORYACCESSQUALIFIER_WRITEONLY_BIT: return "writeonly";
221 const char* getMatrixOrderName (MatrixOrder qualifier)
223 static const char* s_names[] =
225 "column_major", // MATRIXORDER_COLUMN_MAJOR
226 "row_major", // MATRIXORDER_ROW_MAJOR
229 return de::getSizedArrayElement<MATRIXORDER_LAST>(s_names, qualifier);
232 // Layout Implementation
234 Layout::Layout (int location_, int binding_, int offset_, FormatLayout format_, MatrixOrder matrixOrder_)
235 : location (location_)
239 , matrixOrder (matrixOrder_)
243 bool Layout::operator== (const Layout& other) const
245 return location == other.location &&
246 binding == other.binding &&
247 offset == other.offset &&
248 format == other.format &&
249 matrixOrder == other.matrixOrder;
252 bool Layout::operator!= (const Layout& other) const
254 return !(*this == other);
257 // VariableDeclaration Implementation
259 VariableDeclaration::VariableDeclaration (const VarType& varType_, const std::string& name_, Storage storage_, Interpolation interpolation_, const Layout& layout_, deUint32 memoryAccessQualifierBits_)
261 , interpolation (interpolation_)
264 , memoryAccessQualifierBits (memoryAccessQualifierBits_)
269 bool VariableDeclaration::operator== (const VariableDeclaration& other) const
271 return layout == other.layout &&
272 interpolation == other.interpolation &&
273 storage == other.storage &&
274 varType == other.varType &&
275 memoryAccessQualifierBits == other.memoryAccessQualifierBits &&
279 bool VariableDeclaration::operator!= (const VariableDeclaration& other) const
281 return !(*this == other);
284 // InterfaceBlock Implementation
286 InterfaceBlock::InterfaceBlock (void)
288 , storage (glu::STORAGE_LAST)
289 , memoryAccessQualifierFlags (0)
293 // Declaration utilties.
295 std::ostream& operator<< (std::ostream& str, const Layout& layout)
297 std::vector<std::string> layoutDeclarationList;
299 if (layout.location != -1)
300 layoutDeclarationList.push_back("location=" + de::toString(layout.location));
302 if (layout.binding != -1)
303 layoutDeclarationList.push_back("binding=" + de::toString(layout.binding));
305 if (layout.offset != -1)
306 layoutDeclarationList.push_back("offset=" + de::toString(layout.offset));
308 if (layout.format != FORMATLAYOUT_LAST)
309 layoutDeclarationList.push_back(getFormatLayoutName(layout.format));
311 if (layout.matrixOrder != MATRIXORDER_LAST)
312 layoutDeclarationList.push_back(getMatrixOrderName(layout.matrixOrder));
314 if (!layoutDeclarationList.empty())
316 str << "layout(" << layoutDeclarationList[0];
318 for (int layoutNdx = 1; layoutNdx < (int)layoutDeclarationList.size(); ++layoutNdx)
319 str << ", " << layoutDeclarationList[layoutNdx];
327 std::ostream& operator<< (std::ostream& str, const VariableDeclaration& decl)
329 if (decl.layout != Layout())
330 str << decl.layout << " ";
332 for (int bitNdx = 0; (1 << bitNdx) & MEMORYACCESSQUALIFIER_MASK; ++bitNdx)
333 if (decl.memoryAccessQualifierBits & (1 << bitNdx))
334 str << getMemoryAccessQualifierName((glu::MemoryAccessQualifier)(1 << bitNdx)) << " ";
336 if (decl.interpolation != INTERPOLATION_LAST)
337 str << getInterpolationName(decl.interpolation) << " ";
339 if (decl.storage != STORAGE_LAST)
340 str << getStorageName(decl.storage) << " ";
342 str << declare(decl.varType, decl.name);
350 std::ostream& operator<< (std::ostream& str, const Indent& indent)
352 for (int i = 0; i < indent.level; i++)
357 std::ostream& operator<< (std::ostream& str, const DeclareVariable& decl)
359 const VarType& type = decl.varType;
360 const VarType* curType = &type;
361 std::vector<int> arraySizes;
364 while (curType->isArrayType())
366 arraySizes.push_back(curType->getArraySize());
367 curType = &curType->getElementType();
370 if (curType->isBasicType())
372 if (curType->getPrecision() != PRECISION_LAST)
373 str << glu::getPrecisionName(curType->getPrecision()) << " ";
374 str << glu::getDataTypeName(curType->getBasicType());
376 else if (curType->isStructType())
378 const StructType* structPtr = curType->getStructPtr();
380 if (structPtr->hasTypeName())
381 str << structPtr->getTypeName();
383 str << declare(structPtr, decl.indentLevel); // Generate inline declaration.
388 str << " " << decl.name;
390 // Print array sizes.
391 for (std::vector<int>::const_iterator sizeIter = arraySizes.begin(); sizeIter != arraySizes.end(); sizeIter++)
393 const int arrSize = *sizeIter;
394 if (arrSize == VarType::UNSIZED_ARRAY)
397 str << "[" << arrSize << "]";
403 std::ostream& operator<< (std::ostream& str, const DeclareStructTypePtr& decl)
407 // Type name is optional.
408 if (decl.structPtr->hasTypeName())
409 str << " " << decl.structPtr->getTypeName();
411 str << "\n" << indent(decl.indentLevel) << "{\n";
413 for (StructType::ConstIterator memberIter = decl.structPtr->begin(); memberIter != decl.structPtr->end(); memberIter++)
415 str << indent(decl.indentLevel+1);
416 str << declare(memberIter->getType(), memberIter->getName(), decl.indentLevel+1) << ";\n";
419 str << indent(decl.indentLevel) << "}";
424 std::ostream& operator<< (std::ostream& str, const DeclareStructType& decl)
426 return str << declare(&decl.structType, decl.indentLevel);