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_SPIRVCODEGENERATOR
      9 #define SKSL_SPIRVCODEGENERATOR
     10 
     11 #include <stack>
     12 #include <tuple>
     13 #include <unordered_map>
     14 
     15 #include "SkSLCodeGenerator.h"
     16 #include "SkSLMemoryLayout.h"
     17 #include "ir/SkSLBinaryExpression.h"
     18 #include "ir/SkSLBoolLiteral.h"
     19 #include "ir/SkSLConstructor.h"
     20 #include "ir/SkSLDoStatement.h"
     21 #include "ir/SkSLFloatLiteral.h"
     22 #include "ir/SkSLIfStatement.h"
     23 #include "ir/SkSLIndexExpression.h"
     24 #include "ir/SkSLInterfaceBlock.h"
     25 #include "ir/SkSLIntLiteral.h"
     26 #include "ir/SkSLFieldAccess.h"
     27 #include "ir/SkSLForStatement.h"
     28 #include "ir/SkSLFunctionCall.h"
     29 #include "ir/SkSLFunctionDeclaration.h"
     30 #include "ir/SkSLFunctionDefinition.h"
     31 #include "ir/SkSLPrefixExpression.h"
     32 #include "ir/SkSLPostfixExpression.h"
     33 #include "ir/SkSLProgramElement.h"
     34 #include "ir/SkSLReturnStatement.h"
     35 #include "ir/SkSLStatement.h"
     36 #include "ir/SkSLSwitchStatement.h"
     37 #include "ir/SkSLSwizzle.h"
     38 #include "ir/SkSLTernaryExpression.h"
     39 #include "ir/SkSLVarDeclarations.h"
     40 #include "ir/SkSLVarDeclarationsStatement.h"
     41 #include "ir/SkSLVariableReference.h"
     42 #include "ir/SkSLWhileStatement.h"
     43 #include "spirv.h"
     44 
     45 union ConstantValue {
     46     ConstantValue(int64_t i)
     47         : fInt(i) {}
     48 
     49     ConstantValue(double d)
     50         : fDouble(d) {}
     51 
     52     bool operator==(const ConstantValue& other) const {
     53         return fInt == other.fInt;
     54     }
     55 
     56     int64_t fInt;
     57     double fDouble;
     58 };
     59 
     60 enum class ConstantType {
     61     kInt,
     62     kUInt,
     63     kShort,
     64     kUShort,
     65     kFloat,
     66     kDouble,
     67     kHalf,
     68 };
     69 
     70 namespace std {
     71 
     72 template <>
     73 struct hash<std::pair<ConstantValue, ConstantType>> {
     74     size_t operator()(const std::pair<ConstantValue, ConstantType>& key) const {
     75         return key.first.fInt ^ (int) key.second;
     76     }
     77 };
     78 
     79 }
     80 
     81 namespace SkSL {
     82 
     83 #define kLast_Capability SpvCapabilityMultiViewport
     84 
     85 /**
     86  * Converts a Program into a SPIR-V binary.
     87  */
     88 class SPIRVCodeGenerator : public CodeGenerator {
     89 public:
     90     class LValue {
     91     public:
     92         virtual ~LValue() {}
     93 
     94         // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced
     95         // by a pointer (e.g. vector swizzles), returns 0.
     96         virtual SpvId getPointer() = 0;
     97 
     98         virtual SpvId load(OutputStream& out) = 0;
     99 
    100         virtual void store(SpvId value, OutputStream& out) = 0;
    101     };
    102 
    103     SPIRVCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors,
    104                        OutputStream* out)
    105     : INHERITED(program, errors, out)
    106     , fContext(*context)
    107     , fDefaultLayout(MemoryLayout::k140_Standard)
    108     , fCapabilities(0)
    109     , fIdCount(1)
    110     , fBoolTrue(0)
    111     , fBoolFalse(0)
    112     , fSetupFragPosition(false)
    113     , fCurrentBlock(0)
    114     , fSynthetics(nullptr, errors) {
    115         this->setupIntrinsics();
    116     }
    117 
    118     bool generateCode() override;
    119 
    120 private:
    121     enum IntrinsicKind {
    122         kGLSL_STD_450_IntrinsicKind,
    123         kSPIRV_IntrinsicKind,
    124         kSpecial_IntrinsicKind
    125     };
    126 
    127     enum SpecialIntrinsic {
    128         kAtan_SpecialIntrinsic,
    129         kClamp_SpecialIntrinsic,
    130         kMax_SpecialIntrinsic,
    131         kMin_SpecialIntrinsic,
    132         kMix_SpecialIntrinsic,
    133         kMod_SpecialIntrinsic,
    134         kDFdy_SpecialIntrinsic,
    135         kSaturate_SpecialIntrinsic,
    136         kSubpassLoad_SpecialIntrinsic,
    137         kTexture_SpecialIntrinsic,
    138     };
    139 
    140     enum class Precision {
    141         kLow,
    142         kHigh,
    143     };
    144 
    145     void setupIntrinsics();
    146 
    147     SpvId nextId();
    148 
    149     Type getActualType(const Type& type);
    150 
    151     SpvId getType(const Type& type);
    152 
    153     SpvId getType(const Type& type, const MemoryLayout& layout);
    154 
    155     SpvId getImageType(const Type& type);
    156 
    157     SpvId getFunctionType(const FunctionDeclaration& function);
    158 
    159     SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass);
    160 
    161     SpvId getPointerType(const Type& type, const MemoryLayout& layout,
    162                          SpvStorageClass_ storageClass);
    163 
    164     void writePrecisionModifier(Precision precision, SpvId id);
    165 
    166     void writePrecisionModifier(const Type& type, SpvId id);
    167 
    168     std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out);
    169 
    170     void writeLayout(const Layout& layout, SpvId target);
    171 
    172     void writeLayout(const Layout& layout, SpvId target, int member);
    173 
    174     void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId);
    175 
    176     void writeProgramElement(const ProgramElement& pe, OutputStream& out);
    177 
    178     SpvId writeInterfaceBlock(const InterfaceBlock& intf);
    179 
    180     SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out);
    181 
    182     SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out);
    183 
    184     SpvId writeFunction(const FunctionDefinition& f, OutputStream& out);
    185 
    186     void writeGlobalVars(Program::Kind kind, const VarDeclarations& v, OutputStream& out);
    187 
    188     void writeVarDeclarations(const VarDeclarations& decl, OutputStream& out);
    189 
    190     SpvId writeVariableReference(const VariableReference& ref, OutputStream& out);
    191 
    192     std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out);
    193 
    194     SpvId writeExpression(const Expression& expr, OutputStream& out);
    195 
    196     SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out);
    197 
    198     SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out);
    199 
    200 
    201     void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
    202                                       SpvId signedInst, SpvId unsignedInst,
    203                                       const std::vector<SpvId>& args, OutputStream& out);
    204 
    205     /**
    206      * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the
    207      * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2),
    208      * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float,
    209      * vec2, vec3).
    210      */
    211     std::vector<SpvId> vectorize(const std::vector<std::unique_ptr<Expression>>& args,
    212                                  OutputStream& out);
    213 
    214     SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out);
    215 
    216     SpvId writeConstantVector(const Constructor& c);
    217 
    218     SpvId writeFloatConstructor(const Constructor& c, OutputStream& out);
    219 
    220     SpvId writeIntConstructor(const Constructor& c, OutputStream& out);
    221 
    222     SpvId writeUIntConstructor(const Constructor& c, OutputStream& out);
    223 
    224     /**
    225      * Writes a matrix with the diagonal entries all equal to the provided expression, and all other
    226      * entries equal to zero.
    227      */
    228     void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out);
    229 
    230     /**
    231      * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the
    232      * source matrix are filled with zero; entries which do not exist in the destination matrix are
    233      * ignored.
    234      */
    235     void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType,
    236                          OutputStream& out);
    237 
    238     void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn,
    239                         std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry,
    240                         OutputStream& out);
    241 
    242     SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out);
    243 
    244     SpvId writeVectorConstructor(const Constructor& c, OutputStream& out);
    245 
    246     SpvId writeArrayConstructor(const Constructor& c, OutputStream& out);
    247 
    248     SpvId writeConstructor(const Constructor& c, OutputStream& out);
    249 
    250     SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out);
    251 
    252     SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out);
    253 
    254     /**
    255      * Folds the potentially-vector result of a logical operation down to a single bool. If
    256      * operandType is a vector type, assumes that the intermediate result in id is a bvec of the
    257      * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise,
    258      * returns the original id value.
    259      */
    260     SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out);
    261 
    262     SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator,
    263                                 SpvOp_ intOperator, SpvOp_ vectorMergeOperator,
    264                                 SpvOp_ mergeOperator, OutputStream& out);
    265 
    266     SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs,
    267                                          SpvOp_ floatOperator, SpvOp_ intOperator,
    268                                          OutputStream& out);
    269 
    270     SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs,
    271                                SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt,
    272                                SpvOp_ ifBool, OutputStream& out);
    273 
    274     SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt,
    275                                SpvOp_ ifUInt, OutputStream& out);
    276 
    277     SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op,
    278                                 const Type& rightType, SpvId rhs, const Type& resultType,
    279                                 OutputStream& out);
    280 
    281     SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out);
    282 
    283     SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out);
    284 
    285     SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out);
    286 
    287     SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out);
    288 
    289     SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out);
    290 
    291     SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out);
    292 
    293     SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out);
    294 
    295     SpvId writeBoolLiteral(const BoolLiteral& b);
    296 
    297     SpvId writeIntLiteral(const IntLiteral& i);
    298 
    299     SpvId writeFloatLiteral(const FloatLiteral& f);
    300 
    301     void writeStatement(const Statement& s, OutputStream& out);
    302 
    303     void writeBlock(const Block& b, OutputStream& out);
    304 
    305     void writeIfStatement(const IfStatement& stmt, OutputStream& out);
    306 
    307     void writeForStatement(const ForStatement& f, OutputStream& out);
    308 
    309     void writeWhileStatement(const WhileStatement& w, OutputStream& out);
    310 
    311     void writeDoStatement(const DoStatement& d, OutputStream& out);
    312 
    313     void writeSwitchStatement(const SwitchStatement& s, OutputStream& out);
    314 
    315     void writeReturnStatement(const ReturnStatement& r, OutputStream& out);
    316 
    317     void writeCapabilities(OutputStream& out);
    318 
    319     void writeInstructions(const Program& program, OutputStream& out);
    320 
    321     void writeOpCode(SpvOp_ opCode, int length, OutputStream& out);
    322 
    323     void writeWord(int32_t word, OutputStream& out);
    324 
    325     void writeString(const char* string, size_t length, OutputStream& out);
    326 
    327     void writeLabel(SpvId id, OutputStream& out);
    328 
    329     void writeInstruction(SpvOp_ opCode, OutputStream& out);
    330 
    331     void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out);
    332 
    333     void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out);
    334 
    335     void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out);
    336 
    337     void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string,
    338                           OutputStream& out);
    339 
    340     void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out);
    341 
    342     void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3,
    343                           OutputStream& out);
    344 
    345     void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
    346                           OutputStream& out);
    347 
    348     void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
    349                           int32_t word5, OutputStream& out);
    350 
    351     void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
    352                           int32_t word5, int32_t word6, OutputStream& out);
    353 
    354     void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
    355                           int32_t word5, int32_t word6, int32_t word7, OutputStream& out);
    356 
    357     void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4,
    358                           int32_t word5, int32_t word6, int32_t word7, int32_t word8,
    359                           OutputStream& out);
    360 
    361     void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out);
    362 
    363     const Context& fContext;
    364     const MemoryLayout fDefaultLayout;
    365 
    366     uint64_t fCapabilities;
    367     SpvId fIdCount;
    368     SpvId fGLSLExtendedInstructions;
    369     typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic;
    370     std::unordered_map<String, Intrinsic> fIntrinsicMap;
    371     std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap;
    372     std::unordered_map<const Variable*, SpvId> fVariableMap;
    373     std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap;
    374     std::unordered_map<String, SpvId> fImageTypeMap;
    375     std::unordered_map<String, SpvId> fTypeMap;
    376     StringStream fCapabilitiesBuffer;
    377     StringStream fGlobalInitializersBuffer;
    378     StringStream fConstantBuffer;
    379     StringStream fExtraGlobalsBuffer;
    380     StringStream fExternalFunctionsBuffer;
    381     StringStream fVariableBuffer;
    382     StringStream fNameBuffer;
    383     StringStream fDecorationBuffer;
    384 
    385     SpvId fBoolTrue;
    386     SpvId fBoolFalse;
    387     std::unordered_map<std::pair<ConstantValue, ConstantType>, SpvId> fNumberConstants;
    388     // The constant float2(0, 1), used in swizzling
    389     SpvId fConstantZeroOneVector = 0;
    390     bool fSetupFragPosition;
    391     // label of the current block, or 0 if we are not in a block
    392     SpvId fCurrentBlock;
    393     std::stack<SpvId> fBreakTarget;
    394     std::stack<SpvId> fContinueTarget;
    395     SpvId fRTHeightStructId = (SpvId) -1;
    396     SpvId fRTHeightFieldIndex = (SpvId) -1;
    397     // holds variables synthesized during output, for lifetime purposes
    398     SymbolTable fSynthetics;
    399     int fSkInCount = 1;
    400 
    401     friend class PointerLValue;
    402     friend class SwizzleLValue;
    403 
    404     typedef CodeGenerator INHERITED;
    405 };
    406 
    407 }
    408 
    409 #endif
    410