blob: 3ff283627b8b397c3cc91132639f5f10164a12a5 [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
gman@chromium.org8d804792012-10-17 21:33:48 +0000141TVariableInfo::TVariableInfo()
142{
143}
144
145TVariableInfo::TVariableInfo(ShDataType type, int size)
146 : type(type),
147 size(size)
148{
149}
150
alokp@chromium.org07620a52010-09-23 17:53:56 +0000151CollectAttribsUniforms::CollectAttribsUniforms(TVariableInfoList& attribs,
152 TVariableInfoList& uniforms)
153 : mAttribs(attribs),
154 mUniforms(uniforms)
155{
156}
157
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000158// We are only interested in attribute and uniform variable declaration.
alokp@chromium.org07620a52010-09-23 17:53:56 +0000159void CollectAttribsUniforms::visitSymbol(TIntermSymbol*)
160{
161}
162
163void CollectAttribsUniforms::visitConstantUnion(TIntermConstantUnion*)
164{
165}
166
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000167bool CollectAttribsUniforms::visitBinary(Visit, TIntermBinary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000168{
169 return false;
170}
171
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000172bool CollectAttribsUniforms::visitUnary(Visit, TIntermUnary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000173{
174 return false;
175}
176
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000177bool CollectAttribsUniforms::visitSelection(Visit, TIntermSelection*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000178{
179 return false;
180}
181
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000182bool CollectAttribsUniforms::visitAggregate(Visit, TIntermAggregate* node)
183{
184 bool visitChildren = false;
185
186 switch (node->getOp())
187 {
188 case EOpSequence:
189 // We need to visit sequence children to get to variable declarations.
190 visitChildren = true;
191 break;
192 case EOpDeclaration: {
193 const TIntermSequence& sequence = node->getSequence();
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000194 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000195 if (qualifier == EvqAttribute || qualifier == EvqUniform)
196 {
197 TVariableInfoList& infoList = qualifier == EvqAttribute ?
198 mAttribs : mUniforms;
199 for (TIntermSequence::const_iterator i = sequence.begin();
200 i != sequence.end(); ++i)
201 {
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000202 const TIntermSymbol* variable = (*i)->getAsSymbolNode();
203 // The only case in which the sequence will not contain a
204 // TIntermSymbol node is initialization. It will contain a
205 // TInterBinary node in that case. Since attributes and unifroms
206 // cannot be initialized in a shader, we must have only
207 // TIntermSymbol nodes in the sequence.
208 ASSERT(variable != NULL);
zmo@google.comfd747b82011-04-23 01:30:07 +0000209 getVariableInfo(variable->getType(),
210 variable->getOriginalSymbol(),
211 variable->getSymbol(),
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000212 infoList);
213 }
214 }
215 break;
216 }
217 default: break;
218 }
219
220 return visitChildren;
221}
222
223bool CollectAttribsUniforms::visitLoop(Visit, TIntermLoop*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000224{
225 return false;
226}
227
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000228bool CollectAttribsUniforms::visitBranch(Visit, TIntermBranch*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000229{
230 return false;
231}
232