Home | History | Annotate | Download | only in sksl
      1 /*
      2  * Copyright 2016 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #ifndef SKSL_GLSLCODEGENERATOR
      9 #define SKSL_GLSLCODEGENERATOR
     10 
     11 #include <stack>
     12 #include <tuple>
     13 #include <unordered_map>
     14 
     15 #include "SkSLCodeGenerator.h"
     16 #include "SkSLStringStream.h"
     17 #include "ir/SkSLBinaryExpression.h"
     18 #include "ir/SkSLBoolLiteral.h"
     19 #include "ir/SkSLConstructor.h"
     20 #include "ir/SkSLDoStatement.h"
     21 #include "ir/SkSLExtension.h"
     22 #include "ir/SkSLFloatLiteral.h"
     23 #include "ir/SkSLIfStatement.h"
     24 #include "ir/SkSLIndexExpression.h"
     25 #include "ir/SkSLInterfaceBlock.h"
     26 #include "ir/SkSLIntLiteral.h"
     27 #include "ir/SkSLFieldAccess.h"
     28 #include "ir/SkSLForStatement.h"
     29 #include "ir/SkSLFunctionCall.h"
     30 #include "ir/SkSLFunctionDeclaration.h"
     31 #include "ir/SkSLFunctionDefinition.h"
     32 #include "ir/SkSLPrefixExpression.h"
     33 #include "ir/SkSLPostfixExpression.h"
     34 #include "ir/SkSLProgramElement.h"
     35 #include "ir/SkSLReturnStatement.h"
     36 #include "ir/SkSLSetting.h"
     37 #include "ir/SkSLStatement.h"
     38 #include "ir/SkSLSwitchStatement.h"
     39 #include "ir/SkSLSwizzle.h"
     40 #include "ir/SkSLTernaryExpression.h"
     41 #include "ir/SkSLVarDeclarations.h"
     42 #include "ir/SkSLVarDeclarationsStatement.h"
     43 #include "ir/SkSLVariableReference.h"
     44 #include "ir/SkSLWhileStatement.h"
     45 
     46 namespace SkSL {
     47 
     48 #define kLast_Capability SpvCapabilityMultiViewport
     49 
     50 /**
     51  * Converts a Program into GLSL code.
     52  */
     53 class GLSLCodeGenerator : public CodeGenerator {
     54 public:
     55     enum Precedence {
     56         kParentheses_Precedence    =  1,
     57         kPostfix_Precedence        =  2,
     58         kPrefix_Precedence         =  3,
     59         kMultiplicative_Precedence =  4,
     60         kAdditive_Precedence       =  5,
     61         kShift_Precedence          =  6,
     62         kRelational_Precedence     =  7,
     63         kEquality_Precedence       =  8,
     64         kBitwiseAnd_Precedence     =  9,
     65         kBitwiseXor_Precedence     = 10,
     66         kBitwiseOr_Precedence      = 11,
     67         kLogicalAnd_Precedence     = 12,
     68         kLogicalXor_Precedence     = 13,
     69         kLogicalOr_Precedence      = 14,
     70         kTernary_Precedence        = 15,
     71         kAssignment_Precedence     = 16,
     72         kSequence_Precedence       = 17,
     73         kTopLevel_Precedence       = kSequence_Precedence
     74     };
     75 
     76     GLSLCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
     77                       OutputStream* out)
     78     : INHERITED(program, errors, out)
     79     , fLineEnding("\n")
     80     , fContext(*context) {}
     81 
     82     bool generateCode() override;
     83 
     84 protected:
     85     void write(const char* s);
     86 
     87     void writeLine();
     88 
     89     void writeLine(const char* s);
     90 
     91     void write(const String& s);
     92 
     93     void write(StringFragment s);
     94 
     95     void writeLine(const String& s);
     96 
     97     virtual void writeHeader();
     98 
     99     virtual bool usesPrecisionModifiers() const;
    100 
    101     virtual String getTypeName(const Type& type);
    102 
    103     void writeType(const Type& type);
    104 
    105     void writeExtension(const Extension& ext);
    106 
    107     void writeInterfaceBlock(const InterfaceBlock& intf);
    108 
    109     void writeFunctionStart(const FunctionDeclaration& f);
    110 
    111     void writeFunctionDeclaration(const FunctionDeclaration& f);
    112 
    113     virtual void writeFunction(const FunctionDefinition& f);
    114 
    115     void writeLayout(const Layout& layout);
    116 
    117     void writeModifiers(const Modifiers& modifiers, bool globalContext);
    118 
    119     void writeGlobalVars(const VarDeclaration& vs);
    120 
    121     virtual void writeVarInitializer(const Variable& var, const Expression& value);
    122 
    123     const char* getTypePrecision(const Type& type);
    124 
    125     void writeTypePrecision(const Type& type);
    126 
    127     void writeVarDeclarations(const VarDeclarations& decl, bool global);
    128 
    129     void writeFragCoord();
    130 
    131     virtual void writeVariableReference(const VariableReference& ref);
    132 
    133     void writeExpression(const Expression& expr, Precedence parentPrecedence);
    134 
    135     void writeIntrinsicCall(const FunctionCall& c);
    136 
    137     void writeMinAbsHack(Expression& absExpr, Expression& otherExpr);
    138 
    139     void writeDeterminantHack(const Expression& mat);
    140 
    141     void writeInverseHack(const Expression& mat);
    142 
    143     void writeTransposeHack(const Expression& mat);
    144 
    145     void writeInverseSqrtHack(const Expression& x);
    146 
    147     virtual void writeFunctionCall(const FunctionCall& c);
    148 
    149     void writeConstructor(const Constructor& c, Precedence parentPrecedence);
    150 
    151     void writeFieldAccess(const FieldAccess& f);
    152 
    153     virtual void writeSwizzle(const Swizzle& swizzle);
    154 
    155     static Precedence GetBinaryPrecedence(Token::Kind op);
    156 
    157     virtual void writeBinaryExpression(const BinaryExpression& b, Precedence parentPrecedence);
    158 
    159     void writeTernaryExpression(const TernaryExpression& t, Precedence parentPrecedence);
    160 
    161     virtual void writeIndexExpression(const IndexExpression& expr);
    162 
    163     void writePrefixExpression(const PrefixExpression& p, Precedence parentPrecedence);
    164 
    165     void writePostfixExpression(const PostfixExpression& p, Precedence parentPrecedence);
    166 
    167     void writeBoolLiteral(const BoolLiteral& b);
    168 
    169     virtual void writeIntLiteral(const IntLiteral& i);
    170 
    171     void writeFloatLiteral(const FloatLiteral& f);
    172 
    173     virtual void writeSetting(const Setting& s);
    174 
    175     void writeStatement(const Statement& s);
    176 
    177     void writeStatements(const std::vector<std::unique_ptr<Statement>>& statements);
    178 
    179     void writeBlock(const Block& b);
    180 
    181     virtual void writeIfStatement(const IfStatement& stmt);
    182 
    183     void writeForStatement(const ForStatement& f);
    184 
    185     void writeWhileStatement(const WhileStatement& w);
    186 
    187     void writeDoStatement(const DoStatement& d);
    188 
    189     virtual void writeSwitchStatement(const SwitchStatement& s);
    190 
    191     void writeReturnStatement(const ReturnStatement& r);
    192 
    193     virtual void writeProgramElement(const ProgramElement& e);
    194 
    195     const char* fLineEnding;
    196     const Context& fContext;
    197     StringStream fHeader;
    198     StringStream fExtraFunctions;
    199     String fFunctionHeader;
    200     Program::Kind fProgramKind;
    201     int fVarCount = 0;
    202     int fIndentation = 0;
    203     bool fAtLineStart = false;
    204     // Keeps track of which struct types we have written. Given that we are unlikely to ever write
    205     // more than one or two structs per shader, a simple linear search will be faster than anything
    206     // fancier.
    207     std::vector<const Type*> fWrittenStructs;
    208     std::set<String> fWrittenIntrinsics;
    209     // true if we have run into usages of dFdx / dFdy
    210     bool fFoundDerivatives = false;
    211     bool fFoundImageDecl = false;
    212     bool fFoundGSInvocations = false;
    213     bool fSetupFragPositionGlobal = false;
    214     bool fSetupFragPositionLocal = false;
    215     bool fSetupFragCoordWorkaround = false;
    216 
    217     typedef CodeGenerator INHERITED;
    218 };
    219 
    220 }
    221 
    222 #endif
    223