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