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