Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 7 | #include "common/debug.h" |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 8 | #include "compiler/translator/RegenerateStructNames.h" |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 9 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame^] | 10 | namespace sh |
| 11 | { |
| 12 | |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 13 | void RegenerateStructNames::visitSymbol(TIntermSymbol *symbol) |
| 14 | { |
| 15 | ASSERT(symbol); |
| 16 | TType *type = symbol->getTypePointer(); |
| 17 | ASSERT(type); |
| 18 | TStructure *userType = type->getStruct(); |
| 19 | if (!userType) |
| 20 | return; |
| 21 | |
| 22 | if (mSymbolTable.findBuiltIn(userType->name(), mShaderVersion)) |
| 23 | { |
| 24 | // Built-in struct, do not touch it. |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | int uniqueId = userType->uniqueId(); |
| 29 | |
| 30 | ASSERT(mScopeDepth > 0); |
| 31 | if (mScopeDepth == 1) |
| 32 | { |
| 33 | // If a struct is defined at global scope, we don't map its name. |
| 34 | // This is because at global level, the struct might be used to |
| 35 | // declare a uniform, so the same name needs to stay the same for |
| 36 | // vertex/fragment shaders. However, our mapping uses internal ID, |
| 37 | // which will be different for the same struct in vertex/fragment |
| 38 | // shaders. |
| 39 | // This is OK because names for any structs defined in other scopes |
| 40 | // will begin with "_webgl", which is reserved. So there will be |
| 41 | // no conflicts among unmapped struct names from global scope and |
| 42 | // mapped struct names from other scopes. |
| 43 | // However, we need to keep track of these global structs, so if a |
| 44 | // variable is used in a local scope, we don't try to modify the |
| 45 | // struct name through that variable. |
| 46 | mDeclaredGlobalStructs.insert(uniqueId); |
| 47 | return; |
| 48 | } |
| 49 | if (mDeclaredGlobalStructs.count(uniqueId) > 0) |
| 50 | return; |
| 51 | // Map {name} to _webgl_struct_{uniqueId}_{name}. |
| 52 | const char kPrefix[] = "_webgl_struct_"; |
| 53 | if (userType->name().find(kPrefix) == 0) |
| 54 | { |
| 55 | // The name has already been regenerated. |
| 56 | return; |
| 57 | } |
| 58 | std::string id = Str(uniqueId); |
| 59 | TString tmp = kPrefix + TString(id.c_str()); |
| 60 | tmp += "_" + userType->name(); |
| 61 | userType->setName(tmp); |
| 62 | } |
| 63 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 64 | bool RegenerateStructNames::visitBlock(Visit, TIntermBlock *block) |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 65 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 66 | ++mScopeDepth; |
| 67 | TIntermSequence &sequence = *(block->getSequence()); |
| 68 | for (TIntermNode *node : sequence) |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 69 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 70 | node->traverse(this); |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 71 | } |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 72 | --mScopeDepth; |
| 73 | return false; |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 74 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame^] | 75 | |
| 76 | } // namespace sh |