Hash :
b03f0148
Author :
Date :
2024-11-01T10:26:04
Metal: interpolateAtOffset fails validation Fix validation error Found function prototype with an invalid qualifier. The offset parameter was created with "global" qualifier. Bug: angleproject:376718268 Change-Id: I6f314a42fe1d35d54bb673cfa7d58eba526c9ea5 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5979778 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.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
//
// Copyright 2023 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/msl/RewriteInterpolants.h"
#include "compiler/translator/StaticType.h"
#include "compiler/translator/msl/AstHelpers.h"
#include "compiler/translator/tree_util/BuiltIn.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/ReplaceVariable.h"
namespace sh
{
namespace
{
class FindInterpolantsTraverser : public TIntermTraverser
{
public:
FindInterpolantsTraverser(TSymbolTable *symbolTable, const DriverUniformMetal *driverUniforms)
: TIntermTraverser(true, false, false, symbolTable),
mDriverUniforms(driverUniforms),
mUsesSampleInterpolation(false)
{}
bool visitDeclaration(Visit, TIntermDeclaration *node) override
{
const TIntermSequence &sequence = *(node->getSequence());
ASSERT(!sequence.empty());
const TIntermTyped &typedNode = *(sequence.front()->getAsTyped());
TQualifier qualifier = typedNode.getQualifier();
if (qualifier == EvqSampleIn || qualifier == EvqNoPerspectiveSampleIn)
{
mUsesSampleInterpolation = true;
}
return true;
}
const TFunction *getFlipFunction()
{
if (mFlipFunction != nullptr)
{
return mFlipFunction->getFunction();
}
const TType *vec2Type = StaticType::GetQualified<EbtFloat, EbpHigh, EvqParamIn, 2>();
TVariable *offsetParam = new TVariable(mSymbolTable, ImmutableString("offset"), vec2Type,
SymbolType::AngleInternal);
TFunction *function =
new TFunction(mSymbolTable, ImmutableString("ANGLEFlipInterpolationOffset"),
SymbolType::AngleInternal, vec2Type, true);
function->addParameter(offsetParam);
TIntermTyped *flipXY =
mDriverUniforms->getFlipXY(mSymbolTable, DriverUniformFlip::Fragment);
TIntermTyped *flipped = new TIntermBinary(EOpMul, new TIntermSymbol(offsetParam), flipXY);
TIntermBranch *returnStatement = new TIntermBranch(EOpReturn, flipped);
TIntermBlock *body = new TIntermBlock;
body->appendStatement(returnStatement);
mFlipFunction = new TIntermFunctionDefinition(new TIntermFunctionPrototype(function), body);
return function;
}
bool visitAggregate(Visit visit, TIntermAggregate *node) override
{
if (!BuiltInGroup::IsInterpolationFS(node->getOp()))
{
return true;
}
TIntermNode *operand = node->getSequence()->at(0);
ASSERT(operand);
// For all of the interpolation functions, <interpolant> must be an input
// variable or an element of an input variable declared as an array.
const TIntermSymbol *symbolNode = operand->getAsSymbolNode();
if (!symbolNode)
{
const TIntermBinary *binaryNode = operand->getAsBinaryNode();
if (binaryNode &&
(binaryNode->getOp() == EOpIndexDirect || binaryNode->getOp() == EOpIndexIndirect))
{
symbolNode = binaryNode->getLeft()->getAsSymbolNode();
}
}
ASSERT(symbolNode);
// If <interpolant> is declared with a "flat" qualifier, the interpolated
// value will have the same value everywhere for a single primitive, so
// the location used for the interpolation has no effect and the functions
// just return that same value.
const TVariable *variable = &symbolNode->variable();
if (variable->getType().getQualifier() != EvqFlatIn)
{
mInterpolants.insert(variable);
}
// Flip offset's Y if needed.
if (node->getOp() == EOpInterpolateAtOffset)
{
TIntermTyped *offsetNode = node->getSequence()->at(1)->getAsTyped();
TIntermTyped *correctedOffset = TIntermAggregate::CreateFunctionCall(
*getFlipFunction(), new TIntermSequence{offsetNode});
queueReplacementWithParent(node, offsetNode, correctedOffset, OriginalNode::IS_DROPPED);
}
return true;
}
bool usesSampleInterpolation() const { return mUsesSampleInterpolation; }
const std::unordered_set<const TVariable *> &getInterpolants() const { return mInterpolants; }
TIntermFunctionDefinition *getFlipFunctionDefinition() { return mFlipFunction; }
private:
const DriverUniformMetal *mDriverUniforms;
bool mUsesSampleInterpolation;
std::unordered_set<const TVariable *> mInterpolants;
TIntermFunctionDefinition *mFlipFunction = nullptr;
};
class WrapInterpolantsTraverser : public TIntermTraverser
{
public:
WrapInterpolantsTraverser(TSymbolTable *symbolTable)
: TIntermTraverser(true, false, false, symbolTable), mUsesSampleInterpolant(false)
{}
void visitSymbol(TIntermSymbol *node) override
{
// Skip all symbols not previously marked as
// interpolants by FindInterpolantsTraverser
const TType &type = node->variable().getType();
if (!type.isInterpolant())
{
return;
}
TIntermNode *ancestor = getAncestorNode(0);
ASSERT(ancestor);
// Only root-level input varying declarations should be
// reachable by this line and they must not be wrapped.
if (ancestor->getAsDeclarationNode())
{
return;
}
auto checkSkip = [](TIntermNode *node, TIntermNode *parentNode) {
if (TIntermAggregate *callNode = parentNode->getAsAggregate())
{
if (BuiltInGroup::IsInterpolationFS(callNode->getOp()) &&
callNode->getSequence()->at(0) == node)
{
return true;
}
}
return false;
};
// Skip symbols used as the first operand of interpolation functions
if (checkSkip(node, ancestor))
{
return;
}
TIntermNode *original = node;
if (TIntermBinary *binaryNode = ancestor->getAsBinaryNode())
{
if (binaryNode->getOp() == EOpIndexDirect || binaryNode->getOp() == EOpIndexIndirect)
{
ancestor = getAncestorNode(1);
ASSERT(ancestor);
// Skip array elements used as the first operand of interpolation functions
if (checkSkip(binaryNode, ancestor))
{
return;
}
original = binaryNode;
}
}
const char *functionName = nullptr;
TIntermSequence *arguments = new TIntermSequence{original};
switch (type.getQualifier())
{
case EvqFragmentIn:
case EvqSmoothIn:
case EvqNoPerspectiveIn:
// `metal::interpolant` variables cannot be used directly,
// so MSL has a dedicated interpolation function to obtain
// their pixel-center values. This function is included in
// the `MetalFragmentSample` built-in functions group.
functionName = "interpolateAtCenter";
break;
case EvqCentroidIn:
case EvqNoPerspectiveCentroidIn:
functionName = "interpolateAtCentroid";
break;
case EvqSampleIn:
case EvqNoPerspectiveSampleIn:
functionName = "interpolateAtSample";
arguments->push_back(new TIntermSymbol(BuiltInVariable::gl_SampleID()));
mUsesSampleInterpolant = true;
break;
default:
UNREACHABLE();
break;
}
TIntermTyped *replacement = CreateBuiltInFunctionCallNode(
functionName, arguments, *mSymbolTable, kESSLInternalBackendBuiltIns);
queueReplacementWithParent(ancestor, original, replacement, OriginalNode::BECOMES_CHILD);
}
bool usesSampleInterpolant() const { return mUsesSampleInterpolant; }
private:
bool mUsesSampleInterpolant;
};
} // anonymous namespace
[[nodiscard]] bool RewriteInterpolants(TCompiler &compiler,
TIntermBlock &root,
TSymbolTable &symbolTable,
const DriverUniformMetal *driverUniforms,
bool *outUsesSampleInterpolation,
bool *outUsesSampleInterpolant)
{
// Find all fragment inputs used with interpolation functions.
FindInterpolantsTraverser findInterpolantsTraverser(&symbolTable, driverUniforms);
root.traverse(&findInterpolantsTraverser);
// Define ANGLEFlipInterpolationOffset if interpolateAtOffset was used.
if (findInterpolantsTraverser.getFlipFunctionDefinition() != nullptr)
{
const size_t firstFunctionIndex = FindFirstFunctionDefinitionIndex(&root);
root.insertStatement(firstFunctionIndex,
findInterpolantsTraverser.getFlipFunctionDefinition());
}
if (!findInterpolantsTraverser.updateTree(&compiler, &root))
{
return false;
}
*outUsesSampleInterpolation = findInterpolantsTraverser.usesSampleInterpolation();
// Skip further operations when interpolation functions are not used.
if (findInterpolantsTraverser.getInterpolants().empty())
{
return true;
}
// Adjust variable types as per MSL requirements
//
// * Inputs with omitted and smooth interpolation qualifiers will be written as
// metal::interpolant<T, metal::interpolation::perspective>
//
// * Inputs with noperspective interpolation qualifiers will be written as
// metal::interpolant<T, metal::interpolation::no_perspective>
for (const TVariable *var : findInterpolantsTraverser.getInterpolants())
{
TType *replacementType = new TType(var->getType());
replacementType->setInterpolant(true);
TVariable *replacement =
new TVariable(&symbolTable, var->name(), replacementType, var->symbolType());
if (!ReplaceVariable(&compiler, &root, var, replacement))
{
return false;
}
}
// Wrap direct usages of interpolants with explicit interpolation
// functions depending on their auxiliary qualifiers
// in vec4 interpolant -> ANGLE_interpolateAtCenter(interpolant)
// centroid in vec4 interpolant -> ANGLE_interpolateAtCentroid(interpolant)
// sample in vec4 interpolant -> ANGLE_interpolateAtSample(interpolant, gl_SampleID)
WrapInterpolantsTraverser wrapInterpolantsTraverser(&symbolTable);
root.traverse(&wrapInterpolantsTraverser);
if (!wrapInterpolantsTraverser.updateTree(&compiler, &root))
{
return false;
}
*outUsesSampleInterpolant = wrapInterpolantsTraverser.usesSampleInterpolant();
return true;
}
} // namespace sh