blob: 1e9486f8d339de0862437c0a6561c5bd3690e2ae [file] [log] [blame]
alokp@chromium.org07620a52010-09-23 17:53:56 +00001//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// 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()) {
22 switch (type.getNominalSize()) {
23 case 2: return SH_FLOAT_MAT2;
24 case 3: return SH_FLOAT_MAT3;
25 case 4: return SH_FLOAT_MAT4;
26 default: UNREACHABLE();
27 }
28 } else if (type.isVector()) {
29 switch (type.getNominalSize()) {
30 case 2: return SH_FLOAT_VEC2;
31 case 3: return SH_FLOAT_VEC3;
32 case 4: return SH_FLOAT_VEC4;
33 default: UNREACHABLE();
34 }
35 } else {
36 return SH_FLOAT;
37 }
38 case EbtInt:
39 if (type.isMatrix()) {
40 UNREACHABLE();
41 } else if (type.isVector()) {
42 switch (type.getNominalSize()) {
43 case 2: return SH_INT_VEC2;
44 case 3: return SH_INT_VEC3;
45 case 4: return SH_INT_VEC4;
46 default: UNREACHABLE();
47 }
48 } else {
49 return SH_INT;
50 }
51 case EbtBool:
52 if (type.isMatrix()) {
53 UNREACHABLE();
54 } else if (type.isVector()) {
55 switch (type.getNominalSize()) {
56 case 2: return SH_BOOL_VEC2;
57 case 3: return SH_BOOL_VEC3;
58 case 4: return SH_BOOL_VEC4;
59 default: UNREACHABLE();
60 }
61 } else {
62 return SH_BOOL;
63 }
64 case EbtSampler2D: return SH_SAMPLER_2D;
65 case EbtSamplerCube: return SH_SAMPLER_CUBE;
apatrick@chromium.org65756022012-01-17 21:45:38 +000066 case EbtSamplerExternalOES: return SH_SAMPLER_EXTERNAL_OES;
kbr@chromium.org205fef32011-11-22 20:50:02 +000067 case EbtSampler2DRect: return SH_SAMPLER_2D_RECT_ARB;
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000068 default: UNREACHABLE();
69 }
70 return SH_NONE;
71}
72
73static void getBuiltInVariableInfo(const TType& type,
74 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +000075 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000076 TVariableInfoList& infoList);
77static void getUserDefinedVariableInfo(const TType& type,
78 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +000079 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000080 TVariableInfoList& infoList);
81
82// Returns info for an attribute or uniform.
83static void getVariableInfo(const TType& type,
84 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +000085 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000086 TVariableInfoList& infoList)
87{
88 if (type.getBasicType() == EbtStruct) {
89 if (type.isArray()) {
90 for (int i = 0; i < type.getArraySize(); ++i) {
91 TString lname = name + arrayBrackets(i);
zmo@google.comfd747b82011-04-23 01:30:07 +000092 TString lmappedName = mappedName + arrayBrackets(i);
93 getUserDefinedVariableInfo(type, lname, lmappedName, infoList);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000094 }
95 } else {
zmo@google.comfd747b82011-04-23 01:30:07 +000096 getUserDefinedVariableInfo(type, name, mappedName, infoList);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000097 }
98 } else {
zmo@google.comfd747b82011-04-23 01:30:07 +000099 getBuiltInVariableInfo(type, name, mappedName, infoList);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000100 }
101}
102
103void getBuiltInVariableInfo(const TType& type,
104 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000105 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000106 TVariableInfoList& infoList)
107{
108 ASSERT(type.getBasicType() != EbtStruct);
109
110 TVariableInfo varInfo;
111 if (type.isArray()) {
112 varInfo.name = (name + "[0]").c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000113 varInfo.mappedName = (mappedName + "[0]").c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000114 varInfo.size = type.getArraySize();
115 } else {
116 varInfo.name = name.c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000117 varInfo.mappedName = mappedName.c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000118 varInfo.size = 1;
119 }
120 varInfo.type = getVariableDataType(type);
121 infoList.push_back(varInfo);
122}
123
124void getUserDefinedVariableInfo(const TType& type,
125 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000126 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000127 TVariableInfoList& infoList)
128{
129 ASSERT(type.getBasicType() == EbtStruct);
130
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000131 const TTypeList* structure = type.getStruct();
132 for (size_t i = 0; i < structure->size(); ++i) {
133 const TType* fieldType = (*structure)[i].type;
134 getVariableInfo(*fieldType,
zmo@google.comfd747b82011-04-23 01:30:07 +0000135 name + "." + fieldType->getFieldName(),
136 mappedName + "." + fieldType->getFieldName(),
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000137 infoList);
138 }
139}
140
alokp@chromium.org07620a52010-09-23 17:53:56 +0000141CollectAttribsUniforms::CollectAttribsUniforms(TVariableInfoList& attribs,
142 TVariableInfoList& uniforms)
143 : mAttribs(attribs),
144 mUniforms(uniforms)
145{
146}
147
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000148// We are only interested in attribute and uniform variable declaration.
alokp@chromium.org07620a52010-09-23 17:53:56 +0000149void CollectAttribsUniforms::visitSymbol(TIntermSymbol*)
150{
151}
152
153void CollectAttribsUniforms::visitConstantUnion(TIntermConstantUnion*)
154{
155}
156
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000157bool CollectAttribsUniforms::visitBinary(Visit, TIntermBinary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000158{
159 return false;
160}
161
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000162bool CollectAttribsUniforms::visitUnary(Visit, TIntermUnary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000163{
164 return false;
165}
166
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000167bool CollectAttribsUniforms::visitSelection(Visit, TIntermSelection*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000168{
169 return false;
170}
171
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000172bool CollectAttribsUniforms::visitAggregate(Visit, TIntermAggregate* node)
173{
174 bool visitChildren = false;
175
176 switch (node->getOp())
177 {
178 case EOpSequence:
179 // We need to visit sequence children to get to variable declarations.
180 visitChildren = true;
181 break;
182 case EOpDeclaration: {
183 const TIntermSequence& sequence = node->getSequence();
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000184 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000185 if (qualifier == EvqAttribute || qualifier == EvqUniform)
186 {
187 TVariableInfoList& infoList = qualifier == EvqAttribute ?
188 mAttribs : mUniforms;
189 for (TIntermSequence::const_iterator i = sequence.begin();
190 i != sequence.end(); ++i)
191 {
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000192 const TIntermSymbol* variable = (*i)->getAsSymbolNode();
193 // The only case in which the sequence will not contain a
194 // TIntermSymbol node is initialization. It will contain a
195 // TInterBinary node in that case. Since attributes and unifroms
196 // cannot be initialized in a shader, we must have only
197 // TIntermSymbol nodes in the sequence.
198 ASSERT(variable != NULL);
zmo@google.comfd747b82011-04-23 01:30:07 +0000199 getVariableInfo(variable->getType(),
200 variable->getOriginalSymbol(),
201 variable->getSymbol(),
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000202 infoList);
203 }
204 }
205 break;
206 }
207 default: break;
208 }
209
210 return visitChildren;
211}
212
213bool CollectAttribsUniforms::visitLoop(Visit, TIntermLoop*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000214{
215 return false;
216}
217
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000218bool CollectAttribsUniforms::visitBranch(Visit, TIntermBranch*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000219{
220 return false;
221}
222