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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/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" |
| 13 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 14 | #include "src/sksl/ir/SkSLExtension.h" |
| 15 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 16 | #include "src/sksl/ir/SkSLVariableReference.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 17 | |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 18 | #ifdef SK_VULKAN |
| 19 | #include "src/gpu/vk/GrVkCaps.h" |
| 20 | #endif |
| 21 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 22 | namespace SkSL { |
| 23 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 24 | static const int32_t SKSL_MAGIC = 0x0; // FIXME: we should probably register a magic number |
| 25 | |
| 26 | void SPIRVCodeGenerator::setupIntrinsics() { |
| 27 | #define ALL_GLSL(x) std::make_tuple(kGLSL_STD_450_IntrinsicKind, GLSLstd450 ## x, GLSLstd450 ## x, \ |
| 28 | GLSLstd450 ## x, GLSLstd450 ## x) |
| 29 | #define BY_TYPE_GLSL(ifFloat, ifInt, ifUInt) std::make_tuple(kGLSL_STD_450_IntrinsicKind, \ |
| 30 | GLSLstd450 ## ifFloat, \ |
| 31 | GLSLstd450 ## ifInt, \ |
| 32 | GLSLstd450 ## ifUInt, \ |
| 33 | SpvOpUndef) |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 34 | #define ALL_SPIRV(x) std::make_tuple(kSPIRV_IntrinsicKind, SpvOp ## x, SpvOp ## x, SpvOp ## x, \ |
| 35 | SpvOp ## x) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 36 | #define SPECIAL(x) std::make_tuple(kSpecial_IntrinsicKind, k ## x ## _SpecialIntrinsic, \ |
| 37 | k ## x ## _SpecialIntrinsic, k ## x ## _SpecialIntrinsic, \ |
| 38 | k ## x ## _SpecialIntrinsic) |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 39 | fIntrinsicMap[String("round")] = ALL_GLSL(Round); |
| 40 | fIntrinsicMap[String("roundEven")] = ALL_GLSL(RoundEven); |
| 41 | fIntrinsicMap[String("trunc")] = ALL_GLSL(Trunc); |
| 42 | fIntrinsicMap[String("abs")] = BY_TYPE_GLSL(FAbs, SAbs, SAbs); |
| 43 | fIntrinsicMap[String("sign")] = BY_TYPE_GLSL(FSign, SSign, SSign); |
| 44 | fIntrinsicMap[String("floor")] = ALL_GLSL(Floor); |
| 45 | fIntrinsicMap[String("ceil")] = ALL_GLSL(Ceil); |
| 46 | fIntrinsicMap[String("fract")] = ALL_GLSL(Fract); |
| 47 | fIntrinsicMap[String("radians")] = ALL_GLSL(Radians); |
| 48 | fIntrinsicMap[String("degrees")] = ALL_GLSL(Degrees); |
| 49 | fIntrinsicMap[String("sin")] = ALL_GLSL(Sin); |
| 50 | fIntrinsicMap[String("cos")] = ALL_GLSL(Cos); |
| 51 | fIntrinsicMap[String("tan")] = ALL_GLSL(Tan); |
| 52 | fIntrinsicMap[String("asin")] = ALL_GLSL(Asin); |
| 53 | fIntrinsicMap[String("acos")] = ALL_GLSL(Acos); |
| 54 | fIntrinsicMap[String("atan")] = SPECIAL(Atan); |
| 55 | fIntrinsicMap[String("sinh")] = ALL_GLSL(Sinh); |
| 56 | fIntrinsicMap[String("cosh")] = ALL_GLSL(Cosh); |
| 57 | fIntrinsicMap[String("tanh")] = ALL_GLSL(Tanh); |
| 58 | fIntrinsicMap[String("asinh")] = ALL_GLSL(Asinh); |
| 59 | fIntrinsicMap[String("acosh")] = ALL_GLSL(Acosh); |
| 60 | fIntrinsicMap[String("atanh")] = ALL_GLSL(Atanh); |
| 61 | fIntrinsicMap[String("pow")] = ALL_GLSL(Pow); |
| 62 | fIntrinsicMap[String("exp")] = ALL_GLSL(Exp); |
| 63 | fIntrinsicMap[String("log")] = ALL_GLSL(Log); |
| 64 | fIntrinsicMap[String("exp2")] = ALL_GLSL(Exp2); |
| 65 | fIntrinsicMap[String("log2")] = ALL_GLSL(Log2); |
| 66 | fIntrinsicMap[String("sqrt")] = ALL_GLSL(Sqrt); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 67 | fIntrinsicMap[String("inverse")] = ALL_GLSL(MatrixInverse); |
| 68 | fIntrinsicMap[String("transpose")] = ALL_SPIRV(Transpose); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 69 | fIntrinsicMap[String("inversesqrt")] = ALL_GLSL(InverseSqrt); |
| 70 | fIntrinsicMap[String("determinant")] = ALL_GLSL(Determinant); |
| 71 | fIntrinsicMap[String("matrixInverse")] = ALL_GLSL(MatrixInverse); |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 72 | fIntrinsicMap[String("mod")] = SPECIAL(Mod); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 73 | fIntrinsicMap[String("min")] = SPECIAL(Min); |
| 74 | fIntrinsicMap[String("max")] = SPECIAL(Max); |
| 75 | fIntrinsicMap[String("clamp")] = SPECIAL(Clamp); |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 76 | fIntrinsicMap[String("saturate")] = SPECIAL(Saturate); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 77 | fIntrinsicMap[String("dot")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDot, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 78 | SpvOpUndef, SpvOpUndef, SpvOpUndef); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 79 | fIntrinsicMap[String("mix")] = SPECIAL(Mix); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 80 | fIntrinsicMap[String("step")] = ALL_GLSL(Step); |
| 81 | fIntrinsicMap[String("smoothstep")] = ALL_GLSL(SmoothStep); |
| 82 | fIntrinsicMap[String("fma")] = ALL_GLSL(Fma); |
| 83 | fIntrinsicMap[String("frexp")] = ALL_GLSL(Frexp); |
| 84 | fIntrinsicMap[String("ldexp")] = ALL_GLSL(Ldexp); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 85 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 86 | #define PACK(type) fIntrinsicMap[String("pack" #type)] = ALL_GLSL(Pack ## type); \ |
| 87 | fIntrinsicMap[String("unpack" #type)] = ALL_GLSL(Unpack ## type) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 88 | PACK(Snorm4x8); |
| 89 | PACK(Unorm4x8); |
| 90 | PACK(Snorm2x16); |
| 91 | PACK(Unorm2x16); |
| 92 | PACK(Half2x16); |
| 93 | PACK(Double2x32); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 94 | fIntrinsicMap[String("length")] = ALL_GLSL(Length); |
| 95 | fIntrinsicMap[String("distance")] = ALL_GLSL(Distance); |
| 96 | fIntrinsicMap[String("cross")] = ALL_GLSL(Cross); |
| 97 | fIntrinsicMap[String("normalize")] = ALL_GLSL(Normalize); |
| 98 | fIntrinsicMap[String("faceForward")] = ALL_GLSL(FaceForward); |
| 99 | fIntrinsicMap[String("reflect")] = ALL_GLSL(Reflect); |
| 100 | fIntrinsicMap[String("refract")] = ALL_GLSL(Refract); |
| 101 | fIntrinsicMap[String("findLSB")] = ALL_GLSL(FindILsb); |
| 102 | fIntrinsicMap[String("findMSB")] = BY_TYPE_GLSL(FindSMsb, FindSMsb, FindUMsb); |
| 103 | fIntrinsicMap[String("dFdx")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDPdx, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 104 | SpvOpUndef, SpvOpUndef, SpvOpUndef); |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 105 | fIntrinsicMap[String("dFdy")] = SPECIAL(DFdy); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 106 | fIntrinsicMap[String("fwidth")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpFwidth, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 107 | SpvOpUndef, SpvOpUndef, SpvOpUndef); |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 108 | fIntrinsicMap[String("makeSampler2D")] = SPECIAL(SampledImage); |
| 109 | |
Ethan Nicholas | 1386366 | 2019-07-29 13:05:15 -0400 | [diff] [blame] | 110 | fIntrinsicMap[String("sample")] = SPECIAL(Texture); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 111 | fIntrinsicMap[String("subpassLoad")] = SPECIAL(SubpassLoad); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 112 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 113 | fIntrinsicMap[String("any")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 114 | SpvOpUndef, SpvOpUndef, SpvOpAny); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 115 | fIntrinsicMap[String("all")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 116 | SpvOpUndef, SpvOpUndef, SpvOpAll); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 117 | fIntrinsicMap[String("equal")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 118 | SpvOpFOrdEqual, SpvOpIEqual, |
| 119 | SpvOpIEqual, SpvOpLogicalEqual); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 120 | fIntrinsicMap[String("notEqual")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 121 | SpvOpFOrdNotEqual, SpvOpINotEqual, |
| 122 | SpvOpINotEqual, |
| 123 | SpvOpLogicalNotEqual); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 124 | fIntrinsicMap[String("lessThan")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 125 | SpvOpFOrdLessThan, SpvOpSLessThan, |
| 126 | SpvOpULessThan, SpvOpUndef); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 127 | fIntrinsicMap[String("lessThanEqual")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 128 | SpvOpFOrdLessThanEqual, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 129 | SpvOpSLessThanEqual, |
| 130 | SpvOpULessThanEqual, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 131 | SpvOpUndef); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 132 | fIntrinsicMap[String("greaterThan")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 133 | SpvOpFOrdGreaterThan, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 134 | SpvOpSGreaterThan, |
| 135 | SpvOpUGreaterThan, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 136 | SpvOpUndef); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 137 | fIntrinsicMap[String("greaterThanEqual")] = std::make_tuple(kSPIRV_IntrinsicKind, |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 138 | SpvOpFOrdGreaterThanEqual, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 139 | SpvOpSGreaterThanEqual, |
| 140 | SpvOpUGreaterThanEqual, |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 141 | SpvOpUndef); |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 142 | fIntrinsicMap[String("EmitVertex")] = ALL_SPIRV(EmitVertex); |
| 143 | fIntrinsicMap[String("EndPrimitive")] = ALL_SPIRV(EndPrimitive); |
Ethan Nicholas | ffdc3e6 | 2019-09-19 16:58:33 -0400 | [diff] [blame] | 144 | fIntrinsicMap[String("unpremul")] = SPECIAL(Unpremul); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 145 | // interpolateAt* not yet supported... |
| 146 | } |
| 147 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 148 | void SPIRVCodeGenerator::writeWord(int32_t word, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 149 | out.write((const char*) &word, sizeof(word)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 150 | } |
| 151 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 152 | static bool is_float(const Context& context, const Type& type) { |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 153 | if (type.columns() > 1) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 154 | return is_float(context, type.componentType()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 155 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 156 | return type == *context.fFloat_Type || type == *context.fHalf_Type || |
| 157 | type == *context.fDouble_Type; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 158 | } |
| 159 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 160 | static bool is_signed(const Context& context, const Type& type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 161 | if (type.kind() == Type::kVector_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 162 | return is_signed(context, type.componentType()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 163 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 164 | return type == *context.fInt_Type || type == *context.fShort_Type || |
| 165 | type == *context.fByte_Type; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 166 | } |
| 167 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 168 | static bool is_unsigned(const Context& context, const Type& type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 169 | if (type.kind() == Type::kVector_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 170 | return is_unsigned(context, type.componentType()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 171 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 172 | return type == *context.fUInt_Type || type == *context.fUShort_Type || |
| 173 | type == *context.fUByte_Type; |
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_bool(const Context& context, const Type& type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 177 | if (type.kind() == Type::kVector_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 178 | return is_bool(context, type.componentType()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 179 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 180 | return type == *context.fBool_Type; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 181 | } |
| 182 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 183 | static bool is_out(const Variable& var) { |
| 184 | return (var.fModifiers.fFlags & Modifiers::kOut_Flag) != 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 187 | void SPIRVCodeGenerator::writeOpCode(SpvOp_ opCode, int length, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 188 | SkASSERT(opCode != SpvOpLoad || &out != &fConstantBuffer); |
| 189 | SkASSERT(opCode != SpvOpUndef); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 190 | switch (opCode) { |
| 191 | case SpvOpReturn: // fall through |
| 192 | case SpvOpReturnValue: // fall through |
ethannicholas | 552882f | 2016-07-07 06:30:48 -0700 | [diff] [blame] | 193 | case SpvOpKill: // fall through |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 194 | case SpvOpBranch: // fall through |
| 195 | case SpvOpBranchConditional: |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 196 | SkASSERT(fCurrentBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 197 | fCurrentBlock = 0; |
| 198 | break; |
| 199 | case SpvOpConstant: // fall through |
| 200 | case SpvOpConstantTrue: // fall through |
| 201 | case SpvOpConstantFalse: // fall through |
| 202 | case SpvOpConstantComposite: // fall through |
| 203 | case SpvOpTypeVoid: // fall through |
| 204 | case SpvOpTypeInt: // fall through |
| 205 | case SpvOpTypeFloat: // fall through |
| 206 | case SpvOpTypeBool: // fall through |
| 207 | case SpvOpTypeVector: // fall through |
| 208 | case SpvOpTypeMatrix: // fall through |
| 209 | case SpvOpTypeArray: // fall through |
| 210 | case SpvOpTypePointer: // fall through |
| 211 | case SpvOpTypeFunction: // fall through |
| 212 | case SpvOpTypeRuntimeArray: // fall through |
| 213 | case SpvOpTypeStruct: // fall through |
| 214 | case SpvOpTypeImage: // fall through |
| 215 | case SpvOpTypeSampledImage: // fall through |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 216 | case SpvOpTypeSampler: // fall through |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 217 | case SpvOpVariable: // fall through |
| 218 | case SpvOpFunction: // fall through |
| 219 | case SpvOpFunctionParameter: // fall through |
| 220 | case SpvOpFunctionEnd: // fall through |
| 221 | case SpvOpExecutionMode: // fall through |
| 222 | case SpvOpMemoryModel: // fall through |
| 223 | case SpvOpCapability: // fall through |
| 224 | case SpvOpExtInstImport: // fall through |
| 225 | case SpvOpEntryPoint: // fall through |
| 226 | case SpvOpSource: // fall through |
| 227 | case SpvOpSourceExtension: // fall through |
| 228 | case SpvOpName: // fall through |
| 229 | case SpvOpMemberName: // fall through |
| 230 | case SpvOpDecorate: // fall through |
| 231 | case SpvOpMemberDecorate: |
| 232 | break; |
| 233 | default: |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 234 | SkASSERT(fCurrentBlock); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 235 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 236 | this->writeWord((length << 16) | opCode, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 239 | void SPIRVCodeGenerator::writeLabel(SpvId label, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 240 | fCurrentBlock = label; |
| 241 | this->writeInstruction(SpvOpLabel, label, out); |
| 242 | } |
| 243 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 244 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 245 | this->writeOpCode(opCode, 1, out); |
| 246 | } |
| 247 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 248 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 249 | this->writeOpCode(opCode, 2, out); |
| 250 | this->writeWord(word1, out); |
| 251 | } |
| 252 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 253 | void SPIRVCodeGenerator::writeString(const char* string, size_t length, OutputStream& out) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 254 | out.write(string, length); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 255 | switch (length % 4) { |
| 256 | case 1: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 257 | out.write8(0); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 258 | // fall through |
| 259 | case 2: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 260 | out.write8(0); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 261 | // fall through |
| 262 | case 3: |
Ethan Nicholas | 9e1138d | 2016-11-21 10:39:35 -0500 | [diff] [blame] | 263 | out.write8(0); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 264 | break; |
| 265 | default: |
| 266 | this->writeWord(0, out); |
| 267 | } |
| 268 | } |
| 269 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 270 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out) { |
| 271 | this->writeOpCode(opCode, 1 + (string.fLength + 4) / 4, out); |
| 272 | this->writeString(string.fChars, string.fLength, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 276 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 277 | OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 278 | this->writeOpCode(opCode, 2 + (string.fLength + 4) / 4, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 279 | this->writeWord(word1, out); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 280 | this->writeString(string.fChars, string.fLength, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 283 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 284 | StringFragment string, OutputStream& out) { |
| 285 | this->writeOpCode(opCode, 3 + (string.fLength + 4) / 4, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 286 | this->writeWord(word1, out); |
| 287 | this->writeWord(word2, out); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 288 | this->writeString(string.fChars, string.fLength, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 291 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 292 | OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 293 | this->writeOpCode(opCode, 3, out); |
| 294 | this->writeWord(word1, out); |
| 295 | this->writeWord(word2, out); |
| 296 | } |
| 297 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 298 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 299 | int32_t word3, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 300 | this->writeOpCode(opCode, 4, out); |
| 301 | this->writeWord(word1, out); |
| 302 | this->writeWord(word2, out); |
| 303 | this->writeWord(word3, out); |
| 304 | } |
| 305 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 306 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 307 | int32_t word3, int32_t word4, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 308 | this->writeOpCode(opCode, 5, out); |
| 309 | this->writeWord(word1, out); |
| 310 | this->writeWord(word2, out); |
| 311 | this->writeWord(word3, out); |
| 312 | this->writeWord(word4, out); |
| 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, |
| 316 | int32_t word3, int32_t word4, int32_t word5, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 317 | OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 318 | this->writeOpCode(opCode, 6, out); |
| 319 | this->writeWord(word1, out); |
| 320 | this->writeWord(word2, out); |
| 321 | this->writeWord(word3, out); |
| 322 | this->writeWord(word4, out); |
| 323 | this->writeWord(word5, out); |
| 324 | } |
| 325 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 326 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 327 | int32_t word3, int32_t word4, int32_t word5, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 328 | int32_t word6, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 329 | this->writeOpCode(opCode, 7, out); |
| 330 | this->writeWord(word1, out); |
| 331 | this->writeWord(word2, out); |
| 332 | this->writeWord(word3, out); |
| 333 | this->writeWord(word4, out); |
| 334 | this->writeWord(word5, out); |
| 335 | this->writeWord(word6, out); |
| 336 | } |
| 337 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 338 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 339 | int32_t word3, int32_t word4, int32_t word5, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 340 | int32_t word6, int32_t word7, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 341 | this->writeOpCode(opCode, 8, out); |
| 342 | this->writeWord(word1, out); |
| 343 | this->writeWord(word2, out); |
| 344 | this->writeWord(word3, out); |
| 345 | this->writeWord(word4, out); |
| 346 | this->writeWord(word5, out); |
| 347 | this->writeWord(word6, out); |
| 348 | this->writeWord(word7, out); |
| 349 | } |
| 350 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 351 | void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 352 | int32_t word3, int32_t word4, int32_t word5, |
| 353 | int32_t word6, int32_t word7, int32_t word8, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 354 | OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 355 | this->writeOpCode(opCode, 9, out); |
| 356 | this->writeWord(word1, out); |
| 357 | this->writeWord(word2, out); |
| 358 | this->writeWord(word3, out); |
| 359 | this->writeWord(word4, out); |
| 360 | this->writeWord(word5, out); |
| 361 | this->writeWord(word6, out); |
| 362 | this->writeWord(word7, out); |
| 363 | this->writeWord(word8, out); |
| 364 | } |
| 365 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 366 | void SPIRVCodeGenerator::writeCapabilities(OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 367 | for (uint64_t i = 0, bit = 1; i <= kLast_Capability; i++, bit <<= 1) { |
| 368 | if (fCapabilities & bit) { |
| 369 | this->writeInstruction(SpvOpCapability, (SpvId) i, out); |
| 370 | } |
| 371 | } |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 372 | if (fProgram.fKind == Program::kGeometry_Kind) { |
| 373 | this->writeInstruction(SpvOpCapability, SpvCapabilityGeometry, out); |
| 374 | } |
Ethan Nicholas | 81d1511 | 2018-07-13 12:48:50 -0400 | [diff] [blame] | 375 | else { |
| 376 | this->writeInstruction(SpvOpCapability, SpvCapabilityShader, out); |
| 377 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | SpvId SPIRVCodeGenerator::nextId() { |
| 381 | return fIdCount++; |
| 382 | } |
| 383 | |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 384 | void SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& memoryLayout, |
| 385 | SpvId resultId) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 386 | this->writeInstruction(SpvOpName, resultId, type.name().c_str(), fNameBuffer); |
| 387 | // go ahead and write all of the field types, so we don't inadvertently write them while we're |
| 388 | // in the middle of writing the struct instruction |
| 389 | std::vector<SpvId> types; |
| 390 | for (const auto& f : type.fields()) { |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 391 | types.push_back(this->getType(*f.fType, memoryLayout)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 392 | } |
| 393 | this->writeOpCode(SpvOpTypeStruct, 2 + (int32_t) types.size(), fConstantBuffer); |
| 394 | this->writeWord(resultId, fConstantBuffer); |
| 395 | for (SpvId id : types) { |
| 396 | this->writeWord(id, fConstantBuffer); |
| 397 | } |
| 398 | size_t offset = 0; |
| 399 | for (int32_t i = 0; i < (int32_t) type.fields().size(); i++) { |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 400 | const Type::Field& field = type.fields()[i]; |
| 401 | size_t size = memoryLayout.size(*field.fType); |
| 402 | size_t alignment = memoryLayout.alignment(*field.fType); |
| 403 | const Layout& fieldLayout = field.fModifiers.fLayout; |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 404 | if (fieldLayout.fOffset >= 0) { |
Greg Daniel | dbd44c7 | 2017-01-24 15:12:12 -0500 | [diff] [blame] | 405 | if (fieldLayout.fOffset < (int) offset) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 406 | fErrors.error(type.fOffset, |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 407 | "offset of field '" + field.fName + "' must be at " |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 408 | "least " + to_string((int) offset)); |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 409 | } |
| 410 | if (fieldLayout.fOffset % alignment) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 411 | fErrors.error(type.fOffset, |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 412 | "offset of field '" + field.fName + "' must be a multiple" |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 413 | " of " + to_string((int) alignment)); |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 414 | } |
| 415 | offset = fieldLayout.fOffset; |
| 416 | } else { |
| 417 | size_t mod = offset % alignment; |
| 418 | if (mod) { |
| 419 | offset += alignment - mod; |
| 420 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 421 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 422 | this->writeInstruction(SpvOpMemberName, resultId, i, field.fName, fNameBuffer); |
Ethan Nicholas | 1967177 | 2016-11-28 16:30:17 -0500 | [diff] [blame] | 423 | this->writeLayout(fieldLayout, resultId, i); |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 424 | if (field.fModifiers.fLayout.fBuiltin < 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 425 | this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, SpvDecorationOffset, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 426 | (SpvId) offset, fDecorationBuffer); |
| 427 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 428 | if (field.fType->kind() == Type::kMatrix_Kind) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 429 | this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationColMajor, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 430 | fDecorationBuffer); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 431 | this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationMatrixStride, |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 432 | (SpvId) memoryLayout.stride(*field.fType), |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 433 | fDecorationBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 434 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 435 | if (!field.fType->highPrecision()) { |
| 436 | this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, |
| 437 | SpvDecorationRelaxedPrecision, fDecorationBuffer); |
| 438 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 439 | offset += size; |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 440 | Type::Kind kind = field.fType->kind(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 441 | if ((kind == Type::kArray_Kind || kind == Type::kStruct_Kind) && offset % alignment != 0) { |
| 442 | offset += alignment - offset % alignment; |
| 443 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 447 | Type SPIRVCodeGenerator::getActualType(const Type& type) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 448 | if (type.isFloat()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 449 | return *fContext.fFloat_Type; |
| 450 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 451 | if (type.isSigned()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 452 | return *fContext.fInt_Type; |
| 453 | } |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 454 | if (type.isUnsigned()) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 455 | return *fContext.fUInt_Type; |
| 456 | } |
| 457 | if (type.kind() == Type::kMatrix_Kind || type.kind() == Type::kVector_Kind) { |
| 458 | if (type.componentType() == *fContext.fHalf_Type) { |
| 459 | return fContext.fFloat_Type->toCompound(fContext, type.columns(), type.rows()); |
| 460 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 461 | if (type.componentType() == *fContext.fShort_Type || |
| 462 | type.componentType() == *fContext.fByte_Type) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 463 | return fContext.fInt_Type->toCompound(fContext, type.columns(), type.rows()); |
| 464 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 465 | if (type.componentType() == *fContext.fUShort_Type || |
| 466 | type.componentType() == *fContext.fUByte_Type) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 467 | return fContext.fUInt_Type->toCompound(fContext, type.columns(), type.rows()); |
| 468 | } |
| 469 | } |
| 470 | return type; |
| 471 | } |
| 472 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 473 | SpvId SPIRVCodeGenerator::getType(const Type& type) { |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 474 | return this->getType(type, fDefaultLayout); |
| 475 | } |
| 476 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 477 | SpvId SPIRVCodeGenerator::getType(const Type& rawType, const MemoryLayout& layout) { |
| 478 | Type type = this->getActualType(rawType); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 479 | String key = type.name() + to_string((int) layout.fStd); |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 480 | auto entry = fTypeMap.find(key); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 481 | if (entry == fTypeMap.end()) { |
| 482 | SpvId result = this->nextId(); |
| 483 | switch (type.kind()) { |
| 484 | case Type::kScalar_Kind: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 485 | if (type == *fContext.fBool_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 486 | this->writeInstruction(SpvOpTypeBool, result, fConstantBuffer); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 487 | } else if (type == *fContext.fInt_Type || type == *fContext.fShort_Type || |
| 488 | type == *fContext.fIntLiteral_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 489 | this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstantBuffer); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 490 | } else if (type == *fContext.fUInt_Type || type == *fContext.fUShort_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 491 | this->writeInstruction(SpvOpTypeInt, result, 32, 0, fConstantBuffer); |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 492 | } else if (type == *fContext.fFloat_Type || type == *fContext.fHalf_Type || |
| 493 | type == *fContext.fFloatLiteral_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 494 | this->writeInstruction(SpvOpTypeFloat, result, 32, fConstantBuffer); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 495 | } else if (type == *fContext.fDouble_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 496 | this->writeInstruction(SpvOpTypeFloat, result, 64, fConstantBuffer); |
| 497 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 498 | SkASSERT(false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 499 | } |
| 500 | break; |
| 501 | case Type::kVector_Kind: |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 502 | this->writeInstruction(SpvOpTypeVector, result, |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 503 | this->getType(type.componentType(), layout), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 504 | type.columns(), fConstantBuffer); |
| 505 | break; |
| 506 | case Type::kMatrix_Kind: |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 507 | this->writeInstruction(SpvOpTypeMatrix, result, |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 508 | this->getType(index_type(fContext, type), layout), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 509 | type.columns(), fConstantBuffer); |
| 510 | break; |
| 511 | case Type::kStruct_Kind: |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 512 | this->writeStruct(type, layout, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 513 | break; |
| 514 | case Type::kArray_Kind: { |
| 515 | if (type.columns() > 0) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 516 | IntLiteral count(fContext, -1, type.columns()); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 517 | this->writeInstruction(SpvOpTypeArray, result, |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 518 | this->getType(type.componentType(), layout), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 519 | this->writeIntLiteral(count), fConstantBuffer); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 520 | this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 521 | (int32_t) layout.stride(type), |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 522 | fDecorationBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 523 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 524 | SkASSERT(false); // we shouldn't have any runtime-sized arrays right now |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 525 | this->writeInstruction(SpvOpTypeRuntimeArray, result, |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 526 | this->getType(type.componentType(), layout), |
| 527 | fConstantBuffer); |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 528 | this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride, |
| 529 | (int32_t) layout.stride(type), |
| 530 | fDecorationBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 531 | } |
| 532 | break; |
| 533 | } |
| 534 | case Type::kSampler_Kind: { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 535 | SpvId image = result; |
| 536 | if (SpvDimSubpassData != type.dimensions()) { |
Stephen White | 792e230 | 2019-08-09 13:33:51 -0400 | [diff] [blame] | 537 | image = this->getType(type.textureType(), layout); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 538 | } |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 539 | if (SpvDimBuffer == type.dimensions()) { |
| 540 | fCapabilities |= (((uint64_t) 1) << SpvCapabilitySampledBuffer); |
| 541 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 542 | if (SpvDimSubpassData != type.dimensions()) { |
| 543 | this->writeInstruction(SpvOpTypeSampledImage, result, image, fConstantBuffer); |
| 544 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 545 | break; |
| 546 | } |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 547 | case Type::kSeparateSampler_Kind: { |
| 548 | this->writeInstruction(SpvOpTypeSampler, result, fConstantBuffer); |
| 549 | break; |
| 550 | } |
| 551 | case Type::kTexture_Kind: { |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 552 | this->writeInstruction(SpvOpTypeImage, result, |
| 553 | this->getType(*fContext.fFloat_Type, layout), |
Stephen White | 792e230 | 2019-08-09 13:33:51 -0400 | [diff] [blame] | 554 | type.dimensions(), type.isDepth(), type.isArrayed(), |
| 555 | type.isMultisampled(), type.isSampled() ? 1 : 2, |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 556 | SpvImageFormatUnknown, fConstantBuffer); |
| 557 | fImageTypeMap[key] = result; |
| 558 | break; |
| 559 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 560 | default: |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 561 | if (type == *fContext.fVoid_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 562 | this->writeInstruction(SpvOpTypeVoid, result, fConstantBuffer); |
| 563 | } else { |
| 564 | ABORT("invalid type: %s", type.description().c_str()); |
| 565 | } |
| 566 | } |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 567 | fTypeMap[key] = result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 568 | return result; |
| 569 | } |
| 570 | return entry->second; |
| 571 | } |
| 572 | |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 573 | SpvId SPIRVCodeGenerator::getImageType(const Type& type) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 574 | SkASSERT(type.kind() == Type::kSampler_Kind); |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 575 | this->getType(type); |
| 576 | String key = type.name() + to_string((int) fDefaultLayout.fStd); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 577 | SkASSERT(fImageTypeMap.find(key) != fImageTypeMap.end()); |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 578 | return fImageTypeMap[key]; |
| 579 | } |
| 580 | |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 581 | SpvId SPIRVCodeGenerator::getFunctionType(const FunctionDeclaration& function) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 582 | String key = function.fReturnType.description() + "("; |
| 583 | String separator; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 584 | for (size_t i = 0; i < function.fParameters.size(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 585 | key += separator; |
| 586 | separator = ", "; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 587 | key += function.fParameters[i]->fType.description(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 588 | } |
| 589 | key += ")"; |
| 590 | auto entry = fTypeMap.find(key); |
| 591 | if (entry == fTypeMap.end()) { |
| 592 | SpvId result = this->nextId(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 593 | int32_t length = 3 + (int32_t) function.fParameters.size(); |
| 594 | SpvId returnType = this->getType(function.fReturnType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 595 | std::vector<SpvId> parameterTypes; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 596 | for (size_t i = 0; i < function.fParameters.size(); i++) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 597 | // glslang seems to treat all function arguments as pointers whether they need to be or |
| 598 | // not. I was initially puzzled by this until I ran bizarre failures with certain |
| 599 | // patterns of function calls and control constructs, as exemplified by this minimal |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 600 | // failure case: |
| 601 | // |
| 602 | // void sphere(float x) { |
| 603 | // } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 604 | // |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 605 | // void map() { |
| 606 | // sphere(1.0); |
| 607 | // } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 608 | // |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 609 | // void main() { |
| 610 | // for (int i = 0; i < 1; i++) { |
| 611 | // map(); |
| 612 | // } |
| 613 | // } |
| 614 | // |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 615 | // As of this writing, compiling this in the "obvious" way (with sphere taking a float) |
| 616 | // 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] | 617 | // as glslang does, fixes it. It's entirely possible I simply missed whichever part of |
| 618 | // the spec makes this make sense. |
| 619 | // if (is_out(function->fParameters[i])) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 620 | parameterTypes.push_back(this->getPointerType(function.fParameters[i]->fType, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 621 | SpvStorageClassFunction)); |
| 622 | // } else { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 623 | // parameterTypes.push_back(this->getType(function.fParameters[i]->fType)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 624 | // } |
| 625 | } |
| 626 | this->writeOpCode(SpvOpTypeFunction, length, fConstantBuffer); |
| 627 | this->writeWord(result, fConstantBuffer); |
| 628 | this->writeWord(returnType, fConstantBuffer); |
| 629 | for (SpvId id : parameterTypes) { |
| 630 | this->writeWord(id, fConstantBuffer); |
| 631 | } |
| 632 | fTypeMap[key] = result; |
| 633 | return result; |
| 634 | } |
| 635 | return entry->second; |
| 636 | } |
| 637 | |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 638 | SpvId SPIRVCodeGenerator::getPointerType(const Type& type, SpvStorageClass_ storageClass) { |
| 639 | return this->getPointerType(type, fDefaultLayout, storageClass); |
| 640 | } |
| 641 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 642 | SpvId SPIRVCodeGenerator::getPointerType(const Type& rawType, const MemoryLayout& layout, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 643 | SpvStorageClass_ storageClass) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 644 | Type type = this->getActualType(rawType); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 645 | String key = type.description() + "*" + to_string(layout.fStd) + to_string(storageClass); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 646 | auto entry = fTypeMap.find(key); |
| 647 | if (entry == fTypeMap.end()) { |
| 648 | SpvId result = this->nextId(); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 649 | this->writeInstruction(SpvOpTypePointer, result, storageClass, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 650 | this->getType(type), fConstantBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 651 | fTypeMap[key] = result; |
| 652 | return result; |
| 653 | } |
| 654 | return entry->second; |
| 655 | } |
| 656 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 657 | SpvId SPIRVCodeGenerator::writeExpression(const Expression& expr, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 658 | switch (expr.fKind) { |
| 659 | case Expression::kBinary_Kind: |
| 660 | return this->writeBinaryExpression((BinaryExpression&) expr, out); |
| 661 | case Expression::kBoolLiteral_Kind: |
| 662 | return this->writeBoolLiteral((BoolLiteral&) expr); |
| 663 | case Expression::kConstructor_Kind: |
| 664 | return this->writeConstructor((Constructor&) expr, out); |
| 665 | case Expression::kIntLiteral_Kind: |
| 666 | return this->writeIntLiteral((IntLiteral&) expr); |
| 667 | case Expression::kFieldAccess_Kind: |
| 668 | return this->writeFieldAccess(((FieldAccess&) expr), out); |
| 669 | case Expression::kFloatLiteral_Kind: |
| 670 | return this->writeFloatLiteral(((FloatLiteral&) expr)); |
| 671 | case Expression::kFunctionCall_Kind: |
| 672 | return this->writeFunctionCall((FunctionCall&) expr, out); |
| 673 | case Expression::kPrefix_Kind: |
| 674 | return this->writePrefixExpression((PrefixExpression&) expr, out); |
| 675 | case Expression::kPostfix_Kind: |
| 676 | return this->writePostfixExpression((PostfixExpression&) expr, out); |
| 677 | case Expression::kSwizzle_Kind: |
| 678 | return this->writeSwizzle((Swizzle&) expr, out); |
| 679 | case Expression::kVariableReference_Kind: |
| 680 | return this->writeVariableReference((VariableReference&) expr, out); |
| 681 | case Expression::kTernary_Kind: |
| 682 | return this->writeTernaryExpression((TernaryExpression&) expr, out); |
| 683 | case Expression::kIndex_Kind: |
| 684 | return this->writeIndexExpression((IndexExpression&) expr, out); |
| 685 | default: |
| 686 | ABORT("unsupported expression: %s", expr.description().c_str()); |
| 687 | } |
| 688 | return -1; |
| 689 | } |
| 690 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 691 | SpvId SPIRVCodeGenerator::writeIntrinsicCall(const FunctionCall& c, OutputStream& out) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 692 | auto intrinsic = fIntrinsicMap.find(c.fFunction.fName); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 693 | SkASSERT(intrinsic != fIntrinsicMap.end()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 694 | int32_t intrinsicId; |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 695 | if (c.fArguments.size() > 0) { |
| 696 | const Type& type = c.fArguments[0]->fType; |
| 697 | if (std::get<0>(intrinsic->second) == kSpecial_IntrinsicKind || is_float(fContext, type)) { |
| 698 | intrinsicId = std::get<1>(intrinsic->second); |
| 699 | } else if (is_signed(fContext, type)) { |
| 700 | intrinsicId = std::get<2>(intrinsic->second); |
| 701 | } else if (is_unsigned(fContext, type)) { |
| 702 | intrinsicId = std::get<3>(intrinsic->second); |
| 703 | } else if (is_bool(fContext, type)) { |
| 704 | intrinsicId = std::get<4>(intrinsic->second); |
| 705 | } else { |
| 706 | intrinsicId = std::get<1>(intrinsic->second); |
| 707 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 708 | } else { |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 709 | intrinsicId = std::get<1>(intrinsic->second); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 710 | } |
| 711 | switch (std::get<0>(intrinsic->second)) { |
| 712 | case kGLSL_STD_450_IntrinsicKind: { |
| 713 | SpvId result = this->nextId(); |
| 714 | std::vector<SpvId> arguments; |
| 715 | for (size_t i = 0; i < c.fArguments.size(); i++) { |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 716 | if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 717 | arguments.push_back(this->getLValue(*c.fArguments[i], out)->getPointer()); |
| 718 | } else { |
| 719 | arguments.push_back(this->writeExpression(*c.fArguments[i], out)); |
| 720 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 721 | } |
| 722 | this->writeOpCode(SpvOpExtInst, 5 + (int32_t) arguments.size(), out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 723 | this->writeWord(this->getType(c.fType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 724 | this->writeWord(result, out); |
| 725 | this->writeWord(fGLSLExtendedInstructions, out); |
| 726 | this->writeWord(intrinsicId, out); |
| 727 | for (SpvId id : arguments) { |
| 728 | this->writeWord(id, out); |
| 729 | } |
| 730 | return result; |
| 731 | } |
| 732 | case kSPIRV_IntrinsicKind: { |
| 733 | SpvId result = this->nextId(); |
| 734 | std::vector<SpvId> arguments; |
| 735 | for (size_t i = 0; i < c.fArguments.size(); i++) { |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 736 | if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 737 | arguments.push_back(this->getLValue(*c.fArguments[i], out)->getPointer()); |
| 738 | } else { |
| 739 | arguments.push_back(this->writeExpression(*c.fArguments[i], out)); |
| 740 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 741 | } |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 742 | if (c.fType != *fContext.fVoid_Type) { |
| 743 | this->writeOpCode((SpvOp_) intrinsicId, 3 + (int32_t) arguments.size(), out); |
| 744 | this->writeWord(this->getType(c.fType), out); |
| 745 | this->writeWord(result, out); |
| 746 | } else { |
| 747 | this->writeOpCode((SpvOp_) intrinsicId, 1 + (int32_t) arguments.size(), out); |
| 748 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 749 | for (SpvId id : arguments) { |
| 750 | this->writeWord(id, out); |
| 751 | } |
| 752 | return result; |
| 753 | } |
| 754 | case kSpecial_IntrinsicKind: |
| 755 | return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId, out); |
| 756 | default: |
| 757 | ABORT("unsupported intrinsic kind"); |
| 758 | } |
| 759 | } |
| 760 | |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 761 | std::vector<SpvId> SPIRVCodeGenerator::vectorize( |
| 762 | const std::vector<std::unique_ptr<Expression>>& args, |
| 763 | OutputStream& out) { |
| 764 | int vectorSize = 0; |
| 765 | for (const auto& a : args) { |
| 766 | if (a->fType.kind() == Type::kVector_Kind) { |
| 767 | if (vectorSize) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 768 | SkASSERT(a->fType.columns() == vectorSize); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 769 | } |
| 770 | else { |
| 771 | vectorSize = a->fType.columns(); |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | std::vector<SpvId> result; |
| 776 | for (const auto& a : args) { |
| 777 | SpvId raw = this->writeExpression(*a, out); |
| 778 | if (vectorSize && a->fType.kind() == Type::kScalar_Kind) { |
| 779 | SpvId vector = this->nextId(); |
| 780 | this->writeOpCode(SpvOpCompositeConstruct, 3 + vectorSize, out); |
| 781 | this->writeWord(this->getType(a->fType.toCompound(fContext, vectorSize, 1)), out); |
| 782 | this->writeWord(vector, out); |
| 783 | for (int i = 0; i < vectorSize; i++) { |
| 784 | this->writeWord(raw, out); |
| 785 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 786 | this->writePrecisionModifier(a->fType, vector); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 787 | result.push_back(vector); |
| 788 | } else { |
| 789 | result.push_back(raw); |
| 790 | } |
| 791 | } |
| 792 | return result; |
| 793 | } |
| 794 | |
| 795 | void SPIRVCodeGenerator::writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst, |
| 796 | SpvId signedInst, SpvId unsignedInst, |
| 797 | const std::vector<SpvId>& args, |
| 798 | OutputStream& out) { |
| 799 | this->writeOpCode(SpvOpExtInst, 5 + args.size(), out); |
| 800 | this->writeWord(this->getType(type), out); |
| 801 | this->writeWord(id, out); |
| 802 | this->writeWord(fGLSLExtendedInstructions, out); |
| 803 | |
| 804 | if (is_float(fContext, type)) { |
| 805 | this->writeWord(floatInst, out); |
| 806 | } else if (is_signed(fContext, type)) { |
| 807 | this->writeWord(signedInst, out); |
| 808 | } else if (is_unsigned(fContext, type)) { |
| 809 | this->writeWord(unsignedInst, out); |
| 810 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 811 | SkASSERT(false); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 812 | } |
| 813 | for (SpvId a : args) { |
| 814 | this->writeWord(a, out); |
| 815 | } |
| 816 | } |
| 817 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 818 | SpvId SPIRVCodeGenerator::writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 819 | OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 820 | SpvId result = this->nextId(); |
| 821 | switch (kind) { |
| 822 | case kAtan_SpecialIntrinsic: { |
| 823 | std::vector<SpvId> arguments; |
| 824 | for (size_t i = 0; i < c.fArguments.size(); i++) { |
| 825 | arguments.push_back(this->writeExpression(*c.fArguments[i], out)); |
| 826 | } |
| 827 | this->writeOpCode(SpvOpExtInst, 5 + (int32_t) arguments.size(), out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 828 | this->writeWord(this->getType(c.fType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 829 | this->writeWord(result, out); |
| 830 | this->writeWord(fGLSLExtendedInstructions, out); |
| 831 | this->writeWord(arguments.size() == 2 ? GLSLstd450Atan2 : GLSLstd450Atan, out); |
| 832 | for (SpvId id : arguments) { |
| 833 | this->writeWord(id, out); |
| 834 | } |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 835 | break; |
| 836 | } |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 837 | case kSampledImage_SpecialIntrinsic: { |
| 838 | SkASSERT(2 == c.fArguments.size()); |
| 839 | SpvId img = this->writeExpression(*c.fArguments[0], out); |
| 840 | SpvId sampler = this->writeExpression(*c.fArguments[1], out); |
| 841 | this->writeInstruction(SpvOpSampledImage, |
| 842 | this->getType(c.fType), |
| 843 | result, |
| 844 | img, |
| 845 | sampler, |
| 846 | out); |
| 847 | break; |
| 848 | } |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 849 | case kSubpassLoad_SpecialIntrinsic: { |
| 850 | SpvId img = this->writeExpression(*c.fArguments[0], out); |
| 851 | std::vector<std::unique_ptr<Expression>> args; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 852 | args.emplace_back(new FloatLiteral(fContext, -1, 0.0)); |
| 853 | args.emplace_back(new FloatLiteral(fContext, -1, 0.0)); |
| 854 | Constructor ctor(-1, *fContext.fFloat2_Type, std::move(args)); |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 855 | SpvId coords = this->writeConstantVector(ctor); |
| 856 | if (1 == c.fArguments.size()) { |
| 857 | this->writeInstruction(SpvOpImageRead, |
| 858 | this->getType(c.fType), |
| 859 | result, |
| 860 | img, |
| 861 | coords, |
| 862 | out); |
| 863 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 864 | SkASSERT(2 == c.fArguments.size()); |
Ethan Nicholas | 0187ae6 | 2017-05-03 11:03:44 -0400 | [diff] [blame] | 865 | SpvId sample = this->writeExpression(*c.fArguments[1], out); |
| 866 | this->writeInstruction(SpvOpImageRead, |
| 867 | this->getType(c.fType), |
| 868 | result, |
| 869 | img, |
| 870 | coords, |
| 871 | SpvImageOperandsSampleMask, |
| 872 | sample, |
| 873 | out); |
| 874 | } |
| 875 | break; |
| 876 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 877 | case kTexture_SpecialIntrinsic: { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 878 | SpvOp_ op = SpvOpImageSampleImplicitLod; |
| 879 | switch (c.fArguments[0]->fType.dimensions()) { |
| 880 | case SpvDim1D: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 881 | if (c.fArguments[1]->fType == *fContext.fFloat2_Type) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 882 | op = SpvOpImageSampleProjImplicitLod; |
| 883 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 884 | SkASSERT(c.fArguments[1]->fType == *fContext.fFloat_Type); |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 885 | } |
| 886 | break; |
| 887 | case SpvDim2D: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 888 | if (c.fArguments[1]->fType == *fContext.fFloat3_Type) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 889 | op = SpvOpImageSampleProjImplicitLod; |
| 890 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 891 | SkASSERT(c.fArguments[1]->fType == *fContext.fFloat2_Type); |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 892 | } |
| 893 | break; |
| 894 | case SpvDim3D: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 895 | if (c.fArguments[1]->fType == *fContext.fFloat4_Type) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 896 | op = SpvOpImageSampleProjImplicitLod; |
| 897 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 898 | SkASSERT(c.fArguments[1]->fType == *fContext.fFloat3_Type); |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 899 | } |
| 900 | break; |
| 901 | case SpvDimCube: // fall through |
| 902 | case SpvDimRect: // fall through |
| 903 | case SpvDimBuffer: // fall through |
| 904 | case SpvDimSubpassData: |
| 905 | break; |
| 906 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 907 | SpvId type = this->getType(c.fType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 908 | SpvId sampler = this->writeExpression(*c.fArguments[0], out); |
| 909 | SpvId uv = this->writeExpression(*c.fArguments[1], out); |
| 910 | if (c.fArguments.size() == 3) { |
Ethan Nicholas | 2b3dab6 | 2016-11-28 12:03:26 -0500 | [diff] [blame] | 911 | this->writeInstruction(op, type, result, sampler, uv, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 912 | SpvImageOperandsBiasMask, |
| 913 | this->writeExpression(*c.fArguments[2], out), |
| 914 | out); |
| 915 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 916 | SkASSERT(c.fArguments.size() == 2); |
Brian Osman | 8a83ca4 | 2018-02-12 14:32:17 -0500 | [diff] [blame] | 917 | if (fProgram.fSettings.fSharpenTextures) { |
| 918 | FloatLiteral lodBias(fContext, -1, -0.5); |
| 919 | this->writeInstruction(op, type, result, sampler, uv, |
| 920 | SpvImageOperandsBiasMask, |
| 921 | this->writeFloatLiteral(lodBias), |
| 922 | out); |
| 923 | } else { |
| 924 | this->writeInstruction(op, type, result, sampler, uv, |
| 925 | out); |
| 926 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 927 | } |
| 928 | break; |
| 929 | } |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 930 | case kMod_SpecialIntrinsic: { |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 931 | std::vector<SpvId> args = this->vectorize(c.fArguments, out); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 932 | SkASSERT(args.size() == 2); |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 933 | const Type& operandType = c.fArguments[0]->fType; |
| 934 | SpvOp_ op; |
| 935 | if (is_float(fContext, operandType)) { |
| 936 | op = SpvOpFMod; |
| 937 | } else if (is_signed(fContext, operandType)) { |
| 938 | op = SpvOpSMod; |
| 939 | } else if (is_unsigned(fContext, operandType)) { |
| 940 | op = SpvOpUMod; |
| 941 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 942 | SkASSERT(false); |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 943 | return 0; |
| 944 | } |
| 945 | this->writeOpCode(op, 5, out); |
| 946 | this->writeWord(this->getType(operandType), out); |
| 947 | this->writeWord(result, out); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 948 | this->writeWord(args[0], out); |
| 949 | this->writeWord(args[1], out); |
| 950 | break; |
| 951 | } |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 952 | case kDFdy_SpecialIntrinsic: { |
| 953 | SpvId fn = this->writeExpression(*c.fArguments[0], out); |
| 954 | this->writeOpCode(SpvOpDPdy, 4, out); |
| 955 | this->writeWord(this->getType(c.fType), out); |
| 956 | this->writeWord(result, out); |
| 957 | this->writeWord(fn, out); |
| 958 | if (fProgram.fSettings.fFlipY) { |
| 959 | // Flipping Y also negates the Y derivatives. |
| 960 | SpvId flipped = this->nextId(); |
| 961 | this->writeInstruction(SpvOpFNegate, this->getType(c.fType), flipped, result, out); |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 962 | this->writePrecisionModifier(c.fType, flipped); |
Chris Dalton | b8af5ad | 2019-02-25 14:54:21 -0700 | [diff] [blame] | 963 | return flipped; |
| 964 | } |
| 965 | break; |
| 966 | } |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 967 | case kClamp_SpecialIntrinsic: { |
| 968 | std::vector<SpvId> args = this->vectorize(c.fArguments, out); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 969 | SkASSERT(args.size() == 3); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 970 | this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FClamp, GLSLstd450SClamp, |
| 971 | GLSLstd450UClamp, args, out); |
| 972 | break; |
| 973 | } |
| 974 | case kMax_SpecialIntrinsic: { |
| 975 | std::vector<SpvId> args = this->vectorize(c.fArguments, out); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 976 | SkASSERT(args.size() == 2); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 977 | this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FMax, GLSLstd450SMax, |
| 978 | GLSLstd450UMax, args, out); |
| 979 | break; |
| 980 | } |
| 981 | case kMin_SpecialIntrinsic: { |
| 982 | std::vector<SpvId> args = this->vectorize(c.fArguments, out); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 983 | SkASSERT(args.size() == 2); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 984 | this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FMin, GLSLstd450SMin, |
| 985 | GLSLstd450UMin, args, out); |
| 986 | break; |
| 987 | } |
| 988 | case kMix_SpecialIntrinsic: { |
| 989 | std::vector<SpvId> args = this->vectorize(c.fArguments, out); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 990 | SkASSERT(args.size() == 3); |
Ethan Nicholas | 0fc07f9 | 2018-02-27 15:25:47 -0500 | [diff] [blame] | 991 | this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FMix, SpvOpUndef, |
| 992 | SpvOpUndef, args, out); |
| 993 | break; |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 994 | } |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 995 | case kSaturate_SpecialIntrinsic: { |
| 996 | SkASSERT(c.fArguments.size() == 1); |
| 997 | std::vector<std::unique_ptr<Expression>> finalArgs; |
| 998 | finalArgs.push_back(c.fArguments[0]->clone()); |
| 999 | finalArgs.emplace_back(new FloatLiteral(fContext, -1, 0)); |
| 1000 | finalArgs.emplace_back(new FloatLiteral(fContext, -1, 1)); |
| 1001 | std::vector<SpvId> spvArgs = this->vectorize(finalArgs, out); |
| 1002 | this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FClamp, GLSLstd450SClamp, |
| 1003 | GLSLstd450UClamp, spvArgs, out); |
| 1004 | break; |
| 1005 | } |
Ethan Nicholas | ffdc3e6 | 2019-09-19 16:58:33 -0400 | [diff] [blame] | 1006 | case kUnpremul_SpecialIntrinsic: { |
| 1007 | SpvId color = this->writeExpression(*c.fArguments[0], out); |
| 1008 | SpvId a = this->writeSwizzle(*fContext.fHalf_Type, c.fArguments[0]->fType, color, { 3 }, |
| 1009 | out); |
| 1010 | FloatLiteral min(fContext, -1, SKSL_UNPREMUL_MIN); |
| 1011 | SpvId minId = this->writeFloatLiteral(min); |
| 1012 | SpvId nonZeroAlpha = this->nextId(); |
| 1013 | this->writeGLSLExtendedInstruction(*fContext.fHalf_Type, nonZeroAlpha, GLSLstd450FMax, |
| 1014 | SpvOpUndef, SpvOpUndef, { a, minId }, out); |
| 1015 | SpvId rgb = this->writeSwizzle(*fContext.fHalf3_Type, *fContext.fHalf4_Type, color, |
| 1016 | { 0, 1, 2 }, out); |
| 1017 | SpvId scaled = this->writeBinaryExpression(*fContext.fHalf3_Type, rgb, Token::SLASH, |
| 1018 | *fContext.fFloat_Type, nonZeroAlpha, |
| 1019 | *fContext.fHalf3_Type, out); |
| 1020 | this->writeOpCode(SpvOpCompositeConstruct, 5, out); |
| 1021 | this->writeWord(this->getType(c.fType), out); |
| 1022 | this->writeWord(result, out); |
| 1023 | this->writeWord(scaled, out); |
| 1024 | this->writeWord(nonZeroAlpha, out); |
| 1025 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1026 | } |
| 1027 | return result; |
| 1028 | } |
| 1029 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1030 | SpvId SPIRVCodeGenerator::writeFunctionCall(const FunctionCall& c, OutputStream& out) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1031 | const auto& entry = fFunctionMap.find(&c.fFunction); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1032 | if (entry == fFunctionMap.end()) { |
| 1033 | return this->writeIntrinsicCall(c, out); |
| 1034 | } |
| 1035 | // stores (variable, type, lvalue) pairs to extract and save after the function call is complete |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1036 | std::vector<std::tuple<SpvId, const Type*, std::unique_ptr<LValue>>> lvalues; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1037 | std::vector<SpvId> arguments; |
| 1038 | for (size_t i = 0; i < c.fArguments.size(); i++) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1039 | // 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] | 1040 | // passed directly |
| 1041 | SpvId tmpVar; |
| 1042 | // if we need a temporary var to store this argument, this is the value to store in the var |
| 1043 | SpvId tmpValueId; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1044 | if (is_out(*c.fFunction.fParameters[i])) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1045 | std::unique_ptr<LValue> lv = this->getLValue(*c.fArguments[i], out); |
| 1046 | SpvId ptr = lv->getPointer(); |
| 1047 | if (ptr) { |
| 1048 | arguments.push_back(ptr); |
| 1049 | continue; |
| 1050 | } else { |
| 1051 | // lvalue cannot simply be read and written via a pointer (e.g. a swizzle). Need to |
| 1052 | // copy it into a temp, call the function, read the value out of the temp, and then |
| 1053 | // update the lvalue. |
| 1054 | tmpValueId = lv->load(out); |
| 1055 | tmpVar = this->nextId(); |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1056 | lvalues.push_back(std::make_tuple(tmpVar, &c.fArguments[i]->fType, std::move(lv))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1057 | } |
| 1058 | } else { |
| 1059 | // see getFunctionType for an explanation of why we're always using pointer parameters |
| 1060 | tmpValueId = this->writeExpression(*c.fArguments[i], out); |
| 1061 | tmpVar = this->nextId(); |
| 1062 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1063 | this->writeInstruction(SpvOpVariable, |
| 1064 | this->getPointerType(c.fArguments[i]->fType, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1065 | SpvStorageClassFunction), |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1066 | tmpVar, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1067 | SpvStorageClassFunction, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1068 | fVariableBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1069 | this->writeInstruction(SpvOpStore, tmpVar, tmpValueId, out); |
| 1070 | arguments.push_back(tmpVar); |
| 1071 | } |
| 1072 | SpvId result = this->nextId(); |
| 1073 | this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t) c.fArguments.size(), out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1074 | this->writeWord(this->getType(c.fType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1075 | this->writeWord(result, out); |
| 1076 | this->writeWord(entry->second, out); |
| 1077 | for (SpvId id : arguments) { |
| 1078 | this->writeWord(id, out); |
| 1079 | } |
| 1080 | // now that the call is complete, we may need to update some lvalues with the new values of out |
| 1081 | // arguments |
| 1082 | for (const auto& tuple : lvalues) { |
| 1083 | SpvId load = this->nextId(); |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1084 | this->writeInstruction(SpvOpLoad, getType(*std::get<1>(tuple)), load, std::get<0>(tuple), |
| 1085 | out); |
| 1086 | this->writePrecisionModifier(*std::get<1>(tuple), load); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1087 | std::get<2>(tuple)->store(load, out); |
| 1088 | } |
| 1089 | return result; |
| 1090 | } |
| 1091 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1092 | SpvId SPIRVCodeGenerator::writeConstantVector(const Constructor& c) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1093 | SkASSERT(c.fType.kind() == Type::kVector_Kind && c.isConstant()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1094 | SpvId result = this->nextId(); |
| 1095 | std::vector<SpvId> arguments; |
| 1096 | for (size_t i = 0; i < c.fArguments.size(); i++) { |
| 1097 | arguments.push_back(this->writeExpression(*c.fArguments[i], fConstantBuffer)); |
| 1098 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1099 | SpvId type = this->getType(c.fType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1100 | if (c.fArguments.size() == 1) { |
| 1101 | // with a single argument, a vector will have all of its entries equal to the argument |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1102 | this->writeOpCode(SpvOpConstantComposite, 3 + c.fType.columns(), fConstantBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1103 | this->writeWord(type, fConstantBuffer); |
| 1104 | this->writeWord(result, fConstantBuffer); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1105 | for (int i = 0; i < c.fType.columns(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1106 | this->writeWord(arguments[0], fConstantBuffer); |
| 1107 | } |
| 1108 | } else { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1109 | this->writeOpCode(SpvOpConstantComposite, 3 + (int32_t) c.fArguments.size(), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1110 | fConstantBuffer); |
| 1111 | this->writeWord(type, fConstantBuffer); |
| 1112 | this->writeWord(result, fConstantBuffer); |
| 1113 | for (SpvId id : arguments) { |
| 1114 | this->writeWord(id, fConstantBuffer); |
| 1115 | } |
| 1116 | } |
| 1117 | return result; |
| 1118 | } |
| 1119 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1120 | SpvId SPIRVCodeGenerator::writeFloatConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1121 | SkASSERT(c.fType.isFloat()); |
| 1122 | SkASSERT(c.fArguments.size() == 1); |
| 1123 | SkASSERT(c.fArguments[0]->fType.isNumber()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1124 | SpvId result = this->nextId(); |
| 1125 | SpvId parameter = this->writeExpression(*c.fArguments[0], out); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1126 | if (c.fArguments[0]->fType.isSigned()) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1127 | this->writeInstruction(SpvOpConvertSToF, this->getType(c.fType), result, parameter, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1128 | out); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1129 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1130 | SkASSERT(c.fArguments[0]->fType.isUnsigned()); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1131 | this->writeInstruction(SpvOpConvertUToF, this->getType(c.fType), result, parameter, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1132 | out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1133 | } |
| 1134 | return result; |
| 1135 | } |
| 1136 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1137 | SpvId SPIRVCodeGenerator::writeIntConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1138 | SkASSERT(c.fType.isSigned()); |
| 1139 | SkASSERT(c.fArguments.size() == 1); |
| 1140 | SkASSERT(c.fArguments[0]->fType.isNumber()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1141 | SpvId result = this->nextId(); |
| 1142 | SpvId parameter = this->writeExpression(*c.fArguments[0], out); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1143 | if (c.fArguments[0]->fType.isFloat()) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1144 | this->writeInstruction(SpvOpConvertFToS, this->getType(c.fType), result, parameter, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1145 | out); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1146 | } |
| 1147 | else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1148 | SkASSERT(c.fArguments[0]->fType.isUnsigned()); |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1149 | this->writeInstruction(SpvOpBitcast, this->getType(c.fType), result, parameter, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1150 | out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1151 | } |
| 1152 | return result; |
| 1153 | } |
| 1154 | |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1155 | SpvId SPIRVCodeGenerator::writeUIntConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1156 | SkASSERT(c.fType.isUnsigned()); |
| 1157 | SkASSERT(c.fArguments.size() == 1); |
| 1158 | SkASSERT(c.fArguments[0]->fType.isNumber()); |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1159 | SpvId result = this->nextId(); |
| 1160 | SpvId parameter = this->writeExpression(*c.fArguments[0], out); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1161 | if (c.fArguments[0]->fType.isFloat()) { |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1162 | this->writeInstruction(SpvOpConvertFToU, this->getType(c.fType), result, parameter, |
| 1163 | out); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1164 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1165 | SkASSERT(c.fArguments[0]->fType.isSigned()); |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1166 | this->writeInstruction(SpvOpBitcast, this->getType(c.fType), result, parameter, |
| 1167 | out); |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1168 | } |
| 1169 | return result; |
| 1170 | } |
| 1171 | |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1172 | void SPIRVCodeGenerator::writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1173 | OutputStream& out) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1174 | FloatLiteral zero(fContext, -1, 0); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1175 | SpvId zeroId = this->writeFloatLiteral(zero); |
| 1176 | std::vector<SpvId> columnIds; |
| 1177 | for (int column = 0; column < type.columns(); column++) { |
| 1178 | this->writeOpCode(SpvOpCompositeConstruct, 3 + type.rows(), |
| 1179 | out); |
| 1180 | this->writeWord(this->getType(type.componentType().toCompound(fContext, type.rows(), 1)), |
| 1181 | out); |
| 1182 | SpvId columnId = this->nextId(); |
| 1183 | this->writeWord(columnId, out); |
| 1184 | columnIds.push_back(columnId); |
| 1185 | for (int row = 0; row < type.columns(); row++) { |
| 1186 | this->writeWord(row == column ? diagonal : zeroId, out); |
| 1187 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1188 | this->writePrecisionModifier(type, columnId); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1189 | } |
| 1190 | this->writeOpCode(SpvOpCompositeConstruct, 3 + type.columns(), |
| 1191 | out); |
| 1192 | this->writeWord(this->getType(type), out); |
| 1193 | this->writeWord(id, out); |
| 1194 | for (SpvId id : columnIds) { |
| 1195 | this->writeWord(id, out); |
| 1196 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1197 | this->writePrecisionModifier(type, id); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | void SPIRVCodeGenerator::writeMatrixCopy(SpvId id, SpvId src, const Type& srcType, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1201 | const Type& dstType, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1202 | SkASSERT(srcType.kind() == Type::kMatrix_Kind); |
| 1203 | SkASSERT(dstType.kind() == Type::kMatrix_Kind); |
| 1204 | SkASSERT(srcType.componentType() == dstType.componentType()); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1205 | SpvId srcColumnType = this->getType(srcType.componentType().toCompound(fContext, |
| 1206 | srcType.rows(), |
| 1207 | 1)); |
| 1208 | SpvId dstColumnType = this->getType(dstType.componentType().toCompound(fContext, |
| 1209 | dstType.rows(), |
| 1210 | 1)); |
| 1211 | SpvId zeroId; |
| 1212 | if (dstType.componentType() == *fContext.fFloat_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1213 | FloatLiteral zero(fContext, -1, 0.0); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1214 | zeroId = this->writeFloatLiteral(zero); |
| 1215 | } else if (dstType.componentType() == *fContext.fInt_Type) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1216 | IntLiteral zero(fContext, -1, 0); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1217 | zeroId = this->writeIntLiteral(zero); |
| 1218 | } else { |
| 1219 | ABORT("unsupported matrix component type"); |
| 1220 | } |
| 1221 | SpvId zeroColumn = 0; |
| 1222 | SpvId columns[4]; |
| 1223 | for (int i = 0; i < dstType.columns(); i++) { |
| 1224 | if (i < srcType.columns()) { |
| 1225 | // we're still inside the src matrix, copy the column |
| 1226 | SpvId srcColumn = this->nextId(); |
| 1227 | this->writeInstruction(SpvOpCompositeExtract, srcColumnType, srcColumn, src, i, out); |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1228 | this->writePrecisionModifier(dstType, srcColumn); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1229 | SpvId dstColumn; |
| 1230 | if (srcType.rows() == dstType.rows()) { |
| 1231 | // columns are equal size, don't need to do anything |
| 1232 | dstColumn = srcColumn; |
| 1233 | } |
| 1234 | else if (dstType.rows() > srcType.rows()) { |
| 1235 | // dst column is bigger, need to zero-pad it |
| 1236 | dstColumn = this->nextId(); |
| 1237 | int delta = dstType.rows() - srcType.rows(); |
| 1238 | this->writeOpCode(SpvOpCompositeConstruct, 4 + delta, out); |
| 1239 | this->writeWord(dstColumnType, out); |
| 1240 | this->writeWord(dstColumn, out); |
| 1241 | this->writeWord(srcColumn, out); |
| 1242 | for (int i = 0; i < delta; ++i) { |
| 1243 | this->writeWord(zeroId, out); |
| 1244 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1245 | this->writePrecisionModifier(dstType, dstColumn); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1246 | } |
| 1247 | else { |
| 1248 | // dst column is smaller, need to swizzle the src column |
| 1249 | dstColumn = this->nextId(); |
| 1250 | int count = dstType.rows(); |
| 1251 | this->writeOpCode(SpvOpVectorShuffle, 5 + count, out); |
| 1252 | this->writeWord(dstColumnType, out); |
| 1253 | this->writeWord(dstColumn, out); |
| 1254 | this->writeWord(srcColumn, out); |
| 1255 | this->writeWord(srcColumn, out); |
| 1256 | for (int i = 0; i < count; i++) { |
| 1257 | this->writeWord(i, out); |
| 1258 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1259 | this->writePrecisionModifier(dstType, dstColumn); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1260 | } |
| 1261 | columns[i] = dstColumn; |
| 1262 | } else { |
| 1263 | // we're past the end of the src matrix, need a vector of zeroes |
| 1264 | if (!zeroColumn) { |
| 1265 | zeroColumn = this->nextId(); |
| 1266 | this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.rows(), out); |
| 1267 | this->writeWord(dstColumnType, out); |
| 1268 | this->writeWord(zeroColumn, out); |
| 1269 | for (int i = 0; i < dstType.rows(); ++i) { |
| 1270 | this->writeWord(zeroId, out); |
| 1271 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1272 | this->writePrecisionModifier(dstType, zeroColumn); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1273 | } |
| 1274 | columns[i] = zeroColumn; |
| 1275 | } |
| 1276 | } |
| 1277 | this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.columns(), out); |
| 1278 | this->writeWord(this->getType(dstType), out); |
| 1279 | this->writeWord(id, out); |
| 1280 | for (int i = 0; i < dstType.columns(); i++) { |
| 1281 | this->writeWord(columns[i], out); |
| 1282 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1283 | this->writePrecisionModifier(dstType, id); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1284 | } |
| 1285 | |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1286 | void SPIRVCodeGenerator::addColumnEntry(SpvId columnType, Precision precision, |
| 1287 | std::vector<SpvId>* currentColumn, |
Ethan Nicholas | 5c46b72 | 2019-03-22 14:32:37 -0400 | [diff] [blame] | 1288 | std::vector<SpvId>* columnIds, |
| 1289 | int* currentCount, int rows, SpvId entry, |
| 1290 | OutputStream& out) { |
| 1291 | SkASSERT(*currentCount < rows); |
| 1292 | ++(*currentCount); |
| 1293 | currentColumn->push_back(entry); |
| 1294 | if (*currentCount == rows) { |
| 1295 | *currentCount = 0; |
| 1296 | this->writeOpCode(SpvOpCompositeConstruct, 3 + currentColumn->size(), out); |
| 1297 | this->writeWord(columnType, out); |
| 1298 | SpvId columnId = this->nextId(); |
| 1299 | this->writeWord(columnId, out); |
| 1300 | columnIds->push_back(columnId); |
| 1301 | for (SpvId id : *currentColumn) { |
| 1302 | this->writeWord(id, out); |
| 1303 | } |
| 1304 | currentColumn->clear(); |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1305 | this->writePrecisionModifier(precision, columnId); |
Ethan Nicholas | 5c46b72 | 2019-03-22 14:32:37 -0400 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1309 | SpvId SPIRVCodeGenerator::writeMatrixConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1310 | SkASSERT(c.fType.kind() == Type::kMatrix_Kind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1311 | // go ahead and write the arguments so we don't try to write new instructions in the middle of |
| 1312 | // an instruction |
| 1313 | std::vector<SpvId> arguments; |
| 1314 | for (size_t i = 0; i < c.fArguments.size(); i++) { |
| 1315 | arguments.push_back(this->writeExpression(*c.fArguments[i], out)); |
| 1316 | } |
| 1317 | SpvId result = this->nextId(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1318 | int rows = c.fType.rows(); |
| 1319 | int columns = c.fType.columns(); |
Ethan Nicholas | 84645e3 | 2017-02-09 13:57:14 -0500 | [diff] [blame] | 1320 | if (arguments.size() == 1 && c.fArguments[0]->fType.kind() == Type::kScalar_Kind) { |
| 1321 | this->writeUniformScaleMatrix(result, arguments[0], c.fType, out); |
| 1322 | } else if (arguments.size() == 1 && c.fArguments[0]->fType.kind() == Type::kMatrix_Kind) { |
| 1323 | this->writeMatrixCopy(result, arguments[0], c.fArguments[0]->fType, c.fType, out); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1324 | } else if (arguments.size() == 1 && c.fArguments[0]->fType.kind() == Type::kVector_Kind) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1325 | SkASSERT(c.fType.rows() == 2 && c.fType.columns() == 2); |
| 1326 | SkASSERT(c.fArguments[0]->fType.columns() == 4); |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1327 | SpvId componentType = this->getType(c.fType.componentType()); |
| 1328 | SpvId v[4]; |
| 1329 | for (int i = 0; i < 4; ++i) { |
| 1330 | v[i] = this->nextId(); |
| 1331 | this->writeInstruction(SpvOpCompositeExtract, componentType, v[i], arguments[0], i, out); |
| 1332 | } |
| 1333 | SpvId columnType = this->getType(c.fType.componentType().toCompound(fContext, 2, 1)); |
| 1334 | SpvId column1 = this->nextId(); |
| 1335 | this->writeInstruction(SpvOpCompositeConstruct, columnType, column1, v[0], v[1], out); |
| 1336 | SpvId column2 = this->nextId(); |
| 1337 | this->writeInstruction(SpvOpCompositeConstruct, columnType, column2, v[2], v[3], out); |
| 1338 | this->writeInstruction(SpvOpCompositeConstruct, this->getType(c.fType), result, column1, |
| 1339 | column2, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1340 | } else { |
Ethan Nicholas | 5c46b72 | 2019-03-22 14:32:37 -0400 | [diff] [blame] | 1341 | SpvId columnType = this->getType(c.fType.componentType().toCompound(fContext, rows, 1)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1342 | std::vector<SpvId> columnIds; |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1343 | // ids of vectors and scalars we have written to the current column so far |
| 1344 | std::vector<SpvId> currentColumn; |
| 1345 | // the total number of scalars represented by currentColumn's entries |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1346 | int currentCount = 0; |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1347 | Precision precision = c.fType.highPrecision() ? Precision::kHigh : Precision::kLow; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1348 | for (size_t i = 0; i < arguments.size(); i++) { |
Ethan Nicholas | 5c46b72 | 2019-03-22 14:32:37 -0400 | [diff] [blame] | 1349 | if (currentCount == 0 && c.fArguments[i]->fType.kind() == Type::kVector_Kind && |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1350 | c.fArguments[i]->fType.columns() == c.fType.rows()) { |
| 1351 | // this is a complete column by itself |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1352 | columnIds.push_back(arguments[i]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1353 | } else { |
Ethan Nicholas | be850ad | 2018-04-27 10:36:31 -0400 | [diff] [blame] | 1354 | if (c.fArguments[i]->fType.columns() == 1) { |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1355 | this->addColumnEntry(columnType, precision, ¤tColumn, &columnIds, |
| 1356 | ¤tCount, rows, arguments[i], out); |
Ethan Nicholas | be850ad | 2018-04-27 10:36:31 -0400 | [diff] [blame] | 1357 | } else { |
| 1358 | SpvId componentType = this->getType(c.fArguments[i]->fType.componentType()); |
Ethan Nicholas | 811b944 | 2018-05-11 13:16:03 -0400 | [diff] [blame] | 1359 | for (int j = 0; j < c.fArguments[i]->fType.columns(); ++j) { |
Ethan Nicholas | be850ad | 2018-04-27 10:36:31 -0400 | [diff] [blame] | 1360 | SpvId swizzle = this->nextId(); |
| 1361 | this->writeInstruction(SpvOpCompositeExtract, componentType, swizzle, |
| 1362 | arguments[i], j, out); |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1363 | this->addColumnEntry(columnType, precision, ¤tColumn, &columnIds, |
| 1364 | ¤tCount, rows, swizzle, out); |
Ethan Nicholas | be850ad | 2018-04-27 10:36:31 -0400 | [diff] [blame] | 1365 | } |
| 1366 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1367 | } |
| 1368 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1369 | SkASSERT(columnIds.size() == (size_t) columns); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1370 | this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1371 | this->writeWord(this->getType(c.fType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1372 | this->writeWord(result, out); |
| 1373 | for (SpvId id : columnIds) { |
| 1374 | this->writeWord(id, out); |
| 1375 | } |
| 1376 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 1377 | this->writePrecisionModifier(c.fType, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1378 | return result; |
| 1379 | } |
| 1380 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1381 | SpvId SPIRVCodeGenerator::writeVectorConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1382 | SkASSERT(c.fType.kind() == Type::kVector_Kind); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1383 | if (c.isConstant()) { |
| 1384 | return this->writeConstantVector(c); |
| 1385 | } |
| 1386 | // go ahead and write the arguments so we don't try to write new instructions in the middle of |
| 1387 | // an instruction |
| 1388 | std::vector<SpvId> arguments; |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1389 | for (size_t i = 0; i < c.fArguments.size(); i++) { |
| 1390 | if (c.fArguments[i]->fType.kind() == Type::kVector_Kind) { |
| 1391 | // SPIR-V doesn't support vector(vector-of-different-type) directly, so we need to |
| 1392 | // extract the components and convert them in that case manually. On top of that, |
| 1393 | // as of this writing there's a bug in the Intel Vulkan driver where OpCreateComposite |
| 1394 | // doesn't handle vector arguments at all, so we always extract vector components and |
| 1395 | // pass them into OpCreateComposite individually. |
| 1396 | SpvId vec = this->writeExpression(*c.fArguments[i], out); |
| 1397 | SpvOp_ op = SpvOpUndef; |
| 1398 | const Type& src = c.fArguments[i]->fType.componentType(); |
| 1399 | const Type& dst = c.fType.componentType(); |
| 1400 | if (dst == *fContext.fFloat_Type || dst == *fContext.fHalf_Type) { |
| 1401 | if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) { |
| 1402 | if (c.fArguments.size() == 1) { |
| 1403 | return vec; |
| 1404 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1405 | } else if (src == *fContext.fInt_Type || |
| 1406 | src == *fContext.fShort_Type || |
| 1407 | src == *fContext.fByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1408 | op = SpvOpConvertSToF; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1409 | } else if (src == *fContext.fUInt_Type || |
| 1410 | src == *fContext.fUShort_Type || |
| 1411 | src == *fContext.fUByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1412 | op = SpvOpConvertUToF; |
| 1413 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1414 | SkASSERT(false); |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1415 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1416 | } else if (dst == *fContext.fInt_Type || |
| 1417 | dst == *fContext.fShort_Type || |
| 1418 | dst == *fContext.fByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1419 | if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) { |
| 1420 | op = SpvOpConvertFToS; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1421 | } else if (src == *fContext.fInt_Type || |
| 1422 | src == *fContext.fShort_Type || |
| 1423 | src == *fContext.fByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1424 | if (c.fArguments.size() == 1) { |
| 1425 | return vec; |
| 1426 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1427 | } else if (src == *fContext.fUInt_Type || |
| 1428 | src == *fContext.fUShort_Type || |
| 1429 | src == *fContext.fUByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1430 | op = SpvOpBitcast; |
| 1431 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1432 | SkASSERT(false); |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1433 | } |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1434 | } else if (dst == *fContext.fUInt_Type || |
| 1435 | dst == *fContext.fUShort_Type || |
| 1436 | dst == *fContext.fUByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1437 | if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) { |
| 1438 | op = SpvOpConvertFToS; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1439 | } else if (src == *fContext.fInt_Type || |
| 1440 | src == *fContext.fShort_Type || |
| 1441 | src == *fContext.fByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1442 | op = SpvOpBitcast; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1443 | } else if (src == *fContext.fUInt_Type || |
| 1444 | src == *fContext.fUShort_Type || |
| 1445 | src == *fContext.fUByte_Type) { |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1446 | if (c.fArguments.size() == 1) { |
| 1447 | return vec; |
| 1448 | } |
| 1449 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1450 | SkASSERT(false); |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1451 | } |
Ethan Nicholas | 26a8d90 | 2018-01-29 09:25:51 -0500 | [diff] [blame] | 1452 | } |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1453 | for (int j = 0; j < c.fArguments[i]->fType.columns(); j++) { |
| 1454 | SpvId swizzle = this->nextId(); |
| 1455 | this->writeInstruction(SpvOpCompositeExtract, this->getType(src), swizzle, vec, j, |
| 1456 | out); |
| 1457 | if (op != SpvOpUndef) { |
| 1458 | SpvId cast = this->nextId(); |
| 1459 | this->writeInstruction(op, this->getType(dst), cast, swizzle, out); |
| 1460 | arguments.push_back(cast); |
| 1461 | } else { |
| 1462 | arguments.push_back(swizzle); |
| 1463 | } |
Ethan Nicholas | 26a8d90 | 2018-01-29 09:25:51 -0500 | [diff] [blame] | 1464 | } |
Ethan Nicholas | 11e5bff | 2018-01-29 11:08:38 -0500 | [diff] [blame] | 1465 | } else { |
Ethan Nicholas | 26a8d90 | 2018-01-29 09:25:51 -0500 | [diff] [blame] | 1466 | arguments.push_back(this->writeExpression(*c.fArguments[i], out)); |
| 1467 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1468 | } |
| 1469 | SpvId result = this->nextId(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1470 | if (arguments.size() == 1 && c.fArguments[0]->fType.kind() == Type::kScalar_Kind) { |
| 1471 | this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType.columns(), out); |
| 1472 | this->writeWord(this->getType(c.fType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1473 | this->writeWord(result, out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1474 | for (int i = 0; i < c.fType.columns(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1475 | this->writeWord(arguments[0], out); |
| 1476 | } |
| 1477 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1478 | SkASSERT(arguments.size() > 1); |
Ethan Nicholas | 26a8d90 | 2018-01-29 09:25:51 -0500 | [diff] [blame] | 1479 | this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) arguments.size(), out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1480 | this->writeWord(this->getType(c.fType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1481 | this->writeWord(result, out); |
| 1482 | for (SpvId id : arguments) { |
| 1483 | this->writeWord(id, out); |
| 1484 | } |
| 1485 | } |
| 1486 | return result; |
| 1487 | } |
| 1488 | |
Ethan Nicholas | bd55322 | 2017-07-18 15:54:59 -0400 | [diff] [blame] | 1489 | SpvId SPIRVCodeGenerator::writeArrayConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1490 | SkASSERT(c.fType.kind() == Type::kArray_Kind); |
Ethan Nicholas | bd55322 | 2017-07-18 15:54:59 -0400 | [diff] [blame] | 1491 | // go ahead and write the arguments so we don't try to write new instructions in the middle of |
| 1492 | // an instruction |
| 1493 | std::vector<SpvId> arguments; |
| 1494 | for (size_t i = 0; i < c.fArguments.size(); i++) { |
| 1495 | arguments.push_back(this->writeExpression(*c.fArguments[i], out)); |
| 1496 | } |
| 1497 | SpvId result = this->nextId(); |
| 1498 | this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) c.fArguments.size(), out); |
| 1499 | this->writeWord(this->getType(c.fType), out); |
| 1500 | this->writeWord(result, out); |
| 1501 | for (SpvId id : arguments) { |
| 1502 | this->writeWord(id, out); |
| 1503 | } |
| 1504 | return result; |
| 1505 | } |
| 1506 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1507 | SpvId SPIRVCodeGenerator::writeConstructor(const Constructor& c, OutputStream& out) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 1508 | if (c.fArguments.size() == 1 && |
| 1509 | this->getActualType(c.fType) == this->getActualType(c.fArguments[0]->fType)) { |
| 1510 | return this->writeExpression(*c.fArguments[0], out); |
| 1511 | } |
| 1512 | if (c.fType == *fContext.fFloat_Type || c.fType == *fContext.fHalf_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1513 | return this->writeFloatConstructor(c, out); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1514 | } else if (c.fType == *fContext.fInt_Type || |
| 1515 | c.fType == *fContext.fShort_Type || |
| 1516 | c.fType == *fContext.fByte_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1517 | return this->writeIntConstructor(c, out); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1518 | } else if (c.fType == *fContext.fUInt_Type || |
| 1519 | c.fType == *fContext.fUShort_Type || |
| 1520 | c.fType == *fContext.fUByte_Type) { |
Ethan Nicholas | 925f52d | 2017-07-19 10:42:50 -0400 | [diff] [blame] | 1521 | return this->writeUIntConstructor(c, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1522 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1523 | switch (c.fType.kind()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1524 | case Type::kVector_Kind: |
| 1525 | return this->writeVectorConstructor(c, out); |
| 1526 | case Type::kMatrix_Kind: |
| 1527 | return this->writeMatrixConstructor(c, out); |
Ethan Nicholas | bd55322 | 2017-07-18 15:54:59 -0400 | [diff] [blame] | 1528 | case Type::kArray_Kind: |
| 1529 | return this->writeArrayConstructor(c, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1530 | default: |
| 1531 | ABORT("unsupported constructor: %s", c.description().c_str()); |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | SpvStorageClass_ get_storage_class(const Modifiers& modifiers) { |
| 1536 | if (modifiers.fFlags & Modifiers::kIn_Flag) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1537 | SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1538 | return SpvStorageClassInput; |
| 1539 | } else if (modifiers.fFlags & Modifiers::kOut_Flag) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1540 | SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1541 | return SpvStorageClassOutput; |
| 1542 | } else if (modifiers.fFlags & Modifiers::kUniform_Flag) { |
Ethan Nicholas | 39204fd | 2017-11-27 13:12:30 -0500 | [diff] [blame] | 1543 | if (modifiers.fLayout.fFlags & Layout::kPushConstant_Flag) { |
ethannicholas | 8ac838d | 2016-11-22 08:39:36 -0800 | [diff] [blame] | 1544 | return SpvStorageClassPushConstant; |
| 1545 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1546 | return SpvStorageClassUniform; |
| 1547 | } else { |
| 1548 | return SpvStorageClassFunction; |
| 1549 | } |
| 1550 | } |
| 1551 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 1552 | SpvStorageClass_ get_storage_class(const Expression& expr) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1553 | switch (expr.fKind) { |
Ethan Nicholas | c6f5e10 | 2017-03-31 14:53:17 -0400 | [diff] [blame] | 1554 | case Expression::kVariableReference_Kind: { |
| 1555 | const Variable& var = ((VariableReference&) expr).fVariable; |
| 1556 | if (var.fStorage != Variable::kGlobal_Storage) { |
| 1557 | return SpvStorageClassFunction; |
| 1558 | } |
Ethan Nicholas | dc0e1c3 | 2017-07-21 13:23:34 -0400 | [diff] [blame] | 1559 | SpvStorageClass_ result = get_storage_class(var.fModifiers); |
| 1560 | if (result == SpvStorageClassFunction) { |
| 1561 | result = SpvStorageClassPrivate; |
| 1562 | } |
| 1563 | return result; |
Ethan Nicholas | c6f5e10 | 2017-03-31 14:53:17 -0400 | [diff] [blame] | 1564 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1565 | case Expression::kFieldAccess_Kind: |
| 1566 | return get_storage_class(*((FieldAccess&) expr).fBase); |
| 1567 | case Expression::kIndex_Kind: |
| 1568 | return get_storage_class(*((IndexExpression&) expr).fBase); |
| 1569 | default: |
| 1570 | return SpvStorageClassFunction; |
| 1571 | } |
| 1572 | } |
| 1573 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1574 | std::vector<SpvId> SPIRVCodeGenerator::getAccessChain(const Expression& expr, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1575 | std::vector<SpvId> chain; |
| 1576 | switch (expr.fKind) { |
| 1577 | case Expression::kIndex_Kind: { |
| 1578 | IndexExpression& indexExpr = (IndexExpression&) expr; |
| 1579 | chain = this->getAccessChain(*indexExpr.fBase, out); |
| 1580 | chain.push_back(this->writeExpression(*indexExpr.fIndex, out)); |
| 1581 | break; |
| 1582 | } |
| 1583 | case Expression::kFieldAccess_Kind: { |
| 1584 | FieldAccess& fieldExpr = (FieldAccess&) expr; |
| 1585 | chain = this->getAccessChain(*fieldExpr.fBase, out); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1586 | IntLiteral index(fContext, -1, fieldExpr.fFieldIndex); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1587 | chain.push_back(this->writeIntLiteral(index)); |
| 1588 | break; |
| 1589 | } |
Ethan Nicholas | a9a0690 | 2019-01-07 14:42:40 -0500 | [diff] [blame] | 1590 | default: { |
| 1591 | SpvId id = this->getLValue(expr, out)->getPointer(); |
| 1592 | SkASSERT(id != 0); |
| 1593 | chain.push_back(id); |
| 1594 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1595 | } |
| 1596 | return chain; |
| 1597 | } |
| 1598 | |
| 1599 | class PointerLValue : public SPIRVCodeGenerator::LValue { |
| 1600 | public: |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1601 | PointerLValue(SPIRVCodeGenerator& gen, SpvId pointer, SpvId type, |
| 1602 | SPIRVCodeGenerator::Precision precision) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1603 | : fGen(gen) |
| 1604 | , fPointer(pointer) |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1605 | , fType(type) |
| 1606 | , fPrecision(precision) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1607 | |
| 1608 | virtual SpvId getPointer() override { |
| 1609 | return fPointer; |
| 1610 | } |
| 1611 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1612 | virtual SpvId load(OutputStream& out) override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1613 | SpvId result = fGen.nextId(); |
| 1614 | fGen.writeInstruction(SpvOpLoad, fType, result, fPointer, out); |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1615 | fGen.writePrecisionModifier(fPrecision, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1616 | return result; |
| 1617 | } |
| 1618 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1619 | virtual void store(SpvId value, OutputStream& out) override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1620 | fGen.writeInstruction(SpvOpStore, fPointer, value, out); |
| 1621 | } |
| 1622 | |
| 1623 | private: |
| 1624 | SPIRVCodeGenerator& fGen; |
| 1625 | const SpvId fPointer; |
| 1626 | const SpvId fType; |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1627 | const SPIRVCodeGenerator::Precision fPrecision; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1628 | }; |
| 1629 | |
| 1630 | class SwizzleLValue : public SPIRVCodeGenerator::LValue { |
| 1631 | public: |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1632 | SwizzleLValue(SPIRVCodeGenerator& gen, SpvId vecPointer, const std::vector<int>& components, |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1633 | const Type& baseType, const Type& swizzleType, |
| 1634 | SPIRVCodeGenerator::Precision precision) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1635 | : fGen(gen) |
| 1636 | , fVecPointer(vecPointer) |
| 1637 | , fComponents(components) |
| 1638 | , fBaseType(baseType) |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1639 | , fSwizzleType(swizzleType) |
| 1640 | , fPrecision(precision) {} |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1641 | |
| 1642 | virtual SpvId getPointer() override { |
| 1643 | return 0; |
| 1644 | } |
| 1645 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1646 | virtual SpvId load(OutputStream& out) override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1647 | SpvId base = fGen.nextId(); |
| 1648 | fGen.writeInstruction(SpvOpLoad, fGen.getType(fBaseType), base, fVecPointer, out); |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1649 | fGen.writePrecisionModifier(fPrecision, base); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1650 | SpvId result = fGen.nextId(); |
| 1651 | fGen.writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) fComponents.size(), out); |
| 1652 | fGen.writeWord(fGen.getType(fSwizzleType), out); |
| 1653 | fGen.writeWord(result, out); |
| 1654 | fGen.writeWord(base, out); |
| 1655 | fGen.writeWord(base, out); |
| 1656 | for (int component : fComponents) { |
| 1657 | fGen.writeWord(component, out); |
| 1658 | } |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1659 | fGen.writePrecisionModifier(fPrecision, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1660 | return result; |
| 1661 | } |
| 1662 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1663 | virtual void store(SpvId value, OutputStream& out) override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1664 | // use OpVectorShuffle to mix and match the vector components. We effectively create |
| 1665 | // 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] | 1666 | // select components from this virtual vector to make the result vector. For |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1667 | // instance, given: |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1668 | // float3L = ...; |
| 1669 | // float3R = ...; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1670 | // L.xz = R.xy; |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1671 | // 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] | 1672 | // our result vector to look like (R.x, L.y, R.y), so we need to select indices |
| 1673 | // (3, 1, 4). |
| 1674 | SpvId base = fGen.nextId(); |
| 1675 | fGen.writeInstruction(SpvOpLoad, fGen.getType(fBaseType), base, fVecPointer, out); |
| 1676 | SpvId shuffle = fGen.nextId(); |
| 1677 | fGen.writeOpCode(SpvOpVectorShuffle, 5 + fBaseType.columns(), out); |
| 1678 | fGen.writeWord(fGen.getType(fBaseType), out); |
| 1679 | fGen.writeWord(shuffle, out); |
| 1680 | fGen.writeWord(base, out); |
| 1681 | fGen.writeWord(value, out); |
| 1682 | for (int i = 0; i < fBaseType.columns(); i++) { |
| 1683 | // current offset into the virtual vector, defaults to pulling the unmodified |
| 1684 | // value from the left side |
| 1685 | int offset = i; |
| 1686 | // check to see if we are writing this component |
| 1687 | for (size_t j = 0; j < fComponents.size(); j++) { |
| 1688 | if (fComponents[j] == i) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1689 | // we're writing to this component, so adjust the offset to pull from |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1690 | // the correct component of the right side instead of preserving the |
| 1691 | // value from the left |
| 1692 | offset = (int) (j + fBaseType.columns()); |
| 1693 | break; |
| 1694 | } |
| 1695 | } |
| 1696 | fGen.writeWord(offset, out); |
| 1697 | } |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1698 | fGen.writePrecisionModifier(fPrecision, shuffle); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1699 | fGen.writeInstruction(SpvOpStore, fVecPointer, shuffle, out); |
| 1700 | } |
| 1701 | |
| 1702 | private: |
| 1703 | SPIRVCodeGenerator& fGen; |
| 1704 | const SpvId fVecPointer; |
| 1705 | const std::vector<int>& fComponents; |
| 1706 | const Type& fBaseType; |
| 1707 | const Type& fSwizzleType; |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1708 | const SPIRVCodeGenerator::Precision fPrecision; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1709 | }; |
| 1710 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1711 | std::unique_ptr<SPIRVCodeGenerator::LValue> SPIRVCodeGenerator::getLValue(const Expression& expr, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1712 | OutputStream& out) { |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1713 | Precision precision = expr.fType.highPrecision() ? Precision::kHigh : Precision::kLow; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1714 | switch (expr.fKind) { |
| 1715 | case Expression::kVariableReference_Kind: { |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 1716 | SpvId type; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1717 | const Variable& var = ((VariableReference&) expr).fVariable; |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 1718 | if (var.fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) { |
| 1719 | type = this->getType(Type("sk_in", Type::kArray_Kind, var.fType.componentType(), |
| 1720 | fSkInCount)); |
| 1721 | } else { |
| 1722 | type = this->getType(expr.fType); |
| 1723 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1724 | auto entry = fVariableMap.find(&var); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1725 | SkASSERT(entry != fVariableMap.end()); |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 1726 | return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(*this, |
| 1727 | entry->second, |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1728 | type, |
| 1729 | precision)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1730 | } |
| 1731 | case Expression::kIndex_Kind: // fall through |
| 1732 | case Expression::kFieldAccess_Kind: { |
| 1733 | std::vector<SpvId> chain = this->getAccessChain(expr, out); |
| 1734 | SpvId member = this->nextId(); |
| 1735 | this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1736 | this->writeWord(this->getPointerType(expr.fType, get_storage_class(expr)), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1737 | this->writeWord(member, out); |
| 1738 | for (SpvId idx : chain) { |
| 1739 | this->writeWord(idx, out); |
| 1740 | } |
| 1741 | return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue( |
Ethan Nicholas | 8f7e28f | 2018-03-26 14:24:27 -0400 | [diff] [blame] | 1742 | *this, |
| 1743 | member, |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1744 | this->getType(expr.fType), |
| 1745 | precision)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1746 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1747 | case Expression::kSwizzle_Kind: { |
| 1748 | Swizzle& swizzle = (Swizzle&) expr; |
| 1749 | size_t count = swizzle.fComponents.size(); |
| 1750 | SpvId base = this->getLValue(*swizzle.fBase, out)->getPointer(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1751 | SkASSERT(base); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1752 | if (count == 1) { |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1753 | IntLiteral index(fContext, -1, swizzle.fComponents[0]); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1754 | SpvId member = this->nextId(); |
| 1755 | this->writeInstruction(SpvOpAccessChain, |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1756 | this->getPointerType(swizzle.fType, |
| 1757 | get_storage_class(*swizzle.fBase)), |
| 1758 | member, |
| 1759 | base, |
| 1760 | this->writeIntLiteral(index), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1761 | out); |
| 1762 | return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue( |
| 1763 | *this, |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1764 | member, |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1765 | this->getType(expr.fType), |
| 1766 | precision)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1767 | } else { |
| 1768 | return std::unique_ptr<SPIRVCodeGenerator::LValue>(new SwizzleLValue( |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1769 | *this, |
| 1770 | base, |
| 1771 | swizzle.fComponents, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1772 | swizzle.fBase->fType, |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1773 | expr.fType, |
| 1774 | precision)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1775 | } |
| 1776 | } |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 1777 | case Expression::kTernary_Kind: { |
| 1778 | TernaryExpression& t = (TernaryExpression&) expr; |
| 1779 | SpvId test = this->writeExpression(*t.fTest, out); |
| 1780 | SpvId end = this->nextId(); |
| 1781 | SpvId ifTrueLabel = this->nextId(); |
| 1782 | SpvId ifFalseLabel = this->nextId(); |
| 1783 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 1784 | this->writeInstruction(SpvOpBranchConditional, test, ifTrueLabel, ifFalseLabel, out); |
| 1785 | this->writeLabel(ifTrueLabel, out); |
| 1786 | SpvId ifTrue = this->getLValue(*t.fIfTrue, out)->getPointer(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1787 | SkASSERT(ifTrue); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 1788 | this->writeInstruction(SpvOpBranch, end, out); |
| 1789 | ifTrueLabel = fCurrentBlock; |
| 1790 | SpvId ifFalse = this->getLValue(*t.fIfFalse, out)->getPointer(); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1791 | SkASSERT(ifFalse); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 1792 | ifFalseLabel = fCurrentBlock; |
| 1793 | this->writeInstruction(SpvOpBranch, end, out); |
| 1794 | SpvId result = this->nextId(); |
| 1795 | this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, ifTrue, |
| 1796 | ifTrueLabel, ifFalse, ifFalseLabel, out); |
| 1797 | return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue( |
| 1798 | *this, |
| 1799 | result, |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1800 | this->getType(expr.fType), |
| 1801 | precision)); |
Ethan Nicholas | a583b81 | 2018-01-18 13:32:11 -0500 | [diff] [blame] | 1802 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1803 | default: |
| 1804 | // 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] | 1805 | // to the need to store values in temporary variables during function calls (see |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1806 | // comments in getFunctionType); erroneous uses of rvalues as lvalues should have been |
| 1807 | // caught by IRGenerator |
| 1808 | SpvId result = this->nextId(); |
| 1809 | SpvId type = this->getPointerType(expr.fType, SpvStorageClassFunction); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1810 | this->writeInstruction(SpvOpVariable, type, result, SpvStorageClassFunction, |
| 1811 | fVariableBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1812 | this->writeInstruction(SpvOpStore, result, this->writeExpression(expr, out), out); |
| 1813 | return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue( |
| 1814 | *this, |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1815 | result, |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1816 | this->getType(expr.fType), |
| 1817 | precision)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1818 | } |
| 1819 | } |
| 1820 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1821 | SpvId SPIRVCodeGenerator::writeVariableReference(const VariableReference& ref, OutputStream& out) { |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1822 | SpvId result = this->nextId(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1823 | auto entry = fVariableMap.find(&ref.fVariable); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 1824 | SkASSERT(entry != fVariableMap.end()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1825 | SpvId var = entry->second; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1826 | this->writeInstruction(SpvOpLoad, this->getType(ref.fVariable.fType), result, var, out); |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 1827 | this->writePrecisionModifier(ref.fVariable.fType, result); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1828 | if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN && |
| 1829 | fProgram.fSettings.fFlipY) { |
| 1830 | // need to remap to a top-left coordinate system |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 1831 | if (fRTHeightStructId == (SpvId) -1) { |
| 1832 | // height variable hasn't been written yet |
| 1833 | std::shared_ptr<SymbolTable> st(new SymbolTable(&fErrors)); |
| 1834 | SkASSERT(fRTHeightFieldIndex == (SpvId) -1); |
| 1835 | std::vector<Type::Field> fields; |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 1836 | SkASSERT(fProgram.fSettings.fRTHeightOffset >= 0); |
| 1837 | fields.emplace_back(Modifiers(Layout(0, -1, fProgram.fSettings.fRTHeightOffset, -1, |
| 1838 | -1, -1, -1, -1, Layout::Format::kUnspecified, |
| 1839 | Layout::kUnspecified_Primitive, -1, -1, "", |
| 1840 | Layout::kNo_Key, Layout::CType::kDefault), 0), |
| 1841 | SKSL_RTHEIGHT_NAME, fContext.fFloat_Type.get()); |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 1842 | StringFragment name("sksl_synthetic_uniforms"); |
| 1843 | Type intfStruct(-1, name, fields); |
Ethan Nicholas | 0be3480 | 2019-08-15 12:36:58 -0400 | [diff] [blame] | 1844 | int binding; |
| 1845 | int set; |
| 1846 | #ifdef SK_VULKAN |
| 1847 | const GrVkCaps* vkCaps = fProgram.fSettings.fVkCaps; |
| 1848 | SkASSERT(vkCaps); |
| 1849 | binding = vkCaps->getFragmentUniformBinding(); |
| 1850 | set = vkCaps->getFragmentUniformSet(); |
| 1851 | #else |
| 1852 | binding = 0; |
| 1853 | set = 0; |
| 1854 | #endif |
| 1855 | Layout layout(0, -1, -1, binding, -1, set, -1, -1, Layout::Format::kUnspecified, |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 1856 | Layout::kUnspecified_Primitive, -1, -1, "", Layout::kNo_Key, |
Ethan Nicholas | 78aceb2 | 2018-08-31 16:13:58 -0400 | [diff] [blame] | 1857 | Layout::CType::kDefault); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1858 | Variable* intfVar = (Variable*) fSynthetics.takeOwnership(std::unique_ptr<Symbol>( |
| 1859 | new Variable(-1, |
| 1860 | Modifiers(layout, Modifiers::kUniform_Flag), |
| 1861 | name, |
| 1862 | intfStruct, |
| 1863 | Variable::kGlobal_Storage))); |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 1864 | InterfaceBlock intf(-1, intfVar, name, String(""), |
| 1865 | std::vector<std::unique_ptr<Expression>>(), st); |
| 1866 | fRTHeightStructId = this->writeInterfaceBlock(intf); |
| 1867 | fRTHeightFieldIndex = 0; |
| 1868 | } |
| 1869 | SkASSERT(fRTHeightFieldIndex != (SpvId) -1); |
Ethan Nicholas | 20798e5 | 2018-12-04 12:30:50 -0500 | [diff] [blame] | 1870 | // write float4(gl_FragCoord.x, u_skRTHeight - gl_FragCoord.y, 0.0, gl_FragCoord.w) |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1871 | SpvId xId = this->nextId(); |
| 1872 | this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), xId, |
| 1873 | result, 0, out); |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 1874 | IntLiteral fieldIndex(fContext, -1, fRTHeightFieldIndex); |
| 1875 | SpvId fieldIndexId = this->writeIntLiteral(fieldIndex); |
| 1876 | SpvId heightPtr = this->nextId(); |
| 1877 | this->writeOpCode(SpvOpAccessChain, 5, out); |
| 1878 | this->writeWord(this->getPointerType(*fContext.fFloat_Type, SpvStorageClassUniform), out); |
| 1879 | this->writeWord(heightPtr, out); |
| 1880 | this->writeWord(fRTHeightStructId, out); |
| 1881 | this->writeWord(fieldIndexId, out); |
| 1882 | SpvId heightRead = this->nextId(); |
| 1883 | this->writeInstruction(SpvOpLoad, this->getType(*fContext.fFloat_Type), heightRead, |
| 1884 | heightPtr, out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1885 | SpvId rawYId = this->nextId(); |
| 1886 | this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), rawYId, |
| 1887 | result, 1, out); |
| 1888 | SpvId flippedYId = this->nextId(); |
| 1889 | this->writeInstruction(SpvOpFSub, this->getType(*fContext.fFloat_Type), flippedYId, |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 1890 | heightRead, rawYId, out); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1891 | FloatLiteral zero(fContext, -1, 0.0); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1892 | SpvId zeroId = writeFloatLiteral(zero); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 1893 | FloatLiteral one(fContext, -1, 1.0); |
Ethan Nicholas | 20798e5 | 2018-12-04 12:30:50 -0500 | [diff] [blame] | 1894 | SpvId wId = this->nextId(); |
| 1895 | this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), wId, |
| 1896 | result, 3, out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1897 | SpvId flipped = this->nextId(); |
| 1898 | this->writeOpCode(SpvOpCompositeConstruct, 7, out); |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 1899 | this->writeWord(this->getType(*fContext.fFloat4_Type), out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1900 | this->writeWord(flipped, out); |
| 1901 | this->writeWord(xId, out); |
| 1902 | this->writeWord(flippedYId, out); |
| 1903 | this->writeWord(zeroId, out); |
Ethan Nicholas | 20798e5 | 2018-12-04 12:30:50 -0500 | [diff] [blame] | 1904 | this->writeWord(wId, out); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 1905 | return flipped; |
| 1906 | } |
Chris Dalton | b91c466 | 2018-08-01 10:46:22 -0600 | [diff] [blame] | 1907 | if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_CLOCKWISE_BUILTIN && |
| 1908 | !fProgram.fSettings.fFlipY) { |
| 1909 | // FrontFacing in Vulkan is defined in terms of a top-down render target. In skia, we use |
| 1910 | // the default convention of "counter-clockwise face is front". |
| 1911 | SpvId inverse = this->nextId(); |
| 1912 | this->writeInstruction(SpvOpLogicalNot, this->getType(*fContext.fBool_Type), inverse, |
| 1913 | result, out); |
| 1914 | return inverse; |
| 1915 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1916 | return result; |
| 1917 | } |
| 1918 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1919 | SpvId SPIRVCodeGenerator::writeIndexExpression(const IndexExpression& expr, OutputStream& out) { |
Ethan Nicholas | a9a0690 | 2019-01-07 14:42:40 -0500 | [diff] [blame] | 1920 | if (expr.fBase->fType.kind() == Type::Kind::kVector_Kind) { |
| 1921 | SpvId base = this->writeExpression(*expr.fBase, out); |
| 1922 | SpvId index = this->writeExpression(*expr.fIndex, out); |
| 1923 | SpvId result = this->nextId(); |
| 1924 | this->writeInstruction(SpvOpVectorExtractDynamic, this->getType(expr.fType), result, base, |
| 1925 | index, out); |
| 1926 | return result; |
| 1927 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1928 | return getLValue(expr, out)->load(out); |
| 1929 | } |
| 1930 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1931 | SpvId SPIRVCodeGenerator::writeFieldAccess(const FieldAccess& f, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1932 | return getLValue(f, out)->load(out); |
| 1933 | } |
| 1934 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1935 | SpvId SPIRVCodeGenerator::writeSwizzle(const Swizzle& swizzle, OutputStream& out) { |
Ethan Nicholas | ffdc3e6 | 2019-09-19 16:58:33 -0400 | [diff] [blame] | 1936 | return this->writeSwizzle(swizzle.fType, swizzle.fBase->fType, |
| 1937 | this->writeExpression(*swizzle.fBase, out), swizzle.fComponents, out); |
| 1938 | } |
| 1939 | |
| 1940 | SpvId SPIRVCodeGenerator::writeSwizzle(const Type& type, const Type& baseType, SpvId base, |
| 1941 | const std::vector<int> components, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1942 | SpvId result = this->nextId(); |
Ethan Nicholas | ffdc3e6 | 2019-09-19 16:58:33 -0400 | [diff] [blame] | 1943 | size_t count = components.size(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1944 | if (count == 1) { |
Ethan Nicholas | ffdc3e6 | 2019-09-19 16:58:33 -0400 | [diff] [blame] | 1945 | this->writeInstruction(SpvOpCompositeExtract, this->getType(type), result, base, |
| 1946 | components[0], out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1947 | } else { |
| 1948 | this->writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) count, out); |
Ethan Nicholas | ffdc3e6 | 2019-09-19 16:58:33 -0400 | [diff] [blame] | 1949 | this->writeWord(this->getType(type), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1950 | this->writeWord(result, out); |
| 1951 | this->writeWord(base, out); |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 1952 | SpvId other = base; |
Ethan Nicholas | ffdc3e6 | 2019-09-19 16:58:33 -0400 | [diff] [blame] | 1953 | for (int c : components) { |
Ethan Nicholas | e455f65 | 2019-09-13 12:52:55 -0400 | [diff] [blame] | 1954 | if (c < 0) { |
| 1955 | if (!fConstantZeroOneVector) { |
| 1956 | FloatLiteral zero(fContext, -1, 0); |
| 1957 | SpvId zeroId = this->writeFloatLiteral(zero); |
| 1958 | FloatLiteral one(fContext, -1, 1); |
| 1959 | SpvId oneId = this->writeFloatLiteral(one); |
| 1960 | SpvId type = this->getType(*fContext.fFloat2_Type); |
| 1961 | fConstantZeroOneVector = this->nextId(); |
| 1962 | this->writeOpCode(SpvOpConstantComposite, 5, fConstantBuffer); |
| 1963 | this->writeWord(type, fConstantBuffer); |
| 1964 | this->writeWord(fConstantZeroOneVector, fConstantBuffer); |
| 1965 | this->writeWord(zeroId, fConstantBuffer); |
| 1966 | this->writeWord(oneId, fConstantBuffer); |
| 1967 | } |
| 1968 | other = fConstantZeroOneVector; |
| 1969 | break; |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 1970 | } |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 1971 | } |
| 1972 | this->writeWord(other, out); |
Ethan Nicholas | ffdc3e6 | 2019-09-19 16:58:33 -0400 | [diff] [blame] | 1973 | for (int component : components) { |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 1974 | if (component == SKSL_SWIZZLE_0) { |
Ethan Nicholas | ffdc3e6 | 2019-09-19 16:58:33 -0400 | [diff] [blame] | 1975 | this->writeWord(baseType.columns(), out); |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 1976 | } else if (component == SKSL_SWIZZLE_1) { |
Ethan Nicholas | ffdc3e6 | 2019-09-19 16:58:33 -0400 | [diff] [blame] | 1977 | this->writeWord(baseType.columns() + 1, out); |
Ethan Nicholas | ac285b1 | 2019-02-12 16:05:18 -0500 | [diff] [blame] | 1978 | } else { |
| 1979 | this->writeWord(component, out); |
| 1980 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1981 | } |
| 1982 | } |
| 1983 | return result; |
| 1984 | } |
| 1985 | |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 1986 | SpvId SPIRVCodeGenerator::writeBinaryOperation(const Type& resultType, |
| 1987 | const Type& operandType, SpvId lhs, |
| 1988 | SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1989 | SpvOp_ ifUInt, SpvOp_ ifBool, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1990 | SpvId result = this->nextId(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1991 | if (is_float(fContext, operandType)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1992 | this->writeInstruction(ifFloat, this->getType(resultType), result, lhs, rhs, out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1993 | } else if (is_signed(fContext, operandType)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1994 | this->writeInstruction(ifInt, this->getType(resultType), result, lhs, rhs, out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1995 | } else if (is_unsigned(fContext, operandType)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1996 | this->writeInstruction(ifUInt, this->getType(resultType), result, lhs, rhs, out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 1997 | } else if (operandType == *fContext.fBool_Type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 1998 | this->writeInstruction(ifBool, this->getType(resultType), result, lhs, rhs, out); |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 1999 | return result; // skip RelaxedPrecision check |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2000 | } else { |
| 2001 | ABORT("invalid operandType: %s", operandType.description().c_str()); |
| 2002 | } |
Ethan Nicholas | e77739e | 2019-03-14 14:04:43 -0400 | [diff] [blame] | 2003 | if (getActualType(resultType) == operandType && !resultType.highPrecision()) { |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 2004 | this->writeInstruction(SpvOpDecorate, result, SpvDecorationRelaxedPrecision, |
| 2005 | fDecorationBuffer); |
| 2006 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2007 | return result; |
| 2008 | } |
| 2009 | |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2010 | SpvId SPIRVCodeGenerator::foldToBool(SpvId id, const Type& operandType, SpvOp op, |
| 2011 | OutputStream& out) { |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 2012 | if (operandType.kind() == Type::kVector_Kind) { |
| 2013 | SpvId result = this->nextId(); |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2014 | this->writeInstruction(op, this->getType(*fContext.fBool_Type), result, id, out); |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 2015 | return result; |
| 2016 | } |
| 2017 | return id; |
| 2018 | } |
| 2019 | |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2020 | SpvId SPIRVCodeGenerator::writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs, |
| 2021 | SpvOp_ floatOperator, SpvOp_ intOperator, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2022 | SpvOp_ vectorMergeOperator, SpvOp_ mergeOperator, |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2023 | OutputStream& out) { |
| 2024 | SpvOp_ compareOp = is_float(fContext, operandType) ? floatOperator : intOperator; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2025 | SkASSERT(operandType.kind() == Type::kMatrix_Kind); |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2026 | SpvId columnType = this->getType(operandType.componentType().toCompound(fContext, |
| 2027 | operandType.rows(), |
| 2028 | 1)); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2029 | SpvId bvecType = this->getType(fContext.fBool_Type->toCompound(fContext, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2030 | operandType.rows(), |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2031 | 1)); |
| 2032 | SpvId boolType = this->getType(*fContext.fBool_Type); |
| 2033 | SpvId result = 0; |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2034 | for (int i = 0; i < operandType.columns(); i++) { |
| 2035 | SpvId columnL = this->nextId(); |
| 2036 | this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out); |
| 2037 | SpvId columnR = this->nextId(); |
| 2038 | this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2039 | SpvId compare = this->nextId(); |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2040 | this->writeInstruction(compareOp, bvecType, compare, columnL, columnR, out); |
| 2041 | SpvId merge = this->nextId(); |
| 2042 | this->writeInstruction(vectorMergeOperator, boolType, merge, compare, out); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2043 | if (result != 0) { |
| 2044 | SpvId next = this->nextId(); |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2045 | this->writeInstruction(mergeOperator, boolType, next, result, merge, out); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2046 | result = next; |
| 2047 | } |
| 2048 | else { |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2049 | result = merge; |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2050 | } |
| 2051 | } |
| 2052 | return result; |
| 2053 | } |
| 2054 | |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2055 | SpvId SPIRVCodeGenerator::writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs, |
| 2056 | SpvId rhs, SpvOp_ floatOperator, |
| 2057 | SpvOp_ intOperator, |
| 2058 | OutputStream& out) { |
| 2059 | SpvOp_ op = is_float(fContext, operandType) ? floatOperator : intOperator; |
| 2060 | SkASSERT(operandType.kind() == Type::kMatrix_Kind); |
| 2061 | SpvId columnType = this->getType(operandType.componentType().toCompound(fContext, |
| 2062 | operandType.rows(), |
| 2063 | 1)); |
| 2064 | SpvId columns[4]; |
| 2065 | for (int i = 0; i < operandType.columns(); i++) { |
| 2066 | SpvId columnL = this->nextId(); |
| 2067 | this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out); |
| 2068 | SpvId columnR = this->nextId(); |
| 2069 | this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out); |
| 2070 | columns[i] = this->nextId(); |
| 2071 | this->writeInstruction(op, columnType, columns[i], columnL, columnR, out); |
| 2072 | } |
| 2073 | SpvId result = this->nextId(); |
| 2074 | this->writeOpCode(SpvOpCompositeConstruct, 3 + operandType.columns(), out); |
| 2075 | this->writeWord(this->getType(operandType), out); |
| 2076 | this->writeWord(result, out); |
| 2077 | for (int i = 0; i < operandType.columns(); i++) { |
| 2078 | this->writeWord(columns[i], out); |
| 2079 | } |
| 2080 | return result; |
| 2081 | } |
| 2082 | |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2083 | std::unique_ptr<Expression> create_literal_1(const Context& context, const Type& type) { |
| 2084 | if (type.isInteger()) { |
| 2085 | return std::unique_ptr<Expression>(new IntLiteral(-1, 1, &type)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2086 | } |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2087 | else if (type.isFloat()) { |
| 2088 | return std::unique_ptr<Expression>(new FloatLiteral(-1, 1.0, &type)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2089 | } else { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2090 | ABORT("math is unsupported on type '%s'", type.name().c_str()); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2091 | } |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | SpvId SPIRVCodeGenerator::writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op, |
| 2095 | const Type& rightType, SpvId rhs, |
| 2096 | const Type& resultType, OutputStream& out) { |
Ethan Nicholas | ffdc3e6 | 2019-09-19 16:58:33 -0400 | [diff] [blame] | 2097 | // it's important to handle comma early, so we don't end up vectorizing the operands |
| 2098 | if (op == Token::COMMA) { |
| 2099 | return rhs; |
| 2100 | } |
| 2101 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 2102 | Type tmp("<invalid>"); |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2103 | // overall type we are operating on: float2, int, uint4... |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2104 | const Type* operandType; |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2105 | // IR allows mismatched types in expressions (e.g. float2 * float), but they need special |
| 2106 | // handling in SPIR-V |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2107 | if (this->getActualType(leftType) != this->getActualType(rightType)) { |
| 2108 | if (leftType.kind() == Type::kVector_Kind && rightType.isNumber()) { |
| 2109 | if (op == Token::SLASH) { |
| 2110 | SpvId one = this->writeExpression(*create_literal_1(fContext, rightType), out); |
| 2111 | SpvId inverse = this->nextId(); |
| 2112 | this->writeInstruction(SpvOpFDiv, this->getType(rightType), inverse, one, rhs, out); |
| 2113 | rhs = inverse; |
| 2114 | op = Token::STAR; |
| 2115 | } |
| 2116 | if (op == Token::STAR) { |
| 2117 | SpvId result = this->nextId(); |
| 2118 | this->writeInstruction(SpvOpVectorTimesScalar, this->getType(resultType), |
| 2119 | result, lhs, rhs, out); |
| 2120 | return result; |
| 2121 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2122 | // promote number to vector |
| 2123 | SpvId vec = this->nextId(); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2124 | const Type& vecType = leftType; |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2125 | this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out); |
| 2126 | this->writeWord(this->getType(vecType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2127 | this->writeWord(vec, out); |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2128 | for (int i = 0; i < vecType.columns(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2129 | this->writeWord(rhs, out); |
| 2130 | } |
| 2131 | rhs = vec; |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2132 | operandType = &leftType; |
| 2133 | } else if (rightType.kind() == Type::kVector_Kind && leftType.isNumber()) { |
| 2134 | if (op == Token::STAR) { |
| 2135 | SpvId result = this->nextId(); |
| 2136 | this->writeInstruction(SpvOpVectorTimesScalar, this->getType(resultType), |
| 2137 | result, rhs, lhs, out); |
| 2138 | return result; |
| 2139 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2140 | // promote number to vector |
| 2141 | SpvId vec = this->nextId(); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2142 | const Type& vecType = rightType; |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2143 | this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out); |
| 2144 | this->writeWord(this->getType(vecType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2145 | this->writeWord(vec, out); |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2146 | for (int i = 0; i < vecType.columns(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2147 | this->writeWord(lhs, out); |
| 2148 | } |
| 2149 | lhs = vec; |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2150 | operandType = &rightType; |
| 2151 | } else if (leftType.kind() == Type::kMatrix_Kind) { |
| 2152 | SpvOp_ spvop; |
| 2153 | if (rightType.kind() == Type::kMatrix_Kind) { |
| 2154 | spvop = SpvOpMatrixTimesMatrix; |
| 2155 | } else if (rightType.kind() == Type::kVector_Kind) { |
| 2156 | spvop = SpvOpMatrixTimesVector; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2157 | } else { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2158 | SkASSERT(rightType.kind() == Type::kScalar_Kind); |
| 2159 | spvop = SpvOpMatrixTimesScalar; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2160 | } |
| 2161 | SpvId result = this->nextId(); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2162 | this->writeInstruction(spvop, this->getType(resultType), result, lhs, rhs, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2163 | return result; |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2164 | } else if (rightType.kind() == Type::kMatrix_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2165 | SpvId result = this->nextId(); |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2166 | if (leftType.kind() == Type::kVector_Kind) { |
| 2167 | this->writeInstruction(SpvOpVectorTimesMatrix, this->getType(resultType), result, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2168 | lhs, rhs, out); |
| 2169 | } else { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2170 | SkASSERT(leftType.kind() == Type::kScalar_Kind); |
| 2171 | this->writeInstruction(SpvOpMatrixTimesScalar, this->getType(resultType), result, |
| 2172 | rhs, lhs, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2173 | } |
| 2174 | return result; |
| 2175 | } else { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2176 | SkASSERT(false); |
| 2177 | return -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2178 | } |
| 2179 | } else { |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2180 | tmp = this->getActualType(leftType); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 2181 | operandType = &tmp; |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2182 | SkASSERT(*operandType == this->getActualType(rightType)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2183 | } |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2184 | switch (op) { |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 2185 | case Token::EQEQ: { |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2186 | if (operandType->kind() == Type::kMatrix_Kind) { |
| 2187 | return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdEqual, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2188 | SpvOpIEqual, SpvOpAll, SpvOpLogicalAnd, out); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2189 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2190 | SkASSERT(resultType == *fContext.fBool_Type); |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2191 | const Type* tmpType; |
| 2192 | if (operandType->kind() == Type::kVector_Kind) { |
| 2193 | tmpType = &fContext.fBool_Type->toCompound(fContext, |
| 2194 | operandType->columns(), |
| 2195 | operandType->rows()); |
| 2196 | } else { |
| 2197 | tmpType = &resultType; |
| 2198 | } |
| 2199 | return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs, |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 2200 | SpvOpFOrdEqual, SpvOpIEqual, |
| 2201 | SpvOpIEqual, SpvOpLogicalEqual, out), |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2202 | *operandType, SpvOpAll, out); |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 2203 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2204 | case Token::NEQ: |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2205 | if (operandType->kind() == Type::kMatrix_Kind) { |
| 2206 | return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdNotEqual, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2207 | SpvOpINotEqual, SpvOpAny, SpvOpLogicalOr, out); |
Ethan Nicholas | 68990be | 2017-07-13 09:36:52 -0400 | [diff] [blame] | 2208 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2209 | SkASSERT(resultType == *fContext.fBool_Type); |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2210 | const Type* tmpType; |
| 2211 | if (operandType->kind() == Type::kVector_Kind) { |
| 2212 | tmpType = &fContext.fBool_Type->toCompound(fContext, |
| 2213 | operandType->columns(), |
| 2214 | operandType->rows()); |
| 2215 | } else { |
| 2216 | tmpType = &resultType; |
| 2217 | } |
| 2218 | return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs, |
Ethan Nicholas | ef653b8 | 2017-02-21 13:50:00 -0500 | [diff] [blame] | 2219 | SpvOpFOrdNotEqual, SpvOpINotEqual, |
| 2220 | SpvOpINotEqual, SpvOpLogicalNotEqual, |
| 2221 | out), |
Ethan Nicholas | 48e2405 | 2018-03-14 13:51:39 -0400 | [diff] [blame] | 2222 | *operandType, SpvOpAny, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2223 | case Token::GT: |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2224 | SkASSERT(resultType == *fContext.fBool_Type); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2225 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, |
| 2226 | SpvOpFOrdGreaterThan, SpvOpSGreaterThan, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2227 | SpvOpUGreaterThan, SpvOpUndef, out); |
| 2228 | case Token::LT: |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2229 | SkASSERT(resultType == *fContext.fBool_Type); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2230 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFOrdLessThan, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2231 | SpvOpSLessThan, SpvOpULessThan, SpvOpUndef, out); |
| 2232 | case Token::GTEQ: |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2233 | SkASSERT(resultType == *fContext.fBool_Type); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2234 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, |
| 2235 | SpvOpFOrdGreaterThanEqual, SpvOpSGreaterThanEqual, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2236 | SpvOpUGreaterThanEqual, SpvOpUndef, out); |
| 2237 | case Token::LTEQ: |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2238 | SkASSERT(resultType == *fContext.fBool_Type); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2239 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, |
| 2240 | SpvOpFOrdLessThanEqual, SpvOpSLessThanEqual, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2241 | SpvOpULessThanEqual, SpvOpUndef, out); |
| 2242 | case Token::PLUS: |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2243 | if (leftType.kind() == Type::kMatrix_Kind && |
| 2244 | rightType.kind() == Type::kMatrix_Kind) { |
| 2245 | SkASSERT(leftType == rightType); |
| 2246 | return this->writeComponentwiseMatrixBinary(leftType, lhs, rhs, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2247 | SpvOpFAdd, SpvOpIAdd, out); |
| 2248 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2249 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFAdd, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2250 | SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out); |
| 2251 | case Token::MINUS: |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2252 | if (leftType.kind() == Type::kMatrix_Kind && |
| 2253 | rightType.kind() == Type::kMatrix_Kind) { |
| 2254 | SkASSERT(leftType == rightType); |
| 2255 | return this->writeComponentwiseMatrixBinary(leftType, lhs, rhs, |
Ethan Nicholas | 0df2113 | 2018-07-10 09:37:51 -0400 | [diff] [blame] | 2256 | SpvOpFSub, SpvOpISub, out); |
| 2257 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2258 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFSub, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2259 | SpvOpISub, SpvOpISub, SpvOpUndef, out); |
| 2260 | case Token::STAR: |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2261 | if (leftType.kind() == Type::kMatrix_Kind && |
| 2262 | rightType.kind() == Type::kMatrix_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2263 | // matrix multiply |
| 2264 | SpvId result = this->nextId(); |
| 2265 | this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(resultType), result, |
| 2266 | lhs, rhs, out); |
| 2267 | return result; |
| 2268 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2269 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMul, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2270 | SpvOpIMul, SpvOpIMul, SpvOpUndef, out); |
| 2271 | case Token::SLASH: |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2272 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFDiv, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2273 | SpvOpSDiv, SpvOpUDiv, SpvOpUndef, out); |
Ethan Nicholas | b3d0f7c | 2017-05-17 13:13:21 -0400 | [diff] [blame] | 2274 | case Token::PERCENT: |
| 2275 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMod, |
| 2276 | SpvOpSMod, SpvOpUMod, SpvOpUndef, out); |
Ethan Nicholas | fd444be | 2017-07-05 10:05:54 -0400 | [diff] [blame] | 2277 | case Token::SHL: |
| 2278 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef, |
| 2279 | SpvOpShiftLeftLogical, SpvOpShiftLeftLogical, |
| 2280 | SpvOpUndef, out); |
| 2281 | case Token::SHR: |
| 2282 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef, |
| 2283 | SpvOpShiftRightArithmetic, SpvOpShiftRightLogical, |
| 2284 | SpvOpUndef, out); |
| 2285 | case Token::BITWISEAND: |
| 2286 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef, |
| 2287 | SpvOpBitwiseAnd, SpvOpBitwiseAnd, SpvOpUndef, out); |
| 2288 | case Token::BITWISEOR: |
| 2289 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef, |
| 2290 | SpvOpBitwiseOr, SpvOpBitwiseOr, SpvOpUndef, out); |
| 2291 | case Token::BITWISEXOR: |
| 2292 | return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef, |
| 2293 | SpvOpBitwiseXor, SpvOpBitwiseXor, SpvOpUndef, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2294 | default: |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2295 | SkASSERT(false); |
| 2296 | return -1; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2297 | } |
| 2298 | } |
| 2299 | |
Ethan Nicholas | 49465b4 | 2019-04-17 12:22:21 -0400 | [diff] [blame] | 2300 | SpvId SPIRVCodeGenerator::writeBinaryExpression(const BinaryExpression& b, OutputStream& out) { |
| 2301 | // handle cases where we don't necessarily evaluate both LHS and RHS |
| 2302 | switch (b.fOperator) { |
| 2303 | case Token::EQ: { |
| 2304 | SpvId rhs = this->writeExpression(*b.fRight, out); |
| 2305 | this->getLValue(*b.fLeft, out)->store(rhs, out); |
| 2306 | return rhs; |
| 2307 | } |
| 2308 | case Token::LOGICALAND: |
| 2309 | return this->writeLogicalAnd(b, out); |
| 2310 | case Token::LOGICALOR: |
| 2311 | return this->writeLogicalOr(b, out); |
| 2312 | default: |
| 2313 | break; |
| 2314 | } |
| 2315 | |
| 2316 | std::unique_ptr<LValue> lvalue; |
| 2317 | SpvId lhs; |
| 2318 | if (is_assignment(b.fOperator)) { |
| 2319 | lvalue = this->getLValue(*b.fLeft, out); |
| 2320 | lhs = lvalue->load(out); |
| 2321 | } else { |
| 2322 | lvalue = nullptr; |
| 2323 | lhs = this->writeExpression(*b.fLeft, out); |
| 2324 | } |
| 2325 | SpvId rhs = this->writeExpression(*b.fRight, out); |
| 2326 | SpvId result = this->writeBinaryExpression(b.fLeft->fType, lhs, remove_assignment(b.fOperator), |
| 2327 | b.fRight->fType, rhs, b.fType, out); |
| 2328 | if (lvalue) { |
| 2329 | lvalue->store(result, out); |
| 2330 | } |
| 2331 | return result; |
| 2332 | } |
| 2333 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2334 | SpvId SPIRVCodeGenerator::writeLogicalAnd(const BinaryExpression& a, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2335 | SkASSERT(a.fOperator == Token::LOGICALAND); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2336 | BoolLiteral falseLiteral(fContext, -1, false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2337 | SpvId falseConstant = this->writeBoolLiteral(falseLiteral); |
| 2338 | SpvId lhs = this->writeExpression(*a.fLeft, out); |
| 2339 | SpvId rhsLabel = this->nextId(); |
| 2340 | SpvId end = this->nextId(); |
| 2341 | SpvId lhsBlock = fCurrentBlock; |
| 2342 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 2343 | this->writeInstruction(SpvOpBranchConditional, lhs, rhsLabel, end, out); |
| 2344 | this->writeLabel(rhsLabel, out); |
| 2345 | SpvId rhs = this->writeExpression(*a.fRight, out); |
| 2346 | SpvId rhsBlock = fCurrentBlock; |
| 2347 | this->writeInstruction(SpvOpBranch, end, out); |
| 2348 | this->writeLabel(end, out); |
| 2349 | SpvId result = this->nextId(); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2350 | this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, falseConstant, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2351 | lhsBlock, rhs, rhsBlock, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2352 | return result; |
| 2353 | } |
| 2354 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2355 | SpvId SPIRVCodeGenerator::writeLogicalOr(const BinaryExpression& o, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2356 | SkASSERT(o.fOperator == Token::LOGICALOR); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2357 | BoolLiteral trueLiteral(fContext, -1, true); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2358 | SpvId trueConstant = this->writeBoolLiteral(trueLiteral); |
| 2359 | SpvId lhs = this->writeExpression(*o.fLeft, out); |
| 2360 | SpvId rhsLabel = this->nextId(); |
| 2361 | SpvId end = this->nextId(); |
| 2362 | SpvId lhsBlock = fCurrentBlock; |
| 2363 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 2364 | this->writeInstruction(SpvOpBranchConditional, lhs, end, rhsLabel, out); |
| 2365 | this->writeLabel(rhsLabel, out); |
| 2366 | SpvId rhs = this->writeExpression(*o.fRight, out); |
| 2367 | SpvId rhsBlock = fCurrentBlock; |
| 2368 | this->writeInstruction(SpvOpBranch, end, out); |
| 2369 | this->writeLabel(end, out); |
| 2370 | SpvId result = this->nextId(); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2371 | this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, trueConstant, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2372 | lhsBlock, rhs, rhsBlock, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2373 | return result; |
| 2374 | } |
| 2375 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2376 | SpvId SPIRVCodeGenerator::writeTernaryExpression(const TernaryExpression& t, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2377 | SpvId test = this->writeExpression(*t.fTest, out); |
Ethan Nicholas | 0f21c23 | 2018-07-23 09:25:40 -0400 | [diff] [blame] | 2378 | if (t.fIfTrue->fType.columns() == 1 && t.fIfTrue->isConstant() && t.fIfFalse->isConstant()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2379 | // both true and false are constants, can just use OpSelect |
| 2380 | SpvId result = this->nextId(); |
| 2381 | SpvId trueId = this->writeExpression(*t.fIfTrue, out); |
| 2382 | SpvId falseId = this->writeExpression(*t.fIfFalse, out); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2383 | this->writeInstruction(SpvOpSelect, this->getType(t.fType), result, test, trueId, falseId, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2384 | out); |
| 2385 | return result; |
| 2386 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2387 | // 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] | 2388 | // Adreno. Switched to storing the result in a temp variable as glslang does. |
| 2389 | SpvId var = this->nextId(); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2390 | this->writeInstruction(SpvOpVariable, this->getPointerType(t.fType, SpvStorageClassFunction), |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2391 | var, SpvStorageClassFunction, fVariableBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2392 | SpvId trueLabel = this->nextId(); |
| 2393 | SpvId falseLabel = this->nextId(); |
| 2394 | SpvId end = this->nextId(); |
| 2395 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 2396 | this->writeInstruction(SpvOpBranchConditional, test, trueLabel, falseLabel, out); |
| 2397 | this->writeLabel(trueLabel, out); |
| 2398 | this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfTrue, out), out); |
| 2399 | this->writeInstruction(SpvOpBranch, end, out); |
| 2400 | this->writeLabel(falseLabel, out); |
| 2401 | this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfFalse, out), out); |
| 2402 | this->writeInstruction(SpvOpBranch, end, out); |
| 2403 | this->writeLabel(end, out); |
| 2404 | SpvId result = this->nextId(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2405 | this->writeInstruction(SpvOpLoad, this->getType(t.fType), result, var, out); |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 2406 | this->writePrecisionModifier(t.fType, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2407 | return result; |
| 2408 | } |
| 2409 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2410 | SpvId SPIRVCodeGenerator::writePrefixExpression(const PrefixExpression& p, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2411 | if (p.fOperator == Token::MINUS) { |
| 2412 | SpvId result = this->nextId(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2413 | SpvId typeId = this->getType(p.fType); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2414 | SpvId expr = this->writeExpression(*p.fOperand, out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2415 | if (is_float(fContext, p.fType)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2416 | this->writeInstruction(SpvOpFNegate, typeId, result, expr, out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2417 | } else if (is_signed(fContext, p.fType)) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2418 | this->writeInstruction(SpvOpSNegate, typeId, result, expr, out); |
| 2419 | } else { |
| 2420 | ABORT("unsupported prefix expression %s", p.description().c_str()); |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 2421 | } |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 2422 | this->writePrecisionModifier(p.fType, result); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2423 | return result; |
| 2424 | } |
| 2425 | switch (p.fOperator) { |
| 2426 | case Token::PLUS: |
| 2427 | return this->writeExpression(*p.fOperand, out); |
| 2428 | case Token::PLUSPLUS: { |
| 2429 | std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2430 | SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2431 | SpvId result = this->writeBinaryOperation(p.fType, p.fType, lv->load(out), one, |
| 2432 | SpvOpFAdd, SpvOpIAdd, SpvOpIAdd, SpvOpUndef, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2433 | out); |
| 2434 | lv->store(result, out); |
| 2435 | return result; |
| 2436 | } |
| 2437 | case Token::MINUSMINUS: { |
| 2438 | std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2439 | SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2440 | SpvId result = this->writeBinaryOperation(p.fType, p.fType, lv->load(out), one, |
| 2441 | SpvOpFSub, SpvOpISub, SpvOpISub, SpvOpUndef, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2442 | out); |
| 2443 | lv->store(result, out); |
| 2444 | return result; |
| 2445 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2446 | case Token::LOGICALNOT: { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2447 | SkASSERT(p.fOperand->fType == *fContext.fBool_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2448 | SpvId result = this->nextId(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2449 | this->writeInstruction(SpvOpLogicalNot, this->getType(p.fOperand->fType), result, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2450 | this->writeExpression(*p.fOperand, out), out); |
| 2451 | return result; |
| 2452 | } |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2453 | case Token::BITWISENOT: { |
| 2454 | SpvId result = this->nextId(); |
| 2455 | this->writeInstruction(SpvOpNot, this->getType(p.fOperand->fType), result, |
| 2456 | this->writeExpression(*p.fOperand, out), out); |
| 2457 | return result; |
| 2458 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2459 | default: |
| 2460 | ABORT("unsupported prefix expression: %s", p.description().c_str()); |
| 2461 | } |
| 2462 | } |
| 2463 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2464 | SpvId SPIRVCodeGenerator::writePostfixExpression(const PostfixExpression& p, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2465 | std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out); |
| 2466 | SpvId result = lv->load(out); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2467 | SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2468 | switch (p.fOperator) { |
| 2469 | case Token::PLUSPLUS: { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2470 | SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, one, SpvOpFAdd, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2471 | SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out); |
| 2472 | lv->store(temp, out); |
| 2473 | return result; |
| 2474 | } |
| 2475 | case Token::MINUSMINUS: { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2476 | SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, one, SpvOpFSub, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2477 | SpvOpISub, SpvOpISub, SpvOpUndef, out); |
| 2478 | lv->store(temp, out); |
| 2479 | return result; |
| 2480 | } |
| 2481 | default: |
| 2482 | ABORT("unsupported postfix expression %s", p.description().c_str()); |
| 2483 | } |
| 2484 | } |
| 2485 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 2486 | SpvId SPIRVCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2487 | if (b.fValue) { |
| 2488 | if (fBoolTrue == 0) { |
| 2489 | fBoolTrue = this->nextId(); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2490 | this->writeInstruction(SpvOpConstantTrue, this->getType(b.fType), fBoolTrue, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2491 | fConstantBuffer); |
| 2492 | } |
| 2493 | return fBoolTrue; |
| 2494 | } else { |
| 2495 | if (fBoolFalse == 0) { |
| 2496 | fBoolFalse = this->nextId(); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2497 | this->writeInstruction(SpvOpConstantFalse, this->getType(b.fType), fBoolFalse, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2498 | fConstantBuffer); |
| 2499 | } |
| 2500 | return fBoolFalse; |
| 2501 | } |
| 2502 | } |
| 2503 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 2504 | SpvId SPIRVCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2505 | ConstantType type; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2506 | if (i.fType == *fContext.fInt_Type) { |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2507 | type = ConstantType::kInt; |
| 2508 | } else if (i.fType == *fContext.fUInt_Type) { |
| 2509 | type = ConstantType::kUInt; |
Ethan Nicholas | c2d84bf | 2019-09-09 10:49:15 -0400 | [diff] [blame] | 2510 | } else if (i.fType == *fContext.fShort_Type || i.fType == *fContext.fByte_Type) { |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2511 | type = ConstantType::kShort; |
Ethan Nicholas | c2d84bf | 2019-09-09 10:49:15 -0400 | [diff] [blame] | 2512 | } else if (i.fType == *fContext.fUShort_Type || i.fType == *fContext.fUByte_Type) { |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2513 | type = ConstantType::kUShort; |
Ethan Nicholas | c2d84bf | 2019-09-09 10:49:15 -0400 | [diff] [blame] | 2514 | } else { |
| 2515 | SkASSERT(false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2516 | } |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2517 | std::pair<ConstantValue, ConstantType> key(i.fValue, type); |
| 2518 | auto entry = fNumberConstants.find(key); |
| 2519 | if (entry == fNumberConstants.end()) { |
| 2520 | SpvId result = this->nextId(); |
| 2521 | this->writeInstruction(SpvOpConstant, this->getType(i.fType), result, (SpvId) i.fValue, |
| 2522 | fConstantBuffer); |
| 2523 | fNumberConstants[key] = result; |
| 2524 | return result; |
| 2525 | } |
| 2526 | return entry->second; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2527 | } |
| 2528 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 2529 | SpvId SPIRVCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 2530 | if (f.fType != *fContext.fDouble_Type) { |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2531 | ConstantType type; |
| 2532 | if (f.fType == *fContext.fHalf_Type) { |
| 2533 | type = ConstantType::kHalf; |
| 2534 | } else { |
| 2535 | type = ConstantType::kFloat; |
| 2536 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2537 | float value = (float) f.fValue; |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2538 | std::pair<ConstantValue, ConstantType> key(f.fValue, type); |
| 2539 | auto entry = fNumberConstants.find(key); |
| 2540 | if (entry == fNumberConstants.end()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2541 | SpvId result = this->nextId(); |
| 2542 | uint32_t bits; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2543 | SkASSERT(sizeof(bits) == sizeof(value)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2544 | memcpy(&bits, &value, sizeof(bits)); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2545 | this->writeInstruction(SpvOpConstant, this->getType(f.fType), result, bits, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2546 | fConstantBuffer); |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2547 | fNumberConstants[key] = result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2548 | return result; |
| 2549 | } |
| 2550 | return entry->second; |
| 2551 | } else { |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2552 | std::pair<ConstantValue, ConstantType> key(f.fValue, ConstantType::kDouble); |
| 2553 | auto entry = fNumberConstants.find(key); |
| 2554 | if (entry == fNumberConstants.end()) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2555 | SpvId result = this->nextId(); |
| 2556 | uint64_t bits; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2557 | SkASSERT(sizeof(bits) == sizeof(f.fValue)); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2558 | memcpy(&bits, &f.fValue, sizeof(bits)); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2559 | this->writeInstruction(SpvOpConstant, this->getType(f.fType), result, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2560 | bits & 0xffffffff, bits >> 32, fConstantBuffer); |
Ethan Nicholas | cc5d3e0 | 2019-04-19 09:50:56 -0400 | [diff] [blame] | 2561 | fNumberConstants[key] = result; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2562 | return result; |
| 2563 | } |
| 2564 | return entry->second; |
| 2565 | } |
| 2566 | } |
| 2567 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2568 | SpvId SPIRVCodeGenerator::writeFunctionStart(const FunctionDeclaration& f, OutputStream& out) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2569 | SpvId result = fFunctionMap[&f]; |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2570 | this->writeInstruction(SpvOpFunction, this->getType(f.fReturnType), result, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2571 | SpvFunctionControlMaskNone, this->getFunctionType(f), out); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2572 | this->writeInstruction(SpvOpName, result, f.fName, fNameBuffer); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2573 | for (size_t i = 0; i < f.fParameters.size(); i++) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2574 | SpvId id = this->nextId(); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2575 | fVariableMap[f.fParameters[i]] = id; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2576 | SpvId type; |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2577 | type = this->getPointerType(f.fParameters[i]->fType, SpvStorageClassFunction); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2578 | this->writeInstruction(SpvOpFunctionParameter, type, id, out); |
| 2579 | } |
| 2580 | return result; |
| 2581 | } |
| 2582 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2583 | SpvId SPIRVCodeGenerator::writeFunction(const FunctionDefinition& f, OutputStream& out) { |
| 2584 | fVariableBuffer.reset(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2585 | SpvId result = this->writeFunctionStart(f.fDeclaration, out); |
| 2586 | this->writeLabel(this->nextId(), out); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2587 | StringStream bodyBuffer; |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 2588 | this->writeBlock((Block&) *f.fBody, bodyBuffer); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2589 | write_stringstream(fVariableBuffer, out); |
Ethan Nicholas | 8eb64d3 | 2018-12-21 14:50:42 -0500 | [diff] [blame] | 2590 | if (f.fDeclaration.fName == "main") { |
| 2591 | write_stringstream(fGlobalInitializersBuffer, out); |
| 2592 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2593 | write_stringstream(bodyBuffer, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2594 | if (fCurrentBlock) { |
Ethan Nicholas | 70a44b2 | 2017-11-30 09:09:16 -0500 | [diff] [blame] | 2595 | if (f.fDeclaration.fReturnType == *fContext.fVoid_Type) { |
| 2596 | this->writeInstruction(SpvOpReturn, out); |
| 2597 | } else { |
| 2598 | this->writeInstruction(SpvOpUnreachable, out); |
| 2599 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2600 | } |
| 2601 | this->writeInstruction(SpvOpFunctionEnd, out); |
| 2602 | return result; |
| 2603 | } |
| 2604 | |
| 2605 | void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target) { |
| 2606 | if (layout.fLocation >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2607 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationLocation, layout.fLocation, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2608 | fDecorationBuffer); |
| 2609 | } |
| 2610 | if (layout.fBinding >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2611 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationBinding, layout.fBinding, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2612 | fDecorationBuffer); |
| 2613 | } |
| 2614 | if (layout.fIndex >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2615 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationIndex, layout.fIndex, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2616 | fDecorationBuffer); |
| 2617 | } |
| 2618 | if (layout.fSet >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2619 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationDescriptorSet, layout.fSet, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2620 | fDecorationBuffer); |
| 2621 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2622 | if (layout.fInputAttachmentIndex >= 0) { |
| 2623 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationInputAttachmentIndex, |
| 2624 | layout.fInputAttachmentIndex, fDecorationBuffer); |
Ethan Nicholas | be1099f | 2018-01-11 16:09:05 -0500 | [diff] [blame] | 2625 | fCapabilities |= (((uint64_t) 1) << SpvCapabilityInputAttachment); |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2626 | } |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 2627 | if (layout.fBuiltin >= 0 && layout.fBuiltin != SK_FRAGCOLOR_BUILTIN && |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 2628 | layout.fBuiltin != SK_IN_BUILTIN && layout.fBuiltin != SK_OUT_BUILTIN) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2629 | this->writeInstruction(SpvOpDecorate, target, SpvDecorationBuiltIn, layout.fBuiltin, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2630 | fDecorationBuffer); |
| 2631 | } |
| 2632 | } |
| 2633 | |
| 2634 | void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target, int member) { |
| 2635 | if (layout.fLocation >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2636 | this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationLocation, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2637 | layout.fLocation, fDecorationBuffer); |
| 2638 | } |
| 2639 | if (layout.fBinding >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2640 | this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBinding, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2641 | layout.fBinding, fDecorationBuffer); |
| 2642 | } |
| 2643 | if (layout.fIndex >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2644 | this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationIndex, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2645 | layout.fIndex, fDecorationBuffer); |
| 2646 | } |
| 2647 | if (layout.fSet >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2648 | this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationDescriptorSet, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2649 | layout.fSet, fDecorationBuffer); |
| 2650 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2651 | if (layout.fInputAttachmentIndex >= 0) { |
| 2652 | this->writeInstruction(SpvOpDecorate, target, member, SpvDecorationInputAttachmentIndex, |
| 2653 | layout.fInputAttachmentIndex, fDecorationBuffer); |
| 2654 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2655 | if (layout.fBuiltin >= 0) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2656 | this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBuiltIn, |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2657 | layout.fBuiltin, fDecorationBuffer); |
| 2658 | } |
| 2659 | } |
| 2660 | |
Ethan Nicholas | 81d1511 | 2018-07-13 12:48:50 -0400 | [diff] [blame] | 2661 | static void update_sk_in_count(const Modifiers& m, int* outSkInCount) { |
| 2662 | switch (m.fLayout.fPrimitive) { |
| 2663 | case Layout::kPoints_Primitive: |
| 2664 | *outSkInCount = 1; |
| 2665 | break; |
| 2666 | case Layout::kLines_Primitive: |
| 2667 | *outSkInCount = 2; |
| 2668 | break; |
| 2669 | case Layout::kLinesAdjacency_Primitive: |
| 2670 | *outSkInCount = 4; |
| 2671 | break; |
| 2672 | case Layout::kTriangles_Primitive: |
| 2673 | *outSkInCount = 3; |
| 2674 | break; |
| 2675 | case Layout::kTrianglesAdjacency_Primitive: |
| 2676 | *outSkInCount = 6; |
| 2677 | break; |
| 2678 | default: |
| 2679 | return; |
| 2680 | } |
| 2681 | } |
| 2682 | |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 2683 | SpvId SPIRVCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) { |
Ethan Nicholas | 361941e | 2017-05-30 15:41:07 -0400 | [diff] [blame] | 2684 | bool isBuffer = (0 != (intf.fVariable.fModifiers.fFlags & Modifiers::kBuffer_Flag)); |
Ethan Nicholas | 39204fd | 2017-11-27 13:12:30 -0500 | [diff] [blame] | 2685 | bool pushConstant = (0 != (intf.fVariable.fModifiers.fLayout.fFlags & |
| 2686 | Layout::kPushConstant_Flag)); |
Ethan Nicholas | 8d2ba44 | 2018-03-16 16:40:36 -0400 | [diff] [blame] | 2687 | MemoryLayout memoryLayout = (pushConstant || isBuffer) ? |
| 2688 | MemoryLayout(MemoryLayout::k430_Standard) : |
| 2689 | fDefaultLayout; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2690 | SpvId result = this->nextId(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 2691 | const Type* type = &intf.fVariable.fType; |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 2692 | if (fProgram.fInputs.fRTHeight) { |
| 2693 | SkASSERT(fRTHeightStructId == (SpvId) -1); |
| 2694 | SkASSERT(fRTHeightFieldIndex == (SpvId) -1); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 2695 | std::vector<Type::Field> fields = type->fields(); |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 2696 | fRTHeightStructId = result; |
| 2697 | fRTHeightFieldIndex = fields.size(); |
| 2698 | fields.emplace_back(Modifiers(), StringFragment(SKSL_RTHEIGHT_NAME), fContext.fFloat_Type.get()); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2699 | type = new Type(type->fOffset, type->name(), fields); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 2700 | } |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 2701 | SpvId typeId; |
| 2702 | if (intf.fVariable.fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) { |
| 2703 | for (const auto& e : fProgram) { |
| 2704 | if (e.fKind == ProgramElement::kModifiers_Kind) { |
| 2705 | const Modifiers& m = ((ModifiersDeclaration&) e).fModifiers; |
Ethan Nicholas | 81d1511 | 2018-07-13 12:48:50 -0400 | [diff] [blame] | 2706 | update_sk_in_count(m, &fSkInCount); |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 2707 | } |
| 2708 | } |
| 2709 | typeId = this->getType(Type("sk_in", Type::kArray_Kind, intf.fVariable.fType.componentType(), |
| 2710 | fSkInCount), memoryLayout); |
| 2711 | } else { |
| 2712 | typeId = this->getType(*type, memoryLayout); |
| 2713 | } |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 2714 | if (intf.fVariable.fModifiers.fFlags & Modifiers::kBuffer_Flag) { |
| 2715 | this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBufferBlock, fDecorationBuffer); |
Ethan Nicholas | 6ac8d36 | 2019-01-22 21:43:55 +0000 | [diff] [blame] | 2716 | } else if (intf.fVariable.fModifiers.fLayout.fBuiltin == -1) { |
| 2717 | this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBlock, fDecorationBuffer); |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 2718 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2719 | SpvStorageClass_ storageClass = get_storage_class(intf.fVariable.fModifiers); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2720 | SpvId ptrType = this->nextId(); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 2721 | this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, typeId, fConstantBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2722 | this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConstantBuffer); |
Ethan Nicholas | 8d2ba44 | 2018-03-16 16:40:36 -0400 | [diff] [blame] | 2723 | Layout layout = intf.fVariable.fModifiers.fLayout; |
| 2724 | if (intf.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag && layout.fSet == -1) { |
| 2725 | layout.fSet = 0; |
| 2726 | } |
| 2727 | this->writeLayout(layout, result); |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2728 | fVariableMap[&intf.fVariable] = result; |
Greg Daniel | e6ab998 | 2018-08-22 13:56:32 +0000 | [diff] [blame] | 2729 | if (fProgram.fInputs.fRTHeight) { |
Ethan Nicholas | 39b101b | 2017-03-01 12:07:28 -0500 | [diff] [blame] | 2730 | delete type; |
| 2731 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2732 | return result; |
| 2733 | } |
| 2734 | |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 2735 | void SPIRVCodeGenerator::writePrecisionModifier(const Type& type, SpvId id) { |
Ethan Nicholas | 10e93b6 | 2019-03-20 10:46:14 -0400 | [diff] [blame] | 2736 | this->writePrecisionModifier(type.highPrecision() ? Precision::kHigh : Precision::kLow, id); |
| 2737 | } |
| 2738 | |
| 2739 | void SPIRVCodeGenerator::writePrecisionModifier(Precision precision, SpvId id) { |
| 2740 | if (precision == Precision::kLow) { |
Ethan Nicholas | a51d713 | 2017-06-09 10:47:31 -0400 | [diff] [blame] | 2741 | this->writeInstruction(SpvOpDecorate, id, SpvDecorationRelaxedPrecision, fDecorationBuffer); |
| 2742 | } |
| 2743 | } |
| 2744 | |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2745 | #define BUILTIN_IGNORE 9999 |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2746 | void SPIRVCodeGenerator::writeGlobalVars(Program::Kind kind, const VarDeclarations& decl, |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2747 | OutputStream& out) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 2748 | for (size_t i = 0; i < decl.fVars.size(); i++) { |
| 2749 | if (decl.fVars[i]->fKind == Statement::kNop_Kind) { |
| 2750 | continue; |
| 2751 | } |
| 2752 | const VarDeclaration& varDecl = (VarDeclaration&) *decl.fVars[i]; |
| 2753 | const Variable* var = varDecl.fVar; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 2754 | // These haven't been implemented in our SPIR-V generator yet and we only currently use them |
| 2755 | // in the OpenGL backend. |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2756 | SkASSERT(!(var->fModifiers.fFlags & (Modifiers::kReadOnly_Flag | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 2757 | Modifiers::kWriteOnly_Flag | |
| 2758 | Modifiers::kCoherent_Flag | |
| 2759 | Modifiers::kVolatile_Flag | |
| 2760 | Modifiers::kRestrict_Flag))); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2761 | if (var->fModifiers.fLayout.fBuiltin == BUILTIN_IGNORE) { |
| 2762 | continue; |
| 2763 | } |
| 2764 | if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN && |
| 2765 | kind != Program::kFragment_Kind) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2766 | SkASSERT(!fProgram.fSettings.fFragColorIsInOut); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 2767 | continue; |
| 2768 | } |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 2769 | if (!var->fReadCount && !var->fWriteCount && |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 2770 | !(var->fModifiers.fFlags & (Modifiers::kIn_Flag | |
| 2771 | Modifiers::kOut_Flag | |
Ethan Nicholas | 0dd30d9 | 2017-05-01 16:57:07 -0400 | [diff] [blame] | 2772 | Modifiers::kUniform_Flag | |
| 2773 | Modifiers::kBuffer_Flag))) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 2774 | // variable is dead and not an input / output var (the Vulkan debug layers complain if |
| 2775 | // we elide an interface var, even if it's dead) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2776 | continue; |
| 2777 | } |
| 2778 | SpvStorageClass_ storageClass; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 2779 | if (var->fModifiers.fFlags & Modifiers::kIn_Flag) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2780 | storageClass = SpvStorageClassInput; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 2781 | } else if (var->fModifiers.fFlags & Modifiers::kOut_Flag) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2782 | storageClass = SpvStorageClassOutput; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 2783 | } else if (var->fModifiers.fFlags & Modifiers::kUniform_Flag) { |
Stephen White | ff5d7a2 | 2019-07-26 17:42:06 -0400 | [diff] [blame] | 2784 | if (var->fType.kind() == Type::kSampler_Kind || |
| 2785 | var->fType.kind() == Type::kSeparateSampler_Kind || |
| 2786 | var->fType.kind() == Type::kTexture_Kind) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2787 | storageClass = SpvStorageClassUniformConstant; |
| 2788 | } else { |
| 2789 | storageClass = SpvStorageClassUniform; |
| 2790 | } |
| 2791 | } else { |
| 2792 | storageClass = SpvStorageClassPrivate; |
| 2793 | } |
| 2794 | SpvId id = this->nextId(); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 2795 | fVariableMap[var] = id; |
Ethan Nicholas | 5226b77 | 2018-05-03 16:20:41 -0400 | [diff] [blame] | 2796 | SpvId type; |
| 2797 | if (var->fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) { |
| 2798 | type = this->getPointerType(Type("sk_in", Type::kArray_Kind, |
| 2799 | var->fType.componentType(), fSkInCount), |
| 2800 | storageClass); |
| 2801 | } else { |
| 2802 | type = this->getPointerType(var->fType, storageClass); |
| 2803 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2804 | this->writeInstruction(SpvOpVariable, type, id, storageClass, fConstantBuffer); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2805 | this->writeInstruction(SpvOpName, id, var->fName, fNameBuffer); |
Ethan Nicholas | 858fecc | 2019-03-07 13:19:18 -0500 | [diff] [blame] | 2806 | this->writePrecisionModifier(var->fType, id); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 2807 | if (varDecl.fValue) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2808 | SkASSERT(!fCurrentBlock); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 2809 | fCurrentBlock = -1; |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 2810 | SpvId value = this->writeExpression(*varDecl.fValue, fGlobalInitializersBuffer); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2811 | this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuffer); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 2812 | fCurrentBlock = 0; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2813 | } |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 2814 | this->writeLayout(var->fModifiers.fLayout, id); |
Ethan Nicholas | 45b0f15 | 2017-07-24 14:36:40 -0400 | [diff] [blame] | 2815 | if (var->fModifiers.fFlags & Modifiers::kFlat_Flag) { |
| 2816 | this->writeInstruction(SpvOpDecorate, id, SpvDecorationFlat, fDecorationBuffer); |
| 2817 | } |
| 2818 | if (var->fModifiers.fFlags & Modifiers::kNoPerspective_Flag) { |
| 2819 | this->writeInstruction(SpvOpDecorate, id, SpvDecorationNoPerspective, |
| 2820 | fDecorationBuffer); |
| 2821 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2822 | } |
| 2823 | } |
| 2824 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2825 | void SPIRVCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, OutputStream& out) { |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 2826 | for (const auto& stmt : decl.fVars) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2827 | SkASSERT(stmt->fKind == Statement::kVarDeclaration_Kind); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 2828 | VarDeclaration& varDecl = (VarDeclaration&) *stmt; |
| 2829 | const Variable* var = varDecl.fVar; |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 2830 | // These haven't been implemented in our SPIR-V generator yet and we only currently use them |
| 2831 | // in the OpenGL backend. |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 2832 | SkASSERT(!(var->fModifiers.fFlags & (Modifiers::kReadOnly_Flag | |
Brian Salomon | f9f4512 | 2016-11-29 11:59:17 -0500 | [diff] [blame] | 2833 | Modifiers::kWriteOnly_Flag | |
| 2834 | Modifiers::kCoherent_Flag | |
| 2835 | Modifiers::kVolatile_Flag | |
| 2836 | Modifiers::kRestrict_Flag))); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2837 | SpvId id = this->nextId(); |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 2838 | fVariableMap[var] = id; |
| 2839 | SpvId type = this->getPointerType(var->fType, SpvStorageClassFunction); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2840 | this->writeInstruction(SpvOpVariable, type, id, SpvStorageClassFunction, fVariableBuffer); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2841 | this->writeInstruction(SpvOpName, id, var->fName, fNameBuffer); |
Ethan Nicholas | 82a62d2 | 2017-11-07 14:42:10 +0000 | [diff] [blame] | 2842 | if (varDecl.fValue) { |
| 2843 | SpvId value = this->writeExpression(*varDecl.fValue, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2844 | this->writeInstruction(SpvOpStore, id, value, out); |
| 2845 | } |
| 2846 | } |
| 2847 | } |
| 2848 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2849 | void SPIRVCodeGenerator::writeStatement(const Statement& s, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2850 | switch (s.fKind) { |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 2851 | case Statement::kNop_Kind: |
| 2852 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2853 | case Statement::kBlock_Kind: |
| 2854 | this->writeBlock((Block&) s, out); |
| 2855 | break; |
| 2856 | case Statement::kExpression_Kind: |
| 2857 | this->writeExpression(*((ExpressionStatement&) s).fExpression, out); |
| 2858 | break; |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 2859 | case Statement::kReturn_Kind: |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2860 | this->writeReturnStatement((ReturnStatement&) s, out); |
| 2861 | break; |
ethannicholas | 14fe8cc | 2016-09-07 13:37:16 -0700 | [diff] [blame] | 2862 | case Statement::kVarDeclarations_Kind: |
| 2863 | this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2864 | break; |
| 2865 | case Statement::kIf_Kind: |
| 2866 | this->writeIfStatement((IfStatement&) s, out); |
| 2867 | break; |
| 2868 | case Statement::kFor_Kind: |
| 2869 | this->writeForStatement((ForStatement&) s, out); |
| 2870 | break; |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 2871 | case Statement::kWhile_Kind: |
| 2872 | this->writeWhileStatement((WhileStatement&) s, out); |
| 2873 | break; |
| 2874 | case Statement::kDo_Kind: |
| 2875 | this->writeDoStatement((DoStatement&) s, out); |
| 2876 | break; |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 2877 | case Statement::kSwitch_Kind: |
| 2878 | this->writeSwitchStatement((SwitchStatement&) s, out); |
| 2879 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2880 | case Statement::kBreak_Kind: |
| 2881 | this->writeInstruction(SpvOpBranch, fBreakTarget.top(), out); |
| 2882 | break; |
| 2883 | case Statement::kContinue_Kind: |
| 2884 | this->writeInstruction(SpvOpBranch, fContinueTarget.top(), out); |
| 2885 | break; |
| 2886 | case Statement::kDiscard_Kind: |
| 2887 | this->writeInstruction(SpvOpKill, out); |
| 2888 | break; |
| 2889 | default: |
| 2890 | ABORT("unsupported statement: %s", s.description().c_str()); |
| 2891 | } |
| 2892 | } |
| 2893 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2894 | void SPIRVCodeGenerator::writeBlock(const Block& b, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2895 | for (size_t i = 0; i < b.fStatements.size(); i++) { |
| 2896 | this->writeStatement(*b.fStatements[i], out); |
| 2897 | } |
| 2898 | } |
| 2899 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2900 | void SPIRVCodeGenerator::writeIfStatement(const IfStatement& stmt, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2901 | SpvId test = this->writeExpression(*stmt.fTest, out); |
| 2902 | SpvId ifTrue = this->nextId(); |
| 2903 | SpvId ifFalse = this->nextId(); |
| 2904 | if (stmt.fIfFalse) { |
| 2905 | SpvId end = this->nextId(); |
| 2906 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 2907 | this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out); |
| 2908 | this->writeLabel(ifTrue, out); |
| 2909 | this->writeStatement(*stmt.fIfTrue, out); |
| 2910 | if (fCurrentBlock) { |
| 2911 | this->writeInstruction(SpvOpBranch, end, out); |
| 2912 | } |
| 2913 | this->writeLabel(ifFalse, out); |
| 2914 | this->writeStatement(*stmt.fIfFalse, out); |
| 2915 | if (fCurrentBlock) { |
| 2916 | this->writeInstruction(SpvOpBranch, end, out); |
| 2917 | } |
| 2918 | this->writeLabel(end, out); |
| 2919 | } else { |
| 2920 | this->writeInstruction(SpvOpSelectionMerge, ifFalse, SpvSelectionControlMaskNone, out); |
| 2921 | this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out); |
| 2922 | this->writeLabel(ifTrue, out); |
| 2923 | this->writeStatement(*stmt.fIfTrue, out); |
| 2924 | if (fCurrentBlock) { |
| 2925 | this->writeInstruction(SpvOpBranch, ifFalse, out); |
| 2926 | } |
| 2927 | this->writeLabel(ifFalse, out); |
| 2928 | } |
| 2929 | } |
| 2930 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2931 | void SPIRVCodeGenerator::writeForStatement(const ForStatement& f, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2932 | if (f.fInitializer) { |
| 2933 | this->writeStatement(*f.fInitializer, out); |
| 2934 | } |
| 2935 | SpvId header = this->nextId(); |
| 2936 | SpvId start = this->nextId(); |
| 2937 | SpvId body = this->nextId(); |
| 2938 | SpvId next = this->nextId(); |
| 2939 | fContinueTarget.push(next); |
| 2940 | SpvId end = this->nextId(); |
| 2941 | fBreakTarget.push(end); |
| 2942 | this->writeInstruction(SpvOpBranch, header, out); |
| 2943 | this->writeLabel(header, out); |
| 2944 | this->writeInstruction(SpvOpLoopMerge, end, next, SpvLoopControlMaskNone, out); |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 2945 | this->writeInstruction(SpvOpBranch, start, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2946 | this->writeLabel(start, out); |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 2947 | if (f.fTest) { |
| 2948 | SpvId test = this->writeExpression(*f.fTest, out); |
| 2949 | this->writeInstruction(SpvOpBranchConditional, test, body, end, out); |
| 2950 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 2951 | this->writeLabel(body, out); |
| 2952 | this->writeStatement(*f.fStatement, out); |
| 2953 | if (fCurrentBlock) { |
| 2954 | this->writeInstruction(SpvOpBranch, next, out); |
| 2955 | } |
| 2956 | this->writeLabel(next, out); |
| 2957 | if (f.fNext) { |
| 2958 | this->writeExpression(*f.fNext, out); |
| 2959 | } |
| 2960 | this->writeInstruction(SpvOpBranch, header, out); |
| 2961 | this->writeLabel(end, out); |
| 2962 | fBreakTarget.pop(); |
| 2963 | fContinueTarget.pop(); |
| 2964 | } |
| 2965 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2966 | void SPIRVCodeGenerator::writeWhileStatement(const WhileStatement& w, OutputStream& out) { |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 2967 | SpvId header = this->nextId(); |
| 2968 | SpvId start = this->nextId(); |
| 2969 | SpvId body = this->nextId(); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 2970 | SpvId continueTarget = this->nextId(); |
| 2971 | fContinueTarget.push(continueTarget); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 2972 | SpvId end = this->nextId(); |
| 2973 | fBreakTarget.push(end); |
| 2974 | this->writeInstruction(SpvOpBranch, header, out); |
| 2975 | this->writeLabel(header, out); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 2976 | this->writeInstruction(SpvOpLoopMerge, end, continueTarget, SpvLoopControlMaskNone, out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 2977 | this->writeInstruction(SpvOpBranch, start, out); |
| 2978 | this->writeLabel(start, out); |
| 2979 | SpvId test = this->writeExpression(*w.fTest, out); |
| 2980 | this->writeInstruction(SpvOpBranchConditional, test, body, end, out); |
| 2981 | this->writeLabel(body, out); |
| 2982 | this->writeStatement(*w.fStatement, out); |
| 2983 | if (fCurrentBlock) { |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 2984 | this->writeInstruction(SpvOpBranch, continueTarget, out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 2985 | } |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 2986 | this->writeLabel(continueTarget, out); |
| 2987 | this->writeInstruction(SpvOpBranch, header, out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 2988 | this->writeLabel(end, out); |
| 2989 | fBreakTarget.pop(); |
| 2990 | fContinueTarget.pop(); |
| 2991 | } |
| 2992 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 2993 | void SPIRVCodeGenerator::writeDoStatement(const DoStatement& d, OutputStream& out) { |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 2994 | // We believe the do loop code below will work, but Skia doesn't actually use them and |
| 2995 | // adequately testing this code in the absence of Skia exercising it isn't straightforward. For |
| 2996 | // the time being, we just fail with an error due to the lack of testing. If you encounter this |
| 2997 | // message, simply remove the error call below to see whether our do loop support actually |
| 2998 | // works. |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 2999 | fErrors.error(d.fOffset, "internal error: do loop support has been disabled in SPIR-V, see " |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 3000 | "SkSLSPIRVCodeGenerator.cpp for details"); |
| 3001 | |
| 3002 | SpvId header = this->nextId(); |
| 3003 | SpvId start = this->nextId(); |
| 3004 | SpvId next = this->nextId(); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 3005 | SpvId continueTarget = this->nextId(); |
| 3006 | fContinueTarget.push(continueTarget); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 3007 | SpvId end = this->nextId(); |
| 3008 | fBreakTarget.push(end); |
| 3009 | this->writeInstruction(SpvOpBranch, header, out); |
| 3010 | this->writeLabel(header, out); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 3011 | this->writeInstruction(SpvOpLoopMerge, end, continueTarget, SpvLoopControlMaskNone, out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 3012 | this->writeInstruction(SpvOpBranch, start, out); |
| 3013 | this->writeLabel(start, out); |
| 3014 | this->writeStatement(*d.fStatement, out); |
| 3015 | if (fCurrentBlock) { |
| 3016 | this->writeInstruction(SpvOpBranch, next, out); |
| 3017 | } |
| 3018 | this->writeLabel(next, out); |
| 3019 | SpvId test = this->writeExpression(*d.fTest, out); |
Ethan Nicholas | 0d99766 | 2019-04-08 09:46:01 -0400 | [diff] [blame] | 3020 | this->writeInstruction(SpvOpBranchConditional, test, continueTarget, end, out); |
| 3021 | this->writeLabel(continueTarget, out); |
| 3022 | this->writeInstruction(SpvOpBranch, header, out); |
Ethan Nicholas | fd146aa | 2017-01-13 16:40:35 -0500 | [diff] [blame] | 3023 | this->writeLabel(end, out); |
| 3024 | fBreakTarget.pop(); |
| 3025 | fContinueTarget.pop(); |
| 3026 | } |
| 3027 | |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 3028 | void SPIRVCodeGenerator::writeSwitchStatement(const SwitchStatement& s, OutputStream& out) { |
| 3029 | SpvId value = this->writeExpression(*s.fValue, out); |
| 3030 | std::vector<SpvId> labels; |
| 3031 | SpvId end = this->nextId(); |
| 3032 | SpvId defaultLabel = end; |
| 3033 | fBreakTarget.push(end); |
| 3034 | int size = 3; |
| 3035 | for (const auto& c : s.fCases) { |
| 3036 | SpvId label = this->nextId(); |
| 3037 | labels.push_back(label); |
| 3038 | if (c->fValue) { |
| 3039 | size += 2; |
| 3040 | } else { |
| 3041 | defaultLabel = label; |
| 3042 | } |
| 3043 | } |
| 3044 | labels.push_back(end); |
| 3045 | this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out); |
| 3046 | this->writeOpCode(SpvOpSwitch, size, out); |
| 3047 | this->writeWord(value, out); |
| 3048 | this->writeWord(defaultLabel, out); |
| 3049 | for (size_t i = 0; i < s.fCases.size(); ++i) { |
| 3050 | if (!s.fCases[i]->fValue) { |
| 3051 | continue; |
| 3052 | } |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 3053 | SkASSERT(s.fCases[i]->fValue->fKind == Expression::kIntLiteral_Kind); |
Ethan Nicholas | e92b1b1 | 2017-11-13 16:13:21 -0500 | [diff] [blame] | 3054 | this->writeWord(((IntLiteral&) *s.fCases[i]->fValue).fValue, out); |
| 3055 | this->writeWord(labels[i], out); |
| 3056 | } |
| 3057 | for (size_t i = 0; i < s.fCases.size(); ++i) { |
| 3058 | this->writeLabel(labels[i], out); |
| 3059 | for (const auto& stmt : s.fCases[i]->fStatements) { |
| 3060 | this->writeStatement(*stmt, out); |
| 3061 | } |
| 3062 | if (fCurrentBlock) { |
| 3063 | this->writeInstruction(SpvOpBranch, labels[i + 1], out); |
| 3064 | } |
| 3065 | } |
| 3066 | this->writeLabel(end, out); |
| 3067 | fBreakTarget.pop(); |
| 3068 | } |
| 3069 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3070 | void SPIRVCodeGenerator::writeReturnStatement(const ReturnStatement& r, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3071 | if (r.fExpression) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 3072 | this->writeInstruction(SpvOpReturnValue, this->writeExpression(*r.fExpression, out), |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3073 | out); |
| 3074 | } else { |
| 3075 | this->writeInstruction(SpvOpReturn, out); |
| 3076 | } |
| 3077 | } |
| 3078 | |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3079 | void SPIRVCodeGenerator::writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 3080 | SkASSERT(fProgram.fKind == Program::kGeometry_Kind); |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3081 | int invocations = 1; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 3082 | for (const auto& e : fProgram) { |
| 3083 | if (e.fKind == ProgramElement::kModifiers_Kind) { |
| 3084 | const Modifiers& m = ((ModifiersDeclaration&) e).fModifiers; |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3085 | if (m.fFlags & Modifiers::kIn_Flag) { |
| 3086 | if (m.fLayout.fInvocations != -1) { |
| 3087 | invocations = m.fLayout.fInvocations; |
| 3088 | } |
| 3089 | SpvId input; |
| 3090 | switch (m.fLayout.fPrimitive) { |
| 3091 | case Layout::kPoints_Primitive: |
| 3092 | input = SpvExecutionModeInputPoints; |
| 3093 | break; |
| 3094 | case Layout::kLines_Primitive: |
| 3095 | input = SpvExecutionModeInputLines; |
| 3096 | break; |
| 3097 | case Layout::kLinesAdjacency_Primitive: |
| 3098 | input = SpvExecutionModeInputLinesAdjacency; |
| 3099 | break; |
| 3100 | case Layout::kTriangles_Primitive: |
| 3101 | input = SpvExecutionModeTriangles; |
| 3102 | break; |
| 3103 | case Layout::kTrianglesAdjacency_Primitive: |
| 3104 | input = SpvExecutionModeInputTrianglesAdjacency; |
| 3105 | break; |
| 3106 | default: |
| 3107 | input = 0; |
| 3108 | break; |
| 3109 | } |
Ethan Nicholas | 81d1511 | 2018-07-13 12:48:50 -0400 | [diff] [blame] | 3110 | update_sk_in_count(m, &fSkInCount); |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3111 | if (input) { |
| 3112 | this->writeInstruction(SpvOpExecutionMode, entryPoint, input, out); |
| 3113 | } |
| 3114 | } else if (m.fFlags & Modifiers::kOut_Flag) { |
| 3115 | SpvId output; |
| 3116 | switch (m.fLayout.fPrimitive) { |
| 3117 | case Layout::kPoints_Primitive: |
| 3118 | output = SpvExecutionModeOutputPoints; |
| 3119 | break; |
| 3120 | case Layout::kLineStrip_Primitive: |
| 3121 | output = SpvExecutionModeOutputLineStrip; |
| 3122 | break; |
| 3123 | case Layout::kTriangleStrip_Primitive: |
| 3124 | output = SpvExecutionModeOutputTriangleStrip; |
| 3125 | break; |
| 3126 | default: |
| 3127 | output = 0; |
| 3128 | break; |
| 3129 | } |
| 3130 | if (output) { |
| 3131 | this->writeInstruction(SpvOpExecutionMode, entryPoint, output, out); |
| 3132 | } |
| 3133 | if (m.fLayout.fMaxVertices != -1) { |
| 3134 | this->writeInstruction(SpvOpExecutionMode, entryPoint, |
| 3135 | SpvExecutionModeOutputVertices, m.fLayout.fMaxVertices, |
| 3136 | out); |
| 3137 | } |
| 3138 | } |
| 3139 | } |
| 3140 | } |
| 3141 | this->writeInstruction(SpvOpExecutionMode, entryPoint, SpvExecutionModeInvocations, |
| 3142 | invocations, out); |
| 3143 | } |
| 3144 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3145 | void SPIRVCodeGenerator::writeInstructions(const Program& program, OutputStream& out) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3146 | fGLSLExtendedInstructions = this->nextId(); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3147 | StringStream body; |
Ethan Nicholas | 8e48c1e | 2017-03-02 14:33:31 -0500 | [diff] [blame] | 3148 | std::set<SpvId> interfaceVars; |
Ethan Nicholas | b6ba82c | 2018-01-17 15:21:50 -0500 | [diff] [blame] | 3149 | // assign IDs to functions, determine sk_in size |
| 3150 | int skInSize = -1; |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 3151 | for (const auto& e : program) { |
| 3152 | switch (e.fKind) { |
Ethan Nicholas | b6ba82c | 2018-01-17 15:21:50 -0500 | [diff] [blame] | 3153 | case ProgramElement::kFunction_Kind: { |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 3154 | FunctionDefinition& f = (FunctionDefinition&) e; |
Ethan Nicholas | b6ba82c | 2018-01-17 15:21:50 -0500 | [diff] [blame] | 3155 | fFunctionMap[&f.fDeclaration] = this->nextId(); |
| 3156 | break; |
| 3157 | } |
| 3158 | case ProgramElement::kModifiers_Kind: { |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 3159 | Modifiers& m = ((ModifiersDeclaration&) e).fModifiers; |
Ethan Nicholas | b6ba82c | 2018-01-17 15:21:50 -0500 | [diff] [blame] | 3160 | if (m.fFlags & Modifiers::kIn_Flag) { |
| 3161 | switch (m.fLayout.fPrimitive) { |
| 3162 | case Layout::kPoints_Primitive: // break |
| 3163 | case Layout::kLines_Primitive: |
| 3164 | skInSize = 1; |
| 3165 | break; |
| 3166 | case Layout::kLinesAdjacency_Primitive: // break |
| 3167 | skInSize = 2; |
| 3168 | break; |
| 3169 | case Layout::kTriangles_Primitive: // break |
| 3170 | case Layout::kTrianglesAdjacency_Primitive: |
| 3171 | skInSize = 3; |
| 3172 | break; |
| 3173 | default: |
| 3174 | break; |
| 3175 | } |
| 3176 | } |
| 3177 | break; |
| 3178 | } |
| 3179 | default: |
| 3180 | break; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3181 | } |
| 3182 | } |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 3183 | for (const auto& e : program) { |
| 3184 | if (e.fKind == ProgramElement::kInterfaceBlock_Kind) { |
| 3185 | InterfaceBlock& intf = (InterfaceBlock&) e; |
Ethan Nicholas | b6ba82c | 2018-01-17 15:21:50 -0500 | [diff] [blame] | 3186 | if (SK_IN_BUILTIN == intf.fVariable.fModifiers.fLayout.fBuiltin) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 3187 | SkASSERT(skInSize != -1); |
Ethan Nicholas | b6ba82c | 2018-01-17 15:21:50 -0500 | [diff] [blame] | 3188 | intf.fSizes.emplace_back(new IntLiteral(fContext, -1, skInSize)); |
| 3189 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3190 | SpvId id = this->writeInterfaceBlock(intf); |
Ethan Nicholas | 16c1196 | 2018-03-16 12:20:54 -0400 | [diff] [blame] | 3191 | if (((intf.fVariable.fModifiers.fFlags & Modifiers::kIn_Flag) || |
| 3192 | (intf.fVariable.fModifiers.fFlags & Modifiers::kOut_Flag)) && |
| 3193 | intf.fVariable.fModifiers.fLayout.fBuiltin == -1) { |
Ethan Nicholas | 8e48c1e | 2017-03-02 14:33:31 -0500 | [diff] [blame] | 3194 | interfaceVars.insert(id); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3195 | } |
| 3196 | } |
| 3197 | } |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 3198 | for (const auto& e : program) { |
| 3199 | if (e.fKind == ProgramElement::kVar_Kind) { |
| 3200 | this->writeGlobalVars(program.fKind, ((VarDeclarations&) e), body); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3201 | } |
| 3202 | } |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 3203 | for (const auto& e : program) { |
| 3204 | if (e.fKind == ProgramElement::kFunction_Kind) { |
| 3205 | this->writeFunction(((FunctionDefinition&) e), body); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3206 | } |
| 3207 | } |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 3208 | const FunctionDeclaration* main = nullptr; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3209 | for (auto entry : fFunctionMap) { |
ethannicholas | f789b38 | 2016-08-03 12:43:36 -0700 | [diff] [blame] | 3210 | if (entry.first->fName == "main") { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3211 | main = entry.first; |
| 3212 | } |
| 3213 | } |
Ethan Nicholas | 1a668d2 | 2019-04-18 10:37:40 -0400 | [diff] [blame] | 3214 | if (!main) { |
| 3215 | fErrors.error(0, "program does not contain a main() function"); |
| 3216 | return; |
| 3217 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3218 | for (auto entry : fVariableMap) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 3219 | const Variable* var = entry.first; |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 3220 | if (var->fStorage == Variable::kGlobal_Storage && |
Ethan Nicholas | 16c1196 | 2018-03-16 12:20:54 -0400 | [diff] [blame] | 3221 | ((var->fModifiers.fFlags & Modifiers::kIn_Flag) || |
Ethan Nicholas | d23c819 | 2018-09-26 17:01:24 -0400 | [diff] [blame] | 3222 | (var->fModifiers.fFlags & Modifiers::kOut_Flag))) { |
Ethan Nicholas | 8e48c1e | 2017-03-02 14:33:31 -0500 | [diff] [blame] | 3223 | interfaceVars.insert(entry.second); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3224 | } |
| 3225 | } |
| 3226 | this->writeCapabilities(out); |
| 3227 | this->writeInstruction(SpvOpExtInstImport, fGLSLExtendedInstructions, "GLSL.std.450", out); |
| 3228 | this->writeInstruction(SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemoryModelGLSL450, out); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 3229 | this->writeOpCode(SpvOpEntryPoint, (SpvId) (3 + (main->fName.fLength + 4) / 4) + |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3230 | (int32_t) interfaceVars.size(), out); |
| 3231 | switch (program.fKind) { |
| 3232 | case Program::kVertex_Kind: |
| 3233 | this->writeWord(SpvExecutionModelVertex, out); |
| 3234 | break; |
| 3235 | case Program::kFragment_Kind: |
| 3236 | this->writeWord(SpvExecutionModelFragment, out); |
| 3237 | break; |
Ethan Nicholas | 52cad15 | 2017-02-16 16:37:32 -0500 | [diff] [blame] | 3238 | case Program::kGeometry_Kind: |
| 3239 | this->writeWord(SpvExecutionModelGeometry, out); |
| 3240 | break; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 3241 | default: |
| 3242 | ABORT("cannot write this kind of program to SPIR-V\n"); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3243 | } |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3244 | SpvId entryPoint = fFunctionMap[main]; |
| 3245 | this->writeWord(entryPoint, out); |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 3246 | this->writeString(main->fName.fChars, main->fName.fLength, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3247 | for (int var : interfaceVars) { |
| 3248 | this->writeWord(var, out); |
| 3249 | } |
Ethan Nicholas | bb155e2 | 2017-07-24 10:05:09 -0400 | [diff] [blame] | 3250 | if (program.fKind == Program::kGeometry_Kind) { |
| 3251 | this->writeGeometryShaderExecutionMode(entryPoint, out); |
| 3252 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3253 | if (program.fKind == Program::kFragment_Kind) { |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 3254 | this->writeInstruction(SpvOpExecutionMode, |
| 3255 | fFunctionMap[main], |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3256 | SpvExecutionModeOriginUpperLeft, |
| 3257 | out); |
| 3258 | } |
Ethan Nicholas | 3c6ae62 | 2018-04-24 13:06:09 -0400 | [diff] [blame] | 3259 | for (const auto& e : program) { |
| 3260 | if (e.fKind == ProgramElement::kExtension_Kind) { |
| 3261 | this->writeInstruction(SpvOpSourceExtension, ((Extension&) e).fName.c_str(), out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3262 | } |
| 3263 | } |
Greg Daniel | 64773e6 | 2016-11-22 09:44:03 -0500 | [diff] [blame] | 3264 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3265 | write_stringstream(fExtraGlobalsBuffer, out); |
| 3266 | write_stringstream(fNameBuffer, out); |
| 3267 | write_stringstream(fDecorationBuffer, out); |
| 3268 | write_stringstream(fConstantBuffer, out); |
| 3269 | write_stringstream(fExternalFunctionsBuffer, out); |
| 3270 | write_stringstream(body, out); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3271 | } |
| 3272 | |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3273 | bool SPIRVCodeGenerator::generateCode() { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 3274 | SkASSERT(!fErrors.errorCount()); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3275 | this->writeWord(SpvMagicNumber, *fOut); |
| 3276 | this->writeWord(SpvVersion, *fOut); |
| 3277 | this->writeWord(SKSL_MAGIC, *fOut); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3278 | StringStream buffer; |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3279 | this->writeInstructions(fProgram, buffer); |
| 3280 | this->writeWord(fIdCount, *fOut); |
| 3281 | this->writeWord(0, *fOut); // reserved, always zero |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 3282 | write_stringstream(buffer, *fOut); |
Ethan Nicholas | 941e7e2 | 2016-12-12 15:33:30 -0500 | [diff] [blame] | 3283 | return 0 == fErrors.errorCount(); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | } |