blob: a01d79abe63255e425ea1f7d3e1782fdeb2f93a6 [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
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 Etuaho6d40bbd2016-09-30 13:49:38 +010064bool RegenerateStructNames::visitBlock(Visit, TIntermBlock *block)
Zhenyao Moe740add2014-07-18 17:01:01 -070065{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010066 ++mScopeDepth;
67 TIntermSequence &sequence = *(block->getSequence());
68 for (TIntermNode *node : sequence)
Zhenyao Moe740add2014-07-18 17:01:01 -070069 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010070 node->traverse(this);
Zhenyao Moe740add2014-07-18 17:01:01 -070071 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010072 --mScopeDepth;
73 return false;
Zhenyao Moe740add2014-07-18 17:01:01 -070074}
Jamie Madill45bcc782016-11-07 13:58:48 -050075
76} // namespace sh