Hash :
c5c8a25a
Author :
Date :
2021-08-03T14:40:33
Revert "Translator: Clean up type cloning" This reverts commit f016c4352f5203c10511df078b1ed5359afc1b35. Reason for revert: Suspect for crbug.com/1236060 Original change's description: > Translator: Clean up type cloning > > TType has a constructor that aids cloning + helpers to convert between > types. A number of places where a type is constructed from the > information gathered from another type is changed to clone the type and > then use one of these helpers. > > This clean up is part of an ongoing work to improve precision handling. > This change removes many references to TType::getPrecision(). > > Bug: angleproject:4889 > Change-Id: Ib85659ab5363b56ad298f8648fca856edc1ebf8b > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3063944 > Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> > Reviewed-by: Jamie Madill <jmadill@chromium.org> > Reviewed-by: Tim Van Patten <timvp@google.com> Bug: angleproject:4889 Change-Id: I9b5ecacc410b41be382232292ceae2f7ea7906c7 No-Presubmit: true No-Tree-Checks: true No-Try: true Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3064393 Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> Commit-Queue: Austin Eng <enga@chromium.org>
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
//
// Copyright 2002 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.
//
// Scalarize vector and matrix constructor args, so that vectors built from components don't have
// matrix arguments, and matrices built from components don't have vector arguments. This avoids
// driver bugs around vector and matrix constructors.
//
#include "compiler/translator/tree_ops/ScalarizeVecAndMatConstructorArgs.h"
#include "common/debug.h"
#include <algorithm>
#include "angle_gl.h"
#include "common/angleutils.h"
#include "compiler/translator/Compiler.h"
#include "compiler/translator/tree_util/IntermNodePatternMatcher.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
namespace sh
{
namespace
{
TIntermBinary *ConstructVectorIndexBinaryNode(TIntermSymbol *symbolNode, int index)
{
return new TIntermBinary(EOpIndexDirect, symbolNode, CreateIndexNode(index));
}
TIntermBinary *ConstructMatrixIndexBinaryNode(TIntermSymbol *symbolNode, int colIndex, int rowIndex)
{
TIntermBinary *colVectorNode = ConstructVectorIndexBinaryNode(symbolNode, colIndex);
return new TIntermBinary(EOpIndexDirect, colVectorNode, CreateIndexNode(rowIndex));
}
class ScalarizeArgsTraverser : public TIntermTraverser
{
public:
ScalarizeArgsTraverser(sh::GLenum shaderType,
bool fragmentPrecisionHigh,
TSymbolTable *symbolTable)
: TIntermTraverser(true, false, false, symbolTable),
mShaderType(shaderType),
mFragmentPrecisionHigh(fragmentPrecisionHigh),
mNodesToScalarize(IntermNodePatternMatcher::kScalarizedVecOrMatConstructor)
{}
protected:
bool visitAggregate(Visit visit, TIntermAggregate *node) override;
bool visitBlock(Visit visit, TIntermBlock *node) override;
private:
void scalarizeArgs(TIntermAggregate *aggregate, bool scalarizeVector, bool scalarizeMatrix);
// If we have the following code:
// mat4 m(0);
// vec4 v(1, m);
// We will rewrite to:
// mat4 m(0);
// mat4 s0 = m;
// vec4 v(1, s0[0][0], s0[0][1], s0[0][2]);
// This function is to create nodes for "mat4 s0 = m;" and insert it to the code sequence. This
// way the possible side effects of the constructor argument will only be evaluated once.
TVariable *createTempVariable(TIntermTyped *original);
std::vector<TIntermSequence> mBlockStack;
sh::GLenum mShaderType;
bool mFragmentPrecisionHigh;
IntermNodePatternMatcher mNodesToScalarize;
};
bool ScalarizeArgsTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
{
ASSERT(visit == PreVisit);
if (mNodesToScalarize.match(node, getParentNode()))
{
if (node->getType().isVector())
{
scalarizeArgs(node, false, true);
}
else
{
ASSERT(node->getType().isMatrix());
scalarizeArgs(node, true, false);
}
}
return true;
}
bool ScalarizeArgsTraverser::visitBlock(Visit visit, TIntermBlock *node)
{
mBlockStack.push_back(TIntermSequence());
{
for (TIntermNode *child : *node->getSequence())
{
ASSERT(child != nullptr);
child->traverse(this);
mBlockStack.back().push_back(child);
}
}
if (mBlockStack.back().size() > node->getSequence()->size())
{
node->getSequence()->clear();
*(node->getSequence()) = mBlockStack.back();
}
mBlockStack.pop_back();
return false;
}
void ScalarizeArgsTraverser::scalarizeArgs(TIntermAggregate *aggregate,
bool scalarizeVector,
bool scalarizeMatrix)
{
ASSERT(aggregate);
ASSERT(!aggregate->isArray());
int size = static_cast<int>(aggregate->getType().getObjectSize());
TIntermSequence *sequence = aggregate->getSequence();
TIntermSequence originalArgs(*sequence);
sequence->clear();
for (TIntermNode *originalArgNode : originalArgs)
{
ASSERT(size > 0);
TIntermTyped *originalArg = originalArgNode->getAsTyped();
ASSERT(originalArg);
TVariable *argVariable = createTempVariable(originalArg);
if (originalArg->isScalar())
{
sequence->push_back(CreateTempSymbolNode(argVariable));
size--;
}
else if (originalArg->isVector())
{
if (scalarizeVector)
{
int repeat = std::min(size, originalArg->getNominalSize());
size -= repeat;
for (int index = 0; index < repeat; ++index)
{
TIntermSymbol *symbolNode = CreateTempSymbolNode(argVariable);
TIntermBinary *newNode = ConstructVectorIndexBinaryNode(symbolNode, index);
sequence->push_back(newNode);
}
}
else
{
TIntermSymbol *symbolNode = CreateTempSymbolNode(argVariable);
sequence->push_back(symbolNode);
size -= originalArg->getNominalSize();
}
}
else
{
ASSERT(originalArg->isMatrix());
if (scalarizeMatrix)
{
int colIndex = 0, rowIndex = 0;
int repeat = std::min(size, originalArg->getCols() * originalArg->getRows());
size -= repeat;
while (repeat > 0)
{
TIntermSymbol *symbolNode = CreateTempSymbolNode(argVariable);
TIntermBinary *newNode =
ConstructMatrixIndexBinaryNode(symbolNode, colIndex, rowIndex);
sequence->push_back(newNode);
rowIndex++;
if (rowIndex >= originalArg->getRows())
{
rowIndex = 0;
colIndex++;
}
repeat--;
}
}
else
{
TIntermSymbol *symbolNode = CreateTempSymbolNode(argVariable);
sequence->push_back(symbolNode);
size -= originalArg->getCols() * originalArg->getRows();
}
}
}
}
TVariable *ScalarizeArgsTraverser::createTempVariable(TIntermTyped *original)
{
ASSERT(original);
TType *type = new TType(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);
}
TVariable *variable = CreateTempVariable(mSymbolTable, type);
ASSERT(mBlockStack.size() > 0);
TIntermSequence &sequence = mBlockStack.back();
TIntermDeclaration *declaration = CreateTempInitDeclarationNode(variable, original);
sequence.push_back(declaration);
return variable;
}
} // namespace
bool ScalarizeVecAndMatConstructorArgs(TCompiler *compiler,
TIntermBlock *root,
sh::GLenum shaderType,
bool fragmentPrecisionHigh,
TSymbolTable *symbolTable)
{
ScalarizeArgsTraverser scalarizer(shaderType, fragmentPrecisionHigh, symbolTable);
root->traverse(&scalarizer);
return compiler->validateAST(root);
}
} // namespace sh