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