blob: 9b0e91026dafa17ddacf4625286cf7ec1e088520 [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
alokp@chromium.org07620a52010-09-23 17:53:56 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#include "compiler/VariableInfo.h"
8
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +00009static TString arrayBrackets(int index)
10{
11 TStringStream stream;
12 stream << "[" << index << "]";
13 return stream.str();
14}
15
16// Returns the data type for an attribute or uniform.
alokp@chromium.org4888ceb2010-10-01 21:13:12 +000017static ShDataType getVariableDataType(const TType& type)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000018{
19 switch (type.getBasicType()) {
20 case EbtFloat:
21 if (type.isMatrix()) {
shannonwoods@chromium.org8da034c2013-05-30 00:19:15 +000022 switch (type.getCols())
23 {
24 case 2:
25 switch (type.getRows())
26 {
27 case 2: return SH_FLOAT_MAT2;
28 case 3: return SH_FLOAT_MAT2x3;
29 case 4: return SH_FLOAT_MAT2x4;
30 default: UNREACHABLE();
31 }
32 case 3:
33 switch (type.getRows())
34 {
35 case 2: return SH_FLOAT_MAT3x2;
36 case 3: return SH_FLOAT_MAT3;
37 case 4: return SH_FLOAT_MAT3x4;
38 default: UNREACHABLE();
39 }
40 case 4:
41 switch (type.getRows())
42 {
43 case 2: return SH_FLOAT_MAT4x2;
44 case 3: return SH_FLOAT_MAT4x3;
45 case 4: return SH_FLOAT_MAT4;
46 default: UNREACHABLE();
47 }
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000048 }
49 } else if (type.isVector()) {
50 switch (type.getNominalSize()) {
51 case 2: return SH_FLOAT_VEC2;
52 case 3: return SH_FLOAT_VEC3;
53 case 4: return SH_FLOAT_VEC4;
54 default: UNREACHABLE();
55 }
56 } else {
57 return SH_FLOAT;
58 }
59 case EbtInt:
60 if (type.isMatrix()) {
61 UNREACHABLE();
62 } else if (type.isVector()) {
63 switch (type.getNominalSize()) {
64 case 2: return SH_INT_VEC2;
65 case 3: return SH_INT_VEC3;
66 case 4: return SH_INT_VEC4;
67 default: UNREACHABLE();
68 }
69 } else {
70 return SH_INT;
71 }
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +000072 case EbtUInt:
73 if (type.isMatrix()) {
74 UNREACHABLE();
75 } else if (type.isVector()) {
Jamie Madill22d63da2013-06-07 12:45:12 -040076 switch (type.getNominalSize()) {
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +000077 case 2: return SH_UNSIGNED_INT_VEC2;
78 case 3: return SH_UNSIGNED_INT_VEC3;
79 case 4: return SH_UNSIGNED_INT_VEC4;
80 default: UNREACHABLE();
81 }
82 } else {
83 return SH_UNSIGNED_INT;
84 }
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000085 case EbtBool:
86 if (type.isMatrix()) {
87 UNREACHABLE();
88 } else if (type.isVector()) {
89 switch (type.getNominalSize()) {
90 case 2: return SH_BOOL_VEC2;
91 case 3: return SH_BOOL_VEC3;
92 case 4: return SH_BOOL_VEC4;
93 default: UNREACHABLE();
94 }
95 } else {
96 return SH_BOOL;
97 }
98 case EbtSampler2D: return SH_SAMPLER_2D;
99 case EbtSamplerCube: return SH_SAMPLER_CUBE;
apatrick@chromium.org65756022012-01-17 21:45:38 +0000100 case EbtSamplerExternalOES: return SH_SAMPLER_EXTERNAL_OES;
kbr@chromium.org205fef32011-11-22 20:50:02 +0000101 case EbtSampler2DRect: return SH_SAMPLER_2D_RECT_ARB;
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000102 default: UNREACHABLE();
103 }
104 return SH_NONE;
105}
106
107static void getBuiltInVariableInfo(const TType& type,
108 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000109 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000110 TVariableInfoList& infoList);
111static void getUserDefinedVariableInfo(const TType& type,
112 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000113 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000114 TVariableInfoList& infoList,
115 ShHashFunction64 hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000116
117// Returns info for an attribute or uniform.
118static void getVariableInfo(const TType& type,
119 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000120 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000121 TVariableInfoList& infoList,
122 ShHashFunction64 hashFunction)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000123{
124 if (type.getBasicType() == EbtStruct) {
125 if (type.isArray()) {
126 for (int i = 0; i < type.getArraySize(); ++i) {
127 TString lname = name + arrayBrackets(i);
zmo@google.comfd747b82011-04-23 01:30:07 +0000128 TString lmappedName = mappedName + arrayBrackets(i);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000129 getUserDefinedVariableInfo(type, lname, lmappedName, infoList, hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000130 }
131 } else {
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000132 getUserDefinedVariableInfo(type, name, mappedName, infoList, hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000133 }
134 } else {
zmo@google.comfd747b82011-04-23 01:30:07 +0000135 getBuiltInVariableInfo(type, name, mappedName, infoList);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000136 }
137}
138
139void getBuiltInVariableInfo(const TType& type,
140 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000141 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000142 TVariableInfoList& infoList)
143{
144 ASSERT(type.getBasicType() != EbtStruct);
145
146 TVariableInfo varInfo;
147 if (type.isArray()) {
148 varInfo.name = (name + "[0]").c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000149 varInfo.mappedName = (mappedName + "[0]").c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000150 varInfo.size = type.getArraySize();
151 } else {
152 varInfo.name = name.c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000153 varInfo.mappedName = mappedName.c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000154 varInfo.size = 1;
155 }
156 varInfo.type = getVariableDataType(type);
157 infoList.push_back(varInfo);
158}
159
160void getUserDefinedVariableInfo(const TType& type,
161 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000162 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000163 TVariableInfoList& infoList,
164 ShHashFunction64 hashFunction)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000165{
166 ASSERT(type.getBasicType() == EbtStruct);
167
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000168 const TTypeList* structure = type.getStruct();
169 for (size_t i = 0; i < structure->size(); ++i) {
170 const TType* fieldType = (*structure)[i].type;
171 getVariableInfo(*fieldType,
zmo@google.comfd747b82011-04-23 01:30:07 +0000172 name + "." + fieldType->getFieldName(),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000173 mappedName + "." + TIntermTraverser::hash(fieldType->getFieldName(), hashFunction),
174 infoList,
175 hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000176 }
177}
178
gman@chromium.org8d804792012-10-17 21:33:48 +0000179TVariableInfo::TVariableInfo()
180{
181}
182
183TVariableInfo::TVariableInfo(ShDataType type, int size)
184 : type(type),
185 size(size)
186{
187}
188
alokp@chromium.org07620a52010-09-23 17:53:56 +0000189CollectAttribsUniforms::CollectAttribsUniforms(TVariableInfoList& attribs,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000190 TVariableInfoList& uniforms,
191 ShHashFunction64 hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000192 : mAttribs(attribs),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000193 mUniforms(uniforms),
194 mHashFunction(hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000195{
196}
197
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000198// We are only interested in attribute and uniform variable declaration.
alokp@chromium.org07620a52010-09-23 17:53:56 +0000199void CollectAttribsUniforms::visitSymbol(TIntermSymbol*)
200{
201}
202
203void CollectAttribsUniforms::visitConstantUnion(TIntermConstantUnion*)
204{
205}
206
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000207bool CollectAttribsUniforms::visitBinary(Visit, TIntermBinary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000208{
209 return false;
210}
211
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000212bool CollectAttribsUniforms::visitUnary(Visit, TIntermUnary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000213{
214 return false;
215}
216
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000217bool CollectAttribsUniforms::visitSelection(Visit, TIntermSelection*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000218{
219 return false;
220}
221
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000222bool CollectAttribsUniforms::visitAggregate(Visit, TIntermAggregate* node)
223{
224 bool visitChildren = false;
225
226 switch (node->getOp())
227 {
228 case EOpSequence:
229 // We need to visit sequence children to get to variable declarations.
230 visitChildren = true;
231 break;
232 case EOpDeclaration: {
233 const TIntermSequence& sequence = node->getSequence();
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000234 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
Jamie Madillb120eac2013-06-12 14:08:13 -0400235 if (qualifier == EvqAttribute || qualifier == EvqVertexInput || qualifier == EvqUniform)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000236 {
Jamie Madillb120eac2013-06-12 14:08:13 -0400237 TVariableInfoList& infoList = (qualifier == EvqAttribute || qualifier == EvqVertexInput) ?
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000238 mAttribs : mUniforms;
239 for (TIntermSequence::const_iterator i = sequence.begin();
240 i != sequence.end(); ++i)
241 {
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000242 const TIntermSymbol* variable = (*i)->getAsSymbolNode();
243 // The only case in which the sequence will not contain a
244 // TIntermSymbol node is initialization. It will contain a
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000245 // TIntermBinary node in that case. Since attributes and uniforms
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000246 // cannot be initialized in a shader, we must have only
247 // TIntermSymbol nodes in the sequence.
248 ASSERT(variable != NULL);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000249 TString processedSymbol;
250 if (mHashFunction == NULL)
251 processedSymbol = variable->getSymbol();
252 else
253 processedSymbol = TIntermTraverser::hash(variable->getOriginalSymbol(), mHashFunction);
zmo@google.comfd747b82011-04-23 01:30:07 +0000254 getVariableInfo(variable->getType(),
255 variable->getOriginalSymbol(),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000256 processedSymbol,
257 infoList,
258 mHashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000259 }
260 }
261 break;
262 }
263 default: break;
264 }
265
266 return visitChildren;
267}
268
269bool CollectAttribsUniforms::visitLoop(Visit, TIntermLoop*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000270{
271 return false;
272}
273
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000274bool CollectAttribsUniforms::visitBranch(Visit, TIntermBranch*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000275{
276 return false;
277}
278