sksl: Add support for 2x2 matrix inversions in Metal

Bug: skia:
Change-Id: I524319ede491be4884c42d89f485ed98cff0cd29
Reviewed-on: https://skia-review.googlesource.com/c/171221
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
Commit-Queue: Chris Dalton <csmartdalton@google.com>
This commit is contained in:
Chris Dalton 2018-11-15 10:57:49 -05:00 committed by Skia Commit-Bot
parent 2268ad23fe
commit dba7aabcef
2 changed files with 24 additions and 0 deletions

View File

@ -204,6 +204,9 @@ void MetalCodeGenerator::writeFunctionCall(const FunctionCall& c) {
this->write("atan2");
} else if (c.fFunction.fBuiltin && "inversesqrt" == c.fFunction.fName) {
this->write("rsqrt");
} else if (c.fFunction.fBuiltin && "inverse" == c.fFunction.fName) {
SkASSERT(c.fArguments.size() == 1);
this->writeInverseHack(*c.fArguments[0]);
} else if (c.fFunction.fBuiltin && "dFdx" == c.fFunction.fName) {
this->write("dfdx");
} else if (c.fFunction.fBuiltin && "dFdy" == c.fFunction.fName) {
@ -244,6 +247,22 @@ void MetalCodeGenerator::writeFunctionCall(const FunctionCall& c) {
this->write(")");
}
void MetalCodeGenerator::writeInverseHack(const Expression& mat) {
String name = "ERROR_MatrixInverseNotImplementedFor_" + mat.fType.name();
if (mat.fType == *fContext.fFloat2x2_Type) {
name = "_inverse2";
if (fWrittenIntrinsics.find(name) == fWrittenIntrinsics.end()) {
fWrittenIntrinsics.insert(name);
fExtraFunctions.writeText((
"float2x2 " + name + "(float2x2 m) {"
" return float2x2(m[1][1], -m[0][1], -m[1][0], m[0][0]) * (1/determinant(m));"
"}"
).c_str());
}
}
this->write(name);
}
void MetalCodeGenerator::writeSpecialIntrinsic(const FunctionCall & c, SpecialIntrinsic kind) {
switch (kind) {
case kTexture_SpecialIntrinsic:
@ -1444,6 +1463,7 @@ bool MetalCodeGenerator::generateCode() {
fOut = rawOut;
write_stringstream(fHeader, *rawOut);
write_stringstream(fExtraFunctions, *rawOut);
write_stringstream(body, *rawOut);
#ifdef SK_MOLTENVK
this->write("\0");

View File

@ -182,6 +182,8 @@ protected:
void writeFunctionCall(const FunctionCall& c);
void writeInverseHack(const Expression& mat);
void writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind);
void writeConstructor(const Constructor& c);
@ -250,6 +252,7 @@ protected:
const Context& fContext;
StringStream fHeader;
String fFunctionHeader;
StringStream fExtraFunctions;
Program::Kind fProgramKind;
int fVarCount = 0;
int fIndentation = 0;
@ -258,6 +261,7 @@ protected:
// more than one or two structs per shader, a simple linear search will be faster than anything
// fancier.
std::vector<const Type*> fWrittenStructs;
std::set<String> fWrittenIntrinsics;
// true if we have run into usages of dFdx / dFdy
bool fFoundDerivatives = false;
bool fFoundImageDecl = false;