blob: 1804ecbc7e760ae0f4318a52ba8ab04f99f36e11 [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
Ethan Nicholas0df1b042017-03-31 13:56:23 -04008#include "SkSLSPIRVCodeGenerator.h"
Ethan Nicholas9bd301d2017-03-31 16:04:34 +00009
ethannicholasb3058bd2016-07-01 08:22:01 -070010#include "GLSL.std.450.h"
11
12#include "ir/SkSLExpressionStatement.h"
13#include "ir/SkSLExtension.h"
14#include "ir/SkSLIndexExpression.h"
15#include "ir/SkSLVariableReference.h"
ethannicholas5961bc92016-10-12 06:39:56 -070016#include "SkSLCompiler.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070017
18namespace SkSL {
19
ethannicholasb3058bd2016-07-01 08:22:01 -070020static const int32_t SKSL_MAGIC = 0x0; // FIXME: we should probably register a magic number
21
22void SPIRVCodeGenerator::setupIntrinsics() {
23#define ALL_GLSL(x) std::make_tuple(kGLSL_STD_450_IntrinsicKind, GLSLstd450 ## x, GLSLstd450 ## x, \
24 GLSLstd450 ## x, GLSLstd450 ## x)
25#define BY_TYPE_GLSL(ifFloat, ifInt, ifUInt) std::make_tuple(kGLSL_STD_450_IntrinsicKind, \
26 GLSLstd450 ## ifFloat, \
27 GLSLstd450 ## ifInt, \
28 GLSLstd450 ## ifUInt, \
29 SpvOpUndef)
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -040030#define ALL_SPIRV(x) std::make_tuple(kSPIRV_IntrinsicKind, SpvOp ## x, SpvOp ## x, SpvOp ## x, \
31 SpvOp ## x)
ethannicholasb3058bd2016-07-01 08:22:01 -070032#define SPECIAL(x) std::make_tuple(kSpecial_IntrinsicKind, k ## x ## _SpecialIntrinsic, \
33 k ## x ## _SpecialIntrinsic, k ## x ## _SpecialIntrinsic, \
34 k ## x ## _SpecialIntrinsic)
Ethan Nicholas0df1b042017-03-31 13:56:23 -040035 fIntrinsicMap[String("round")] = ALL_GLSL(Round);
36 fIntrinsicMap[String("roundEven")] = ALL_GLSL(RoundEven);
37 fIntrinsicMap[String("trunc")] = ALL_GLSL(Trunc);
38 fIntrinsicMap[String("abs")] = BY_TYPE_GLSL(FAbs, SAbs, SAbs);
39 fIntrinsicMap[String("sign")] = BY_TYPE_GLSL(FSign, SSign, SSign);
40 fIntrinsicMap[String("floor")] = ALL_GLSL(Floor);
41 fIntrinsicMap[String("ceil")] = ALL_GLSL(Ceil);
42 fIntrinsicMap[String("fract")] = ALL_GLSL(Fract);
43 fIntrinsicMap[String("radians")] = ALL_GLSL(Radians);
44 fIntrinsicMap[String("degrees")] = ALL_GLSL(Degrees);
45 fIntrinsicMap[String("sin")] = ALL_GLSL(Sin);
46 fIntrinsicMap[String("cos")] = ALL_GLSL(Cos);
47 fIntrinsicMap[String("tan")] = ALL_GLSL(Tan);
48 fIntrinsicMap[String("asin")] = ALL_GLSL(Asin);
49 fIntrinsicMap[String("acos")] = ALL_GLSL(Acos);
50 fIntrinsicMap[String("atan")] = SPECIAL(Atan);
51 fIntrinsicMap[String("sinh")] = ALL_GLSL(Sinh);
52 fIntrinsicMap[String("cosh")] = ALL_GLSL(Cosh);
53 fIntrinsicMap[String("tanh")] = ALL_GLSL(Tanh);
54 fIntrinsicMap[String("asinh")] = ALL_GLSL(Asinh);
55 fIntrinsicMap[String("acosh")] = ALL_GLSL(Acosh);
56 fIntrinsicMap[String("atanh")] = ALL_GLSL(Atanh);
57 fIntrinsicMap[String("pow")] = ALL_GLSL(Pow);
58 fIntrinsicMap[String("exp")] = ALL_GLSL(Exp);
59 fIntrinsicMap[String("log")] = ALL_GLSL(Log);
60 fIntrinsicMap[String("exp2")] = ALL_GLSL(Exp2);
61 fIntrinsicMap[String("log2")] = ALL_GLSL(Log2);
62 fIntrinsicMap[String("sqrt")] = ALL_GLSL(Sqrt);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -040063 fIntrinsicMap[String("inverse")] = ALL_GLSL(MatrixInverse);
64 fIntrinsicMap[String("transpose")] = ALL_SPIRV(Transpose);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040065 fIntrinsicMap[String("inversesqrt")] = ALL_GLSL(InverseSqrt);
66 fIntrinsicMap[String("determinant")] = ALL_GLSL(Determinant);
67 fIntrinsicMap[String("matrixInverse")] = ALL_GLSL(MatrixInverse);
Ethan Nicholas70a44b22017-11-30 09:09:16 -050068 fIntrinsicMap[String("mod")] = SPECIAL(Mod);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -050069 fIntrinsicMap[String("min")] = SPECIAL(Min);
70 fIntrinsicMap[String("max")] = SPECIAL(Max);
71 fIntrinsicMap[String("clamp")] = SPECIAL(Clamp);
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -040072 fIntrinsicMap[String("saturate")] = SPECIAL(Saturate);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040073 fIntrinsicMap[String("dot")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDot,
Ethan Nicholas0187ae62017-05-03 11:03:44 -040074 SpvOpUndef, SpvOpUndef, SpvOpUndef);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -050075 fIntrinsicMap[String("mix")] = SPECIAL(Mix);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040076 fIntrinsicMap[String("step")] = ALL_GLSL(Step);
77 fIntrinsicMap[String("smoothstep")] = ALL_GLSL(SmoothStep);
78 fIntrinsicMap[String("fma")] = ALL_GLSL(Fma);
79 fIntrinsicMap[String("frexp")] = ALL_GLSL(Frexp);
80 fIntrinsicMap[String("ldexp")] = ALL_GLSL(Ldexp);
ethannicholasb3058bd2016-07-01 08:22:01 -070081
Ethan Nicholas0df1b042017-03-31 13:56:23 -040082#define PACK(type) fIntrinsicMap[String("pack" #type)] = ALL_GLSL(Pack ## type); \
83 fIntrinsicMap[String("unpack" #type)] = ALL_GLSL(Unpack ## type)
ethannicholasb3058bd2016-07-01 08:22:01 -070084 PACK(Snorm4x8);
85 PACK(Unorm4x8);
86 PACK(Snorm2x16);
87 PACK(Unorm2x16);
88 PACK(Half2x16);
89 PACK(Double2x32);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040090 fIntrinsicMap[String("length")] = ALL_GLSL(Length);
91 fIntrinsicMap[String("distance")] = ALL_GLSL(Distance);
92 fIntrinsicMap[String("cross")] = ALL_GLSL(Cross);
93 fIntrinsicMap[String("normalize")] = ALL_GLSL(Normalize);
94 fIntrinsicMap[String("faceForward")] = ALL_GLSL(FaceForward);
95 fIntrinsicMap[String("reflect")] = ALL_GLSL(Reflect);
96 fIntrinsicMap[String("refract")] = ALL_GLSL(Refract);
97 fIntrinsicMap[String("findLSB")] = ALL_GLSL(FindILsb);
98 fIntrinsicMap[String("findMSB")] = BY_TYPE_GLSL(FindSMsb, FindSMsb, FindUMsb);
99 fIntrinsicMap[String("dFdx")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDPdx,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400100 SpvOpUndef, SpvOpUndef, SpvOpUndef);
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700101 fIntrinsicMap[String("dFdy")] = SPECIAL(DFdy);
Chris Dalton09212192018-11-13 15:07:24 -0500102 fIntrinsicMap[String("fwidth")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpFwidth,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400103 SpvOpUndef, SpvOpUndef, SpvOpUndef);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400104 fIntrinsicMap[String("texture")] = SPECIAL(Texture);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400105 fIntrinsicMap[String("subpassLoad")] = SPECIAL(SubpassLoad);
Greg Daniel64773e62016-11-22 09:44:03 -0500106
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400107 fIntrinsicMap[String("any")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400108 SpvOpUndef, SpvOpUndef, SpvOpAny);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400109 fIntrinsicMap[String("all")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400110 SpvOpUndef, SpvOpUndef, SpvOpAll);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400111 fIntrinsicMap[String("equal")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400112 SpvOpFOrdEqual, SpvOpIEqual,
113 SpvOpIEqual, SpvOpLogicalEqual);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400114 fIntrinsicMap[String("notEqual")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400115 SpvOpFOrdNotEqual, SpvOpINotEqual,
116 SpvOpINotEqual,
117 SpvOpLogicalNotEqual);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400118 fIntrinsicMap[String("lessThan")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500119 SpvOpFOrdLessThan, SpvOpSLessThan,
120 SpvOpULessThan, SpvOpUndef);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400121 fIntrinsicMap[String("lessThanEqual")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500122 SpvOpFOrdLessThanEqual,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400123 SpvOpSLessThanEqual,
124 SpvOpULessThanEqual,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400125 SpvOpUndef);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400126 fIntrinsicMap[String("greaterThan")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500127 SpvOpFOrdGreaterThan,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400128 SpvOpSGreaterThan,
129 SpvOpUGreaterThan,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400130 SpvOpUndef);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400131 fIntrinsicMap[String("greaterThanEqual")] = std::make_tuple(kSPIRV_IntrinsicKind,
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500132 SpvOpFOrdGreaterThanEqual,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400133 SpvOpSGreaterThanEqual,
134 SpvOpUGreaterThanEqual,
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400135 SpvOpUndef);
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400136 fIntrinsicMap[String("EmitVertex")] = ALL_SPIRV(EmitVertex);
137 fIntrinsicMap[String("EndPrimitive")] = ALL_SPIRV(EndPrimitive);
ethannicholasb3058bd2016-07-01 08:22:01 -0700138// interpolateAt* not yet supported...
139}
140
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400141void SPIRVCodeGenerator::writeWord(int32_t word, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700142 out.write((const char*) &word, sizeof(word));
ethannicholasb3058bd2016-07-01 08:22:01 -0700143}
144
ethannicholasd598f792016-07-25 10:08:54 -0700145static bool is_float(const Context& context, const Type& type) {
Ethan Nicholas0df21132018-07-10 09:37:51 -0400146 if (type.columns() > 1) {
ethannicholasd598f792016-07-25 10:08:54 -0700147 return is_float(context, type.componentType());
ethannicholasb3058bd2016-07-01 08:22:01 -0700148 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400149 return type == *context.fFloat_Type || type == *context.fHalf_Type ||
150 type == *context.fDouble_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -0700151}
152
ethannicholasd598f792016-07-25 10:08:54 -0700153static bool is_signed(const Context& context, const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700154 if (type.kind() == Type::kVector_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -0700155 return is_signed(context, type.componentType());
ethannicholasb3058bd2016-07-01 08:22:01 -0700156 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400157 return type == *context.fInt_Type || type == *context.fShort_Type ||
158 type == *context.fByte_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -0700159}
160
ethannicholasd598f792016-07-25 10:08:54 -0700161static bool is_unsigned(const Context& context, const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700162 if (type.kind() == Type::kVector_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -0700163 return is_unsigned(context, type.componentType());
ethannicholasb3058bd2016-07-01 08:22:01 -0700164 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400165 return type == *context.fUInt_Type || type == *context.fUShort_Type ||
166 type == *context.fUByte_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -0700167}
168
ethannicholasd598f792016-07-25 10:08:54 -0700169static bool is_bool(const Context& context, const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700170 if (type.kind() == Type::kVector_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -0700171 return is_bool(context, type.componentType());
ethannicholasb3058bd2016-07-01 08:22:01 -0700172 }
ethannicholasd598f792016-07-25 10:08:54 -0700173 return type == *context.fBool_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -0700174}
175
ethannicholasd598f792016-07-25 10:08:54 -0700176static bool is_out(const Variable& var) {
177 return (var.fModifiers.fFlags & Modifiers::kOut_Flag) != 0;
ethannicholasb3058bd2016-07-01 08:22:01 -0700178}
179
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400180void SPIRVCodeGenerator::writeOpCode(SpvOp_ opCode, int length, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400181 SkASSERT(opCode != SpvOpLoad || &out != &fConstantBuffer);
182 SkASSERT(opCode != SpvOpUndef);
ethannicholasb3058bd2016-07-01 08:22:01 -0700183 switch (opCode) {
184 case SpvOpReturn: // fall through
185 case SpvOpReturnValue: // fall through
ethannicholas552882f2016-07-07 06:30:48 -0700186 case SpvOpKill: // fall through
ethannicholasb3058bd2016-07-01 08:22:01 -0700187 case SpvOpBranch: // fall through
188 case SpvOpBranchConditional:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400189 SkASSERT(fCurrentBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700190 fCurrentBlock = 0;
191 break;
192 case SpvOpConstant: // fall through
193 case SpvOpConstantTrue: // fall through
194 case SpvOpConstantFalse: // fall through
195 case SpvOpConstantComposite: // fall through
196 case SpvOpTypeVoid: // fall through
197 case SpvOpTypeInt: // fall through
198 case SpvOpTypeFloat: // fall through
199 case SpvOpTypeBool: // fall through
200 case SpvOpTypeVector: // fall through
201 case SpvOpTypeMatrix: // fall through
202 case SpvOpTypeArray: // fall through
203 case SpvOpTypePointer: // fall through
204 case SpvOpTypeFunction: // fall through
205 case SpvOpTypeRuntimeArray: // fall through
206 case SpvOpTypeStruct: // fall through
207 case SpvOpTypeImage: // fall through
208 case SpvOpTypeSampledImage: // fall through
209 case SpvOpVariable: // fall through
210 case SpvOpFunction: // fall through
211 case SpvOpFunctionParameter: // fall through
212 case SpvOpFunctionEnd: // fall through
213 case SpvOpExecutionMode: // fall through
214 case SpvOpMemoryModel: // fall through
215 case SpvOpCapability: // fall through
216 case SpvOpExtInstImport: // fall through
217 case SpvOpEntryPoint: // fall through
218 case SpvOpSource: // fall through
219 case SpvOpSourceExtension: // fall through
220 case SpvOpName: // fall through
221 case SpvOpMemberName: // fall through
222 case SpvOpDecorate: // fall through
223 case SpvOpMemberDecorate:
224 break;
225 default:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400226 SkASSERT(fCurrentBlock);
ethannicholasb3058bd2016-07-01 08:22:01 -0700227 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700228 this->writeWord((length << 16) | opCode, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700229}
230
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400231void SPIRVCodeGenerator::writeLabel(SpvId label, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700232 fCurrentBlock = label;
233 this->writeInstruction(SpvOpLabel, label, out);
234}
235
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400236void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700237 this->writeOpCode(opCode, 1, out);
238}
239
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400240void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700241 this->writeOpCode(opCode, 2, out);
242 this->writeWord(word1, out);
243}
244
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700245void SPIRVCodeGenerator::writeString(const char* string, size_t length, OutputStream& out) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400246 out.write(string, length);
ethannicholasb3058bd2016-07-01 08:22:01 -0700247 switch (length % 4) {
248 case 1:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500249 out.write8(0);
ethannicholasb3058bd2016-07-01 08:22:01 -0700250 // fall through
251 case 2:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500252 out.write8(0);
ethannicholasb3058bd2016-07-01 08:22:01 -0700253 // fall through
254 case 3:
Ethan Nicholas9e1138d2016-11-21 10:39:35 -0500255 out.write8(0);
ethannicholasb3058bd2016-07-01 08:22:01 -0700256 break;
257 default:
258 this->writeWord(0, out);
259 }
260}
261
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700262void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out) {
263 this->writeOpCode(opCode, 1 + (string.fLength + 4) / 4, out);
264 this->writeString(string.fChars, string.fLength, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700265}
266
267
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700268void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400269 OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700270 this->writeOpCode(opCode, 2 + (string.fLength + 4) / 4, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700271 this->writeWord(word1, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700272 this->writeString(string.fChars, string.fLength, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700273}
274
Greg Daniel64773e62016-11-22 09:44:03 -0500275void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700276 StringFragment string, OutputStream& out) {
277 this->writeOpCode(opCode, 3 + (string.fLength + 4) / 4, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700278 this->writeWord(word1, out);
279 this->writeWord(word2, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700280 this->writeString(string.fChars, string.fLength, out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700281}
282
Greg Daniel64773e62016-11-22 09:44:03 -0500283void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400284 OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700285 this->writeOpCode(opCode, 3, out);
286 this->writeWord(word1, out);
287 this->writeWord(word2, out);
288}
289
Greg Daniel64773e62016-11-22 09:44:03 -0500290void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400291 int32_t word3, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700292 this->writeOpCode(opCode, 4, out);
293 this->writeWord(word1, out);
294 this->writeWord(word2, out);
295 this->writeWord(word3, out);
296}
297
Greg Daniel64773e62016-11-22 09:44:03 -0500298void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400299 int32_t word3, int32_t word4, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700300 this->writeOpCode(opCode, 5, out);
301 this->writeWord(word1, out);
302 this->writeWord(word2, out);
303 this->writeWord(word3, out);
304 this->writeWord(word4, out);
305}
306
Greg Daniel64773e62016-11-22 09:44:03 -0500307void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
308 int32_t word3, int32_t word4, int32_t word5,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400309 OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700310 this->writeOpCode(opCode, 6, out);
311 this->writeWord(word1, out);
312 this->writeWord(word2, out);
313 this->writeWord(word3, out);
314 this->writeWord(word4, out);
315 this->writeWord(word5, out);
316}
317
Greg Daniel64773e62016-11-22 09:44:03 -0500318void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
ethannicholasb3058bd2016-07-01 08:22:01 -0700319 int32_t word3, int32_t word4, int32_t word5,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400320 int32_t word6, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700321 this->writeOpCode(opCode, 7, out);
322 this->writeWord(word1, out);
323 this->writeWord(word2, out);
324 this->writeWord(word3, out);
325 this->writeWord(word4, out);
326 this->writeWord(word5, out);
327 this->writeWord(word6, out);
328}
329
Greg Daniel64773e62016-11-22 09:44:03 -0500330void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
ethannicholasb3058bd2016-07-01 08:22:01 -0700331 int32_t word3, int32_t word4, int32_t word5,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400332 int32_t word6, int32_t word7, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700333 this->writeOpCode(opCode, 8, out);
334 this->writeWord(word1, out);
335 this->writeWord(word2, out);
336 this->writeWord(word3, out);
337 this->writeWord(word4, out);
338 this->writeWord(word5, out);
339 this->writeWord(word6, out);
340 this->writeWord(word7, out);
341}
342
Greg Daniel64773e62016-11-22 09:44:03 -0500343void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
ethannicholasb3058bd2016-07-01 08:22:01 -0700344 int32_t word3, int32_t word4, int32_t word5,
345 int32_t word6, int32_t word7, int32_t word8,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400346 OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700347 this->writeOpCode(opCode, 9, out);
348 this->writeWord(word1, out);
349 this->writeWord(word2, out);
350 this->writeWord(word3, out);
351 this->writeWord(word4, out);
352 this->writeWord(word5, out);
353 this->writeWord(word6, out);
354 this->writeWord(word7, out);
355 this->writeWord(word8, out);
356}
357
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400358void SPIRVCodeGenerator::writeCapabilities(OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700359 for (uint64_t i = 0, bit = 1; i <= kLast_Capability; i++, bit <<= 1) {
360 if (fCapabilities & bit) {
361 this->writeInstruction(SpvOpCapability, (SpvId) i, out);
362 }
363 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400364 if (fProgram.fKind == Program::kGeometry_Kind) {
365 this->writeInstruction(SpvOpCapability, SpvCapabilityGeometry, out);
366 }
Ethan Nicholas81d15112018-07-13 12:48:50 -0400367 else {
368 this->writeInstruction(SpvOpCapability, SpvCapabilityShader, out);
369 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700370}
371
372SpvId SPIRVCodeGenerator::nextId() {
373 return fIdCount++;
374}
375
Ethan Nicholas19671772016-11-28 16:30:17 -0500376void SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& memoryLayout,
377 SpvId resultId) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700378 this->writeInstruction(SpvOpName, resultId, type.name().c_str(), fNameBuffer);
379 // go ahead and write all of the field types, so we don't inadvertently write them while we're
380 // in the middle of writing the struct instruction
381 std::vector<SpvId> types;
382 for (const auto& f : type.fields()) {
Ethan Nicholas19671772016-11-28 16:30:17 -0500383 types.push_back(this->getType(*f.fType, memoryLayout));
ethannicholasb3058bd2016-07-01 08:22:01 -0700384 }
385 this->writeOpCode(SpvOpTypeStruct, 2 + (int32_t) types.size(), fConstantBuffer);
386 this->writeWord(resultId, fConstantBuffer);
387 for (SpvId id : types) {
388 this->writeWord(id, fConstantBuffer);
389 }
390 size_t offset = 0;
391 for (int32_t i = 0; i < (int32_t) type.fields().size(); i++) {
Ethan Nicholas19671772016-11-28 16:30:17 -0500392 size_t size = memoryLayout.size(*type.fields()[i].fType);
393 size_t alignment = memoryLayout.alignment(*type.fields()[i].fType);
394 const Layout& fieldLayout = type.fields()[i].fModifiers.fLayout;
395 if (fieldLayout.fOffset >= 0) {
Greg Danieldbd44c72017-01-24 15:12:12 -0500396 if (fieldLayout.fOffset < (int) offset) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700397 fErrors.error(type.fOffset,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500398 "offset of field '" + type.fields()[i].fName + "' must be at "
399 "least " + to_string((int) offset));
Ethan Nicholas19671772016-11-28 16:30:17 -0500400 }
401 if (fieldLayout.fOffset % alignment) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700402 fErrors.error(type.fOffset,
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500403 "offset of field '" + type.fields()[i].fName + "' must be a multiple"
404 " of " + to_string((int) alignment));
Ethan Nicholas19671772016-11-28 16:30:17 -0500405 }
406 offset = fieldLayout.fOffset;
407 } else {
408 size_t mod = offset % alignment;
409 if (mod) {
410 offset += alignment - mod;
411 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700412 }
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700413 this->writeInstruction(SpvOpMemberName, resultId, i, type.fields()[i].fName, fNameBuffer);
Ethan Nicholas19671772016-11-28 16:30:17 -0500414 this->writeLayout(fieldLayout, resultId, i);
ethannicholasb3058bd2016-07-01 08:22:01 -0700415 if (type.fields()[i].fModifiers.fLayout.fBuiltin < 0) {
Greg Daniel64773e62016-11-22 09:44:03 -0500416 this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, SpvDecorationOffset,
ethannicholasb3058bd2016-07-01 08:22:01 -0700417 (SpvId) offset, fDecorationBuffer);
418 }
ethannicholas0730be72016-09-01 07:59:02 -0700419 if (type.fields()[i].fType->kind() == Type::kMatrix_Kind) {
Greg Daniel64773e62016-11-22 09:44:03 -0500420 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationColMajor,
ethannicholasb3058bd2016-07-01 08:22:01 -0700421 fDecorationBuffer);
Greg Daniel64773e62016-11-22 09:44:03 -0500422 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationMatrixStride,
Ethan Nicholas19671772016-11-28 16:30:17 -0500423 (SpvId) memoryLayout.stride(*type.fields()[i].fType),
ethannicholas8ac838d2016-11-22 08:39:36 -0800424 fDecorationBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -0700425 }
426 offset += size;
ethannicholas0730be72016-09-01 07:59:02 -0700427 Type::Kind kind = type.fields()[i].fType->kind();
ethannicholasb3058bd2016-07-01 08:22:01 -0700428 if ((kind == Type::kArray_Kind || kind == Type::kStruct_Kind) && offset % alignment != 0) {
429 offset += alignment - offset % alignment;
430 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700431 }
432}
433
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400434Type SPIRVCodeGenerator::getActualType(const Type& type) {
Ethan Nicholase1f55022019-02-05 17:17:40 -0500435 if (type.isFloat()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400436 return *fContext.fFloat_Type;
437 }
Ethan Nicholase1f55022019-02-05 17:17:40 -0500438 if (type.isSigned()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400439 return *fContext.fInt_Type;
440 }
Ethan Nicholase1f55022019-02-05 17:17:40 -0500441 if (type.isUnsigned()) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400442 return *fContext.fUInt_Type;
443 }
444 if (type.kind() == Type::kMatrix_Kind || type.kind() == Type::kVector_Kind) {
445 if (type.componentType() == *fContext.fHalf_Type) {
446 return fContext.fFloat_Type->toCompound(fContext, type.columns(), type.rows());
447 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400448 if (type.componentType() == *fContext.fShort_Type ||
449 type.componentType() == *fContext.fByte_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400450 return fContext.fInt_Type->toCompound(fContext, type.columns(), type.rows());
451 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400452 if (type.componentType() == *fContext.fUShort_Type ||
453 type.componentType() == *fContext.fUByte_Type) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400454 return fContext.fUInt_Type->toCompound(fContext, type.columns(), type.rows());
455 }
456 }
457 return type;
458}
459
ethannicholasb3058bd2016-07-01 08:22:01 -0700460SpvId SPIRVCodeGenerator::getType(const Type& type) {
ethannicholas8ac838d2016-11-22 08:39:36 -0800461 return this->getType(type, fDefaultLayout);
462}
463
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400464SpvId SPIRVCodeGenerator::getType(const Type& rawType, const MemoryLayout& layout) {
465 Type type = this->getActualType(rawType);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400466 String key = type.name() + to_string((int) layout.fStd);
ethannicholas8ac838d2016-11-22 08:39:36 -0800467 auto entry = fTypeMap.find(key);
ethannicholasb3058bd2016-07-01 08:22:01 -0700468 if (entry == fTypeMap.end()) {
469 SpvId result = this->nextId();
470 switch (type.kind()) {
471 case Type::kScalar_Kind:
ethannicholasd598f792016-07-25 10:08:54 -0700472 if (type == *fContext.fBool_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700473 this->writeInstruction(SpvOpTypeBool, result, fConstantBuffer);
Ethan Nicholase1f55022019-02-05 17:17:40 -0500474 } else if (type == *fContext.fInt_Type || type == *fContext.fShort_Type ||
475 type == *fContext.fIntLiteral_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700476 this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstantBuffer);
Ethan Nicholase1f55022019-02-05 17:17:40 -0500477 } else if (type == *fContext.fUInt_Type || type == *fContext.fUShort_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700478 this->writeInstruction(SpvOpTypeInt, result, 32, 0, fConstantBuffer);
Ethan Nicholase1f55022019-02-05 17:17:40 -0500479 } else if (type == *fContext.fFloat_Type || type == *fContext.fHalf_Type ||
480 type == *fContext.fFloatLiteral_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700481 this->writeInstruction(SpvOpTypeFloat, result, 32, fConstantBuffer);
ethannicholasd598f792016-07-25 10:08:54 -0700482 } else if (type == *fContext.fDouble_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700483 this->writeInstruction(SpvOpTypeFloat, result, 64, fConstantBuffer);
484 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400485 SkASSERT(false);
ethannicholasb3058bd2016-07-01 08:22:01 -0700486 }
487 break;
488 case Type::kVector_Kind:
Greg Daniel64773e62016-11-22 09:44:03 -0500489 this->writeInstruction(SpvOpTypeVector, result,
ethannicholas8ac838d2016-11-22 08:39:36 -0800490 this->getType(type.componentType(), layout),
ethannicholasb3058bd2016-07-01 08:22:01 -0700491 type.columns(), fConstantBuffer);
492 break;
493 case Type::kMatrix_Kind:
Greg Daniel64773e62016-11-22 09:44:03 -0500494 this->writeInstruction(SpvOpTypeMatrix, result,
ethannicholas8ac838d2016-11-22 08:39:36 -0800495 this->getType(index_type(fContext, type), layout),
ethannicholasb3058bd2016-07-01 08:22:01 -0700496 type.columns(), fConstantBuffer);
497 break;
498 case Type::kStruct_Kind:
ethannicholas8ac838d2016-11-22 08:39:36 -0800499 this->writeStruct(type, layout, result);
ethannicholasb3058bd2016-07-01 08:22:01 -0700500 break;
501 case Type::kArray_Kind: {
502 if (type.columns() > 0) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700503 IntLiteral count(fContext, -1, type.columns());
Greg Daniel64773e62016-11-22 09:44:03 -0500504 this->writeInstruction(SpvOpTypeArray, result,
ethannicholas8ac838d2016-11-22 08:39:36 -0800505 this->getType(type.componentType(), layout),
ethannicholasb3058bd2016-07-01 08:22:01 -0700506 this->writeIntLiteral(count), fConstantBuffer);
Greg Daniel64773e62016-11-22 09:44:03 -0500507 this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400508 (int32_t) layout.stride(type),
ethannicholas8ac838d2016-11-22 08:39:36 -0800509 fDecorationBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -0700510 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400511 SkASSERT(false); // we shouldn't have any runtime-sized arrays right now
Greg Daniel64773e62016-11-22 09:44:03 -0500512 this->writeInstruction(SpvOpTypeRuntimeArray, result,
ethannicholas8ac838d2016-11-22 08:39:36 -0800513 this->getType(type.componentType(), layout),
514 fConstantBuffer);
Ethan Nicholas0dd30d92017-05-01 16:57:07 -0400515 this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride,
516 (int32_t) layout.stride(type),
517 fDecorationBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -0700518 }
519 break;
520 }
521 case Type::kSampler_Kind: {
Greg Daniel64773e62016-11-22 09:44:03 -0500522 SpvId image = result;
523 if (SpvDimSubpassData != type.dimensions()) {
524 image = this->nextId();
525 }
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400526 if (SpvDimBuffer == type.dimensions()) {
527 fCapabilities |= (((uint64_t) 1) << SpvCapabilitySampledBuffer);
528 }
ethannicholas8ac838d2016-11-22 08:39:36 -0800529 this->writeInstruction(SpvOpTypeImage, image,
530 this->getType(*fContext.fFloat_Type, layout),
ethannicholasb3058bd2016-07-01 08:22:01 -0700531 type.dimensions(), type.isDepth(), type.isArrayed(),
Greg Daniel64773e62016-11-22 09:44:03 -0500532 type.isMultisampled(), type.isSampled() ? 1 : 2,
ethannicholasb3058bd2016-07-01 08:22:01 -0700533 SpvImageFormatUnknown, fConstantBuffer);
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400534 fImageTypeMap[key] = image;
Greg Daniel64773e62016-11-22 09:44:03 -0500535 if (SpvDimSubpassData != type.dimensions()) {
536 this->writeInstruction(SpvOpTypeSampledImage, result, image, fConstantBuffer);
537 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700538 break;
539 }
540 default:
ethannicholasd598f792016-07-25 10:08:54 -0700541 if (type == *fContext.fVoid_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700542 this->writeInstruction(SpvOpTypeVoid, result, fConstantBuffer);
543 } else {
544 ABORT("invalid type: %s", type.description().c_str());
545 }
546 }
ethannicholas8ac838d2016-11-22 08:39:36 -0800547 fTypeMap[key] = result;
ethannicholasb3058bd2016-07-01 08:22:01 -0700548 return result;
549 }
550 return entry->second;
551}
552
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400553SpvId SPIRVCodeGenerator::getImageType(const Type& type) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400554 SkASSERT(type.kind() == Type::kSampler_Kind);
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400555 this->getType(type);
556 String key = type.name() + to_string((int) fDefaultLayout.fStd);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400557 SkASSERT(fImageTypeMap.find(key) != fImageTypeMap.end());
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400558 return fImageTypeMap[key];
559}
560
ethannicholasd598f792016-07-25 10:08:54 -0700561SpvId SPIRVCodeGenerator::getFunctionType(const FunctionDeclaration& function) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400562 String key = function.fReturnType.description() + "(";
563 String separator;
ethannicholasd598f792016-07-25 10:08:54 -0700564 for (size_t i = 0; i < function.fParameters.size(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700565 key += separator;
566 separator = ", ";
ethannicholasd598f792016-07-25 10:08:54 -0700567 key += function.fParameters[i]->fType.description();
ethannicholasb3058bd2016-07-01 08:22:01 -0700568 }
569 key += ")";
570 auto entry = fTypeMap.find(key);
571 if (entry == fTypeMap.end()) {
572 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -0700573 int32_t length = 3 + (int32_t) function.fParameters.size();
574 SpvId returnType = this->getType(function.fReturnType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700575 std::vector<SpvId> parameterTypes;
ethannicholasd598f792016-07-25 10:08:54 -0700576 for (size_t i = 0; i < function.fParameters.size(); i++) {
Greg Daniel64773e62016-11-22 09:44:03 -0500577 // glslang seems to treat all function arguments as pointers whether they need to be or
578 // not. I was initially puzzled by this until I ran bizarre failures with certain
579 // patterns of function calls and control constructs, as exemplified by this minimal
ethannicholasb3058bd2016-07-01 08:22:01 -0700580 // failure case:
581 //
582 // void sphere(float x) {
583 // }
Greg Daniel64773e62016-11-22 09:44:03 -0500584 //
ethannicholasb3058bd2016-07-01 08:22:01 -0700585 // void map() {
586 // sphere(1.0);
587 // }
Greg Daniel64773e62016-11-22 09:44:03 -0500588 //
ethannicholasb3058bd2016-07-01 08:22:01 -0700589 // void main() {
590 // for (int i = 0; i < 1; i++) {
591 // map();
592 // }
593 // }
594 //
Greg Daniel64773e62016-11-22 09:44:03 -0500595 // As of this writing, compiling this in the "obvious" way (with sphere taking a float)
596 // crashes. Making it take a float* and storing the argument in a temporary variable,
ethannicholasb3058bd2016-07-01 08:22:01 -0700597 // as glslang does, fixes it. It's entirely possible I simply missed whichever part of
598 // the spec makes this make sense.
599// if (is_out(function->fParameters[i])) {
ethannicholasd598f792016-07-25 10:08:54 -0700600 parameterTypes.push_back(this->getPointerType(function.fParameters[i]->fType,
ethannicholasb3058bd2016-07-01 08:22:01 -0700601 SpvStorageClassFunction));
602// } else {
ethannicholasd598f792016-07-25 10:08:54 -0700603// parameterTypes.push_back(this->getType(function.fParameters[i]->fType));
ethannicholasb3058bd2016-07-01 08:22:01 -0700604// }
605 }
606 this->writeOpCode(SpvOpTypeFunction, length, fConstantBuffer);
607 this->writeWord(result, fConstantBuffer);
608 this->writeWord(returnType, fConstantBuffer);
609 for (SpvId id : parameterTypes) {
610 this->writeWord(id, fConstantBuffer);
611 }
612 fTypeMap[key] = result;
613 return result;
614 }
615 return entry->second;
616}
617
ethannicholas8ac838d2016-11-22 08:39:36 -0800618SpvId SPIRVCodeGenerator::getPointerType(const Type& type, SpvStorageClass_ storageClass) {
619 return this->getPointerType(type, fDefaultLayout, storageClass);
620}
621
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400622SpvId SPIRVCodeGenerator::getPointerType(const Type& rawType, const MemoryLayout& layout,
ethannicholasb3058bd2016-07-01 08:22:01 -0700623 SpvStorageClass_ storageClass) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400624 Type type = this->getActualType(rawType);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400625 String key = type.description() + "*" + to_string(layout.fStd) + to_string(storageClass);
ethannicholasb3058bd2016-07-01 08:22:01 -0700626 auto entry = fTypeMap.find(key);
627 if (entry == fTypeMap.end()) {
628 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -0500629 this->writeInstruction(SpvOpTypePointer, result, storageClass,
ethannicholasd598f792016-07-25 10:08:54 -0700630 this->getType(type), fConstantBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -0700631 fTypeMap[key] = result;
632 return result;
633 }
634 return entry->second;
635}
636
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400637SpvId SPIRVCodeGenerator::writeExpression(const Expression& expr, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700638 switch (expr.fKind) {
639 case Expression::kBinary_Kind:
640 return this->writeBinaryExpression((BinaryExpression&) expr, out);
641 case Expression::kBoolLiteral_Kind:
642 return this->writeBoolLiteral((BoolLiteral&) expr);
643 case Expression::kConstructor_Kind:
644 return this->writeConstructor((Constructor&) expr, out);
645 case Expression::kIntLiteral_Kind:
646 return this->writeIntLiteral((IntLiteral&) expr);
647 case Expression::kFieldAccess_Kind:
648 return this->writeFieldAccess(((FieldAccess&) expr), out);
649 case Expression::kFloatLiteral_Kind:
650 return this->writeFloatLiteral(((FloatLiteral&) expr));
651 case Expression::kFunctionCall_Kind:
652 return this->writeFunctionCall((FunctionCall&) expr, out);
653 case Expression::kPrefix_Kind:
654 return this->writePrefixExpression((PrefixExpression&) expr, out);
655 case Expression::kPostfix_Kind:
656 return this->writePostfixExpression((PostfixExpression&) expr, out);
657 case Expression::kSwizzle_Kind:
658 return this->writeSwizzle((Swizzle&) expr, out);
659 case Expression::kVariableReference_Kind:
660 return this->writeVariableReference((VariableReference&) expr, out);
661 case Expression::kTernary_Kind:
662 return this->writeTernaryExpression((TernaryExpression&) expr, out);
663 case Expression::kIndex_Kind:
664 return this->writeIndexExpression((IndexExpression&) expr, out);
665 default:
666 ABORT("unsupported expression: %s", expr.description().c_str());
667 }
668 return -1;
669}
670
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400671SpvId SPIRVCodeGenerator::writeIntrinsicCall(const FunctionCall& c, OutputStream& out) {
ethannicholasd598f792016-07-25 10:08:54 -0700672 auto intrinsic = fIntrinsicMap.find(c.fFunction.fName);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400673 SkASSERT(intrinsic != fIntrinsicMap.end());
ethannicholasb3058bd2016-07-01 08:22:01 -0700674 int32_t intrinsicId;
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400675 if (c.fArguments.size() > 0) {
676 const Type& type = c.fArguments[0]->fType;
677 if (std::get<0>(intrinsic->second) == kSpecial_IntrinsicKind || is_float(fContext, type)) {
678 intrinsicId = std::get<1>(intrinsic->second);
679 } else if (is_signed(fContext, type)) {
680 intrinsicId = std::get<2>(intrinsic->second);
681 } else if (is_unsigned(fContext, type)) {
682 intrinsicId = std::get<3>(intrinsic->second);
683 } else if (is_bool(fContext, type)) {
684 intrinsicId = std::get<4>(intrinsic->second);
685 } else {
686 intrinsicId = std::get<1>(intrinsic->second);
687 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700688 } else {
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400689 intrinsicId = std::get<1>(intrinsic->second);
ethannicholasb3058bd2016-07-01 08:22:01 -0700690 }
691 switch (std::get<0>(intrinsic->second)) {
692 case kGLSL_STD_450_IntrinsicKind: {
693 SpvId result = this->nextId();
694 std::vector<SpvId> arguments;
695 for (size_t i = 0; i < c.fArguments.size(); i++) {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400696 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
697 arguments.push_back(this->getLValue(*c.fArguments[i], out)->getPointer());
698 } else {
699 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
700 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700701 }
702 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) arguments.size(), out);
ethannicholasd598f792016-07-25 10:08:54 -0700703 this->writeWord(this->getType(c.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700704 this->writeWord(result, out);
705 this->writeWord(fGLSLExtendedInstructions, out);
706 this->writeWord(intrinsicId, out);
707 for (SpvId id : arguments) {
708 this->writeWord(id, out);
709 }
710 return result;
711 }
712 case kSPIRV_IntrinsicKind: {
713 SpvId result = this->nextId();
714 std::vector<SpvId> arguments;
715 for (size_t i = 0; i < c.fArguments.size(); i++) {
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -0400716 if (c.fFunction.fParameters[i]->fModifiers.fFlags & Modifiers::kOut_Flag) {
717 arguments.push_back(this->getLValue(*c.fArguments[i], out)->getPointer());
718 } else {
719 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
720 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700721 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -0400722 if (c.fType != *fContext.fVoid_Type) {
723 this->writeOpCode((SpvOp_) intrinsicId, 3 + (int32_t) arguments.size(), out);
724 this->writeWord(this->getType(c.fType), out);
725 this->writeWord(result, out);
726 } else {
727 this->writeOpCode((SpvOp_) intrinsicId, 1 + (int32_t) arguments.size(), out);
728 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700729 for (SpvId id : arguments) {
730 this->writeWord(id, out);
731 }
732 return result;
733 }
734 case kSpecial_IntrinsicKind:
735 return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId, out);
736 default:
737 ABORT("unsupported intrinsic kind");
738 }
739}
740
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500741std::vector<SpvId> SPIRVCodeGenerator::vectorize(
742 const std::vector<std::unique_ptr<Expression>>& args,
743 OutputStream& out) {
744 int vectorSize = 0;
745 for (const auto& a : args) {
746 if (a->fType.kind() == Type::kVector_Kind) {
747 if (vectorSize) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400748 SkASSERT(a->fType.columns() == vectorSize);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500749 }
750 else {
751 vectorSize = a->fType.columns();
752 }
753 }
754 }
755 std::vector<SpvId> result;
756 for (const auto& a : args) {
757 SpvId raw = this->writeExpression(*a, out);
758 if (vectorSize && a->fType.kind() == Type::kScalar_Kind) {
759 SpvId vector = this->nextId();
760 this->writeOpCode(SpvOpCompositeConstruct, 3 + vectorSize, out);
761 this->writeWord(this->getType(a->fType.toCompound(fContext, vectorSize, 1)), out);
762 this->writeWord(vector, out);
763 for (int i = 0; i < vectorSize; i++) {
764 this->writeWord(raw, out);
765 }
766 result.push_back(vector);
767 } else {
768 result.push_back(raw);
769 }
770 }
771 return result;
772}
773
774void SPIRVCodeGenerator::writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
775 SpvId signedInst, SpvId unsignedInst,
776 const std::vector<SpvId>& args,
777 OutputStream& out) {
778 this->writeOpCode(SpvOpExtInst, 5 + args.size(), out);
779 this->writeWord(this->getType(type), out);
780 this->writeWord(id, out);
781 this->writeWord(fGLSLExtendedInstructions, out);
782
783 if (is_float(fContext, type)) {
784 this->writeWord(floatInst, out);
785 } else if (is_signed(fContext, type)) {
786 this->writeWord(signedInst, out);
787 } else if (is_unsigned(fContext, type)) {
788 this->writeWord(unsignedInst, out);
789 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400790 SkASSERT(false);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500791 }
792 for (SpvId a : args) {
793 this->writeWord(a, out);
794 }
795}
796
Greg Daniel64773e62016-11-22 09:44:03 -0500797SpvId SPIRVCodeGenerator::writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind,
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400798 OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700799 SpvId result = this->nextId();
800 switch (kind) {
801 case kAtan_SpecialIntrinsic: {
802 std::vector<SpvId> arguments;
803 for (size_t i = 0; i < c.fArguments.size(); i++) {
804 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
805 }
806 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) arguments.size(), out);
ethannicholasd598f792016-07-25 10:08:54 -0700807 this->writeWord(this->getType(c.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -0700808 this->writeWord(result, out);
809 this->writeWord(fGLSLExtendedInstructions, out);
810 this->writeWord(arguments.size() == 2 ? GLSLstd450Atan2 : GLSLstd450Atan, out);
811 for (SpvId id : arguments) {
812 this->writeWord(id, out);
813 }
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400814 break;
815 }
816 case kSubpassLoad_SpecialIntrinsic: {
817 SpvId img = this->writeExpression(*c.fArguments[0], out);
818 std::vector<std::unique_ptr<Expression>> args;
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700819 args.emplace_back(new FloatLiteral(fContext, -1, 0.0));
820 args.emplace_back(new FloatLiteral(fContext, -1, 0.0));
821 Constructor ctor(-1, *fContext.fFloat2_Type, std::move(args));
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400822 SpvId coords = this->writeConstantVector(ctor);
823 if (1 == c.fArguments.size()) {
824 this->writeInstruction(SpvOpImageRead,
825 this->getType(c.fType),
826 result,
827 img,
828 coords,
829 out);
830 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400831 SkASSERT(2 == c.fArguments.size());
Ethan Nicholas0187ae62017-05-03 11:03:44 -0400832 SpvId sample = this->writeExpression(*c.fArguments[1], out);
833 this->writeInstruction(SpvOpImageRead,
834 this->getType(c.fType),
835 result,
836 img,
837 coords,
838 SpvImageOperandsSampleMask,
839 sample,
840 out);
841 }
842 break;
843 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700844 case kTexture_SpecialIntrinsic: {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500845 SpvOp_ op = SpvOpImageSampleImplicitLod;
846 switch (c.fArguments[0]->fType.dimensions()) {
847 case SpvDim1D:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400848 if (c.fArguments[1]->fType == *fContext.fFloat2_Type) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500849 op = SpvOpImageSampleProjImplicitLod;
850 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400851 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500852 }
853 break;
854 case SpvDim2D:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400855 if (c.fArguments[1]->fType == *fContext.fFloat3_Type) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500856 op = SpvOpImageSampleProjImplicitLod;
857 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400858 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat2_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500859 }
860 break;
861 case SpvDim3D:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400862 if (c.fArguments[1]->fType == *fContext.fFloat4_Type) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500863 op = SpvOpImageSampleProjImplicitLod;
864 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400865 SkASSERT(c.fArguments[1]->fType == *fContext.fFloat3_Type);
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500866 }
867 break;
868 case SpvDimCube: // fall through
869 case SpvDimRect: // fall through
870 case SpvDimBuffer: // fall through
871 case SpvDimSubpassData:
872 break;
873 }
ethannicholasd598f792016-07-25 10:08:54 -0700874 SpvId type = this->getType(c.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -0700875 SpvId sampler = this->writeExpression(*c.fArguments[0], out);
876 SpvId uv = this->writeExpression(*c.fArguments[1], out);
877 if (c.fArguments.size() == 3) {
Ethan Nicholas2b3dab62016-11-28 12:03:26 -0500878 this->writeInstruction(op, type, result, sampler, uv,
ethannicholasb3058bd2016-07-01 08:22:01 -0700879 SpvImageOperandsBiasMask,
880 this->writeExpression(*c.fArguments[2], out),
881 out);
882 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400883 SkASSERT(c.fArguments.size() == 2);
Brian Osman8a83ca42018-02-12 14:32:17 -0500884 if (fProgram.fSettings.fSharpenTextures) {
885 FloatLiteral lodBias(fContext, -1, -0.5);
886 this->writeInstruction(op, type, result, sampler, uv,
887 SpvImageOperandsBiasMask,
888 this->writeFloatLiteral(lodBias),
889 out);
890 } else {
891 this->writeInstruction(op, type, result, sampler, uv,
892 out);
893 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700894 }
895 break;
896 }
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500897 case kMod_SpecialIntrinsic: {
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500898 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400899 SkASSERT(args.size() == 2);
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500900 const Type& operandType = c.fArguments[0]->fType;
901 SpvOp_ op;
902 if (is_float(fContext, operandType)) {
903 op = SpvOpFMod;
904 } else if (is_signed(fContext, operandType)) {
905 op = SpvOpSMod;
906 } else if (is_unsigned(fContext, operandType)) {
907 op = SpvOpUMod;
908 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400909 SkASSERT(false);
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500910 return 0;
911 }
912 this->writeOpCode(op, 5, out);
913 this->writeWord(this->getType(operandType), out);
914 this->writeWord(result, out);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500915 this->writeWord(args[0], out);
916 this->writeWord(args[1], out);
917 break;
918 }
Chris Daltonb8af5ad2019-02-25 14:54:21 -0700919 case kDFdy_SpecialIntrinsic: {
920 SpvId fn = this->writeExpression(*c.fArguments[0], out);
921 this->writeOpCode(SpvOpDPdy, 4, out);
922 this->writeWord(this->getType(c.fType), out);
923 this->writeWord(result, out);
924 this->writeWord(fn, out);
925 if (fProgram.fSettings.fFlipY) {
926 // Flipping Y also negates the Y derivatives.
927 SpvId flipped = this->nextId();
928 this->writeInstruction(SpvOpFNegate, this->getType(c.fType), flipped, result, out);
929 return flipped;
930 }
931 break;
932 }
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500933 case kClamp_SpecialIntrinsic: {
934 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400935 SkASSERT(args.size() == 3);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500936 this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FClamp, GLSLstd450SClamp,
937 GLSLstd450UClamp, args, out);
938 break;
939 }
940 case kMax_SpecialIntrinsic: {
941 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400942 SkASSERT(args.size() == 2);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500943 this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FMax, GLSLstd450SMax,
944 GLSLstd450UMax, args, out);
945 break;
946 }
947 case kMin_SpecialIntrinsic: {
948 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400949 SkASSERT(args.size() == 2);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500950 this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FMin, GLSLstd450SMin,
951 GLSLstd450UMin, args, out);
952 break;
953 }
954 case kMix_SpecialIntrinsic: {
955 std::vector<SpvId> args = this->vectorize(c.fArguments, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400956 SkASSERT(args.size() == 3);
Ethan Nicholas0fc07f92018-02-27 15:25:47 -0500957 this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FMix, SpvOpUndef,
958 SpvOpUndef, args, out);
959 break;
Ethan Nicholas70a44b22017-11-30 09:09:16 -0500960 }
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400961 case kSaturate_SpecialIntrinsic: {
962 SkASSERT(c.fArguments.size() == 1);
963 std::vector<std::unique_ptr<Expression>> finalArgs;
964 finalArgs.push_back(c.fArguments[0]->clone());
965 finalArgs.emplace_back(new FloatLiteral(fContext, -1, 0));
966 finalArgs.emplace_back(new FloatLiteral(fContext, -1, 1));
967 std::vector<SpvId> spvArgs = this->vectorize(finalArgs, out);
968 this->writeGLSLExtendedInstruction(c.fType, result, GLSLstd450FClamp, GLSLstd450SClamp,
969 GLSLstd450UClamp, spvArgs, out);
970 break;
971 }
ethannicholasb3058bd2016-07-01 08:22:01 -0700972 }
973 return result;
974}
975
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400976SpvId SPIRVCodeGenerator::writeFunctionCall(const FunctionCall& c, OutputStream& out) {
ethannicholasd598f792016-07-25 10:08:54 -0700977 const auto& entry = fFunctionMap.find(&c.fFunction);
ethannicholasb3058bd2016-07-01 08:22:01 -0700978 if (entry == fFunctionMap.end()) {
979 return this->writeIntrinsicCall(c, out);
980 }
981 // stores (variable, type, lvalue) pairs to extract and save after the function call is complete
982 std::vector<std::tuple<SpvId, SpvId, std::unique_ptr<LValue>>> lvalues;
983 std::vector<SpvId> arguments;
984 for (size_t i = 0; i < c.fArguments.size(); i++) {
Greg Daniel64773e62016-11-22 09:44:03 -0500985 // id of temporary variable that we will use to hold this argument, or 0 if it is being
ethannicholasb3058bd2016-07-01 08:22:01 -0700986 // passed directly
987 SpvId tmpVar;
988 // if we need a temporary var to store this argument, this is the value to store in the var
989 SpvId tmpValueId;
ethannicholasd598f792016-07-25 10:08:54 -0700990 if (is_out(*c.fFunction.fParameters[i])) {
ethannicholasb3058bd2016-07-01 08:22:01 -0700991 std::unique_ptr<LValue> lv = this->getLValue(*c.fArguments[i], out);
992 SpvId ptr = lv->getPointer();
993 if (ptr) {
994 arguments.push_back(ptr);
995 continue;
996 } else {
997 // lvalue cannot simply be read and written via a pointer (e.g. a swizzle). Need to
998 // copy it into a temp, call the function, read the value out of the temp, and then
999 // update the lvalue.
1000 tmpValueId = lv->load(out);
1001 tmpVar = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07001002 lvalues.push_back(std::make_tuple(tmpVar, this->getType(c.fArguments[i]->fType),
ethannicholasb3058bd2016-07-01 08:22:01 -07001003 std::move(lv)));
1004 }
1005 } else {
1006 // see getFunctionType for an explanation of why we're always using pointer parameters
1007 tmpValueId = this->writeExpression(*c.fArguments[i], out);
1008 tmpVar = this->nextId();
1009 }
Greg Daniel64773e62016-11-22 09:44:03 -05001010 this->writeInstruction(SpvOpVariable,
1011 this->getPointerType(c.fArguments[i]->fType,
ethannicholasb3058bd2016-07-01 08:22:01 -07001012 SpvStorageClassFunction),
Greg Daniel64773e62016-11-22 09:44:03 -05001013 tmpVar,
ethannicholasb3058bd2016-07-01 08:22:01 -07001014 SpvStorageClassFunction,
ethannicholasd598f792016-07-25 10:08:54 -07001015 fVariableBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07001016 this->writeInstruction(SpvOpStore, tmpVar, tmpValueId, out);
1017 arguments.push_back(tmpVar);
1018 }
1019 SpvId result = this->nextId();
1020 this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t) c.fArguments.size(), out);
ethannicholasd598f792016-07-25 10:08:54 -07001021 this->writeWord(this->getType(c.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001022 this->writeWord(result, out);
1023 this->writeWord(entry->second, out);
1024 for (SpvId id : arguments) {
1025 this->writeWord(id, out);
1026 }
1027 // now that the call is complete, we may need to update some lvalues with the new values of out
1028 // arguments
1029 for (const auto& tuple : lvalues) {
1030 SpvId load = this->nextId();
1031 this->writeInstruction(SpvOpLoad, std::get<1>(tuple), load, std::get<0>(tuple), out);
1032 std::get<2>(tuple)->store(load, out);
1033 }
1034 return result;
1035}
1036
ethannicholasf789b382016-08-03 12:43:36 -07001037SpvId SPIRVCodeGenerator::writeConstantVector(const Constructor& c) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001038 SkASSERT(c.fType.kind() == Type::kVector_Kind && c.isConstant());
ethannicholasb3058bd2016-07-01 08:22:01 -07001039 SpvId result = this->nextId();
1040 std::vector<SpvId> arguments;
1041 for (size_t i = 0; i < c.fArguments.size(); i++) {
1042 arguments.push_back(this->writeExpression(*c.fArguments[i], fConstantBuffer));
1043 }
ethannicholasd598f792016-07-25 10:08:54 -07001044 SpvId type = this->getType(c.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -07001045 if (c.fArguments.size() == 1) {
1046 // with a single argument, a vector will have all of its entries equal to the argument
ethannicholasd598f792016-07-25 10:08:54 -07001047 this->writeOpCode(SpvOpConstantComposite, 3 + c.fType.columns(), fConstantBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07001048 this->writeWord(type, fConstantBuffer);
1049 this->writeWord(result, fConstantBuffer);
ethannicholasd598f792016-07-25 10:08:54 -07001050 for (int i = 0; i < c.fType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001051 this->writeWord(arguments[0], fConstantBuffer);
1052 }
1053 } else {
Greg Daniel64773e62016-11-22 09:44:03 -05001054 this->writeOpCode(SpvOpConstantComposite, 3 + (int32_t) c.fArguments.size(),
ethannicholasb3058bd2016-07-01 08:22:01 -07001055 fConstantBuffer);
1056 this->writeWord(type, fConstantBuffer);
1057 this->writeWord(result, fConstantBuffer);
1058 for (SpvId id : arguments) {
1059 this->writeWord(id, fConstantBuffer);
1060 }
1061 }
1062 return result;
1063}
1064
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001065SpvId SPIRVCodeGenerator::writeFloatConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001066 SkASSERT(c.fType.isFloat());
1067 SkASSERT(c.fArguments.size() == 1);
1068 SkASSERT(c.fArguments[0]->fType.isNumber());
ethannicholasb3058bd2016-07-01 08:22:01 -07001069 SpvId result = this->nextId();
1070 SpvId parameter = this->writeExpression(*c.fArguments[0], out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001071 if (c.fArguments[0]->fType.isSigned()) {
Greg Daniel64773e62016-11-22 09:44:03 -05001072 this->writeInstruction(SpvOpConvertSToF, this->getType(c.fType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001073 out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001074 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001075 SkASSERT(c.fArguments[0]->fType.isUnsigned());
Greg Daniel64773e62016-11-22 09:44:03 -05001076 this->writeInstruction(SpvOpConvertUToF, this->getType(c.fType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001077 out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001078 }
1079 return result;
1080}
1081
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001082SpvId SPIRVCodeGenerator::writeIntConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001083 SkASSERT(c.fType.isSigned());
1084 SkASSERT(c.fArguments.size() == 1);
1085 SkASSERT(c.fArguments[0]->fType.isNumber());
ethannicholasb3058bd2016-07-01 08:22:01 -07001086 SpvId result = this->nextId();
1087 SpvId parameter = this->writeExpression(*c.fArguments[0], out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001088 if (c.fArguments[0]->fType.isFloat()) {
Greg Daniel64773e62016-11-22 09:44:03 -05001089 this->writeInstruction(SpvOpConvertFToS, this->getType(c.fType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001090 out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001091 }
1092 else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001093 SkASSERT(c.fArguments[0]->fType.isUnsigned());
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001094 this->writeInstruction(SpvOpBitcast, this->getType(c.fType), result, parameter,
ethannicholasb3058bd2016-07-01 08:22:01 -07001095 out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001096 }
1097 return result;
1098}
1099
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001100SpvId SPIRVCodeGenerator::writeUIntConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001101 SkASSERT(c.fType.isUnsigned());
1102 SkASSERT(c.fArguments.size() == 1);
1103 SkASSERT(c.fArguments[0]->fType.isNumber());
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001104 SpvId result = this->nextId();
1105 SpvId parameter = this->writeExpression(*c.fArguments[0], out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001106 if (c.fArguments[0]->fType.isFloat()) {
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001107 this->writeInstruction(SpvOpConvertFToU, this->getType(c.fType), result, parameter,
1108 out);
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001109 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001110 SkASSERT(c.fArguments[0]->fType.isSigned());
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001111 this->writeInstruction(SpvOpBitcast, this->getType(c.fType), result, parameter,
1112 out);
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001113 }
1114 return result;
1115}
1116
Ethan Nicholas84645e32017-02-09 13:57:14 -05001117void SPIRVCodeGenerator::writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001118 OutputStream& out) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001119 FloatLiteral zero(fContext, -1, 0);
Ethan Nicholas84645e32017-02-09 13:57:14 -05001120 SpvId zeroId = this->writeFloatLiteral(zero);
1121 std::vector<SpvId> columnIds;
1122 for (int column = 0; column < type.columns(); column++) {
1123 this->writeOpCode(SpvOpCompositeConstruct, 3 + type.rows(),
1124 out);
1125 this->writeWord(this->getType(type.componentType().toCompound(fContext, type.rows(), 1)),
1126 out);
1127 SpvId columnId = this->nextId();
1128 this->writeWord(columnId, out);
1129 columnIds.push_back(columnId);
1130 for (int row = 0; row < type.columns(); row++) {
1131 this->writeWord(row == column ? diagonal : zeroId, out);
1132 }
1133 }
1134 this->writeOpCode(SpvOpCompositeConstruct, 3 + type.columns(),
1135 out);
1136 this->writeWord(this->getType(type), out);
1137 this->writeWord(id, out);
1138 for (SpvId id : columnIds) {
1139 this->writeWord(id, out);
1140 }
1141}
1142
1143void SPIRVCodeGenerator::writeMatrixCopy(SpvId id, SpvId src, const Type& srcType,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001144 const Type& dstType, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001145 SkASSERT(srcType.kind() == Type::kMatrix_Kind);
1146 SkASSERT(dstType.kind() == Type::kMatrix_Kind);
1147 SkASSERT(srcType.componentType() == dstType.componentType());
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001148 SpvId srcColumnType = this->getType(srcType.componentType().toCompound(fContext,
1149 srcType.rows(),
1150 1));
1151 SpvId dstColumnType = this->getType(dstType.componentType().toCompound(fContext,
1152 dstType.rows(),
1153 1));
1154 SpvId zeroId;
1155 if (dstType.componentType() == *fContext.fFloat_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001156 FloatLiteral zero(fContext, -1, 0.0);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001157 zeroId = this->writeFloatLiteral(zero);
1158 } else if (dstType.componentType() == *fContext.fInt_Type) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001159 IntLiteral zero(fContext, -1, 0);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001160 zeroId = this->writeIntLiteral(zero);
1161 } else {
1162 ABORT("unsupported matrix component type");
1163 }
1164 SpvId zeroColumn = 0;
1165 SpvId columns[4];
1166 for (int i = 0; i < dstType.columns(); i++) {
1167 if (i < srcType.columns()) {
1168 // we're still inside the src matrix, copy the column
1169 SpvId srcColumn = this->nextId();
1170 this->writeInstruction(SpvOpCompositeExtract, srcColumnType, srcColumn, src, i, out);
1171 SpvId dstColumn;
1172 if (srcType.rows() == dstType.rows()) {
1173 // columns are equal size, don't need to do anything
1174 dstColumn = srcColumn;
1175 }
1176 else if (dstType.rows() > srcType.rows()) {
1177 // dst column is bigger, need to zero-pad it
1178 dstColumn = this->nextId();
1179 int delta = dstType.rows() - srcType.rows();
1180 this->writeOpCode(SpvOpCompositeConstruct, 4 + delta, out);
1181 this->writeWord(dstColumnType, out);
1182 this->writeWord(dstColumn, out);
1183 this->writeWord(srcColumn, out);
1184 for (int i = 0; i < delta; ++i) {
1185 this->writeWord(zeroId, out);
1186 }
1187 }
1188 else {
1189 // dst column is smaller, need to swizzle the src column
1190 dstColumn = this->nextId();
1191 int count = dstType.rows();
1192 this->writeOpCode(SpvOpVectorShuffle, 5 + count, out);
1193 this->writeWord(dstColumnType, out);
1194 this->writeWord(dstColumn, out);
1195 this->writeWord(srcColumn, out);
1196 this->writeWord(srcColumn, out);
1197 for (int i = 0; i < count; i++) {
1198 this->writeWord(i, out);
1199 }
1200 }
1201 columns[i] = dstColumn;
1202 } else {
1203 // we're past the end of the src matrix, need a vector of zeroes
1204 if (!zeroColumn) {
1205 zeroColumn = this->nextId();
1206 this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.rows(), out);
1207 this->writeWord(dstColumnType, out);
1208 this->writeWord(zeroColumn, out);
1209 for (int i = 0; i < dstType.rows(); ++i) {
1210 this->writeWord(zeroId, out);
1211 }
1212 }
1213 columns[i] = zeroColumn;
1214 }
1215 }
1216 this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.columns(), out);
1217 this->writeWord(this->getType(dstType), out);
1218 this->writeWord(id, out);
1219 for (int i = 0; i < dstType.columns(); i++) {
1220 this->writeWord(columns[i], out);
1221 }
Ethan Nicholas84645e32017-02-09 13:57:14 -05001222}
1223
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001224SpvId SPIRVCodeGenerator::writeMatrixConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001225 SkASSERT(c.fType.kind() == Type::kMatrix_Kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07001226 // go ahead and write the arguments so we don't try to write new instructions in the middle of
1227 // an instruction
1228 std::vector<SpvId> arguments;
1229 for (size_t i = 0; i < c.fArguments.size(); i++) {
1230 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
1231 }
1232 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07001233 int rows = c.fType.rows();
1234 int columns = c.fType.columns();
Ethan Nicholas84645e32017-02-09 13:57:14 -05001235 if (arguments.size() == 1 && c.fArguments[0]->fType.kind() == Type::kScalar_Kind) {
1236 this->writeUniformScaleMatrix(result, arguments[0], c.fType, out);
1237 } else if (arguments.size() == 1 && c.fArguments[0]->fType.kind() == Type::kMatrix_Kind) {
1238 this->writeMatrixCopy(result, arguments[0], c.fArguments[0]->fType, c.fType, out);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001239 } else if (arguments.size() == 1 && c.fArguments[0]->fType.kind() == Type::kVector_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001240 SkASSERT(c.fType.rows() == 2 && c.fType.columns() == 2);
1241 SkASSERT(c.fArguments[0]->fType.columns() == 4);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001242 SpvId componentType = this->getType(c.fType.componentType());
1243 SpvId v[4];
1244 for (int i = 0; i < 4; ++i) {
1245 v[i] = this->nextId();
1246 this->writeInstruction(SpvOpCompositeExtract, componentType, v[i], arguments[0], i, out);
1247 }
1248 SpvId columnType = this->getType(c.fType.componentType().toCompound(fContext, 2, 1));
1249 SpvId column1 = this->nextId();
1250 this->writeInstruction(SpvOpCompositeConstruct, columnType, column1, v[0], v[1], out);
1251 SpvId column2 = this->nextId();
1252 this->writeInstruction(SpvOpCompositeConstruct, columnType, column2, v[2], v[3], out);
1253 this->writeInstruction(SpvOpCompositeConstruct, this->getType(c.fType), result, column1,
1254 column2, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001255 } else {
1256 std::vector<SpvId> columnIds;
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001257 // ids of vectors and scalars we have written to the current column so far
1258 std::vector<SpvId> currentColumn;
1259 // the total number of scalars represented by currentColumn's entries
ethannicholasb3058bd2016-07-01 08:22:01 -07001260 int currentCount = 0;
1261 for (size_t i = 0; i < arguments.size(); i++) {
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001262 if (c.fArguments[i]->fType.kind() == Type::kVector_Kind &&
1263 c.fArguments[i]->fType.columns() == c.fType.rows()) {
1264 // this is a complete column by itself
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001265 SkASSERT(currentCount == 0);
ethannicholasb3058bd2016-07-01 08:22:01 -07001266 columnIds.push_back(arguments[i]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001267 } else {
Ethan Nicholasbe850ad2018-04-27 10:36:31 -04001268 if (c.fArguments[i]->fType.columns() == 1) {
1269 currentColumn.push_back(arguments[i]);
1270 } else {
1271 SpvId componentType = this->getType(c.fArguments[i]->fType.componentType());
Ethan Nicholas811b9442018-05-11 13:16:03 -04001272 for (int j = 0; j < c.fArguments[i]->fType.columns(); ++j) {
Ethan Nicholasbe850ad2018-04-27 10:36:31 -04001273 SpvId swizzle = this->nextId();
1274 this->writeInstruction(SpvOpCompositeExtract, componentType, swizzle,
1275 arguments[i], j, out);
1276 currentColumn.push_back(swizzle);
1277 }
1278 }
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001279 currentCount += c.fArguments[i]->fType.columns();
1280 if (currentCount == rows) {
1281 currentCount = 0;
1282 this->writeOpCode(SpvOpCompositeConstruct, 3 + currentColumn.size(), out);
Greg Daniel64773e62016-11-22 09:44:03 -05001283 this->writeWord(this->getType(c.fType.componentType().toCompound(fContext, rows,
1284 1)),
ethannicholasb3058bd2016-07-01 08:22:01 -07001285 out);
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001286 SpvId columnId = this->nextId();
1287 this->writeWord(columnId, out);
1288 columnIds.push_back(columnId);
1289 for (SpvId id : currentColumn) {
1290 this->writeWord(id, out);
1291 }
1292 currentColumn.clear();
ethannicholasb3058bd2016-07-01 08:22:01 -07001293 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001294 SkASSERT(currentCount < rows);
ethannicholasb3058bd2016-07-01 08:22:01 -07001295 }
1296 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001297 SkASSERT(columnIds.size() == (size_t) columns);
ethannicholasb3058bd2016-07-01 08:22:01 -07001298 this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, out);
ethannicholasd598f792016-07-25 10:08:54 -07001299 this->writeWord(this->getType(c.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001300 this->writeWord(result, out);
1301 for (SpvId id : columnIds) {
1302 this->writeWord(id, out);
1303 }
1304 }
1305 return result;
1306}
1307
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001308SpvId SPIRVCodeGenerator::writeVectorConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001309 SkASSERT(c.fType.kind() == Type::kVector_Kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07001310 if (c.isConstant()) {
1311 return this->writeConstantVector(c);
1312 }
1313 // go ahead and write the arguments so we don't try to write new instructions in the middle of
1314 // an instruction
1315 std::vector<SpvId> arguments;
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001316 for (size_t i = 0; i < c.fArguments.size(); i++) {
1317 if (c.fArguments[i]->fType.kind() == Type::kVector_Kind) {
1318 // SPIR-V doesn't support vector(vector-of-different-type) directly, so we need to
1319 // extract the components and convert them in that case manually. On top of that,
1320 // as of this writing there's a bug in the Intel Vulkan driver where OpCreateComposite
1321 // doesn't handle vector arguments at all, so we always extract vector components and
1322 // pass them into OpCreateComposite individually.
1323 SpvId vec = this->writeExpression(*c.fArguments[i], out);
1324 SpvOp_ op = SpvOpUndef;
1325 const Type& src = c.fArguments[i]->fType.componentType();
1326 const Type& dst = c.fType.componentType();
1327 if (dst == *fContext.fFloat_Type || dst == *fContext.fHalf_Type) {
1328 if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) {
1329 if (c.fArguments.size() == 1) {
1330 return vec;
1331 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001332 } else if (src == *fContext.fInt_Type ||
1333 src == *fContext.fShort_Type ||
1334 src == *fContext.fByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001335 op = SpvOpConvertSToF;
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001336 } else if (src == *fContext.fUInt_Type ||
1337 src == *fContext.fUShort_Type ||
1338 src == *fContext.fUByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001339 op = SpvOpConvertUToF;
1340 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001341 SkASSERT(false);
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001342 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001343 } else if (dst == *fContext.fInt_Type ||
1344 dst == *fContext.fShort_Type ||
1345 dst == *fContext.fByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001346 if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) {
1347 op = SpvOpConvertFToS;
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001348 } else if (src == *fContext.fInt_Type ||
1349 src == *fContext.fShort_Type ||
1350 src == *fContext.fByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001351 if (c.fArguments.size() == 1) {
1352 return vec;
1353 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001354 } else if (src == *fContext.fUInt_Type ||
1355 src == *fContext.fUShort_Type ||
1356 src == *fContext.fUByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001357 op = SpvOpBitcast;
1358 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001359 SkASSERT(false);
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001360 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001361 } else if (dst == *fContext.fUInt_Type ||
1362 dst == *fContext.fUShort_Type ||
1363 dst == *fContext.fUByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001364 if (src == *fContext.fFloat_Type || src == *fContext.fHalf_Type) {
1365 op = SpvOpConvertFToS;
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001366 } else if (src == *fContext.fInt_Type ||
1367 src == *fContext.fShort_Type ||
1368 src == *fContext.fByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001369 op = SpvOpBitcast;
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001370 } else if (src == *fContext.fUInt_Type ||
1371 src == *fContext.fUShort_Type ||
1372 src == *fContext.fUByte_Type) {
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001373 if (c.fArguments.size() == 1) {
1374 return vec;
1375 }
1376 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001377 SkASSERT(false);
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001378 }
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001379 }
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001380 for (int j = 0; j < c.fArguments[i]->fType.columns(); j++) {
1381 SpvId swizzle = this->nextId();
1382 this->writeInstruction(SpvOpCompositeExtract, this->getType(src), swizzle, vec, j,
1383 out);
1384 if (op != SpvOpUndef) {
1385 SpvId cast = this->nextId();
1386 this->writeInstruction(op, this->getType(dst), cast, swizzle, out);
1387 arguments.push_back(cast);
1388 } else {
1389 arguments.push_back(swizzle);
1390 }
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001391 }
Ethan Nicholas11e5bff2018-01-29 11:08:38 -05001392 } else {
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001393 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
1394 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001395 }
1396 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07001397 if (arguments.size() == 1 && c.fArguments[0]->fType.kind() == Type::kScalar_Kind) {
1398 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType.columns(), out);
1399 this->writeWord(this->getType(c.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001400 this->writeWord(result, out);
ethannicholasd598f792016-07-25 10:08:54 -07001401 for (int i = 0; i < c.fType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001402 this->writeWord(arguments[0], out);
1403 }
1404 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001405 SkASSERT(arguments.size() > 1);
Ethan Nicholas26a8d902018-01-29 09:25:51 -05001406 this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) arguments.size(), out);
ethannicholasd598f792016-07-25 10:08:54 -07001407 this->writeWord(this->getType(c.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001408 this->writeWord(result, out);
1409 for (SpvId id : arguments) {
1410 this->writeWord(id, out);
1411 }
1412 }
1413 return result;
1414}
1415
Ethan Nicholasbd553222017-07-18 15:54:59 -04001416SpvId SPIRVCodeGenerator::writeArrayConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001417 SkASSERT(c.fType.kind() == Type::kArray_Kind);
Ethan Nicholasbd553222017-07-18 15:54:59 -04001418 // go ahead and write the arguments so we don't try to write new instructions in the middle of
1419 // an instruction
1420 std::vector<SpvId> arguments;
1421 for (size_t i = 0; i < c.fArguments.size(); i++) {
1422 arguments.push_back(this->writeExpression(*c.fArguments[i], out));
1423 }
1424 SpvId result = this->nextId();
1425 this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) c.fArguments.size(), out);
1426 this->writeWord(this->getType(c.fType), out);
1427 this->writeWord(result, out);
1428 for (SpvId id : arguments) {
1429 this->writeWord(id, out);
1430 }
1431 return result;
1432}
1433
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001434SpvId SPIRVCodeGenerator::writeConstructor(const Constructor& c, OutputStream& out) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001435 if (c.fArguments.size() == 1 &&
1436 this->getActualType(c.fType) == this->getActualType(c.fArguments[0]->fType)) {
1437 return this->writeExpression(*c.fArguments[0], out);
1438 }
1439 if (c.fType == *fContext.fFloat_Type || c.fType == *fContext.fHalf_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001440 return this->writeFloatConstructor(c, out);
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001441 } else if (c.fType == *fContext.fInt_Type ||
1442 c.fType == *fContext.fShort_Type ||
1443 c.fType == *fContext.fByte_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001444 return this->writeIntConstructor(c, out);
Ruiqi Maob609e6d2018-07-17 10:19:38 -04001445 } else if (c.fType == *fContext.fUInt_Type ||
1446 c.fType == *fContext.fUShort_Type ||
1447 c.fType == *fContext.fUByte_Type) {
Ethan Nicholas925f52d2017-07-19 10:42:50 -04001448 return this->writeUIntConstructor(c, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001449 }
ethannicholasd598f792016-07-25 10:08:54 -07001450 switch (c.fType.kind()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001451 case Type::kVector_Kind:
1452 return this->writeVectorConstructor(c, out);
1453 case Type::kMatrix_Kind:
1454 return this->writeMatrixConstructor(c, out);
Ethan Nicholasbd553222017-07-18 15:54:59 -04001455 case Type::kArray_Kind:
1456 return this->writeArrayConstructor(c, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001457 default:
1458 ABORT("unsupported constructor: %s", c.description().c_str());
1459 }
1460}
1461
1462SpvStorageClass_ get_storage_class(const Modifiers& modifiers) {
1463 if (modifiers.fFlags & Modifiers::kIn_Flag) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001464 SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag));
ethannicholasb3058bd2016-07-01 08:22:01 -07001465 return SpvStorageClassInput;
1466 } else if (modifiers.fFlags & Modifiers::kOut_Flag) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001467 SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag));
ethannicholasb3058bd2016-07-01 08:22:01 -07001468 return SpvStorageClassOutput;
1469 } else if (modifiers.fFlags & Modifiers::kUniform_Flag) {
Ethan Nicholas39204fd2017-11-27 13:12:30 -05001470 if (modifiers.fLayout.fFlags & Layout::kPushConstant_Flag) {
ethannicholas8ac838d2016-11-22 08:39:36 -08001471 return SpvStorageClassPushConstant;
1472 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001473 return SpvStorageClassUniform;
1474 } else {
1475 return SpvStorageClassFunction;
1476 }
1477}
1478
ethannicholasf789b382016-08-03 12:43:36 -07001479SpvStorageClass_ get_storage_class(const Expression& expr) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001480 switch (expr.fKind) {
Ethan Nicholasc6f5e102017-03-31 14:53:17 -04001481 case Expression::kVariableReference_Kind: {
1482 const Variable& var = ((VariableReference&) expr).fVariable;
1483 if (var.fStorage != Variable::kGlobal_Storage) {
1484 return SpvStorageClassFunction;
1485 }
Ethan Nicholasdc0e1c32017-07-21 13:23:34 -04001486 SpvStorageClass_ result = get_storage_class(var.fModifiers);
1487 if (result == SpvStorageClassFunction) {
1488 result = SpvStorageClassPrivate;
1489 }
1490 return result;
Ethan Nicholasc6f5e102017-03-31 14:53:17 -04001491 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001492 case Expression::kFieldAccess_Kind:
1493 return get_storage_class(*((FieldAccess&) expr).fBase);
1494 case Expression::kIndex_Kind:
1495 return get_storage_class(*((IndexExpression&) expr).fBase);
1496 default:
1497 return SpvStorageClassFunction;
1498 }
1499}
1500
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001501std::vector<SpvId> SPIRVCodeGenerator::getAccessChain(const Expression& expr, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001502 std::vector<SpvId> chain;
1503 switch (expr.fKind) {
1504 case Expression::kIndex_Kind: {
1505 IndexExpression& indexExpr = (IndexExpression&) expr;
1506 chain = this->getAccessChain(*indexExpr.fBase, out);
1507 chain.push_back(this->writeExpression(*indexExpr.fIndex, out));
1508 break;
1509 }
1510 case Expression::kFieldAccess_Kind: {
1511 FieldAccess& fieldExpr = (FieldAccess&) expr;
1512 chain = this->getAccessChain(*fieldExpr.fBase, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001513 IntLiteral index(fContext, -1, fieldExpr.fFieldIndex);
ethannicholasb3058bd2016-07-01 08:22:01 -07001514 chain.push_back(this->writeIntLiteral(index));
1515 break;
1516 }
Ethan Nicholasa9a06902019-01-07 14:42:40 -05001517 default: {
1518 SpvId id = this->getLValue(expr, out)->getPointer();
1519 SkASSERT(id != 0);
1520 chain.push_back(id);
1521 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001522 }
1523 return chain;
1524}
1525
1526class PointerLValue : public SPIRVCodeGenerator::LValue {
1527public:
Greg Daniel64773e62016-11-22 09:44:03 -05001528 PointerLValue(SPIRVCodeGenerator& gen, SpvId pointer, SpvId type)
ethannicholasb3058bd2016-07-01 08:22:01 -07001529 : fGen(gen)
1530 , fPointer(pointer)
1531 , fType(type) {}
1532
1533 virtual SpvId getPointer() override {
1534 return fPointer;
1535 }
1536
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001537 virtual SpvId load(OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001538 SpvId result = fGen.nextId();
1539 fGen.writeInstruction(SpvOpLoad, fType, result, fPointer, out);
1540 return result;
1541 }
1542
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001543 virtual void store(SpvId value, OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001544 fGen.writeInstruction(SpvOpStore, fPointer, value, out);
1545 }
1546
1547private:
1548 SPIRVCodeGenerator& fGen;
1549 const SpvId fPointer;
1550 const SpvId fType;
1551};
1552
1553class SwizzleLValue : public SPIRVCodeGenerator::LValue {
1554public:
Greg Daniel64773e62016-11-22 09:44:03 -05001555 SwizzleLValue(SPIRVCodeGenerator& gen, SpvId vecPointer, const std::vector<int>& components,
ethannicholasb3058bd2016-07-01 08:22:01 -07001556 const Type& baseType, const Type& swizzleType)
1557 : fGen(gen)
1558 , fVecPointer(vecPointer)
1559 , fComponents(components)
1560 , fBaseType(baseType)
1561 , fSwizzleType(swizzleType) {}
1562
1563 virtual SpvId getPointer() override {
1564 return 0;
1565 }
1566
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001567 virtual SpvId load(OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001568 SpvId base = fGen.nextId();
1569 fGen.writeInstruction(SpvOpLoad, fGen.getType(fBaseType), base, fVecPointer, out);
1570 SpvId result = fGen.nextId();
1571 fGen.writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) fComponents.size(), out);
1572 fGen.writeWord(fGen.getType(fSwizzleType), out);
1573 fGen.writeWord(result, out);
1574 fGen.writeWord(base, out);
1575 fGen.writeWord(base, out);
1576 for (int component : fComponents) {
1577 fGen.writeWord(component, out);
1578 }
1579 return result;
1580 }
1581
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001582 virtual void store(SpvId value, OutputStream& out) override {
ethannicholasb3058bd2016-07-01 08:22:01 -07001583 // use OpVectorShuffle to mix and match the vector components. We effectively create
1584 // a virtual vector out of the concatenation of the left and right vectors, and then
Greg Daniel64773e62016-11-22 09:44:03 -05001585 // select components from this virtual vector to make the result vector. For
ethannicholasb3058bd2016-07-01 08:22:01 -07001586 // instance, given:
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001587 // float3L = ...;
1588 // float3R = ...;
ethannicholasb3058bd2016-07-01 08:22:01 -07001589 // L.xz = R.xy;
Greg Daniel64773e62016-11-22 09:44:03 -05001590 // 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 -07001591 // our result vector to look like (R.x, L.y, R.y), so we need to select indices
1592 // (3, 1, 4).
1593 SpvId base = fGen.nextId();
1594 fGen.writeInstruction(SpvOpLoad, fGen.getType(fBaseType), base, fVecPointer, out);
1595 SpvId shuffle = fGen.nextId();
1596 fGen.writeOpCode(SpvOpVectorShuffle, 5 + fBaseType.columns(), out);
1597 fGen.writeWord(fGen.getType(fBaseType), out);
1598 fGen.writeWord(shuffle, out);
1599 fGen.writeWord(base, out);
1600 fGen.writeWord(value, out);
1601 for (int i = 0; i < fBaseType.columns(); i++) {
1602 // current offset into the virtual vector, defaults to pulling the unmodified
1603 // value from the left side
1604 int offset = i;
1605 // check to see if we are writing this component
1606 for (size_t j = 0; j < fComponents.size(); j++) {
1607 if (fComponents[j] == i) {
Greg Daniel64773e62016-11-22 09:44:03 -05001608 // we're writing to this component, so adjust the offset to pull from
ethannicholasb3058bd2016-07-01 08:22:01 -07001609 // the correct component of the right side instead of preserving the
1610 // value from the left
1611 offset = (int) (j + fBaseType.columns());
1612 break;
1613 }
1614 }
1615 fGen.writeWord(offset, out);
1616 }
1617 fGen.writeInstruction(SpvOpStore, fVecPointer, shuffle, out);
1618 }
1619
1620private:
1621 SPIRVCodeGenerator& fGen;
1622 const SpvId fVecPointer;
1623 const std::vector<int>& fComponents;
1624 const Type& fBaseType;
1625 const Type& fSwizzleType;
1626};
1627
Greg Daniel64773e62016-11-22 09:44:03 -05001628std::unique_ptr<SPIRVCodeGenerator::LValue> SPIRVCodeGenerator::getLValue(const Expression& expr,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001629 OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001630 switch (expr.fKind) {
1631 case Expression::kVariableReference_Kind: {
Ethan Nicholas5226b772018-05-03 16:20:41 -04001632 SpvId type;
ethannicholasd598f792016-07-25 10:08:54 -07001633 const Variable& var = ((VariableReference&) expr).fVariable;
Ethan Nicholas5226b772018-05-03 16:20:41 -04001634 if (var.fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) {
1635 type = this->getType(Type("sk_in", Type::kArray_Kind, var.fType.componentType(),
1636 fSkInCount));
1637 } else {
1638 type = this->getType(expr.fType);
1639 }
ethannicholasd598f792016-07-25 10:08:54 -07001640 auto entry = fVariableMap.find(&var);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001641 SkASSERT(entry != fVariableMap.end());
Ethan Nicholas5226b772018-05-03 16:20:41 -04001642 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(*this,
1643 entry->second,
1644 type));
ethannicholasb3058bd2016-07-01 08:22:01 -07001645 }
1646 case Expression::kIndex_Kind: // fall through
1647 case Expression::kFieldAccess_Kind: {
1648 std::vector<SpvId> chain = this->getAccessChain(expr, out);
1649 SpvId member = this->nextId();
1650 this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out);
Greg Daniel64773e62016-11-22 09:44:03 -05001651 this->writeWord(this->getPointerType(expr.fType, get_storage_class(expr)), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001652 this->writeWord(member, out);
1653 for (SpvId idx : chain) {
1654 this->writeWord(idx, out);
1655 }
1656 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
Ethan Nicholas8f7e28f2018-03-26 14:24:27 -04001657 *this,
1658 member,
1659 this->getType(expr.fType)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001660 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001661 case Expression::kSwizzle_Kind: {
1662 Swizzle& swizzle = (Swizzle&) expr;
1663 size_t count = swizzle.fComponents.size();
1664 SpvId base = this->getLValue(*swizzle.fBase, out)->getPointer();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001665 SkASSERT(base);
ethannicholasb3058bd2016-07-01 08:22:01 -07001666 if (count == 1) {
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001667 IntLiteral index(fContext, -1, swizzle.fComponents[0]);
ethannicholasb3058bd2016-07-01 08:22:01 -07001668 SpvId member = this->nextId();
1669 this->writeInstruction(SpvOpAccessChain,
Greg Daniel64773e62016-11-22 09:44:03 -05001670 this->getPointerType(swizzle.fType,
1671 get_storage_class(*swizzle.fBase)),
1672 member,
1673 base,
1674 this->writeIntLiteral(index),
ethannicholasb3058bd2016-07-01 08:22:01 -07001675 out);
1676 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
1677 *this,
Greg Daniel64773e62016-11-22 09:44:03 -05001678 member,
ethannicholasd598f792016-07-25 10:08:54 -07001679 this->getType(expr.fType)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001680 } else {
1681 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new SwizzleLValue(
Greg Daniel64773e62016-11-22 09:44:03 -05001682 *this,
1683 base,
1684 swizzle.fComponents,
ethannicholasd598f792016-07-25 10:08:54 -07001685 swizzle.fBase->fType,
1686 expr.fType));
ethannicholasb3058bd2016-07-01 08:22:01 -07001687 }
1688 }
Ethan Nicholasa583b812018-01-18 13:32:11 -05001689 case Expression::kTernary_Kind: {
1690 TernaryExpression& t = (TernaryExpression&) expr;
1691 SpvId test = this->writeExpression(*t.fTest, out);
1692 SpvId end = this->nextId();
1693 SpvId ifTrueLabel = this->nextId();
1694 SpvId ifFalseLabel = this->nextId();
1695 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
1696 this->writeInstruction(SpvOpBranchConditional, test, ifTrueLabel, ifFalseLabel, out);
1697 this->writeLabel(ifTrueLabel, out);
1698 SpvId ifTrue = this->getLValue(*t.fIfTrue, out)->getPointer();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001699 SkASSERT(ifTrue);
Ethan Nicholasa583b812018-01-18 13:32:11 -05001700 this->writeInstruction(SpvOpBranch, end, out);
1701 ifTrueLabel = fCurrentBlock;
1702 SpvId ifFalse = this->getLValue(*t.fIfFalse, out)->getPointer();
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001703 SkASSERT(ifFalse);
Ethan Nicholasa583b812018-01-18 13:32:11 -05001704 ifFalseLabel = fCurrentBlock;
1705 this->writeInstruction(SpvOpBranch, end, out);
1706 SpvId result = this->nextId();
1707 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, ifTrue,
1708 ifTrueLabel, ifFalse, ifFalseLabel, out);
1709 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
1710 *this,
1711 result,
1712 this->getType(expr.fType)));
1713 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001714 default:
1715 // expr isn't actually an lvalue, create a dummy variable for it. This case happens due
Greg Daniel64773e62016-11-22 09:44:03 -05001716 // to the need to store values in temporary variables during function calls (see
ethannicholasb3058bd2016-07-01 08:22:01 -07001717 // comments in getFunctionType); erroneous uses of rvalues as lvalues should have been
1718 // caught by IRGenerator
1719 SpvId result = this->nextId();
1720 SpvId type = this->getPointerType(expr.fType, SpvStorageClassFunction);
ethannicholasd598f792016-07-25 10:08:54 -07001721 this->writeInstruction(SpvOpVariable, type, result, SpvStorageClassFunction,
1722 fVariableBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07001723 this->writeInstruction(SpvOpStore, result, this->writeExpression(expr, out), out);
1724 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue(
1725 *this,
Greg Daniel64773e62016-11-22 09:44:03 -05001726 result,
ethannicholasd598f792016-07-25 10:08:54 -07001727 this->getType(expr.fType)));
ethannicholasb3058bd2016-07-01 08:22:01 -07001728 }
1729}
1730
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001731SpvId SPIRVCodeGenerator::writeVariableReference(const VariableReference& ref, OutputStream& out) {
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001732 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07001733 auto entry = fVariableMap.find(&ref.fVariable);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001734 SkASSERT(entry != fVariableMap.end());
ethannicholasb3058bd2016-07-01 08:22:01 -07001735 SpvId var = entry->second;
ethannicholasd598f792016-07-25 10:08:54 -07001736 this->writeInstruction(SpvOpLoad, this->getType(ref.fVariable.fType), result, var, out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001737 if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN &&
1738 fProgram.fSettings.fFlipY) {
1739 // need to remap to a top-left coordinate system
Greg Daniele6ab9982018-08-22 13:56:32 +00001740 if (fRTHeightStructId == (SpvId) -1) {
1741 // height variable hasn't been written yet
1742 std::shared_ptr<SymbolTable> st(new SymbolTable(&fErrors));
1743 SkASSERT(fRTHeightFieldIndex == (SpvId) -1);
1744 std::vector<Type::Field> fields;
1745 fields.emplace_back(Modifiers(), SKSL_RTHEIGHT_NAME, fContext.fFloat_Type.get());
1746 StringFragment name("sksl_synthetic_uniforms");
1747 Type intfStruct(-1, name, fields);
1748 Layout layout(0, -1, -1, 1, -1, -1, -1, -1, Layout::Format::kUnspecified,
1749 Layout::kUnspecified_Primitive, -1, -1, "", Layout::kNo_Key,
Ethan Nicholas78aceb22018-08-31 16:13:58 -04001750 Layout::CType::kDefault);
Greg Daniele6ab9982018-08-22 13:56:32 +00001751 Variable* intfVar = new Variable(-1,
1752 Modifiers(layout, Modifiers::kUniform_Flag),
1753 name,
1754 intfStruct,
1755 Variable::kGlobal_Storage);
1756 fSynthetics.takeOwnership(intfVar);
1757 InterfaceBlock intf(-1, intfVar, name, String(""),
1758 std::vector<std::unique_ptr<Expression>>(), st);
1759 fRTHeightStructId = this->writeInterfaceBlock(intf);
1760 fRTHeightFieldIndex = 0;
1761 }
1762 SkASSERT(fRTHeightFieldIndex != (SpvId) -1);
Ethan Nicholas20798e52018-12-04 12:30:50 -05001763 // write float4(gl_FragCoord.x, u_skRTHeight - gl_FragCoord.y, 0.0, gl_FragCoord.w)
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001764 SpvId xId = this->nextId();
1765 this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), xId,
1766 result, 0, out);
Greg Daniele6ab9982018-08-22 13:56:32 +00001767 IntLiteral fieldIndex(fContext, -1, fRTHeightFieldIndex);
1768 SpvId fieldIndexId = this->writeIntLiteral(fieldIndex);
1769 SpvId heightPtr = this->nextId();
1770 this->writeOpCode(SpvOpAccessChain, 5, out);
1771 this->writeWord(this->getPointerType(*fContext.fFloat_Type, SpvStorageClassUniform), out);
1772 this->writeWord(heightPtr, out);
1773 this->writeWord(fRTHeightStructId, out);
1774 this->writeWord(fieldIndexId, out);
1775 SpvId heightRead = this->nextId();
1776 this->writeInstruction(SpvOpLoad, this->getType(*fContext.fFloat_Type), heightRead,
1777 heightPtr, out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001778 SpvId rawYId = this->nextId();
1779 this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), rawYId,
1780 result, 1, out);
1781 SpvId flippedYId = this->nextId();
1782 this->writeInstruction(SpvOpFSub, this->getType(*fContext.fFloat_Type), flippedYId,
Greg Daniele6ab9982018-08-22 13:56:32 +00001783 heightRead, rawYId, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001784 FloatLiteral zero(fContext, -1, 0.0);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001785 SpvId zeroId = writeFloatLiteral(zero);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07001786 FloatLiteral one(fContext, -1, 1.0);
Ethan Nicholas20798e52018-12-04 12:30:50 -05001787 SpvId wId = this->nextId();
1788 this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fFloat_Type), wId,
1789 result, 3, out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001790 SpvId flipped = this->nextId();
1791 this->writeOpCode(SpvOpCompositeConstruct, 7, out);
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001792 this->writeWord(this->getType(*fContext.fFloat4_Type), out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001793 this->writeWord(flipped, out);
1794 this->writeWord(xId, out);
1795 this->writeWord(flippedYId, out);
1796 this->writeWord(zeroId, out);
Ethan Nicholas20798e52018-12-04 12:30:50 -05001797 this->writeWord(wId, out);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05001798 return flipped;
1799 }
Chris Daltonb91c4662018-08-01 10:46:22 -06001800 if (ref.fVariable.fModifiers.fLayout.fBuiltin == SK_CLOCKWISE_BUILTIN &&
1801 !fProgram.fSettings.fFlipY) {
1802 // FrontFacing in Vulkan is defined in terms of a top-down render target. In skia, we use
1803 // the default convention of "counter-clockwise face is front".
1804 SpvId inverse = this->nextId();
1805 this->writeInstruction(SpvOpLogicalNot, this->getType(*fContext.fBool_Type), inverse,
1806 result, out);
1807 return inverse;
1808 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001809 return result;
1810}
1811
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001812SpvId SPIRVCodeGenerator::writeIndexExpression(const IndexExpression& expr, OutputStream& out) {
Ethan Nicholasa9a06902019-01-07 14:42:40 -05001813 if (expr.fBase->fType.kind() == Type::Kind::kVector_Kind) {
1814 SpvId base = this->writeExpression(*expr.fBase, out);
1815 SpvId index = this->writeExpression(*expr.fIndex, out);
1816 SpvId result = this->nextId();
1817 this->writeInstruction(SpvOpVectorExtractDynamic, this->getType(expr.fType), result, base,
1818 index, out);
1819 return result;
1820 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001821 return getLValue(expr, out)->load(out);
1822}
1823
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001824SpvId SPIRVCodeGenerator::writeFieldAccess(const FieldAccess& f, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001825 return getLValue(f, out)->load(out);
1826}
1827
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001828SpvId SPIRVCodeGenerator::writeSwizzle(const Swizzle& swizzle, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001829 SpvId base = this->writeExpression(*swizzle.fBase, out);
1830 SpvId result = this->nextId();
1831 size_t count = swizzle.fComponents.size();
1832 if (count == 1) {
Greg Daniel64773e62016-11-22 09:44:03 -05001833 this->writeInstruction(SpvOpCompositeExtract, this->getType(swizzle.fType), result, base,
1834 swizzle.fComponents[0], out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001835 } else {
1836 this->writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) count, out);
ethannicholasd598f792016-07-25 10:08:54 -07001837 this->writeWord(this->getType(swizzle.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001838 this->writeWord(result, out);
1839 this->writeWord(base, out);
Ethan Nicholasac285b12019-02-12 16:05:18 -05001840 SpvId other;
1841 int last = swizzle.fComponents.back();
1842 if (last < 0) {
1843 if (!fConstantZeroOneVector) {
1844 FloatLiteral zero(fContext, -1, 0);
1845 SpvId zeroId = this->writeFloatLiteral(zero);
1846 FloatLiteral one(fContext, -1, 1);
Ethan Nicholas0b2c0542019-02-25 14:29:11 -05001847 SpvId oneId = this->writeFloatLiteral(one);
Ethan Nicholasac285b12019-02-12 16:05:18 -05001848 SpvId type = this->getType(*fContext.fFloat2_Type);
1849 fConstantZeroOneVector = this->nextId();
1850 this->writeOpCode(SpvOpConstantComposite, 5, fConstantBuffer);
1851 this->writeWord(type, fConstantBuffer);
1852 this->writeWord(fConstantZeroOneVector, fConstantBuffer);
1853 this->writeWord(zeroId, fConstantBuffer);
1854 this->writeWord(oneId, fConstantBuffer);
1855 }
1856 other = fConstantZeroOneVector;
1857 } else {
1858 other = base;
1859 }
1860 this->writeWord(other, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07001861 for (int component : swizzle.fComponents) {
Ethan Nicholasac285b12019-02-12 16:05:18 -05001862 if (component == SKSL_SWIZZLE_0) {
Ethan Nicholas0b2c0542019-02-25 14:29:11 -05001863 this->writeWord(swizzle.fBase->fType.columns(), out);
Ethan Nicholasac285b12019-02-12 16:05:18 -05001864 } else if (component == SKSL_SWIZZLE_1) {
Ethan Nicholas0b2c0542019-02-25 14:29:11 -05001865 this->writeWord(swizzle.fBase->fType.columns() + 1, out);
Ethan Nicholasac285b12019-02-12 16:05:18 -05001866 } else {
1867 this->writeWord(component, out);
1868 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001869 }
1870 }
1871 return result;
1872}
1873
Greg Daniel64773e62016-11-22 09:44:03 -05001874SpvId SPIRVCodeGenerator::writeBinaryOperation(const Type& resultType,
1875 const Type& operandType, SpvId lhs,
1876 SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001877 SpvOp_ ifUInt, SpvOp_ ifBool, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001878 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07001879 if (is_float(fContext, operandType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001880 this->writeInstruction(ifFloat, this->getType(resultType), result, lhs, rhs, out);
ethannicholasd598f792016-07-25 10:08:54 -07001881 } else if (is_signed(fContext, operandType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001882 this->writeInstruction(ifInt, this->getType(resultType), result, lhs, rhs, out);
ethannicholasd598f792016-07-25 10:08:54 -07001883 } else if (is_unsigned(fContext, operandType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001884 this->writeInstruction(ifUInt, this->getType(resultType), result, lhs, rhs, out);
ethannicholasd598f792016-07-25 10:08:54 -07001885 } else if (operandType == *fContext.fBool_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001886 this->writeInstruction(ifBool, this->getType(resultType), result, lhs, rhs, out);
1887 } else {
1888 ABORT("invalid operandType: %s", operandType.description().c_str());
1889 }
1890 return result;
1891}
1892
1893bool is_assignment(Token::Kind op) {
1894 switch (op) {
1895 case Token::EQ: // fall through
1896 case Token::PLUSEQ: // fall through
1897 case Token::MINUSEQ: // fall through
1898 case Token::STAREQ: // fall through
1899 case Token::SLASHEQ: // fall through
1900 case Token::PERCENTEQ: // fall through
1901 case Token::SHLEQ: // fall through
1902 case Token::SHREQ: // fall through
1903 case Token::BITWISEOREQ: // fall through
1904 case Token::BITWISEXOREQ: // fall through
1905 case Token::BITWISEANDEQ: // fall through
1906 case Token::LOGICALOREQ: // fall through
1907 case Token::LOGICALXOREQ: // fall through
1908 case Token::LOGICALANDEQ:
1909 return true;
1910 default:
1911 return false;
1912 }
1913}
1914
Ethan Nicholas48e24052018-03-14 13:51:39 -04001915SpvId SPIRVCodeGenerator::foldToBool(SpvId id, const Type& operandType, SpvOp op,
1916 OutputStream& out) {
Ethan Nicholasef653b82017-02-21 13:50:00 -05001917 if (operandType.kind() == Type::kVector_Kind) {
1918 SpvId result = this->nextId();
Ethan Nicholas48e24052018-03-14 13:51:39 -04001919 this->writeInstruction(op, this->getType(*fContext.fBool_Type), result, id, out);
Ethan Nicholasef653b82017-02-21 13:50:00 -05001920 return result;
1921 }
1922 return id;
1923}
1924
Ethan Nicholas68990be2017-07-13 09:36:52 -04001925SpvId SPIRVCodeGenerator::writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs,
1926 SpvOp_ floatOperator, SpvOp_ intOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -04001927 SpvOp_ vectorMergeOperator, SpvOp_ mergeOperator,
Ethan Nicholas68990be2017-07-13 09:36:52 -04001928 OutputStream& out) {
1929 SpvOp_ compareOp = is_float(fContext, operandType) ? floatOperator : intOperator;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001930 SkASSERT(operandType.kind() == Type::kMatrix_Kind);
Ethan Nicholas0df21132018-07-10 09:37:51 -04001931 SpvId columnType = this->getType(operandType.componentType().toCompound(fContext,
1932 operandType.rows(),
1933 1));
Ethan Nicholas68990be2017-07-13 09:36:52 -04001934 SpvId bvecType = this->getType(fContext.fBool_Type->toCompound(fContext,
Ethan Nicholas0df21132018-07-10 09:37:51 -04001935 operandType.rows(),
Ethan Nicholas68990be2017-07-13 09:36:52 -04001936 1));
1937 SpvId boolType = this->getType(*fContext.fBool_Type);
1938 SpvId result = 0;
Ethan Nicholas0df21132018-07-10 09:37:51 -04001939 for (int i = 0; i < operandType.columns(); i++) {
1940 SpvId columnL = this->nextId();
1941 this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out);
1942 SpvId columnR = this->nextId();
1943 this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001944 SpvId compare = this->nextId();
Ethan Nicholas0df21132018-07-10 09:37:51 -04001945 this->writeInstruction(compareOp, bvecType, compare, columnL, columnR, out);
1946 SpvId merge = this->nextId();
1947 this->writeInstruction(vectorMergeOperator, boolType, merge, compare, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001948 if (result != 0) {
1949 SpvId next = this->nextId();
Ethan Nicholas0df21132018-07-10 09:37:51 -04001950 this->writeInstruction(mergeOperator, boolType, next, result, merge, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001951 result = next;
1952 }
1953 else {
Ethan Nicholas0df21132018-07-10 09:37:51 -04001954 result = merge;
Ethan Nicholas68990be2017-07-13 09:36:52 -04001955 }
1956 }
1957 return result;
1958}
1959
Ethan Nicholas0df21132018-07-10 09:37:51 -04001960SpvId SPIRVCodeGenerator::writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs,
1961 SpvId rhs, SpvOp_ floatOperator,
1962 SpvOp_ intOperator,
1963 OutputStream& out) {
1964 SpvOp_ op = is_float(fContext, operandType) ? floatOperator : intOperator;
1965 SkASSERT(operandType.kind() == Type::kMatrix_Kind);
1966 SpvId columnType = this->getType(operandType.componentType().toCompound(fContext,
1967 operandType.rows(),
1968 1));
1969 SpvId columns[4];
1970 for (int i = 0; i < operandType.columns(); i++) {
1971 SpvId columnL = this->nextId();
1972 this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out);
1973 SpvId columnR = this->nextId();
1974 this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out);
1975 columns[i] = this->nextId();
1976 this->writeInstruction(op, columnType, columns[i], columnL, columnR, out);
1977 }
1978 SpvId result = this->nextId();
1979 this->writeOpCode(SpvOpCompositeConstruct, 3 + operandType.columns(), out);
1980 this->writeWord(this->getType(operandType), out);
1981 this->writeWord(result, out);
1982 for (int i = 0; i < operandType.columns(); i++) {
1983 this->writeWord(columns[i], out);
1984 }
1985 return result;
1986}
1987
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001988SpvId SPIRVCodeGenerator::writeBinaryExpression(const BinaryExpression& b, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001989 // handle cases where we don't necessarily evaluate both LHS and RHS
1990 switch (b.fOperator) {
1991 case Token::EQ: {
1992 SpvId rhs = this->writeExpression(*b.fRight, out);
1993 this->getLValue(*b.fLeft, out)->store(rhs, out);
1994 return rhs;
1995 }
1996 case Token::LOGICALAND:
1997 return this->writeLogicalAnd(b, out);
1998 case Token::LOGICALOR:
1999 return this->writeLogicalOr(b, out);
2000 default:
2001 break;
2002 }
2003
2004 // "normal" operators
ethannicholasd598f792016-07-25 10:08:54 -07002005 const Type& resultType = b.fType;
ethannicholasb3058bd2016-07-01 08:22:01 -07002006 std::unique_ptr<LValue> lvalue;
2007 SpvId lhs;
2008 if (is_assignment(b.fOperator)) {
2009 lvalue = this->getLValue(*b.fLeft, out);
2010 lhs = lvalue->load(out);
2011 } else {
2012 lvalue = nullptr;
2013 lhs = this->writeExpression(*b.fLeft, out);
2014 }
2015 SpvId rhs = this->writeExpression(*b.fRight, out);
Ethan Nicholas6feb6912017-06-30 12:23:36 -04002016 if (b.fOperator == Token::COMMA) {
2017 return rhs;
2018 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002019 Type tmp("<invalid>");
Ethan Nicholas48e24052018-03-14 13:51:39 -04002020 // overall type we are operating on: float2, int, uint4...
ethannicholasb3058bd2016-07-01 08:22:01 -07002021 const Type* operandType;
Ethan Nicholas48e24052018-03-14 13:51:39 -04002022 // IR allows mismatched types in expressions (e.g. float2 * float), but they need special
2023 // handling in SPIR-V
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002024 if (this->getActualType(b.fLeft->fType) != this->getActualType(b.fRight->fType)) {
Greg Daniel64773e62016-11-22 09:44:03 -05002025 if (b.fLeft->fType.kind() == Type::kVector_Kind &&
ethannicholasd598f792016-07-25 10:08:54 -07002026 b.fRight->fType.isNumber()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002027 // promote number to vector
2028 SpvId vec = this->nextId();
Ethan Nicholas48e24052018-03-14 13:51:39 -04002029 const Type& vecType = b.fLeft->fType;
2030 this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out);
2031 this->writeWord(this->getType(vecType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002032 this->writeWord(vec, out);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002033 for (int i = 0; i < vecType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002034 this->writeWord(rhs, out);
2035 }
2036 rhs = vec;
Ethan Nicholas48e24052018-03-14 13:51:39 -04002037 operandType = &b.fLeft->fType;
Greg Daniel64773e62016-11-22 09:44:03 -05002038 } else if (b.fRight->fType.kind() == Type::kVector_Kind &&
ethannicholasd598f792016-07-25 10:08:54 -07002039 b.fLeft->fType.isNumber()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002040 // promote number to vector
2041 SpvId vec = this->nextId();
Ethan Nicholas48e24052018-03-14 13:51:39 -04002042 const Type& vecType = b.fRight->fType;
2043 this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out);
2044 this->writeWord(this->getType(vecType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002045 this->writeWord(vec, out);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002046 for (int i = 0; i < vecType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002047 this->writeWord(lhs, out);
2048 }
2049 lhs = vec;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002050 SkASSERT(!lvalue);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002051 operandType = &b.fRight->fType;
ethannicholasd598f792016-07-25 10:08:54 -07002052 } else if (b.fLeft->fType.kind() == Type::kMatrix_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002053 SpvOp_ op;
ethannicholasd598f792016-07-25 10:08:54 -07002054 if (b.fRight->fType.kind() == Type::kMatrix_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002055 op = SpvOpMatrixTimesMatrix;
ethannicholasd598f792016-07-25 10:08:54 -07002056 } else if (b.fRight->fType.kind() == Type::kVector_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002057 op = SpvOpMatrixTimesVector;
2058 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002059 SkASSERT(b.fRight->fType.kind() == Type::kScalar_Kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002060 op = SpvOpMatrixTimesScalar;
2061 }
2062 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002063 this->writeInstruction(op, this->getType(b.fType), result, lhs, rhs, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002064 if (b.fOperator == Token::STAREQ) {
2065 lvalue->store(result, out);
2066 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002067 SkASSERT(b.fOperator == Token::STAR);
ethannicholasb3058bd2016-07-01 08:22:01 -07002068 }
2069 return result;
ethannicholasd598f792016-07-25 10:08:54 -07002070 } else if (b.fRight->fType.kind() == Type::kMatrix_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002071 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002072 if (b.fLeft->fType.kind() == Type::kVector_Kind) {
Greg Daniel64773e62016-11-22 09:44:03 -05002073 this->writeInstruction(SpvOpVectorTimesMatrix, this->getType(b.fType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002074 lhs, rhs, out);
2075 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002076 SkASSERT(b.fLeft->fType.kind() == Type::kScalar_Kind);
Greg Daniel64773e62016-11-22 09:44:03 -05002077 this->writeInstruction(SpvOpMatrixTimesScalar, this->getType(b.fType), result, rhs,
ethannicholasb3058bd2016-07-01 08:22:01 -07002078 lhs, out);
2079 }
2080 if (b.fOperator == Token::STAREQ) {
2081 lvalue->store(result, out);
2082 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002083 SkASSERT(b.fOperator == Token::STAR);
ethannicholasb3058bd2016-07-01 08:22:01 -07002084 }
2085 return result;
2086 } else {
Ethan Nicholase1f55022019-02-05 17:17:40 -05002087 ABORT("unsupported binary expression: %s (%s, %s)", b.description().c_str(),
2088 b.fLeft->fType.description().c_str(), b.fRight->fType.description().c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -07002089 }
2090 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002091 tmp = this->getActualType(b.fLeft->fType);
2092 operandType = &tmp;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002093 SkASSERT(*operandType == this->getActualType(b.fRight->fType));
ethannicholasb3058bd2016-07-01 08:22:01 -07002094 }
2095 switch (b.fOperator) {
Ethan Nicholasef653b82017-02-21 13:50:00 -05002096 case Token::EQEQ: {
Ethan Nicholas68990be2017-07-13 09:36:52 -04002097 if (operandType->kind() == Type::kMatrix_Kind) {
2098 return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdEqual,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002099 SpvOpIEqual, SpvOpAll, SpvOpLogicalAnd, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002100 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002101 SkASSERT(resultType == *fContext.fBool_Type);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002102 const Type* tmpType;
2103 if (operandType->kind() == Type::kVector_Kind) {
2104 tmpType = &fContext.fBool_Type->toCompound(fContext,
2105 operandType->columns(),
2106 operandType->rows());
2107 } else {
2108 tmpType = &resultType;
2109 }
2110 return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs,
Ethan Nicholasef653b82017-02-21 13:50:00 -05002111 SpvOpFOrdEqual, SpvOpIEqual,
2112 SpvOpIEqual, SpvOpLogicalEqual, out),
Ethan Nicholas48e24052018-03-14 13:51:39 -04002113 *operandType, SpvOpAll, out);
Ethan Nicholasef653b82017-02-21 13:50:00 -05002114 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002115 case Token::NEQ:
Ethan Nicholas68990be2017-07-13 09:36:52 -04002116 if (operandType->kind() == Type::kMatrix_Kind) {
2117 return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdNotEqual,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002118 SpvOpINotEqual, SpvOpAny, SpvOpLogicalOr, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002119 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002120 SkASSERT(resultType == *fContext.fBool_Type);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002121 const Type* tmpType;
2122 if (operandType->kind() == Type::kVector_Kind) {
2123 tmpType = &fContext.fBool_Type->toCompound(fContext,
2124 operandType->columns(),
2125 operandType->rows());
2126 } else {
2127 tmpType = &resultType;
2128 }
2129 return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs,
Ethan Nicholasef653b82017-02-21 13:50:00 -05002130 SpvOpFOrdNotEqual, SpvOpINotEqual,
2131 SpvOpINotEqual, SpvOpLogicalNotEqual,
2132 out),
Ethan Nicholas48e24052018-03-14 13:51:39 -04002133 *operandType, SpvOpAny, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002134 case Token::GT:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002135 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002136 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2137 SpvOpFOrdGreaterThan, SpvOpSGreaterThan,
ethannicholasb3058bd2016-07-01 08:22:01 -07002138 SpvOpUGreaterThan, SpvOpUndef, out);
2139 case Token::LT:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002140 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002141 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFOrdLessThan,
ethannicholasb3058bd2016-07-01 08:22:01 -07002142 SpvOpSLessThan, SpvOpULessThan, SpvOpUndef, out);
2143 case Token::GTEQ:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002144 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002145 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2146 SpvOpFOrdGreaterThanEqual, SpvOpSGreaterThanEqual,
ethannicholasb3058bd2016-07-01 08:22:01 -07002147 SpvOpUGreaterThanEqual, SpvOpUndef, out);
2148 case Token::LTEQ:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002149 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002150 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2151 SpvOpFOrdLessThanEqual, SpvOpSLessThanEqual,
ethannicholasb3058bd2016-07-01 08:22:01 -07002152 SpvOpULessThanEqual, SpvOpUndef, out);
2153 case Token::PLUS:
Ethan Nicholas0df21132018-07-10 09:37:51 -04002154 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
2155 b.fRight->fType.kind() == Type::kMatrix_Kind) {
2156 SkASSERT(b.fLeft->fType == b.fRight->fType);
2157 return this->writeComponentwiseMatrixBinary(b.fLeft->fType, lhs, rhs,
2158 SpvOpFAdd, SpvOpIAdd, out);
2159 }
Greg Daniel64773e62016-11-22 09:44:03 -05002160 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFAdd,
ethannicholasb3058bd2016-07-01 08:22:01 -07002161 SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
2162 case Token::MINUS:
Ethan Nicholas0df21132018-07-10 09:37:51 -04002163 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
2164 b.fRight->fType.kind() == Type::kMatrix_Kind) {
2165 SkASSERT(b.fLeft->fType == b.fRight->fType);
2166 return this->writeComponentwiseMatrixBinary(b.fLeft->fType, lhs, rhs,
2167 SpvOpFSub, SpvOpISub, out);
2168 }
Greg Daniel64773e62016-11-22 09:44:03 -05002169 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFSub,
ethannicholasb3058bd2016-07-01 08:22:01 -07002170 SpvOpISub, SpvOpISub, SpvOpUndef, out);
2171 case Token::STAR:
Greg Daniel64773e62016-11-22 09:44:03 -05002172 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
ethannicholasd598f792016-07-25 10:08:54 -07002173 b.fRight->fType.kind() == Type::kMatrix_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002174 // matrix multiply
2175 SpvId result = this->nextId();
2176 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(resultType), result,
2177 lhs, rhs, out);
2178 return result;
2179 }
Greg Daniel64773e62016-11-22 09:44:03 -05002180 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMul,
ethannicholasb3058bd2016-07-01 08:22:01 -07002181 SpvOpIMul, SpvOpIMul, SpvOpUndef, out);
2182 case Token::SLASH:
Greg Daniel64773e62016-11-22 09:44:03 -05002183 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFDiv,
ethannicholasb3058bd2016-07-01 08:22:01 -07002184 SpvOpSDiv, SpvOpUDiv, SpvOpUndef, out);
Ethan Nicholasb3d0f7c2017-05-17 13:13:21 -04002185 case Token::PERCENT:
2186 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMod,
2187 SpvOpSMod, SpvOpUMod, SpvOpUndef, out);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002188 case Token::SHL:
2189 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2190 SpvOpShiftLeftLogical, SpvOpShiftLeftLogical,
2191 SpvOpUndef, out);
2192 case Token::SHR:
2193 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2194 SpvOpShiftRightArithmetic, SpvOpShiftRightLogical,
2195 SpvOpUndef, out);
2196 case Token::BITWISEAND:
2197 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2198 SpvOpBitwiseAnd, SpvOpBitwiseAnd, SpvOpUndef, out);
2199 case Token::BITWISEOR:
2200 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2201 SpvOpBitwiseOr, SpvOpBitwiseOr, SpvOpUndef, out);
2202 case Token::BITWISEXOR:
2203 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2204 SpvOpBitwiseXor, SpvOpBitwiseXor, SpvOpUndef, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002205 case Token::PLUSEQ: {
Ethan Nicholas0df21132018-07-10 09:37:51 -04002206 SpvId result;
2207 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
2208 b.fRight->fType.kind() == Type::kMatrix_Kind) {
2209 SkASSERT(b.fLeft->fType == b.fRight->fType);
2210 result = this->writeComponentwiseMatrixBinary(b.fLeft->fType, lhs, rhs,
2211 SpvOpFAdd, SpvOpIAdd, out);
2212 }
2213 else {
2214 result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFAdd,
ethannicholasb3058bd2016-07-01 08:22:01 -07002215 SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
Ethan Nicholas0df21132018-07-10 09:37:51 -04002216 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002217 SkASSERT(lvalue);
ethannicholasb3058bd2016-07-01 08:22:01 -07002218 lvalue->store(result, out);
2219 return result;
2220 }
2221 case Token::MINUSEQ: {
Ethan Nicholas0df21132018-07-10 09:37:51 -04002222 SpvId result;
2223 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
2224 b.fRight->fType.kind() == Type::kMatrix_Kind) {
2225 SkASSERT(b.fLeft->fType == b.fRight->fType);
2226 result = this->writeComponentwiseMatrixBinary(b.fLeft->fType, lhs, rhs,
2227 SpvOpFSub, SpvOpISub, out);
2228 }
2229 else {
2230 result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFSub,
ethannicholasb3058bd2016-07-01 08:22:01 -07002231 SpvOpISub, SpvOpISub, SpvOpUndef, out);
Ethan Nicholas0df21132018-07-10 09:37:51 -04002232 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002233 SkASSERT(lvalue);
ethannicholasb3058bd2016-07-01 08:22:01 -07002234 lvalue->store(result, out);
2235 return result;
2236 }
2237 case Token::STAREQ: {
Greg Daniel64773e62016-11-22 09:44:03 -05002238 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
ethannicholasd598f792016-07-25 10:08:54 -07002239 b.fRight->fType.kind() == Type::kMatrix_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002240 // matrix multiply
2241 SpvId result = this->nextId();
2242 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(resultType), result,
2243 lhs, rhs, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002244 SkASSERT(lvalue);
ethannicholasb3058bd2016-07-01 08:22:01 -07002245 lvalue->store(result, out);
2246 return result;
2247 }
Greg Daniel64773e62016-11-22 09:44:03 -05002248 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMul,
ethannicholasb3058bd2016-07-01 08:22:01 -07002249 SpvOpIMul, SpvOpIMul, SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002250 SkASSERT(lvalue);
ethannicholasb3058bd2016-07-01 08:22:01 -07002251 lvalue->store(result, out);
2252 return result;
2253 }
2254 case Token::SLASHEQ: {
Greg Daniel64773e62016-11-22 09:44:03 -05002255 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFDiv,
ethannicholasb3058bd2016-07-01 08:22:01 -07002256 SpvOpSDiv, SpvOpUDiv, SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002257 SkASSERT(lvalue);
ethannicholasb3058bd2016-07-01 08:22:01 -07002258 lvalue->store(result, out);
2259 return result;
2260 }
Ethan Nicholasb3d0f7c2017-05-17 13:13:21 -04002261 case Token::PERCENTEQ: {
2262 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMod,
2263 SpvOpSMod, SpvOpUMod, SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002264 SkASSERT(lvalue);
Ethan Nicholasb3d0f7c2017-05-17 13:13:21 -04002265 lvalue->store(result, out);
2266 return result;
2267 }
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002268 case Token::SHLEQ: {
2269 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2270 SpvOpUndef, SpvOpShiftLeftLogical,
2271 SpvOpShiftLeftLogical, SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002272 SkASSERT(lvalue);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002273 lvalue->store(result, out);
2274 return result;
2275 }
2276 case Token::SHREQ: {
2277 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2278 SpvOpUndef, SpvOpShiftRightArithmetic,
2279 SpvOpShiftRightLogical, SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002280 SkASSERT(lvalue);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002281 lvalue->store(result, out);
2282 return result;
2283 }
2284 case Token::BITWISEANDEQ: {
2285 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2286 SpvOpUndef, SpvOpBitwiseAnd, SpvOpBitwiseAnd,
2287 SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002288 SkASSERT(lvalue);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002289 lvalue->store(result, out);
2290 return result;
2291 }
2292 case Token::BITWISEOREQ: {
2293 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2294 SpvOpUndef, SpvOpBitwiseOr, SpvOpBitwiseOr,
2295 SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002296 SkASSERT(lvalue);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002297 lvalue->store(result, out);
2298 return result;
2299 }
2300 case Token::BITWISEXOREQ: {
2301 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2302 SpvOpUndef, SpvOpBitwiseXor, SpvOpBitwiseXor,
2303 SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002304 SkASSERT(lvalue);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002305 lvalue->store(result, out);
2306 return result;
2307 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002308 default:
ethannicholasb3058bd2016-07-01 08:22:01 -07002309 ABORT("unsupported binary expression: %s", b.description().c_str());
2310 }
2311}
2312
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002313SpvId SPIRVCodeGenerator::writeLogicalAnd(const BinaryExpression& a, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002314 SkASSERT(a.fOperator == Token::LOGICALAND);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002315 BoolLiteral falseLiteral(fContext, -1, false);
ethannicholasb3058bd2016-07-01 08:22:01 -07002316 SpvId falseConstant = this->writeBoolLiteral(falseLiteral);
2317 SpvId lhs = this->writeExpression(*a.fLeft, out);
2318 SpvId rhsLabel = this->nextId();
2319 SpvId end = this->nextId();
2320 SpvId lhsBlock = fCurrentBlock;
2321 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2322 this->writeInstruction(SpvOpBranchConditional, lhs, rhsLabel, end, out);
2323 this->writeLabel(rhsLabel, out);
2324 SpvId rhs = this->writeExpression(*a.fRight, out);
2325 SpvId rhsBlock = fCurrentBlock;
2326 this->writeInstruction(SpvOpBranch, end, out);
2327 this->writeLabel(end, out);
2328 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002329 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, falseConstant,
ethannicholasd598f792016-07-25 10:08:54 -07002330 lhsBlock, rhs, rhsBlock, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002331 return result;
2332}
2333
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002334SpvId SPIRVCodeGenerator::writeLogicalOr(const BinaryExpression& o, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002335 SkASSERT(o.fOperator == Token::LOGICALOR);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002336 BoolLiteral trueLiteral(fContext, -1, true);
ethannicholasb3058bd2016-07-01 08:22:01 -07002337 SpvId trueConstant = this->writeBoolLiteral(trueLiteral);
2338 SpvId lhs = this->writeExpression(*o.fLeft, out);
2339 SpvId rhsLabel = this->nextId();
2340 SpvId end = this->nextId();
2341 SpvId lhsBlock = fCurrentBlock;
2342 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2343 this->writeInstruction(SpvOpBranchConditional, lhs, end, rhsLabel, out);
2344 this->writeLabel(rhsLabel, out);
2345 SpvId rhs = this->writeExpression(*o.fRight, out);
2346 SpvId rhsBlock = fCurrentBlock;
2347 this->writeInstruction(SpvOpBranch, end, out);
2348 this->writeLabel(end, out);
2349 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002350 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, trueConstant,
ethannicholasd598f792016-07-25 10:08:54 -07002351 lhsBlock, rhs, rhsBlock, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002352 return result;
2353}
2354
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002355SpvId SPIRVCodeGenerator::writeTernaryExpression(const TernaryExpression& t, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002356 SpvId test = this->writeExpression(*t.fTest, out);
Ethan Nicholas0f21c232018-07-23 09:25:40 -04002357 if (t.fIfTrue->fType.columns() == 1 && t.fIfTrue->isConstant() && t.fIfFalse->isConstant()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002358 // both true and false are constants, can just use OpSelect
2359 SpvId result = this->nextId();
2360 SpvId trueId = this->writeExpression(*t.fIfTrue, out);
2361 SpvId falseId = this->writeExpression(*t.fIfFalse, out);
Greg Daniel64773e62016-11-22 09:44:03 -05002362 this->writeInstruction(SpvOpSelect, this->getType(t.fType), result, test, trueId, falseId,
ethannicholasb3058bd2016-07-01 08:22:01 -07002363 out);
2364 return result;
2365 }
Greg Daniel64773e62016-11-22 09:44:03 -05002366 // was originally using OpPhi to choose the result, but for some reason that is crashing on
ethannicholasb3058bd2016-07-01 08:22:01 -07002367 // Adreno. Switched to storing the result in a temp variable as glslang does.
2368 SpvId var = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002369 this->writeInstruction(SpvOpVariable, this->getPointerType(t.fType, SpvStorageClassFunction),
ethannicholasd598f792016-07-25 10:08:54 -07002370 var, SpvStorageClassFunction, fVariableBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002371 SpvId trueLabel = this->nextId();
2372 SpvId falseLabel = this->nextId();
2373 SpvId end = this->nextId();
2374 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2375 this->writeInstruction(SpvOpBranchConditional, test, trueLabel, falseLabel, out);
2376 this->writeLabel(trueLabel, out);
2377 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfTrue, out), out);
2378 this->writeInstruction(SpvOpBranch, end, out);
2379 this->writeLabel(falseLabel, out);
2380 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfFalse, out), out);
2381 this->writeInstruction(SpvOpBranch, end, out);
2382 this->writeLabel(end, out);
2383 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002384 this->writeInstruction(SpvOpLoad, this->getType(t.fType), result, var, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002385 return result;
2386}
2387
ethannicholasd598f792016-07-25 10:08:54 -07002388std::unique_ptr<Expression> create_literal_1(const Context& context, const Type& type) {
Ethan Nicholas024301a2017-11-10 13:49:18 -05002389 if (type.isInteger()) {
Ethan Nicholas00543112018-07-31 09:44:36 -04002390 return std::unique_ptr<Expression>(new IntLiteral(-1, 1, &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07002391 }
Ethan Nicholas024301a2017-11-10 13:49:18 -05002392 else if (type.isFloat()) {
Ethan Nicholas00543112018-07-31 09:44:36 -04002393 return std::unique_ptr<Expression>(new FloatLiteral(-1, 1.0, &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07002394 } else {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002395 ABORT("math is unsupported on type '%s'", type.name().c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -07002396 }
2397}
2398
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002399SpvId SPIRVCodeGenerator::writePrefixExpression(const PrefixExpression& p, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002400 if (p.fOperator == Token::MINUS) {
2401 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002402 SpvId typeId = this->getType(p.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -07002403 SpvId expr = this->writeExpression(*p.fOperand, out);
ethannicholasd598f792016-07-25 10:08:54 -07002404 if (is_float(fContext, p.fType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002405 this->writeInstruction(SpvOpFNegate, typeId, result, expr, out);
ethannicholasd598f792016-07-25 10:08:54 -07002406 } else if (is_signed(fContext, p.fType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002407 this->writeInstruction(SpvOpSNegate, typeId, result, expr, out);
2408 } else {
2409 ABORT("unsupported prefix expression %s", p.description().c_str());
Brian Salomon23356442018-11-30 15:33:19 -05002410 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002411 return result;
2412 }
2413 switch (p.fOperator) {
2414 case Token::PLUS:
2415 return this->writeExpression(*p.fOperand, out);
2416 case Token::PLUSPLUS: {
2417 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
ethannicholasd598f792016-07-25 10:08:54 -07002418 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out);
Greg Daniel64773e62016-11-22 09:44:03 -05002419 SpvId result = this->writeBinaryOperation(p.fType, p.fType, lv->load(out), one,
2420 SpvOpFAdd, SpvOpIAdd, SpvOpIAdd, SpvOpUndef,
ethannicholasb3058bd2016-07-01 08:22:01 -07002421 out);
2422 lv->store(result, out);
2423 return result;
2424 }
2425 case Token::MINUSMINUS: {
2426 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
ethannicholasd598f792016-07-25 10:08:54 -07002427 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out);
Greg Daniel64773e62016-11-22 09:44:03 -05002428 SpvId result = this->writeBinaryOperation(p.fType, p.fType, lv->load(out), one,
2429 SpvOpFSub, SpvOpISub, SpvOpISub, SpvOpUndef,
ethannicholasb3058bd2016-07-01 08:22:01 -07002430 out);
2431 lv->store(result, out);
2432 return result;
2433 }
ethannicholas5961bc92016-10-12 06:39:56 -07002434 case Token::LOGICALNOT: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002435 SkASSERT(p.fOperand->fType == *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07002436 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002437 this->writeInstruction(SpvOpLogicalNot, this->getType(p.fOperand->fType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002438 this->writeExpression(*p.fOperand, out), out);
2439 return result;
2440 }
ethannicholas5961bc92016-10-12 06:39:56 -07002441 case Token::BITWISENOT: {
2442 SpvId result = this->nextId();
2443 this->writeInstruction(SpvOpNot, this->getType(p.fOperand->fType), result,
2444 this->writeExpression(*p.fOperand, out), out);
2445 return result;
2446 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002447 default:
2448 ABORT("unsupported prefix expression: %s", p.description().c_str());
2449 }
2450}
2451
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002452SpvId SPIRVCodeGenerator::writePostfixExpression(const PostfixExpression& p, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002453 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
2454 SpvId result = lv->load(out);
ethannicholasd598f792016-07-25 10:08:54 -07002455 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002456 switch (p.fOperator) {
2457 case Token::PLUSPLUS: {
Greg Daniel64773e62016-11-22 09:44:03 -05002458 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, one, SpvOpFAdd,
ethannicholasb3058bd2016-07-01 08:22:01 -07002459 SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
2460 lv->store(temp, out);
2461 return result;
2462 }
2463 case Token::MINUSMINUS: {
Greg Daniel64773e62016-11-22 09:44:03 -05002464 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, one, SpvOpFSub,
ethannicholasb3058bd2016-07-01 08:22:01 -07002465 SpvOpISub, SpvOpISub, SpvOpUndef, out);
2466 lv->store(temp, out);
2467 return result;
2468 }
2469 default:
2470 ABORT("unsupported postfix expression %s", p.description().c_str());
2471 }
2472}
2473
ethannicholasf789b382016-08-03 12:43:36 -07002474SpvId SPIRVCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002475 if (b.fValue) {
2476 if (fBoolTrue == 0) {
2477 fBoolTrue = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002478 this->writeInstruction(SpvOpConstantTrue, this->getType(b.fType), fBoolTrue,
ethannicholasb3058bd2016-07-01 08:22:01 -07002479 fConstantBuffer);
2480 }
2481 return fBoolTrue;
2482 } else {
2483 if (fBoolFalse == 0) {
2484 fBoolFalse = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002485 this->writeInstruction(SpvOpConstantFalse, this->getType(b.fType), fBoolFalse,
ethannicholasb3058bd2016-07-01 08:22:01 -07002486 fConstantBuffer);
2487 }
2488 return fBoolFalse;
2489 }
2490}
2491
ethannicholasf789b382016-08-03 12:43:36 -07002492SpvId SPIRVCodeGenerator::writeIntLiteral(const IntLiteral& i) {
ethannicholasd598f792016-07-25 10:08:54 -07002493 if (i.fType == *fContext.fInt_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002494 auto entry = fIntConstants.find(i.fValue);
2495 if (entry == fIntConstants.end()) {
2496 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002497 this->writeInstruction(SpvOpConstant, this->getType(i.fType), result, (SpvId) i.fValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07002498 fConstantBuffer);
2499 fIntConstants[i.fValue] = result;
2500 return result;
2501 }
2502 return entry->second;
2503 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002504 SkASSERT(i.fType == *fContext.fUInt_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07002505 auto entry = fUIntConstants.find(i.fValue);
2506 if (entry == fUIntConstants.end()) {
2507 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002508 this->writeInstruction(SpvOpConstant, this->getType(i.fType), result, (SpvId) i.fValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07002509 fConstantBuffer);
2510 fUIntConstants[i.fValue] = result;
2511 return result;
2512 }
2513 return entry->second;
2514 }
2515}
2516
ethannicholasf789b382016-08-03 12:43:36 -07002517SpvId SPIRVCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
Ethan Nicholase1f55022019-02-05 17:17:40 -05002518 if (f.fType != *fContext.fDouble_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002519 float value = (float) f.fValue;
2520 auto entry = fFloatConstants.find(value);
2521 if (entry == fFloatConstants.end()) {
2522 SpvId result = this->nextId();
2523 uint32_t bits;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002524 SkASSERT(sizeof(bits) == sizeof(value));
ethannicholasb3058bd2016-07-01 08:22:01 -07002525 memcpy(&bits, &value, sizeof(bits));
Greg Daniel64773e62016-11-22 09:44:03 -05002526 this->writeInstruction(SpvOpConstant, this->getType(f.fType), result, bits,
ethannicholasb3058bd2016-07-01 08:22:01 -07002527 fConstantBuffer);
2528 fFloatConstants[value] = result;
2529 return result;
2530 }
2531 return entry->second;
2532 } else {
ethannicholasb3058bd2016-07-01 08:22:01 -07002533 auto entry = fDoubleConstants.find(f.fValue);
2534 if (entry == fDoubleConstants.end()) {
2535 SpvId result = this->nextId();
2536 uint64_t bits;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002537 SkASSERT(sizeof(bits) == sizeof(f.fValue));
ethannicholasb3058bd2016-07-01 08:22:01 -07002538 memcpy(&bits, &f.fValue, sizeof(bits));
Greg Daniel64773e62016-11-22 09:44:03 -05002539 this->writeInstruction(SpvOpConstant, this->getType(f.fType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002540 bits & 0xffffffff, bits >> 32, fConstantBuffer);
2541 fDoubleConstants[f.fValue] = result;
2542 return result;
2543 }
2544 return entry->second;
2545 }
2546}
2547
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002548SpvId SPIRVCodeGenerator::writeFunctionStart(const FunctionDeclaration& f, OutputStream& out) {
ethannicholasd598f792016-07-25 10:08:54 -07002549 SpvId result = fFunctionMap[&f];
Greg Daniel64773e62016-11-22 09:44:03 -05002550 this->writeInstruction(SpvOpFunction, this->getType(f.fReturnType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002551 SpvFunctionControlMaskNone, this->getFunctionType(f), out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002552 this->writeInstruction(SpvOpName, result, f.fName, fNameBuffer);
ethannicholasd598f792016-07-25 10:08:54 -07002553 for (size_t i = 0; i < f.fParameters.size(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002554 SpvId id = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002555 fVariableMap[f.fParameters[i]] = id;
ethannicholasb3058bd2016-07-01 08:22:01 -07002556 SpvId type;
ethannicholasd598f792016-07-25 10:08:54 -07002557 type = this->getPointerType(f.fParameters[i]->fType, SpvStorageClassFunction);
ethannicholasb3058bd2016-07-01 08:22:01 -07002558 this->writeInstruction(SpvOpFunctionParameter, type, id, out);
2559 }
2560 return result;
2561}
2562
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002563SpvId SPIRVCodeGenerator::writeFunction(const FunctionDefinition& f, OutputStream& out) {
2564 fVariableBuffer.reset();
ethannicholasb3058bd2016-07-01 08:22:01 -07002565 SpvId result = this->writeFunctionStart(f.fDeclaration, out);
2566 this->writeLabel(this->nextId(), out);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002567 StringStream bodyBuffer;
Ethan Nicholascb670962017-04-20 19:31:52 -04002568 this->writeBlock((Block&) *f.fBody, bodyBuffer);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002569 write_stringstream(fVariableBuffer, out);
Ethan Nicholas8eb64d32018-12-21 14:50:42 -05002570 if (f.fDeclaration.fName == "main") {
2571 write_stringstream(fGlobalInitializersBuffer, out);
2572 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002573 write_stringstream(bodyBuffer, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002574 if (fCurrentBlock) {
Ethan Nicholas70a44b22017-11-30 09:09:16 -05002575 if (f.fDeclaration.fReturnType == *fContext.fVoid_Type) {
2576 this->writeInstruction(SpvOpReturn, out);
2577 } else {
2578 this->writeInstruction(SpvOpUnreachable, out);
2579 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002580 }
2581 this->writeInstruction(SpvOpFunctionEnd, out);
2582 return result;
2583}
2584
2585void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target) {
2586 if (layout.fLocation >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002587 this->writeInstruction(SpvOpDecorate, target, SpvDecorationLocation, layout.fLocation,
ethannicholasb3058bd2016-07-01 08:22:01 -07002588 fDecorationBuffer);
2589 }
2590 if (layout.fBinding >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002591 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBinding, layout.fBinding,
ethannicholasb3058bd2016-07-01 08:22:01 -07002592 fDecorationBuffer);
2593 }
2594 if (layout.fIndex >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002595 this->writeInstruction(SpvOpDecorate, target, SpvDecorationIndex, layout.fIndex,
ethannicholasb3058bd2016-07-01 08:22:01 -07002596 fDecorationBuffer);
2597 }
2598 if (layout.fSet >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002599 this->writeInstruction(SpvOpDecorate, target, SpvDecorationDescriptorSet, layout.fSet,
ethannicholasb3058bd2016-07-01 08:22:01 -07002600 fDecorationBuffer);
2601 }
Greg Daniel64773e62016-11-22 09:44:03 -05002602 if (layout.fInputAttachmentIndex >= 0) {
2603 this->writeInstruction(SpvOpDecorate, target, SpvDecorationInputAttachmentIndex,
2604 layout.fInputAttachmentIndex, fDecorationBuffer);
Ethan Nicholasbe1099f2018-01-11 16:09:05 -05002605 fCapabilities |= (((uint64_t) 1) << SpvCapabilityInputAttachment);
Greg Daniel64773e62016-11-22 09:44:03 -05002606 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04002607 if (layout.fBuiltin >= 0 && layout.fBuiltin != SK_FRAGCOLOR_BUILTIN &&
Greg Daniele6ab9982018-08-22 13:56:32 +00002608 layout.fBuiltin != SK_IN_BUILTIN && layout.fBuiltin != SK_OUT_BUILTIN) {
Greg Daniel64773e62016-11-22 09:44:03 -05002609 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBuiltIn, layout.fBuiltin,
ethannicholasb3058bd2016-07-01 08:22:01 -07002610 fDecorationBuffer);
2611 }
2612}
2613
2614void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target, int member) {
2615 if (layout.fLocation >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002616 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationLocation,
ethannicholasb3058bd2016-07-01 08:22:01 -07002617 layout.fLocation, fDecorationBuffer);
2618 }
2619 if (layout.fBinding >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002620 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBinding,
ethannicholasb3058bd2016-07-01 08:22:01 -07002621 layout.fBinding, fDecorationBuffer);
2622 }
2623 if (layout.fIndex >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002624 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationIndex,
ethannicholasb3058bd2016-07-01 08:22:01 -07002625 layout.fIndex, fDecorationBuffer);
2626 }
2627 if (layout.fSet >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002628 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationDescriptorSet,
ethannicholasb3058bd2016-07-01 08:22:01 -07002629 layout.fSet, fDecorationBuffer);
2630 }
Greg Daniel64773e62016-11-22 09:44:03 -05002631 if (layout.fInputAttachmentIndex >= 0) {
2632 this->writeInstruction(SpvOpDecorate, target, member, SpvDecorationInputAttachmentIndex,
2633 layout.fInputAttachmentIndex, fDecorationBuffer);
2634 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002635 if (layout.fBuiltin >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002636 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBuiltIn,
ethannicholasb3058bd2016-07-01 08:22:01 -07002637 layout.fBuiltin, fDecorationBuffer);
2638 }
2639}
2640
Ethan Nicholas81d15112018-07-13 12:48:50 -04002641static void update_sk_in_count(const Modifiers& m, int* outSkInCount) {
2642 switch (m.fLayout.fPrimitive) {
2643 case Layout::kPoints_Primitive:
2644 *outSkInCount = 1;
2645 break;
2646 case Layout::kLines_Primitive:
2647 *outSkInCount = 2;
2648 break;
2649 case Layout::kLinesAdjacency_Primitive:
2650 *outSkInCount = 4;
2651 break;
2652 case Layout::kTriangles_Primitive:
2653 *outSkInCount = 3;
2654 break;
2655 case Layout::kTrianglesAdjacency_Primitive:
2656 *outSkInCount = 6;
2657 break;
2658 default:
2659 return;
2660 }
2661}
2662
ethannicholasf789b382016-08-03 12:43:36 -07002663SpvId SPIRVCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) {
Ethan Nicholas361941e2017-05-30 15:41:07 -04002664 bool isBuffer = (0 != (intf.fVariable.fModifiers.fFlags & Modifiers::kBuffer_Flag));
Ethan Nicholas39204fd2017-11-27 13:12:30 -05002665 bool pushConstant = (0 != (intf.fVariable.fModifiers.fLayout.fFlags &
2666 Layout::kPushConstant_Flag));
Ethan Nicholas8d2ba442018-03-16 16:40:36 -04002667 MemoryLayout memoryLayout = (pushConstant || isBuffer) ?
2668 MemoryLayout(MemoryLayout::k430_Standard) :
2669 fDefaultLayout;
ethannicholasb3058bd2016-07-01 08:22:01 -07002670 SpvId result = this->nextId();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002671 const Type* type = &intf.fVariable.fType;
Greg Daniele6ab9982018-08-22 13:56:32 +00002672 if (fProgram.fInputs.fRTHeight) {
2673 SkASSERT(fRTHeightStructId == (SpvId) -1);
2674 SkASSERT(fRTHeightFieldIndex == (SpvId) -1);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002675 std::vector<Type::Field> fields = type->fields();
Greg Daniele6ab9982018-08-22 13:56:32 +00002676 fRTHeightStructId = result;
2677 fRTHeightFieldIndex = fields.size();
2678 fields.emplace_back(Modifiers(), StringFragment(SKSL_RTHEIGHT_NAME), fContext.fFloat_Type.get());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002679 type = new Type(type->fOffset, type->name(), fields);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002680 }
Ethan Nicholas5226b772018-05-03 16:20:41 -04002681 SpvId typeId;
2682 if (intf.fVariable.fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) {
2683 for (const auto& e : fProgram) {
2684 if (e.fKind == ProgramElement::kModifiers_Kind) {
2685 const Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholas81d15112018-07-13 12:48:50 -04002686 update_sk_in_count(m, &fSkInCount);
Ethan Nicholas5226b772018-05-03 16:20:41 -04002687 }
2688 }
2689 typeId = this->getType(Type("sk_in", Type::kArray_Kind, intf.fVariable.fType.componentType(),
2690 fSkInCount), memoryLayout);
2691 } else {
2692 typeId = this->getType(*type, memoryLayout);
2693 }
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04002694 if (intf.fVariable.fModifiers.fFlags & Modifiers::kBuffer_Flag) {
2695 this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBufferBlock, fDecorationBuffer);
Ethan Nicholas6ac8d362019-01-22 21:43:55 +00002696 } else if (intf.fVariable.fModifiers.fLayout.fBuiltin == -1) {
2697 this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBlock, fDecorationBuffer);
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04002698 }
ethannicholasd598f792016-07-25 10:08:54 -07002699 SpvStorageClass_ storageClass = get_storage_class(intf.fVariable.fModifiers);
ethannicholasb3058bd2016-07-01 08:22:01 -07002700 SpvId ptrType = this->nextId();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002701 this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, typeId, fConstantBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002702 this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConstantBuffer);
Ethan Nicholas8d2ba442018-03-16 16:40:36 -04002703 Layout layout = intf.fVariable.fModifiers.fLayout;
2704 if (intf.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag && layout.fSet == -1) {
2705 layout.fSet = 0;
2706 }
2707 this->writeLayout(layout, result);
ethannicholasd598f792016-07-25 10:08:54 -07002708 fVariableMap[&intf.fVariable] = result;
Greg Daniele6ab9982018-08-22 13:56:32 +00002709 if (fProgram.fInputs.fRTHeight) {
Ethan Nicholas39b101b2017-03-01 12:07:28 -05002710 delete type;
2711 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002712 return result;
2713}
2714
Ethan Nicholasa51d7132017-06-09 10:47:31 -04002715void SPIRVCodeGenerator::writePrecisionModifier(const Modifiers& modifiers, SpvId id) {
2716 if ((modifiers.fFlags & Modifiers::kLowp_Flag) |
2717 (modifiers.fFlags & Modifiers::kMediump_Flag)) {
2718 this->writeInstruction(SpvOpDecorate, id, SpvDecorationRelaxedPrecision, fDecorationBuffer);
2719 }
2720}
2721
ethannicholas5961bc92016-10-12 06:39:56 -07002722#define BUILTIN_IGNORE 9999
Greg Daniel64773e62016-11-22 09:44:03 -05002723void SPIRVCodeGenerator::writeGlobalVars(Program::Kind kind, const VarDeclarations& decl,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002724 OutputStream& out) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002725 for (size_t i = 0; i < decl.fVars.size(); i++) {
2726 if (decl.fVars[i]->fKind == Statement::kNop_Kind) {
2727 continue;
2728 }
2729 const VarDeclaration& varDecl = (VarDeclaration&) *decl.fVars[i];
2730 const Variable* var = varDecl.fVar;
Brian Salomonf9f45122016-11-29 11:59:17 -05002731 // These haven't been implemented in our SPIR-V generator yet and we only currently use them
2732 // in the OpenGL backend.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002733 SkASSERT(!(var->fModifiers.fFlags & (Modifiers::kReadOnly_Flag |
Brian Salomonf9f45122016-11-29 11:59:17 -05002734 Modifiers::kWriteOnly_Flag |
2735 Modifiers::kCoherent_Flag |
2736 Modifiers::kVolatile_Flag |
2737 Modifiers::kRestrict_Flag)));
ethannicholas5961bc92016-10-12 06:39:56 -07002738 if (var->fModifiers.fLayout.fBuiltin == BUILTIN_IGNORE) {
2739 continue;
2740 }
2741 if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN &&
2742 kind != Program::kFragment_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002743 SkASSERT(!fProgram.fSettings.fFragColorIsInOut);
ethannicholas5961bc92016-10-12 06:39:56 -07002744 continue;
2745 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05002746 if (!var->fReadCount && !var->fWriteCount &&
ethannicholas14fe8cc2016-09-07 13:37:16 -07002747 !(var->fModifiers.fFlags & (Modifiers::kIn_Flag |
2748 Modifiers::kOut_Flag |
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04002749 Modifiers::kUniform_Flag |
2750 Modifiers::kBuffer_Flag))) {
ethannicholasd598f792016-07-25 10:08:54 -07002751 // variable is dead and not an input / output var (the Vulkan debug layers complain if
2752 // we elide an interface var, even if it's dead)
ethannicholasb3058bd2016-07-01 08:22:01 -07002753 continue;
2754 }
2755 SpvStorageClass_ storageClass;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002756 if (var->fModifiers.fFlags & Modifiers::kIn_Flag) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002757 storageClass = SpvStorageClassInput;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002758 } else if (var->fModifiers.fFlags & Modifiers::kOut_Flag) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002759 storageClass = SpvStorageClassOutput;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002760 } else if (var->fModifiers.fFlags & Modifiers::kUniform_Flag) {
2761 if (var->fType.kind() == Type::kSampler_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002762 storageClass = SpvStorageClassUniformConstant;
2763 } else {
2764 storageClass = SpvStorageClassUniform;
2765 }
2766 } else {
2767 storageClass = SpvStorageClassPrivate;
2768 }
2769 SpvId id = this->nextId();
ethannicholas14fe8cc2016-09-07 13:37:16 -07002770 fVariableMap[var] = id;
Ethan Nicholas5226b772018-05-03 16:20:41 -04002771 SpvId type;
2772 if (var->fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) {
2773 type = this->getPointerType(Type("sk_in", Type::kArray_Kind,
2774 var->fType.componentType(), fSkInCount),
2775 storageClass);
2776 } else {
2777 type = this->getPointerType(var->fType, storageClass);
2778 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002779 this->writeInstruction(SpvOpVariable, type, id, storageClass, fConstantBuffer);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002780 this->writeInstruction(SpvOpName, id, var->fName, fNameBuffer);
Ethan Nicholasa51d7132017-06-09 10:47:31 -04002781 this->writePrecisionModifier(var->fModifiers, id);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002782 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002783 SkASSERT(!fCurrentBlock);
ethannicholasf789b382016-08-03 12:43:36 -07002784 fCurrentBlock = -1;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002785 SpvId value = this->writeExpression(*varDecl.fValue, fGlobalInitializersBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002786 this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuffer);
ethannicholasf789b382016-08-03 12:43:36 -07002787 fCurrentBlock = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -07002788 }
ethannicholas14fe8cc2016-09-07 13:37:16 -07002789 this->writeLayout(var->fModifiers.fLayout, id);
Ethan Nicholas45b0f152017-07-24 14:36:40 -04002790 if (var->fModifiers.fFlags & Modifiers::kFlat_Flag) {
2791 this->writeInstruction(SpvOpDecorate, id, SpvDecorationFlat, fDecorationBuffer);
2792 }
2793 if (var->fModifiers.fFlags & Modifiers::kNoPerspective_Flag) {
2794 this->writeInstruction(SpvOpDecorate, id, SpvDecorationNoPerspective,
2795 fDecorationBuffer);
2796 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002797 }
2798}
2799
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002800void SPIRVCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, OutputStream& out) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002801 for (const auto& stmt : decl.fVars) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002802 SkASSERT(stmt->fKind == Statement::kVarDeclaration_Kind);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002803 VarDeclaration& varDecl = (VarDeclaration&) *stmt;
2804 const Variable* var = varDecl.fVar;
Brian Salomonf9f45122016-11-29 11:59:17 -05002805 // These haven't been implemented in our SPIR-V generator yet and we only currently use them
2806 // in the OpenGL backend.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002807 SkASSERT(!(var->fModifiers.fFlags & (Modifiers::kReadOnly_Flag |
Brian Salomonf9f45122016-11-29 11:59:17 -05002808 Modifiers::kWriteOnly_Flag |
2809 Modifiers::kCoherent_Flag |
2810 Modifiers::kVolatile_Flag |
2811 Modifiers::kRestrict_Flag)));
ethannicholasb3058bd2016-07-01 08:22:01 -07002812 SpvId id = this->nextId();
ethannicholas14fe8cc2016-09-07 13:37:16 -07002813 fVariableMap[var] = id;
2814 SpvId type = this->getPointerType(var->fType, SpvStorageClassFunction);
ethannicholasb3058bd2016-07-01 08:22:01 -07002815 this->writeInstruction(SpvOpVariable, type, id, SpvStorageClassFunction, fVariableBuffer);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002816 this->writeInstruction(SpvOpName, id, var->fName, fNameBuffer);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002817 if (varDecl.fValue) {
2818 SpvId value = this->writeExpression(*varDecl.fValue, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002819 this->writeInstruction(SpvOpStore, id, value, out);
2820 }
2821 }
2822}
2823
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002824void SPIRVCodeGenerator::writeStatement(const Statement& s, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002825 switch (s.fKind) {
Ethan Nicholascb670962017-04-20 19:31:52 -04002826 case Statement::kNop_Kind:
2827 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07002828 case Statement::kBlock_Kind:
2829 this->writeBlock((Block&) s, out);
2830 break;
2831 case Statement::kExpression_Kind:
2832 this->writeExpression(*((ExpressionStatement&) s).fExpression, out);
2833 break;
Greg Daniel64773e62016-11-22 09:44:03 -05002834 case Statement::kReturn_Kind:
ethannicholasb3058bd2016-07-01 08:22:01 -07002835 this->writeReturnStatement((ReturnStatement&) s, out);
2836 break;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002837 case Statement::kVarDeclarations_Kind:
2838 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002839 break;
2840 case Statement::kIf_Kind:
2841 this->writeIfStatement((IfStatement&) s, out);
2842 break;
2843 case Statement::kFor_Kind:
2844 this->writeForStatement((ForStatement&) s, out);
2845 break;
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002846 case Statement::kWhile_Kind:
2847 this->writeWhileStatement((WhileStatement&) s, out);
2848 break;
2849 case Statement::kDo_Kind:
2850 this->writeDoStatement((DoStatement&) s, out);
2851 break;
Ethan Nicholase92b1b12017-11-13 16:13:21 -05002852 case Statement::kSwitch_Kind:
2853 this->writeSwitchStatement((SwitchStatement&) s, out);
2854 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07002855 case Statement::kBreak_Kind:
2856 this->writeInstruction(SpvOpBranch, fBreakTarget.top(), out);
2857 break;
2858 case Statement::kContinue_Kind:
2859 this->writeInstruction(SpvOpBranch, fContinueTarget.top(), out);
2860 break;
2861 case Statement::kDiscard_Kind:
2862 this->writeInstruction(SpvOpKill, out);
2863 break;
2864 default:
2865 ABORT("unsupported statement: %s", s.description().c_str());
2866 }
2867}
2868
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002869void SPIRVCodeGenerator::writeBlock(const Block& b, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002870 for (size_t i = 0; i < b.fStatements.size(); i++) {
2871 this->writeStatement(*b.fStatements[i], out);
2872 }
2873}
2874
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002875void SPIRVCodeGenerator::writeIfStatement(const IfStatement& stmt, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002876 SpvId test = this->writeExpression(*stmt.fTest, out);
2877 SpvId ifTrue = this->nextId();
2878 SpvId ifFalse = this->nextId();
2879 if (stmt.fIfFalse) {
2880 SpvId end = this->nextId();
2881 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2882 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out);
2883 this->writeLabel(ifTrue, out);
2884 this->writeStatement(*stmt.fIfTrue, out);
2885 if (fCurrentBlock) {
2886 this->writeInstruction(SpvOpBranch, end, out);
2887 }
2888 this->writeLabel(ifFalse, out);
2889 this->writeStatement(*stmt.fIfFalse, out);
2890 if (fCurrentBlock) {
2891 this->writeInstruction(SpvOpBranch, end, out);
2892 }
2893 this->writeLabel(end, out);
2894 } else {
2895 this->writeInstruction(SpvOpSelectionMerge, ifFalse, SpvSelectionControlMaskNone, out);
2896 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out);
2897 this->writeLabel(ifTrue, out);
2898 this->writeStatement(*stmt.fIfTrue, out);
2899 if (fCurrentBlock) {
2900 this->writeInstruction(SpvOpBranch, ifFalse, out);
2901 }
2902 this->writeLabel(ifFalse, out);
2903 }
2904}
2905
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002906void SPIRVCodeGenerator::writeForStatement(const ForStatement& f, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002907 if (f.fInitializer) {
2908 this->writeStatement(*f.fInitializer, out);
2909 }
2910 SpvId header = this->nextId();
2911 SpvId start = this->nextId();
2912 SpvId body = this->nextId();
2913 SpvId next = this->nextId();
2914 fContinueTarget.push(next);
2915 SpvId end = this->nextId();
2916 fBreakTarget.push(end);
2917 this->writeInstruction(SpvOpBranch, header, out);
2918 this->writeLabel(header, out);
2919 this->writeInstruction(SpvOpLoopMerge, end, next, SpvLoopControlMaskNone, out);
ethannicholasf789b382016-08-03 12:43:36 -07002920 this->writeInstruction(SpvOpBranch, start, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002921 this->writeLabel(start, out);
ethannicholas22f939e2016-10-13 13:25:34 -07002922 if (f.fTest) {
2923 SpvId test = this->writeExpression(*f.fTest, out);
2924 this->writeInstruction(SpvOpBranchConditional, test, body, end, out);
2925 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002926 this->writeLabel(body, out);
2927 this->writeStatement(*f.fStatement, out);
2928 if (fCurrentBlock) {
2929 this->writeInstruction(SpvOpBranch, next, out);
2930 }
2931 this->writeLabel(next, out);
2932 if (f.fNext) {
2933 this->writeExpression(*f.fNext, out);
2934 }
2935 this->writeInstruction(SpvOpBranch, header, out);
2936 this->writeLabel(end, out);
2937 fBreakTarget.pop();
2938 fContinueTarget.pop();
2939}
2940
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002941void SPIRVCodeGenerator::writeWhileStatement(const WhileStatement& w, OutputStream& out) {
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002942 // We believe the while loop code below will work, but Skia doesn't actually use them and
2943 // adequately testing this code in the absence of Skia exercising it isn't straightforward. For
2944 // the time being, we just fail with an error due to the lack of testing. If you encounter this
2945 // message, simply remove the error call below to see whether our while loop support actually
2946 // works.
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002947 fErrors.error(w.fOffset, "internal error: while loop support has been disabled in SPIR-V, "
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002948 "see SkSLSPIRVCodeGenerator.cpp for details");
2949
2950 SpvId header = this->nextId();
2951 SpvId start = this->nextId();
2952 SpvId body = this->nextId();
2953 fContinueTarget.push(start);
2954 SpvId end = this->nextId();
2955 fBreakTarget.push(end);
2956 this->writeInstruction(SpvOpBranch, header, out);
2957 this->writeLabel(header, out);
2958 this->writeInstruction(SpvOpLoopMerge, end, start, SpvLoopControlMaskNone, out);
2959 this->writeInstruction(SpvOpBranch, start, out);
2960 this->writeLabel(start, out);
2961 SpvId test = this->writeExpression(*w.fTest, out);
2962 this->writeInstruction(SpvOpBranchConditional, test, body, end, out);
2963 this->writeLabel(body, out);
2964 this->writeStatement(*w.fStatement, out);
2965 if (fCurrentBlock) {
2966 this->writeInstruction(SpvOpBranch, start, out);
2967 }
2968 this->writeLabel(end, out);
2969 fBreakTarget.pop();
2970 fContinueTarget.pop();
2971}
2972
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002973void SPIRVCodeGenerator::writeDoStatement(const DoStatement& d, OutputStream& out) {
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002974 // We believe the do loop code below will work, but Skia doesn't actually use them and
2975 // adequately testing this code in the absence of Skia exercising it isn't straightforward. For
2976 // the time being, we just fail with an error due to the lack of testing. If you encounter this
2977 // message, simply remove the error call below to see whether our do loop support actually
2978 // works.
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002979 fErrors.error(d.fOffset, "internal error: do loop support has been disabled in SPIR-V, see "
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002980 "SkSLSPIRVCodeGenerator.cpp for details");
2981
2982 SpvId header = this->nextId();
2983 SpvId start = this->nextId();
2984 SpvId next = this->nextId();
2985 fContinueTarget.push(next);
2986 SpvId end = this->nextId();
2987 fBreakTarget.push(end);
2988 this->writeInstruction(SpvOpBranch, header, out);
2989 this->writeLabel(header, out);
2990 this->writeInstruction(SpvOpLoopMerge, end, start, SpvLoopControlMaskNone, out);
2991 this->writeInstruction(SpvOpBranch, start, out);
2992 this->writeLabel(start, out);
2993 this->writeStatement(*d.fStatement, out);
2994 if (fCurrentBlock) {
2995 this->writeInstruction(SpvOpBranch, next, out);
2996 }
2997 this->writeLabel(next, out);
2998 SpvId test = this->writeExpression(*d.fTest, out);
2999 this->writeInstruction(SpvOpBranchConditional, test, start, end, out);
3000 this->writeLabel(end, out);
3001 fBreakTarget.pop();
3002 fContinueTarget.pop();
3003}
3004
Ethan Nicholase92b1b12017-11-13 16:13:21 -05003005void SPIRVCodeGenerator::writeSwitchStatement(const SwitchStatement& s, OutputStream& out) {
3006 SpvId value = this->writeExpression(*s.fValue, out);
3007 std::vector<SpvId> labels;
3008 SpvId end = this->nextId();
3009 SpvId defaultLabel = end;
3010 fBreakTarget.push(end);
3011 int size = 3;
3012 for (const auto& c : s.fCases) {
3013 SpvId label = this->nextId();
3014 labels.push_back(label);
3015 if (c->fValue) {
3016 size += 2;
3017 } else {
3018 defaultLabel = label;
3019 }
3020 }
3021 labels.push_back(end);
3022 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
3023 this->writeOpCode(SpvOpSwitch, size, out);
3024 this->writeWord(value, out);
3025 this->writeWord(defaultLabel, out);
3026 for (size_t i = 0; i < s.fCases.size(); ++i) {
3027 if (!s.fCases[i]->fValue) {
3028 continue;
3029 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003030 SkASSERT(s.fCases[i]->fValue->fKind == Expression::kIntLiteral_Kind);
Ethan Nicholase92b1b12017-11-13 16:13:21 -05003031 this->writeWord(((IntLiteral&) *s.fCases[i]->fValue).fValue, out);
3032 this->writeWord(labels[i], out);
3033 }
3034 for (size_t i = 0; i < s.fCases.size(); ++i) {
3035 this->writeLabel(labels[i], out);
3036 for (const auto& stmt : s.fCases[i]->fStatements) {
3037 this->writeStatement(*stmt, out);
3038 }
3039 if (fCurrentBlock) {
3040 this->writeInstruction(SpvOpBranch, labels[i + 1], out);
3041 }
3042 }
3043 this->writeLabel(end, out);
3044 fBreakTarget.pop();
3045}
3046
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003047void SPIRVCodeGenerator::writeReturnStatement(const ReturnStatement& r, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07003048 if (r.fExpression) {
Greg Daniel64773e62016-11-22 09:44:03 -05003049 this->writeInstruction(SpvOpReturnValue, this->writeExpression(*r.fExpression, out),
ethannicholasb3058bd2016-07-01 08:22:01 -07003050 out);
3051 } else {
3052 this->writeInstruction(SpvOpReturn, out);
3053 }
3054}
3055
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003056void SPIRVCodeGenerator::writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003057 SkASSERT(fProgram.fKind == Program::kGeometry_Kind);
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003058 int invocations = 1;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003059 for (const auto& e : fProgram) {
3060 if (e.fKind == ProgramElement::kModifiers_Kind) {
3061 const Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003062 if (m.fFlags & Modifiers::kIn_Flag) {
3063 if (m.fLayout.fInvocations != -1) {
3064 invocations = m.fLayout.fInvocations;
3065 }
3066 SpvId input;
3067 switch (m.fLayout.fPrimitive) {
3068 case Layout::kPoints_Primitive:
3069 input = SpvExecutionModeInputPoints;
3070 break;
3071 case Layout::kLines_Primitive:
3072 input = SpvExecutionModeInputLines;
3073 break;
3074 case Layout::kLinesAdjacency_Primitive:
3075 input = SpvExecutionModeInputLinesAdjacency;
3076 break;
3077 case Layout::kTriangles_Primitive:
3078 input = SpvExecutionModeTriangles;
3079 break;
3080 case Layout::kTrianglesAdjacency_Primitive:
3081 input = SpvExecutionModeInputTrianglesAdjacency;
3082 break;
3083 default:
3084 input = 0;
3085 break;
3086 }
Ethan Nicholas81d15112018-07-13 12:48:50 -04003087 update_sk_in_count(m, &fSkInCount);
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003088 if (input) {
3089 this->writeInstruction(SpvOpExecutionMode, entryPoint, input, out);
3090 }
3091 } else if (m.fFlags & Modifiers::kOut_Flag) {
3092 SpvId output;
3093 switch (m.fLayout.fPrimitive) {
3094 case Layout::kPoints_Primitive:
3095 output = SpvExecutionModeOutputPoints;
3096 break;
3097 case Layout::kLineStrip_Primitive:
3098 output = SpvExecutionModeOutputLineStrip;
3099 break;
3100 case Layout::kTriangleStrip_Primitive:
3101 output = SpvExecutionModeOutputTriangleStrip;
3102 break;
3103 default:
3104 output = 0;
3105 break;
3106 }
3107 if (output) {
3108 this->writeInstruction(SpvOpExecutionMode, entryPoint, output, out);
3109 }
3110 if (m.fLayout.fMaxVertices != -1) {
3111 this->writeInstruction(SpvOpExecutionMode, entryPoint,
3112 SpvExecutionModeOutputVertices, m.fLayout.fMaxVertices,
3113 out);
3114 }
3115 }
3116 }
3117 }
3118 this->writeInstruction(SpvOpExecutionMode, entryPoint, SpvExecutionModeInvocations,
3119 invocations, out);
3120}
3121
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003122void SPIRVCodeGenerator::writeInstructions(const Program& program, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07003123 fGLSLExtendedInstructions = this->nextId();
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003124 StringStream body;
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003125 std::set<SpvId> interfaceVars;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003126 // assign IDs to functions, determine sk_in size
3127 int skInSize = -1;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003128 for (const auto& e : program) {
3129 switch (e.fKind) {
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003130 case ProgramElement::kFunction_Kind: {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003131 FunctionDefinition& f = (FunctionDefinition&) e;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003132 fFunctionMap[&f.fDeclaration] = this->nextId();
3133 break;
3134 }
3135 case ProgramElement::kModifiers_Kind: {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003136 Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003137 if (m.fFlags & Modifiers::kIn_Flag) {
3138 switch (m.fLayout.fPrimitive) {
3139 case Layout::kPoints_Primitive: // break
3140 case Layout::kLines_Primitive:
3141 skInSize = 1;
3142 break;
3143 case Layout::kLinesAdjacency_Primitive: // break
3144 skInSize = 2;
3145 break;
3146 case Layout::kTriangles_Primitive: // break
3147 case Layout::kTrianglesAdjacency_Primitive:
3148 skInSize = 3;
3149 break;
3150 default:
3151 break;
3152 }
3153 }
3154 break;
3155 }
3156 default:
3157 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07003158 }
3159 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003160 for (const auto& e : program) {
3161 if (e.fKind == ProgramElement::kInterfaceBlock_Kind) {
3162 InterfaceBlock& intf = (InterfaceBlock&) e;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003163 if (SK_IN_BUILTIN == intf.fVariable.fModifiers.fLayout.fBuiltin) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003164 SkASSERT(skInSize != -1);
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003165 intf.fSizes.emplace_back(new IntLiteral(fContext, -1, skInSize));
3166 }
ethannicholasb3058bd2016-07-01 08:22:01 -07003167 SpvId id = this->writeInterfaceBlock(intf);
Ethan Nicholas16c11962018-03-16 12:20:54 -04003168 if (((intf.fVariable.fModifiers.fFlags & Modifiers::kIn_Flag) ||
3169 (intf.fVariable.fModifiers.fFlags & Modifiers::kOut_Flag)) &&
3170 intf.fVariable.fModifiers.fLayout.fBuiltin == -1) {
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003171 interfaceVars.insert(id);
ethannicholasb3058bd2016-07-01 08:22:01 -07003172 }
3173 }
3174 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003175 for (const auto& e : program) {
3176 if (e.fKind == ProgramElement::kVar_Kind) {
3177 this->writeGlobalVars(program.fKind, ((VarDeclarations&) e), body);
ethannicholasb3058bd2016-07-01 08:22:01 -07003178 }
3179 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003180 for (const auto& e : program) {
3181 if (e.fKind == ProgramElement::kFunction_Kind) {
3182 this->writeFunction(((FunctionDefinition&) e), body);
ethannicholasb3058bd2016-07-01 08:22:01 -07003183 }
3184 }
ethannicholasd598f792016-07-25 10:08:54 -07003185 const FunctionDeclaration* main = nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07003186 for (auto entry : fFunctionMap) {
ethannicholasf789b382016-08-03 12:43:36 -07003187 if (entry.first->fName == "main") {
ethannicholasb3058bd2016-07-01 08:22:01 -07003188 main = entry.first;
3189 }
3190 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003191 SkASSERT(main);
ethannicholasb3058bd2016-07-01 08:22:01 -07003192 for (auto entry : fVariableMap) {
ethannicholasd598f792016-07-25 10:08:54 -07003193 const Variable* var = entry.first;
Greg Daniel64773e62016-11-22 09:44:03 -05003194 if (var->fStorage == Variable::kGlobal_Storage &&
Ethan Nicholas16c11962018-03-16 12:20:54 -04003195 ((var->fModifiers.fFlags & Modifiers::kIn_Flag) ||
Ethan Nicholasd23c8192018-09-26 17:01:24 -04003196 (var->fModifiers.fFlags & Modifiers::kOut_Flag))) {
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003197 interfaceVars.insert(entry.second);
ethannicholasb3058bd2016-07-01 08:22:01 -07003198 }
3199 }
3200 this->writeCapabilities(out);
3201 this->writeInstruction(SpvOpExtInstImport, fGLSLExtendedInstructions, "GLSL.std.450", out);
3202 this->writeInstruction(SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemoryModelGLSL450, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07003203 this->writeOpCode(SpvOpEntryPoint, (SpvId) (3 + (main->fName.fLength + 4) / 4) +
ethannicholasb3058bd2016-07-01 08:22:01 -07003204 (int32_t) interfaceVars.size(), out);
3205 switch (program.fKind) {
3206 case Program::kVertex_Kind:
3207 this->writeWord(SpvExecutionModelVertex, out);
3208 break;
3209 case Program::kFragment_Kind:
3210 this->writeWord(SpvExecutionModelFragment, out);
3211 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05003212 case Program::kGeometry_Kind:
3213 this->writeWord(SpvExecutionModelGeometry, out);
3214 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04003215 default:
3216 ABORT("cannot write this kind of program to SPIR-V\n");
ethannicholasb3058bd2016-07-01 08:22:01 -07003217 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003218 SpvId entryPoint = fFunctionMap[main];
3219 this->writeWord(entryPoint, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07003220 this->writeString(main->fName.fChars, main->fName.fLength, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003221 for (int var : interfaceVars) {
3222 this->writeWord(var, out);
3223 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003224 if (program.fKind == Program::kGeometry_Kind) {
3225 this->writeGeometryShaderExecutionMode(entryPoint, out);
3226 }
ethannicholasb3058bd2016-07-01 08:22:01 -07003227 if (program.fKind == Program::kFragment_Kind) {
Greg Daniel64773e62016-11-22 09:44:03 -05003228 this->writeInstruction(SpvOpExecutionMode,
3229 fFunctionMap[main],
ethannicholasb3058bd2016-07-01 08:22:01 -07003230 SpvExecutionModeOriginUpperLeft,
3231 out);
3232 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003233 for (const auto& e : program) {
3234 if (e.fKind == ProgramElement::kExtension_Kind) {
3235 this->writeInstruction(SpvOpSourceExtension, ((Extension&) e).fName.c_str(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003236 }
3237 }
Greg Daniel64773e62016-11-22 09:44:03 -05003238
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003239 write_stringstream(fExtraGlobalsBuffer, out);
3240 write_stringstream(fNameBuffer, out);
3241 write_stringstream(fDecorationBuffer, out);
3242 write_stringstream(fConstantBuffer, out);
3243 write_stringstream(fExternalFunctionsBuffer, out);
3244 write_stringstream(body, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003245}
3246
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003247bool SPIRVCodeGenerator::generateCode() {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003248 SkASSERT(!fErrors.errorCount());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003249 this->writeWord(SpvMagicNumber, *fOut);
3250 this->writeWord(SpvVersion, *fOut);
3251 this->writeWord(SKSL_MAGIC, *fOut);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003252 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003253 this->writeInstructions(fProgram, buffer);
3254 this->writeWord(fIdCount, *fOut);
3255 this->writeWord(0, *fOut); // reserved, always zero
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003256 write_stringstream(buffer, *fOut);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003257 return 0 == fErrors.errorCount();
ethannicholasb3058bd2016-07-01 08:22:01 -07003258}
3259
3260}