updated some printfs, added comment about sched_yield
[mesa.git] / src / mesa / shader / slang / Include / Types.h
1 //
2 //Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 //All rights reserved.
4 //
5 //Redistribution and use in source and binary forms, with or without
6 //modification, are permitted provided that the following conditions
7 //are met:
8 //
9 // Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 //
17 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
18 // contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 //POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #ifndef _TYPES_INCLUDED
36 #define _TYPES_INCLUDED
37
38 #include "../Include/Common.h"
39 #include "../Include/BaseTypes.h"
40
41 //
42 // Need to have association of line numbers to types in a list for building structs.
43 //
44 class TType;
45 struct TTypeLine {
46 TType* type;
47 int line;
48 };
49 typedef TVector<TTypeLine> TTypeList;
50
51 inline TTypeList* NewPoolTTypeList()
52 {
53 void* memory = GlobalPoolAllocator.allocate(sizeof(TTypeList));
54 return new(memory) TTypeList;
55 }
56
57 //
58 // This is a workaround for a problem with the yacc stack, It can't have
59 // types that the compiler thinks non-trivial constructors. It should
60 // just be used while recognizing the grammar, not anything else. Pointers
61 // could be used, but also trying to avoid lots of memory management overhead.
62 //
63 // Not as bad as it looks, there is no actual assumption that the fields
64 // match up or are name the same or anything like that.
65 //
66 class TPublicType {
67 public:
68 TBasicType type;
69 TQualifier qualifier;
70 int size; // size of vector or matrix, not size of array
71 bool matrix;
72 bool array;
73 TType* userDef;
74 int line;
75 };
76
77 typedef std::map<TTypeList*, TTypeList*> TStructureMap;
78 typedef std::map<TTypeList*, TTypeList*>::iterator TStructureMapIterator;
79 //
80 // Base class for things that have a type.
81 //
82 class TType {
83 public:
84 POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)
85 explicit TType(TBasicType t, TQualifier q = EvqTemporary, int s = 1, bool m = false, bool a = false) :
86 type(t), qualifier(q), size(s), matrix(m), array(a), arraySize(0), structure(0),
87 structureSize(0), maxArraySize(0), arrayInformationType(0), fieldName(0), typeName(0), mangled(0)
88 { }
89 explicit TType(TPublicType p) :
90 type(p.type), qualifier(p.qualifier), size(p.size), matrix(p.matrix), array(p.array), arraySize(0),
91 structure(0), structureSize(0), maxArraySize(0), arrayInformationType(0), fieldName(0), typeName(0), mangled(0)
92 {
93 if (p.userDef) {
94 structure = p.userDef->getStruct();
95 structureSize = setStructSize(p.userDef->getStruct());
96 typeName = NewPoolTString(p.userDef->getTypeName().c_str());
97 }
98 }
99 explicit TType(TTypeList* userDef, TString n) :
100 type(EbtStruct), qualifier(EvqTemporary), size(1), matrix(false), array(false), arraySize(0),
101 structure(userDef), maxArraySize(0), arrayInformationType(0), fieldName(0), mangled(0) {
102 structureSize = setStructSize(userDef);
103 typeName = NewPoolTString(n.c_str());
104 }
105 explicit TType() {}
106 virtual ~TType() {}
107
108 TType (const TType& type) { *this = type; }
109
110 void copyType(const TType& copyOf, TStructureMap& remapper)
111 {
112 type = copyOf.type;
113 qualifier = copyOf.qualifier;
114 size = copyOf.size;
115 matrix = copyOf.matrix;
116 array = copyOf.array;
117 arraySize = copyOf.arraySize;
118
119 TStructureMapIterator iter;
120 if (copyOf.structure) {
121 if ((iter = remapper.find(structure)) == remapper.end()) {
122 // create the new structure here
123 structure = NewPoolTTypeList();
124 for (unsigned int i = 0; i < copyOf.structure->size(); ++i) {
125 TTypeLine typeLine;
126 typeLine.line = (*copyOf.structure)[i].line;
127 typeLine.type = (*copyOf.structure)[i].type->clone(remapper);
128 structure->push_back(typeLine);
129 }
130 } else {
131 structure = iter->second;
132 }
133 } else
134 structure = 0;
135
136 fieldName = 0;
137 if (copyOf.fieldName)
138 fieldName = NewPoolTString(copyOf.fieldName->c_str());
139 typeName = 0;
140 if (copyOf.typeName)
141 typeName = NewPoolTString(copyOf.typeName->c_str());
142
143 mangled = 0;
144 if (copyOf.mangled)
145 mangled = NewPoolTString(copyOf.mangled->c_str());
146
147 structureSize = copyOf.structureSize;
148 maxArraySize = copyOf.maxArraySize;
149 assert (copyOf.arrayInformationType == 0);
150 arrayInformationType = 0; // arrayInformationType should not be set for builtIn symbol table level
151 }
152
153 TType* clone(TStructureMap& remapper)
154 {
155 TType *newType = new TType();
156 newType->copyType(*this, remapper);
157
158 return newType;
159 }
160
161 int setStructSize(TTypeList* userDef)
162 {
163 int stSize = 0;
164 for (TTypeList::iterator tl = userDef->begin(); tl != userDef->end(); tl++) {
165 if (((*tl).type)->isArray()) {
166 if (((*tl).type)->getStruct()) {
167 int structSize = setStructSize(((*tl).type)->getStruct());
168 stSize += structSize * ((*tl).type)->getArraySize();
169 } else {
170 stSize += ((*tl).type)->getInstanceSize() * ((*tl).type)->getArraySize();
171 }
172 } else if (((*tl).type)->isMatrix() || ((*tl).type)->isVector()){
173 stSize += ((*tl).type)->getInstanceSize();
174 } else if (((*tl).type)->getStruct()) {
175 //?? We should actually be calling getStructSize() function and not setStructSize. This problem occurs in case
176 // of nested/embedded structs.
177 stSize += setStructSize(((*tl).type)->getStruct());
178 } else
179 stSize += 1;
180 }
181 structureSize = stSize;
182 return stSize;
183 }
184
185 virtual void setType(TBasicType t, int s, bool m, bool a, int aS = 0)
186 { type = t; size = s; matrix = m; array = a; arraySize = aS; }
187 virtual void setType(TBasicType t, int s, bool m, TType* userDef = 0)
188 { type = t;
189 size = s;
190 matrix = m;
191 if (userDef)
192 structure = userDef->getStruct();
193 // leave array information intact.
194 }
195 virtual void setTypeName(const TString& n) { typeName = NewPoolTString(n.c_str()); }
196 virtual void setFieldName(const TString& n) { fieldName = NewPoolTString(n.c_str()); }
197 virtual const TString& getTypeName() const
198 {
199 assert (typeName);
200 return *typeName;
201 }
202
203 virtual const TString& getFieldName() const
204 {
205 assert (fieldName);
206 return *fieldName;
207 }
208
209 virtual TBasicType getBasicType() const { return type; }
210 virtual TQualifier getQualifier() const { return qualifier; }
211 virtual void changeQualifier(TQualifier q) { qualifier = q; }
212
213 // One-dimensional size of single instance type
214 virtual int getNominalSize() const { return size; }
215
216 // Full-dimensional size of single instance of type
217 virtual int getInstanceSize() const
218 {
219 if (matrix)
220 return size * size;
221 else
222 return size;
223 }
224
225 virtual bool isMatrix() const { return matrix ? true : false; }
226 virtual bool isArray() const { return array ? true : false; }
227 int getArraySize() const { return arraySize; }
228 void setArraySize(int s) { array = true; arraySize = s; }
229 void setMaxArraySize (int s) { maxArraySize = s; }
230 int getMaxArraySize () const { return maxArraySize; }
231 void setArrayInformationType(TType* t) { arrayInformationType = t; }
232 TType* getArrayInformationType() { return arrayInformationType; }
233 virtual bool isVector() const { return size > 1 && !matrix; }
234 static char* getBasicString(TBasicType t) {
235 switch (t) {
236 case EbtVoid: return "void"; break;
237 case EbtFloat: return "float"; break;
238 case EbtInt: return "int"; break;
239 case EbtBool: return "bool"; break;
240 case EbtSampler1D: return "sampler1D"; break;
241 case EbtSampler2D: return "sampler2D"; break;
242 case EbtSampler3D: return "sampler3D"; break;
243 case EbtSamplerCube: return "samplerCube"; break;
244 case EbtSampler1DShadow: return "sampler1DShadow"; break;
245 case EbtSampler2DShadow: return "sampler2DShadow"; break;
246 case EbtStruct: return "structure"; break;
247 default: return "unknown type";
248 }
249 }
250 const char* getBasicString() const { return TType::getBasicString(type); }
251 const char* getQualifierString() const { return ::getQualifierString(qualifier); }
252 TTypeList* getStruct() { return structure; }
253 int getStructSize() const { return structureSize; }
254 TTypeList* getStruct() const { return structure; }
255 TString& getMangledName() {
256 if (!mangled) {
257 mangled = NewPoolTString("");
258 buildMangledName(*mangled);
259 *mangled+=';';
260 }
261
262 return *mangled;
263 }
264 bool operator==(const TType& right) const {
265 return type == right.type &&
266 size == right.size &&
267 matrix == right.matrix &&
268 array == right.array &&
269 structure == right.structure;
270 // don't check the qualifier, it's not ever what's being sought after
271 }
272 bool operator!=(const TType& right) const {
273 return !operator==(right);
274 }
275 TString getCompleteString() const;
276
277 protected:
278 void buildMangledName(TString&);
279
280 TBasicType type : 6;
281 TQualifier qualifier : 7;
282 int size : 8; // size of vector or matrix, not size of array
283 unsigned int matrix : 1;
284 unsigned int array : 1;
285
286 int arraySize;
287 TTypeList* structure; // 0 unless this is a struct
288 int structureSize;
289 int maxArraySize;
290 TType* arrayInformationType;
291 TString *fieldName; // for structure field names
292 TString *typeName; // for structure field type name
293 TString *mangled;
294
295 };
296
297 #endif // _TYPES_INCLUDED_