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