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