blob: b331581a1822d25705918e6c3e3d32148845de06 [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
Jamie Madill4667c452014-07-08 15:02:36 -04007#include "angle_gl.h"
Geoff Lang17732822013-08-29 13:46:49 -04008#include "compiler/translator/VariableInfo.h"
Jamie Madillaa72d782014-07-02 15:31:19 -04009#include "compiler/translator/util.h"
Jamie Madilld5512cd2014-07-10 17:50:08 -040010#include "common/utilities.h"
alokp@chromium.org07620a52010-09-23 17:53:56 +000011
Jamie Madill42bcf322014-08-25 16:20:46 -040012static void ExpandUserDefinedVariable(const sh::ShaderVariable &variable,
Jamie Madill23a8a432014-07-09 13:27:42 -040013 const std::string &name,
14 const std::string &mappedName,
15 bool markStaticUse,
Jamie Madill42bcf322014-08-25 16:20:46 -040016 std::vector<sh::ShaderVariable> *expanded);
Zhenyao Mod2d340b2013-09-23 14:57:05 -040017
Jamie Madill42bcf322014-08-25 16:20:46 -040018static void ExpandVariable(const sh::ShaderVariable &variable,
Jamie Madill23a8a432014-07-09 13:27:42 -040019 const std::string &name,
20 const std::string &mappedName,
21 bool markStaticUse,
Jamie Madill42bcf322014-08-25 16:20:46 -040022 std::vector<sh::ShaderVariable> *expanded)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000023{
Jamie Madill23a8a432014-07-09 13:27:42 -040024 if (variable.isStruct())
Jamie Madill4667c452014-07-08 15:02:36 -040025 {
Jamie Madill23a8a432014-07-09 13:27:42 -040026 if (variable.isArray())
27 {
28 for (size_t elementIndex = 0; elementIndex < variable.elementCount(); elementIndex++)
29 {
30 std::string lname = name + ArrayString(elementIndex);
31 std::string lmappedName = mappedName + ArrayString(elementIndex);
32 ExpandUserDefinedVariable(variable, lname, lmappedName, markStaticUse, expanded);
33 }
34 }
35 else
36 {
37 ExpandUserDefinedVariable(variable, name, mappedName, markStaticUse, expanded);
38 }
Jamie Madill4667c452014-07-08 15:02:36 -040039 }
40 else
41 {
Jamie Madill42bcf322014-08-25 16:20:46 -040042 sh::ShaderVariable expandedVar = variable;
Jamie Madill23a8a432014-07-09 13:27:42 -040043
44 expandedVar.name = name;
45 expandedVar.mappedName = mappedName;
46
47 // Mark all expanded fields as used if the parent is used
48 if (markStaticUse)
49 {
50 expandedVar.staticUse = true;
51 }
52
53 if (expandedVar.isArray())
54 {
55 expandedVar.name += "[0]";
56 expandedVar.mappedName += "[0]";
57 }
58
59 expanded->push_back(expandedVar);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000060 }
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000061}
62
Jamie Madill42bcf322014-08-25 16:20:46 -040063static void ExpandUserDefinedVariable(const sh::ShaderVariable &variable,
Jamie Madill23a8a432014-07-09 13:27:42 -040064 const std::string &name,
65 const std::string &mappedName,
66 bool markStaticUse,
Jamie Madill42bcf322014-08-25 16:20:46 -040067 std::vector<sh::ShaderVariable> *expanded)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000068{
Jamie Madill23a8a432014-07-09 13:27:42 -040069 ASSERT(variable.isStruct());
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000070
Jamie Madill42bcf322014-08-25 16:20:46 -040071 const std::vector<sh::ShaderVariable> &fields = variable.fields;
Jamie Madill23a8a432014-07-09 13:27:42 -040072
73 for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill4667c452014-07-08 15:02:36 -040074 {
Jamie Madill42bcf322014-08-25 16:20:46 -040075 const sh::ShaderVariable &field = fields[fieldIndex];
Jamie Madill23a8a432014-07-09 13:27:42 -040076 ExpandVariable(field,
77 name + "." + field.name,
78 mappedName + "." + field.mappedName,
79 markStaticUse,
80 expanded);
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +000081 }
82}
83
Jamie Madilla718c1e2014-07-02 15:31:22 -040084template <class VarT>
Jamie Madilld5512cd2014-07-10 17:50:08 -040085static VarT *FindVariable(const TString &name,
Jamie Madill23a8a432014-07-09 13:27:42 -040086 std::vector<VarT> *infoList)
Zhenyao Mod2d340b2013-09-23 14:57:05 -040087{
88 // TODO(zmo): optimize this function.
Jamie Madilla718c1e2014-07-02 15:31:22 -040089 for (size_t ii = 0; ii < infoList->size(); ++ii)
Zhenyao Mod2d340b2013-09-23 14:57:05 -040090 {
Jamie Madill23a8a432014-07-09 13:27:42 -040091 if ((*infoList)[ii].name.c_str() == name)
Jamie Madilla718c1e2014-07-02 15:31:22 -040092 return &((*infoList)[ii]);
Zhenyao Mod2d340b2013-09-23 14:57:05 -040093 }
Jamie Madilld5512cd2014-07-10 17:50:08 -040094
Zhenyao Mod2d340b2013-09-23 14:57:05 -040095 return NULL;
96}
97
Jamie Madilla718c1e2014-07-02 15:31:22 -040098CollectVariables::CollectVariables(std::vector<sh::Attribute> *attribs,
Jamie Madilld5512cd2014-07-10 17:50:08 -040099 std::vector<sh::Attribute> *outputVariables,
Jamie Madilla718c1e2014-07-02 15:31:22 -0400100 std::vector<sh::Uniform> *uniforms,
101 std::vector<sh::Varying> *varyings,
Jamie Madilld5512cd2014-07-10 17:50:08 -0400102 std::vector<sh::InterfaceBlock> *interfaceBlocks,
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400103 ShHashFunction64 hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000104 : mAttribs(attribs),
Jamie Madilld5512cd2014-07-10 17:50:08 -0400105 mOutputVariables(outputVariables),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000106 mUniforms(uniforms),
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400107 mVaryings(varyings),
Jamie Madilld5512cd2014-07-10 17:50:08 -0400108 mInterfaceBlocks(interfaceBlocks),
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400109 mPointCoordAdded(false),
110 mFrontFacingAdded(false),
111 mFragCoordAdded(false),
daniel@transgaming.com0aa3b5a2012-11-28 19:43:24 +0000112 mHashFunction(hashFunction)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000113{
114}
115
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400116// We want to check whether a uniform/varying is statically used
117// because we only count the used ones in packing computing.
118// Also, gl_FragCoord, gl_PointCoord, and gl_FrontFacing count
119// toward varying counting if they are statically used in a fragment
120// shader.
Jamie Madill4667c452014-07-08 15:02:36 -0400121void CollectVariables::visitSymbol(TIntermSymbol *symbol)
alokp@chromium.org07620a52010-09-23 17:53:56 +0000122{
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400123 ASSERT(symbol != NULL);
Jamie Madilla718c1e2014-07-02 15:31:22 -0400124 sh::ShaderVariable *var = NULL;
Jamie Madilld5512cd2014-07-10 17:50:08 -0400125 const TString &symbolName = symbol->getSymbol();
Jamie Madill4667c452014-07-08 15:02:36 -0400126
127 if (sh::IsVarying(symbol->getQualifier()))
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400128 {
Jamie Madilld5512cd2014-07-10 17:50:08 -0400129 var = FindVariable(symbolName, mVaryings);
Jamie Madill4667c452014-07-08 15:02:36 -0400130 }
Jamie Madilld5512cd2014-07-10 17:50:08 -0400131 else if (symbol->getType() != EbtInterfaceBlock)
Jamie Madill4667c452014-07-08 15:02:36 -0400132 {
133 switch (symbol->getQualifier())
Jamie Madilla718c1e2014-07-02 15:31:22 -0400134 {
Jamie Madilld5512cd2014-07-10 17:50:08 -0400135 case EvqAttribute:
136 case EvqVertexIn:
137 var = FindVariable(symbolName, mAttribs);
138 break;
139 case EvqFragmentOut:
140 var = FindVariable(symbolName, mOutputVariables);
141 break;
Jamie Madill4667c452014-07-08 15:02:36 -0400142 case EvqUniform:
Jamie Madilld5512cd2014-07-10 17:50:08 -0400143 {
144 const TInterfaceBlock *interfaceBlock = symbol->getType().getInterfaceBlock();
145 if (interfaceBlock)
146 {
147 sh::InterfaceBlock *namedBlock = FindVariable(interfaceBlock->name(), mInterfaceBlocks);
148 ASSERT(namedBlock);
149 var = FindVariable(symbolName, &namedBlock->fields);
150
151 // Set static use on the parent interface block here
152 namedBlock->staticUse = true;
153 }
154 else
155 {
156 var = FindVariable(symbolName, mUniforms);
157 }
158
159 // It's an internal error to reference an undefined user uniform
160 ASSERT(symbolName.compare(0, 3, "gl_") == 0 || var);
161 }
Jamie Madill4667c452014-07-08 15:02:36 -0400162 break;
163 case EvqFragCoord:
164 if (!mFragCoordAdded)
165 {
166 sh::Varying info;
167 info.name = "gl_FragCoord";
168 info.mappedName = "gl_FragCoord";
169 info.type = GL_FLOAT_VEC4;
170 info.arraySize = 0;
171 info.precision = GL_MEDIUM_FLOAT; // Use mediump as it doesn't really matter.
172 info.staticUse = true;
173 mVaryings->push_back(info);
174 mFragCoordAdded = true;
175 }
176 return;
177 case EvqFrontFacing:
178 if (!mFrontFacingAdded)
179 {
180 sh::Varying info;
181 info.name = "gl_FrontFacing";
182 info.mappedName = "gl_FrontFacing";
183 info.type = GL_BOOL;
184 info.arraySize = 0;
185 info.precision = GL_NONE;
186 info.staticUse = true;
187 mVaryings->push_back(info);
188 mFrontFacingAdded = true;
189 }
190 return;
191 case EvqPointCoord:
192 if (!mPointCoordAdded)
193 {
194 sh::Varying info;
195 info.name = "gl_PointCoord";
196 info.mappedName = "gl_PointCoord";
197 info.type = GL_FLOAT_VEC2;
198 info.arraySize = 0;
199 info.precision = GL_MEDIUM_FLOAT; // Use mediump as it doesn't really matter.
200 info.staticUse = true;
201 mVaryings->push_back(info);
202 mPointCoordAdded = true;
203 }
204 return;
205 default:
206 break;
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400207 }
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400208 }
209 if (var)
Jamie Madilla718c1e2014-07-02 15:31:22 -0400210 {
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400211 var->staticUse = true;
Jamie Madilla718c1e2014-07-02 15:31:22 -0400212 }
213}
214
Jamie Madill42bcf322014-08-25 16:20:46 -0400215class NameHashingTraverser : public sh::GetVariableTraverser
Jamie Madill23a8a432014-07-09 13:27:42 -0400216{
217 public:
Jamie Madill42bcf322014-08-25 16:20:46 -0400218 NameHashingTraverser(ShHashFunction64 hashFunction)
219 : mHashFunction(hashFunction)
Jamie Madill23a8a432014-07-09 13:27:42 -0400220 {}
221
222 private:
Jamie Madill42bcf322014-08-25 16:20:46 -0400223 DISALLOW_COPY_AND_ASSIGN(NameHashingTraverser);
224
225 virtual void visitVariable(sh::ShaderVariable *variable)
Jamie Madill23a8a432014-07-09 13:27:42 -0400226 {
227 TString stringName = TString(variable->name.c_str());
228 variable->mappedName = TIntermTraverser::hash(stringName, mHashFunction).c_str();
229 }
230
231 ShHashFunction64 mHashFunction;
232};
233
234// Attributes, which cannot have struct fields, are a special case
235template <>
236void CollectVariables::visitVariable(const TIntermSymbol *variable,
237 std::vector<sh::Attribute> *infoList) const
238{
239 ASSERT(variable);
240 const TType &type = variable->getType();
241 ASSERT(!type.getStruct());
242
243 sh::Attribute attribute;
244
245 attribute.type = sh::GLVariableType(type);
246 attribute.precision = sh::GLVariablePrecision(type);
247 attribute.name = variable->getSymbol().c_str();
248 attribute.arraySize = static_cast<unsigned int>(type.getArraySize());
249 attribute.mappedName = TIntermTraverser::hash(variable->getSymbol(), mHashFunction).c_str();
250 attribute.location = variable->getType().getLayoutQualifier().location;
251
252 infoList->push_back(attribute);
253}
254
Jamie Madilld5512cd2014-07-10 17:50:08 -0400255template <>
256void CollectVariables::visitVariable(const TIntermSymbol *variable,
257 std::vector<sh::InterfaceBlock> *infoList) const
258{
259 sh::InterfaceBlock interfaceBlock;
260 const TInterfaceBlock *blockType = variable->getType().getInterfaceBlock();
Jamie Madill42bcf322014-08-25 16:20:46 -0400261 ASSERT(blockType);
Jamie Madilld5512cd2014-07-10 17:50:08 -0400262
263 interfaceBlock.name = blockType->name().c_str();
264 interfaceBlock.mappedName = TIntermTraverser::hash(variable->getSymbol(), mHashFunction).c_str();
Jamie Madill42bcf322014-08-25 16:20:46 -0400265 interfaceBlock.instanceName = (blockType->hasInstanceName() ? blockType->instanceName().c_str() : "");
Jamie Madilld5512cd2014-07-10 17:50:08 -0400266 interfaceBlock.arraySize = variable->getArraySize();
Jamie Madill42bcf322014-08-25 16:20:46 -0400267 interfaceBlock.isRowMajorLayout = (blockType->matrixPacking() == EmpRowMajor);
Jamie Madilld5512cd2014-07-10 17:50:08 -0400268 interfaceBlock.layout = sh::GetBlockLayoutType(blockType->blockStorage());
269
Jamie Madill42bcf322014-08-25 16:20:46 -0400270 sh::GetInterfaceBlockFields(*blockType, &interfaceBlock.fields);
Jamie Madilld5512cd2014-07-10 17:50:08 -0400271
272 infoList->push_back(interfaceBlock);
273}
274
Jamie Madill23a8a432014-07-09 13:27:42 -0400275template <typename VarT>
Jamie Madill4667c452014-07-08 15:02:36 -0400276void CollectVariables::visitVariable(const TIntermSymbol *variable,
277 std::vector<VarT> *infoList) const
278{
Jamie Madill42bcf322014-08-25 16:20:46 -0400279 NameHashingTraverser traverser(mHashFunction);
280 traverser.traverse(variable->getType(), variable->getSymbol(), infoList);
Jamie Madill4667c452014-07-08 15:02:36 -0400281}
282
283template <typename VarT>
284void CollectVariables::visitInfoList(const TIntermSequence &sequence,
285 std::vector<VarT> *infoList) const
Jamie Madilla718c1e2014-07-02 15:31:22 -0400286{
287 for (size_t seqIndex = 0; seqIndex < sequence.size(); seqIndex++)
288 {
Jamie Madill4667c452014-07-08 15:02:36 -0400289 const TIntermSymbol *variable = sequence[seqIndex]->getAsSymbolNode();
Jamie Madilla718c1e2014-07-02 15:31:22 -0400290 // The only case in which the sequence will not contain a
291 // TIntermSymbol node is initialization. It will contain a
292 // TInterBinary node in that case. Since attributes, uniforms,
293 // and varyings cannot be initialized in a shader, we must have
294 // only TIntermSymbol nodes in the sequence.
295 ASSERT(variable != NULL);
Jamie Madill4667c452014-07-08 15:02:36 -0400296 visitVariable(variable, infoList);
Jamie Madilla718c1e2014-07-02 15:31:22 -0400297 }
alokp@chromium.org07620a52010-09-23 17:53:56 +0000298}
299
Jamie Madill4667c452014-07-08 15:02:36 -0400300bool CollectVariables::visitAggregate(Visit, TIntermAggregate *node)
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000301{
Zhenyao Mod2d340b2013-09-23 14:57:05 -0400302 bool visitChildren = true;
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000303
304 switch (node->getOp())
305 {
Jamie Madill4667c452014-07-08 15:02:36 -0400306 case EOpDeclaration:
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000307 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700308 const TIntermSequence &sequence = *(node->getSequence());
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400309 ASSERT(!sequence.empty());
310
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700311 const TIntermTyped &typedNode = *(sequence.front()->getAsTyped());
Jamie Madilld5512cd2014-07-10 17:50:08 -0400312 TQualifier qualifier = typedNode.getQualifier();
313
314 if (typedNode.getBasicType() == EbtInterfaceBlock)
315 {
316 visitInfoList(sequence, mInterfaceBlocks);
317 }
318 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn ||
319 qualifier == EvqFragmentOut || qualifier == EvqUniform ||
320 sh::IsVarying(qualifier))
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400321 {
Jamie Madill4667c452014-07-08 15:02:36 -0400322 switch (qualifier)
323 {
324 case EvqAttribute:
325 case EvqVertexIn:
326 visitInfoList(sequence, mAttribs);
327 break;
Jamie Madilld5512cd2014-07-10 17:50:08 -0400328 case EvqFragmentOut:
329 visitInfoList(sequence, mOutputVariables);
330 break;
Jamie Madill4667c452014-07-08 15:02:36 -0400331 case EvqUniform:
332 visitInfoList(sequence, mUniforms);
333 break;
334 default:
Jamie Madill3b5c2da2014-08-19 15:23:32 -0400335 visitInfoList(sequence, mVaryings);
Jamie Madill4667c452014-07-08 15:02:36 -0400336 break;
337 }
Zhenyao Mo74da9f22013-09-23 14:57:01 -0400338
Jamie Madill1c28e1f2014-08-04 11:37:54 -0400339 visitChildren = false;
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000340 }
Jamie Madill4667c452014-07-08 15:02:36 -0400341 break;
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000342 }
Jamie Madill4667c452014-07-08 15:02:36 -0400343 default: break;
alokp@chromium.orgee76f6a2010-09-27 19:28:55 +0000344 }
345
346 return visitChildren;
347}
Jamie Madill23a8a432014-07-09 13:27:42 -0400348
349template <typename VarT>
Jamie Madill42bcf322014-08-25 16:20:46 -0400350void ExpandVariables(const std::vector<VarT> &compact,
351 std::vector<sh::ShaderVariable> *expanded)
Jamie Madill23a8a432014-07-09 13:27:42 -0400352{
353 for (size_t variableIndex = 0; variableIndex < compact.size(); variableIndex++)
354 {
Jamie Madill42bcf322014-08-25 16:20:46 -0400355 const sh::ShaderVariable &variable = compact[variableIndex];
Jamie Madill23a8a432014-07-09 13:27:42 -0400356 ExpandVariable(variable, variable.name, variable.mappedName, variable.staticUse, expanded);
357 }
358}
359
Jamie Madill42bcf322014-08-25 16:20:46 -0400360template void ExpandVariables(const std::vector<sh::Uniform> &, std::vector<sh::ShaderVariable> *);
361template void ExpandVariables(const std::vector<sh::Varying> &, std::vector<sh::ShaderVariable> *);