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