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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 15 | #include "src/sksl/SkSLCodeGenerator.h" |
| 16 | #include "src/sksl/SkSLMemoryLayout.h" |
| 17 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 18 | #include "src/sksl/ir/SkSLBoolLiteral.h" |
| 19 | #include "src/sksl/ir/SkSLConstructor.h" |
| 20 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 21 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 22 | #include "src/sksl/ir/SkSLFloatLiteral.h" |
| 23 | #include "src/sksl/ir/SkSLForStatement.h" |
| 24 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 25 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 26 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 27 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 28 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 29 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 30 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 31 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 32 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 33 | #include "src/sksl/ir/SkSLProgramElement.h" |
| 34 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 35 | #include "src/sksl/ir/SkSLStatement.h" |
| 36 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 37 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 38 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 39 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 40 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 41 | #include "src/sksl/ir/SkSLVariableReference.h" |
| 42 | #include "src/sksl/ir/SkSLWhileStatement.h" |
| 43 | #include "src/sksl/spirv.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 44 | |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 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 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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() {} |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 93 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 98 | virtual SpvId load(OutputStream& out) = 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 99 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 100 | virtual void store(SpvId value, OutputStream& out) = 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 103 | SPIRVCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 104 | OutputStream* out) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 105 | : INHERITED(program, errors, out) |
| 106 | , fContext(*context) |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 107 | , fDefaultLayout(MemoryLayout::k140_Standard) |
Ethan Nicholas | 81d1511 | 2018-07-13 12:48:50 -0400 | [diff] [blame] | 108 | , fCapabilities(0) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 109 | , fIdCount(1) |
| 110 | , fBoolTrue(0) |
| 111 | , fBoolFalse(0) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 112 | , fSetupFragPosition(false) |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 113 | , fCurrentBlock(0) |
| 114 | , fSynthetics(nullptr, errors) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 115 | this->setupIntrinsics(); |
| 116 | } |
| 117 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 118 | bool generateCode() override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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, |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 129 | kClamp_SpecialIntrinsic, |
| 130 | kMax_SpecialIntrinsic, |
| 131 | kMin_SpecialIntrinsic, |
| 132 | kMix_SpecialIntrinsic, |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 133 | kMod_SpecialIntrinsic, |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 134 | kDFdy_SpecialIntrinsic, |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 135 | kSaturate_SpecialIntrinsic, |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 136 | kSubpassLoad_SpecialIntrinsic, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 137 | kTexture_SpecialIntrinsic, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 138 | }; |
| 139 | |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 140 | enum class Precision { |
| 141 | kLow, |
| 142 | kHigh, |
| 143 | }; |
| 144 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 145 | void setupIntrinsics(); |
| 146 | |
| 147 | SpvId nextId(); |
| 148 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 149 | Type getActualType(const Type& type); |
| 150 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 151 | SpvId getType(const Type& type); |
| 152 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 153 | SpvId getType(const Type& type, const MemoryLayout& layout); |
| 154 | |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 155 | SpvId getImageType(const Type& type); |
| 156 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 157 | SpvId getFunctionType(const FunctionDeclaration& function); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 158 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 159 | SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 160 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 161 | SpvId getPointerType(const Type& type, const MemoryLayout& layout, |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 162 | SpvStorageClass_ storageClass); |
| 163 | |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 164 | void writePrecisionModifier(Precision precision, SpvId id); |
| 165 | |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 166 | void writePrecisionModifier(const Type& type, SpvId id); |
Ethan Nicholas | a51d713 | 2017-06-09 10:47:31 -0400 | [diff] [blame] | 167 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 168 | std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 169 | |
| 170 | void writeLayout(const Layout& layout, SpvId target); |
| 171 | |
| 172 | void writeLayout(const Layout& layout, SpvId target, int member); |
| 173 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 174 | void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId); |
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 | void writeProgramElement(const ProgramElement& pe, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 177 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 178 | SpvId writeInterfaceBlock(const InterfaceBlock& intf); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 179 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 180 | SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 181 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 182 | SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 183 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 184 | SpvId writeFunction(const FunctionDefinition& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 185 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 186 | void writeGlobalVars(Program::Kind kind, const VarDeclarations& v, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 187 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 188 | void writeVarDeclarations(const VarDeclarations& decl, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 189 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 190 | SpvId writeVariableReference(const VariableReference& ref, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 191 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 192 | std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 193 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 194 | SpvId writeExpression(const Expression& expr, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 195 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 196 | SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out); |
| 197 | |
| 198 | SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out); |
| 199 | |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 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 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 214 | SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 215 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 216 | SpvId writeConstantVector(const Constructor& c); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 217 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 218 | SpvId writeFloatConstructor(const Constructor& c, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 219 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 220 | SpvId writeIntConstructor(const Constructor& c, OutputStream& out); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 221 | |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 222 | SpvId writeUIntConstructor(const Constructor& c, OutputStream& out); |
| 223 | |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 224 | /** |
| 225 | * Writes a matrix with the diagonal entries all equal to the provided expression, and all other |
| 226 | * entries equal to zero. |
| 227 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 228 | void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 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, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 236 | OutputStream& out); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 237 | |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 238 | void addColumnEntry(SpvId columnType, Precision precision, std::vector<SpvId>* currentColumn, |
Ethan Nicholas | 5c46b72 | 2019-03-22 14:32:37 -0400 | [diff] [blame] | 239 | std::vector<SpvId>* columnIds, int* currentCount, int rows, SpvId entry, |
| 240 | OutputStream& out); |
| 241 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 242 | SpvId writeMatrixConstructor(const Constructor& c, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 243 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 244 | SpvId writeVectorConstructor(const Constructor& c, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 245 | |
Ethan Nicholas | bd55322 | 2017-07-18 15:54:59 -0400 | [diff] [blame] | 246 | SpvId writeArrayConstructor(const Constructor& c, OutputStream& out); |
| 247 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 248 | SpvId writeConstructor(const Constructor& c, 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 | SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 251 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 252 | SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 253 | |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 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 | */ |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 260 | SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out); |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 261 | |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 262 | SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 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); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 269 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 270 | SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs, |
| 271 | SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 272 | SpvOp_ ifBool, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 273 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 274 | SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 275 | SpvOp_ ifUInt, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 276 | |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 277 | SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op, |
| 278 | const Type& rightType, SpvId rhs, const Type& resultType, |
| 279 | OutputStream& out); |
| 280 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 281 | SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 282 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 283 | SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 284 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 285 | SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 286 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 287 | SpvId writeLogicalAnd(const BinaryExpression& b, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 288 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 289 | SpvId writeLogicalOr(const BinaryExpression& o, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 290 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 291 | SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 292 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 293 | SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 294 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 295 | SpvId writeBoolLiteral(const BoolLiteral& b); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 296 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 297 | SpvId writeIntLiteral(const IntLiteral& i); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 298 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 299 | SpvId writeFloatLiteral(const FloatLiteral& f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 300 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 301 | void writeStatement(const Statement& s, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 302 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 303 | void writeBlock(const Block& b, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 304 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 305 | void writeIfStatement(const IfStatement& stmt, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 306 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 307 | void writeForStatement(const ForStatement& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 308 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 309 | void writeWhileStatement(const WhileStatement& w, OutputStream& out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 310 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 311 | void writeDoStatement(const DoStatement& d, OutputStream& out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 312 | |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 313 | void writeSwitchStatement(const SwitchStatement& s, OutputStream& out); |
| 314 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 315 | void writeReturnStatement(const ReturnStatement& r, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 316 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 317 | void writeCapabilities(OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 318 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 319 | void writeInstructions(const Program& program, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 320 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 321 | void writeOpCode(SpvOp_ opCode, int length, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 322 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 323 | void writeWord(int32_t word, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 324 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 325 | void writeString(const char* string, size_t length, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 326 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 327 | void writeLabel(SpvId id, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 328 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 329 | void writeInstruction(SpvOp_ opCode, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 330 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 331 | void writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 332 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 333 | void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 334 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 335 | void writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 336 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 337 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, StringFragment string, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 338 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 339 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 340 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 341 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 342 | 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] | 343 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 344 | |
| 345 | 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] | 346 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 347 | |
| 348 | 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] | 349 | int32_t word5, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 350 | |
| 351 | 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] | 352 | int32_t word5, int32_t word6, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 353 | |
| 354 | 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] | 355 | int32_t word5, int32_t word6, int32_t word7, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 356 | |
| 357 | 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] | 358 | int32_t word5, int32_t word6, int32_t word7, int32_t word8, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 359 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 360 | |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 361 | void writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out); |
| 362 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 363 | const Context& fContext; |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 364 | const MemoryLayout fDefaultLayout; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 365 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 366 | uint64_t fCapabilities; |
| 367 | SpvId fIdCount; |
| 368 | SpvId fGLSLExtendedInstructions; |
| 369 | 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] | 370 | std::unordered_map<String, Intrinsic> fIntrinsicMap; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 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; |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 374 | std::unordered_map<String, SpvId> fImageTypeMap; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 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; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 384 | |
| 385 | SpvId fBoolTrue; |
| 386 | SpvId fBoolFalse; |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 387 | std::unordered_map<std::pair<ConstantValue, ConstantType>, SpvId> fNumberConstants; |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 388 | // The constant float2(0, 1), used in swizzling |
| 389 | SpvId fConstantZeroOneVector = 0; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 390 | bool fSetupFragPosition; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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; |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 395 | SpvId fRTHeightStructId = (SpvId) -1; |
| 396 | SpvId fRTHeightFieldIndex = (SpvId) -1; |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 397 | // holds variables synthesized during output, for lifetime purposes |
| 398 | SymbolTable fSynthetics; |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 399 | int fSkInCount = 1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 400 | |
| 401 | friend class PointerLValue; |
| 402 | friend class SwizzleLValue; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 403 | |
| 404 | typedef CodeGenerator INHERITED; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | } |
| 408 | |
| 409 | #endif |