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 |
19 namespace SkSL { | 21 namespace SkSL { |
20 | 22 |
21 void GLSLCodeGenerator::write(const char* s) { | 23 void GLSLCodeGenerator::write(const char* s) { |
22 if (s[0] == 0) { | 24 if (s[0] == 0) { |
23 return; | 25 return; |
24 } | 26 } |
25 if (fAtLineStart) { | 27 if (fAtLineStart) { |
26 for (int i = 0; i < fIndentation; i++) { | 28 for (int i = 0; i < fIndentation; i++) { |
27 *fOut << " "; | 29 *fOut << " "; |
28 } | 30 } |
(...skipping 30 matching lines...) Expand all Loading... |
59 if (*search == type) { | 61 if (*search == type) { |
60 // already written | 62 // already written |
61 this->write(type.name()); | 63 this->write(type.name()); |
62 return; | 64 return; |
63 } | 65 } |
64 } | 66 } |
65 fWrittenStructs.push_back(&type); | 67 fWrittenStructs.push_back(&type); |
66 this->writeLine("struct " + type.name() + " {"); | 68 this->writeLine("struct " + type.name() + " {"); |
67 fIndentation++; | 69 fIndentation++; |
68 for (const auto& f : type.fields()) { | 70 for (const auto& f : type.fields()) { |
69 this->writeModifiers(f.fModifiers); | 71 this->writeModifiers(f.fModifiers, false); |
70 // sizes (which must be static in structs) are part of the type name
here | 72 // sizes (which must be static in structs) are part of the type name
here |
71 this->writeType(*f.fType); | 73 this->writeType(*f.fType); |
72 this->writeLine(" " + f.fName + ";"); | 74 this->writeLine(" " + f.fName + ";"); |
73 } | 75 } |
74 fIndentation--; | 76 fIndentation--; |
75 this->writeLine("}"); | 77 this->writeLine("}"); |
76 } else { | 78 } else { |
77 this->write(type.name()); | 79 this->write(type.name()); |
78 } | 80 } |
79 } | 81 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 this->writeTernaryExpression((TernaryExpression&) expr, parentPreced
ence); | 119 this->writeTernaryExpression((TernaryExpression&) expr, parentPreced
ence); |
118 break; | 120 break; |
119 case Expression::kIndex_Kind: | 121 case Expression::kIndex_Kind: |
120 this->writeIndexExpression((IndexExpression&) expr); | 122 this->writeIndexExpression((IndexExpression&) expr); |
121 break; | 123 break; |
122 default: | 124 default: |
123 ABORT("unsupported expression: %s", expr.description().c_str()); | 125 ABORT("unsupported expression: %s", expr.description().c_str()); |
124 } | 126 } |
125 } | 127 } |
126 | 128 |
| 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 |
127 void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { | 151 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 } |
128 this->write(c.fFunction.fName + "("); | 163 this->write(c.fFunction.fName + "("); |
129 const char* separator = ""; | 164 const char* separator = ""; |
130 for (const auto& arg : c.fArguments) { | 165 for (const auto& arg : c.fArguments) { |
131 this->write(separator); | 166 this->write(separator); |
132 separator = ", "; | 167 separator = ", "; |
133 this->writeExpression(*arg, kSequence_Precedence); | 168 this->writeExpression(*arg, kSequence_Precedence); |
134 } | 169 } |
135 this->write(")"); | 170 this->write(")"); |
136 } | 171 } |
137 | 172 |
138 void GLSLCodeGenerator::writeConstructor(const Constructor& c) { | 173 void GLSLCodeGenerator::writeConstructor(const Constructor& c) { |
139 this->write(c.fType.name() + "("); | 174 this->write(c.fType.name() + "("); |
140 const char* separator = ""; | 175 const char* separator = ""; |
141 for (const auto& arg : c.fArguments) { | 176 for (const auto& arg : c.fArguments) { |
142 this->write(separator); | 177 this->write(separator); |
143 separator = ", "; | 178 separator = ", "; |
144 this->writeExpression(*arg, kSequence_Precedence); | 179 this->writeExpression(*arg, kSequence_Precedence); |
145 } | 180 } |
146 this->write(")"); | 181 this->write(")"); |
147 } | 182 } |
148 | 183 |
149 void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) { | 184 void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) { |
150 this->write(ref.fVariable.fName); | 185 if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN) { |
| 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 } |
151 } | 194 } |
152 | 195 |
153 void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) { | 196 void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) { |
154 this->writeExpression(*expr.fBase, kPostfix_Precedence); | 197 this->writeExpression(*expr.fBase, kPostfix_Precedence); |
155 this->write("["); | 198 this->write("["); |
156 this->writeExpression(*expr.fIndex, kTopLevel_Precedence); | 199 this->writeExpression(*expr.fIndex, kTopLevel_Precedence); |
157 this->write("]"); | 200 this->write("]"); |
158 } | 201 } |
159 | 202 |
160 void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) { | 203 void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) { |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 this->write(to_string(f.fValue)); | 320 this->write(to_string(f.fValue)); |
278 } | 321 } |
279 | 322 |
280 void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) { | 323 void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) { |
281 this->writeType(f.fDeclaration.fReturnType); | 324 this->writeType(f.fDeclaration.fReturnType); |
282 this->write(" " + f.fDeclaration.fName + "("); | 325 this->write(" " + f.fDeclaration.fName + "("); |
283 const char* separator = ""; | 326 const char* separator = ""; |
284 for (const auto& param : f.fDeclaration.fParameters) { | 327 for (const auto& param : f.fDeclaration.fParameters) { |
285 this->write(separator); | 328 this->write(separator); |
286 separator = ", "; | 329 separator = ", "; |
287 this->writeModifiers(param->fModifiers); | 330 this->writeModifiers(param->fModifiers, false); |
288 this->writeType(param->fType); | 331 this->writeType(param->fType); |
289 this->write(" " + param->fName); | 332 this->write(" " + param->fName); |
290 } | 333 } |
291 this->write(") "); | 334 this->writeLine(") {"); |
292 this->writeBlock(*f.fBody); | 335 |
293 this->writeLine(); | 336 fFunctionHeader = ""; |
| 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()); |
294 } | 351 } |
295 | 352 |
296 void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers) { | 353 void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers, |
297 this->write(modifiers.description()); | 354 bool globalContext) { |
| 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 } |
298 } | 402 } |
299 | 403 |
300 void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { | 404 void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { |
301 if (intf.fVariable.fName == "gl_PerVertex") { | 405 if (intf.fVariable.fName == "gl_PerVertex") { |
302 return; | 406 return; |
303 } | 407 } |
304 this->writeModifiers(intf.fVariable.fModifiers); | 408 this->writeModifiers(intf.fVariable.fModifiers, true); |
305 this->writeLine(intf.fVariable.fType.name() + " {"); | 409 this->writeLine(intf.fVariable.fType.name() + " {"); |
306 fIndentation++; | 410 fIndentation++; |
307 for (const auto& f : intf.fVariable.fType.fields()) { | 411 for (const auto& f : intf.fVariable.fType.fields()) { |
308 this->writeModifiers(f.fModifiers); | 412 this->writeModifiers(f.fModifiers, false); |
309 this->writeType(*f.fType); | 413 this->writeType(*f.fType); |
310 this->writeLine(" " + f.fName + ";"); | 414 this->writeLine(" " + f.fName + ";"); |
311 } | 415 } |
312 fIndentation--; | 416 fIndentation--; |
313 this->writeLine("};"); | 417 this->writeLine("};"); |
314 } | 418 } |
315 | 419 |
316 void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl) { | 420 void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool g
lobal) { |
317 ASSERT(decl.fVars.size() > 0); | 421 ASSERT(decl.fVars.size() > 0); |
318 this->writeModifiers(decl.fVars[0].fVar->fModifiers); | 422 this->writeModifiers(decl.fVars[0].fVar->fModifiers, global); |
319 this->writeType(decl.fBaseType); | 423 this->writeType(decl.fBaseType); |
320 std::string separator = " "; | 424 std::string separator = " "; |
321 for (const auto& var : decl.fVars) { | 425 for (const auto& var : decl.fVars) { |
322 ASSERT(var.fVar->fModifiers == decl.fVars[0].fVar->fModifiers); | 426 ASSERT(var.fVar->fModifiers == decl.fVars[0].fVar->fModifiers); |
323 this->write(separator); | 427 this->write(separator); |
324 separator = ", "; | 428 separator = ", "; |
325 this->write(var.fVar->fName); | 429 this->write(var.fVar->fName); |
326 for (const auto& size : var.fSizes) { | 430 for (const auto& size : var.fSizes) { |
327 this->write("["); | 431 this->write("["); |
328 this->writeExpression(*size, kTopLevel_Precedence); | 432 this->writeExpression(*size, kTopLevel_Precedence); |
(...skipping 13 matching lines...) Expand all Loading... |
342 this->writeBlock((Block&) s); | 446 this->writeBlock((Block&) s); |
343 break; | 447 break; |
344 case Statement::kExpression_Kind: | 448 case Statement::kExpression_Kind: |
345 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopL
evel_Precedence); | 449 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopL
evel_Precedence); |
346 this->write(";"); | 450 this->write(";"); |
347 break; | 451 break; |
348 case Statement::kReturn_Kind: | 452 case Statement::kReturn_Kind: |
349 this->writeReturnStatement((ReturnStatement&) s); | 453 this->writeReturnStatement((ReturnStatement&) s); |
350 break; | 454 break; |
351 case Statement::kVarDeclarations_Kind: | 455 case Statement::kVarDeclarations_Kind: |
352 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclara
tion); | 456 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclara
tion, false); |
353 break; | 457 break; |
354 case Statement::kIf_Kind: | 458 case Statement::kIf_Kind: |
355 this->writeIfStatement((IfStatement&) s); | 459 this->writeIfStatement((IfStatement&) s); |
356 break; | 460 break; |
357 case Statement::kFor_Kind: | 461 case Statement::kFor_Kind: |
358 this->writeForStatement((ForStatement&) s); | 462 this->writeForStatement((ForStatement&) s); |
359 break; | 463 break; |
360 case Statement::kWhile_Kind: | 464 case Statement::kWhile_Kind: |
361 this->writeWhileStatement((WhileStatement&) s); | 465 this->writeWhileStatement((WhileStatement&) s); |
362 break; | 466 break; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 if (r.fExpression) { | 541 if (r.fExpression) { |
438 this->write(" "); | 542 this->write(" "); |
439 this->writeExpression(*r.fExpression, kTopLevel_Precedence); | 543 this->writeExpression(*r.fExpression, kTopLevel_Precedence); |
440 } | 544 } |
441 this->write(";"); | 545 this->write(";"); |
442 } | 546 } |
443 | 547 |
444 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out)
{ | 548 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out)
{ |
445 ASSERT(fOut == nullptr); | 549 ASSERT(fOut == nullptr); |
446 fOut = &out; | 550 fOut = &out; |
| 551 fProgramKind = program.fKind; |
447 this->write("#version " + to_string(fCaps.fVersion)); | 552 this->write("#version " + to_string(fCaps.fVersion)); |
448 if (fCaps.fStandard == GLCaps::kGLES_Standard) { | 553 if (fCaps.fStandard == GLCaps::kGLES_Standard) { |
449 this->write(" es"); | 554 this->write(" es"); |
| 555 } else if (fCaps.fIsCoreProfile) { |
| 556 this->write(" core"); |
450 } | 557 } |
451 this->writeLine(); | 558 this->writeLine(); |
452 for (const auto& e : program.fElements) { | 559 for (const auto& e : program.fElements) { |
453 switch (e->fKind) { | 560 switch (e->fKind) { |
454 case ProgramElement::kExtension_Kind: | 561 case ProgramElement::kExtension_Kind: |
455 this->writeExtension((Extension&) *e); | 562 this->writeExtension((Extension&) *e); |
456 break; | 563 break; |
457 case ProgramElement::kVar_Kind: { | 564 case ProgramElement::kVar_Kind: { |
458 VarDeclarations& decl = (VarDeclarations&) *e; | 565 VarDeclarations& decl = (VarDeclarations&) *e; |
459 if (decl.fVars.size() > 0 && | 566 if (decl.fVars.size() > 0) { |
460 decl.fVars[0].fVar->fModifiers.fLayout.fBuiltin == -1) { | 567 int builtin = decl.fVars[0].fVar->fModifiers.fLayout.fBuilti
n; |
461 this->writeVarDeclarations(decl); | 568 if (builtin == -1) { |
462 this->writeLine(); | 569 // normal var |
| 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 } |
463 } | 576 } |
464 break; | 577 break; |
465 } | 578 } |
466 case ProgramElement::kInterfaceBlock_Kind: | 579 case ProgramElement::kInterfaceBlock_Kind: |
467 this->writeInterfaceBlock((InterfaceBlock&) *e); | 580 this->writeInterfaceBlock((InterfaceBlock&) *e); |
468 break; | 581 break; |
469 case ProgramElement::kFunction_Kind: | 582 case ProgramElement::kFunction_Kind: |
470 this->writeFunction((FunctionDefinition&) *e); | 583 this->writeFunction((FunctionDefinition&) *e); |
471 break; | 584 break; |
472 default: | 585 default: |
473 printf("%s\n", e->description().c_str()); | 586 printf("%s\n", e->description().c_str()); |
474 ABORT("unsupported program element"); | 587 ABORT("unsupported program element"); |
475 } | 588 } |
476 } | 589 } |
477 fOut = nullptr; | 590 fOut = nullptr; |
478 } | 591 } |
479 | 592 |
480 } | 593 } |
OLD | NEW |