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 | bcf35f8 | 2017-03-30 18:42:48 +0000 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLSPIRVCodeGenerator.h" |
Ethan Nicholas | 9bd301d | 2017-03-31 16:04:34 +0000 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/sksl/GLSL.std.450.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/sksl/SkSLCompiler.h" |
| 13 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 14 | #include "src/sksl/ir/SkSLExtension.h" |
| 15 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 16 | #include "src/sksl/ir/SkSLVariableReference.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 17 | |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 18 | #ifdef SK_VULKAN |
| 19 | #include "src/gpu/vk/GrVkCaps.h" |
| 20 | #endif |
| 21 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 22 | namespace SkSL { |
| 23 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 24 | static const int32_t SKSL_MAGIC = 0x0; // FIXME: we should probably register a magic number |
| 25 | |
| 26 | void SPIRVCodeGenerator::setupIntrinsics() { |
| 27 | #define ALL_GLSL(x) std::make_tuple(kGLSL_STD_450_IntrinsicKind, GLSLstd450 ## x, GLSLstd450 ## x, \ |
| 28 | GLSLstd450 ## x, GLSLstd450 ## x) |
| 29 | #define BY_TYPE_GLSL(ifFloat, ifInt, ifUInt) std::make_tuple(kGLSL_STD_450_IntrinsicKind, \ |
| 30 | GLSLstd450 ## ifFloat, \ |
| 31 | GLSLstd450 ## ifInt, \ |
| 32 | GLSLstd450 ## ifUInt, \ |
| 33 | SpvOpUndef) |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 34 | #define ALL_SPIRV(x) std::make_tuple(kSPIRV_IntrinsicKind, SpvOp ## x, SpvOp ## x, SpvOp ## x, \ |
| 35 | SpvOp ## x) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 36 | #define SPECIAL(x) std::make_tuple(kSpecial_IntrinsicKind, k ## x ## _SpecialIntrinsic, \ |
| 37 | k ## x ## _SpecialIntrinsic, k ## x ## _SpecialIntrinsic, \ |
| 38 | k ## x ## _SpecialIntrinsic) |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 39 | fIntrinsicMap[String("round")] = ALL_GLSL(Round); |
| 40 | fIntrinsicMap[String("roundEven")] = ALL_GLSL(RoundEven); |
| 41 | fIntrinsicMap[String("trunc")] = ALL_GLSL(Trunc); |
| 42 | fIntrinsicMap[String("abs")] = BY_TYPE_GLSL(FAbs, SAbs, SAbs); |
| 43 | fIntrinsicMap[String("sign")] = BY_TYPE_GLSL(FSign, SSign, SSign); |
| 44 | fIntrinsicMap[String("floor")] = ALL_GLSL(Floor); |
| 45 | fIntrinsicMap[String("ceil")] = ALL_GLSL(Ceil); |
| 46 | fIntrinsicMap[String("fract")] = ALL_GLSL(Fract); |
| 47 | fIntrinsicMap[String("radians")] = ALL_GLSL(Radians); |
| 48 | fIntrinsicMap[String("degrees")] = ALL_GLSL(Degrees); |
| 49 | fIntrinsicMap[String("sin")] = ALL_GLSL(Sin); |
| 50 | fIntrinsicMap[String("cos")] = ALL_GLSL(Cos); |
| 51 | fIntrinsicMap[String("tan")] = ALL_GLSL(Tan); |
| 52 | fIntrinsicMap[String("asin")] = ALL_GLSL(Asin); |
| 53 | fIntrinsicMap[String("acos")] = ALL_GLSL(Acos); |
| 54 | fIntrinsicMap[String("atan")] = SPECIAL(Atan); |
| 55 | fIntrinsicMap[String("sinh")] = ALL_GLSL(Sinh); |
| 56 | fIntrinsicMap[String("cosh")] = ALL_GLSL(Cosh); |
| 57 | fIntrinsicMap[String("tanh")] = ALL_GLSL(Tanh); |
| 58 | fIntrinsicMap[String("asinh")] = ALL_GLSL(Asinh); |
| 59 | fIntrinsicMap[String("acosh")] = ALL_GLSL(Acosh); |
| 60 | fIntrinsicMap[String("atanh")] = ALL_GLSL(Atanh); |
| 61 | fIntrinsicMap[String("pow")] = ALL_GLSL(Pow); |
| 62 | fIntrinsicMap[String("exp")] = ALL_GLSL(Exp); |
| 63 | fIntrinsicMap[String("log")] = ALL_GLSL(Log); |
| 64 | fIntrinsicMap[String("exp2")] = ALL_GLSL(Exp2); |
| 65 | fIntrinsicMap[String("log2")] = ALL_GLSL(Log2); |
| 66 | fIntrinsicMap[String("sqrt")] = ALL_GLSL(Sqrt); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 67 | fIntrinsicMap[String("inverse")] = ALL_GLSL(MatrixInverse); |
John Stiles | a07338f | 2020-12-17 12:39:08 -0500 | [diff] [blame] | 68 | fIntrinsicMap[String("outerProduct")] = ALL_SPIRV(OuterProduct); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 69 | fIntrinsicMap[String("transpose")] = ALL_SPIRV(Transpose); |
John Stiles | 3679cd1 | 2020-12-09 16:22:12 -0500 | [diff] [blame] | 70 | fIntrinsicMap[String("isinf")] = ALL_SPIRV(IsInf); |
| 71 | fIntrinsicMap[String("isnan")] = ALL_SPIRV(IsNan); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 72 | fIntrinsicMap[String("inversesqrt")] = ALL_GLSL(InverseSqrt); |
| 73 | fIntrinsicMap[String("determinant")] = ALL_GLSL(Determinant); |
| 74 | fIntrinsicMap[String("matrixInverse")] = ALL_GLSL(MatrixInverse); |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 75 | fIntrinsicMap[String("mod")] = SPECIAL(Mod); |
John Stiles | 0195727 | 2020-12-09 17:14:47 -0500 | [diff] [blame] | 76 | fIntrinsicMap[String("modf")] = ALL_GLSL(Modf); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 77 | fIntrinsicMap[String("min")] = SPECIAL(Min); |
| 78 | fIntrinsicMap[String("max")] = SPECIAL(Max); |
| 79 | fIntrinsicMap[String("clamp")] = SPECIAL(Clamp); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 80 | fIntrinsicMap[String("saturate")] = SPECIAL(Saturate); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 81 | fIntrinsicMap[String("dot")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDot, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 82 | SpvOpUndef, SpvOpUndef, SpvOpUndef); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 83 | fIntrinsicMap[String("mix")] = SPECIAL(Mix); |
Brian Osman | 6ba3be1 | 2020-11-13 16:32:52 -0500 | [diff] [blame] | 84 | fIntrinsicMap[String("step")] = SPECIAL(Step); |
| 85 | fIntrinsicMap[String("smoothstep")] = SPECIAL(SmoothStep); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 86 | fIntrinsicMap[String("fma")] = ALL_GLSL(Fma); |
| 87 | fIntrinsicMap[String("frexp")] = ALL_GLSL(Frexp); |
| 88 | fIntrinsicMap[String("ldexp")] = ALL_GLSL(Ldexp); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 89 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 90 | #define PACK(type) fIntrinsicMap[String("pack" #type)] = ALL_GLSL(Pack ## type); \ |
| 91 | fIntrinsicMap[String("unpack" #type)] = ALL_GLSL(Unpack ## type) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 92 | PACK(Snorm4x8); |
| 93 | PACK(Unorm4x8); |
| 94 | PACK(Snorm2x16); |
| 95 | PACK(Unorm2x16); |
| 96 | PACK(Half2x16); |
| 97 | PACK(Double2x32); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 98 | fIntrinsicMap[String("length")] = ALL_GLSL(Length); |
| 99 | fIntrinsicMap[String("distance")] = ALL_GLSL(Distance); |
| 100 | fIntrinsicMap[String("cross")] = ALL_GLSL(Cross); |
| 101 | fIntrinsicMap[String("normalize")] = ALL_GLSL(Normalize); |
John Stiles | 0d19fb4 | 2020-12-10 17:04:37 -0500 | [diff] [blame] | 102 | fIntrinsicMap[String("faceforward")] = ALL_GLSL(FaceForward); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 103 | fIntrinsicMap[String("reflect")] = ALL_GLSL(Reflect); |
| 104 | fIntrinsicMap[String("refract")] = ALL_GLSL(Refract); |
John Stiles | e7dc7cb | 2020-12-22 15:37:14 -0500 | [diff] [blame^] | 105 | fIntrinsicMap[String("bitCount")] = ALL_SPIRV(BitCount); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 106 | fIntrinsicMap[String("findLSB")] = ALL_GLSL(FindILsb); |
| 107 | fIntrinsicMap[String("findMSB")] = BY_TYPE_GLSL(FindSMsb, FindSMsb, FindUMsb); |
| 108 | fIntrinsicMap[String("dFdx")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDPdx, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 109 | SpvOpUndef, SpvOpUndef, SpvOpUndef); |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 110 | fIntrinsicMap[String("dFdy")] = SPECIAL(DFdy); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 111 | fIntrinsicMap[String("fwidth")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpFwidth, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 112 | SpvOpUndef, SpvOpUndef, SpvOpUndef); |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 113 | fIntrinsicMap[String("makeSampler2D")] = SPECIAL(SampledImage); |
| 114 | |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 115 | fIntrinsicMap[String("sample")] = SPECIAL(Texture); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 116 | fIntrinsicMap[String("subpassLoad")] = SPECIAL(SubpassLoad); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 117 | |
John Stiles | cc9ff00 | 2020-12-09 18:39:41 -0500 | [diff] [blame] | 118 | fIntrinsicMap[String("floatBitsToInt")] = ALL_SPIRV(Bitcast); |
| 119 | fIntrinsicMap[String("floatBitsToUint")] = ALL_SPIRV(Bitcast); |
| 120 | fIntrinsicMap[String("intBitsToFloat")] = ALL_SPIRV(Bitcast); |
| 121 | fIntrinsicMap[String("uintBitsToFloat")] = ALL_SPIRV(Bitcast); |
| 122 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 123 | fIntrinsicMap[String("any")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 124 | SpvOpUndef, SpvOpUndef, SpvOpAny); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 125 | fIntrinsicMap[String("all")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 126 | SpvOpUndef, SpvOpUndef, SpvOpAll); |
Brian Osman | 540c13a | 2020-11-24 16:55:34 -0500 | [diff] [blame] | 127 | fIntrinsicMap[String("not")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef, |
| 128 | SpvOpUndef, SpvOpUndef, |
| 129 | SpvOpLogicalNot); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 130 | fIntrinsicMap[String("equal")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 131 | SpvOpFOrdEqual, SpvOpIEqual, |
| 132 | SpvOpIEqual, SpvOpLogicalEqual); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 133 | fIntrinsicMap[String("notEqual")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 134 | SpvOpFOrdNotEqual, SpvOpINotEqual, |
| 135 | SpvOpINotEqual, |
| 136 | SpvOpLogicalNotEqual); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 137 | fIntrinsicMap[String("lessThan")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 138 | SpvOpFOrdLessThan, SpvOpSLessThan, |
| 139 | SpvOpULessThan, SpvOpUndef); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 140 | fIntrinsicMap[String("lessThanEqual")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 141 | SpvOpFOrdLessThanEqual, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 142 | SpvOpSLessThanEqual, |
| 143 | SpvOpULessThanEqual, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 144 | SpvOpUndef); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 145 | fIntrinsicMap[String("greaterThan")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 146 | SpvOpFOrdGreaterThan, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 147 | SpvOpSGreaterThan, |
| 148 | SpvOpUGreaterThan, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 149 | SpvOpUndef); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 150 | fIntrinsicMap[String("greaterThanEqual")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 151 | SpvOpFOrdGreaterThanEqual, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 152 | SpvOpSGreaterThanEqual, |
| 153 | SpvOpUGreaterThanEqual, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 154 | SpvOpUndef); |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 155 | fIntrinsicMap[String("EmitVertex")] = ALL_SPIRV(EmitVertex); |
| 156 | fIntrinsicMap[String("EndPrimitive")] = ALL_SPIRV(EndPrimitive); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 157 | // interpolateAt* not yet supported... |
| 158 | } |
| 159 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 160 | void SPIRVCodeGenerator::writeWord(int32_t word, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 161 | out.write((const char*) &word, sizeof(word)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 162 | } |
| 163 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 164 | static bool is_float(const Context& context, const Type& type) { |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 165 | if (type.columns() > 1) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 166 | return is_float(context, type.componentType()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 167 | } |
John Stiles | 123501f | 2020-12-09 10:08:13 -0500 | [diff] [blame] | 168 | return type.isFloat(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 169 | } |
| 170 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 171 | static bool is_signed(const Context& context, const Type& type) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 172 | if (type.isVector()) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 173 | return is_signed(context, type.componentType()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 174 | } |
John Stiles | 123501f | 2020-12-09 10:08:13 -0500 | [diff] [blame] | 175 | return type.isSigned(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 176 | } |
| 177 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 178 | static bool is_unsigned(const Context& context, const Type& type) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 179 | if (type.isVector()) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 180 | return is_unsigned(context, type.componentType()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 181 | } |
John Stiles | 123501f | 2020-12-09 10:08:13 -0500 | [diff] [blame] | 182 | return type.isUnsigned(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 183 | } |
| 184 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 185 | static bool is_bool(const Context& context, const Type& type) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 186 | if (type.isVector()) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 187 | return is_bool(context, type.componentType()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 188 | } |
John Stiles | 123501f | 2020-12-09 10:08:13 -0500 | [diff] [blame] | 189 | return type.isBoolean(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 190 | } |
| 191 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 192 | static bool is_out(const Variable& var) { |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 193 | return (var.modifiers().fFlags & Modifiers::kOut_Flag) != 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 196 | void SPIRVCodeGenerator::writeOpCode(SpvOp_ opCode, int length, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 197 | SkASSERT(opCode != SpvOpLoad || &out != &fConstantBuffer); |
| 198 | SkASSERT(opCode != SpvOpUndef); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 199 | switch (opCode) { |
| 200 | case SpvOpReturn: // fall through |
| 201 | case SpvOpReturnValue: // fall through |
ethannicholas | 552882f | 2016-07-07 06:30:48 -0700 | [diff] [blame] | 202 | case SpvOpKill: // fall through |
Ethan Nicholas | 7fb3936 | 2020-12-16 15:25:19 -0500 | [diff] [blame] | 203 | case SpvOpSwitch: // fall through |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 204 | case SpvOpBranch: // fall through |
| 205 | case SpvOpBranchConditional: |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 206 | SkASSERT(fCurrentBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 207 | fCurrentBlock = 0; |
| 208 | break; |
| 209 | case SpvOpConstant: // fall through |
| 210 | case SpvOpConstantTrue: // fall through |
| 211 | case SpvOpConstantFalse: // fall through |
| 212 | case SpvOpConstantComposite: // fall through |
| 213 | case SpvOpTypeVoid: // fall through |
| 214 | case SpvOpTypeInt: // fall through |
| 215 | case SpvOpTypeFloat: // fall through |
| 216 | case SpvOpTypeBool: // fall through |
| 217 | case SpvOpTypeVector: // fall through |
| 218 | case SpvOpTypeMatrix: // fall through |
| 219 | case SpvOpTypeArray: // fall through |
| 220 | case SpvOpTypePointer: // fall through |
| 221 | case SpvOpTypeFunction: // fall through |
| 222 | case SpvOpTypeRuntimeArray: // fall through |
| 223 | case SpvOpTypeStruct: // fall through |
| 224 | case SpvOpTypeImage: // fall through |
| 225 | case SpvOpTypeSampledImage: // fall through |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 226 | case SpvOpTypeSampler: // fall through |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 227 | case SpvOpVariable: // fall through |
| 228 | case SpvOpFunction: // fall through |
| 229 | case SpvOpFunctionParameter: // fall through |
| 230 | case SpvOpFunctionEnd: // fall through |
| 231 | case SpvOpExecutionMode: // fall through |
| 232 | case SpvOpMemoryModel: // fall through |
| 233 | case SpvOpCapability: // fall through |
| 234 | case SpvOpExtInstImport: // fall through |
| 235 | case SpvOpEntryPoint: // fall through |
| 236 | case SpvOpSource: // fall through |
| 237 | case SpvOpSourceExtension: // fall through |
| 238 | case SpvOpName: // fall through |
| 239 | case SpvOpMemberName: // fall through |
| 240 | case SpvOpDecorate: // fall through |
| 241 | case SpvOpMemberDecorate: |
| 242 | break; |
| 243 | default: |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 244 | SkASSERT(fCurrentBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 245 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 246 | this->writeWord((length << 16) | opCode, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 249 | void SPIRVCodeGenerator::writeLabel(SpvId label, OutputStream& out) { |
Ethan Nicholas | 7fb3936 | 2020-12-16 15:25:19 -0500 | [diff] [blame] | 250 | SkASSERT(!fCurrentBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 251 | fCurrentBlock = label; |
| 252 | this->writeInstruction(SpvOpLabel, label, out); |
| 253 | } |
| 254 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 255 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 256 | this->writeOpCode(opCode, 1, out); |
| 257 | } |
| 258 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 259 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 260 | this->writeOpCode(opCode, 2, out); |
| 261 | this->writeWord(word1, out); |
| 262 | } |
| 263 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 264 | void SPIRVCodeGenerator::writeString(const char* string, size_t length, OutputStream& out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 265 | out.write(string, length); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 266 | switch (length % 4) { |
| 267 | case 1: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 268 | out.write8(0); |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 269 | [[fallthrough]]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 270 | case 2: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 271 | out.write8(0); |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 272 | [[fallthrough]]; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 273 | case 3: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 274 | out.write8(0); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 275 | break; |
| 276 | default: |
| 277 | this->writeWord(0, out); |
| 278 | } |
| 279 | } |
| 280 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 281 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out) { |
| 282 | this->writeOpCode(opCode, 1 + (string.fLength + 4) / 4, out); |
| 283 | this->writeString(string.fChars, string.fLength, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 287 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 288 | OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 289 | this->writeOpCode(opCode, 2 + (string.fLength + 4) / 4, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 290 | this->writeWord(word1, out); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 291 | this->writeString(string.fChars, string.fLength, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 294 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 295 | StringFragment string, OutputStream& out) { |
| 296 | this->writeOpCode(opCode, 3 + (string.fLength + 4) / 4, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 297 | this->writeWord(word1, out); |
| 298 | this->writeWord(word2, out); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 299 | this->writeString(string.fChars, string.fLength, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 302 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 303 | OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 304 | this->writeOpCode(opCode, 3, out); |
| 305 | this->writeWord(word1, out); |
| 306 | this->writeWord(word2, out); |
| 307 | } |
| 308 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 309 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 310 | int32_t word3, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 311 | this->writeOpCode(opCode, 4, out); |
| 312 | this->writeWord(word1, out); |
| 313 | this->writeWord(word2, out); |
| 314 | this->writeWord(word3, out); |
| 315 | } |
| 316 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 317 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 318 | int32_t word3, int32_t word4, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 319 | this->writeOpCode(opCode, 5, out); |
| 320 | this->writeWord(word1, out); |
| 321 | this->writeWord(word2, out); |
| 322 | this->writeWord(word3, out); |
| 323 | this->writeWord(word4, out); |
| 324 | } |
| 325 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 326 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
| 327 | int32_t word3, int32_t word4, int32_t word5, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 328 | OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 329 | this->writeOpCode(opCode, 6, out); |
| 330 | this->writeWord(word1, out); |
| 331 | this->writeWord(word2, out); |
| 332 | this->writeWord(word3, out); |
| 333 | this->writeWord(word4, out); |
| 334 | this->writeWord(word5, out); |
| 335 | } |
| 336 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 337 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 338 | int32_t word3, int32_t word4, int32_t word5, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 339 | int32_t word6, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 340 | this->writeOpCode(opCode, 7, out); |
| 341 | this->writeWord(word1, out); |
| 342 | this->writeWord(word2, out); |
| 343 | this->writeWord(word3, out); |
| 344 | this->writeWord(word4, out); |
| 345 | this->writeWord(word5, out); |
| 346 | this->writeWord(word6, out); |
| 347 | } |
| 348 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 349 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 350 | int32_t word3, int32_t word4, int32_t word5, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 351 | int32_t word6, int32_t word7, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 352 | this->writeOpCode(opCode, 8, out); |
| 353 | this->writeWord(word1, out); |
| 354 | this->writeWord(word2, out); |
| 355 | this->writeWord(word3, out); |
| 356 | this->writeWord(word4, out); |
| 357 | this->writeWord(word5, out); |
| 358 | this->writeWord(word6, out); |
| 359 | this->writeWord(word7, out); |
| 360 | } |
| 361 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 362 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 363 | int32_t word3, int32_t word4, int32_t word5, |
| 364 | 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 | this->writeOpCode(opCode, 9, out); |
| 367 | this->writeWord(word1, out); |
| 368 | this->writeWord(word2, out); |
| 369 | this->writeWord(word3, out); |
| 370 | this->writeWord(word4, out); |
| 371 | this->writeWord(word5, out); |
| 372 | this->writeWord(word6, out); |
| 373 | this->writeWord(word7, out); |
| 374 | this->writeWord(word8, out); |
| 375 | } |
| 376 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 377 | void SPIRVCodeGenerator::writeCapabilities(OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 378 | for (uint64_t i = 0, bit = 1; i <= kLast_Capability; i++, bit <<= 1) { |
| 379 | if (fCapabilities & bit) { |
| 380 | this->writeInstruction(SpvOpCapability, (SpvId) i, out); |
| 381 | } |
| 382 | } |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 383 | if (fProgram.fKind == Program::kGeometry_Kind) { |
| 384 | this->writeInstruction(SpvOpCapability, SpvCapabilityGeometry, out); |
| 385 | } |
Ethan Nicholas | 81d1511 | 2018-07-13 12:48:50 -0400 | [diff] [blame] | 386 | else { |
| 387 | this->writeInstruction(SpvOpCapability, SpvCapabilityShader, out); |
| 388 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | SpvId SPIRVCodeGenerator::nextId() { |
| 392 | return fIdCount++; |
| 393 | } |
| 394 | |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 395 | void SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& memoryLayout, |
| 396 | SpvId resultId) { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 397 | this->writeInstruction(SpvOpName, resultId, String(type.name()).c_str(), fNameBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 398 | // go ahead and write all of the field types, so we don't inadvertently write them while we're |
| 399 | // in the middle of writing the struct instruction |
| 400 | std::vector<SpvId> types; |
| 401 | for (const auto& f : type.fields()) { |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 402 | types.push_back(this->getType(*f.fType, memoryLayout)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 403 | } |
| 404 | this->writeOpCode(SpvOpTypeStruct, 2 + (int32_t) types.size(), fConstantBuffer); |
| 405 | this->writeWord(resultId, fConstantBuffer); |
| 406 | for (SpvId id : types) { |
| 407 | this->writeWord(id, fConstantBuffer); |
| 408 | } |
| 409 | size_t offset = 0; |
| 410 | for (int32_t i = 0; i < (int32_t) type.fields().size(); i++) { |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 411 | const Type::Field& field = type.fields()[i]; |
John Stiles | 21f5f45 | 2020-11-30 09:57:59 -0500 | [diff] [blame] | 412 | if (!MemoryLayout::LayoutIsSupported(*field.fType)) { |
John Stiles | 0023c0c | 2020-11-16 13:32:18 -0500 | [diff] [blame] | 413 | fErrors.error(type.fOffset, "type '" + field.fType->name() + "' is not permitted here"); |
| 414 | return; |
| 415 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 416 | size_t size = memoryLayout.size(*field.fType); |
| 417 | size_t alignment = memoryLayout.alignment(*field.fType); |
| 418 | const Layout& fieldLayout = field.fModifiers.fLayout; |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 419 | if (fieldLayout.fOffset >= 0) { |
Greg Daniel | dbd44c7 | 2017-01-24 15:12:12 -0500 | [diff] [blame] | 420 | if (fieldLayout.fOffset < (int) offset) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 421 | fErrors.error(type.fOffset, |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 422 | "offset of field '" + field.fName + "' must be at " |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 423 | "least " + to_string((int) offset)); |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 424 | } |
| 425 | if (fieldLayout.fOffset % alignment) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 426 | fErrors.error(type.fOffset, |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 427 | "offset of field '" + field.fName + "' must be a multiple" |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 428 | " of " + to_string((int) alignment)); |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 429 | } |
| 430 | offset = fieldLayout.fOffset; |
| 431 | } else { |
| 432 | size_t mod = offset % alignment; |
| 433 | if (mod) { |
| 434 | offset += alignment - mod; |
| 435 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 436 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 437 | this->writeInstruction(SpvOpMemberName, resultId, i, field.fName, fNameBuffer); |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 438 | this->writeLayout(fieldLayout, resultId, i); |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 439 | if (field.fModifiers.fLayout.fBuiltin < 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 440 | this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, SpvDecorationOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 441 | (SpvId) offset, fDecorationBuffer); |
| 442 | } |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 443 | if (field.fType->isMatrix()) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 444 | this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationColMajor, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 445 | fDecorationBuffer); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 446 | this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationMatrixStride, |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 447 | (SpvId) memoryLayout.stride(*field.fType), |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 448 | fDecorationBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 449 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 450 | if (!field.fType->highPrecision()) { |
| 451 | this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, |
| 452 | SpvDecorationRelaxedPrecision, fDecorationBuffer); |
| 453 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 454 | offset += size; |
John Stiles | c0c5106 | 2020-12-03 17:16:29 -0500 | [diff] [blame] | 455 | if ((field.fType->isArray() || field.fType->isStruct()) && offset % alignment != 0) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 456 | offset += alignment - offset % alignment; |
| 457 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 458 | } |
| 459 | } |
| 460 | |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 461 | const Type& SPIRVCodeGenerator::getActualType(const Type& type) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 462 | if (type.isFloat()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 463 | return *fContext.fFloat_Type; |
| 464 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 465 | if (type.isSigned()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 466 | return *fContext.fInt_Type; |
| 467 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 468 | if (type.isUnsigned()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 469 | return *fContext.fUInt_Type; |
| 470 | } |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 471 | if (type.isMatrix() || type.isVector()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 472 | if (type.componentType() == *fContext.fHalf_Type) { |
| 473 | return fContext.fFloat_Type->toCompound(fContext, type.columns(), type.rows()); |
| 474 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 475 | if (type.componentType() == *fContext.fShort_Type || |
| 476 | type.componentType() == *fContext.fByte_Type) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 477 | return fContext.fInt_Type->toCompound(fContext, type.columns(), type.rows()); |
| 478 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 479 | if (type.componentType() == *fContext.fUShort_Type || |
| 480 | type.componentType() == *fContext.fUByte_Type) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 481 | return fContext.fUInt_Type->toCompound(fContext, type.columns(), type.rows()); |
| 482 | } |
| 483 | } |
| 484 | return type; |
| 485 | } |
| 486 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 487 | SpvId SPIRVCodeGenerator::getType(const Type& type) { |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 488 | return this->getType(type, fDefaultLayout); |
| 489 | } |
| 490 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 491 | SpvId SPIRVCodeGenerator::getType(const Type& rawType, const MemoryLayout& layout) { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 492 | const Type& type = this->getActualType(rawType); |
Jim Van Verth | f3ec983 | 2020-10-21 16:09:57 -0400 | [diff] [blame] | 493 | String key = type.name(); |
John Stiles | c0c5106 | 2020-12-03 17:16:29 -0500 | [diff] [blame] | 494 | if (type.isStruct() || type.isArray()) { |
Jim Van Verth | f3ec983 | 2020-10-21 16:09:57 -0400 | [diff] [blame] | 495 | key += to_string((int)layout.fStd); |
| 496 | } |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 497 | auto entry = fTypeMap.find(key); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 498 | if (entry == fTypeMap.end()) { |
| 499 | SpvId result = this->nextId(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 500 | switch (type.typeKind()) { |
| 501 | case Type::TypeKind::kScalar: |
John Stiles | 4a7dc46 | 2020-11-25 11:08:08 -0500 | [diff] [blame] | 502 | if (type.isBoolean()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 503 | this->writeInstruction(SpvOpTypeBool, result, fConstantBuffer); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 504 | } else if (type == *fContext.fInt_Type || type == *fContext.fShort_Type || |
| 505 | type == *fContext.fIntLiteral_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 506 | this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstantBuffer); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 507 | } else if (type == *fContext.fUInt_Type || type == *fContext.fUShort_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 508 | this->writeInstruction(SpvOpTypeInt, result, 32, 0, fConstantBuffer); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 509 | } else if (type == *fContext.fFloat_Type || type == *fContext.fHalf_Type || |
| 510 | type == *fContext.fFloatLiteral_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 511 | this->writeInstruction(SpvOpTypeFloat, result, 32, fConstantBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 512 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 513 | SkASSERT(false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 514 | } |
| 515 | break; |
John Stiles | fd41d87 | 2020-11-25 22:39:45 -0500 | [diff] [blame] | 516 | case Type::TypeKind::kEnum: |
| 517 | this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstantBuffer); |
| 518 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 519 | case Type::TypeKind::kVector: |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 520 | this->writeInstruction(SpvOpTypeVector, result, |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 521 | this->getType(type.componentType(), layout), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 522 | type.columns(), fConstantBuffer); |
| 523 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 524 | case Type::TypeKind::kMatrix: |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 525 | this->writeInstruction(SpvOpTypeMatrix, result, |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 526 | this->getType(index_type(fContext, type), layout), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 527 | type.columns(), fConstantBuffer); |
| 528 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 529 | case Type::TypeKind::kStruct: |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 530 | this->writeStruct(type, layout, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 531 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 532 | case Type::TypeKind::kArray: { |
John Stiles | 21f5f45 | 2020-11-30 09:57:59 -0500 | [diff] [blame] | 533 | if (!MemoryLayout::LayoutIsSupported(type)) { |
| 534 | fErrors.error(type.fOffset, "type '" + type.name() + "' is not permitted here"); |
| 535 | return this->nextId(); |
| 536 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 537 | if (type.columns() > 0) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 538 | IntLiteral count(fContext, -1, type.columns()); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 539 | this->writeInstruction(SpvOpTypeArray, result, |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 540 | this->getType(type.componentType(), layout), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 541 | this->writeIntLiteral(count), fConstantBuffer); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 542 | this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 543 | (int32_t) layout.stride(type), |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 544 | fDecorationBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 545 | } else { |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 546 | // We shouldn't have any runtime-sized arrays right now |
| 547 | fErrors.error(type.fOffset, "runtime-sized arrays are not supported in SPIR-V"); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 548 | this->writeInstruction(SpvOpTypeRuntimeArray, result, |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 549 | this->getType(type.componentType(), layout), |
| 550 | fConstantBuffer); |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 551 | this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride, |
| 552 | (int32_t) layout.stride(type), |
| 553 | fDecorationBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 554 | } |
| 555 | break; |
| 556 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 557 | case Type::TypeKind::kSampler: { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 558 | SpvId image = result; |
| 559 | if (SpvDimSubpassData != type.dimensions()) { |
Stephen White | 792e230 | 2019-08-09 13:33:51 -0400 | [diff] [blame] | 560 | image = this->getType(type.textureType(), layout); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 561 | } |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 562 | if (SpvDimBuffer == type.dimensions()) { |
| 563 | fCapabilities |= (((uint64_t) 1) << SpvCapabilitySampledBuffer); |
| 564 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 565 | if (SpvDimSubpassData != type.dimensions()) { |
| 566 | this->writeInstruction(SpvOpTypeSampledImage, result, image, fConstantBuffer); |
| 567 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 568 | break; |
| 569 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 570 | case Type::TypeKind::kSeparateSampler: { |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 571 | this->writeInstruction(SpvOpTypeSampler, result, fConstantBuffer); |
| 572 | break; |
| 573 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 574 | case Type::TypeKind::kTexture: { |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 575 | this->writeInstruction(SpvOpTypeImage, result, |
| 576 | this->getType(*fContext.fFloat_Type, layout), |
John Stiles | c0c5106 | 2020-12-03 17:16:29 -0500 | [diff] [blame] | 577 | type.dimensions(), type.isDepth(), type.isArrayedTexture(), |
Stephen White | 792e230 | 2019-08-09 13:33:51 -0400 | [diff] [blame] | 578 | type.isMultisampled(), type.isSampled() ? 1 : 2, |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 579 | SpvImageFormatUnknown, fConstantBuffer); |
| 580 | fImageTypeMap[key] = result; |
| 581 | break; |
| 582 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 583 | default: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 584 | if (type == *fContext.fVoid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 585 | this->writeInstruction(SpvOpTypeVoid, result, fConstantBuffer); |
| 586 | } else { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 587 | #ifdef SK_DEBUG |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 588 | ABORT("invalid type: %s", type.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 589 | #endif |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 590 | } |
| 591 | } |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 592 | fTypeMap[key] = result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 593 | return result; |
| 594 | } |
| 595 | return entry->second; |
| 596 | } |
| 597 | |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 598 | SpvId SPIRVCodeGenerator::getImageType(const Type& type) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 599 | SkASSERT(type.typeKind() == Type::TypeKind::kSampler); |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 600 | this->getType(type); |
| 601 | String key = type.name() + to_string((int) fDefaultLayout.fStd); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 602 | SkASSERT(fImageTypeMap.find(key) != fImageTypeMap.end()); |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 603 | return fImageTypeMap[key]; |
| 604 | } |
| 605 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 606 | SpvId SPIRVCodeGenerator::getFunctionType(const FunctionDeclaration& function) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 607 | String key = to_string(this->getType(function.returnType())) + "("; |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 608 | String separator; |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 609 | const std::vector<const Variable*>& parameters = function.parameters(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 610 | for (size_t i = 0; i < parameters.size(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 611 | key += separator; |
| 612 | separator = ", "; |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 613 | key += to_string(this->getType(parameters[i]->type())); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 614 | } |
| 615 | key += ")"; |
| 616 | auto entry = fTypeMap.find(key); |
| 617 | if (entry == fTypeMap.end()) { |
| 618 | SpvId result = this->nextId(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 619 | int32_t length = 3 + (int32_t) parameters.size(); |
| 620 | SpvId returnType = this->getType(function.returnType()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 621 | std::vector<SpvId> parameterTypes; |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 622 | for (size_t i = 0; i < parameters.size(); i++) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 623 | // glslang seems to treat all function arguments as pointers whether they need to be or |
| 624 | // not. I was initially puzzled by this until I ran bizarre failures with certain |
| 625 | // patterns of function calls and control constructs, as exemplified by this minimal |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 626 | // failure case: |
| 627 | // |
| 628 | // void sphere(float x) { |
| 629 | // } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 630 | // |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 631 | // void map() { |
| 632 | // sphere(1.0); |
| 633 | // } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 634 | // |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 635 | // void main() { |
| 636 | // for (int i = 0; i < 1; i++) { |
| 637 | // map(); |
| 638 | // } |
| 639 | // } |
| 640 | // |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 641 | // As of this writing, compiling this in the "obvious" way (with sphere taking a float) |
| 642 | // crashes. Making it take a float* and storing the argument in a temporary variable, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 643 | // as glslang does, fixes it. It's entirely possible I simply missed whichever part of |
| 644 | // the spec makes this make sense. |
| 645 | // if (is_out(function->fParameters[i])) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 646 | parameterTypes.push_back(this->getPointerType(parameters[i]->type(), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 647 | SpvStorageClassFunction)); |
| 648 | // } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 649 | // parameterTypes.push_back(this->getType(function.fParameters[i]->fType)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 650 | // } |
| 651 | } |
| 652 | this->writeOpCode(SpvOpTypeFunction, length, fConstantBuffer); |
| 653 | this->writeWord(result, fConstantBuffer); |
| 654 | this->writeWord(returnType, fConstantBuffer); |
| 655 | for (SpvId id : parameterTypes) { |
| 656 | this->writeWord(id, fConstantBuffer); |
| 657 | } |
| 658 | fTypeMap[key] = result; |
| 659 | return result; |
| 660 | } |
| 661 | return entry->second; |
| 662 | } |
| 663 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 664 | SpvId SPIRVCodeGenerator::getPointerType(const Type& type, SpvStorageClass_ storageClass) { |
| 665 | return this->getPointerType(type, fDefaultLayout, storageClass); |
| 666 | } |
| 667 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 668 | SpvId SPIRVCodeGenerator::getPointerType(const Type& rawType, const MemoryLayout& layout, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 669 | SpvStorageClass_ storageClass) { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 670 | const Type& type = this->getActualType(rawType); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 671 | String key = type.displayName() + "*" + to_string(layout.fStd) + to_string(storageClass); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 672 | auto entry = fTypeMap.find(key); |
| 673 | if (entry == fTypeMap.end()) { |
| 674 | SpvId result = this->nextId(); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 675 | this->writeInstruction(SpvOpTypePointer, result, storageClass, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 676 | this->getType(type), fConstantBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 677 | fTypeMap[key] = result; |
| 678 | return result; |
| 679 | } |
| 680 | return entry->second; |
| 681 | } |
| 682 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 683 | SpvId SPIRVCodeGenerator::writeExpression(const Expression& expr, OutputStream& out) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 684 | switch (expr.kind()) { |
| 685 | case Expression::Kind::kBinary: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 686 | return this->writeBinaryExpression(expr.as<BinaryExpression>(), out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 687 | case Expression::Kind::kBoolLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 688 | return this->writeBoolLiteral(expr.as<BoolLiteral>()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 689 | case Expression::Kind::kConstructor: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 690 | return this->writeConstructor(expr.as<Constructor>(), out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 691 | case Expression::Kind::kIntLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 692 | return this->writeIntLiteral(expr.as<IntLiteral>()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 693 | case Expression::Kind::kFieldAccess: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 694 | return this->writeFieldAccess(expr.as<FieldAccess>(), out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 695 | case Expression::Kind::kFloatLiteral: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 696 | return this->writeFloatLiteral(expr.as<FloatLiteral>()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 697 | case Expression::Kind::kFunctionCall: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 698 | return this->writeFunctionCall(expr.as<FunctionCall>(), out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 699 | case Expression::Kind::kPrefix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 700 | return this->writePrefixExpression(expr.as<PrefixExpression>(), out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 701 | case Expression::Kind::kPostfix: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 702 | return this->writePostfixExpression(expr.as<PostfixExpression>(), out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 703 | case Expression::Kind::kSwizzle: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 704 | return this->writeSwizzle(expr.as<Swizzle>(), out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 705 | case Expression::Kind::kVariableReference: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 706 | return this->writeVariableReference(expr.as<VariableReference>(), out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 707 | case Expression::Kind::kTernary: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 708 | return this->writeTernaryExpression(expr.as<TernaryExpression>(), out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 709 | case Expression::Kind::kIndex: |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 710 | return this->writeIndexExpression(expr.as<IndexExpression>(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 711 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 712 | #ifdef SK_DEBUG |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 713 | ABORT("unsupported expression: %s", expr.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 714 | #endif |
| 715 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 716 | } |
| 717 | return -1; |
| 718 | } |
| 719 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 720 | SpvId SPIRVCodeGenerator::writeIntrinsicCall(const FunctionCall& c, OutputStream& out) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 721 | const FunctionDeclaration& function = c.function(); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 722 | const ExpressionArray& arguments = c.arguments(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 723 | auto intrinsic = fIntrinsicMap.find(function.name()); |
John Stiles | 93e661a | 2020-12-08 16:17:00 -0500 | [diff] [blame] | 724 | if (intrinsic == fIntrinsicMap.end()) { |
| 725 | fErrors.error(c.fOffset, "unsupported intrinsic '" + function.description() + "'"); |
| 726 | return -1; |
| 727 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 728 | int32_t intrinsicId; |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 729 | if (arguments.size() > 0) { |
| 730 | const Type& type = arguments[0]->type(); |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 731 | if (std::get<0>(intrinsic->second) == kSpecial_IntrinsicKind || is_float(fContext, type)) { |
| 732 | intrinsicId = std::get<1>(intrinsic->second); |
| 733 | } else if (is_signed(fContext, type)) { |
| 734 | intrinsicId = std::get<2>(intrinsic->second); |
| 735 | } else if (is_unsigned(fContext, type)) { |
| 736 | intrinsicId = std::get<3>(intrinsic->second); |
| 737 | } else if (is_bool(fContext, type)) { |
| 738 | intrinsicId = std::get<4>(intrinsic->second); |
| 739 | } else { |
| 740 | intrinsicId = std::get<1>(intrinsic->second); |
| 741 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 742 | } else { |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 743 | intrinsicId = std::get<1>(intrinsic->second); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 744 | } |
| 745 | switch (std::get<0>(intrinsic->second)) { |
| 746 | case kGLSL_STD_450_IntrinsicKind: { |
| 747 | SpvId result = this->nextId(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 748 | std::vector<SpvId> argumentIds; |
| 749 | for (size_t i = 0; i < arguments.size(); i++) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 750 | if (function.parameters()[i]->modifiers().fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 751 | argumentIds.push_back(this->getLValue(*arguments[i], out)->getPointer()); |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 752 | } else { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 753 | argumentIds.push_back(this->writeExpression(*arguments[i], out)); |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 754 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 755 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 756 | this->writeOpCode(SpvOpExtInst, 5 + (int32_t) argumentIds.size(), out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 757 | this->writeWord(this->getType(c.type()), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 758 | this->writeWord(result, out); |
| 759 | this->writeWord(fGLSLExtendedInstructions, out); |
| 760 | this->writeWord(intrinsicId, out); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 761 | for (SpvId id : argumentIds) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 762 | this->writeWord(id, out); |
| 763 | } |
| 764 | return result; |
| 765 | } |
| 766 | case kSPIRV_IntrinsicKind: { |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 767 | // GLSL supports dot(float, float), but SPIR-V does not. Convert it to FMul |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 768 | if (intrinsicId == SpvOpDot && arguments[0]->type().isScalar()) { |
Brian Osman | 46787d5 | 2020-11-24 14:18:23 -0500 | [diff] [blame] | 769 | intrinsicId = SpvOpFMul; |
| 770 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 771 | SpvId result = this->nextId(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 772 | std::vector<SpvId> argumentIds; |
| 773 | for (size_t i = 0; i < arguments.size(); i++) { |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 774 | if (function.parameters()[i]->modifiers().fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 775 | argumentIds.push_back(this->getLValue(*arguments[i], out)->getPointer()); |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 776 | } else { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 777 | argumentIds.push_back(this->writeExpression(*arguments[i], out)); |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 778 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 779 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 780 | if (c.type() != *fContext.fVoid_Type) { |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 781 | this->writeOpCode((SpvOp_) intrinsicId, 3 + (int32_t) arguments.size(), out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 782 | this->writeWord(this->getType(c.type()), out); |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 783 | this->writeWord(result, out); |
| 784 | } else { |
| 785 | this->writeOpCode((SpvOp_) intrinsicId, 1 + (int32_t) arguments.size(), out); |
| 786 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 787 | for (SpvId id : argumentIds) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 788 | this->writeWord(id, out); |
| 789 | } |
| 790 | return result; |
| 791 | } |
| 792 | case kSpecial_IntrinsicKind: |
| 793 | return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId, out); |
| 794 | default: |
John Stiles | 93e661a | 2020-12-08 16:17:00 -0500 | [diff] [blame] | 795 | fErrors.error(c.fOffset, "unsupported intrinsic '" + function.description() + "'"); |
| 796 | return -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 800 | std::vector<SpvId> SPIRVCodeGenerator::vectorize(const ExpressionArray& args, OutputStream& out) { |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 801 | int vectorSize = 0; |
| 802 | for (const auto& a : args) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 803 | if (a->type().isVector()) { |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 804 | if (vectorSize) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 805 | SkASSERT(a->type().columns() == vectorSize); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 806 | } |
| 807 | else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 808 | vectorSize = a->type().columns(); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | } |
| 812 | std::vector<SpvId> result; |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 813 | result.reserve(args.size()); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 814 | for (const auto& arg : args) { |
| 815 | const Type& argType = arg->type(); |
| 816 | SpvId raw = this->writeExpression(*arg, out); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 817 | if (vectorSize && argType.isScalar()) { |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 818 | SpvId vector = this->nextId(); |
| 819 | this->writeOpCode(SpvOpCompositeConstruct, 3 + vectorSize, out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 820 | this->writeWord(this->getType(argType.toCompound(fContext, vectorSize, 1)), out); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 821 | this->writeWord(vector, out); |
| 822 | for (int i = 0; i < vectorSize; i++) { |
| 823 | this->writeWord(raw, out); |
| 824 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 825 | this->writePrecisionModifier(argType, vector); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 826 | result.push_back(vector); |
| 827 | } else { |
| 828 | result.push_back(raw); |
| 829 | } |
| 830 | } |
| 831 | return result; |
| 832 | } |
| 833 | |
| 834 | void SPIRVCodeGenerator::writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst, |
| 835 | SpvId signedInst, SpvId unsignedInst, |
| 836 | const std::vector<SpvId>& args, |
| 837 | OutputStream& out) { |
| 838 | this->writeOpCode(SpvOpExtInst, 5 + args.size(), out); |
| 839 | this->writeWord(this->getType(type), out); |
| 840 | this->writeWord(id, out); |
| 841 | this->writeWord(fGLSLExtendedInstructions, out); |
| 842 | |
| 843 | if (is_float(fContext, type)) { |
| 844 | this->writeWord(floatInst, out); |
| 845 | } else if (is_signed(fContext, type)) { |
| 846 | this->writeWord(signedInst, out); |
| 847 | } else if (is_unsigned(fContext, type)) { |
| 848 | this->writeWord(unsignedInst, out); |
| 849 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 850 | SkASSERT(false); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 851 | } |
| 852 | for (SpvId a : args) { |
| 853 | this->writeWord(a, out); |
| 854 | } |
| 855 | } |
| 856 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 857 | SpvId SPIRVCodeGenerator::writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 858 | OutputStream& out) { |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 859 | const ExpressionArray& arguments = c.arguments(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 860 | SpvId result = this->nextId(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 861 | const Type& callType = c.type(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 862 | switch (kind) { |
| 863 | case kAtan_SpecialIntrinsic: { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 864 | std::vector<SpvId> argumentIds; |
| 865 | for (const std::unique_ptr<Expression>& arg : arguments) { |
| 866 | argumentIds.push_back(this->writeExpression(*arg, out)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 867 | } |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 868 | this->writeOpCode(SpvOpExtInst, 5 + (int32_t) argumentIds.size(), out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 869 | this->writeWord(this->getType(callType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 870 | this->writeWord(result, out); |
| 871 | this->writeWord(fGLSLExtendedInstructions, out); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 872 | this->writeWord(argumentIds.size() == 2 ? GLSLstd450Atan2 : GLSLstd450Atan, out); |
| 873 | for (SpvId id : argumentIds) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 874 | this->writeWord(id, out); |
| 875 | } |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 876 | break; |
| 877 | } |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 878 | case kSampledImage_SpecialIntrinsic: { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 879 | SkASSERT(arguments.size() == 2); |
| 880 | SpvId img = this->writeExpression(*arguments[0], out); |
| 881 | SpvId sampler = this->writeExpression(*arguments[1], out); |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 882 | this->writeInstruction(SpvOpSampledImage, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 883 | this->getType(callType), |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 884 | result, |
| 885 | img, |
| 886 | sampler, |
| 887 | out); |
| 888 | break; |
| 889 | } |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 890 | case kSubpassLoad_SpecialIntrinsic: { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 891 | SpvId img = this->writeExpression(*arguments[0], out); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 892 | ExpressionArray args; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 893 | args.reserve_back(2); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 894 | args.push_back(std::make_unique<IntLiteral>(fContext, /*offset=*/-1, /*value=*/0)); |
| 895 | args.push_back(std::make_unique<IntLiteral>(fContext, /*offset=*/-1, /*value=*/0)); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 896 | Constructor ctor(-1, fContext.fInt2_Type.get(), std::move(args)); |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 897 | SpvId coords = this->writeConstantVector(ctor); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 898 | if (arguments.size() == 1) { |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 899 | this->writeInstruction(SpvOpImageRead, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 900 | this->getType(callType), |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 901 | result, |
| 902 | img, |
| 903 | coords, |
| 904 | out); |
| 905 | } else { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 906 | SkASSERT(arguments.size() == 2); |
| 907 | SpvId sample = this->writeExpression(*arguments[1], out); |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 908 | this->writeInstruction(SpvOpImageRead, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 909 | this->getType(callType), |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 910 | result, |
| 911 | img, |
| 912 | coords, |
| 913 | SpvImageOperandsSampleMask, |
| 914 | sample, |
| 915 | out); |
| 916 | } |
| 917 | break; |
| 918 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 919 | case kTexture_SpecialIntrinsic: { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 920 | SpvOp_ op = SpvOpImageSampleImplicitLod; |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 921 | const Type& arg1Type = arguments[1]->type(); |
| 922 | switch (arguments[0]->type().dimensions()) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 923 | case SpvDim1D: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 924 | if (arg1Type == *fContext.fFloat2_Type) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 925 | op = SpvOpImageSampleProjImplicitLod; |
| 926 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 927 | SkASSERT(arg1Type == *fContext.fFloat_Type); |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 928 | } |
| 929 | break; |
| 930 | case SpvDim2D: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 931 | if (arg1Type == *fContext.fFloat3_Type) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 932 | op = SpvOpImageSampleProjImplicitLod; |
| 933 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 934 | SkASSERT(arg1Type == *fContext.fFloat2_Type); |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 935 | } |
| 936 | break; |
| 937 | case SpvDim3D: |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 938 | if (arg1Type == *fContext.fFloat4_Type) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 939 | op = SpvOpImageSampleProjImplicitLod; |
| 940 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 941 | SkASSERT(arg1Type == *fContext.fFloat3_Type); |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 942 | } |
| 943 | break; |
| 944 | case SpvDimCube: // fall through |
| 945 | case SpvDimRect: // fall through |
| 946 | case SpvDimBuffer: // fall through |
| 947 | case SpvDimSubpassData: |
| 948 | break; |
| 949 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 950 | SpvId type = this->getType(callType); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 951 | SpvId sampler = this->writeExpression(*arguments[0], out); |
| 952 | SpvId uv = this->writeExpression(*arguments[1], out); |
| 953 | if (arguments.size() == 3) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 954 | this->writeInstruction(op, type, result, sampler, uv, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 955 | SpvImageOperandsBiasMask, |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 956 | this->writeExpression(*arguments[2], out), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 957 | out); |
| 958 | } else { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 959 | SkASSERT(arguments.size() == 2); |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 960 | if (fProgram.fSettings.fSharpenTextures) { |
| 961 | FloatLiteral lodBias(fContext, -1, -0.5); |
| 962 | this->writeInstruction(op, type, result, sampler, uv, |
| 963 | SpvImageOperandsBiasMask, |
| 964 | this->writeFloatLiteral(lodBias), |
| 965 | out); |
| 966 | } else { |
| 967 | this->writeInstruction(op, type, result, sampler, uv, |
| 968 | out); |
| 969 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 970 | } |
| 971 | break; |
| 972 | } |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 973 | case kMod_SpecialIntrinsic: { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 974 | std::vector<SpvId> args = this->vectorize(arguments, out); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 975 | SkASSERT(args.size() == 2); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 976 | const Type& operandType = arguments[0]->type(); |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 977 | SpvOp_ op; |
| 978 | if (is_float(fContext, operandType)) { |
| 979 | op = SpvOpFMod; |
| 980 | } else if (is_signed(fContext, operandType)) { |
| 981 | op = SpvOpSMod; |
| 982 | } else if (is_unsigned(fContext, operandType)) { |
| 983 | op = SpvOpUMod; |
| 984 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 985 | SkASSERT(false); |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 986 | return 0; |
| 987 | } |
| 988 | this->writeOpCode(op, 5, out); |
| 989 | this->writeWord(this->getType(operandType), out); |
| 990 | this->writeWord(result, out); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 991 | this->writeWord(args[0], out); |
| 992 | this->writeWord(args[1], out); |
| 993 | break; |
| 994 | } |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 995 | case kDFdy_SpecialIntrinsic: { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 996 | SpvId fn = this->writeExpression(*arguments[0], out); |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 997 | this->writeOpCode(SpvOpDPdy, 4, out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 998 | this->writeWord(this->getType(callType), out); |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 999 | this->writeWord(result, out); |
| 1000 | this->writeWord(fn, out); |
| 1001 | if (fProgram.fSettings.fFlipY) { |
| 1002 | // Flipping Y also negates the Y derivatives. |
| 1003 | SpvId flipped = this->nextId(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1004 | this->writeInstruction(SpvOpFNegate, this->getType(callType), flipped, result, |
| 1005 | out); |
| 1006 | this->writePrecisionModifier(callType, flipped); |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 1007 | return flipped; |
| 1008 | } |
| 1009 | break; |
| 1010 | } |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 1011 | case kClamp_SpecialIntrinsic: { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1012 | std::vector<SpvId> args = this->vectorize(arguments, out); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1013 | SkASSERT(args.size() == 3); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1014 | this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FClamp, GLSLstd450SClamp, |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 1015 | GLSLstd450UClamp, args, out); |
| 1016 | break; |
| 1017 | } |
| 1018 | case kMax_SpecialIntrinsic: { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1019 | std::vector<SpvId> args = this->vectorize(arguments, out); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1020 | SkASSERT(args.size() == 2); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1021 | this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMax, GLSLstd450SMax, |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 1022 | GLSLstd450UMax, args, out); |
| 1023 | break; |
| 1024 | } |
| 1025 | case kMin_SpecialIntrinsic: { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1026 | std::vector<SpvId> args = this->vectorize(arguments, out); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1027 | SkASSERT(args.size() == 2); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1028 | this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMin, GLSLstd450SMin, |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 1029 | GLSLstd450UMin, args, out); |
| 1030 | break; |
| 1031 | } |
| 1032 | case kMix_SpecialIntrinsic: { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1033 | std::vector<SpvId> args = this->vectorize(arguments, out); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1034 | SkASSERT(args.size() == 3); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1035 | this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMix, SpvOpUndef, |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 1036 | SpvOpUndef, args, out); |
| 1037 | break; |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 1038 | } |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 1039 | case kSaturate_SpecialIntrinsic: { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1040 | SkASSERT(arguments.size() == 1); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1041 | ExpressionArray finalArgs; |
John Stiles | f4bda74 | 2020-10-14 16:57:41 -0400 | [diff] [blame] | 1042 | finalArgs.reserve_back(3); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1043 | finalArgs.push_back(arguments[0]->clone()); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1044 | finalArgs.push_back(std::make_unique<FloatLiteral>(fContext, /*offset=*/-1, |
| 1045 | /*value=*/0)); |
| 1046 | finalArgs.push_back(std::make_unique<FloatLiteral>(fContext, /*offset=*/-1, |
| 1047 | /*value=*/1)); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 1048 | std::vector<SpvId> spvArgs = this->vectorize(finalArgs, out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1049 | this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FClamp, GLSLstd450SClamp, |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 1050 | GLSLstd450UClamp, spvArgs, out); |
| 1051 | break; |
| 1052 | } |
Brian Osman | 6ba3be1 | 2020-11-13 16:32:52 -0500 | [diff] [blame] | 1053 | case kSmoothStep_SpecialIntrinsic: { |
| 1054 | std::vector<SpvId> args = this->vectorize(arguments, out); |
| 1055 | SkASSERT(args.size() == 3); |
| 1056 | this->writeGLSLExtendedInstruction(callType, result, GLSLstd450SmoothStep, SpvOpUndef, |
| 1057 | SpvOpUndef, args, out); |
| 1058 | break; |
| 1059 | } |
| 1060 | case kStep_SpecialIntrinsic: { |
| 1061 | std::vector<SpvId> args = this->vectorize(arguments, out); |
| 1062 | SkASSERT(args.size() == 2); |
| 1063 | this->writeGLSLExtendedInstruction(callType, result, GLSLstd450Step, SpvOpUndef, |
| 1064 | SpvOpUndef, args, out); |
| 1065 | break; |
| 1066 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1067 | } |
| 1068 | return result; |
| 1069 | } |
| 1070 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1071 | SpvId SPIRVCodeGenerator::writeFunctionCall(const FunctionCall& c, OutputStream& out) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1072 | const FunctionDeclaration& function = c.function(); |
John Stiles | 8e3b6be | 2020-10-13 11:14:08 -0400 | [diff] [blame] | 1073 | const ExpressionArray& arguments = c.arguments(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1074 | const auto& entry = fFunctionMap.find(&function); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1075 | if (entry == fFunctionMap.end()) { |
| 1076 | return this->writeIntrinsicCall(c, out); |
| 1077 | } |
| 1078 | // stores (variable, type, lvalue) pairs to extract and save after the function call is complete |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1079 | std::vector<std::tuple<SpvId, const Type*, std::unique_ptr<LValue>>> lvalues; |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1080 | std::vector<SpvId> argumentIds; |
| 1081 | for (size_t i = 0; i < arguments.size(); i++) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1082 | // id of temporary variable that we will use to hold this argument, or 0 if it is being |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1083 | // passed directly |
| 1084 | SpvId tmpVar; |
| 1085 | // if we need a temporary var to store this argument, this is the value to store in the var |
| 1086 | SpvId tmpValueId; |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 1087 | if (is_out(*function.parameters()[i])) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1088 | std::unique_ptr<LValue> lv = this->getLValue(*arguments[i], out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1089 | SpvId ptr = lv->getPointer(); |
| 1090 | if (ptr) { |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1091 | argumentIds.push_back(ptr); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1092 | continue; |
| 1093 | } else { |
| 1094 | // lvalue cannot simply be read and written via a pointer (e.g. a swizzle). Need to |
| 1095 | // copy it into a temp, call the function, read the value out of the temp, and then |
| 1096 | // update the lvalue. |
| 1097 | tmpValueId = lv->load(out); |
| 1098 | tmpVar = this->nextId(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1099 | lvalues.push_back(std::make_tuple(tmpVar, &arguments[i]->type(), std::move(lv))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1100 | } |
| 1101 | } else { |
| 1102 | // see getFunctionType for an explanation of why we're always using pointer parameters |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1103 | tmpValueId = this->writeExpression(*arguments[i], out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1104 | tmpVar = this->nextId(); |
| 1105 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1106 | this->writeInstruction(SpvOpVariable, |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1107 | this->getPointerType(arguments[i]->type(), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1108 | SpvStorageClassFunction), |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1109 | tmpVar, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1110 | SpvStorageClassFunction, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1111 | fVariableBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1112 | this->writeInstruction(SpvOpStore, tmpVar, tmpValueId, out); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1113 | argumentIds.push_back(tmpVar); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1114 | } |
| 1115 | SpvId result = this->nextId(); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1116 | this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t) arguments.size(), out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1117 | this->writeWord(this->getType(c.type()), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1118 | this->writeWord(result, out); |
| 1119 | this->writeWord(entry->second, out); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 1120 | for (SpvId id : argumentIds) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1121 | this->writeWord(id, out); |
| 1122 | } |
| 1123 | // now that the call is complete, we may need to update some lvalues with the new values of out |
| 1124 | // arguments |
| 1125 | for (const auto& tuple : lvalues) { |
| 1126 | SpvId load = this->nextId(); |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1127 | this->writeInstruction(SpvOpLoad, getType(*std::get<1>(tuple)), load, std::get<0>(tuple), |
| 1128 | out); |
| 1129 | this->writePrecisionModifier(*std::get<1>(tuple), load); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1130 | std::get<2>(tuple)->store(load, out); |
| 1131 | } |
| 1132 | return result; |
| 1133 | } |
| 1134 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1135 | SpvId SPIRVCodeGenerator::writeConstantVector(const Constructor& c) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1136 | const Type& type = c.type(); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1137 | SkASSERT(type.isVector() && c.isCompileTimeConstant()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1138 | SpvId result = this->nextId(); |
| 1139 | std::vector<SpvId> arguments; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1140 | for (const std::unique_ptr<Expression>& arg : c.arguments()) { |
| 1141 | arguments.push_back(this->writeExpression(*arg, fConstantBuffer)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1142 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1143 | SpvId typeId = this->getType(type); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1144 | if (c.arguments().size() == 1) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1145 | // with a single argument, a vector will have all of its entries equal to the argument |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1146 | this->writeOpCode(SpvOpConstantComposite, 3 + type.columns(), fConstantBuffer); |
| 1147 | this->writeWord(typeId, fConstantBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1148 | this->writeWord(result, fConstantBuffer); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1149 | for (int i = 0; i < type.columns(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1150 | this->writeWord(arguments[0], fConstantBuffer); |
| 1151 | } |
| 1152 | } else { |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1153 | this->writeOpCode(SpvOpConstantComposite, 3 + (int32_t) c.arguments().size(), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1154 | fConstantBuffer); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1155 | this->writeWord(typeId, fConstantBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1156 | this->writeWord(result, fConstantBuffer); |
| 1157 | for (SpvId id : arguments) { |
| 1158 | this->writeWord(id, fConstantBuffer); |
| 1159 | } |
| 1160 | } |
| 1161 | return result; |
| 1162 | } |
| 1163 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1164 | SpvId SPIRVCodeGenerator::writeFloatConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1165 | const Type& constructorType = c.type(); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1166 | SkASSERT(c.arguments().size() == 1); |
| 1167 | const Type& argType = c.arguments()[0]->type(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1168 | SkASSERT(constructorType.isFloat()); |
| 1169 | SkASSERT(argType.isNumber()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1170 | SpvId result = this->nextId(); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1171 | SpvId parameter = this->writeExpression(*c.arguments()[0], out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1172 | if (argType.isSigned()) { |
| 1173 | this->writeInstruction(SpvOpConvertSToF, this->getType(constructorType), result, parameter, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1174 | out); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1175 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1176 | SkASSERT(argType.isUnsigned()); |
| 1177 | this->writeInstruction(SpvOpConvertUToF, this->getType(constructorType), result, parameter, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1178 | out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1179 | } |
| 1180 | return result; |
| 1181 | } |
| 1182 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1183 | SpvId SPIRVCodeGenerator::writeIntConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1184 | const Type& constructorType = c.type(); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1185 | SkASSERT(c.arguments().size() == 1); |
| 1186 | const Type& argType = c.arguments()[0]->type(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1187 | SkASSERT(constructorType.isSigned()); |
| 1188 | SkASSERT(argType.isNumber()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1189 | SpvId result = this->nextId(); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1190 | SpvId parameter = this->writeExpression(*c.arguments()[0], out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1191 | if (argType.isFloat()) { |
| 1192 | this->writeInstruction(SpvOpConvertFToS, this->getType(constructorType), result, parameter, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1193 | out); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1194 | } |
| 1195 | else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1196 | SkASSERT(argType.isUnsigned()); |
| 1197 | this->writeInstruction(SpvOpBitcast, this->getType(constructorType), result, parameter, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1198 | out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1199 | } |
| 1200 | return result; |
| 1201 | } |
| 1202 | |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1203 | SpvId SPIRVCodeGenerator::writeUIntConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1204 | const Type& constructorType = c.type(); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1205 | SkASSERT(c.arguments().size() == 1); |
| 1206 | const Type& argType = c.arguments()[0]->type(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1207 | SkASSERT(constructorType.isUnsigned()); |
| 1208 | SkASSERT(argType.isNumber()); |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1209 | SpvId result = this->nextId(); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1210 | SpvId parameter = this->writeExpression(*c.arguments()[0], out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1211 | if (argType.isFloat()) { |
| 1212 | this->writeInstruction(SpvOpConvertFToU, this->getType(constructorType), result, parameter, |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1213 | out); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1214 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1215 | SkASSERT(argType.isSigned()); |
| 1216 | this->writeInstruction(SpvOpBitcast, this->getType(constructorType), result, parameter, |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1217 | out); |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1218 | } |
| 1219 | return result; |
| 1220 | } |
| 1221 | |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1222 | void SPIRVCodeGenerator::writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1223 | OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1224 | FloatLiteral zero(fContext, -1, 0); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1225 | SpvId zeroId = this->writeFloatLiteral(zero); |
| 1226 | std::vector<SpvId> columnIds; |
| 1227 | for (int column = 0; column < type.columns(); column++) { |
| 1228 | this->writeOpCode(SpvOpCompositeConstruct, 3 + type.rows(), |
| 1229 | out); |
| 1230 | this->writeWord(this->getType(type.componentType().toCompound(fContext, type.rows(), 1)), |
| 1231 | out); |
| 1232 | SpvId columnId = this->nextId(); |
| 1233 | this->writeWord(columnId, out); |
| 1234 | columnIds.push_back(columnId); |
| 1235 | for (int row = 0; row < type.columns(); row++) { |
| 1236 | this->writeWord(row == column ? diagonal : zeroId, out); |
| 1237 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1238 | this->writePrecisionModifier(type, columnId); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1239 | } |
| 1240 | this->writeOpCode(SpvOpCompositeConstruct, 3 + type.columns(), |
| 1241 | out); |
| 1242 | this->writeWord(this->getType(type), out); |
| 1243 | this->writeWord(id, out); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 1244 | for (SpvId columnId : columnIds) { |
| 1245 | this->writeWord(columnId, out); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1246 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1247 | this->writePrecisionModifier(type, id); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | void SPIRVCodeGenerator::writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1251 | const Type& dstType, OutputStream& out) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1252 | SkASSERT(srcType.isMatrix()); |
| 1253 | SkASSERT(dstType.isMatrix()); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1254 | SkASSERT(srcType.componentType() == dstType.componentType()); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1255 | SpvId srcColumnType = this->getType(srcType.componentType().toCompound(fContext, |
| 1256 | srcType.rows(), |
| 1257 | 1)); |
| 1258 | SpvId dstColumnType = this->getType(dstType.componentType().toCompound(fContext, |
| 1259 | dstType.rows(), |
| 1260 | 1)); |
| 1261 | SpvId zeroId; |
| 1262 | if (dstType.componentType() == *fContext.fFloat_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1263 | FloatLiteral zero(fContext, -1, 0.0); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1264 | zeroId = this->writeFloatLiteral(zero); |
| 1265 | } else if (dstType.componentType() == *fContext.fInt_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1266 | IntLiteral zero(fContext, -1, 0); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1267 | zeroId = this->writeIntLiteral(zero); |
| 1268 | } else { |
| 1269 | ABORT("unsupported matrix component type"); |
| 1270 | } |
| 1271 | SpvId zeroColumn = 0; |
| 1272 | SpvId columns[4]; |
| 1273 | for (int i = 0; i < dstType.columns(); i++) { |
| 1274 | if (i < srcType.columns()) { |
| 1275 | // we're still inside the src matrix, copy the column |
| 1276 | SpvId srcColumn = this->nextId(); |
| 1277 | this->writeInstruction(SpvOpCompositeExtract, srcColumnType, srcColumn, src, i, out); |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1278 | this->writePrecisionModifier(dstType, srcColumn); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1279 | SpvId dstColumn; |
| 1280 | if (srcType.rows() == dstType.rows()) { |
| 1281 | // columns are equal size, don't need to do anything |
| 1282 | dstColumn = srcColumn; |
| 1283 | } |
| 1284 | else if (dstType.rows() > srcType.rows()) { |
| 1285 | // dst column is bigger, need to zero-pad it |
| 1286 | dstColumn = this->nextId(); |
| 1287 | int delta = dstType.rows() - srcType.rows(); |
| 1288 | this->writeOpCode(SpvOpCompositeConstruct, 4 + delta, out); |
| 1289 | this->writeWord(dstColumnType, out); |
| 1290 | this->writeWord(dstColumn, out); |
| 1291 | this->writeWord(srcColumn, out); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 1292 | for (int j = 0; j < delta; ++j) { |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1293 | this->writeWord(zeroId, out); |
| 1294 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1295 | this->writePrecisionModifier(dstType, dstColumn); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1296 | } |
| 1297 | else { |
| 1298 | // dst column is smaller, need to swizzle the src column |
| 1299 | dstColumn = this->nextId(); |
| 1300 | int count = dstType.rows(); |
| 1301 | this->writeOpCode(SpvOpVectorShuffle, 5 + count, out); |
| 1302 | this->writeWord(dstColumnType, out); |
| 1303 | this->writeWord(dstColumn, out); |
| 1304 | this->writeWord(srcColumn, out); |
| 1305 | this->writeWord(srcColumn, out); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 1306 | for (int j = 0; j < count; j++) { |
| 1307 | this->writeWord(j, out); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1308 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1309 | this->writePrecisionModifier(dstType, dstColumn); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1310 | } |
| 1311 | columns[i] = dstColumn; |
| 1312 | } else { |
| 1313 | // we're past the end of the src matrix, need a vector of zeroes |
| 1314 | if (!zeroColumn) { |
| 1315 | zeroColumn = this->nextId(); |
| 1316 | this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.rows(), out); |
| 1317 | this->writeWord(dstColumnType, out); |
| 1318 | this->writeWord(zeroColumn, out); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 1319 | for (int j = 0; j < dstType.rows(); ++j) { |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1320 | this->writeWord(zeroId, out); |
| 1321 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1322 | this->writePrecisionModifier(dstType, zeroColumn); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1323 | } |
| 1324 | columns[i] = zeroColumn; |
| 1325 | } |
| 1326 | } |
| 1327 | this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.columns(), out); |
| 1328 | this->writeWord(this->getType(dstType), out); |
| 1329 | this->writeWord(id, out); |
| 1330 | for (int i = 0; i < dstType.columns(); i++) { |
| 1331 | this->writeWord(columns[i], out); |
| 1332 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1333 | this->writePrecisionModifier(dstType, id); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1334 | } |
| 1335 | |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1336 | void SPIRVCodeGenerator::addColumnEntry(SpvId columnType, Precision precision, |
| 1337 | std::vector<SpvId>* currentColumn, |
Ethan Nicholas | 5c46b72 | 2019-03-22 14:32:37 -0400 | [diff] [blame] | 1338 | std::vector<SpvId>* columnIds, |
| 1339 | int* currentCount, int rows, SpvId entry, |
| 1340 | OutputStream& out) { |
| 1341 | SkASSERT(*currentCount < rows); |
| 1342 | ++(*currentCount); |
| 1343 | currentColumn->push_back(entry); |
| 1344 | if (*currentCount == rows) { |
| 1345 | *currentCount = 0; |
| 1346 | this->writeOpCode(SpvOpCompositeConstruct, 3 + currentColumn->size(), out); |
| 1347 | this->writeWord(columnType, out); |
| 1348 | SpvId columnId = this->nextId(); |
| 1349 | this->writeWord(columnId, out); |
| 1350 | columnIds->push_back(columnId); |
| 1351 | for (SpvId id : *currentColumn) { |
| 1352 | this->writeWord(id, out); |
| 1353 | } |
| 1354 | currentColumn->clear(); |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1355 | this->writePrecisionModifier(precision, columnId); |
Ethan Nicholas | 5c46b72 | 2019-03-22 14:32:37 -0400 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1359 | SpvId SPIRVCodeGenerator::writeMatrixConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1360 | const Type& type = c.type(); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1361 | SkASSERT(type.isMatrix()); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1362 | SkASSERT(c.arguments().size() > 0); |
| 1363 | const Type& arg0Type = c.arguments()[0]->type(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1364 | // go ahead and write the arguments so we don't try to write new instructions in the middle of |
| 1365 | // an instruction |
| 1366 | std::vector<SpvId> arguments; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1367 | for (size_t i = 0; i < c.arguments().size(); i++) { |
| 1368 | arguments.push_back(this->writeExpression(*c.arguments()[i], out)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1369 | } |
| 1370 | SpvId result = this->nextId(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1371 | int rows = type.rows(); |
| 1372 | int columns = type.columns(); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1373 | if (arguments.size() == 1 && arg0Type.isScalar()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1374 | this->writeUniformScaleMatrix(result, arguments[0], type, out); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1375 | } else if (arguments.size() == 1 && arg0Type.isMatrix()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1376 | this->writeMatrixCopy(result, arguments[0], arg0Type, type, out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1377 | } else if (arguments.size() == 1 && |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1378 | arg0Type.isVector()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1379 | SkASSERT(type.rows() == 2 && type.columns() == 2); |
| 1380 | SkASSERT(arg0Type.columns() == 4); |
| 1381 | SpvId componentType = this->getType(type.componentType()); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1382 | SpvId v[4]; |
| 1383 | for (int i = 0; i < 4; ++i) { |
| 1384 | v[i] = this->nextId(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1385 | this->writeInstruction(SpvOpCompositeExtract, componentType, v[i], arguments[0], i, |
| 1386 | out); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1387 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1388 | SpvId columnType = this->getType(type.componentType().toCompound(fContext, 2, 1)); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1389 | SpvId column1 = this->nextId(); |
| 1390 | this->writeInstruction(SpvOpCompositeConstruct, columnType, column1, v[0], v[1], out); |
| 1391 | SpvId column2 = this->nextId(); |
| 1392 | this->writeInstruction(SpvOpCompositeConstruct, columnType, column2, v[2], v[3], out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1393 | this->writeInstruction(SpvOpCompositeConstruct, this->getType(type), result, column1, |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1394 | column2, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1395 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1396 | SpvId columnType = this->getType(type.componentType().toCompound(fContext, rows, 1)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1397 | std::vector<SpvId> columnIds; |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1398 | // ids of vectors and scalars we have written to the current column so far |
| 1399 | std::vector<SpvId> currentColumn; |
| 1400 | // the total number of scalars represented by currentColumn's entries |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1401 | int currentCount = 0; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1402 | Precision precision = type.highPrecision() ? Precision::kHigh : Precision::kLow; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1403 | for (size_t i = 0; i < arguments.size(); i++) { |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1404 | const Type& argType = c.arguments()[i]->type(); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1405 | if (currentCount == 0 && argType.isVector() && |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1406 | argType.columns() == type.rows()) { |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1407 | // this is a complete column by itself |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1408 | columnIds.push_back(arguments[i]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1409 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1410 | if (argType.columns() == 1) { |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1411 | this->addColumnEntry(columnType, precision, ¤tColumn, &columnIds, |
| 1412 | ¤tCount, rows, arguments[i], out); |
Ethan Nicholas | be850ad | 2018-04-27 10:36:31 -0400 | [diff] [blame] | 1413 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1414 | SpvId componentType = this->getType(argType.componentType()); |
| 1415 | for (int j = 0; j < argType.columns(); ++j) { |
Ethan Nicholas | be850ad | 2018-04-27 10:36:31 -0400 | [diff] [blame] | 1416 | SpvId swizzle = this->nextId(); |
| 1417 | this->writeInstruction(SpvOpCompositeExtract, componentType, swizzle, |
| 1418 | arguments[i], j, out); |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1419 | this->addColumnEntry(columnType, precision, ¤tColumn, &columnIds, |
| 1420 | ¤tCount, rows, swizzle, out); |
Ethan Nicholas | be850ad | 2018-04-27 10:36:31 -0400 | [diff] [blame] | 1421 | } |
| 1422 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1423 | } |
| 1424 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1425 | SkASSERT(columnIds.size() == (size_t) columns); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1426 | this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1427 | this->writeWord(this->getType(type), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1428 | this->writeWord(result, out); |
| 1429 | for (SpvId id : columnIds) { |
| 1430 | this->writeWord(id, out); |
| 1431 | } |
| 1432 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1433 | this->writePrecisionModifier(type, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1434 | return result; |
| 1435 | } |
| 1436 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1437 | SpvId SPIRVCodeGenerator::writeVectorConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1438 | const Type& type = c.type(); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1439 | SkASSERT(type.isVector()); |
Brian Osman | b6b9573 | 2020-06-30 11:44:27 -0400 | [diff] [blame] | 1440 | if (c.isCompileTimeConstant()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1441 | return this->writeConstantVector(c); |
| 1442 | } |
| 1443 | // go ahead and write the arguments so we don't try to write new instructions in the middle of |
| 1444 | // an instruction |
| 1445 | std::vector<SpvId> arguments; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1446 | for (size_t i = 0; i < c.arguments().size(); i++) { |
| 1447 | const Type& argType = c.arguments()[i]->type(); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1448 | if (argType.isVector()) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1449 | // SPIR-V doesn't support vector(vector-of-different-type) directly, so we need to |
| 1450 | // extract the components and convert them in that case manually. On top of that, |
| 1451 | // as of this writing there's a bug in the Intel Vulkan driver where OpCreateComposite |
| 1452 | // doesn't handle vector arguments at all, so we always extract vector components and |
| 1453 | // pass them into OpCreateComposite individually. |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1454 | SpvId vec = this->writeExpression(*c.arguments()[i], out); |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1455 | SpvOp_ op = SpvOpUndef; |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1456 | const Type& src = argType.componentType(); |
| 1457 | const Type& dst = type.componentType(); |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1458 | if (dst == *fContext.fFloat_Type || dst == *fContext.fHalf_Type) { |
| 1459 | if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) { |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1460 | if (c.arguments().size() == 1) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1461 | return vec; |
| 1462 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1463 | } else if (src == *fContext.fInt_Type || |
| 1464 | src == *fContext.fShort_Type || |
| 1465 | src == *fContext.fByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1466 | op = SpvOpConvertSToF; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1467 | } else if (src == *fContext.fUInt_Type || |
| 1468 | src == *fContext.fUShort_Type || |
| 1469 | src == *fContext.fUByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1470 | op = SpvOpConvertUToF; |
| 1471 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1472 | SkASSERT(false); |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1473 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1474 | } else if (dst == *fContext.fInt_Type || |
| 1475 | dst == *fContext.fShort_Type || |
| 1476 | dst == *fContext.fByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1477 | if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) { |
| 1478 | op = SpvOpConvertFToS; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1479 | } else if (src == *fContext.fInt_Type || |
| 1480 | src == *fContext.fShort_Type || |
| 1481 | src == *fContext.fByte_Type) { |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1482 | if (c.arguments().size() == 1) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1483 | return vec; |
| 1484 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1485 | } else if (src == *fContext.fUInt_Type || |
| 1486 | src == *fContext.fUShort_Type || |
| 1487 | src == *fContext.fUByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1488 | op = SpvOpBitcast; |
| 1489 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1490 | SkASSERT(false); |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1491 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1492 | } else if (dst == *fContext.fUInt_Type || |
| 1493 | dst == *fContext.fUShort_Type || |
| 1494 | dst == *fContext.fUByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1495 | if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) { |
| 1496 | op = SpvOpConvertFToS; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1497 | } else if (src == *fContext.fInt_Type || |
| 1498 | src == *fContext.fShort_Type || |
| 1499 | src == *fContext.fByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1500 | op = SpvOpBitcast; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1501 | } else if (src == *fContext.fUInt_Type || |
| 1502 | src == *fContext.fUShort_Type || |
| 1503 | src == *fContext.fUByte_Type) { |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1504 | if (c.arguments().size() == 1) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1505 | return vec; |
| 1506 | } |
| 1507 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1508 | SkASSERT(false); |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1509 | } |
Ethan Nicholas | 26a8d90 | 2018-01-29 09:25:51 -0500 | [diff] [blame] | 1510 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1511 | for (int j = 0; j < argType.columns(); j++) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1512 | SpvId swizzle = this->nextId(); |
| 1513 | this->writeInstruction(SpvOpCompositeExtract, this->getType(src), swizzle, vec, j, |
| 1514 | out); |
| 1515 | if (op != SpvOpUndef) { |
| 1516 | SpvId cast = this->nextId(); |
| 1517 | this->writeInstruction(op, this->getType(dst), cast, swizzle, out); |
| 1518 | arguments.push_back(cast); |
| 1519 | } else { |
| 1520 | arguments.push_back(swizzle); |
| 1521 | } |
Ethan Nicholas | 26a8d90 | 2018-01-29 09:25:51 -0500 | [diff] [blame] | 1522 | } |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1523 | } else { |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1524 | arguments.push_back(this->writeExpression(*c.arguments()[i], out)); |
Ethan Nicholas | 26a8d90 | 2018-01-29 09:25:51 -0500 | [diff] [blame] | 1525 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1526 | } |
| 1527 | SpvId result = this->nextId(); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1528 | if (arguments.size() == 1 && c.arguments()[0]->type().isScalar()) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1529 | this->writeOpCode(SpvOpCompositeConstruct, 3 + type.columns(), out); |
| 1530 | this->writeWord(this->getType(type), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1531 | this->writeWord(result, out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1532 | for (int i = 0; i < type.columns(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1533 | this->writeWord(arguments[0], out); |
| 1534 | } |
| 1535 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1536 | SkASSERT(arguments.size() > 1); |
Ethan Nicholas | 26a8d90 | 2018-01-29 09:25:51 -0500 | [diff] [blame] | 1537 | this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) arguments.size(), out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1538 | this->writeWord(this->getType(type), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1539 | this->writeWord(result, out); |
| 1540 | for (SpvId id : arguments) { |
| 1541 | this->writeWord(id, out); |
| 1542 | } |
| 1543 | } |
| 1544 | return result; |
| 1545 | } |
| 1546 | |
Ethan Nicholas | bd55322 | 2017-07-18 15:54:59 -0400 | [diff] [blame] | 1547 | SpvId SPIRVCodeGenerator::writeArrayConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1548 | const Type& type = c.type(); |
John Stiles | c0c5106 | 2020-12-03 17:16:29 -0500 | [diff] [blame] | 1549 | SkASSERT(type.isArray()); |
Ethan Nicholas | bd55322 | 2017-07-18 15:54:59 -0400 | [diff] [blame] | 1550 | // go ahead and write the arguments so we don't try to write new instructions in the middle of |
| 1551 | // an instruction |
| 1552 | std::vector<SpvId> arguments; |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1553 | for (size_t i = 0; i < c.arguments().size(); i++) { |
| 1554 | arguments.push_back(this->writeExpression(*c.arguments()[i], out)); |
Ethan Nicholas | bd55322 | 2017-07-18 15:54:59 -0400 | [diff] [blame] | 1555 | } |
| 1556 | SpvId result = this->nextId(); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1557 | this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) c.arguments().size(), out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1558 | this->writeWord(this->getType(type), out); |
Ethan Nicholas | bd55322 | 2017-07-18 15:54:59 -0400 | [diff] [blame] | 1559 | this->writeWord(result, out); |
| 1560 | for (SpvId id : arguments) { |
| 1561 | this->writeWord(id, out); |
| 1562 | } |
| 1563 | return result; |
| 1564 | } |
| 1565 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1566 | SpvId SPIRVCodeGenerator::writeConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1567 | const Type& type = c.type(); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 1568 | if (c.arguments().size() == 1 && |
| 1569 | this->getActualType(type) == this->getActualType(c.arguments()[0]->type())) { |
| 1570 | return this->writeExpression(*c.arguments()[0], out); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1571 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1572 | if (type == *fContext.fFloat_Type || type == *fContext.fHalf_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1573 | return this->writeFloatConstructor(c, out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1574 | } else if (type == *fContext.fInt_Type || |
| 1575 | type == *fContext.fShort_Type || |
| 1576 | type == *fContext.fByte_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1577 | return this->writeIntConstructor(c, out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1578 | } else if (type == *fContext.fUInt_Type || |
| 1579 | type == *fContext.fUShort_Type || |
| 1580 | type == *fContext.fUByte_Type) { |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1581 | return this->writeUIntConstructor(c, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1582 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1583 | switch (type.typeKind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1584 | case Type::TypeKind::kVector: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1585 | return this->writeVectorConstructor(c, out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1586 | case Type::TypeKind::kMatrix: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1587 | return this->writeMatrixConstructor(c, out); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1588 | case Type::TypeKind::kArray: |
Ethan Nicholas | bd55322 | 2017-07-18 15:54:59 -0400 | [diff] [blame] | 1589 | return this->writeArrayConstructor(c, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1590 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1591 | #ifdef SK_DEBUG |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1592 | ABORT("unsupported constructor: %s", c.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 1593 | #endif |
| 1594 | return -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | SpvStorageClass_ get_storage_class(const Modifiers& modifiers) { |
| 1599 | if (modifiers.fFlags & Modifiers::kIn_Flag) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1600 | SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1601 | return SpvStorageClassInput; |
| 1602 | } else if (modifiers.fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1603 | SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1604 | return SpvStorageClassOutput; |
| 1605 | } else if (modifiers.fFlags & Modifiers::kUniform_Flag) { |
Ethan Nicholas | 39204fd | 2017-11-27 13:12:30 -0500 | [diff] [blame] | 1606 | if (modifiers.fLayout.fFlags & Layout::kPushConstant_Flag) { |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 1607 | return SpvStorageClassPushConstant; |
| 1608 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1609 | return SpvStorageClassUniform; |
| 1610 | } else { |
| 1611 | return SpvStorageClassFunction; |
| 1612 | } |
| 1613 | } |
| 1614 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1615 | SpvStorageClass_ get_storage_class(const Expression& expr) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1616 | switch (expr.kind()) { |
| 1617 | case Expression::Kind::kVariableReference: { |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1618 | const Variable& var = *expr.as<VariableReference>().variable(); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 1619 | if (var.storage() != Variable::Storage::kGlobal) { |
Ethan Nicholas | c6f5e10 | 2017-03-31 14:53:17 -0400 | [diff] [blame] | 1620 | return SpvStorageClassFunction; |
| 1621 | } |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1622 | SpvStorageClass_ result = get_storage_class(var.modifiers()); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1623 | if (result == SpvStorageClassFunction) { |
| 1624 | result = SpvStorageClassPrivate; |
| 1625 | } |
| 1626 | return result; |
Ethan Nicholas | c6f5e10 | 2017-03-31 14:53:17 -0400 | [diff] [blame] | 1627 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1628 | case Expression::Kind::kFieldAccess: |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 1629 | return get_storage_class(*expr.as<FieldAccess>().base()); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1630 | case Expression::Kind::kIndex: |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1631 | return get_storage_class(*expr.as<IndexExpression>().base()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1632 | default: |
| 1633 | return SpvStorageClassFunction; |
| 1634 | } |
| 1635 | } |
| 1636 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1637 | std::vector<SpvId> SPIRVCodeGenerator::getAccessChain(const Expression& expr, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1638 | std::vector<SpvId> chain; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1639 | switch (expr.kind()) { |
| 1640 | case Expression::Kind::kIndex: { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1641 | IndexExpression& indexExpr = (IndexExpression&) expr; |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1642 | chain = this->getAccessChain(*indexExpr.base(), out); |
| 1643 | chain.push_back(this->writeExpression(*indexExpr.index(), out)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1644 | break; |
| 1645 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1646 | case Expression::Kind::kFieldAccess: { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1647 | FieldAccess& fieldExpr = (FieldAccess&) expr; |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 1648 | chain = this->getAccessChain(*fieldExpr.base(), out); |
| 1649 | IntLiteral index(fContext, -1, fieldExpr.fieldIndex()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1650 | chain.push_back(this->writeIntLiteral(index)); |
| 1651 | break; |
| 1652 | } |
Ethan Nicholas | a9a0690 | 2019-01-07 14:42:40 -0500 | [diff] [blame] | 1653 | default: { |
| 1654 | SpvId id = this->getLValue(expr, out)->getPointer(); |
| 1655 | SkASSERT(id != 0); |
| 1656 | chain.push_back(id); |
| 1657 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1658 | } |
| 1659 | return chain; |
| 1660 | } |
| 1661 | |
| 1662 | class PointerLValue : public SPIRVCodeGenerator::LValue { |
| 1663 | public: |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1664 | PointerLValue(SPIRVCodeGenerator& gen, SpvId pointer, SpvId type, |
| 1665 | SPIRVCodeGenerator::Precision precision) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1666 | : fGen(gen) |
| 1667 | , fPointer(pointer) |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1668 | , fType(type) |
| 1669 | , fPrecision(precision) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1670 | |
John Stiles | 1cf2c8d | 2020-08-13 22:58:04 -0400 | [diff] [blame] | 1671 | SpvId getPointer() override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1672 | return fPointer; |
| 1673 | } |
| 1674 | |
John Stiles | 1cf2c8d | 2020-08-13 22:58:04 -0400 | [diff] [blame] | 1675 | SpvId load(OutputStream& out) override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1676 | SpvId result = fGen.nextId(); |
| 1677 | fGen.writeInstruction(SpvOpLoad, fType, result, fPointer, out); |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1678 | fGen.writePrecisionModifier(fPrecision, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1679 | return result; |
| 1680 | } |
| 1681 | |
John Stiles | 1cf2c8d | 2020-08-13 22:58:04 -0400 | [diff] [blame] | 1682 | void store(SpvId value, OutputStream& out) override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1683 | fGen.writeInstruction(SpvOpStore, fPointer, value, out); |
| 1684 | } |
| 1685 | |
| 1686 | private: |
| 1687 | SPIRVCodeGenerator& fGen; |
| 1688 | const SpvId fPointer; |
| 1689 | const SpvId fType; |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1690 | const SPIRVCodeGenerator::Precision fPrecision; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1691 | }; |
| 1692 | |
| 1693 | class SwizzleLValue : public SPIRVCodeGenerator::LValue { |
| 1694 | public: |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 1695 | SwizzleLValue(SPIRVCodeGenerator& gen, SpvId vecPointer, const ComponentArray& components, |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1696 | const Type& baseType, const Type& swizzleType, |
| 1697 | SPIRVCodeGenerator::Precision precision) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1698 | : fGen(gen) |
| 1699 | , fVecPointer(vecPointer) |
| 1700 | , fComponents(components) |
| 1701 | , fBaseType(baseType) |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1702 | , fSwizzleType(swizzleType) |
| 1703 | , fPrecision(precision) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1704 | |
John Stiles | 1cf2c8d | 2020-08-13 22:58:04 -0400 | [diff] [blame] | 1705 | SpvId getPointer() override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1706 | return 0; |
| 1707 | } |
| 1708 | |
John Stiles | 1cf2c8d | 2020-08-13 22:58:04 -0400 | [diff] [blame] | 1709 | SpvId load(OutputStream& out) override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1710 | SpvId base = fGen.nextId(); |
| 1711 | fGen.writeInstruction(SpvOpLoad, fGen.getType(fBaseType), base, fVecPointer, out); |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1712 | fGen.writePrecisionModifier(fPrecision, base); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1713 | SpvId result = fGen.nextId(); |
| 1714 | fGen.writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) fComponents.size(), out); |
| 1715 | fGen.writeWord(fGen.getType(fSwizzleType), out); |
| 1716 | fGen.writeWord(result, out); |
| 1717 | fGen.writeWord(base, out); |
| 1718 | fGen.writeWord(base, out); |
| 1719 | for (int component : fComponents) { |
| 1720 | fGen.writeWord(component, out); |
| 1721 | } |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1722 | fGen.writePrecisionModifier(fPrecision, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1723 | return result; |
| 1724 | } |
| 1725 | |
John Stiles | 1cf2c8d | 2020-08-13 22:58:04 -0400 | [diff] [blame] | 1726 | void store(SpvId value, OutputStream& out) override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1727 | // use OpVectorShuffle to mix and match the vector components. We effectively create |
| 1728 | // a virtual vector out of the concatenation of the left and right vectors, and then |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1729 | // select components from this virtual vector to make the result vector. For |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1730 | // instance, given: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1731 | // float3L = ...; |
| 1732 | // float3R = ...; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1733 | // L.xz = R.xy; |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1734 | // we end up with the virtual vector (L.x, L.y, L.z, R.x, R.y, R.z). Then we want |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1735 | // our result vector to look like (R.x, L.y, R.y), so we need to select indices |
| 1736 | // (3, 1, 4). |
| 1737 | SpvId base = fGen.nextId(); |
| 1738 | fGen.writeInstruction(SpvOpLoad, fGen.getType(fBaseType), base, fVecPointer, out); |
| 1739 | SpvId shuffle = fGen.nextId(); |
| 1740 | fGen.writeOpCode(SpvOpVectorShuffle, 5 + fBaseType.columns(), out); |
| 1741 | fGen.writeWord(fGen.getType(fBaseType), out); |
| 1742 | fGen.writeWord(shuffle, out); |
| 1743 | fGen.writeWord(base, out); |
| 1744 | fGen.writeWord(value, out); |
| 1745 | for (int i = 0; i < fBaseType.columns(); i++) { |
| 1746 | // current offset into the virtual vector, defaults to pulling the unmodified |
| 1747 | // value from the left side |
| 1748 | int offset = i; |
| 1749 | // check to see if we are writing this component |
| 1750 | for (size_t j = 0; j < fComponents.size(); j++) { |
| 1751 | if (fComponents[j] == i) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1752 | // we're writing to this component, so adjust the offset to pull from |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1753 | // the correct component of the right side instead of preserving the |
| 1754 | // value from the left |
| 1755 | offset = (int) (j + fBaseType.columns()); |
| 1756 | break; |
| 1757 | } |
| 1758 | } |
| 1759 | fGen.writeWord(offset, out); |
| 1760 | } |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1761 | fGen.writePrecisionModifier(fPrecision, shuffle); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1762 | fGen.writeInstruction(SpvOpStore, fVecPointer, shuffle, out); |
| 1763 | } |
| 1764 | |
| 1765 | private: |
| 1766 | SPIRVCodeGenerator& fGen; |
| 1767 | const SpvId fVecPointer; |
John Stiles | 750109b | 2020-10-30 13:45:46 -0400 | [diff] [blame] | 1768 | const ComponentArray& fComponents; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1769 | const Type& fBaseType; |
| 1770 | const Type& fSwizzleType; |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1771 | const SPIRVCodeGenerator::Precision fPrecision; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1772 | }; |
| 1773 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1774 | std::unique_ptr<SPIRVCodeGenerator::LValue> SPIRVCodeGenerator::getLValue(const Expression& expr, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1775 | OutputStream& out) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1776 | const Type& type = expr.type(); |
| 1777 | Precision precision = type.highPrecision() ? Precision::kHigh : Precision::kLow; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1778 | switch (expr.kind()) { |
| 1779 | case Expression::Kind::kVariableReference: { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1780 | SpvId typeId; |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1781 | const Variable& var = *expr.as<VariableReference>().variable(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1782 | if (var.modifiers().fLayout.fBuiltin == SK_IN_BUILTIN) { |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 1783 | typeId = this->getType(*Type::MakeArrayType("sk_in", var.type().componentType(), |
| 1784 | fSkInCount)); |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 1785 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1786 | typeId = this->getType(type); |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 1787 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1788 | auto entry = fVariableMap.find(&var); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1789 | SkASSERT(entry != fVariableMap.end()); |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 1790 | return std::make_unique<PointerLValue>(*this, entry->second, typeId, precision); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1791 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1792 | case Expression::Kind::kIndex: // fall through |
| 1793 | case Expression::Kind::kFieldAccess: { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1794 | std::vector<SpvId> chain = this->getAccessChain(expr, out); |
| 1795 | SpvId member = this->nextId(); |
| 1796 | this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1797 | this->writeWord(this->getPointerType(type, get_storage_class(expr)), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1798 | this->writeWord(member, out); |
| 1799 | for (SpvId idx : chain) { |
| 1800 | this->writeWord(idx, out); |
| 1801 | } |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 1802 | return std::make_unique<PointerLValue>(*this, member, this->getType(type), precision); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1803 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1804 | case Expression::Kind::kSwizzle: { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1805 | Swizzle& swizzle = (Swizzle&) expr; |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 1806 | size_t count = swizzle.components().size(); |
| 1807 | SpvId base = this->getLValue(*swizzle.base(), out)->getPointer(); |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 1808 | if (!base) { |
| 1809 | fErrors.error(swizzle.fOffset, "unable to retrieve lvalue from swizzle"); |
| 1810 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1811 | if (count == 1) { |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 1812 | IntLiteral index(fContext, -1, swizzle.components()[0]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1813 | SpvId member = this->nextId(); |
| 1814 | this->writeInstruction(SpvOpAccessChain, |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1815 | this->getPointerType(type, |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 1816 | get_storage_class(*swizzle.base())), |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1817 | member, |
| 1818 | base, |
| 1819 | this->writeIntLiteral(index), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1820 | out); |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 1821 | return std::make_unique<PointerLValue>(*this, member, this->getType(type), |
| 1822 | precision); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1823 | } else { |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 1824 | return std::make_unique<SwizzleLValue>(*this, base, swizzle.components(), |
| 1825 | swizzle.base()->type(), type, precision); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1826 | } |
| 1827 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 1828 | case Expression::Kind::kTernary: { |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 1829 | TernaryExpression& t = (TernaryExpression&) expr; |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 1830 | SpvId test = this->writeExpression(*t.test(), out); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 1831 | SpvId end = this->nextId(); |
| 1832 | SpvId ifTrueLabel = this->nextId(); |
| 1833 | SpvId ifFalseLabel = this->nextId(); |
| 1834 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 1835 | this->writeInstruction(SpvOpBranchConditional, test, ifTrueLabel, ifFalseLabel, out); |
| 1836 | this->writeLabel(ifTrueLabel, out); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 1837 | SpvId ifTrue = this->getLValue(*t.ifTrue(), out)->getPointer(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1838 | SkASSERT(ifTrue); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 1839 | this->writeInstruction(SpvOpBranch, end, out); |
| 1840 | ifTrueLabel = fCurrentBlock; |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 1841 | SpvId ifFalse = this->getLValue(*t.ifFalse(), out)->getPointer(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1842 | SkASSERT(ifFalse); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 1843 | ifFalseLabel = fCurrentBlock; |
| 1844 | this->writeInstruction(SpvOpBranch, end, out); |
| 1845 | SpvId result = this->nextId(); |
| 1846 | this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, ifTrue, |
| 1847 | ifTrueLabel, ifFalse, ifFalseLabel, out); |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 1848 | return std::make_unique<PointerLValue>(*this, result, this->getType(type), precision); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 1849 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1850 | default: { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1851 | // expr isn't actually an lvalue, create a dummy variable for it. This case happens due |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1852 | // to the need to store values in temporary variables during function calls (see |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1853 | // comments in getFunctionType); erroneous uses of rvalues as lvalues should have been |
| 1854 | // caught by IRGenerator |
| 1855 | SpvId result = this->nextId(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1856 | SpvId pointerType = this->getPointerType(type, SpvStorageClassFunction); |
| 1857 | this->writeInstruction(SpvOpVariable, pointerType, result, SpvStorageClassFunction, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1858 | fVariableBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1859 | this->writeInstruction(SpvOpStore, result, this->writeExpression(expr, out), out); |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 1860 | return std::make_unique<PointerLValue>(*this, result, this->getType(type), precision); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1861 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1862 | } |
| 1863 | } |
| 1864 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1865 | SpvId SPIRVCodeGenerator::writeVariableReference(const VariableReference& ref, OutputStream& out) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1866 | SpvId result = this->nextId(); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1867 | auto entry = fVariableMap.find(ref.variable()); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1868 | SkASSERT(entry != fVariableMap.end()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1869 | SpvId var = entry->second; |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1870 | this->writeInstruction(SpvOpLoad, this->getType(ref.variable()->type()), result, var, out); |
| 1871 | this->writePrecisionModifier(ref.variable()->type(), result); |
| 1872 | if (ref.variable()->modifiers().fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN && |
Brian Osman | e38bedd | 2020-12-21 11:51:54 -0500 | [diff] [blame] | 1873 | fProgram.fSettings.fFlipY) { |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1874 | // The x component never changes, so just grab it |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1875 | SpvId xId = this->nextId(); |
| 1876 | this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), xId, |
| 1877 | result, 0, out); |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1878 | |
| 1879 | // Calculate the y component which may need to be flipped |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1880 | SpvId rawYId = this->nextId(); |
| 1881 | this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), rawYId, |
| 1882 | result, 1, out); |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1883 | SpvId flippedYId = 0; |
| 1884 | if (fProgram.fSettings.fFlipY) { |
| 1885 | // need to remap to a top-left coordinate system |
| 1886 | if (fRTHeightStructId == (SpvId)-1) { |
| 1887 | // height variable hasn't been written yet |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1888 | SkASSERT(fRTHeightFieldIndex == (SpvId)-1); |
| 1889 | std::vector<Type::Field> fields; |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 1890 | if (fProgram.fSettings.fRTHeightOffset < 0) { |
| 1891 | fErrors.error(ref.fOffset, "RTHeightOffset is negative"); |
| 1892 | } |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1893 | fields.emplace_back( |
| 1894 | Modifiers(Layout(0, -1, fProgram.fSettings.fRTHeightOffset, -1, -1, -1, -1, |
| 1895 | -1, Layout::Format::kUnspecified, |
| 1896 | Layout::kUnspecified_Primitive, 1, -1, "", "", |
| 1897 | Layout::kNo_Key, Layout::CType::kDefault), |
| 1898 | 0), |
| 1899 | SKSL_RTHEIGHT_NAME, fContext.fFloat_Type.get()); |
| 1900 | StringFragment name("sksl_synthetic_uniforms"); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 1901 | std::unique_ptr<Type> intfStruct = Type::MakeStructType(/*offset=*/-1, name, |
| 1902 | fields); |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1903 | int binding = fProgram.fSettings.fRTHeightBinding; |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 1904 | if (binding == -1) { |
| 1905 | fErrors.error(ref.fOffset, "layout(binding=...) is required in SPIR-V"); |
| 1906 | } |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1907 | int set = fProgram.fSettings.fRTHeightSet; |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 1908 | if (set == -1) { |
| 1909 | fErrors.error(ref.fOffset, "layout(set=...) is required in SPIR-V"); |
| 1910 | } |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1911 | Layout layout(0, -1, -1, binding, -1, set, -1, -1, Layout::Format::kUnspecified, |
| 1912 | Layout::kUnspecified_Primitive, -1, -1, "", "", Layout::kNo_Key, |
| 1913 | Layout::CType::kDefault); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 1914 | Modifiers modifiers(layout, Modifiers::kUniform_Flag); |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 1915 | const Variable* intfVar = fSynthetics.takeOwnershipOfSymbol( |
| 1916 | std::make_unique<Variable>(/*offset=*/-1, |
John Stiles | 586df95 | 2020-11-12 18:27:13 -0500 | [diff] [blame] | 1917 | fProgram.fModifiers->addToPool(modifiers), |
John Stiles | 3ae071e | 2020-08-05 15:29:29 -0400 | [diff] [blame] | 1918 | name, |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 1919 | intfStruct.get(), |
Brian Osman | 3887a01 | 2020-09-30 13:22:27 -0400 | [diff] [blame] | 1920 | /*builtin=*/false, |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 1921 | Variable::Storage::kGlobal)); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 1922 | InterfaceBlock intf(/*offset=*/-1, intfVar, name, |
| 1923 | /*instanceName=*/"", /*arraySize=*/0, |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 1924 | std::make_shared<SymbolTable>(&fErrors, /*builtin=*/false)); |
Stephen White | 8857497 | 2020-06-23 19:09:29 -0400 | [diff] [blame] | 1925 | |
| 1926 | fRTHeightStructId = this->writeInterfaceBlock(intf, false); |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1927 | fRTHeightFieldIndex = 0; |
Jim Van Verth | f3ec983 | 2020-10-21 16:09:57 -0400 | [diff] [blame] | 1928 | fRTHeightStorageClass = SpvStorageClassUniform; |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1929 | } |
| 1930 | SkASSERT(fRTHeightFieldIndex != (SpvId)-1); |
| 1931 | |
| 1932 | IntLiteral fieldIndex(fContext, -1, fRTHeightFieldIndex); |
| 1933 | SpvId fieldIndexId = this->writeIntLiteral(fieldIndex); |
| 1934 | SpvId heightPtr = this->nextId(); |
| 1935 | this->writeOpCode(SpvOpAccessChain, 5, out); |
Jim Van Verth | f3ec983 | 2020-10-21 16:09:57 -0400 | [diff] [blame] | 1936 | this->writeWord(this->getPointerType(*fContext.fFloat_Type, fRTHeightStorageClass), |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1937 | out); |
| 1938 | this->writeWord(heightPtr, out); |
| 1939 | this->writeWord(fRTHeightStructId, out); |
| 1940 | this->writeWord(fieldIndexId, out); |
| 1941 | SpvId heightRead = this->nextId(); |
| 1942 | this->writeInstruction(SpvOpLoad, this->getType(*fContext.fFloat_Type), heightRead, |
| 1943 | heightPtr, out); |
| 1944 | |
| 1945 | flippedYId = this->nextId(); |
| 1946 | this->writeInstruction(SpvOpFSub, this->getType(*fContext.fFloat_Type), flippedYId, |
| 1947 | heightRead, rawYId, out); |
| 1948 | } |
| 1949 | |
| 1950 | // The z component will always be zero so we just get an id to the 0 literal |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1951 | FloatLiteral zero(fContext, -1, 0.0); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1952 | SpvId zeroId = writeFloatLiteral(zero); |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1953 | |
Brian Osman | e38bedd | 2020-12-21 11:51:54 -0500 | [diff] [blame] | 1954 | // Calculate the w component |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1955 | SpvId rawWId = this->nextId(); |
| 1956 | this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), rawWId, |
Ethan Nicholas | 20798e5 | 2018-12-04 12:30:50 -0500 | [diff] [blame] | 1957 | result, 3, out); |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1958 | |
| 1959 | // Fill in the new fragcoord with the components from above |
| 1960 | SpvId adjusted = this->nextId(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1961 | this->writeOpCode(SpvOpCompositeConstruct, 7, out); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1962 | this->writeWord(this->getType(*fContext.fFloat4_Type), out); |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1963 | this->writeWord(adjusted, out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1964 | this->writeWord(xId, out); |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1965 | if (fProgram.fSettings.fFlipY) { |
| 1966 | this->writeWord(flippedYId, out); |
| 1967 | } else { |
| 1968 | this->writeWord(rawYId, out); |
| 1969 | } |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1970 | this->writeWord(zeroId, out); |
Brian Osman | e38bedd | 2020-12-21 11:51:54 -0500 | [diff] [blame] | 1971 | this->writeWord(rawWId, out); |
Greg Daniel | a85e4bf | 2020-06-17 16:32:45 -0400 | [diff] [blame] | 1972 | |
| 1973 | return adjusted; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1974 | } |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 1975 | if (ref.variable()->modifiers().fLayout.fBuiltin == SK_CLOCKWISE_BUILTIN && |
Chris Dalton | b91c466 | 2018-08-01 10:46:22 -0600 | [diff] [blame] | 1976 | !fProgram.fSettings.fFlipY) { |
| 1977 | // FrontFacing in Vulkan is defined in terms of a top-down render target. In skia, we use |
| 1978 | // the default convention of "counter-clockwise face is front". |
| 1979 | SpvId inverse = this->nextId(); |
| 1980 | this->writeInstruction(SpvOpLogicalNot, this->getType(*fContext.fBool_Type), inverse, |
| 1981 | result, out); |
| 1982 | return inverse; |
| 1983 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1984 | return result; |
| 1985 | } |
| 1986 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1987 | SpvId SPIRVCodeGenerator::writeIndexExpression(const IndexExpression& expr, OutputStream& out) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 1988 | if (expr.base()->type().isVector()) { |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 1989 | SpvId base = this->writeExpression(*expr.base(), out); |
| 1990 | SpvId index = this->writeExpression(*expr.index(), out); |
Ethan Nicholas | a9a0690 | 2019-01-07 14:42:40 -0500 | [diff] [blame] | 1991 | SpvId result = this->nextId(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 1992 | this->writeInstruction(SpvOpVectorExtractDynamic, this->getType(expr.type()), result, base, |
Ethan Nicholas | a9a0690 | 2019-01-07 14:42:40 -0500 | [diff] [blame] | 1993 | index, out); |
| 1994 | return result; |
| 1995 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1996 | return getLValue(expr, out)->load(out); |
| 1997 | } |
| 1998 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1999 | SpvId SPIRVCodeGenerator::writeFieldAccess(const FieldAccess& f, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2000 | return getLValue(f, out)->load(out); |
| 2001 | } |
| 2002 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2003 | SpvId SPIRVCodeGenerator::writeSwizzle(const Swizzle& swizzle, OutputStream& out) { |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 2004 | SpvId base = this->writeExpression(*swizzle.base(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2005 | SpvId result = this->nextId(); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 2006 | size_t count = swizzle.components().size(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2007 | if (count == 1) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2008 | this->writeInstruction(SpvOpCompositeExtract, this->getType(swizzle.type()), result, base, |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 2009 | swizzle.components()[0], out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2010 | } else { |
| 2011 | this->writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) count, out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2012 | this->writeWord(this->getType(swizzle.type()), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2013 | this->writeWord(result, out); |
| 2014 | this->writeWord(base, out); |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2015 | this->writeWord(base, out); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 2016 | for (int component : swizzle.components()) { |
Brian Osman | 2564767 | 2020-09-15 15:16:56 -0400 | [diff] [blame] | 2017 | this->writeWord(component, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | return result; |
| 2021 | } |
| 2022 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2023 | SpvId SPIRVCodeGenerator::writeBinaryOperation(const Type& resultType, |
| 2024 | const Type& operandType, SpvId lhs, |
| 2025 | SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2026 | SpvOp_ ifUInt, SpvOp_ ifBool, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2027 | SpvId result = this->nextId(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2028 | if (is_float(fContext, operandType)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2029 | this->writeInstruction(ifFloat, this->getType(resultType), result, lhs, rhs, out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2030 | } else if (is_signed(fContext, operandType)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2031 | this->writeInstruction(ifInt, this->getType(resultType), result, lhs, rhs, out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2032 | } else if (is_unsigned(fContext, operandType)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2033 | this->writeInstruction(ifUInt, this->getType(resultType), result, lhs, rhs, out); |
John Stiles | 123501f | 2020-12-09 10:08:13 -0500 | [diff] [blame] | 2034 | } else if (is_bool(fContext, operandType)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2035 | this->writeInstruction(ifBool, this->getType(resultType), result, lhs, rhs, out); |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 2036 | return result; // skip RelaxedPrecision check |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2037 | } else { |
John Stiles | 123501f | 2020-12-09 10:08:13 -0500 | [diff] [blame] | 2038 | fErrors.error(operandType.fOffset, |
| 2039 | "unsupported operand for binary expression: " + operandType.description()); |
| 2040 | return result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2041 | } |
Ethan Nicholas | e77739e | 2019-03-14 14:04:43 -0400 | [diff] [blame] | 2042 | if (getActualType(resultType) == operandType && !resultType.highPrecision()) { |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 2043 | this->writeInstruction(SpvOpDecorate, result, SpvDecorationRelaxedPrecision, |
| 2044 | fDecorationBuffer); |
| 2045 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2046 | return result; |
| 2047 | } |
| 2048 | |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2049 | SpvId SPIRVCodeGenerator::foldToBool(SpvId id, const Type& operandType, SpvOp op, |
| 2050 | OutputStream& out) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2051 | if (operandType.isVector()) { |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 2052 | SpvId result = this->nextId(); |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2053 | this->writeInstruction(op, this->getType(*fContext.fBool_Type), result, id, out); |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 2054 | return result; |
| 2055 | } |
| 2056 | return id; |
| 2057 | } |
| 2058 | |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2059 | SpvId SPIRVCodeGenerator::writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, |
| 2060 | SpvOp_ floatOperator, SpvOp_ intOperator, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2061 | SpvOp_ vectorMergeOperator, SpvOp_ mergeOperator, |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2062 | OutputStream& out) { |
| 2063 | SpvOp_ compareOp = is_float(fContext, operandType) ? floatOperator : intOperator; |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2064 | SkASSERT(operandType.isMatrix()); |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2065 | SpvId columnType = this->getType(operandType.componentType().toCompound(fContext, |
| 2066 | operandType.rows(), |
| 2067 | 1)); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2068 | SpvId bvecType = this->getType(fContext.fBool_Type->toCompound(fContext, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2069 | operandType.rows(), |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2070 | 1)); |
| 2071 | SpvId boolType = this->getType(*fContext.fBool_Type); |
| 2072 | SpvId result = 0; |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2073 | for (int i = 0; i < operandType.columns(); i++) { |
| 2074 | SpvId columnL = this->nextId(); |
| 2075 | this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out); |
| 2076 | SpvId columnR = this->nextId(); |
| 2077 | this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2078 | SpvId compare = this->nextId(); |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2079 | this->writeInstruction(compareOp, bvecType, compare, columnL, columnR, out); |
| 2080 | SpvId merge = this->nextId(); |
| 2081 | this->writeInstruction(vectorMergeOperator, boolType, merge, compare, out); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2082 | if (result != 0) { |
| 2083 | SpvId next = this->nextId(); |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2084 | this->writeInstruction(mergeOperator, boolType, next, result, merge, out); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2085 | result = next; |
| 2086 | } |
| 2087 | else { |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2088 | result = merge; |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2089 | } |
| 2090 | } |
| 2091 | return result; |
| 2092 | } |
| 2093 | |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2094 | SpvId SPIRVCodeGenerator::writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, |
| 2095 | SpvId rhs, SpvOp_ floatOperator, |
| 2096 | SpvOp_ intOperator, |
| 2097 | OutputStream& out) { |
| 2098 | SpvOp_ op = is_float(fContext, operandType) ? floatOperator : intOperator; |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2099 | SkASSERT(operandType.isMatrix()); |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2100 | SpvId columnType = this->getType(operandType.componentType().toCompound(fContext, |
| 2101 | operandType.rows(), |
| 2102 | 1)); |
| 2103 | SpvId columns[4]; |
| 2104 | for (int i = 0; i < operandType.columns(); i++) { |
| 2105 | SpvId columnL = this->nextId(); |
| 2106 | this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out); |
| 2107 | SpvId columnR = this->nextId(); |
| 2108 | this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out); |
| 2109 | columns[i] = this->nextId(); |
| 2110 | this->writeInstruction(op, columnType, columns[i], columnL, columnR, out); |
| 2111 | } |
| 2112 | SpvId result = this->nextId(); |
| 2113 | this->writeOpCode(SpvOpCompositeConstruct, 3 + operandType.columns(), out); |
| 2114 | this->writeWord(this->getType(operandType), out); |
| 2115 | this->writeWord(result, out); |
| 2116 | for (int i = 0; i < operandType.columns(); i++) { |
| 2117 | this->writeWord(columns[i], out); |
| 2118 | } |
| 2119 | return result; |
| 2120 | } |
| 2121 | |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2122 | std::unique_ptr<Expression> create_literal_1(const Context& context, const Type& type) { |
| 2123 | if (type.isInteger()) { |
| 2124 | return std::unique_ptr<Expression>(new IntLiteral(-1, 1, &type)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2125 | } |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2126 | else if (type.isFloat()) { |
| 2127 | return std::unique_ptr<Expression>(new FloatLiteral(-1, 1.0, &type)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2128 | } else { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2129 | ABORT("math is unsupported on type '%s'", String(type.name()).c_str()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2130 | } |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | SpvId SPIRVCodeGenerator::writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op, |
| 2134 | const Type& rightType, SpvId rhs, |
| 2135 | const Type& resultType, OutputStream& out) { |
John Stiles | d0614f2 | 2020-12-09 11:11:41 -0500 | [diff] [blame] | 2136 | // The comma operator ignores the type of the left-hand side entirely. |
| 2137 | if (op == Token::Kind::TK_COMMA) { |
| 2138 | return rhs; |
| 2139 | } |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2140 | // overall type we are operating on: float2, int, uint4... |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2141 | const Type* operandType; |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2142 | // IR allows mismatched types in expressions (e.g. float2 * float), but they need special |
| 2143 | // handling in SPIR-V |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2144 | if (this->getActualType(leftType) != this->getActualType(rightType)) { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2145 | if (leftType.isVector() && rightType.isNumber()) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2146 | if (op == Token::Kind::TK_SLASH) { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2147 | SpvId one = this->writeExpression(*create_literal_1(fContext, rightType), out); |
| 2148 | SpvId inverse = this->nextId(); |
| 2149 | this->writeInstruction(SpvOpFDiv, this->getType(rightType), inverse, one, rhs, out); |
| 2150 | rhs = inverse; |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2151 | op = Token::Kind::TK_STAR; |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2152 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2153 | if (op == Token::Kind::TK_STAR) { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2154 | SpvId result = this->nextId(); |
| 2155 | this->writeInstruction(SpvOpVectorTimesScalar, this->getType(resultType), |
| 2156 | result, lhs, rhs, out); |
| 2157 | return result; |
| 2158 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2159 | // promote number to vector |
| 2160 | SpvId vec = this->nextId(); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2161 | const Type& vecType = leftType; |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2162 | this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out); |
| 2163 | this->writeWord(this->getType(vecType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2164 | this->writeWord(vec, out); |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2165 | for (int i = 0; i < vecType.columns(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2166 | this->writeWord(rhs, out); |
| 2167 | } |
| 2168 | rhs = vec; |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2169 | operandType = &leftType; |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2170 | } else if (rightType.isVector() && leftType.isNumber()) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2171 | if (op == Token::Kind::TK_STAR) { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2172 | SpvId result = this->nextId(); |
| 2173 | this->writeInstruction(SpvOpVectorTimesScalar, this->getType(resultType), |
| 2174 | result, rhs, lhs, out); |
| 2175 | return result; |
| 2176 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2177 | // promote number to vector |
| 2178 | SpvId vec = this->nextId(); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2179 | const Type& vecType = rightType; |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2180 | this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out); |
| 2181 | this->writeWord(this->getType(vecType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2182 | this->writeWord(vec, out); |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2183 | for (int i = 0; i < vecType.columns(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2184 | this->writeWord(lhs, out); |
| 2185 | } |
| 2186 | lhs = vec; |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2187 | operandType = &rightType; |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2188 | } else if (leftType.isMatrix()) { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2189 | SpvOp_ spvop; |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2190 | if (rightType.isMatrix()) { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2191 | spvop = SpvOpMatrixTimesMatrix; |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2192 | } else if (rightType.isVector()) { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2193 | spvop = SpvOpMatrixTimesVector; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2194 | } else { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2195 | SkASSERT(rightType.isScalar()); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2196 | spvop = SpvOpMatrixTimesScalar; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2197 | } |
| 2198 | SpvId result = this->nextId(); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2199 | this->writeInstruction(spvop, this->getType(resultType), result, lhs, rhs, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2200 | return result; |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2201 | } else if (rightType.isMatrix()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2202 | SpvId result = this->nextId(); |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2203 | if (leftType.isVector()) { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2204 | this->writeInstruction(SpvOpVectorTimesMatrix, this->getType(resultType), result, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2205 | lhs, rhs, out); |
| 2206 | } else { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2207 | SkASSERT(leftType.isScalar()); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2208 | this->writeInstruction(SpvOpMatrixTimesScalar, this->getType(resultType), result, |
| 2209 | rhs, lhs, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2210 | } |
| 2211 | return result; |
| 2212 | } else { |
John Stiles | d8ca6b6 | 2020-11-23 14:28:36 -0500 | [diff] [blame] | 2213 | fErrors.error(leftType.fOffset, "unsupported mixed-type expression"); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2214 | return -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2215 | } |
| 2216 | } else { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 2217 | operandType = &this->getActualType(leftType); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2218 | SkASSERT(*operandType == this->getActualType(rightType)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2219 | } |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2220 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2221 | case Token::Kind::TK_EQEQ: { |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2222 | if (operandType->isMatrix()) { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2223 | return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdEqual, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2224 | SpvOpIEqual, SpvOpAll, SpvOpLogicalAnd, out); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2225 | } |
John Stiles | 4a7dc46 | 2020-11-25 11:08:08 -0500 | [diff] [blame] | 2226 | SkASSERT(resultType.isBoolean()); |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2227 | const Type* tmpType; |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2228 | if (operandType->isVector()) { |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2229 | tmpType = &fContext.fBool_Type->toCompound(fContext, |
| 2230 | operandType->columns(), |
| 2231 | operandType->rows()); |
| 2232 | } else { |
| 2233 | tmpType = &resultType; |
| 2234 | } |
| 2235 | return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs, |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 2236 | SpvOpFOrdEqual, SpvOpIEqual, |
| 2237 | SpvOpIEqual, SpvOpLogicalEqual, out), |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2238 | *operandType, SpvOpAll, out); |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 2239 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2240 | case Token::Kind::TK_NEQ: |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2241 | if (operandType->isMatrix()) { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2242 | return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdNotEqual, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2243 | SpvOpINotEqual, SpvOpAny, SpvOpLogicalOr, out); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2244 | } |
John Stiles | 4a7dc46 | 2020-11-25 11:08:08 -0500 | [diff] [blame] | 2245 | [[fallthrough]]; |
| 2246 | case Token::Kind::TK_LOGICALXOR: |
| 2247 | SkASSERT(resultType.isBoolean()); |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2248 | const Type* tmpType; |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2249 | if (operandType->isVector()) { |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2250 | tmpType = &fContext.fBool_Type->toCompound(fContext, |
| 2251 | operandType->columns(), |
| 2252 | operandType->rows()); |
| 2253 | } else { |
| 2254 | tmpType = &resultType; |
| 2255 | } |
| 2256 | return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs, |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 2257 | SpvOpFOrdNotEqual, SpvOpINotEqual, |
| 2258 | SpvOpINotEqual, SpvOpLogicalNotEqual, |
| 2259 | out), |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2260 | *operandType, SpvOpAny, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2261 | case Token::Kind::TK_GT: |
John Stiles | 4a7dc46 | 2020-11-25 11:08:08 -0500 | [diff] [blame] | 2262 | SkASSERT(resultType.isBoolean()); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2263 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, |
| 2264 | SpvOpFOrdGreaterThan, SpvOpSGreaterThan, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2265 | SpvOpUGreaterThan, SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2266 | case Token::Kind::TK_LT: |
John Stiles | 4a7dc46 | 2020-11-25 11:08:08 -0500 | [diff] [blame] | 2267 | SkASSERT(resultType.isBoolean()); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2268 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFOrdLessThan, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2269 | SpvOpSLessThan, SpvOpULessThan, SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2270 | case Token::Kind::TK_GTEQ: |
John Stiles | 4a7dc46 | 2020-11-25 11:08:08 -0500 | [diff] [blame] | 2271 | SkASSERT(resultType.isBoolean()); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2272 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, |
| 2273 | SpvOpFOrdGreaterThanEqual, SpvOpSGreaterThanEqual, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2274 | SpvOpUGreaterThanEqual, SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2275 | case Token::Kind::TK_LTEQ: |
John Stiles | 4a7dc46 | 2020-11-25 11:08:08 -0500 | [diff] [blame] | 2276 | SkASSERT(resultType.isBoolean()); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2277 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, |
| 2278 | SpvOpFOrdLessThanEqual, SpvOpSLessThanEqual, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2279 | SpvOpULessThanEqual, SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2280 | case Token::Kind::TK_PLUS: |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2281 | if (leftType.isMatrix() && rightType.isMatrix()) { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2282 | SkASSERT(leftType == rightType); |
| 2283 | return this->writeComponentwiseMatrixBinary(leftType, lhs, rhs, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2284 | SpvOpFAdd, SpvOpIAdd, out); |
| 2285 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2286 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFAdd, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2287 | SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2288 | case Token::Kind::TK_MINUS: |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2289 | if (leftType.isMatrix() && rightType.isMatrix()) { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2290 | SkASSERT(leftType == rightType); |
| 2291 | return this->writeComponentwiseMatrixBinary(leftType, lhs, rhs, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2292 | SpvOpFSub, SpvOpISub, out); |
| 2293 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2294 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFSub, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2295 | SpvOpISub, SpvOpISub, SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2296 | case Token::Kind::TK_STAR: |
John Stiles | 9aeed13 | 2020-11-24 17:36:06 -0500 | [diff] [blame] | 2297 | if (leftType.isMatrix() && rightType.isMatrix()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2298 | // matrix multiply |
| 2299 | SpvId result = this->nextId(); |
| 2300 | this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(resultType), result, |
| 2301 | lhs, rhs, out); |
| 2302 | return result; |
| 2303 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2304 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMul, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2305 | SpvOpIMul, SpvOpIMul, SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2306 | case Token::Kind::TK_SLASH: |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2307 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFDiv, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2308 | SpvOpSDiv, SpvOpUDiv, SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2309 | case Token::Kind::TK_PERCENT: |
Ethan Nicholas | b3d0f7c | 2017-05-17 13:13:21 -0400 | [diff] [blame] | 2310 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMod, |
| 2311 | SpvOpSMod, SpvOpUMod, SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2312 | case Token::Kind::TK_SHL: |
Ethan Nicholas | fd444be | 2017-07-05 10:05:54 -0400 | [diff] [blame] | 2313 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef, |
| 2314 | SpvOpShiftLeftLogical, SpvOpShiftLeftLogical, |
| 2315 | SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2316 | case Token::Kind::TK_SHR: |
Ethan Nicholas | fd444be | 2017-07-05 10:05:54 -0400 | [diff] [blame] | 2317 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef, |
| 2318 | SpvOpShiftRightArithmetic, SpvOpShiftRightLogical, |
| 2319 | SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2320 | case Token::Kind::TK_BITWISEAND: |
Ethan Nicholas | fd444be | 2017-07-05 10:05:54 -0400 | [diff] [blame] | 2321 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef, |
| 2322 | SpvOpBitwiseAnd, SpvOpBitwiseAnd, SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2323 | case Token::Kind::TK_BITWISEOR: |
Ethan Nicholas | fd444be | 2017-07-05 10:05:54 -0400 | [diff] [blame] | 2324 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef, |
| 2325 | SpvOpBitwiseOr, SpvOpBitwiseOr, SpvOpUndef, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2326 | case Token::Kind::TK_BITWISEXOR: |
Ethan Nicholas | fd444be | 2017-07-05 10:05:54 -0400 | [diff] [blame] | 2327 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef, |
| 2328 | SpvOpBitwiseXor, SpvOpBitwiseXor, SpvOpUndef, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2329 | default: |
John Stiles | 5570c51 | 2020-11-19 17:58:07 -0500 | [diff] [blame] | 2330 | fErrors.error(0, "unsupported token"); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2331 | return -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2332 | } |
| 2333 | } |
| 2334 | |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2335 | SpvId SPIRVCodeGenerator::writeBinaryExpression(const BinaryExpression& b, OutputStream& out) { |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 2336 | const Expression& left = *b.left(); |
| 2337 | const Expression& right = *b.right(); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 2338 | Token::Kind op = b.getOperator(); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2339 | // handle cases where we don't necessarily evaluate both LHS and RHS |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 2340 | switch (op) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2341 | case Token::Kind::TK_EQ: { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 2342 | SpvId rhs = this->writeExpression(right, out); |
| 2343 | this->getLValue(left, out)->store(rhs, out); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2344 | return rhs; |
| 2345 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2346 | case Token::Kind::TK_LOGICALAND: |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2347 | return this->writeLogicalAnd(b, out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2348 | case Token::Kind::TK_LOGICALOR: |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2349 | return this->writeLogicalOr(b, out); |
| 2350 | default: |
| 2351 | break; |
| 2352 | } |
| 2353 | |
| 2354 | std::unique_ptr<LValue> lvalue; |
| 2355 | SpvId lhs; |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 2356 | if (Compiler::IsAssignment(op)) { |
| 2357 | lvalue = this->getLValue(left, out); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2358 | lhs = lvalue->load(out); |
| 2359 | } else { |
| 2360 | lvalue = nullptr; |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 2361 | lhs = this->writeExpression(left, out); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2362 | } |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 2363 | SpvId rhs = this->writeExpression(right, out); |
| 2364 | SpvId result = this->writeBinaryExpression(left.type(), lhs, Compiler::RemoveAssignment(op), |
| 2365 | right.type(), rhs, b.type(), out); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2366 | if (lvalue) { |
| 2367 | lvalue->store(result, out); |
| 2368 | } |
| 2369 | return result; |
| 2370 | } |
| 2371 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2372 | SpvId SPIRVCodeGenerator::writeLogicalAnd(const BinaryExpression& a, OutputStream& out) { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 2373 | SkASSERT(a.getOperator() == Token::Kind::TK_LOGICALAND); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2374 | BoolLiteral falseLiteral(fContext, -1, false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2375 | SpvId falseConstant = this->writeBoolLiteral(falseLiteral); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 2376 | SpvId lhs = this->writeExpression(*a.left(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2377 | SpvId rhsLabel = this->nextId(); |
| 2378 | SpvId end = this->nextId(); |
| 2379 | SpvId lhsBlock = fCurrentBlock; |
| 2380 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 2381 | this->writeInstruction(SpvOpBranchConditional, lhs, rhsLabel, end, out); |
| 2382 | this->writeLabel(rhsLabel, out); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 2383 | SpvId rhs = this->writeExpression(*a.right(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2384 | SpvId rhsBlock = fCurrentBlock; |
| 2385 | this->writeInstruction(SpvOpBranch, end, out); |
| 2386 | this->writeLabel(end, out); |
| 2387 | SpvId result = this->nextId(); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2388 | this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, falseConstant, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2389 | lhsBlock, rhs, rhsBlock, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2390 | return result; |
| 2391 | } |
| 2392 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2393 | SpvId SPIRVCodeGenerator::writeLogicalOr(const BinaryExpression& o, OutputStream& out) { |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 2394 | SkASSERT(o.getOperator() == Token::Kind::TK_LOGICALOR); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2395 | BoolLiteral trueLiteral(fContext, -1, true); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2396 | SpvId trueConstant = this->writeBoolLiteral(trueLiteral); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 2397 | SpvId lhs = this->writeExpression(*o.left(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2398 | SpvId rhsLabel = this->nextId(); |
| 2399 | SpvId end = this->nextId(); |
| 2400 | SpvId lhsBlock = fCurrentBlock; |
| 2401 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 2402 | this->writeInstruction(SpvOpBranchConditional, lhs, end, rhsLabel, out); |
| 2403 | this->writeLabel(rhsLabel, out); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 2404 | SpvId rhs = this->writeExpression(*o.right(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2405 | SpvId rhsBlock = fCurrentBlock; |
| 2406 | this->writeInstruction(SpvOpBranch, end, out); |
| 2407 | this->writeLabel(end, out); |
| 2408 | SpvId result = this->nextId(); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2409 | this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, trueConstant, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2410 | lhsBlock, rhs, rhsBlock, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2411 | return result; |
| 2412 | } |
| 2413 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2414 | SpvId SPIRVCodeGenerator::writeTernaryExpression(const TernaryExpression& t, OutputStream& out) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2415 | const Type& type = t.type(); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 2416 | SpvId test = this->writeExpression(*t.test(), out); |
| 2417 | if (t.ifTrue()->type().columns() == 1 && |
| 2418 | t.ifTrue()->isCompileTimeConstant() && |
| 2419 | t.ifFalse()->isCompileTimeConstant()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2420 | // both true and false are constants, can just use OpSelect |
| 2421 | SpvId result = this->nextId(); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 2422 | SpvId trueId = this->writeExpression(*t.ifTrue(), out); |
| 2423 | SpvId falseId = this->writeExpression(*t.ifFalse(), out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2424 | this->writeInstruction(SpvOpSelect, this->getType(type), result, test, trueId, falseId, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2425 | out); |
| 2426 | return result; |
| 2427 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2428 | // was originally using OpPhi to choose the result, but for some reason that is crashing on |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2429 | // Adreno. Switched to storing the result in a temp variable as glslang does. |
| 2430 | SpvId var = this->nextId(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2431 | this->writeInstruction(SpvOpVariable, this->getPointerType(type, SpvStorageClassFunction), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2432 | var, SpvStorageClassFunction, fVariableBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2433 | SpvId trueLabel = this->nextId(); |
| 2434 | SpvId falseLabel = this->nextId(); |
| 2435 | SpvId end = this->nextId(); |
| 2436 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 2437 | this->writeInstruction(SpvOpBranchConditional, test, trueLabel, falseLabel, out); |
| 2438 | this->writeLabel(trueLabel, out); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 2439 | this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.ifTrue(), out), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2440 | this->writeInstruction(SpvOpBranch, end, out); |
| 2441 | this->writeLabel(falseLabel, out); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 2442 | this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.ifFalse(), out), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2443 | this->writeInstruction(SpvOpBranch, end, out); |
| 2444 | this->writeLabel(end, out); |
| 2445 | SpvId result = this->nextId(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2446 | this->writeInstruction(SpvOpLoad, this->getType(type), result, var, out); |
| 2447 | this->writePrecisionModifier(type, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2448 | return result; |
| 2449 | } |
| 2450 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2451 | SpvId SPIRVCodeGenerator::writePrefixExpression(const PrefixExpression& p, OutputStream& out) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2452 | const Type& type = p.type(); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2453 | if (p.getOperator() == Token::Kind::TK_MINUS) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2454 | SpvId result = this->nextId(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2455 | SpvId typeId = this->getType(type); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2456 | SpvId expr = this->writeExpression(*p.operand(), out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2457 | if (is_float(fContext, type)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2458 | this->writeInstruction(SpvOpFNegate, typeId, result, expr, out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2459 | } else if (is_signed(fContext, type)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2460 | this->writeInstruction(SpvOpSNegate, typeId, result, expr, out); |
| 2461 | } else { |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2462 | #ifdef SK_DEBUG |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2463 | ABORT("unsupported prefix expression %s", p.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2464 | #endif |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 2465 | } |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2466 | this->writePrecisionModifier(type, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2467 | return result; |
| 2468 | } |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2469 | switch (p.getOperator()) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2470 | case Token::Kind::TK_PLUS: |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2471 | return this->writeExpression(*p.operand(), out); |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2472 | case Token::Kind::TK_PLUSPLUS: { |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2473 | std::unique_ptr<LValue> lv = this->getLValue(*p.operand(), out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2474 | SpvId one = this->writeExpression(*create_literal_1(fContext, type), out); |
| 2475 | SpvId result = this->writeBinaryOperation(type, type, lv->load(out), one, |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2476 | SpvOpFAdd, SpvOpIAdd, SpvOpIAdd, SpvOpUndef, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2477 | out); |
| 2478 | lv->store(result, out); |
| 2479 | return result; |
| 2480 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2481 | case Token::Kind::TK_MINUSMINUS: { |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2482 | std::unique_ptr<LValue> lv = this->getLValue(*p.operand(), out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2483 | SpvId one = this->writeExpression(*create_literal_1(fContext, type), out); |
| 2484 | SpvId result = this->writeBinaryOperation(type, type, lv->load(out), one, SpvOpFSub, |
| 2485 | SpvOpISub, SpvOpISub, SpvOpUndef, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2486 | lv->store(result, out); |
| 2487 | return result; |
| 2488 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2489 | case Token::Kind::TK_LOGICALNOT: { |
John Stiles | 4a7dc46 | 2020-11-25 11:08:08 -0500 | [diff] [blame] | 2490 | SkASSERT(p.operand()->type().isBoolean()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2491 | SpvId result = this->nextId(); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2492 | this->writeInstruction(SpvOpLogicalNot, this->getType(type), result, |
| 2493 | this->writeExpression(*p.operand(), out), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2494 | return result; |
| 2495 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2496 | case Token::Kind::TK_BITWISENOT: { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2497 | SpvId result = this->nextId(); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2498 | this->writeInstruction(SpvOpNot, this->getType(type), result, |
| 2499 | this->writeExpression(*p.operand(), out), out); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2500 | return result; |
| 2501 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2502 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2503 | #ifdef SK_DEBUG |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2504 | ABORT("unsupported prefix expression: %s", p.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2505 | #endif |
| 2506 | return -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2507 | } |
| 2508 | } |
| 2509 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2510 | SpvId SPIRVCodeGenerator::writePostfixExpression(const PostfixExpression& p, OutputStream& out) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2511 | const Type& type = p.type(); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2512 | std::unique_ptr<LValue> lv = this->getLValue(*p.operand(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2513 | SpvId result = lv->load(out); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2514 | SpvId one = this->writeExpression(*create_literal_1(fContext, type), out); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 2515 | switch (p.getOperator()) { |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2516 | case Token::Kind::TK_PLUSPLUS: { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2517 | SpvId temp = this->writeBinaryOperation(type, type, result, one, SpvOpFAdd, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2518 | SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out); |
| 2519 | lv->store(temp, out); |
| 2520 | return result; |
| 2521 | } |
Ethan Nicholas | 5a9e7fb | 2020-04-17 12:45:51 -0400 | [diff] [blame] | 2522 | case Token::Kind::TK_MINUSMINUS: { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2523 | SpvId temp = this->writeBinaryOperation(type, type, result, one, SpvOpFSub, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2524 | SpvOpISub, SpvOpISub, SpvOpUndef, out); |
| 2525 | lv->store(temp, out); |
| 2526 | return result; |
| 2527 | } |
| 2528 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2529 | #ifdef SK_DEBUG |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2530 | ABORT("unsupported postfix expression %s", p.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2531 | #endif |
| 2532 | return -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2533 | } |
| 2534 | } |
| 2535 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 2536 | SpvId SPIRVCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 2537 | if (b.value()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2538 | if (fBoolTrue == 0) { |
| 2539 | fBoolTrue = this->nextId(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2540 | this->writeInstruction(SpvOpConstantTrue, this->getType(b.type()), fBoolTrue, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2541 | fConstantBuffer); |
| 2542 | } |
| 2543 | return fBoolTrue; |
| 2544 | } else { |
| 2545 | if (fBoolFalse == 0) { |
| 2546 | fBoolFalse = this->nextId(); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2547 | this->writeInstruction(SpvOpConstantFalse, this->getType(b.type()), fBoolFalse, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2548 | fConstantBuffer); |
| 2549 | } |
| 2550 | return fBoolFalse; |
| 2551 | } |
| 2552 | } |
| 2553 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 2554 | SpvId SPIRVCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2555 | const Type& type = i.type(); |
| 2556 | ConstantType constantType; |
John Stiles | fd41d87 | 2020-11-25 22:39:45 -0500 | [diff] [blame] | 2557 | if (type == *fContext.fInt_Type || type.typeKind() == Type::TypeKind::kEnum) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2558 | constantType = ConstantType::kInt; |
| 2559 | } else if (type == *fContext.fUInt_Type) { |
| 2560 | constantType = ConstantType::kUInt; |
| 2561 | } else if (type == *fContext.fShort_Type || type == *fContext.fByte_Type) { |
| 2562 | constantType = ConstantType::kShort; |
| 2563 | } else if (type == *fContext.fUShort_Type || type == *fContext.fUByte_Type) { |
| 2564 | constantType = ConstantType::kUShort; |
Ethan Nicholas | c2d84bf | 2019-09-09 10:49:15 -0400 | [diff] [blame] | 2565 | } else { |
| 2566 | SkASSERT(false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2567 | } |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 2568 | std::pair<ConstantValue, ConstantType> key(i.value(), constantType); |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2569 | auto entry = fNumberConstants.find(key); |
| 2570 | if (entry == fNumberConstants.end()) { |
| 2571 | SpvId result = this->nextId(); |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 2572 | this->writeInstruction(SpvOpConstant, this->getType(type), result, (SpvId) i.value(), |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2573 | fConstantBuffer); |
| 2574 | fNumberConstants[key] = result; |
| 2575 | return result; |
| 2576 | } |
| 2577 | return entry->second; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2578 | } |
| 2579 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 2580 | SpvId SPIRVCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2581 | const Type& type = f.type(); |
| 2582 | ConstantType constantType; |
| 2583 | if (type == *fContext.fHalf_Type) { |
| 2584 | constantType = ConstantType::kHalf; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2585 | } else { |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2586 | constantType = ConstantType::kFloat; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2587 | } |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 2588 | float value = (float) f.value(); |
| 2589 | std::pair<ConstantValue, ConstantType> key(f.value(), constantType); |
John Stiles | 8c57866 | 2020-06-01 15:32:47 +0000 | [diff] [blame] | 2590 | auto entry = fNumberConstants.find(key); |
| 2591 | if (entry == fNumberConstants.end()) { |
| 2592 | SpvId result = this->nextId(); |
| 2593 | uint32_t bits; |
| 2594 | SkASSERT(sizeof(bits) == sizeof(value)); |
| 2595 | memcpy(&bits, &value, sizeof(bits)); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 2596 | this->writeInstruction(SpvOpConstant, this->getType(type), result, bits, |
John Stiles | 8c57866 | 2020-06-01 15:32:47 +0000 | [diff] [blame] | 2597 | fConstantBuffer); |
| 2598 | fNumberConstants[key] = result; |
| 2599 | return result; |
| 2600 | } |
| 2601 | return entry->second; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2602 | } |
| 2603 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2604 | SpvId SPIRVCodeGenerator::writeFunctionStart(const FunctionDeclaration& f, OutputStream& out) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2605 | SpvId result = fFunctionMap[&f]; |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2606 | this->writeInstruction(SpvOpFunction, this->getType(f.returnType()), result, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2607 | SpvFunctionControlMaskNone, this->getFunctionType(f), out); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 2608 | this->writeInstruction(SpvOpName, result, f.name(), fNameBuffer); |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 2609 | const std::vector<const Variable*>& parameters = f.parameters(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2610 | for (size_t i = 0; i < parameters.size(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2611 | SpvId id = this->nextId(); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2612 | fVariableMap[parameters[i]] = id; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2613 | SpvId type; |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 2614 | type = this->getPointerType(parameters[i]->type(), SpvStorageClassFunction); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2615 | this->writeInstruction(SpvOpFunctionParameter, type, id, out); |
| 2616 | } |
| 2617 | return result; |
| 2618 | } |
| 2619 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2620 | SpvId SPIRVCodeGenerator::writeFunction(const FunctionDefinition& f, OutputStream& out) { |
| 2621 | fVariableBuffer.reset(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 2622 | SpvId result = this->writeFunctionStart(f.declaration(), out); |
Ethan Nicholas | 7fb3936 | 2020-12-16 15:25:19 -0500 | [diff] [blame] | 2623 | fCurrentBlock = 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2624 | this->writeLabel(this->nextId(), out); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2625 | StringStream bodyBuffer; |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 2626 | this->writeBlock((Block&) *f.body(), bodyBuffer); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2627 | write_stringstream(fVariableBuffer, out); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 2628 | if (f.declaration().name() == "main") { |
Ethan Nicholas | 8eb64d3 | 2018-12-21 14:50:42 -0500 | [diff] [blame] | 2629 | write_stringstream(fGlobalInitializersBuffer, out); |
| 2630 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2631 | write_stringstream(bodyBuffer, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2632 | if (fCurrentBlock) { |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 2633 | if (f.declaration().returnType() == *fContext.fVoid_Type) { |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 2634 | this->writeInstruction(SpvOpReturn, out); |
| 2635 | } else { |
| 2636 | this->writeInstruction(SpvOpUnreachable, out); |
| 2637 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2638 | } |
| 2639 | this->writeInstruction(SpvOpFunctionEnd, out); |
| 2640 | return result; |
| 2641 | } |
| 2642 | |
| 2643 | void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target) { |
| 2644 | if (layout.fLocation >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2645 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationLocation, layout.fLocation, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2646 | fDecorationBuffer); |
| 2647 | } |
| 2648 | if (layout.fBinding >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2649 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationBinding, layout.fBinding, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2650 | fDecorationBuffer); |
| 2651 | } |
| 2652 | if (layout.fIndex >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2653 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationIndex, layout.fIndex, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2654 | fDecorationBuffer); |
| 2655 | } |
| 2656 | if (layout.fSet >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2657 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationDescriptorSet, layout.fSet, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2658 | fDecorationBuffer); |
| 2659 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2660 | if (layout.fInputAttachmentIndex >= 0) { |
| 2661 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationInputAttachmentIndex, |
| 2662 | layout.fInputAttachmentIndex, fDecorationBuffer); |
Ethan Nicholas | be1099f | 2018-01-11 16:09:05 -0500 | [diff] [blame] | 2663 | fCapabilities |= (((uint64_t) 1) << SpvCapabilityInputAttachment); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2664 | } |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 2665 | if (layout.fBuiltin >= 0 && layout.fBuiltin != SK_FRAGCOLOR_BUILTIN && |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 2666 | layout.fBuiltin != SK_IN_BUILTIN && layout.fBuiltin != SK_OUT_BUILTIN) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2667 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationBuiltIn, layout.fBuiltin, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2668 | fDecorationBuffer); |
| 2669 | } |
| 2670 | } |
| 2671 | |
| 2672 | void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target, int member) { |
| 2673 | if (layout.fLocation >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2674 | this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationLocation, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2675 | layout.fLocation, fDecorationBuffer); |
| 2676 | } |
| 2677 | if (layout.fBinding >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2678 | this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBinding, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2679 | layout.fBinding, fDecorationBuffer); |
| 2680 | } |
| 2681 | if (layout.fIndex >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2682 | this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationIndex, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2683 | layout.fIndex, fDecorationBuffer); |
| 2684 | } |
| 2685 | if (layout.fSet >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2686 | this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationDescriptorSet, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2687 | layout.fSet, fDecorationBuffer); |
| 2688 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2689 | if (layout.fInputAttachmentIndex >= 0) { |
| 2690 | this->writeInstruction(SpvOpDecorate, target, member, SpvDecorationInputAttachmentIndex, |
| 2691 | layout.fInputAttachmentIndex, fDecorationBuffer); |
| 2692 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2693 | if (layout.fBuiltin >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2694 | this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBuiltIn, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2695 | layout.fBuiltin, fDecorationBuffer); |
| 2696 | } |
| 2697 | } |
| 2698 | |
Ethan Nicholas | 81d1511 | 2018-07-13 12:48:50 -0400 | [diff] [blame] | 2699 | static void update_sk_in_count(const Modifiers& m, int* outSkInCount) { |
| 2700 | switch (m.fLayout.fPrimitive) { |
| 2701 | case Layout::kPoints_Primitive: |
| 2702 | *outSkInCount = 1; |
| 2703 | break; |
| 2704 | case Layout::kLines_Primitive: |
| 2705 | *outSkInCount = 2; |
| 2706 | break; |
| 2707 | case Layout::kLinesAdjacency_Primitive: |
| 2708 | *outSkInCount = 4; |
| 2709 | break; |
| 2710 | case Layout::kTriangles_Primitive: |
| 2711 | *outSkInCount = 3; |
| 2712 | break; |
| 2713 | case Layout::kTrianglesAdjacency_Primitive: |
| 2714 | *outSkInCount = 6; |
| 2715 | break; |
| 2716 | default: |
| 2717 | return; |
| 2718 | } |
| 2719 | } |
| 2720 | |
Stephen White | 8857497 | 2020-06-23 19:09:29 -0400 | [diff] [blame] | 2721 | SpvId SPIRVCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight) { |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 2722 | bool isBuffer = ((intf.variable().modifiers().fFlags & Modifiers::kBuffer_Flag) != 0); |
| 2723 | bool pushConstant = ((intf.variable().modifiers().fLayout.fFlags & |
| 2724 | Layout::kPushConstant_Flag) != 0); |
Ethan Nicholas | 8d2ba44 | 2018-03-16 16:40:36 -0400 | [diff] [blame] | 2725 | MemoryLayout memoryLayout = (pushConstant || isBuffer) ? |
| 2726 | MemoryLayout(MemoryLayout::k430_Standard) : |
| 2727 | fDefaultLayout; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2728 | SpvId result = this->nextId(); |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 2729 | std::unique_ptr<Type> rtHeightStructType; |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 2730 | const Type* type = &intf.variable().type(); |
John Stiles | 21f5f45 | 2020-11-30 09:57:59 -0500 | [diff] [blame] | 2731 | if (!MemoryLayout::LayoutIsSupported(*type)) { |
John Stiles | 0023c0c | 2020-11-16 13:32:18 -0500 | [diff] [blame] | 2732 | fErrors.error(type->fOffset, "type '" + type->name() + "' is not permitted here"); |
| 2733 | return this->nextId(); |
| 2734 | } |
Jim Van Verth | f3ec983 | 2020-10-21 16:09:57 -0400 | [diff] [blame] | 2735 | Modifiers intfModifiers = intf.variable().modifiers(); |
| 2736 | SpvStorageClass_ storageClass = get_storage_class(intfModifiers); |
Stephen White | 8857497 | 2020-06-23 19:09:29 -0400 | [diff] [blame] | 2737 | if (fProgram.fInputs.fRTHeight && appendRTHeight) { |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 2738 | SkASSERT(fRTHeightStructId == (SpvId) -1); |
| 2739 | SkASSERT(fRTHeightFieldIndex == (SpvId) -1); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 2740 | std::vector<Type::Field> fields = type->fields(); |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 2741 | fRTHeightStructId = result; |
| 2742 | fRTHeightFieldIndex = fields.size(); |
Jim Van Verth | f3ec983 | 2020-10-21 16:09:57 -0400 | [diff] [blame] | 2743 | fRTHeightStorageClass = storageClass; |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 2744 | fields.emplace_back(Modifiers(), StringFragment(SKSL_RTHEIGHT_NAME), |
| 2745 | fContext.fFloat_Type.get()); |
| 2746 | rtHeightStructType = Type::MakeStructType(type->fOffset, type->name(), std::move(fields)); |
| 2747 | type = rtHeightStructType.get(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 2748 | } |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 2749 | SpvId typeId; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2750 | if (intfModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) { |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 2751 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 2752 | if (e->is<ModifiersDeclaration>()) { |
Ethan Nicholas | 077050b | 2020-10-13 10:30:20 -0400 | [diff] [blame] | 2753 | const Modifiers& m = e->as<ModifiersDeclaration>().modifiers(); |
Ethan Nicholas | 81d1511 | 2018-07-13 12:48:50 -0400 | [diff] [blame] | 2754 | update_sk_in_count(m, &fSkInCount); |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 2755 | } |
| 2756 | } |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 2757 | typeId = this->getType( |
| 2758 | *Type::MakeArrayType("sk_in", intf.variable().type().componentType(), fSkInCount), |
| 2759 | memoryLayout); |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 2760 | } else { |
| 2761 | typeId = this->getType(*type, memoryLayout); |
| 2762 | } |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2763 | if (intfModifiers.fFlags & Modifiers::kBuffer_Flag) { |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 2764 | this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBufferBlock, fDecorationBuffer); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2765 | } else if (intfModifiers.fLayout.fBuiltin == -1) { |
Ethan Nicholas | 6ac8d36 | 2019-01-22 21:43:55 +0000 | [diff] [blame] | 2766 | this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBlock, fDecorationBuffer); |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 2767 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2768 | SpvId ptrType = this->nextId(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 2769 | this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, typeId, fConstantBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2770 | this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConstantBuffer); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2771 | Layout layout = intfModifiers.fLayout; |
| 2772 | if (intfModifiers.fFlags & Modifiers::kUniform_Flag && layout.fSet == -1) { |
Ethan Nicholas | 8d2ba44 | 2018-03-16 16:40:36 -0400 | [diff] [blame] | 2773 | layout.fSet = 0; |
| 2774 | } |
| 2775 | this->writeLayout(layout, result); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 2776 | fVariableMap[&intf.variable()] = result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2777 | return result; |
| 2778 | } |
| 2779 | |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 2780 | void SPIRVCodeGenerator::writePrecisionModifier(const Type& type, SpvId id) { |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 2781 | this->writePrecisionModifier(type.highPrecision() ? Precision::kHigh : Precision::kLow, id); |
| 2782 | } |
| 2783 | |
| 2784 | void SPIRVCodeGenerator::writePrecisionModifier(Precision precision, SpvId id) { |
| 2785 | if (precision == Precision::kLow) { |
Ethan Nicholas | a51d713 | 2017-06-09 10:47:31 -0400 | [diff] [blame] | 2786 | this->writeInstruction(SpvOpDecorate, id, SpvDecorationRelaxedPrecision, fDecorationBuffer); |
| 2787 | } |
| 2788 | } |
| 2789 | |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 2790 | bool is_dead(const Variable& var, const ProgramUsage* usage) { |
| 2791 | ProgramUsage::VariableCounts counts = usage->get(var); |
| 2792 | if (counts.fRead || counts.fWrite) { |
Chris Dalton | 2284aab | 2019-11-15 11:02:24 -0700 | [diff] [blame] | 2793 | return false; |
| 2794 | } |
| 2795 | // not entirely sure what the rules are for when it's safe to elide interface variables, but it |
| 2796 | // causes various problems to elide some of them even when dead. But it also causes problems |
| 2797 | // *not* to elide sk_SampleMask when it's not being used. |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2798 | if (!(var.modifiers().fFlags & (Modifiers::kIn_Flag | |
| 2799 | Modifiers::kOut_Flag | |
| 2800 | Modifiers::kUniform_Flag | |
| 2801 | Modifiers::kBuffer_Flag))) { |
Chris Dalton | 2284aab | 2019-11-15 11:02:24 -0700 | [diff] [blame] | 2802 | return true; |
| 2803 | } |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2804 | return var.modifiers().fLayout.fBuiltin == SK_SAMPLEMASK_BUILTIN; |
Chris Dalton | 2284aab | 2019-11-15 11:02:24 -0700 | [diff] [blame] | 2805 | } |
| 2806 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2807 | #define BUILTIN_IGNORE 9999 |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2808 | void SPIRVCodeGenerator::writeGlobalVar(Program::Kind kind, const VarDeclaration& varDecl, |
| 2809 | OutputStream& out) { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2810 | const Variable& var = varDecl.var(); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2811 | // These haven't been implemented in our SPIR-V generator yet and we only currently use them |
| 2812 | // in the OpenGL backend. |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2813 | SkASSERT(!(var.modifiers().fFlags & (Modifiers::kReadOnly_Flag | |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 2814 | Modifiers::kWriteOnly_Flag | |
| 2815 | Modifiers::kCoherent_Flag | |
| 2816 | Modifiers::kVolatile_Flag | |
| 2817 | Modifiers::kRestrict_Flag))); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2818 | if (var.modifiers().fLayout.fBuiltin == BUILTIN_IGNORE) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2819 | return; |
| 2820 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2821 | if (var.modifiers().fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN && |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2822 | kind != Program::kFragment_Kind) { |
| 2823 | SkASSERT(!fProgram.fSettings.fFragColorIsInOut); |
| 2824 | return; |
| 2825 | } |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 2826 | if (is_dead(var, fProgram.fUsage.get())) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2827 | return; |
| 2828 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2829 | const Type& type = var.type(); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2830 | SpvStorageClass_ storageClass; |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2831 | if (var.modifiers().fFlags & Modifiers::kIn_Flag) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2832 | storageClass = SpvStorageClassInput; |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2833 | } else if (var.modifiers().fFlags & Modifiers::kOut_Flag) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2834 | storageClass = SpvStorageClassOutput; |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2835 | } else if (var.modifiers().fFlags & Modifiers::kUniform_Flag) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2836 | if (type.typeKind() == Type::TypeKind::kSampler || |
| 2837 | type.typeKind() == Type::TypeKind::kSeparateSampler || |
| 2838 | type.typeKind() == Type::TypeKind::kTexture) { |
| 2839 | storageClass = SpvStorageClassUniformConstant; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2840 | } else { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2841 | storageClass = SpvStorageClassUniform; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2842 | } |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2843 | } else { |
| 2844 | storageClass = SpvStorageClassPrivate; |
| 2845 | } |
| 2846 | SpvId id = this->nextId(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2847 | fVariableMap[&var] = id; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2848 | SpvId typeId; |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2849 | if (var.modifiers().fLayout.fBuiltin == SK_IN_BUILTIN) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2850 | typeId = this->getPointerType( |
John Stiles | ad2d494 | 2020-12-11 16:55:58 -0500 | [diff] [blame] | 2851 | *Type::MakeArrayType("sk_in", type.componentType(), fSkInCount), |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2852 | storageClass); |
| 2853 | } else { |
| 2854 | typeId = this->getPointerType(type, storageClass); |
| 2855 | } |
| 2856 | this->writeInstruction(SpvOpVariable, typeId, id, storageClass, fConstantBuffer); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2857 | this->writeInstruction(SpvOpName, id, var.name(), fNameBuffer); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2858 | this->writePrecisionModifier(type, id); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2859 | if (varDecl.value()) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2860 | SkASSERT(!fCurrentBlock); |
| 2861 | fCurrentBlock = -1; |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2862 | SpvId value = this->writeExpression(*varDecl.value(), fGlobalInitializersBuffer); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2863 | this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuffer); |
| 2864 | fCurrentBlock = 0; |
| 2865 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2866 | this->writeLayout(var.modifiers().fLayout, id); |
| 2867 | if (var.modifiers().fFlags & Modifiers::kFlat_Flag) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2868 | this->writeInstruction(SpvOpDecorate, id, SpvDecorationFlat, fDecorationBuffer); |
| 2869 | } |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2870 | if (var.modifiers().fFlags & Modifiers::kNoPerspective_Flag) { |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2871 | this->writeInstruction(SpvOpDecorate, id, SpvDecorationNoPerspective, |
| 2872 | fDecorationBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2873 | } |
| 2874 | } |
| 2875 | |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2876 | void SPIRVCodeGenerator::writeVarDeclaration(const VarDeclaration& varDecl, OutputStream& out) { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2877 | const Variable& var = varDecl.var(); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2878 | // These haven't been implemented in our SPIR-V generator yet and we only currently use them |
| 2879 | // in the OpenGL backend. |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2880 | SkASSERT(!(var.modifiers().fFlags & (Modifiers::kReadOnly_Flag | |
| 2881 | Modifiers::kWriteOnly_Flag | |
| 2882 | Modifiers::kCoherent_Flag | |
| 2883 | Modifiers::kVolatile_Flag | |
| 2884 | Modifiers::kRestrict_Flag))); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2885 | SpvId id = this->nextId(); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2886 | fVariableMap[&var] = id; |
| 2887 | SpvId type = this->getPointerType(var.type(), SpvStorageClassFunction); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2888 | this->writeInstruction(SpvOpVariable, type, id, SpvStorageClassFunction, fVariableBuffer); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 2889 | this->writeInstruction(SpvOpName, id, var.name(), fNameBuffer); |
| 2890 | if (varDecl.value()) { |
| 2891 | SpvId value = this->writeExpression(*varDecl.value(), out); |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2892 | this->writeInstruction(SpvOpStore, id, value, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2893 | } |
| 2894 | } |
| 2895 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2896 | void SPIRVCodeGenerator::writeStatement(const Statement& s, OutputStream& out) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2897 | switch (s.kind()) { |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 2898 | case Statement::Kind::kInlineMarker: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2899 | case Statement::Kind::kNop: |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 2900 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2901 | case Statement::Kind::kBlock: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2902 | this->writeBlock((Block&) s, out); |
| 2903 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2904 | case Statement::Kind::kExpression: |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 2905 | this->writeExpression(*s.as<ExpressionStatement>().expression(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2906 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2907 | case Statement::Kind::kReturn: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 2908 | this->writeReturnStatement(s.as<ReturnStatement>(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2909 | break; |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 2910 | case Statement::Kind::kVarDeclaration: |
| 2911 | this->writeVarDeclaration(s.as<VarDeclaration>(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2912 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2913 | case Statement::Kind::kIf: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 2914 | this->writeIfStatement(s.as<IfStatement>(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2915 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2916 | case Statement::Kind::kFor: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 2917 | this->writeForStatement(s.as<ForStatement>(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2918 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2919 | case Statement::Kind::kDo: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 2920 | this->writeDoStatement(s.as<DoStatement>(), out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 2921 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2922 | case Statement::Kind::kSwitch: |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 2923 | this->writeSwitchStatement(s.as<SwitchStatement>(), out); |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 2924 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2925 | case Statement::Kind::kBreak: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2926 | this->writeInstruction(SpvOpBranch, fBreakTarget.top(), out); |
| 2927 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2928 | case Statement::Kind::kContinue: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2929 | this->writeInstruction(SpvOpBranch, fContinueTarget.top(), out); |
| 2930 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 2931 | case Statement::Kind::kDiscard: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2932 | this->writeInstruction(SpvOpKill, out); |
| 2933 | break; |
| 2934 | default: |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2935 | #ifdef SK_DEBUG |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2936 | ABORT("unsupported statement: %s", s.description().c_str()); |
Ethan Nicholas | 2a099da | 2020-01-02 14:40:54 -0500 | [diff] [blame] | 2937 | #endif |
| 2938 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2939 | } |
| 2940 | } |
| 2941 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2942 | void SPIRVCodeGenerator::writeBlock(const Block& b, OutputStream& out) { |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 2943 | for (const std::unique_ptr<Statement>& stmt : b.children()) { |
| 2944 | this->writeStatement(*stmt, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2945 | } |
| 2946 | } |
| 2947 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2948 | void SPIRVCodeGenerator::writeIfStatement(const IfStatement& stmt, OutputStream& out) { |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 2949 | SpvId test = this->writeExpression(*stmt.test(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2950 | SpvId ifTrue = this->nextId(); |
| 2951 | SpvId ifFalse = this->nextId(); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 2952 | if (stmt.ifFalse()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2953 | SpvId end = this->nextId(); |
| 2954 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 2955 | this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out); |
| 2956 | this->writeLabel(ifTrue, out); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 2957 | this->writeStatement(*stmt.ifTrue(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2958 | if (fCurrentBlock) { |
| 2959 | this->writeInstruction(SpvOpBranch, end, out); |
| 2960 | } |
| 2961 | this->writeLabel(ifFalse, out); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 2962 | this->writeStatement(*stmt.ifFalse(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2963 | if (fCurrentBlock) { |
| 2964 | this->writeInstruction(SpvOpBranch, end, out); |
| 2965 | } |
| 2966 | this->writeLabel(end, out); |
| 2967 | } else { |
| 2968 | this->writeInstruction(SpvOpSelectionMerge, ifFalse, SpvSelectionControlMaskNone, out); |
| 2969 | this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out); |
| 2970 | this->writeLabel(ifTrue, out); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 2971 | this->writeStatement(*stmt.ifTrue(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2972 | if (fCurrentBlock) { |
| 2973 | this->writeInstruction(SpvOpBranch, ifFalse, out); |
| 2974 | } |
| 2975 | this->writeLabel(ifFalse, out); |
| 2976 | } |
| 2977 | } |
| 2978 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2979 | void SPIRVCodeGenerator::writeForStatement(const ForStatement& f, OutputStream& out) { |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 2980 | if (f.initializer()) { |
| 2981 | this->writeStatement(*f.initializer(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2982 | } |
| 2983 | SpvId header = this->nextId(); |
| 2984 | SpvId start = this->nextId(); |
| 2985 | SpvId body = this->nextId(); |
| 2986 | SpvId next = this->nextId(); |
| 2987 | fContinueTarget.push(next); |
| 2988 | SpvId end = this->nextId(); |
| 2989 | fBreakTarget.push(end); |
| 2990 | this->writeInstruction(SpvOpBranch, header, out); |
| 2991 | this->writeLabel(header, out); |
| 2992 | this->writeInstruction(SpvOpLoopMerge, end, next, SpvLoopControlMaskNone, out); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 2993 | this->writeInstruction(SpvOpBranch, start, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2994 | this->writeLabel(start, out); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 2995 | if (f.test()) { |
| 2996 | SpvId test = this->writeExpression(*f.test(), out); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 2997 | this->writeInstruction(SpvOpBranchConditional, test, body, end, out); |
Ethan Nicholas | 7fb3936 | 2020-12-16 15:25:19 -0500 | [diff] [blame] | 2998 | } else { |
| 2999 | this->writeInstruction(SpvOpBranch, body, out); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 3000 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3001 | this->writeLabel(body, out); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 3002 | this->writeStatement(*f.statement(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3003 | if (fCurrentBlock) { |
| 3004 | this->writeInstruction(SpvOpBranch, next, out); |
| 3005 | } |
| 3006 | this->writeLabel(next, out); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 3007 | if (f.next()) { |
| 3008 | this->writeExpression(*f.next(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3009 | } |
| 3010 | this->writeInstruction(SpvOpBranch, header, out); |
| 3011 | this->writeLabel(end, out); |
| 3012 | fBreakTarget.pop(); |
| 3013 | fContinueTarget.pop(); |
| 3014 | } |
| 3015 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3016 | void SPIRVCodeGenerator::writeDoStatement(const DoStatement& d, OutputStream& out) { |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 3017 | SpvId header = this->nextId(); |
| 3018 | SpvId start = this->nextId(); |
| 3019 | SpvId next = this->nextId(); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 3020 | SpvId continueTarget = this->nextId(); |
| 3021 | fContinueTarget.push(continueTarget); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 3022 | SpvId end = this->nextId(); |
| 3023 | fBreakTarget.push(end); |
| 3024 | this->writeInstruction(SpvOpBranch, header, out); |
| 3025 | this->writeLabel(header, out); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 3026 | this->writeInstruction(SpvOpLoopMerge, end, continueTarget, SpvLoopControlMaskNone, out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 3027 | this->writeInstruction(SpvOpBranch, start, out); |
| 3028 | this->writeLabel(start, out); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 3029 | this->writeStatement(*d.statement(), out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 3030 | if (fCurrentBlock) { |
| 3031 | this->writeInstruction(SpvOpBranch, next, out); |
| 3032 | } |
| 3033 | this->writeLabel(next, out); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 3034 | SpvId test = this->writeExpression(*d.test(), out); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 3035 | this->writeInstruction(SpvOpBranchConditional, test, continueTarget, end, out); |
| 3036 | this->writeLabel(continueTarget, out); |
| 3037 | this->writeInstruction(SpvOpBranch, header, out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 3038 | this->writeLabel(end, out); |
| 3039 | fBreakTarget.pop(); |
| 3040 | fContinueTarget.pop(); |
| 3041 | } |
| 3042 | |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 3043 | void SPIRVCodeGenerator::writeSwitchStatement(const SwitchStatement& s, OutputStream& out) { |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 3044 | SpvId value = this->writeExpression(*s.value(), out); |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 3045 | std::vector<SpvId> labels; |
| 3046 | SpvId end = this->nextId(); |
| 3047 | SpvId defaultLabel = end; |
| 3048 | fBreakTarget.push(end); |
| 3049 | int size = 3; |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 3050 | auto& cases = s.cases(); |
| 3051 | for (const std::unique_ptr<SwitchCase>& c : cases) { |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 3052 | SpvId label = this->nextId(); |
| 3053 | labels.push_back(label); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 3054 | if (c->value()) { |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 3055 | size += 2; |
| 3056 | } else { |
| 3057 | defaultLabel = label; |
| 3058 | } |
| 3059 | } |
| 3060 | labels.push_back(end); |
| 3061 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 3062 | this->writeOpCode(SpvOpSwitch, size, out); |
| 3063 | this->writeWord(value, out); |
| 3064 | this->writeWord(defaultLabel, out); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 3065 | for (size_t i = 0; i < cases.size(); ++i) { |
| 3066 | if (!cases[i]->value()) { |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 3067 | continue; |
| 3068 | } |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 3069 | this->writeWord(cases[i]->value()->as<IntLiteral>().value(), out); |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 3070 | this->writeWord(labels[i], out); |
| 3071 | } |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 3072 | for (size_t i = 0; i < cases.size(); ++i) { |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 3073 | this->writeLabel(labels[i], out); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 3074 | for (const auto& stmt : cases[i]->statements()) { |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 3075 | this->writeStatement(*stmt, out); |
| 3076 | } |
| 3077 | if (fCurrentBlock) { |
| 3078 | this->writeInstruction(SpvOpBranch, labels[i + 1], out); |
| 3079 | } |
| 3080 | } |
| 3081 | this->writeLabel(end, out); |
| 3082 | fBreakTarget.pop(); |
| 3083 | } |
| 3084 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3085 | void SPIRVCodeGenerator::writeReturnStatement(const ReturnStatement& r, OutputStream& out) { |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 3086 | if (r.expression()) { |
| 3087 | this->writeInstruction(SpvOpReturnValue, this->writeExpression(*r.expression(), out), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3088 | out); |
| 3089 | } else { |
| 3090 | this->writeInstruction(SpvOpReturn, out); |
| 3091 | } |
| 3092 | } |
| 3093 | |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3094 | void SPIRVCodeGenerator::writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 3095 | SkASSERT(fProgram.fKind == Program::kGeometry_Kind); |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3096 | int invocations = 1; |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 3097 | for (const ProgramElement* e : fProgram.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 3098 | if (e->is<ModifiersDeclaration>()) { |
Ethan Nicholas | 077050b | 2020-10-13 10:30:20 -0400 | [diff] [blame] | 3099 | const Modifiers& m = e->as<ModifiersDeclaration>().modifiers(); |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3100 | if (m.fFlags & Modifiers::kIn_Flag) { |
| 3101 | if (m.fLayout.fInvocations != -1) { |
| 3102 | invocations = m.fLayout.fInvocations; |
| 3103 | } |
| 3104 | SpvId input; |
| 3105 | switch (m.fLayout.fPrimitive) { |
| 3106 | case Layout::kPoints_Primitive: |
| 3107 | input = SpvExecutionModeInputPoints; |
| 3108 | break; |
| 3109 | case Layout::kLines_Primitive: |
| 3110 | input = SpvExecutionModeInputLines; |
| 3111 | break; |
| 3112 | case Layout::kLinesAdjacency_Primitive: |
| 3113 | input = SpvExecutionModeInputLinesAdjacency; |
| 3114 | break; |
| 3115 | case Layout::kTriangles_Primitive: |
| 3116 | input = SpvExecutionModeTriangles; |
| 3117 | break; |
| 3118 | case Layout::kTrianglesAdjacency_Primitive: |
| 3119 | input = SpvExecutionModeInputTrianglesAdjacency; |
| 3120 | break; |
| 3121 | default: |
| 3122 | input = 0; |
| 3123 | break; |
| 3124 | } |
Ethan Nicholas | 81d1511 | 2018-07-13 12:48:50 -0400 | [diff] [blame] | 3125 | update_sk_in_count(m, &fSkInCount); |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3126 | if (input) { |
| 3127 | this->writeInstruction(SpvOpExecutionMode, entryPoint, input, out); |
| 3128 | } |
| 3129 | } else if (m.fFlags & Modifiers::kOut_Flag) { |
| 3130 | SpvId output; |
| 3131 | switch (m.fLayout.fPrimitive) { |
| 3132 | case Layout::kPoints_Primitive: |
| 3133 | output = SpvExecutionModeOutputPoints; |
| 3134 | break; |
| 3135 | case Layout::kLineStrip_Primitive: |
| 3136 | output = SpvExecutionModeOutputLineStrip; |
| 3137 | break; |
| 3138 | case Layout::kTriangleStrip_Primitive: |
| 3139 | output = SpvExecutionModeOutputTriangleStrip; |
| 3140 | break; |
| 3141 | default: |
| 3142 | output = 0; |
| 3143 | break; |
| 3144 | } |
| 3145 | if (output) { |
| 3146 | this->writeInstruction(SpvOpExecutionMode, entryPoint, output, out); |
| 3147 | } |
| 3148 | if (m.fLayout.fMaxVertices != -1) { |
| 3149 | this->writeInstruction(SpvOpExecutionMode, entryPoint, |
| 3150 | SpvExecutionModeOutputVertices, m.fLayout.fMaxVertices, |
| 3151 | out); |
| 3152 | } |
| 3153 | } |
| 3154 | } |
| 3155 | } |
| 3156 | this->writeInstruction(SpvOpExecutionMode, entryPoint, SpvExecutionModeInvocations, |
| 3157 | invocations, out); |
| 3158 | } |
| 3159 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3160 | void SPIRVCodeGenerator::writeInstructions(const Program& program, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3161 | fGLSLExtendedInstructions = this->nextId(); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3162 | StringStream body; |
Ethan Nicholas | 8e48c1e | 2017-03-02 14:33:31 -0500 | [diff] [blame] | 3163 | std::set<SpvId> interfaceVars; |
Brian Osman | 1f8f575 | 2020-10-28 14:46:21 -0400 | [diff] [blame] | 3164 | // assign IDs to functions |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 3165 | for (const ProgramElement* e : program.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 3166 | switch (e->kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 3167 | case ProgramElement::Kind::kFunction: { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 3168 | const FunctionDefinition& f = e->as<FunctionDefinition>(); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 3169 | fFunctionMap[&f.declaration()] = this->nextId(); |
Ethan Nicholas | b6ba82c | 2018-01-17 15:21:50 -0500 | [diff] [blame] | 3170 | break; |
| 3171 | } |
Ethan Nicholas | b6ba82c | 2018-01-17 15:21:50 -0500 | [diff] [blame] | 3172 | default: |
| 3173 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3174 | } |
| 3175 | } |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 3176 | for (const ProgramElement* e : program.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 3177 | if (e->is<InterfaceBlock>()) { |
Brian Osman | 1f8f575 | 2020-10-28 14:46:21 -0400 | [diff] [blame] | 3178 | const InterfaceBlock& intf = e->as<InterfaceBlock>(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3179 | SpvId id = this->writeInterfaceBlock(intf); |
Brian Osman | 1f8f575 | 2020-10-28 14:46:21 -0400 | [diff] [blame] | 3180 | |
| 3181 | const Modifiers& modifiers = intf.variable().modifiers(); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 3182 | if (((modifiers.fFlags & Modifiers::kIn_Flag) || |
| 3183 | (modifiers.fFlags & Modifiers::kOut_Flag)) && |
| 3184 | modifiers.fLayout.fBuiltin == -1 && |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 3185 | !is_dead(intf.variable(), fProgram.fUsage.get())) { |
Ethan Nicholas | 8e48c1e | 2017-03-02 14:33:31 -0500 | [diff] [blame] | 3186 | interfaceVars.insert(id); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3187 | } |
| 3188 | } |
| 3189 | } |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 3190 | for (const ProgramElement* e : program.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 3191 | if (e->is<GlobalVarDeclaration>()) { |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 3192 | this->writeGlobalVar(program.fKind, |
| 3193 | e->as<GlobalVarDeclaration>().declaration()->as<VarDeclaration>(), |
| 3194 | body); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3195 | } |
| 3196 | } |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 3197 | for (const ProgramElement* e : program.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 3198 | if (e->is<FunctionDefinition>()) { |
| 3199 | this->writeFunction(e->as<FunctionDefinition>(), body); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3200 | } |
| 3201 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 3202 | const FunctionDeclaration* main = nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3203 | for (auto entry : fFunctionMap) { |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 3204 | if (entry.first->name() == "main") { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3205 | main = entry.first; |
| 3206 | } |
| 3207 | } |
Ethan Nicholas | 1a668d2 | 2019-04-18 10:37:40 -0400 | [diff] [blame] | 3208 | if (!main) { |
| 3209 | fErrors.error(0, "program does not contain a main() function"); |
| 3210 | return; |
| 3211 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3212 | for (auto entry : fVariableMap) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 3213 | const Variable* var = entry.first; |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 3214 | if (var->storage() == Variable::Storage::kGlobal && |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 3215 | ((var->modifiers().fFlags & Modifiers::kIn_Flag) || |
Brian Osman | 010ce6a | 2020-10-19 16:34:10 -0400 | [diff] [blame] | 3216 | (var->modifiers().fFlags & Modifiers::kOut_Flag)) && |
| 3217 | !is_dead(*var, fProgram.fUsage.get())) { |
Ethan Nicholas | 8e48c1e | 2017-03-02 14:33:31 -0500 | [diff] [blame] | 3218 | interfaceVars.insert(entry.second); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3219 | } |
| 3220 | } |
| 3221 | this->writeCapabilities(out); |
| 3222 | this->writeInstruction(SpvOpExtInstImport, fGLSLExtendedInstructions, "GLSL.std.450", out); |
| 3223 | this->writeInstruction(SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemoryModelGLSL450, out); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 3224 | this->writeOpCode(SpvOpEntryPoint, (SpvId) (3 + (main->name().fLength + 4) / 4) + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3225 | (int32_t) interfaceVars.size(), out); |
| 3226 | switch (program.fKind) { |
| 3227 | case Program::kVertex_Kind: |
| 3228 | this->writeWord(SpvExecutionModelVertex, out); |
| 3229 | break; |
| 3230 | case Program::kFragment_Kind: |
| 3231 | this->writeWord(SpvExecutionModelFragment, out); |
| 3232 | break; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 3233 | case Program::kGeometry_Kind: |
| 3234 | this->writeWord(SpvExecutionModelGeometry, out); |
| 3235 | break; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 3236 | default: |
| 3237 | ABORT("cannot write this kind of program to SPIR-V\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3238 | } |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3239 | SpvId entryPoint = fFunctionMap[main]; |
| 3240 | this->writeWord(entryPoint, out); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 3241 | this->writeString(main->name().fChars, main->name().fLength, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3242 | for (int var : interfaceVars) { |
| 3243 | this->writeWord(var, out); |
| 3244 | } |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3245 | if (program.fKind == Program::kGeometry_Kind) { |
| 3246 | this->writeGeometryShaderExecutionMode(entryPoint, out); |
| 3247 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3248 | if (program.fKind == Program::kFragment_Kind) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 3249 | this->writeInstruction(SpvOpExecutionMode, |
| 3250 | fFunctionMap[main], |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3251 | SpvExecutionModeOriginUpperLeft, |
| 3252 | out); |
| 3253 | } |
Brian Osman | 133724c | 2020-10-28 14:14:39 -0400 | [diff] [blame] | 3254 | for (const ProgramElement* e : program.elements()) { |
Brian Osman | 1179fcf | 2020-10-08 16:04:40 -0400 | [diff] [blame] | 3255 | if (e->is<Extension>()) { |
| 3256 | this->writeInstruction(SpvOpSourceExtension, e->as<Extension>().name().c_str(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3257 | } |
| 3258 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 3259 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3260 | write_stringstream(fExtraGlobalsBuffer, out); |
| 3261 | write_stringstream(fNameBuffer, out); |
| 3262 | write_stringstream(fDecorationBuffer, out); |
| 3263 | write_stringstream(fConstantBuffer, out); |
| 3264 | write_stringstream(fExternalFunctionsBuffer, out); |
| 3265 | write_stringstream(body, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3266 | } |
| 3267 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3268 | bool SPIRVCodeGenerator::generateCode() { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 3269 | SkASSERT(!fErrors.errorCount()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3270 | this->writeWord(SpvMagicNumber, *fOut); |
| 3271 | this->writeWord(SpvVersion, *fOut); |
| 3272 | this->writeWord(SKSL_MAGIC, *fOut); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3273 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3274 | this->writeInstructions(fProgram, buffer); |
| 3275 | this->writeWord(fIdCount, *fOut); |
| 3276 | this->writeWord(0, *fOut); // reserved, always zero |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3277 | write_stringstream(buffer, *fOut); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3278 | return 0 == fErrors.errorCount(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3279 | } |
| 3280 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 3281 | } // namespace SkSL |