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; } |
| 31 | |
| 32 | bool visitAggregate(Visit visit, TIntermAggregate *node) override; |
| 33 | |
| 34 | private: |
| 35 | void insertInitCode(TIntermSequence *sequence); |
| 36 | |
| 37 | const InitVariableList &mVariables; |
| 38 | bool mCodeInserted; |
| 39 | }; |
| 40 | |
| 41 | // VariableInitializer implementation. |
| 42 | |
| 43 | bool VariableInitializer::visitAggregate(Visit visit, TIntermAggregate *node) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 44 | { |
| 45 | bool visitChildren = !mCodeInserted; |
| 46 | switch (node->getOp()) |
| 47 | { |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 48 | case EOpFunction: |
| 49 | { |
| 50 | // Function definition. |
| 51 | ASSERT(visit == PreVisit); |
| 52 | if (node->getName() == "main(") |
| 53 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 54 | TIntermSequence *sequence = node->getSequence(); |
Olli Etuaho | f51fdd2 | 2016-10-03 10:03:40 +0100 | [diff] [blame] | 55 | ASSERT(sequence->size() == 2); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame^] | 56 | TIntermBlock *body = (*sequence)[1]->getAsBlock(); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 57 | ASSERT(body); |
| 58 | insertInitCode(body->getSequence()); |
| 59 | mCodeInserted = true; |
| 60 | } |
| 61 | break; |
| 62 | } |
| 63 | default: |
| 64 | visitChildren = false; |
| 65 | break; |
| 66 | } |
| 67 | return visitChildren; |
| 68 | } |
| 69 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 70 | void VariableInitializer::insertInitCode(TIntermSequence *sequence) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 71 | { |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 72 | for (const auto &var : mVariables) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 73 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 74 | TString name = TString(var.name.c_str()); |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 75 | TType type = sh::GetShaderVariableType(var); |
| 76 | |
| 77 | // Assign the array elements one by one to keep the AST compatible with ESSL 1.00 which |
| 78 | // doesn't have array assignment. |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 79 | if (var.isArray()) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 80 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 81 | size_t pos = name.find_last_of('['); |
| 82 | if (pos != TString::npos) |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 83 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 84 | name = name.substr(0, pos); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 85 | } |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 86 | TType elementType = type; |
| 87 | elementType.clearArrayness(); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 88 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 89 | for (unsigned int i = 0; i < var.arraySize; ++i) |
| 90 | { |
Olli Etuaho | 3272a6d | 2016-08-29 17:54:50 +0300 | [diff] [blame] | 91 | TIntermSymbol *arraySymbol = new TIntermSymbol(0, name, type); |
| 92 | TIntermBinary *element = new TIntermBinary(EOpIndexDirect, arraySymbol, |
| 93 | TIntermTyped::CreateIndexNode(i)); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 94 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 95 | TIntermTyped *zero = TIntermTyped::CreateZero(elementType); |
| 96 | TIntermBinary *assignment = new TIntermBinary(EOpAssign, element, zero); |
| 97 | |
| 98 | sequence->insert(sequence->begin(), assignment); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 101 | else |
| 102 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 103 | TIntermSymbol *symbol = new TIntermSymbol(0, name, type); |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 104 | TIntermTyped *zero = TIntermTyped::CreateZero(type); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 105 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 106 | TIntermBinary *assign = new TIntermBinary(EOpAssign, symbol, zero); |
| 107 | sequence->insert(sequence->begin(), assign); |
| 108 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 112 | } // namespace anonymous |
| 113 | |
| 114 | void InitializeVariables(TIntermNode *root, const InitVariableList &vars) |
| 115 | { |
| 116 | VariableInitializer initializer(vars); |
| 117 | root->traverse(&initializer); |
| 118 | } |