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

Unified Diff: src/sksl/SkSLGLSLCodeGenerator.cpp

Issue 2185393003: added initial GLSL support to skslc (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: added initial GLSL support to skslc Created 4 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: src/sksl/SkSLGLSLCodeGenerator.cpp
diff --git a/src/sksl/SkSLGLSLCodeGenerator.cpp b/src/sksl/SkSLGLSLCodeGenerator.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..09fd91e72d2cccfd94e6ea7e94e234c3bce0a756
--- /dev/null
+++ b/src/sksl/SkSLGLSLCodeGenerator.cpp
@@ -0,0 +1,422 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkSLGLSLCodeGenerator.h"
+
+#include "string.h"
+
+#include "GLSL.std.450.h"
+
+#include "ir/SkSLExpressionStatement.h"
+#include "ir/SkSLExtension.h"
+#include "ir/SkSLIndexExpression.h"
+#include "ir/SkSLVariableReference.h"
+
+namespace SkSL {
+
+void GLSLCodeGenerator::write(const char* s) {
+ if (fAtLineStart) {
dogben 2016/07/31 23:20:20 nit: maybe "if (s[0] == '\0') return;"? Maybe I'm
ethannicholas 2016/08/02 16:13:17 Done.
+ for (int i = 0; i < fIndentation; i++) {
+ *fOut << " ";
+ }
+ }
+ *fOut << s;
+ fAtLineStart = false;
+}
+
+void GLSLCodeGenerator::writeLine(const char* s) {
+ this->write(s);
+ *fOut << "\n";
+ fAtLineStart = true;
+}
+
+void GLSLCodeGenerator::write(std::string s) {
+ this->write(s.c_str());
+}
+
+void GLSLCodeGenerator::writeLine(std::string s) {
+ this->writeLine(s.c_str());
+}
+
+void GLSLCodeGenerator::write(std::string& s) {
+ this->write(s.c_str());
+}
+
+void GLSLCodeGenerator::writeLine(std::string& s) {
+ this->writeLine(s.c_str());
+}
+
+void GLSLCodeGenerator::writeLine() {
+ this->writeLine("");
+}
+
+void GLSLCodeGenerator::writeExtension(Extension& ext) {
+ this->writeLine("#extension " + ext.fName + " : enable");
+}
+
+void GLSLCodeGenerator::writeStruct(const Type& type) {
+ this->writeLine("struct " + type.name() + " {");
+ fIndentation++;
+ for (const auto& f : type.fields()) {
+ this->writeLine(f.fType.name() + " " + f.fName + ";");
dogben 2016/07/31 23:20:20 Missing modifiers?
ethannicholas 2016/08/02 16:13:17 Fixed.
+ }
+ fIndentation--;
+ this->writeLine("};");
+}
+
+void GLSLCodeGenerator::writeExpression(Expression& expr, Precedence parentPrecedence) {
+ switch (expr.fKind) {
+ case Expression::kBinary_Kind:
dogben 2016/07/31 23:20:20 nit: Indentation is inconsistent.
ethannicholas 2016/08/02 16:13:17 Damn you, tabs!
+ this->writeBinaryExpression((BinaryExpression&) expr, parentPrecedence);
+ break;
+ case Expression::kBoolLiteral_Kind:
+ this->writeBoolLiteral((BoolLiteral&) expr);
+ break;
+ case Expression::kConstructor_Kind:
+ this->writeConstructor((Constructor&) expr);
+ break;
+ case Expression::kIntLiteral_Kind:
+ this->writeIntLiteral((IntLiteral&) expr);
+ break;
+ case Expression::kFieldAccess_Kind:
+ this->writeFieldAccess(((FieldAccess&) expr));
+ break;
+ case Expression::kFloatLiteral_Kind:
+ this->writeFloatLiteral(((FloatLiteral&) expr));
+ break;
+ case Expression::kFunctionCall_Kind:
+ this->writeFunctionCall((FunctionCall&) expr);
+ break;
+ case Expression::kPrefix_Kind:
+ this->writePrefixExpression((PrefixExpression&) expr);
dogben 2016/07/31 23:20:20 Don't we need to pass in parentPrecedence? Maybe i
ethannicholas 2016/08/02 16:13:17 We sure do!
+ break;
+ case Expression::kPostfix_Kind:
+ this->writePostfixExpression((PostfixExpression&) expr);
+ break;
+ case Expression::kSwizzle_Kind:
+ this->writeSwizzle((Swizzle&) expr);
+ break;
+ case Expression::kVariableReference_Kind:
+ this->writeVariableReference((VariableReference&) expr);
+ break;
+ case Expression::kTernary_Kind:
+ this->writeTernaryExpression((TernaryExpression&) expr, parentPrecedence);
+ break;
+ case Expression::kIndex_Kind:
+ this->writeIndexExpression((IndexExpression&) expr);
+ break;
+ default:
+ ABORT("unsupported expression: %s", expr.description().c_str());
+ }
+}
+
+void GLSLCodeGenerator::writeFunctionCall(FunctionCall& c) {
+ this->write(c.fFunction.fName + "(");
+ const char* separator = "";
+ for (const auto& arg : c.fArguments) {
+ this->write(separator);
+ separator = ", ";
+ this->writeExpression(*arg, kTopLevel_Precedence);
dogben 2016/07/31 23:20:20 nit: kSequence_Precedence, although it shouldn't m
+ }
+ this->write(")");
+}
+
+void GLSLCodeGenerator::writeConstructor(Constructor& c) {
+ this->write(c.fType.name() + "(");
+ const char* separator = "";
+ for (const auto& arg : c.fArguments) {
+ this->write(separator);
+ separator = ", ";
+ this->writeExpression(*arg, kTopLevel_Precedence);
+ }
+ this->write(")");
+}
+
+void GLSLCodeGenerator::writeVariableReference(VariableReference& ref) {
+ this->write(ref.fVariable.fName);
+}
+
+void GLSLCodeGenerator::writeIndexExpression(IndexExpression& expr) {
+ this->writeExpression(*expr.fBase, kPostfix_Precedence);
+ this->write("[");
+ this->writeExpression(*expr.fIndex, kTopLevel_Precedence);
+ this->write("]");
+}
+
+void GLSLCodeGenerator::writeFieldAccess(FieldAccess& f) {
+ if (!f.fInAnonymousInterfaceBlock) {
+ this->writeExpression(*f.fBase, kPostfix_Precedence);
+ this->write(".");
+ }
+ this->write(f.fBase->fType.fields()[f.fFieldIndex].fName);
+}
+
+void GLSLCodeGenerator::writeSwizzle(Swizzle& swizzle) {
+ this->writeExpression(*swizzle.fBase, kPostfix_Precedence);
+ this->write(".");
+ for (int c : swizzle.fComponents) {
+ this->write(&("x\0y\0z\0w\0"[c * 2]));
egdaniel 2016/08/01 13:18:27 not that functionally it really matters, but is th
ethannicholas 2016/08/02 16:13:17 Correct. Since it doesn't matter whether you use r
+ }
+}
+
+static GLSLCodeGenerator::Precedence get_binary_precedence(Token::Kind op) {
+ switch (op) {
+ case Token::STAR: // fall through
+ case Token::SLASH: // fall through
+ case Token::PERCENT: return GLSLCodeGenerator::kMultiplicative_Precedence;
+ case Token::PLUS: // fall through
+ case Token::MINUS: return GLSLCodeGenerator::kAdditive_Precedence;
+ case Token::SHL: // fall through
+ case Token::SHR: return GLSLCodeGenerator::kShift_Precedence;
+ case Token::LT: // fall through
+ case Token::GT: // fall through
+ case Token::LTEQ: // fall through
+ case Token::GTEQ: return GLSLCodeGenerator::kRelational_Precedence;
+ case Token::EQEQ: // fall through
+ case Token::NEQ: return GLSLCodeGenerator::kEquality_Precedence;
+ case Token::BITWISEAND: return GLSLCodeGenerator::kBitwiseAnd_Precedence;
+ case Token::BITWISEXOR: return GLSLCodeGenerator::kBitwiseXor_Precedence;
+ case Token::BITWISEOR: return GLSLCodeGenerator::kBitwiseOr_Precedence;
+ case Token::LOGICALAND: return GLSLCodeGenerator::kLogicalAnd_Precedence;
+ case Token::LOGICALXOR: return GLSLCodeGenerator::kLogicalXor_Precedence;
+ case Token::LOGICALOR: return GLSLCodeGenerator::kLogicalOr_Precedence;
+ case Token::EQ: // fall through
+ case Token::PLUSEQ: // fall through
+ case Token::MINUSEQ: // fall through
+ case Token::STAREQ: // fall through
+ case Token::SLASHEQ: // fall through
+ case Token::PERCENTEQ: // fall through
+ case Token::SHLEQ: // fall through
+ case Token::SHREQ: // fall through
+ case Token::LOGICALANDEQ: // fall through
+ case Token::LOGICALXOREQ: // fall through
+ case Token::LOGICALOREQ: return GLSLCodeGenerator::kAssignment_Precedence;
+ default: ABORT("unsupported binary operator");
+ }
+}
+
+void GLSLCodeGenerator::writeBinaryExpression(BinaryExpression& b, Precedence parentPrecedence) {
+ Precedence precedence = get_binary_precedence(b.fOperator);
+ if (precedence >= parentPrecedence) {
+ this->write("(");
+ }
+ this->writeExpression(*b.fLeft, precedence);
+ this->write(" " + Token::OperatorName(b.fOperator) + " ");
+ this->writeExpression(*b.fRight, precedence);
+ if (precedence >= parentPrecedence) {
+ this->write(")");
+ }
+}
+
+void GLSLCodeGenerator::writeTernaryExpression(TernaryExpression& t, Precedence parentPrecedence) {
+ if (kTernary_Precedence >= parentPrecedence) {
+ this->write("(");
+ }
+ this->writeExpression(*t.fTest, kTernary_Precedence);
+ this->write(" ? ");
+ this->writeExpression(*t.fIfTrue, kTernary_Precedence);
+ this->write(" : ");
+ this->writeExpression(*t.fIfFalse, kTernary_Precedence);
+ if (kTernary_Precedence >= parentPrecedence) {
+ this->write(")");
+ }
+}
+
+void GLSLCodeGenerator::writePrefixExpression(PrefixExpression& p) {
+ this->write(Token::OperatorName(p.fOperator));
+ this->writeExpression(*p.fOperand, kPrefix_Precedence);
+}
+
+void GLSLCodeGenerator::writePostfixExpression(PostfixExpression& p) {
+ this->writeExpression(*p.fOperand, kPostfix_Precedence);
+ this->write(Token::OperatorName(p.fOperator));
+}
+
+void GLSLCodeGenerator::writeBoolLiteral(BoolLiteral& b) {
+ this->write(b.fValue ? "true" : "false");
+}
+
+void GLSLCodeGenerator::writeIntLiteral(IntLiteral& i) {
+ this->write(to_string(i.fValue));
+}
+
+void GLSLCodeGenerator::writeFloatLiteral(FloatLiteral& f) {
+ this->write(to_string(f.fValue));
dogben 2016/07/31 23:20:19 Docs for std::to_string say std::to_string(1e-9) =
ethannicholas 2016/08/02 16:13:17 My implementation of to_string (note that this is
+}
+
+void GLSLCodeGenerator::writeFunction(const FunctionDefinition& f) {
+ this->write(f.fDeclaration.fReturnType.name() + " " + f.fDeclaration.fName + "(");
+ const char* separator = "";
+ for (const auto& param : f.fDeclaration.fParameters) {
+ this->write(separator);
+ separator = ", ";
+ this->write(param->fType.name() + " " + param->fName);
dogben 2016/07/31 23:20:19 Missing modifiers and sizes.
ethannicholas 2016/08/02 16:13:17 Fixed.
+ }
+ this->write(") ");
+ this->writeBlock(*f.fBody);
+}
+
+void GLSLCodeGenerator::writeModifiers(const Modifiers& modifiers) {
+ this->write(modifiers.description());
+}
+
+void GLSLCodeGenerator::writeInterfaceBlock(InterfaceBlock& intf) {
+ if (intf.fVariable.fName == "gl_PerVertex") {
+ return;
+ }
+ this->writeModifiers(intf.fVariable.fModifiers);
+ this->writeStruct(intf.fVariable.fType);
dogben 2016/07/31 23:20:20 We're generating a struct for an interface? I don'
ethannicholas 2016/08/02 16:13:17 Stupid extra "struct" keyword. Fixed.
+}
+
+void GLSLCodeGenerator::writeVarDeclaration(VarDeclaration& decl) {
+ for (size_t i = 0; i < decl.fVars.size(); i++) {
+ if (decl.fVars[i]->fModifiers.fLayout.fBuiltin >= 0) {
dogben 2016/07/31 23:20:20 I don't know what this is about. builtin isn't doc
ethannicholas 2016/08/02 16:13:17 It's from the SPIR-V spec. I've added a comment to
+ continue;
+ }
+ this->writeModifiers(decl.fVars[i]->fModifiers);
+ this->write(decl.fVars[i]->fType.name() + " " + decl.fVars[i]->fName);
dogben 2016/07/31 23:20:20 If this is a top-level var declaration, fType migh
ethannicholas 2016/08/02 16:13:17 Done.
+ if (decl.fValues[i]) {
+ this->write(" = ");
+ this->writeExpression(*decl.fValues[i], kTopLevel_Precedence);
dogben 2016/07/31 23:20:20 nit: kSequence_Precedence, with same caveat as abo
ethannicholas 2016/08/02 16:13:17 Done.
+ }
+ this->writeLine(";");
+ }
+}
+
+void GLSLCodeGenerator::writeStatement(Statement& s) {
+ switch (s.fKind) {
+ case Statement::kBlock_Kind:
+ this->writeBlock((Block&) s);
+ break;
+ case Statement::kExpression_Kind:
+ this->writeExpression(*((ExpressionStatement&) s).fExpression, kTopLevel_Precedence);
+ this->writeLine(";");
+ break;
+ case Statement::kReturn_Kind:
+ this->writeReturnStatement((ReturnStatement&) s);
+ break;
+ case Statement::kVarDeclaration_Kind:
+ this->writeVarDeclaration(*((VarDeclarationStatement&) s).fDeclaration);
+ break;
+ case Statement::kIf_Kind:
+ this->writeIfStatement((IfStatement&) s);
+ break;
+ case Statement::kFor_Kind:
+ this->writeForStatement((ForStatement&) s);
+ break;
+ case Statement::kWhile_Kind:
+ this->writeWhileStatement((WhileStatement&) s);
+ break;
+ case Statement::kDo_Kind:
+ this->writeDoStatement((DoStatement&) s);
+ break;
+ case Statement::kBreak_Kind:
+ this->writeLine("break;");
+ break;
+ case Statement::kContinue_Kind:
+ this->writeLine("continue;");
+ break;
+ case Statement::kDiscard_Kind:
+ this->writeLine("discard;");
+ break;
+ default:
+ ABORT("unsupported statement: %s", s.description().c_str());
+ }
+}
+
+void GLSLCodeGenerator::writeBlock(Block& b) {
+ this->writeLine("{");
+ fIndentation++;
+ for (const auto& s : b.fStatements) {
+ this->writeStatement(*s);
+ }
+ fIndentation--;
+ this->writeLine("}");
+}
+
+void GLSLCodeGenerator::writeIfStatement(IfStatement& stmt) {
+ this->write("if (");
+ this->writeExpression(*stmt.fTest, kTopLevel_Precedence);
+ this->write(") ");
+ this->writeStatement(*stmt.fIfTrue);
+ if (stmt.fIfFalse) {
+ this->write("else ");
+ this->writeStatement(*stmt.fIfFalse);
+ }
+}
+
+void GLSLCodeGenerator::writeForStatement(ForStatement& f) {
+ this->write("for (");
+ if (f.fInitializer) {
+ this->writeStatement(*f.fInitializer);
+ }
dogben 2016/07/31 23:20:20 else write semicolon
ethannicholas 2016/08/02 16:13:17 Done.
+ if (f.fTest) {
+ this->writeExpression(*f.fTest, kTopLevel_Precedence);
+ }
+ this->write("; ");
+ if (f.fNext) {
+ this->writeExpression(*f.fNext, kTopLevel_Precedence);
+ }
+ this->write(") ");
+ this->writeStatement(*f.fStatement);
+}
+
+void GLSLCodeGenerator::writeWhileStatement(WhileStatement& w) {
+ this->write("while (");
+ this->writeExpression(*w.fTest, kTopLevel_Precedence);
+ this->write(") ");
+ this->writeStatement(*w.fStatement);
+}
+
+void GLSLCodeGenerator::writeDoStatement(DoStatement& d) {
+ this->write("do ");
+ this->writeStatement(*d.fStatement);
+ this->write("while (");
+ this->writeExpression(*d.fTest, kTopLevel_Precedence);
+ this->writeLine(");");
+}
+
+void GLSLCodeGenerator::writeReturnStatement(ReturnStatement& r) {
+ this->write("return");
+ if (r.fExpression) {
+ this->write(" ");
+ this->writeExpression(*r.fExpression, kTopLevel_Precedence);
+ }
+ this->writeLine(";");
+}
+
+void GLSLCodeGenerator::generateCode(Program& program, std::ostream& out) {
+ ASSERT(fOut == nullptr);
+ fOut = &out;
+ this->write("#version " + to_string(fCaps.fVersion));
+ if (fCaps.fStandard == GLCaps::kGLES_Standard) {
egdaniel 2016/08/01 13:18:27 I don't see fCaps used anywhere else so why split
ethannicholas 2016/08/02 16:13:17 Going to start paying a lot more attention to it i
+ this->write(" es");
+ }
+ this->writeLine();
+ for (const auto& e : program.fElements) {
+ switch (e->fKind) {
+ case ProgramElement::kExtension_Kind:
+ this->writeExtension((Extension&) *e);
+ break;
+ case ProgramElement::kVar_Kind:
+ this->writeVarDeclaration((VarDeclaration&) *e);
+ break;
+ case ProgramElement::kInterfaceBlock_Kind:
+ this->writeInterfaceBlock((InterfaceBlock&) *e);
+ break;
+ case ProgramElement::kFunction_Kind:
+ this->writeFunction((FunctionDefinition&) *e);
+ break;
+ default:
+ printf("%s\n", e->description().c_str());
+ ABORT("unsupported program element");
+ }
+ }
+ fOut = nullptr;
+}
+
+}

Powered by Google App Engine
This is Rietveld 408576698