alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 1 | // |
shannonwoods@chromium.org | e429ab7 | 2013-05-30 00:12:52 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 7 | #include "angle_gl.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 8 | #include "compiler/translator/VariableInfo.h" |
Jamie Madill | aa72d78 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 9 | #include "compiler/translator/util.h" |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 10 | |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 11 | template <typename VarT> |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 12 | static void ExpandUserDefinedVariable(const VarT &variable, |
| 13 | const std::string &name, |
| 14 | const std::string &mappedName, |
| 15 | bool markStaticUse, |
| 16 | std::vector<VarT> *expanded); |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 17 | |
| 18 | // Returns info for an attribute, uniform, or varying. |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 19 | template <typename VarT> |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 20 | static void ExpandVariable(const VarT &variable, |
| 21 | const std::string &name, |
| 22 | const std::string &mappedName, |
| 23 | bool markStaticUse, |
| 24 | std::vector<VarT> *expanded) |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 25 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 26 | if (variable.isStruct()) |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 27 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 28 | if (variable.isArray()) |
| 29 | { |
| 30 | for (size_t elementIndex = 0; elementIndex < variable.elementCount(); elementIndex++) |
| 31 | { |
| 32 | std::string lname = name + ArrayString(elementIndex); |
| 33 | std::string lmappedName = mappedName + ArrayString(elementIndex); |
| 34 | ExpandUserDefinedVariable(variable, lname, lmappedName, markStaticUse, expanded); |
| 35 | } |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | ExpandUserDefinedVariable(variable, name, mappedName, markStaticUse, expanded); |
| 40 | } |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 41 | } |
| 42 | else |
| 43 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 44 | VarT expandedVar = variable; |
| 45 | |
| 46 | expandedVar.name = name; |
| 47 | expandedVar.mappedName = mappedName; |
| 48 | |
| 49 | // Mark all expanded fields as used if the parent is used |
| 50 | if (markStaticUse) |
| 51 | { |
| 52 | expandedVar.staticUse = true; |
| 53 | } |
| 54 | |
| 55 | if (expandedVar.isArray()) |
| 56 | { |
| 57 | expandedVar.name += "[0]"; |
| 58 | expandedVar.mappedName += "[0]"; |
| 59 | } |
| 60 | |
| 61 | expanded->push_back(expandedVar); |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 62 | } |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 65 | template <class VarT> |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 66 | static void ExpandUserDefinedVariable(const VarT &variable, |
| 67 | const std::string &name, |
| 68 | const std::string &mappedName, |
| 69 | bool markStaticUse, |
| 70 | std::vector<VarT> *expanded) |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 71 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 72 | ASSERT(variable.isStruct()); |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 73 | |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 74 | const std::vector<VarT> &fields = variable.fields; |
| 75 | |
| 76 | for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++) |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 77 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 78 | const VarT &field = fields[fieldIndex]; |
| 79 | ExpandVariable(field, |
| 80 | name + "." + field.name, |
| 81 | mappedName + "." + field.mappedName, |
| 82 | markStaticUse, |
| 83 | expanded); |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 87 | template <class VarT> |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 88 | static VarT* findVariable(const TString &name, |
| 89 | std::vector<VarT> *infoList) |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 90 | { |
| 91 | // TODO(zmo): optimize this function. |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 92 | for (size_t ii = 0; ii < infoList->size(); ++ii) |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 93 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 94 | if ((*infoList)[ii].name.c_str() == name) |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 95 | return &((*infoList)[ii]); |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 96 | } |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 100 | CollectVariables::CollectVariables(std::vector<sh::Attribute> *attribs, |
| 101 | std::vector<sh::Uniform> *uniforms, |
| 102 | std::vector<sh::Varying> *varyings, |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 103 | ShHashFunction64 hashFunction) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 104 | : mAttribs(attribs), |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 105 | mUniforms(uniforms), |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 106 | mVaryings(varyings), |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 107 | mPointCoordAdded(false), |
| 108 | mFrontFacingAdded(false), |
| 109 | mFragCoordAdded(false), |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 110 | mHashFunction(hashFunction) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 111 | { |
| 112 | } |
| 113 | |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 114 | // We want to check whether a uniform/varying is statically used |
| 115 | // because we only count the used ones in packing computing. |
| 116 | // Also, gl_FragCoord, gl_PointCoord, and gl_FrontFacing count |
| 117 | // toward varying counting if they are statically used in a fragment |
| 118 | // shader. |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 119 | void CollectVariables::visitSymbol(TIntermSymbol *symbol) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 120 | { |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 121 | ASSERT(symbol != NULL); |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 122 | sh::ShaderVariable *var = NULL; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 123 | |
| 124 | if (sh::IsVarying(symbol->getQualifier())) |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 125 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 126 | var = findVariable(symbol->getSymbol(), mVaryings); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 127 | } |
| 128 | else |
| 129 | { |
| 130 | switch (symbol->getQualifier()) |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 131 | { |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 132 | case EvqUniform: |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 133 | var = findVariable(symbol->getSymbol(), mUniforms); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 134 | break; |
| 135 | case EvqFragCoord: |
| 136 | if (!mFragCoordAdded) |
| 137 | { |
| 138 | sh::Varying info; |
| 139 | info.name = "gl_FragCoord"; |
| 140 | info.mappedName = "gl_FragCoord"; |
| 141 | info.type = GL_FLOAT_VEC4; |
| 142 | info.arraySize = 0; |
| 143 | info.precision = GL_MEDIUM_FLOAT; // Use mediump as it doesn't really matter. |
| 144 | info.staticUse = true; |
| 145 | mVaryings->push_back(info); |
| 146 | mFragCoordAdded = true; |
| 147 | } |
| 148 | return; |
| 149 | case EvqFrontFacing: |
| 150 | if (!mFrontFacingAdded) |
| 151 | { |
| 152 | sh::Varying info; |
| 153 | info.name = "gl_FrontFacing"; |
| 154 | info.mappedName = "gl_FrontFacing"; |
| 155 | info.type = GL_BOOL; |
| 156 | info.arraySize = 0; |
| 157 | info.precision = GL_NONE; |
| 158 | info.staticUse = true; |
| 159 | mVaryings->push_back(info); |
| 160 | mFrontFacingAdded = true; |
| 161 | } |
| 162 | return; |
| 163 | case EvqPointCoord: |
| 164 | if (!mPointCoordAdded) |
| 165 | { |
| 166 | sh::Varying info; |
| 167 | info.name = "gl_PointCoord"; |
| 168 | info.mappedName = "gl_PointCoord"; |
| 169 | info.type = GL_FLOAT_VEC2; |
| 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 | mPointCoordAdded = true; |
| 175 | } |
| 176 | return; |
| 177 | default: |
| 178 | break; |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 179 | } |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 180 | } |
| 181 | if (var) |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 182 | { |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 183 | var->staticUse = true; |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | template <typename VarT> |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 188 | class NameHashingTraverser : public sh::GetVariableTraverser<VarT> |
| 189 | { |
| 190 | public: |
| 191 | NameHashingTraverser(std::vector<VarT> *output, ShHashFunction64 hashFunction) |
| 192 | : sh::GetVariableTraverser<VarT>(output), |
| 193 | mHashFunction(hashFunction) |
| 194 | {} |
| 195 | |
| 196 | private: |
| 197 | void visitVariable(VarT *variable) |
| 198 | { |
| 199 | TString stringName = TString(variable->name.c_str()); |
| 200 | variable->mappedName = TIntermTraverser::hash(stringName, mHashFunction).c_str(); |
| 201 | } |
| 202 | |
| 203 | ShHashFunction64 mHashFunction; |
| 204 | }; |
| 205 | |
| 206 | // Attributes, which cannot have struct fields, are a special case |
| 207 | template <> |
| 208 | void CollectVariables::visitVariable(const TIntermSymbol *variable, |
| 209 | std::vector<sh::Attribute> *infoList) const |
| 210 | { |
| 211 | ASSERT(variable); |
| 212 | const TType &type = variable->getType(); |
| 213 | ASSERT(!type.getStruct()); |
| 214 | |
| 215 | sh::Attribute attribute; |
| 216 | |
| 217 | attribute.type = sh::GLVariableType(type); |
| 218 | attribute.precision = sh::GLVariablePrecision(type); |
| 219 | attribute.name = variable->getSymbol().c_str(); |
| 220 | attribute.arraySize = static_cast<unsigned int>(type.getArraySize()); |
| 221 | attribute.mappedName = TIntermTraverser::hash(variable->getSymbol(), mHashFunction).c_str(); |
| 222 | attribute.location = variable->getType().getLayoutQualifier().location; |
| 223 | |
| 224 | infoList->push_back(attribute); |
| 225 | } |
| 226 | |
| 227 | template <typename VarT> |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 228 | void CollectVariables::visitVariable(const TIntermSymbol *variable, |
| 229 | std::vector<VarT> *infoList) const |
| 230 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 231 | NameHashingTraverser<VarT> traverser(infoList, mHashFunction); |
| 232 | traverser.traverse(variable->getType(), variable->getSymbol()); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | template <typename VarT> |
| 236 | void CollectVariables::visitInfoList(const TIntermSequence &sequence, |
| 237 | std::vector<VarT> *infoList) const |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 238 | { |
| 239 | for (size_t seqIndex = 0; seqIndex < sequence.size(); seqIndex++) |
| 240 | { |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 241 | const TIntermSymbol *variable = sequence[seqIndex]->getAsSymbolNode(); |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 242 | // The only case in which the sequence will not contain a |
| 243 | // TIntermSymbol node is initialization. It will contain a |
| 244 | // TInterBinary node in that case. Since attributes, uniforms, |
| 245 | // and varyings cannot be initialized in a shader, we must have |
| 246 | // only TIntermSymbol nodes in the sequence. |
| 247 | ASSERT(variable != NULL); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 248 | visitVariable(variable, infoList); |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 249 | } |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 252 | bool CollectVariables::visitAggregate(Visit, TIntermAggregate *node) |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 253 | { |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 254 | bool visitChildren = true; |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 255 | |
| 256 | switch (node->getOp()) |
| 257 | { |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 258 | case EOpDeclaration: |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 259 | { |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 260 | const TIntermSequence &sequence = node->getSequence(); |
| 261 | TQualifier qualifier = sequence.front()->getAsTyped()->getQualifier(); |
| 262 | if (qualifier == EvqAttribute || qualifier == EvqVertexIn || qualifier == EvqUniform || |
| 263 | sh::IsVarying(qualifier)) |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 264 | { |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 265 | switch (qualifier) |
| 266 | { |
| 267 | case EvqAttribute: |
| 268 | case EvqVertexIn: |
| 269 | visitInfoList(sequence, mAttribs); |
| 270 | break; |
| 271 | case EvqUniform: |
| 272 | visitInfoList(sequence, mUniforms); |
| 273 | break; |
| 274 | default: |
| 275 | visitInfoList(sequence, mVaryings); |
| 276 | break; |
| 277 | } |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 278 | |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 279 | if (!sequence.empty()) |
| 280 | { |
| 281 | visitChildren = false; |
| 282 | } |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 283 | } |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 284 | break; |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 285 | } |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 286 | default: break; |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | return visitChildren; |
| 290 | } |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame^] | 291 | |
| 292 | template <typename VarT> |
| 293 | void ExpandVariables(const std::vector<VarT> &compact, std::vector<VarT> *expanded) |
| 294 | { |
| 295 | for (size_t variableIndex = 0; variableIndex < compact.size(); variableIndex++) |
| 296 | { |
| 297 | const VarT &variable = compact[variableIndex]; |
| 298 | ExpandVariable(variable, variable.name, variable.mappedName, variable.staticUse, expanded); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | template void ExpandVariables(const std::vector<sh::Uniform> &, std::vector<sh::Uniform> *); |
| 303 | template void ExpandVariables(const std::vector<sh::Varying> &, std::vector<sh::Varying> *); |