ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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 <sstream> |
| 12 | #include <stack> |
| 13 | #include <tuple> |
| 14 | #include <unordered_map> |
| 15 | |
| 16 | #include "SkSLCodeGenerator.h" |
| 17 | #include "ir/SkSLBinaryExpression.h" |
| 18 | #include "ir/SkSLBoolLiteral.h" |
| 19 | #include "ir/SkSLConstructor.h" |
| 20 | #include "ir/SkSLFloatLiteral.h" |
| 21 | #include "ir/SkSLIfStatement.h" |
| 22 | #include "ir/SkSLIndexExpression.h" |
| 23 | #include "ir/SkSLInterfaceBlock.h" |
| 24 | #include "ir/SkSLIntLiteral.h" |
| 25 | #include "ir/SkSLFieldAccess.h" |
| 26 | #include "ir/SkSLForStatement.h" |
| 27 | #include "ir/SkSLFunctionCall.h" |
| 28 | #include "ir/SkSLFunctionDeclaration.h" |
| 29 | #include "ir/SkSLFunctionDefinition.h" |
| 30 | #include "ir/SkSLPrefixExpression.h" |
| 31 | #include "ir/SkSLPostfixExpression.h" |
| 32 | #include "ir/SkSLProgramElement.h" |
| 33 | #include "ir/SkSLReturnStatement.h" |
| 34 | #include "ir/SkSLStatement.h" |
| 35 | #include "ir/SkSLSwizzle.h" |
| 36 | #include "ir/SkSLTernaryExpression.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 37 | #include "ir/SkSLVarDeclarations.h" |
| 38 | #include "ir/SkSLVarDeclarationsStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 39 | #include "ir/SkSLVariableReference.h" |
| 40 | #include "spirv.h" |
| 41 | |
| 42 | namespace SkSL { |
| 43 | |
| 44 | #define kLast_Capability SpvCapabilityMultiViewport |
| 45 | |
| 46 | /** |
| 47 | * Converts a Program into a SPIR-V binary. |
| 48 | */ |
| 49 | class SPIRVCodeGenerator : public CodeGenerator { |
| 50 | public: |
| 51 | class LValue { |
| 52 | public: |
| 53 | virtual ~LValue() {} |
| 54 | |
| 55 | // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced |
| 56 | // by a pointer (e.g. vector swizzles), returns 0. |
| 57 | virtual SpvId getPointer() = 0; |
| 58 | |
| 59 | virtual SpvId load(std::ostream& out) = 0; |
| 60 | |
| 61 | virtual void store(SpvId value, std::ostream& out) = 0; |
| 62 | }; |
| 63 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 64 | SPIRVCodeGenerator(const Context* context) |
| 65 | : fContext(*context) |
| 66 | , fCapabilities(1 << SpvCapabilityShader) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 67 | , fIdCount(1) |
| 68 | , fBoolTrue(0) |
| 69 | , fBoolFalse(0) |
| 70 | , fCurrentBlock(0) { |
| 71 | this->setupIntrinsics(); |
| 72 | } |
| 73 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 74 | void generateCode(const Program& program, std::ostream& out) override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | enum IntrinsicKind { |
| 78 | kGLSL_STD_450_IntrinsicKind, |
| 79 | kSPIRV_IntrinsicKind, |
| 80 | kSpecial_IntrinsicKind |
| 81 | }; |
| 82 | |
| 83 | enum SpecialIntrinsic { |
| 84 | kAtan_SpecialIntrinsic, |
| 85 | kTexture_SpecialIntrinsic, |
| 86 | kTexture2D_SpecialIntrinsic, |
| 87 | kTextureProj_SpecialIntrinsic |
| 88 | }; |
| 89 | |
| 90 | void setupIntrinsics(); |
| 91 | |
| 92 | SpvId nextId(); |
| 93 | |
| 94 | SpvId getType(const Type& type); |
| 95 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 96 | SpvId getFunctionType(const FunctionDeclaration& function); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 97 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 98 | SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 99 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 100 | std::vector<SpvId> getAccessChain(const Expression& expr, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 101 | |
| 102 | void writeLayout(const Layout& layout, SpvId target); |
| 103 | |
| 104 | void writeLayout(const Layout& layout, SpvId target, int member); |
| 105 | |
egdaniel | 988283c | 2016-11-16 07:29:51 -0800 | [diff] [blame] | 106 | void writeStruct(const Type& type, SpvId resultId); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 107 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 108 | void writeProgramElement(const ProgramElement& pe, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 109 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 110 | SpvId writeInterfaceBlock(const InterfaceBlock& intf); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 111 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 112 | SpvId writeFunctionStart(const FunctionDeclaration& f, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 113 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 114 | SpvId writeFunctionDeclaration(const FunctionDeclaration& f, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 115 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 116 | SpvId writeFunction(const FunctionDefinition& f, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 117 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 118 | void writeGlobalVars(Program::Kind kind, const VarDeclarations& v, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 119 | |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 120 | void writeVarDeclarations(const VarDeclarations& decl, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 121 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 122 | SpvId writeVariableReference(const VariableReference& ref, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 123 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 124 | std::unique_ptr<LValue> getLValue(const Expression& value, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 125 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 126 | SpvId writeExpression(const Expression& expr, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 127 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 128 | SpvId writeIntrinsicCall(const FunctionCall& c, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 129 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 130 | SpvId writeFunctionCall(const FunctionCall& c, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 131 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 132 | SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 133 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 134 | SpvId writeConstantVector(const Constructor& c); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 135 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 136 | SpvId writeFloatConstructor(const Constructor& c, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 137 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 138 | SpvId writeIntConstructor(const Constructor& c, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 139 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 140 | SpvId writeMatrixConstructor(const Constructor& c, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 141 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 142 | SpvId writeVectorConstructor(const Constructor& c, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 143 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 144 | SpvId writeConstructor(const Constructor& c, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 145 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 146 | SpvId writeFieldAccess(const FieldAccess& f, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 147 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 148 | SpvId writeSwizzle(const Swizzle& swizzle, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 149 | |
| 150 | SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs, |
| 151 | SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt, |
| 152 | SpvOp_ ifBool, std::ostream& out); |
| 153 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 154 | SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt, |
| 155 | SpvOp_ ifUInt, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 156 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 157 | SpvId writeBinaryExpression(const BinaryExpression& b, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 158 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 159 | SpvId writeTernaryExpression(const TernaryExpression& t, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 160 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 161 | SpvId writeIndexExpression(const IndexExpression& expr, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 162 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 163 | SpvId writeLogicalAnd(const BinaryExpression& b, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 164 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 165 | SpvId writeLogicalOr(const BinaryExpression& o, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 166 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 167 | SpvId writePrefixExpression(const PrefixExpression& p, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 168 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 169 | SpvId writePostfixExpression(const PostfixExpression& p, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 170 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 171 | SpvId writeBoolLiteral(const BoolLiteral& b); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 172 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 173 | SpvId writeIntLiteral(const IntLiteral& i); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 174 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 175 | SpvId writeFloatLiteral(const FloatLiteral& f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 176 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 177 | void writeStatement(const Statement& s, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 178 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 179 | void writeBlock(const Block& b, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 180 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 181 | void writeIfStatement(const IfStatement& stmt, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 182 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 183 | void writeForStatement(const ForStatement& f, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 184 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 185 | void writeReturnStatement(const ReturnStatement& r, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 186 | |
| 187 | void writeCapabilities(std::ostream& out); |
| 188 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 189 | void writeInstructions(const Program& program, std::ostream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 190 | |
| 191 | void writeOpCode(SpvOp_ opCode, int length, std::ostream& out); |
| 192 | |
| 193 | void writeWord(int32_t word, std::ostream& out); |
| 194 | |
| 195 | void writeString(const char* string, std::ostream& out); |
| 196 | |
| 197 | void writeLabel(SpvId id, std::ostream& out); |
| 198 | |
| 199 | void writeInstruction(SpvOp_ opCode, std::ostream& out); |
| 200 | |
| 201 | void writeInstruction(SpvOp_ opCode, const char* string, std::ostream& out); |
| 202 | |
| 203 | void writeInstruction(SpvOp_ opCode, int32_t word1, std::ostream& out); |
| 204 | |
| 205 | void writeInstruction(SpvOp_ opCode, int32_t word1, const char* string, std::ostream& out); |
| 206 | |
| 207 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, const char* string, |
| 208 | std::ostream& out); |
| 209 | |
| 210 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, std::ostream& out); |
| 211 | |
| 212 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, |
| 213 | std::ostream& out); |
| 214 | |
| 215 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4, |
| 216 | std::ostream& out); |
| 217 | |
| 218 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4, |
| 219 | int32_t word5, std::ostream& out); |
| 220 | |
| 221 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4, |
| 222 | int32_t word5, int32_t word6, std::ostream& out); |
| 223 | |
| 224 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4, |
| 225 | int32_t word5, int32_t word6, int32_t word7, std::ostream& out); |
| 226 | |
| 227 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4, |
| 228 | int32_t word5, int32_t word6, int32_t word7, int32_t word8, |
| 229 | std::ostream& out); |
| 230 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 231 | const Context& fContext; |
| 232 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 233 | uint64_t fCapabilities; |
| 234 | SpvId fIdCount; |
| 235 | SpvId fGLSLExtendedInstructions; |
| 236 | typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic; |
| 237 | std::unordered_map<std::string, Intrinsic> fIntrinsicMap; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 238 | std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap; |
| 239 | std::unordered_map<const Variable*, SpvId> fVariableMap; |
| 240 | std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 241 | std::unordered_map<std::string, SpvId> fTypeMap; |
| 242 | std::stringstream fCapabilitiesBuffer; |
| 243 | std::stringstream fGlobalInitializersBuffer; |
| 244 | std::stringstream fConstantBuffer; |
| 245 | std::stringstream fExternalFunctionsBuffer; |
| 246 | std::stringstream fVariableBuffer; |
| 247 | std::stringstream fNameBuffer; |
| 248 | std::stringstream fDecorationBuffer; |
| 249 | |
| 250 | SpvId fBoolTrue; |
| 251 | SpvId fBoolFalse; |
| 252 | std::unordered_map<int64_t, SpvId> fIntConstants; |
| 253 | std::unordered_map<uint64_t, SpvId> fUIntConstants; |
| 254 | std::unordered_map<float, SpvId> fFloatConstants; |
| 255 | std::unordered_map<double, SpvId> fDoubleConstants; |
| 256 | // label of the current block, or 0 if we are not in a block |
| 257 | SpvId fCurrentBlock; |
| 258 | std::stack<SpvId> fBreakTarget; |
| 259 | std::stack<SpvId> fContinueTarget; |
| 260 | |
| 261 | friend class PointerLValue; |
| 262 | friend class SwizzleLValue; |
| 263 | }; |
| 264 | |
| 265 | } |
| 266 | |
| 267 | #endif |