Hash :
9ff58ddd
Author :
Date :
2020-08-26T01:08:00
Metal: Support array of matrices varying. MSL doesn't support array of matrices as varying variable. This CL converts array of matrices varying to array of vectors. This fixes WebGL test: conformance/glsl/misc/shader-varying-packing-restrictions.html Bug: angleproject:4846 Bug: angleproject:2634 Change-Id: I3cfc638dc01092ddf1add7df02c131d1a5c8ba1d Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2392172 Commit-Queue: Le Hoang Quyen <le.hoang.q@gmail.com> Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Reviewed-by: Jamie Madill <jmadill@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
//
// 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.
//
// ReplaceArrayOfMatrixVarying: Find any references to array of matrices varying
// and replace it with array of vectors.
//
#include "compiler/translator/tree_util/ReplaceArrayOfMatrixVarying.h"
#include <vector>
#include "common/bitset_utils.h"
#include "common/debug.h"
#include "common/utilities.h"
#include "compiler/translator/Compiler.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/BuiltIn.h"
#include "compiler/translator/tree_util/FindMain.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/tree_util/ReplaceVariable.h"
#include "compiler/translator/tree_util/RunAtTheEndOfShader.h"
#include "compiler/translator/util.h"
namespace sh
{
// We create two variables to replace the given varying:
// - The new varying which is an array of vectors to be used at input/ouput only.
// - The new global variable which is a same type as given variable, to temporarily be used
// as replacements for assignments, arithmetic ops and so on. During input/ouput phrase, this temp
// variable will be copied from/to the array of vectors variable above.
// NOTE(hqle): Consider eliminating the need for using temp variable.
namespace
{
class CollectVaryingTraverser : public TIntermTraverser
{
public:
CollectVaryingTraverser(std::vector<const TVariable *> *varyingsOut)
: TIntermTraverser(true, false, false), mVaryingsOut(varyingsOut)
{}
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override
{
const TIntermSequence &sequence = *(node->getSequence());
if (sequence.size() != 1)
{
return false;
}
TIntermTyped *variableType = sequence.front()->getAsTyped();
if (!variableType || !IsVarying(variableType->getQualifier()) ||
!variableType->isMatrix() || !variableType->isArray())
{
return false;
}
TIntermSymbol *variableSymbol = variableType->getAsSymbolNode();
if (!variableSymbol)
{
return false;
}
mVaryingsOut->push_back(&variableSymbol->variable());
return false;
}
private:
std::vector<const TVariable *> *mVaryingsOut;
};
} // namespace
ANGLE_NO_DISCARD bool ReplaceArrayOfMatrixVarying(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable,
const TVariable *varying)
{
const TType &type = varying->getType();
// Create global variable to temporarily acts as the given variable in places such as
// arithmetic, assignments an so on.
TType *tmpReplacementType = new TType(type);
tmpReplacementType->setQualifier(EvqGlobal);
tmpReplacementType->realize();
TVariable *tempReplaceVar = new TVariable(
symbolTable, ImmutableString(std::string("ANGLE_AOM_Temp_") + varying->name().data()),
tmpReplacementType, SymbolType::AngleInternal);
if (!ReplaceVariable(compiler, root, varying, tempReplaceVar))
{
return false;
}
// Create array of vectors type
TType *varyingReplaceType =
new TType(type.getBasicType(), type.getPrecision(), type.getQualifier(),
static_cast<unsigned char>(type.getRows()), 1);
varyingReplaceType->setInvariant(type.isInvariant());
varyingReplaceType->setMemoryQualifier(type.getMemoryQualifier());
varyingReplaceType->setLayoutQualifier(type.getLayoutQualifier());
varyingReplaceType->makeArray(type.getCols() * type.getOutermostArraySize());
varyingReplaceType->realize();
TVariable *varyingReplaceVar =
new TVariable(symbolTable, varying->name(), varyingReplaceType, SymbolType::UserDefined);
TIntermSymbol *varyingReplaceDeclarator = new TIntermSymbol(varyingReplaceVar);
TIntermDeclaration *varyingReplaceDecl = new TIntermDeclaration;
varyingReplaceDecl->appendDeclarator(varyingReplaceDeclarator);
root->insertStatement(0, varyingReplaceDecl);
// Copy from/to the temp variable
TIntermBlock *reassignBlock = new TIntermBlock;
TIntermSymbol *tempReplaceSymbol = new TIntermSymbol(tempReplaceVar);
TIntermSymbol *varyingReplaceSymbol = new TIntermSymbol(varyingReplaceVar);
bool isInput = IsVaryingIn(type.getQualifier());
for (unsigned int i = 0; i < type.getOutermostArraySize(); ++i)
{
TIntermBinary *tempMatrixIndexed =
new TIntermBinary(EOpIndexDirect, tempReplaceSymbol->deepCopy(), CreateIndexNode(i));
for (int col = 0; col < type.getCols(); ++col)
{
TIntermBinary *tempMatrixColIndexed = new TIntermBinary(
EOpIndexDirect, tempMatrixIndexed->deepCopy(), CreateIndexNode(col));
TIntermBinary *vectorIndexed =
new TIntermBinary(EOpIndexDirect, varyingReplaceSymbol->deepCopy(),
CreateIndexNode(i * type.getCols() + col));
TIntermBinary *assignment;
if (isInput)
{
assignment = new TIntermBinary(EOpAssign, tempMatrixColIndexed, vectorIndexed);
}
else
{
assignment = new TIntermBinary(EOpAssign, vectorIndexed, tempMatrixColIndexed);
}
reassignBlock->appendStatement(assignment);
}
}
if (isInput)
{
TIntermFunctionDefinition *main = FindMain(root);
main->getBody()->insertStatement(0, reassignBlock);
return compiler->validateAST(root);
}
else
{
return RunAtTheEndOfShader(compiler, root, reassignBlock, symbolTable);
}
}
ANGLE_NO_DISCARD bool ReplaceArrayOfMatrixVaryings(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable)
{
std::vector<const TVariable *> arrayOfMatrixVars;
CollectVaryingTraverser varCollector(&arrayOfMatrixVars);
root->traverse(&varCollector);
for (const TVariable *var : arrayOfMatrixVars)
{
if (!ReplaceArrayOfMatrixVarying(compiler, root, symbolTable, var))
{
return false;
}
}
return true;
}
} // namespace sh