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

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

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

Powered by Google App Engine
This is Rietveld 408576698