blob: 81bcb1b086584388d2af11cc1956713a0f81650b [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 Nicholasbcf35f82017-03-30 18:42:48 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/sksl/SkSLSPIRVCodeGenerator.h"
Ethan Nicholas9bd301d2017-03-31 16:04:34 +00009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/sksl/GLSL.std.450.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070011
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#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"
ethannicholasb3058bd2016-07-01 08:22:01 -070017
Ethan Nicholas0be34802019-08-15 12:36:58 -040018#ifdef SK_VULKAN
19#include "src/gpu/vk/GrVkCaps.h"
20#endif
21
ethannicholasb3058bd2016-07-01 08:22:01 -070022namespace SkSL {
23
ethannicholasb3058bd2016-07-01 08:22:01 -070024static const int32_t SKSL_MAGIC = 0x0; // FIXME: we should probably register a magic number
25
26void SPIRVCodeGenerator::setupIntrinsics() {
27#define ALL_GLSL(x) std::make_tuple(kGLSL_STD_450_IntrinsicKind, GLSLstd450 ## x, GLSLstd450 ## x, \
28 GLSLstd450 ## x, GLSLstd450 ## x)
29#define BY_TYPE_GLSL(ifFloat, ifInt, ifUInt) std::make_tuple(kGLSL_STD_450_IntrinsicKind, \
30 GLSLstd450 ## ifFloat, \
31 GLSLstd450 ## ifInt, \
32 GLSLstd450 ## ifUInt, \
33 SpvOpUndef)
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -040034#define ALL_SPIRV(x) std::make_tuple(kSPIRV_IntrinsicKind, SpvOp ## x, SpvOp ## x, SpvOp ## x, \
35 SpvOp ## x)
ethannicholasb3058bd2016-07-01 08:22:01 -070036#define SPECIAL(x) std::make_tuple(kSpecial_IntrinsicKind, k ## x ## _SpecialIntrinsic, \
37 k ## x ## _SpecialIntrinsic, k ## x ## _SpecialIntrinsic, \
38 k ## x ## _SpecialIntrinsic)
Ethan Nicholas0df1b042017-03-31 13:56:23 -040039 fIntrinsicMap[String("round")] = ALL_GLSL(Round);
40 fIntrinsicMap[String("roundEven")] = ALL_GLSL(RoundEven);
41 fIntrinsicMap[String("trunc")] = ALL_GLSL(Trunc);
42 fIntrinsicMap[String("abs")] = BY_TYPE_GLSL(FAbs, SAbs, SAbs);
43 fIntrinsicMap[String("sign")] = BY_TYPE_GLSL(FSign, SSign, SSign);
44 fIntrinsicMap[String("floor")] = ALL_GLSL(Floor);
45 fIntrinsicMap[String("ceil")] = ALL_GLSL(Ceil);
46 fIntrinsicMap[String("fract")] = ALL_GLSL(Fract);
47 fIntrinsicMap[String("radians")] = ALL_GLSL(Radians);
48 fIntrinsicMap[String("degrees")] = ALL_GLSL(Degrees);
49 fIntrinsicMap[String("sin")] = ALL_GLSL(Sin);
50 fIntrinsicMap[String("cos")] = ALL_GLSL(Cos);
51 fIntrinsicMap[String("tan")] = ALL_GLSL(Tan);
52 fIntrinsicMap[String("asin")] = ALL_GLSL(Asin);
53 fIntrinsicMap[String("acos")] = ALL_GLSL(Acos);
54 fIntrinsicMap[String("atan")] = SPECIAL(Atan);
55 fIntrinsicMap[String("sinh")] = ALL_GLSL(Sinh);
56 fIntrinsicMap[String("cosh")] = ALL_GLSL(Cosh);
57 fIntrinsicMap[String("tanh")] = ALL_GLSL(Tanh);
58 fIntrinsicMap[String("asinh")] = ALL_GLSL(Asinh);
59 fIntrinsicMap[String("acosh")] = ALL_GLSL(Acosh);
60 fIntrinsicMap[String("atanh")] = ALL_GLSL(Atanh);
61 fIntrinsicMap[String("pow")] = ALL_GLSL(Pow);
62 fIntrinsicMap[String("exp")] = ALL_GLSL(Exp);
63 fIntrinsicMap[String("log")] = ALL_GLSL(Log);
64 fIntrinsicMap[String("exp2")] = ALL_GLSL(Exp2);
65 fIntrinsicMap[String("log2")] = ALL_GLSL(Log2);
66 fIntrinsicMap[String("sqrt")] = ALL_GLSL(Sqrt);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -040067 fIntrinsicMap[String("inverse")] = ALL_GLSL(MatrixInverse);
68 fIntrinsicMap[String("transpose")] = ALL_SPIRV(Transpose);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040069 fIntrinsicMap[String("inversesqrt")] = ALL_GLSL(InverseSqrt);
70 fIntrinsicMap[String("determinant")] = ALL_GLSL(Determinant);
71 fIntrinsicMap[String("matrixInverse")] = ALL_GLSL(MatrixInverse);
Ethan Nicholas70a44b22017-11-30 09:09:16 -050072 fIntrinsicMap[String("mod")] = SPECIAL(Mod);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -050073 fIntrinsicMap[String("min")] = SPECIAL(Min);
74 fIntrinsicMap[String("max")] = SPECIAL(Max);
75 fIntrinsicMap[String("clamp")] = SPECIAL(Clamp);
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -040076 fIntrinsicMap[String("saturate")] = SPECIAL(Saturate);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040077 fIntrinsicMap[String("dot")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDot,
Ethan Nicholas0187ae62017-05-03 11:03:44 -040078 SpvOpUndef, SpvOpUndef, SpvOpUndef);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -050079 fIntrinsicMap[String("mix")] = SPECIAL(Mix);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040080 fIntrinsicMap[String("step")] = ALL_GLSL(Step);
81 fIntrinsicMap[String("smoothstep")] = ALL_GLSL(SmoothStep);
82 fIntrinsicMap[String("fma")] = ALL_GLSL(Fma);
83 fIntrinsicMap[String("frexp")] = ALL_GLSL(Frexp);
84 fIntrinsicMap[String("ldexp")] = ALL_GLSL(Ldexp);
ethannicholasb3058bd2016-07-01 08:22:01 -070085
Ethan Nicholas0df1b042017-03-31 13:56:23 -040086#define PACK(type) fIntrinsicMap[String("pack" #type)] = ALL_GLSL(Pack ## type); \
87 fIntrinsicMap[String("unpack" #type)] = ALL_GLSL(Unpack ## type)
ethannicholasb3058bd2016-07-01 08:22:01 -070088 PACK(Snorm4x8);
89 PACK(Unorm4x8);
90 PACK(Snorm2x16);
91 PACK(Unorm2x16);
92 PACK(Half2x16);
93 PACK(Double2x32);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040094 fIntrinsicMap[String("length")] = ALL_GLSL(Length);
95 fIntrinsicMap[String("distance")] = ALL_GLSL(Distance);
96 fIntrinsicMap[String("cross")] = ALL_GLSL(Cross);
97 fIntrinsicMap[String("normalize")] = ALL_GLSL(Normalize);
98 fIntrinsicMap[String("faceForward")] = ALL_GLSL(FaceForward);
99 fIntrinsicMap[String("reflect")] = ALL_GLSL(Reflect);
100 fIntrinsicMap[String("refract")] = ALL_GLSL(Refract);
101 fIntrinsicMap[String("findLSB")] = ALL_GLSL(FindILsb);
102 fIntrinsicMap[String("findMSB")] = BY_TYPE_GLSL(FindSMsb, FindSMsb, FindUMsb);
103 fIntrinsicMap[String("dFdx")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDPdx,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400104 SpvOpUndef, SpvOpUndef, SpvOpUndef);
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700105 fIntrinsicMap[String("dFdy")] = SPECIAL(DFdy);
Chris Dalton09212192018-11-13 15:07:24 -0500106 fIntrinsicMap[String("fwidth")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpFwidth,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400107 SpvOpUndef, SpvOpUndef, SpvOpUndef);
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400108 fIntrinsicMap[String("makeSampler2D")] = SPECIAL(SampledImage);
109
Ethan Nicholas13863662019-07-29 13:05:15 -0400110 fIntrinsicMap[String("sample")] = SPECIAL(Texture);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400111 fIntrinsicMap[String("subpassLoad")] = SPECIAL(SubpassLoad);
Greg Daniel64773e62016-11-22 09:44:03 -0500112
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400113 fIntrinsicMap[String("any")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400114 SpvOpUndef, SpvOpUndef, SpvOpAny);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400115 fIntrinsicMap[String("all")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400116 SpvOpUndef, SpvOpUndef, SpvOpAll);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400117 fIntrinsicMap[String("equal")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400118 SpvOpFOrdEqual, SpvOpIEqual,
119 SpvOpIEqual, SpvOpLogicalEqual);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400120 fIntrinsicMap[String("notEqual")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400121 SpvOpFOrdNotEqual, SpvOpINotEqual,
122 SpvOpINotEqual,
123 SpvOpLogicalNotEqual);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400124 fIntrinsicMap[String("lessThan")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500125 SpvOpFOrdLessThan, SpvOpSLessThan,
126 SpvOpULessThan, SpvOpUndef);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400127 fIntrinsicMap[String("lessThanEqual")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500128 SpvOpFOrdLessThanEqual,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400129 SpvOpSLessThanEqual,
130 SpvOpULessThanEqual,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400131 SpvOpUndef);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400132 fIntrinsicMap[String("greaterThan")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500133 SpvOpFOrdGreaterThan,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400134 SpvOpSGreaterThan,
135 SpvOpUGreaterThan,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400136 SpvOpUndef);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400137 fIntrinsicMap[String("greaterThanEqual")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500138 SpvOpFOrdGreaterThanEqual,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400139 SpvOpSGreaterThanEqual,
140 SpvOpUGreaterThanEqual,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400141 SpvOpUndef);
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400142 fIntrinsicMap[String("EmitVertex")] = ALL_SPIRV(EmitVertex);
143 fIntrinsicMap[String("EndPrimitive")] = ALL_SPIRV(EndPrimitive);
ethannicholasb3058bd2016-07-01 08:22:01 -0700144// interpolateAt* not yet supported...
145}
146
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400147void SPIRVCodeGenerator::writeWord(int32_t word, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700148 out.write((const char*) &word, sizeof(word));
ethannicholasb3058bd2016-07-01 08:22:01 -0700149}
150
ethannicholasd598f792016-07-25 10:08:54 -0700151static bool is_float(const Context& context, const Type& type) {
Ethan Nicholas0df21132018-07-10 09:37:51 -0400152 if (type.columns() > 1) {
ethannicholasd598f792016-07-25 10:08:54 -0700153 return is_float(context, type.componentType());
ethannicholasb3058bd2016-07-01 08:22:01 -0700154 }
John Stiles8c578662020-06-01 15:32:47 +0000155 return type == *context.fFloat_Type || type == *context.fHalf_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -0700156}
157
ethannicholasd598f792016-07-25 10:08:54 -0700158static bool is_signed(const Context& context, const Type& type) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400159 if (type.typeKind() == Type::TypeKind::kVector) {
ethannicholasd598f792016-07-25 10:08:54 -0700160 return is_signed(context, type.componentType());
ethannicholasb3058bd2016-07-01 08:22:01 -0700161 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400162 return type == *context.fInt_Type || type == *context.fShort_Type ||
163 type == *context.fByte_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -0700164}
165
ethannicholasd598f792016-07-25 10:08:54 -0700166static bool is_unsigned(const Context& context, const Type& type) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400167 if (type.typeKind() == Type::TypeKind::kVector) {
ethannicholasd598f792016-07-25 10:08:54 -0700168 return is_unsigned(context, type.componentType());
ethannicholasb3058bd2016-07-01 08:22:01 -0700169 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400170 return type == *context.fUInt_Type || type == *context.fUShort_Type ||
171 type == *context.fUByte_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -0700172}
173
ethannicholasd598f792016-07-25 10:08:54 -0700174static bool is_bool(const Context& context, const Type& type) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400175 if (type.typeKind() == Type::TypeKind::kVector) {
ethannicholasd598f792016-07-25 10:08:54 -0700176 return is_bool(context, type.componentType());
ethannicholasb3058bd2016-07-01 08:22:01 -0700177 }
ethannicholasd598f792016-07-25 10:08:54 -0700178 return type == *context.fBool_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -0700179}
180
ethannicholasd598f792016-07-25 10:08:54 -0700181static bool is_out(const Variable& var) {
182 return (var.fModifiers.fFlags & Modifiers::kOut_Flag) != 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700183}
184
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400185void SPIRVCodeGenerator::writeOpCode(SpvOp_ opCode, int length, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400186 SkASSERT(opCode != SpvOpLoad || &out != &fConstantBuffer);
187 SkASSERT(opCode != SpvOpUndef);
ethannicholasb3058bd2016-07-01 08:22:01 -0700188 switch (opCode) {
189 case SpvOpReturn: // fall through
190 case SpvOpReturnValue: // fall through
ethannicholas552882f2016-07-07 06:30:48 -0700191 case SpvOpKill: // fall through
ethannicholasb3058bd2016-07-01 08:22:01 -0700192 case SpvOpBranch: // fall through
193 case SpvOpBranchConditional:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400194 SkASSERT(fCurrentBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700195 fCurrentBlock = 0;
196 break;
197 case SpvOpConstant: // fall through
198 case SpvOpConstantTrue: // fall through
199 case SpvOpConstantFalse: // fall through
200 case SpvOpConstantComposite: // fall through
201 case SpvOpTypeVoid: // fall through
202 case SpvOpTypeInt: // fall through
203 case SpvOpTypeFloat: // fall through
204 case SpvOpTypeBool: // fall through
205 case SpvOpTypeVector: // fall through
206 case SpvOpTypeMatrix: // fall through
207 case SpvOpTypeArray: // fall through
208 case SpvOpTypePointer: // fall through
209 case SpvOpTypeFunction: // fall through
210 case SpvOpTypeRuntimeArray: // fall through
211 case SpvOpTypeStruct: // fall through
212 case SpvOpTypeImage: // fall through
213 case SpvOpTypeSampledImage: // fall through
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400214 case SpvOpTypeSampler: // fall through
ethannicholasb3058bd2016-07-01 08:22:01 -0700215 case SpvOpVariable: // fall through
216 case SpvOpFunction: // fall through
217 case SpvOpFunctionParameter: // fall through
218 case SpvOpFunctionEnd: // fall through
219 case SpvOpExecutionMode: // fall through
220 case SpvOpMemoryModel: // fall through
221 case SpvOpCapability: // fall through
222 case SpvOpExtInstImport: // fall through
223 case SpvOpEntryPoint: // fall through
224 case SpvOpSource: // fall through
225 case SpvOpSourceExtension: // fall through
226 case SpvOpName: // fall through
227 case SpvOpMemberName: // fall through
228 case SpvOpDecorate: // fall through
229 case SpvOpMemberDecorate:
230 break;
231 default:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400232 SkASSERT(fCurrentBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700233 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700234 this->writeWord((length << 16) | opCode, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700235}
236
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400237void SPIRVCodeGenerator::writeLabel(SpvId label, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700238 fCurrentBlock = label;
239 this->writeInstruction(SpvOpLabel, label, out);
240}
241
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400242void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700243 this->writeOpCode(opCode, 1, out);
244}
245
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400246void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700247 this->writeOpCode(opCode, 2, out);
248 this->writeWord(word1, out);
249}
250
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700251void SPIRVCodeGenerator::writeString(const char* string, size_t length, OutputStream& out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400252 out.write(string, length);
ethannicholasb3058bd2016-07-01 08:22:01 -0700253 switch (length % 4) {
254 case 1:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500255 out.write8(0);
John Stiles30212b72020-06-11 17:55:07 -0400256 [[fallthrough]];
ethannicholasb3058bd2016-07-01 08:22:01 -0700257 case 2:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500258 out.write8(0);
John Stiles30212b72020-06-11 17:55:07 -0400259 [[fallthrough]];
ethannicholasb3058bd2016-07-01 08:22:01 -0700260 case 3:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500261 out.write8(0);
ethannicholasb3058bd2016-07-01 08:22:01 -0700262 break;
263 default:
264 this->writeWord(0, out);
265 }
266}
267
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700268void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out) {
269 this->writeOpCode(opCode, 1 + (string.fLength + 4) / 4, out);
270 this->writeString(string.fChars, string.fLength, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700271}
272
273
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700274void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400275 OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700276 this->writeOpCode(opCode, 2 + (string.fLength + 4) / 4, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700277 this->writeWord(word1, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700278 this->writeString(string.fChars, string.fLength, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700279}
280
Greg Daniel64773e62016-11-22 09:44:03 -0500281void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700282 StringFragment string, OutputStream& out) {
283 this->writeOpCode(opCode, 3 + (string.fLength + 4) / 4, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700284 this->writeWord(word1, out);
285 this->writeWord(word2, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700286 this->writeString(string.fChars, string.fLength, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700287}
288
Greg Daniel64773e62016-11-22 09:44:03 -0500289void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400290 OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700291 this->writeOpCode(opCode, 3, out);
292 this->writeWord(word1, out);
293 this->writeWord(word2, out);
294}
295
Greg Daniel64773e62016-11-22 09:44:03 -0500296void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400297 int32_t word3, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700298 this->writeOpCode(opCode, 4, out);
299 this->writeWord(word1, out);
300 this->writeWord(word2, out);
301 this->writeWord(word3, out);
302}
303
Greg Daniel64773e62016-11-22 09:44:03 -0500304void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400305 int32_t word3, int32_t word4, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700306 this->writeOpCode(opCode, 5, out);
307 this->writeWord(word1, out);
308 this->writeWord(word2, out);
309 this->writeWord(word3, out);
310 this->writeWord(word4, out);
311}
312
Greg Daniel64773e62016-11-22 09:44:03 -0500313void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
314 int32_t word3, int32_t word4, int32_t word5,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400315 OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700316 this->writeOpCode(opCode, 6, out);
317 this->writeWord(word1, out);
318 this->writeWord(word2, out);
319 this->writeWord(word3, out);
320 this->writeWord(word4, out);
321 this->writeWord(word5, out);
322}
323
Greg Daniel64773e62016-11-22 09:44:03 -0500324void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
ethannicholasb3058bd2016-07-01 08:22:01 -0700325 int32_t word3, int32_t word4, int32_t word5,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400326 int32_t word6, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700327 this->writeOpCode(opCode, 7, out);
328 this->writeWord(word1, out);
329 this->writeWord(word2, out);
330 this->writeWord(word3, out);
331 this->writeWord(word4, out);
332 this->writeWord(word5, out);
333 this->writeWord(word6, out);
334}
335
Greg Daniel64773e62016-11-22 09:44:03 -0500336void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
ethannicholasb3058bd2016-07-01 08:22:01 -0700337 int32_t word3, int32_t word4, int32_t word5,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400338 int32_t word6, int32_t word7, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700339 this->writeOpCode(opCode, 8, out);
340 this->writeWord(word1, out);
341 this->writeWord(word2, out);
342 this->writeWord(word3, out);
343 this->writeWord(word4, out);
344 this->writeWord(word5, out);
345 this->writeWord(word6, out);
346 this->writeWord(word7, out);
347}
348
Greg Daniel64773e62016-11-22 09:44:03 -0500349void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
ethannicholasb3058bd2016-07-01 08:22:01 -0700350 int32_t word3, int32_t word4, int32_t word5,
351 int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400352 OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700353 this->writeOpCode(opCode, 9, out);
354 this->writeWord(word1, out);
355 this->writeWord(word2, out);
356 this->writeWord(word3, out);
357 this->writeWord(word4, out);
358 this->writeWord(word5, out);
359 this->writeWord(word6, out);
360 this->writeWord(word7, out);
361 this->writeWord(word8, out);
362}
363
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400364void SPIRVCodeGenerator::writeCapabilities(OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700365 for (uint64_t i = 0, bit = 1; i <= kLast_Capability; i++, bit <<= 1) {
366 if (fCapabilities & bit) {
367 this->writeInstruction(SpvOpCapability, (SpvId) i, out);
368 }
369 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400370 if (fProgram.fKind == Program::kGeometry_Kind) {
371 this->writeInstruction(SpvOpCapability, SpvCapabilityGeometry, out);
372 }
Ethan Nicholas81d15112018-07-13 12:48:50 -0400373 else {
374 this->writeInstruction(SpvOpCapability, SpvCapabilityShader, out);
375 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700376}
377
378SpvId SPIRVCodeGenerator::nextId() {
379 return fIdCount++;
380}
381
Ethan Nicholas19671772016-11-28 16:30:17 -0500382void SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& memoryLayout,
383 SpvId resultId) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700384 this->writeInstruction(SpvOpName, resultId, type.name().c_str(), fNameBuffer);
385 // go ahead and write all of the field types, so we don't inadvertently write them while we're
386 // in the middle of writing the struct instruction
387 std::vector<SpvId> types;
388 for (const auto& f : type.fields()) {
Ethan Nicholas19671772016-11-28 16:30:17 -0500389 types.push_back(this->getType(*f.fType, memoryLayout));
ethannicholasb3058bd2016-07-01 08:22:01 -0700390 }
391 this->writeOpCode(SpvOpTypeStruct, 2 + (int32_t) types.size(), fConstantBuffer);
392 this->writeWord(resultId, fConstantBuffer);
393 for (SpvId id : types) {
394 this->writeWord(id, fConstantBuffer);
395 }
396 size_t offset = 0;
397 for (int32_t i = 0; i < (int32_t) type.fields().size(); i++) {
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400398 const Type::Field& field = type.fields()[i];
399 size_t size = memoryLayout.size(*field.fType);
400 size_t alignment = memoryLayout.alignment(*field.fType);
401 const Layout& fieldLayout = field.fModifiers.fLayout;
Ethan Nicholas19671772016-11-28 16:30:17 -0500402 if (fieldLayout.fOffset >= 0) {
Greg Danieldbd44c72017-01-24 15:12:12 -0500403 if (fieldLayout.fOffset < (int) offset) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700404 fErrors.error(type.fOffset,
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400405 "offset of field '" + field.fName + "' must be at "
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500406 "least " + to_string((int) offset));
Ethan Nicholas19671772016-11-28 16:30:17 -0500407 }
408 if (fieldLayout.fOffset % alignment) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700409 fErrors.error(type.fOffset,
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400410 "offset of field '" + field.fName + "' must be a multiple"
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500411 " of " + to_string((int) alignment));
Ethan Nicholas19671772016-11-28 16:30:17 -0500412 }
413 offset = fieldLayout.fOffset;
414 } else {
415 size_t mod = offset % alignment;
416 if (mod) {
417 offset += alignment - mod;
418 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700419 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400420 this->writeInstruction(SpvOpMemberName, resultId, i, field.fName, fNameBuffer);
Ethan Nicholas19671772016-11-28 16:30:17 -0500421 this->writeLayout(fieldLayout, resultId, i);
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400422 if (field.fModifiers.fLayout.fBuiltin < 0) {
Greg Daniel64773e62016-11-22 09:44:03 -0500423 this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, SpvDecorationOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700424 (SpvId) offset, fDecorationBuffer);
425 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400426 if (field.fType->typeKind() == Type::TypeKind::kMatrix) {
Greg Daniel64773e62016-11-22 09:44:03 -0500427 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationColMajor,
ethannicholasb3058bd2016-07-01 08:22:01 -0700428 fDecorationBuffer);
Greg Daniel64773e62016-11-22 09:44:03 -0500429 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationMatrixStride,
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400430 (SpvId) memoryLayout.stride(*field.fType),
ethannicholas8ac838d2016-11-22 08:39:36 -0800431 fDecorationBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -0700432 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400433 if (!field.fType->highPrecision()) {
434 this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i,
435 SpvDecorationRelaxedPrecision, fDecorationBuffer);
436 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700437 offset += size;
Ethan Nicholase6592142020-09-08 10:22:09 -0400438 Type::TypeKind kind = field.fType->typeKind();
439 if ((kind == Type::TypeKind::kArray || kind == Type::TypeKind::kStruct) &&
440 offset % alignment != 0) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700441 offset += alignment - offset % alignment;
442 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700443 }
444}
445
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400446Type SPIRVCodeGenerator::getActualType(const Type& type) {
Ethan Nicholase1f55022019-02-05 17:17:40 -0500447 if (type.isFloat()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400448 return *fContext.fFloat_Type;
449 }
Ethan Nicholase1f55022019-02-05 17:17:40 -0500450 if (type.isSigned()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400451 return *fContext.fInt_Type;
452 }
Ethan Nicholase1f55022019-02-05 17:17:40 -0500453 if (type.isUnsigned()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400454 return *fContext.fUInt_Type;
455 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400456 if (type.typeKind() == Type::TypeKind::kMatrix || type.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400457 if (type.componentType() == *fContext.fHalf_Type) {
458 return fContext.fFloat_Type->toCompound(fContext, type.columns(), type.rows());
459 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400460 if (type.componentType() == *fContext.fShort_Type ||
461 type.componentType() == *fContext.fByte_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400462 return fContext.fInt_Type->toCompound(fContext, type.columns(), type.rows());
463 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400464 if (type.componentType() == *fContext.fUShort_Type ||
465 type.componentType() == *fContext.fUByte_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400466 return fContext.fUInt_Type->toCompound(fContext, type.columns(), type.rows());
467 }
468 }
469 return type;
470}
471
ethannicholasb3058bd2016-07-01 08:22:01 -0700472SpvId SPIRVCodeGenerator::getType(const Type& type) {
ethannicholas8ac838d2016-11-22 08:39:36 -0800473 return this->getType(type, fDefaultLayout);
474}
475
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400476SpvId SPIRVCodeGenerator::getType(const Type& rawType, const MemoryLayout& layout) {
477 Type type = this->getActualType(rawType);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400478 String key = type.name() + to_string((int) layout.fStd);
ethannicholas8ac838d2016-11-22 08:39:36 -0800479 auto entry = fTypeMap.find(key);
ethannicholasb3058bd2016-07-01 08:22:01 -0700480 if (entry == fTypeMap.end()) {
481 SpvId result = this->nextId();
Ethan Nicholase6592142020-09-08 10:22:09 -0400482 switch (type.typeKind()) {
483 case Type::TypeKind::kScalar:
ethannicholasd598f792016-07-25 10:08:54 -0700484 if (type == *fContext.fBool_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700485 this->writeInstruction(SpvOpTypeBool, result, fConstantBuffer);
Ethan Nicholase1f55022019-02-05 17:17:40 -0500486 } else if (type == *fContext.fInt_Type || type == *fContext.fShort_Type ||
487 type == *fContext.fIntLiteral_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700488 this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstantBuffer);
Ethan Nicholase1f55022019-02-05 17:17:40 -0500489 } else if (type == *fContext.fUInt_Type || type == *fContext.fUShort_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700490 this->writeInstruction(SpvOpTypeInt, result, 32, 0, fConstantBuffer);
Ethan Nicholase1f55022019-02-05 17:17:40 -0500491 } else if (type == *fContext.fFloat_Type || type == *fContext.fHalf_Type ||
492 type == *fContext.fFloatLiteral_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700493 this->writeInstruction(SpvOpTypeFloat, result, 32, fConstantBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -0700494 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400495 SkASSERT(false);
ethannicholasb3058bd2016-07-01 08:22:01 -0700496 }
497 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400498 case Type::TypeKind::kVector:
Greg Daniel64773e62016-11-22 09:44:03 -0500499 this->writeInstruction(SpvOpTypeVector, result,
ethannicholas8ac838d2016-11-22 08:39:36 -0800500 this->getType(type.componentType(), layout),
ethannicholasb3058bd2016-07-01 08:22:01 -0700501 type.columns(), fConstantBuffer);
502 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400503 case Type::TypeKind::kMatrix:
Greg Daniel64773e62016-11-22 09:44:03 -0500504 this->writeInstruction(SpvOpTypeMatrix, result,
ethannicholas8ac838d2016-11-22 08:39:36 -0800505 this->getType(index_type(fContext, type), layout),
ethannicholasb3058bd2016-07-01 08:22:01 -0700506 type.columns(), fConstantBuffer);
507 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400508 case Type::TypeKind::kStruct:
ethannicholas8ac838d2016-11-22 08:39:36 -0800509 this->writeStruct(type, layout, result);
ethannicholasb3058bd2016-07-01 08:22:01 -0700510 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400511 case Type::TypeKind::kArray: {
ethannicholasb3058bd2016-07-01 08:22:01 -0700512 if (type.columns() > 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700513 IntLiteral count(fContext, -1, type.columns());
Greg Daniel64773e62016-11-22 09:44:03 -0500514 this->writeInstruction(SpvOpTypeArray, result,
ethannicholas8ac838d2016-11-22 08:39:36 -0800515 this->getType(type.componentType(), layout),
ethannicholasb3058bd2016-07-01 08:22:01 -0700516 this->writeIntLiteral(count), fConstantBuffer);
Greg Daniel64773e62016-11-22 09:44:03 -0500517 this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400518 (int32_t) layout.stride(type),
ethannicholas8ac838d2016-11-22 08:39:36 -0800519 fDecorationBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -0700520 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400521 SkASSERT(false); // we shouldn't have any runtime-sized arrays right now
Greg Daniel64773e62016-11-22 09:44:03 -0500522 this->writeInstruction(SpvOpTypeRuntimeArray, result,
ethannicholas8ac838d2016-11-22 08:39:36 -0800523 this->getType(type.componentType(), layout),
524 fConstantBuffer);
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400525 this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride,
526 (int32_t) layout.stride(type),
527 fDecorationBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -0700528 }
529 break;
530 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400531 case Type::TypeKind::kSampler: {
Greg Daniel64773e62016-11-22 09:44:03 -0500532 SpvId image = result;
533 if (SpvDimSubpassData != type.dimensions()) {
Stephen White792e2302019-08-09 13:33:51 -0400534 image = this->getType(type.textureType(), layout);
Greg Daniel64773e62016-11-22 09:44:03 -0500535 }
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400536 if (SpvDimBuffer == type.dimensions()) {
537 fCapabilities |= (((uint64_t) 1) << SpvCapabilitySampledBuffer);
538 }
Greg Daniel64773e62016-11-22 09:44:03 -0500539 if (SpvDimSubpassData != type.dimensions()) {
540 this->writeInstruction(SpvOpTypeSampledImage, result, image, fConstantBuffer);
541 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700542 break;
543 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400544 case Type::TypeKind::kSeparateSampler: {
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400545 this->writeInstruction(SpvOpTypeSampler, result, fConstantBuffer);
546 break;
547 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400548 case Type::TypeKind::kTexture: {
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400549 this->writeInstruction(SpvOpTypeImage, result,
550 this->getType(*fContext.fFloat_Type, layout),
Stephen White792e2302019-08-09 13:33:51 -0400551 type.dimensions(), type.isDepth(), type.isArrayed(),
552 type.isMultisampled(), type.isSampled() ? 1 : 2,
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400553 SpvImageFormatUnknown, fConstantBuffer);
554 fImageTypeMap[key] = result;
555 break;
556 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700557 default:
ethannicholasd598f792016-07-25 10:08:54 -0700558 if (type == *fContext.fVoid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700559 this->writeInstruction(SpvOpTypeVoid, result, fConstantBuffer);
560 } else {
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500561#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -0700562 ABORT("invalid type: %s", type.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500563#endif
ethannicholasb3058bd2016-07-01 08:22:01 -0700564 }
565 }
ethannicholas8ac838d2016-11-22 08:39:36 -0800566 fTypeMap[key] = result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700567 return result;
568 }
569 return entry->second;
570}
571
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400572SpvId SPIRVCodeGenerator::getImageType(const Type& type) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400573 SkASSERT(type.typeKind() == Type::TypeKind::kSampler);
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400574 this->getType(type);
575 String key = type.name() + to_string((int) fDefaultLayout.fStd);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400576 SkASSERT(fImageTypeMap.find(key) != fImageTypeMap.end());
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400577 return fImageTypeMap[key];
578}
579
ethannicholasd598f792016-07-25 10:08:54 -0700580SpvId SPIRVCodeGenerator::getFunctionType(const FunctionDeclaration& function) {
Brian Osmanc4f937b2020-03-25 15:39:07 -0400581 String key = to_string(this->getType(function.fReturnType)) + "(";
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400582 String separator;
ethannicholasd598f792016-07-25 10:08:54 -0700583 for (size_t i = 0; i < function.fParameters.size(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700584 key += separator;
585 separator = ", ";
Ethan Nicholas30d30222020-09-11 12:27:26 -0400586 key += to_string(this->getType(function.fParameters[i]->type()));
ethannicholasb3058bd2016-07-01 08:22:01 -0700587 }
588 key += ")";
589 auto entry = fTypeMap.find(key);
590 if (entry == fTypeMap.end()) {
591 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -0700592 int32_t length = 3 + (int32_t) function.fParameters.size();
593 SpvId returnType = this->getType(function.fReturnType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700594 std::vector<SpvId> parameterTypes;
ethannicholasd598f792016-07-25 10:08:54 -0700595 for (size_t i = 0; i < function.fParameters.size(); i++) {
Greg Daniel64773e62016-11-22 09:44:03 -0500596 // glslang seems to treat all function arguments as pointers whether they need to be or
597 // not. I was initially puzzled by this until I ran bizarre failures with certain
598 // patterns of function calls and control constructs, as exemplified by this minimal
ethannicholasb3058bd2016-07-01 08:22:01 -0700599 // failure case:
600 //
601 // void sphere(float x) {
602 // }
Greg Daniel64773e62016-11-22 09:44:03 -0500603 //
ethannicholasb3058bd2016-07-01 08:22:01 -0700604 // void map() {
605 // sphere(1.0);
606 // }
Greg Daniel64773e62016-11-22 09:44:03 -0500607 //
ethannicholasb3058bd2016-07-01 08:22:01 -0700608 // void main() {
609 // for (int i = 0; i < 1; i++) {
610 // map();
611 // }
612 // }
613 //
Greg Daniel64773e62016-11-22 09:44:03 -0500614 // As of this writing, compiling this in the "obvious" way (with sphere taking a float)
615 // crashes. Making it take a float* and storing the argument in a temporary variable,
ethannicholasb3058bd2016-07-01 08:22:01 -0700616 // as glslang does, fixes it. It's entirely possible I simply missed whichever part of
617 // the spec makes this make sense.
618// if (is_out(function->fParameters[i])) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400619 parameterTypes.push_back(this->getPointerType(function.fParameters[i]->type(),
ethannicholasb3058bd2016-07-01 08:22:01 -0700620 SpvStorageClassFunction));
621// } else {
ethannicholasd598f792016-07-25 10:08:54 -0700622// parameterTypes.push_back(this->getType(function.fParameters[i]->fType));
ethannicholasb3058bd2016-07-01 08:22:01 -0700623// }
624 }
625 this->writeOpCode(SpvOpTypeFunction, length, fConstantBuffer);
626 this->writeWord(result, fConstantBuffer);
627 this->writeWord(returnType, fConstantBuffer);
628 for (SpvId id : parameterTypes) {
629 this->writeWord(id, fConstantBuffer);
630 }
631 fTypeMap[key] = result;
632 return result;
633 }
634 return entry->second;
635}
636
ethannicholas8ac838d2016-11-22 08:39:36 -0800637SpvId SPIRVCodeGenerator::getPointerType(const Type& type, SpvStorageClass_ storageClass) {
638 return this->getPointerType(type, fDefaultLayout, storageClass);
639}
640
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400641SpvId SPIRVCodeGenerator::getPointerType(const Type& rawType, const MemoryLayout& layout,
ethannicholasb3058bd2016-07-01 08:22:01 -0700642 SpvStorageClass_ storageClass) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400643 Type type = this->getActualType(rawType);
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500644 String key = type.displayName() + "*" + to_string(layout.fStd) + to_string(storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700645 auto entry = fTypeMap.find(key);
646 if (entry == fTypeMap.end()) {
647 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -0500648 this->writeInstruction(SpvOpTypePointer, result, storageClass,
ethannicholasd598f792016-07-25 10:08:54 -0700649 this->getType(type), fConstantBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -0700650 fTypeMap[key] = result;
651 return result;
652 }
653 return entry->second;
654}
655
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400656SpvId SPIRVCodeGenerator::writeExpression(const Expression& expr, OutputStream& out) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400657 switch (expr.kind()) {
658 case Expression::Kind::kBinary:
John Stiles81365af2020-08-18 09:24:00 -0400659 return this->writeBinaryExpression(expr.as<BinaryExpression>(), out);
Ethan Nicholase6592142020-09-08 10:22:09 -0400660 case Expression::Kind::kBoolLiteral:
John Stiles81365af2020-08-18 09:24:00 -0400661 return this->writeBoolLiteral(expr.as<BoolLiteral>());
Ethan Nicholase6592142020-09-08 10:22:09 -0400662 case Expression::Kind::kConstructor:
John Stiles81365af2020-08-18 09:24:00 -0400663 return this->writeConstructor(expr.as<Constructor>(), out);
Ethan Nicholase6592142020-09-08 10:22:09 -0400664 case Expression::Kind::kIntLiteral:
John Stiles81365af2020-08-18 09:24:00 -0400665 return this->writeIntLiteral(expr.as<IntLiteral>());
Ethan Nicholase6592142020-09-08 10:22:09 -0400666 case Expression::Kind::kFieldAccess:
John Stiles81365af2020-08-18 09:24:00 -0400667 return this->writeFieldAccess(expr.as<FieldAccess>(), out);
Ethan Nicholase6592142020-09-08 10:22:09 -0400668 case Expression::Kind::kFloatLiteral:
John Stiles81365af2020-08-18 09:24:00 -0400669 return this->writeFloatLiteral(expr.as<FloatLiteral>());
Ethan Nicholase6592142020-09-08 10:22:09 -0400670 case Expression::Kind::kFunctionCall:
John Stiles81365af2020-08-18 09:24:00 -0400671 return this->writeFunctionCall(expr.as<FunctionCall>(), out);
Ethan Nicholase6592142020-09-08 10:22:09 -0400672 case Expression::Kind::kPrefix:
John Stiles81365af2020-08-18 09:24:00 -0400673 return this->writePrefixExpression(expr.as<PrefixExpression>(), out);
Ethan Nicholase6592142020-09-08 10:22:09 -0400674 case Expression::Kind::kPostfix:
John Stiles81365af2020-08-18 09:24:00 -0400675 return this->writePostfixExpression(expr.as<PostfixExpression>(), out);
Ethan Nicholase6592142020-09-08 10:22:09 -0400676 case Expression::Kind::kSwizzle:
John Stiles81365af2020-08-18 09:24:00 -0400677 return this->writeSwizzle(expr.as<Swizzle>(), out);
Ethan Nicholase6592142020-09-08 10:22:09 -0400678 case Expression::Kind::kVariableReference:
John Stiles81365af2020-08-18 09:24:00 -0400679 return this->writeVariableReference(expr.as<VariableReference>(), out);
Ethan Nicholase6592142020-09-08 10:22:09 -0400680 case Expression::Kind::kTernary:
John Stiles81365af2020-08-18 09:24:00 -0400681 return this->writeTernaryExpression(expr.as<TernaryExpression>(), out);
Ethan Nicholase6592142020-09-08 10:22:09 -0400682 case Expression::Kind::kIndex:
John Stiles81365af2020-08-18 09:24:00 -0400683 return this->writeIndexExpression(expr.as<IndexExpression>(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700684 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500685#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -0700686 ABORT("unsupported expression: %s", expr.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -0500687#endif
688 break;
ethannicholasb3058bd2016-07-01 08:22:01 -0700689 }
690 return -1;
691}
692
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400693SpvId SPIRVCodeGenerator::writeIntrinsicCall(const FunctionCall& c, OutputStream& out) {
ethannicholasd598f792016-07-25 10:08:54 -0700694 auto intrinsic = fIntrinsicMap.find(c.fFunction.fName);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400695 SkASSERT(intrinsic != fIntrinsicMap.end());
ethannicholasb3058bd2016-07-01 08:22:01 -0700696 int32_t intrinsicId;
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400697 if (c.fArguments.size() > 0) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400698 const Type& type = c.fArguments[0]->type();
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400699 if (std::get<0>(intrinsic->second) == kSpecial_IntrinsicKind || is_float(fContext, type)) {
700 intrinsicId = std::get<1>(intrinsic->second);
701 } else if (is_signed(fContext, type)) {
702 intrinsicId = std::get<2>(intrinsic->second);
703 } else if (is_unsigned(fContext, type)) {
704 intrinsicId = std::get<3>(intrinsic->second);
705 } else if (is_bool(fContext, type)) {
706 intrinsicId = std::get<4>(intrinsic->second);
707 } else {
708 intrinsicId = std::get<1>(intrinsic->second);
709 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700710 } else {
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400711 intrinsicId = std::get<1>(intrinsic->second);
ethannicholasb3058bd2016-07-01 08:22:01 -0700712 }
713 switch (std::get<0>(intrinsic->second)) {
714 case kGLSL_STD_450_IntrinsicKind: {
715 SpvId result = this->nextId();
716 std::vector<SpvId> arguments;
717 for (size_t i = 0; i < c.fArguments.size(); i++) {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400718 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
719 arguments.push_back(this->getLValue(*c.fArguments[i], out)->getPointer());
720 } else {
721 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
722 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700723 }
724 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) arguments.size(), out);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400725 this->writeWord(this->getType(c.type()), out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700726 this->writeWord(result, out);
727 this->writeWord(fGLSLExtendedInstructions, out);
728 this->writeWord(intrinsicId, out);
729 for (SpvId id : arguments) {
730 this->writeWord(id, out);
731 }
732 return result;
733 }
734 case kSPIRV_IntrinsicKind: {
735 SpvId result = this->nextId();
736 std::vector<SpvId> arguments;
737 for (size_t i = 0; i < c.fArguments.size(); i++) {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400738 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
739 arguments.push_back(this->getLValue(*c.fArguments[i], out)->getPointer());
740 } else {
741 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
742 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700743 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400744 if (c.type() != *fContext.fVoid_Type) {
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400745 this->writeOpCode((SpvOp_) intrinsicId, 3 + (int32_t) arguments.size(), out);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400746 this->writeWord(this->getType(c.type()), out);
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400747 this->writeWord(result, out);
748 } else {
749 this->writeOpCode((SpvOp_) intrinsicId, 1 + (int32_t) arguments.size(), out);
750 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700751 for (SpvId id : arguments) {
752 this->writeWord(id, out);
753 }
754 return result;
755 }
756 case kSpecial_IntrinsicKind:
757 return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId, out);
758 default:
759 ABORT("unsupported intrinsic kind");
760 }
761}
762
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500763std::vector<SpvId> SPIRVCodeGenerator::vectorize(
764 const std::vector<std::unique_ptr<Expression>>& args,
765 OutputStream& out) {
766 int vectorSize = 0;
767 for (const auto& a : args) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400768 if (a->type().typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500769 if (vectorSize) {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400770 SkASSERT(a->type().columns() == vectorSize);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500771 }
772 else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400773 vectorSize = a->type().columns();
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500774 }
775 }
776 }
777 std::vector<SpvId> result;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400778 for (const auto& arg : args) {
779 const Type& argType = arg->type();
780 SpvId raw = this->writeExpression(*arg, out);
781 if (vectorSize && argType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500782 SpvId vector = this->nextId();
783 this->writeOpCode(SpvOpCompositeConstruct, 3 + vectorSize, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400784 this->writeWord(this->getType(argType.toCompound(fContext, vectorSize, 1)), out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500785 this->writeWord(vector, out);
786 for (int i = 0; i < vectorSize; i++) {
787 this->writeWord(raw, out);
788 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400789 this->writePrecisionModifier(argType, vector);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500790 result.push_back(vector);
791 } else {
792 result.push_back(raw);
793 }
794 }
795 return result;
796}
797
798void SPIRVCodeGenerator::writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
799 SpvId signedInst, SpvId unsignedInst,
800 const std::vector<SpvId>& args,
801 OutputStream& out) {
802 this->writeOpCode(SpvOpExtInst, 5 + args.size(), out);
803 this->writeWord(this->getType(type), out);
804 this->writeWord(id, out);
805 this->writeWord(fGLSLExtendedInstructions, out);
806
807 if (is_float(fContext, type)) {
808 this->writeWord(floatInst, out);
809 } else if (is_signed(fContext, type)) {
810 this->writeWord(signedInst, out);
811 } else if (is_unsigned(fContext, type)) {
812 this->writeWord(unsignedInst, out);
813 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400814 SkASSERT(false);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500815 }
816 for (SpvId a : args) {
817 this->writeWord(a, out);
818 }
819}
820
Greg Daniel64773e62016-11-22 09:44:03 -0500821SpvId SPIRVCodeGenerator::writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400822 OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700823 SpvId result = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400824 const Type& callType = c.type();
ethannicholasb3058bd2016-07-01 08:22:01 -0700825 switch (kind) {
826 case kAtan_SpecialIntrinsic: {
827 std::vector<SpvId> arguments;
828 for (size_t i = 0; i < c.fArguments.size(); i++) {
829 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
830 }
831 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) arguments.size(), out);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400832 this->writeWord(this->getType(callType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700833 this->writeWord(result, out);
834 this->writeWord(fGLSLExtendedInstructions, out);
835 this->writeWord(arguments.size() == 2 ? GLSLstd450Atan2 : GLSLstd450Atan, out);
836 for (SpvId id : arguments) {
837 this->writeWord(id, out);
838 }
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400839 break;
840 }
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400841 case kSampledImage_SpecialIntrinsic: {
842 SkASSERT(2 == c.fArguments.size());
843 SpvId img = this->writeExpression(*c.fArguments[0], out);
844 SpvId sampler = this->writeExpression(*c.fArguments[1], out);
845 this->writeInstruction(SpvOpSampledImage,
Ethan Nicholas30d30222020-09-11 12:27:26 -0400846 this->getType(callType),
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400847 result,
848 img,
849 sampler,
850 out);
851 break;
852 }
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400853 case kSubpassLoad_SpecialIntrinsic: {
854 SpvId img = this->writeExpression(*c.fArguments[0], out);
855 std::vector<std::unique_ptr<Expression>> args;
Greg Danielcf1a4f52020-09-08 15:25:23 -0400856 args.emplace_back(new IntLiteral(fContext, -1, 0));
857 args.emplace_back(new IntLiteral(fContext, -1, 0));
Ethan Nicholas30d30222020-09-11 12:27:26 -0400858 Constructor ctor(-1, fContext.fInt2_Type.get(), std::move(args));
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400859 SpvId coords = this->writeConstantVector(ctor);
860 if (1 == c.fArguments.size()) {
861 this->writeInstruction(SpvOpImageRead,
Ethan Nicholas30d30222020-09-11 12:27:26 -0400862 this->getType(callType),
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400863 result,
864 img,
865 coords,
866 out);
867 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400868 SkASSERT(2 == c.fArguments.size());
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400869 SpvId sample = this->writeExpression(*c.fArguments[1], out);
870 this->writeInstruction(SpvOpImageRead,
Ethan Nicholas30d30222020-09-11 12:27:26 -0400871 this->getType(callType),
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400872 result,
873 img,
874 coords,
875 SpvImageOperandsSampleMask,
876 sample,
877 out);
878 }
879 break;
880 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700881 case kTexture_SpecialIntrinsic: {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500882 SpvOp_ op = SpvOpImageSampleImplicitLod;
Ethan Nicholas30d30222020-09-11 12:27:26 -0400883 const Type& arg1Type = c.fArguments[1]->type();
884 switch (c.fArguments[0]->type().dimensions()) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500885 case SpvDim1D:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400886 if (arg1Type == *fContext.fFloat2_Type) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500887 op = SpvOpImageSampleProjImplicitLod;
888 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400889 SkASSERT(arg1Type == *fContext.fFloat_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500890 }
891 break;
892 case SpvDim2D:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400893 if (arg1Type == *fContext.fFloat3_Type) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500894 op = SpvOpImageSampleProjImplicitLod;
895 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400896 SkASSERT(arg1Type == *fContext.fFloat2_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500897 }
898 break;
899 case SpvDim3D:
Ethan Nicholas30d30222020-09-11 12:27:26 -0400900 if (arg1Type == *fContext.fFloat4_Type) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500901 op = SpvOpImageSampleProjImplicitLod;
902 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -0400903 SkASSERT(arg1Type == *fContext.fFloat3_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500904 }
905 break;
906 case SpvDimCube: // fall through
907 case SpvDimRect: // fall through
908 case SpvDimBuffer: // fall through
909 case SpvDimSubpassData:
910 break;
911 }
Ethan Nicholas30d30222020-09-11 12:27:26 -0400912 SpvId type = this->getType(callType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700913 SpvId sampler = this->writeExpression(*c.fArguments[0], out);
914 SpvId uv = this->writeExpression(*c.fArguments[1], out);
915 if (c.fArguments.size() == 3) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500916 this->writeInstruction(op, type, result, sampler, uv,
ethannicholasb3058bd2016-07-01 08:22:01 -0700917 SpvImageOperandsBiasMask,
918 this->writeExpression(*c.fArguments[2], out),
919 out);
920 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400921 SkASSERT(c.fArguments.size() == 2);
Brian Osman8a83ca42018-02-12 14:32:17 -0500922 if (fProgram.fSettings.fSharpenTextures) {
923 FloatLiteral lodBias(fContext, -1, -0.5);
924 this->writeInstruction(op, type, result, sampler, uv,
925 SpvImageOperandsBiasMask,
926 this->writeFloatLiteral(lodBias),
927 out);
928 } else {
929 this->writeInstruction(op, type, result, sampler, uv,
930 out);
931 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700932 }
933 break;
934 }
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500935 case kMod_SpecialIntrinsic: {
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500936 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400937 SkASSERT(args.size() == 2);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400938 const Type& operandType = c.fArguments[0]->type();
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500939 SpvOp_ op;
940 if (is_float(fContext, operandType)) {
941 op = SpvOpFMod;
942 } else if (is_signed(fContext, operandType)) {
943 op = SpvOpSMod;
944 } else if (is_unsigned(fContext, operandType)) {
945 op = SpvOpUMod;
946 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400947 SkASSERT(false);
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500948 return 0;
949 }
950 this->writeOpCode(op, 5, out);
951 this->writeWord(this->getType(operandType), out);
952 this->writeWord(result, out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500953 this->writeWord(args[0], out);
954 this->writeWord(args[1], out);
955 break;
956 }
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700957 case kDFdy_SpecialIntrinsic: {
958 SpvId fn = this->writeExpression(*c.fArguments[0], out);
959 this->writeOpCode(SpvOpDPdy, 4, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400960 this->writeWord(this->getType(callType), out);
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700961 this->writeWord(result, out);
962 this->writeWord(fn, out);
963 if (fProgram.fSettings.fFlipY) {
964 // Flipping Y also negates the Y derivatives.
965 SpvId flipped = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -0400966 this->writeInstruction(SpvOpFNegate, this->getType(callType), flipped, result,
967 out);
968 this->writePrecisionModifier(callType, flipped);
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700969 return flipped;
970 }
971 break;
972 }
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500973 case kClamp_SpecialIntrinsic: {
974 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400975 SkASSERT(args.size() == 3);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400976 this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FClamp, GLSLstd450SClamp,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500977 GLSLstd450UClamp, args, out);
978 break;
979 }
980 case kMax_SpecialIntrinsic: {
981 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400982 SkASSERT(args.size() == 2);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400983 this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMax, GLSLstd450SMax,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500984 GLSLstd450UMax, args, out);
985 break;
986 }
987 case kMin_SpecialIntrinsic: {
988 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400989 SkASSERT(args.size() == 2);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400990 this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMin, GLSLstd450SMin,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500991 GLSLstd450UMin, args, out);
992 break;
993 }
994 case kMix_SpecialIntrinsic: {
995 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400996 SkASSERT(args.size() == 3);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400997 this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMix, SpvOpUndef,
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500998 SpvOpUndef, args, out);
999 break;
Ethan Nicholas70a44b22017-11-30 09:09:16 -05001000 }
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -04001001 case kSaturate_SpecialIntrinsic: {
1002 SkASSERT(c.fArguments.size() == 1);
1003 std::vector<std::unique_ptr<Expression>> finalArgs;
1004 finalArgs.push_back(c.fArguments[0]->clone());
1005 finalArgs.emplace_back(new FloatLiteral(fContext, -1, 0));
1006 finalArgs.emplace_back(new FloatLiteral(fContext, -1, 1));
1007 std::vector<SpvId> spvArgs = this->vectorize(finalArgs, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001008 this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FClamp, GLSLstd450SClamp,
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -04001009 GLSLstd450UClamp, spvArgs, out);
1010 break;
1011 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001012 }
1013 return result;
1014}
1015
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001016SpvId SPIRVCodeGenerator::writeFunctionCall(const FunctionCall& c, OutputStream& out) {
ethannicholasd598f792016-07-25 10:08:54 -07001017 const auto& entry = fFunctionMap.find(&c.fFunction);
ethannicholasb3058bd2016-07-01 08:22:01 -07001018 if (entry == fFunctionMap.end()) {
1019 return this->writeIntrinsicCall(c, out);
1020 }
1021 // stores (variable, type, lvalue) pairs to extract and save after the function call is complete
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001022 std::vector<std::tuple<SpvId, const Type*, std::unique_ptr<LValue>>> lvalues;
ethannicholasb3058bd2016-07-01 08:22:01 -07001023 std::vector<SpvId> arguments;
1024 for (size_t i = 0; i < c.fArguments.size(); i++) {
Greg Daniel64773e62016-11-22 09:44:03 -05001025 // id of temporary variable that we will use to hold this argument, or 0 if it is being
ethannicholasb3058bd2016-07-01 08:22:01 -07001026 // passed directly
1027 SpvId tmpVar;
1028 // if we need a temporary var to store this argument, this is the value to store in the var
1029 SpvId tmpValueId;
ethannicholasd598f792016-07-25 10:08:54 -07001030 if (is_out(*c.fFunction.fParameters[i])) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001031 std::unique_ptr<LValue> lv = this->getLValue(*c.fArguments[i], out);
1032 SpvId ptr = lv->getPointer();
1033 if (ptr) {
1034 arguments.push_back(ptr);
1035 continue;
1036 } else {
1037 // lvalue cannot simply be read and written via a pointer (e.g. a swizzle). Need to
1038 // copy it into a temp, call the function, read the value out of the temp, and then
1039 // update the lvalue.
1040 tmpValueId = lv->load(out);
1041 tmpVar = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001042 lvalues.push_back(std::make_tuple(tmpVar, &c.fArguments[i]->type(), std::move(lv)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001043 }
1044 } else {
1045 // see getFunctionType for an explanation of why we're always using pointer parameters
1046 tmpValueId = this->writeExpression(*c.fArguments[i], out);
1047 tmpVar = this->nextId();
1048 }
Greg Daniel64773e62016-11-22 09:44:03 -05001049 this->writeInstruction(SpvOpVariable,
Ethan Nicholas30d30222020-09-11 12:27:26 -04001050 this->getPointerType(c.fArguments[i]->type(),
ethannicholasb3058bd2016-07-01 08:22:01 -07001051 SpvStorageClassFunction),
Greg Daniel64773e62016-11-22 09:44:03 -05001052 tmpVar,
ethannicholasb3058bd2016-07-01 08:22:01 -07001053 SpvStorageClassFunction,
ethannicholasd598f792016-07-25 10:08:54 -07001054 fVariableBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07001055 this->writeInstruction(SpvOpStore, tmpVar, tmpValueId, out);
1056 arguments.push_back(tmpVar);
1057 }
1058 SpvId result = this->nextId();
1059 this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t) c.fArguments.size(), out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001060 this->writeWord(this->getType(c.type()), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001061 this->writeWord(result, out);
1062 this->writeWord(entry->second, out);
1063 for (SpvId id : arguments) {
1064 this->writeWord(id, out);
1065 }
1066 // now that the call is complete, we may need to update some lvalues with the new values of out
1067 // arguments
1068 for (const auto& tuple : lvalues) {
1069 SpvId load = this->nextId();
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001070 this->writeInstruction(SpvOpLoad, getType(*std::get<1>(tuple)), load, std::get<0>(tuple),
1071 out);
1072 this->writePrecisionModifier(*std::get<1>(tuple), load);
ethannicholasb3058bd2016-07-01 08:22:01 -07001073 std::get<2>(tuple)->store(load, out);
1074 }
1075 return result;
1076}
1077
ethannicholasf789b382016-08-03 12:43:36 -07001078SpvId SPIRVCodeGenerator::writeConstantVector(const Constructor& c) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001079 const Type& type = c.type();
1080 SkASSERT(type.typeKind() == Type::TypeKind::kVector && c.isCompileTimeConstant());
ethannicholasb3058bd2016-07-01 08:22:01 -07001081 SpvId result = this->nextId();
1082 std::vector<SpvId> arguments;
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001083 for (const std::unique_ptr<Expression>& arg : c.arguments()) {
1084 arguments.push_back(this->writeExpression(*arg, fConstantBuffer));
ethannicholasb3058bd2016-07-01 08:22:01 -07001085 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04001086 SpvId typeId = this->getType(type);
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001087 if (c.arguments().size() == 1) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001088 // with a single argument, a vector will have all of its entries equal to the argument
Ethan Nicholas30d30222020-09-11 12:27:26 -04001089 this->writeOpCode(SpvOpConstantComposite, 3 + type.columns(), fConstantBuffer);
1090 this->writeWord(typeId, fConstantBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07001091 this->writeWord(result, fConstantBuffer);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001092 for (int i = 0; i < type.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001093 this->writeWord(arguments[0], fConstantBuffer);
1094 }
1095 } else {
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001096 this->writeOpCode(SpvOpConstantComposite, 3 + (int32_t) c.arguments().size(),
ethannicholasb3058bd2016-07-01 08:22:01 -07001097 fConstantBuffer);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001098 this->writeWord(typeId, fConstantBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07001099 this->writeWord(result, fConstantBuffer);
1100 for (SpvId id : arguments) {
1101 this->writeWord(id, fConstantBuffer);
1102 }
1103 }
1104 return result;
1105}
1106
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001107SpvId SPIRVCodeGenerator::writeFloatConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001108 const Type& constructorType = c.type();
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001109 SkASSERT(c.arguments().size() == 1);
1110 const Type& argType = c.arguments()[0]->type();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001111 SkASSERT(constructorType.isFloat());
1112 SkASSERT(argType.isNumber());
ethannicholasb3058bd2016-07-01 08:22:01 -07001113 SpvId result = this->nextId();
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001114 SpvId parameter = this->writeExpression(*c.arguments()[0], out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001115 if (argType.isSigned()) {
1116 this->writeInstruction(SpvOpConvertSToF, this->getType(constructorType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001117 out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001118 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001119 SkASSERT(argType.isUnsigned());
1120 this->writeInstruction(SpvOpConvertUToF, this->getType(constructorType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001121 out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001122 }
1123 return result;
1124}
1125
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001126SpvId SPIRVCodeGenerator::writeIntConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001127 const Type& constructorType = c.type();
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001128 SkASSERT(c.arguments().size() == 1);
1129 const Type& argType = c.arguments()[0]->type();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001130 SkASSERT(constructorType.isSigned());
1131 SkASSERT(argType.isNumber());
ethannicholasb3058bd2016-07-01 08:22:01 -07001132 SpvId result = this->nextId();
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001133 SpvId parameter = this->writeExpression(*c.arguments()[0], out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001134 if (argType.isFloat()) {
1135 this->writeInstruction(SpvOpConvertFToS, this->getType(constructorType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001136 out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001137 }
1138 else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001139 SkASSERT(argType.isUnsigned());
1140 this->writeInstruction(SpvOpBitcast, this->getType(constructorType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001141 out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001142 }
1143 return result;
1144}
1145
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001146SpvId SPIRVCodeGenerator::writeUIntConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001147 const Type& constructorType = c.type();
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001148 SkASSERT(c.arguments().size() == 1);
1149 const Type& argType = c.arguments()[0]->type();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001150 SkASSERT(constructorType.isUnsigned());
1151 SkASSERT(argType.isNumber());
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001152 SpvId result = this->nextId();
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001153 SpvId parameter = this->writeExpression(*c.arguments()[0], out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001154 if (argType.isFloat()) {
1155 this->writeInstruction(SpvOpConvertFToU, this->getType(constructorType), result, parameter,
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001156 out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001157 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001158 SkASSERT(argType.isSigned());
1159 this->writeInstruction(SpvOpBitcast, this->getType(constructorType), result, parameter,
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001160 out);
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001161 }
1162 return result;
1163}
1164
Ethan Nicholas84645e32017-02-09 13:57:14 -05001165void SPIRVCodeGenerator::writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001166 OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001167 FloatLiteral zero(fContext, -1, 0);
Ethan Nicholas84645e32017-02-09 13:57:14 -05001168 SpvId zeroId = this->writeFloatLiteral(zero);
1169 std::vector<SpvId> columnIds;
1170 for (int column = 0; column < type.columns(); column++) {
1171 this->writeOpCode(SpvOpCompositeConstruct, 3 + type.rows(),
1172 out);
1173 this->writeWord(this->getType(type.componentType().toCompound(fContext, type.rows(), 1)),
1174 out);
1175 SpvId columnId = this->nextId();
1176 this->writeWord(columnId, out);
1177 columnIds.push_back(columnId);
1178 for (int row = 0; row < type.columns(); row++) {
1179 this->writeWord(row == column ? diagonal : zeroId, out);
1180 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001181 this->writePrecisionModifier(type, columnId);
Ethan Nicholas84645e32017-02-09 13:57:14 -05001182 }
1183 this->writeOpCode(SpvOpCompositeConstruct, 3 + type.columns(),
1184 out);
1185 this->writeWord(this->getType(type), out);
1186 this->writeWord(id, out);
John Stilesf621e232020-08-25 13:33:02 -04001187 for (SpvId columnId : columnIds) {
1188 this->writeWord(columnId, out);
Ethan Nicholas84645e32017-02-09 13:57:14 -05001189 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001190 this->writePrecisionModifier(type, id);
Ethan Nicholas84645e32017-02-09 13:57:14 -05001191}
1192
1193void SPIRVCodeGenerator::writeMatrixCopy(SpvId id, SpvId src, const Type& srcType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001194 const Type& dstType, OutputStream& out) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001195 SkASSERT(srcType.typeKind() == Type::TypeKind::kMatrix);
1196 SkASSERT(dstType.typeKind() == Type::TypeKind::kMatrix);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001197 SkASSERT(srcType.componentType() == dstType.componentType());
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001198 SpvId srcColumnType = this->getType(srcType.componentType().toCompound(fContext,
1199 srcType.rows(),
1200 1));
1201 SpvId dstColumnType = this->getType(dstType.componentType().toCompound(fContext,
1202 dstType.rows(),
1203 1));
1204 SpvId zeroId;
1205 if (dstType.componentType() == *fContext.fFloat_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001206 FloatLiteral zero(fContext, -1, 0.0);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001207 zeroId = this->writeFloatLiteral(zero);
1208 } else if (dstType.componentType() == *fContext.fInt_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001209 IntLiteral zero(fContext, -1, 0);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001210 zeroId = this->writeIntLiteral(zero);
1211 } else {
1212 ABORT("unsupported matrix component type");
1213 }
1214 SpvId zeroColumn = 0;
1215 SpvId columns[4];
1216 for (int i = 0; i < dstType.columns(); i++) {
1217 if (i < srcType.columns()) {
1218 // we're still inside the src matrix, copy the column
1219 SpvId srcColumn = this->nextId();
1220 this->writeInstruction(SpvOpCompositeExtract, srcColumnType, srcColumn, src, i, out);
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001221 this->writePrecisionModifier(dstType, srcColumn);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001222 SpvId dstColumn;
1223 if (srcType.rows() == dstType.rows()) {
1224 // columns are equal size, don't need to do anything
1225 dstColumn = srcColumn;
1226 }
1227 else if (dstType.rows() > srcType.rows()) {
1228 // dst column is bigger, need to zero-pad it
1229 dstColumn = this->nextId();
1230 int delta = dstType.rows() - srcType.rows();
1231 this->writeOpCode(SpvOpCompositeConstruct, 4 + delta, out);
1232 this->writeWord(dstColumnType, out);
1233 this->writeWord(dstColumn, out);
1234 this->writeWord(srcColumn, out);
John Stilesf621e232020-08-25 13:33:02 -04001235 for (int j = 0; j < delta; ++j) {
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001236 this->writeWord(zeroId, out);
1237 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001238 this->writePrecisionModifier(dstType, dstColumn);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001239 }
1240 else {
1241 // dst column is smaller, need to swizzle the src column
1242 dstColumn = this->nextId();
1243 int count = dstType.rows();
1244 this->writeOpCode(SpvOpVectorShuffle, 5 + count, out);
1245 this->writeWord(dstColumnType, out);
1246 this->writeWord(dstColumn, out);
1247 this->writeWord(srcColumn, out);
1248 this->writeWord(srcColumn, out);
John Stilesf621e232020-08-25 13:33:02 -04001249 for (int j = 0; j < count; j++) {
1250 this->writeWord(j, out);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001251 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001252 this->writePrecisionModifier(dstType, dstColumn);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001253 }
1254 columns[i] = dstColumn;
1255 } else {
1256 // we're past the end of the src matrix, need a vector of zeroes
1257 if (!zeroColumn) {
1258 zeroColumn = this->nextId();
1259 this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.rows(), out);
1260 this->writeWord(dstColumnType, out);
1261 this->writeWord(zeroColumn, out);
John Stilesf621e232020-08-25 13:33:02 -04001262 for (int j = 0; j < dstType.rows(); ++j) {
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001263 this->writeWord(zeroId, out);
1264 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001265 this->writePrecisionModifier(dstType, zeroColumn);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001266 }
1267 columns[i] = zeroColumn;
1268 }
1269 }
1270 this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.columns(), out);
1271 this->writeWord(this->getType(dstType), out);
1272 this->writeWord(id, out);
1273 for (int i = 0; i < dstType.columns(); i++) {
1274 this->writeWord(columns[i], out);
1275 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001276 this->writePrecisionModifier(dstType, id);
Ethan Nicholas84645e32017-02-09 13:57:14 -05001277}
1278
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001279void SPIRVCodeGenerator::addColumnEntry(SpvId columnType, Precision precision,
1280 std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -04001281 std::vector<SpvId>* columnIds,
1282 int* currentCount, int rows, SpvId entry,
1283 OutputStream& out) {
1284 SkASSERT(*currentCount < rows);
1285 ++(*currentCount);
1286 currentColumn->push_back(entry);
1287 if (*currentCount == rows) {
1288 *currentCount = 0;
1289 this->writeOpCode(SpvOpCompositeConstruct, 3 + currentColumn->size(), out);
1290 this->writeWord(columnType, out);
1291 SpvId columnId = this->nextId();
1292 this->writeWord(columnId, out);
1293 columnIds->push_back(columnId);
1294 for (SpvId id : *currentColumn) {
1295 this->writeWord(id, out);
1296 }
1297 currentColumn->clear();
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001298 this->writePrecisionModifier(precision, columnId);
Ethan Nicholas5c46b722019-03-22 14:32:37 -04001299 }
1300}
1301
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001302SpvId SPIRVCodeGenerator::writeMatrixConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001303 const Type& type = c.type();
1304 SkASSERT(type.typeKind() == Type::TypeKind::kMatrix);
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001305 SkASSERT(c.arguments().size() > 0);
1306 const Type& arg0Type = c.arguments()[0]->type();
ethannicholasb3058bd2016-07-01 08:22:01 -07001307 // go ahead and write the arguments so we don't try to write new instructions in the middle of
1308 // an instruction
1309 std::vector<SpvId> arguments;
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001310 for (size_t i = 0; i < c.arguments().size(); i++) {
1311 arguments.push_back(this->writeExpression(*c.arguments()[i], out));
ethannicholasb3058bd2016-07-01 08:22:01 -07001312 }
1313 SpvId result = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001314 int rows = type.rows();
1315 int columns = type.columns();
1316 if (arguments.size() == 1 && arg0Type.typeKind() == Type::TypeKind::kScalar) {
1317 this->writeUniformScaleMatrix(result, arguments[0], type, out);
1318 } else if (arguments.size() == 1 && arg0Type.typeKind() == Type::TypeKind::kMatrix) {
1319 this->writeMatrixCopy(result, arguments[0], arg0Type, type, out);
Ethan Nicholase6592142020-09-08 10:22:09 -04001320 } else if (arguments.size() == 1 &&
Ethan Nicholas30d30222020-09-11 12:27:26 -04001321 arg0Type.typeKind() == Type::TypeKind::kVector) {
1322 SkASSERT(type.rows() == 2 && type.columns() == 2);
1323 SkASSERT(arg0Type.columns() == 4);
1324 SpvId componentType = this->getType(type.componentType());
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001325 SpvId v[4];
1326 for (int i = 0; i < 4; ++i) {
1327 v[i] = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001328 this->writeInstruction(SpvOpCompositeExtract, componentType, v[i], arguments[0], i,
1329 out);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001330 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04001331 SpvId columnType = this->getType(type.componentType().toCompound(fContext, 2, 1));
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001332 SpvId column1 = this->nextId();
1333 this->writeInstruction(SpvOpCompositeConstruct, columnType, column1, v[0], v[1], out);
1334 SpvId column2 = this->nextId();
1335 this->writeInstruction(SpvOpCompositeConstruct, columnType, column2, v[2], v[3], out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001336 this->writeInstruction(SpvOpCompositeConstruct, this->getType(type), result, column1,
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001337 column2, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001338 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001339 SpvId columnType = this->getType(type.componentType().toCompound(fContext, rows, 1));
ethannicholasb3058bd2016-07-01 08:22:01 -07001340 std::vector<SpvId> columnIds;
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001341 // ids of vectors and scalars we have written to the current column so far
1342 std::vector<SpvId> currentColumn;
1343 // the total number of scalars represented by currentColumn's entries
ethannicholasb3058bd2016-07-01 08:22:01 -07001344 int currentCount = 0;
Ethan Nicholas30d30222020-09-11 12:27:26 -04001345 Precision precision = type.highPrecision() ? Precision::kHigh : Precision::kLow;
ethannicholasb3058bd2016-07-01 08:22:01 -07001346 for (size_t i = 0; i < arguments.size(); i++) {
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001347 const Type& argType = c.arguments()[i]->type();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001348 if (currentCount == 0 && argType.typeKind() == Type::TypeKind::kVector &&
1349 argType.columns() == type.rows()) {
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001350 // this is a complete column by itself
ethannicholasb3058bd2016-07-01 08:22:01 -07001351 columnIds.push_back(arguments[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001352 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001353 if (argType.columns() == 1) {
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001354 this->addColumnEntry(columnType, precision, &currentColumn, &columnIds,
1355 &currentCount, rows, arguments[i], out);
Ethan Nicholasbe850ad2018-04-27 10:36:31 -04001356 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001357 SpvId componentType = this->getType(argType.componentType());
1358 for (int j = 0; j < argType.columns(); ++j) {
Ethan Nicholasbe850ad2018-04-27 10:36:31 -04001359 SpvId swizzle = this->nextId();
1360 this->writeInstruction(SpvOpCompositeExtract, componentType, swizzle,
1361 arguments[i], j, out);
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001362 this->addColumnEntry(columnType, precision, &currentColumn, &columnIds,
1363 &currentCount, rows, swizzle, out);
Ethan Nicholasbe850ad2018-04-27 10:36:31 -04001364 }
1365 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001366 }
1367 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001368 SkASSERT(columnIds.size() == (size_t) columns);
ethannicholasb3058bd2016-07-01 08:22:01 -07001369 this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001370 this->writeWord(this->getType(type), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001371 this->writeWord(result, out);
1372 for (SpvId id : columnIds) {
1373 this->writeWord(id, out);
1374 }
1375 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04001376 this->writePrecisionModifier(type, result);
ethannicholasb3058bd2016-07-01 08:22:01 -07001377 return result;
1378}
1379
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001380SpvId SPIRVCodeGenerator::writeVectorConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001381 const Type& type = c.type();
1382 SkASSERT(type.typeKind() == Type::TypeKind::kVector);
Brian Osmanb6b95732020-06-30 11:44:27 -04001383 if (c.isCompileTimeConstant()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001384 return this->writeConstantVector(c);
1385 }
1386 // go ahead and write the arguments so we don't try to write new instructions in the middle of
1387 // an instruction
1388 std::vector<SpvId> arguments;
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001389 for (size_t i = 0; i < c.arguments().size(); i++) {
1390 const Type& argType = c.arguments()[i]->type();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001391 if (argType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001392 // SPIR-V doesn't support vector(vector-of-different-type) directly, so we need to
1393 // extract the components and convert them in that case manually. On top of that,
1394 // as of this writing there's a bug in the Intel Vulkan driver where OpCreateComposite
1395 // doesn't handle vector arguments at all, so we always extract vector components and
1396 // pass them into OpCreateComposite individually.
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001397 SpvId vec = this->writeExpression(*c.arguments()[i], out);
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001398 SpvOp_ op = SpvOpUndef;
Ethan Nicholas30d30222020-09-11 12:27:26 -04001399 const Type& src = argType.componentType();
1400 const Type& dst = type.componentType();
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001401 if (dst == *fContext.fFloat_Type || dst == *fContext.fHalf_Type) {
1402 if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) {
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001403 if (c.arguments().size() == 1) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001404 return vec;
1405 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001406 } else if (src == *fContext.fInt_Type ||
1407 src == *fContext.fShort_Type ||
1408 src == *fContext.fByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001409 op = SpvOpConvertSToF;
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001410 } else if (src == *fContext.fUInt_Type ||
1411 src == *fContext.fUShort_Type ||
1412 src == *fContext.fUByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001413 op = SpvOpConvertUToF;
1414 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001415 SkASSERT(false);
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001416 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001417 } else if (dst == *fContext.fInt_Type ||
1418 dst == *fContext.fShort_Type ||
1419 dst == *fContext.fByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001420 if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) {
1421 op = SpvOpConvertFToS;
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001422 } else if (src == *fContext.fInt_Type ||
1423 src == *fContext.fShort_Type ||
1424 src == *fContext.fByte_Type) {
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001425 if (c.arguments().size() == 1) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001426 return vec;
1427 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001428 } else if (src == *fContext.fUInt_Type ||
1429 src == *fContext.fUShort_Type ||
1430 src == *fContext.fUByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001431 op = SpvOpBitcast;
1432 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001433 SkASSERT(false);
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001434 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001435 } else if (dst == *fContext.fUInt_Type ||
1436 dst == *fContext.fUShort_Type ||
1437 dst == *fContext.fUByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001438 if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) {
1439 op = SpvOpConvertFToS;
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001440 } else if (src == *fContext.fInt_Type ||
1441 src == *fContext.fShort_Type ||
1442 src == *fContext.fByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001443 op = SpvOpBitcast;
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001444 } else if (src == *fContext.fUInt_Type ||
1445 src == *fContext.fUShort_Type ||
1446 src == *fContext.fUByte_Type) {
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001447 if (c.arguments().size() == 1) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001448 return vec;
1449 }
1450 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001451 SkASSERT(false);
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001452 }
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001453 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04001454 for (int j = 0; j < argType.columns(); j++) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001455 SpvId swizzle = this->nextId();
1456 this->writeInstruction(SpvOpCompositeExtract, this->getType(src), swizzle, vec, j,
1457 out);
1458 if (op != SpvOpUndef) {
1459 SpvId cast = this->nextId();
1460 this->writeInstruction(op, this->getType(dst), cast, swizzle, out);
1461 arguments.push_back(cast);
1462 } else {
1463 arguments.push_back(swizzle);
1464 }
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001465 }
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001466 } else {
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001467 arguments.push_back(this->writeExpression(*c.arguments()[i], out));
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001468 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001469 }
1470 SpvId result = this->nextId();
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001471 if (arguments.size() == 1 && c.arguments()[0]->type().typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001472 this->writeOpCode(SpvOpCompositeConstruct, 3 + type.columns(), out);
1473 this->writeWord(this->getType(type), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001474 this->writeWord(result, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001475 for (int i = 0; i < type.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001476 this->writeWord(arguments[0], out);
1477 }
1478 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001479 SkASSERT(arguments.size() > 1);
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001480 this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) arguments.size(), out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001481 this->writeWord(this->getType(type), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001482 this->writeWord(result, out);
1483 for (SpvId id : arguments) {
1484 this->writeWord(id, out);
1485 }
1486 }
1487 return result;
1488}
1489
Ethan Nicholasbd553222017-07-18 15:54:59 -04001490SpvId SPIRVCodeGenerator::writeArrayConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001491 const Type& type = c.type();
1492 SkASSERT(type.typeKind() == Type::TypeKind::kArray);
Ethan Nicholasbd553222017-07-18 15:54:59 -04001493 // go ahead and write the arguments so we don't try to write new instructions in the middle of
1494 // an instruction
1495 std::vector<SpvId> arguments;
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001496 for (size_t i = 0; i < c.arguments().size(); i++) {
1497 arguments.push_back(this->writeExpression(*c.arguments()[i], out));
Ethan Nicholasbd553222017-07-18 15:54:59 -04001498 }
1499 SpvId result = this->nextId();
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001500 this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) c.arguments().size(), out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001501 this->writeWord(this->getType(type), out);
Ethan Nicholasbd553222017-07-18 15:54:59 -04001502 this->writeWord(result, out);
1503 for (SpvId id : arguments) {
1504 this->writeWord(id, out);
1505 }
1506 return result;
1507}
1508
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001509SpvId SPIRVCodeGenerator::writeConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001510 const Type& type = c.type();
Ethan Nicholasf70f0442020-09-29 12:41:35 -04001511 if (c.arguments().size() == 1 &&
1512 this->getActualType(type) == this->getActualType(c.arguments()[0]->type())) {
1513 return this->writeExpression(*c.arguments()[0], out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001514 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04001515 if (type == *fContext.fFloat_Type || type == *fContext.fHalf_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001516 return this->writeFloatConstructor(c, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001517 } else if (type == *fContext.fInt_Type ||
1518 type == *fContext.fShort_Type ||
1519 type == *fContext.fByte_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001520 return this->writeIntConstructor(c, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001521 } else if (type == *fContext.fUInt_Type ||
1522 type == *fContext.fUShort_Type ||
1523 type == *fContext.fUByte_Type) {
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001524 return this->writeUIntConstructor(c, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001525 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04001526 switch (type.typeKind()) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001527 case Type::TypeKind::kVector:
ethannicholasb3058bd2016-07-01 08:22:01 -07001528 return this->writeVectorConstructor(c, out);
Ethan Nicholase6592142020-09-08 10:22:09 -04001529 case Type::TypeKind::kMatrix:
ethannicholasb3058bd2016-07-01 08:22:01 -07001530 return this->writeMatrixConstructor(c, out);
Ethan Nicholase6592142020-09-08 10:22:09 -04001531 case Type::TypeKind::kArray:
Ethan Nicholasbd553222017-07-18 15:54:59 -04001532 return this->writeArrayConstructor(c, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001533 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001534#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07001535 ABORT("unsupported constructor: %s", c.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001536#endif
1537 return -1;
ethannicholasb3058bd2016-07-01 08:22:01 -07001538 }
1539}
1540
1541SpvStorageClass_ get_storage_class(const Modifiers& modifiers) {
1542 if (modifiers.fFlags & Modifiers::kIn_Flag) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001543 SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag));
ethannicholasb3058bd2016-07-01 08:22:01 -07001544 return SpvStorageClassInput;
1545 } else if (modifiers.fFlags & Modifiers::kOut_Flag) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001546 SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag));
ethannicholasb3058bd2016-07-01 08:22:01 -07001547 return SpvStorageClassOutput;
1548 } else if (modifiers.fFlags & Modifiers::kUniform_Flag) {
Ethan Nicholas39204fd2017-11-27 13:12:30 -05001549 if (modifiers.fLayout.fFlags & Layout::kPushConstant_Flag) {
ethannicholas8ac838d2016-11-22 08:39:36 -08001550 return SpvStorageClassPushConstant;
1551 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001552 return SpvStorageClassUniform;
1553 } else {
1554 return SpvStorageClassFunction;
1555 }
1556}
1557
ethannicholasf789b382016-08-03 12:43:36 -07001558SpvStorageClass_ get_storage_class(const Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001559 switch (expr.kind()) {
1560 case Expression::Kind::kVariableReference: {
Brian Osman79457ef2020-09-24 15:01:27 -04001561 const Variable& var = *expr.as<VariableReference>().fVariable;
Ethan Nicholasc6f5e102017-03-31 14:53:17 -04001562 if (var.fStorage != Variable::kGlobal_Storage) {
1563 return SpvStorageClassFunction;
1564 }
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001565 SpvStorageClass_ result = get_storage_class(var.fModifiers);
1566 if (result == SpvStorageClassFunction) {
1567 result = SpvStorageClassPrivate;
1568 }
1569 return result;
Ethan Nicholasc6f5e102017-03-31 14:53:17 -04001570 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001571 case Expression::Kind::kFieldAccess:
Brian Osman79457ef2020-09-24 15:01:27 -04001572 return get_storage_class(*expr.as<FieldAccess>().fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -04001573 case Expression::Kind::kIndex:
Brian Osman79457ef2020-09-24 15:01:27 -04001574 return get_storage_class(*expr.as<IndexExpression>().fBase);
ethannicholasb3058bd2016-07-01 08:22:01 -07001575 default:
1576 return SpvStorageClassFunction;
1577 }
1578}
1579
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001580std::vector<SpvId> SPIRVCodeGenerator::getAccessChain(const Expression& expr, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001581 std::vector<SpvId> chain;
Ethan Nicholase6592142020-09-08 10:22:09 -04001582 switch (expr.kind()) {
1583 case Expression::Kind::kIndex: {
ethannicholasb3058bd2016-07-01 08:22:01 -07001584 IndexExpression& indexExpr = (IndexExpression&) expr;
1585 chain = this->getAccessChain(*indexExpr.fBase, out);
1586 chain.push_back(this->writeExpression(*indexExpr.fIndex, out));
1587 break;
1588 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001589 case Expression::Kind::kFieldAccess: {
ethannicholasb3058bd2016-07-01 08:22:01 -07001590 FieldAccess& fieldExpr = (FieldAccess&) expr;
1591 chain = this->getAccessChain(*fieldExpr.fBase, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001592 IntLiteral index(fContext, -1, fieldExpr.fFieldIndex);
ethannicholasb3058bd2016-07-01 08:22:01 -07001593 chain.push_back(this->writeIntLiteral(index));
1594 break;
1595 }
Ethan Nicholasa9a06902019-01-07 14:42:40 -05001596 default: {
1597 SpvId id = this->getLValue(expr, out)->getPointer();
1598 SkASSERT(id != 0);
1599 chain.push_back(id);
1600 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001601 }
1602 return chain;
1603}
1604
1605class PointerLValue : public SPIRVCodeGenerator::LValue {
1606public:
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001607 PointerLValue(SPIRVCodeGenerator& gen, SpvId pointer, SpvId type,
1608 SPIRVCodeGenerator::Precision precision)
ethannicholasb3058bd2016-07-01 08:22:01 -07001609 : fGen(gen)
1610 , fPointer(pointer)
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001611 , fType(type)
1612 , fPrecision(precision) {}
ethannicholasb3058bd2016-07-01 08:22:01 -07001613
John Stiles1cf2c8d2020-08-13 22:58:04 -04001614 SpvId getPointer() override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001615 return fPointer;
1616 }
1617
John Stiles1cf2c8d2020-08-13 22:58:04 -04001618 SpvId load(OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001619 SpvId result = fGen.nextId();
1620 fGen.writeInstruction(SpvOpLoad, fType, result, fPointer, out);
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001621 fGen.writePrecisionModifier(fPrecision, result);
ethannicholasb3058bd2016-07-01 08:22:01 -07001622 return result;
1623 }
1624
John Stiles1cf2c8d2020-08-13 22:58:04 -04001625 void store(SpvId value, OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001626 fGen.writeInstruction(SpvOpStore, fPointer, value, out);
1627 }
1628
1629private:
1630 SPIRVCodeGenerator& fGen;
1631 const SpvId fPointer;
1632 const SpvId fType;
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001633 const SPIRVCodeGenerator::Precision fPrecision;
ethannicholasb3058bd2016-07-01 08:22:01 -07001634};
1635
1636class SwizzleLValue : public SPIRVCodeGenerator::LValue {
1637public:
Greg Daniel64773e62016-11-22 09:44:03 -05001638 SwizzleLValue(SPIRVCodeGenerator& gen, SpvId vecPointer, const std::vector<int>& components,
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001639 const Type& baseType, const Type& swizzleType,
1640 SPIRVCodeGenerator::Precision precision)
ethannicholasb3058bd2016-07-01 08:22:01 -07001641 : fGen(gen)
1642 , fVecPointer(vecPointer)
1643 , fComponents(components)
1644 , fBaseType(baseType)
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001645 , fSwizzleType(swizzleType)
1646 , fPrecision(precision) {}
ethannicholasb3058bd2016-07-01 08:22:01 -07001647
John Stiles1cf2c8d2020-08-13 22:58:04 -04001648 SpvId getPointer() override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001649 return 0;
1650 }
1651
John Stiles1cf2c8d2020-08-13 22:58:04 -04001652 SpvId load(OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001653 SpvId base = fGen.nextId();
1654 fGen.writeInstruction(SpvOpLoad, fGen.getType(fBaseType), base, fVecPointer, out);
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001655 fGen.writePrecisionModifier(fPrecision, base);
ethannicholasb3058bd2016-07-01 08:22:01 -07001656 SpvId result = fGen.nextId();
1657 fGen.writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) fComponents.size(), out);
1658 fGen.writeWord(fGen.getType(fSwizzleType), out);
1659 fGen.writeWord(result, out);
1660 fGen.writeWord(base, out);
1661 fGen.writeWord(base, out);
1662 for (int component : fComponents) {
1663 fGen.writeWord(component, out);
1664 }
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001665 fGen.writePrecisionModifier(fPrecision, result);
ethannicholasb3058bd2016-07-01 08:22:01 -07001666 return result;
1667 }
1668
John Stiles1cf2c8d2020-08-13 22:58:04 -04001669 void store(SpvId value, OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001670 // use OpVectorShuffle to mix and match the vector components. We effectively create
1671 // a virtual vector out of the concatenation of the left and right vectors, and then
Greg Daniel64773e62016-11-22 09:44:03 -05001672 // select components from this virtual vector to make the result vector. For
ethannicholasb3058bd2016-07-01 08:22:01 -07001673 // instance, given:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001674 // float3L = ...;
1675 // float3R = ...;
ethannicholasb3058bd2016-07-01 08:22:01 -07001676 // L.xz = R.xy;
Greg Daniel64773e62016-11-22 09:44:03 -05001677 // we end up with the virtual vector (L.x, L.y, L.z, R.x, R.y, R.z). Then we want
ethannicholasb3058bd2016-07-01 08:22:01 -07001678 // our result vector to look like (R.x, L.y, R.y), so we need to select indices
1679 // (3, 1, 4).
1680 SpvId base = fGen.nextId();
1681 fGen.writeInstruction(SpvOpLoad, fGen.getType(fBaseType), base, fVecPointer, out);
1682 SpvId shuffle = fGen.nextId();
1683 fGen.writeOpCode(SpvOpVectorShuffle, 5 + fBaseType.columns(), out);
1684 fGen.writeWord(fGen.getType(fBaseType), out);
1685 fGen.writeWord(shuffle, out);
1686 fGen.writeWord(base, out);
1687 fGen.writeWord(value, out);
1688 for (int i = 0; i < fBaseType.columns(); i++) {
1689 // current offset into the virtual vector, defaults to pulling the unmodified
1690 // value from the left side
1691 int offset = i;
1692 // check to see if we are writing this component
1693 for (size_t j = 0; j < fComponents.size(); j++) {
1694 if (fComponents[j] == i) {
Greg Daniel64773e62016-11-22 09:44:03 -05001695 // we're writing to this component, so adjust the offset to pull from
ethannicholasb3058bd2016-07-01 08:22:01 -07001696 // the correct component of the right side instead of preserving the
1697 // value from the left
1698 offset = (int) (j + fBaseType.columns());
1699 break;
1700 }
1701 }
1702 fGen.writeWord(offset, out);
1703 }
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001704 fGen.writePrecisionModifier(fPrecision, shuffle);
ethannicholasb3058bd2016-07-01 08:22:01 -07001705 fGen.writeInstruction(SpvOpStore, fVecPointer, shuffle, out);
1706 }
1707
1708private:
1709 SPIRVCodeGenerator& fGen;
1710 const SpvId fVecPointer;
1711 const std::vector<int>& fComponents;
1712 const Type& fBaseType;
1713 const Type& fSwizzleType;
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001714 const SPIRVCodeGenerator::Precision fPrecision;
ethannicholasb3058bd2016-07-01 08:22:01 -07001715};
1716
Greg Daniel64773e62016-11-22 09:44:03 -05001717std::unique_ptr<SPIRVCodeGenerator::LValue> SPIRVCodeGenerator::getLValue(const Expression& expr,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001718 OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001719 const Type& type = expr.type();
1720 Precision precision = type.highPrecision() ? Precision::kHigh : Precision::kLow;
Ethan Nicholase6592142020-09-08 10:22:09 -04001721 switch (expr.kind()) {
1722 case Expression::Kind::kVariableReference: {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001723 SpvId typeId;
Brian Osman79457ef2020-09-24 15:01:27 -04001724 const Variable& var = *expr.as<VariableReference>().fVariable;
Ethan Nicholas5226b772018-05-03 16:20:41 -04001725 if (var.fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001726 typeId = this->getType(Type("sk_in", Type::TypeKind::kArray,
1727 var.type().componentType(), fSkInCount));
Ethan Nicholas5226b772018-05-03 16:20:41 -04001728 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001729 typeId = this->getType(type);
Ethan Nicholas5226b772018-05-03 16:20:41 -04001730 }
ethannicholasd598f792016-07-25 10:08:54 -07001731 auto entry = fVariableMap.find(&var);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001732 SkASSERT(entry != fVariableMap.end());
Ethan Nicholas5226b772018-05-03 16:20:41 -04001733 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(*this,
1734 entry->second,
Ethan Nicholas30d30222020-09-11 12:27:26 -04001735 typeId,
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001736 precision));
ethannicholasb3058bd2016-07-01 08:22:01 -07001737 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001738 case Expression::Kind::kIndex: // fall through
1739 case Expression::Kind::kFieldAccess: {
ethannicholasb3058bd2016-07-01 08:22:01 -07001740 std::vector<SpvId> chain = this->getAccessChain(expr, out);
1741 SpvId member = this->nextId();
1742 this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001743 this->writeWord(this->getPointerType(type, get_storage_class(expr)), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001744 this->writeWord(member, out);
1745 for (SpvId idx : chain) {
1746 this->writeWord(idx, out);
1747 }
1748 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
Ethan Nicholas30d30222020-09-11 12:27:26 -04001749 *this,
1750 member,
1751 this->getType(type),
1752 precision));
ethannicholasb3058bd2016-07-01 08:22:01 -07001753 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001754 case Expression::Kind::kSwizzle: {
ethannicholasb3058bd2016-07-01 08:22:01 -07001755 Swizzle& swizzle = (Swizzle&) expr;
1756 size_t count = swizzle.fComponents.size();
1757 SpvId base = this->getLValue(*swizzle.fBase, out)->getPointer();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001758 SkASSERT(base);
ethannicholasb3058bd2016-07-01 08:22:01 -07001759 if (count == 1) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001760 IntLiteral index(fContext, -1, swizzle.fComponents[0]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001761 SpvId member = this->nextId();
1762 this->writeInstruction(SpvOpAccessChain,
Ethan Nicholas30d30222020-09-11 12:27:26 -04001763 this->getPointerType(type,
Greg Daniel64773e62016-11-22 09:44:03 -05001764 get_storage_class(*swizzle.fBase)),
1765 member,
1766 base,
1767 this->writeIntLiteral(index),
ethannicholasb3058bd2016-07-01 08:22:01 -07001768 out);
1769 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
Ethan Nicholas30d30222020-09-11 12:27:26 -04001770 *this,
1771 member,
1772 this->getType(type),
1773 precision));
ethannicholasb3058bd2016-07-01 08:22:01 -07001774 } else {
1775 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new SwizzleLValue(
Ethan Nicholas30d30222020-09-11 12:27:26 -04001776 *this,
1777 base,
1778 swizzle.fComponents,
1779 swizzle.fBase->type(),
1780 type,
1781 precision));
ethannicholasb3058bd2016-07-01 08:22:01 -07001782 }
1783 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001784 case Expression::Kind::kTernary: {
Ethan Nicholasa583b812018-01-18 13:32:11 -05001785 TernaryExpression& t = (TernaryExpression&) expr;
1786 SpvId test = this->writeExpression(*t.fTest, out);
1787 SpvId end = this->nextId();
1788 SpvId ifTrueLabel = this->nextId();
1789 SpvId ifFalseLabel = this->nextId();
1790 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
1791 this->writeInstruction(SpvOpBranchConditional, test, ifTrueLabel, ifFalseLabel, out);
1792 this->writeLabel(ifTrueLabel, out);
1793 SpvId ifTrue = this->getLValue(*t.fIfTrue, out)->getPointer();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001794 SkASSERT(ifTrue);
Ethan Nicholasa583b812018-01-18 13:32:11 -05001795 this->writeInstruction(SpvOpBranch, end, out);
1796 ifTrueLabel = fCurrentBlock;
1797 SpvId ifFalse = this->getLValue(*t.fIfFalse, out)->getPointer();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001798 SkASSERT(ifFalse);
Ethan Nicholasa583b812018-01-18 13:32:11 -05001799 ifFalseLabel = fCurrentBlock;
1800 this->writeInstruction(SpvOpBranch, end, out);
1801 SpvId result = this->nextId();
1802 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, ifTrue,
1803 ifTrueLabel, ifFalse, ifFalseLabel, out);
1804 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
Ethan Nicholas30d30222020-09-11 12:27:26 -04001805 *this,
1806 result,
1807 this->getType(type),
1808 precision));
Ethan Nicholasa583b812018-01-18 13:32:11 -05001809 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04001810 default: {
ethannicholasb3058bd2016-07-01 08:22:01 -07001811 // expr isn't actually an lvalue, create a dummy variable for it. This case happens due
Greg Daniel64773e62016-11-22 09:44:03 -05001812 // to the need to store values in temporary variables during function calls (see
ethannicholasb3058bd2016-07-01 08:22:01 -07001813 // comments in getFunctionType); erroneous uses of rvalues as lvalues should have been
1814 // caught by IRGenerator
1815 SpvId result = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001816 SpvId pointerType = this->getPointerType(type, SpvStorageClassFunction);
1817 this->writeInstruction(SpvOpVariable, pointerType, result, SpvStorageClassFunction,
ethannicholasd598f792016-07-25 10:08:54 -07001818 fVariableBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07001819 this->writeInstruction(SpvOpStore, result, this->writeExpression(expr, out), out);
1820 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
Ethan Nicholas30d30222020-09-11 12:27:26 -04001821 *this,
1822 result,
1823 this->getType(type),
1824 precision));
1825 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001826 }
1827}
1828
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001829SpvId SPIRVCodeGenerator::writeVariableReference(const VariableReference& ref, OutputStream& out) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001830 SpvId result = this->nextId();
Brian Osman79457ef2020-09-24 15:01:27 -04001831 auto entry = fVariableMap.find(ref.fVariable);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001832 SkASSERT(entry != fVariableMap.end());
ethannicholasb3058bd2016-07-01 08:22:01 -07001833 SpvId var = entry->second;
Brian Osman79457ef2020-09-24 15:01:27 -04001834 this->writeInstruction(SpvOpLoad, this->getType(ref.fVariable->type()), result, var, out);
1835 this->writePrecisionModifier(ref.fVariable->type(), result);
1836 if (ref.fVariable->fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN &&
Greg Daniela85e4bf2020-06-17 16:32:45 -04001837 (fProgram.fSettings.fFlipY || fProgram.fSettings.fInverseW)) {
1838 // The x component never changes, so just grab it
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001839 SpvId xId = this->nextId();
1840 this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), xId,
1841 result, 0, out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001842
1843 // Calculate the y component which may need to be flipped
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001844 SpvId rawYId = this->nextId();
1845 this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), rawYId,
1846 result, 1, out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001847 SpvId flippedYId = 0;
1848 if (fProgram.fSettings.fFlipY) {
1849 // need to remap to a top-left coordinate system
1850 if (fRTHeightStructId == (SpvId)-1) {
1851 // height variable hasn't been written yet
1852 std::shared_ptr<SymbolTable> st(new SymbolTable(&fErrors));
1853 SkASSERT(fRTHeightFieldIndex == (SpvId)-1);
1854 std::vector<Type::Field> fields;
1855 SkASSERT(fProgram.fSettings.fRTHeightOffset >= 0);
1856 fields.emplace_back(
1857 Modifiers(Layout(0, -1, fProgram.fSettings.fRTHeightOffset, -1, -1, -1, -1,
1858 -1, Layout::Format::kUnspecified,
1859 Layout::kUnspecified_Primitive, 1, -1, "", "",
1860 Layout::kNo_Key, Layout::CType::kDefault),
1861 0),
1862 SKSL_RTHEIGHT_NAME, fContext.fFloat_Type.get());
1863 StringFragment name("sksl_synthetic_uniforms");
1864 Type intfStruct(-1, name, fields);
1865
1866 int binding = fProgram.fSettings.fRTHeightBinding;
1867 int set = fProgram.fSettings.fRTHeightSet;
1868 SkASSERT(binding != -1 && set != -1);
1869
1870 Layout layout(0, -1, -1, binding, -1, set, -1, -1, Layout::Format::kUnspecified,
1871 Layout::kUnspecified_Primitive, -1, -1, "", "", Layout::kNo_Key,
1872 Layout::CType::kDefault);
John Stiles3ae071e2020-08-05 15:29:29 -04001873 const Variable* intfVar = fSynthetics.takeOwnershipOfSymbol(
1874 std::make_unique<Variable>(/*offset=*/-1,
1875 Modifiers(layout, Modifiers::kUniform_Flag),
1876 name,
Ethan Nicholas30d30222020-09-11 12:27:26 -04001877 &intfStruct,
Brian Osman3887a012020-09-30 13:22:27 -04001878 /*builtin=*/false,
John Stiles3ae071e2020-08-05 15:29:29 -04001879 Variable::kGlobal_Storage));
Greg Daniela85e4bf2020-06-17 16:32:45 -04001880 InterfaceBlock intf(-1, intfVar, name, String(""),
1881 std::vector<std::unique_ptr<Expression>>(), st);
Stephen White88574972020-06-23 19:09:29 -04001882
1883 fRTHeightStructId = this->writeInterfaceBlock(intf, false);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001884 fRTHeightFieldIndex = 0;
1885 }
1886 SkASSERT(fRTHeightFieldIndex != (SpvId)-1);
1887
1888 IntLiteral fieldIndex(fContext, -1, fRTHeightFieldIndex);
1889 SpvId fieldIndexId = this->writeIntLiteral(fieldIndex);
1890 SpvId heightPtr = this->nextId();
1891 this->writeOpCode(SpvOpAccessChain, 5, out);
1892 this->writeWord(this->getPointerType(*fContext.fFloat_Type, SpvStorageClassUniform),
1893 out);
1894 this->writeWord(heightPtr, out);
1895 this->writeWord(fRTHeightStructId, out);
1896 this->writeWord(fieldIndexId, out);
1897 SpvId heightRead = this->nextId();
1898 this->writeInstruction(SpvOpLoad, this->getType(*fContext.fFloat_Type), heightRead,
1899 heightPtr, out);
1900
1901 flippedYId = this->nextId();
1902 this->writeInstruction(SpvOpFSub, this->getType(*fContext.fFloat_Type), flippedYId,
1903 heightRead, rawYId, out);
1904 }
1905
1906 // The z component will always be zero so we just get an id to the 0 literal
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001907 FloatLiteral zero(fContext, -1, 0.0);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001908 SpvId zeroId = writeFloatLiteral(zero);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001909
1910 // Calculate the w component which may need to be inverted
1911 SpvId rawWId = this->nextId();
1912 this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), rawWId,
Ethan Nicholas20798e52018-12-04 12:30:50 -05001913 result, 3, out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001914 SpvId invWId = 0;
1915 if (fProgram.fSettings.fInverseW) {
1916 // We need to invert w
1917 FloatLiteral one(fContext, -1, 1.0);
1918 SpvId oneId = writeFloatLiteral(one);
1919 invWId = this->nextId();
1920 this->writeInstruction(SpvOpFDiv, this->getType(*fContext.fFloat_Type), invWId, oneId,
1921 rawWId, out);
1922 }
1923
1924 // Fill in the new fragcoord with the components from above
1925 SpvId adjusted = this->nextId();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001926 this->writeOpCode(SpvOpCompositeConstruct, 7, out);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001927 this->writeWord(this->getType(*fContext.fFloat4_Type), out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001928 this->writeWord(adjusted, out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001929 this->writeWord(xId, out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001930 if (fProgram.fSettings.fFlipY) {
1931 this->writeWord(flippedYId, out);
1932 } else {
1933 this->writeWord(rawYId, out);
1934 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001935 this->writeWord(zeroId, out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001936 if (fProgram.fSettings.fInverseW) {
1937 this->writeWord(invWId, out);
1938 } else {
1939 this->writeWord(rawWId, out);
1940 }
1941
1942 return adjusted;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001943 }
Brian Osman79457ef2020-09-24 15:01:27 -04001944 if (ref.fVariable->fModifiers.fLayout.fBuiltin == SK_CLOCKWISE_BUILTIN &&
Chris Daltonb91c4662018-08-01 10:46:22 -06001945 !fProgram.fSettings.fFlipY) {
1946 // FrontFacing in Vulkan is defined in terms of a top-down render target. In skia, we use
1947 // the default convention of "counter-clockwise face is front".
1948 SpvId inverse = this->nextId();
1949 this->writeInstruction(SpvOpLogicalNot, this->getType(*fContext.fBool_Type), inverse,
1950 result, out);
1951 return inverse;
1952 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001953 return result;
1954}
1955
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001956SpvId SPIRVCodeGenerator::writeIndexExpression(const IndexExpression& expr, OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001957 if (expr.fBase->type().typeKind() == Type::TypeKind::kVector) {
Ethan Nicholasa9a06902019-01-07 14:42:40 -05001958 SpvId base = this->writeExpression(*expr.fBase, out);
1959 SpvId index = this->writeExpression(*expr.fIndex, out);
1960 SpvId result = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04001961 this->writeInstruction(SpvOpVectorExtractDynamic, this->getType(expr.type()), result, base,
Ethan Nicholasa9a06902019-01-07 14:42:40 -05001962 index, out);
1963 return result;
1964 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001965 return getLValue(expr, out)->load(out);
1966}
1967
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001968SpvId SPIRVCodeGenerator::writeFieldAccess(const FieldAccess& f, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001969 return getLValue(f, out)->load(out);
1970}
1971
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001972SpvId SPIRVCodeGenerator::writeSwizzle(const Swizzle& swizzle, OutputStream& out) {
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001973 SpvId base = this->writeExpression(*swizzle.fBase, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001974 SpvId result = this->nextId();
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001975 size_t count = swizzle.fComponents.size();
ethannicholasb3058bd2016-07-01 08:22:01 -07001976 if (count == 1) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04001977 this->writeInstruction(SpvOpCompositeExtract, this->getType(swizzle.type()), result, base,
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001978 swizzle.fComponents[0], out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001979 } else {
1980 this->writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) count, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04001981 this->writeWord(this->getType(swizzle.type()), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001982 this->writeWord(result, out);
1983 this->writeWord(base, out);
Brian Osman25647672020-09-15 15:16:56 -04001984 this->writeWord(base, out);
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001985 for (int component : swizzle.fComponents) {
Brian Osman25647672020-09-15 15:16:56 -04001986 this->writeWord(component, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001987 }
1988 }
1989 return result;
1990}
1991
Greg Daniel64773e62016-11-22 09:44:03 -05001992SpvId SPIRVCodeGenerator::writeBinaryOperation(const Type& resultType,
1993 const Type& operandType, SpvId lhs,
1994 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001995 SpvOp_ ifUInt, SpvOp_ ifBool, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001996 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07001997 if (is_float(fContext, operandType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001998 this->writeInstruction(ifFloat, this->getType(resultType), result, lhs, rhs, out);
ethannicholasd598f792016-07-25 10:08:54 -07001999 } else if (is_signed(fContext, operandType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002000 this->writeInstruction(ifInt, this->getType(resultType), result, lhs, rhs, out);
ethannicholasd598f792016-07-25 10:08:54 -07002001 } else if (is_unsigned(fContext, operandType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002002 this->writeInstruction(ifUInt, this->getType(resultType), result, lhs, rhs, out);
ethannicholasd598f792016-07-25 10:08:54 -07002003 } else if (operandType == *fContext.fBool_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002004 this->writeInstruction(ifBool, this->getType(resultType), result, lhs, rhs, out);
Ethan Nicholas858fecc2019-03-07 13:19:18 -05002005 return result; // skip RelaxedPrecision check
ethannicholasb3058bd2016-07-01 08:22:01 -07002006 } else {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002007#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07002008 ABORT("invalid operandType: %s", operandType.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002009#endif
ethannicholasb3058bd2016-07-01 08:22:01 -07002010 }
Ethan Nicholase77739e2019-03-14 14:04:43 -04002011 if (getActualType(resultType) == operandType && !resultType.highPrecision()) {
Ethan Nicholas858fecc2019-03-07 13:19:18 -05002012 this->writeInstruction(SpvOpDecorate, result, SpvDecorationRelaxedPrecision,
2013 fDecorationBuffer);
2014 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002015 return result;
2016}
2017
Ethan Nicholas48e24052018-03-14 13:51:39 -04002018SpvId SPIRVCodeGenerator::foldToBool(SpvId id, const Type& operandType, SpvOp op,
2019 OutputStream& out) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002020 if (operandType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholasef653b82017-02-21 13:50:00 -05002021 SpvId result = this->nextId();
Ethan Nicholas48e24052018-03-14 13:51:39 -04002022 this->writeInstruction(op, this->getType(*fContext.fBool_Type), result, id, out);
Ethan Nicholasef653b82017-02-21 13:50:00 -05002023 return result;
2024 }
2025 return id;
2026}
2027
Ethan Nicholas68990be2017-07-13 09:36:52 -04002028SpvId SPIRVCodeGenerator::writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs,
2029 SpvOp_ floatOperator, SpvOp_ intOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002030 SpvOp_ vectorMergeOperator, SpvOp_ mergeOperator,
Ethan Nicholas68990be2017-07-13 09:36:52 -04002031 OutputStream& out) {
2032 SpvOp_ compareOp = is_float(fContext, operandType) ? floatOperator : intOperator;
Ethan Nicholase6592142020-09-08 10:22:09 -04002033 SkASSERT(operandType.typeKind() == Type::TypeKind::kMatrix);
Ethan Nicholas0df21132018-07-10 09:37:51 -04002034 SpvId columnType = this->getType(operandType.componentType().toCompound(fContext,
2035 operandType.rows(),
2036 1));
Ethan Nicholas68990be2017-07-13 09:36:52 -04002037 SpvId bvecType = this->getType(fContext.fBool_Type->toCompound(fContext,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002038 operandType.rows(),
Ethan Nicholas68990be2017-07-13 09:36:52 -04002039 1));
2040 SpvId boolType = this->getType(*fContext.fBool_Type);
2041 SpvId result = 0;
Ethan Nicholas0df21132018-07-10 09:37:51 -04002042 for (int i = 0; i < operandType.columns(); i++) {
2043 SpvId columnL = this->nextId();
2044 this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out);
2045 SpvId columnR = this->nextId();
2046 this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002047 SpvId compare = this->nextId();
Ethan Nicholas0df21132018-07-10 09:37:51 -04002048 this->writeInstruction(compareOp, bvecType, compare, columnL, columnR, out);
2049 SpvId merge = this->nextId();
2050 this->writeInstruction(vectorMergeOperator, boolType, merge, compare, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002051 if (result != 0) {
2052 SpvId next = this->nextId();
Ethan Nicholas0df21132018-07-10 09:37:51 -04002053 this->writeInstruction(mergeOperator, boolType, next, result, merge, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002054 result = next;
2055 }
2056 else {
Ethan Nicholas0df21132018-07-10 09:37:51 -04002057 result = merge;
Ethan Nicholas68990be2017-07-13 09:36:52 -04002058 }
2059 }
2060 return result;
2061}
2062
Ethan Nicholas0df21132018-07-10 09:37:51 -04002063SpvId SPIRVCodeGenerator::writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs,
2064 SpvId rhs, SpvOp_ floatOperator,
2065 SpvOp_ intOperator,
2066 OutputStream& out) {
2067 SpvOp_ op = is_float(fContext, operandType) ? floatOperator : intOperator;
Ethan Nicholase6592142020-09-08 10:22:09 -04002068 SkASSERT(operandType.typeKind() == Type::TypeKind::kMatrix);
Ethan Nicholas0df21132018-07-10 09:37:51 -04002069 SpvId columnType = this->getType(operandType.componentType().toCompound(fContext,
2070 operandType.rows(),
2071 1));
2072 SpvId columns[4];
2073 for (int i = 0; i < operandType.columns(); i++) {
2074 SpvId columnL = this->nextId();
2075 this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out);
2076 SpvId columnR = this->nextId();
2077 this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out);
2078 columns[i] = this->nextId();
2079 this->writeInstruction(op, columnType, columns[i], columnL, columnR, out);
2080 }
2081 SpvId result = this->nextId();
2082 this->writeOpCode(SpvOpCompositeConstruct, 3 + operandType.columns(), out);
2083 this->writeWord(this->getType(operandType), out);
2084 this->writeWord(result, out);
2085 for (int i = 0; i < operandType.columns(); i++) {
2086 this->writeWord(columns[i], out);
2087 }
2088 return result;
2089}
2090
Ethan Nicholas49465b42019-04-17 12:22:21 -04002091std::unique_ptr<Expression> create_literal_1(const Context& context, const Type& type) {
2092 if (type.isInteger()) {
2093 return std::unique_ptr<Expression>(new IntLiteral(-1, 1, &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07002094 }
Ethan Nicholas49465b42019-04-17 12:22:21 -04002095 else if (type.isFloat()) {
2096 return std::unique_ptr<Expression>(new FloatLiteral(-1, 1.0, &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07002097 } else {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002098 ABORT("math is unsupported on type '%s'", type.name().c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -07002099 }
Ethan Nicholas49465b42019-04-17 12:22:21 -04002100}
2101
2102SpvId SPIRVCodeGenerator::writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op,
2103 const Type& rightType, SpvId rhs,
2104 const Type& resultType, OutputStream& out) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002105 Type tmp("<invalid>");
Ethan Nicholas48e24052018-03-14 13:51:39 -04002106 // overall type we are operating on: float2, int, uint4...
ethannicholasb3058bd2016-07-01 08:22:01 -07002107 const Type* operandType;
Ethan Nicholas48e24052018-03-14 13:51:39 -04002108 // IR allows mismatched types in expressions (e.g. float2 * float), but they need special
2109 // handling in SPIR-V
Ethan Nicholas49465b42019-04-17 12:22:21 -04002110 if (this->getActualType(leftType) != this->getActualType(rightType)) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002111 if (leftType.typeKind() == Type::TypeKind::kVector && rightType.isNumber()) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002112 if (op == Token::Kind::TK_SLASH) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002113 SpvId one = this->writeExpression(*create_literal_1(fContext, rightType), out);
2114 SpvId inverse = this->nextId();
2115 this->writeInstruction(SpvOpFDiv, this->getType(rightType), inverse, one, rhs, out);
2116 rhs = inverse;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002117 op = Token::Kind::TK_STAR;
Ethan Nicholas49465b42019-04-17 12:22:21 -04002118 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002119 if (op == Token::Kind::TK_STAR) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002120 SpvId result = this->nextId();
2121 this->writeInstruction(SpvOpVectorTimesScalar, this->getType(resultType),
2122 result, lhs, rhs, out);
2123 return result;
2124 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002125 // promote number to vector
2126 SpvId vec = this->nextId();
Ethan Nicholas49465b42019-04-17 12:22:21 -04002127 const Type& vecType = leftType;
Ethan Nicholas48e24052018-03-14 13:51:39 -04002128 this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out);
2129 this->writeWord(this->getType(vecType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002130 this->writeWord(vec, out);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002131 for (int i = 0; i < vecType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002132 this->writeWord(rhs, out);
2133 }
2134 rhs = vec;
Ethan Nicholas49465b42019-04-17 12:22:21 -04002135 operandType = &leftType;
Ethan Nicholase6592142020-09-08 10:22:09 -04002136 } else if (rightType.typeKind() == Type::TypeKind::kVector && leftType.isNumber()) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002137 if (op == Token::Kind::TK_STAR) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002138 SpvId result = this->nextId();
2139 this->writeInstruction(SpvOpVectorTimesScalar, this->getType(resultType),
2140 result, rhs, lhs, out);
2141 return result;
2142 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002143 // promote number to vector
2144 SpvId vec = this->nextId();
Ethan Nicholas49465b42019-04-17 12:22:21 -04002145 const Type& vecType = rightType;
Ethan Nicholas48e24052018-03-14 13:51:39 -04002146 this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out);
2147 this->writeWord(this->getType(vecType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002148 this->writeWord(vec, out);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002149 for (int i = 0; i < vecType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002150 this->writeWord(lhs, out);
2151 }
2152 lhs = vec;
Ethan Nicholas49465b42019-04-17 12:22:21 -04002153 operandType = &rightType;
Ethan Nicholase6592142020-09-08 10:22:09 -04002154 } else if (leftType.typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002155 SpvOp_ spvop;
Ethan Nicholase6592142020-09-08 10:22:09 -04002156 if (rightType.typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002157 spvop = SpvOpMatrixTimesMatrix;
Ethan Nicholase6592142020-09-08 10:22:09 -04002158 } else if (rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002159 spvop = SpvOpMatrixTimesVector;
ethannicholasb3058bd2016-07-01 08:22:01 -07002160 } else {
Ethan Nicholase6592142020-09-08 10:22:09 -04002161 SkASSERT(rightType.typeKind() == Type::TypeKind::kScalar);
Ethan Nicholas49465b42019-04-17 12:22:21 -04002162 spvop = SpvOpMatrixTimesScalar;
ethannicholasb3058bd2016-07-01 08:22:01 -07002163 }
2164 SpvId result = this->nextId();
Ethan Nicholas49465b42019-04-17 12:22:21 -04002165 this->writeInstruction(spvop, this->getType(resultType), result, lhs, rhs, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002166 return result;
Ethan Nicholase6592142020-09-08 10:22:09 -04002167 } else if (rightType.typeKind() == Type::TypeKind::kMatrix) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002168 SpvId result = this->nextId();
Ethan Nicholase6592142020-09-08 10:22:09 -04002169 if (leftType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002170 this->writeInstruction(SpvOpVectorTimesMatrix, this->getType(resultType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002171 lhs, rhs, out);
2172 } else {
Ethan Nicholase6592142020-09-08 10:22:09 -04002173 SkASSERT(leftType.typeKind() == Type::TypeKind::kScalar);
Ethan Nicholas49465b42019-04-17 12:22:21 -04002174 this->writeInstruction(SpvOpMatrixTimesScalar, this->getType(resultType), result,
2175 rhs, lhs, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002176 }
2177 return result;
2178 } else {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002179 SkASSERT(false);
2180 return -1;
ethannicholasb3058bd2016-07-01 08:22:01 -07002181 }
2182 } else {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002183 tmp = this->getActualType(leftType);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002184 operandType = &tmp;
Ethan Nicholas49465b42019-04-17 12:22:21 -04002185 SkASSERT(*operandType == this->getActualType(rightType));
ethannicholasb3058bd2016-07-01 08:22:01 -07002186 }
Ethan Nicholas49465b42019-04-17 12:22:21 -04002187 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002188 case Token::Kind::TK_EQEQ: {
Ethan Nicholase6592142020-09-08 10:22:09 -04002189 if (operandType->typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -04002190 return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdEqual,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002191 SpvOpIEqual, SpvOpAll, SpvOpLogicalAnd, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002192 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002193 SkASSERT(resultType == *fContext.fBool_Type);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002194 const Type* tmpType;
Ethan Nicholase6592142020-09-08 10:22:09 -04002195 if (operandType->typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas48e24052018-03-14 13:51:39 -04002196 tmpType = &fContext.fBool_Type->toCompound(fContext,
2197 operandType->columns(),
2198 operandType->rows());
2199 } else {
2200 tmpType = &resultType;
2201 }
2202 return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs,
Ethan Nicholasef653b82017-02-21 13:50:00 -05002203 SpvOpFOrdEqual, SpvOpIEqual,
2204 SpvOpIEqual, SpvOpLogicalEqual, out),
Ethan Nicholas48e24052018-03-14 13:51:39 -04002205 *operandType, SpvOpAll, out);
Ethan Nicholasef653b82017-02-21 13:50:00 -05002206 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002207 case Token::Kind::TK_NEQ:
Ethan Nicholase6592142020-09-08 10:22:09 -04002208 if (operandType->typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -04002209 return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdNotEqual,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002210 SpvOpINotEqual, SpvOpAny, SpvOpLogicalOr, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002211 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002212 SkASSERT(resultType == *fContext.fBool_Type);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002213 const Type* tmpType;
Ethan Nicholase6592142020-09-08 10:22:09 -04002214 if (operandType->typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas48e24052018-03-14 13:51:39 -04002215 tmpType = &fContext.fBool_Type->toCompound(fContext,
2216 operandType->columns(),
2217 operandType->rows());
2218 } else {
2219 tmpType = &resultType;
2220 }
2221 return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs,
Ethan Nicholasef653b82017-02-21 13:50:00 -05002222 SpvOpFOrdNotEqual, SpvOpINotEqual,
2223 SpvOpINotEqual, SpvOpLogicalNotEqual,
2224 out),
Ethan Nicholas48e24052018-03-14 13:51:39 -04002225 *operandType, SpvOpAny, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002226 case Token::Kind::TK_GT:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002227 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002228 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2229 SpvOpFOrdGreaterThan, SpvOpSGreaterThan,
ethannicholasb3058bd2016-07-01 08:22:01 -07002230 SpvOpUGreaterThan, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002231 case Token::Kind::TK_LT:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002232 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002233 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFOrdLessThan,
ethannicholasb3058bd2016-07-01 08:22:01 -07002234 SpvOpSLessThan, SpvOpULessThan, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002235 case Token::Kind::TK_GTEQ:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002236 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002237 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2238 SpvOpFOrdGreaterThanEqual, SpvOpSGreaterThanEqual,
ethannicholasb3058bd2016-07-01 08:22:01 -07002239 SpvOpUGreaterThanEqual, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002240 case Token::Kind::TK_LTEQ:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002241 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002242 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2243 SpvOpFOrdLessThanEqual, SpvOpSLessThanEqual,
ethannicholasb3058bd2016-07-01 08:22:01 -07002244 SpvOpULessThanEqual, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002245 case Token::Kind::TK_PLUS:
Ethan Nicholase6592142020-09-08 10:22:09 -04002246 if (leftType.typeKind() == Type::TypeKind::kMatrix &&
2247 rightType.typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002248 SkASSERT(leftType == rightType);
2249 return this->writeComponentwiseMatrixBinary(leftType, lhs, rhs,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002250 SpvOpFAdd, SpvOpIAdd, out);
2251 }
Greg Daniel64773e62016-11-22 09:44:03 -05002252 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFAdd,
ethannicholasb3058bd2016-07-01 08:22:01 -07002253 SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002254 case Token::Kind::TK_MINUS:
Ethan Nicholase6592142020-09-08 10:22:09 -04002255 if (leftType.typeKind() == Type::TypeKind::kMatrix &&
2256 rightType.typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002257 SkASSERT(leftType == rightType);
2258 return this->writeComponentwiseMatrixBinary(leftType, lhs, rhs,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002259 SpvOpFSub, SpvOpISub, out);
2260 }
Greg Daniel64773e62016-11-22 09:44:03 -05002261 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFSub,
ethannicholasb3058bd2016-07-01 08:22:01 -07002262 SpvOpISub, SpvOpISub, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002263 case Token::Kind::TK_STAR:
Ethan Nicholase6592142020-09-08 10:22:09 -04002264 if (leftType.typeKind() == Type::TypeKind::kMatrix &&
2265 rightType.typeKind() == Type::TypeKind::kMatrix) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002266 // matrix multiply
2267 SpvId result = this->nextId();
2268 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(resultType), result,
2269 lhs, rhs, out);
2270 return result;
2271 }
Greg Daniel64773e62016-11-22 09:44:03 -05002272 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMul,
ethannicholasb3058bd2016-07-01 08:22:01 -07002273 SpvOpIMul, SpvOpIMul, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002274 case Token::Kind::TK_SLASH:
Greg Daniel64773e62016-11-22 09:44:03 -05002275 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFDiv,
ethannicholasb3058bd2016-07-01 08:22:01 -07002276 SpvOpSDiv, SpvOpUDiv, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002277 case Token::Kind::TK_PERCENT:
Ethan Nicholasb3d0f7c2017-05-17 13:13:21 -04002278 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMod,
2279 SpvOpSMod, SpvOpUMod, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002280 case Token::Kind::TK_SHL:
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002281 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2282 SpvOpShiftLeftLogical, SpvOpShiftLeftLogical,
2283 SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002284 case Token::Kind::TK_SHR:
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002285 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2286 SpvOpShiftRightArithmetic, SpvOpShiftRightLogical,
2287 SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002288 case Token::Kind::TK_BITWISEAND:
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002289 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2290 SpvOpBitwiseAnd, SpvOpBitwiseAnd, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002291 case Token::Kind::TK_BITWISEOR:
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002292 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2293 SpvOpBitwiseOr, SpvOpBitwiseOr, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002294 case Token::Kind::TK_BITWISEXOR:
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002295 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2296 SpvOpBitwiseXor, SpvOpBitwiseXor, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002297 case Token::Kind::TK_COMMA:
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04002298 return rhs;
ethannicholasb3058bd2016-07-01 08:22:01 -07002299 default:
Ethan Nicholas49465b42019-04-17 12:22:21 -04002300 SkASSERT(false);
2301 return -1;
ethannicholasb3058bd2016-07-01 08:22:01 -07002302 }
2303}
2304
Ethan Nicholas49465b42019-04-17 12:22:21 -04002305SpvId SPIRVCodeGenerator::writeBinaryExpression(const BinaryExpression& b, OutputStream& out) {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002306 const Expression& left = b.left();
2307 const Expression& right = b.right();
2308 Token::Kind op = b.getOperator();
Ethan Nicholas49465b42019-04-17 12:22:21 -04002309 // handle cases where we don't necessarily evaluate both LHS and RHS
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002310 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002311 case Token::Kind::TK_EQ: {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002312 SpvId rhs = this->writeExpression(right, out);
2313 this->getLValue(left, out)->store(rhs, out);
Ethan Nicholas49465b42019-04-17 12:22:21 -04002314 return rhs;
2315 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002316 case Token::Kind::TK_LOGICALAND:
Ethan Nicholas49465b42019-04-17 12:22:21 -04002317 return this->writeLogicalAnd(b, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002318 case Token::Kind::TK_LOGICALOR:
Ethan Nicholas49465b42019-04-17 12:22:21 -04002319 return this->writeLogicalOr(b, out);
2320 default:
2321 break;
2322 }
2323
2324 std::unique_ptr<LValue> lvalue;
2325 SpvId lhs;
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002326 if (Compiler::IsAssignment(op)) {
2327 lvalue = this->getLValue(left, out);
Ethan Nicholas49465b42019-04-17 12:22:21 -04002328 lhs = lvalue->load(out);
2329 } else {
2330 lvalue = nullptr;
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002331 lhs = this->writeExpression(left, out);
Ethan Nicholas49465b42019-04-17 12:22:21 -04002332 }
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002333 SpvId rhs = this->writeExpression(right, out);
2334 SpvId result = this->writeBinaryExpression(left.type(), lhs, Compiler::RemoveAssignment(op),
2335 right.type(), rhs, b.type(), out);
Ethan Nicholas49465b42019-04-17 12:22:21 -04002336 if (lvalue) {
2337 lvalue->store(result, out);
2338 }
2339 return result;
2340}
2341
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002342SpvId SPIRVCodeGenerator::writeLogicalAnd(const BinaryExpression& a, OutputStream& out) {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002343 SkASSERT(a.getOperator() == Token::Kind::TK_LOGICALAND);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002344 BoolLiteral falseLiteral(fContext, -1, false);
ethannicholasb3058bd2016-07-01 08:22:01 -07002345 SpvId falseConstant = this->writeBoolLiteral(falseLiteral);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002346 SpvId lhs = this->writeExpression(a.left(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002347 SpvId rhsLabel = this->nextId();
2348 SpvId end = this->nextId();
2349 SpvId lhsBlock = fCurrentBlock;
2350 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2351 this->writeInstruction(SpvOpBranchConditional, lhs, rhsLabel, end, out);
2352 this->writeLabel(rhsLabel, out);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002353 SpvId rhs = this->writeExpression(a.right(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002354 SpvId rhsBlock = fCurrentBlock;
2355 this->writeInstruction(SpvOpBranch, end, out);
2356 this->writeLabel(end, out);
2357 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002358 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, falseConstant,
ethannicholasd598f792016-07-25 10:08:54 -07002359 lhsBlock, rhs, rhsBlock, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002360 return result;
2361}
2362
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002363SpvId SPIRVCodeGenerator::writeLogicalOr(const BinaryExpression& o, OutputStream& out) {
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002364 SkASSERT(o.getOperator() == Token::Kind::TK_LOGICALOR);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002365 BoolLiteral trueLiteral(fContext, -1, true);
ethannicholasb3058bd2016-07-01 08:22:01 -07002366 SpvId trueConstant = this->writeBoolLiteral(trueLiteral);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002367 SpvId lhs = this->writeExpression(o.left(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002368 SpvId rhsLabel = this->nextId();
2369 SpvId end = this->nextId();
2370 SpvId lhsBlock = fCurrentBlock;
2371 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2372 this->writeInstruction(SpvOpBranchConditional, lhs, end, rhsLabel, out);
2373 this->writeLabel(rhsLabel, out);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -04002374 SpvId rhs = this->writeExpression(o.right(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002375 SpvId rhsBlock = fCurrentBlock;
2376 this->writeInstruction(SpvOpBranch, end, out);
2377 this->writeLabel(end, out);
2378 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002379 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, trueConstant,
ethannicholasd598f792016-07-25 10:08:54 -07002380 lhsBlock, rhs, rhsBlock, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002381 return result;
2382}
2383
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002384SpvId SPIRVCodeGenerator::writeTernaryExpression(const TernaryExpression& t, OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002385 const Type& type = t.type();
ethannicholasb3058bd2016-07-01 08:22:01 -07002386 SpvId test = this->writeExpression(*t.fTest, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04002387 if (t.fIfTrue->type().columns() == 1 &&
Brian Osmanb6b95732020-06-30 11:44:27 -04002388 t.fIfTrue->isCompileTimeConstant() &&
2389 t.fIfFalse->isCompileTimeConstant()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002390 // both true and false are constants, can just use OpSelect
2391 SpvId result = this->nextId();
2392 SpvId trueId = this->writeExpression(*t.fIfTrue, out);
2393 SpvId falseId = this->writeExpression(*t.fIfFalse, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04002394 this->writeInstruction(SpvOpSelect, this->getType(type), result, test, trueId, falseId,
ethannicholasb3058bd2016-07-01 08:22:01 -07002395 out);
2396 return result;
2397 }
Greg Daniel64773e62016-11-22 09:44:03 -05002398 // was originally using OpPhi to choose the result, but for some reason that is crashing on
ethannicholasb3058bd2016-07-01 08:22:01 -07002399 // Adreno. Switched to storing the result in a temp variable as glslang does.
2400 SpvId var = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04002401 this->writeInstruction(SpvOpVariable, this->getPointerType(type, SpvStorageClassFunction),
ethannicholasd598f792016-07-25 10:08:54 -07002402 var, SpvStorageClassFunction, fVariableBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002403 SpvId trueLabel = this->nextId();
2404 SpvId falseLabel = this->nextId();
2405 SpvId end = this->nextId();
2406 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2407 this->writeInstruction(SpvOpBranchConditional, test, trueLabel, falseLabel, out);
2408 this->writeLabel(trueLabel, out);
2409 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfTrue, out), out);
2410 this->writeInstruction(SpvOpBranch, end, out);
2411 this->writeLabel(falseLabel, out);
2412 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfFalse, out), out);
2413 this->writeInstruction(SpvOpBranch, end, out);
2414 this->writeLabel(end, out);
2415 SpvId result = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04002416 this->writeInstruction(SpvOpLoad, this->getType(type), result, var, out);
2417 this->writePrecisionModifier(type, result);
ethannicholasb3058bd2016-07-01 08:22:01 -07002418 return result;
2419}
2420
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002421SpvId SPIRVCodeGenerator::writePrefixExpression(const PrefixExpression& p, OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002422 const Type& type = p.type();
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002423 if (p.fOperator == Token::Kind::TK_MINUS) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002424 SpvId result = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04002425 SpvId typeId = this->getType(type);
ethannicholasb3058bd2016-07-01 08:22:01 -07002426 SpvId expr = this->writeExpression(*p.fOperand, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04002427 if (is_float(fContext, type)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002428 this->writeInstruction(SpvOpFNegate, typeId, result, expr, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04002429 } else if (is_signed(fContext, type)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002430 this->writeInstruction(SpvOpSNegate, typeId, result, expr, out);
2431 } else {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002432#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07002433 ABORT("unsupported prefix expression %s", p.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002434#endif
Brian Salomon23356442018-11-30 15:33:19 -05002435 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002436 this->writePrecisionModifier(type, result);
ethannicholasb3058bd2016-07-01 08:22:01 -07002437 return result;
2438 }
2439 switch (p.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002440 case Token::Kind::TK_PLUS:
ethannicholasb3058bd2016-07-01 08:22:01 -07002441 return this->writeExpression(*p.fOperand, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002442 case Token::Kind::TK_PLUSPLUS: {
ethannicholasb3058bd2016-07-01 08:22:01 -07002443 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04002444 SpvId one = this->writeExpression(*create_literal_1(fContext, type), out);
2445 SpvId result = this->writeBinaryOperation(type, type, lv->load(out), one,
Greg Daniel64773e62016-11-22 09:44:03 -05002446 SpvOpFAdd, SpvOpIAdd, SpvOpIAdd, SpvOpUndef,
ethannicholasb3058bd2016-07-01 08:22:01 -07002447 out);
2448 lv->store(result, out);
2449 return result;
2450 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002451 case Token::Kind::TK_MINUSMINUS: {
ethannicholasb3058bd2016-07-01 08:22:01 -07002452 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04002453 SpvId one = this->writeExpression(*create_literal_1(fContext, type), out);
2454 SpvId result = this->writeBinaryOperation(type, type, lv->load(out), one, SpvOpFSub,
2455 SpvOpISub, SpvOpISub, SpvOpUndef, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002456 lv->store(result, out);
2457 return result;
2458 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002459 case Token::Kind::TK_LOGICALNOT: {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002460 SkASSERT(p.fOperand->type() == *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07002461 SpvId result = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04002462 this->writeInstruction(SpvOpLogicalNot, this->getType(p.fOperand->type()), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002463 this->writeExpression(*p.fOperand, out), out);
2464 return result;
2465 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002466 case Token::Kind::TK_BITWISENOT: {
ethannicholas5961bc92016-10-12 06:39:56 -07002467 SpvId result = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04002468 this->writeInstruction(SpvOpNot, this->getType(p.fOperand->type()), result,
ethannicholas5961bc92016-10-12 06:39:56 -07002469 this->writeExpression(*p.fOperand, out), out);
2470 return result;
2471 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002472 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002473#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07002474 ABORT("unsupported prefix expression: %s", p.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002475#endif
2476 return -1;
ethannicholasb3058bd2016-07-01 08:22:01 -07002477 }
2478}
2479
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002480SpvId SPIRVCodeGenerator::writePostfixExpression(const PostfixExpression& p, OutputStream& out) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002481 const Type& type = p.type();
ethannicholasb3058bd2016-07-01 08:22:01 -07002482 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
2483 SpvId result = lv->load(out);
Ethan Nicholas30d30222020-09-11 12:27:26 -04002484 SpvId one = this->writeExpression(*create_literal_1(fContext, type), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002485 switch (p.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002486 case Token::Kind::TK_PLUSPLUS: {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002487 SpvId temp = this->writeBinaryOperation(type, type, result, one, SpvOpFAdd,
ethannicholasb3058bd2016-07-01 08:22:01 -07002488 SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
2489 lv->store(temp, out);
2490 return result;
2491 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002492 case Token::Kind::TK_MINUSMINUS: {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002493 SpvId temp = this->writeBinaryOperation(type, type, result, one, SpvOpFSub,
ethannicholasb3058bd2016-07-01 08:22:01 -07002494 SpvOpISub, SpvOpISub, SpvOpUndef, out);
2495 lv->store(temp, out);
2496 return result;
2497 }
2498 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002499#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07002500 ABORT("unsupported postfix expression %s", p.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002501#endif
2502 return -1;
ethannicholasb3058bd2016-07-01 08:22:01 -07002503 }
2504}
2505
ethannicholasf789b382016-08-03 12:43:36 -07002506SpvId SPIRVCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
Ethan Nicholas59d660c2020-09-28 09:18:15 -04002507 if (b.value()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002508 if (fBoolTrue == 0) {
2509 fBoolTrue = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04002510 this->writeInstruction(SpvOpConstantTrue, this->getType(b.type()), fBoolTrue,
ethannicholasb3058bd2016-07-01 08:22:01 -07002511 fConstantBuffer);
2512 }
2513 return fBoolTrue;
2514 } else {
2515 if (fBoolFalse == 0) {
2516 fBoolFalse = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04002517 this->writeInstruction(SpvOpConstantFalse, this->getType(b.type()), fBoolFalse,
ethannicholasb3058bd2016-07-01 08:22:01 -07002518 fConstantBuffer);
2519 }
2520 return fBoolFalse;
2521 }
2522}
2523
ethannicholasf789b382016-08-03 12:43:36 -07002524SpvId SPIRVCodeGenerator::writeIntLiteral(const IntLiteral& i) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002525 const Type& type = i.type();
2526 ConstantType constantType;
2527 if (type == *fContext.fInt_Type) {
2528 constantType = ConstantType::kInt;
2529 } else if (type == *fContext.fUInt_Type) {
2530 constantType = ConstantType::kUInt;
2531 } else if (type == *fContext.fShort_Type || type == *fContext.fByte_Type) {
2532 constantType = ConstantType::kShort;
2533 } else if (type == *fContext.fUShort_Type || type == *fContext.fUByte_Type) {
2534 constantType = ConstantType::kUShort;
Ethan Nicholasc2d84bf2019-09-09 10:49:15 -04002535 } else {
2536 SkASSERT(false);
ethannicholasb3058bd2016-07-01 08:22:01 -07002537 }
Ethan Nicholase96cdd12020-09-28 16:27:18 -04002538 std::pair<ConstantValue, ConstantType> key(i.value(), constantType);
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04002539 auto entry = fNumberConstants.find(key);
2540 if (entry == fNumberConstants.end()) {
2541 SpvId result = this->nextId();
Ethan Nicholase96cdd12020-09-28 16:27:18 -04002542 this->writeInstruction(SpvOpConstant, this->getType(type), result, (SpvId) i.value(),
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04002543 fConstantBuffer);
2544 fNumberConstants[key] = result;
2545 return result;
2546 }
2547 return entry->second;
ethannicholasb3058bd2016-07-01 08:22:01 -07002548}
2549
ethannicholasf789b382016-08-03 12:43:36 -07002550SpvId SPIRVCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002551 const Type& type = f.type();
2552 ConstantType constantType;
2553 if (type == *fContext.fHalf_Type) {
2554 constantType = ConstantType::kHalf;
ethannicholasb3058bd2016-07-01 08:22:01 -07002555 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002556 constantType = ConstantType::kFloat;
ethannicholasb3058bd2016-07-01 08:22:01 -07002557 }
Ethan Nicholas135e2372020-09-29 10:21:29 -04002558 float value = (float) f.value();
2559 std::pair<ConstantValue, ConstantType> key(f.value(), constantType);
John Stiles8c578662020-06-01 15:32:47 +00002560 auto entry = fNumberConstants.find(key);
2561 if (entry == fNumberConstants.end()) {
2562 SpvId result = this->nextId();
2563 uint32_t bits;
2564 SkASSERT(sizeof(bits) == sizeof(value));
2565 memcpy(&bits, &value, sizeof(bits));
Ethan Nicholas30d30222020-09-11 12:27:26 -04002566 this->writeInstruction(SpvOpConstant, this->getType(type), result, bits,
John Stiles8c578662020-06-01 15:32:47 +00002567 fConstantBuffer);
2568 fNumberConstants[key] = result;
2569 return result;
2570 }
2571 return entry->second;
ethannicholasb3058bd2016-07-01 08:22:01 -07002572}
2573
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002574SpvId SPIRVCodeGenerator::writeFunctionStart(const FunctionDeclaration& f, OutputStream& out) {
ethannicholasd598f792016-07-25 10:08:54 -07002575 SpvId result = fFunctionMap[&f];
Greg Daniel64773e62016-11-22 09:44:03 -05002576 this->writeInstruction(SpvOpFunction, this->getType(f.fReturnType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002577 SpvFunctionControlMaskNone, this->getFunctionType(f), out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002578 this->writeInstruction(SpvOpName, result, f.fName, fNameBuffer);
ethannicholasd598f792016-07-25 10:08:54 -07002579 for (size_t i = 0; i < f.fParameters.size(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002580 SpvId id = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002581 fVariableMap[f.fParameters[i]] = id;
ethannicholasb3058bd2016-07-01 08:22:01 -07002582 SpvId type;
Ethan Nicholas30d30222020-09-11 12:27:26 -04002583 type = this->getPointerType(f.fParameters[i]->type(), SpvStorageClassFunction);
ethannicholasb3058bd2016-07-01 08:22:01 -07002584 this->writeInstruction(SpvOpFunctionParameter, type, id, out);
2585 }
2586 return result;
2587}
2588
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002589SpvId SPIRVCodeGenerator::writeFunction(const FunctionDefinition& f, OutputStream& out) {
2590 fVariableBuffer.reset();
ethannicholasb3058bd2016-07-01 08:22:01 -07002591 SpvId result = this->writeFunctionStart(f.fDeclaration, out);
2592 this->writeLabel(this->nextId(), out);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002593 StringStream bodyBuffer;
Ethan Nicholascb670962017-04-20 19:31:52 -04002594 this->writeBlock((Block&) *f.fBody, bodyBuffer);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002595 write_stringstream(fVariableBuffer, out);
Ethan Nicholas8eb64d32018-12-21 14:50:42 -05002596 if (f.fDeclaration.fName == "main") {
2597 write_stringstream(fGlobalInitializersBuffer, out);
2598 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002599 write_stringstream(bodyBuffer, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002600 if (fCurrentBlock) {
Ethan Nicholas70a44b22017-11-30 09:09:16 -05002601 if (f.fDeclaration.fReturnType == *fContext.fVoid_Type) {
2602 this->writeInstruction(SpvOpReturn, out);
2603 } else {
2604 this->writeInstruction(SpvOpUnreachable, out);
2605 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002606 }
2607 this->writeInstruction(SpvOpFunctionEnd, out);
2608 return result;
2609}
2610
2611void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target) {
2612 if (layout.fLocation >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002613 this->writeInstruction(SpvOpDecorate, target, SpvDecorationLocation, layout.fLocation,
ethannicholasb3058bd2016-07-01 08:22:01 -07002614 fDecorationBuffer);
2615 }
2616 if (layout.fBinding >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002617 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBinding, layout.fBinding,
ethannicholasb3058bd2016-07-01 08:22:01 -07002618 fDecorationBuffer);
2619 }
2620 if (layout.fIndex >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002621 this->writeInstruction(SpvOpDecorate, target, SpvDecorationIndex, layout.fIndex,
ethannicholasb3058bd2016-07-01 08:22:01 -07002622 fDecorationBuffer);
2623 }
2624 if (layout.fSet >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002625 this->writeInstruction(SpvOpDecorate, target, SpvDecorationDescriptorSet, layout.fSet,
ethannicholasb3058bd2016-07-01 08:22:01 -07002626 fDecorationBuffer);
2627 }
Greg Daniel64773e62016-11-22 09:44:03 -05002628 if (layout.fInputAttachmentIndex >= 0) {
2629 this->writeInstruction(SpvOpDecorate, target, SpvDecorationInputAttachmentIndex,
2630 layout.fInputAttachmentIndex, fDecorationBuffer);
Ethan Nicholasbe1099f2018-01-11 16:09:05 -05002631 fCapabilities |= (((uint64_t) 1) << SpvCapabilityInputAttachment);
Greg Daniel64773e62016-11-22 09:44:03 -05002632 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04002633 if (layout.fBuiltin >= 0 && layout.fBuiltin != SK_FRAGCOLOR_BUILTIN &&
Greg Daniele6ab9982018-08-22 13:56:32 +00002634 layout.fBuiltin != SK_IN_BUILTIN && layout.fBuiltin != SK_OUT_BUILTIN) {
Greg Daniel64773e62016-11-22 09:44:03 -05002635 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBuiltIn, layout.fBuiltin,
ethannicholasb3058bd2016-07-01 08:22:01 -07002636 fDecorationBuffer);
2637 }
2638}
2639
2640void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target, int member) {
2641 if (layout.fLocation >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002642 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationLocation,
ethannicholasb3058bd2016-07-01 08:22:01 -07002643 layout.fLocation, fDecorationBuffer);
2644 }
2645 if (layout.fBinding >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002646 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBinding,
ethannicholasb3058bd2016-07-01 08:22:01 -07002647 layout.fBinding, fDecorationBuffer);
2648 }
2649 if (layout.fIndex >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002650 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationIndex,
ethannicholasb3058bd2016-07-01 08:22:01 -07002651 layout.fIndex, fDecorationBuffer);
2652 }
2653 if (layout.fSet >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002654 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationDescriptorSet,
ethannicholasb3058bd2016-07-01 08:22:01 -07002655 layout.fSet, fDecorationBuffer);
2656 }
Greg Daniel64773e62016-11-22 09:44:03 -05002657 if (layout.fInputAttachmentIndex >= 0) {
2658 this->writeInstruction(SpvOpDecorate, target, member, SpvDecorationInputAttachmentIndex,
2659 layout.fInputAttachmentIndex, fDecorationBuffer);
2660 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002661 if (layout.fBuiltin >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002662 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBuiltIn,
ethannicholasb3058bd2016-07-01 08:22:01 -07002663 layout.fBuiltin, fDecorationBuffer);
2664 }
2665}
2666
Ethan Nicholas81d15112018-07-13 12:48:50 -04002667static void update_sk_in_count(const Modifiers& m, int* outSkInCount) {
2668 switch (m.fLayout.fPrimitive) {
2669 case Layout::kPoints_Primitive:
2670 *outSkInCount = 1;
2671 break;
2672 case Layout::kLines_Primitive:
2673 *outSkInCount = 2;
2674 break;
2675 case Layout::kLinesAdjacency_Primitive:
2676 *outSkInCount = 4;
2677 break;
2678 case Layout::kTriangles_Primitive:
2679 *outSkInCount = 3;
2680 break;
2681 case Layout::kTrianglesAdjacency_Primitive:
2682 *outSkInCount = 6;
2683 break;
2684 default:
2685 return;
2686 }
2687}
2688
Stephen White88574972020-06-23 19:09:29 -04002689SpvId SPIRVCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight) {
Ethan Nicholas361941e2017-05-30 15:41:07 -04002690 bool isBuffer = (0 != (intf.fVariable.fModifiers.fFlags & Modifiers::kBuffer_Flag));
Ethan Nicholas39204fd2017-11-27 13:12:30 -05002691 bool pushConstant = (0 != (intf.fVariable.fModifiers.fLayout.fFlags &
2692 Layout::kPushConstant_Flag));
Ethan Nicholas8d2ba442018-03-16 16:40:36 -04002693 MemoryLayout memoryLayout = (pushConstant || isBuffer) ?
2694 MemoryLayout(MemoryLayout::k430_Standard) :
2695 fDefaultLayout;
ethannicholasb3058bd2016-07-01 08:22:01 -07002696 SpvId result = this->nextId();
Ethan Nicholas30d30222020-09-11 12:27:26 -04002697 const Type* type = &intf.fVariable.type();
Stephen White88574972020-06-23 19:09:29 -04002698 if (fProgram.fInputs.fRTHeight && appendRTHeight) {
Greg Daniele6ab9982018-08-22 13:56:32 +00002699 SkASSERT(fRTHeightStructId == (SpvId) -1);
2700 SkASSERT(fRTHeightFieldIndex == (SpvId) -1);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002701 std::vector<Type::Field> fields = type->fields();
Greg Daniele6ab9982018-08-22 13:56:32 +00002702 fRTHeightStructId = result;
2703 fRTHeightFieldIndex = fields.size();
2704 fields.emplace_back(Modifiers(), StringFragment(SKSL_RTHEIGHT_NAME), fContext.fFloat_Type.get());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002705 type = new Type(type->fOffset, type->name(), fields);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002706 }
Ethan Nicholas5226b772018-05-03 16:20:41 -04002707 SpvId typeId;
2708 if (intf.fVariable.fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) {
2709 for (const auto& e : fProgram) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002710 if (e.kind() == ProgramElement::Kind::kModifiers) {
Ethan Nicholas5226b772018-05-03 16:20:41 -04002711 const Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholas81d15112018-07-13 12:48:50 -04002712 update_sk_in_count(m, &fSkInCount);
Ethan Nicholas5226b772018-05-03 16:20:41 -04002713 }
2714 }
Ethan Nicholase6592142020-09-08 10:22:09 -04002715 typeId = this->getType(Type("sk_in", Type::TypeKind::kArray,
Ethan Nicholas30d30222020-09-11 12:27:26 -04002716 intf.fVariable.type().componentType(),
Ethan Nicholase6592142020-09-08 10:22:09 -04002717 fSkInCount),
2718 memoryLayout);
Ethan Nicholas5226b772018-05-03 16:20:41 -04002719 } else {
2720 typeId = this->getType(*type, memoryLayout);
2721 }
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04002722 if (intf.fVariable.fModifiers.fFlags & Modifiers::kBuffer_Flag) {
2723 this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBufferBlock, fDecorationBuffer);
Ethan Nicholas6ac8d362019-01-22 21:43:55 +00002724 } else if (intf.fVariable.fModifiers.fLayout.fBuiltin == -1) {
2725 this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBlock, fDecorationBuffer);
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04002726 }
ethannicholasd598f792016-07-25 10:08:54 -07002727 SpvStorageClass_ storageClass = get_storage_class(intf.fVariable.fModifiers);
ethannicholasb3058bd2016-07-01 08:22:01 -07002728 SpvId ptrType = this->nextId();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002729 this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, typeId, fConstantBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002730 this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConstantBuffer);
Ethan Nicholas8d2ba442018-03-16 16:40:36 -04002731 Layout layout = intf.fVariable.fModifiers.fLayout;
2732 if (intf.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag && layout.fSet == -1) {
2733 layout.fSet = 0;
2734 }
2735 this->writeLayout(layout, result);
ethannicholasd598f792016-07-25 10:08:54 -07002736 fVariableMap[&intf.fVariable] = result;
Stephen White88574972020-06-23 19:09:29 -04002737 if (fProgram.fInputs.fRTHeight && appendRTHeight) {
Ethan Nicholas39b101b2017-03-01 12:07:28 -05002738 delete type;
2739 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002740 return result;
2741}
2742
Ethan Nicholas858fecc2019-03-07 13:19:18 -05002743void SPIRVCodeGenerator::writePrecisionModifier(const Type& type, SpvId id) {
Ethan Nicholas10e93b62019-03-20 10:46:14 -04002744 this->writePrecisionModifier(type.highPrecision() ? Precision::kHigh : Precision::kLow, id);
2745}
2746
2747void SPIRVCodeGenerator::writePrecisionModifier(Precision precision, SpvId id) {
2748 if (precision == Precision::kLow) {
Ethan Nicholasa51d7132017-06-09 10:47:31 -04002749 this->writeInstruction(SpvOpDecorate, id, SpvDecorationRelaxedPrecision, fDecorationBuffer);
2750 }
2751}
2752
Chris Dalton2284aab2019-11-15 11:02:24 -07002753bool is_dead(const Variable& var) {
2754 if (var.fReadCount || var.fWriteCount) {
2755 return false;
2756 }
2757 // not entirely sure what the rules are for when it's safe to elide interface variables, but it
2758 // causes various problems to elide some of them even when dead. But it also causes problems
2759 // *not* to elide sk_SampleMask when it's not being used.
2760 if (!(var.fModifiers.fFlags & (Modifiers::kIn_Flag |
2761 Modifiers::kOut_Flag |
2762 Modifiers::kUniform_Flag |
2763 Modifiers::kBuffer_Flag))) {
2764 return true;
2765 }
2766 return var.fModifiers.fLayout.fBuiltin == SK_SAMPLEMASK_BUILTIN;
2767}
2768
ethannicholas5961bc92016-10-12 06:39:56 -07002769#define BUILTIN_IGNORE 9999
Greg Daniel64773e62016-11-22 09:44:03 -05002770void SPIRVCodeGenerator::writeGlobalVars(Program::Kind kind, const VarDeclarations& decl,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002771 OutputStream& out) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002772 for (size_t i = 0; i < decl.fVars.size(); i++) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002773 if (decl.fVars[i]->kind() == Statement::Kind::kNop) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002774 continue;
2775 }
2776 const VarDeclaration& varDecl = (VarDeclaration&) *decl.fVars[i];
2777 const Variable* var = varDecl.fVar;
Brian Salomonf9f45122016-11-29 11:59:17 -05002778 // These haven't been implemented in our SPIR-V generator yet and we only currently use them
2779 // in the OpenGL backend.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002780 SkASSERT(!(var->fModifiers.fFlags & (Modifiers::kReadOnly_Flag |
Chris Daltonb0fd4b12019-10-29 13:41:22 -06002781 Modifiers::kWriteOnly_Flag |
2782 Modifiers::kCoherent_Flag |
2783 Modifiers::kVolatile_Flag |
2784 Modifiers::kRestrict_Flag)));
ethannicholas5961bc92016-10-12 06:39:56 -07002785 if (var->fModifiers.fLayout.fBuiltin == BUILTIN_IGNORE) {
2786 continue;
2787 }
2788 if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN &&
2789 kind != Program::kFragment_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002790 SkASSERT(!fProgram.fSettings.fFragColorIsInOut);
ethannicholas5961bc92016-10-12 06:39:56 -07002791 continue;
2792 }
Chris Dalton2284aab2019-11-15 11:02:24 -07002793 if (is_dead(*var)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002794 continue;
2795 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002796 const Type& type = var->type();
ethannicholasb3058bd2016-07-01 08:22:01 -07002797 SpvStorageClass_ storageClass;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002798 if (var->fModifiers.fFlags & Modifiers::kIn_Flag) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002799 storageClass = SpvStorageClassInput;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002800 } else if (var->fModifiers.fFlags & Modifiers::kOut_Flag) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002801 storageClass = SpvStorageClassOutput;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002802 } else if (var->fModifiers.fFlags & Modifiers::kUniform_Flag) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002803 if (type.typeKind() == Type::TypeKind::kSampler ||
2804 type.typeKind() == Type::TypeKind::kSeparateSampler ||
2805 type.typeKind() == Type::TypeKind::kTexture) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002806 storageClass = SpvStorageClassUniformConstant;
2807 } else {
2808 storageClass = SpvStorageClassUniform;
2809 }
2810 } else {
2811 storageClass = SpvStorageClassPrivate;
2812 }
2813 SpvId id = this->nextId();
ethannicholas14fe8cc2016-09-07 13:37:16 -07002814 fVariableMap[var] = id;
Ethan Nicholas30d30222020-09-11 12:27:26 -04002815 SpvId typeId;
Ethan Nicholas5226b772018-05-03 16:20:41 -04002816 if (var->fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002817 typeId = this->getPointerType(Type("sk_in", Type::TypeKind::kArray,
2818 type.componentType(), fSkInCount),
Ethan Nicholas5226b772018-05-03 16:20:41 -04002819 storageClass);
2820 } else {
Ethan Nicholas30d30222020-09-11 12:27:26 -04002821 typeId = this->getPointerType(type, storageClass);
Ethan Nicholas5226b772018-05-03 16:20:41 -04002822 }
Ethan Nicholas30d30222020-09-11 12:27:26 -04002823 this->writeInstruction(SpvOpVariable, typeId, id, storageClass, fConstantBuffer);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002824 this->writeInstruction(SpvOpName, id, var->fName, fNameBuffer);
Ethan Nicholas30d30222020-09-11 12:27:26 -04002825 this->writePrecisionModifier(type, id);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002826 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002827 SkASSERT(!fCurrentBlock);
ethannicholasf789b382016-08-03 12:43:36 -07002828 fCurrentBlock = -1;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002829 SpvId value = this->writeExpression(*varDecl.fValue, fGlobalInitializersBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002830 this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuffer);
ethannicholasf789b382016-08-03 12:43:36 -07002831 fCurrentBlock = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -07002832 }
ethannicholas14fe8cc2016-09-07 13:37:16 -07002833 this->writeLayout(var->fModifiers.fLayout, id);
Ethan Nicholas45b0f152017-07-24 14:36:40 -04002834 if (var->fModifiers.fFlags & Modifiers::kFlat_Flag) {
2835 this->writeInstruction(SpvOpDecorate, id, SpvDecorationFlat, fDecorationBuffer);
2836 }
2837 if (var->fModifiers.fFlags & Modifiers::kNoPerspective_Flag) {
2838 this->writeInstruction(SpvOpDecorate, id, SpvDecorationNoPerspective,
2839 fDecorationBuffer);
2840 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002841 }
2842}
2843
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002844void SPIRVCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, OutputStream& out) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002845 for (const auto& stmt : decl.fVars) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002846 SkASSERT(stmt->kind() == Statement::Kind::kVarDeclaration);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002847 VarDeclaration& varDecl = (VarDeclaration&) *stmt;
2848 const Variable* var = varDecl.fVar;
Brian Salomonf9f45122016-11-29 11:59:17 -05002849 // These haven't been implemented in our SPIR-V generator yet and we only currently use them
2850 // in the OpenGL backend.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002851 SkASSERT(!(var->fModifiers.fFlags & (Modifiers::kReadOnly_Flag |
Brian Salomonf9f45122016-11-29 11:59:17 -05002852 Modifiers::kWriteOnly_Flag |
2853 Modifiers::kCoherent_Flag |
2854 Modifiers::kVolatile_Flag |
2855 Modifiers::kRestrict_Flag)));
ethannicholasb3058bd2016-07-01 08:22:01 -07002856 SpvId id = this->nextId();
ethannicholas14fe8cc2016-09-07 13:37:16 -07002857 fVariableMap[var] = id;
Ethan Nicholas30d30222020-09-11 12:27:26 -04002858 SpvId type = this->getPointerType(var->type(), SpvStorageClassFunction);
ethannicholasb3058bd2016-07-01 08:22:01 -07002859 this->writeInstruction(SpvOpVariable, type, id, SpvStorageClassFunction, fVariableBuffer);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002860 this->writeInstruction(SpvOpName, id, var->fName, fNameBuffer);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002861 if (varDecl.fValue) {
2862 SpvId value = this->writeExpression(*varDecl.fValue, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002863 this->writeInstruction(SpvOpStore, id, value, out);
2864 }
2865 }
2866}
2867
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002868void SPIRVCodeGenerator::writeStatement(const Statement& s, OutputStream& out) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002869 switch (s.kind()) {
John Stiles98c1f822020-09-09 14:18:53 -04002870 case Statement::Kind::kInlineMarker:
Ethan Nicholase6592142020-09-08 10:22:09 -04002871 case Statement::Kind::kNop:
Ethan Nicholascb670962017-04-20 19:31:52 -04002872 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002873 case Statement::Kind::kBlock:
ethannicholasb3058bd2016-07-01 08:22:01 -07002874 this->writeBlock((Block&) s, out);
2875 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002876 case Statement::Kind::kExpression:
Ethan Nicholasd503a5a2020-09-30 09:29:55 -04002877 this->writeExpression(*s.as<ExpressionStatement>().expression(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002878 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002879 case Statement::Kind::kReturn:
John Stiles26f98502020-08-18 09:30:51 -04002880 this->writeReturnStatement(s.as<ReturnStatement>(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002881 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002882 case Statement::Kind::kVarDeclarations:
John Stiles26f98502020-08-18 09:30:51 -04002883 this->writeVarDeclarations(*s.as<VarDeclarationsStatement>().fDeclaration, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002884 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002885 case Statement::Kind::kIf:
John Stiles26f98502020-08-18 09:30:51 -04002886 this->writeIfStatement(s.as<IfStatement>(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002887 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002888 case Statement::Kind::kFor:
John Stiles26f98502020-08-18 09:30:51 -04002889 this->writeForStatement(s.as<ForStatement>(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002890 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002891 case Statement::Kind::kWhile:
John Stiles26f98502020-08-18 09:30:51 -04002892 this->writeWhileStatement(s.as<WhileStatement>(), out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002893 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002894 case Statement::Kind::kDo:
John Stiles26f98502020-08-18 09:30:51 -04002895 this->writeDoStatement(s.as<DoStatement>(), out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002896 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002897 case Statement::Kind::kSwitch:
John Stiles26f98502020-08-18 09:30:51 -04002898 this->writeSwitchStatement(s.as<SwitchStatement>(), out);
Ethan Nicholase92b1b12017-11-13 16:13:21 -05002899 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002900 case Statement::Kind::kBreak:
ethannicholasb3058bd2016-07-01 08:22:01 -07002901 this->writeInstruction(SpvOpBranch, fBreakTarget.top(), out);
2902 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002903 case Statement::Kind::kContinue:
ethannicholasb3058bd2016-07-01 08:22:01 -07002904 this->writeInstruction(SpvOpBranch, fContinueTarget.top(), out);
2905 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002906 case Statement::Kind::kDiscard:
ethannicholasb3058bd2016-07-01 08:22:01 -07002907 this->writeInstruction(SpvOpKill, out);
2908 break;
2909 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002910#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07002911 ABORT("unsupported statement: %s", s.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002912#endif
2913 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07002914 }
2915}
2916
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002917void SPIRVCodeGenerator::writeBlock(const Block& b, OutputStream& out) {
Ethan Nicholas7bd60432020-09-25 14:31:59 -04002918 for (const std::unique_ptr<Statement>& stmt : b.children()) {
2919 this->writeStatement(*stmt, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002920 }
2921}
2922
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002923void SPIRVCodeGenerator::writeIfStatement(const IfStatement& stmt, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002924 SpvId test = this->writeExpression(*stmt.fTest, out);
2925 SpvId ifTrue = this->nextId();
2926 SpvId ifFalse = this->nextId();
2927 if (stmt.fIfFalse) {
2928 SpvId end = this->nextId();
2929 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2930 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out);
2931 this->writeLabel(ifTrue, out);
2932 this->writeStatement(*stmt.fIfTrue, out);
2933 if (fCurrentBlock) {
2934 this->writeInstruction(SpvOpBranch, end, out);
2935 }
2936 this->writeLabel(ifFalse, out);
2937 this->writeStatement(*stmt.fIfFalse, out);
2938 if (fCurrentBlock) {
2939 this->writeInstruction(SpvOpBranch, end, out);
2940 }
2941 this->writeLabel(end, out);
2942 } else {
2943 this->writeInstruction(SpvOpSelectionMerge, ifFalse, SpvSelectionControlMaskNone, out);
2944 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out);
2945 this->writeLabel(ifTrue, out);
2946 this->writeStatement(*stmt.fIfTrue, out);
2947 if (fCurrentBlock) {
2948 this->writeInstruction(SpvOpBranch, ifFalse, out);
2949 }
2950 this->writeLabel(ifFalse, out);
2951 }
2952}
2953
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002954void SPIRVCodeGenerator::writeForStatement(const ForStatement& f, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002955 if (f.fInitializer) {
2956 this->writeStatement(*f.fInitializer, out);
2957 }
2958 SpvId header = this->nextId();
2959 SpvId start = this->nextId();
2960 SpvId body = this->nextId();
2961 SpvId next = this->nextId();
2962 fContinueTarget.push(next);
2963 SpvId end = this->nextId();
2964 fBreakTarget.push(end);
2965 this->writeInstruction(SpvOpBranch, header, out);
2966 this->writeLabel(header, out);
2967 this->writeInstruction(SpvOpLoopMerge, end, next, SpvLoopControlMaskNone, out);
ethannicholasf789b382016-08-03 12:43:36 -07002968 this->writeInstruction(SpvOpBranch, start, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002969 this->writeLabel(start, out);
ethannicholas22f939e2016-10-13 13:25:34 -07002970 if (f.fTest) {
2971 SpvId test = this->writeExpression(*f.fTest, out);
2972 this->writeInstruction(SpvOpBranchConditional, test, body, end, out);
2973 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002974 this->writeLabel(body, out);
2975 this->writeStatement(*f.fStatement, out);
2976 if (fCurrentBlock) {
2977 this->writeInstruction(SpvOpBranch, next, out);
2978 }
2979 this->writeLabel(next, out);
2980 if (f.fNext) {
2981 this->writeExpression(*f.fNext, out);
2982 }
2983 this->writeInstruction(SpvOpBranch, header, out);
2984 this->writeLabel(end, out);
2985 fBreakTarget.pop();
2986 fContinueTarget.pop();
2987}
2988
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002989void SPIRVCodeGenerator::writeWhileStatement(const WhileStatement& w, OutputStream& out) {
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002990 SpvId header = this->nextId();
2991 SpvId start = this->nextId();
2992 SpvId body = this->nextId();
Ethan Nicholas0d997662019-04-08 09:46:01 -04002993 SpvId continueTarget = this->nextId();
2994 fContinueTarget.push(continueTarget);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002995 SpvId end = this->nextId();
2996 fBreakTarget.push(end);
2997 this->writeInstruction(SpvOpBranch, header, out);
2998 this->writeLabel(header, out);
Ethan Nicholas0d997662019-04-08 09:46:01 -04002999 this->writeInstruction(SpvOpLoopMerge, end, continueTarget, SpvLoopControlMaskNone, out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003000 this->writeInstruction(SpvOpBranch, start, out);
3001 this->writeLabel(start, out);
3002 SpvId test = this->writeExpression(*w.fTest, out);
3003 this->writeInstruction(SpvOpBranchConditional, test, body, end, out);
3004 this->writeLabel(body, out);
3005 this->writeStatement(*w.fStatement, out);
3006 if (fCurrentBlock) {
Ethan Nicholas0d997662019-04-08 09:46:01 -04003007 this->writeInstruction(SpvOpBranch, continueTarget, out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003008 }
Ethan Nicholas0d997662019-04-08 09:46:01 -04003009 this->writeLabel(continueTarget, out);
3010 this->writeInstruction(SpvOpBranch, header, out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003011 this->writeLabel(end, out);
3012 fBreakTarget.pop();
3013 fContinueTarget.pop();
3014}
3015
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003016void SPIRVCodeGenerator::writeDoStatement(const DoStatement& d, OutputStream& out) {
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003017 SpvId header = this->nextId();
3018 SpvId start = this->nextId();
3019 SpvId next = this->nextId();
Ethan Nicholas0d997662019-04-08 09:46:01 -04003020 SpvId continueTarget = this->nextId();
3021 fContinueTarget.push(continueTarget);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003022 SpvId end = this->nextId();
3023 fBreakTarget.push(end);
3024 this->writeInstruction(SpvOpBranch, header, out);
3025 this->writeLabel(header, out);
Ethan Nicholas0d997662019-04-08 09:46:01 -04003026 this->writeInstruction(SpvOpLoopMerge, end, continueTarget, SpvLoopControlMaskNone, out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003027 this->writeInstruction(SpvOpBranch, start, out);
3028 this->writeLabel(start, out);
Ethan Nicholas1fd61162020-09-28 13:14:19 -04003029 this->writeStatement(*d.statement(), out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003030 if (fCurrentBlock) {
3031 this->writeInstruction(SpvOpBranch, next, out);
3032 }
3033 this->writeLabel(next, out);
Ethan Nicholas1fd61162020-09-28 13:14:19 -04003034 SpvId test = this->writeExpression(*d.test(), out);
Ethan Nicholas0d997662019-04-08 09:46:01 -04003035 this->writeInstruction(SpvOpBranchConditional, test, continueTarget, end, out);
3036 this->writeLabel(continueTarget, out);
3037 this->writeInstruction(SpvOpBranch, header, out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003038 this->writeLabel(end, out);
3039 fBreakTarget.pop();
3040 fContinueTarget.pop();
3041}
3042
Ethan Nicholase92b1b12017-11-13 16:13:21 -05003043void SPIRVCodeGenerator::writeSwitchStatement(const SwitchStatement& s, OutputStream& out) {
3044 SpvId value = this->writeExpression(*s.fValue, out);
3045 std::vector<SpvId> labels;
3046 SpvId end = this->nextId();
3047 SpvId defaultLabel = end;
3048 fBreakTarget.push(end);
3049 int size = 3;
3050 for (const auto& c : s.fCases) {
3051 SpvId label = this->nextId();
3052 labels.push_back(label);
3053 if (c->fValue) {
3054 size += 2;
3055 } else {
3056 defaultLabel = label;
3057 }
3058 }
3059 labels.push_back(end);
3060 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
3061 this->writeOpCode(SpvOpSwitch, size, out);
3062 this->writeWord(value, out);
3063 this->writeWord(defaultLabel, out);
3064 for (size_t i = 0; i < s.fCases.size(); ++i) {
3065 if (!s.fCases[i]->fValue) {
3066 continue;
3067 }
Ethan Nicholase96cdd12020-09-28 16:27:18 -04003068 this->writeWord(s.fCases[i]->fValue->as<IntLiteral>().value(), out);
Ethan Nicholase92b1b12017-11-13 16:13:21 -05003069 this->writeWord(labels[i], out);
3070 }
3071 for (size_t i = 0; i < s.fCases.size(); ++i) {
3072 this->writeLabel(labels[i], out);
3073 for (const auto& stmt : s.fCases[i]->fStatements) {
3074 this->writeStatement(*stmt, out);
3075 }
3076 if (fCurrentBlock) {
3077 this->writeInstruction(SpvOpBranch, labels[i + 1], out);
3078 }
3079 }
3080 this->writeLabel(end, out);
3081 fBreakTarget.pop();
3082}
3083
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003084void SPIRVCodeGenerator::writeReturnStatement(const ReturnStatement& r, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07003085 if (r.fExpression) {
Greg Daniel64773e62016-11-22 09:44:03 -05003086 this->writeInstruction(SpvOpReturnValue, this->writeExpression(*r.fExpression, out),
ethannicholasb3058bd2016-07-01 08:22:01 -07003087 out);
3088 } else {
3089 this->writeInstruction(SpvOpReturn, out);
3090 }
3091}
3092
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003093void SPIRVCodeGenerator::writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003094 SkASSERT(fProgram.fKind == Program::kGeometry_Kind);
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003095 int invocations = 1;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003096 for (const auto& e : fProgram) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003097 if (e.kind() == ProgramElement::Kind::kModifiers) {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003098 const Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003099 if (m.fFlags & Modifiers::kIn_Flag) {
3100 if (m.fLayout.fInvocations != -1) {
3101 invocations = m.fLayout.fInvocations;
3102 }
3103 SpvId input;
3104 switch (m.fLayout.fPrimitive) {
3105 case Layout::kPoints_Primitive:
3106 input = SpvExecutionModeInputPoints;
3107 break;
3108 case Layout::kLines_Primitive:
3109 input = SpvExecutionModeInputLines;
3110 break;
3111 case Layout::kLinesAdjacency_Primitive:
3112 input = SpvExecutionModeInputLinesAdjacency;
3113 break;
3114 case Layout::kTriangles_Primitive:
3115 input = SpvExecutionModeTriangles;
3116 break;
3117 case Layout::kTrianglesAdjacency_Primitive:
3118 input = SpvExecutionModeInputTrianglesAdjacency;
3119 break;
3120 default:
3121 input = 0;
3122 break;
3123 }
Ethan Nicholas81d15112018-07-13 12:48:50 -04003124 update_sk_in_count(m, &fSkInCount);
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003125 if (input) {
3126 this->writeInstruction(SpvOpExecutionMode, entryPoint, input, out);
3127 }
3128 } else if (m.fFlags & Modifiers::kOut_Flag) {
3129 SpvId output;
3130 switch (m.fLayout.fPrimitive) {
3131 case Layout::kPoints_Primitive:
3132 output = SpvExecutionModeOutputPoints;
3133 break;
3134 case Layout::kLineStrip_Primitive:
3135 output = SpvExecutionModeOutputLineStrip;
3136 break;
3137 case Layout::kTriangleStrip_Primitive:
3138 output = SpvExecutionModeOutputTriangleStrip;
3139 break;
3140 default:
3141 output = 0;
3142 break;
3143 }
3144 if (output) {
3145 this->writeInstruction(SpvOpExecutionMode, entryPoint, output, out);
3146 }
3147 if (m.fLayout.fMaxVertices != -1) {
3148 this->writeInstruction(SpvOpExecutionMode, entryPoint,
3149 SpvExecutionModeOutputVertices, m.fLayout.fMaxVertices,
3150 out);
3151 }
3152 }
3153 }
3154 }
3155 this->writeInstruction(SpvOpExecutionMode, entryPoint, SpvExecutionModeInvocations,
3156 invocations, out);
3157}
3158
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003159void SPIRVCodeGenerator::writeInstructions(const Program& program, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07003160 fGLSLExtendedInstructions = this->nextId();
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003161 StringStream body;
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003162 std::set<SpvId> interfaceVars;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003163 // assign IDs to functions, determine sk_in size
3164 int skInSize = -1;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003165 for (const auto& e : program) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003166 switch (e.kind()) {
3167 case ProgramElement::Kind::kFunction: {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003168 FunctionDefinition& f = (FunctionDefinition&) e;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003169 fFunctionMap[&f.fDeclaration] = this->nextId();
3170 break;
3171 }
Ethan Nicholase6592142020-09-08 10:22:09 -04003172 case ProgramElement::Kind::kModifiers: {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003173 Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003174 if (m.fFlags & Modifiers::kIn_Flag) {
3175 switch (m.fLayout.fPrimitive) {
3176 case Layout::kPoints_Primitive: // break
3177 case Layout::kLines_Primitive:
3178 skInSize = 1;
3179 break;
3180 case Layout::kLinesAdjacency_Primitive: // break
3181 skInSize = 2;
3182 break;
3183 case Layout::kTriangles_Primitive: // break
3184 case Layout::kTrianglesAdjacency_Primitive:
3185 skInSize = 3;
3186 break;
3187 default:
3188 break;
3189 }
3190 }
3191 break;
3192 }
3193 default:
3194 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07003195 }
3196 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003197 for (const auto& e : program) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003198 if (e.kind() == ProgramElement::Kind::kInterfaceBlock) {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003199 InterfaceBlock& intf = (InterfaceBlock&) e;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003200 if (SK_IN_BUILTIN == intf.fVariable.fModifiers.fLayout.fBuiltin) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003201 SkASSERT(skInSize != -1);
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003202 intf.fSizes.emplace_back(new IntLiteral(fContext, -1, skInSize));
3203 }
ethannicholasb3058bd2016-07-01 08:22:01 -07003204 SpvId id = this->writeInterfaceBlock(intf);
Ethan Nicholas16c11962018-03-16 12:20:54 -04003205 if (((intf.fVariable.fModifiers.fFlags & Modifiers::kIn_Flag) ||
3206 (intf.fVariable.fModifiers.fFlags & Modifiers::kOut_Flag)) &&
Chris Dalton2284aab2019-11-15 11:02:24 -07003207 intf.fVariable.fModifiers.fLayout.fBuiltin == -1 &&
3208 !is_dead(intf.fVariable)) {
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003209 interfaceVars.insert(id);
ethannicholasb3058bd2016-07-01 08:22:01 -07003210 }
3211 }
3212 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003213 for (const auto& e : program) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003214 if (e.kind() == ProgramElement::Kind::kVar) {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003215 this->writeGlobalVars(program.fKind, ((VarDeclarations&) e), body);
ethannicholasb3058bd2016-07-01 08:22:01 -07003216 }
3217 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003218 for (const auto& e : program) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003219 if (e.kind() == ProgramElement::Kind::kFunction) {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003220 this->writeFunction(((FunctionDefinition&) e), body);
ethannicholasb3058bd2016-07-01 08:22:01 -07003221 }
3222 }
ethannicholasd598f792016-07-25 10:08:54 -07003223 const FunctionDeclaration* main = nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07003224 for (auto entry : fFunctionMap) {
ethannicholasf789b382016-08-03 12:43:36 -07003225 if (entry.first->fName == "main") {
ethannicholasb3058bd2016-07-01 08:22:01 -07003226 main = entry.first;
3227 }
3228 }
Ethan Nicholas1a668d22019-04-18 10:37:40 -04003229 if (!main) {
3230 fErrors.error(0, "program does not contain a main() function");
3231 return;
3232 }
ethannicholasb3058bd2016-07-01 08:22:01 -07003233 for (auto entry : fVariableMap) {
ethannicholasd598f792016-07-25 10:08:54 -07003234 const Variable* var = entry.first;
Greg Daniel64773e62016-11-22 09:44:03 -05003235 if (var->fStorage == Variable::kGlobal_Storage &&
Ethan Nicholas16c11962018-03-16 12:20:54 -04003236 ((var->fModifiers.fFlags & Modifiers::kIn_Flag) ||
Chris Dalton2284aab2019-11-15 11:02:24 -07003237 (var->fModifiers.fFlags & Modifiers::kOut_Flag)) && !is_dead(*var)) {
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003238 interfaceVars.insert(entry.second);
ethannicholasb3058bd2016-07-01 08:22:01 -07003239 }
3240 }
3241 this->writeCapabilities(out);
3242 this->writeInstruction(SpvOpExtInstImport, fGLSLExtendedInstructions, "GLSL.std.450", out);
3243 this->writeInstruction(SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemoryModelGLSL450, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07003244 this->writeOpCode(SpvOpEntryPoint, (SpvId) (3 + (main->fName.fLength + 4) / 4) +
ethannicholasb3058bd2016-07-01 08:22:01 -07003245 (int32_t) interfaceVars.size(), out);
3246 switch (program.fKind) {
3247 case Program::kVertex_Kind:
3248 this->writeWord(SpvExecutionModelVertex, out);
3249 break;
3250 case Program::kFragment_Kind:
3251 this->writeWord(SpvExecutionModelFragment, out);
3252 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05003253 case Program::kGeometry_Kind:
3254 this->writeWord(SpvExecutionModelGeometry, out);
3255 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04003256 default:
3257 ABORT("cannot write this kind of program to SPIR-V\n");
ethannicholasb3058bd2016-07-01 08:22:01 -07003258 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003259 SpvId entryPoint = fFunctionMap[main];
3260 this->writeWord(entryPoint, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07003261 this->writeString(main->fName.fChars, main->fName.fLength, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003262 for (int var : interfaceVars) {
3263 this->writeWord(var, out);
3264 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003265 if (program.fKind == Program::kGeometry_Kind) {
3266 this->writeGeometryShaderExecutionMode(entryPoint, out);
3267 }
ethannicholasb3058bd2016-07-01 08:22:01 -07003268 if (program.fKind == Program::kFragment_Kind) {
Greg Daniel64773e62016-11-22 09:44:03 -05003269 this->writeInstruction(SpvOpExecutionMode,
3270 fFunctionMap[main],
ethannicholasb3058bd2016-07-01 08:22:01 -07003271 SpvExecutionModeOriginUpperLeft,
3272 out);
3273 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003274 for (const auto& e : program) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003275 if (e.kind() == ProgramElement::Kind::kExtension) {
Ethan Nicholasefb09e22020-09-30 10:17:00 -04003276 this->writeInstruction(SpvOpSourceExtension, ((Extension&) e).name().c_str(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003277 }
3278 }
Greg Daniel64773e62016-11-22 09:44:03 -05003279
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003280 write_stringstream(fExtraGlobalsBuffer, out);
3281 write_stringstream(fNameBuffer, out);
3282 write_stringstream(fDecorationBuffer, out);
3283 write_stringstream(fConstantBuffer, out);
3284 write_stringstream(fExternalFunctionsBuffer, out);
3285 write_stringstream(body, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003286}
3287
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003288bool SPIRVCodeGenerator::generateCode() {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003289 SkASSERT(!fErrors.errorCount());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003290 this->writeWord(SpvMagicNumber, *fOut);
3291 this->writeWord(SpvVersion, *fOut);
3292 this->writeWord(SKSL_MAGIC, *fOut);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003293 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003294 this->writeInstructions(fProgram, buffer);
3295 this->writeWord(fIdCount, *fOut);
3296 this->writeWord(0, *fOut); // reserved, always zero
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003297 write_stringstream(buffer, *fOut);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003298 return 0 == fErrors.errorCount();
ethannicholasb3058bd2016-07-01 08:22:01 -07003299}
3300
John Stilesa6841be2020-08-06 14:11:56 -04003301} // namespace SkSL