| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkSLGLSLCodeGenerator.h" | 8 #include "SkSLGLSLCodeGenerator.h" |
| 9 | 9 |
| 10 #include "string.h" | 10 #include "string.h" |
| 11 | 11 |
| 12 #include "GLSL.std.450.h" | 12 #include "GLSL.std.450.h" |
| 13 | 13 |
| 14 #include "ir/SkSLExpressionStatement.h" | 14 #include "ir/SkSLExpressionStatement.h" |
| 15 #include "ir/SkSLExtension.h" | 15 #include "ir/SkSLExtension.h" |
| 16 #include "ir/SkSLIndexExpression.h" | 16 #include "ir/SkSLIndexExpression.h" |
| 17 #include "ir/SkSLVariableReference.h" | 17 #include "ir/SkSLVariableReference.h" |
| 18 | 18 |
| 19 #define SK_FRAGCOLOR_BUILTIN 10001 | |
| 20 | |
| 21 namespace SkSL { | 19 namespace SkSL { |
| 22 | 20 |
| 23 void GLSLCodeGenerator::write(const char* s) { | 21 void GLSLCodeGenerator::write(const char* s) { |
| 24 if (s[0] == 0) { | 22 if (s[0] == 0) { |
| 25 return; | 23 return; |
| 26 } | 24 } |
| 27 if (fAtLineStart) { | 25 if (fAtLineStart) { |
| 28 for (int i = 0; i < fIndentation; i++) { | 26 for (int i = 0; i < fIndentation; i++) { |
| 29 *fOut << " "; | 27 *fOut << " "; |
| 30 } | 28 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 61 if (*search == type) { | 59 if (*search == type) { |
| 62 // already written | 60 // already written |
| 63 this->write(type.name()); | 61 this->write(type.name()); |
| 64 return; | 62 return; |
| 65 } | 63 } |
| 66 } | 64 } |
| 67 fWrittenStructs.push_back(&type); | 65 fWrittenStructs.push_back(&type); |
| 68 this->writeLine("struct " + type.name() + " {"); | 66 this->writeLine("struct " + type.name() + " {"); |
| 69 fIndentation++; | 67 fIndentation++; |
| 70 for (const auto& f : type.fields()) { | 68 for (const auto& f : type.fields()) { |
| 71 this->writeModifiers(f.fModifiers, false); | 69 this->writeModifiers(f.fModifiers); |
| 72 // sizes (which must be static in structs) are part of the type name
here | 70 // sizes (which must be static in structs) are part of the type name
here |
| 73 this->writeType(*f.fType); | 71 this->writeType(*f.fType); |
| 74 this->writeLine(" " + f.fName + ";"); | 72 this->writeLine(" " + f.fName + ";"); |
| 75 } | 73 } |
| 76 fIndentation--; | 74 fIndentation--; |
| 77 this->writeLine("}"); | 75 this->writeLine("}"); |
| 78 } else { | 76 } else { |
| 79 this->write(type.name()); | 77 this->write(type.name()); |
| 80 } | 78 } |
| 81 } | 79 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 this->writeTernaryExpression((TernaryExpression&) expr, parentPreced
ence); | 117 this->writeTernaryExpression((TernaryExpression&) expr, parentPreced
ence); |
| 120 break; | 118 break; |
| 121 case Expression::kIndex_Kind: | 119 case Expression::kIndex_Kind: |
| 122 this->writeIndexExpression((IndexExpression&) expr); | 120 this->writeIndexExpression((IndexExpression&) expr); |
| 123 break; | 121 break; |
| 124 default: | 122 default: |
| 125 ABORT("unsupported expression: %s", expr.description().c_str()); | 123 ABORT("unsupported expression: %s", expr.description().c_str()); |
| 126 } | 124 } |
| 127 } | 125 } |
| 128 | 126 |
| 129 static bool is_abs(Expression& expr) { | |
| 130 if (expr.fKind != Expression::kFunctionCall_Kind) { | |
| 131 return false; | |
| 132 } | |
| 133 return ((FunctionCall&) expr).fFunction.fName == "abs"; | |
| 134 } | |
| 135 | |
| 136 // turns min(abs(x), y) into (abs(x) > (tmpVar = y) ? tmpVar : abs(x)) to avoid
a Tegra3 compiler | |
| 137 // bug. | |
| 138 void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherEx
pr) { | |
| 139 ASSERT(!fCaps.fCanUseMinAndAbsTogether); | |
| 140 std::string varName = "minAbsHackVar" + to_string(fVarCount++); | |
| 141 this->fFunctionHeader += " " + otherExpr.fType.name() + " " + varName + "
;\n"; | |
| 142 this->write("("); | |
| 143 this->writeExpression(absExpr, kTopLevel_Precedence); | |
| 144 this->write(" > (" + varName + " = "); | |
| 145 this->writeExpression(otherExpr, kRelational_Precedence); | |
| 146 this->write(") ? " + varName + " : "); | |
| 147 this->writeExpression(absExpr, kTernary_Precedence); | |
| 148 this->write(")"); | |
| 149 } | |
| 150 | |
| 151 void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { | 127 void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
| 152 if (!fCaps.fCanUseMinAndAbsTogether && c.fFunction.fName == "min") { | |
| 153 ASSERT(c.fArguments.size() == 2); | |
| 154 if (is_abs(*c.fArguments[0])) { | |
| 155 this->writeMinAbsHack(*c.fArguments[0], *c.fArguments[1]); | |
| 156 return; | |
| 157 } | |
| 158 if (is_abs(*c.fArguments[1])) { | |
| 159 this->writeMinAbsHack(*c.fArguments[1], *c.fArguments[0]); | |
| 160 return; | |
| 161 } | |
| 162 } | |
| 163 this->write(c.fFunction.fName + "("); | 128 this->write(c.fFunction.fName + "("); |
| 164 const char* separator = ""; | 129 const char* separator = ""; |
| 165 for (const auto& arg : c.fArguments) { | 130 for (const auto& arg : c.fArguments) { |
| 166 this->write(separator); | 131 this->write(separator); |
| 167 separator = ", "; | 132 separator = ", "; |
| 168 this->writeExpression(*arg, kSequence_Precedence); | 133 this->writeExpression(*arg, kSequence_Precedence); |
| 169 } | 134 } |
| 170 this->write(")"); | 135 this->write(")"); |
| 171 } | 136 } |
| 172 | 137 |
| 173 void GLSLCodeGenerator::writeConstructor(const Constructor& c) { | 138 void GLSLCodeGenerator::writeConstructor(const Constructor& c) { |
| 174 this->write(c.fType.name() + "("); | 139 this->write(c.fType.name() + "("); |
| 175 const char* separator = ""; | 140 const char* separator = ""; |
| 176 for (const auto& arg : c.fArguments) { | 141 for (const auto& arg : c.fArguments) { |
| 177 this->write(separator); | 142 this->write(separator); |
| 178 separator = ", "; | 143 separator = ", "; |
| 179 this->writeExpression(*arg, kSequence_Precedence); | 144 this->writeExpression(*arg, kSequence_Precedence); |
| 180 } | 145 } |
| 181 this->write(")"); | 146 this->write(")"); |
| 182 } | 147 } |
| 183 | 148 |
| 184 void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) { | 149 void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) { |
| 185 if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN) { | 150 this->write(ref.fVariable.fName); |
| 186 if (fCaps.fMustDeclareFragmentShaderOutput) { | |
| 187 this->write("sk_FragColor"); | |
| 188 } else { | |
| 189 this->write("gl_FragColor"); | |
| 190 } | |
| 191 } else { | |
| 192 this->write(ref.fVariable.fName); | |
| 193 } | |
| 194 } | 151 } |
| 195 | 152 |
| 196 void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) { | 153 void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) { |
| 197 this->writeExpression(*expr.fBase, kPostfix_Precedence); | 154 this->writeExpression(*expr.fBase, kPostfix_Precedence); |
| 198 this->write("["); | 155 this->write("["); |
| 199 this->writeExpression(*expr.fIndex, kTopLevel_Precedence); | 156 this->writeExpression(*expr.fIndex, kTopLevel_Precedence); |
| 200 this->write("]"); | 157 this->write("]"); |
| 201 } | 158 } |
| 202 | 159 |
| 203 void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) { | 160 void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) { |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 this->write(to_string(f.fValue)); | 277 this->write(to_string(f.fValue)); |
| 321 } | 278 } |
| 322 | 279 |
| 323 void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) { | 280 void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) { |
| 324 this->writeType(f.fDeclaration.fReturnType); | 281 this->writeType(f.fDeclaration.fReturnType); |
| 325 this->write(" " + f.fDeclaration.fName + "("); | 282 this->write(" " + f.fDeclaration.fName + "("); |
| 326 const char* separator = ""; | 283 const char* separator = ""; |
| 327 for (const auto& param : f.fDeclaration.fParameters) { | 284 for (const auto& param : f.fDeclaration.fParameters) { |
| 328 this->write(separator); | 285 this->write(separator); |
| 329 separator = ", "; | 286 separator = ", "; |
| 330 this->writeModifiers(param->fModifiers, false); | 287 this->writeModifiers(param->fModifiers); |
| 331 this->writeType(param->fType); | 288 this->writeType(param->fType); |
| 332 this->write(" " + param->fName); | 289 this->write(" " + param->fName); |
| 333 } | 290 } |
| 334 this->writeLine(") {"); | 291 this->write(") "); |
| 335 | 292 this->writeBlock(*f.fBody); |
| 336 fFunctionHeader = ""; | 293 this->writeLine(); |
| 337 std::ostream* oldOut = fOut; | |
| 338 std::stringstream buffer; | |
| 339 fOut = &buffer; | |
| 340 fIndentation++; | |
| 341 for (const auto& s : f.fBody->fStatements) { | |
| 342 this->writeStatement(*s); | |
| 343 this->writeLine(); | |
| 344 } | |
| 345 fIndentation--; | |
| 346 this->writeLine("}"); | |
| 347 | |
| 348 fOut = oldOut; | |
| 349 this->write(fFunctionHeader); | |
| 350 this->write(buffer.str()); | |
| 351 } | 294 } |
| 352 | 295 |
| 353 void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers, | 296 void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers) { |
| 354 bool globalContext) { | 297 this->write(modifiers.description()); |
| 355 if (modifiers.fFlags & Modifiers::kNoPerspective_Flag) { | |
| 356 this->write("noperspective "); | |
| 357 } | |
| 358 if (modifiers.fFlags & Modifiers::kFlat_Flag) { | |
| 359 this->write("flat "); | |
| 360 } | |
| 361 std::string layout = modifiers.fLayout.description(); | |
| 362 if (layout.length()) { | |
| 363 this->write(layout + " "); | |
| 364 } | |
| 365 if ((modifiers.fFlags & Modifiers::kIn_Flag) && | |
| 366 (modifiers.fFlags & Modifiers::kOut_Flag)) { | |
| 367 this->write("inout "); | |
| 368 } else if (modifiers.fFlags & Modifiers::kIn_Flag) { | |
| 369 if (globalContext && fCaps.fVersion < 130) { | |
| 370 this->write(fProgramKind == Program::kVertex_Kind ? "attribute " | |
| 371 : "varying "); | |
| 372 } else { | |
| 373 this->write("in "); | |
| 374 } | |
| 375 } else if (modifiers.fFlags & Modifiers::kOut_Flag) { | |
| 376 if (globalContext && fCaps.fVersion < 130) { | |
| 377 this->write("varying "); | |
| 378 } else { | |
| 379 this->write("out "); | |
| 380 } | |
| 381 } | |
| 382 if (modifiers.fFlags & Modifiers::kUniform_Flag) { | |
| 383 this->write("uniform "); | |
| 384 } | |
| 385 if (modifiers.fFlags & Modifiers::kConst_Flag) { | |
| 386 this->write("const "); | |
| 387 } | |
| 388 if (fCaps.fUsesPrecisionModifiers) { | |
| 389 bool modifier = false; | |
| 390 if (modifiers.fFlags & Modifiers::kLowp_Flag) { | |
| 391 this->write("lowp "); | |
| 392 modifier = true; | |
| 393 } | |
| 394 if (modifiers.fFlags & Modifiers::kHighp_Flag) { | |
| 395 this->write("highp "); | |
| 396 modifier = true; | |
| 397 } | |
| 398 if (!modifier) { | |
| 399 this->write("mediump "); | |
| 400 } | |
| 401 } | |
| 402 } | 298 } |
| 403 | 299 |
| 404 void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { | 300 void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { |
| 405 if (intf.fVariable.fName == "gl_PerVertex") { | 301 if (intf.fVariable.fName == "gl_PerVertex") { |
| 406 return; | 302 return; |
| 407 } | 303 } |
| 408 this->writeModifiers(intf.fVariable.fModifiers, true); | 304 this->writeModifiers(intf.fVariable.fModifiers); |
| 409 this->writeLine(intf.fVariable.fType.name() + " {"); | 305 this->writeLine(intf.fVariable.fType.name() + " {"); |
| 410 fIndentation++; | 306 fIndentation++; |
| 411 for (const auto& f : intf.fVariable.fType.fields()) { | 307 for (const auto& f : intf.fVariable.fType.fields()) { |
| 412 this->writeModifiers(f.fModifiers, false); | 308 this->writeModifiers(f.fModifiers); |
| 413 this->writeType(*f.fType); | 309 this->writeType(*f.fType); |
| 414 this->writeLine(" " + f.fName + ";"); | 310 this->writeLine(" " + f.fName + ";"); |
| 415 } | 311 } |
| 416 fIndentation--; | 312 fIndentation--; |
| 417 this->writeLine("};"); | 313 this->writeLine("};"); |
| 418 } | 314 } |
| 419 | 315 |
| 420 void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool g
lobal) { | 316 void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl) { |
| 421 ASSERT(decl.fVars.size() > 0); | 317 ASSERT(decl.fVars.size() > 0); |
| 422 this->writeModifiers(decl.fVars[0].fVar->fModifiers, global); | 318 this->writeModifiers(decl.fVars[0].fVar->fModifiers); |
| 423 this->writeType(decl.fBaseType); | 319 this->writeType(decl.fBaseType); |
| 424 std::string separator = " "; | 320 std::string separator = " "; |
| 425 for (const auto& var : decl.fVars) { | 321 for (const auto& var : decl.fVars) { |
| 426 ASSERT(var.fVar->fModifiers == decl.fVars[0].fVar->fModifiers); | 322 ASSERT(var.fVar->fModifiers == decl.fVars[0].fVar->fModifiers); |
| 427 this->write(separator); | 323 this->write(separator); |
| 428 separator = ", "; | 324 separator = ", "; |
| 429 this->write(var.fVar->fName); | 325 this->write(var.fVar->fName); |
| 430 for (const auto& size : var.fSizes) { | 326 for (const auto& size : var.fSizes) { |
| 431 this->write("["); | 327 this->write("["); |
| 432 this->writeExpression(*size, kTopLevel_Precedence); | 328 this->writeExpression(*size, kTopLevel_Precedence); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 446 this->writeBlock((Block&) s); | 342 this->writeBlock((Block&) s); |
| 447 break; | 343 break; |
| 448 case Statement::kExpression_Kind: | 344 case Statement::kExpression_Kind: |
| 449 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopL
evel_Precedence); | 345 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopL
evel_Precedence); |
| 450 this->write(";"); | 346 this->write(";"); |
| 451 break; | 347 break; |
| 452 case Statement::kReturn_Kind: | 348 case Statement::kReturn_Kind: |
| 453 this->writeReturnStatement((ReturnStatement&) s); | 349 this->writeReturnStatement((ReturnStatement&) s); |
| 454 break; | 350 break; |
| 455 case Statement::kVarDeclarations_Kind: | 351 case Statement::kVarDeclarations_Kind: |
| 456 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclara
tion, false); | 352 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclara
tion); |
| 457 break; | 353 break; |
| 458 case Statement::kIf_Kind: | 354 case Statement::kIf_Kind: |
| 459 this->writeIfStatement((IfStatement&) s); | 355 this->writeIfStatement((IfStatement&) s); |
| 460 break; | 356 break; |
| 461 case Statement::kFor_Kind: | 357 case Statement::kFor_Kind: |
| 462 this->writeForStatement((ForStatement&) s); | 358 this->writeForStatement((ForStatement&) s); |
| 463 break; | 359 break; |
| 464 case Statement::kWhile_Kind: | 360 case Statement::kWhile_Kind: |
| 465 this->writeWhileStatement((WhileStatement&) s); | 361 this->writeWhileStatement((WhileStatement&) s); |
| 466 break; | 362 break; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 if (r.fExpression) { | 437 if (r.fExpression) { |
| 542 this->write(" "); | 438 this->write(" "); |
| 543 this->writeExpression(*r.fExpression, kTopLevel_Precedence); | 439 this->writeExpression(*r.fExpression, kTopLevel_Precedence); |
| 544 } | 440 } |
| 545 this->write(";"); | 441 this->write(";"); |
| 546 } | 442 } |
| 547 | 443 |
| 548 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out)
{ | 444 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out)
{ |
| 549 ASSERT(fOut == nullptr); | 445 ASSERT(fOut == nullptr); |
| 550 fOut = &out; | 446 fOut = &out; |
| 551 fProgramKind = program.fKind; | |
| 552 this->write("#version " + to_string(fCaps.fVersion)); | 447 this->write("#version " + to_string(fCaps.fVersion)); |
| 553 if (fCaps.fStandard == GLCaps::kGLES_Standard) { | 448 if (fCaps.fStandard == GLCaps::kGLES_Standard) { |
| 554 this->write(" es"); | 449 this->write(" es"); |
| 555 } else if (fCaps.fIsCoreProfile) { | |
| 556 this->write(" core"); | |
| 557 } | 450 } |
| 558 this->writeLine(); | 451 this->writeLine(); |
| 559 for (const auto& e : program.fElements) { | 452 for (const auto& e : program.fElements) { |
| 560 switch (e->fKind) { | 453 switch (e->fKind) { |
| 561 case ProgramElement::kExtension_Kind: | 454 case ProgramElement::kExtension_Kind: |
| 562 this->writeExtension((Extension&) *e); | 455 this->writeExtension((Extension&) *e); |
| 563 break; | 456 break; |
| 564 case ProgramElement::kVar_Kind: { | 457 case ProgramElement::kVar_Kind: { |
| 565 VarDeclarations& decl = (VarDeclarations&) *e; | 458 VarDeclarations& decl = (VarDeclarations&) *e; |
| 566 if (decl.fVars.size() > 0) { | 459 if (decl.fVars.size() > 0 && |
| 567 int builtin = decl.fVars[0].fVar->fModifiers.fLayout.fBuilti
n; | 460 decl.fVars[0].fVar->fModifiers.fLayout.fBuiltin == -1) { |
| 568 if (builtin == -1) { | 461 this->writeVarDeclarations(decl); |
| 569 // normal var | 462 this->writeLine(); |
| 570 this->writeVarDeclarations(decl, true); | |
| 571 this->writeLine(); | |
| 572 } else if (builtin == SK_FRAGCOLOR_BUILTIN && | |
| 573 fCaps.fMustDeclareFragmentShaderOutput) { | |
| 574 this->writeLine("out vec4 sk_FragColor;"); | |
| 575 } | |
| 576 } | 463 } |
| 577 break; | 464 break; |
| 578 } | 465 } |
| 579 case ProgramElement::kInterfaceBlock_Kind: | 466 case ProgramElement::kInterfaceBlock_Kind: |
| 580 this->writeInterfaceBlock((InterfaceBlock&) *e); | 467 this->writeInterfaceBlock((InterfaceBlock&) *e); |
| 581 break; | 468 break; |
| 582 case ProgramElement::kFunction_Kind: | 469 case ProgramElement::kFunction_Kind: |
| 583 this->writeFunction((FunctionDefinition&) *e); | 470 this->writeFunction((FunctionDefinition&) *e); |
| 584 break; | 471 break; |
| 585 default: | 472 default: |
| 586 printf("%s\n", e->description().c_str()); | 473 printf("%s\n", e->description().c_str()); |
| 587 ABORT("unsupported program element"); | 474 ABORT("unsupported program element"); |
| 588 } | 475 } |
| 589 } | 476 } |
| 590 fOut = nullptr; | 477 fOut = nullptr; |
| 591 } | 478 } |
| 592 | 479 |
| 593 } | 480 } |
| OLD | NEW |