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