blob: 1a7d8aaf8e096ab6c1e24d95d016b343824dd215 [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
10void RegenerateStructNames::visitSymbol(TIntermSymbol *symbol)
11{
12 ASSERT(symbol);
13 TType *type = symbol->getTypePointer();
14 ASSERT(type);
15 TStructure *userType = type->getStruct();
16 if (!userType)
17 return;
18
19 if (mSymbolTable.findBuiltIn(userType->name(), mShaderVersion))
20 {
21 // Built-in struct, do not touch it.
22 return;
23 }
24
25 int uniqueId = userType->uniqueId();
26
27 ASSERT(mScopeDepth > 0);
28 if (mScopeDepth == 1)
29 {
30 // If a struct is defined at global scope, we don't map its name.
31 // This is because at global level, the struct might be used to
32 // declare a uniform, so the same name needs to stay the same for
33 // vertex/fragment shaders. However, our mapping uses internal ID,
34 // which will be different for the same struct in vertex/fragment
35 // shaders.
36 // This is OK because names for any structs defined in other scopes
37 // will begin with "_webgl", which is reserved. So there will be
38 // no conflicts among unmapped struct names from global scope and
39 // mapped struct names from other scopes.
40 // However, we need to keep track of these global structs, so if a
41 // variable is used in a local scope, we don't try to modify the
42 // struct name through that variable.
43 mDeclaredGlobalStructs.insert(uniqueId);
44 return;
45 }
46 if (mDeclaredGlobalStructs.count(uniqueId) > 0)
47 return;
48 // Map {name} to _webgl_struct_{uniqueId}_{name}.
49 const char kPrefix[] = "_webgl_struct_";
50 if (userType->name().find(kPrefix) == 0)
51 {
52 // The name has already been regenerated.
53 return;
54 }
55 std::string id = Str(uniqueId);
56 TString tmp = kPrefix + TString(id.c_str());
57 tmp += "_" + userType->name();
58 userType->setName(tmp);
59}
60
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010061bool RegenerateStructNames::visitBlock(Visit, TIntermBlock *block)
Zhenyao Moe740add2014-07-18 17:01:01 -070062{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010063 ++mScopeDepth;
64 TIntermSequence &sequence = *(block->getSequence());
65 for (TIntermNode *node : sequence)
Zhenyao Moe740add2014-07-18 17:01:01 -070066 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010067 node->traverse(this);
Zhenyao Moe740add2014-07-18 17:01:01 -070068 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +010069 --mScopeDepth;
70 return false;
Zhenyao Moe740add2014-07-18 17:01:01 -070071}