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