diff options
Diffstat (limited to 'src/sksl/SkSLGLSLCodeGenerator.cpp')
-rw-r--r-- | src/sksl/SkSLGLSLCodeGenerator.cpp | 111 |
1 files changed, 47 insertions, 64 deletions
diff --git a/src/sksl/SkSLGLSLCodeGenerator.cpp b/src/sksl/SkSLGLSLCodeGenerator.cpp index 36a43cf927..4326f4c794 100644 --- a/src/sksl/SkSLGLSLCodeGenerator.cpp +++ b/src/sksl/SkSLGLSLCodeGenerator.cpp @@ -34,7 +34,7 @@ void GLSLCodeGenerator::write(const char* s) { void GLSLCodeGenerator::writeLine(const char* s) { this->write(s); - fOut->writeText(fLineEnding); + fOut->write8('\n'); fAtLineStart = true; } @@ -108,9 +108,6 @@ void GLSLCodeGenerator::writeExpression(const Expression& expr, Precedence paren case Expression::kPostfix_Kind: this->writePostfixExpression((PostfixExpression&) expr, parentPrecedence); break; - case Expression::kSetting_Kind: - this->writeSetting((Setting&) expr); - break; case Expression::kSwizzle_Kind: this->writeSwizzle((Swizzle&) expr); break; @@ -374,7 +371,7 @@ void GLSLCodeGenerator::writeSwizzle(const Swizzle& swizzle) { } } -GLSLCodeGenerator::Precedence GLSLCodeGenerator::GetBinaryPrecedence(Token::Kind op) { +static GLSLCodeGenerator::Precedence get_binary_precedence(Token::Kind op) { switch (op) { case Token::STAR: // fall through case Token::SLASH: // fall through @@ -416,7 +413,7 @@ GLSLCodeGenerator::Precedence GLSLCodeGenerator::GetBinaryPrecedence(Token::Kind void GLSLCodeGenerator::writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence) { - Precedence precedence = GetBinaryPrecedence(b.fOperator); + Precedence precedence = get_binary_precedence(b.fOperator); if (precedence >= parentPrecedence) { this->write("("); } @@ -483,10 +480,6 @@ void GLSLCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { this->write(to_string(f.fValue)); } -void GLSLCodeGenerator::writeSetting(const Setting& s) { - ABORT("internal error; setting was not folded to a constant during compilation\n"); -} - void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) { this->writeType(f.fDeclaration.fReturnType); this->write(" " + f.fDeclaration.fName + "("); @@ -524,7 +517,7 @@ void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) { fOut = oldOut; this->write(fFunctionHeader); - this->write(buffer.str()); + this->write(String(buffer.data(), buffer.size())); } void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers, @@ -624,10 +617,6 @@ void GLSLCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { this->writeLine(";"); } -void GLSLCodeGenerator::writeVarInitializer(const Variable& var, const Expression& value) { - this->writeExpression(value, kTopLevel_Precedence); -} - void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool global) { ASSERT(decl.fVars.size() > 0); bool wroteType = false; @@ -651,7 +640,7 @@ void GLSLCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, bool g } if (var.fValue) { this->write(" = "); - this->writeVarInitializer(*var.fVar, *var.fValue); + this->writeExpression(*var.fValue, kTopLevel_Precedence); } if (!fFoundImageDecl && var.fVar->fType == *fContext.fImage2D_Type) { if (fProgram.fSettings.fCaps->imageLoadStoreExtensionString()) { @@ -808,7 +797,10 @@ void GLSLCodeGenerator::writeReturnStatement(const ReturnStatement& r) { this->write(";"); } -void GLSLCodeGenerator::writeHeader() { +bool GLSLCodeGenerator::generateCode() { + OutputStream* rawOut = fOut; + fOut = &fHeader; + fProgramKind = fProgram.fKind; this->write(fProgram.fSettings.fCaps->versionDeclString()); this->writeLine(); for (const auto& e : fProgram.fElements) { @@ -816,6 +808,8 @@ void GLSLCodeGenerator::writeHeader() { this->writeExtension((Extension&) *e); } } + StringStream body; + fOut = &body; if (fProgram.fSettings.fCaps->usesPrecisionModifiers()) { this->write("precision "); switch (fProgram.fDefaultPrecision) { @@ -834,58 +828,47 @@ void GLSLCodeGenerator::writeHeader() { } this->writeLine(" float;"); } -} - -void GLSLCodeGenerator::writeProgramElement(const ProgramElement& e) { - switch (e.fKind) { - case ProgramElement::kExtension_Kind: - break; - case ProgramElement::kVar_Kind: { - VarDeclarations& decl = (VarDeclarations&) e; - if (decl.fVars.size() > 0) { - int builtin = ((VarDeclaration&) *decl.fVars[0]).fVar->fModifiers.fLayout.fBuiltin; - if (builtin == -1) { - // normal var - this->writeVarDeclarations(decl, true); - this->writeLine(); - } else if (builtin == SK_FRAGCOLOR_BUILTIN && - fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput()) { - this->write("out "); - if (fProgram.fSettings.fCaps->usesPrecisionModifiers()) { - this->write("mediump "); + for (const auto& e : fProgram.fElements) { + switch (e->fKind) { + case ProgramElement::kExtension_Kind: + break; + case ProgramElement::kVar_Kind: { + VarDeclarations& decl = (VarDeclarations&) *e; + if (decl.fVars.size() > 0) { + ASSERT(decl.fVars[0]->fKind == Statement::kVarDeclaration_Kind); + int builtin = + ((VarDeclaration&) *decl.fVars[0]).fVar->fModifiers.fLayout.fBuiltin; + if (builtin == -1) { + // normal var + this->writeVarDeclarations(decl, true); + this->writeLine(); + } else if (builtin == SK_FRAGCOLOR_BUILTIN && + fProgram.fSettings.fCaps->mustDeclareFragmentShaderOutput()) { + this->write("out "); + if (fProgram.fSettings.fCaps->usesPrecisionModifiers()) { + this->write("mediump "); + } + this->writeLine("vec4 sk_FragColor;"); } - this->writeLine("vec4 sk_FragColor;"); } + break; } - break; + case ProgramElement::kInterfaceBlock_Kind: + this->writeInterfaceBlock((InterfaceBlock&) *e); + break; + case ProgramElement::kFunction_Kind: + this->writeFunction((FunctionDefinition&) *e); + break; + case ProgramElement::kModifiers_Kind: + this->writeModifiers(((ModifiersDeclaration&) *e).fModifiers, true); + this->writeLine(";"); + break; + default: + printf("%s\n", e->description().c_str()); + ABORT("unsupported program element"); } - case ProgramElement::kInterfaceBlock_Kind: - this->writeInterfaceBlock((InterfaceBlock&) e); - break; - case ProgramElement::kFunction_Kind: - this->writeFunction((FunctionDefinition&) e); - break; - case ProgramElement::kModifiers_Kind: - this->writeModifiers(((ModifiersDeclaration&) e).fModifiers, true); - this->writeLine(";"); - break; - default: - printf("%s\n", e.description().c_str()); - ABORT("unsupported program element"); - } -} - -bool GLSLCodeGenerator::generateCode() { - OutputStream* rawOut = fOut; - fOut = &fHeader; - fProgramKind = fProgram.fKind; - this->writeHeader(); - StringStream body; - fOut = &body; - for (const auto& e : fProgram.fElements) { - this->writeProgramElement(*e); } - fOut = rawOut; + fOut = nullptr; write_stringstream(fHeader, *rawOut); write_stringstream(body, *rawOut); |