1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Random Shader Generator
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 Variable Type class.
22 *//*--------------------------------------------------------------------*/
24 #include "rsgVariableType.hpp"
25 #include "rsgToken.hpp"
32 VariableType& VariableType::operator= (const VariableType& other)
39 m_elementType = DE_NULL;
40 m_baseType = other.m_baseType;
41 m_precision = other.m_precision;
42 m_typeName = other.m_typeName;
43 m_numElements = other.m_numElements;
44 m_members = other.m_members;
45 m_elementType = DE_NULL;
47 if (other.m_elementType)
48 m_elementType = new VariableType(*other.m_elementType);
53 VariableType::VariableType (const VariableType& other)
54 : m_elementType(DE_NULL)
59 bool VariableType::operator!= (const VariableType& other) const
61 if (m_baseType != other.m_baseType)
63 if (m_precision != other.m_precision)
65 if (m_numElements != other.m_numElements)
67 if (!!m_elementType != !!other.m_elementType)
69 if (m_elementType && *m_elementType != *other.m_elementType)
71 if (m_members != other.m_members)
76 bool VariableType::operator== (const VariableType& other) const
78 return !(*this != other);
81 int VariableType::getScalarSize (void) const
90 case TYPE_SAMPLER_CUBE:
96 for (vector<Member>::const_iterator i = m_members.begin(); i != m_members.end(); i++)
97 sum += i->getType().getScalarSize();
103 DE_ASSERT(m_elementType);
104 return m_elementType->getScalarSize() * m_numElements;
113 int VariableType::getMemberScalarOffset (int memberNdx) const
115 DE_ASSERT(isStruct());
118 for (vector<Member>::const_iterator i = m_members.begin(); i != m_members.begin() + memberNdx; i++)
119 curOffset += i->getType().getScalarSize();
124 int VariableType::getElementScalarOffset (int elementNdx) const
126 DE_ASSERT(isArray());
127 return elementNdx * getElementType().getScalarSize();
130 const VariableType& VariableType::getScalarType (Type baseType)
136 static const VariableType s_floatTypes[] =
138 VariableType(TYPE_FLOAT, 1)
139 // \todo [pyry] Extend with different precision variants?
141 return s_floatTypes[0];
146 static const VariableType s_intTypes[] =
148 VariableType(TYPE_INT, 1)
150 return s_intTypes[0];
155 static const VariableType s_boolTypes[] =
157 VariableType(TYPE_BOOL, 1)
159 return s_boolTypes[0];
162 case TYPE_SAMPLER_2D:
164 static const VariableType sampler2DType = VariableType(TYPE_SAMPLER_2D, 1);
165 return sampler2DType;
168 case TYPE_SAMPLER_CUBE:
170 static const VariableType samplerCubeType = VariableType(TYPE_SAMPLER_CUBE, 1);
171 return samplerCubeType;
176 throw Exception("VariableType::getScalarType(): unsupported type");
180 const VariableType& VariableType::getElementType (void) const
182 DE_ASSERT(m_precision == PRECISION_NONE); // \todo [pyry] Precision
188 case TYPE_SAMPLER_2D:
189 case TYPE_SAMPLER_CUBE:
190 return getScalarType(m_baseType);
194 DE_ASSERT(m_elementType);
195 return *m_elementType;
200 throw Exception("VariableType::getElementType(): unsupported type");
204 void VariableType::tokenizeShortType (TokenStream& str) const
208 case PRECISION_LOW: str << Token::LOW_PRECISION; break;
209 case PRECISION_MEDIUM: str << Token::MEDIUM_PRECISION; break;
210 case PRECISION_HIGH: str << Token::HIGH_PRECISION; break;
211 default: /* nothing */ break;
221 switch (m_numElements)
223 case 1: str << Token::FLOAT; break;
224 case 2: str << Token::VEC2; break;
225 case 3: str << Token::VEC3; break;
226 case 4: str << Token::VEC4; break;
227 default: DE_ASSERT(DE_FALSE); break;
232 switch (m_numElements)
234 case 1: str << Token::INT; break;
235 case 2: str << Token::IVEC2; break;
236 case 3: str << Token::IVEC3; break;
237 case 4: str << Token::IVEC4; break;
238 default: DE_ASSERT(DE_FALSE); break;
243 switch (m_numElements)
245 case 1: str << Token::BOOL; break;
246 case 2: str << Token::BVEC2; break;
247 case 3: str << Token::BVEC3; break;
248 case 4: str << Token::BVEC4; break;
249 default: DE_ASSERT(DE_FALSE); break;
253 case TYPE_SAMPLER_2D: str << Token::SAMPLER2D; break;
254 case TYPE_SAMPLER_CUBE: str << Token::SAMPLERCUBE; break;
257 DE_ASSERT(m_typeName != "");
258 str << Token(m_typeName.c_str());
262 DE_ASSERT(m_elementType);
263 m_elementType->tokenizeShortType(str);
264 str << Token::LEFT_BRACKET << Token(m_numElements) << Token::RIGHT_BRACKET;