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