| 1 |
/* |
| 2 |
* Copyright 2016 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/sksl/codegen/SkSLGLSLCodeGenerator.h" |
| 9 |
|
| 10 |
#include "include/core/SkSpan.h" |
| 11 |
#include "include/core/SkTypes.h" |
| 12 |
#include "include/private/base/SkTArray.h" |
| 13 |
#include "src/base/SkEnumBitMask.h" |
| 14 |
#include "src/base/SkNoDestructor.h" |
| 15 |
#include "src/base/SkStringView.h" |
| 16 |
#include "src/core/SkTHash.h" |
| 17 |
#include "src/core/SkTraceEvent.h" |
| 18 |
#include "src/sksl/SkSLAnalysis.h" |
| 19 |
#include "src/sksl/SkSLBuiltinTypes.h" |
| 20 |
#include "src/sksl/SkSLCompiler.h" |
| 21 |
#include "src/sksl/SkSLContext.h" |
| 22 |
#include "src/sksl/SkSLDefines.h" |
| 23 |
#include "src/sksl/SkSLErrorReporter.h" |
| 24 |
#include "src/sksl/SkSLGLSL.h" |
| 25 |
#include "src/sksl/SkSLIntrinsicList.h" |
| 26 |
#include "src/sksl/SkSLOperator.h" |
| 27 |
#include "src/sksl/SkSLOutputStream.h" |
| 28 |
#include "src/sksl/SkSLPosition.h" |
| 29 |
#include "src/sksl/SkSLProgramSettings.h" |
| 30 |
#include "src/sksl/SkSLString.h" |
| 31 |
#include "src/sksl/SkSLStringStream.h" |
| 32 |
#include "src/sksl/SkSLUtil.h" |
| 33 |
#include "src/sksl/codegen/SkSLCodeGenTypes.h" |
| 34 |
#include "src/sksl/codegen/SkSLCodeGenerator.h" |
| 35 |
#include "src/sksl/codegen/SkSLNativeShader.h" |
| 36 |
#include "src/sksl/ir/SkSLBinaryExpression.h" |
| 37 |
#include "src/sksl/ir/SkSLBlock.h" |
| 38 |
#include "src/sksl/ir/SkSLConstructor.h" |
| 39 |
#include "src/sksl/ir/SkSLConstructorArrayCast.h" |
| 40 |
#include "src/sksl/ir/SkSLConstructorCompound.h" |
| 41 |
#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" |
| 42 |
#include "src/sksl/ir/SkSLDoStatement.h" |
| 43 |
#include "src/sksl/ir/SkSLExpression.h" |
| 44 |
#include "src/sksl/ir/SkSLExpressionStatement.h" |
| 45 |
#include "src/sksl/ir/SkSLExtension.h" |
| 46 |
#include "src/sksl/ir/SkSLFieldAccess.h" |
| 47 |
#include "src/sksl/ir/SkSLForStatement.h" |
| 48 |
#include "src/sksl/ir/SkSLFunctionCall.h" |
| 49 |
#include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 50 |
#include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 51 |
#include "src/sksl/ir/SkSLFunctionPrototype.h" |
| 52 |
#include "src/sksl/ir/SkSLIRNode.h" |
| 53 |
#include "src/sksl/ir/SkSLIfStatement.h" |
| 54 |
#include "src/sksl/ir/SkSLIndexExpression.h" |
| 55 |
#include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 56 |
#include "src/sksl/ir/SkSLLayout.h" |
| 57 |
#include "src/sksl/ir/SkSLLiteral.h" |
| 58 |
#include "src/sksl/ir/SkSLModifierFlags.h" |
| 59 |
#include "src/sksl/ir/SkSLModifiersDeclaration.h" |
| 60 |
#include "src/sksl/ir/SkSLPostfixExpression.h" |
| 61 |
#include "src/sksl/ir/SkSLPrefixExpression.h" |
| 62 |
#include "src/sksl/ir/SkSLProgram.h" |
| 63 |
#include "src/sksl/ir/SkSLProgramElement.h" |
| 64 |
#include "src/sksl/ir/SkSLReturnStatement.h" |
| 65 |
#include "src/sksl/ir/SkSLSetting.h" |
| 66 |
#include "src/sksl/ir/SkSLStatement.h" |
| 67 |
#include "src/sksl/ir/SkSLStructDefinition.h" |
| 68 |
#include "src/sksl/ir/SkSLSwitchCase.h" |
| 69 |
#include "src/sksl/ir/SkSLSwitchStatement.h" |
| 70 |
#include "src/sksl/ir/SkSLSwizzle.h" |
| 71 |
#include "src/sksl/ir/SkSLTernaryExpression.h" |
| 72 |
#include "src/sksl/ir/SkSLType.h" |
| 73 |
#include "src/sksl/ir/SkSLVarDeclarations.h" |
| 74 |
#include "src/sksl/ir/SkSLVariable.h" |
| 75 |
#include "src/sksl/ir/SkSLVariableReference.h" |
| 76 |
#include "src/sksl/spirv.h" |
| 77 |
|
| 78 |
#include <cstddef> |
| 79 |
#include <cstdint> |
| 80 |
#include <initializer_list> |
| 81 |
#include <memory> |
| 82 |
#include <string> |
| 83 |
#include <string_view> |
| 84 |
#include <vector> |
| 85 |
|
| 86 |
using namespace skia_private; |
| 87 |
|
| 88 |
namespace SkSL { |
| 89 |
|
| 90 |
class GLSLCodeGenerator final : public CodeGenerator { |
| 91 |
public: |
| 92 |
GLSLCodeGenerator(const Context* context, |
| 93 |
const ShaderCaps* caps, |
| 94 |
const Program* program, |
| 95 |
OutputStream* out, |
| 96 |
PrettyPrint pp) |
| 97 |
: CodeGenerator(context, caps, program, out), fPrettyPrint(pp) {} |
| 98 |
|
| 99 |
bool generateCode() override; |
| 100 |
|
| 101 |
protected: |
| 102 |
using Precedence = OperatorPrecedence; |
| 103 |
|
| 104 |
void write(std::string_view s); |
| 105 |
|
| 106 |
void writeLine(std::string_view s = std::string_view()); |
| 107 |
|
| 108 |
void finishLine(); |
| 109 |
|
| 110 |
void writeHeader(); |
| 111 |
|
| 112 |
bool usesPrecisionModifiers() const; |
| 113 |
|
| 114 |
void writeIdentifier(std::string_view identifier); |
| 115 |
|
| 116 |
std::string getTypeName(const Type& type); |
| 117 |
|
| 118 |
void writeStructDefinition(const StructDefinition& s); |
| 119 |
|
| 120 |
void writeType(const Type& type); |
| 121 |
|
| 122 |
void writeExtension(std::string_view name, bool require = true); |
| 123 |
|
| 124 |
void writeInterfaceBlock(const InterfaceBlock& intf); |
| 125 |
|
| 126 |
void writeFunctionDeclaration(const FunctionDeclaration& f); |
| 127 |
|
| 128 |
void writeFunctionPrototype(const FunctionPrototype& f); |
| 129 |
|
| 130 |
void writeFunction(const FunctionDefinition& f); |
| 131 |
|
| 132 |
void writeLayout(const Layout& layout); |
| 133 |
|
| 134 |
void writeModifiers(const Layout& layout, ModifierFlags flags, bool globalContext); |
| 135 |
|
| 136 |
void writeInputVars(); |
| 137 |
|
| 138 |
void writeVarInitializer(const Variable& var, const Expression& value); |
| 139 |
|
| 140 |
const char* getTypePrecision(const Type& type); |
| 141 |
|
| 142 |
void writeTypePrecision(const Type& type); |
| 143 |
|
| 144 |
void writeGlobalVarDeclaration(const GlobalVarDeclaration& e); |
| 145 |
|
| 146 |
void writeVarDeclaration(const VarDeclaration& var, bool global); |
| 147 |
|
| 148 |
void writeFragCoord(); |
| 149 |
|
| 150 |
void writeVariableReference(const VariableReference& ref); |
| 151 |
|
| 152 |
void writeExpression(const Expression& expr, Precedence parentPrecedence); |
| 153 |
|
| 154 |
void writeIntrinsicCall(const FunctionCall& c); |
| 155 |
|
| 156 |
void writeMinAbsHack(Expression& absExpr, Expression& otherExpr); |
| 157 |
|
| 158 |
void writeDeterminantHack(const Expression& mat); |
| 159 |
|
| 160 |
void writeInverseHack(const Expression& mat); |
| 161 |
|
| 162 |
void writeTransposeHack(const Expression& mat); |
| 163 |
|
| 164 |
void writeInverseSqrtHack(const Expression& x); |
| 165 |
|
| 166 |
void writeMatrixComparisonWorkaround(const BinaryExpression& x); |
| 167 |
|
| 168 |
void writeFunctionCall(const FunctionCall& c); |
| 169 |
|
| 170 |
void writeConstructorCompound(const ConstructorCompound& c, Precedence parentPrecedence); |
| 171 |
|
| 172 |
void writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c, |
| 173 |
Precedence parentPrecedence); |
| 174 |
|
| 175 |
void writeAnyConstructor(const AnyConstructor& c, Precedence parentPrecedence); |
| 176 |
|
| 177 |
void writeCastConstructor(const AnyConstructor& c, Precedence parentPrecedence); |
| 178 |
|
| 179 |
void writeFieldAccess(const FieldAccess& f); |
| 180 |
|
| 181 |
void writeSwizzle(const Swizzle& swizzle); |
| 182 |
|
| 183 |
void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence); |
| 184 |
|
| 185 |
void writeShortCircuitWorkaroundExpression(const BinaryExpression& b, |
| 186 |
Precedence parentPrecedence); |
| 187 |
|
| 188 |
void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence); |
| 189 |
|
| 190 |
void writeIndexExpression(const IndexExpression& expr); |
| 191 |
|
| 192 |
void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence); |
| 193 |
|
| 194 |
void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence); |
| 195 |
|
| 196 |
void writeLiteral(const Literal& l); |
| 197 |
|
| 198 |
void writeStatement(const Statement& s); |
| 199 |
|
| 200 |
void writeBlock(const Block& b); |
| 201 |
|
| 202 |
void writeIfStatement(const IfStatement& stmt); |
| 203 |
|
| 204 |
void writeForStatement(const ForStatement& f); |
| 205 |
|
| 206 |
void writeDoStatement(const DoStatement& d); |
| 207 |
|
| 208 |
void writeExpressionStatement(const ExpressionStatement& s); |
| 209 |
|
| 210 |
void writeSwitchStatement(const SwitchStatement& s); |
| 211 |
|
| 212 |
void writeReturnStatement(const ReturnStatement& r); |
| 213 |
|
| 214 |
void writeProgramElement(const ProgramElement& e); |
| 215 |
|
| 216 |
bool shouldRewriteVoidTypedFunctions(const FunctionDeclaration* func) const; |
| 217 |
|
| 218 |
StringStream fExtensions; |
| 219 |
StringStream fGlobals; |
| 220 |
StringStream fExtraFunctions; |
| 221 |
std::string fFunctionHeader; |
| 222 |
int fVarCount = 0; |
| 223 |
int fIndentation = 0; |
| 224 |
bool fAtLineStart = false; |
| 225 |
const FunctionDeclaration* fCurrentFunction = nullptr; |
| 226 |
|
| 227 |
// true if we have run into usages of dFdx / dFdy |
| 228 |
bool fFoundDerivatives = false; |
| 229 |
bool fFoundExternalSamplerDecl = false; |
| 230 |
bool fFoundRectSamplerDecl = false; |
| 231 |
bool fSetupClockwise = false; |
| 232 |
bool fSetupFragPosition = false; |
| 233 |
bool fSetupFragCoordWorkaround = false; |
| 234 |
PrettyPrint fPrettyPrint; |
| 235 |
|
| 236 |
// Workaround/polyfill flags |
| 237 |
bool fWrittenAbsEmulation = false; |
| 238 |
bool fWrittenDeterminant2 = false, fWrittenDeterminant3 = false, fWrittenDeterminant4 = false; |
| 239 |
bool fWrittenInverse2 = false, fWrittenInverse3 = false, fWrittenInverse4 = false; |
| 240 |
bool fWrittenTranspose[3][3] = {}; |
| 241 |
}; |
| 242 |
|
| 243 |
void GLSLCodeGenerator::write(std::string_view s) { |
| 244 |
if (s.empty()) { |
| 245 |
return; |
| 246 |
} |
| 247 |
if (fAtLineStart && fPrettyPrint == PrettyPrint::kYes) { |
| 248 |
for (int i = 0; i < fIndentation; i++) { |
| 249 |
fOut->writeText(" "); |
| 250 |
} |
| 251 |
} |
| 252 |
fOut->write(s.data(), s.length()); |
| 253 |
fAtLineStart = false; |
| 254 |
} |
| 255 |
|
| 256 |
void GLSLCodeGenerator::writeLine(std::string_view s) { |
| 257 |
this->write(s); |
| 258 |
fOut->writeText("\n"); |
| 259 |
fAtLineStart = true; |
| 260 |
} |
| 261 |
|
| 262 |
void GLSLCodeGenerator::finishLine() { |
| 263 |
if (!fAtLineStart) { |
| 264 |
this->writeLine(); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
void GLSLCodeGenerator::writeExtension(std::string_view name, bool require) { |
| 269 |
fExtensions.writeText("#extension "); |
| 270 |
fExtensions.write(name.data(), name.length()); |
| 271 |
fExtensions.writeText(require ? " : require\n" : " : enable\n"); |
| 272 |
} |
| 273 |
|
| 274 |
bool GLSLCodeGenerator::usesPrecisionModifiers() const { |
| 275 |
return fCaps.fUsesPrecisionModifiers; |
| 276 |
} |
| 277 |
|
| 278 |
static bool is_reserved_identifier(std::string_view identifier) { |
| 279 |
// This list was taken from https://registry.khronos.org/OpenGL/specs/gl/GLSLangSpec.4.60.pdf |
| 280 |
// in section 3.6, "Keywords." Built-in types, and entries that our parser already recognizes as |
| 281 |
// reserved or otherwise non-identifiers, have been eliminated. |
| 282 |
using ReservedWordSet = THashSet<std::string_view>; |
| 283 |
static const SkNoDestructor<ReservedWordSet> kAllReservedWords(ReservedWordSet{ |
| 284 |
"active", |
| 285 |
"centroid", |
| 286 |
"coherent", |
| 287 |
"common", |
| 288 |
"filter", |
| 289 |
"partition", |
| 290 |
"patch", |
| 291 |
"precise", |
| 292 |
"resource", |
| 293 |
"restrict", |
| 294 |
"shared", |
| 295 |
"smooth", |
| 296 |
"subroutine", |
| 297 |
}); |
| 298 |
|
| 299 |
return kAllReservedWords->contains(identifier); |
| 300 |
} |
| 301 |
|
| 302 |
void GLSLCodeGenerator::writeIdentifier(std::string_view identifier) { |
| 303 |
// GLSL forbids two underscores in a row. |
| 304 |
// If an identifier contains "__" or "_X", replace each "_" in the identifier with "_X". |
| 305 |
if (skstd::contains(identifier, "__") || skstd::contains(identifier, "_X")) { |
| 306 |
for (const char c : identifier) { |
| 307 |
if (c == '_') { |
| 308 |
this->write("_X"); |
| 309 |
} else { |
| 310 |
this->write(std::string_view(&c, 1)); |
| 311 |
} |
| 312 |
} |
| 313 |
} else { |
| 314 |
if (is_reserved_identifier(identifier)) { |
| 315 |
this->write("_skReserved_"); |
| 316 |
} |
| 317 |
this->write(identifier); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
// Returns the name of the type with array dimensions, e.g. `float[2]`. |
| 322 |
std::string GLSLCodeGenerator::getTypeName(const Type& raw) { |
| 323 |
const Type& type = raw.resolve().scalarTypeForLiteral(); |
| 324 |
switch (type.typeKind()) { |
| 325 |
case Type::TypeKind::kVector: { |
| 326 |
const Type& component = type.componentType(); |
| 327 |
std::string result; |
| 328 |
if (component.matches(*fContext.fTypes.fFloat) || |
| 329 |
component.matches(*fContext.fTypes.fHalf)) { |
| 330 |
result = "vec"; |
| 331 |
} |
| 332 |
else if (component.isSigned()) { |
| 333 |
result = "ivec"; |
| 334 |
} |
| 335 |
else if (component.isUnsigned()) { |
| 336 |
result = "uvec"; |
| 337 |
} |
| 338 |
else if (component.matches(*fContext.fTypes.fBool)) { |
| 339 |
result = "bvec"; |
| 340 |
} |
| 341 |
else { |
| 342 |
SK_ABORT("unsupported vector type"); |
| 343 |
} |
| 344 |
result += std::to_string(type.columns()); |
| 345 |
return result; |
| 346 |
} |
| 347 |
case Type::TypeKind::kMatrix: { |
| 348 |
std::string result; |
| 349 |
const Type& component = type.componentType(); |
| 350 |
if (component.matches(*fContext.fTypes.fFloat) || |
| 351 |
component.matches(*fContext.fTypes.fHalf)) { |
| 352 |
result = "mat"; |
| 353 |
} |
| 354 |
else { |
| 355 |
SK_ABORT("unsupported matrix type"); |
| 356 |
} |
| 357 |
result += std::to_string(type.columns()); |
| 358 |
if (type.columns() != type.rows()) { |
| 359 |
result += "x"; |
| 360 |
result += std::to_string(type.rows()); |
| 361 |
} |
| 362 |
return result; |
| 363 |
} |
| 364 |
case Type::TypeKind::kArray: { |
| 365 |
std::string baseTypeName = this->getTypeName(type.componentType()); |
| 366 |
if (type.isUnsizedArray()) { |
| 367 |
return String::printf("%s[]", baseTypeName.c_str()); |
| 368 |
} |
| 369 |
return String::printf("%s[%d]", baseTypeName.c_str(), type.columns()); |
| 370 |
} |
| 371 |
case Type::TypeKind::kScalar: { |
| 372 |
if (type.matches(*fContext.fTypes.fHalf)) { |
| 373 |
return "float"; |
| 374 |
} |
| 375 |
else if (type.matches(*fContext.fTypes.fShort)) { |
| 376 |
return "int"; |
| 377 |
} |
| 378 |
else if (type.matches(*fContext.fTypes.fUShort)) { |
| 379 |
return "uint"; |
| 380 |
} |
| 381 |
|
| 382 |
return std::string(type.name()); |
| 383 |
} |
| 384 |
default: |
| 385 |
return std::string(type.name()); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
void GLSLCodeGenerator::writeStructDefinition(const StructDefinition& s) { |
| 390 |
const Type& type = s.type(); |
| 391 |
this->write("struct "); |
| 392 |
this->writeIdentifier(type.name()); |
| 393 |
this->writeLine(" {"); |
| 394 |
fIndentation++; |
| 395 |
for (const auto& f : type.fields()) { |
| 396 |
this->writeModifiers(f.fLayout, f.fModifierFlags, /*globalContext=*/false); |
| 397 |
this->writeTypePrecision(*f.fType); |
| 398 |
const Type& baseType = f.fType->isArray() ? f.fType->componentType() : *f.fType; |
| 399 |
this->writeType(baseType); |
| 400 |
this->write(" "); |
| 401 |
this->writeIdentifier(f.fName); |
| 402 |
if (f.fType->isArray()) { |
| 403 |
this->write("[" + std::to_string(f.fType->columns()) + "]"); |
| 404 |
} |
| 405 |
this->writeLine(";"); |
| 406 |
} |
| 407 |
fIndentation--; |
| 408 |
this->writeLine("};"); |
| 409 |
} |
| 410 |
|
| 411 |
void GLSLCodeGenerator::writeType(const Type& type) { |
| 412 |
this->writeIdentifier(this->getTypeName(type)); |
| 413 |
} |
| 414 |
|
| 415 |
void GLSLCodeGenerator::writeExpression(const Expression& expr, Precedence parentPrecedence) { |
| 416 |
switch (expr.kind()) { |
| 417 |
case Expression::Kind::kBinary: |
| 418 |
this->writeBinaryExpression(expr.as<BinaryExpression>(), parentPrecedence); |
| 419 |
break; |
| 420 |
case Expression::Kind::kConstructorDiagonalMatrix: |
| 421 |
this->writeConstructorDiagonalMatrix(expr.as<ConstructorDiagonalMatrix>(), |
| 422 |
parentPrecedence); |
| 423 |
break; |
| 424 |
case Expression::Kind::kConstructorArrayCast: |
| 425 |
this->writeExpression(*expr.as<ConstructorArrayCast>().argument(), parentPrecedence); |
| 426 |
break; |
| 427 |
case Expression::Kind::kConstructorCompound: |
| 428 |
this->writeConstructorCompound(expr.as<ConstructorCompound>(), parentPrecedence); |
| 429 |
break; |
| 430 |
case Expression::Kind::kConstructorArray: |
| 431 |
case Expression::Kind::kConstructorMatrixResize: |
| 432 |
case Expression::Kind::kConstructorSplat: |
| 433 |
case Expression::Kind::kConstructorStruct: |
| 434 |
this->writeAnyConstructor(expr.asAnyConstructor(), parentPrecedence); |
| 435 |
break; |
| 436 |
case Expression::Kind::kConstructorScalarCast: |
| 437 |
case Expression::Kind::kConstructorCompoundCast: |
| 438 |
this->writeCastConstructor(expr.asAnyConstructor(), parentPrecedence); |
| 439 |
break; |
| 440 |
case Expression::Kind::kEmpty: |
| 441 |
this->write("false"); |
| 442 |
break; |
| 443 |
case Expression::Kind::kFieldAccess: |
| 444 |
this->writeFieldAccess(expr.as<FieldAccess>()); |
| 445 |
break; |
| 446 |
case Expression::Kind::kFunctionCall: |
| 447 |
this->writeFunctionCall(expr.as<FunctionCall>()); |
| 448 |
break; |
| 449 |
case Expression::Kind::kLiteral: |
| 450 |
this->writeLiteral(expr.as<Literal>()); |
| 451 |
break; |
| 452 |
case Expression::Kind::kPrefix: |
| 453 |
this->writePrefixExpression(expr.as<PrefixExpression>(), parentPrecedence); |
| 454 |
break; |
| 455 |
case Expression::Kind::kPostfix: |
| 456 |
this->writePostfixExpression(expr.as<PostfixExpression>(), parentPrecedence); |
| 457 |
break; |
| 458 |
case Expression::Kind::kSetting: |
| 459 |
this->writeExpression(*expr.as<Setting>().toLiteral(fCaps), parentPrecedence); |
| 460 |
break; |
| 461 |
case Expression::Kind::kSwizzle: |
| 462 |
this->writeSwizzle(expr.as<Swizzle>()); |
| 463 |
break; |
| 464 |
case Expression::Kind::kVariableReference: |
| 465 |
this->writeVariableReference(expr.as<VariableReference>()); |
| 466 |
break; |
| 467 |
case Expression::Kind::kTernary: |
| 468 |
this->writeTernaryExpression(expr.as<TernaryExpression>(), parentPrecedence); |
| 469 |
break; |
| 470 |
case Expression::Kind::kIndex: |
| 471 |
this->writeIndexExpression(expr.as<IndexExpression>()); |
| 472 |
break; |
| 473 |
default: |
| 474 |
SkDEBUGFAILF("unsupported expression: %s", expr.description().c_str()); |
| 475 |
break; |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
static bool is_abs(Expression& expr) { |
| 480 |
return expr.is<FunctionCall>() && |
| 481 |
expr.as<FunctionCall>().function().intrinsicKind() == k_abs_IntrinsicKind; |
| 482 |
} |
| 483 |
|
| 484 |
// turns min(abs(x), y) into ((tmpVar1 = abs(x)) < (tmpVar2 = y) ? tmpVar1 : tmpVar2) to avoid a |
| 485 |
// Tegra3 compiler bug. |
| 486 |
void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherExpr) { |
| 487 |
SkASSERT(!fCaps.fCanUseMinAndAbsTogether); |
| 488 |
std::string tmpVar1 = "minAbsHackVar" + std::to_string(fVarCount++); |
| 489 |
std::string tmpVar2 = "minAbsHackVar" + std::to_string(fVarCount++); |
| 490 |
this->fFunctionHeader += std::string(" ") + this->getTypePrecision(absExpr.type()) + |
| 491 |
this->getTypeName(absExpr.type()) + " " + tmpVar1 + ";\n"; |
| 492 |
this->fFunctionHeader += std::string(" ") + this->getTypePrecision(otherExpr.type()) + |
| 493 |
this->getTypeName(otherExpr.type()) + " " + tmpVar2 + ";\n"; |
| 494 |
this->write("((" + tmpVar1 + " = "); |
| 495 |
this->writeExpression(absExpr, Precedence::kAssignment); |
| 496 |
this->write(") < (" + tmpVar2 + " = "); |
| 497 |
this->writeExpression(otherExpr, Precedence::kAssignment); |
| 498 |
this->write(") ? " + tmpVar1 + " : " + tmpVar2 + ")"); |
| 499 |
} |
| 500 |
|
| 501 |
void GLSLCodeGenerator::writeInverseSqrtHack(const Expression& x) { |
| 502 |
this->write("(1.0 / sqrt("); |
| 503 |
this->writeExpression(x, Precedence::kExpression); |
| 504 |
this->write("))"); |
| 505 |
} |
| 506 |
|
| 507 |
static constexpr char kDeterminant2[] = |
| 508 |
"float _determinant2(mat2 m) {" |
| 509 |
"return m[0].x*m[1].y - m[0].y*m[1].x;" |
| 510 |
"}"; |
| 511 |
|
| 512 |
static constexpr char kDeterminant3[] = |
| 513 |
"float _determinant3(mat3 m) {" |
| 514 |
"float " |
| 515 |
"a00 = m[0].x, a01 = m[0].y, a02 = m[0].z," |
| 516 |
"a10 = m[1].x, a11 = m[1].y, a12 = m[1].z," |
| 517 |
"a20 = m[2].x, a21 = m[2].y, a22 = m[2].z," |
| 518 |
"b01 = a22*a11 - a12*a21," |
| 519 |
"b11 =-a22*a10 + a12*a20," |
| 520 |
"b21 = a21*a10 - a11*a20;" |
| 521 |
"return a00*b01 + a01*b11 + a02*b21;" |
| 522 |
"}"; |
| 523 |
|
| 524 |
static constexpr char kDeterminant4[] = |
| 525 |
"mat4 _determinant4(mat4 m) {" |
| 526 |
"float " |
| 527 |
"a00 = m[0].x, a01 = m[0].y, a02 = m[0].z, a03 = m[0].w," |
| 528 |
"a10 = m[1].x, a11 = m[1].y, a12 = m[1].z, a13 = m[1].w," |
| 529 |
"a20 = m[2].x, a21 = m[2].y, a22 = m[2].z, a23 = m[2].w," |
| 530 |
"a30 = m[3].x, a31 = m[3].y, a32 = m[3].z, a33 = m[3].w," |
| 531 |
"b00 = a00*a11 - a01*a10," |
| 532 |
"b01 = a00*a12 - a02*a10," |
| 533 |
"b02 = a00*a13 - a03*a10," |
| 534 |
"b03 = a01*a12 - a02*a11," |
| 535 |
"b04 = a01*a13 - a03*a11," |
| 536 |
"b05 = a02*a13 - a03*a12," |
| 537 |
"b06 = a20*a31 - a21*a30," |
| 538 |
"b07 = a20*a32 - a22*a30," |
| 539 |
"b08 = a20*a33 - a23*a30," |
| 540 |
"b09 = a21*a32 - a22*a31," |
| 541 |
"b10 = a21*a33 - a23*a31," |
| 542 |
"b11 = a22*a33 - a23*a32;" |
| 543 |
"return b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06;" |
| 544 |
"}"; |
| 545 |
|
| 546 |
void GLSLCodeGenerator::writeDeterminantHack(const Expression& mat) { |
| 547 |
const Type& type = mat.type(); |
| 548 |
if (type.matches(*fContext.fTypes.fFloat2x2) || |
| 549 |
type.matches(*fContext.fTypes.fHalf2x2)) { |
| 550 |
this->write("_determinant2("); |
| 551 |
if (!fWrittenDeterminant2) { |
| 552 |
fWrittenDeterminant2 = true; |
| 553 |
fExtraFunctions.writeText(kDeterminant2); |
| 554 |
} |
| 555 |
} else if (type.matches(*fContext.fTypes.fFloat3x3) || |
| 556 |
type.matches(*fContext.fTypes.fHalf3x3)) { |
| 557 |
this->write("_determinant3("); |
| 558 |
if (!fWrittenDeterminant3) { |
| 559 |
fWrittenDeterminant3 = true; |
| 560 |
fExtraFunctions.writeText(kDeterminant3); |
| 561 |
} |
| 562 |
} else if (type.matches(*fContext.fTypes.fFloat4x4) || |
| 563 |
type.matches(*fContext.fTypes.fHalf4x4)) { |
| 564 |
this->write("_determinant4("); |
| 565 |
if (!fWrittenDeterminant4) { |
| 566 |
fWrittenDeterminant4 = true; |
| 567 |
fExtraFunctions.writeText(kDeterminant4); |
| 568 |
} |
| 569 |
} else { |
| 570 |
SkDEBUGFAILF("no polyfill for determinant(%s)", type.description().c_str()); |
| 571 |
this->write("determinant("); |
| 572 |
} |
| 573 |
this->writeExpression(mat, Precedence::kExpression); |
| 574 |
this->write(")"); |
| 575 |
} |
| 576 |
|
| 577 |
static constexpr char kInverse2[] = |
| 578 |
"mat2 _inverse2(mat2 m) {" |
| 579 |
"return mat2(m[1].y, -m[0].y, -m[1].x, m[0].x) / (m[0].x * m[1].y - m[0].y * m[1].x);" |
| 580 |
"}"; |
| 581 |
|
| 582 |
static constexpr char kInverse3[] = |
| 583 |
"mat3 _inverse3(mat3 m) {" |
| 584 |
"float " |
| 585 |
"a00 = m[0].x, a01 = m[0].y, a02 = m[0].z," |
| 586 |
"a10 = m[1].x, a11 = m[1].y, a12 = m[1].z," |
| 587 |
"a20 = m[2].x, a21 = m[2].y, a22 = m[2].z," |
| 588 |
"b01 = a22*a11 - a12*a21," |
| 589 |
"b11 =-a22*a10 + a12*a20," |
| 590 |
"b21 = a21*a10 - a11*a20," |
| 591 |
"det = a00*b01 + a01*b11 + a02*b21;" |
| 592 |
"return mat3(" |
| 593 |
"b01, (-a22*a01 + a02*a21), ( a12*a01 - a02*a11)," |
| 594 |
"b11, ( a22*a00 - a02*a20), (-a12*a00 + a02*a10)," |
| 595 |
"b21, (-a21*a00 + a01*a20), ( a11*a00 - a01*a10)) / det;" |
| 596 |
"}"; |
| 597 |
|
| 598 |
static constexpr char kInverse4[] = |
| 599 |
"mat4 _inverse4(mat4 m) {" |
| 600 |
"float " |
| 601 |
"a00 = m[0].x, a01 = m[0].y, a02 = m[0].z, a03 = m[0].w," |
| 602 |
"a10 = m[1].x, a11 = m[1].y, a12 = m[1].z, a13 = m[1].w," |
| 603 |
"a20 = m[2].x, a21 = m[2].y, a22 = m[2].z, a23 = m[2].w," |
| 604 |
"a30 = m[3].x, a31 = m[3].y, a32 = m[3].z, a33 = m[3].w," |
| 605 |
"b00 = a00*a11 - a01*a10," |
| 606 |
"b01 = a00*a12 - a02*a10," |
| 607 |
"b02 = a00*a13 - a03*a10," |
| 608 |
"b03 = a01*a12 - a02*a11," |
| 609 |
"b04 = a01*a13 - a03*a11," |
| 610 |
"b05 = a02*a13 - a03*a12," |
| 611 |
"b06 = a20*a31 - a21*a30," |
| 612 |
"b07 = a20*a32 - a22*a30," |
| 613 |
"b08 = a20*a33 - a23*a30," |
| 614 |
"b09 = a21*a32 - a22*a31," |
| 615 |
"b10 = a21*a33 - a23*a31," |
| 616 |
"b11 = a22*a33 - a23*a32," |
| 617 |
"det = b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06;" |
| 618 |
"return mat4(" |
| 619 |
"a11*b11 - a12*b10 + a13*b09," |
| 620 |
"a02*b10 - a01*b11 - a03*b09," |
| 621 |
"a31*b05 - a32*b04 + a33*b03," |
| 622 |
"a22*b04 - a21*b05 - a23*b03," |
| 623 |
"a12*b08 - a10*b11 - a13*b07," |
| 624 |
"a00*b11 - a02*b08 + a03*b07," |
| 625 |
"a32*b02 - a30*b05 - a33*b01," |
| 626 |
"a20*b05 - a22*b02 + a23*b01," |
| 627 |
"a10*b10 - a11*b08 + a13*b06," |
| 628 |
"a01*b08 - a00*b10 - a03*b06," |
| 629 |
"a30*b04 - a31*b02 + a33*b00," |
| 630 |
"a21*b02 - a20*b04 - a23*b00," |
| 631 |
"a11*b07 - a10*b09 - a12*b06," |
| 632 |
"a00*b09 - a01*b07 + a02*b06," |
| 633 |
"a31*b01 - a30*b03 - a32*b00," |
| 634 |
"a20*b03 - a21*b01 + a22*b00) / det;" |
| 635 |
"}"; |
| 636 |
|
| 637 |
void GLSLCodeGenerator::writeInverseHack(const Expression& mat) { |
| 638 |
const Type& type = mat.type(); |
| 639 |
if (type.matches(*fContext.fTypes.fFloat2x2) || type.matches(*fContext.fTypes.fHalf2x2)) { |
| 640 |
this->write("_inverse2("); |
| 641 |
if (!fWrittenInverse2) { |
| 642 |
fWrittenInverse2 = true; |
| 643 |
fExtraFunctions.writeText(kInverse2); |
| 644 |
} |
| 645 |
} else if (type.matches(*fContext.fTypes.fFloat3x3) || |
| 646 |
type.matches(*fContext.fTypes.fHalf3x3)) { |
| 647 |
this->write("_inverse3("); |
| 648 |
if (!fWrittenInverse3) { |
| 649 |
fWrittenInverse3 = true; |
| 650 |
fExtraFunctions.writeText(kInverse3); |
| 651 |
} |
| 652 |
} else if (type.matches(*fContext.fTypes.fFloat4x4) || |
| 653 |
type.matches(*fContext.fTypes.fHalf4x4)) { |
| 654 |
this->write("_inverse4("); |
| 655 |
if (!fWrittenInverse4) { |
| 656 |
fWrittenInverse4 = true; |
| 657 |
fExtraFunctions.writeText(kInverse4); |
| 658 |
} |
| 659 |
} else { |
| 660 |
SkDEBUGFAILF("no polyfill for inverse(%s)", type.description().c_str()); |
| 661 |
this->write("inverse("); |
| 662 |
} |
| 663 |
this->writeExpression(mat, Precedence::kExpression); |
| 664 |
this->write(")"); |
| 665 |
} |
| 666 |
|
| 667 |
void GLSLCodeGenerator::writeTransposeHack(const Expression& mat) { |
| 668 |
const Type& type = mat.type(); |
| 669 |
int c = type.columns(); |
| 670 |
int r = type.rows(); |
| 671 |
std::string name = "transpose" + std::to_string(c) + std::to_string(r); |
| 672 |
|
| 673 |
SkASSERT(c >= 2 && c <= 4); |
| 674 |
SkASSERT(r >= 2 && r <= 4); |
| 675 |
bool* writtenThisTranspose = &fWrittenTranspose[c - 2][r - 2]; |
| 676 |
if (!*writtenThisTranspose) { |
| 677 |
*writtenThisTranspose = true; |
| 678 |
std::string typeName = this->getTypeName(type); |
| 679 |
const Type& base = type.componentType(); |
| 680 |
std::string transposed = this->getTypeName(base.toCompound(fContext, r, c)); |
| 681 |
fExtraFunctions.writeText((transposed + " " + name + "(" + typeName + " m) { return " + |
| 682 |
transposed + "(").c_str()); |
| 683 |
auto separator = SkSL::String::Separator(); |
| 684 |
for (int row = 0; row < r; ++row) { |
| 685 |
for (int column = 0; column < c; ++column) { |
| 686 |
fExtraFunctions.writeText(separator().c_str()); |
| 687 |
fExtraFunctions.writeText(("m[" + std::to_string(column) + "][" + |
| 688 |
std::to_string(row) + "]").c_str()); |
| 689 |
} |
| 690 |
} |
| 691 |
fExtraFunctions.writeText("); }\n"); |
| 692 |
} |
| 693 |
this->write(name + "("); |
| 694 |
this->writeExpression(mat, Precedence::kExpression); |
| 695 |
this->write(")"); |
| 696 |
} |
| 697 |
|
| 698 |
void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
| 699 |
const FunctionDeclaration& function = c.function(); |
| 700 |
const ExpressionArray& arguments = c.arguments(); |
| 701 |
bool isTextureFunctionWithBias = false; |
| 702 |
bool nameWritten = false; |
| 703 |
const char* closingParen = ")"; |
| 704 |
switch (c.function().intrinsicKind()) { |
| 705 |
case k_abs_IntrinsicKind: { |
| 706 |
if (!fCaps.fEmulateAbsIntFunction) |
| 707 |
break; |
| 708 |
SkASSERT(arguments.size() == 1); |
| 709 |
if (!arguments[0]->type().matches(*fContext.fTypes.fInt)) { |
| 710 |
break; |
| 711 |
} |
| 712 |
// abs(int) on Intel OSX is incorrect, so emulate it: |
| 713 |
this->write("_absemulation"); |
| 714 |
nameWritten = true; |
| 715 |
if (!fWrittenAbsEmulation) { |
| 716 |
fWrittenAbsEmulation = true; |
| 717 |
fExtraFunctions.writeText("int _absemulation(int x) { return x * sign(x); }\n"); |
| 718 |
} |
| 719 |
break; |
| 720 |
} |
| 721 |
case k_atan_IntrinsicKind: |
| 722 |
if (fCaps.fMustForceNegatedAtanParamToFloat && |
| 723 |
arguments.size() == 2 && |
| 724 |
arguments[1]->is<PrefixExpression>()) { |
| 725 |
const PrefixExpression& p = arguments[1]->as<PrefixExpression>(); |
| 726 |
if (p.getOperator().kind() == Operator::Kind::MINUS) { |
| 727 |
this->write("atan("); |
| 728 |
this->writeExpression(*arguments[0], Precedence::kSequence); |
| 729 |
this->write(", -1.0 * "); |
| 730 |
this->writeExpression(*p.operand(), Precedence::kMultiplicative); |
| 731 |
this->write(")"); |
| 732 |
return; |
| 733 |
} |
| 734 |
} |
| 735 |
break; |
| 736 |
case k_ldexp_IntrinsicKind: |
| 737 |
if (fCaps.fMustForceNegatedLdexpParamToMultiply && |
| 738 |
arguments.size() == 2 && |
| 739 |
arguments[1]->is<PrefixExpression>()) { |
| 740 |
const PrefixExpression& p = arguments[1]->as<PrefixExpression>(); |
| 741 |
if (p.getOperator().kind() == Operator::Kind::MINUS) { |
| 742 |
this->write("ldexp("); |
| 743 |
this->writeExpression(*arguments[0], Precedence::kSequence); |
| 744 |
this->write(", "); |
| 745 |
this->writeExpression(*p.operand(), Precedence::kMultiplicative); |
| 746 |
this->write(" * -1)"); |
| 747 |
return; |
| 748 |
} |
| 749 |
} |
| 750 |
break; |
| 751 |
case k_dFdy_IntrinsicKind: |
| 752 |
// Flipping Y also negates the Y derivatives. |
| 753 |
closingParen = "))"; |
| 754 |
this->write("("); |
| 755 |
if (!fProgram.fConfig->fSettings.fForceNoRTFlip) { |
| 756 |
this->write(SKSL_RTFLIP_NAME ".y * "); |
| 757 |
} |
| 758 |
this->write("dFdy"); |
| 759 |
nameWritten = true; |
| 760 |
[[fallthrough]]; |
| 761 |
case k_dFdx_IntrinsicKind: |
| 762 |
case k_fwidth_IntrinsicKind: |
| 763 |
if (!fFoundDerivatives && |
| 764 |
fCaps.shaderDerivativeExtensionString()) { |
| 765 |
this->writeExtension(fCaps.shaderDerivativeExtensionString()); |
| 766 |
fFoundDerivatives = true; |
| 767 |
} |
| 768 |
break; |
| 769 |
case k_determinant_IntrinsicKind: |
| 770 |
if (!fCaps.fBuiltinDeterminantSupport) { |
| 771 |
SkASSERT(arguments.size() == 1); |
| 772 |
this->writeDeterminantHack(*arguments[0]); |
| 773 |
return; |
| 774 |
} |
| 775 |
break; |
| 776 |
case k_fma_IntrinsicKind: |
| 777 |
if (!fCaps.fBuiltinFMASupport) { |
| 778 |
SkASSERT(arguments.size() == 3); |
| 779 |
this->write("(("); |
| 780 |
this->writeExpression(*arguments[0], Precedence::kSequence); |
| 781 |
this->write(") * ("); |
| 782 |
this->writeExpression(*arguments[1], Precedence::kSequence); |
| 783 |
this->write(") + ("); |
| 784 |
this->writeExpression(*arguments[2], Precedence::kSequence); |
| 785 |
this->write("))"); |
| 786 |
return; |
| 787 |
} |
| 788 |
break; |
| 789 |
case k_fract_IntrinsicKind: |
| 790 |
if (!fCaps.fCanUseFractForNegativeValues) { |
| 791 |
SkASSERT(arguments.size() == 1); |
| 792 |
this->write("(0.5 - sign("); |
| 793 |
this->writeExpression(*arguments[0], Precedence::kSequence); |
| 794 |
this->write(") * (0.5 - fract(abs("); |
| 795 |
this->writeExpression(*arguments[0], Precedence::kSequence); |
| 796 |
this->write("))))"); |
| 797 |
return; |
| 798 |
} |
| 799 |
break; |
| 800 |
case k_inverse_IntrinsicKind: |
| 801 |
if (fCaps.fGLSLGeneration < SkSL::GLSLGeneration::k140) { |
| 802 |
SkASSERT(arguments.size() == 1); |
| 803 |
this->writeInverseHack(*arguments[0]); |
| 804 |
return; |
| 805 |
} |
| 806 |
break; |
| 807 |
case k_inversesqrt_IntrinsicKind: |
| 808 |
if (fCaps.fGLSLGeneration < SkSL::GLSLGeneration::k130) { |
| 809 |
SkASSERT(arguments.size() == 1); |
| 810 |
this->writeInverseSqrtHack(*arguments[0]); |
| 811 |
return; |
| 812 |
} |
| 813 |
break; |
| 814 |
case k_min_IntrinsicKind: |
| 815 |
if (!fCaps.fCanUseMinAndAbsTogether) { |
| 816 |
SkASSERT(arguments.size() == 2); |
| 817 |
if (is_abs(*arguments[0])) { |
| 818 |
this->writeMinAbsHack(*arguments[0], *arguments[1]); |
| 819 |
return; |
| 820 |
} |
| 821 |
if (is_abs(*arguments[1])) { |
| 822 |
// note that this violates the GLSL left-to-right evaluation semantics. |
| 823 |
// I doubt it will ever end up mattering, but it's worth calling out. |
| 824 |
this->writeMinAbsHack(*arguments[1], *arguments[0]); |
| 825 |
return; |
| 826 |
} |
| 827 |
} |
| 828 |
break; |
| 829 |
case k_pow_IntrinsicKind: |
| 830 |
if (!fCaps.fRemovePowWithConstantExponent) { |
| 831 |
break; |
| 832 |
} |
| 833 |
// pow(x, y) on some NVIDIA drivers causes crashes if y is a constant. |
| 834 |
// It's hard to tell what constitutes "constant" here, so just replace in all cases. |
| 835 |
|
| 836 |
// Change pow(x, y) into exp2(y * log2(x)) |
| 837 |
this->write("exp2("); |
| 838 |
this->writeExpression(*arguments[1], Precedence::kMultiplicative); |
| 839 |
this->write(" * log2("); |
| 840 |
this->writeExpression(*arguments[0], Precedence::kSequence); |
| 841 |
this->write("))"); |
| 842 |
return; |
| 843 |
case k_saturate_IntrinsicKind: |
| 844 |
SkASSERT(arguments.size() == 1); |
| 845 |
this->write("clamp("); |
| 846 |
this->writeExpression(*arguments[0], Precedence::kSequence); |
| 847 |
this->write(", 0.0, 1.0)"); |
| 848 |
return; |
| 849 |
case k_sample_IntrinsicKind: { |
| 850 |
const char* dim = ""; |
| 851 |
bool proj = false; |
| 852 |
const Type& arg0Type = arguments[0]->type(); |
| 853 |
const Type& arg1Type = arguments[1]->type(); |
| 854 |
switch (arg0Type.dimensions()) { |
| 855 |
case SpvDim1D: |
| 856 |
dim = "1D"; |
| 857 |
isTextureFunctionWithBias = true; |
| 858 |
if (arg1Type.matches(*fContext.fTypes.fFloat)) { |
| 859 |
proj = false; |
| 860 |
} else { |
| 861 |
SkASSERT(arg1Type.matches(*fContext.fTypes.fFloat2)); |
| 862 |
proj = true; |
| 863 |
} |
| 864 |
break; |
| 865 |
case SpvDim2D: |
| 866 |
dim = "2D"; |
| 867 |
if (!arg0Type.matches(*fContext.fTypes.fSamplerExternalOES)) { |
| 868 |
isTextureFunctionWithBias = true; |
| 869 |
} |
| 870 |
if (arg1Type.matches(*fContext.fTypes.fFloat2)) { |
| 871 |
proj = false; |
| 872 |
} else { |
| 873 |
SkASSERT(arg1Type.matches(*fContext.fTypes.fFloat3)); |
| 874 |
proj = true; |
| 875 |
} |
| 876 |
break; |
| 877 |
case SpvDim3D: |
| 878 |
dim = "3D"; |
| 879 |
isTextureFunctionWithBias = true; |
| 880 |
if (arg1Type.matches(*fContext.fTypes.fFloat3)) { |
| 881 |
proj = false; |
| 882 |
} else { |
| 883 |
SkASSERT(arg1Type.matches(*fContext.fTypes.fFloat4)); |
| 884 |
proj = true; |
| 885 |
} |
| 886 |
break; |
| 887 |
case SpvDimCube: |
| 888 |
dim = "Cube"; |
| 889 |
isTextureFunctionWithBias = true; |
| 890 |
proj = false; |
| 891 |
break; |
| 892 |
case SpvDimRect: |
| 893 |
dim = "2DRect"; |
| 894 |
proj = false; |
| 895 |
break; |
| 896 |
case SpvDimBuffer: |
| 897 |
SkASSERT(false); // doesn't exist |
| 898 |
dim = "Buffer"; |
| 899 |
proj = false; |
| 900 |
break; |
| 901 |
case SpvDimSubpassData: |
| 902 |
SkASSERT(false); // doesn't exist |
| 903 |
dim = "SubpassData"; |
| 904 |
proj = false; |
| 905 |
break; |
| 906 |
} |
| 907 |
this->write("texture"); |
| 908 |
if (fCaps.fGLSLGeneration < SkSL::GLSLGeneration::k130) { |
| 909 |
this->write(dim); |
| 910 |
} |
| 911 |
if (proj) { |
| 912 |
this->write("Proj"); |
| 913 |
} |
| 914 |
nameWritten = true; |
| 915 |
break; |
| 916 |
} |
| 917 |
case k_sampleGrad_IntrinsicKind: { |
| 918 |
SkASSERT(arguments.size() == 4); |
| 919 |
this->write("textureGrad"); |
| 920 |
nameWritten = true; |
| 921 |
break; |
| 922 |
} |
| 923 |
case k_sampleLod_IntrinsicKind: { |
| 924 |
SkASSERT(arguments.size() == 3); |
| 925 |
this->write("textureLod"); |
| 926 |
nameWritten = true; |
| 927 |
break; |
| 928 |
} |
| 929 |
case k_transpose_IntrinsicKind: |
| 930 |
if (fCaps.fGLSLGeneration < SkSL::GLSLGeneration::k130) { |
| 931 |
SkASSERT(arguments.size() == 1); |
| 932 |
this->writeTransposeHack(*arguments[0]); |
| 933 |
return; |
| 934 |
} |
| 935 |
break; |
| 936 |
default: |
| 937 |
break; |
| 938 |
} |
| 939 |
|
| 940 |
if (!nameWritten) { |
| 941 |
this->writeIdentifier(function.mangledName()); |
| 942 |
} |
| 943 |
this->write("("); |
| 944 |
auto separator = SkSL::String::Separator(); |
| 945 |
for (const auto& arg : arguments) { |
| 946 |
this->write(separator()); |
| 947 |
this->writeExpression(*arg, Precedence::kSequence); |
| 948 |
} |
| 949 |
if (fProgram.fConfig->fSettings.fSharpenTextures && isTextureFunctionWithBias) { |
| 950 |
this->write(String::printf(", %g", kSharpenTexturesBias)); |
| 951 |
} |
| 952 |
this->write(closingParen); |
| 953 |
} |
| 954 |
|
| 955 |
void GLSLCodeGenerator::writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c, |
| 956 |
Precedence parentPrecedence) { |
| 957 |
if (c.type().columns() == 4 && c.type().rows() == 2) { |
| 958 |
// Due to a longstanding bug in glslang and Mesa, several GPU drivers generate diagonal 4x2 |
| 959 |
// matrices incorrectly. (skbug.com/40043085, https://github.com/KhronosGroup/glslang/pull/2646) |
| 960 |
// We can work around this issue by multiplying a scalar by the identity matrix. |
| 961 |
// In practice, this doesn't come up naturally in real code and we don't know every affected |
| 962 |
// driver, so we just apply this workaround everywhere. |
| 963 |
this->write("("); |
| 964 |
this->writeType(c.type()); |
| 965 |
this->write("(1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0) * "); |
| 966 |
this->writeExpression(*c.argument(), Precedence::kMultiplicative); |
| 967 |
this->write(")"); |
| 968 |
return; |
| 969 |
} |
| 970 |
this->writeAnyConstructor(c, parentPrecedence); |
| 971 |
} |
| 972 |
|
| 973 |
void GLSLCodeGenerator::writeConstructorCompound(const ConstructorCompound& c, |
| 974 |
Precedence parentPrecedence) { |
| 975 |
// If this is a 2x2 matrix constructor containing a single argument... |
| 976 |
if (c.type().isMatrix() && c.arguments().size() == 1) { |
| 977 |
// ... and that argument is a vec4... |
| 978 |
const Expression& expr = *c.arguments().front(); |
| 979 |
if (expr.type().isVector() && expr.type().columns() == 4) { |
| 980 |
// ... let's rewrite the cast to dodge issues on very old GPUs. (skbug.com/40043276) |
| 981 |
if (Analysis::IsTrivialExpression(expr)) { |
| 982 |
this->writeType(c.type()); |
| 983 |
this->write("("); |
| 984 |
this->writeExpression(expr, Precedence::kPostfix); |
| 985 |
this->write(".xy, "); |
| 986 |
this->writeExpression(expr, Precedence::kPostfix); |
| 987 |
this->write(".zw)"); |
| 988 |
} else { |
| 989 |
std::string tempVec = "_tempVec" + std::to_string(fVarCount++); |
| 990 |
this->fFunctionHeader += std::string(" ") + this->getTypePrecision(expr.type()) + |
| 991 |
this->getTypeName(expr.type()) + " " + tempVec + ";\n"; |
| 992 |
this->write("(("); |
| 993 |
this->write(tempVec); |
| 994 |
this->write(" = "); |
| 995 |
this->writeExpression(expr, Precedence::kAssignment); |
| 996 |
this->write("), "); |
| 997 |
this->writeType(c.type()); |
| 998 |
this->write("("); |
| 999 |
this->write(tempVec); |
| 1000 |
this->write(".xy, "); |
| 1001 |
this->write(tempVec); |
| 1002 |
this->write(".zw))"); |
| 1003 |
} |
| 1004 |
return; |
| 1005 |
} |
| 1006 |
} |
| 1007 |
this->writeAnyConstructor(c, parentPrecedence); |
| 1008 |
} |
| 1009 |
|
| 1010 |
void GLSLCodeGenerator::writeCastConstructor(const AnyConstructor& c, Precedence parentPrecedence) { |
| 1011 |
const auto arguments = c.argumentSpan(); |
| 1012 |
SkASSERT(arguments.size() == 1); |
| 1013 |
|
| 1014 |
const Expression& argument = *arguments.front(); |
| 1015 |
if ((this->getTypeName(c.type()) == this->getTypeName(argument.type()) || |
| 1016 |
(argument.type().matches(*fContext.fTypes.fFloatLiteral)))) { |
| 1017 |
// In cases like half(float), they're different types as far as SkSL is concerned but |
| 1018 |
// the same type as far as GLSL is concerned. We avoid a redundant float(float) by just |
| 1019 |
// writing out the inner expression here. |
| 1020 |
this->writeExpression(argument, parentPrecedence); |
| 1021 |
return; |
| 1022 |
} |
| 1023 |
|
| 1024 |
// This cast should be emitted as-is. |
| 1025 |
return this->writeAnyConstructor(c, parentPrecedence); |
| 1026 |
} |
| 1027 |
|
| 1028 |
void GLSLCodeGenerator::writeAnyConstructor(const AnyConstructor& c, Precedence parentPrecedence) { |
| 1029 |
this->writeType(c.type()); |
| 1030 |
this->write("("); |
| 1031 |
auto separator = SkSL::String::Separator(); |
| 1032 |
for (const auto& arg : c.argumentSpan()) { |
| 1033 |
this->write(separator()); |
| 1034 |
this->writeExpression(*arg, Precedence::kSequence); |
| 1035 |
} |
| 1036 |
this->write(")"); |
| 1037 |
} |
| 1038 |
|
| 1039 |
void GLSLCodeGenerator::writeFragCoord() { |
| 1040 |
if (!fCaps.fCanUseFragCoord) { |
| 1041 |
if (!fSetupFragCoordWorkaround) { |
| 1042 |
const char* precision = this->usesPrecisionModifiers() ? "highp " : ""; |
| 1043 |
fFunctionHeader += precision; |
| 1044 |
fFunctionHeader += " float sk_FragCoord_InvW = 1. / sk_FragCoord_Workaround.w;\n"; |
| 1045 |
fFunctionHeader += precision; |
| 1046 |
fFunctionHeader += " vec4 sk_FragCoord_Resolved = " |
| 1047 |
"vec4(sk_FragCoord_Workaround.xyz * sk_FragCoord_InvW, sk_FragCoord_InvW);\n"; |
| 1048 |
// Ensure that we get exact .5 values for x and y. |
| 1049 |
fFunctionHeader += " sk_FragCoord_Resolved.xy = floor(sk_FragCoord_Resolved.xy) + " |
| 1050 |
"vec2(.5);\n"; |
| 1051 |
fSetupFragCoordWorkaround = true; |
| 1052 |
} |
| 1053 |
this->write("sk_FragCoord_Resolved"); |
| 1054 |
return; |
| 1055 |
} |
| 1056 |
|
| 1057 |
if (!fSetupFragPosition) { |
| 1058 |
fFunctionHeader += this->usesPrecisionModifiers() ? "highp " : ""; |
| 1059 |
fFunctionHeader += " vec4 sk_FragCoord = vec4(" |
| 1060 |
"gl_FragCoord.x, "; |
| 1061 |
if (fProgram.fConfig->fSettings.fForceNoRTFlip) { |
| 1062 |
fFunctionHeader += "gl_FragCoord.y, "; |
| 1063 |
} else { |
| 1064 |
fFunctionHeader += SKSL_RTFLIP_NAME ".x + " SKSL_RTFLIP_NAME ".y * gl_FragCoord.y, "; |
| 1065 |
} |
| 1066 |
fFunctionHeader += |
| 1067 |
"gl_FragCoord.z, " |
| 1068 |
"gl_FragCoord.w);\n"; |
| 1069 |
fSetupFragPosition = true; |
| 1070 |
} |
| 1071 |
this->write("sk_FragCoord"); |
| 1072 |
} |
| 1073 |
|
| 1074 |
void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) { |
| 1075 |
switch (ref.variable()->layout().fBuiltin) { |
| 1076 |
case SK_FRAGCOLOR_BUILTIN: |
| 1077 |
if (fCaps.mustDeclareFragmentShaderOutput()) { |
| 1078 |
this->write("sk_FragColor"); |
| 1079 |
} else { |
| 1080 |
this->write("gl_FragColor"); |
| 1081 |
} |
| 1082 |
break; |
| 1083 |
case SK_SECONDARYFRAGCOLOR_BUILTIN: |
| 1084 |
if (fCaps.fDualSourceBlendingSupport) { |
| 1085 |
this->write("gl_SecondaryFragColorEXT"); |
| 1086 |
} else { |
| 1087 |
fContext.fErrors->error(ref.position(), "'sk_SecondaryFragColor' not supported"); |
| 1088 |
} |
| 1089 |
break; |
| 1090 |
case SK_FRAGCOORD_BUILTIN: |
| 1091 |
this->writeFragCoord(); |
| 1092 |
break; |
| 1093 |
case SK_CLOCKWISE_BUILTIN: |
| 1094 |
if (!fSetupClockwise) { |
| 1095 |
fFunctionHeader += " bool sk_Clockwise = gl_FrontFacing;\n"; |
| 1096 |
if (!fProgram.fConfig->fSettings.fForceNoRTFlip) { |
| 1097 |
fFunctionHeader += " if (" SKSL_RTFLIP_NAME ".y < 0.0) {\n" |
| 1098 |
" sk_Clockwise = !sk_Clockwise;\n" |
| 1099 |
" }\n"; |
| 1100 |
} |
| 1101 |
fSetupClockwise = true; |
| 1102 |
} |
| 1103 |
this->write("sk_Clockwise"); |
| 1104 |
break; |
| 1105 |
case SK_VERTEXID_BUILTIN: |
| 1106 |
this->write("gl_VertexID"); |
| 1107 |
break; |
| 1108 |
case SK_INSTANCEID_BUILTIN: |
| 1109 |
this->write("gl_InstanceID"); |
| 1110 |
break; |
| 1111 |
case SK_LASTFRAGCOLOR_BUILTIN: |
| 1112 |
if (fCaps.fFBFetchColorName) { |
| 1113 |
this->write(fCaps.fFBFetchColorName); |
| 1114 |
} else { |
| 1115 |
fContext.fErrors->error(ref.position(), "'sk_LastFragColor' not supported"); |
| 1116 |
} |
| 1117 |
break; |
| 1118 |
case SK_SAMPLEMASKIN_BUILTIN: |
| 1119 |
// GLSL defines gl_SampleMaskIn as an array of ints. SkSL defines it as a scalar uint. |
| 1120 |
this->write("uint(gl_SampleMaskIn[0])"); |
| 1121 |
break; |
| 1122 |
case SK_SAMPLEMASK_BUILTIN: |
| 1123 |
// GLSL defines gl_SampleMask as an array of ints. SkSL defines it as a scalar uint. |
| 1124 |
this->write("gl_SampleMask[0]"); |
| 1125 |
break; |
| 1126 |
default: |
| 1127 |
this->writeIdentifier(ref.variable()->mangledName()); |
| 1128 |
break; |
| 1129 |
} |
| 1130 |
} |
| 1131 |
|
| 1132 |
void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) { |
| 1133 |
this->writeExpression(*expr.base(), Precedence::kPostfix); |
| 1134 |
this->write("["); |
| 1135 |
this->writeExpression(*expr.index(), Precedence::kExpression); |
| 1136 |
this->write("]"); |
| 1137 |
} |
| 1138 |
|
| 1139 |
bool is_sk_position(const Expression& expr) { |
| 1140 |
if (!expr.is<FieldAccess>()) { |
| 1141 |
return false; |
| 1142 |
} |
| 1143 |
const FieldAccess& f = expr.as<FieldAccess>(); |
| 1144 |
return f.base()->type().fields()[f.fieldIndex()].fLayout.fBuiltin == SK_POSITION_BUILTIN; |
| 1145 |
} |
| 1146 |
|
| 1147 |
bool is_sk_samplemask(const Expression& expr) { |
| 1148 |
if (!expr.is<VariableReference>()) { |
| 1149 |
return false; |
| 1150 |
} |
| 1151 |
const VariableReference& v = expr.as<VariableReference>(); |
| 1152 |
return v.variable()->layout().fBuiltin == SK_SAMPLEMASK_BUILTIN; |
| 1153 |
} |
| 1154 |
|
| 1155 |
void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) { |
| 1156 |
if (f.ownerKind() == FieldAccess::OwnerKind::kDefault) { |
| 1157 |
this->writeExpression(*f.base(), Precedence::kPostfix); |
| 1158 |
this->write("."); |
| 1159 |
} |
| 1160 |
const Type& baseType = f.base()->type(); |
| 1161 |
int builtin = baseType.fields()[f.fieldIndex()].fLayout.fBuiltin; |
| 1162 |
if (builtin == SK_POSITION_BUILTIN) { |
| 1163 |
this->write("gl_Position"); |
| 1164 |
} else if (builtin == SK_POINTSIZE_BUILTIN) { |
| 1165 |
this->write("gl_PointSize"); |
| 1166 |
} else { |
| 1167 |
this->writeIdentifier(baseType.fields()[f.fieldIndex()].fName); |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
void GLSLCodeGenerator::writeSwizzle(const Swizzle& swizzle) { |
| 1172 |
this->writeExpression(*swizzle.base(), Precedence::kPostfix); |
| 1173 |
this->write("."); |
| 1174 |
this->write(Swizzle::MaskString(swizzle.components())); |
| 1175 |
} |
| 1176 |
|
| 1177 |
void GLSLCodeGenerator::writeMatrixComparisonWorkaround(const BinaryExpression& b) { |
| 1178 |
const Expression& left = *b.left(); |
| 1179 |
const Expression& right = *b.right(); |
| 1180 |
Operator op = b.getOperator(); |
| 1181 |
|
| 1182 |
SkASSERT(op.kind() == Operator::Kind::EQEQ || op.kind() == Operator::Kind::NEQ); |
| 1183 |
SkASSERT(left.type().isMatrix()); |
| 1184 |
SkASSERT(right.type().isMatrix()); |
| 1185 |
|
| 1186 |
std::string tempMatrix1 = "_tempMatrix" + std::to_string(fVarCount++); |
| 1187 |
std::string tempMatrix2 = "_tempMatrix" + std::to_string(fVarCount++); |
| 1188 |
|
| 1189 |
this->fFunctionHeader += std::string(" ") + this->getTypePrecision(left.type()) + |
| 1190 |
this->getTypeName(left.type()) + " " + tempMatrix1 + ";\n " + |
| 1191 |
this->getTypePrecision(right.type()) + |
| 1192 |
this->getTypeName(right.type()) + " " + tempMatrix2 + ";\n"; |
| 1193 |
this->write("((" + tempMatrix1 + " = "); |
| 1194 |
this->writeExpression(left, Precedence::kAssignment); |
| 1195 |
this->write("), (" + tempMatrix2 + " = "); |
| 1196 |
this->writeExpression(right, Precedence::kAssignment); |
| 1197 |
this->write("), (" + tempMatrix1); |
| 1198 |
this->write(op.operatorName()); |
| 1199 |
this->write(tempMatrix2 + "))"); |
| 1200 |
} |
| 1201 |
|
| 1202 |
void GLSLCodeGenerator::writeBinaryExpression(const BinaryExpression& b, |
| 1203 |
Precedence parentPrecedence) { |
| 1204 |
const Expression& left = *b.left(); |
| 1205 |
const Expression& right = *b.right(); |
| 1206 |
Operator op = b.getOperator(); |
| 1207 |
if (fCaps.fUnfoldShortCircuitAsTernary && (op.kind() == Operator::Kind::LOGICALAND || |
| 1208 |
op.kind() == Operator::Kind::LOGICALOR)) { |
| 1209 |
this->writeShortCircuitWorkaroundExpression(b, parentPrecedence); |
| 1210 |
return; |
| 1211 |
} |
| 1212 |
|
| 1213 |
if (fCaps.fRewriteMatrixComparisons && left.type().isMatrix() && |
| 1214 |
right.type().isMatrix() && op.isEquality()) { |
| 1215 |
this->writeMatrixComparisonWorkaround(b); |
| 1216 |
return; |
| 1217 |
} |
| 1218 |
|
| 1219 |
Precedence precedence = op.getBinaryPrecedence(); |
| 1220 |
if (precedence >= parentPrecedence) { |
| 1221 |
this->write("("); |
| 1222 |
} |
| 1223 |
const bool needsPositionWorkaround = ProgramConfig::IsVertex(fProgram.fConfig->fKind) && |
| 1224 |
op.isAssignment() && |
| 1225 |
is_sk_position(left) && |
| 1226 |
!Analysis::ContainsRTAdjust(right) && |
| 1227 |
!fCaps.fCanUseFragCoord; |
| 1228 |
if (needsPositionWorkaround) { |
| 1229 |
this->write("sk_FragCoord_Workaround = ("); |
| 1230 |
} |
| 1231 |
this->writeExpression(left, precedence); |
| 1232 |
this->write(op.operatorName()); |
| 1233 |
|
| 1234 |
const bool isAssignmentToSampleMask = ProgramConfig::IsFragment(fProgram.fConfig->fKind) && |
| 1235 |
op.isAssignment() && |
| 1236 |
is_sk_samplemask(left); |
| 1237 |
if (isAssignmentToSampleMask) { |
| 1238 |
// GLSL defines the sample masks as signed ints; SkSL (and Metal/WebGPU) use unsigned ints. |
| 1239 |
this->write("int("); |
| 1240 |
} |
| 1241 |
this->writeExpression(right, precedence); |
| 1242 |
if (isAssignmentToSampleMask) { |
| 1243 |
this->write(")"); |
| 1244 |
} |
| 1245 |
if (needsPositionWorkaround) { |
| 1246 |
this->write(")"); |
| 1247 |
} |
| 1248 |
if (precedence >= parentPrecedence) { |
| 1249 |
this->write(")"); |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
void GLSLCodeGenerator::writeShortCircuitWorkaroundExpression(const BinaryExpression& b, |
| 1254 |
Precedence parentPrecedence) { |
| 1255 |
if (Precedence::kTernary >= parentPrecedence) { |
| 1256 |
this->write("("); |
| 1257 |
} |
| 1258 |
|
| 1259 |
// Transform: |
| 1260 |
// a && b => a ? b : false |
| 1261 |
// a || b => a ? true : b |
| 1262 |
this->writeExpression(*b.left(), Precedence::kTernary); |
| 1263 |
this->write(" ? "); |
| 1264 |
if (b.getOperator().kind() == Operator::Kind::LOGICALAND) { |
| 1265 |
this->writeExpression(*b.right(), Precedence::kTernary); |
| 1266 |
} else { |
| 1267 |
Literal boolTrue(Position(), /*value=*/1, fContext.fTypes.fBool.get()); |
| 1268 |
this->writeLiteral(boolTrue); |
| 1269 |
} |
| 1270 |
this->write(" : "); |
| 1271 |
if (b.getOperator().kind() == Operator::Kind::LOGICALAND) { |
| 1272 |
Literal boolFalse(Position(), /*value=*/0, fContext.fTypes.fBool.get()); |
| 1273 |
this->writeLiteral(boolFalse); |
| 1274 |
} else { |
| 1275 |
this->writeExpression(*b.right(), Precedence::kTernary); |
| 1276 |
} |
| 1277 |
if (Precedence::kTernary >= parentPrecedence) { |
| 1278 |
this->write(")"); |
| 1279 |
} |
| 1280 |
} |
| 1281 |
|
| 1282 |
void GLSLCodeGenerator::writeTernaryExpression(const TernaryExpression& t, |
| 1283 |
Precedence parentPrecedence) { |
| 1284 |
if (Precedence::kTernary >= parentPrecedence) { |
| 1285 |
this->write("("); |
| 1286 |
} |
| 1287 |
this->writeExpression(*t.test(), Precedence::kTernary); |
| 1288 |
this->write(" ? "); |
| 1289 |
this->writeExpression(*t.ifTrue(), Precedence::kTernary); |
| 1290 |
this->write(" : "); |
| 1291 |
this->writeExpression(*t.ifFalse(), Precedence::kTernary); |
| 1292 |
if (Precedence::kTernary >= parentPrecedence) { |
| 1293 |
this->write(")"); |
| 1294 |
} |
| 1295 |
} |
| 1296 |
|
| 1297 |
void GLSLCodeGenerator::writePrefixExpression(const PrefixExpression& p, |
| 1298 |
Precedence parentPrecedence) { |
| 1299 |
if (Precedence::kPrefix >= parentPrecedence) { |
| 1300 |
this->write("("); |
| 1301 |
} |
| 1302 |
this->write(p.getOperator().tightOperatorName()); |
| 1303 |
this->writeExpression(*p.operand(), Precedence::kPrefix); |
| 1304 |
if (Precedence::kPrefix >= parentPrecedence) { |
| 1305 |
this->write(")"); |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
void GLSLCodeGenerator::writePostfixExpression(const PostfixExpression& p, |
| 1310 |
Precedence parentPrecedence) { |
| 1311 |
if (Precedence::kPostfix >= parentPrecedence) { |
| 1312 |
this->write("("); |
| 1313 |
} |
| 1314 |
this->writeExpression(*p.operand(), Precedence::kPostfix); |
| 1315 |
this->write(p.getOperator().tightOperatorName()); |
| 1316 |
if (Precedence::kPostfix >= parentPrecedence) { |
| 1317 |
this->write(")"); |
| 1318 |
} |
| 1319 |
} |
| 1320 |
|
| 1321 |
void GLSLCodeGenerator::writeLiteral(const Literal& l) { |
| 1322 |
const Type& type = l.type(); |
| 1323 |
if (type.isInteger()) { |
| 1324 |
if (type.matches(*fContext.fTypes.fUInt)) { |
| 1325 |
this->write(std::to_string(l.intValue() & 0xffffffff) + "u"); |
| 1326 |
} else if (type.matches(*fContext.fTypes.fUShort)) { |
| 1327 |
this->write(std::to_string(l.intValue() & 0xffff) + "u"); |
| 1328 |
} else { |
| 1329 |
this->write(std::to_string(l.intValue())); |
| 1330 |
} |
| 1331 |
return; |
| 1332 |
} |
| 1333 |
this->write(l.description(OperatorPrecedence::kExpression)); |
| 1334 |
} |
| 1335 |
|
| 1336 |
bool GLSLCodeGenerator::shouldRewriteVoidTypedFunctions(const FunctionDeclaration* func) const { |
| 1337 |
// We can change void-typed user functions to return a (meaningless) float so that sequence |
| 1338 |
// expressions will work normally in WebGL2. (skbug.com/294893925) |
| 1339 |
return func && |
| 1340 |
!func->isMain() && |
| 1341 |
func->returnType().isVoid() && |
| 1342 |
!fCaps.fCanUseVoidInSequenceExpressions; |
| 1343 |
} |
| 1344 |
|
| 1345 |
void GLSLCodeGenerator::writeFunctionDeclaration(const FunctionDeclaration& f) { |
| 1346 |
if (this->shouldRewriteVoidTypedFunctions(&f)) { |
| 1347 |
this->write("float "); |
| 1348 |
} else { |
| 1349 |
this->writeTypePrecision(f.returnType()); |
| 1350 |
this->writeType(f.returnType()); |
| 1351 |
this->write(" "); |
| 1352 |
} |
| 1353 |
this->writeIdentifier(f.mangledName()); |
| 1354 |
this->write("("); |
| 1355 |
auto separator = SkSL::String::Separator(); |
| 1356 |
for (size_t index = 0; index < f.parameters().size(); ++index) { |
| 1357 |
const Variable* param = f.parameters()[index]; |
| 1358 |
|
| 1359 |
// This is a workaround for our test files. They use the runtime effect signature, so main |
| 1360 |
// takes a coords parameter. We detect these at IR generation time, and we omit them from |
| 1361 |
// the declaration here, so the function is valid GLSL. (Well, valid as long as the |
| 1362 |
// coordinates aren't actually referenced.) |
| 1363 |
if (f.isMain() && param == f.getMainCoordsParameter()) { |
| 1364 |
continue; |
| 1365 |
} |
| 1366 |
this->write(separator()); |
| 1367 |
ModifierFlags flags = param->modifierFlags(); |
| 1368 |
if (fCaps.fRemoveConstFromFunctionParameters) { |
| 1369 |
flags &= ~ModifierFlag::kConst; |
| 1370 |
} |
| 1371 |
this->writeModifiers(param->layout(), flags, /*globalContext=*/false); |
| 1372 |
std::vector<int> sizes; |
| 1373 |
const Type* type = ¶m->type(); |
| 1374 |
if (type->isArray()) { |
| 1375 |
sizes.push_back(type->columns()); |
| 1376 |
type = &type->componentType(); |
| 1377 |
} |
| 1378 |
this->writeTypePrecision(*type); |
| 1379 |
this->writeType(*type); |
| 1380 |
this->write(" "); |
| 1381 |
if (!param->name().empty()) { |
| 1382 |
this->writeIdentifier(param->mangledName()); |
| 1383 |
} else { |
| 1384 |
// By the spec, GLSL does not require function parameters to be named (see |
| 1385 |
// `single_declaration` in the Shading Language Grammar), but some older versions of |
| 1386 |
// GLSL report "formal parameter lacks a name" if a parameter is not named. |
| 1387 |
this->write("_skAnonymousParam"); |
| 1388 |
this->write(std::to_string(index)); |
| 1389 |
} |
| 1390 |
for (int s : sizes) { |
| 1391 |
this->write("["); |
| 1392 |
if (s != Type::kUnsizedArray) { |
| 1393 |
this->write(std::to_string(s)); |
| 1394 |
} |
| 1395 |
this->write("]"); |
| 1396 |
} |
| 1397 |
} |
| 1398 |
this->write(")"); |
| 1399 |
} |
| 1400 |
|
| 1401 |
void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) { |
| 1402 |
fSetupFragPosition = false; |
| 1403 |
fSetupFragCoordWorkaround = false; |
| 1404 |
fSetupClockwise = false; |
| 1405 |
fCurrentFunction = &f.declaration(); |
| 1406 |
|
| 1407 |
this->writeFunctionDeclaration(f.declaration()); |
| 1408 |
this->writeLine(" {"); |
| 1409 |
fIndentation++; |
| 1410 |
|
| 1411 |
fFunctionHeader.clear(); |
| 1412 |
OutputStream* oldOut = fOut; |
| 1413 |
StringStream buffer; |
| 1414 |
fOut = &buffer; |
| 1415 |
for (const std::unique_ptr<Statement>& stmt : f.body()->as<Block>().children()) { |
| 1416 |
if (!stmt->isEmpty()) { |
| 1417 |
this->writeStatement(*stmt); |
| 1418 |
this->finishLine(); |
| 1419 |
} |
| 1420 |
} |
| 1421 |
|
| 1422 |
if (this->shouldRewriteVoidTypedFunctions(&f.declaration())) { |
| 1423 |
// If we can't use void in sequence expressions, we rewrite void-typed user functions to |
| 1424 |
// return a (never-used) float in case they are used in a sequence expression. |
| 1425 |
this->writeLine("return 0.0;"); |
| 1426 |
} |
| 1427 |
|
| 1428 |
fIndentation--; |
| 1429 |
this->writeLine("}"); |
| 1430 |
|
| 1431 |
fOut = oldOut; |
| 1432 |
this->write(fFunctionHeader); |
| 1433 |
this->write(buffer.str()); |
| 1434 |
|
| 1435 |
fCurrentFunction = nullptr; |
| 1436 |
} |
| 1437 |
|
| 1438 |
void GLSLCodeGenerator::writeFunctionPrototype(const FunctionPrototype& f) { |
| 1439 |
this->writeFunctionDeclaration(f.declaration()); |
| 1440 |
this->writeLine(";"); |
| 1441 |
} |
| 1442 |
|
| 1443 |
void GLSLCodeGenerator::writeModifiers(const Layout& layout, |
| 1444 |
ModifierFlags flags, |
| 1445 |
bool globalContext) { |
| 1446 |
this->write(layout.paddedDescription()); |
| 1447 |
|
| 1448 |
// For GLSL 4.1 and below, qualifier-order matters! These are written out in Modifier-bit order. |
| 1449 |
if (flags & ModifierFlag::kFlat) { |
| 1450 |
this->write("flat "); |
| 1451 |
} |
| 1452 |
if (flags & ModifierFlag::kNoPerspective) { |
| 1453 |
this->write("noperspective "); |
| 1454 |
} |
| 1455 |
|
| 1456 |
if (flags.isConst()) { |
| 1457 |
this->write("const "); |
| 1458 |
} |
| 1459 |
if (flags.isUniform()) { |
| 1460 |
this->write("uniform "); |
| 1461 |
} |
| 1462 |
if ((flags & ModifierFlag::kIn) && (flags & ModifierFlag::kOut)) { |
| 1463 |
this->write("inout "); |
| 1464 |
} else if (flags & ModifierFlag::kIn) { |
| 1465 |
if (globalContext && fCaps.fGLSLGeneration < SkSL::GLSLGeneration::k130) { |
| 1466 |
this->write(ProgramConfig::IsVertex(fProgram.fConfig->fKind) ? "attribute " |
| 1467 |
: "varying "); |
| 1468 |
} else { |
| 1469 |
this->write("in "); |
| 1470 |
} |
| 1471 |
} else if (flags & ModifierFlag::kOut) { |
| 1472 |
if (globalContext && fCaps.fGLSLGeneration < SkSL::GLSLGeneration::k130) { |
| 1473 |
this->write("varying "); |
| 1474 |
} else { |
| 1475 |
this->write("out "); |
| 1476 |
} |
| 1477 |
} |
| 1478 |
|
| 1479 |
if (flags.isReadOnly()) { |
| 1480 |
this->write("readonly "); |
| 1481 |
} |
| 1482 |
if (flags.isWriteOnly()) { |
| 1483 |
this->write("writeonly "); |
| 1484 |
} |
| 1485 |
if (flags.isBuffer()) { |
| 1486 |
this->write("buffer "); |
| 1487 |
} |
| 1488 |
} |
| 1489 |
|
| 1490 |
void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { |
| 1491 |
if (intf.typeName() == "sk_PerVertex") { |
| 1492 |
return; |
| 1493 |
} |
| 1494 |
const Type* structType = &intf.var()->type().componentType(); |
| 1495 |
this->writeModifiers(intf.var()->layout(), intf.var()->modifierFlags(), /*globalContext=*/true); |
| 1496 |
this->writeType(*structType); |
| 1497 |
this->writeLine(" {"); |
| 1498 |
fIndentation++; |
| 1499 |
for (const auto& f : structType->fields()) { |
| 1500 |
this->writeModifiers(f.fLayout, f.fModifierFlags, /*globalContext=*/false); |
| 1501 |
this->writeTypePrecision(*f.fType); |
| 1502 |
this->writeType(*f.fType); |
| 1503 |
this->write(" "); |
| 1504 |
this->writeIdentifier(f.fName); |
| 1505 |
this->writeLine(";"); |
| 1506 |
} |
| 1507 |
fIndentation--; |
| 1508 |
this->write("}"); |
| 1509 |
if (!intf.instanceName().empty()) { |
| 1510 |
this->write(" "); |
| 1511 |
this->writeIdentifier(intf.instanceName()); |
| 1512 |
if (intf.arraySize() > 0) { |
| 1513 |
this->write("["); |
| 1514 |
this->write(std::to_string(intf.arraySize())); |
| 1515 |
this->write("]"); |
| 1516 |
} |
| 1517 |
} |
| 1518 |
this->writeLine(";"); |
| 1519 |
} |
| 1520 |
|
| 1521 |
void GLSLCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) { |
| 1522 |
this->writeExpression(value, Precedence::kExpression); |
| 1523 |
} |
| 1524 |
|
| 1525 |
const char* GLSLCodeGenerator::getTypePrecision(const Type& type) { |
| 1526 |
if (this->usesPrecisionModifiers()) { |
| 1527 |
switch (type.typeKind()) { |
| 1528 |
case Type::TypeKind::kScalar: |
| 1529 |
if (type.matches(*fContext.fTypes.fShort) || |
| 1530 |
type.matches(*fContext.fTypes.fUShort) || |
| 1531 |
type.matches(*fContext.fTypes.fHalf)) { |
| 1532 |
return fProgram.fConfig->fSettings.fForceHighPrecision ? "highp " : "mediump "; |
| 1533 |
} |
| 1534 |
if (type.matches(*fContext.fTypes.fFloat) || |
| 1535 |
type.matches(*fContext.fTypes.fInt) || |
| 1536 |
type.matches(*fContext.fTypes.fUInt)) { |
| 1537 |
return "highp "; |
| 1538 |
} |
| 1539 |
return ""; |
| 1540 |
case Type::TypeKind::kVector: // fall through |
| 1541 |
case Type::TypeKind::kMatrix: |
| 1542 |
case Type::TypeKind::kArray: |
| 1543 |
return this->getTypePrecision(type.componentType()); |
| 1544 |
default: |
| 1545 |
break; |
| 1546 |
} |
| 1547 |
} |
| 1548 |
return ""; |
| 1549 |
} |
| 1550 |
|
| 1551 |
void GLSLCodeGenerator::writeTypePrecision(const Type& type) { |
| 1552 |
this->write(this->getTypePrecision(type)); |
| 1553 |
} |
| 1554 |
|
| 1555 |
void GLSLCodeGenerator::writeGlobalVarDeclaration(const GlobalVarDeclaration& e) { |
| 1556 |
const VarDeclaration& decl = e.as<GlobalVarDeclaration>().varDeclaration(); |
| 1557 |
switch (decl.var()->layout().fBuiltin) { |
| 1558 |
case -1: |
| 1559 |
// normal var |
| 1560 |
this->writeVarDeclaration(decl, /*global=*/true); |
| 1561 |
this->finishLine(); |
| 1562 |
break; |
| 1563 |
|
| 1564 |
case SK_FRAGCOLOR_BUILTIN: |
| 1565 |
if (fCaps.mustDeclareFragmentShaderOutput()) { |
| 1566 |
if (fProgram.fConfig->fSettings.fFragColorIsInOut) { |
| 1567 |
this->write("inout "); |
| 1568 |
} else { |
| 1569 |
this->write("out "); |
| 1570 |
} |
| 1571 |
if (this->usesPrecisionModifiers()) { |
| 1572 |
this->write("mediump "); |
| 1573 |
} |
| 1574 |
this->writeLine("vec4 sk_FragColor;"); |
| 1575 |
} |
| 1576 |
break; |
| 1577 |
|
| 1578 |
default: |
| 1579 |
break; |
| 1580 |
} |
| 1581 |
} |
| 1582 |
|
| 1583 |
void GLSLCodeGenerator::writeVarDeclaration(const VarDeclaration& decl, bool global) { |
| 1584 |
const Variable* var = decl.var(); |
| 1585 |
this->writeModifiers(var->layout(), var->modifierFlags(), global); |
| 1586 |
|
| 1587 |
if (global && !var->modifierFlags().isUniform()) { |
| 1588 |
if (decl.baseType().typeKind() == Type::TypeKind::kSampler || |
| 1589 |
decl.baseType().typeKind() == Type::TypeKind::kSeparateSampler || |
| 1590 |
decl.baseType().typeKind() == Type::TypeKind::kTexture) { |
| 1591 |
// We don't require the `uniform` modifier on textures/samplers, but GLSL does. |
| 1592 |
this->write("uniform "); |
| 1593 |
} |
| 1594 |
} |
| 1595 |
|
| 1596 |
this->writeTypePrecision(decl.baseType()); |
| 1597 |
this->writeType(decl.baseType()); |
| 1598 |
this->write(" "); |
| 1599 |
this->writeIdentifier(var->mangledName()); |
| 1600 |
if (decl.arraySize() > 0) { |
| 1601 |
this->write("["); |
| 1602 |
this->write(std::to_string(decl.arraySize())); |
| 1603 |
this->write("]"); |
| 1604 |
} |
| 1605 |
if (decl.value()) { |
| 1606 |
this->write(" = "); |
| 1607 |
this->writeVarInitializer(*var, *decl.value()); |
| 1608 |
} |
| 1609 |
if (!fFoundExternalSamplerDecl && |
| 1610 |
var->type().matches(*fContext.fTypes.fSamplerExternalOES)) { |
| 1611 |
if (!fCaps.fExternalTextureSupport) { |
| 1612 |
fContext.fErrors->error(decl.position(), "external texture support is not enabled"); |
| 1613 |
} else { |
| 1614 |
if (fCaps.externalTextureExtensionString()) { |
| 1615 |
this->writeExtension(fCaps.externalTextureExtensionString()); |
| 1616 |
} |
| 1617 |
if (fCaps.secondExternalTextureExtensionString()) { |
| 1618 |
this->writeExtension(fCaps.secondExternalTextureExtensionString()); |
| 1619 |
} |
| 1620 |
fFoundExternalSamplerDecl = true; |
| 1621 |
} |
| 1622 |
} |
| 1623 |
if (!fFoundRectSamplerDecl && var->type().matches(*fContext.fTypes.fSampler2DRect)) { |
| 1624 |
fFoundRectSamplerDecl = true; |
| 1625 |
} |
| 1626 |
this->write(";"); |
| 1627 |
} |
| 1628 |
|
| 1629 |
void GLSLCodeGenerator::writeStatement(const Statement& s) { |
| 1630 |
switch (s.kind()) { |
| 1631 |
case Statement::Kind::kBlock: |
| 1632 |
this->writeBlock(s.as<Block>()); |
| 1633 |
break; |
| 1634 |
case Statement::Kind::kExpression: |
| 1635 |
this->writeExpressionStatement(s.as<ExpressionStatement>()); |
| 1636 |
break; |
| 1637 |
case Statement::Kind::kReturn: |
| 1638 |
this->writeReturnStatement(s.as<ReturnStatement>()); |
| 1639 |
break; |
| 1640 |
case Statement::Kind::kVarDeclaration: |
| 1641 |
this->writeVarDeclaration(s.as<VarDeclaration>(), /*global=*/false); |
| 1642 |
break; |
| 1643 |
case Statement::Kind::kIf: |
| 1644 |
this->writeIfStatement(s.as<IfStatement>()); |
| 1645 |
break; |
| 1646 |
case Statement::Kind::kFor: |
| 1647 |
this->writeForStatement(s.as<ForStatement>()); |
| 1648 |
break; |
| 1649 |
case Statement::Kind::kDo: |
| 1650 |
this->writeDoStatement(s.as<DoStatement>()); |
| 1651 |
break; |
| 1652 |
case Statement::Kind::kSwitch: |
| 1653 |
this->writeSwitchStatement(s.as<SwitchStatement>()); |
| 1654 |
break; |
| 1655 |
case Statement::Kind::kBreak: |
| 1656 |
this->write("break;"); |
| 1657 |
break; |
| 1658 |
case Statement::Kind::kContinue: |
| 1659 |
this->write("continue;"); |
| 1660 |
break; |
| 1661 |
case Statement::Kind::kDiscard: |
| 1662 |
this->write("discard;"); |
| 1663 |
break; |
| 1664 |
case Statement::Kind::kNop: |
| 1665 |
this->write(";"); |
| 1666 |
break; |
| 1667 |
default: |
| 1668 |
SkDEBUGFAILF("unsupported statement: %s", s.description().c_str()); |
| 1669 |
break; |
| 1670 |
} |
| 1671 |
} |
| 1672 |
|
| 1673 |
void GLSLCodeGenerator::writeBlock(const Block& b) { |
| 1674 |
// Write scope markers if this block is a scope, or if the block is empty (since we need to emit |
| 1675 |
// something here to make the code valid). |
| 1676 |
bool isScope = b.isScope() || b.isEmpty(); |
| 1677 |
if (isScope) { |
| 1678 |
this->writeLine("{"); |
| 1679 |
fIndentation++; |
| 1680 |
} |
| 1681 |
for (const std::unique_ptr<Statement>& stmt : b.children()) { |
| 1682 |
if (!stmt->isEmpty()) { |
| 1683 |
this->writeStatement(*stmt); |
| 1684 |
this->finishLine(); |
| 1685 |
} |
| 1686 |
} |
| 1687 |
if (isScope) { |
| 1688 |
fIndentation--; |
| 1689 |
this->write("}"); |
| 1690 |
} |
| 1691 |
} |
| 1692 |
|
| 1693 |
void GLSLCodeGenerator::writeIfStatement(const IfStatement& stmt) { |
| 1694 |
this->write("if ("); |
| 1695 |
this->writeExpression(*stmt.test(), Precedence::kExpression); |
| 1696 |
this->write(") "); |
| 1697 |
this->writeStatement(*stmt.ifTrue()); |
| 1698 |
if (stmt.ifFalse()) { |
| 1699 |
this->write(" else "); |
| 1700 |
this->writeStatement(*stmt.ifFalse()); |
| 1701 |
} |
| 1702 |
} |
| 1703 |
|
| 1704 |
void GLSLCodeGenerator::writeForStatement(const ForStatement& f) { |
| 1705 |
// Emit loops of the form 'for(;test;)' as 'while(test)', which is probably how they started |
| 1706 |
if (!f.initializer() && f.test() && !f.next()) { |
| 1707 |
this->write("while ("); |
| 1708 |
this->writeExpression(*f.test(), Precedence::kExpression); |
| 1709 |
this->write(") "); |
| 1710 |
this->writeStatement(*f.statement()); |
| 1711 |
return; |
| 1712 |
} |
| 1713 |
|
| 1714 |
this->write("for ("); |
| 1715 |
if (f.initializer() && !f.initializer()->isEmpty()) { |
| 1716 |
this->writeStatement(*f.initializer()); |
| 1717 |
} else { |
| 1718 |
this->write("; "); |
| 1719 |
} |
| 1720 |
if (f.test()) { |
| 1721 |
if (fCaps.fAddAndTrueToLoopCondition) { |
| 1722 |
this->write("("); |
| 1723 |
this->writeExpression(*f.test(), Precedence::kLogicalAnd); |
| 1724 |
this->write(" && true)"); |
| 1725 |
} else { |
| 1726 |
this->writeExpression(*f.test(), Precedence::kExpression); |
| 1727 |
} |
| 1728 |
} |
| 1729 |
this->write("; "); |
| 1730 |
if (f.next()) { |
| 1731 |
this->writeExpression(*f.next(), Precedence::kExpression); |
| 1732 |
} |
| 1733 |
this->write(") "); |
| 1734 |
this->writeStatement(*f.statement()); |
| 1735 |
} |
| 1736 |
|
| 1737 |
void GLSLCodeGenerator::writeDoStatement(const DoStatement& d) { |
| 1738 |
if (!fCaps.fRewriteDoWhileLoops) { |
| 1739 |
this->write("do "); |
| 1740 |
this->writeStatement(*d.statement()); |
| 1741 |
this->write(" while ("); |
| 1742 |
this->writeExpression(*d.test(), Precedence::kExpression); |
| 1743 |
this->write(");"); |
| 1744 |
return; |
| 1745 |
} |
| 1746 |
|
| 1747 |
// Otherwise, do the do while loop workaround, to rewrite loops of the form: |
| 1748 |
// do { |
| 1749 |
// CODE; |
| 1750 |
// } while (CONDITION) |
| 1751 |
// |
| 1752 |
// to loops of the form |
| 1753 |
// bool temp = false; |
| 1754 |
// while (true) { |
| 1755 |
// if (temp) { |
| 1756 |
// if (!CONDITION) { |
| 1757 |
// break; |
| 1758 |
// } |
| 1759 |
// } |
| 1760 |
// temp = true; |
| 1761 |
// CODE; |
| 1762 |
// } |
| 1763 |
std::string tmpVar = "_tmpLoopSeenOnce" + std::to_string(fVarCount++); |
| 1764 |
this->write("bool "); |
| 1765 |
this->write(tmpVar); |
| 1766 |
this->writeLine(" = false;"); |
| 1767 |
this->writeLine("while (true) {"); |
| 1768 |
fIndentation++; |
| 1769 |
this->write("if ("); |
| 1770 |
this->write(tmpVar); |
| 1771 |
this->writeLine(") {"); |
| 1772 |
fIndentation++; |
| 1773 |
this->write("if (!"); |
| 1774 |
this->writeExpression(*d.test(), Precedence::kPrefix); |
| 1775 |
this->writeLine(") {"); |
| 1776 |
fIndentation++; |
| 1777 |
this->writeLine("break;"); |
| 1778 |
fIndentation--; |
| 1779 |
this->writeLine("}"); |
| 1780 |
fIndentation--; |
| 1781 |
this->writeLine("}"); |
| 1782 |
this->write(tmpVar); |
| 1783 |
this->writeLine(" = true;"); |
| 1784 |
this->writeStatement(*d.statement()); |
| 1785 |
this->finishLine(); |
| 1786 |
fIndentation--; |
| 1787 |
this->write("}"); |
| 1788 |
} |
| 1789 |
|
| 1790 |
void GLSLCodeGenerator::writeExpressionStatement(const ExpressionStatement& s) { |
| 1791 |
if (fProgram.fConfig->fSettings.fOptimize && !Analysis::HasSideEffects(*s.expression())) { |
| 1792 |
// Don't emit dead expressions. |
| 1793 |
return; |
| 1794 |
} |
| 1795 |
this->writeExpression(*s.expression(), Precedence::kStatement); |
| 1796 |
this->write(";"); |
| 1797 |
} |
| 1798 |
|
| 1799 |
void GLSLCodeGenerator::writeSwitchStatement(const SwitchStatement& s) { |
| 1800 |
if (fCaps.fRewriteSwitchStatements) { |
| 1801 |
std::string fallthroughVar = "_tmpSwitchFallthrough" + std::to_string(fVarCount++); |
| 1802 |
std::string valueVar = "_tmpSwitchValue" + std::to_string(fVarCount++); |
| 1803 |
std::string loopVar = "_tmpSwitchLoop" + std::to_string(fVarCount++); |
| 1804 |
this->write("int "); |
| 1805 |
this->write(valueVar); |
| 1806 |
this->write(" = "); |
| 1807 |
this->writeExpression(*s.value(), Precedence::kAssignment); |
| 1808 |
this->write(", "); |
| 1809 |
this->write(fallthroughVar); |
| 1810 |
this->writeLine(" = 0;"); |
| 1811 |
this->write("for (int "); |
| 1812 |
this->write(loopVar); |
| 1813 |
this->write(" = 0; "); |
| 1814 |
this->write(loopVar); |
| 1815 |
this->write(" < 1; "); |
| 1816 |
this->write(loopVar); |
| 1817 |
this->writeLine("++) {"); |
| 1818 |
fIndentation++; |
| 1819 |
|
| 1820 |
bool firstCase = true; |
| 1821 |
for (const std::unique_ptr<Statement>& stmt : s.cases()) { |
| 1822 |
const SwitchCase& c = stmt->as<SwitchCase>(); |
| 1823 |
if (!c.isDefault()) { |
| 1824 |
this->write("if (("); |
| 1825 |
if (firstCase) { |
| 1826 |
firstCase = false; |
| 1827 |
} else { |
| 1828 |
this->write(fallthroughVar); |
| 1829 |
this->write(" > 0) || ("); |
| 1830 |
} |
| 1831 |
this->write(valueVar); |
| 1832 |
this->write(" == "); |
| 1833 |
this->write(std::to_string(c.value())); |
| 1834 |
this->writeLine(")) {"); |
| 1835 |
fIndentation++; |
| 1836 |
|
| 1837 |
// We write the entire case-block statement here, and then set `switchFallthrough` |
| 1838 |
// to 1. If the case-block had a break statement in it, we break out of the outer |
| 1839 |
// for-loop entirely, meaning the `switchFallthrough` assignment never occurs, nor |
| 1840 |
// does any code after it inside the switch. We've forbidden `continue` statements |
| 1841 |
// inside switch case-blocks entirely, so we don't need to consider their effect on |
| 1842 |
// control flow; see the Finalizer in FunctionDefinition::Convert. |
| 1843 |
this->writeStatement(*c.statement()); |
| 1844 |
this->finishLine(); |
| 1845 |
this->write(fallthroughVar); |
| 1846 |
this->write(" = 1;"); |
| 1847 |
this->writeLine(); |
| 1848 |
|
| 1849 |
fIndentation--; |
| 1850 |
this->writeLine("}"); |
| 1851 |
} else { |
| 1852 |
// This is the default case. Since it's always last, we can just dump in the code. |
| 1853 |
this->writeStatement(*c.statement()); |
| 1854 |
this->finishLine(); |
| 1855 |
} |
| 1856 |
} |
| 1857 |
|
| 1858 |
fIndentation--; |
| 1859 |
this->writeLine("}"); |
| 1860 |
return; |
| 1861 |
} |
| 1862 |
|
| 1863 |
this->write("switch ("); |
| 1864 |
this->writeExpression(*s.value(), Precedence::kExpression); |
| 1865 |
this->writeLine(") {"); |
| 1866 |
fIndentation++; |
| 1867 |
// If a switch contains only a `default` case and nothing else, this confuses some drivers and |
| 1868 |
// can lead to a crash. Adding a real case before the default seems to work around the bug, |
| 1869 |
// and doesn't change the meaning of the switch. (skbug.com/40043548) |
| 1870 |
if (s.cases().size() == 1 && s.cases().front()->as<SwitchCase>().isDefault()) { |
| 1871 |
this->writeLine("case 0:"); |
| 1872 |
} |
| 1873 |
|
| 1874 |
// The GLSL spec insists that the last case in a switch statement must have an associated |
| 1875 |
// statement. In practice, the Apple GLSL compiler crashes if that statement is a no-op, such as |
| 1876 |
// a semicolon or an empty brace pair. (This is filed as FB11992149.) It also crashes if we put |
| 1877 |
// two `break` statements in a row. To work around this while honoring the rules of the |
| 1878 |
// standard, we inject an extra break if and only if the last switch-case block is empty. |
| 1879 |
bool foundEmptyCase = false; |
| 1880 |
|
| 1881 |
for (const std::unique_ptr<Statement>& stmt : s.cases()) { |
| 1882 |
const SwitchCase& c = stmt->as<SwitchCase>(); |
| 1883 |
if (c.isDefault()) { |
| 1884 |
this->writeLine("default:"); |
| 1885 |
} else { |
| 1886 |
this->write("case "); |
| 1887 |
this->write(std::to_string(c.value())); |
| 1888 |
this->writeLine(":"); |
| 1889 |
} |
| 1890 |
if (c.statement()->isEmpty()) { |
| 1891 |
foundEmptyCase = true; |
| 1892 |
} else { |
| 1893 |
foundEmptyCase = false; |
| 1894 |
fIndentation++; |
| 1895 |
this->writeStatement(*c.statement()); |
| 1896 |
this->finishLine(); |
| 1897 |
fIndentation--; |
| 1898 |
} |
| 1899 |
} |
| 1900 |
if (foundEmptyCase) { |
| 1901 |
fIndentation++; |
| 1902 |
this->writeLine("break;"); |
| 1903 |
fIndentation--; |
| 1904 |
} |
| 1905 |
fIndentation--; |
| 1906 |
this->finishLine(); |
| 1907 |
this->write("}"); |
| 1908 |
} |
| 1909 |
|
| 1910 |
void GLSLCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
| 1911 |
SkASSERT(fCurrentFunction); |
| 1912 |
|
| 1913 |
this->write("return"); |
| 1914 |
if (r.expression()) { |
| 1915 |
this->write(" "); |
| 1916 |
this->writeExpression(*r.expression(), Precedence::kExpression); |
| 1917 |
} else if (this->shouldRewriteVoidTypedFunctions(fCurrentFunction)) { |
| 1918 |
// We need to rewrite `return` statements to say `return 0.0` since we are converting |
| 1919 |
// void-typed functions to return floats instead. |
| 1920 |
this->write(" 0.0"); |
| 1921 |
} |
| 1922 |
this->write(";"); |
| 1923 |
} |
| 1924 |
|
| 1925 |
void GLSLCodeGenerator::writeHeader() { |
| 1926 |
if (fCaps.fVersionDeclString) { |
| 1927 |
this->write(fCaps.fVersionDeclString); |
| 1928 |
this->finishLine(); |
| 1929 |
} |
| 1930 |
} |
| 1931 |
|
| 1932 |
void GLSLCodeGenerator::writeProgramElement(const ProgramElement& e) { |
| 1933 |
switch (e.kind()) { |
| 1934 |
case ProgramElement::Kind::kExtension: |
| 1935 |
this->writeExtension(e.as<Extension>().name()); |
| 1936 |
break; |
| 1937 |
|
| 1938 |
case ProgramElement::Kind::kGlobalVar: |
| 1939 |
this->writeGlobalVarDeclaration(e.as<GlobalVarDeclaration>()); |
| 1940 |
break; |
| 1941 |
|
| 1942 |
case ProgramElement::Kind::kInterfaceBlock: |
| 1943 |
this->writeInterfaceBlock(e.as<InterfaceBlock>()); |
| 1944 |
break; |
| 1945 |
|
| 1946 |
case ProgramElement::Kind::kFunction: |
| 1947 |
this->writeFunction(e.as<FunctionDefinition>()); |
| 1948 |
break; |
| 1949 |
|
| 1950 |
case ProgramElement::Kind::kFunctionPrototype: |
| 1951 |
this->writeFunctionPrototype(e.as<FunctionPrototype>()); |
| 1952 |
break; |
| 1953 |
|
| 1954 |
case ProgramElement::Kind::kModifiers: { |
| 1955 |
const ModifiersDeclaration& d = e.as<ModifiersDeclaration>(); |
| 1956 |
this->writeModifiers(d.layout(), d.modifierFlags(), /*globalContext=*/true); |
| 1957 |
this->writeLine(";"); |
| 1958 |
break; |
| 1959 |
} |
| 1960 |
case ProgramElement::Kind::kStructDefinition: |
| 1961 |
this->writeStructDefinition(e.as<StructDefinition>()); |
| 1962 |
break; |
| 1963 |
|
| 1964 |
default: |
| 1965 |
SkDEBUGFAILF("unsupported program element %s\n", e.description().c_str()); |
| 1966 |
break; |
| 1967 |
} |
| 1968 |
} |
| 1969 |
|
| 1970 |
void GLSLCodeGenerator::writeInputVars() { |
| 1971 |
// If we are using sk_FragCoordWorkaround, we don't need to apply RTFlip to gl_FragCoord. |
| 1972 |
uint8_t useRTFlipUniform = fProgram.fInterface.fRTFlipUniform; |
| 1973 |
if (!fCaps.fCanUseFragCoord) { |
| 1974 |
useRTFlipUniform &= ~Program::Interface::kRTFlip_FragCoord; |
| 1975 |
} |
| 1976 |
|
| 1977 |
if (useRTFlipUniform != Program::Interface::kRTFlip_None) { |
| 1978 |
const char* precision = this->usesPrecisionModifiers() ? "highp " : ""; |
| 1979 |
fGlobals.writeText("uniform "); |
| 1980 |
fGlobals.writeText(precision); |
| 1981 |
fGlobals.writeText("vec2 " SKSL_RTFLIP_NAME ";\n"); |
| 1982 |
} |
| 1983 |
} |
| 1984 |
|
| 1985 |
bool GLSLCodeGenerator::generateCode() { |
| 1986 |
this->writeHeader(); |
| 1987 |
OutputStream* rawOut = fOut; |
| 1988 |
StringStream body; |
| 1989 |
fOut = &body; |
| 1990 |
// Write all the program elements except for functions. |
| 1991 |
for (const ProgramElement* e : fProgram.elements()) { |
| 1992 |
if (!e->is<FunctionDefinition>()) { |
| 1993 |
this->writeProgramElement(*e); |
| 1994 |
} |
| 1995 |
} |
| 1996 |
// Emit prototypes for every built-in function; these aren't always added in perfect order. |
| 1997 |
for (const ProgramElement* e : fProgram.fSharedElements) { |
| 1998 |
if (e->is<FunctionDefinition>()) { |
| 1999 |
this->writeFunctionDeclaration(e->as<FunctionDefinition>().declaration()); |
| 2000 |
this->writeLine(";"); |
| 2001 |
} |
| 2002 |
} |
| 2003 |
// Write the functions last. |
| 2004 |
// Why don't we write things in their original order? Because the Inliner likes to move function |
| 2005 |
// bodies around. After inlining, code can inadvertently move upwards, above ProgramElements |
| 2006 |
// that the code relies on. |
| 2007 |
for (const ProgramElement* e : fProgram.elements()) { |
| 2008 |
if (e->is<FunctionDefinition>()) { |
| 2009 |
this->writeProgramElement(*e); |
| 2010 |
} |
| 2011 |
} |
| 2012 |
fOut = rawOut; |
| 2013 |
|
| 2014 |
write_stringstream(fExtensions, *rawOut); |
| 2015 |
this->writeInputVars(); |
| 2016 |
write_stringstream(fGlobals, *rawOut); |
| 2017 |
|
| 2018 |
if (!fCaps.fCanUseFragCoord) { |
| 2019 |
Layout layout; |
| 2020 |
if (ProgramConfig::IsVertex(fProgram.fConfig->fKind)) { |
| 2021 |
this->writeModifiers(layout, ModifierFlag::kOut, /*globalContext=*/true); |
| 2022 |
if (this->usesPrecisionModifiers()) { |
| 2023 |
this->write("highp "); |
| 2024 |
} |
| 2025 |
this->write("vec4 sk_FragCoord_Workaround;\n"); |
| 2026 |
} else if (ProgramConfig::IsFragment(fProgram.fConfig->fKind)) { |
| 2027 |
this->writeModifiers(layout, ModifierFlag::kIn, /*globalContext=*/true); |
| 2028 |
if (this->usesPrecisionModifiers()) { |
| 2029 |
this->write("highp "); |
| 2030 |
} |
| 2031 |
this->write("vec4 sk_FragCoord_Workaround;\n"); |
| 2032 |
} |
| 2033 |
} |
| 2034 |
|
| 2035 |
if (this->usesPrecisionModifiers()) { |
| 2036 |
const char* precision = |
| 2037 |
fProgram.fConfig->fSettings.fForceHighPrecision ? "highp" : "mediump"; |
| 2038 |
this->write(String::printf("precision %s float;\n", precision)); |
| 2039 |
this->write(String::printf("precision %s sampler2D;\n", precision)); |
| 2040 |
if (fFoundExternalSamplerDecl && !fCaps.fNoDefaultPrecisionForExternalSamplers) { |
| 2041 |
this->write(String::printf("precision %s samplerExternalOES;\n", precision)); |
| 2042 |
} |
| 2043 |
if (fFoundRectSamplerDecl) { |
| 2044 |
this->write(String::printf("precision %s sampler2DRect;\n", precision)); |
| 2045 |
} |
| 2046 |
} |
| 2047 |
write_stringstream(fExtraFunctions, *rawOut); |
| 2048 |
write_stringstream(body, *rawOut); |
| 2049 |
return fContext.fErrors->errorCount() == 0; |
| 2050 |
} |
| 2051 |
|
| 2052 |
bool ToGLSL(Program& program, const ShaderCaps* caps, OutputStream& out, PrettyPrint pp) { |
| 2053 |
TRACE_EVENT0("skia.shaders", "SkSL::ToGLSL"); |
| 2054 |
SkASSERT(caps != nullptr); |
| 2055 |
|
| 2056 |
program.fContext->fErrors->setSource(*program.fSource); |
| 2057 |
GLSLCodeGenerator cg(program.fContext.get(), caps, &program, &out, pp); |
| 2058 |
bool result = cg.generateCode(); |
| 2059 |
program.fContext->fErrors->setSource(std::string_view()); |
| 2060 |
|
| 2061 |
return result; |
| 2062 |
} |
| 2063 |
|
| 2064 |
bool ToGLSL(Program& program, const ShaderCaps* caps, OutputStream& out) { |
| 2065 |
#if defined(SK_DEBUG) |
| 2066 |
constexpr PrettyPrint defaultPrintOpts = PrettyPrint::kYes; |
| 2067 |
#else |
| 2068 |
constexpr PrettyPrint defaultPrintOpts = PrettyPrint::kNo; |
| 2069 |
#endif |
| 2070 |
return ToGLSL(program, caps, out, defaultPrintOpts); |
| 2071 |
} |
| 2072 |
|
| 2073 |
bool ToGLSL(Program& program, const ShaderCaps* caps, NativeShader* out) { |
| 2074 |
StringStream buffer; |
| 2075 |
if (!ToGLSL(program, caps, buffer)) { |
| 2076 |
return false; |
| 2077 |
} |
| 2078 |
out->fText = buffer.str(); |
| 2079 |
return true; |
| 2080 |
} |
| 2081 |
|
| 2082 |
} // namespace SkSL |