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" |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame^] | 11 | #include "compiler/translator/FindMain.h" |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 12 | #include "compiler/translator/IntermNode.h" |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 13 | #include "compiler/translator/SymbolTable.h" |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 14 | #include "compiler/translator/util.h" |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 15 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 16 | namespace sh |
| 17 | { |
| 18 | |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 19 | namespace |
| 20 | { |
| 21 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame^] | 22 | void InsertInitCode(TIntermSequence *sequence, |
| 23 | const InitVariableList &variables, |
| 24 | const TSymbolTable &symbolTable) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 25 | { |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame^] | 26 | for (const auto &var : variables) |
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 | TString name = TString(var.name.c_str()); |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 29 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 30 | if (var.isArray()) |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 31 | { |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 32 | // Assign the array elements one by one to keep the AST compatible with ESSL 1.00 which |
| 33 | // doesn't have array assignment. |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 34 | size_t pos = name.find_last_of('['); |
| 35 | if (pos != TString::npos) |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 36 | { |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 37 | name = name.substr(0, pos); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 38 | } |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 39 | TType elementType = sh::GetShaderVariableBasicType(var); |
| 40 | TType arrayType = elementType; |
| 41 | arrayType.setArraySize(var.elementCount()); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 42 | |
Kai Ninomiya | d1bed17 | 2017-04-12 17:35:54 -0700 | [diff] [blame] | 43 | // Workaround for http://crbug.com/709317 |
| 44 | // This loop is reversed to initialize elements in increasing |
| 45 | // order [0 1 2 ...]. Otherwise, they're initialized in |
| 46 | // decreasing order [... 2 1 0], due to |
| 47 | // `sequence->insert(sequence->begin(), ...)` below. |
| 48 | for (unsigned int i = var.arraySize; i > 0; --i) |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 49 | { |
Kai Ninomiya | d1bed17 | 2017-04-12 17:35:54 -0700 | [diff] [blame] | 50 | unsigned int index = i - 1; |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 51 | TIntermSymbol *arraySymbol = new TIntermSymbol(0, name, arrayType); |
Olli Etuaho | 3272a6d | 2016-08-29 17:54:50 +0300 | [diff] [blame] | 52 | TIntermBinary *element = new TIntermBinary(EOpIndexDirect, arraySymbol, |
Kai Ninomiya | d1bed17 | 2017-04-12 17:35:54 -0700 | [diff] [blame] | 53 | TIntermTyped::CreateIndexNode(index)); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 54 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 55 | TIntermTyped *zero = TIntermTyped::CreateZero(elementType); |
| 56 | TIntermBinary *assignment = new TIntermBinary(EOpAssign, element, zero); |
| 57 | |
| 58 | sequence->insert(sequence->begin(), assignment); |
Zhenyao Mo | f931268 | 2016-07-22 12:51:31 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 61 | else if (var.isStruct()) |
| 62 | { |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame^] | 63 | TVariable *structInfo = reinterpret_cast<TVariable *>(symbolTable.findGlobal(name)); |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 64 | ASSERT(structInfo); |
| 65 | |
| 66 | TIntermSymbol *symbol = new TIntermSymbol(0, name, structInfo->getType()); |
| 67 | TIntermTyped *zero = TIntermTyped::CreateZero(structInfo->getType()); |
| 68 | |
| 69 | TIntermBinary *assign = new TIntermBinary(EOpAssign, symbol, zero); |
| 70 | sequence->insert(sequence->begin(), assign); |
| 71 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 72 | else |
| 73 | { |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 74 | TType type = sh::GetShaderVariableBasicType(var); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 75 | TIntermSymbol *symbol = new TIntermSymbol(0, name, type); |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 76 | TIntermTyped *zero = TIntermTyped::CreateZero(type); |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 77 | |
Corentin Wallez | 509e456 | 2016-08-25 14:55:44 -0400 | [diff] [blame] | 78 | TIntermBinary *assign = new TIntermBinary(EOpAssign, symbol, zero); |
| 79 | sequence->insert(sequence->begin(), assign); |
| 80 | } |
Zhenyao Mo | 4a667fe | 2014-02-11 12:35:01 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 84 | } // namespace anonymous |
| 85 | |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame^] | 86 | void InitializeVariables(TIntermBlock *root, |
Zhenyao Mo | d749096 | 2016-11-09 15:49:51 -0800 | [diff] [blame] | 87 | const InitVariableList &vars, |
| 88 | const TSymbolTable &symbolTable) |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 89 | { |
Olli Etuaho | 9cbc07c | 2017-05-10 18:22:01 +0300 | [diff] [blame^] | 90 | TIntermFunctionDefinition *main = FindMain(root); |
| 91 | ASSERT(main != nullptr); |
| 92 | TIntermBlock *body = main->getBody(); |
| 93 | InsertInitCode(body->getSequence(), vars, symbolTable); |
Zhenyao Mo | 7211191 | 2016-07-20 17:45:56 -0700 | [diff] [blame] | 94 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 95 | |
| 96 | } // namespace sh |