blob: cce5e366d35823ce0d33934bd486479c95ef2c11 [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{
176 ASSERT(type.getBasicType() == EbtStruct);
177
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000178 const TTypeList* structure = type.getStruct();
179 for (size_t i = 0; i < structure->size(); ++i) {
180 const TType* fieldType = (*structure)[i].type;
181 getVariableInfo(*fieldType,
zmo@google.comfd747b82011-04-23 01:30:07 +0000182 name + "." + fieldType->getFieldName(),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000183 mappedName + "." + TIntermTraverser::hash(fieldType->getFieldName(), hashFunction),
184 infoList,
185 hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000186 }
187}
188
gman@chromium.org8d804792012-10-17 21:33:48 +0000189TVariableInfo::TVariableInfo()
190{
191}
192
193TVariableInfo::TVariableInfo(ShDataType type, int size)
194 : type(type),
195 size(size)
196{
197}
198
alokp@chromium.org07620a52010-09-23 17:53:56 +0000199CollectAttribsUniforms::CollectAttribsUniforms(TVariableInfoList& attribs,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000200 TVariableInfoList& uniforms,
201 ShHashFunction64 hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000202 : mAttribs(attribs),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000203 mUniforms(uniforms),
204 mHashFunction(hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000205{
206}
207
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000208// We are only interested in attribute and uniform variable declaration.
alokp@chromium.org07620a52010-09-23 17:53:56 +0000209void CollectAttribsUniforms::visitSymbol(TIntermSymbol*)
210{
211}
212
213void CollectAttribsUniforms::visitConstantUnion(TIntermConstantUnion*)
214{
215}
216
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000217bool CollectAttribsUniforms::visitBinary(Visit, TIntermBinary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000218{
219 return false;
220}
221
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000222bool CollectAttribsUniforms::visitUnary(Visit, TIntermUnary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000223{
224 return false;
225}
226
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000227bool CollectAttribsUniforms::visitSelection(Visit, TIntermSelection*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000228{
229 return false;
230}
231
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000232bool CollectAttribsUniforms::visitAggregate(Visit, TIntermAggregate* node)
233{
234 bool visitChildren = false;
235
236 switch (node->getOp())
237 {
238 case EOpSequence:
239 // We need to visit sequence children to get to variable declarations.
240 visitChildren = true;
241 break;
242 case EOpDeclaration: {
243 const TIntermSequence& sequence = node->getSequence();
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000244 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
Jamie Madillb120eac2013-06-12 14:08:13 -0400245 if (qualifier == EvqAttribute || qualifier == EvqVertexInput || qualifier == EvqUniform)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000246 {
Jamie Madillb120eac2013-06-12 14:08:13 -0400247 TVariableInfoList& infoList = (qualifier == EvqAttribute || qualifier == EvqVertexInput) ?
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000248 mAttribs : mUniforms;
249 for (TIntermSequence::const_iterator i = sequence.begin();
250 i != sequence.end(); ++i)
251 {
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000252 const TIntermSymbol* variable = (*i)->getAsSymbolNode();
253 // The only case in which the sequence will not contain a
254 // TIntermSymbol node is initialization. It will contain a
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000255 // TIntermBinary node in that case. Since attributes and uniforms
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000256 // cannot be initialized in a shader, we must have only
257 // TIntermSymbol nodes in the sequence.
258 ASSERT(variable != NULL);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000259 TString processedSymbol;
260 if (mHashFunction == NULL)
261 processedSymbol = variable->getSymbol();
262 else
263 processedSymbol = TIntermTraverser::hash(variable->getOriginalSymbol(), mHashFunction);
zmo@google.comfd747b82011-04-23 01:30:07 +0000264 getVariableInfo(variable->getType(),
265 variable->getOriginalSymbol(),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000266 processedSymbol,
267 infoList,
268 mHashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000269 }
270 }
271 break;
272 }
273 default: break;
274 }
275
276 return visitChildren;
277}
278
279bool CollectAttribsUniforms::visitLoop(Visit, TIntermLoop*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000280{
281 return false;
282}
283
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000284bool CollectAttribsUniforms::visitBranch(Visit, TIntermBranch*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000285{
286 return false;
287}
288