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