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" |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 this->fFunctionHeader += " " + absExpr.fType.name() + " " + tmpVar1 + ";\
n"; | 143 this->fFunctionHeader += " " + absExpr.fType.name() + " " + tmpVar1 + ";\
n"; |
144 this->fFunctionHeader += " " + otherExpr.fType.name() + " " + tmpVar2 + "
;\n"; | 144 this->fFunctionHeader += " " + otherExpr.fType.name() + " " + tmpVar2 + "
;\n"; |
145 this->write("((" + tmpVar1 + " = "); | 145 this->write("((" + tmpVar1 + " = "); |
146 this->writeExpression(absExpr, kTopLevel_Precedence); | 146 this->writeExpression(absExpr, kTopLevel_Precedence); |
147 this->write(") < (" + tmpVar2 + " = "); | 147 this->write(") < (" + tmpVar2 + " = "); |
148 this->writeExpression(otherExpr, kAssignment_Precedence); | 148 this->writeExpression(otherExpr, kAssignment_Precedence); |
149 this->write(") ? " + tmpVar1 + " : " + tmpVar2 + ")"); | 149 this->write(") ? " + tmpVar1 + " : " + tmpVar2 + ")"); |
150 } | 150 } |
151 | 151 |
152 void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { | 152 void GLSLCodeGenerator::writeFunctionCall(const FunctionCall& c) { |
153 if (!fCaps.fCanUseMinAndAbsTogether && c.fFunction.fName == "min") { | 153 if (!fCaps.fCanUseMinAndAbsTogether && c.fFunction.fName == "min" && c.fFunc
tion.fBuiltin) { |
154 ASSERT(c.fArguments.size() == 2); | 154 ASSERT(c.fArguments.size() == 2); |
155 if (is_abs(*c.fArguments[0])) { | 155 if (is_abs(*c.fArguments[0])) { |
156 this->writeMinAbsHack(*c.fArguments[0], *c.fArguments[1]); | 156 this->writeMinAbsHack(*c.fArguments[0], *c.fArguments[1]); |
157 return; | 157 return; |
158 } | 158 } |
159 if (is_abs(*c.fArguments[1])) { | 159 if (is_abs(*c.fArguments[1])) { |
160 // note that this violates the GLSL left-to-right evaluation semanti
cs. I doubt it will | 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. | 161 // ever end up mattering, but it's worth calling out. |
162 this->writeMinAbsHack(*c.fArguments[1], *c.fArguments[0]); | 162 this->writeMinAbsHack(*c.fArguments[1], *c.fArguments[0]); |
163 return; | 163 return; |
164 } | 164 } |
165 } | 165 } |
166 if (fCaps.fMustForceNegatedAtanParamToFloat && c.fFunction.fName == "atan" &
& | 166 if (fCaps.fMustForceNegatedAtanParamToFloat && c.fFunction.fName == "atan" &
& |
167 c.fArguments.size() == 2 && c.fArguments[1]->fKind == Expression::kPrefi
x_Kind) { | 167 c.fFunction.fBuiltin && c.fArguments.size() == 2 && |
| 168 c.fArguments[1]->fKind == Expression::kPrefix_Kind) { |
168 const PrefixExpression& p = (PrefixExpression&) *c.fArguments[1]; | 169 const PrefixExpression& p = (PrefixExpression&) *c.fArguments[1]; |
169 if (p.fOperator == Token::MINUS) { | 170 if (p.fOperator == Token::MINUS) { |
170 this->write("atan("); | 171 this->write("atan("); |
171 this->writeExpression(*c.fArguments[0], kSequence_Precedence); | 172 this->writeExpression(*c.fArguments[0], kSequence_Precedence); |
172 this->write(", -1.0 * "); | 173 this->write(", -1.0 * "); |
173 this->writeExpression(*p.fOperand, kMultiplicative_Precedence); | 174 this->writeExpression(*p.fOperand, kMultiplicative_Precedence); |
174 this->write(")"); | 175 this->write(")"); |
175 return; | 176 return; |
176 } | 177 } |
177 } | 178 } |
| 179 if (!fFoundDerivatives && fCaps.fShaderDerivativeExtensionString != "" && |
| 180 (c.fFunction.fName == "dFdx" || c.fFunction.fName == "dFdy") && c.fFunct
ion.fBuiltin) { |
| 181 ASSERT(fCaps.fShaderDerivativeSupport); |
| 182 fHeader << "#extension " << fCaps.fShaderDerivativeExtensionString << "
: require\n"; |
| 183 fFoundDerivatives = true; |
| 184 } |
178 this->write(c.fFunction.fName + "("); | 185 this->write(c.fFunction.fName + "("); |
179 const char* separator = ""; | 186 const char* separator = ""; |
180 for (const auto& arg : c.fArguments) { | 187 for (const auto& arg : c.fArguments) { |
181 this->write(separator); | 188 this->write(separator); |
182 separator = ", "; | 189 separator = ", "; |
183 this->writeExpression(*arg, kSequence_Precedence); | 190 this->writeExpression(*arg, kSequence_Precedence); |
184 } | 191 } |
185 this->write(")"); | 192 this->write(")"); |
186 } | 193 } |
187 | 194 |
(...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
571 this->write("return"); | 578 this->write("return"); |
572 if (r.fExpression) { | 579 if (r.fExpression) { |
573 this->write(" "); | 580 this->write(" "); |
574 this->writeExpression(*r.fExpression, kTopLevel_Precedence); | 581 this->writeExpression(*r.fExpression, kTopLevel_Precedence); |
575 } | 582 } |
576 this->write(";"); | 583 this->write(";"); |
577 } | 584 } |
578 | 585 |
579 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out)
{ | 586 void GLSLCodeGenerator::generateCode(const Program& program, std::ostream& out)
{ |
580 ASSERT(fOut == nullptr); | 587 ASSERT(fOut == nullptr); |
581 fOut = &out; | 588 fOut = &fHeader; |
582 fProgramKind = program.fKind; | 589 fProgramKind = program.fKind; |
583 this->write("#version " + to_string(fCaps.fVersion)); | 590 this->write("#version " + to_string(fCaps.fVersion)); |
584 if (fCaps.fStandard == GLCaps::kGLES_Standard && fCaps.fVersion >= 300) { | 591 if (fCaps.fStandard == GLCaps::kGLES_Standard && fCaps.fVersion >= 300) { |
585 this->write(" es"); | 592 this->write(" es"); |
586 } else if (fCaps.fIsCoreProfile) { | 593 } else if (fCaps.fIsCoreProfile) { |
587 this->write(" core"); | 594 this->write(" core"); |
588 } | 595 } |
589 this->writeLine(); | 596 this->writeLine(); |
590 for (const auto& e : program.fElements) { | 597 for (const auto& e : program.fElements) { |
591 if (e->fKind == ProgramElement::kExtension_Kind) { | 598 if (e->fKind == ProgramElement::kExtension_Kind) { |
592 this->writeExtension((Extension&) *e); | 599 this->writeExtension((Extension&) *e); |
593 } | 600 } |
594 } | 601 } |
| 602 std::stringstream body; |
| 603 fOut = &body; |
595 if (fCaps.fStandard == GLCaps::kGLES_Standard) { | 604 if (fCaps.fStandard == GLCaps::kGLES_Standard) { |
596 this->write("precision "); | 605 this->write("precision "); |
597 switch (program.fDefaultPrecision) { | 606 switch (program.fDefaultPrecision) { |
598 case Modifiers::kLowp_Flag: | 607 case Modifiers::kLowp_Flag: |
599 this->write("lowp"); | 608 this->write("lowp"); |
600 break; | 609 break; |
601 case Modifiers::kMediump_Flag: | 610 case Modifiers::kMediump_Flag: |
602 this->write("mediump"); | 611 this->write("mediump"); |
603 break; | 612 break; |
604 case Modifiers::kHighp_Flag: | 613 case Modifiers::kHighp_Flag: |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 case ProgramElement::kModifiers_Kind: | 651 case ProgramElement::kModifiers_Kind: |
643 this->writeModifiers(((ModifiersDeclaration&) *e).fModifiers, tr
ue); | 652 this->writeModifiers(((ModifiersDeclaration&) *e).fModifiers, tr
ue); |
644 this->writeLine(";"); | 653 this->writeLine(";"); |
645 break; | 654 break; |
646 default: | 655 default: |
647 printf("%s\n", e->description().c_str()); | 656 printf("%s\n", e->description().c_str()); |
648 ABORT("unsupported program element"); | 657 ABORT("unsupported program element"); |
649 } | 658 } |
650 } | 659 } |
651 fOut = nullptr; | 660 fOut = nullptr; |
| 661 |
| 662 out << fHeader.str(); |
| 663 out << body.str(); |
652 } | 664 } |
653 | 665 |
654 } | 666 } |
OLD | NEW |