blob: e6163bd0eba5d862062ad1b8de7b031ea3f94e9c [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;
Nicolas Capens344e7142013-06-24 15:39:21 -0400102 case EbtISampler2D: return SH_INT_SAMPLER_2D;
103 case EbtISamplerCube: return SH_INT_SAMPLER_CUBE;
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000104 default: UNREACHABLE();
105 }
106 return SH_NONE;
107}
108
109static void getBuiltInVariableInfo(const TType& type,
110 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000111 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000112 TVariableInfoList& infoList);
113static void getUserDefinedVariableInfo(const TType& type,
114 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000115 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000116 TVariableInfoList& infoList,
117 ShHashFunction64 hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000118
119// Returns info for an attribute or uniform.
120static void getVariableInfo(const TType& type,
121 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000122 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000123 TVariableInfoList& infoList,
124 ShHashFunction64 hashFunction)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000125{
126 if (type.getBasicType() == EbtStruct) {
127 if (type.isArray()) {
128 for (int i = 0; i < type.getArraySize(); ++i) {
129 TString lname = name + arrayBrackets(i);
zmo@google.comfd747b82011-04-23 01:30:07 +0000130 TString lmappedName = mappedName + arrayBrackets(i);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000131 getUserDefinedVariableInfo(type, lname, lmappedName, infoList, hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000132 }
133 } else {
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000134 getUserDefinedVariableInfo(type, name, mappedName, infoList, hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000135 }
136 } else {
zmo@google.comfd747b82011-04-23 01:30:07 +0000137 getBuiltInVariableInfo(type, name, mappedName, infoList);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000138 }
139}
140
141void getBuiltInVariableInfo(const TType& type,
142 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000143 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000144 TVariableInfoList& infoList)
145{
146 ASSERT(type.getBasicType() != EbtStruct);
147
148 TVariableInfo varInfo;
149 if (type.isArray()) {
150 varInfo.name = (name + "[0]").c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000151 varInfo.mappedName = (mappedName + "[0]").c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000152 varInfo.size = type.getArraySize();
153 } else {
154 varInfo.name = name.c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000155 varInfo.mappedName = mappedName.c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000156 varInfo.size = 1;
157 }
158 varInfo.type = getVariableDataType(type);
159 infoList.push_back(varInfo);
160}
161
162void getUserDefinedVariableInfo(const TType& type,
163 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000164 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000165 TVariableInfoList& infoList,
166 ShHashFunction64 hashFunction)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000167{
168 ASSERT(type.getBasicType() == EbtStruct);
169
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000170 const TTypeList* structure = type.getStruct();
171 for (size_t i = 0; i < structure->size(); ++i) {
172 const TType* fieldType = (*structure)[i].type;
173 getVariableInfo(*fieldType,
zmo@google.comfd747b82011-04-23 01:30:07 +0000174 name + "." + fieldType->getFieldName(),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000175 mappedName + "." + TIntermTraverser::hash(fieldType->getFieldName(), hashFunction),
176 infoList,
177 hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000178 }
179}
180
gman@chromium.org8d804792012-10-17 21:33:48 +0000181TVariableInfo::TVariableInfo()
182{
183}
184
185TVariableInfo::TVariableInfo(ShDataType type, int size)
186 : type(type),
187 size(size)
188{
189}
190
alokp@chromium.org07620a52010-09-23 17:53:56 +0000191CollectAttribsUniforms::CollectAttribsUniforms(TVariableInfoList& attribs,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000192 TVariableInfoList& uniforms,
193 ShHashFunction64 hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000194 : mAttribs(attribs),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000195 mUniforms(uniforms),
196 mHashFunction(hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000197{
198}
199
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000200// We are only interested in attribute and uniform variable declaration.
alokp@chromium.org07620a52010-09-23 17:53:56 +0000201void CollectAttribsUniforms::visitSymbol(TIntermSymbol*)
202{
203}
204
205void CollectAttribsUniforms::visitConstantUnion(TIntermConstantUnion*)
206{
207}
208
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000209bool CollectAttribsUniforms::visitBinary(Visit, TIntermBinary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000210{
211 return false;
212}
213
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000214bool CollectAttribsUniforms::visitUnary(Visit, TIntermUnary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000215{
216 return false;
217}
218
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000219bool CollectAttribsUniforms::visitSelection(Visit, TIntermSelection*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000220{
221 return false;
222}
223
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000224bool CollectAttribsUniforms::visitAggregate(Visit, TIntermAggregate* node)
225{
226 bool visitChildren = false;
227
228 switch (node->getOp())
229 {
230 case EOpSequence:
231 // We need to visit sequence children to get to variable declarations.
232 visitChildren = true;
233 break;
234 case EOpDeclaration: {
235 const TIntermSequence& sequence = node->getSequence();
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000236 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
Jamie Madillb120eac2013-06-12 14:08:13 -0400237 if (qualifier == EvqAttribute || qualifier == EvqVertexInput || qualifier == EvqUniform)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000238 {
Jamie Madillb120eac2013-06-12 14:08:13 -0400239 TVariableInfoList& infoList = (qualifier == EvqAttribute || qualifier == EvqVertexInput) ?
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000240 mAttribs : mUniforms;
241 for (TIntermSequence::const_iterator i = sequence.begin();
242 i != sequence.end(); ++i)
243 {
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000244 const TIntermSymbol* variable = (*i)->getAsSymbolNode();
245 // The only case in which the sequence will not contain a
246 // TIntermSymbol node is initialization. It will contain a
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000247 // TIntermBinary node in that case. Since attributes and uniforms
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000248 // cannot be initialized in a shader, we must have only
249 // TIntermSymbol nodes in the sequence.
250 ASSERT(variable != NULL);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000251 TString processedSymbol;
252 if (mHashFunction == NULL)
253 processedSymbol = variable->getSymbol();
254 else
255 processedSymbol = TIntermTraverser::hash(variable->getOriginalSymbol(), mHashFunction);
zmo@google.comfd747b82011-04-23 01:30:07 +0000256 getVariableInfo(variable->getType(),
257 variable->getOriginalSymbol(),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000258 processedSymbol,
259 infoList,
260 mHashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000261 }
262 }
263 break;
264 }
265 default: break;
266 }
267
268 return visitChildren;
269}
270
271bool CollectAttribsUniforms::visitLoop(Visit, TIntermLoop*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000272{
273 return false;
274}
275
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000276bool CollectAttribsUniforms::visitBranch(Visit, TIntermBranch*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000277{
278 return false;
279}
280