blob: da914666de9f7f3e282dec90c9d349bfd2d722d9 [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 = ", ";
Brian Osmanc4f937b2020-03-25 15:39:07 -0400586 key += to_string(this->getType(function.fParameters[i]->fType));
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])) {
ethannicholasd598f792016-07-25 10:08:54 -0700619 parameterTypes.push_back(this->getPointerType(function.fParameters[i]->fType,
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) {
698 const Type& type = c.fArguments[0]->fType;
699 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);
ethannicholasd598f792016-07-25 10:08:54 -0700725 this->writeWord(this->getType(c.fType), 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 Nicholasbb155e22017-07-24 10:05:09 -0400744 if (c.fType != *fContext.fVoid_Type) {
745 this->writeOpCode((SpvOp_) intrinsicId, 3 + (int32_t) arguments.size(), out);
746 this->writeWord(this->getType(c.fType), out);
747 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 Nicholase6592142020-09-08 10:22:09 -0400768 if (a->fType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500769 if (vectorSize) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400770 SkASSERT(a->fType.columns() == vectorSize);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500771 }
772 else {
773 vectorSize = a->fType.columns();
774 }
775 }
776 }
777 std::vector<SpvId> result;
778 for (const auto& a : args) {
779 SpvId raw = this->writeExpression(*a, out);
Ethan Nicholase6592142020-09-08 10:22:09 -0400780 if (vectorSize && a->fType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500781 SpvId vector = this->nextId();
782 this->writeOpCode(SpvOpCompositeConstruct, 3 + vectorSize, out);
783 this->writeWord(this->getType(a->fType.toCompound(fContext, vectorSize, 1)), out);
784 this->writeWord(vector, out);
785 for (int i = 0; i < vectorSize; i++) {
786 this->writeWord(raw, out);
787 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -0400788 this->writePrecisionModifier(a->fType, vector);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500789 result.push_back(vector);
790 } else {
791 result.push_back(raw);
792 }
793 }
794 return result;
795}
796
797void SPIRVCodeGenerator::writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
798 SpvId signedInst, SpvId unsignedInst,
799 const std::vector<SpvId>& args,
800 OutputStream& out) {
801 this->writeOpCode(SpvOpExtInst, 5 + args.size(), out);
802 this->writeWord(this->getType(type), out);
803 this->writeWord(id, out);
804 this->writeWord(fGLSLExtendedInstructions, out);
805
806 if (is_float(fContext, type)) {
807 this->writeWord(floatInst, out);
808 } else if (is_signed(fContext, type)) {
809 this->writeWord(signedInst, out);
810 } else if (is_unsigned(fContext, type)) {
811 this->writeWord(unsignedInst, out);
812 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400813 SkASSERT(false);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500814 }
815 for (SpvId a : args) {
816 this->writeWord(a, out);
817 }
818}
819
Greg Daniel64773e62016-11-22 09:44:03 -0500820SpvId SPIRVCodeGenerator::writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400821 OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700822 SpvId result = this->nextId();
823 switch (kind) {
824 case kAtan_SpecialIntrinsic: {
825 std::vector<SpvId> arguments;
826 for (size_t i = 0; i < c.fArguments.size(); i++) {
827 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
828 }
829 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) arguments.size(), out);
ethannicholasd598f792016-07-25 10:08:54 -0700830 this->writeWord(this->getType(c.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700831 this->writeWord(result, out);
832 this->writeWord(fGLSLExtendedInstructions, out);
833 this->writeWord(arguments.size() == 2 ? GLSLstd450Atan2 : GLSLstd450Atan, out);
834 for (SpvId id : arguments) {
835 this->writeWord(id, out);
836 }
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400837 break;
838 }
Stephen Whiteff5d7a22019-07-26 17:42:06 -0400839 case kSampledImage_SpecialIntrinsic: {
840 SkASSERT(2 == c.fArguments.size());
841 SpvId img = this->writeExpression(*c.fArguments[0], out);
842 SpvId sampler = this->writeExpression(*c.fArguments[1], out);
843 this->writeInstruction(SpvOpSampledImage,
844 this->getType(c.fType),
845 result,
846 img,
847 sampler,
848 out);
849 break;
850 }
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400851 case kSubpassLoad_SpecialIntrinsic: {
852 SpvId img = this->writeExpression(*c.fArguments[0], out);
853 std::vector<std::unique_ptr<Expression>> args;
Greg Danielcf1a4f52020-09-08 15:25:23 -0400854 args.emplace_back(new IntLiteral(fContext, -1, 0));
855 args.emplace_back(new IntLiteral(fContext, -1, 0));
856 Constructor ctor(-1, *fContext.fInt2_Type, std::move(args));
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400857 SpvId coords = this->writeConstantVector(ctor);
858 if (1 == c.fArguments.size()) {
859 this->writeInstruction(SpvOpImageRead,
860 this->getType(c.fType),
861 result,
862 img,
863 coords,
864 out);
865 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400866 SkASSERT(2 == c.fArguments.size());
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400867 SpvId sample = this->writeExpression(*c.fArguments[1], out);
868 this->writeInstruction(SpvOpImageRead,
869 this->getType(c.fType),
870 result,
871 img,
872 coords,
873 SpvImageOperandsSampleMask,
874 sample,
875 out);
876 }
877 break;
878 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700879 case kTexture_SpecialIntrinsic: {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500880 SpvOp_ op = SpvOpImageSampleImplicitLod;
881 switch (c.fArguments[0]->fType.dimensions()) {
882 case SpvDim1D:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400883 if (c.fArguments[1]->fType == *fContext.fFloat2_Type) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500884 op = SpvOpImageSampleProjImplicitLod;
885 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400886 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500887 }
888 break;
889 case SpvDim2D:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400890 if (c.fArguments[1]->fType == *fContext.fFloat3_Type) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500891 op = SpvOpImageSampleProjImplicitLod;
892 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400893 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat2_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500894 }
895 break;
896 case SpvDim3D:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400897 if (c.fArguments[1]->fType == *fContext.fFloat4_Type) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500898 op = SpvOpImageSampleProjImplicitLod;
899 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400900 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat3_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500901 }
902 break;
903 case SpvDimCube: // fall through
904 case SpvDimRect: // fall through
905 case SpvDimBuffer: // fall through
906 case SpvDimSubpassData:
907 break;
908 }
ethannicholasd598f792016-07-25 10:08:54 -0700909 SpvId type = this->getType(c.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700910 SpvId sampler = this->writeExpression(*c.fArguments[0], out);
911 SpvId uv = this->writeExpression(*c.fArguments[1], out);
912 if (c.fArguments.size() == 3) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500913 this->writeInstruction(op, type, result, sampler, uv,
ethannicholasb3058bd2016-07-01 08:22:01 -0700914 SpvImageOperandsBiasMask,
915 this->writeExpression(*c.fArguments[2], out),
916 out);
917 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400918 SkASSERT(c.fArguments.size() == 2);
Brian Osman8a83ca42018-02-12 14:32:17 -0500919 if (fProgram.fSettings.fSharpenTextures) {
920 FloatLiteral lodBias(fContext, -1, -0.5);
921 this->writeInstruction(op, type, result, sampler, uv,
922 SpvImageOperandsBiasMask,
923 this->writeFloatLiteral(lodBias),
924 out);
925 } else {
926 this->writeInstruction(op, type, result, sampler, uv,
927 out);
928 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700929 }
930 break;
931 }
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500932 case kMod_SpecialIntrinsic: {
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500933 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400934 SkASSERT(args.size() == 2);
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500935 const Type& operandType = c.fArguments[0]->fType;
936 SpvOp_ op;
937 if (is_float(fContext, operandType)) {
938 op = SpvOpFMod;
939 } else if (is_signed(fContext, operandType)) {
940 op = SpvOpSMod;
941 } else if (is_unsigned(fContext, operandType)) {
942 op = SpvOpUMod;
943 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400944 SkASSERT(false);
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500945 return 0;
946 }
947 this->writeOpCode(op, 5, out);
948 this->writeWord(this->getType(operandType), out);
949 this->writeWord(result, out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500950 this->writeWord(args[0], out);
951 this->writeWord(args[1], out);
952 break;
953 }
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700954 case kDFdy_SpecialIntrinsic: {
955 SpvId fn = this->writeExpression(*c.fArguments[0], out);
956 this->writeOpCode(SpvOpDPdy, 4, out);
957 this->writeWord(this->getType(c.fType), out);
958 this->writeWord(result, out);
959 this->writeWord(fn, out);
960 if (fProgram.fSettings.fFlipY) {
961 // Flipping Y also negates the Y derivatives.
962 SpvId flipped = this->nextId();
963 this->writeInstruction(SpvOpFNegate, this->getType(c.fType), flipped, result, out);
Ethan Nicholas10e93b62019-03-20 10:46:14 -0400964 this->writePrecisionModifier(c.fType, flipped);
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700965 return flipped;
966 }
967 break;
968 }
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500969 case kClamp_SpecialIntrinsic: {
970 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400971 SkASSERT(args.size() == 3);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500972 this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FClamp, GLSLstd450SClamp,
973 GLSLstd450UClamp, args, out);
974 break;
975 }
976 case kMax_SpecialIntrinsic: {
977 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400978 SkASSERT(args.size() == 2);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500979 this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FMax, GLSLstd450SMax,
980 GLSLstd450UMax, args, out);
981 break;
982 }
983 case kMin_SpecialIntrinsic: {
984 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400985 SkASSERT(args.size() == 2);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500986 this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FMin, GLSLstd450SMin,
987 GLSLstd450UMin, args, out);
988 break;
989 }
990 case kMix_SpecialIntrinsic: {
991 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400992 SkASSERT(args.size() == 3);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500993 this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FMix, SpvOpUndef,
994 SpvOpUndef, args, out);
995 break;
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500996 }
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400997 case kSaturate_SpecialIntrinsic: {
998 SkASSERT(c.fArguments.size() == 1);
999 std::vector<std::unique_ptr<Expression>> finalArgs;
1000 finalArgs.push_back(c.fArguments[0]->clone());
1001 finalArgs.emplace_back(new FloatLiteral(fContext, -1, 0));
1002 finalArgs.emplace_back(new FloatLiteral(fContext, -1, 1));
1003 std::vector<SpvId> spvArgs = this->vectorize(finalArgs, out);
1004 this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FClamp, GLSLstd450SClamp,
1005 GLSLstd450UClamp, spvArgs, out);
1006 break;
1007 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001008 }
1009 return result;
1010}
1011
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001012SpvId SPIRVCodeGenerator::writeFunctionCall(const FunctionCall& c, OutputStream& out) {
ethannicholasd598f792016-07-25 10:08:54 -07001013 const auto& entry = fFunctionMap.find(&c.fFunction);
ethannicholasb3058bd2016-07-01 08:22:01 -07001014 if (entry == fFunctionMap.end()) {
1015 return this->writeIntrinsicCall(c, out);
1016 }
1017 // stores (variable, type, lvalue) pairs to extract and save after the function call is complete
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001018 std::vector<std::tuple<SpvId, const Type*, std::unique_ptr<LValue>>> lvalues;
ethannicholasb3058bd2016-07-01 08:22:01 -07001019 std::vector<SpvId> arguments;
1020 for (size_t i = 0; i < c.fArguments.size(); i++) {
Greg Daniel64773e62016-11-22 09:44:03 -05001021 // id of temporary variable that we will use to hold this argument, or 0 if it is being
ethannicholasb3058bd2016-07-01 08:22:01 -07001022 // passed directly
1023 SpvId tmpVar;
1024 // if we need a temporary var to store this argument, this is the value to store in the var
1025 SpvId tmpValueId;
ethannicholasd598f792016-07-25 10:08:54 -07001026 if (is_out(*c.fFunction.fParameters[i])) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001027 std::unique_ptr<LValue> lv = this->getLValue(*c.fArguments[i], out);
1028 SpvId ptr = lv->getPointer();
1029 if (ptr) {
1030 arguments.push_back(ptr);
1031 continue;
1032 } else {
1033 // lvalue cannot simply be read and written via a pointer (e.g. a swizzle). Need to
1034 // copy it into a temp, call the function, read the value out of the temp, and then
1035 // update the lvalue.
1036 tmpValueId = lv->load(out);
1037 tmpVar = this->nextId();
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001038 lvalues.push_back(std::make_tuple(tmpVar, &c.fArguments[i]->fType, std::move(lv)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001039 }
1040 } else {
1041 // see getFunctionType for an explanation of why we're always using pointer parameters
1042 tmpValueId = this->writeExpression(*c.fArguments[i], out);
1043 tmpVar = this->nextId();
1044 }
Greg Daniel64773e62016-11-22 09:44:03 -05001045 this->writeInstruction(SpvOpVariable,
1046 this->getPointerType(c.fArguments[i]->fType,
ethannicholasb3058bd2016-07-01 08:22:01 -07001047 SpvStorageClassFunction),
Greg Daniel64773e62016-11-22 09:44:03 -05001048 tmpVar,
ethannicholasb3058bd2016-07-01 08:22:01 -07001049 SpvStorageClassFunction,
ethannicholasd598f792016-07-25 10:08:54 -07001050 fVariableBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07001051 this->writeInstruction(SpvOpStore, tmpVar, tmpValueId, out);
1052 arguments.push_back(tmpVar);
1053 }
1054 SpvId result = this->nextId();
1055 this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t) c.fArguments.size(), out);
ethannicholasd598f792016-07-25 10:08:54 -07001056 this->writeWord(this->getType(c.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001057 this->writeWord(result, out);
1058 this->writeWord(entry->second, out);
1059 for (SpvId id : arguments) {
1060 this->writeWord(id, out);
1061 }
1062 // now that the call is complete, we may need to update some lvalues with the new values of out
1063 // arguments
1064 for (const auto& tuple : lvalues) {
1065 SpvId load = this->nextId();
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001066 this->writeInstruction(SpvOpLoad, getType(*std::get<1>(tuple)), load, std::get<0>(tuple),
1067 out);
1068 this->writePrecisionModifier(*std::get<1>(tuple), load);
ethannicholasb3058bd2016-07-01 08:22:01 -07001069 std::get<2>(tuple)->store(load, out);
1070 }
1071 return result;
1072}
1073
ethannicholasf789b382016-08-03 12:43:36 -07001074SpvId SPIRVCodeGenerator::writeConstantVector(const Constructor& c) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001075 SkASSERT(c.fType.typeKind() == Type::TypeKind::kVector && c.isCompileTimeConstant());
ethannicholasb3058bd2016-07-01 08:22:01 -07001076 SpvId result = this->nextId();
1077 std::vector<SpvId> arguments;
1078 for (size_t i = 0; i < c.fArguments.size(); i++) {
1079 arguments.push_back(this->writeExpression(*c.fArguments[i], fConstantBuffer));
1080 }
ethannicholasd598f792016-07-25 10:08:54 -07001081 SpvId type = this->getType(c.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -07001082 if (c.fArguments.size() == 1) {
1083 // with a single argument, a vector will have all of its entries equal to the argument
ethannicholasd598f792016-07-25 10:08:54 -07001084 this->writeOpCode(SpvOpConstantComposite, 3 + c.fType.columns(), fConstantBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07001085 this->writeWord(type, fConstantBuffer);
1086 this->writeWord(result, fConstantBuffer);
ethannicholasd598f792016-07-25 10:08:54 -07001087 for (int i = 0; i < c.fType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001088 this->writeWord(arguments[0], fConstantBuffer);
1089 }
1090 } else {
Greg Daniel64773e62016-11-22 09:44:03 -05001091 this->writeOpCode(SpvOpConstantComposite, 3 + (int32_t) c.fArguments.size(),
ethannicholasb3058bd2016-07-01 08:22:01 -07001092 fConstantBuffer);
1093 this->writeWord(type, fConstantBuffer);
1094 this->writeWord(result, fConstantBuffer);
1095 for (SpvId id : arguments) {
1096 this->writeWord(id, fConstantBuffer);
1097 }
1098 }
1099 return result;
1100}
1101
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001102SpvId SPIRVCodeGenerator::writeFloatConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001103 SkASSERT(c.fType.isFloat());
1104 SkASSERT(c.fArguments.size() == 1);
1105 SkASSERT(c.fArguments[0]->fType.isNumber());
ethannicholasb3058bd2016-07-01 08:22:01 -07001106 SpvId result = this->nextId();
1107 SpvId parameter = this->writeExpression(*c.fArguments[0], out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001108 if (c.fArguments[0]->fType.isSigned()) {
Greg Daniel64773e62016-11-22 09:44:03 -05001109 this->writeInstruction(SpvOpConvertSToF, this->getType(c.fType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001110 out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001111 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001112 SkASSERT(c.fArguments[0]->fType.isUnsigned());
Greg Daniel64773e62016-11-22 09:44:03 -05001113 this->writeInstruction(SpvOpConvertUToF, this->getType(c.fType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001114 out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001115 }
1116 return result;
1117}
1118
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001119SpvId SPIRVCodeGenerator::writeIntConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001120 SkASSERT(c.fType.isSigned());
1121 SkASSERT(c.fArguments.size() == 1);
1122 SkASSERT(c.fArguments[0]->fType.isNumber());
ethannicholasb3058bd2016-07-01 08:22:01 -07001123 SpvId result = this->nextId();
1124 SpvId parameter = this->writeExpression(*c.fArguments[0], out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001125 if (c.fArguments[0]->fType.isFloat()) {
Greg Daniel64773e62016-11-22 09:44:03 -05001126 this->writeInstruction(SpvOpConvertFToS, this->getType(c.fType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001127 out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001128 }
1129 else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001130 SkASSERT(c.fArguments[0]->fType.isUnsigned());
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001131 this->writeInstruction(SpvOpBitcast, this->getType(c.fType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001132 out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001133 }
1134 return result;
1135}
1136
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001137SpvId SPIRVCodeGenerator::writeUIntConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001138 SkASSERT(c.fType.isUnsigned());
1139 SkASSERT(c.fArguments.size() == 1);
1140 SkASSERT(c.fArguments[0]->fType.isNumber());
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001141 SpvId result = this->nextId();
1142 SpvId parameter = this->writeExpression(*c.fArguments[0], out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001143 if (c.fArguments[0]->fType.isFloat()) {
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001144 this->writeInstruction(SpvOpConvertFToU, this->getType(c.fType), result, parameter,
1145 out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001146 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001147 SkASSERT(c.fArguments[0]->fType.isSigned());
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001148 this->writeInstruction(SpvOpBitcast, this->getType(c.fType), result, parameter,
1149 out);
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001150 }
1151 return result;
1152}
1153
Ethan Nicholas84645e32017-02-09 13:57:14 -05001154void SPIRVCodeGenerator::writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001155 OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001156 FloatLiteral zero(fContext, -1, 0);
Ethan Nicholas84645e32017-02-09 13:57:14 -05001157 SpvId zeroId = this->writeFloatLiteral(zero);
1158 std::vector<SpvId> columnIds;
1159 for (int column = 0; column < type.columns(); column++) {
1160 this->writeOpCode(SpvOpCompositeConstruct, 3 + type.rows(),
1161 out);
1162 this->writeWord(this->getType(type.componentType().toCompound(fContext, type.rows(), 1)),
1163 out);
1164 SpvId columnId = this->nextId();
1165 this->writeWord(columnId, out);
1166 columnIds.push_back(columnId);
1167 for (int row = 0; row < type.columns(); row++) {
1168 this->writeWord(row == column ? diagonal : zeroId, out);
1169 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001170 this->writePrecisionModifier(type, columnId);
Ethan Nicholas84645e32017-02-09 13:57:14 -05001171 }
1172 this->writeOpCode(SpvOpCompositeConstruct, 3 + type.columns(),
1173 out);
1174 this->writeWord(this->getType(type), out);
1175 this->writeWord(id, out);
John Stilesf621e232020-08-25 13:33:02 -04001176 for (SpvId columnId : columnIds) {
1177 this->writeWord(columnId, out);
Ethan Nicholas84645e32017-02-09 13:57:14 -05001178 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001179 this->writePrecisionModifier(type, id);
Ethan Nicholas84645e32017-02-09 13:57:14 -05001180}
1181
1182void SPIRVCodeGenerator::writeMatrixCopy(SpvId id, SpvId src, const Type& srcType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001183 const Type& dstType, OutputStream& out) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001184 SkASSERT(srcType.typeKind() == Type::TypeKind::kMatrix);
1185 SkASSERT(dstType.typeKind() == Type::TypeKind::kMatrix);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001186 SkASSERT(srcType.componentType() == dstType.componentType());
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001187 SpvId srcColumnType = this->getType(srcType.componentType().toCompound(fContext,
1188 srcType.rows(),
1189 1));
1190 SpvId dstColumnType = this->getType(dstType.componentType().toCompound(fContext,
1191 dstType.rows(),
1192 1));
1193 SpvId zeroId;
1194 if (dstType.componentType() == *fContext.fFloat_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001195 FloatLiteral zero(fContext, -1, 0.0);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001196 zeroId = this->writeFloatLiteral(zero);
1197 } else if (dstType.componentType() == *fContext.fInt_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001198 IntLiteral zero(fContext, -1, 0);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001199 zeroId = this->writeIntLiteral(zero);
1200 } else {
1201 ABORT("unsupported matrix component type");
1202 }
1203 SpvId zeroColumn = 0;
1204 SpvId columns[4];
1205 for (int i = 0; i < dstType.columns(); i++) {
1206 if (i < srcType.columns()) {
1207 // we're still inside the src matrix, copy the column
1208 SpvId srcColumn = this->nextId();
1209 this->writeInstruction(SpvOpCompositeExtract, srcColumnType, srcColumn, src, i, out);
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001210 this->writePrecisionModifier(dstType, srcColumn);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001211 SpvId dstColumn;
1212 if (srcType.rows() == dstType.rows()) {
1213 // columns are equal size, don't need to do anything
1214 dstColumn = srcColumn;
1215 }
1216 else if (dstType.rows() > srcType.rows()) {
1217 // dst column is bigger, need to zero-pad it
1218 dstColumn = this->nextId();
1219 int delta = dstType.rows() - srcType.rows();
1220 this->writeOpCode(SpvOpCompositeConstruct, 4 + delta, out);
1221 this->writeWord(dstColumnType, out);
1222 this->writeWord(dstColumn, out);
1223 this->writeWord(srcColumn, out);
John Stilesf621e232020-08-25 13:33:02 -04001224 for (int j = 0; j < delta; ++j) {
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001225 this->writeWord(zeroId, out);
1226 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001227 this->writePrecisionModifier(dstType, dstColumn);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001228 }
1229 else {
1230 // dst column is smaller, need to swizzle the src column
1231 dstColumn = this->nextId();
1232 int count = dstType.rows();
1233 this->writeOpCode(SpvOpVectorShuffle, 5 + count, out);
1234 this->writeWord(dstColumnType, out);
1235 this->writeWord(dstColumn, out);
1236 this->writeWord(srcColumn, out);
1237 this->writeWord(srcColumn, out);
John Stilesf621e232020-08-25 13:33:02 -04001238 for (int j = 0; j < count; j++) {
1239 this->writeWord(j, out);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001240 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001241 this->writePrecisionModifier(dstType, dstColumn);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001242 }
1243 columns[i] = dstColumn;
1244 } else {
1245 // we're past the end of the src matrix, need a vector of zeroes
1246 if (!zeroColumn) {
1247 zeroColumn = this->nextId();
1248 this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.rows(), out);
1249 this->writeWord(dstColumnType, out);
1250 this->writeWord(zeroColumn, out);
John Stilesf621e232020-08-25 13:33:02 -04001251 for (int j = 0; j < dstType.rows(); ++j) {
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001252 this->writeWord(zeroId, out);
1253 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001254 this->writePrecisionModifier(dstType, zeroColumn);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001255 }
1256 columns[i] = zeroColumn;
1257 }
1258 }
1259 this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.columns(), out);
1260 this->writeWord(this->getType(dstType), out);
1261 this->writeWord(id, out);
1262 for (int i = 0; i < dstType.columns(); i++) {
1263 this->writeWord(columns[i], out);
1264 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001265 this->writePrecisionModifier(dstType, id);
Ethan Nicholas84645e32017-02-09 13:57:14 -05001266}
1267
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001268void SPIRVCodeGenerator::addColumnEntry(SpvId columnType, Precision precision,
1269 std::vector<SpvId>* currentColumn,
Ethan Nicholas5c46b722019-03-22 14:32:37 -04001270 std::vector<SpvId>* columnIds,
1271 int* currentCount, int rows, SpvId entry,
1272 OutputStream& out) {
1273 SkASSERT(*currentCount < rows);
1274 ++(*currentCount);
1275 currentColumn->push_back(entry);
1276 if (*currentCount == rows) {
1277 *currentCount = 0;
1278 this->writeOpCode(SpvOpCompositeConstruct, 3 + currentColumn->size(), out);
1279 this->writeWord(columnType, out);
1280 SpvId columnId = this->nextId();
1281 this->writeWord(columnId, out);
1282 columnIds->push_back(columnId);
1283 for (SpvId id : *currentColumn) {
1284 this->writeWord(id, out);
1285 }
1286 currentColumn->clear();
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001287 this->writePrecisionModifier(precision, columnId);
Ethan Nicholas5c46b722019-03-22 14:32:37 -04001288 }
1289}
1290
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001291SpvId SPIRVCodeGenerator::writeMatrixConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001292 SkASSERT(c.fType.typeKind() == Type::TypeKind::kMatrix);
ethannicholasb3058bd2016-07-01 08:22:01 -07001293 // go ahead and write the arguments so we don't try to write new instructions in the middle of
1294 // an instruction
1295 std::vector<SpvId> arguments;
1296 for (size_t i = 0; i < c.fArguments.size(); i++) {
1297 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
1298 }
1299 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07001300 int rows = c.fType.rows();
1301 int columns = c.fType.columns();
Ethan Nicholase6592142020-09-08 10:22:09 -04001302 if (arguments.size() == 1 && c.fArguments[0]->fType.typeKind() == Type::TypeKind::kScalar) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05001303 this->writeUniformScaleMatrix(result, arguments[0], c.fType, out);
Ethan Nicholase6592142020-09-08 10:22:09 -04001304 } else if (arguments.size() == 1 &&
1305 c.fArguments[0]->fType.typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas84645e32017-02-09 13:57:14 -05001306 this->writeMatrixCopy(result, arguments[0], c.fArguments[0]->fType, c.fType, out);
Ethan Nicholase6592142020-09-08 10:22:09 -04001307 } else if (arguments.size() == 1 &&
1308 c.fArguments[0]->fType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001309 SkASSERT(c.fType.rows() == 2 && c.fType.columns() == 2);
1310 SkASSERT(c.fArguments[0]->fType.columns() == 4);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001311 SpvId componentType = this->getType(c.fType.componentType());
1312 SpvId v[4];
1313 for (int i = 0; i < 4; ++i) {
1314 v[i] = this->nextId();
1315 this->writeInstruction(SpvOpCompositeExtract, componentType, v[i], arguments[0], i, out);
1316 }
1317 SpvId columnType = this->getType(c.fType.componentType().toCompound(fContext, 2, 1));
1318 SpvId column1 = this->nextId();
1319 this->writeInstruction(SpvOpCompositeConstruct, columnType, column1, v[0], v[1], out);
1320 SpvId column2 = this->nextId();
1321 this->writeInstruction(SpvOpCompositeConstruct, columnType, column2, v[2], v[3], out);
1322 this->writeInstruction(SpvOpCompositeConstruct, this->getType(c.fType), result, column1,
1323 column2, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001324 } else {
Ethan Nicholas5c46b722019-03-22 14:32:37 -04001325 SpvId columnType = this->getType(c.fType.componentType().toCompound(fContext, rows, 1));
ethannicholasb3058bd2016-07-01 08:22:01 -07001326 std::vector<SpvId> columnIds;
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001327 // ids of vectors and scalars we have written to the current column so far
1328 std::vector<SpvId> currentColumn;
1329 // the total number of scalars represented by currentColumn's entries
ethannicholasb3058bd2016-07-01 08:22:01 -07001330 int currentCount = 0;
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001331 Precision precision = c.fType.highPrecision() ? Precision::kHigh : Precision::kLow;
ethannicholasb3058bd2016-07-01 08:22:01 -07001332 for (size_t i = 0; i < arguments.size(); i++) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001333 if (currentCount == 0 &&
1334 c.fArguments[i]->fType.typeKind() == Type::TypeKind::kVector &&
1335 c.fArguments[i]->fType.columns() == c.fType.rows()) {
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001336 // this is a complete column by itself
ethannicholasb3058bd2016-07-01 08:22:01 -07001337 columnIds.push_back(arguments[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001338 } else {
Ethan Nicholasbe850ad2018-04-27 10:36:31 -04001339 if (c.fArguments[i]->fType.columns() == 1) {
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001340 this->addColumnEntry(columnType, precision, &currentColumn, &columnIds,
1341 &currentCount, rows, arguments[i], out);
Ethan Nicholasbe850ad2018-04-27 10:36:31 -04001342 } else {
1343 SpvId componentType = this->getType(c.fArguments[i]->fType.componentType());
Ethan Nicholas811b9442018-05-11 13:16:03 -04001344 for (int j = 0; j < c.fArguments[i]->fType.columns(); ++j) {
Ethan Nicholasbe850ad2018-04-27 10:36:31 -04001345 SpvId swizzle = this->nextId();
1346 this->writeInstruction(SpvOpCompositeExtract, componentType, swizzle,
1347 arguments[i], j, out);
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001348 this->addColumnEntry(columnType, precision, &currentColumn, &columnIds,
1349 &currentCount, rows, swizzle, out);
Ethan Nicholasbe850ad2018-04-27 10:36:31 -04001350 }
1351 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001352 }
1353 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001354 SkASSERT(columnIds.size() == (size_t) columns);
ethannicholasb3058bd2016-07-01 08:22:01 -07001355 this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, out);
ethannicholasd598f792016-07-25 10:08:54 -07001356 this->writeWord(this->getType(c.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001357 this->writeWord(result, out);
1358 for (SpvId id : columnIds) {
1359 this->writeWord(id, out);
1360 }
1361 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04001362 this->writePrecisionModifier(c.fType, result);
ethannicholasb3058bd2016-07-01 08:22:01 -07001363 return result;
1364}
1365
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001366SpvId SPIRVCodeGenerator::writeVectorConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001367 SkASSERT(c.fType.typeKind() == Type::TypeKind::kVector);
Brian Osmanb6b95732020-06-30 11:44:27 -04001368 if (c.isCompileTimeConstant()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001369 return this->writeConstantVector(c);
1370 }
1371 // go ahead and write the arguments so we don't try to write new instructions in the middle of
1372 // an instruction
1373 std::vector<SpvId> arguments;
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001374 for (size_t i = 0; i < c.fArguments.size(); i++) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001375 if (c.fArguments[i]->fType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001376 // SPIR-V doesn't support vector(vector-of-different-type) directly, so we need to
1377 // extract the components and convert them in that case manually. On top of that,
1378 // as of this writing there's a bug in the Intel Vulkan driver where OpCreateComposite
1379 // doesn't handle vector arguments at all, so we always extract vector components and
1380 // pass them into OpCreateComposite individually.
1381 SpvId vec = this->writeExpression(*c.fArguments[i], out);
1382 SpvOp_ op = SpvOpUndef;
1383 const Type& src = c.fArguments[i]->fType.componentType();
1384 const Type& dst = c.fType.componentType();
1385 if (dst == *fContext.fFloat_Type || dst == *fContext.fHalf_Type) {
1386 if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) {
1387 if (c.fArguments.size() == 1) {
1388 return vec;
1389 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001390 } else if (src == *fContext.fInt_Type ||
1391 src == *fContext.fShort_Type ||
1392 src == *fContext.fByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001393 op = SpvOpConvertSToF;
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001394 } else if (src == *fContext.fUInt_Type ||
1395 src == *fContext.fUShort_Type ||
1396 src == *fContext.fUByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001397 op = SpvOpConvertUToF;
1398 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001399 SkASSERT(false);
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001400 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001401 } else if (dst == *fContext.fInt_Type ||
1402 dst == *fContext.fShort_Type ||
1403 dst == *fContext.fByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001404 if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) {
1405 op = SpvOpConvertFToS;
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 if (c.fArguments.size() == 1) {
1410 return vec;
1411 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001412 } else if (src == *fContext.fUInt_Type ||
1413 src == *fContext.fUShort_Type ||
1414 src == *fContext.fUByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001415 op = SpvOpBitcast;
1416 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001417 SkASSERT(false);
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001418 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001419 } else if (dst == *fContext.fUInt_Type ||
1420 dst == *fContext.fUShort_Type ||
1421 dst == *fContext.fUByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001422 if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) {
1423 op = SpvOpConvertFToS;
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001424 } else if (src == *fContext.fInt_Type ||
1425 src == *fContext.fShort_Type ||
1426 src == *fContext.fByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001427 op = SpvOpBitcast;
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 if (c.fArguments.size() == 1) {
1432 return vec;
1433 }
1434 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001435 SkASSERT(false);
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001436 }
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001437 }
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001438 for (int j = 0; j < c.fArguments[i]->fType.columns(); j++) {
1439 SpvId swizzle = this->nextId();
1440 this->writeInstruction(SpvOpCompositeExtract, this->getType(src), swizzle, vec, j,
1441 out);
1442 if (op != SpvOpUndef) {
1443 SpvId cast = this->nextId();
1444 this->writeInstruction(op, this->getType(dst), cast, swizzle, out);
1445 arguments.push_back(cast);
1446 } else {
1447 arguments.push_back(swizzle);
1448 }
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001449 }
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001450 } else {
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001451 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
1452 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001453 }
1454 SpvId result = this->nextId();
Ethan Nicholase6592142020-09-08 10:22:09 -04001455 if (arguments.size() == 1 && c.fArguments[0]->fType.typeKind() == Type::TypeKind::kScalar) {
ethannicholasd598f792016-07-25 10:08:54 -07001456 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType.columns(), out);
1457 this->writeWord(this->getType(c.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001458 this->writeWord(result, out);
ethannicholasd598f792016-07-25 10:08:54 -07001459 for (int i = 0; i < c.fType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001460 this->writeWord(arguments[0], out);
1461 }
1462 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001463 SkASSERT(arguments.size() > 1);
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001464 this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) arguments.size(), out);
ethannicholasd598f792016-07-25 10:08:54 -07001465 this->writeWord(this->getType(c.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001466 this->writeWord(result, out);
1467 for (SpvId id : arguments) {
1468 this->writeWord(id, out);
1469 }
1470 }
1471 return result;
1472}
1473
Ethan Nicholasbd553222017-07-18 15:54:59 -04001474SpvId SPIRVCodeGenerator::writeArrayConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001475 SkASSERT(c.fType.typeKind() == Type::TypeKind::kArray);
Ethan Nicholasbd553222017-07-18 15:54:59 -04001476 // go ahead and write the arguments so we don't try to write new instructions in the middle of
1477 // an instruction
1478 std::vector<SpvId> arguments;
1479 for (size_t i = 0; i < c.fArguments.size(); i++) {
1480 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
1481 }
1482 SpvId result = this->nextId();
1483 this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) c.fArguments.size(), out);
1484 this->writeWord(this->getType(c.fType), out);
1485 this->writeWord(result, out);
1486 for (SpvId id : arguments) {
1487 this->writeWord(id, out);
1488 }
1489 return result;
1490}
1491
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001492SpvId SPIRVCodeGenerator::writeConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001493 if (c.fArguments.size() == 1 &&
1494 this->getActualType(c.fType) == this->getActualType(c.fArguments[0]->fType)) {
1495 return this->writeExpression(*c.fArguments[0], out);
1496 }
1497 if (c.fType == *fContext.fFloat_Type || c.fType == *fContext.fHalf_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001498 return this->writeFloatConstructor(c, out);
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001499 } else if (c.fType == *fContext.fInt_Type ||
1500 c.fType == *fContext.fShort_Type ||
1501 c.fType == *fContext.fByte_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001502 return this->writeIntConstructor(c, out);
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001503 } else if (c.fType == *fContext.fUInt_Type ||
1504 c.fType == *fContext.fUShort_Type ||
1505 c.fType == *fContext.fUByte_Type) {
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001506 return this->writeUIntConstructor(c, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001507 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001508 switch (c.fType.typeKind()) {
1509 case Type::TypeKind::kVector:
ethannicholasb3058bd2016-07-01 08:22:01 -07001510 return this->writeVectorConstructor(c, out);
Ethan Nicholase6592142020-09-08 10:22:09 -04001511 case Type::TypeKind::kMatrix:
ethannicholasb3058bd2016-07-01 08:22:01 -07001512 return this->writeMatrixConstructor(c, out);
Ethan Nicholase6592142020-09-08 10:22:09 -04001513 case Type::TypeKind::kArray:
Ethan Nicholasbd553222017-07-18 15:54:59 -04001514 return this->writeArrayConstructor(c, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001515 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001516#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07001517 ABORT("unsupported constructor: %s", c.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05001518#endif
1519 return -1;
ethannicholasb3058bd2016-07-01 08:22:01 -07001520 }
1521}
1522
1523SpvStorageClass_ get_storage_class(const Modifiers& modifiers) {
1524 if (modifiers.fFlags & Modifiers::kIn_Flag) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001525 SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag));
ethannicholasb3058bd2016-07-01 08:22:01 -07001526 return SpvStorageClassInput;
1527 } else if (modifiers.fFlags & Modifiers::kOut_Flag) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001528 SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag));
ethannicholasb3058bd2016-07-01 08:22:01 -07001529 return SpvStorageClassOutput;
1530 } else if (modifiers.fFlags & Modifiers::kUniform_Flag) {
Ethan Nicholas39204fd2017-11-27 13:12:30 -05001531 if (modifiers.fLayout.fFlags & Layout::kPushConstant_Flag) {
ethannicholas8ac838d2016-11-22 08:39:36 -08001532 return SpvStorageClassPushConstant;
1533 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001534 return SpvStorageClassUniform;
1535 } else {
1536 return SpvStorageClassFunction;
1537 }
1538}
1539
ethannicholasf789b382016-08-03 12:43:36 -07001540SpvStorageClass_ get_storage_class(const Expression& expr) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001541 switch (expr.kind()) {
1542 case Expression::Kind::kVariableReference: {
Ethan Nicholasc6f5e102017-03-31 14:53:17 -04001543 const Variable& var = ((VariableReference&) expr).fVariable;
1544 if (var.fStorage != Variable::kGlobal_Storage) {
1545 return SpvStorageClassFunction;
1546 }
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001547 SpvStorageClass_ result = get_storage_class(var.fModifiers);
1548 if (result == SpvStorageClassFunction) {
1549 result = SpvStorageClassPrivate;
1550 }
1551 return result;
Ethan Nicholasc6f5e102017-03-31 14:53:17 -04001552 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001553 case Expression::Kind::kFieldAccess:
ethannicholasb3058bd2016-07-01 08:22:01 -07001554 return get_storage_class(*((FieldAccess&) expr).fBase);
Ethan Nicholase6592142020-09-08 10:22:09 -04001555 case Expression::Kind::kIndex:
ethannicholasb3058bd2016-07-01 08:22:01 -07001556 return get_storage_class(*((IndexExpression&) expr).fBase);
1557 default:
1558 return SpvStorageClassFunction;
1559 }
1560}
1561
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001562std::vector<SpvId> SPIRVCodeGenerator::getAccessChain(const Expression& expr, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001563 std::vector<SpvId> chain;
Ethan Nicholase6592142020-09-08 10:22:09 -04001564 switch (expr.kind()) {
1565 case Expression::Kind::kIndex: {
ethannicholasb3058bd2016-07-01 08:22:01 -07001566 IndexExpression& indexExpr = (IndexExpression&) expr;
1567 chain = this->getAccessChain(*indexExpr.fBase, out);
1568 chain.push_back(this->writeExpression(*indexExpr.fIndex, out));
1569 break;
1570 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001571 case Expression::Kind::kFieldAccess: {
ethannicholasb3058bd2016-07-01 08:22:01 -07001572 FieldAccess& fieldExpr = (FieldAccess&) expr;
1573 chain = this->getAccessChain(*fieldExpr.fBase, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001574 IntLiteral index(fContext, -1, fieldExpr.fFieldIndex);
ethannicholasb3058bd2016-07-01 08:22:01 -07001575 chain.push_back(this->writeIntLiteral(index));
1576 break;
1577 }
Ethan Nicholasa9a06902019-01-07 14:42:40 -05001578 default: {
1579 SpvId id = this->getLValue(expr, out)->getPointer();
1580 SkASSERT(id != 0);
1581 chain.push_back(id);
1582 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001583 }
1584 return chain;
1585}
1586
1587class PointerLValue : public SPIRVCodeGenerator::LValue {
1588public:
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001589 PointerLValue(SPIRVCodeGenerator& gen, SpvId pointer, SpvId type,
1590 SPIRVCodeGenerator::Precision precision)
ethannicholasb3058bd2016-07-01 08:22:01 -07001591 : fGen(gen)
1592 , fPointer(pointer)
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001593 , fType(type)
1594 , fPrecision(precision) {}
ethannicholasb3058bd2016-07-01 08:22:01 -07001595
John Stiles1cf2c8d2020-08-13 22:58:04 -04001596 SpvId getPointer() override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001597 return fPointer;
1598 }
1599
John Stiles1cf2c8d2020-08-13 22:58:04 -04001600 SpvId load(OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001601 SpvId result = fGen.nextId();
1602 fGen.writeInstruction(SpvOpLoad, fType, result, fPointer, out);
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001603 fGen.writePrecisionModifier(fPrecision, result);
ethannicholasb3058bd2016-07-01 08:22:01 -07001604 return result;
1605 }
1606
John Stiles1cf2c8d2020-08-13 22:58:04 -04001607 void store(SpvId value, OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001608 fGen.writeInstruction(SpvOpStore, fPointer, value, out);
1609 }
1610
1611private:
1612 SPIRVCodeGenerator& fGen;
1613 const SpvId fPointer;
1614 const SpvId fType;
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001615 const SPIRVCodeGenerator::Precision fPrecision;
ethannicholasb3058bd2016-07-01 08:22:01 -07001616};
1617
1618class SwizzleLValue : public SPIRVCodeGenerator::LValue {
1619public:
Greg Daniel64773e62016-11-22 09:44:03 -05001620 SwizzleLValue(SPIRVCodeGenerator& gen, SpvId vecPointer, const std::vector<int>& components,
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001621 const Type& baseType, const Type& swizzleType,
1622 SPIRVCodeGenerator::Precision precision)
ethannicholasb3058bd2016-07-01 08:22:01 -07001623 : fGen(gen)
1624 , fVecPointer(vecPointer)
1625 , fComponents(components)
1626 , fBaseType(baseType)
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001627 , fSwizzleType(swizzleType)
1628 , fPrecision(precision) {}
ethannicholasb3058bd2016-07-01 08:22:01 -07001629
John Stiles1cf2c8d2020-08-13 22:58:04 -04001630 SpvId getPointer() override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001631 return 0;
1632 }
1633
John Stiles1cf2c8d2020-08-13 22:58:04 -04001634 SpvId load(OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001635 SpvId base = fGen.nextId();
1636 fGen.writeInstruction(SpvOpLoad, fGen.getType(fBaseType), base, fVecPointer, out);
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001637 fGen.writePrecisionModifier(fPrecision, base);
ethannicholasb3058bd2016-07-01 08:22:01 -07001638 SpvId result = fGen.nextId();
1639 fGen.writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) fComponents.size(), out);
1640 fGen.writeWord(fGen.getType(fSwizzleType), out);
1641 fGen.writeWord(result, out);
1642 fGen.writeWord(base, out);
1643 fGen.writeWord(base, out);
1644 for (int component : fComponents) {
1645 fGen.writeWord(component, out);
1646 }
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001647 fGen.writePrecisionModifier(fPrecision, result);
ethannicholasb3058bd2016-07-01 08:22:01 -07001648 return result;
1649 }
1650
John Stiles1cf2c8d2020-08-13 22:58:04 -04001651 void store(SpvId value, OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001652 // use OpVectorShuffle to mix and match the vector components. We effectively create
1653 // a virtual vector out of the concatenation of the left and right vectors, and then
Greg Daniel64773e62016-11-22 09:44:03 -05001654 // select components from this virtual vector to make the result vector. For
ethannicholasb3058bd2016-07-01 08:22:01 -07001655 // instance, given:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001656 // float3L = ...;
1657 // float3R = ...;
ethannicholasb3058bd2016-07-01 08:22:01 -07001658 // L.xz = R.xy;
Greg Daniel64773e62016-11-22 09:44:03 -05001659 // 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 -07001660 // our result vector to look like (R.x, L.y, R.y), so we need to select indices
1661 // (3, 1, 4).
1662 SpvId base = fGen.nextId();
1663 fGen.writeInstruction(SpvOpLoad, fGen.getType(fBaseType), base, fVecPointer, out);
1664 SpvId shuffle = fGen.nextId();
1665 fGen.writeOpCode(SpvOpVectorShuffle, 5 + fBaseType.columns(), out);
1666 fGen.writeWord(fGen.getType(fBaseType), out);
1667 fGen.writeWord(shuffle, out);
1668 fGen.writeWord(base, out);
1669 fGen.writeWord(value, out);
1670 for (int i = 0; i < fBaseType.columns(); i++) {
1671 // current offset into the virtual vector, defaults to pulling the unmodified
1672 // value from the left side
1673 int offset = i;
1674 // check to see if we are writing this component
1675 for (size_t j = 0; j < fComponents.size(); j++) {
1676 if (fComponents[j] == i) {
Greg Daniel64773e62016-11-22 09:44:03 -05001677 // we're writing to this component, so adjust the offset to pull from
ethannicholasb3058bd2016-07-01 08:22:01 -07001678 // the correct component of the right side instead of preserving the
1679 // value from the left
1680 offset = (int) (j + fBaseType.columns());
1681 break;
1682 }
1683 }
1684 fGen.writeWord(offset, out);
1685 }
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001686 fGen.writePrecisionModifier(fPrecision, shuffle);
ethannicholasb3058bd2016-07-01 08:22:01 -07001687 fGen.writeInstruction(SpvOpStore, fVecPointer, shuffle, out);
1688 }
1689
1690private:
1691 SPIRVCodeGenerator& fGen;
1692 const SpvId fVecPointer;
1693 const std::vector<int>& fComponents;
1694 const Type& fBaseType;
1695 const Type& fSwizzleType;
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001696 const SPIRVCodeGenerator::Precision fPrecision;
ethannicholasb3058bd2016-07-01 08:22:01 -07001697};
1698
Greg Daniel64773e62016-11-22 09:44:03 -05001699std::unique_ptr<SPIRVCodeGenerator::LValue> SPIRVCodeGenerator::getLValue(const Expression& expr,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001700 OutputStream& out) {
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001701 Precision precision = expr.fType.highPrecision() ? Precision::kHigh : Precision::kLow;
Ethan Nicholase6592142020-09-08 10:22:09 -04001702 switch (expr.kind()) {
1703 case Expression::Kind::kVariableReference: {
Ethan Nicholas5226b772018-05-03 16:20:41 -04001704 SpvId type;
ethannicholasd598f792016-07-25 10:08:54 -07001705 const Variable& var = ((VariableReference&) expr).fVariable;
Ethan Nicholas5226b772018-05-03 16:20:41 -04001706 if (var.fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001707 type = this->getType(Type("sk_in", Type::TypeKind::kArray,
1708 var.fType.componentType(), fSkInCount));
Ethan Nicholas5226b772018-05-03 16:20:41 -04001709 } else {
1710 type = this->getType(expr.fType);
1711 }
ethannicholasd598f792016-07-25 10:08:54 -07001712 auto entry = fVariableMap.find(&var);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001713 SkASSERT(entry != fVariableMap.end());
Ethan Nicholas5226b772018-05-03 16:20:41 -04001714 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(*this,
1715 entry->second,
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001716 type,
1717 precision));
ethannicholasb3058bd2016-07-01 08:22:01 -07001718 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001719 case Expression::Kind::kIndex: // fall through
1720 case Expression::Kind::kFieldAccess: {
ethannicholasb3058bd2016-07-01 08:22:01 -07001721 std::vector<SpvId> chain = this->getAccessChain(expr, out);
1722 SpvId member = this->nextId();
1723 this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out);
Greg Daniel64773e62016-11-22 09:44:03 -05001724 this->writeWord(this->getPointerType(expr.fType, get_storage_class(expr)), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001725 this->writeWord(member, out);
1726 for (SpvId idx : chain) {
1727 this->writeWord(idx, out);
1728 }
1729 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001730 *this,
1731 member,
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001732 this->getType(expr.fType),
1733 precision));
ethannicholasb3058bd2016-07-01 08:22:01 -07001734 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001735 case Expression::Kind::kSwizzle: {
ethannicholasb3058bd2016-07-01 08:22:01 -07001736 Swizzle& swizzle = (Swizzle&) expr;
1737 size_t count = swizzle.fComponents.size();
1738 SpvId base = this->getLValue(*swizzle.fBase, out)->getPointer();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001739 SkASSERT(base);
ethannicholasb3058bd2016-07-01 08:22:01 -07001740 if (count == 1) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001741 IntLiteral index(fContext, -1, swizzle.fComponents[0]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001742 SpvId member = this->nextId();
1743 this->writeInstruction(SpvOpAccessChain,
Greg Daniel64773e62016-11-22 09:44:03 -05001744 this->getPointerType(swizzle.fType,
1745 get_storage_class(*swizzle.fBase)),
1746 member,
1747 base,
1748 this->writeIntLiteral(index),
ethannicholasb3058bd2016-07-01 08:22:01 -07001749 out);
1750 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
1751 *this,
Greg Daniel64773e62016-11-22 09:44:03 -05001752 member,
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001753 this->getType(expr.fType),
1754 precision));
ethannicholasb3058bd2016-07-01 08:22:01 -07001755 } else {
1756 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new SwizzleLValue(
Greg Daniel64773e62016-11-22 09:44:03 -05001757 *this,
1758 base,
1759 swizzle.fComponents,
ethannicholasd598f792016-07-25 10:08:54 -07001760 swizzle.fBase->fType,
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001761 expr.fType,
1762 precision));
ethannicholasb3058bd2016-07-01 08:22:01 -07001763 }
1764 }
Ethan Nicholase6592142020-09-08 10:22:09 -04001765 case Expression::Kind::kTernary: {
Ethan Nicholasa583b812018-01-18 13:32:11 -05001766 TernaryExpression& t = (TernaryExpression&) expr;
1767 SpvId test = this->writeExpression(*t.fTest, out);
1768 SpvId end = this->nextId();
1769 SpvId ifTrueLabel = this->nextId();
1770 SpvId ifFalseLabel = this->nextId();
1771 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
1772 this->writeInstruction(SpvOpBranchConditional, test, ifTrueLabel, ifFalseLabel, out);
1773 this->writeLabel(ifTrueLabel, out);
1774 SpvId ifTrue = this->getLValue(*t.fIfTrue, out)->getPointer();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001775 SkASSERT(ifTrue);
Ethan Nicholasa583b812018-01-18 13:32:11 -05001776 this->writeInstruction(SpvOpBranch, end, out);
1777 ifTrueLabel = fCurrentBlock;
1778 SpvId ifFalse = this->getLValue(*t.fIfFalse, out)->getPointer();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001779 SkASSERT(ifFalse);
Ethan Nicholasa583b812018-01-18 13:32:11 -05001780 ifFalseLabel = fCurrentBlock;
1781 this->writeInstruction(SpvOpBranch, end, out);
1782 SpvId result = this->nextId();
1783 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, ifTrue,
1784 ifTrueLabel, ifFalse, ifFalseLabel, out);
1785 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
1786 *this,
1787 result,
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001788 this->getType(expr.fType),
1789 precision));
Ethan Nicholasa583b812018-01-18 13:32:11 -05001790 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001791 default:
1792 // expr isn't actually an lvalue, create a dummy variable for it. This case happens due
Greg Daniel64773e62016-11-22 09:44:03 -05001793 // to the need to store values in temporary variables during function calls (see
ethannicholasb3058bd2016-07-01 08:22:01 -07001794 // comments in getFunctionType); erroneous uses of rvalues as lvalues should have been
1795 // caught by IRGenerator
1796 SpvId result = this->nextId();
1797 SpvId type = this->getPointerType(expr.fType, SpvStorageClassFunction);
ethannicholasd598f792016-07-25 10:08:54 -07001798 this->writeInstruction(SpvOpVariable, type, result, SpvStorageClassFunction,
1799 fVariableBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07001800 this->writeInstruction(SpvOpStore, result, this->writeExpression(expr, out), out);
1801 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
1802 *this,
Greg Daniel64773e62016-11-22 09:44:03 -05001803 result,
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001804 this->getType(expr.fType),
1805 precision));
ethannicholasb3058bd2016-07-01 08:22:01 -07001806 }
1807}
1808
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001809SpvId SPIRVCodeGenerator::writeVariableReference(const VariableReference& ref, OutputStream& out) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001810 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07001811 auto entry = fVariableMap.find(&ref.fVariable);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001812 SkASSERT(entry != fVariableMap.end());
ethannicholasb3058bd2016-07-01 08:22:01 -07001813 SpvId var = entry->second;
ethannicholasd598f792016-07-25 10:08:54 -07001814 this->writeInstruction(SpvOpLoad, this->getType(ref.fVariable.fType), result, var, out);
Ethan Nicholas10e93b62019-03-20 10:46:14 -04001815 this->writePrecisionModifier(ref.fVariable.fType, result);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001816 if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN &&
Greg Daniela85e4bf2020-06-17 16:32:45 -04001817 (fProgram.fSettings.fFlipY || fProgram.fSettings.fInverseW)) {
1818 // The x component never changes, so just grab it
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001819 SpvId xId = this->nextId();
1820 this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), xId,
1821 result, 0, out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001822
1823 // Calculate the y component which may need to be flipped
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001824 SpvId rawYId = this->nextId();
1825 this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), rawYId,
1826 result, 1, out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001827 SpvId flippedYId = 0;
1828 if (fProgram.fSettings.fFlipY) {
1829 // need to remap to a top-left coordinate system
1830 if (fRTHeightStructId == (SpvId)-1) {
1831 // height variable hasn't been written yet
1832 std::shared_ptr<SymbolTable> st(new SymbolTable(&fErrors));
1833 SkASSERT(fRTHeightFieldIndex == (SpvId)-1);
1834 std::vector<Type::Field> fields;
1835 SkASSERT(fProgram.fSettings.fRTHeightOffset >= 0);
1836 fields.emplace_back(
1837 Modifiers(Layout(0, -1, fProgram.fSettings.fRTHeightOffset, -1, -1, -1, -1,
1838 -1, Layout::Format::kUnspecified,
1839 Layout::kUnspecified_Primitive, 1, -1, "", "",
1840 Layout::kNo_Key, Layout::CType::kDefault),
1841 0),
1842 SKSL_RTHEIGHT_NAME, fContext.fFloat_Type.get());
1843 StringFragment name("sksl_synthetic_uniforms");
1844 Type intfStruct(-1, name, fields);
1845
1846 int binding = fProgram.fSettings.fRTHeightBinding;
1847 int set = fProgram.fSettings.fRTHeightSet;
1848 SkASSERT(binding != -1 && set != -1);
1849
1850 Layout layout(0, -1, -1, binding, -1, set, -1, -1, Layout::Format::kUnspecified,
1851 Layout::kUnspecified_Primitive, -1, -1, "", "", Layout::kNo_Key,
1852 Layout::CType::kDefault);
John Stiles3ae071e2020-08-05 15:29:29 -04001853 const Variable* intfVar = fSynthetics.takeOwnershipOfSymbol(
1854 std::make_unique<Variable>(/*offset=*/-1,
1855 Modifiers(layout, Modifiers::kUniform_Flag),
1856 name,
1857 intfStruct,
1858 Variable::kGlobal_Storage));
Greg Daniela85e4bf2020-06-17 16:32:45 -04001859 InterfaceBlock intf(-1, intfVar, name, String(""),
1860 std::vector<std::unique_ptr<Expression>>(), st);
Stephen White88574972020-06-23 19:09:29 -04001861
1862 fRTHeightStructId = this->writeInterfaceBlock(intf, false);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001863 fRTHeightFieldIndex = 0;
1864 }
1865 SkASSERT(fRTHeightFieldIndex != (SpvId)-1);
1866
1867 IntLiteral fieldIndex(fContext, -1, fRTHeightFieldIndex);
1868 SpvId fieldIndexId = this->writeIntLiteral(fieldIndex);
1869 SpvId heightPtr = this->nextId();
1870 this->writeOpCode(SpvOpAccessChain, 5, out);
1871 this->writeWord(this->getPointerType(*fContext.fFloat_Type, SpvStorageClassUniform),
1872 out);
1873 this->writeWord(heightPtr, out);
1874 this->writeWord(fRTHeightStructId, out);
1875 this->writeWord(fieldIndexId, out);
1876 SpvId heightRead = this->nextId();
1877 this->writeInstruction(SpvOpLoad, this->getType(*fContext.fFloat_Type), heightRead,
1878 heightPtr, out);
1879
1880 flippedYId = this->nextId();
1881 this->writeInstruction(SpvOpFSub, this->getType(*fContext.fFloat_Type), flippedYId,
1882 heightRead, rawYId, out);
1883 }
1884
1885 // The z component will always be zero so we just get an id to the 0 literal
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001886 FloatLiteral zero(fContext, -1, 0.0);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001887 SpvId zeroId = writeFloatLiteral(zero);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001888
1889 // Calculate the w component which may need to be inverted
1890 SpvId rawWId = this->nextId();
1891 this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), rawWId,
Ethan Nicholas20798e52018-12-04 12:30:50 -05001892 result, 3, out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001893 SpvId invWId = 0;
1894 if (fProgram.fSettings.fInverseW) {
1895 // We need to invert w
1896 FloatLiteral one(fContext, -1, 1.0);
1897 SpvId oneId = writeFloatLiteral(one);
1898 invWId = this->nextId();
1899 this->writeInstruction(SpvOpFDiv, this->getType(*fContext.fFloat_Type), invWId, oneId,
1900 rawWId, out);
1901 }
1902
1903 // Fill in the new fragcoord with the components from above
1904 SpvId adjusted = this->nextId();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001905 this->writeOpCode(SpvOpCompositeConstruct, 7, out);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001906 this->writeWord(this->getType(*fContext.fFloat4_Type), out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001907 this->writeWord(adjusted, out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001908 this->writeWord(xId, out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001909 if (fProgram.fSettings.fFlipY) {
1910 this->writeWord(flippedYId, out);
1911 } else {
1912 this->writeWord(rawYId, out);
1913 }
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001914 this->writeWord(zeroId, out);
Greg Daniela85e4bf2020-06-17 16:32:45 -04001915 if (fProgram.fSettings.fInverseW) {
1916 this->writeWord(invWId, out);
1917 } else {
1918 this->writeWord(rawWId, out);
1919 }
1920
1921 return adjusted;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001922 }
Chris Daltonb91c4662018-08-01 10:46:22 -06001923 if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_CLOCKWISE_BUILTIN &&
1924 !fProgram.fSettings.fFlipY) {
1925 // FrontFacing in Vulkan is defined in terms of a top-down render target. In skia, we use
1926 // the default convention of "counter-clockwise face is front".
1927 SpvId inverse = this->nextId();
1928 this->writeInstruction(SpvOpLogicalNot, this->getType(*fContext.fBool_Type), inverse,
1929 result, out);
1930 return inverse;
1931 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001932 return result;
1933}
1934
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001935SpvId SPIRVCodeGenerator::writeIndexExpression(const IndexExpression& expr, OutputStream& out) {
Ethan Nicholase6592142020-09-08 10:22:09 -04001936 if (expr.fBase->fType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholasa9a06902019-01-07 14:42:40 -05001937 SpvId base = this->writeExpression(*expr.fBase, out);
1938 SpvId index = this->writeExpression(*expr.fIndex, out);
1939 SpvId result = this->nextId();
1940 this->writeInstruction(SpvOpVectorExtractDynamic, this->getType(expr.fType), result, base,
1941 index, out);
1942 return result;
1943 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001944 return getLValue(expr, out)->load(out);
1945}
1946
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001947SpvId SPIRVCodeGenerator::writeFieldAccess(const FieldAccess& f, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001948 return getLValue(f, out)->load(out);
1949}
1950
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001951SpvId SPIRVCodeGenerator::writeSwizzle(const Swizzle& swizzle, OutputStream& out) {
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001952 SpvId base = this->writeExpression(*swizzle.fBase, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001953 SpvId result = this->nextId();
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001954 size_t count = swizzle.fComponents.size();
ethannicholasb3058bd2016-07-01 08:22:01 -07001955 if (count == 1) {
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001956 this->writeInstruction(SpvOpCompositeExtract, this->getType(swizzle.fType), result, base,
1957 swizzle.fComponents[0], out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001958 } else {
1959 this->writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) count, out);
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001960 this->writeWord(this->getType(swizzle.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001961 this->writeWord(result, out);
1962 this->writeWord(base, out);
Ethan Nicholase455f652019-09-13 12:52:55 -04001963 SpvId other = base;
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001964 for (int c : swizzle.fComponents) {
Ethan Nicholase455f652019-09-13 12:52:55 -04001965 if (c < 0) {
1966 if (!fConstantZeroOneVector) {
1967 FloatLiteral zero(fContext, -1, 0);
1968 SpvId zeroId = this->writeFloatLiteral(zero);
1969 FloatLiteral one(fContext, -1, 1);
1970 SpvId oneId = this->writeFloatLiteral(one);
1971 SpvId type = this->getType(*fContext.fFloat2_Type);
1972 fConstantZeroOneVector = this->nextId();
1973 this->writeOpCode(SpvOpConstantComposite, 5, fConstantBuffer);
1974 this->writeWord(type, fConstantBuffer);
1975 this->writeWord(fConstantZeroOneVector, fConstantBuffer);
1976 this->writeWord(zeroId, fConstantBuffer);
1977 this->writeWord(oneId, fConstantBuffer);
1978 }
1979 other = fConstantZeroOneVector;
1980 break;
Ethan Nicholasac285b12019-02-12 16:05:18 -05001981 }
Ethan Nicholasac285b12019-02-12 16:05:18 -05001982 }
1983 this->writeWord(other, out);
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001984 for (int component : swizzle.fComponents) {
Ethan Nicholasac285b12019-02-12 16:05:18 -05001985 if (component == SKSL_SWIZZLE_0) {
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001986 this->writeWord(swizzle.fBase->fType.columns(), out);
Ethan Nicholasac285b12019-02-12 16:05:18 -05001987 } else if (component == SKSL_SWIZZLE_1) {
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04001988 this->writeWord(swizzle.fBase->fType.columns() + 1, out);
Ethan Nicholasac285b12019-02-12 16:05:18 -05001989 } else {
1990 this->writeWord(component, out);
1991 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001992 }
1993 }
1994 return result;
1995}
1996
Greg Daniel64773e62016-11-22 09:44:03 -05001997SpvId SPIRVCodeGenerator::writeBinaryOperation(const Type& resultType,
1998 const Type& operandType, SpvId lhs,
1999 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002000 SpvOp_ ifUInt, SpvOp_ ifBool, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002001 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002002 if (is_float(fContext, operandType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002003 this->writeInstruction(ifFloat, this->getType(resultType), result, lhs, rhs, out);
ethannicholasd598f792016-07-25 10:08:54 -07002004 } else if (is_signed(fContext, operandType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002005 this->writeInstruction(ifInt, this->getType(resultType), result, lhs, rhs, out);
ethannicholasd598f792016-07-25 10:08:54 -07002006 } else if (is_unsigned(fContext, operandType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002007 this->writeInstruction(ifUInt, this->getType(resultType), result, lhs, rhs, out);
ethannicholasd598f792016-07-25 10:08:54 -07002008 } else if (operandType == *fContext.fBool_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002009 this->writeInstruction(ifBool, this->getType(resultType), result, lhs, rhs, out);
Ethan Nicholas858fecc2019-03-07 13:19:18 -05002010 return result; // skip RelaxedPrecision check
ethannicholasb3058bd2016-07-01 08:22:01 -07002011 } else {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002012#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07002013 ABORT("invalid operandType: %s", operandType.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002014#endif
ethannicholasb3058bd2016-07-01 08:22:01 -07002015 }
Ethan Nicholase77739e2019-03-14 14:04:43 -04002016 if (getActualType(resultType) == operandType && !resultType.highPrecision()) {
Ethan Nicholas858fecc2019-03-07 13:19:18 -05002017 this->writeInstruction(SpvOpDecorate, result, SpvDecorationRelaxedPrecision,
2018 fDecorationBuffer);
2019 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002020 return result;
2021}
2022
Ethan Nicholas48e24052018-03-14 13:51:39 -04002023SpvId SPIRVCodeGenerator::foldToBool(SpvId id, const Type& operandType, SpvOp op,
2024 OutputStream& out) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002025 if (operandType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholasef653b82017-02-21 13:50:00 -05002026 SpvId result = this->nextId();
Ethan Nicholas48e24052018-03-14 13:51:39 -04002027 this->writeInstruction(op, this->getType(*fContext.fBool_Type), result, id, out);
Ethan Nicholasef653b82017-02-21 13:50:00 -05002028 return result;
2029 }
2030 return id;
2031}
2032
Ethan Nicholas68990be2017-07-13 09:36:52 -04002033SpvId SPIRVCodeGenerator::writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs,
2034 SpvOp_ floatOperator, SpvOp_ intOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002035 SpvOp_ vectorMergeOperator, SpvOp_ mergeOperator,
Ethan Nicholas68990be2017-07-13 09:36:52 -04002036 OutputStream& out) {
2037 SpvOp_ compareOp = is_float(fContext, operandType) ? floatOperator : intOperator;
Ethan Nicholase6592142020-09-08 10:22:09 -04002038 SkASSERT(operandType.typeKind() == Type::TypeKind::kMatrix);
Ethan Nicholas0df21132018-07-10 09:37:51 -04002039 SpvId columnType = this->getType(operandType.componentType().toCompound(fContext,
2040 operandType.rows(),
2041 1));
Ethan Nicholas68990be2017-07-13 09:36:52 -04002042 SpvId bvecType = this->getType(fContext.fBool_Type->toCompound(fContext,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002043 operandType.rows(),
Ethan Nicholas68990be2017-07-13 09:36:52 -04002044 1));
2045 SpvId boolType = this->getType(*fContext.fBool_Type);
2046 SpvId result = 0;
Ethan Nicholas0df21132018-07-10 09:37:51 -04002047 for (int i = 0; i < operandType.columns(); i++) {
2048 SpvId columnL = this->nextId();
2049 this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out);
2050 SpvId columnR = this->nextId();
2051 this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002052 SpvId compare = this->nextId();
Ethan Nicholas0df21132018-07-10 09:37:51 -04002053 this->writeInstruction(compareOp, bvecType, compare, columnL, columnR, out);
2054 SpvId merge = this->nextId();
2055 this->writeInstruction(vectorMergeOperator, boolType, merge, compare, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002056 if (result != 0) {
2057 SpvId next = this->nextId();
Ethan Nicholas0df21132018-07-10 09:37:51 -04002058 this->writeInstruction(mergeOperator, boolType, next, result, merge, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002059 result = next;
2060 }
2061 else {
Ethan Nicholas0df21132018-07-10 09:37:51 -04002062 result = merge;
Ethan Nicholas68990be2017-07-13 09:36:52 -04002063 }
2064 }
2065 return result;
2066}
2067
Ethan Nicholas0df21132018-07-10 09:37:51 -04002068SpvId SPIRVCodeGenerator::writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs,
2069 SpvId rhs, SpvOp_ floatOperator,
2070 SpvOp_ intOperator,
2071 OutputStream& out) {
2072 SpvOp_ op = is_float(fContext, operandType) ? floatOperator : intOperator;
Ethan Nicholase6592142020-09-08 10:22:09 -04002073 SkASSERT(operandType.typeKind() == Type::TypeKind::kMatrix);
Ethan Nicholas0df21132018-07-10 09:37:51 -04002074 SpvId columnType = this->getType(operandType.componentType().toCompound(fContext,
2075 operandType.rows(),
2076 1));
2077 SpvId columns[4];
2078 for (int i = 0; i < operandType.columns(); i++) {
2079 SpvId columnL = this->nextId();
2080 this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out);
2081 SpvId columnR = this->nextId();
2082 this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out);
2083 columns[i] = this->nextId();
2084 this->writeInstruction(op, columnType, columns[i], columnL, columnR, out);
2085 }
2086 SpvId result = this->nextId();
2087 this->writeOpCode(SpvOpCompositeConstruct, 3 + operandType.columns(), out);
2088 this->writeWord(this->getType(operandType), out);
2089 this->writeWord(result, out);
2090 for (int i = 0; i < operandType.columns(); i++) {
2091 this->writeWord(columns[i], out);
2092 }
2093 return result;
2094}
2095
Ethan Nicholas49465b42019-04-17 12:22:21 -04002096std::unique_ptr<Expression> create_literal_1(const Context& context, const Type& type) {
2097 if (type.isInteger()) {
2098 return std::unique_ptr<Expression>(new IntLiteral(-1, 1, &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07002099 }
Ethan Nicholas49465b42019-04-17 12:22:21 -04002100 else if (type.isFloat()) {
2101 return std::unique_ptr<Expression>(new FloatLiteral(-1, 1.0, &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07002102 } else {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002103 ABORT("math is unsupported on type '%s'", type.name().c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -07002104 }
Ethan Nicholas49465b42019-04-17 12:22:21 -04002105}
2106
2107SpvId SPIRVCodeGenerator::writeBinaryExpression(const Type& leftType, SpvId lhs, Token::Kind op,
2108 const Type& rightType, SpvId rhs,
2109 const Type& resultType, OutputStream& out) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002110 Type tmp("<invalid>");
Ethan Nicholas48e24052018-03-14 13:51:39 -04002111 // overall type we are operating on: float2, int, uint4...
ethannicholasb3058bd2016-07-01 08:22:01 -07002112 const Type* operandType;
Ethan Nicholas48e24052018-03-14 13:51:39 -04002113 // IR allows mismatched types in expressions (e.g. float2 * float), but they need special
2114 // handling in SPIR-V
Ethan Nicholas49465b42019-04-17 12:22:21 -04002115 if (this->getActualType(leftType) != this->getActualType(rightType)) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002116 if (leftType.typeKind() == Type::TypeKind::kVector && rightType.isNumber()) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002117 if (op == Token::Kind::TK_SLASH) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002118 SpvId one = this->writeExpression(*create_literal_1(fContext, rightType), out);
2119 SpvId inverse = this->nextId();
2120 this->writeInstruction(SpvOpFDiv, this->getType(rightType), inverse, one, rhs, out);
2121 rhs = inverse;
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002122 op = Token::Kind::TK_STAR;
Ethan Nicholas49465b42019-04-17 12:22:21 -04002123 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002124 if (op == Token::Kind::TK_STAR) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002125 SpvId result = this->nextId();
2126 this->writeInstruction(SpvOpVectorTimesScalar, this->getType(resultType),
2127 result, lhs, rhs, out);
2128 return result;
2129 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002130 // promote number to vector
2131 SpvId vec = this->nextId();
Ethan Nicholas49465b42019-04-17 12:22:21 -04002132 const Type& vecType = leftType;
Ethan Nicholas48e24052018-03-14 13:51:39 -04002133 this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out);
2134 this->writeWord(this->getType(vecType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002135 this->writeWord(vec, out);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002136 for (int i = 0; i < vecType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002137 this->writeWord(rhs, out);
2138 }
2139 rhs = vec;
Ethan Nicholas49465b42019-04-17 12:22:21 -04002140 operandType = &leftType;
Ethan Nicholase6592142020-09-08 10:22:09 -04002141 } else if (rightType.typeKind() == Type::TypeKind::kVector && leftType.isNumber()) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002142 if (op == Token::Kind::TK_STAR) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002143 SpvId result = this->nextId();
2144 this->writeInstruction(SpvOpVectorTimesScalar, this->getType(resultType),
2145 result, rhs, lhs, out);
2146 return result;
2147 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002148 // promote number to vector
2149 SpvId vec = this->nextId();
Ethan Nicholas49465b42019-04-17 12:22:21 -04002150 const Type& vecType = rightType;
Ethan Nicholas48e24052018-03-14 13:51:39 -04002151 this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out);
2152 this->writeWord(this->getType(vecType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002153 this->writeWord(vec, out);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002154 for (int i = 0; i < vecType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002155 this->writeWord(lhs, out);
2156 }
2157 lhs = vec;
Ethan Nicholas49465b42019-04-17 12:22:21 -04002158 operandType = &rightType;
Ethan Nicholase6592142020-09-08 10:22:09 -04002159 } else if (leftType.typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002160 SpvOp_ spvop;
Ethan Nicholase6592142020-09-08 10:22:09 -04002161 if (rightType.typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002162 spvop = SpvOpMatrixTimesMatrix;
Ethan Nicholase6592142020-09-08 10:22:09 -04002163 } else if (rightType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002164 spvop = SpvOpMatrixTimesVector;
ethannicholasb3058bd2016-07-01 08:22:01 -07002165 } else {
Ethan Nicholase6592142020-09-08 10:22:09 -04002166 SkASSERT(rightType.typeKind() == Type::TypeKind::kScalar);
Ethan Nicholas49465b42019-04-17 12:22:21 -04002167 spvop = SpvOpMatrixTimesScalar;
ethannicholasb3058bd2016-07-01 08:22:01 -07002168 }
2169 SpvId result = this->nextId();
Ethan Nicholas49465b42019-04-17 12:22:21 -04002170 this->writeInstruction(spvop, this->getType(resultType), result, lhs, rhs, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002171 return result;
Ethan Nicholase6592142020-09-08 10:22:09 -04002172 } else if (rightType.typeKind() == Type::TypeKind::kMatrix) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002173 SpvId result = this->nextId();
Ethan Nicholase6592142020-09-08 10:22:09 -04002174 if (leftType.typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002175 this->writeInstruction(SpvOpVectorTimesMatrix, this->getType(resultType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002176 lhs, rhs, out);
2177 } else {
Ethan Nicholase6592142020-09-08 10:22:09 -04002178 SkASSERT(leftType.typeKind() == Type::TypeKind::kScalar);
Ethan Nicholas49465b42019-04-17 12:22:21 -04002179 this->writeInstruction(SpvOpMatrixTimesScalar, this->getType(resultType), result,
2180 rhs, lhs, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002181 }
2182 return result;
2183 } else {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002184 SkASSERT(false);
2185 return -1;
ethannicholasb3058bd2016-07-01 08:22:01 -07002186 }
2187 } else {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002188 tmp = this->getActualType(leftType);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002189 operandType = &tmp;
Ethan Nicholas49465b42019-04-17 12:22:21 -04002190 SkASSERT(*operandType == this->getActualType(rightType));
ethannicholasb3058bd2016-07-01 08:22:01 -07002191 }
Ethan Nicholas49465b42019-04-17 12:22:21 -04002192 switch (op) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002193 case Token::Kind::TK_EQEQ: {
Ethan Nicholase6592142020-09-08 10:22:09 -04002194 if (operandType->typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -04002195 return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdEqual,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002196 SpvOpIEqual, SpvOpAll, SpvOpLogicalAnd, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002197 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002198 SkASSERT(resultType == *fContext.fBool_Type);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002199 const Type* tmpType;
Ethan Nicholase6592142020-09-08 10:22:09 -04002200 if (operandType->typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas48e24052018-03-14 13:51:39 -04002201 tmpType = &fContext.fBool_Type->toCompound(fContext,
2202 operandType->columns(),
2203 operandType->rows());
2204 } else {
2205 tmpType = &resultType;
2206 }
2207 return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs,
Ethan Nicholasef653b82017-02-21 13:50:00 -05002208 SpvOpFOrdEqual, SpvOpIEqual,
2209 SpvOpIEqual, SpvOpLogicalEqual, out),
Ethan Nicholas48e24052018-03-14 13:51:39 -04002210 *operandType, SpvOpAll, out);
Ethan Nicholasef653b82017-02-21 13:50:00 -05002211 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002212 case Token::Kind::TK_NEQ:
Ethan Nicholase6592142020-09-08 10:22:09 -04002213 if (operandType->typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas68990be2017-07-13 09:36:52 -04002214 return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdNotEqual,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002215 SpvOpINotEqual, SpvOpAny, SpvOpLogicalOr, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002216 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002217 SkASSERT(resultType == *fContext.fBool_Type);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002218 const Type* tmpType;
Ethan Nicholase6592142020-09-08 10:22:09 -04002219 if (operandType->typeKind() == Type::TypeKind::kVector) {
Ethan Nicholas48e24052018-03-14 13:51:39 -04002220 tmpType = &fContext.fBool_Type->toCompound(fContext,
2221 operandType->columns(),
2222 operandType->rows());
2223 } else {
2224 tmpType = &resultType;
2225 }
2226 return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs,
Ethan Nicholasef653b82017-02-21 13:50:00 -05002227 SpvOpFOrdNotEqual, SpvOpINotEqual,
2228 SpvOpINotEqual, SpvOpLogicalNotEqual,
2229 out),
Ethan Nicholas48e24052018-03-14 13:51:39 -04002230 *operandType, SpvOpAny, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002231 case Token::Kind::TK_GT:
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,
2234 SpvOpFOrdGreaterThan, SpvOpSGreaterThan,
ethannicholasb3058bd2016-07-01 08:22:01 -07002235 SpvOpUGreaterThan, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002236 case Token::Kind::TK_LT:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002237 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002238 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFOrdLessThan,
ethannicholasb3058bd2016-07-01 08:22:01 -07002239 SpvOpSLessThan, SpvOpULessThan, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002240 case Token::Kind::TK_GTEQ:
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 SpvOpFOrdGreaterThanEqual, SpvOpSGreaterThanEqual,
ethannicholasb3058bd2016-07-01 08:22:01 -07002244 SpvOpUGreaterThanEqual, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002245 case Token::Kind::TK_LTEQ:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002246 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002247 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2248 SpvOpFOrdLessThanEqual, SpvOpSLessThanEqual,
ethannicholasb3058bd2016-07-01 08:22:01 -07002249 SpvOpULessThanEqual, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002250 case Token::Kind::TK_PLUS:
Ethan Nicholase6592142020-09-08 10:22:09 -04002251 if (leftType.typeKind() == Type::TypeKind::kMatrix &&
2252 rightType.typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002253 SkASSERT(leftType == rightType);
2254 return this->writeComponentwiseMatrixBinary(leftType, lhs, rhs,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002255 SpvOpFAdd, SpvOpIAdd, out);
2256 }
Greg Daniel64773e62016-11-22 09:44:03 -05002257 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFAdd,
ethannicholasb3058bd2016-07-01 08:22:01 -07002258 SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002259 case Token::Kind::TK_MINUS:
Ethan Nicholase6592142020-09-08 10:22:09 -04002260 if (leftType.typeKind() == Type::TypeKind::kMatrix &&
2261 rightType.typeKind() == Type::TypeKind::kMatrix) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002262 SkASSERT(leftType == rightType);
2263 return this->writeComponentwiseMatrixBinary(leftType, lhs, rhs,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002264 SpvOpFSub, SpvOpISub, out);
2265 }
Greg Daniel64773e62016-11-22 09:44:03 -05002266 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFSub,
ethannicholasb3058bd2016-07-01 08:22:01 -07002267 SpvOpISub, SpvOpISub, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002268 case Token::Kind::TK_STAR:
Ethan Nicholase6592142020-09-08 10:22:09 -04002269 if (leftType.typeKind() == Type::TypeKind::kMatrix &&
2270 rightType.typeKind() == Type::TypeKind::kMatrix) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002271 // matrix multiply
2272 SpvId result = this->nextId();
2273 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(resultType), result,
2274 lhs, rhs, out);
2275 return result;
2276 }
Greg Daniel64773e62016-11-22 09:44:03 -05002277 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMul,
ethannicholasb3058bd2016-07-01 08:22:01 -07002278 SpvOpIMul, SpvOpIMul, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002279 case Token::Kind::TK_SLASH:
Greg Daniel64773e62016-11-22 09:44:03 -05002280 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFDiv,
ethannicholasb3058bd2016-07-01 08:22:01 -07002281 SpvOpSDiv, SpvOpUDiv, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002282 case Token::Kind::TK_PERCENT:
Ethan Nicholasb3d0f7c2017-05-17 13:13:21 -04002283 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMod,
2284 SpvOpSMod, SpvOpUMod, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002285 case Token::Kind::TK_SHL:
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002286 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2287 SpvOpShiftLeftLogical, SpvOpShiftLeftLogical,
2288 SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002289 case Token::Kind::TK_SHR:
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002290 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2291 SpvOpShiftRightArithmetic, SpvOpShiftRightLogical,
2292 SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002293 case Token::Kind::TK_BITWISEAND:
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002294 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2295 SpvOpBitwiseAnd, SpvOpBitwiseAnd, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002296 case Token::Kind::TK_BITWISEOR:
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002297 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2298 SpvOpBitwiseOr, SpvOpBitwiseOr, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002299 case Token::Kind::TK_BITWISEXOR:
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002300 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2301 SpvOpBitwiseXor, SpvOpBitwiseXor, SpvOpUndef, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002302 case Token::Kind::TK_COMMA:
Ethan Nicholas5a9a9b82019-09-20 12:59:22 -04002303 return rhs;
ethannicholasb3058bd2016-07-01 08:22:01 -07002304 default:
Ethan Nicholas49465b42019-04-17 12:22:21 -04002305 SkASSERT(false);
2306 return -1;
ethannicholasb3058bd2016-07-01 08:22:01 -07002307 }
2308}
2309
Ethan Nicholas49465b42019-04-17 12:22:21 -04002310SpvId SPIRVCodeGenerator::writeBinaryExpression(const BinaryExpression& b, OutputStream& out) {
2311 // handle cases where we don't necessarily evaluate both LHS and RHS
2312 switch (b.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002313 case Token::Kind::TK_EQ: {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002314 SpvId rhs = this->writeExpression(*b.fRight, out);
2315 this->getLValue(*b.fLeft, out)->store(rhs, out);
2316 return rhs;
2317 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002318 case Token::Kind::TK_LOGICALAND:
Ethan Nicholas49465b42019-04-17 12:22:21 -04002319 return this->writeLogicalAnd(b, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002320 case Token::Kind::TK_LOGICALOR:
Ethan Nicholas49465b42019-04-17 12:22:21 -04002321 return this->writeLogicalOr(b, out);
2322 default:
2323 break;
2324 }
2325
2326 std::unique_ptr<LValue> lvalue;
2327 SpvId lhs;
Brian Osman401a0092020-09-10 14:47:24 -04002328 if (Compiler::IsAssignment(b.fOperator)) {
Ethan Nicholas49465b42019-04-17 12:22:21 -04002329 lvalue = this->getLValue(*b.fLeft, out);
2330 lhs = lvalue->load(out);
2331 } else {
2332 lvalue = nullptr;
2333 lhs = this->writeExpression(*b.fLeft, out);
2334 }
2335 SpvId rhs = this->writeExpression(*b.fRight, out);
Brian Osman401a0092020-09-10 14:47:24 -04002336 SpvId result = this->writeBinaryExpression(b.fLeft->fType, lhs,
2337 Compiler::RemoveAssignment(b.fOperator),
Ethan Nicholas49465b42019-04-17 12:22:21 -04002338 b.fRight->fType, rhs, b.fType, out);
2339 if (lvalue) {
2340 lvalue->store(result, out);
2341 }
2342 return result;
2343}
2344
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002345SpvId SPIRVCodeGenerator::writeLogicalAnd(const BinaryExpression& a, OutputStream& out) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002346 SkASSERT(a.fOperator == Token::Kind::TK_LOGICALAND);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002347 BoolLiteral falseLiteral(fContext, -1, false);
ethannicholasb3058bd2016-07-01 08:22:01 -07002348 SpvId falseConstant = this->writeBoolLiteral(falseLiteral);
2349 SpvId lhs = this->writeExpression(*a.fLeft, out);
2350 SpvId rhsLabel = this->nextId();
2351 SpvId end = this->nextId();
2352 SpvId lhsBlock = fCurrentBlock;
2353 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2354 this->writeInstruction(SpvOpBranchConditional, lhs, rhsLabel, end, out);
2355 this->writeLabel(rhsLabel, out);
2356 SpvId rhs = this->writeExpression(*a.fRight, out);
2357 SpvId rhsBlock = fCurrentBlock;
2358 this->writeInstruction(SpvOpBranch, end, out);
2359 this->writeLabel(end, out);
2360 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002361 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, falseConstant,
ethannicholasd598f792016-07-25 10:08:54 -07002362 lhsBlock, rhs, rhsBlock, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002363 return result;
2364}
2365
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002366SpvId SPIRVCodeGenerator::writeLogicalOr(const BinaryExpression& o, OutputStream& out) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002367 SkASSERT(o.fOperator == Token::Kind::TK_LOGICALOR);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002368 BoolLiteral trueLiteral(fContext, -1, true);
ethannicholasb3058bd2016-07-01 08:22:01 -07002369 SpvId trueConstant = this->writeBoolLiteral(trueLiteral);
2370 SpvId lhs = this->writeExpression(*o.fLeft, out);
2371 SpvId rhsLabel = this->nextId();
2372 SpvId end = this->nextId();
2373 SpvId lhsBlock = fCurrentBlock;
2374 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2375 this->writeInstruction(SpvOpBranchConditional, lhs, end, rhsLabel, out);
2376 this->writeLabel(rhsLabel, out);
2377 SpvId rhs = this->writeExpression(*o.fRight, out);
2378 SpvId rhsBlock = fCurrentBlock;
2379 this->writeInstruction(SpvOpBranch, end, out);
2380 this->writeLabel(end, out);
2381 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002382 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, trueConstant,
ethannicholasd598f792016-07-25 10:08:54 -07002383 lhsBlock, rhs, rhsBlock, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002384 return result;
2385}
2386
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002387SpvId SPIRVCodeGenerator::writeTernaryExpression(const TernaryExpression& t, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002388 SpvId test = this->writeExpression(*t.fTest, out);
Brian Osmanb6b95732020-06-30 11:44:27 -04002389 if (t.fIfTrue->fType.columns() == 1 &&
2390 t.fIfTrue->isCompileTimeConstant() &&
2391 t.fIfFalse->isCompileTimeConstant()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002392 // both true and false are constants, can just use OpSelect
2393 SpvId result = this->nextId();
2394 SpvId trueId = this->writeExpression(*t.fIfTrue, out);
2395 SpvId falseId = this->writeExpression(*t.fIfFalse, out);
Greg Daniel64773e62016-11-22 09:44:03 -05002396 this->writeInstruction(SpvOpSelect, this->getType(t.fType), result, test, trueId, falseId,
ethannicholasb3058bd2016-07-01 08:22:01 -07002397 out);
2398 return result;
2399 }
Greg Daniel64773e62016-11-22 09:44:03 -05002400 // was originally using OpPhi to choose the result, but for some reason that is crashing on
ethannicholasb3058bd2016-07-01 08:22:01 -07002401 // Adreno. Switched to storing the result in a temp variable as glslang does.
2402 SpvId var = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002403 this->writeInstruction(SpvOpVariable, this->getPointerType(t.fType, SpvStorageClassFunction),
ethannicholasd598f792016-07-25 10:08:54 -07002404 var, SpvStorageClassFunction, fVariableBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002405 SpvId trueLabel = this->nextId();
2406 SpvId falseLabel = this->nextId();
2407 SpvId end = this->nextId();
2408 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2409 this->writeInstruction(SpvOpBranchConditional, test, trueLabel, falseLabel, out);
2410 this->writeLabel(trueLabel, out);
2411 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfTrue, out), out);
2412 this->writeInstruction(SpvOpBranch, end, out);
2413 this->writeLabel(falseLabel, out);
2414 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfFalse, out), out);
2415 this->writeInstruction(SpvOpBranch, end, out);
2416 this->writeLabel(end, out);
2417 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002418 this->writeInstruction(SpvOpLoad, this->getType(t.fType), result, var, out);
Ethan Nicholas10e93b62019-03-20 10:46:14 -04002419 this->writePrecisionModifier(t.fType, result);
ethannicholasb3058bd2016-07-01 08:22:01 -07002420 return result;
2421}
2422
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002423SpvId SPIRVCodeGenerator::writePrefixExpression(const PrefixExpression& p, OutputStream& out) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002424 if (p.fOperator == Token::Kind::TK_MINUS) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002425 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002426 SpvId typeId = this->getType(p.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -07002427 SpvId expr = this->writeExpression(*p.fOperand, out);
ethannicholasd598f792016-07-25 10:08:54 -07002428 if (is_float(fContext, p.fType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002429 this->writeInstruction(SpvOpFNegate, typeId, result, expr, out);
ethannicholasd598f792016-07-25 10:08:54 -07002430 } else if (is_signed(fContext, p.fType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002431 this->writeInstruction(SpvOpSNegate, typeId, result, expr, out);
2432 } else {
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002433#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07002434 ABORT("unsupported prefix expression %s", p.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002435#endif
Brian Salomon23356442018-11-30 15:33:19 -05002436 }
Ethan Nicholas10e93b62019-03-20 10:46:14 -04002437 this->writePrecisionModifier(p.fType, result);
ethannicholasb3058bd2016-07-01 08:22:01 -07002438 return result;
2439 }
2440 switch (p.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002441 case Token::Kind::TK_PLUS:
ethannicholasb3058bd2016-07-01 08:22:01 -07002442 return this->writeExpression(*p.fOperand, out);
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002443 case Token::Kind::TK_PLUSPLUS: {
ethannicholasb3058bd2016-07-01 08:22:01 -07002444 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
ethannicholasd598f792016-07-25 10:08:54 -07002445 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out);
Greg Daniel64773e62016-11-22 09:44:03 -05002446 SpvId result = this->writeBinaryOperation(p.fType, p.fType, lv->load(out), one,
2447 SpvOpFAdd, SpvOpIAdd, SpvOpIAdd, SpvOpUndef,
ethannicholasb3058bd2016-07-01 08:22:01 -07002448 out);
2449 lv->store(result, out);
2450 return result;
2451 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002452 case Token::Kind::TK_MINUSMINUS: {
ethannicholasb3058bd2016-07-01 08:22:01 -07002453 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
ethannicholasd598f792016-07-25 10:08:54 -07002454 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out);
Greg Daniel64773e62016-11-22 09:44:03 -05002455 SpvId result = this->writeBinaryOperation(p.fType, p.fType, lv->load(out), one,
2456 SpvOpFSub, SpvOpISub, SpvOpISub, SpvOpUndef,
ethannicholasb3058bd2016-07-01 08:22:01 -07002457 out);
2458 lv->store(result, out);
2459 return result;
2460 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002461 case Token::Kind::TK_LOGICALNOT: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002462 SkASSERT(p.fOperand->fType == *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07002463 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002464 this->writeInstruction(SpvOpLogicalNot, this->getType(p.fOperand->fType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002465 this->writeExpression(*p.fOperand, out), out);
2466 return result;
2467 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002468 case Token::Kind::TK_BITWISENOT: {
ethannicholas5961bc92016-10-12 06:39:56 -07002469 SpvId result = this->nextId();
2470 this->writeInstruction(SpvOpNot, this->getType(p.fOperand->fType), result,
2471 this->writeExpression(*p.fOperand, out), out);
2472 return result;
2473 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002474 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002475#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07002476 ABORT("unsupported prefix expression: %s", p.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002477#endif
2478 return -1;
ethannicholasb3058bd2016-07-01 08:22:01 -07002479 }
2480}
2481
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002482SpvId SPIRVCodeGenerator::writePostfixExpression(const PostfixExpression& p, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002483 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
2484 SpvId result = lv->load(out);
ethannicholasd598f792016-07-25 10:08:54 -07002485 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002486 switch (p.fOperator) {
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002487 case Token::Kind::TK_PLUSPLUS: {
Greg Daniel64773e62016-11-22 09:44:03 -05002488 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, one, SpvOpFAdd,
ethannicholasb3058bd2016-07-01 08:22:01 -07002489 SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
2490 lv->store(temp, out);
2491 return result;
2492 }
Ethan Nicholas5a9e7fb2020-04-17 12:45:51 -04002493 case Token::Kind::TK_MINUSMINUS: {
Greg Daniel64773e62016-11-22 09:44:03 -05002494 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, one, SpvOpFSub,
ethannicholasb3058bd2016-07-01 08:22:01 -07002495 SpvOpISub, SpvOpISub, SpvOpUndef, out);
2496 lv->store(temp, out);
2497 return result;
2498 }
2499 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002500#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07002501 ABORT("unsupported postfix expression %s", p.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002502#endif
2503 return -1;
ethannicholasb3058bd2016-07-01 08:22:01 -07002504 }
2505}
2506
ethannicholasf789b382016-08-03 12:43:36 -07002507SpvId SPIRVCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002508 if (b.fValue) {
2509 if (fBoolTrue == 0) {
2510 fBoolTrue = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002511 this->writeInstruction(SpvOpConstantTrue, this->getType(b.fType), fBoolTrue,
ethannicholasb3058bd2016-07-01 08:22:01 -07002512 fConstantBuffer);
2513 }
2514 return fBoolTrue;
2515 } else {
2516 if (fBoolFalse == 0) {
2517 fBoolFalse = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002518 this->writeInstruction(SpvOpConstantFalse, this->getType(b.fType), fBoolFalse,
ethannicholasb3058bd2016-07-01 08:22:01 -07002519 fConstantBuffer);
2520 }
2521 return fBoolFalse;
2522 }
2523}
2524
ethannicholasf789b382016-08-03 12:43:36 -07002525SpvId SPIRVCodeGenerator::writeIntLiteral(const IntLiteral& i) {
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04002526 ConstantType type;
ethannicholasd598f792016-07-25 10:08:54 -07002527 if (i.fType == *fContext.fInt_Type) {
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04002528 type = ConstantType::kInt;
2529 } else if (i.fType == *fContext.fUInt_Type) {
2530 type = ConstantType::kUInt;
Ethan Nicholasc2d84bf2019-09-09 10:49:15 -04002531 } else if (i.fType == *fContext.fShort_Type || i.fType == *fContext.fByte_Type) {
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04002532 type = ConstantType::kShort;
Ethan Nicholasc2d84bf2019-09-09 10:49:15 -04002533 } else if (i.fType == *fContext.fUShort_Type || i.fType == *fContext.fUByte_Type) {
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04002534 type = ConstantType::kUShort;
Ethan Nicholasc2d84bf2019-09-09 10:49:15 -04002535 } else {
2536 SkASSERT(false);
ethannicholasb3058bd2016-07-01 08:22:01 -07002537 }
Ethan Nicholascc5d3e02019-04-19 09:50:56 -04002538 std::pair<ConstantValue, ConstantType> key(i.fValue, type);
2539 auto entry = fNumberConstants.find(key);
2540 if (entry == fNumberConstants.end()) {
2541 SpvId result = this->nextId();
2542 this->writeInstruction(SpvOpConstant, this->getType(i.fType), result, (SpvId) i.fValue,
2543 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) {
John Stiles8c578662020-06-01 15:32:47 +00002551 ConstantType type;
2552 if (f.fType == *fContext.fHalf_Type) {
2553 type = ConstantType::kHalf;
ethannicholasb3058bd2016-07-01 08:22:01 -07002554 } else {
John Stiles8c578662020-06-01 15:32:47 +00002555 type = ConstantType::kFloat;
ethannicholasb3058bd2016-07-01 08:22:01 -07002556 }
John Stiles8c578662020-06-01 15:32:47 +00002557 float value = (float) f.fValue;
2558 std::pair<ConstantValue, ConstantType> key(f.fValue, type);
2559 auto entry = fNumberConstants.find(key);
2560 if (entry == fNumberConstants.end()) {
2561 SpvId result = this->nextId();
2562 uint32_t bits;
2563 SkASSERT(sizeof(bits) == sizeof(value));
2564 memcpy(&bits, &value, sizeof(bits));
2565 this->writeInstruction(SpvOpConstant, this->getType(f.fType), result, bits,
2566 fConstantBuffer);
2567 fNumberConstants[key] = result;
2568 return result;
2569 }
2570 return entry->second;
ethannicholasb3058bd2016-07-01 08:22:01 -07002571}
2572
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002573SpvId SPIRVCodeGenerator::writeFunctionStart(const FunctionDeclaration& f, OutputStream& out) {
ethannicholasd598f792016-07-25 10:08:54 -07002574 SpvId result = fFunctionMap[&f];
Greg Daniel64773e62016-11-22 09:44:03 -05002575 this->writeInstruction(SpvOpFunction, this->getType(f.fReturnType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002576 SpvFunctionControlMaskNone, this->getFunctionType(f), out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002577 this->writeInstruction(SpvOpName, result, f.fName, fNameBuffer);
ethannicholasd598f792016-07-25 10:08:54 -07002578 for (size_t i = 0; i < f.fParameters.size(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002579 SpvId id = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002580 fVariableMap[f.fParameters[i]] = id;
ethannicholasb3058bd2016-07-01 08:22:01 -07002581 SpvId type;
ethannicholasd598f792016-07-25 10:08:54 -07002582 type = this->getPointerType(f.fParameters[i]->fType, SpvStorageClassFunction);
ethannicholasb3058bd2016-07-01 08:22:01 -07002583 this->writeInstruction(SpvOpFunctionParameter, type, id, out);
2584 }
2585 return result;
2586}
2587
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002588SpvId SPIRVCodeGenerator::writeFunction(const FunctionDefinition& f, OutputStream& out) {
2589 fVariableBuffer.reset();
ethannicholasb3058bd2016-07-01 08:22:01 -07002590 SpvId result = this->writeFunctionStart(f.fDeclaration, out);
2591 this->writeLabel(this->nextId(), out);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002592 StringStream bodyBuffer;
Ethan Nicholascb670962017-04-20 19:31:52 -04002593 this->writeBlock((Block&) *f.fBody, bodyBuffer);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002594 write_stringstream(fVariableBuffer, out);
Ethan Nicholas8eb64d32018-12-21 14:50:42 -05002595 if (f.fDeclaration.fName == "main") {
2596 write_stringstream(fGlobalInitializersBuffer, out);
2597 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002598 write_stringstream(bodyBuffer, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002599 if (fCurrentBlock) {
Ethan Nicholas70a44b22017-11-30 09:09:16 -05002600 if (f.fDeclaration.fReturnType == *fContext.fVoid_Type) {
2601 this->writeInstruction(SpvOpReturn, out);
2602 } else {
2603 this->writeInstruction(SpvOpUnreachable, out);
2604 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002605 }
2606 this->writeInstruction(SpvOpFunctionEnd, out);
2607 return result;
2608}
2609
2610void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target) {
2611 if (layout.fLocation >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002612 this->writeInstruction(SpvOpDecorate, target, SpvDecorationLocation, layout.fLocation,
ethannicholasb3058bd2016-07-01 08:22:01 -07002613 fDecorationBuffer);
2614 }
2615 if (layout.fBinding >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002616 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBinding, layout.fBinding,
ethannicholasb3058bd2016-07-01 08:22:01 -07002617 fDecorationBuffer);
2618 }
2619 if (layout.fIndex >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002620 this->writeInstruction(SpvOpDecorate, target, SpvDecorationIndex, layout.fIndex,
ethannicholasb3058bd2016-07-01 08:22:01 -07002621 fDecorationBuffer);
2622 }
2623 if (layout.fSet >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002624 this->writeInstruction(SpvOpDecorate, target, SpvDecorationDescriptorSet, layout.fSet,
ethannicholasb3058bd2016-07-01 08:22:01 -07002625 fDecorationBuffer);
2626 }
Greg Daniel64773e62016-11-22 09:44:03 -05002627 if (layout.fInputAttachmentIndex >= 0) {
2628 this->writeInstruction(SpvOpDecorate, target, SpvDecorationInputAttachmentIndex,
2629 layout.fInputAttachmentIndex, fDecorationBuffer);
Ethan Nicholasbe1099f2018-01-11 16:09:05 -05002630 fCapabilities |= (((uint64_t) 1) << SpvCapabilityInputAttachment);
Greg Daniel64773e62016-11-22 09:44:03 -05002631 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04002632 if (layout.fBuiltin >= 0 && layout.fBuiltin != SK_FRAGCOLOR_BUILTIN &&
Greg Daniele6ab9982018-08-22 13:56:32 +00002633 layout.fBuiltin != SK_IN_BUILTIN && layout.fBuiltin != SK_OUT_BUILTIN) {
Greg Daniel64773e62016-11-22 09:44:03 -05002634 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBuiltIn, layout.fBuiltin,
ethannicholasb3058bd2016-07-01 08:22:01 -07002635 fDecorationBuffer);
2636 }
2637}
2638
2639void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target, int member) {
2640 if (layout.fLocation >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002641 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationLocation,
ethannicholasb3058bd2016-07-01 08:22:01 -07002642 layout.fLocation, fDecorationBuffer);
2643 }
2644 if (layout.fBinding >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002645 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBinding,
ethannicholasb3058bd2016-07-01 08:22:01 -07002646 layout.fBinding, fDecorationBuffer);
2647 }
2648 if (layout.fIndex >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002649 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationIndex,
ethannicholasb3058bd2016-07-01 08:22:01 -07002650 layout.fIndex, fDecorationBuffer);
2651 }
2652 if (layout.fSet >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002653 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationDescriptorSet,
ethannicholasb3058bd2016-07-01 08:22:01 -07002654 layout.fSet, fDecorationBuffer);
2655 }
Greg Daniel64773e62016-11-22 09:44:03 -05002656 if (layout.fInputAttachmentIndex >= 0) {
2657 this->writeInstruction(SpvOpDecorate, target, member, SpvDecorationInputAttachmentIndex,
2658 layout.fInputAttachmentIndex, fDecorationBuffer);
2659 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002660 if (layout.fBuiltin >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002661 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBuiltIn,
ethannicholasb3058bd2016-07-01 08:22:01 -07002662 layout.fBuiltin, fDecorationBuffer);
2663 }
2664}
2665
Ethan Nicholas81d15112018-07-13 12:48:50 -04002666static void update_sk_in_count(const Modifiers& m, int* outSkInCount) {
2667 switch (m.fLayout.fPrimitive) {
2668 case Layout::kPoints_Primitive:
2669 *outSkInCount = 1;
2670 break;
2671 case Layout::kLines_Primitive:
2672 *outSkInCount = 2;
2673 break;
2674 case Layout::kLinesAdjacency_Primitive:
2675 *outSkInCount = 4;
2676 break;
2677 case Layout::kTriangles_Primitive:
2678 *outSkInCount = 3;
2679 break;
2680 case Layout::kTrianglesAdjacency_Primitive:
2681 *outSkInCount = 6;
2682 break;
2683 default:
2684 return;
2685 }
2686}
2687
Stephen White88574972020-06-23 19:09:29 -04002688SpvId SPIRVCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTHeight) {
Ethan Nicholas361941e2017-05-30 15:41:07 -04002689 bool isBuffer = (0 != (intf.fVariable.fModifiers.fFlags & Modifiers::kBuffer_Flag));
Ethan Nicholas39204fd2017-11-27 13:12:30 -05002690 bool pushConstant = (0 != (intf.fVariable.fModifiers.fLayout.fFlags &
2691 Layout::kPushConstant_Flag));
Ethan Nicholas8d2ba442018-03-16 16:40:36 -04002692 MemoryLayout memoryLayout = (pushConstant || isBuffer) ?
2693 MemoryLayout(MemoryLayout::k430_Standard) :
2694 fDefaultLayout;
ethannicholasb3058bd2016-07-01 08:22:01 -07002695 SpvId result = this->nextId();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002696 const Type* type = &intf.fVariable.fType;
Stephen White88574972020-06-23 19:09:29 -04002697 if (fProgram.fInputs.fRTHeight && appendRTHeight) {
Greg Daniele6ab9982018-08-22 13:56:32 +00002698 SkASSERT(fRTHeightStructId == (SpvId) -1);
2699 SkASSERT(fRTHeightFieldIndex == (SpvId) -1);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002700 std::vector<Type::Field> fields = type->fields();
Greg Daniele6ab9982018-08-22 13:56:32 +00002701 fRTHeightStructId = result;
2702 fRTHeightFieldIndex = fields.size();
2703 fields.emplace_back(Modifiers(), StringFragment(SKSL_RTHEIGHT_NAME), fContext.fFloat_Type.get());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002704 type = new Type(type->fOffset, type->name(), fields);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002705 }
Ethan Nicholas5226b772018-05-03 16:20:41 -04002706 SpvId typeId;
2707 if (intf.fVariable.fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) {
2708 for (const auto& e : fProgram) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002709 if (e.kind() == ProgramElement::Kind::kModifiers) {
Ethan Nicholas5226b772018-05-03 16:20:41 -04002710 const Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholas81d15112018-07-13 12:48:50 -04002711 update_sk_in_count(m, &fSkInCount);
Ethan Nicholas5226b772018-05-03 16:20:41 -04002712 }
2713 }
Ethan Nicholase6592142020-09-08 10:22:09 -04002714 typeId = this->getType(Type("sk_in", Type::TypeKind::kArray,
2715 intf.fVariable.fType.componentType(),
2716 fSkInCount),
2717 memoryLayout);
Ethan Nicholas5226b772018-05-03 16:20:41 -04002718 } else {
2719 typeId = this->getType(*type, memoryLayout);
2720 }
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04002721 if (intf.fVariable.fModifiers.fFlags & Modifiers::kBuffer_Flag) {
2722 this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBufferBlock, fDecorationBuffer);
Ethan Nicholas6ac8d362019-01-22 21:43:55 +00002723 } else if (intf.fVariable.fModifiers.fLayout.fBuiltin == -1) {
2724 this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBlock, fDecorationBuffer);
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04002725 }
ethannicholasd598f792016-07-25 10:08:54 -07002726 SpvStorageClass_ storageClass = get_storage_class(intf.fVariable.fModifiers);
ethannicholasb3058bd2016-07-01 08:22:01 -07002727 SpvId ptrType = this->nextId();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002728 this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, typeId, fConstantBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002729 this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConstantBuffer);
Ethan Nicholas8d2ba442018-03-16 16:40:36 -04002730 Layout layout = intf.fVariable.fModifiers.fLayout;
2731 if (intf.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag && layout.fSet == -1) {
2732 layout.fSet = 0;
2733 }
2734 this->writeLayout(layout, result);
ethannicholasd598f792016-07-25 10:08:54 -07002735 fVariableMap[&intf.fVariable] = result;
Stephen White88574972020-06-23 19:09:29 -04002736 if (fProgram.fInputs.fRTHeight && appendRTHeight) {
Ethan Nicholas39b101b2017-03-01 12:07:28 -05002737 delete type;
2738 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002739 return result;
2740}
2741
Ethan Nicholas858fecc2019-03-07 13:19:18 -05002742void SPIRVCodeGenerator::writePrecisionModifier(const Type& type, SpvId id) {
Ethan Nicholas10e93b62019-03-20 10:46:14 -04002743 this->writePrecisionModifier(type.highPrecision() ? Precision::kHigh : Precision::kLow, id);
2744}
2745
2746void SPIRVCodeGenerator::writePrecisionModifier(Precision precision, SpvId id) {
2747 if (precision == Precision::kLow) {
Ethan Nicholasa51d7132017-06-09 10:47:31 -04002748 this->writeInstruction(SpvOpDecorate, id, SpvDecorationRelaxedPrecision, fDecorationBuffer);
2749 }
2750}
2751
Chris Dalton2284aab2019-11-15 11:02:24 -07002752bool is_dead(const Variable& var) {
2753 if (var.fReadCount || var.fWriteCount) {
2754 return false;
2755 }
2756 // not entirely sure what the rules are for when it's safe to elide interface variables, but it
2757 // causes various problems to elide some of them even when dead. But it also causes problems
2758 // *not* to elide sk_SampleMask when it's not being used.
2759 if (!(var.fModifiers.fFlags & (Modifiers::kIn_Flag |
2760 Modifiers::kOut_Flag |
2761 Modifiers::kUniform_Flag |
2762 Modifiers::kBuffer_Flag))) {
2763 return true;
2764 }
2765 return var.fModifiers.fLayout.fBuiltin == SK_SAMPLEMASK_BUILTIN;
2766}
2767
ethannicholas5961bc92016-10-12 06:39:56 -07002768#define BUILTIN_IGNORE 9999
Greg Daniel64773e62016-11-22 09:44:03 -05002769void SPIRVCodeGenerator::writeGlobalVars(Program::Kind kind, const VarDeclarations& decl,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002770 OutputStream& out) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002771 for (size_t i = 0; i < decl.fVars.size(); i++) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002772 if (decl.fVars[i]->kind() == Statement::Kind::kNop) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002773 continue;
2774 }
2775 const VarDeclaration& varDecl = (VarDeclaration&) *decl.fVars[i];
2776 const Variable* var = varDecl.fVar;
Brian Salomonf9f45122016-11-29 11:59:17 -05002777 // These haven't been implemented in our SPIR-V generator yet and we only currently use them
2778 // in the OpenGL backend.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002779 SkASSERT(!(var->fModifiers.fFlags & (Modifiers::kReadOnly_Flag |
Chris Daltonb0fd4b12019-10-29 13:41:22 -06002780 Modifiers::kWriteOnly_Flag |
2781 Modifiers::kCoherent_Flag |
2782 Modifiers::kVolatile_Flag |
2783 Modifiers::kRestrict_Flag)));
ethannicholas5961bc92016-10-12 06:39:56 -07002784 if (var->fModifiers.fLayout.fBuiltin == BUILTIN_IGNORE) {
2785 continue;
2786 }
2787 if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN &&
2788 kind != Program::kFragment_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002789 SkASSERT(!fProgram.fSettings.fFragColorIsInOut);
ethannicholas5961bc92016-10-12 06:39:56 -07002790 continue;
2791 }
Chris Dalton2284aab2019-11-15 11:02:24 -07002792 if (is_dead(*var)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002793 continue;
2794 }
2795 SpvStorageClass_ storageClass;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002796 if (var->fModifiers.fFlags & Modifiers::kIn_Flag) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002797 storageClass = SpvStorageClassInput;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002798 } else if (var->fModifiers.fFlags & Modifiers::kOut_Flag) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002799 storageClass = SpvStorageClassOutput;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002800 } else if (var->fModifiers.fFlags & Modifiers::kUniform_Flag) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002801 if (var->fType.typeKind() == Type::TypeKind::kSampler ||
2802 var->fType.typeKind() == Type::TypeKind::kSeparateSampler ||
2803 var->fType.typeKind() == Type::TypeKind::kTexture) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002804 storageClass = SpvStorageClassUniformConstant;
2805 } else {
2806 storageClass = SpvStorageClassUniform;
2807 }
2808 } else {
2809 storageClass = SpvStorageClassPrivate;
2810 }
2811 SpvId id = this->nextId();
ethannicholas14fe8cc2016-09-07 13:37:16 -07002812 fVariableMap[var] = id;
Ethan Nicholas5226b772018-05-03 16:20:41 -04002813 SpvId type;
2814 if (var->fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002815 type = this->getPointerType(Type("sk_in", Type::TypeKind::kArray,
Ethan Nicholas5226b772018-05-03 16:20:41 -04002816 var->fType.componentType(), fSkInCount),
2817 storageClass);
2818 } else {
2819 type = this->getPointerType(var->fType, storageClass);
2820 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002821 this->writeInstruction(SpvOpVariable, type, id, storageClass, fConstantBuffer);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002822 this->writeInstruction(SpvOpName, id, var->fName, fNameBuffer);
Ethan Nicholas858fecc2019-03-07 13:19:18 -05002823 this->writePrecisionModifier(var->fType, id);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002824 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002825 SkASSERT(!fCurrentBlock);
ethannicholasf789b382016-08-03 12:43:36 -07002826 fCurrentBlock = -1;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002827 SpvId value = this->writeExpression(*varDecl.fValue, fGlobalInitializersBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002828 this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuffer);
ethannicholasf789b382016-08-03 12:43:36 -07002829 fCurrentBlock = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -07002830 }
ethannicholas14fe8cc2016-09-07 13:37:16 -07002831 this->writeLayout(var->fModifiers.fLayout, id);
Ethan Nicholas45b0f152017-07-24 14:36:40 -04002832 if (var->fModifiers.fFlags & Modifiers::kFlat_Flag) {
2833 this->writeInstruction(SpvOpDecorate, id, SpvDecorationFlat, fDecorationBuffer);
2834 }
2835 if (var->fModifiers.fFlags & Modifiers::kNoPerspective_Flag) {
2836 this->writeInstruction(SpvOpDecorate, id, SpvDecorationNoPerspective,
2837 fDecorationBuffer);
2838 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002839 }
2840}
2841
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002842void SPIRVCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, OutputStream& out) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002843 for (const auto& stmt : decl.fVars) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002844 SkASSERT(stmt->kind() == Statement::Kind::kVarDeclaration);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002845 VarDeclaration& varDecl = (VarDeclaration&) *stmt;
2846 const Variable* var = varDecl.fVar;
Brian Salomonf9f45122016-11-29 11:59:17 -05002847 // These haven't been implemented in our SPIR-V generator yet and we only currently use them
2848 // in the OpenGL backend.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002849 SkASSERT(!(var->fModifiers.fFlags & (Modifiers::kReadOnly_Flag |
Brian Salomonf9f45122016-11-29 11:59:17 -05002850 Modifiers::kWriteOnly_Flag |
2851 Modifiers::kCoherent_Flag |
2852 Modifiers::kVolatile_Flag |
2853 Modifiers::kRestrict_Flag)));
ethannicholasb3058bd2016-07-01 08:22:01 -07002854 SpvId id = this->nextId();
ethannicholas14fe8cc2016-09-07 13:37:16 -07002855 fVariableMap[var] = id;
2856 SpvId type = this->getPointerType(var->fType, SpvStorageClassFunction);
ethannicholasb3058bd2016-07-01 08:22:01 -07002857 this->writeInstruction(SpvOpVariable, type, id, SpvStorageClassFunction, fVariableBuffer);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002858 this->writeInstruction(SpvOpName, id, var->fName, fNameBuffer);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002859 if (varDecl.fValue) {
2860 SpvId value = this->writeExpression(*varDecl.fValue, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002861 this->writeInstruction(SpvOpStore, id, value, out);
2862 }
2863 }
2864}
2865
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002866void SPIRVCodeGenerator::writeStatement(const Statement& s, OutputStream& out) {
Ethan Nicholase6592142020-09-08 10:22:09 -04002867 switch (s.kind()) {
John Stiles98c1f822020-09-09 14:18:53 -04002868 case Statement::Kind::kInlineMarker:
Ethan Nicholase6592142020-09-08 10:22:09 -04002869 case Statement::Kind::kNop:
Ethan Nicholascb670962017-04-20 19:31:52 -04002870 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002871 case Statement::Kind::kBlock:
ethannicholasb3058bd2016-07-01 08:22:01 -07002872 this->writeBlock((Block&) s, out);
2873 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002874 case Statement::Kind::kExpression:
John Stiles26f98502020-08-18 09:30:51 -04002875 this->writeExpression(*s.as<ExpressionStatement>().fExpression, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002876 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002877 case Statement::Kind::kReturn:
John Stiles26f98502020-08-18 09:30:51 -04002878 this->writeReturnStatement(s.as<ReturnStatement>(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002879 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002880 case Statement::Kind::kVarDeclarations:
John Stiles26f98502020-08-18 09:30:51 -04002881 this->writeVarDeclarations(*s.as<VarDeclarationsStatement>().fDeclaration, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002882 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002883 case Statement::Kind::kIf:
John Stiles26f98502020-08-18 09:30:51 -04002884 this->writeIfStatement(s.as<IfStatement>(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002885 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002886 case Statement::Kind::kFor:
John Stiles26f98502020-08-18 09:30:51 -04002887 this->writeForStatement(s.as<ForStatement>(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002888 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002889 case Statement::Kind::kWhile:
John Stiles26f98502020-08-18 09:30:51 -04002890 this->writeWhileStatement(s.as<WhileStatement>(), out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002891 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002892 case Statement::Kind::kDo:
John Stiles26f98502020-08-18 09:30:51 -04002893 this->writeDoStatement(s.as<DoStatement>(), out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002894 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002895 case Statement::Kind::kSwitch:
John Stiles26f98502020-08-18 09:30:51 -04002896 this->writeSwitchStatement(s.as<SwitchStatement>(), out);
Ethan Nicholase92b1b12017-11-13 16:13:21 -05002897 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002898 case Statement::Kind::kBreak:
ethannicholasb3058bd2016-07-01 08:22:01 -07002899 this->writeInstruction(SpvOpBranch, fBreakTarget.top(), out);
2900 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002901 case Statement::Kind::kContinue:
ethannicholasb3058bd2016-07-01 08:22:01 -07002902 this->writeInstruction(SpvOpBranch, fContinueTarget.top(), out);
2903 break;
Ethan Nicholase6592142020-09-08 10:22:09 -04002904 case Statement::Kind::kDiscard:
ethannicholasb3058bd2016-07-01 08:22:01 -07002905 this->writeInstruction(SpvOpKill, out);
2906 break;
2907 default:
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002908#ifdef SK_DEBUG
ethannicholasb3058bd2016-07-01 08:22:01 -07002909 ABORT("unsupported statement: %s", s.description().c_str());
Ethan Nicholas2a099da2020-01-02 14:40:54 -05002910#endif
2911 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07002912 }
2913}
2914
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002915void SPIRVCodeGenerator::writeBlock(const Block& b, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002916 for (size_t i = 0; i < b.fStatements.size(); i++) {
2917 this->writeStatement(*b.fStatements[i], out);
2918 }
2919}
2920
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002921void SPIRVCodeGenerator::writeIfStatement(const IfStatement& stmt, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002922 SpvId test = this->writeExpression(*stmt.fTest, out);
2923 SpvId ifTrue = this->nextId();
2924 SpvId ifFalse = this->nextId();
2925 if (stmt.fIfFalse) {
2926 SpvId end = this->nextId();
2927 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2928 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out);
2929 this->writeLabel(ifTrue, out);
2930 this->writeStatement(*stmt.fIfTrue, out);
2931 if (fCurrentBlock) {
2932 this->writeInstruction(SpvOpBranch, end, out);
2933 }
2934 this->writeLabel(ifFalse, out);
2935 this->writeStatement(*stmt.fIfFalse, out);
2936 if (fCurrentBlock) {
2937 this->writeInstruction(SpvOpBranch, end, out);
2938 }
2939 this->writeLabel(end, out);
2940 } else {
2941 this->writeInstruction(SpvOpSelectionMerge, ifFalse, SpvSelectionControlMaskNone, out);
2942 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out);
2943 this->writeLabel(ifTrue, out);
2944 this->writeStatement(*stmt.fIfTrue, out);
2945 if (fCurrentBlock) {
2946 this->writeInstruction(SpvOpBranch, ifFalse, out);
2947 }
2948 this->writeLabel(ifFalse, out);
2949 }
2950}
2951
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002952void SPIRVCodeGenerator::writeForStatement(const ForStatement& f, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002953 if (f.fInitializer) {
2954 this->writeStatement(*f.fInitializer, out);
2955 }
2956 SpvId header = this->nextId();
2957 SpvId start = this->nextId();
2958 SpvId body = this->nextId();
2959 SpvId next = this->nextId();
2960 fContinueTarget.push(next);
2961 SpvId end = this->nextId();
2962 fBreakTarget.push(end);
2963 this->writeInstruction(SpvOpBranch, header, out);
2964 this->writeLabel(header, out);
2965 this->writeInstruction(SpvOpLoopMerge, end, next, SpvLoopControlMaskNone, out);
ethannicholasf789b382016-08-03 12:43:36 -07002966 this->writeInstruction(SpvOpBranch, start, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002967 this->writeLabel(start, out);
ethannicholas22f939e2016-10-13 13:25:34 -07002968 if (f.fTest) {
2969 SpvId test = this->writeExpression(*f.fTest, out);
2970 this->writeInstruction(SpvOpBranchConditional, test, body, end, out);
2971 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002972 this->writeLabel(body, out);
2973 this->writeStatement(*f.fStatement, out);
2974 if (fCurrentBlock) {
2975 this->writeInstruction(SpvOpBranch, next, out);
2976 }
2977 this->writeLabel(next, out);
2978 if (f.fNext) {
2979 this->writeExpression(*f.fNext, out);
2980 }
2981 this->writeInstruction(SpvOpBranch, header, out);
2982 this->writeLabel(end, out);
2983 fBreakTarget.pop();
2984 fContinueTarget.pop();
2985}
2986
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002987void SPIRVCodeGenerator::writeWhileStatement(const WhileStatement& w, OutputStream& out) {
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002988 SpvId header = this->nextId();
2989 SpvId start = this->nextId();
2990 SpvId body = this->nextId();
Ethan Nicholas0d997662019-04-08 09:46:01 -04002991 SpvId continueTarget = this->nextId();
2992 fContinueTarget.push(continueTarget);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002993 SpvId end = this->nextId();
2994 fBreakTarget.push(end);
2995 this->writeInstruction(SpvOpBranch, header, out);
2996 this->writeLabel(header, out);
Ethan Nicholas0d997662019-04-08 09:46:01 -04002997 this->writeInstruction(SpvOpLoopMerge, end, continueTarget, SpvLoopControlMaskNone, out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002998 this->writeInstruction(SpvOpBranch, start, out);
2999 this->writeLabel(start, out);
3000 SpvId test = this->writeExpression(*w.fTest, out);
3001 this->writeInstruction(SpvOpBranchConditional, test, body, end, out);
3002 this->writeLabel(body, out);
3003 this->writeStatement(*w.fStatement, out);
3004 if (fCurrentBlock) {
Ethan Nicholas0d997662019-04-08 09:46:01 -04003005 this->writeInstruction(SpvOpBranch, continueTarget, out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003006 }
Ethan Nicholas0d997662019-04-08 09:46:01 -04003007 this->writeLabel(continueTarget, out);
3008 this->writeInstruction(SpvOpBranch, header, out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003009 this->writeLabel(end, out);
3010 fBreakTarget.pop();
3011 fContinueTarget.pop();
3012}
3013
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003014void SPIRVCodeGenerator::writeDoStatement(const DoStatement& d, OutputStream& out) {
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003015 SpvId header = this->nextId();
3016 SpvId start = this->nextId();
3017 SpvId next = this->nextId();
Ethan Nicholas0d997662019-04-08 09:46:01 -04003018 SpvId continueTarget = this->nextId();
3019 fContinueTarget.push(continueTarget);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003020 SpvId end = this->nextId();
3021 fBreakTarget.push(end);
3022 this->writeInstruction(SpvOpBranch, header, out);
3023 this->writeLabel(header, out);
Ethan Nicholas0d997662019-04-08 09:46:01 -04003024 this->writeInstruction(SpvOpLoopMerge, end, continueTarget, SpvLoopControlMaskNone, out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003025 this->writeInstruction(SpvOpBranch, start, out);
3026 this->writeLabel(start, out);
3027 this->writeStatement(*d.fStatement, out);
3028 if (fCurrentBlock) {
3029 this->writeInstruction(SpvOpBranch, next, out);
3030 }
3031 this->writeLabel(next, out);
3032 SpvId test = this->writeExpression(*d.fTest, out);
Ethan Nicholas0d997662019-04-08 09:46:01 -04003033 this->writeInstruction(SpvOpBranchConditional, test, continueTarget, end, out);
3034 this->writeLabel(continueTarget, out);
3035 this->writeInstruction(SpvOpBranch, header, out);
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05003036 this->writeLabel(end, out);
3037 fBreakTarget.pop();
3038 fContinueTarget.pop();
3039}
3040
Ethan Nicholase92b1b12017-11-13 16:13:21 -05003041void SPIRVCodeGenerator::writeSwitchStatement(const SwitchStatement& s, OutputStream& out) {
3042 SpvId value = this->writeExpression(*s.fValue, out);
3043 std::vector<SpvId> labels;
3044 SpvId end = this->nextId();
3045 SpvId defaultLabel = end;
3046 fBreakTarget.push(end);
3047 int size = 3;
3048 for (const auto& c : s.fCases) {
3049 SpvId label = this->nextId();
3050 labels.push_back(label);
3051 if (c->fValue) {
3052 size += 2;
3053 } else {
3054 defaultLabel = label;
3055 }
3056 }
3057 labels.push_back(end);
3058 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
3059 this->writeOpCode(SpvOpSwitch, size, out);
3060 this->writeWord(value, out);
3061 this->writeWord(defaultLabel, out);
3062 for (size_t i = 0; i < s.fCases.size(); ++i) {
3063 if (!s.fCases[i]->fValue) {
3064 continue;
3065 }
John Stiles81365af2020-08-18 09:24:00 -04003066 this->writeWord(s.fCases[i]->fValue->as<IntLiteral>().fValue, out);
Ethan Nicholase92b1b12017-11-13 16:13:21 -05003067 this->writeWord(labels[i], out);
3068 }
3069 for (size_t i = 0; i < s.fCases.size(); ++i) {
3070 this->writeLabel(labels[i], out);
3071 for (const auto& stmt : s.fCases[i]->fStatements) {
3072 this->writeStatement(*stmt, out);
3073 }
3074 if (fCurrentBlock) {
3075 this->writeInstruction(SpvOpBranch, labels[i + 1], out);
3076 }
3077 }
3078 this->writeLabel(end, out);
3079 fBreakTarget.pop();
3080}
3081
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003082void SPIRVCodeGenerator::writeReturnStatement(const ReturnStatement& r, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07003083 if (r.fExpression) {
Greg Daniel64773e62016-11-22 09:44:03 -05003084 this->writeInstruction(SpvOpReturnValue, this->writeExpression(*r.fExpression, out),
ethannicholasb3058bd2016-07-01 08:22:01 -07003085 out);
3086 } else {
3087 this->writeInstruction(SpvOpReturn, out);
3088 }
3089}
3090
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003091void SPIRVCodeGenerator::writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003092 SkASSERT(fProgram.fKind == Program::kGeometry_Kind);
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003093 int invocations = 1;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003094 for (const auto& e : fProgram) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003095 if (e.kind() == ProgramElement::Kind::kModifiers) {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003096 const Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003097 if (m.fFlags & Modifiers::kIn_Flag) {
3098 if (m.fLayout.fInvocations != -1) {
3099 invocations = m.fLayout.fInvocations;
3100 }
3101 SpvId input;
3102 switch (m.fLayout.fPrimitive) {
3103 case Layout::kPoints_Primitive:
3104 input = SpvExecutionModeInputPoints;
3105 break;
3106 case Layout::kLines_Primitive:
3107 input = SpvExecutionModeInputLines;
3108 break;
3109 case Layout::kLinesAdjacency_Primitive:
3110 input = SpvExecutionModeInputLinesAdjacency;
3111 break;
3112 case Layout::kTriangles_Primitive:
3113 input = SpvExecutionModeTriangles;
3114 break;
3115 case Layout::kTrianglesAdjacency_Primitive:
3116 input = SpvExecutionModeInputTrianglesAdjacency;
3117 break;
3118 default:
3119 input = 0;
3120 break;
3121 }
Ethan Nicholas81d15112018-07-13 12:48:50 -04003122 update_sk_in_count(m, &fSkInCount);
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003123 if (input) {
3124 this->writeInstruction(SpvOpExecutionMode, entryPoint, input, out);
3125 }
3126 } else if (m.fFlags & Modifiers::kOut_Flag) {
3127 SpvId output;
3128 switch (m.fLayout.fPrimitive) {
3129 case Layout::kPoints_Primitive:
3130 output = SpvExecutionModeOutputPoints;
3131 break;
3132 case Layout::kLineStrip_Primitive:
3133 output = SpvExecutionModeOutputLineStrip;
3134 break;
3135 case Layout::kTriangleStrip_Primitive:
3136 output = SpvExecutionModeOutputTriangleStrip;
3137 break;
3138 default:
3139 output = 0;
3140 break;
3141 }
3142 if (output) {
3143 this->writeInstruction(SpvOpExecutionMode, entryPoint, output, out);
3144 }
3145 if (m.fLayout.fMaxVertices != -1) {
3146 this->writeInstruction(SpvOpExecutionMode, entryPoint,
3147 SpvExecutionModeOutputVertices, m.fLayout.fMaxVertices,
3148 out);
3149 }
3150 }
3151 }
3152 }
3153 this->writeInstruction(SpvOpExecutionMode, entryPoint, SpvExecutionModeInvocations,
3154 invocations, out);
3155}
3156
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003157void SPIRVCodeGenerator::writeInstructions(const Program& program, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07003158 fGLSLExtendedInstructions = this->nextId();
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003159 StringStream body;
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003160 std::set<SpvId> interfaceVars;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003161 // assign IDs to functions, determine sk_in size
3162 int skInSize = -1;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003163 for (const auto& e : program) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003164 switch (e.kind()) {
3165 case ProgramElement::Kind::kFunction: {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003166 FunctionDefinition& f = (FunctionDefinition&) e;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003167 fFunctionMap[&f.fDeclaration] = this->nextId();
3168 break;
3169 }
Ethan Nicholase6592142020-09-08 10:22:09 -04003170 case ProgramElement::Kind::kModifiers: {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003171 Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003172 if (m.fFlags & Modifiers::kIn_Flag) {
3173 switch (m.fLayout.fPrimitive) {
3174 case Layout::kPoints_Primitive: // break
3175 case Layout::kLines_Primitive:
3176 skInSize = 1;
3177 break;
3178 case Layout::kLinesAdjacency_Primitive: // break
3179 skInSize = 2;
3180 break;
3181 case Layout::kTriangles_Primitive: // break
3182 case Layout::kTrianglesAdjacency_Primitive:
3183 skInSize = 3;
3184 break;
3185 default:
3186 break;
3187 }
3188 }
3189 break;
3190 }
3191 default:
3192 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07003193 }
3194 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003195 for (const auto& e : program) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003196 if (e.kind() == ProgramElement::Kind::kInterfaceBlock) {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003197 InterfaceBlock& intf = (InterfaceBlock&) e;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003198 if (SK_IN_BUILTIN == intf.fVariable.fModifiers.fLayout.fBuiltin) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003199 SkASSERT(skInSize != -1);
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003200 intf.fSizes.emplace_back(new IntLiteral(fContext, -1, skInSize));
3201 }
ethannicholasb3058bd2016-07-01 08:22:01 -07003202 SpvId id = this->writeInterfaceBlock(intf);
Ethan Nicholas16c11962018-03-16 12:20:54 -04003203 if (((intf.fVariable.fModifiers.fFlags & Modifiers::kIn_Flag) ||
3204 (intf.fVariable.fModifiers.fFlags & Modifiers::kOut_Flag)) &&
Chris Dalton2284aab2019-11-15 11:02:24 -07003205 intf.fVariable.fModifiers.fLayout.fBuiltin == -1 &&
3206 !is_dead(intf.fVariable)) {
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003207 interfaceVars.insert(id);
ethannicholasb3058bd2016-07-01 08:22:01 -07003208 }
3209 }
3210 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003211 for (const auto& e : program) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003212 if (e.kind() == ProgramElement::Kind::kVar) {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003213 this->writeGlobalVars(program.fKind, ((VarDeclarations&) e), body);
ethannicholasb3058bd2016-07-01 08:22:01 -07003214 }
3215 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003216 for (const auto& e : program) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003217 if (e.kind() == ProgramElement::Kind::kFunction) {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003218 this->writeFunction(((FunctionDefinition&) e), body);
ethannicholasb3058bd2016-07-01 08:22:01 -07003219 }
3220 }
ethannicholasd598f792016-07-25 10:08:54 -07003221 const FunctionDeclaration* main = nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07003222 for (auto entry : fFunctionMap) {
ethannicholasf789b382016-08-03 12:43:36 -07003223 if (entry.first->fName == "main") {
ethannicholasb3058bd2016-07-01 08:22:01 -07003224 main = entry.first;
3225 }
3226 }
Ethan Nicholas1a668d22019-04-18 10:37:40 -04003227 if (!main) {
3228 fErrors.error(0, "program does not contain a main() function");
3229 return;
3230 }
ethannicholasb3058bd2016-07-01 08:22:01 -07003231 for (auto entry : fVariableMap) {
ethannicholasd598f792016-07-25 10:08:54 -07003232 const Variable* var = entry.first;
Greg Daniel64773e62016-11-22 09:44:03 -05003233 if (var->fStorage == Variable::kGlobal_Storage &&
Ethan Nicholas16c11962018-03-16 12:20:54 -04003234 ((var->fModifiers.fFlags & Modifiers::kIn_Flag) ||
Chris Dalton2284aab2019-11-15 11:02:24 -07003235 (var->fModifiers.fFlags & Modifiers::kOut_Flag)) && !is_dead(*var)) {
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003236 interfaceVars.insert(entry.second);
ethannicholasb3058bd2016-07-01 08:22:01 -07003237 }
3238 }
3239 this->writeCapabilities(out);
3240 this->writeInstruction(SpvOpExtInstImport, fGLSLExtendedInstructions, "GLSL.std.450", out);
3241 this->writeInstruction(SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemoryModelGLSL450, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07003242 this->writeOpCode(SpvOpEntryPoint, (SpvId) (3 + (main->fName.fLength + 4) / 4) +
ethannicholasb3058bd2016-07-01 08:22:01 -07003243 (int32_t) interfaceVars.size(), out);
3244 switch (program.fKind) {
3245 case Program::kVertex_Kind:
3246 this->writeWord(SpvExecutionModelVertex, out);
3247 break;
3248 case Program::kFragment_Kind:
3249 this->writeWord(SpvExecutionModelFragment, out);
3250 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05003251 case Program::kGeometry_Kind:
3252 this->writeWord(SpvExecutionModelGeometry, out);
3253 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04003254 default:
3255 ABORT("cannot write this kind of program to SPIR-V\n");
ethannicholasb3058bd2016-07-01 08:22:01 -07003256 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003257 SpvId entryPoint = fFunctionMap[main];
3258 this->writeWord(entryPoint, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07003259 this->writeString(main->fName.fChars, main->fName.fLength, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003260 for (int var : interfaceVars) {
3261 this->writeWord(var, out);
3262 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003263 if (program.fKind == Program::kGeometry_Kind) {
3264 this->writeGeometryShaderExecutionMode(entryPoint, out);
3265 }
ethannicholasb3058bd2016-07-01 08:22:01 -07003266 if (program.fKind == Program::kFragment_Kind) {
Greg Daniel64773e62016-11-22 09:44:03 -05003267 this->writeInstruction(SpvOpExecutionMode,
3268 fFunctionMap[main],
ethannicholasb3058bd2016-07-01 08:22:01 -07003269 SpvExecutionModeOriginUpperLeft,
3270 out);
3271 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003272 for (const auto& e : program) {
Ethan Nicholase6592142020-09-08 10:22:09 -04003273 if (e.kind() == ProgramElement::Kind::kExtension) {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003274 this->writeInstruction(SpvOpSourceExtension, ((Extension&) e).fName.c_str(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003275 }
3276 }
Greg Daniel64773e62016-11-22 09:44:03 -05003277
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003278 write_stringstream(fExtraGlobalsBuffer, out);
3279 write_stringstream(fNameBuffer, out);
3280 write_stringstream(fDecorationBuffer, out);
3281 write_stringstream(fConstantBuffer, out);
3282 write_stringstream(fExternalFunctionsBuffer, out);
3283 write_stringstream(body, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003284}
3285
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003286bool SPIRVCodeGenerator::generateCode() {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003287 SkASSERT(!fErrors.errorCount());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003288 this->writeWord(SpvMagicNumber, *fOut);
3289 this->writeWord(SpvVersion, *fOut);
3290 this->writeWord(SKSL_MAGIC, *fOut);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003291 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003292 this->writeInstructions(fProgram, buffer);
3293 this->writeWord(fIdCount, *fOut);
3294 this->writeWord(0, *fOut); // reserved, always zero
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003295 write_stringstream(buffer, *fOut);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003296 return 0 == fErrors.errorCount();
ethannicholasb3058bd2016-07-01 08:22:01 -07003297}
3298
John Stilesa6841be2020-08-06 14:11:56 -04003299} // namespace SkSL