Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(71)

Side by Side Diff: src/sksl/SkSLGLSLCodeGenerator.cpp

Issue 2288033003: Turned on SkSL->GLSL compiler (Closed)
Patch Set: fixed broken test Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/SkSLModifiersDeclaration.h"
17 #include "ir/SkSLVariableReference.h" 18 #include "ir/SkSLVariableReference.h"
18 19
20 #define SK_FRAGCOLOR_BUILTIN 10001
21
19 namespace SkSL { 22 namespace SkSL {
20 23
21 void GLSLCodeGenerator::write(const char* s) { 24 void GLSLCodeGenerator::write(const char* s) {
22 if (s[0] == 0) { 25 if (s[0] == 0) {
23 return; 26 return;
24 } 27 }
25 if (fAtLineStart) { 28 if (fAtLineStart) {
26 for (int i = 0; i < fIndentation; i++) { 29 for (int i = 0; i < fIndentation; i++) {
27 *fOut << " "; 30 *fOut << " ";
28 } 31 }
(...skipping 30 matching lines...) Expand all
59 if (*search == type) { 62 if (*search == type) {
60 // already written 63 // already written
61 this->write(type.name()); 64 this->write(type.name());
62 return; 65 return;
63 } 66 }
64 } 67 }
65 fWrittenStructs.push_back(&type); 68 fWrittenStructs.push_back(&type);
66 this->writeLine("struct " + type.name() + " {"); 69 this->writeLine("struct " + type.name() + " {");
67 fIndentation++; 70 fIndentation++;
68 for (const auto& f : type.fields()) { 71 for (const auto& f : type.fields()) {
69 this->writeModifiers(f.fModifiers); 72 this->writeModifiers(f.fModifiers, false);
70 // sizes (which must be static in structs) are part of the type name here 73 // sizes (which must be static in structs) are part of the type name here
71 this->writeType(*f.fType); 74 this->writeType(*f.fType);
72 this->writeLine(" " + f.fName + ";"); 75 this->writeLine(" " + f.fName + ";");
73 } 76 }
74 fIndentation--; 77 fIndentation--;
75 this->writeLine("}"); 78 this->writeLine("}");
76 } else { 79 } else {
77 this->write(type.name()); 80 this->write(type.name());
78 } 81 }
79 } 82 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 this->writeTernaryExpression((TernaryExpression&) expr, parentPreced ence); 120 this->writeTernaryExpression((TernaryExpression&) expr, parentPreced ence);
118 break; 121 break;
119 case Expression::kIndex_Kind: 122 case Expression::kIndex_Kind:
120 this->writeIndexExpression((IndexExpression&) expr); 123 this->writeIndexExpression((IndexExpression&) expr);
121 break; 124 break;
122 default: 125 default:
123 ABORT("unsupported expression: %s", expr.description().c_str()); 126 ABORT("unsupported expression: %s", expr.description().c_str());
124 } 127 }
125 } 128 }
126 129
130 static bool is_abs(Expression& expr) {
131 if (expr.fKind != Expression::kFunctionCall_Kind) {
132 return false;
133 }
134 return ((FunctionCall&) expr).fFunction.fName == "abs";
135 }
136
137 // turns min(abs(x), y) into (abs(x) > (tmpVar = y) ? tmpVar : abs(x)) to avoid a Tegra3 compiler
138 // bug.
139 void GLSLCodeGenerator::writeMinAbsHack(Expression& absExpr, Expression& otherEx pr) {
140 ASSERT(!fCaps.fCanUseMinAndAbsTogether);
141 std::string varName = "minAbsHackVar" + to_string(fVarCount++);
142 this->fFunctionHeader += " " + otherExpr.fType.name() + " " + varName + " ;\n";
143 this->write("(");
144 this->writeExpression(absExpr, kTopLevel_Precedence);
145 this->write(" > (" + varName + " = ");
146 this->writeExpression(otherExpr, kRelational_Precedence);
dogben 2016/09/30 15:00:51 nit: kAssignment_Precedence
ethannicholas 2016/10/06 13:16:47 Done.
147 this->write(") ? " + varName + " : ");
148 this->writeExpression(absExpr, kTernary_Precedence);
dogben 2016/09/30 15:00:51 Why make a temp var for otherExpr but not absExpr?
ethannicholas 2016/10/06 13:16:47 Fixed.
149 this->write(")");
150 }
151
127 void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { 152 void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) {
153 if (!fCaps.fCanUseMinAndAbsTogether && c.fFunction.fName == "min") {
154 ASSERT(c.fArguments.size() == 2);
155 if (is_abs(*c.fArguments[0])) {
156 this->writeMinAbsHack(*c.fArguments[0], *c.fArguments[1]);
157 return;
158 }
159 if (is_abs(*c.fArguments[1])) {
160 this->writeMinAbsHack(*c.fArguments[1], *c.fArguments[0]);
dogben 2016/09/30 15:00:51 Is argument evaluation order undefined? E.g. "min(
ethannicholas 2016/10/06 13:16:47 GLSL specifies left to right, but I haven't decide
161 return;
162 }
163 }
128 this->write(c.fFunction.fName + "("); 164 this->write(c.fFunction.fName + "(");
129 const char* separator = ""; 165 const char* separator = "";
130 for (const auto& arg : c.fArguments) { 166 for (const auto& arg : c.fArguments) {
131 this->write(separator); 167 this->write(separator);
132 separator = ", "; 168 separator = ", ";
133 this->writeExpression(*arg, kSequence_Precedence); 169 this->writeExpression(*arg, kSequence_Precedence);
134 } 170 }
135 this->write(")"); 171 this->write(")");
136 } 172 }
137 173
138 void GLSLCodeGenerator::writeConstructor(const Constructor& c) { 174 void GLSLCodeGenerator::writeConstructor(const Constructor& c) {
139 this->write(c.fType.name() + "("); 175 this->write(c.fType.name() + "(");
140 const char* separator = ""; 176 const char* separator = "";
141 for (const auto& arg : c.fArguments) { 177 for (const auto& arg : c.fArguments) {
142 this->write(separator); 178 this->write(separator);
143 separator = ", "; 179 separator = ", ";
144 this->writeExpression(*arg, kSequence_Precedence); 180 this->writeExpression(*arg, kSequence_Precedence);
145 } 181 }
146 this->write(")"); 182 this->write(")");
147 } 183 }
148 184
149 void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) { 185 void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) {
150 this->write(ref.fVariable.fName); 186 if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN) {
187 if (fCaps.fMustDeclareFragmentShaderOutput) {
188 this->write("sk_FragColor");
189 } else {
190 this->write("gl_FragColor");
191 }
192 } else {
193 this->write(ref.fVariable.fName);
194 }
151 } 195 }
152 196
153 void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) { 197 void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) {
154 this->writeExpression(*expr.fBase, kPostfix_Precedence); 198 this->writeExpression(*expr.fBase, kPostfix_Precedence);
155 this->write("["); 199 this->write("[");
156 this->writeExpression(*expr.fIndex, kTopLevel_Precedence); 200 this->writeExpression(*expr.fIndex, kTopLevel_Precedence);
157 this->write("]"); 201 this->write("]");
158 } 202 }
159 203
160 void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) { 204 void GLSLCodeGenerator::writeFieldAccess(const FieldAccess& f) {
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 if (kPostfix_Precedence >= parentPrecedence) { 307 if (kPostfix_Precedence >= parentPrecedence) {
264 this->write(")"); 308 this->write(")");
265 } 309 }
266 } 310 }
267 311
268 void GLSLCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { 312 void GLSLCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
269 this->write(b.fValue ? "true" : "false"); 313 this->write(b.fValue ? "true" : "false");
270 } 314 }
271 315
272 void GLSLCodeGenerator::writeIntLiteral(const IntLiteral& i) { 316 void GLSLCodeGenerator::writeIntLiteral(const IntLiteral& i) {
273 this->write(to_string(i.fValue)); 317 if (i.fType == *fContext.fUInt_Type) {
318 this->write(to_string(i.fValue & 0xffffffff) + "u");
319 } else {
320 this->write(to_string(i.fValue));
321 }
274 } 322 }
275 323
276 void GLSLCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { 324 void GLSLCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
277 this->write(to_string(f.fValue)); 325 this->write(to_string(f.fValue));
278 } 326 }
279 327
280 void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) { 328 void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) {
281 this->writeType(f.fDeclaration.fReturnType); 329 this->writeType(f.fDeclaration.fReturnType);
282 this->write(" " + f.fDeclaration.fName + "("); 330 this->write(" " + f.fDeclaration.fName + "(");
283 const char* separator = ""; 331 const char* separator = "";
284 for (const auto& param : f.fDeclaration.fParameters) { 332 for (const auto& param : f.fDeclaration.fParameters) {
285 this->write(separator); 333 this->write(separator);
286 separator = ", "; 334 separator = ", ";
287 this->writeModifiers(param->fModifiers); 335 this->writeModifiers(param->fModifiers, false);
288 this->writeType(param->fType); 336 std::vector<int> sizes;
337 const Type* type = &param->fType;
338 while (type->kind() == Type::kArray_Kind) {
339 sizes.push_back(type->columns());
340 type = &type->componentType();
341 }
342 this->writeType(*type);
289 this->write(" " + param->fName); 343 this->write(" " + param->fName);
344 for (int s : sizes) {
345 if (s <= 0) {
346 this->write("[]");
347 } else {
348 this->write("[" + to_string(s) + "]");
349 }
350 }
290 } 351 }
291 this->write(") "); 352 this->writeLine(") {");
292 this->writeBlock(*f.fBody); 353
293 this->writeLine(); 354 fFunctionHeader = "";
355 std::ostream* oldOut = fOut;
356 std::stringstream buffer;
357 fOut = &buffer;
358 fIndentation++;
359 for (const auto& s : f.fBody->fStatements) {
360 this->writeStatement(*s);
361 this->writeLine();
362 }
363 fIndentation--;
364 this->writeLine("}");
365
366 fOut = oldOut;
367 this->write(fFunctionHeader);
368 this->write(buffer.str());
294 } 369 }
295 370
296 void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers) { 371 void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers,
297 this->write(modifiers.description()); 372 bool globalContext) {
373 if (modifiers.fFlags & Modifiers::kNoPerspective_Flag) {
374 this->write("noperspective ");
375 }
376 if (modifiers.fFlags & Modifiers::kFlat_Flag) {
377 this->write("flat ");
378 }
379 std::string layout = modifiers.fLayout.description();
380 if (layout.length()) {
381 this->write(layout + " ");
382 }
383 if ((modifiers.fFlags & Modifiers::kIn_Flag) &&
384 (modifiers.fFlags & Modifiers::kOut_Flag)) {
385 this->write("inout ");
386 } else if (modifiers.fFlags & Modifiers::kIn_Flag) {
387 if (globalContext && fCaps.fVersion < 130) {
388 this->write(fProgramKind == Program::kVertex_Kind ? "attribute "
389 : "varying ");
390 } else {
391 this->write("in ");
392 }
393 } else if (modifiers.fFlags & Modifiers::kOut_Flag) {
394 if (globalContext && fCaps.fVersion < 130) {
395 this->write("varying ");
396 } else {
397 this->write("out ");
398 }
399 }
400 if (modifiers.fFlags & Modifiers::kUniform_Flag) {
401 this->write("uniform ");
402 }
403 if (modifiers.fFlags & Modifiers::kConst_Flag) {
404 this->write("const ");
405 }
406 if (fCaps.fUsesPrecisionModifiers) {
407 if (modifiers.fFlags & Modifiers::kLowp_Flag) {
408 this->write("lowp ");
409 }
410 if (modifiers.fFlags & Modifiers::kMediump_Flag) {
411 this->write("mediump ");
412 }
413 if (modifiers.fFlags & Modifiers::kHighp_Flag) {
414 this->write("highp ");
415 }
416 }
298 } 417 }
299 418
300 void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { 419 void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) {
301 if (intf.fVariable.fName == "gl_PerVertex") { 420 if (intf.fVariable.fName == "gl_PerVertex") {
302 return; 421 return;
303 } 422 }
304 this->writeModifiers(intf.fVariable.fModifiers); 423 this->writeModifiers(intf.fVariable.fModifiers, true);
305 this->writeLine(intf.fVariable.fType.name() + " {"); 424 this->writeLine(intf.fVariable.fType.name() + " {");
306 fIndentation++; 425 fIndentation++;
307 for (const auto& f : intf.fVariable.fType.fields()) { 426 for (const auto& f : intf.fVariable.fType.fields()) {
308 this->writeModifiers(f.fModifiers); 427 this->writeModifiers(f.fModifiers, false);
309 this->writeType(*f.fType); 428 this->writeType(*f.fType);
310 this->writeLine(" " + f.fName + ";"); 429 this->writeLine(" " + f.fName + ";");
311 } 430 }
312 fIndentation--; 431 fIndentation--;
313 this->writeLine("};"); 432 this->writeLine("};");
314 } 433 }
315 434
316 void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl) { 435 void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool g lobal) {
317 ASSERT(decl.fVars.size() > 0); 436 ASSERT(decl.fVars.size() > 0);
318 this->writeModifiers(decl.fVars[0].fVar->fModifiers); 437 this->writeModifiers(decl.fVars[0].fVar->fModifiers, global);
319 this->writeType(decl.fBaseType); 438 this->writeType(decl.fBaseType);
320 std::string separator = " "; 439 std::string separator = " ";
321 for (const auto& var : decl.fVars) { 440 for (const auto& var : decl.fVars) {
322 ASSERT(var.fVar->fModifiers == decl.fVars[0].fVar->fModifiers); 441 ASSERT(var.fVar->fModifiers == decl.fVars[0].fVar->fModifiers);
323 this->write(separator); 442 this->write(separator);
324 separator = ", "; 443 separator = ", ";
325 this->write(var.fVar->fName); 444 this->write(var.fVar->fName);
326 for (const auto& size : var.fSizes) { 445 for (const auto& size : var.fSizes) {
327 this->write("["); 446 this->write("[");
328 this->writeExpression(*size, kTopLevel_Precedence); 447 if (size) {
448 this->writeExpression(*size, kTopLevel_Precedence);
449 }
329 this->write("]"); 450 this->write("]");
330 } 451 }
331 if (var.fValue) { 452 if (var.fValue) {
332 this->write(" = "); 453 this->write(" = ");
333 this->writeExpression(*var.fValue, kTopLevel_Precedence); 454 this->writeExpression(*var.fValue, kTopLevel_Precedence);
334 } 455 }
335 } 456 }
336 this->write(";"); 457 this->write(";");
337 } 458 }
338 459
339 void GLSLCodeGenerator::writeStatement(const Statement& s) { 460 void GLSLCodeGenerator::writeStatement(const Statement& s) {
340 switch (s.fKind) { 461 switch (s.fKind) {
341 case Statement::kBlock_Kind: 462 case Statement::kBlock_Kind:
342 this->writeBlock((Block&) s); 463 this->writeBlock((Block&) s);
343 break; 464 break;
344 case Statement::kExpression_Kind: 465 case Statement::kExpression_Kind:
345 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopL evel_Precedence); 466 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopL evel_Precedence);
346 this->write(";"); 467 this->write(";");
347 break; 468 break;
348 case Statement::kReturn_Kind: 469 case Statement::kReturn_Kind:
349 this->writeReturnStatement((ReturnStatement&) s); 470 this->writeReturnStatement((ReturnStatement&) s);
350 break; 471 break;
351 case Statement::kVarDeclarations_Kind: 472 case Statement::kVarDeclarations_Kind:
352 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclara tion); 473 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclara tion, false);
353 break; 474 break;
354 case Statement::kIf_Kind: 475 case Statement::kIf_Kind:
355 this->writeIfStatement((IfStatement&) s); 476 this->writeIfStatement((IfStatement&) s);
356 break; 477 break;
357 case Statement::kFor_Kind: 478 case Statement::kFor_Kind:
358 this->writeForStatement((ForStatement&) s); 479 this->writeForStatement((ForStatement&) s);
359 break; 480 break;
360 case Statement::kWhile_Kind: 481 case Statement::kWhile_Kind:
361 this->writeWhileStatement((WhileStatement&) s); 482 this->writeWhileStatement((WhileStatement&) s);
362 break; 483 break;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 if (r.fExpression) { 558 if (r.fExpression) {
438 this->write(" "); 559 this->write(" ");
439 this->writeExpression(*r.fExpression, kTopLevel_Precedence); 560 this->writeExpression(*r.fExpression, kTopLevel_Precedence);
440 } 561 }
441 this->write(";"); 562 this->write(";");
442 } 563 }
443 564
444 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out) { 565 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out) {
445 ASSERT(fOut == nullptr); 566 ASSERT(fOut == nullptr);
446 fOut = &out; 567 fOut = &out;
568 fProgramKind = program.fKind;
447 this->write("#version " + to_string(fCaps.fVersion)); 569 this->write("#version " + to_string(fCaps.fVersion));
448 if (fCaps.fStandard == GLCaps::kGLES_Standard) { 570 if (fCaps.fStandard == GLCaps::kGLES_Standard && fCaps.fVersion >= 300) {
449 this->write(" es"); 571 this->write(" es");
572 } else if (fCaps.fIsCoreProfile) {
573 this->write(" core");
450 } 574 }
451 this->writeLine(); 575 this->writeLine();
452 for (const auto& e : program.fElements) { 576 for (const auto& e : program.fElements) {
577 if (e->fKind == ProgramElement::kExtension_Kind) {
578 this->writeExtension((Extension&) *e);
579 }
580 }
581 if (fCaps.fStandard == GLCaps::kGLES_Standard) {
582 this->write("precision ");
583 switch (program.fDefaultPrecision) {
584 case Modifiers::kLowp_Flag:
585 this->write("lowp");
586 break;
587 case Modifiers::kMediump_Flag:
588 this->write("mediump");
589 break;
590 case Modifiers::kHighp_Flag:
591 this->write("highp");
592 break;
593 default:
594 ASSERT(false);
595 this->write("<error>");
596 }
597 this->writeLine(" float;");
598 }
599 for (const auto& e : program.fElements) {
453 switch (e->fKind) { 600 switch (e->fKind) {
454 case ProgramElement::kExtension_Kind: 601 case ProgramElement::kExtension_Kind:
455 this->writeExtension((Extension&) *e);
456 break; 602 break;
457 case ProgramElement::kVar_Kind: { 603 case ProgramElement::kVar_Kind: {
458 VarDeclarations& decl = (VarDeclarations&) *e; 604 VarDeclarations& decl = (VarDeclarations&) *e;
459 if (decl.fVars.size() > 0 && 605 if (decl.fVars.size() > 0) {
460 decl.fVars[0].fVar->fModifiers.fLayout.fBuiltin == -1) { 606 int builtin = decl.fVars[0].fVar->fModifiers.fLayout.fBuilti n;
461 this->writeVarDeclarations(decl); 607 if (builtin == -1) {
462 this->writeLine(); 608 // normal var
609 this->writeVarDeclarations(decl, true);
610 this->writeLine();
611 } else if (builtin == SK_FRAGCOLOR_BUILTIN &&
612 fCaps.fMustDeclareFragmentShaderOutput) {
613 this->write("out ");
dogben 2016/09/30 15:00:51 Does this ever need to be "varying "?
ethannicholas 2016/10/06 13:16:47 I don't think so -- versions new enough to require
614 if (fCaps.fUsesPrecisionModifiers) {
615 this->write("mediump ");
616 }
617 this->writeLine("vec4 sk_FragColor;");
618 }
463 } 619 }
464 break; 620 break;
465 } 621 }
466 case ProgramElement::kInterfaceBlock_Kind: 622 case ProgramElement::kInterfaceBlock_Kind:
467 this->writeInterfaceBlock((InterfaceBlock&) *e); 623 this->writeInterfaceBlock((InterfaceBlock&) *e);
468 break; 624 break;
469 case ProgramElement::kFunction_Kind: 625 case ProgramElement::kFunction_Kind:
470 this->writeFunction((FunctionDefinition&) *e); 626 this->writeFunction((FunctionDefinition&) *e);
471 break; 627 break;
628 case ProgramElement::kModifiers_Kind:
629 this->writeModifiers(((ModifiersDeclaration&) *e).fModifiers, tr ue);
630 this->writeLine(";");
631 break;
472 default: 632 default:
473 printf("%s\n", e->description().c_str()); 633 printf("%s\n", e->description().c_str());
474 ABORT("unsupported program element"); 634 ABORT("unsupported program element");
475 } 635 }
476 } 636 }
477 fOut = nullptr; 637 fOut = nullptr;
478 } 638 }
479 639
480 } 640 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698