blob: 32940d8d896cafe2d8c7557c0340696f2cce30d0 [file] [log] [blame]
Zhenyao Moe740add2014-07-18 17:01:01 -07001//
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 Etuahod57e0db2015-04-24 15:05:08 +03007#include "common/debug.h"
Zhenyao Moe740add2014-07-18 17:01:01 -07008#include "compiler/translator/RegenerateStructNames.h"
Zhenyao Moe740add2014-07-18 17:01:01 -07009
Jamie Madill45bcc782016-11-07 13:58:48 -050010namespace sh
11{
12
Zhenyao Moe740add2014-07-18 17:01:01 -070013void 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 Etuahoae4dbf32017-12-08 20:49:00 +010022 if (userType->symbolType() == SymbolType::BuiltIn ||
23 userType->symbolType() == SymbolType::Empty)
Zhenyao Moe740add2014-07-18 17:01:01 -070024 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +010025 // Built-in struct or nameless struct, do not touch it.
Zhenyao Moe740add2014-07-18 17:01:01 -070026 return;
27 }
28
Olli Etuahoae4dbf32017-12-08 20:49:00 +010029 ASSERT(userType->name() != nullptr);
30
Olli Etuaho97fa8552017-11-28 16:28:42 +020031 int uniqueId = userType->uniqueId().get();
Zhenyao Moe740add2014-07-18 17:01:01 -070032
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 Etuahoae4dbf32017-12-08 20:49:00 +010056 if (userType->name()->find(kPrefix) == 0)
Zhenyao Moe740add2014-07-18 17:01:01 -070057 {
58 // The name has already been regenerated.
59 return;
60 }
61 std::string id = Str(uniqueId);
Jamie Madilld7b1ab52016-12-12 14:42:19 -050062 TString tmp = kPrefix + TString(id.c_str());
Olli Etuahoae4dbf32017-12-08 20:49:00 +010063 tmp += "_" + *userType->name();
Zhenyao Moe740add2014-07-18 17:01:01 -070064 userType->setName(tmp);
65}
66
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010067bool RegenerateStructNames::visitBlock(Visit, TIntermBlock *block)
Zhenyao Moe740add2014-07-18 17:01:01 -070068{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010069 ++mScopeDepth;
70 TIntermSequence &sequence = *(block->getSequence());
71 for (TIntermNode *node : sequence)
Zhenyao Moe740add2014-07-18 17:01:01 -070072 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010073 node->traverse(this);
Zhenyao Moe740add2014-07-18 17:01:01 -070074 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010075 --mScopeDepth;
76 return false;
Zhenyao Moe740add2014-07-18 17:01:01 -070077}
Jamie Madill45bcc782016-11-07 13:58:48 -050078
79} // namespace sh