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/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 Loading... |
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 Loading... |
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 Loading... |
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 = ¶m->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::kHighp_Flag) { | |
411 this->write("highp "); | |
412 } | |
413 } | |
414 } | 298 } |
415 | 299 |
416 void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { | 300 void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { |
417 if (intf.fVariable.fName == "gl_PerVertex") { | 301 if (intf.fVariable.fName == "gl_PerVertex") { |
418 return; | 302 return; |
419 } | 303 } |
420 this->writeModifiers(intf.fVariable.fModifiers, true); | 304 this->writeModifiers(intf.fVariable.fModifiers); |
421 this->writeLine(intf.fVariable.fType.name() + " {"); | 305 this->writeLine(intf.fVariable.fType.name() + " {"); |
422 fIndentation++; | 306 fIndentation++; |
423 for (const auto& f : intf.fVariable.fType.fields()) { | 307 for (const auto& f : intf.fVariable.fType.fields()) { |
424 this->writeModifiers(f.fModifiers, false); | 308 this->writeModifiers(f.fModifiers); |
425 this->writeType(*f.fType); | 309 this->writeType(*f.fType); |
426 this->writeLine(" " + f.fName + ";"); | 310 this->writeLine(" " + f.fName + ";"); |
427 } | 311 } |
428 fIndentation--; | 312 fIndentation--; |
429 this->writeLine("};"); | 313 this->writeLine("};"); |
430 } | 314 } |
431 | 315 |
432 void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool g
lobal) { | 316 void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl) { |
433 ASSERT(decl.fVars.size() > 0); | 317 ASSERT(decl.fVars.size() > 0); |
434 this->writeModifiers(decl.fVars[0].fVar->fModifiers, global); | 318 this->writeModifiers(decl.fVars[0].fVar->fModifiers); |
435 this->writeType(decl.fBaseType); | 319 this->writeType(decl.fBaseType); |
436 std::string separator = " "; | 320 std::string separator = " "; |
437 for (const auto& var : decl.fVars) { | 321 for (const auto& var : decl.fVars) { |
438 ASSERT(var.fVar->fModifiers == decl.fVars[0].fVar->fModifiers); | 322 ASSERT(var.fVar->fModifiers == decl.fVars[0].fVar->fModifiers); |
439 this->write(separator); | 323 this->write(separator); |
440 separator = ", "; | 324 separator = ", "; |
441 this->write(var.fVar->fName); | 325 this->write(var.fVar->fName); |
442 for (const auto& size : var.fSizes) { | 326 for (const auto& size : var.fSizes) { |
443 this->write("["); | 327 this->write("["); |
444 if (size) { | 328 this->writeExpression(*size, kTopLevel_Precedence); |
445 this->writeExpression(*size, kTopLevel_Precedence); | |
446 } | |
447 this->write("]"); | 329 this->write("]"); |
448 } | 330 } |
449 if (var.fValue) { | 331 if (var.fValue) { |
450 this->write(" = "); | 332 this->write(" = "); |
451 this->writeExpression(*var.fValue, kTopLevel_Precedence); | 333 this->writeExpression(*var.fValue, kTopLevel_Precedence); |
452 } | 334 } |
453 } | 335 } |
454 this->write(";"); | 336 this->write(";"); |
455 } | 337 } |
456 | 338 |
457 void GLSLCodeGenerator::writeStatement(const Statement& s) { | 339 void GLSLCodeGenerator::writeStatement(const Statement& s) { |
458 switch (s.fKind) { | 340 switch (s.fKind) { |
459 case Statement::kBlock_Kind: | 341 case Statement::kBlock_Kind: |
460 this->writeBlock((Block&) s); | 342 this->writeBlock((Block&) s); |
461 break; | 343 break; |
462 case Statement::kExpression_Kind: | 344 case Statement::kExpression_Kind: |
463 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopL
evel_Precedence); | 345 this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopL
evel_Precedence); |
464 this->write(";"); | 346 this->write(";"); |
465 break; | 347 break; |
466 case Statement::kReturn_Kind: | 348 case Statement::kReturn_Kind: |
467 this->writeReturnStatement((ReturnStatement&) s); | 349 this->writeReturnStatement((ReturnStatement&) s); |
468 break; | 350 break; |
469 case Statement::kVarDeclarations_Kind: | 351 case Statement::kVarDeclarations_Kind: |
470 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclara
tion, false); | 352 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclara
tion); |
471 break; | 353 break; |
472 case Statement::kIf_Kind: | 354 case Statement::kIf_Kind: |
473 this->writeIfStatement((IfStatement&) s); | 355 this->writeIfStatement((IfStatement&) s); |
474 break; | 356 break; |
475 case Statement::kFor_Kind: | 357 case Statement::kFor_Kind: |
476 this->writeForStatement((ForStatement&) s); | 358 this->writeForStatement((ForStatement&) s); |
477 break; | 359 break; |
478 case Statement::kWhile_Kind: | 360 case Statement::kWhile_Kind: |
479 this->writeWhileStatement((WhileStatement&) s); | 361 this->writeWhileStatement((WhileStatement&) s); |
480 break; | 362 break; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 if (r.fExpression) { | 437 if (r.fExpression) { |
556 this->write(" "); | 438 this->write(" "); |
557 this->writeExpression(*r.fExpression, kTopLevel_Precedence); | 439 this->writeExpression(*r.fExpression, kTopLevel_Precedence); |
558 } | 440 } |
559 this->write(";"); | 441 this->write(";"); |
560 } | 442 } |
561 | 443 |
562 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out)
{ | 444 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out)
{ |
563 ASSERT(fOut == nullptr); | 445 ASSERT(fOut == nullptr); |
564 fOut = &out; | 446 fOut = &out; |
565 fProgramKind = program.fKind; | |
566 this->write("#version " + to_string(fCaps.fVersion)); | 447 this->write("#version " + to_string(fCaps.fVersion)); |
567 if (fCaps.fStandard == GLCaps::kGLES_Standard && fCaps.fVersion >= 300) { | 448 if (fCaps.fStandard == GLCaps::kGLES_Standard) { |
568 this->write(" es"); | 449 this->write(" es"); |
569 } else if (fCaps.fIsCoreProfile) { | |
570 this->write(" core"); | |
571 } | 450 } |
572 this->writeLine(); | 451 this->writeLine(); |
573 for (const auto& e : program.fElements) { | 452 for (const auto& e : program.fElements) { |
574 if (e->fKind == ProgramElement::kExtension_Kind) { | |
575 this->writeExtension((Extension&) *e); | |
576 } | |
577 } | |
578 if (fCaps.fStandard == GLCaps::kGLES_Standard) { | |
579 this->writeLine("precision mediump float;"); | |
580 } | |
581 for (const auto& e : program.fElements) { | |
582 switch (e->fKind) { | 453 switch (e->fKind) { |
583 case ProgramElement::kExtension_Kind: | 454 case ProgramElement::kExtension_Kind: |
| 455 this->writeExtension((Extension&) *e); |
584 break; | 456 break; |
585 case ProgramElement::kVar_Kind: { | 457 case ProgramElement::kVar_Kind: { |
586 VarDeclarations& decl = (VarDeclarations&) *e; | 458 VarDeclarations& decl = (VarDeclarations&) *e; |
587 if (decl.fVars.size() > 0) { | 459 if (decl.fVars.size() > 0 && |
588 int builtin = decl.fVars[0].fVar->fModifiers.fLayout.fBuilti
n; | 460 decl.fVars[0].fVar->fModifiers.fLayout.fBuiltin == -1) { |
589 if (builtin == -1) { | 461 this->writeVarDeclarations(decl); |
590 // normal var | 462 this->writeLine(); |
591 this->writeVarDeclarations(decl, true); | |
592 this->writeLine(); | |
593 } else if (builtin == SK_FRAGCOLOR_BUILTIN && | |
594 fCaps.fMustDeclareFragmentShaderOutput) { | |
595 this->write("out "); | |
596 if (fCaps.fUsesPrecisionModifiers) { | |
597 this->write("mediump "); | |
598 } | |
599 this->writeLine("vec4 sk_FragColor;"); | |
600 } | |
601 } | 463 } |
602 break; | 464 break; |
603 } | 465 } |
604 case ProgramElement::kInterfaceBlock_Kind: | 466 case ProgramElement::kInterfaceBlock_Kind: |
605 this->writeInterfaceBlock((InterfaceBlock&) *e); | 467 this->writeInterfaceBlock((InterfaceBlock&) *e); |
606 break; | 468 break; |
607 case ProgramElement::kFunction_Kind: | 469 case ProgramElement::kFunction_Kind: |
608 this->writeFunction((FunctionDefinition&) *e); | 470 this->writeFunction((FunctionDefinition&) *e); |
609 break; | 471 break; |
610 case ProgramElement::kModifiers_Kind: | |
611 this->writeModifiers(((ModifiersDeclaration&) *e).fModifiers, tr
ue); | |
612 this->writeLine(";"); | |
613 break; | |
614 default: | 472 default: |
615 printf("%s\n", e->description().c_str()); | 473 printf("%s\n", e->description().c_str()); |
616 ABORT("unsupported program element"); | 474 ABORT("unsupported program element"); |
617 } | 475 } |
618 } | 476 } |
619 fOut = nullptr; | 477 fOut = nullptr; |
620 } | 478 } |
621 | 479 |
622 } | 480 } |
OLD | NEW |