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