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 | |
Kai Ninomiya | d1bed17 | 2017-04-12 17:35:54 -0700 | [diff] [blame] | 85 | // Workaround for http://crbug.com/709317 |
| 86 | // This loop is reversed to initialize elements in increasing |
| 87 | // order [0 1 2 ...]. Otherwise, they're initialized in |
| 88 | // decreasing order [... 2 1 0], due to |
| 89 | // `sequence->insert(sequence->begin(), ...)` below. |
| 90 | for (unsigned int i = var.arraySize; i > 0; --i) |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 91 | { |
Kai Ninomiya | d1bed17 | 2017-04-12 17:35:54 -0700 | [diff] [blame] | 92 | unsigned int index = i - 1; |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 93 | TIntermSymbol *arraySymbol = new TIntermSymbol(0, name, arrayType); |
Olli Etuaho | 3272a6d | 2016-08-29 17:54:50 +0300 | [diff] [blame] | 94 | TIntermBinary *element = new TIntermBinary(EOpIndexDirect, arraySymbol, |
Kai Ninomiya | d1bed17 | 2017-04-12 17:35:54 -0700 | [diff] [blame] | 95 | TIntermTyped::CreateIndexNode(index)); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 96 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 97 | TIntermTyped *zero = TIntermTyped::CreateZero(elementType); |
| 98 | TIntermBinary *assignment = new TIntermBinary(EOpAssign, element, zero); |
| 99 | |
| 100 | sequence->insert(sequence->begin(), assignment); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 103 | else if (var.isStruct()) |
| 104 | { |
| 105 | TVariable *structInfo = reinterpret_cast<TVariable *>(mSymbolTable.findGlobal(name)); |
| 106 | ASSERT(structInfo); |
| 107 | |
| 108 | TIntermSymbol *symbol = new TIntermSymbol(0, name, structInfo->getType()); |
| 109 | TIntermTyped *zero = TIntermTyped::CreateZero(structInfo->getType()); |
| 110 | |
| 111 | TIntermBinary *assign = new TIntermBinary(EOpAssign, symbol, zero); |
| 112 | sequence->insert(sequence->begin(), assign); |
| 113 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 114 | else |
| 115 | { |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 116 | TType type = sh::GetShaderVariableBasicType(var); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 117 | TIntermSymbol *symbol = new TIntermSymbol(0, name, type); |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 118 | TIntermTyped *zero = TIntermTyped::CreateZero(type); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 119 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 120 | TIntermBinary *assign = new TIntermBinary(EOpAssign, symbol, zero); |
| 121 | sequence->insert(sequence->begin(), assign); |
| 122 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 126 | } // namespace anonymous |
| 127 | |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 128 | void InitializeVariables(TIntermNode *root, |
| 129 | const InitVariableList &vars, |
| 130 | const TSymbolTable &symbolTable) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 131 | { |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 132 | VariableInitializer initializer(vars, symbolTable); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 133 | root->traverse(&initializer); |
| 134 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 135 | |
| 136 | } // namespace sh |