blob: 843f1877c980a33edfcc505896838795203787f2 [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;
kbr@chromium.org205fef32011-11-22 20:50:02 +000066 case EbtSampler2DRect: return SH_SAMPLER_2D_RECT_ARB;
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000067 default: UNREACHABLE();
68 }
69 return SH_NONE;
70}
71
72static void getBuiltInVariableInfo(const TType& type,
73 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +000074 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000075 TVariableInfoList& infoList);
76static void getUserDefinedVariableInfo(const TType& type,
77 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +000078 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000079 TVariableInfoList& infoList);
80
81// Returns info for an attribute or uniform.
82static void getVariableInfo(const TType& type,
83 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +000084 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000085 TVariableInfoList& infoList)
86{
87 if (type.getBasicType() == EbtStruct) {
88 if (type.isArray()) {
89 for (int i = 0; i < type.getArraySize(); ++i) {
90 TString lname = name + arrayBrackets(i);
zmo@google.comfd747b82011-04-23 01:30:07 +000091 TString lmappedName = mappedName + arrayBrackets(i);
92 getUserDefinedVariableInfo(type, lname, lmappedName, infoList);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000093 }
94 } else {
zmo@google.comfd747b82011-04-23 01:30:07 +000095 getUserDefinedVariableInfo(type, name, mappedName, infoList);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000096 }
97 } else {
zmo@google.comfd747b82011-04-23 01:30:07 +000098 getBuiltInVariableInfo(type, name, mappedName, infoList);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000099 }
100}
101
102void getBuiltInVariableInfo(const TType& type,
103 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000104 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000105 TVariableInfoList& infoList)
106{
107 ASSERT(type.getBasicType() != EbtStruct);
108
109 TVariableInfo varInfo;
110 if (type.isArray()) {
111 varInfo.name = (name + "[0]").c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000112 varInfo.mappedName = (mappedName + "[0]").c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000113 varInfo.size = type.getArraySize();
114 } else {
115 varInfo.name = name.c_str();
zmo@google.comfd747b82011-04-23 01:30:07 +0000116 varInfo.mappedName = mappedName.c_str();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000117 varInfo.size = 1;
118 }
119 varInfo.type = getVariableDataType(type);
120 infoList.push_back(varInfo);
121}
122
123void getUserDefinedVariableInfo(const TType& type,
124 const TString& name,
zmo@google.comfd747b82011-04-23 01:30:07 +0000125 const TString& mappedName,
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000126 TVariableInfoList& infoList)
127{
128 ASSERT(type.getBasicType() == EbtStruct);
129
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000130 const TTypeList* structure = type.getStruct();
131 for (size_t i = 0; i < structure->size(); ++i) {
132 const TType* fieldType = (*structure)[i].type;
133 getVariableInfo(*fieldType,
zmo@google.comfd747b82011-04-23 01:30:07 +0000134 name + "." + fieldType->getFieldName(),
135 mappedName + "." + fieldType->getFieldName(),
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000136 infoList);
137 }
138}
139
alokp@chromium.org07620a52010-09-23 17:53:56 +0000140CollectAttribsUniforms::CollectAttribsUniforms(TVariableInfoList& attribs,
141 TVariableInfoList& uniforms)
142 : mAttribs(attribs),
143 mUniforms(uniforms)
144{
145}
146
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000147// We are only interested in attribute and uniform variable declaration.
alokp@chromium.org07620a52010-09-23 17:53:56 +0000148void CollectAttribsUniforms::visitSymbol(TIntermSymbol*)
149{
150}
151
152void CollectAttribsUniforms::visitConstantUnion(TIntermConstantUnion*)
153{
154}
155
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000156bool CollectAttribsUniforms::visitBinary(Visit, TIntermBinary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000157{
158 return false;
159}
160
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000161bool CollectAttribsUniforms::visitUnary(Visit, TIntermUnary*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000162{
163 return false;
164}
165
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000166bool CollectAttribsUniforms::visitSelection(Visit, TIntermSelection*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000167{
168 return false;
169}
170
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000171bool CollectAttribsUniforms::visitAggregate(Visit, TIntermAggregate* node)
172{
173 bool visitChildren = false;
174
175 switch (node->getOp())
176 {
177 case EOpSequence:
178 // We need to visit sequence children to get to variable declarations.
179 visitChildren = true;
180 break;
181 case EOpDeclaration: {
182 const TIntermSequence& sequence = node->getSequence();
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000183 TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier();
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000184 if (qualifier == EvqAttribute || qualifier == EvqUniform)
185 {
186 TVariableInfoList& infoList = qualifier == EvqAttribute ?
187 mAttribs : mUniforms;
188 for (TIntermSequence::const_iterator i = sequence.begin();
189 i != sequence.end(); ++i)
190 {
alokp@chromium.org10e6e9e2010-09-27 21:03:45 +0000191 const TIntermSymbol* variable = (*i)->getAsSymbolNode();
192 // The only case in which the sequence will not contain a
193 // TIntermSymbol node is initialization. It will contain a
194 // TInterBinary node in that case. Since attributes and unifroms
195 // cannot be initialized in a shader, we must have only
196 // TIntermSymbol nodes in the sequence.
197 ASSERT(variable != NULL);
zmo@google.comfd747b82011-04-23 01:30:07 +0000198 getVariableInfo(variable->getType(),
199 variable->getOriginalSymbol(),
200 variable->getSymbol(),
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000201 infoList);
202 }
203 }
204 break;
205 }
206 default: break;
207 }
208
209 return visitChildren;
210}
211
212bool CollectAttribsUniforms::visitLoop(Visit, TIntermLoop*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000213{
214 return false;
215}
216
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000217bool CollectAttribsUniforms::visitBranch(Visit, TIntermBranch*)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000218{
219 return false;
220}
221