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) |
Olli Etuaho | 3d0d9a4 | 2015-06-01 12:16:36 +0300 | [diff] [blame] | 137 | : TIntermTraverser(true, false, false), |
| 138 | mAttribs(attribs), |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 139 | mOutputVariables(outputVariables), |
daniel@transgaming.com | 0aa3b5a | 2012-11-28 19:43:24 +0000 | [diff] [blame] | 140 | mUniforms(uniforms), |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 141 | mVaryings(varyings), |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 142 | mInterfaceBlocks(interfaceBlocks), |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 143 | mDepthRangeAdded(false), |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 144 | mPointCoordAdded(false), |
| 145 | mFrontFacingAdded(false), |
| 146 | mFragCoordAdded(false), |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 147 | mInstanceIDAdded(false), |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 148 | mPositionAdded(false), |
| 149 | mPointSizeAdded(false), |
Erik Dahlström | ea7a212 | 2014-11-17 16:15:57 +0100 | [diff] [blame] | 150 | mLastFragDataAdded(false), |
Kimmo Kinnunen | 0932df6 | 2015-07-21 14:35:11 +0300 | [diff] [blame^] | 151 | mFragColorAdded(false), |
| 152 | mFragDataAdded(false), |
| 153 | mFragDepthAdded(false), |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 154 | mHashFunction(hashFunction), |
| 155 | mSymbolTable(symbolTable) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 156 | { |
| 157 | } |
| 158 | |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 159 | // We want to check whether a uniform/varying is statically used |
| 160 | // because we only count the used ones in packing computing. |
| 161 | // Also, gl_FragCoord, gl_PointCoord, and gl_FrontFacing count |
| 162 | // toward varying counting if they are statically used in a fragment |
| 163 | // shader. |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 164 | void CollectVariables::visitSymbol(TIntermSymbol *symbol) |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 165 | { |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 166 | ASSERT(symbol != NULL); |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 167 | ShaderVariable *var = NULL; |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 168 | const TString &symbolName = symbol->getSymbol(); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 169 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 170 | if (IsVarying(symbol->getQualifier())) |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 171 | { |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 172 | var = FindVariable(symbolName, mVaryings); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 173 | } |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 174 | else if (symbol->getType().getBasicType() == EbtInterfaceBlock) |
| 175 | { |
| 176 | UNREACHABLE(); |
| 177 | } |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 178 | else if (symbolName == "gl_DepthRange") |
| 179 | { |
| 180 | ASSERT(symbol->getQualifier() == EvqUniform); |
| 181 | |
| 182 | if (!mDepthRangeAdded) |
| 183 | { |
| 184 | Uniform info; |
| 185 | const char kName[] = "gl_DepthRange"; |
| 186 | info.name = kName; |
| 187 | info.mappedName = kName; |
| 188 | info.type = GL_STRUCT_ANGLEX; |
| 189 | info.arraySize = 0; |
| 190 | info.precision = GL_NONE; |
| 191 | info.staticUse = true; |
| 192 | |
| 193 | ShaderVariable nearInfo; |
| 194 | const char kNearName[] = "near"; |
| 195 | nearInfo.name = kNearName; |
| 196 | nearInfo.mappedName = kNearName; |
| 197 | nearInfo.type = GL_FLOAT; |
| 198 | nearInfo.arraySize = 0; |
| 199 | nearInfo.precision = GL_HIGH_FLOAT; |
| 200 | nearInfo.staticUse = true; |
| 201 | |
| 202 | ShaderVariable farInfo; |
| 203 | const char kFarName[] = "far"; |
| 204 | farInfo.name = kFarName; |
| 205 | farInfo.mappedName = kFarName; |
| 206 | farInfo.type = GL_FLOAT; |
| 207 | farInfo.arraySize = 0; |
| 208 | farInfo.precision = GL_HIGH_FLOAT; |
| 209 | farInfo.staticUse = true; |
| 210 | |
| 211 | ShaderVariable diffInfo; |
| 212 | const char kDiffName[] = "diff"; |
| 213 | diffInfo.name = kDiffName; |
| 214 | diffInfo.mappedName = kDiffName; |
| 215 | diffInfo.type = GL_FLOAT; |
| 216 | diffInfo.arraySize = 0; |
| 217 | diffInfo.precision = GL_HIGH_FLOAT; |
| 218 | diffInfo.staticUse = true; |
| 219 | |
| 220 | info.fields.push_back(nearInfo); |
| 221 | info.fields.push_back(farInfo); |
| 222 | info.fields.push_back(diffInfo); |
| 223 | |
| 224 | mUniforms->push_back(info); |
| 225 | mDepthRangeAdded = true; |
| 226 | } |
| 227 | } |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 228 | else |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 229 | { |
| 230 | switch (symbol->getQualifier()) |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 231 | { |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 232 | case EvqAttribute: |
| 233 | case EvqVertexIn: |
| 234 | var = FindVariable(symbolName, mAttribs); |
| 235 | break; |
| 236 | case EvqFragmentOut: |
| 237 | var = FindVariable(symbolName, mOutputVariables); |
| 238 | break; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 239 | case EvqUniform: |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 240 | { |
| 241 | const TInterfaceBlock *interfaceBlock = symbol->getType().getInterfaceBlock(); |
| 242 | if (interfaceBlock) |
| 243 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 244 | InterfaceBlock *namedBlock = FindVariable(interfaceBlock->name(), mInterfaceBlocks); |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 245 | ASSERT(namedBlock); |
| 246 | var = FindVariable(symbolName, &namedBlock->fields); |
| 247 | |
| 248 | // Set static use on the parent interface block here |
| 249 | namedBlock->staticUse = true; |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | var = FindVariable(symbolName, mUniforms); |
| 254 | } |
| 255 | |
| 256 | // It's an internal error to reference an undefined user uniform |
Jamie Madill | 55def58 | 2015-05-04 11:24:57 -0400 | [diff] [blame] | 257 | ASSERT(symbolName.compare(0, 3, "gl_") != 0 || var); |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 258 | } |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 259 | break; |
| 260 | case EvqFragCoord: |
| 261 | if (!mFragCoordAdded) |
| 262 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 263 | Varying info; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 264 | const char kName[] = "gl_FragCoord"; |
| 265 | info.name = kName; |
| 266 | info.mappedName = kName; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 267 | info.type = GL_FLOAT_VEC4; |
| 268 | info.arraySize = 0; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 269 | info.precision = GL_MEDIUM_FLOAT; // Defined by spec. |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 270 | info.staticUse = true; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 271 | info.isInvariant = mSymbolTable.isVaryingInvariant(kName); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 272 | mVaryings->push_back(info); |
| 273 | mFragCoordAdded = true; |
| 274 | } |
| 275 | return; |
| 276 | case EvqFrontFacing: |
| 277 | if (!mFrontFacingAdded) |
| 278 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 279 | Varying info; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 280 | const char kName[] = "gl_FrontFacing"; |
| 281 | info.name = kName; |
| 282 | info.mappedName = kName; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 283 | info.type = GL_BOOL; |
| 284 | info.arraySize = 0; |
| 285 | info.precision = GL_NONE; |
| 286 | info.staticUse = true; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 287 | info.isInvariant = mSymbolTable.isVaryingInvariant(kName); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 288 | mVaryings->push_back(info); |
| 289 | mFrontFacingAdded = true; |
| 290 | } |
| 291 | return; |
| 292 | case EvqPointCoord: |
| 293 | if (!mPointCoordAdded) |
| 294 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 295 | Varying info; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 296 | const char kName[] = "gl_PointCoord"; |
| 297 | info.name = kName; |
| 298 | info.mappedName = kName; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 299 | info.type = GL_FLOAT_VEC2; |
| 300 | info.arraySize = 0; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 301 | info.precision = GL_MEDIUM_FLOAT; // Defined by spec. |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 302 | info.staticUse = true; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 303 | info.isInvariant = mSymbolTable.isVaryingInvariant(kName); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 304 | mVaryings->push_back(info); |
| 305 | mPointCoordAdded = true; |
| 306 | } |
| 307 | return; |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 308 | case EvqInstanceID: |
| 309 | if (!mInstanceIDAdded) |
| 310 | { |
| 311 | Attribute info; |
| 312 | const char kName[] = "gl_InstanceID"; |
| 313 | info.name = kName; |
| 314 | info.mappedName = kName; |
| 315 | info.type = GL_INT; |
| 316 | info.arraySize = 0; |
| 317 | info.precision = GL_HIGH_INT; // Defined by spec. |
| 318 | info.staticUse = true; |
| 319 | info.location = -1; |
| 320 | mAttribs->push_back(info); |
| 321 | mInstanceIDAdded = true; |
| 322 | } |
| 323 | return; |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 324 | case EvqPosition: |
| 325 | if (!mPositionAdded) |
| 326 | { |
| 327 | Varying info; |
| 328 | const char kName[] = "gl_Position"; |
| 329 | info.name = kName; |
| 330 | info.mappedName = kName; |
| 331 | info.type = GL_FLOAT_VEC4; |
| 332 | info.arraySize = 0; |
| 333 | info.precision = GL_HIGH_FLOAT; // Defined by spec. |
| 334 | info.staticUse = true; |
| 335 | info.isInvariant = mSymbolTable.isVaryingInvariant(kName); |
| 336 | mVaryings->push_back(info); |
| 337 | mPositionAdded = true; |
| 338 | } |
| 339 | return; |
| 340 | case EvqPointSize: |
| 341 | if (!mPointSizeAdded) |
| 342 | { |
| 343 | Varying info; |
| 344 | const char kName[] = "gl_PointSize"; |
| 345 | info.name = kName; |
| 346 | info.mappedName = kName; |
| 347 | info.type = GL_FLOAT; |
| 348 | info.arraySize = 0; |
| 349 | info.precision = GL_MEDIUM_FLOAT; // Defined by spec. |
| 350 | info.staticUse = true; |
| 351 | info.isInvariant = mSymbolTable.isVaryingInvariant(kName); |
| 352 | mVaryings->push_back(info); |
| 353 | mPointSizeAdded = true; |
| 354 | } |
| 355 | return; |
Erik Dahlström | ea7a212 | 2014-11-17 16:15:57 +0100 | [diff] [blame] | 356 | case EvqLastFragData: |
| 357 | if (!mLastFragDataAdded) |
| 358 | { |
| 359 | Varying info; |
| 360 | const char kName[] = "gl_LastFragData"; |
| 361 | info.name = kName; |
| 362 | info.mappedName = kName; |
| 363 | info.type = GL_FLOAT_VEC4; |
| 364 | info.arraySize = static_cast<const TVariable*>(mSymbolTable.findBuiltIn("gl_MaxDrawBuffers", 100))->getConstPointer()->getIConst(); |
| 365 | info.precision = GL_MEDIUM_FLOAT; // Defined by spec. |
| 366 | info.staticUse = true; |
| 367 | info.isInvariant = mSymbolTable.isVaryingInvariant(kName); |
| 368 | mVaryings->push_back(info); |
| 369 | mLastFragDataAdded = true; |
| 370 | } |
| 371 | return; |
Kimmo Kinnunen | 0932df6 | 2015-07-21 14:35:11 +0300 | [diff] [blame^] | 372 | case EvqFragColor: |
| 373 | if (!mFragColorAdded) |
| 374 | { |
| 375 | Attribute info; |
| 376 | const char kName[] = "gl_FragColor"; |
| 377 | info.name = kName; |
| 378 | info.mappedName = kName; |
| 379 | info.type = GL_FLOAT_VEC4; |
| 380 | info.arraySize = 0; |
| 381 | info.precision = GL_MEDIUM_FLOAT; // Defined by spec. |
| 382 | info.staticUse = true; |
| 383 | mOutputVariables->push_back(info); |
| 384 | mFragColorAdded = true; |
| 385 | } |
| 386 | return; |
| 387 | case EvqFragData: |
| 388 | if (!mFragDataAdded) |
| 389 | { |
| 390 | Attribute info; |
| 391 | const char kName[] = "gl_FragData"; |
| 392 | info.name = kName; |
| 393 | info.mappedName = kName; |
| 394 | info.type = GL_FLOAT_VEC4; |
| 395 | info.arraySize = static_cast<const TVariable *>( |
| 396 | mSymbolTable.findBuiltIn("gl_MaxDrawBuffers", 100)) |
| 397 | ->getConstPointer() |
| 398 | ->getIConst(); |
| 399 | info.precision = GL_MEDIUM_FLOAT; // Defined by spec. |
| 400 | info.staticUse = true; |
| 401 | mOutputVariables->push_back(info); |
| 402 | mFragDataAdded = true; |
| 403 | } |
| 404 | return; |
| 405 | case EvqFragDepth: |
| 406 | if (!mFragDepthAdded) |
| 407 | { |
| 408 | Attribute info; |
| 409 | const char kName[] = "gl_FragDepthEXT"; |
| 410 | info.name = kName; |
| 411 | info.mappedName = kName; |
| 412 | info.type = GL_FLOAT; |
| 413 | info.arraySize = 0; |
| 414 | info.precision = |
| 415 | GLVariablePrecision(static_cast<const TVariable *>( |
| 416 | mSymbolTable.findBuiltIn("gl_FragDepthEXT", 100)) |
| 417 | ->getType()); |
| 418 | info.staticUse = true; |
| 419 | mOutputVariables->push_back(info); |
| 420 | mFragDepthAdded = true; |
| 421 | } |
| 422 | return; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 423 | default: |
| 424 | break; |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 425 | } |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 426 | } |
| 427 | if (var) |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 428 | { |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 429 | var->staticUse = true; |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 433 | class NameHashingTraverser : public GetVariableTraverser |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 434 | { |
| 435 | public: |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 436 | NameHashingTraverser(ShHashFunction64 hashFunction, |
| 437 | const TSymbolTable &symbolTable) |
| 438 | : GetVariableTraverser(symbolTable), |
| 439 | mHashFunction(hashFunction) |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 440 | {} |
| 441 | |
| 442 | private: |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 443 | virtual void visitVariable(ShaderVariable *variable) |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 444 | { |
| 445 | TString stringName = TString(variable->name.c_str()); |
| 446 | variable->mappedName = TIntermTraverser::hash(stringName, mHashFunction).c_str(); |
| 447 | } |
| 448 | |
| 449 | ShHashFunction64 mHashFunction; |
| 450 | }; |
| 451 | |
| 452 | // Attributes, which cannot have struct fields, are a special case |
| 453 | template <> |
| 454 | void CollectVariables::visitVariable(const TIntermSymbol *variable, |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 455 | std::vector<Attribute> *infoList) const |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 456 | { |
| 457 | ASSERT(variable); |
| 458 | const TType &type = variable->getType(); |
| 459 | ASSERT(!type.getStruct()); |
| 460 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 461 | Attribute attribute; |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 462 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 463 | attribute.type = GLVariableType(type); |
| 464 | attribute.precision = GLVariablePrecision(type); |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 465 | attribute.name = variable->getSymbol().c_str(); |
| 466 | attribute.arraySize = static_cast<unsigned int>(type.getArraySize()); |
| 467 | attribute.mappedName = TIntermTraverser::hash(variable->getSymbol(), mHashFunction).c_str(); |
| 468 | attribute.location = variable->getType().getLayoutQualifier().location; |
| 469 | |
| 470 | infoList->push_back(attribute); |
| 471 | } |
| 472 | |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 473 | template <> |
| 474 | void CollectVariables::visitVariable(const TIntermSymbol *variable, |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 475 | std::vector<InterfaceBlock> *infoList) const |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 476 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 477 | InterfaceBlock interfaceBlock; |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 478 | const TInterfaceBlock *blockType = variable->getType().getInterfaceBlock(); |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 479 | ASSERT(blockType); |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 480 | |
| 481 | interfaceBlock.name = blockType->name().c_str(); |
| 482 | interfaceBlock.mappedName = TIntermTraverser::hash(variable->getSymbol(), mHashFunction).c_str(); |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 483 | interfaceBlock.instanceName = (blockType->hasInstanceName() ? blockType->instanceName().c_str() : ""); |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 484 | interfaceBlock.arraySize = variable->getArraySize(); |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 485 | interfaceBlock.isRowMajorLayout = (blockType->matrixPacking() == EmpRowMajor); |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 486 | interfaceBlock.layout = GetBlockLayoutType(blockType->blockStorage()); |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 487 | |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 488 | // Gather field information |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 489 | const TFieldList &fieldList = blockType->fields(); |
| 490 | |
| 491 | for (size_t fieldIndex = 0; fieldIndex < fieldList.size(); ++fieldIndex) |
| 492 | { |
| 493 | const TField &field = *fieldList[fieldIndex]; |
| 494 | const TString &fullFieldName = InterfaceBlockFieldName(*blockType, field); |
| 495 | const TType &fieldType = *field.type(); |
| 496 | |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 497 | GetVariableTraverser traverser(mSymbolTable); |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 498 | traverser.traverse(fieldType, fullFieldName, &interfaceBlock.fields); |
| 499 | |
| 500 | interfaceBlock.fields.back().isRowMajorLayout = (fieldType.getLayoutQualifier().matrixPacking == EmpRowMajor); |
| 501 | } |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 502 | |
| 503 | infoList->push_back(interfaceBlock); |
| 504 | } |
| 505 | |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 506 | template <typename VarT> |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 507 | void CollectVariables::visitVariable(const TIntermSymbol *variable, |
| 508 | std::vector<VarT> *infoList) const |
| 509 | { |
Zhenyao Mo | 94ac7b7 | 2014-10-15 18:22:08 -0700 | [diff] [blame] | 510 | NameHashingTraverser traverser(mHashFunction, mSymbolTable); |
Jamie Madill | 42bcf32 | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 511 | traverser.traverse(variable->getType(), variable->getSymbol(), infoList); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | template <typename VarT> |
| 515 | void CollectVariables::visitInfoList(const TIntermSequence &sequence, |
| 516 | std::vector<VarT> *infoList) const |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 517 | { |
| 518 | for (size_t seqIndex = 0; seqIndex < sequence.size(); seqIndex++) |
| 519 | { |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 520 | const TIntermSymbol *variable = sequence[seqIndex]->getAsSymbolNode(); |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 521 | // The only case in which the sequence will not contain a |
| 522 | // TIntermSymbol node is initialization. It will contain a |
| 523 | // TInterBinary node in that case. Since attributes, uniforms, |
| 524 | // and varyings cannot be initialized in a shader, we must have |
| 525 | // only TIntermSymbol nodes in the sequence. |
| 526 | ASSERT(variable != NULL); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 527 | visitVariable(variable, infoList); |
Jamie Madill | a718c1e | 2014-07-02 15:31:22 -0400 | [diff] [blame] | 528 | } |
alokp@chromium.org | 07620a5 | 2010-09-23 17:53:56 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 531 | bool CollectVariables::visitAggregate(Visit, TIntermAggregate *node) |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 532 | { |
Zhenyao Mo | d2d340b | 2013-09-23 14:57:05 -0400 | [diff] [blame] | 533 | bool visitChildren = true; |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 534 | |
| 535 | switch (node->getOp()) |
| 536 | { |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 537 | case EOpDeclaration: |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 538 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 539 | const TIntermSequence &sequence = *(node->getSequence()); |
Jamie Madill | 1c28e1f | 2014-08-04 11:37:54 -0400 | [diff] [blame] | 540 | ASSERT(!sequence.empty()); |
| 541 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 542 | const TIntermTyped &typedNode = *(sequence.front()->getAsTyped()); |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 543 | TQualifier qualifier = typedNode.getQualifier(); |
| 544 | |
| 545 | if (typedNode.getBasicType() == EbtInterfaceBlock) |
| 546 | { |
| 547 | visitInfoList(sequence, mInterfaceBlocks); |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 548 | visitChildren = false; |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 549 | } |
| 550 | else if (qualifier == EvqAttribute || qualifier == EvqVertexIn || |
| 551 | qualifier == EvqFragmentOut || qualifier == EvqUniform || |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 552 | IsVarying(qualifier)) |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 553 | { |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 554 | switch (qualifier) |
| 555 | { |
| 556 | case EvqAttribute: |
| 557 | case EvqVertexIn: |
| 558 | visitInfoList(sequence, mAttribs); |
| 559 | break; |
Jamie Madill | d5512cd | 2014-07-10 17:50:08 -0400 | [diff] [blame] | 560 | case EvqFragmentOut: |
| 561 | visitInfoList(sequence, mOutputVariables); |
| 562 | break; |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 563 | case EvqUniform: |
| 564 | visitInfoList(sequence, mUniforms); |
| 565 | break; |
| 566 | default: |
Jamie Madill | 3b5c2da | 2014-08-19 15:23:32 -0400 | [diff] [blame] | 567 | visitInfoList(sequence, mVaryings); |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 568 | break; |
| 569 | } |
Zhenyao Mo | 74da9f2 | 2013-09-23 14:57:01 -0400 | [diff] [blame] | 570 | |
Jamie Madill | 1c28e1f | 2014-08-04 11:37:54 -0400 | [diff] [blame] | 571 | visitChildren = false; |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 572 | } |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 573 | break; |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 574 | } |
Jamie Madill | 4667c45 | 2014-07-08 15:02:36 -0400 | [diff] [blame] | 575 | default: break; |
alokp@chromium.org | ee76f6a | 2010-09-27 19:28:55 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | return visitChildren; |
| 579 | } |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 580 | |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 581 | bool CollectVariables::visitBinary(Visit, TIntermBinary *binaryNode) |
| 582 | { |
| 583 | if (binaryNode->getOp() == EOpIndexDirectInterfaceBlock) |
| 584 | { |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 585 | // NOTE: we do not determine static use for individual blocks of an array |
| 586 | TIntermTyped *blockNode = binaryNode->getLeft()->getAsTyped(); |
| 587 | ASSERT(blockNode); |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 588 | |
| 589 | TIntermConstantUnion *constantUnion = binaryNode->getRight()->getAsConstantUnion(); |
| 590 | ASSERT(constantUnion); |
| 591 | |
Jamie Madill | a6f267f | 2014-08-27 11:44:15 -0400 | [diff] [blame] | 592 | const TInterfaceBlock *interfaceBlock = blockNode->getType().getInterfaceBlock(); |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 593 | InterfaceBlock *namedBlock = FindVariable(interfaceBlock->name(), mInterfaceBlocks); |
Jamie Madill | b547ddf | 2014-08-25 16:20:46 -0400 | [diff] [blame] | 594 | ASSERT(namedBlock); |
| 595 | namedBlock->staticUse = true; |
| 596 | |
| 597 | unsigned int fieldIndex = constantUnion->getUConst(0); |
| 598 | ASSERT(fieldIndex < namedBlock->fields.size()); |
| 599 | namedBlock->fields[fieldIndex].staticUse = true; |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | return true; |
| 604 | } |
| 605 | |
Zhenyao Mo | 409078f | 2014-10-28 13:23:18 -0700 | [diff] [blame] | 606 | void ExpandUniforms(const std::vector<Uniform> &compact, |
| 607 | std::vector<ShaderVariable> *expanded) |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 608 | { |
| 609 | for (size_t variableIndex = 0; variableIndex < compact.size(); variableIndex++) |
| 610 | { |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 611 | const ShaderVariable &variable = compact[variableIndex]; |
Jamie Madill | 23a8a43 | 2014-07-09 13:27:42 -0400 | [diff] [blame] | 612 | ExpandVariable(variable, variable.name, variable.mappedName, variable.staticUse, expanded); |
| 613 | } |
| 614 | } |
| 615 | |
Jamie Madill | a2fbb84 | 2014-09-03 09:40:47 -0400 | [diff] [blame] | 616 | } |