blob: 3a012582c0e545ec2cde66496001a54a3aef79c2 [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;
Nicolas Capens8772b582013-06-24 16:14:19 -040099 case EbtSampler3D: return SH_SAMPLER_3D;
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000100 case EbtSamplerCube: return SH_SAMPLER_CUBE;
apatrick@chromium.org65756022012-01-17 21:45:38 +0000101 case EbtSamplerExternalOES: return SH_SAMPLER_EXTERNAL_OES;
kbr@chromium.org205fef32011-11-22 20:50:02 +0000102 case EbtSampler2DRect: return SH_SAMPLER_2D_RECT_ARB;
Nicolas Capens8772b582013-06-24 16:14:19 -0400103 case EbtSampler2DArray: return SH_SAMPLER_2D_ARRAY;
Nicolas Capens344e7142013-06-24 15:39:21 -0400104 case EbtISampler2D: return SH_INT_SAMPLER_2D;
Nicolas Capens8772b582013-06-24 16:14:19 -0400105 case EbtISampler3D: return SH_INT_SAMPLER_3D;
Nicolas Capens344e7142013-06-24 15:39:21 -0400106 case EbtISamplerCube: return SH_INT_SAMPLER_CUBE;
Nicolas Capens8772b582013-06-24 16:14:19 -0400107 case EbtISampler2DArray: return SH_INT_SAMPLER_2D_ARRAY;
Nicolas Capens2ffe0bb2013-06-24 15:56:19 -0400108 case EbtUSampler2D: return SH_UNSIGNED_INT_SAMPLER_2D;
Nicolas Capens8772b582013-06-24 16:14:19 -0400109 case EbtUSampler3D: return SH_UNSIGNED_INT_SAMPLER_3D;
Nicolas Capens2ffe0bb2013-06-24 15:56:19 -0400110 case EbtUSamplerCube: return SH_UNSIGNED_INT_SAMPLER_CUBE;
Nicolas Capens8772b582013-06-24 16:14:19 -0400111 case EbtUSampler2DArray: return SH_UNSIGNED_INT_SAMPLER_2D_ARRAY;
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000112 default: UNREACHABLE();
113 }
114 return SH_NONE;
115}
116
117static void getBuiltInVariableInfo(const TType& type,
118 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000119 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000120 TVariableInfoList& infoList);
121static void getUserDefinedVariableInfo(const TType& type,
122 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000123 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000124 TVariableInfoList& infoList,
125 ShHashFunction64 hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000126
127// Returns info for an attribute or uniform.
128static void getVariableInfo(const TType& type,
129 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000130 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000131 TVariableInfoList& infoList,
132 ShHashFunction64 hashFunction)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000133{
134 if (type.getBasicType() == EbtStruct) {
135 if (type.isArray()) {
136 for (int i = 0; i < type.getArraySize(); ++i) {
137 TString lname = name + arrayBrackets(i);
zmo@google.comfd747b82011-04-23 01:30:07 +0000138 TString lmappedName = mappedName + arrayBrackets(i);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000139 getUserDefinedVariableInfo(type, lname, lmappedName, infoList, hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000140 }
141 } else {
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000142 getUserDefinedVariableInfo(type, name, mappedName, infoList, hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000143 }
144 } else {
zmo@google.comfd747b82011-04-23 01:30:07 +0000145 getBuiltInVariableInfo(type, name, mappedName, infoList);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000146 }
147}
148
149void getBuiltInVariableInfo(const TType& type,
150 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000151 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000152 TVariableInfoList& infoList)
153{
154 ASSERT(type.getBasicType() != EbtStruct);
155
156 TVariableInfo varInfo;
157 if (type.isArray()) {
158 varInfo.name = (name + "[0]").c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000159 varInfo.mappedName = (mappedName + "[0]").c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000160 varInfo.size = type.getArraySize();
161 } else {
162 varInfo.name = name.c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000163 varInfo.mappedName = mappedName.c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000164 varInfo.size = 1;
165 }
166 varInfo.type = getVariableDataType(type);
167 infoList.push_back(varInfo);
168}
169
170void getUserDefinedVariableInfo(const TType& type,
171 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000172 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000173 TVariableInfoList& infoList,
174 ShHashFunction64 hashFunction)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000175{
Jamie Madill98493dd2013-07-08 14:39:03 -0400176 ASSERT(type.getBasicType() == EbtStruct || type.isInterfaceBlock());
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000177
Jamie Madill98493dd2013-07-08 14:39:03 -0400178 const TFieldList& fields = type.getStruct()->fields();
179 for (size_t i = 0; i < fields.size(); ++i) {
180 const TType& fieldType = *(fields[i]->type());
181 const TString& fieldName = fields[i]->name();
182 getVariableInfo(fieldType,
183 name + "." + fieldName,
184 mappedName + "." + TIntermTraverser::hash(fieldName, hashFunction),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000185 infoList,
186 hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000187 }
188}
189
gman@chromium.org8d804792012-10-17 21:33:48 +0000190TVariableInfo::TVariableInfo()
191{
192}
193
194TVariableInfo::TVariableInfo(ShDataType type, int size)
195 : type(type),
196 size(size)
197{
198}
199
alokp@chromium.org07620a52010-09-23 17:53:56 +0000200CollectAttribsUniforms::CollectAttribsUniforms(TVariableInfoList& attribs,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000201 TVariableInfoList& uniforms,
202 ShHashFunction64 hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000203 : mAttribs(attribs),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000204 mUniforms(uniforms),
205 mHashFunction(hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000206{
207}
208
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000209// We are only interested in attribute and uniform variable declaration.
alokp@chromium.org07620a52010-09-23 17:53:56 +0000210void CollectAttribsUniforms::visitSymbol(TIntermSymbol*)
211{
212}
213
214void CollectAttribsUniforms::visitConstantUnion(TIntermConstantUnion*)
215{
216}
217
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000218bool CollectAttribsUniforms::visitBinary(Visit, TIntermBinary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000219{
220 return false;
221}
222
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000223bool CollectAttribsUniforms::visitUnary(Visit, TIntermUnary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000224{
225 return false;
226}
227
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000228bool CollectAttribsUniforms::visitSelection(Visit, TIntermSelection*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000229{
230 return false;
231}
232
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000233bool CollectAttribsUniforms::visitAggregate(Visit, TIntermAggregate* node)
234{
235 bool visitChildren = false;
236
237 switch (node->getOp())
238 {
239 case EOpSequence:
240 // We need to visit sequence children to get to variable declarations.
241 visitChildren = true;
242 break;
243 case EOpDeclaration: {
244 const TIntermSequence& sequence = node->getSequence();
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000245 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
Jamie Madillb120eac2013-06-12 14:08:13 -0400246 if (qualifier == EvqAttribute || qualifier == EvqVertexInput || qualifier == EvqUniform)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000247 {
Jamie Madillb120eac2013-06-12 14:08:13 -0400248 TVariableInfoList& infoList = (qualifier == EvqAttribute || qualifier == EvqVertexInput) ?
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000249 mAttribs : mUniforms;
250 for (TIntermSequence::const_iterator i = sequence.begin();
251 i != sequence.end(); ++i)
252 {
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000253 const TIntermSymbol* variable = (*i)->getAsSymbolNode();
254 // The only case in which the sequence will not contain a
255 // TIntermSymbol node is initialization. It will contain a
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000256 // TIntermBinary node in that case. Since attributes and uniforms
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000257 // cannot be initialized in a shader, we must have only
258 // TIntermSymbol nodes in the sequence.
259 ASSERT(variable != NULL);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000260 TString processedSymbol;
261 if (mHashFunction == NULL)
262 processedSymbol = variable->getSymbol();
263 else
264 processedSymbol = TIntermTraverser::hash(variable->getOriginalSymbol(), mHashFunction);
zmo@google.comfd747b82011-04-23 01:30:07 +0000265 getVariableInfo(variable->getType(),
266 variable->getOriginalSymbol(),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000267 processedSymbol,
268 infoList,
269 mHashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000270 }
271 }
272 break;
273 }
274 default: break;
275 }
276
277 return visitChildren;
278}
279
280bool CollectAttribsUniforms::visitLoop(Visit, TIntermLoop*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000281{
282 return false;
283}
284
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000285bool CollectAttribsUniforms::visitBranch(Visit, TIntermBranch*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000286{
287 return false;
288}
289