Hash :
edcfab40
Author :
Date :
2023-11-23T00:00:00
Metal: Implement textureCubeGrad transformation for AGX Wrapped all affected built-in function calls with helpers that transform derivative values. Fixed all *.texturegrad.* dEQP failures on Apple silicon. Fixed: angleproject:7021 Fixed: angleproject:8433 Change-Id: I16b023840ad267ab72d31fde3cb0fa7048e5310c Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5071254 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Alexey Knyazev <lexa.knyazev@gmail.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
//
// 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/PreTransformTextureCubeGradDerivatives.h"
#include "compiler/translator/StaticType.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/FindFunction.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
namespace sh
{
namespace
{
constexpr ImmutableString kFunctionAGX("ANGLE_textureGradAGX");
const TType *kBoolType = StaticType::GetTemporary<EbtBool, EbpUndefined>();
const TType *kVec3Type = StaticType::GetTemporary<EbtFloat, EbpMedium, 3>();
const TType *kVec4Type = StaticType::GetTemporary<EbtFloat, EbpMedium, 4>();
const TType *kVec3InType = StaticType::GetQualified<EbtFloat, EbpMedium, EvqParamIn, 3>();
const TType *kVec4InType = StaticType::GetQualified<EbtFloat, EbpMedium, EvqParamIn, 4>();
class PreTransformTextureCubeGradTraverser : public TIntermTraverser
{
public:
PreTransformTextureCubeGradTraverser(TSymbolTable *symbolTable, int shaderVersion)
: TIntermTraverser(true, false, false, symbolTable), mShaderVersion(shaderVersion)
{}
const TVariable *getSwizzledVariable(const TVariable *source,
const TVariable *xMajor,
const TVariable *yMajor,
TIntermBlock *body)
{
TIntermSwizzle *sYZX = new TIntermSwizzle(new TIntermSymbol(source), {1, 2, 0});
TIntermSwizzle *sXZY = new TIntermSwizzle(new TIntermSymbol(source), {0, 2, 1});
TIntermSwizzle *sXYZ = new TIntermSwizzle(new TIntermSymbol(source), {0, 1, 2});
TIntermTernary *secondRule = new TIntermTernary(new TIntermSymbol(yMajor), sXZY, sXYZ);
const TVariable *var = CreateTempVariable(mSymbolTable, kVec3Type);
body->appendStatement(CreateTempInitDeclarationNode(
var, new TIntermTernary(new TIntermSymbol(xMajor), sYZX, secondRule)));
return var;
}
const TFunction *getReplacementFunction(const TType &textureType, const TType &returnType)
{
const TBasicType samplerType = textureType.getBasicType();
ASSERT(IsSamplerCube(samplerType));
if (mReplacementFunctions[samplerType] != nullptr)
{
return mReplacementFunctions[samplerType]->getFunction();
}
// Sampler
TType *texType = new TType(textureType);
texType->setQualifier(EvqParamIn);
const TVariable *texture =
new TVariable(mSymbolTable, kEmptyImmutableString, texType, SymbolType::AngleInternal);
// Direction vector
const TType *directionType =
samplerType == EbtSamplerCubeShadow ? kVec4InType : kVec3InType;
const TVariable *direction = new TVariable(mSymbolTable, kEmptyImmutableString,
directionType, SymbolType::AngleInternal);
// Derivatives
const TVariable *dPdx = new TVariable(mSymbolTable, kEmptyImmutableString, kVec3InType,
SymbolType::AngleInternal);
const TVariable *dPdy = new TVariable(mSymbolTable, kEmptyImmutableString, kVec3InType,
SymbolType::AngleInternal);
TFunction *function =
new TFunction(mSymbolTable, kFunctionAGX, SymbolType::AngleInternal, &returnType, true);
function->addParameter(texture);
function->addParameter(direction);
function->addParameter(dPdx);
function->addParameter(dPdy);
TIntermBlock *body = new TIntermBlock;
// Select major axis. Apple GPUs have the following rules:
// * X wins over Y and Z
// * Y wins over Z
// vec3 absDirection = abs(direction.xyz);
const TVariable *absDirection = CreateTempVariable(mSymbolTable, kVec3Type);
body->appendStatement(CreateTempInitDeclarationNode(
absDirection, CreateBuiltInFunctionCallNode(
"abs", {new TIntermSwizzle(new TIntermSymbol(direction), {0, 1, 2})},
*mSymbolTable, mShaderVersion)));
TIntermSwizzle *absDirectionX = new TIntermSwizzle(new TIntermSymbol(absDirection), {0});
TIntermSwizzle *absDirectionY = new TIntermSwizzle(new TIntermSymbol(absDirection), {1});
TIntermSwizzle *absDirectionZ = new TIntermSwizzle(new TIntermSymbol(absDirection), {2});
// bool xMajor = absDirection.x >= max(absDirection.y, absDirection.z);
const TVariable *xMajor = CreateTempVariable(mSymbolTable, kBoolType);
body->appendStatement(CreateTempInitDeclarationNode(
xMajor,
new TIntermBinary(EOpGreaterThanEqual, absDirectionX,
CreateBuiltInFunctionCallNode("max", {absDirectionY, absDirectionZ},
*mSymbolTable, mShaderVersion))));
// bool yMajor = absDirection.y >= absDirection.z;
const TVariable *yMajor = CreateTempVariable(mSymbolTable, kBoolType);
body->appendStatement(CreateTempInitDeclarationNode(
yMajor, new TIntermBinary(EOpGreaterThanEqual, absDirectionY->deepCopy(),
absDirectionZ->deepCopy())));
// Prepare input vectors
// vec3 faceDirection = xMajor ? direction.yzx : (yMajor ? direction.xzy : direction.xyz);
const TVariable *faceDirection = getSwizzledVariable(direction, xMajor, yMajor, body);
// vec3 dQdx = xMajor ? dPdx.yzx : (yMajor ? dPdx.xzy : dPdx);
const TVariable *dQdx = getSwizzledVariable(dPdx, xMajor, yMajor, body);
// vec3 dQdy = xMajor ? dPdy.yzx : (yMajor ? dPdy.xzy : dPdy);
const TVariable *dQdy = getSwizzledVariable(dPdy, xMajor, yMajor, body);
// Transform all derivatives; Q = faceDirection
// vec4 d = vec4(dQdx.xy, dQdy.xy) - (Q.xy / Q.z).xyxy * vec4(dQdx.zz, dQdy.zz);
TIntermAggregate *packXY = TIntermAggregate::CreateConstructor(
*kVec4Type, {new TIntermSwizzle(new TIntermSymbol(dQdx), {0, 1}),
new TIntermSwizzle(new TIntermSymbol(dQdy), {0, 1})});
TIntermAggregate *packZZ = TIntermAggregate::CreateConstructor(
*kVec4Type, {new TIntermSwizzle(new TIntermSymbol(dQdx), {2, 2}),
new TIntermSwizzle(new TIntermSymbol(dQdy), {2, 2})});
TIntermSwizzle *division = new TIntermSwizzle(
new TIntermBinary(EOpDiv, new TIntermSwizzle(new TIntermSymbol(faceDirection), {0, 1}),
new TIntermSwizzle(new TIntermSymbol(faceDirection), {2})),
{0, 1, 0, 1});
const TVariable *d = CreateTempVariable(mSymbolTable, kVec4Type);
body->appendStatement(CreateTempInitDeclarationNode(
d, new TIntermBinary(EOpSub, packXY, new TIntermBinary(EOpMul, division, packZZ))));
// Final swizzle to put the transformed values into target components
// X major: X and Z; Y major: X and Y; Z major: Y and Z
TIntermTernary *transformedX = new TIntermTernary(
new TIntermSymbol(xMajor), new TIntermSwizzle(new TIntermSymbol(d), {0, 0, 1}),
new TIntermSwizzle(new TIntermSymbol(d), {0, 1, 0}));
TIntermTernary *transformedY = new TIntermTernary(
new TIntermSymbol(xMajor), new TIntermSwizzle(new TIntermSymbol(d), {2, 2, 3}),
new TIntermSwizzle(new TIntermSymbol(d), {2, 3, 2}));
TIntermTyped *nativeCall = CreateBuiltInFunctionCallNode(
mShaderVersion == 100 ? "textureCubeGradEXT" : "textureGrad",
{new TIntermSymbol(texture), new TIntermSymbol(direction), transformedX, transformedY},
*mSymbolTable, mShaderVersion);
body->appendStatement(new TIntermBranch(EOpReturn, nativeCall));
mReplacementFunctions[samplerType] =
new TIntermFunctionDefinition(new TIntermFunctionPrototype(function), body);
mNewFunctionType = samplerType;
return function;
}
bool visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) override
{
// Do not traverse the wrapper function
return node->getFunction()->name() != kFunctionAGX;
}
bool visitAggregate(Visit visit, TIntermAggregate *node) override
{
if (mFound)
{
return false;
}
switch (node->getOp())
{
case EOpTextureCubeGradEXT:
case EOpTextureGrad:
break;
default:
return true;
}
TIntermSequence *parameters = node->getSequence();
TIntermTyped *tex = parameters->at(0)->getAsTyped();
if (!IsSamplerCube(tex->getBasicType()))
{
return true;
}
queueReplacement(TIntermAggregate::CreateFunctionCall(
*getReplacementFunction(tex->getType(), node->getType()), parameters),
OriginalNode::IS_DROPPED);
mFound = true;
return false;
}
void nextIteration()
{
mNewFunctionType = EbtVoid;
mFound = false;
}
TIntermFunctionDefinition *getNewReplacementFunction()
{
return mNewFunctionType != EbtVoid ? mReplacementFunctions[mNewFunctionType] : nullptr;
}
bool found() const { return mFound; }
private:
const int mShaderVersion;
std::map<TBasicType, TIntermFunctionDefinition *> mReplacementFunctions;
TBasicType mNewFunctionType = EbtVoid;
bool mFound = false;
};
} // anonymous namespace
bool PreTransformTextureCubeGradDerivatives(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable,
int shaderVersion)
{
PreTransformTextureCubeGradTraverser traverser(symbolTable, shaderVersion);
do
{
traverser.nextIteration();
root->traverse(&traverser);
if (traverser.found())
{
TIntermFunctionDefinition *newFunction = traverser.getNewReplacementFunction();
if (newFunction != nullptr)
{
root->insertStatement(FindFirstFunctionDefinitionIndex(root), newFunction);
}
if (!traverser.updateTree(compiler, root))
{
return false;
}
}
} while (traverser.found());
return true;
}
} // namespace sh