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 | |
Olli Etuaho | ae4dbf3 | 2017-12-08 20:49:00 +0100 | [diff] [blame^] | 22 | if (userType->symbolType() == SymbolType::BuiltIn || |
| 23 | userType->symbolType() == SymbolType::Empty) |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 24 | { |
Olli Etuaho | ae4dbf3 | 2017-12-08 20:49:00 +0100 | [diff] [blame^] | 25 | // Built-in struct or nameless struct, do not touch it. |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 26 | return; |
| 27 | } |
| 28 | |
Olli Etuaho | ae4dbf3 | 2017-12-08 20:49:00 +0100 | [diff] [blame^] | 29 | ASSERT(userType->name() != nullptr); |
| 30 | |
Olli Etuaho | 97fa855 | 2017-11-28 16:28:42 +0200 | [diff] [blame] | 31 | int uniqueId = userType->uniqueId().get(); |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 32 | |
| 33 | ASSERT(mScopeDepth > 0); |
| 34 | if (mScopeDepth == 1) |
| 35 | { |
| 36 | // If a struct is defined at global scope, we don't map its name. |
| 37 | // This is because at global level, the struct might be used to |
| 38 | // declare a uniform, so the same name needs to stay the same for |
| 39 | // vertex/fragment shaders. However, our mapping uses internal ID, |
| 40 | // which will be different for the same struct in vertex/fragment |
| 41 | // shaders. |
| 42 | // This is OK because names for any structs defined in other scopes |
| 43 | // will begin with "_webgl", which is reserved. So there will be |
| 44 | // no conflicts among unmapped struct names from global scope and |
| 45 | // mapped struct names from other scopes. |
| 46 | // However, we need to keep track of these global structs, so if a |
| 47 | // variable is used in a local scope, we don't try to modify the |
| 48 | // struct name through that variable. |
| 49 | mDeclaredGlobalStructs.insert(uniqueId); |
| 50 | return; |
| 51 | } |
| 52 | if (mDeclaredGlobalStructs.count(uniqueId) > 0) |
| 53 | return; |
| 54 | // Map {name} to _webgl_struct_{uniqueId}_{name}. |
| 55 | const char kPrefix[] = "_webgl_struct_"; |
Olli Etuaho | ae4dbf3 | 2017-12-08 20:49:00 +0100 | [diff] [blame^] | 56 | if (userType->name()->find(kPrefix) == 0) |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 57 | { |
| 58 | // The name has already been regenerated. |
| 59 | return; |
| 60 | } |
| 61 | std::string id = Str(uniqueId); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 62 | TString tmp = kPrefix + TString(id.c_str()); |
Olli Etuaho | ae4dbf3 | 2017-12-08 20:49:00 +0100 | [diff] [blame^] | 63 | tmp += "_" + *userType->name(); |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 64 | userType->setName(tmp); |
| 65 | } |
| 66 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 67 | bool RegenerateStructNames::visitBlock(Visit, TIntermBlock *block) |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 68 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 69 | ++mScopeDepth; |
| 70 | TIntermSequence &sequence = *(block->getSequence()); |
| 71 | for (TIntermNode *node : sequence) |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 72 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 73 | node->traverse(this); |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 74 | } |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 75 | --mScopeDepth; |
| 76 | return false; |
Zhenyao Mo | e740add | 2014-07-18 17:01:01 -0700 | [diff] [blame] | 77 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 78 | |
| 79 | } // namespace sh |