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