1 #ifndef _RSGVARIABLETYPE_HPP
2 #define _RSGVARIABLETYPE_HPP
3 /*-------------------------------------------------------------------------
4 * drawElements Quality Program Random Shader Generator
5 * ----------------------------------------------------
6 *
7 * Copyright 2014 The Android Open Source Project
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 *//*!
22 * \file
23 * \brief Variable Type class.
24 *//*--------------------------------------------------------------------*/
25
26 #include "rsgDefs.hpp"
27
28 #include <vector>
29 #include <string>
30
31 namespace rsg
32 {
33
34 class TokenStream;
35
36 class VariableType
37 {
38 public:
39 enum Type
40 {
41 TYPE_VOID = 0,
42 TYPE_FLOAT,
43 TYPE_INT,
44 TYPE_BOOL,
45 TYPE_STRUCT,
46 TYPE_ARRAY,
47 TYPE_SAMPLER_2D,
48 TYPE_SAMPLER_CUBE,
49
50 TYPE_LAST
51 };
52
53 enum Precision
54 {
55 PRECISION_NONE = 0,
56 PRECISION_LOW,
57 PRECISION_MEDIUM,
58 PRECISION_HIGH,
59
60 PRECISION_LAST
61 };
62
63 class Member
64 {
65 public:
Member(void)66 Member(void) : m_type(DE_NULL), m_name()
67 {
68 }
69
Member(const VariableType & type,const char * name)70 Member(const VariableType &type, const char *name) : m_type(new VariableType(type)), m_name(name)
71 {
72 }
73
~Member(void)74 ~Member(void)
75 {
76 delete m_type;
77 }
78
Member(const Member & other)79 Member(const Member &other) : m_type(DE_NULL), m_name(other.m_name)
80 {
81 if (other.m_type)
82 m_type = new VariableType(*other.m_type);
83 }
84
operator =(const Member & other)85 Member &operator=(const Member &other)
86 {
87 if (this == &other)
88 return *this;
89
90 delete m_type;
91
92 m_type = DE_NULL;
93 m_name = other.m_name;
94
95 if (other.m_type)
96 m_type = new VariableType(*other.m_type);
97
98 return *this;
99 }
100
operator !=(const Member & other) const101 bool operator!=(const Member &other) const
102 {
103 if (m_name != other.m_name)
104 return true;
105 if (!!m_type != !!other.m_type)
106 return true;
107 if (m_type && *m_type != *other.m_type)
108 return true;
109 return false;
110 }
111
operator ==(const Member & other) const112 bool operator==(const Member &other) const
113 {
114 return !(*this != other);
115 }
116
getType(void) const117 const VariableType &getType(void) const
118 {
119 return *m_type;
120 }
getName(void) const121 const char *getName(void) const
122 {
123 return m_name.c_str();
124 }
125
126 private:
127 VariableType *m_type;
128 std::string m_name;
129 };
130
131 VariableType(void);
132 VariableType(Type baseType, int numElements = 0);
133 VariableType(Type baseType, const VariableType &elementType, int numElements);
134 VariableType(Type baseType, const char *typeName);
135 ~VariableType(void);
136
getBaseType(void) const137 Type getBaseType(void) const
138 {
139 return m_baseType;
140 }
getPrecision(void) const141 Precision getPrecision(void) const
142 {
143 return m_precision;
144 }
getTypeName(void) const145 const char *getTypeName(void) const
146 {
147 return m_typeName.c_str();
148 }
getNumElements(void) const149 int getNumElements(void) const
150 {
151 return m_numElements;
152 }
153 const VariableType &getElementType(void) const;
154
getMembers(void) const155 const std::vector<Member> &getMembers(void) const
156 {
157 return m_members;
158 }
getMembers(void)159 std::vector<Member> &getMembers(void)
160 {
161 return m_members;
162 }
163
164 int getScalarSize(void) const;
165 int getElementScalarOffset(int elementNdx) const;
166 int getMemberScalarOffset(int memberNdx) const;
167
168 bool operator!=(const VariableType &other) const;
169 bool operator==(const VariableType &other) const;
170
171 VariableType &operator=(const VariableType &other);
172 VariableType(const VariableType &other);
173
174 void tokenizeShortType(TokenStream &str) const;
175
isStruct(void) const176 bool isStruct(void) const
177 {
178 return m_baseType == TYPE_STRUCT;
179 }
isArray(void) const180 bool isArray(void) const
181 {
182 return m_baseType == TYPE_ARRAY;
183 }
isFloatOrVec(void) const184 bool isFloatOrVec(void) const
185 {
186 return m_baseType == TYPE_FLOAT;
187 }
isIntOrVec(void) const188 bool isIntOrVec(void) const
189 {
190 return m_baseType == TYPE_INT;
191 }
isBoolOrVec(void) const192 bool isBoolOrVec(void) const
193 {
194 return m_baseType == TYPE_BOOL;
195 }
isSampler(void) const196 bool isSampler(void) const
197 {
198 return m_baseType == TYPE_SAMPLER_2D || m_baseType == TYPE_SAMPLER_CUBE;
199 }
isVoid(void) const200 bool isVoid(void) const
201 {
202 return m_baseType == TYPE_VOID;
203 }
204
205 static const VariableType &getScalarType(Type baseType);
206
207 private:
208 Type m_baseType;
209 Precision m_precision;
210 std::string m_typeName;
211 int m_numElements;
212 VariableType *m_elementType;
213 std::vector<Member> m_members;
214 };
215
VariableType(void)216 inline VariableType::VariableType(void)
217 : m_baseType(TYPE_VOID)
218 , m_precision(PRECISION_NONE)
219 , m_typeName()
220 , m_numElements(0)
221 , m_elementType(DE_NULL)
222 {
223 }
224
VariableType(Type baseType,int numElements)225 inline VariableType::VariableType(Type baseType, int numElements)
226 : m_baseType(baseType)
227 , m_precision(PRECISION_NONE)
228 , m_typeName()
229 , m_numElements(numElements)
230 , m_elementType(DE_NULL)
231 {
232 DE_ASSERT(baseType != TYPE_ARRAY && baseType != TYPE_STRUCT);
233 }
234
VariableType(Type baseType,const VariableType & elementType,int numElements)235 inline VariableType::VariableType(Type baseType, const VariableType &elementType, int numElements)
236 : m_baseType(baseType)
237 , m_precision(PRECISION_NONE)
238 , m_typeName()
239 , m_numElements(numElements)
240 , m_elementType(new VariableType(elementType))
241 {
242 DE_ASSERT(baseType == TYPE_ARRAY);
243 }
244
VariableType(Type baseType,const char * typeName)245 inline VariableType::VariableType(Type baseType, const char *typeName)
246 : m_baseType(baseType)
247 , m_precision(PRECISION_NONE)
248 , m_typeName(typeName)
249 , m_numElements(0)
250 , m_elementType(DE_NULL)
251 {
252 DE_ASSERT(baseType == TYPE_STRUCT);
253 }
254
~VariableType(void)255 inline VariableType::~VariableType(void)
256 {
257 delete m_elementType;
258 }
259
260 } // namespace rsg
261
262 #endif // _RSGVARIABLETYPE_HPP
263