Hash :
0f00fbae
Author :
Date :
2022-01-21T00:28:48
Translator: Make vec/matrix size getters unsigned TType stored type's primary and secondary sizes as `unsigned char`, but the getters returned `int`. This caused unnecessary casts when the size was passed from one TType to another, as well as comparisons with other unsigned numbers. This change specifies the type of these members as `uint8_t` and makes the getters return the same type. The call sites are accordingly adjusted to remove unnecessary casts, use the correct type in local variables, and add casts when passed to ostream::operator<<. Bug: angleproject:6755 Change-Id: Ia4d86bd4ccb5c1a2ae1e10a0085a5166c3a6bcf7 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3402850 Reviewed-by: Tim Van Patten <timvp@google.com> Commit-Queue: Shahbaz Youssefi <syoussefi@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 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
//
// Copyright 2020 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 "compiler/translator/tree_ops/ConvertUnsupportedConstructorsToFunctionCalls.h"
#include "compiler/translator/ImmutableString.h"
#include "compiler/translator/Symbol.h"
#include "compiler/translator/tree_util/FindFunction.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermRebuild.h"
using namespace sh;
namespace
{
void AppendMatrixElementArgument(TIntermSymbol *parameter,
int colIndex,
int rowIndex,
TIntermSequence *returnCtorArgs)
{
TIntermBinary *matColN =
new TIntermBinary(EOpIndexDirect, parameter->deepCopy(), CreateIndexNode(colIndex));
TIntermSwizzle *matElem = new TIntermSwizzle(matColN, {rowIndex});
returnCtorArgs->push_back(matElem);
}
// Adds the argument to sequence for a scalar constructor.
// Given scalar(scalarA) appends scalarA
// Given scalar(vecA) appends vecA.x
// Given scalar(matA) appends matA[0].x
void AppendScalarFromNonScalarArguments(TFunction &function, TIntermSequence *returnCtorArgs)
{
const TVariable *var = function.getParam(0);
TIntermSymbol *arg0 = new TIntermSymbol(var);
const TType &type = arg0->getType();
if (type.isScalar())
{
returnCtorArgs->push_back(arg0);
}
else if (type.isVector())
{
TIntermSwizzle *vecX = new TIntermSwizzle(arg0, {0});
returnCtorArgs->push_back(vecX);
}
else if (type.isMatrix())
{
AppendMatrixElementArgument(arg0, 0, 0, returnCtorArgs);
}
}
// Adds the arguments to sequence for a vector constructor from a scalar.
// Given vecN(scalarA) appends scalarA, scalarA, ... n times
void AppendVectorFromScalarArgument(const TType &type,
TFunction &function,
TIntermSequence *returnCtorArgs)
{
const uint8_t vectorSize = type.getNominalSize();
const TVariable *var = function.getParam(0);
TIntermSymbol *v = new TIntermSymbol(var);
for (uint8_t i = 0; i < vectorSize; ++i)
{
returnCtorArgs->push_back(v->deepCopy());
}
}
// Adds the arguments to sequence for a vector or matrix constructor from the available arguments
// applying arguments in order until the requested number of values have been extracted from the
// given arguments or until there are no more arguments.
void AppendValuesFromMultipleArguments(int numValuesNeeded,
TFunction &function,
TIntermSequence *returnCtorArgs)
{
size_t numParameters = function.getParamCount();
size_t paramIndex = 0;
uint8_t colIndex = 0;
uint8_t rowIndex = 0;
for (int i = 0; i < numValuesNeeded && paramIndex < numParameters; ++i)
{
const TVariable *p = function.getParam(paramIndex);
TIntermSymbol *parameter = new TIntermSymbol(p);
if (parameter->isScalar())
{
returnCtorArgs->push_back(parameter);
++paramIndex;
}
else if (parameter->isVector())
{
TIntermSwizzle *vecS = new TIntermSwizzle(parameter->deepCopy(), {rowIndex++});
returnCtorArgs->push_back(vecS);
if (rowIndex == parameter->getNominalSize())
{
++paramIndex;
rowIndex = 0;
}
}
else if (parameter->isMatrix())
{
AppendMatrixElementArgument(parameter, colIndex, rowIndex++, returnCtorArgs);
if (rowIndex == parameter->getSecondarySize())
{
rowIndex = 0;
++colIndex;
if (colIndex == parameter->getNominalSize())
{
colIndex = 0;
++paramIndex;
}
}
}
}
}
// Adds the arguments for a matrix constructor from a scalar
// putting the scalar along the diagonal and 0 everywhere else.
void AppendMatrixFromScalarArgument(const TType &type,
TFunction &function,
TIntermSequence *returnCtorArgs)
{
const TVariable *var = function.getParam(0);
TIntermSymbol *v = new TIntermSymbol(var);
const uint8_t numCols = type.getNominalSize();
const uint8_t numRows = type.getSecondarySize();
for (uint8_t col = 0; col < numCols; ++col)
{
for (uint8_t row = 0; row < numRows; ++row)
{
if (col == row)
{
returnCtorArgs->push_back(v->deepCopy());
}
else
{
returnCtorArgs->push_back(CreateFloatNode(0.0f, sh::EbpUndefined));
}
}
}
}
// Add the argument for a matrix constructor from a matrix
// copying elements from the same column/row and otherwise
// initialize to the identity matrix.
void AppendMatrixFromMatrixArgument(const TType &type,
TFunction &function,
TIntermSequence *returnCtorArgs)
{
const TVariable *var = function.getParam(0);
TIntermSymbol *v = new TIntermSymbol(var);
const uint8_t dstCols = type.getNominalSize();
const uint8_t dstRows = type.getSecondarySize();
const uint8_t srcCols = v->getNominalSize();
const uint8_t srcRows = v->getSecondarySize();
for (uint8_t dstCol = 0; dstCol < dstCols; ++dstCol)
{
for (uint8_t dstRow = 0; dstRow < dstRows; ++dstRow)
{
if (dstRow < srcRows && dstCol < srcCols)
{
AppendMatrixElementArgument(v, dstCol, dstRow, returnCtorArgs);
}
else
{
returnCtorArgs->push_back(
CreateFloatNode(dstRow == dstCol ? 1.0f : 0.0f, sh::EbpUndefined));
}
}
}
}
class Rebuild : public TIntermRebuild
{
public:
explicit Rebuild(TCompiler &compiler) : TIntermRebuild(compiler, false, true) {}
PostResult visitAggregatePost(TIntermAggregate &node) override
{
if (!node.isConstructor())
{
return node;
}
TIntermSequence &arguments = *node.getSequence();
if (arguments.empty())
{
return node;
}
const TType &type = node.getType();
const TType &arg0Type = arguments[0]->getAsTyped()->getType();
if (!type.isScalar() && !type.isVector() && !type.isMatrix())
{
return node;
}
if (type.isArray())
{
return node;
}
// check for type_ctor(sameType)
// scalar(scalar) -> passthrough
// vecN(vecN) -> passthrough
// matN(matN) -> passthrough
if (arguments.size() == 1 && arg0Type == type)
{
return node;
}
// The following are simple casts:
//
// - basic(s) (where basic is int, uint, float or bool, and s is scalar).
// - gvecN(vN) (where the argument is a single vector with the same number of components).
// - matNxM(mNxM) (where the argument is a single matrix with the same dimensions). Note
// that
// matrices are always float, so there's no actual cast and this would be a no-op.
//
const bool isSingleScalarCast =
arguments.size() == 1 && type.isScalar() && arg0Type.isScalar();
const bool isSingleVectorCast = arguments.size() == 1 && type.isVector() &&
arg0Type.isVector() &&
type.getNominalSize() == arg0Type.getNominalSize();
const bool isSingleMatrixCast =
arguments.size() == 1 && type.isMatrix() && arg0Type.isMatrix() &&
type.getCols() == arg0Type.getCols() && type.getRows() == arg0Type.getRows();
if (isSingleScalarCast || isSingleVectorCast || isSingleMatrixCast)
{
return node;
}
// Cases we need to handle:
// scalar(vec)
// scalar(mat)
// vecN(scalar)
// vecN(vecM)
// vecN(a,...)
// matN(scalar) -> diag
// matN(vec) -> fail!
// manN(matM) -> corner + ident
// matN(a, ...)
// Build a function and pass all the constructor's arguments to it.
TIntermBlock *body = new TIntermBlock;
TFunction *function = new TFunction(&mSymbolTable, ImmutableString(""),
SymbolType::AngleInternal, &type, true);
for (size_t i = 0; i < arguments.size(); ++i)
{
TIntermTyped &arg = *arguments[i]->getAsTyped();
TType *argType = new TType(arg.getBasicType(), arg.getPrecision(), EvqParamIn,
arg.getNominalSize(), arg.getSecondarySize());
TVariable *var = CreateTempVariable(&mSymbolTable, argType);
function->addParameter(var);
}
// Build a return statement for the function that
// converts the arguments into the required type.
TIntermSequence *returnCtorArgs = new TIntermSequence();
if (type.isScalar())
{
AppendScalarFromNonScalarArguments(*function, returnCtorArgs);
}
else if (type.isVector())
{
if (arguments.size() == 1 && arg0Type.isScalar())
{
AppendVectorFromScalarArgument(type, *function, returnCtorArgs);
}
else
{
AppendValuesFromMultipleArguments(type.getNominalSize(), *function, returnCtorArgs);
}
}
else if (type.isMatrix())
{
if (arguments.size() == 1 && arg0Type.isScalar())
{
// MSL already handles this case
AppendMatrixFromScalarArgument(type, *function, returnCtorArgs);
}
else if (arg0Type.isMatrix())
{
AppendMatrixFromMatrixArgument(type, *function, returnCtorArgs);
}
else
{
AppendValuesFromMultipleArguments(type.getNominalSize() * type.getSecondarySize(),
*function, returnCtorArgs);
}
}
TIntermBranch *returnStatement =
new TIntermBranch(EOpReturn, TIntermAggregate::CreateConstructor(type, returnCtorArgs));
body->appendStatement(returnStatement);
TIntermFunctionDefinition *functionDefinition =
CreateInternalFunctionDefinitionNode(*function, body);
mFunctionDefs.push_back(functionDefinition);
TIntermTyped *functionCall = TIntermAggregate::CreateFunctionCall(*function, &arguments);
return *functionCall;
}
bool rewrite(TIntermBlock &root)
{
if (!rebuildInPlace(root))
{
return true;
}
size_t firstFunctionIndex = FindFirstFunctionDefinitionIndex(&root);
for (TIntermFunctionDefinition *functionDefinition : mFunctionDefs)
{
root.insertChildNodes(firstFunctionIndex, TIntermSequence({functionDefinition}));
}
return mCompiler.validateAST(&root);
}
private:
TVector<TIntermFunctionDefinition *> mFunctionDefs;
};
} // anonymous namespace
bool sh::ConvertUnsupportedConstructorsToFunctionCalls(TCompiler &compiler, TIntermBlock &root)
{
return Rebuild(compiler).rewrite(root);
}