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 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_SPIRVCODEGENERATOR |
| 9 | #define SKSL_SPIRVCODEGENERATOR |
| 10 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 11 | #include <stack> |
| 12 | #include <tuple> |
| 13 | #include <unordered_map> |
| 14 | |
| 15 | #include "SkSLCodeGenerator.h" |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 16 | #include "SkSLMemoryLayout.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 17 | #include "ir/SkSLBinaryExpression.h" |
| 18 | #include "ir/SkSLBoolLiteral.h" |
| 19 | #include "ir/SkSLConstructor.h" |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 20 | #include "ir/SkSLDoStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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/SkSLSwizzle.h" |
| 37 | #include "ir/SkSLTernaryExpression.h" |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 38 | #include "ir/SkSLVarDeclarations.h" |
| 39 | #include "ir/SkSLVarDeclarationsStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 40 | #include "ir/SkSLVariableReference.h" |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 41 | #include "ir/SkSLWhileStatement.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 42 | #include "spirv.h" |
| 43 | |
| 44 | namespace SkSL { |
| 45 | |
| 46 | #define kLast_Capability SpvCapabilityMultiViewport |
| 47 | |
| 48 | /** |
| 49 | * Converts a Program into a SPIR-V binary. |
| 50 | */ |
| 51 | class SPIRVCodeGenerator : public CodeGenerator { |
| 52 | public: |
| 53 | class LValue { |
| 54 | public: |
| 55 | virtual ~LValue() {} |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 56 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 57 | // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced |
| 58 | // by a pointer (e.g. vector swizzles), returns 0. |
| 59 | virtual SpvId getPointer() = 0; |
| 60 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 61 | virtual SpvId load(OutputStream& out) = 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 62 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 63 | virtual void store(SpvId value, OutputStream& out) = 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 66 | SPIRVCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 67 | OutputStream* out) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 68 | : INHERITED(program, errors, out) |
| 69 | , fContext(*context) |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 70 | , fDefaultLayout(MemoryLayout::k140_Standard) |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 71 | , fCapabilities(1 << SpvCapabilityShader) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 72 | , fIdCount(1) |
| 73 | , fBoolTrue(0) |
| 74 | , fBoolFalse(0) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 75 | , fSetupFragPosition(false) |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 76 | , fCurrentBlock(0) |
| 77 | , fSynthetics(nullptr, errors) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 78 | this->setupIntrinsics(); |
| 79 | } |
| 80 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 81 | bool generateCode() override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | enum IntrinsicKind { |
| 85 | kGLSL_STD_450_IntrinsicKind, |
| 86 | kSPIRV_IntrinsicKind, |
| 87 | kSpecial_IntrinsicKind |
| 88 | }; |
| 89 | |
| 90 | enum SpecialIntrinsic { |
| 91 | kAtan_SpecialIntrinsic, |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 92 | kSubpassLoad_SpecialIntrinsic, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame^] | 93 | kTexelFetch_SpecialIntrinsic, |
| 94 | kTexture_SpecialIntrinsic, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | void setupIntrinsics(); |
| 98 | |
| 99 | SpvId nextId(); |
| 100 | |
| 101 | SpvId getType(const Type& type); |
| 102 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 103 | SpvId getType(const Type& type, const MemoryLayout& layout); |
| 104 | |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame^] | 105 | SpvId getImageType(const Type& type); |
| 106 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 107 | SpvId getFunctionType(const FunctionDeclaration& function); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 108 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 109 | SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 110 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 111 | SpvId getPointerType(const Type& type, const MemoryLayout& layout, |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 112 | SpvStorageClass_ storageClass); |
| 113 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 114 | std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 115 | |
| 116 | void writeLayout(const Layout& layout, SpvId target); |
| 117 | |
| 118 | void writeLayout(const Layout& layout, SpvId target, int member); |
| 119 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 120 | void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 121 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 122 | void writeProgramElement(const ProgramElement& pe, OutputStream& 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 | SpvId writeInterfaceBlock(const InterfaceBlock& intf); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 125 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 126 | SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 127 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 128 | SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 129 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 130 | SpvId writeFunction(const FunctionDefinition& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 131 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 132 | void writeGlobalVars(Program::Kind kind, const VarDeclarations& v, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 133 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 134 | void writeVarDeclarations(const VarDeclarations& decl, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 135 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 136 | SpvId writeVariableReference(const VariableReference& ref, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 137 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 138 | std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 139 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 140 | SpvId writeExpression(const Expression& expr, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 141 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 142 | SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out); |
| 143 | |
| 144 | SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out); |
| 145 | |
| 146 | SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& 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 writeConstantVector(const Constructor& c); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 149 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 150 | SpvId writeFloatConstructor(const Constructor& c, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 151 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 152 | SpvId writeIntConstructor(const Constructor& c, OutputStream& out); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Writes a matrix with the diagonal entries all equal to the provided expression, and all other |
| 156 | * entries equal to zero. |
| 157 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 158 | void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the |
| 162 | * source matrix are filled with zero; entries which do not exist in the destination matrix are |
| 163 | * ignored. |
| 164 | */ |
| 165 | void writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, const Type& dstType, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 166 | OutputStream& out); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 167 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 168 | SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 169 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 170 | SpvId writeVectorConstructor(const Constructor& c, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 171 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 172 | SpvId writeConstructor(const Constructor& c, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 173 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 174 | SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 175 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 176 | SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 177 | |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 178 | /** |
| 179 | * Folds the potentially-vector result of a logical operation down to a single bool. If |
| 180 | * operandType is a vector type, assumes that the intermediate result in id is a bvec of the |
| 181 | * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise, |
| 182 | * returns the original id value. |
| 183 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 184 | SpvId foldToBool(SpvId id, const Type& operandType, OutputStream& out); |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 185 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 186 | SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs, |
| 187 | SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 188 | SpvOp_ ifBool, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 189 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 190 | SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 191 | SpvOp_ ifUInt, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 192 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 193 | SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 194 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 195 | SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 196 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 197 | SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 198 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 199 | SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 200 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 201 | SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 202 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 203 | SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 204 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 205 | SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 206 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 207 | SpvId writeBoolLiteral(const BoolLiteral& b); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 208 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 209 | SpvId writeIntLiteral(const IntLiteral& i); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 210 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 211 | SpvId writeFloatLiteral(const FloatLiteral& f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 212 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 213 | void writeStatement(const Statement& s, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 214 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 215 | void writeBlock(const Block& b, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 216 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 217 | void writeIfStatement(const IfStatement& stmt, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 218 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 219 | void writeForStatement(const ForStatement& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 220 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 221 | void writeWhileStatement(const WhileStatement& w, OutputStream& out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 222 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 223 | void writeDoStatement(const DoStatement& d, OutputStream& out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 224 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 225 | void writeReturnStatement(const ReturnStatement& r, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 226 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 227 | void writeCapabilities(OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 228 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 229 | void writeInstructions(const Program& program, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 230 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 231 | void writeOpCode(SpvOp_ opCode, int length, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 232 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 233 | void writeWord(int32_t word, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 234 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 235 | void writeString(const char* string, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 236 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 237 | void writeLabel(SpvId id, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 238 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 239 | void writeInstruction(SpvOp_ opCode, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 240 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 241 | void writeInstruction(SpvOp_ opCode, const char* string, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 242 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 243 | void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 244 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 245 | void writeInstruction(SpvOp_ opCode, int32_t word1, const char* string, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 246 | |
| 247 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, const char* string, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 248 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 249 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 250 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 251 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 252 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 253 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 254 | |
| 255 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 256 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 257 | |
| 258 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 259 | int32_t word5, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 260 | |
| 261 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 262 | int32_t word5, int32_t word6, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 263 | |
| 264 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 265 | int32_t word5, int32_t word6, int32_t word7, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 266 | |
| 267 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, int32_t word3, int32_t word4, |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 268 | int32_t word5, int32_t word6, int32_t word7, int32_t word8, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 269 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 270 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 271 | const Context& fContext; |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 272 | const MemoryLayout fDefaultLayout; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 273 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 274 | uint64_t fCapabilities; |
| 275 | SpvId fIdCount; |
| 276 | SpvId fGLSLExtendedInstructions; |
| 277 | typedef std::tuple<IntrinsicKind, int32_t, int32_t, int32_t, int32_t> Intrinsic; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 278 | std::unordered_map<String, Intrinsic> fIntrinsicMap; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 279 | std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap; |
| 280 | std::unordered_map<const Variable*, SpvId> fVariableMap; |
| 281 | std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap; |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame^] | 282 | std::unordered_map<String, SpvId> fImageTypeMap; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 283 | std::unordered_map<String, SpvId> fTypeMap; |
| 284 | StringStream fCapabilitiesBuffer; |
| 285 | StringStream fGlobalInitializersBuffer; |
| 286 | StringStream fConstantBuffer; |
| 287 | StringStream fExtraGlobalsBuffer; |
| 288 | StringStream fExternalFunctionsBuffer; |
| 289 | StringStream fVariableBuffer; |
| 290 | StringStream fNameBuffer; |
| 291 | StringStream fDecorationBuffer; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 292 | |
| 293 | SpvId fBoolTrue; |
| 294 | SpvId fBoolFalse; |
| 295 | std::unordered_map<int64_t, SpvId> fIntConstants; |
| 296 | std::unordered_map<uint64_t, SpvId> fUIntConstants; |
| 297 | std::unordered_map<float, SpvId> fFloatConstants; |
| 298 | std::unordered_map<double, SpvId> fDoubleConstants; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 299 | bool fSetupFragPosition; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 300 | // label of the current block, or 0 if we are not in a block |
| 301 | SpvId fCurrentBlock; |
| 302 | std::stack<SpvId> fBreakTarget; |
| 303 | std::stack<SpvId> fContinueTarget; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 304 | SpvId fRTHeightStructId = (SpvId) -1; |
| 305 | SpvId fRTHeightFieldIndex = (SpvId) -1; |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 306 | // holds variables synthesized during output, for lifetime purposes |
| 307 | SymbolTable fSynthetics; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 308 | |
| 309 | friend class PointerLValue; |
| 310 | friend class SwizzleLValue; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 311 | |
| 312 | typedef CodeGenerator INHERITED; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | } |
| 316 | |
| 317 | #endif |