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> |
John Stiles | d7437ee | 2021-08-02 11:56:16 -0400 | [diff] [blame] | 14 | #include <unordered_set> |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 15 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 16 | #include "include/private/SkSLModifiers.h" |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 17 | #include "include/private/SkSLProgramElement.h" |
| 18 | #include "include/private/SkSLStatement.h" |
John Stiles | cd80689 | 2021-01-06 13:33:31 -0500 | [diff] [blame] | 19 | #include "src/core/SkOpts.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/sksl/SkSLMemoryLayout.h" |
Mike Klein | 4b432fa | 2019-06-06 11:44:05 -0500 | [diff] [blame] | 21 | #include "src/sksl/SkSLStringStream.h" |
John Stiles | 3738ef5 | 2021-04-13 10:41:57 -0400 | [diff] [blame] | 22 | #include "src/sksl/codegen/SkSLCodeGenerator.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/sksl/ir/SkSLConstructor.h" |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLConstructorArray.h" |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 26 | #include "src/sksl/ir/SkSLConstructorCompound.h" |
| 27 | #include "src/sksl/ir/SkSLConstructorCompoundCast.h" |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 28 | #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 29 | #include "src/sksl/ir/SkSLConstructorMatrixResize.h" |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 30 | #include "src/sksl/ir/SkSLConstructorScalarCast.h" |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 31 | #include "src/sksl/ir/SkSLConstructorSplat.h" |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 32 | #include "src/sksl/ir/SkSLConstructorStruct.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 33 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 34 | #include "src/sksl/ir/SkSLFieldAccess.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 35 | #include "src/sksl/ir/SkSLForStatement.h" |
| 36 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 37 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 38 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 39 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 40 | #include "src/sksl/ir/SkSLIndexExpression.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 41 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 42 | #include "src/sksl/ir/SkSLLiteral.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 43 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 44 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 45 | #include "src/sksl/ir/SkSLReturnStatement.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 46 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 47 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 48 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 49 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 50 | #include "src/sksl/ir/SkSLVariableReference.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 51 | #include "src/sksl/spirv.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 52 | |
John Stiles | cd80689 | 2021-01-06 13:33:31 -0500 | [diff] [blame] | 53 | namespace SkSL { |
| 54 | |
John Stiles | bdc3d3c | 2021-01-06 18:41:40 -0500 | [diff] [blame] | 55 | struct SPIRVNumberConstant { |
| 56 | bool operator==(const SPIRVNumberConstant& that) const { |
| 57 | return fValueBits == that.fValueBits && |
| 58 | fKind == that.fKind; |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 59 | } |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 60 | int32_t fValueBits; |
John Stiles | bdc3d3c | 2021-01-06 18:41:40 -0500 | [diff] [blame] | 61 | SkSL::Type::NumberKind fKind; |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 62 | }; |
| 63 | |
John Stiles | cd80689 | 2021-01-06 13:33:31 -0500 | [diff] [blame] | 64 | struct SPIRVVectorConstant { |
| 65 | bool operator==(const SPIRVVectorConstant& that) const { |
| 66 | return fTypeId == that.fTypeId && |
| 67 | fValueId[0] == that.fValueId[0] && |
| 68 | fValueId[1] == that.fValueId[1] && |
| 69 | fValueId[2] == that.fValueId[2] && |
| 70 | fValueId[3] == that.fValueId[3]; |
| 71 | } |
| 72 | SpvId fTypeId; |
| 73 | SpvId fValueId[4]; |
| 74 | }; |
| 75 | |
| 76 | } // namespace SkSL |
| 77 | |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 78 | namespace std { |
| 79 | |
| 80 | template <> |
John Stiles | bdc3d3c | 2021-01-06 18:41:40 -0500 | [diff] [blame] | 81 | struct hash<SkSL::SPIRVNumberConstant> { |
| 82 | size_t operator()(const SkSL::SPIRVNumberConstant& key) const { |
| 83 | return key.fValueBits ^ (int)key.fKind; |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 84 | } |
| 85 | }; |
| 86 | |
John Stiles | cd80689 | 2021-01-06 13:33:31 -0500 | [diff] [blame] | 87 | template <> |
| 88 | struct hash<SkSL::SPIRVVectorConstant> { |
| 89 | size_t operator()(const SkSL::SPIRVVectorConstant& key) const { |
| 90 | return SkOpts::hash(&key, sizeof(key)); |
| 91 | } |
| 92 | }; |
| 93 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 94 | } // namespace std |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 95 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 96 | namespace SkSL { |
| 97 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Converts a Program into a SPIR-V binary. |
| 100 | */ |
| 101 | class SPIRVCodeGenerator : public CodeGenerator { |
| 102 | public: |
| 103 | class LValue { |
| 104 | public: |
| 105 | virtual ~LValue() {} |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 106 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 107 | // returns a pointer to the lvalue, if possible. If the lvalue cannot be directly referenced |
John Stiles | 3f14d28 | 2021-02-05 09:31:04 -0500 | [diff] [blame] | 108 | // by a pointer (e.g. vector swizzles), returns -1. |
| 109 | virtual SpvId getPointer() { return -1; } |
| 110 | |
Ethan Nicholas | e0707b7 | 2021-03-17 11:16:41 -0400 | [diff] [blame] | 111 | // Returns true if a valid pointer returned by getPointer represents a memory object |
| 112 | // (see https://github.com/KhronosGroup/SPIRV-Tools/issues/2892). Has no meaning if |
| 113 | // getPointer() returns -1. |
| 114 | virtual bool isMemoryObjectPointer() const { return true; } |
| 115 | |
John Stiles | 3f14d28 | 2021-02-05 09:31:04 -0500 | [diff] [blame] | 116 | // Applies a swizzle to the components of the LValue, if possible. This is used to create |
| 117 | // LValues that are swizzes-of-swizzles. Non-swizzle LValues can just return false. |
| 118 | virtual bool applySwizzle(const ComponentArray& components, const Type& newType) { |
| 119 | return false; |
| 120 | } |
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 | virtual SpvId load(OutputStream& out) = 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 123 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 124 | virtual void store(SpvId value, OutputStream& out) = 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 127 | SPIRVCodeGenerator(const Context* context, |
| 128 | const Program* program, |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 129 | OutputStream* out) |
Ethan Nicholas | 3abc6c6 | 2021-08-13 11:20:09 -0400 | [diff] [blame] | 130 | : INHERITED(context, program, out) |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 131 | , fDefaultLayout(MemoryLayout::k140_Standard) |
| 132 | , fCapabilities(0) |
| 133 | , fIdCount(1) |
Brian Osman | 8b43dad | 2020-10-09 13:31:42 -0400 | [diff] [blame] | 134 | , fSetupFragPosition(false) |
| 135 | , fCurrentBlock(0) |
Ethan Nicholas | c7774a7 | 2021-08-27 15:34:05 -0400 | [diff] [blame] | 136 | , fSynthetics(fContext, /*builtin=*/true) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 137 | this->setupIntrinsics(); |
| 138 | } |
| 139 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 140 | bool generateCode() override; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 141 | |
| 142 | private: |
John Stiles | aaac4e4 | 2021-05-06 14:08:28 -0400 | [diff] [blame] | 143 | enum IntrinsicOpcodeKind { |
| 144 | kGLSL_STD_450_IntrinsicOpcodeKind, |
| 145 | kSPIRV_IntrinsicOpcodeKind, |
| 146 | kSpecial_IntrinsicOpcodeKind |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | enum SpecialIntrinsic { |
| 150 | kAtan_SpecialIntrinsic, |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 151 | kClamp_SpecialIntrinsic, |
Brian Osman | d1b593f | 2020-12-28 13:00:46 -0500 | [diff] [blame] | 152 | kMatrixCompMult_SpecialIntrinsic, |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 153 | kMax_SpecialIntrinsic, |
| 154 | kMin_SpecialIntrinsic, |
| 155 | kMix_SpecialIntrinsic, |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 156 | kMod_SpecialIntrinsic, |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 157 | kDFdy_SpecialIntrinsic, |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 158 | kSaturate_SpecialIntrinsic, |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 159 | kSampledImage_SpecialIntrinsic, |
Brian Osman | 6ba3be1 | 2020-11-13 16:32:52 -0500 | [diff] [blame] | 160 | kSmoothStep_SpecialIntrinsic, |
| 161 | kStep_SpecialIntrinsic, |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 162 | kSubpassLoad_SpecialIntrinsic, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 163 | kTexture_SpecialIntrinsic, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 164 | }; |
| 165 | |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 166 | enum class Precision { |
Ethan Nicholas | 8f352ce | 2021-03-17 14:12:20 -0400 | [diff] [blame] | 167 | kDefault, |
| 168 | kRelaxed, |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 169 | }; |
| 170 | |
John Stiles | 0736712 | 2021-09-08 13:12:26 -0400 | [diff] [blame] | 171 | struct TempVar { |
| 172 | SpvId spvId; |
| 173 | const Type* type; |
| 174 | std::unique_ptr<SPIRVCodeGenerator::LValue> lvalue; |
| 175 | }; |
| 176 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 177 | void setupIntrinsics(); |
| 178 | |
Ethan Nicholas | 8f352ce | 2021-03-17 14:12:20 -0400 | [diff] [blame] | 179 | /** |
| 180 | * Pass in the type to automatically add a RelaxedPrecision decoration for the id when |
| 181 | * appropriate, or null to never add one. |
| 182 | */ |
| 183 | SpvId nextId(const Type* type); |
| 184 | |
| 185 | SpvId nextId(Precision precision); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 186 | |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 187 | const Type& getActualType(const Type& type); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 188 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 189 | SpvId getType(const Type& type); |
| 190 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 191 | SpvId getType(const Type& type, const MemoryLayout& layout); |
| 192 | |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 193 | SpvId getImageType(const Type& type); |
| 194 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 195 | SpvId getFunctionType(const FunctionDeclaration& function); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 196 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 197 | SpvId getPointerType(const Type& type, SpvStorageClass_ storageClass); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 198 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 199 | SpvId getPointerType(const Type& type, const MemoryLayout& layout, |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 200 | SpvStorageClass_ storageClass); |
| 201 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 202 | std::vector<SpvId> getAccessChain(const Expression& expr, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 203 | |
| 204 | void writeLayout(const Layout& layout, SpvId target); |
| 205 | |
| 206 | void writeLayout(const Layout& layout, SpvId target, int member); |
| 207 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 208 | void writeStruct(const Type& type, const MemoryLayout& layout, SpvId resultId); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 209 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 210 | void writeProgramElement(const ProgramElement& pe, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 211 | |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 212 | SpvId writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTFlip = true); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 213 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 214 | SpvId writeFunctionStart(const FunctionDeclaration& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 215 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 216 | SpvId writeFunctionDeclaration(const FunctionDeclaration& f, OutputStream& out); |
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 writeFunction(const FunctionDefinition& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 219 | |
John Stiles | dbd4e6f | 2021-02-16 13:29:15 -0500 | [diff] [blame] | 220 | void writeGlobalVar(ProgramKind kind, const VarDeclaration& v); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 221 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 222 | void writeVarDeclaration(const VarDeclaration& var, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 223 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 224 | SpvId writeVariableReference(const VariableReference& ref, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 225 | |
John Stiles | e40d166 | 2021-01-29 10:08:50 -0500 | [diff] [blame] | 226 | int findUniformFieldIndex(const Variable& var) const; |
| 227 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 228 | std::unique_ptr<LValue> getLValue(const Expression& value, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 229 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 230 | SpvId writeExpression(const Expression& expr, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 231 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 232 | SpvId writeIntrinsicCall(const FunctionCall& c, OutputStream& out); |
| 233 | |
John Stiles | 0736712 | 2021-09-08 13:12:26 -0400 | [diff] [blame] | 234 | SpvId writeFunctionCallArgument(const Expression& arg, |
| 235 | const Modifiers& paramModifiers, |
| 236 | std::vector<TempVar>* tempVars, |
| 237 | OutputStream& out); |
| 238 | |
| 239 | void copyBackTempVars(const std::vector<TempVar>& tempVars, OutputStream& out); |
| 240 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 241 | SpvId writeFunctionCall(const FunctionCall& c, OutputStream& out); |
| 242 | |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 243 | |
| 244 | void writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst, |
| 245 | SpvId signedInst, SpvId unsignedInst, |
| 246 | const std::vector<SpvId>& args, OutputStream& out); |
| 247 | |
| 248 | /** |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 249 | * Promotes an expression to a vector. If the expression is already a vector with vectorSize |
| 250 | * columns, returns it unmodified. If the expression is a scalar, either promotes it to a |
| 251 | * vector (if vectorSize > 1) or returns it unmodified (if vectorSize == 1). Asserts if the |
| 252 | * expression is already a vector and it does not have vectorSize columns. |
| 253 | */ |
| 254 | SpvId vectorize(const Expression& expr, int vectorSize, OutputStream& out); |
| 255 | |
| 256 | /** |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 257 | * Given a list of potentially mixed scalars and vectors, promotes the scalars to match the |
| 258 | * size of the vectors and returns the ids of the written expressions. e.g. given (float, vec2), |
| 259 | * returns (vec2(float), vec2). It is an error to use mismatched vector sizes, e.g. (float, |
| 260 | * vec2, vec3). |
| 261 | */ |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 262 | std::vector<SpvId> vectorize(const ExpressionArray& args, OutputStream& out); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 263 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 264 | SpvId writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 265 | |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 266 | SpvId writeConstantVector(const AnyConstructor& c); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 267 | |
John Stiles | a91bf05 | 2021-05-17 09:34:03 -0400 | [diff] [blame] | 268 | SpvId writeScalarToMatrixSplat(const Type& matrixType, SpvId scalarId, OutputStream& out); |
| 269 | |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 270 | SpvId writeFloatConstructor(const AnyConstructor& c, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 271 | |
John Stiles | d9d5271 | 2021-01-13 17:15:02 -0500 | [diff] [blame] | 272 | SpvId castScalarToFloat(SpvId inputId, const Type& inputType, const Type& outputType, |
| 273 | OutputStream& out); |
| 274 | |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 275 | SpvId writeIntConstructor(const AnyConstructor& c, OutputStream& out); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 276 | |
John Stiles | d9d5271 | 2021-01-13 17:15:02 -0500 | [diff] [blame] | 277 | SpvId castScalarToSignedInt(SpvId inputId, const Type& inputType, const Type& outputType, |
| 278 | OutputStream& out); |
| 279 | |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 280 | SpvId writeUIntConstructor(const AnyConstructor& c, OutputStream& out); |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 281 | |
John Stiles | d9d5271 | 2021-01-13 17:15:02 -0500 | [diff] [blame] | 282 | SpvId castScalarToUnsignedInt(SpvId inputId, const Type& inputType, const Type& outputType, |
| 283 | OutputStream& out); |
| 284 | |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 285 | SpvId writeBooleanConstructor(const AnyConstructor& c, OutputStream& out); |
John Stiles | a60fb17 | 2021-01-14 11:06:20 -0500 | [diff] [blame] | 286 | |
John Stiles | 48c2884 | 2021-01-14 11:05:03 -0500 | [diff] [blame] | 287 | SpvId castScalarToBoolean(SpvId inputId, const Type& inputType, const Type& outputType, |
| 288 | OutputStream& out); |
| 289 | |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 290 | SpvId castScalarToType(SpvId inputExprId, const Type& inputType, const Type& outputType, |
| 291 | OutputStream& out); |
| 292 | |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 293 | /** |
| 294 | * Writes a matrix with the diagonal entries all equal to the provided expression, and all other |
| 295 | * entries equal to zero. |
| 296 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 297 | void writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, OutputStream& out); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 298 | |
| 299 | /** |
| 300 | * Writes a potentially-different-sized copy of a matrix. Entries which do not exist in the |
| 301 | * source matrix are filled with zero; entries which do not exist in the destination matrix are |
| 302 | * ignored. |
| 303 | */ |
John Stiles | 268a73f | 2021-04-07 12:30:22 -0400 | [diff] [blame] | 304 | SpvId writeMatrixCopy(SpvId src, const Type& srcType, const Type& dstType, OutputStream& out); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 305 | |
John Stiles | beb2fbf | 2021-07-08 18:54:39 -0400 | [diff] [blame] | 306 | void addColumnEntry(const Type& columnType, std::vector<SpvId>* currentColumn, |
| 307 | std::vector<SpvId>* columnIds, int rows, SpvId entry, OutputStream& out); |
Ethan Nicholas | 5c46b72 | 2019-03-22 14:32:37 -0400 | [diff] [blame] | 308 | |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 309 | SpvId writeConstructorCompound(const ConstructorCompound& c, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 310 | |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 311 | SpvId writeMatrixConstructor(const ConstructorCompound& c, OutputStream& out); |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 312 | |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 313 | SpvId writeVectorConstructor(const ConstructorCompound& c, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 314 | |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 315 | SpvId writeCompositeConstructor(const AnyConstructor& c, OutputStream& out); |
Ethan Nicholas | bd55322 | 2017-07-18 15:54:59 -0400 | [diff] [blame] | 316 | |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 317 | SpvId writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c, OutputStream& out); |
| 318 | |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 319 | SpvId writeConstructorMatrixResize(const ConstructorMatrixResize& c, OutputStream& out); |
| 320 | |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 321 | SpvId writeConstructorScalarCast(const ConstructorScalarCast& c, OutputStream& out); |
| 322 | |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 323 | SpvId writeConstructorSplat(const ConstructorSplat& c, OutputStream& out); |
| 324 | |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 325 | SpvId writeConstructorCompoundCast(const ConstructorCompoundCast& c, OutputStream& out); |
John Stiles | b14a819 | 2021-04-05 11:40:46 -0400 | [diff] [blame] | 326 | |
| 327 | SpvId writeComposite(const std::vector<SpvId>& arguments, const Type& type, OutputStream& out); |
| 328 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 329 | SpvId writeFieldAccess(const FieldAccess& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 330 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 331 | SpvId writeSwizzle(const Swizzle& swizzle, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 332 | |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 333 | /** |
| 334 | * Folds the potentially-vector result of a logical operation down to a single bool. If |
| 335 | * operandType is a vector type, assumes that the intermediate result in id is a bvec of the |
| 336 | * same dimensions, and applys all() to it to fold it down to a single bool value. Otherwise, |
| 337 | * returns the original id value. |
| 338 | */ |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 339 | SpvId foldToBool(SpvId id, const Type& operandType, SpvOp op, OutputStream& out); |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 340 | |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 341 | SpvId writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, SpvOp_ floatOperator, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 342 | SpvOp_ intOperator, SpvOp_ vectorMergeOperator, |
| 343 | SpvOp_ mergeOperator, OutputStream& out); |
| 344 | |
John Stiles | bc5c2a0 | 2021-04-08 11:44:53 -0400 | [diff] [blame] | 345 | SpvId writeStructComparison(const Type& structType, SpvId lhs, Operator op, SpvId rhs, |
| 346 | OutputStream& out); |
| 347 | |
John Stiles | 3509210 | 2021-04-08 23:30:51 -0400 | [diff] [blame] | 348 | SpvId writeArrayComparison(const Type& structType, SpvId lhs, Operator op, SpvId rhs, |
| 349 | OutputStream& out); |
| 350 | |
| 351 | // Used by writeStructComparison and writeArrayComparison to logically combine field-by-field |
| 352 | // comparisons into an overall comparison result. |
| 353 | // - `a.x == b.x` merged with `a.y == b.y` generates `(a.x == b.x) && (a.y == b.y)` |
| 354 | // - `a.x != b.x` merged with `a.y != b.y` generates `(a.x != b.x) || (a.y != b.y)` |
| 355 | SpvId mergeComparisons(SpvId comparison, SpvId allComparisons, Operator op, OutputStream& out); |
| 356 | |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 357 | SpvId writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, SpvId rhs, |
John Stiles | 43b593c | 2021-05-13 22:03:27 -0400 | [diff] [blame] | 358 | SpvOp_ op, OutputStream& out); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 359 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 360 | SpvId writeBinaryOperation(const Type& resultType, const Type& operandType, SpvId lhs, |
| 361 | SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, SpvOp_ ifUInt, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 362 | SpvOp_ ifBool, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 363 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 364 | SpvId writeBinaryOperation(const BinaryExpression& expr, SpvOp_ ifFloat, SpvOp_ ifInt, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 365 | SpvOp_ ifUInt, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 366 | |
John Stiles | d94bfdd | 2021-03-25 11:44:08 -0400 | [diff] [blame] | 367 | SpvId writeReciprocal(const Type& type, SpvId value, OutputStream& out); |
| 368 | |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 369 | SpvId writeBinaryExpression(const Type& leftType, SpvId lhs, Operator op, |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 370 | const Type& rightType, SpvId rhs, const Type& resultType, |
| 371 | OutputStream& out); |
| 372 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 373 | SpvId writeBinaryExpression(const BinaryExpression& b, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 374 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 375 | SpvId writeTernaryExpression(const TernaryExpression& t, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 376 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 377 | SpvId writeIndexExpression(const IndexExpression& expr, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 378 | |
John Stiles | bc5c2a0 | 2021-04-08 11:44:53 -0400 | [diff] [blame] | 379 | SpvId writeLogicalAnd(const Expression& left, const Expression& right, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 380 | |
John Stiles | bc5c2a0 | 2021-04-08 11:44:53 -0400 | [diff] [blame] | 381 | SpvId writeLogicalOr(const Expression& left, const Expression& right, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 382 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 383 | SpvId writePrefixExpression(const PrefixExpression& p, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 384 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 385 | SpvId writePostfixExpression(const PostfixExpression& p, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 386 | |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 387 | SpvId writeLiteral(const Literal& f); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 388 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 389 | void writeStatement(const Statement& s, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 390 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 391 | void writeBlock(const Block& b, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 392 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 393 | void writeIfStatement(const IfStatement& stmt, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 394 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 395 | void writeForStatement(const ForStatement& f, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 396 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 397 | void writeDoStatement(const DoStatement& d, OutputStream& out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 398 | |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 399 | void writeSwitchStatement(const SwitchStatement& s, OutputStream& out); |
| 400 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 401 | void writeReturnStatement(const ReturnStatement& r, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 402 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 403 | void writeCapabilities(OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 404 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 405 | void writeInstructions(const Program& program, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 406 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 407 | void writeOpCode(SpvOp_ opCode, int length, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 408 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 409 | void writeWord(int32_t word, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 410 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 411 | void writeString(skstd::string_view s, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 412 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 413 | void writeLabel(SpvId id, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 414 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 415 | void writeInstruction(SpvOp_ opCode, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 416 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 417 | void writeInstruction(SpvOp_ opCode, skstd::string_view string, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 418 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 419 | void writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 420 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 421 | void writeInstruction(SpvOp_ opCode, int32_t word1, skstd::string_view string, |
| 422 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 423 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 424 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, skstd::string_view string, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 425 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 426 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 427 | void writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 428 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 429 | 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] | 430 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 431 | |
| 432 | 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] | 433 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 434 | |
| 435 | 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] | 436 | int32_t word5, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 437 | |
| 438 | 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] | 439 | int32_t word5, int32_t word6, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 440 | |
| 441 | 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] | 442 | int32_t word5, int32_t word6, int32_t word7, OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 443 | |
| 444 | 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] | 445 | int32_t word5, int32_t word6, int32_t word7, int32_t word8, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 446 | OutputStream& out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 447 | |
John Stiles | d7437ee | 2021-08-02 11:56:16 -0400 | [diff] [blame] | 448 | bool isDead(const Variable& var) const; |
| 449 | |
Brian Osman | 2a4c0fb | 2021-01-22 13:41:40 -0500 | [diff] [blame] | 450 | MemoryLayout memoryLayoutForVariable(const Variable&) const; |
| 451 | |
John Stiles | 4d6310a | 2021-01-26 19:58:22 -0500 | [diff] [blame] | 452 | struct EntrypointAdapter { |
| 453 | std::unique_ptr<FunctionDefinition> entrypointDef; |
| 454 | std::unique_ptr<FunctionDeclaration> entrypointDecl; |
| 455 | Layout fLayout; |
| 456 | Modifiers fModifiers; |
| 457 | }; |
| 458 | |
| 459 | EntrypointAdapter writeEntrypointAdapter(const FunctionDeclaration& main); |
| 460 | |
John Stiles | e40d166 | 2021-01-29 10:08:50 -0500 | [diff] [blame] | 461 | struct UniformBuffer { |
| 462 | std::unique_ptr<InterfaceBlock> fInterfaceBlock; |
| 463 | std::unique_ptr<Variable> fInnerVariable; |
| 464 | std::unique_ptr<Type> fStruct; |
| 465 | }; |
| 466 | |
| 467 | void writeUniformBuffer(std::shared_ptr<SymbolTable> topLevelSymbolTable); |
| 468 | |
Ethan Nicholas | 89cfde1 | 2021-09-27 11:20:34 -0400 | [diff] [blame] | 469 | void addRTFlipUniform(int line); |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 470 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 471 | const MemoryLayout fDefaultLayout; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 472 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 473 | uint64_t fCapabilities; |
| 474 | SpvId fIdCount; |
| 475 | SpvId fGLSLExtendedInstructions; |
John Stiles | aaac4e4 | 2021-05-06 14:08:28 -0400 | [diff] [blame] | 476 | typedef std::tuple<IntrinsicOpcodeKind, int32_t, int32_t, int32_t, int32_t> Intrinsic; |
| 477 | std::unordered_map<IntrinsicKind, Intrinsic> fIntrinsicMap; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 478 | std::unordered_map<const FunctionDeclaration*, SpvId> fFunctionMap; |
| 479 | std::unordered_map<const Variable*, SpvId> fVariableMap; |
| 480 | std::unordered_map<const Variable*, int32_t> fInterfaceBlockMap; |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 481 | std::unordered_map<String, SpvId> fImageTypeMap; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 482 | std::unordered_map<String, SpvId> fTypeMap; |
| 483 | StringStream fCapabilitiesBuffer; |
| 484 | StringStream fGlobalInitializersBuffer; |
| 485 | StringStream fConstantBuffer; |
| 486 | StringStream fExtraGlobalsBuffer; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 487 | StringStream fVariableBuffer; |
| 488 | StringStream fNameBuffer; |
| 489 | StringStream fDecorationBuffer; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 490 | |
John Stiles | bdc3d3c | 2021-01-06 18:41:40 -0500 | [diff] [blame] | 491 | std::unordered_map<SPIRVNumberConstant, SpvId> fNumberConstants; |
John Stiles | cd80689 | 2021-01-06 13:33:31 -0500 | [diff] [blame] | 492 | std::unordered_map<SPIRVVectorConstant, SpvId> fVectorConstants; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 493 | bool fSetupFragPosition; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 494 | // label of the current block, or 0 if we are not in a block |
| 495 | SpvId fCurrentBlock; |
| 496 | std::stack<SpvId> fBreakTarget; |
| 497 | std::stack<SpvId> fContinueTarget; |
Brian Salomon | d8d85b9 | 2021-07-07 09:41:17 -0400 | [diff] [blame] | 498 | bool fWroteRTFlip = false; |
Ethan Nicholas | 8feeff9 | 2017-03-30 14:11:58 -0400 | [diff] [blame] | 499 | // holds variables synthesized during output, for lifetime purposes |
| 500 | SymbolTable fSynthetics; |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 501 | int fSkInCount = 1; |
John Stiles | e40d166 | 2021-01-29 10:08:50 -0500 | [diff] [blame] | 502 | // Holds a list of uniforms that were declared as globals at the top-level instead of in an |
| 503 | // interface block. |
| 504 | UniformBuffer fUniformBuffer; |
| 505 | std::vector<const VarDeclaration*> fTopLevelUniforms; |
| 506 | std::unordered_map<const Variable*, int> fTopLevelUniformMap; //<var, UniformBuffer field index> |
John Stiles | d7437ee | 2021-08-02 11:56:16 -0400 | [diff] [blame] | 507 | std::unordered_set<const Variable*> fSPIRVBonusVariables; |
John Stiles | e40d166 | 2021-01-29 10:08:50 -0500 | [diff] [blame] | 508 | SpvId fUniformBufferId = -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 509 | |
| 510 | friend class PointerLValue; |
| 511 | friend class SwizzleLValue; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 512 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 513 | using INHERITED = CodeGenerator; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 514 | }; |
| 515 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 516 | } // namespace SkSL |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 517 | |
| 518 | #endif |