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