Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2013 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 | |
| 7 | #include "compiler/translator/InitializeVariables.h" |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 8 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 9 | #include "angle_gl.h" |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 10 | #include "common/debug.h" |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 11 | #include "compiler/translator/IntermNode.h" |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame^] | 12 | #include "compiler/translator/SymbolTable.h" |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 13 | #include "compiler/translator/util.h" |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 14 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 15 | namespace sh |
| 16 | { |
| 17 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 18 | namespace |
| 19 | { |
| 20 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 21 | class VariableInitializer : public TIntermTraverser |
| 22 | { |
| 23 | public: |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame^] | 24 | VariableInitializer(const InitVariableList &vars, const TSymbolTable &symbolTable) |
| 25 | : TIntermTraverser(true, false, false), |
| 26 | mVariables(vars), |
| 27 | mSymbolTable(symbolTable), |
| 28 | mCodeInserted(false) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 29 | { |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame^] | 30 | ASSERT(mSymbolTable.atGlobalLevel()); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 31 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 32 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 33 | protected: |
| 34 | bool visitBinary(Visit, TIntermBinary *node) override { return false; } |
| 35 | bool visitUnary(Visit, TIntermUnary *node) override { return false; } |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 36 | bool visitIfElse(Visit, TIntermIfElse *node) override { return false; } |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 37 | bool visitLoop(Visit, TIntermLoop *node) override { return false; } |
| 38 | bool visitBranch(Visit, TIntermBranch *node) override { return false; } |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 39 | bool visitAggregate(Visit, TIntermAggregate *node) override { return false; } |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 40 | |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 41 | bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override; |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 42 | |
| 43 | private: |
| 44 | void insertInitCode(TIntermSequence *sequence); |
| 45 | |
| 46 | const InitVariableList &mVariables; |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame^] | 47 | const TSymbolTable &mSymbolTable; |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 48 | bool mCodeInserted; |
| 49 | }; |
| 50 | |
| 51 | // VariableInitializer implementation. |
| 52 | |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 53 | bool VariableInitializer::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 54 | { |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 55 | // Function definition. |
| 56 | ASSERT(visit == PreVisit); |
| 57 | if (node->getFunctionSymbolInfo()->isMain()) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 58 | { |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 59 | TIntermBlock *body = node->getBody(); |
| 60 | insertInitCode(body->getSequence()); |
| 61 | mCodeInserted = true; |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 62 | } |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 63 | return false; |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 66 | void VariableInitializer::insertInitCode(TIntermSequence *sequence) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 67 | { |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 68 | for (const auto &var : mVariables) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 69 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 70 | TString name = TString(var.name.c_str()); |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 71 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 72 | if (var.isArray()) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 73 | { |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame^] | 74 | // Assign the array elements one by one to keep the AST compatible with ESSL 1.00 which |
| 75 | // doesn't have array assignment. |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 76 | size_t pos = name.find_last_of('['); |
| 77 | if (pos != TString::npos) |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 78 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 79 | name = name.substr(0, pos); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 80 | } |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame^] | 81 | TType elementType = sh::GetShaderVariableBasicType(var); |
| 82 | TType arrayType = elementType; |
| 83 | arrayType.setArraySize(var.elementCount()); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 84 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 85 | for (unsigned int i = 0; i < var.arraySize; ++i) |
| 86 | { |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame^] | 87 | TIntermSymbol *arraySymbol = new TIntermSymbol(0, name, arrayType); |
Olli Etuaho | 3272a6d | 2016-08-29 17:54:50 +0300 | [diff] [blame] | 88 | TIntermBinary *element = new TIntermBinary(EOpIndexDirect, arraySymbol, |
| 89 | TIntermTyped::CreateIndexNode(i)); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 90 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 91 | TIntermTyped *zero = TIntermTyped::CreateZero(elementType); |
| 92 | TIntermBinary *assignment = new TIntermBinary(EOpAssign, element, zero); |
| 93 | |
| 94 | sequence->insert(sequence->begin(), assignment); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame^] | 97 | else if (var.isStruct()) |
| 98 | { |
| 99 | TVariable *structInfo = reinterpret_cast<TVariable *>(mSymbolTable.findGlobal(name)); |
| 100 | ASSERT(structInfo); |
| 101 | |
| 102 | TIntermSymbol *symbol = new TIntermSymbol(0, name, structInfo->getType()); |
| 103 | TIntermTyped *zero = TIntermTyped::CreateZero(structInfo->getType()); |
| 104 | |
| 105 | TIntermBinary *assign = new TIntermBinary(EOpAssign, symbol, zero); |
| 106 | sequence->insert(sequence->begin(), assign); |
| 107 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 108 | else |
| 109 | { |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame^] | 110 | TType type = sh::GetShaderVariableBasicType(var); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 111 | TIntermSymbol *symbol = new TIntermSymbol(0, name, type); |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 112 | TIntermTyped *zero = TIntermTyped::CreateZero(type); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 113 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 114 | TIntermBinary *assign = new TIntermBinary(EOpAssign, symbol, zero); |
| 115 | sequence->insert(sequence->begin(), assign); |
| 116 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 120 | } // namespace anonymous |
| 121 | |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame^] | 122 | void InitializeVariables(TIntermNode *root, |
| 123 | const InitVariableList &vars, |
| 124 | const TSymbolTable &symbolTable) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 125 | { |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame^] | 126 | VariableInitializer initializer(vars, symbolTable); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 127 | root->traverse(&initializer); |
| 128 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 129 | |
| 130 | } // namespace sh |