Hash :
3272a6d3
Author :
Date :
2016-08-29T17:54:50
Promote and fold indexing nodes similarly to other binary ops Indexing nodes now get their type set in TIntermBinary::promote, same as math and logic ops. They are also constant folded through TIntermBinary::fold() instead of having special functions for constant folding them in ParseContext. Index nodes for struct and interface block member access now always have integer type, instead of sometimes having the type of the field they were used to access. Usage of TIntermBinary constructor is cleaned up so only the constructor that takes in left and right operands is used. The type of TIntermBinary nodes is always determined automatically. Together these changes make the code considerably cleaner. Note that the code for constant folding for array indexing is actually never hit because constant folding array constructors is still intentionally disabled in the code. BUG=angleproject:1490 TEST=angle_unittests Change-Id: Ifcec45257476cdb0d495c7d72e3cf2f83388e8c5 Reviewed-on: https://chromium-review.googlesource.com/377961 Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
//
// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "common/debug.h"
#include "compiler/translator/ScalarizeVecAndMatConstructorArgs.h"
#include <algorithm>
#include "angle_gl.h"
#include "common/angleutils.h"
namespace
{
bool ContainsMatrixNode(const TIntermSequence &sequence)
{
for (size_t ii = 0; ii < sequence.size(); ++ii)
{
TIntermTyped *node = sequence[ii]->getAsTyped();
if (node && node->isMatrix())
return true;
}
return false;
}
bool ContainsVectorNode(const TIntermSequence &sequence)
{
for (size_t ii = 0; ii < sequence.size(); ++ii)
{
TIntermTyped *node = sequence[ii]->getAsTyped();
if (node && node->isVector())
return true;
}
return false;
}
TIntermBinary *ConstructVectorIndexBinaryNode(TIntermSymbol *symbolNode, int index)
{
return new TIntermBinary(EOpIndexDirect, symbolNode, TIntermTyped::CreateIndexNode(index));
}
TIntermBinary *ConstructMatrixIndexBinaryNode(
TIntermSymbol *symbolNode, int colIndex, int rowIndex)
{
TIntermBinary *colVectorNode =
ConstructVectorIndexBinaryNode(symbolNode, colIndex);
return new TIntermBinary(EOpIndexDirect, colVectorNode,
TIntermTyped::CreateIndexNode(rowIndex));
}
} // namespace anonymous
bool ScalarizeVecAndMatConstructorArgs::visitAggregate(Visit visit, TIntermAggregate *node)
{
if (visit == PreVisit)
{
switch (node->getOp())
{
case EOpSequence:
mSequenceStack.push_back(TIntermSequence());
{
for (TIntermSequence::const_iterator iter = node->getSequence()->begin();
iter != node->getSequence()->end(); ++iter)
{
TIntermNode *child = *iter;
ASSERT(child != NULL);
child->traverse(this);
mSequenceStack.back().push_back(child);
}
}
if (mSequenceStack.back().size() > node->getSequence()->size())
{
node->getSequence()->clear();
*(node->getSequence()) = mSequenceStack.back();
}
mSequenceStack.pop_back();
return false;
case EOpConstructVec2:
case EOpConstructVec3:
case EOpConstructVec4:
case EOpConstructBVec2:
case EOpConstructBVec3:
case EOpConstructBVec4:
case EOpConstructIVec2:
case EOpConstructIVec3:
case EOpConstructIVec4:
if (ContainsMatrixNode(*(node->getSequence())))
scalarizeArgs(node, false, true);
break;
case EOpConstructMat2:
case EOpConstructMat2x3:
case EOpConstructMat2x4:
case EOpConstructMat3x2:
case EOpConstructMat3:
case EOpConstructMat3x4:
case EOpConstructMat4x2:
case EOpConstructMat4x3:
case EOpConstructMat4:
if (ContainsVectorNode(*(node->getSequence())))
scalarizeArgs(node, true, false);
break;
default:
break;
}
}
return true;
}
void ScalarizeVecAndMatConstructorArgs::scalarizeArgs(
TIntermAggregate *aggregate, bool scalarizeVector, bool scalarizeMatrix)
{
ASSERT(aggregate);
int size = 0;
switch (aggregate->getOp())
{
case EOpConstructVec2:
case EOpConstructBVec2:
case EOpConstructIVec2:
size = 2;
break;
case EOpConstructVec3:
case EOpConstructBVec3:
case EOpConstructIVec3:
size = 3;
break;
case EOpConstructVec4:
case EOpConstructBVec4:
case EOpConstructIVec4:
case EOpConstructMat2:
size = 4;
break;
case EOpConstructMat2x3:
case EOpConstructMat3x2:
size = 6;
break;
case EOpConstructMat2x4:
case EOpConstructMat4x2:
size = 8;
break;
case EOpConstructMat3:
size = 9;
break;
case EOpConstructMat3x4:
case EOpConstructMat4x3:
size = 12;
break;
case EOpConstructMat4:
size = 16;
break;
default:
break;
}
TIntermSequence *sequence = aggregate->getSequence();
TIntermSequence original(*sequence);
sequence->clear();
for (size_t ii = 0; ii < original.size(); ++ii)
{
ASSERT(size > 0);
TIntermTyped *node = original[ii]->getAsTyped();
ASSERT(node);
TString varName = createTempVariable(node);
if (node->isScalar())
{
TIntermSymbol *symbolNode =
new TIntermSymbol(-1, varName, node->getType());
sequence->push_back(symbolNode);
size--;
}
else if (node->isVector())
{
if (scalarizeVector)
{
int repeat = std::min(size, node->getNominalSize());
size -= repeat;
for (int index = 0; index < repeat; ++index)
{
TIntermSymbol *symbolNode =
new TIntermSymbol(-1, varName, node->getType());
TIntermBinary *newNode = ConstructVectorIndexBinaryNode(
symbolNode, index);
sequence->push_back(newNode);
}
}
else
{
TIntermSymbol *symbolNode =
new TIntermSymbol(-1, varName, node->getType());
sequence->push_back(symbolNode);
size -= node->getNominalSize();
}
}
else
{
ASSERT(node->isMatrix());
if (scalarizeMatrix)
{
int colIndex = 0, rowIndex = 0;
int repeat = std::min(size, node->getCols() * node->getRows());
size -= repeat;
while (repeat > 0)
{
TIntermSymbol *symbolNode =
new TIntermSymbol(-1, varName, node->getType());
TIntermBinary *newNode = ConstructMatrixIndexBinaryNode(
symbolNode, colIndex, rowIndex);
sequence->push_back(newNode);
rowIndex++;
if (rowIndex >= node->getRows())
{
rowIndex = 0;
colIndex++;
}
repeat--;
}
}
else
{
TIntermSymbol *symbolNode =
new TIntermSymbol(-1, varName, node->getType());
sequence->push_back(symbolNode);
size -= node->getCols() * node->getRows();
}
}
}
}
TString ScalarizeVecAndMatConstructorArgs::createTempVariable(TIntermTyped *original)
{
TString tempVarName = "_webgl_tmp_";
if (original->isScalar())
{
tempVarName += "scalar_";
}
else if (original->isVector())
{
tempVarName += "vec_";
}
else
{
ASSERT(original->isMatrix());
tempVarName += "mat_";
}
tempVarName += Str(mTempVarCount).c_str();
mTempVarCount++;
ASSERT(original);
TType type = original->getType();
type.setQualifier(EvqTemporary);
if (mShaderType == GL_FRAGMENT_SHADER &&
type.getBasicType() == EbtFloat &&
type.getPrecision() == EbpUndefined)
{
// We use the highest available precision for the temporary variable
// to avoid computing the actual precision using the rules defined
// in GLSL ES 1.0 Section 4.5.2.
type.setPrecision(mFragmentPrecisionHigh ? EbpHigh : EbpMedium);
}
TIntermSymbol *symbolNode = new TIntermSymbol(-1, tempVarName, type);
TIntermBinary *init = new TIntermBinary(EOpInitialize, symbolNode, original);
TIntermAggregate *decl = new TIntermAggregate(EOpDeclaration);
decl->getSequence()->push_back(init);
ASSERT(mSequenceStack.size() > 0);
TIntermSequence &sequence = mSequenceStack.back();
sequence.push_back(decl);
return tempVarName;
}