blob: df0c2f9e29b03e25e8f7fe57ea450bde0499cd70 [file] [log] [blame]
Qin Jiajia7835b522016-10-08 11:20:17 +08001//
2// Copyright 2016 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// UseInterfaceBlockFields.cpp: insert statements to reference all members in InterfaceBlock list at
8// the beginning of main. This is to work around a Mac driver that treats unused standard/shared
9// uniform blocks as inactive.
10
11#include "compiler/translator/UseInterfaceBlockFields.h"
12
Olli Etuaho9cbc07c2017-05-10 18:22:01 +030013#include "compiler/translator/FindMain.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080014#include "compiler/translator/IntermNode.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Zhenyao Mod7490962016-11-09 15:49:51 -080016#include "compiler/translator/SymbolTable.h"
Qin Jiajia7835b522016-10-08 11:20:17 +080017#include "compiler/translator/util.h"
18
19namespace sh
20{
21
22namespace
23{
24
Olli Etuaho283c2192017-10-23 17:11:50 +030025void AddNodeUseStatements(TIntermTyped *node, TIntermSequence *sequence)
26{
27 if (node->isArray())
28 {
29 for (unsigned int i = 0u; i < node->getOutermostArraySize(); ++i)
30 {
31 TIntermBinary *element =
32 new TIntermBinary(EOpIndexDirect, node->deepCopy(), CreateIndexNode(i));
33 AddNodeUseStatements(element, sequence);
34 }
35 }
36 else
37 {
38 sequence->insert(sequence->begin(), node);
39 }
40}
41
Olli Etuaho9cbc07c2017-05-10 18:22:01 +030042void AddFieldUseStatements(const ShaderVariable &var,
43 TIntermSequence *sequence,
44 const TSymbolTable &symbolTable)
Qin Jiajia7835b522016-10-08 11:20:17 +080045{
Olli Etuahofbb1c792018-01-19 16:26:59 +020046 ASSERT(var.name.find_last_of('[') == TString::npos);
47 TIntermSymbol *symbol = ReferenceGlobalVariable(ImmutableString(var.name), symbolTable);
Olli Etuaho283c2192017-10-23 17:11:50 +030048 AddNodeUseStatements(symbol, sequence);
Qin Jiajia7835b522016-10-08 11:20:17 +080049}
50
Olli Etuaho4cafb862017-07-05 14:05:06 +030051void InsertUseCode(const InterfaceBlock &block, TIntermTyped *blockNode, TIntermSequence *sequence)
52{
53 for (unsigned int i = 0; i < block.fields.size(); ++i)
54 {
55 TIntermBinary *element = new TIntermBinary(EOpIndexDirectInterfaceBlock,
56 blockNode->deepCopy(), CreateIndexNode(i));
57 sequence->insert(sequence->begin(), element);
58 }
59}
60
Olli Etuaho9cbc07c2017-05-10 18:22:01 +030061void InsertUseCode(TIntermSequence *sequence,
62 const InterfaceBlockList &blocks,
63 const TSymbolTable &symbolTable)
Qin Jiajia7835b522016-10-08 11:20:17 +080064{
Olli Etuaho9cbc07c2017-05-10 18:22:01 +030065 for (const auto &block : blocks)
Qin Jiajia7835b522016-10-08 11:20:17 +080066 {
67 if (block.instanceName.empty())
68 {
69 for (const auto &var : block.fields)
70 {
Olli Etuaho9cbc07c2017-05-10 18:22:01 +030071 AddFieldUseStatements(var, sequence, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +080072 }
73 }
Olli Etuaho4cafb862017-07-05 14:05:06 +030074 else if (block.arraySize > 0u)
Qin Jiajia7835b522016-10-08 11:20:17 +080075 {
Olli Etuahofbb1c792018-01-19 16:26:59 +020076 TIntermSymbol *arraySymbol =
77 ReferenceGlobalVariable(ImmutableString(block.instanceName), symbolTable);
Olli Etuaho4cafb862017-07-05 14:05:06 +030078 for (unsigned int i = 0u; i < block.arraySize; ++i)
Qin Jiajia7835b522016-10-08 11:20:17 +080079 {
Olli Etuaho4cafb862017-07-05 14:05:06 +030080 TIntermBinary *elementSymbol =
81 new TIntermBinary(EOpIndexDirect, arraySymbol->deepCopy(), CreateIndexNode(i));
82 InsertUseCode(block, elementSymbol, sequence);
Qin Jiajia7835b522016-10-08 11:20:17 +080083 }
84 }
85 else
86 {
Olli Etuahofbb1c792018-01-19 16:26:59 +020087 TIntermSymbol *blockSymbol =
88 ReferenceGlobalVariable(ImmutableString(block.instanceName), symbolTable);
Olli Etuaho4cafb862017-07-05 14:05:06 +030089 InsertUseCode(block, blockSymbol, sequence);
Qin Jiajia7835b522016-10-08 11:20:17 +080090 }
91 }
92}
93
94} // namespace anonymous
95
Olli Etuaho9cbc07c2017-05-10 18:22:01 +030096void UseInterfaceBlockFields(TIntermBlock *root,
Zhenyao Mod7490962016-11-09 15:49:51 -080097 const InterfaceBlockList &blocks,
98 const TSymbolTable &symbolTable)
Qin Jiajia7835b522016-10-08 11:20:17 +080099{
Martin Radevb50ccd32017-07-06 17:09:58 +0300100 TIntermBlock *mainBody = FindMainBody(root);
Olli Etuaho9cbc07c2017-05-10 18:22:01 +0300101 InsertUseCode(mainBody->getSequence(), blocks, symbolTable);
Qin Jiajia7835b522016-10-08 11:20:17 +0800102}
103
104} // namespace sh