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