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" |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 8 | #include "compiler/translator/SymbolTable.h" |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 9 | #include "compiler/translator/VariableInfo.h" |
Jamie Madill | aa72d78 | 2014-07-02 15:31:19 -0400 | [diff] [blame] | 10 | #include "compiler/translator/util.h" |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 11 | #include "common/utilities.h" |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 12 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 13 | namespace sh |
| 14 | { |
| 15 | |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 16 | namespace |
| 17 | { |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 18 | |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 19 | TString InterfaceBlockFieldName(const TInterfaceBlock &interfaceBlock, const TField &field) |
| 20 | { |
| 21 | if (interfaceBlock.hasInstanceName()) |
| 22 | { |
| 23 | return interfaceBlock.name() + "." + field.name(); |
| 24 | } |
| 25 | else |
| 26 | { |
| 27 | return field.name(); |
| 28 | } |
| 29 | } |
| 30 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 31 | BlockLayoutType GetBlockLayoutType(TLayoutBlockStorage blockStorage) |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 32 | { |
| 33 | switch (blockStorage) |
| 34 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 35 | case EbsPacked: return BLOCKLAYOUT_PACKED; |
| 36 | case EbsShared: return BLOCKLAYOUT_SHARED; |
| 37 | case EbsStd140: return BLOCKLAYOUT_STANDARD; |
| 38 | default: UNREACHABLE(); return BLOCKLAYOUT_SHARED; |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 42 | void ExpandUserDefinedVariable(const ShaderVariable &variable, |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 43 | const std::string &name, |
| 44 | const std::string &mappedName, |
| 45 | bool markStaticUse, |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 46 | std::vector<ShaderVariable> *expanded); |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 47 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 48 | void ExpandVariable(const ShaderVariable &variable, |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 49 | const std::string &name, |
| 50 | const std::string &mappedName, |
| 51 | bool markStaticUse, |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 52 | std::vector<ShaderVariable> *expanded) |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 53 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 54 | if (variable.isStruct()) |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 55 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 56 | if (variable.isArray()) |
| 57 | { |
| 58 | for (size_t elementIndex = 0; elementIndex < variable.elementCount(); elementIndex++) |
| 59 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 60 | std::string lname = name + ::ArrayString(elementIndex); |
| 61 | std::string lmappedName = mappedName + ::ArrayString(elementIndex); |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 62 | ExpandUserDefinedVariable(variable, lname, lmappedName, markStaticUse, expanded); |
| 63 | } |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | ExpandUserDefinedVariable(variable, name, mappedName, markStaticUse, expanded); |
| 68 | } |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 69 | } |
| 70 | else |
| 71 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 72 | ShaderVariable expandedVar = variable; |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 73 | |
| 74 | expandedVar.name = name; |
| 75 | expandedVar.mappedName = mappedName; |
| 76 | |
| 77 | // Mark all expanded fields as used if the parent is used |
| 78 | if (markStaticUse) |
| 79 | { |
| 80 | expandedVar.staticUse = true; |
| 81 | } |
| 82 | |
| 83 | if (expandedVar.isArray()) |
| 84 | { |
| 85 | expandedVar.name += "[0]"; |
| 86 | expandedVar.mappedName += "[0]"; |
| 87 | } |
| 88 | |
| 89 | expanded->push_back(expandedVar); |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 90 | } |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 93 | void ExpandUserDefinedVariable(const ShaderVariable &variable, |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 94 | const std::string &name, |
| 95 | const std::string &mappedName, |
| 96 | bool markStaticUse, |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 97 | std::vector<ShaderVariable> *expanded) |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 98 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 99 | ASSERT(variable.isStruct()); |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 100 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 101 | const std::vector<ShaderVariable> &fields = variable.fields; |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 102 | |
| 103 | for (size_t fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++) |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 104 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 105 | const ShaderVariable &field = fields[fieldIndex]; |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 106 | ExpandVariable(field, |
| 107 | name + "." + field.name, |
| 108 | mappedName + "." + field.mappedName, |
| 109 | markStaticUse, |
| 110 | expanded); |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 114 | template <class VarT> |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 115 | VarT *FindVariable(const TString &name, |
| 116 | std::vector<VarT> *infoList) |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 117 | { |
| 118 | // TODO(zmo): optimize this function. |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 119 | for (size_t ii = 0; ii < infoList->size(); ++ii) |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 120 | { |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 121 | if ((*infoList)[ii].name.c_str() == name) |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 122 | return &((*infoList)[ii]); |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 123 | } |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 124 | |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 125 | return NULL; |
| 126 | } |
| 127 | |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 130 | CollectVariables::CollectVariables(std::vector<sh::Attribute> *attribs, |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 131 | std::vector<sh::Attribute> *outputVariables, |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 132 | std::vector<sh::Uniform> *uniforms, |
| 133 | std::vector<sh::Varying> *varyings, |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 134 | std::vector<sh::InterfaceBlock> *interfaceBlocks, |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 135 | ShHashFunction64 hashFunction, |
| 136 | const TSymbolTable &symbolTable) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 137 | : mAttribs(attribs), |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 138 | mOutputVariables(outputVariables), |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 139 | mUniforms(uniforms), |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 140 | mVaryings(varyings), |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 141 | mInterfaceBlocks(interfaceBlocks), |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 142 | mPointCoordAdded(false), |
| 143 | mFrontFacingAdded(false), |
| 144 | mFragCoordAdded(false), |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 145 | mPositionAdded(false), |
| 146 | mPointSizeAdded(false), |
| 147 | mHashFunction(hashFunction), |
| 148 | mSymbolTable(symbolTable) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 149 | { |
| 150 | } |
| 151 | |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 152 | // We want to check whether a uniform/varying is statically used |
| 153 | // because we only count the used ones in packing computing. |
| 154 | // Also, gl_FragCoord, gl_PointCoord, and gl_FrontFacing count |
| 155 | // toward varying counting if they are statically used in a fragment |
| 156 | // shader. |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 157 | void CollectVariables::visitSymbol(TIntermSymbol *symbol) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 158 | { |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 159 | ASSERT(symbol != NULL); |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 160 | ShaderVariable *var = NULL; |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 161 | const TString &symbolName = symbol->getSymbol(); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 162 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 163 | if (IsVarying(symbol->getQualifier())) |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 164 | { |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 165 | var = FindVariable(symbolName, mVaryings); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 166 | } |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 167 | else if (symbol->getType().getBasicType() == EbtInterfaceBlock) |
| 168 | { |
| 169 | UNREACHABLE(); |
| 170 | } |
| 171 | else |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 172 | { |
| 173 | switch (symbol->getQualifier()) |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 174 | { |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 175 | case EvqAttribute: |
| 176 | case EvqVertexIn: |
| 177 | var = FindVariable(symbolName, mAttribs); |
| 178 | break; |
| 179 | case EvqFragmentOut: |
| 180 | var = FindVariable(symbolName, mOutputVariables); |
| 181 | break; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 182 | case EvqUniform: |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 183 | { |
| 184 | const TInterfaceBlock *interfaceBlock = symbol->getType().getInterfaceBlock(); |
| 185 | if (interfaceBlock) |
| 186 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 187 | InterfaceBlock *namedBlock = FindVariable(interfaceBlock->name(), mInterfaceBlocks); |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 188 | ASSERT(namedBlock); |
| 189 | var = FindVariable(symbolName, &namedBlock->fields); |
| 190 | |
| 191 | // Set static use on the parent interface block here |
| 192 | namedBlock->staticUse = true; |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 193 | |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 194 | } |
| 195 | else |
| 196 | { |
| 197 | var = FindVariable(symbolName, mUniforms); |
| 198 | } |
| 199 | |
| 200 | // It's an internal error to reference an undefined user uniform |
| 201 | ASSERT(symbolName.compare(0, 3, "gl_") == 0 || var); |
| 202 | } |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 203 | break; |
| 204 | case EvqFragCoord: |
| 205 | if (!mFragCoordAdded) |
| 206 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 207 | Varying info; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 208 | const char kName[] = "gl_FragCoord"; |
| 209 | info.name = kName; |
| 210 | info.mappedName = kName; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 211 | info.type = GL_FLOAT_VEC4; |
| 212 | info.arraySize = 0; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 213 | info.precision = GL_MEDIUM_FLOAT; // Defined by spec. |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 214 | info.staticUse = true; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 215 | info.isInvariant = mSymbolTable.isVaryingInvariant(kName); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 216 | mVaryings->push_back(info); |
| 217 | mFragCoordAdded = true; |
| 218 | } |
| 219 | return; |
| 220 | case EvqFrontFacing: |
| 221 | if (!mFrontFacingAdded) |
| 222 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 223 | Varying info; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 224 | const char kName[] = "gl_FrontFacing"; |
| 225 | info.name = kName; |
| 226 | info.mappedName = kName; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 227 | info.type = GL_BOOL; |
| 228 | info.arraySize = 0; |
| 229 | info.precision = GL_NONE; |
| 230 | info.staticUse = true; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 231 | info.isInvariant = mSymbolTable.isVaryingInvariant(kName); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 232 | mVaryings->push_back(info); |
| 233 | mFrontFacingAdded = true; |
| 234 | } |
| 235 | return; |
| 236 | case EvqPointCoord: |
| 237 | if (!mPointCoordAdded) |
| 238 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 239 | Varying info; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 240 | const char kName[] = "gl_PointCoord"; |
| 241 | info.name = kName; |
| 242 | info.mappedName = kName; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 243 | info.type = GL_FLOAT_VEC2; |
| 244 | info.arraySize = 0; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 245 | info.precision = GL_MEDIUM_FLOAT; // Defined by spec. |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 246 | info.staticUse = true; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 247 | info.isInvariant = mSymbolTable.isVaryingInvariant(kName); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 248 | mVaryings->push_back(info); |
| 249 | mPointCoordAdded = true; |
| 250 | } |
| 251 | return; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 252 | case EvqPosition: |
| 253 | if (!mPositionAdded) |
| 254 | { |
| 255 | Varying info; |
| 256 | const char kName[] = "gl_Position"; |
| 257 | info.name = kName; |
| 258 | info.mappedName = kName; |
| 259 | info.type = GL_FLOAT_VEC4; |
| 260 | info.arraySize = 0; |
| 261 | info.precision = GL_HIGH_FLOAT; // Defined by spec. |
| 262 | info.staticUse = true; |
| 263 | info.isInvariant = mSymbolTable.isVaryingInvariant(kName); |
| 264 | mVaryings->push_back(info); |
| 265 | mPositionAdded = true; |
| 266 | } |
| 267 | return; |
| 268 | case EvqPointSize: |
| 269 | if (!mPointSizeAdded) |
| 270 | { |
| 271 | Varying info; |
| 272 | const char kName[] = "gl_PointSize"; |
| 273 | info.name = kName; |
| 274 | info.mappedName = kName; |
| 275 | info.type = GL_FLOAT; |
| 276 | info.arraySize = 0; |
| 277 | info.precision = GL_MEDIUM_FLOAT; // Defined by spec. |
| 278 | info.staticUse = true; |
| 279 | info.isInvariant = mSymbolTable.isVaryingInvariant(kName); |
| 280 | mVaryings->push_back(info); |
| 281 | mPointSizeAdded = true; |
| 282 | } |
| 283 | return; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 284 | default: |
| 285 | break; |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 286 | } |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 287 | } |
| 288 | if (var) |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 289 | { |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 290 | var->staticUse = true; |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 294 | class NameHashingTraverser : public GetVariableTraverser |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 295 | { |
| 296 | public: |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 297 | NameHashingTraverser(ShHashFunction64 hashFunction, |
| 298 | const TSymbolTable &symbolTable) |
| 299 | : GetVariableTraverser(symbolTable), |
| 300 | mHashFunction(hashFunction) |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 301 | {} |
| 302 | |
| 303 | private: |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 304 | DISALLOW_COPY_AND_ASSIGN(NameHashingTraverser); |
| 305 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 306 | virtual void visitVariable(ShaderVariable *variable) |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 307 | { |
| 308 | TString stringName = TString(variable->name.c_str()); |
| 309 | variable->mappedName = TIntermTraverser::hash(stringName, mHashFunction).c_str(); |
| 310 | } |
| 311 | |
| 312 | ShHashFunction64 mHashFunction; |
| 313 | }; |
| 314 | |
| 315 | // Attributes, which cannot have struct fields, are a special case |
| 316 | template <> |
| 317 | void CollectVariables::visitVariable(const TIntermSymbol *variable, |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 318 | std::vector<Attribute> *infoList) const |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 319 | { |
| 320 | ASSERT(variable); |
| 321 | const TType &type = variable->getType(); |
| 322 | ASSERT(!type.getStruct()); |
| 323 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 324 | Attribute attribute; |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 325 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 326 | attribute.type = GLVariableType(type); |
| 327 | attribute.precision = GLVariablePrecision(type); |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 328 | attribute.name = variable->getSymbol().c_str(); |
| 329 | attribute.arraySize = static_cast<unsigned int>(type.getArraySize()); |
| 330 | attribute.mappedName = TIntermTraverser::hash(variable->getSymbol(), mHashFunction).c_str(); |
| 331 | attribute.location = variable->getType().getLayoutQualifier().location; |
| 332 | |
| 333 | infoList->push_back(attribute); |
| 334 | } |
| 335 | |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 336 | template <> |
| 337 | void CollectVariables::visitVariable(const TIntermSymbol *variable, |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 338 | std::vector<InterfaceBlock> *infoList) const |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 339 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 340 | InterfaceBlock interfaceBlock; |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 341 | const TInterfaceBlock *blockType = variable->getType().getInterfaceBlock(); |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 342 | ASSERT(blockType); |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 343 | |
| 344 | interfaceBlock.name = blockType->name().c_str(); |
| 345 | interfaceBlock.mappedName = TIntermTraverser::hash(variable->getSymbol(), mHashFunction).c_str(); |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 346 | interfaceBlock.instanceName = (blockType->hasInstanceName() ? blockType->instanceName().c_str() : ""); |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 347 | interfaceBlock.arraySize = variable->getArraySize(); |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 348 | interfaceBlock.isRowMajorLayout = (blockType->matrixPacking() == EmpRowMajor); |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 349 | interfaceBlock.layout = GetBlockLayoutType(blockType->blockStorage()); |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 350 | |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 351 | // Gather field information |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 352 | const TFieldList &fieldList = blockType->fields(); |
| 353 | |
| 354 | for (size_t fieldIndex = 0; fieldIndex < fieldList.size(); ++fieldIndex) |
| 355 | { |
| 356 | const TField &field = *fieldList[fieldIndex]; |
| 357 | const TString &fullFieldName = InterfaceBlockFieldName(*blockType, field); |
| 358 | const TType &fieldType = *field.type(); |
| 359 | |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 360 | GetVariableTraverser traverser(mSymbolTable); |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 361 | traverser.traverse(fieldType, fullFieldName, &interfaceBlock.fields); |
| 362 | |
| 363 | interfaceBlock.fields.back().isRowMajorLayout = (fieldType.getLayoutQualifier().matrixPacking == EmpRowMajor); |
| 364 | } |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 365 | |
| 366 | infoList->push_back(interfaceBlock); |
| 367 | } |
| 368 | |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 369 | template <typename VarT> |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 370 | void CollectVariables::visitVariable(const TIntermSymbol *variable, |
| 371 | std::vector<VarT> *infoList) const |
| 372 | { |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 373 | NameHashingTraverser traverser(mHashFunction, mSymbolTable); |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 374 | traverser.traverse(variable->getType(), variable->getSymbol(), infoList); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | template <typename VarT> |
| 378 | void CollectVariables::visitInfoList(const TIntermSequence &sequence, |
| 379 | std::vector<VarT> *infoList) const |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 380 | { |
| 381 | for (size_t seqIndex = 0; seqIndex < sequence.size(); seqIndex++) |
| 382 | { |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 383 | const TIntermSymbol *variable = sequence[seqIndex]->getAsSymbolNode(); |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 384 | // The only case in which the sequence will not contain a |
| 385 | // TIntermSymbol node is initialization. It will contain a |
| 386 | // TInterBinary node in that case. Since attributes, uniforms, |
| 387 | // and varyings cannot be initialized in a shader, we must have |
| 388 | // only TIntermSymbol nodes in the sequence. |
| 389 | ASSERT(variable != NULL); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 390 | visitVariable(variable, infoList); |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 391 | } |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 394 | bool CollectVariables::visitAggregate(Visit, TIntermAggregate *node) |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 395 | { |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 396 | bool visitChildren = true; |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 397 | |
| 398 | switch (node->getOp()) |
| 399 | { |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 400 | case EOpDeclaration: |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 401 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 402 | const TIntermSequence &sequence = *(node->getSequence()); |
Jamie Madill | 1c28e1f | 2014-08-04 11:37:54 -0400 | [diff] [blame] | 403 | ASSERT(!sequence.empty()); |
| 404 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 405 | const TIntermTyped &typedNode = *(sequence.front()->getAsTyped()); |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 406 | TQualifier qualifier = typedNode.getQualifier(); |
| 407 | |
| 408 | if (typedNode.getBasicType() == EbtInterfaceBlock) |
| 409 | { |
| 410 | visitInfoList(sequence, mInterfaceBlocks); |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 411 | visitChildren = false; |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 412 | } |
| 413 | else if (qualifier == EvqAttribute || qualifier == EvqVertexIn || |
| 414 | qualifier == EvqFragmentOut || qualifier == EvqUniform || |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 415 | IsVarying(qualifier)) |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 416 | { |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 417 | switch (qualifier) |
| 418 | { |
| 419 | case EvqAttribute: |
| 420 | case EvqVertexIn: |
| 421 | visitInfoList(sequence, mAttribs); |
| 422 | break; |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 423 | case EvqFragmentOut: |
| 424 | visitInfoList(sequence, mOutputVariables); |
| 425 | break; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 426 | case EvqUniform: |
| 427 | visitInfoList(sequence, mUniforms); |
| 428 | break; |
| 429 | default: |
Jamie Madill | 3b5c2da | 2014-08-19 15:23:32 -0400 | [diff] [blame] | 430 | visitInfoList(sequence, mVaryings); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 431 | break; |
| 432 | } |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 433 | |
Jamie Madill | 1c28e1f | 2014-08-04 11:37:54 -0400 | [diff] [blame] | 434 | visitChildren = false; |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 435 | } |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 436 | break; |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 437 | } |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 438 | default: break; |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | return visitChildren; |
| 442 | } |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 443 | |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 444 | bool CollectVariables::visitBinary(Visit, TIntermBinary *binaryNode) |
| 445 | { |
| 446 | if (binaryNode->getOp() == EOpIndexDirectInterfaceBlock) |
| 447 | { |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 448 | // NOTE: we do not determine static use for individual blocks of an array |
| 449 | TIntermTyped *blockNode = binaryNode->getLeft()->getAsTyped(); |
| 450 | ASSERT(blockNode); |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 451 | |
| 452 | TIntermConstantUnion *constantUnion = binaryNode->getRight()->getAsConstantUnion(); |
| 453 | ASSERT(constantUnion); |
| 454 | |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 455 | const TInterfaceBlock *interfaceBlock = blockNode->getType().getInterfaceBlock(); |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 456 | InterfaceBlock *namedBlock = FindVariable(interfaceBlock->name(), mInterfaceBlocks); |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 457 | ASSERT(namedBlock); |
| 458 | namedBlock->staticUse = true; |
| 459 | |
| 460 | unsigned int fieldIndex = constantUnion->getUConst(0); |
| 461 | ASSERT(fieldIndex < namedBlock->fields.size()); |
| 462 | namedBlock->fields[fieldIndex].staticUse = true; |
| 463 | return false; |
| 464 | } |
| 465 | |
| 466 | return true; |
| 467 | } |
| 468 | |
Zhenyao Mo | 409078f | 2014-10-28 13:23:18 -0700 | [diff] [blame^] | 469 | void ExpandUniforms(const std::vector<Uniform> &compact, |
| 470 | std::vector<ShaderVariable> *expanded) |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 471 | { |
| 472 | for (size_t variableIndex = 0; variableIndex < compact.size(); variableIndex++) |
| 473 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 474 | const ShaderVariable &variable = compact[variableIndex]; |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 475 | ExpandVariable(variable, variable.name, variable.mappedName, variable.staticUse, expanded); |
| 476 | } |
| 477 | } |
| 478 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 479 | } |