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

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

Issue 2387643003: 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 (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);
147 this->write(") ? " + varName + " : ");
148 this->writeExpression(absExpr, kTernary_Precedence);
149 this->write(")");
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 this->writeMinAbsHack(*c.fArguments[1], *c.fArguments[0]);
161 return;
162 }
163 }
164 this->write(c.fFunction.fName + "("); 128 this->write(c.fFunction.fName + "(");
165 const char* separator = ""; 129 const char* separator = "";
166 for (const auto& arg : c.fArguments) { 130 for (const auto& arg : c.fArguments) {
167 this->write(separator); 131 this->write(separator);
168 separator = ", "; 132 separator = ", ";
169 this->writeExpression(*arg, kSequence_Precedence); 133 this->writeExpression(*arg, kSequence_Precedence);
170 } 134 }
171 this->write(")"); 135 this->write(")");
172 } 136 }
173 137
174 void GLSLCodeGenerator::writeConstructor(const Constructor& c) { 138 void GLSLCodeGenerator::writeConstructor(const Constructor& c) {
175 this->write(c.fType.name() + "("); 139 this->write(c.fType.name() + "(");
176 const char* separator = ""; 140 const char* separator = "";
177 for (const auto& arg : c.fArguments) { 141 for (const auto& arg : c.fArguments) {
178 this->write(separator); 142 this->write(separator);
179 separator = ", "; 143 separator = ", ";
180 this->writeExpression(*arg, kSequence_Precedence); 144 this->writeExpression(*arg, kSequence_Precedence);
181 } 145 }
182 this->write(")"); 146 this->write(")");
183 } 147 }
184 148
185 void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) { 149 void GLSLCodeGenerator::writeVariableReference(const VariableReference& ref) {
186 if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN) { 150 this->write(ref.fVariable.fName);
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 }
195 } 151 }
196 152
197 void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) { 153 void GLSLCodeGenerator::writeIndexExpression(const IndexExpression& expr) {
198 this->writeExpression(*expr.fBase, kPostfix_Precedence); 154 this->writeExpression(*expr.fBase, kPostfix_Precedence);
199 this->write("["); 155 this->write("[");
200 this->writeExpression(*expr.fIndex, kTopLevel_Precedence); 156 this->writeExpression(*expr.fIndex, kTopLevel_Precedence);
201 this->write("]"); 157 this->write("]");
202 } 158 }
203 159
204 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
307 if (kPostfix_Precedence >= parentPrecedence) { 263 if (kPostfix_Precedence >= parentPrecedence) {
308 this->write(")"); 264 this->write(")");
309 } 265 }
310 } 266 }
311 267
312 void GLSLCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { 268 void GLSLCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
313 this->write(b.fValue ? "true" : "false"); 269 this->write(b.fValue ? "true" : "false");
314 } 270 }
315 271
316 void GLSLCodeGenerator::writeIntLiteral(const IntLiteral& i) { 272 void GLSLCodeGenerator::writeIntLiteral(const IntLiteral& i) {
317 if (i.fType == *fContext.fUInt_Type) { 273 this->write(to_string(i.fValue));
318 this->write(to_string(i.fValue & 0xffffffff) + "u");
319 } else {
320 this->write(to_string(i.fValue));
321 }
322 } 274 }
323 275
324 void GLSLCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { 276 void GLSLCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
325 this->write(to_string(f.fValue)); 277 this->write(to_string(f.fValue));
326 } 278 }
327 279
328 void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) { 280 void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) {
329 this->writeType(f.fDeclaration.fReturnType); 281 this->writeType(f.fDeclaration.fReturnType);
330 this->write(" " + f.fDeclaration.fName + "("); 282 this->write(" " + f.fDeclaration.fName + "(");
331 const char* separator = ""; 283 const char* separator = "";
332 for (const auto& param : f.fDeclaration.fParameters) { 284 for (const auto& param : f.fDeclaration.fParameters) {
333 this->write(separator); 285 this->write(separator);
334 separator = ", "; 286 separator = ", ";
335 this->writeModifiers(param->fModifiers, false); 287 this->writeModifiers(param->fModifiers);
336 std::vector<int> sizes; 288 this->writeType(param->fType);
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);
343 this->write(" " + param->fName); 289 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 }
351 } 290 }
352 this->writeLine(") {"); 291 this->write(") ");
353 292 this->writeBlock(*f.fBody);
354 fFunctionHeader = ""; 293 this->writeLine();
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());
369 } 294 }
370 295
371 void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers, 296 void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers) {
372 bool globalContext) { 297 this->write(modifiers.description());
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 }
417 } 298 }
418 299
419 void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { 300 void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) {
420 if (intf.fVariable.fName == "gl_PerVertex") { 301 if (intf.fVariable.fName == "gl_PerVertex") {
421 return; 302 return;
422 } 303 }
423 this->writeModifiers(intf.fVariable.fModifiers, true); 304 this->writeModifiers(intf.fVariable.fModifiers);
424 this->writeLine(intf.fVariable.fType.name() + " {"); 305 this->writeLine(intf.fVariable.fType.name() + " {");
425 fIndentation++; 306 fIndentation++;
426 for (const auto& f : intf.fVariable.fType.fields()) { 307 for (const auto& f : intf.fVariable.fType.fields()) {
427 this->writeModifiers(f.fModifiers, false); 308 this->writeModifiers(f.fModifiers);
428 this->writeType(*f.fType); 309 this->writeType(*f.fType);
429 this->writeLine(" " + f.fName + ";"); 310 this->writeLine(" " + f.fName + ";");
430 } 311 }
431 fIndentation--; 312 fIndentation--;
432 this->writeLine("};"); 313 this->writeLine("};");
433 } 314 }
434 315
435 void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool g lobal) { 316 void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl) {
436 ASSERT(decl.fVars.size() > 0); 317 ASSERT(decl.fVars.size() > 0);
437 this->writeModifiers(decl.fVars[0].fVar->fModifiers, global); 318 this->writeModifiers(decl.fVars[0].fVar->fModifiers);
438 this->writeType(decl.fBaseType); 319 this->writeType(decl.fBaseType);
439 std::string separator = " "; 320 std::string separator = " ";
440 for (const auto& var : decl.fVars) { 321 for (const auto& var : decl.fVars) {
441 ASSERT(var.fVar->fModifiers == decl.fVars[0].fVar->fModifiers); 322 ASSERT(var.fVar->fModifiers == decl.fVars[0].fVar->fModifiers);
442 this->write(separator); 323 this->write(separator);
443 separator = ", "; 324 separator = ", ";
444 this->write(var.fVar->fName); 325 this->write(var.fVar->fName);
445 for (const auto& size : var.fSizes) { 326 for (const auto& size : var.fSizes) {
446 this->write("["); 327 this->write("[");
447 if (size) { 328 this->writeExpression(*size, kTopLevel_Precedence);
448 this->writeExpression(*size, kTopLevel_Precedence);
449 }
450 this->write("]"); 329 this->write("]");
451 } 330 }
452 if (var.fValue) { 331 if (var.fValue) {
453 this->write(" = "); 332 this->write(" = ");
454 this->writeExpression(*var.fValue, kTopLevel_Precedence); 333 this->writeExpression(*var.fValue, kTopLevel_Precedence);
455 } 334 }
456 } 335 }
457 this->write(";"); 336 this->write(";");
458 } 337 }
459 338
460 void GLSLCodeGenerator::writeStatement(const Statement& s) { 339 void GLSLCodeGenerator::writeStatement(const Statement& s) {
461 switch (s.fKind) { 340 switch (s.fKind) {
462 case Statement::kBlock_Kind: 341 case Statement::kBlock_Kind:
463 this->writeBlock((Block&) s); 342 this->writeBlock((Block&) s);
464 break; 343 break;
465 case Statement::kExpression_Kind: 344 case Statement::kExpression_Kind:
466 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopL evel_Precedence); 345 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopL evel_Precedence);
467 this->write(";"); 346 this->write(";");
468 break; 347 break;
469 case Statement::kReturn_Kind: 348 case Statement::kReturn_Kind:
470 this->writeReturnStatement((ReturnStatement&) s); 349 this->writeReturnStatement((ReturnStatement&) s);
471 break; 350 break;
472 case Statement::kVarDeclarations_Kind: 351 case Statement::kVarDeclarations_Kind:
473 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclara tion, false); 352 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclara tion);
474 break; 353 break;
475 case Statement::kIf_Kind: 354 case Statement::kIf_Kind:
476 this->writeIfStatement((IfStatement&) s); 355 this->writeIfStatement((IfStatement&) s);
477 break; 356 break;
478 case Statement::kFor_Kind: 357 case Statement::kFor_Kind:
479 this->writeForStatement((ForStatement&) s); 358 this->writeForStatement((ForStatement&) s);
480 break; 359 break;
481 case Statement::kWhile_Kind: 360 case Statement::kWhile_Kind:
482 this->writeWhileStatement((WhileStatement&) s); 361 this->writeWhileStatement((WhileStatement&) s);
483 break; 362 break;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 if (r.fExpression) { 437 if (r.fExpression) {
559 this->write(" "); 438 this->write(" ");
560 this->writeExpression(*r.fExpression, kTopLevel_Precedence); 439 this->writeExpression(*r.fExpression, kTopLevel_Precedence);
561 } 440 }
562 this->write(";"); 441 this->write(";");
563 } 442 }
564 443
565 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out) { 444 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out) {
566 ASSERT(fOut == nullptr); 445 ASSERT(fOut == nullptr);
567 fOut = &out; 446 fOut = &out;
568 fProgramKind = program.fKind;
569 this->write("#version " + to_string(fCaps.fVersion)); 447 this->write("#version " + to_string(fCaps.fVersion));
570 if (fCaps.fStandard == GLCaps::kGLES_Standard && fCaps.fVersion >= 300) { 448 if (fCaps.fStandard == GLCaps::kGLES_Standard) {
571 this->write(" es"); 449 this->write(" es");
572 } else if (fCaps.fIsCoreProfile) {
573 this->write(" core");
574 } 450 }
575 this->writeLine(); 451 this->writeLine();
576 for (const auto& e : program.fElements) { 452 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) {
600 switch (e->fKind) { 453 switch (e->fKind) {
601 case ProgramElement::kExtension_Kind: 454 case ProgramElement::kExtension_Kind:
455 this->writeExtension((Extension&) *e);
602 break; 456 break;
603 case ProgramElement::kVar_Kind: { 457 case ProgramElement::kVar_Kind: {
604 VarDeclarations& decl = (VarDeclarations&) *e; 458 VarDeclarations& decl = (VarDeclarations&) *e;
605 if (decl.fVars.size() > 0) { 459 if (decl.fVars.size() > 0 &&
606 int builtin = decl.fVars[0].fVar->fModifiers.fLayout.fBuilti n; 460 decl.fVars[0].fVar->fModifiers.fLayout.fBuiltin == -1) {
607 if (builtin == -1) { 461 this->writeVarDeclarations(decl);
608 // normal var 462 this->writeLine();
609 this->writeVarDeclarations(decl, true);
610 this->writeLine();
611 } else if (builtin == SK_FRAGCOLOR_BUILTIN &&
612 fCaps.fMustDeclareFragmentShaderOutput) {
613 this->write("out ");
614 if (fCaps.fUsesPrecisionModifiers) {
615 this->write("mediump ");
616 }
617 this->writeLine("vec4 sk_FragColor;");
618 }
619 } 463 }
620 break; 464 break;
621 } 465 }
622 case ProgramElement::kInterfaceBlock_Kind: 466 case ProgramElement::kInterfaceBlock_Kind:
623 this->writeInterfaceBlock((InterfaceBlock&) *e); 467 this->writeInterfaceBlock((InterfaceBlock&) *e);
624 break; 468 break;
625 case ProgramElement::kFunction_Kind: 469 case ProgramElement::kFunction_Kind:
626 this->writeFunction((FunctionDefinition&) *e); 470 this->writeFunction((FunctionDefinition&) *e);
627 break; 471 break;
628 case ProgramElement::kModifiers_Kind:
629 this->writeModifiers(((ModifiersDeclaration&) *e).fModifiers, tr ue);
630 this->writeLine(";");
631 break;
632 default: 472 default:
633 printf("%s\n", e->description().c_str()); 473 printf("%s\n", e->description().c_str());
634 ABORT("unsupported program element"); 474 ABORT("unsupported program element");
635 } 475 }
636 } 476 }
637 fOut = nullptr; 477 fOut = nullptr;
638 } 478 }
639 479
640 } 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