blob: 388fe7d4639eac2fdee8c136d74cf9ad8cb8cb60 [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 }
72 case EbtBool:
73 if (type.isMatrix()) {
74 UNREACHABLE();
75 } else if (type.isVector()) {
76 switch (type.getNominalSize()) {
77 case 2: return SH_BOOL_VEC2;
78 case 3: return SH_BOOL_VEC3;
79 case 4: return SH_BOOL_VEC4;
80 default: UNREACHABLE();
81 }
82 } else {
83 return SH_BOOL;
84 }
85 case EbtSampler2D: return SH_SAMPLER_2D;
86 case EbtSamplerCube: return SH_SAMPLER_CUBE;
apatrick@chromium.org65756022012-01-17 21:45:38 +000087 case EbtSamplerExternalOES: return SH_SAMPLER_EXTERNAL_OES;
kbr@chromium.org205fef32011-11-22 20:50:02 +000088 case EbtSampler2DRect: return SH_SAMPLER_2D_RECT_ARB;
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000089 default: UNREACHABLE();
90 }
91 return SH_NONE;
92}
93
94static void getBuiltInVariableInfo(const TType& type,
95 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +000096 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000097 TVariableInfoList& infoList);
98static void getUserDefinedVariableInfo(const TType& type,
99 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000100 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000101 TVariableInfoList& infoList,
102 ShHashFunction64 hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000103
104// Returns info for an attribute or uniform.
105static void getVariableInfo(const TType& type,
106 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000107 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000108 TVariableInfoList& infoList,
109 ShHashFunction64 hashFunction)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000110{
111 if (type.getBasicType() == EbtStruct) {
112 if (type.isArray()) {
113 for (int i = 0; i < type.getArraySize(); ++i) {
114 TString lname = name + arrayBrackets(i);
zmo@google.comfd747b82011-04-23 01:30:07 +0000115 TString lmappedName = mappedName + arrayBrackets(i);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000116 getUserDefinedVariableInfo(type, lname, lmappedName, infoList, hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000117 }
118 } else {
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000119 getUserDefinedVariableInfo(type, name, mappedName, infoList, hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000120 }
121 } else {
zmo@google.comfd747b82011-04-23 01:30:07 +0000122 getBuiltInVariableInfo(type, name, mappedName, infoList);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000123 }
124}
125
126void getBuiltInVariableInfo(const TType& type,
127 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000128 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000129 TVariableInfoList& infoList)
130{
131 ASSERT(type.getBasicType() != EbtStruct);
132
133 TVariableInfo varInfo;
134 if (type.isArray()) {
135 varInfo.name = (name + "[0]").c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000136 varInfo.mappedName = (mappedName + "[0]").c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000137 varInfo.size = type.getArraySize();
138 } else {
139 varInfo.name = name.c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000140 varInfo.mappedName = mappedName.c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000141 varInfo.size = 1;
142 }
143 varInfo.type = getVariableDataType(type);
144 infoList.push_back(varInfo);
145}
146
147void getUserDefinedVariableInfo(const TType& type,
148 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000149 const TString& mappedName,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000150 TVariableInfoList& infoList,
151 ShHashFunction64 hashFunction)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000152{
153 ASSERT(type.getBasicType() == EbtStruct);
154
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000155 const TTypeList* structure = type.getStruct();
156 for (size_t i = 0; i < structure->size(); ++i) {
157 const TType* fieldType = (*structure)[i].type;
158 getVariableInfo(*fieldType,
zmo@google.comfd747b82011-04-23 01:30:07 +0000159 name + "." + fieldType->getFieldName(),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000160 mappedName + "." + TIntermTraverser::hash(fieldType->getFieldName(), hashFunction),
161 infoList,
162 hashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000163 }
164}
165
gman@chromium.org8d804792012-10-17 21:33:48 +0000166TVariableInfo::TVariableInfo()
167{
168}
169
170TVariableInfo::TVariableInfo(ShDataType type, int size)
171 : type(type),
172 size(size)
173{
174}
175
alokp@chromium.org07620a52010-09-23 17:53:56 +0000176CollectAttribsUniforms::CollectAttribsUniforms(TVariableInfoList& attribs,
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000177 TVariableInfoList& uniforms,
178 ShHashFunction64 hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000179 : mAttribs(attribs),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000180 mUniforms(uniforms),
181 mHashFunction(hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000182{
183}
184
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000185// We are only interested in attribute and uniform variable declaration.
alokp@chromium.org07620a52010-09-23 17:53:56 +0000186void CollectAttribsUniforms::visitSymbol(TIntermSymbol*)
187{
188}
189
190void CollectAttribsUniforms::visitConstantUnion(TIntermConstantUnion*)
191{
192}
193
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000194bool CollectAttribsUniforms::visitBinary(Visit, TIntermBinary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000195{
196 return false;
197}
198
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000199bool CollectAttribsUniforms::visitUnary(Visit, TIntermUnary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000200{
201 return false;
202}
203
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000204bool CollectAttribsUniforms::visitSelection(Visit, TIntermSelection*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000205{
206 return false;
207}
208
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000209bool CollectAttribsUniforms::visitAggregate(Visit, TIntermAggregate* node)
210{
211 bool visitChildren = false;
212
213 switch (node->getOp())
214 {
215 case EOpSequence:
216 // We need to visit sequence children to get to variable declarations.
217 visitChildren = true;
218 break;
219 case EOpDeclaration: {
220 const TIntermSequence& sequence = node->getSequence();
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000221 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000222 if (qualifier == EvqAttribute || qualifier == EvqUniform)
223 {
224 TVariableInfoList& infoList = qualifier == EvqAttribute ?
225 mAttribs : mUniforms;
226 for (TIntermSequence::const_iterator i = sequence.begin();
227 i != sequence.end(); ++i)
228 {
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000229 const TIntermSymbol* variable = (*i)->getAsSymbolNode();
230 // The only case in which the sequence will not contain a
231 // TIntermSymbol node is initialization. It will contain a
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +0000232 // TIntermBinary node in that case. Since attributes and uniforms
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000233 // cannot be initialized in a shader, we must have only
234 // TIntermSymbol nodes in the sequence.
235 ASSERT(variable != NULL);
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000236 TString processedSymbol;
237 if (mHashFunction == NULL)
238 processedSymbol = variable->getSymbol();
239 else
240 processedSymbol = TIntermTraverser::hash(variable->getOriginalSymbol(), mHashFunction);
zmo@google.comfd747b82011-04-23 01:30:07 +0000241 getVariableInfo(variable->getType(),
242 variable->getOriginalSymbol(),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000243 processedSymbol,
244 infoList,
245 mHashFunction);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000246 }
247 }
248 break;
249 }
250 default: break;
251 }
252
253 return visitChildren;
254}
255
256bool CollectAttribsUniforms::visitLoop(Visit, TIntermLoop*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000257{
258 return false;
259}
260
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000261bool CollectAttribsUniforms::visitBranch(Visit, TIntermBranch*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000262{
263 return false;
264}
265