blob: c03b1ce4285c033d62648ef362c1775224d22507 [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);
Ethan Nicholas858fecc2019-03-07 13:19:18 -05001887 return result; // skip RelaxedPrecision check
ethannicholasb3058bd2016-07-01 08:22:01 -07001888 } else {
1889 ABORT("invalid operandType: %s", operandType.description().c_str());
1890 }
Ethan Nicholas858fecc2019-03-07 13:19:18 -05001891 if (resultType == operandType && !resultType.highPrecision()) {
1892 this->writeInstruction(SpvOpDecorate, result, SpvDecorationRelaxedPrecision,
1893 fDecorationBuffer);
1894 }
ethannicholasb3058bd2016-07-01 08:22:01 -07001895 return result;
1896}
1897
1898bool is_assignment(Token::Kind op) {
1899 switch (op) {
1900 case Token::EQ: // fall through
1901 case Token::PLUSEQ: // fall through
1902 case Token::MINUSEQ: // fall through
1903 case Token::STAREQ: // fall through
1904 case Token::SLASHEQ: // fall through
1905 case Token::PERCENTEQ: // fall through
1906 case Token::SHLEQ: // fall through
1907 case Token::SHREQ: // fall through
1908 case Token::BITWISEOREQ: // fall through
1909 case Token::BITWISEXOREQ: // fall through
1910 case Token::BITWISEANDEQ: // fall through
1911 case Token::LOGICALOREQ: // fall through
1912 case Token::LOGICALXOREQ: // fall through
1913 case Token::LOGICALANDEQ:
1914 return true;
1915 default:
1916 return false;
1917 }
1918}
1919
Ethan Nicholas48e24052018-03-14 13:51:39 -04001920SpvId SPIRVCodeGenerator::foldToBool(SpvId id, const Type& operandType, SpvOp op,
1921 OutputStream& out) {
Ethan Nicholasef653b82017-02-21 13:50:00 -05001922 if (operandType.kind() == Type::kVector_Kind) {
1923 SpvId result = this->nextId();
Ethan Nicholas48e24052018-03-14 13:51:39 -04001924 this->writeInstruction(op, this->getType(*fContext.fBool_Type), result, id, out);
Ethan Nicholasef653b82017-02-21 13:50:00 -05001925 return result;
1926 }
1927 return id;
1928}
1929
Ethan Nicholas68990be2017-07-13 09:36:52 -04001930SpvId SPIRVCodeGenerator::writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs,
1931 SpvOp_ floatOperator, SpvOp_ intOperator,
Ethan Nicholas0df21132018-07-10 09:37:51 -04001932 SpvOp_ vectorMergeOperator, SpvOp_ mergeOperator,
Ethan Nicholas68990be2017-07-13 09:36:52 -04001933 OutputStream& out) {
1934 SpvOp_ compareOp = is_float(fContext, operandType) ? floatOperator : intOperator;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04001935 SkASSERT(operandType.kind() == Type::kMatrix_Kind);
Ethan Nicholas0df21132018-07-10 09:37:51 -04001936 SpvId columnType = this->getType(operandType.componentType().toCompound(fContext,
1937 operandType.rows(),
1938 1));
Ethan Nicholas68990be2017-07-13 09:36:52 -04001939 SpvId bvecType = this->getType(fContext.fBool_Type->toCompound(fContext,
Ethan Nicholas0df21132018-07-10 09:37:51 -04001940 operandType.rows(),
Ethan Nicholas68990be2017-07-13 09:36:52 -04001941 1));
1942 SpvId boolType = this->getType(*fContext.fBool_Type);
1943 SpvId result = 0;
Ethan Nicholas0df21132018-07-10 09:37:51 -04001944 for (int i = 0; i < operandType.columns(); i++) {
1945 SpvId columnL = this->nextId();
1946 this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out);
1947 SpvId columnR = this->nextId();
1948 this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001949 SpvId compare = this->nextId();
Ethan Nicholas0df21132018-07-10 09:37:51 -04001950 this->writeInstruction(compareOp, bvecType, compare, columnL, columnR, out);
1951 SpvId merge = this->nextId();
1952 this->writeInstruction(vectorMergeOperator, boolType, merge, compare, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001953 if (result != 0) {
1954 SpvId next = this->nextId();
Ethan Nicholas0df21132018-07-10 09:37:51 -04001955 this->writeInstruction(mergeOperator, boolType, next, result, merge, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04001956 result = next;
1957 }
1958 else {
Ethan Nicholas0df21132018-07-10 09:37:51 -04001959 result = merge;
Ethan Nicholas68990be2017-07-13 09:36:52 -04001960 }
1961 }
1962 return result;
1963}
1964
Ethan Nicholas0df21132018-07-10 09:37:51 -04001965SpvId SPIRVCodeGenerator::writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs,
1966 SpvId rhs, SpvOp_ floatOperator,
1967 SpvOp_ intOperator,
1968 OutputStream& out) {
1969 SpvOp_ op = is_float(fContext, operandType) ? floatOperator : intOperator;
1970 SkASSERT(operandType.kind() == Type::kMatrix_Kind);
1971 SpvId columnType = this->getType(operandType.componentType().toCompound(fContext,
1972 operandType.rows(),
1973 1));
1974 SpvId columns[4];
1975 for (int i = 0; i < operandType.columns(); i++) {
1976 SpvId columnL = this->nextId();
1977 this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out);
1978 SpvId columnR = this->nextId();
1979 this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out);
1980 columns[i] = this->nextId();
1981 this->writeInstruction(op, columnType, columns[i], columnL, columnR, out);
1982 }
1983 SpvId result = this->nextId();
1984 this->writeOpCode(SpvOpCompositeConstruct, 3 + operandType.columns(), out);
1985 this->writeWord(this->getType(operandType), out);
1986 this->writeWord(result, out);
1987 for (int i = 0; i < operandType.columns(); i++) {
1988 this->writeWord(columns[i], out);
1989 }
1990 return result;
1991}
1992
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001993SpvId SPIRVCodeGenerator::writeBinaryExpression(const BinaryExpression& b, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07001994 // handle cases where we don't necessarily evaluate both LHS and RHS
1995 switch (b.fOperator) {
1996 case Token::EQ: {
1997 SpvId rhs = this->writeExpression(*b.fRight, out);
1998 this->getLValue(*b.fLeft, out)->store(rhs, out);
1999 return rhs;
2000 }
2001 case Token::LOGICALAND:
2002 return this->writeLogicalAnd(b, out);
2003 case Token::LOGICALOR:
2004 return this->writeLogicalOr(b, out);
2005 default:
2006 break;
2007 }
2008
2009 // "normal" operators
ethannicholasd598f792016-07-25 10:08:54 -07002010 const Type& resultType = b.fType;
ethannicholasb3058bd2016-07-01 08:22:01 -07002011 std::unique_ptr<LValue> lvalue;
2012 SpvId lhs;
2013 if (is_assignment(b.fOperator)) {
2014 lvalue = this->getLValue(*b.fLeft, out);
2015 lhs = lvalue->load(out);
2016 } else {
2017 lvalue = nullptr;
2018 lhs = this->writeExpression(*b.fLeft, out);
2019 }
2020 SpvId rhs = this->writeExpression(*b.fRight, out);
Ethan Nicholas6feb6912017-06-30 12:23:36 -04002021 if (b.fOperator == Token::COMMA) {
2022 return rhs;
2023 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002024 Type tmp("<invalid>");
Ethan Nicholas48e24052018-03-14 13:51:39 -04002025 // overall type we are operating on: float2, int, uint4...
ethannicholasb3058bd2016-07-01 08:22:01 -07002026 const Type* operandType;
Ethan Nicholas48e24052018-03-14 13:51:39 -04002027 // IR allows mismatched types in expressions (e.g. float2 * float), but they need special
2028 // handling in SPIR-V
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002029 if (this->getActualType(b.fLeft->fType) != this->getActualType(b.fRight->fType)) {
Greg Daniel64773e62016-11-22 09:44:03 -05002030 if (b.fLeft->fType.kind() == Type::kVector_Kind &&
ethannicholasd598f792016-07-25 10:08:54 -07002031 b.fRight->fType.isNumber()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002032 // promote number to vector
2033 SpvId vec = this->nextId();
Ethan Nicholas48e24052018-03-14 13:51:39 -04002034 const Type& vecType = b.fLeft->fType;
2035 this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out);
2036 this->writeWord(this->getType(vecType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002037 this->writeWord(vec, out);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002038 for (int i = 0; i < vecType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002039 this->writeWord(rhs, out);
2040 }
2041 rhs = vec;
Ethan Nicholas48e24052018-03-14 13:51:39 -04002042 operandType = &b.fLeft->fType;
Greg Daniel64773e62016-11-22 09:44:03 -05002043 } else if (b.fRight->fType.kind() == Type::kVector_Kind &&
ethannicholasd598f792016-07-25 10:08:54 -07002044 b.fLeft->fType.isNumber()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002045 // promote number to vector
2046 SpvId vec = this->nextId();
Ethan Nicholas48e24052018-03-14 13:51:39 -04002047 const Type& vecType = b.fRight->fType;
2048 this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out);
2049 this->writeWord(this->getType(vecType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002050 this->writeWord(vec, out);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002051 for (int i = 0; i < vecType.columns(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002052 this->writeWord(lhs, out);
2053 }
2054 lhs = vec;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002055 SkASSERT(!lvalue);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002056 operandType = &b.fRight->fType;
ethannicholasd598f792016-07-25 10:08:54 -07002057 } else if (b.fLeft->fType.kind() == Type::kMatrix_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002058 SpvOp_ op;
ethannicholasd598f792016-07-25 10:08:54 -07002059 if (b.fRight->fType.kind() == Type::kMatrix_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002060 op = SpvOpMatrixTimesMatrix;
ethannicholasd598f792016-07-25 10:08:54 -07002061 } else if (b.fRight->fType.kind() == Type::kVector_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002062 op = SpvOpMatrixTimesVector;
2063 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002064 SkASSERT(b.fRight->fType.kind() == Type::kScalar_Kind);
ethannicholasb3058bd2016-07-01 08:22:01 -07002065 op = SpvOpMatrixTimesScalar;
2066 }
2067 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002068 this->writeInstruction(op, this->getType(b.fType), result, lhs, rhs, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002069 if (b.fOperator == Token::STAREQ) {
2070 lvalue->store(result, out);
2071 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002072 SkASSERT(b.fOperator == Token::STAR);
ethannicholasb3058bd2016-07-01 08:22:01 -07002073 }
2074 return result;
ethannicholasd598f792016-07-25 10:08:54 -07002075 } else if (b.fRight->fType.kind() == Type::kMatrix_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002076 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002077 if (b.fLeft->fType.kind() == Type::kVector_Kind) {
Greg Daniel64773e62016-11-22 09:44:03 -05002078 this->writeInstruction(SpvOpVectorTimesMatrix, this->getType(b.fType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002079 lhs, rhs, out);
2080 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002081 SkASSERT(b.fLeft->fType.kind() == Type::kScalar_Kind);
Greg Daniel64773e62016-11-22 09:44:03 -05002082 this->writeInstruction(SpvOpMatrixTimesScalar, this->getType(b.fType), result, rhs,
ethannicholasb3058bd2016-07-01 08:22:01 -07002083 lhs, out);
2084 }
2085 if (b.fOperator == Token::STAREQ) {
2086 lvalue->store(result, out);
2087 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002088 SkASSERT(b.fOperator == Token::STAR);
ethannicholasb3058bd2016-07-01 08:22:01 -07002089 }
2090 return result;
2091 } else {
Ethan Nicholase1f55022019-02-05 17:17:40 -05002092 ABORT("unsupported binary expression: %s (%s, %s)", b.description().c_str(),
2093 b.fLeft->fType.description().c_str(), b.fRight->fType.description().c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -07002094 }
2095 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -04002096 tmp = this->getActualType(b.fLeft->fType);
2097 operandType = &tmp;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002098 SkASSERT(*operandType == this->getActualType(b.fRight->fType));
ethannicholasb3058bd2016-07-01 08:22:01 -07002099 }
2100 switch (b.fOperator) {
Ethan Nicholasef653b82017-02-21 13:50:00 -05002101 case Token::EQEQ: {
Ethan Nicholas68990be2017-07-13 09:36:52 -04002102 if (operandType->kind() == Type::kMatrix_Kind) {
2103 return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdEqual,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002104 SpvOpIEqual, SpvOpAll, SpvOpLogicalAnd, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002105 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002106 SkASSERT(resultType == *fContext.fBool_Type);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002107 const Type* tmpType;
2108 if (operandType->kind() == Type::kVector_Kind) {
2109 tmpType = &fContext.fBool_Type->toCompound(fContext,
2110 operandType->columns(),
2111 operandType->rows());
2112 } else {
2113 tmpType = &resultType;
2114 }
2115 return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs,
Ethan Nicholasef653b82017-02-21 13:50:00 -05002116 SpvOpFOrdEqual, SpvOpIEqual,
2117 SpvOpIEqual, SpvOpLogicalEqual, out),
Ethan Nicholas48e24052018-03-14 13:51:39 -04002118 *operandType, SpvOpAll, out);
Ethan Nicholasef653b82017-02-21 13:50:00 -05002119 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002120 case Token::NEQ:
Ethan Nicholas68990be2017-07-13 09:36:52 -04002121 if (operandType->kind() == Type::kMatrix_Kind) {
2122 return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdNotEqual,
Ethan Nicholas0df21132018-07-10 09:37:51 -04002123 SpvOpINotEqual, SpvOpAny, SpvOpLogicalOr, out);
Ethan Nicholas68990be2017-07-13 09:36:52 -04002124 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002125 SkASSERT(resultType == *fContext.fBool_Type);
Ethan Nicholas48e24052018-03-14 13:51:39 -04002126 const Type* tmpType;
2127 if (operandType->kind() == Type::kVector_Kind) {
2128 tmpType = &fContext.fBool_Type->toCompound(fContext,
2129 operandType->columns(),
2130 operandType->rows());
2131 } else {
2132 tmpType = &resultType;
2133 }
2134 return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs,
Ethan Nicholasef653b82017-02-21 13:50:00 -05002135 SpvOpFOrdNotEqual, SpvOpINotEqual,
2136 SpvOpINotEqual, SpvOpLogicalNotEqual,
2137 out),
Ethan Nicholas48e24052018-03-14 13:51:39 -04002138 *operandType, SpvOpAny, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002139 case Token::GT:
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,
2142 SpvOpFOrdGreaterThan, SpvOpSGreaterThan,
ethannicholasb3058bd2016-07-01 08:22:01 -07002143 SpvOpUGreaterThan, SpvOpUndef, out);
2144 case Token::LT:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002145 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002146 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFOrdLessThan,
ethannicholasb3058bd2016-07-01 08:22:01 -07002147 SpvOpSLessThan, SpvOpULessThan, SpvOpUndef, out);
2148 case Token::GTEQ:
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 SpvOpFOrdGreaterThanEqual, SpvOpSGreaterThanEqual,
ethannicholasb3058bd2016-07-01 08:22:01 -07002152 SpvOpUGreaterThanEqual, SpvOpUndef, out);
2153 case Token::LTEQ:
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002154 SkASSERT(resultType == *fContext.fBool_Type);
Greg Daniel64773e62016-11-22 09:44:03 -05002155 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2156 SpvOpFOrdLessThanEqual, SpvOpSLessThanEqual,
ethannicholasb3058bd2016-07-01 08:22:01 -07002157 SpvOpULessThanEqual, SpvOpUndef, out);
2158 case Token::PLUS:
Ethan Nicholas0df21132018-07-10 09:37:51 -04002159 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
2160 b.fRight->fType.kind() == Type::kMatrix_Kind) {
2161 SkASSERT(b.fLeft->fType == b.fRight->fType);
2162 return this->writeComponentwiseMatrixBinary(b.fLeft->fType, lhs, rhs,
2163 SpvOpFAdd, SpvOpIAdd, out);
2164 }
Greg Daniel64773e62016-11-22 09:44:03 -05002165 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFAdd,
ethannicholasb3058bd2016-07-01 08:22:01 -07002166 SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
2167 case Token::MINUS:
Ethan Nicholas0df21132018-07-10 09:37:51 -04002168 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
2169 b.fRight->fType.kind() == Type::kMatrix_Kind) {
2170 SkASSERT(b.fLeft->fType == b.fRight->fType);
2171 return this->writeComponentwiseMatrixBinary(b.fLeft->fType, lhs, rhs,
2172 SpvOpFSub, SpvOpISub, out);
2173 }
Greg Daniel64773e62016-11-22 09:44:03 -05002174 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFSub,
ethannicholasb3058bd2016-07-01 08:22:01 -07002175 SpvOpISub, SpvOpISub, SpvOpUndef, out);
2176 case Token::STAR:
Greg Daniel64773e62016-11-22 09:44:03 -05002177 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
ethannicholasd598f792016-07-25 10:08:54 -07002178 b.fRight->fType.kind() == Type::kMatrix_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002179 // matrix multiply
2180 SpvId result = this->nextId();
2181 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(resultType), result,
2182 lhs, rhs, out);
2183 return result;
2184 }
Greg Daniel64773e62016-11-22 09:44:03 -05002185 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMul,
ethannicholasb3058bd2016-07-01 08:22:01 -07002186 SpvOpIMul, SpvOpIMul, SpvOpUndef, out);
2187 case Token::SLASH:
Greg Daniel64773e62016-11-22 09:44:03 -05002188 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFDiv,
ethannicholasb3058bd2016-07-01 08:22:01 -07002189 SpvOpSDiv, SpvOpUDiv, SpvOpUndef, out);
Ethan Nicholasb3d0f7c2017-05-17 13:13:21 -04002190 case Token::PERCENT:
2191 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMod,
2192 SpvOpSMod, SpvOpUMod, SpvOpUndef, out);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002193 case Token::SHL:
2194 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2195 SpvOpShiftLeftLogical, SpvOpShiftLeftLogical,
2196 SpvOpUndef, out);
2197 case Token::SHR:
2198 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2199 SpvOpShiftRightArithmetic, SpvOpShiftRightLogical,
2200 SpvOpUndef, out);
2201 case Token::BITWISEAND:
2202 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2203 SpvOpBitwiseAnd, SpvOpBitwiseAnd, SpvOpUndef, out);
2204 case Token::BITWISEOR:
2205 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2206 SpvOpBitwiseOr, SpvOpBitwiseOr, SpvOpUndef, out);
2207 case Token::BITWISEXOR:
2208 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2209 SpvOpBitwiseXor, SpvOpBitwiseXor, SpvOpUndef, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002210 case Token::PLUSEQ: {
Ethan Nicholas0df21132018-07-10 09:37:51 -04002211 SpvId result;
2212 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
2213 b.fRight->fType.kind() == Type::kMatrix_Kind) {
2214 SkASSERT(b.fLeft->fType == b.fRight->fType);
2215 result = this->writeComponentwiseMatrixBinary(b.fLeft->fType, lhs, rhs,
2216 SpvOpFAdd, SpvOpIAdd, out);
2217 }
2218 else {
2219 result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFAdd,
ethannicholasb3058bd2016-07-01 08:22:01 -07002220 SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
Ethan Nicholas0df21132018-07-10 09:37:51 -04002221 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002222 SkASSERT(lvalue);
ethannicholasb3058bd2016-07-01 08:22:01 -07002223 lvalue->store(result, out);
2224 return result;
2225 }
2226 case Token::MINUSEQ: {
Ethan Nicholas0df21132018-07-10 09:37:51 -04002227 SpvId result;
2228 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
2229 b.fRight->fType.kind() == Type::kMatrix_Kind) {
2230 SkASSERT(b.fLeft->fType == b.fRight->fType);
2231 result = this->writeComponentwiseMatrixBinary(b.fLeft->fType, lhs, rhs,
2232 SpvOpFSub, SpvOpISub, out);
2233 }
2234 else {
2235 result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFSub,
ethannicholasb3058bd2016-07-01 08:22:01 -07002236 SpvOpISub, SpvOpISub, SpvOpUndef, out);
Ethan Nicholas0df21132018-07-10 09:37:51 -04002237 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002238 SkASSERT(lvalue);
ethannicholasb3058bd2016-07-01 08:22:01 -07002239 lvalue->store(result, out);
2240 return result;
2241 }
2242 case Token::STAREQ: {
Greg Daniel64773e62016-11-22 09:44:03 -05002243 if (b.fLeft->fType.kind() == Type::kMatrix_Kind &&
ethannicholasd598f792016-07-25 10:08:54 -07002244 b.fRight->fType.kind() == Type::kMatrix_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002245 // matrix multiply
2246 SpvId result = this->nextId();
2247 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(resultType), result,
2248 lhs, rhs, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002249 SkASSERT(lvalue);
ethannicholasb3058bd2016-07-01 08:22:01 -07002250 lvalue->store(result, out);
2251 return result;
2252 }
Greg Daniel64773e62016-11-22 09:44:03 -05002253 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMul,
ethannicholasb3058bd2016-07-01 08:22:01 -07002254 SpvOpIMul, SpvOpIMul, SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002255 SkASSERT(lvalue);
ethannicholasb3058bd2016-07-01 08:22:01 -07002256 lvalue->store(result, out);
2257 return result;
2258 }
2259 case Token::SLASHEQ: {
Greg Daniel64773e62016-11-22 09:44:03 -05002260 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFDiv,
ethannicholasb3058bd2016-07-01 08:22:01 -07002261 SpvOpSDiv, SpvOpUDiv, SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002262 SkASSERT(lvalue);
ethannicholasb3058bd2016-07-01 08:22:01 -07002263 lvalue->store(result, out);
2264 return result;
2265 }
Ethan Nicholasb3d0f7c2017-05-17 13:13:21 -04002266 case Token::PERCENTEQ: {
2267 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMod,
2268 SpvOpSMod, SpvOpUMod, SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002269 SkASSERT(lvalue);
Ethan Nicholasb3d0f7c2017-05-17 13:13:21 -04002270 lvalue->store(result, out);
2271 return result;
2272 }
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002273 case Token::SHLEQ: {
2274 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2275 SpvOpUndef, SpvOpShiftLeftLogical,
2276 SpvOpShiftLeftLogical, SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002277 SkASSERT(lvalue);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002278 lvalue->store(result, out);
2279 return result;
2280 }
2281 case Token::SHREQ: {
2282 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2283 SpvOpUndef, SpvOpShiftRightArithmetic,
2284 SpvOpShiftRightLogical, SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002285 SkASSERT(lvalue);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002286 lvalue->store(result, out);
2287 return result;
2288 }
2289 case Token::BITWISEANDEQ: {
2290 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2291 SpvOpUndef, SpvOpBitwiseAnd, SpvOpBitwiseAnd,
2292 SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002293 SkASSERT(lvalue);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002294 lvalue->store(result, out);
2295 return result;
2296 }
2297 case Token::BITWISEOREQ: {
2298 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2299 SpvOpUndef, SpvOpBitwiseOr, SpvOpBitwiseOr,
2300 SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002301 SkASSERT(lvalue);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002302 lvalue->store(result, out);
2303 return result;
2304 }
2305 case Token::BITWISEXOREQ: {
2306 SpvId result = this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2307 SpvOpUndef, SpvOpBitwiseXor, SpvOpBitwiseXor,
2308 SpvOpUndef, out);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002309 SkASSERT(lvalue);
Ethan Nicholasfd444be2017-07-05 10:05:54 -04002310 lvalue->store(result, out);
2311 return result;
2312 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002313 default:
ethannicholasb3058bd2016-07-01 08:22:01 -07002314 ABORT("unsupported binary expression: %s", b.description().c_str());
2315 }
2316}
2317
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002318SpvId SPIRVCodeGenerator::writeLogicalAnd(const BinaryExpression& a, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002319 SkASSERT(a.fOperator == Token::LOGICALAND);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002320 BoolLiteral falseLiteral(fContext, -1, false);
ethannicholasb3058bd2016-07-01 08:22:01 -07002321 SpvId falseConstant = this->writeBoolLiteral(falseLiteral);
2322 SpvId lhs = this->writeExpression(*a.fLeft, out);
2323 SpvId rhsLabel = this->nextId();
2324 SpvId end = this->nextId();
2325 SpvId lhsBlock = fCurrentBlock;
2326 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2327 this->writeInstruction(SpvOpBranchConditional, lhs, rhsLabel, end, out);
2328 this->writeLabel(rhsLabel, out);
2329 SpvId rhs = this->writeExpression(*a.fRight, out);
2330 SpvId rhsBlock = fCurrentBlock;
2331 this->writeInstruction(SpvOpBranch, end, out);
2332 this->writeLabel(end, out);
2333 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002334 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, falseConstant,
ethannicholasd598f792016-07-25 10:08:54 -07002335 lhsBlock, rhs, rhsBlock, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002336 return result;
2337}
2338
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002339SpvId SPIRVCodeGenerator::writeLogicalOr(const BinaryExpression& o, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002340 SkASSERT(o.fOperator == Token::LOGICALOR);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002341 BoolLiteral trueLiteral(fContext, -1, true);
ethannicholasb3058bd2016-07-01 08:22:01 -07002342 SpvId trueConstant = this->writeBoolLiteral(trueLiteral);
2343 SpvId lhs = this->writeExpression(*o.fLeft, out);
2344 SpvId rhsLabel = this->nextId();
2345 SpvId end = this->nextId();
2346 SpvId lhsBlock = fCurrentBlock;
2347 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2348 this->writeInstruction(SpvOpBranchConditional, lhs, end, rhsLabel, out);
2349 this->writeLabel(rhsLabel, out);
2350 SpvId rhs = this->writeExpression(*o.fRight, out);
2351 SpvId rhsBlock = fCurrentBlock;
2352 this->writeInstruction(SpvOpBranch, end, out);
2353 this->writeLabel(end, out);
2354 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002355 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result, trueConstant,
ethannicholasd598f792016-07-25 10:08:54 -07002356 lhsBlock, rhs, rhsBlock, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002357 return result;
2358}
2359
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002360SpvId SPIRVCodeGenerator::writeTernaryExpression(const TernaryExpression& t, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002361 SpvId test = this->writeExpression(*t.fTest, out);
Ethan Nicholas0f21c232018-07-23 09:25:40 -04002362 if (t.fIfTrue->fType.columns() == 1 && t.fIfTrue->isConstant() && t.fIfFalse->isConstant()) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002363 // both true and false are constants, can just use OpSelect
2364 SpvId result = this->nextId();
2365 SpvId trueId = this->writeExpression(*t.fIfTrue, out);
2366 SpvId falseId = this->writeExpression(*t.fIfFalse, out);
Greg Daniel64773e62016-11-22 09:44:03 -05002367 this->writeInstruction(SpvOpSelect, this->getType(t.fType), result, test, trueId, falseId,
ethannicholasb3058bd2016-07-01 08:22:01 -07002368 out);
2369 return result;
2370 }
Greg Daniel64773e62016-11-22 09:44:03 -05002371 // was originally using OpPhi to choose the result, but for some reason that is crashing on
ethannicholasb3058bd2016-07-01 08:22:01 -07002372 // Adreno. Switched to storing the result in a temp variable as glslang does.
2373 SpvId var = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002374 this->writeInstruction(SpvOpVariable, this->getPointerType(t.fType, SpvStorageClassFunction),
ethannicholasd598f792016-07-25 10:08:54 -07002375 var, SpvStorageClassFunction, fVariableBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002376 SpvId trueLabel = this->nextId();
2377 SpvId falseLabel = this->nextId();
2378 SpvId end = this->nextId();
2379 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2380 this->writeInstruction(SpvOpBranchConditional, test, trueLabel, falseLabel, out);
2381 this->writeLabel(trueLabel, out);
2382 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfTrue, out), out);
2383 this->writeInstruction(SpvOpBranch, end, out);
2384 this->writeLabel(falseLabel, out);
2385 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfFalse, out), out);
2386 this->writeInstruction(SpvOpBranch, end, out);
2387 this->writeLabel(end, out);
2388 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002389 this->writeInstruction(SpvOpLoad, this->getType(t.fType), result, var, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002390 return result;
2391}
2392
ethannicholasd598f792016-07-25 10:08:54 -07002393std::unique_ptr<Expression> create_literal_1(const Context& context, const Type& type) {
Ethan Nicholas024301a2017-11-10 13:49:18 -05002394 if (type.isInteger()) {
Ethan Nicholas00543112018-07-31 09:44:36 -04002395 return std::unique_ptr<Expression>(new IntLiteral(-1, 1, &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07002396 }
Ethan Nicholas024301a2017-11-10 13:49:18 -05002397 else if (type.isFloat()) {
Ethan Nicholas00543112018-07-31 09:44:36 -04002398 return std::unique_ptr<Expression>(new FloatLiteral(-1, 1.0, &type));
ethannicholasb3058bd2016-07-01 08:22:01 -07002399 } else {
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002400 ABORT("math is unsupported on type '%s'", type.name().c_str());
ethannicholasb3058bd2016-07-01 08:22:01 -07002401 }
2402}
2403
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002404SpvId SPIRVCodeGenerator::writePrefixExpression(const PrefixExpression& p, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002405 if (p.fOperator == Token::MINUS) {
2406 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002407 SpvId typeId = this->getType(p.fType);
ethannicholasb3058bd2016-07-01 08:22:01 -07002408 SpvId expr = this->writeExpression(*p.fOperand, out);
ethannicholasd598f792016-07-25 10:08:54 -07002409 if (is_float(fContext, p.fType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002410 this->writeInstruction(SpvOpFNegate, typeId, result, expr, out);
ethannicholasd598f792016-07-25 10:08:54 -07002411 } else if (is_signed(fContext, p.fType)) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002412 this->writeInstruction(SpvOpSNegate, typeId, result, expr, out);
2413 } else {
2414 ABORT("unsupported prefix expression %s", p.description().c_str());
Brian Salomon23356442018-11-30 15:33:19 -05002415 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002416 return result;
2417 }
2418 switch (p.fOperator) {
2419 case Token::PLUS:
2420 return this->writeExpression(*p.fOperand, out);
2421 case Token::PLUSPLUS: {
2422 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
ethannicholasd598f792016-07-25 10:08:54 -07002423 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out);
Greg Daniel64773e62016-11-22 09:44:03 -05002424 SpvId result = this->writeBinaryOperation(p.fType, p.fType, lv->load(out), one,
2425 SpvOpFAdd, SpvOpIAdd, SpvOpIAdd, SpvOpUndef,
ethannicholasb3058bd2016-07-01 08:22:01 -07002426 out);
2427 lv->store(result, out);
2428 return result;
2429 }
2430 case Token::MINUSMINUS: {
2431 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
ethannicholasd598f792016-07-25 10:08:54 -07002432 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out);
Greg Daniel64773e62016-11-22 09:44:03 -05002433 SpvId result = this->writeBinaryOperation(p.fType, p.fType, lv->load(out), one,
2434 SpvOpFSub, SpvOpISub, SpvOpISub, SpvOpUndef,
ethannicholasb3058bd2016-07-01 08:22:01 -07002435 out);
2436 lv->store(result, out);
2437 return result;
2438 }
ethannicholas5961bc92016-10-12 06:39:56 -07002439 case Token::LOGICALNOT: {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002440 SkASSERT(p.fOperand->fType == *fContext.fBool_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07002441 SpvId result = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002442 this->writeInstruction(SpvOpLogicalNot, this->getType(p.fOperand->fType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002443 this->writeExpression(*p.fOperand, out), out);
2444 return result;
2445 }
ethannicholas5961bc92016-10-12 06:39:56 -07002446 case Token::BITWISENOT: {
2447 SpvId result = this->nextId();
2448 this->writeInstruction(SpvOpNot, this->getType(p.fOperand->fType), result,
2449 this->writeExpression(*p.fOperand, out), out);
2450 return result;
2451 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002452 default:
2453 ABORT("unsupported prefix expression: %s", p.description().c_str());
2454 }
2455}
2456
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002457SpvId SPIRVCodeGenerator::writePostfixExpression(const PostfixExpression& p, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002458 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out);
2459 SpvId result = lv->load(out);
ethannicholasd598f792016-07-25 10:08:54 -07002460 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002461 switch (p.fOperator) {
2462 case Token::PLUSPLUS: {
Greg Daniel64773e62016-11-22 09:44:03 -05002463 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, one, SpvOpFAdd,
ethannicholasb3058bd2016-07-01 08:22:01 -07002464 SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
2465 lv->store(temp, out);
2466 return result;
2467 }
2468 case Token::MINUSMINUS: {
Greg Daniel64773e62016-11-22 09:44:03 -05002469 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, one, SpvOpFSub,
ethannicholasb3058bd2016-07-01 08:22:01 -07002470 SpvOpISub, SpvOpISub, SpvOpUndef, out);
2471 lv->store(temp, out);
2472 return result;
2473 }
2474 default:
2475 ABORT("unsupported postfix expression %s", p.description().c_str());
2476 }
2477}
2478
ethannicholasf789b382016-08-03 12:43:36 -07002479SpvId SPIRVCodeGenerator::writeBoolLiteral(const BoolLiteral& b) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002480 if (b.fValue) {
2481 if (fBoolTrue == 0) {
2482 fBoolTrue = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002483 this->writeInstruction(SpvOpConstantTrue, this->getType(b.fType), fBoolTrue,
ethannicholasb3058bd2016-07-01 08:22:01 -07002484 fConstantBuffer);
2485 }
2486 return fBoolTrue;
2487 } else {
2488 if (fBoolFalse == 0) {
2489 fBoolFalse = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002490 this->writeInstruction(SpvOpConstantFalse, this->getType(b.fType), fBoolFalse,
ethannicholasb3058bd2016-07-01 08:22:01 -07002491 fConstantBuffer);
2492 }
2493 return fBoolFalse;
2494 }
2495}
2496
ethannicholasf789b382016-08-03 12:43:36 -07002497SpvId SPIRVCodeGenerator::writeIntLiteral(const IntLiteral& i) {
ethannicholasd598f792016-07-25 10:08:54 -07002498 if (i.fType == *fContext.fInt_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002499 auto entry = fIntConstants.find(i.fValue);
2500 if (entry == fIntConstants.end()) {
2501 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002502 this->writeInstruction(SpvOpConstant, this->getType(i.fType), result, (SpvId) i.fValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07002503 fConstantBuffer);
2504 fIntConstants[i.fValue] = result;
2505 return result;
2506 }
2507 return entry->second;
2508 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002509 SkASSERT(i.fType == *fContext.fUInt_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -07002510 auto entry = fUIntConstants.find(i.fValue);
2511 if (entry == fUIntConstants.end()) {
2512 SpvId result = this->nextId();
Greg Daniel64773e62016-11-22 09:44:03 -05002513 this->writeInstruction(SpvOpConstant, this->getType(i.fType), result, (SpvId) i.fValue,
ethannicholasb3058bd2016-07-01 08:22:01 -07002514 fConstantBuffer);
2515 fUIntConstants[i.fValue] = result;
2516 return result;
2517 }
2518 return entry->second;
2519 }
2520}
2521
ethannicholasf789b382016-08-03 12:43:36 -07002522SpvId SPIRVCodeGenerator::writeFloatLiteral(const FloatLiteral& f) {
Ethan Nicholase1f55022019-02-05 17:17:40 -05002523 if (f.fType != *fContext.fDouble_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002524 float value = (float) f.fValue;
2525 auto entry = fFloatConstants.find(value);
2526 if (entry == fFloatConstants.end()) {
2527 SpvId result = this->nextId();
2528 uint32_t bits;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002529 SkASSERT(sizeof(bits) == sizeof(value));
ethannicholasb3058bd2016-07-01 08:22:01 -07002530 memcpy(&bits, &value, sizeof(bits));
Greg Daniel64773e62016-11-22 09:44:03 -05002531 this->writeInstruction(SpvOpConstant, this->getType(f.fType), result, bits,
ethannicholasb3058bd2016-07-01 08:22:01 -07002532 fConstantBuffer);
2533 fFloatConstants[value] = result;
2534 return result;
2535 }
2536 return entry->second;
2537 } else {
ethannicholasb3058bd2016-07-01 08:22:01 -07002538 auto entry = fDoubleConstants.find(f.fValue);
2539 if (entry == fDoubleConstants.end()) {
2540 SpvId result = this->nextId();
2541 uint64_t bits;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002542 SkASSERT(sizeof(bits) == sizeof(f.fValue));
ethannicholasb3058bd2016-07-01 08:22:01 -07002543 memcpy(&bits, &f.fValue, sizeof(bits));
Greg Daniel64773e62016-11-22 09:44:03 -05002544 this->writeInstruction(SpvOpConstant, this->getType(f.fType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002545 bits & 0xffffffff, bits >> 32, fConstantBuffer);
2546 fDoubleConstants[f.fValue] = result;
2547 return result;
2548 }
2549 return entry->second;
2550 }
2551}
2552
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002553SpvId SPIRVCodeGenerator::writeFunctionStart(const FunctionDeclaration& f, OutputStream& out) {
ethannicholasd598f792016-07-25 10:08:54 -07002554 SpvId result = fFunctionMap[&f];
Greg Daniel64773e62016-11-22 09:44:03 -05002555 this->writeInstruction(SpvOpFunction, this->getType(f.fReturnType), result,
ethannicholasb3058bd2016-07-01 08:22:01 -07002556 SpvFunctionControlMaskNone, this->getFunctionType(f), out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002557 this->writeInstruction(SpvOpName, result, f.fName, fNameBuffer);
ethannicholasd598f792016-07-25 10:08:54 -07002558 for (size_t i = 0; i < f.fParameters.size(); i++) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002559 SpvId id = this->nextId();
ethannicholasd598f792016-07-25 10:08:54 -07002560 fVariableMap[f.fParameters[i]] = id;
ethannicholasb3058bd2016-07-01 08:22:01 -07002561 SpvId type;
ethannicholasd598f792016-07-25 10:08:54 -07002562 type = this->getPointerType(f.fParameters[i]->fType, SpvStorageClassFunction);
ethannicholasb3058bd2016-07-01 08:22:01 -07002563 this->writeInstruction(SpvOpFunctionParameter, type, id, out);
2564 }
2565 return result;
2566}
2567
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002568SpvId SPIRVCodeGenerator::writeFunction(const FunctionDefinition& f, OutputStream& out) {
2569 fVariableBuffer.reset();
ethannicholasb3058bd2016-07-01 08:22:01 -07002570 SpvId result = this->writeFunctionStart(f.fDeclaration, out);
2571 this->writeLabel(this->nextId(), out);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002572 StringStream bodyBuffer;
Ethan Nicholascb670962017-04-20 19:31:52 -04002573 this->writeBlock((Block&) *f.fBody, bodyBuffer);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002574 write_stringstream(fVariableBuffer, out);
Ethan Nicholas8eb64d32018-12-21 14:50:42 -05002575 if (f.fDeclaration.fName == "main") {
2576 write_stringstream(fGlobalInitializersBuffer, out);
2577 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002578 write_stringstream(bodyBuffer, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002579 if (fCurrentBlock) {
Ethan Nicholas70a44b22017-11-30 09:09:16 -05002580 if (f.fDeclaration.fReturnType == *fContext.fVoid_Type) {
2581 this->writeInstruction(SpvOpReturn, out);
2582 } else {
2583 this->writeInstruction(SpvOpUnreachable, out);
2584 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002585 }
2586 this->writeInstruction(SpvOpFunctionEnd, out);
2587 return result;
2588}
2589
2590void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target) {
2591 if (layout.fLocation >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002592 this->writeInstruction(SpvOpDecorate, target, SpvDecorationLocation, layout.fLocation,
ethannicholasb3058bd2016-07-01 08:22:01 -07002593 fDecorationBuffer);
2594 }
2595 if (layout.fBinding >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002596 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBinding, layout.fBinding,
ethannicholasb3058bd2016-07-01 08:22:01 -07002597 fDecorationBuffer);
2598 }
2599 if (layout.fIndex >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002600 this->writeInstruction(SpvOpDecorate, target, SpvDecorationIndex, layout.fIndex,
ethannicholasb3058bd2016-07-01 08:22:01 -07002601 fDecorationBuffer);
2602 }
2603 if (layout.fSet >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002604 this->writeInstruction(SpvOpDecorate, target, SpvDecorationDescriptorSet, layout.fSet,
ethannicholasb3058bd2016-07-01 08:22:01 -07002605 fDecorationBuffer);
2606 }
Greg Daniel64773e62016-11-22 09:44:03 -05002607 if (layout.fInputAttachmentIndex >= 0) {
2608 this->writeInstruction(SpvOpDecorate, target, SpvDecorationInputAttachmentIndex,
2609 layout.fInputAttachmentIndex, fDecorationBuffer);
Ethan Nicholasbe1099f2018-01-11 16:09:05 -05002610 fCapabilities |= (((uint64_t) 1) << SpvCapabilityInputAttachment);
Greg Daniel64773e62016-11-22 09:44:03 -05002611 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04002612 if (layout.fBuiltin >= 0 && layout.fBuiltin != SK_FRAGCOLOR_BUILTIN &&
Greg Daniele6ab9982018-08-22 13:56:32 +00002613 layout.fBuiltin != SK_IN_BUILTIN && layout.fBuiltin != SK_OUT_BUILTIN) {
Greg Daniel64773e62016-11-22 09:44:03 -05002614 this->writeInstruction(SpvOpDecorate, target, SpvDecorationBuiltIn, layout.fBuiltin,
ethannicholasb3058bd2016-07-01 08:22:01 -07002615 fDecorationBuffer);
2616 }
2617}
2618
2619void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target, int member) {
2620 if (layout.fLocation >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002621 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationLocation,
ethannicholasb3058bd2016-07-01 08:22:01 -07002622 layout.fLocation, fDecorationBuffer);
2623 }
2624 if (layout.fBinding >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002625 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBinding,
ethannicholasb3058bd2016-07-01 08:22:01 -07002626 layout.fBinding, fDecorationBuffer);
2627 }
2628 if (layout.fIndex >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002629 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationIndex,
ethannicholasb3058bd2016-07-01 08:22:01 -07002630 layout.fIndex, fDecorationBuffer);
2631 }
2632 if (layout.fSet >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002633 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationDescriptorSet,
ethannicholasb3058bd2016-07-01 08:22:01 -07002634 layout.fSet, fDecorationBuffer);
2635 }
Greg Daniel64773e62016-11-22 09:44:03 -05002636 if (layout.fInputAttachmentIndex >= 0) {
2637 this->writeInstruction(SpvOpDecorate, target, member, SpvDecorationInputAttachmentIndex,
2638 layout.fInputAttachmentIndex, fDecorationBuffer);
2639 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002640 if (layout.fBuiltin >= 0) {
Greg Daniel64773e62016-11-22 09:44:03 -05002641 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBuiltIn,
ethannicholasb3058bd2016-07-01 08:22:01 -07002642 layout.fBuiltin, fDecorationBuffer);
2643 }
2644}
2645
Ethan Nicholas81d15112018-07-13 12:48:50 -04002646static void update_sk_in_count(const Modifiers& m, int* outSkInCount) {
2647 switch (m.fLayout.fPrimitive) {
2648 case Layout::kPoints_Primitive:
2649 *outSkInCount = 1;
2650 break;
2651 case Layout::kLines_Primitive:
2652 *outSkInCount = 2;
2653 break;
2654 case Layout::kLinesAdjacency_Primitive:
2655 *outSkInCount = 4;
2656 break;
2657 case Layout::kTriangles_Primitive:
2658 *outSkInCount = 3;
2659 break;
2660 case Layout::kTrianglesAdjacency_Primitive:
2661 *outSkInCount = 6;
2662 break;
2663 default:
2664 return;
2665 }
2666}
2667
ethannicholasf789b382016-08-03 12:43:36 -07002668SpvId SPIRVCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf) {
Ethan Nicholas361941e2017-05-30 15:41:07 -04002669 bool isBuffer = (0 != (intf.fVariable.fModifiers.fFlags & Modifiers::kBuffer_Flag));
Ethan Nicholas39204fd2017-11-27 13:12:30 -05002670 bool pushConstant = (0 != (intf.fVariable.fModifiers.fLayout.fFlags &
2671 Layout::kPushConstant_Flag));
Ethan Nicholas8d2ba442018-03-16 16:40:36 -04002672 MemoryLayout memoryLayout = (pushConstant || isBuffer) ?
2673 MemoryLayout(MemoryLayout::k430_Standard) :
2674 fDefaultLayout;
ethannicholasb3058bd2016-07-01 08:22:01 -07002675 SpvId result = this->nextId();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002676 const Type* type = &intf.fVariable.fType;
Greg Daniele6ab9982018-08-22 13:56:32 +00002677 if (fProgram.fInputs.fRTHeight) {
2678 SkASSERT(fRTHeightStructId == (SpvId) -1);
2679 SkASSERT(fRTHeightFieldIndex == (SpvId) -1);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002680 std::vector<Type::Field> fields = type->fields();
Greg Daniele6ab9982018-08-22 13:56:32 +00002681 fRTHeightStructId = result;
2682 fRTHeightFieldIndex = fields.size();
2683 fields.emplace_back(Modifiers(), StringFragment(SKSL_RTHEIGHT_NAME), fContext.fFloat_Type.get());
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002684 type = new Type(type->fOffset, type->name(), fields);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002685 }
Ethan Nicholas5226b772018-05-03 16:20:41 -04002686 SpvId typeId;
2687 if (intf.fVariable.fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) {
2688 for (const auto& e : fProgram) {
2689 if (e.fKind == ProgramElement::kModifiers_Kind) {
2690 const Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholas81d15112018-07-13 12:48:50 -04002691 update_sk_in_count(m, &fSkInCount);
Ethan Nicholas5226b772018-05-03 16:20:41 -04002692 }
2693 }
2694 typeId = this->getType(Type("sk_in", Type::kArray_Kind, intf.fVariable.fType.componentType(),
2695 fSkInCount), memoryLayout);
2696 } else {
2697 typeId = this->getType(*type, memoryLayout);
2698 }
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04002699 if (intf.fVariable.fModifiers.fFlags & Modifiers::kBuffer_Flag) {
2700 this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBufferBlock, fDecorationBuffer);
Ethan Nicholas6ac8d362019-01-22 21:43:55 +00002701 } else if (intf.fVariable.fModifiers.fLayout.fBuiltin == -1) {
2702 this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBlock, fDecorationBuffer);
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04002703 }
ethannicholasd598f792016-07-25 10:08:54 -07002704 SpvStorageClass_ storageClass = get_storage_class(intf.fVariable.fModifiers);
ethannicholasb3058bd2016-07-01 08:22:01 -07002705 SpvId ptrType = this->nextId();
Ethan Nicholas941e7e22016-12-12 15:33:30 -05002706 this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, typeId, fConstantBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002707 this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConstantBuffer);
Ethan Nicholas8d2ba442018-03-16 16:40:36 -04002708 Layout layout = intf.fVariable.fModifiers.fLayout;
2709 if (intf.fVariable.fModifiers.fFlags & Modifiers::kUniform_Flag && layout.fSet == -1) {
2710 layout.fSet = 0;
2711 }
2712 this->writeLayout(layout, result);
ethannicholasd598f792016-07-25 10:08:54 -07002713 fVariableMap[&intf.fVariable] = result;
Greg Daniele6ab9982018-08-22 13:56:32 +00002714 if (fProgram.fInputs.fRTHeight) {
Ethan Nicholas39b101b2017-03-01 12:07:28 -05002715 delete type;
2716 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002717 return result;
2718}
2719
Ethan Nicholas858fecc2019-03-07 13:19:18 -05002720void SPIRVCodeGenerator::writePrecisionModifier(const Type& type, SpvId id) {
2721 if (!type.highPrecision()) {
Ethan Nicholasa51d7132017-06-09 10:47:31 -04002722 this->writeInstruction(SpvOpDecorate, id, SpvDecorationRelaxedPrecision, fDecorationBuffer);
2723 }
2724}
2725
ethannicholas5961bc92016-10-12 06:39:56 -07002726#define BUILTIN_IGNORE 9999
Greg Daniel64773e62016-11-22 09:44:03 -05002727void SPIRVCodeGenerator::writeGlobalVars(Program::Kind kind, const VarDeclarations& decl,
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002728 OutputStream& out) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002729 for (size_t i = 0; i < decl.fVars.size(); i++) {
2730 if (decl.fVars[i]->fKind == Statement::kNop_Kind) {
2731 continue;
2732 }
2733 const VarDeclaration& varDecl = (VarDeclaration&) *decl.fVars[i];
2734 const Variable* var = varDecl.fVar;
Brian Salomonf9f45122016-11-29 11:59:17 -05002735 // These haven't been implemented in our SPIR-V generator yet and we only currently use them
2736 // in the OpenGL backend.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002737 SkASSERT(!(var->fModifiers.fFlags & (Modifiers::kReadOnly_Flag |
Brian Salomonf9f45122016-11-29 11:59:17 -05002738 Modifiers::kWriteOnly_Flag |
2739 Modifiers::kCoherent_Flag |
2740 Modifiers::kVolatile_Flag |
2741 Modifiers::kRestrict_Flag)));
ethannicholas5961bc92016-10-12 06:39:56 -07002742 if (var->fModifiers.fLayout.fBuiltin == BUILTIN_IGNORE) {
2743 continue;
2744 }
2745 if (var->fModifiers.fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN &&
2746 kind != Program::kFragment_Kind) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002747 SkASSERT(!fProgram.fSettings.fFragColorIsInOut);
ethannicholas5961bc92016-10-12 06:39:56 -07002748 continue;
2749 }
Ethan Nicholas86a43402017-01-19 13:32:00 -05002750 if (!var->fReadCount && !var->fWriteCount &&
ethannicholas14fe8cc2016-09-07 13:37:16 -07002751 !(var->fModifiers.fFlags & (Modifiers::kIn_Flag |
2752 Modifiers::kOut_Flag |
Ethan Nicholas0dd30d92017-05-01 16:57:07 -04002753 Modifiers::kUniform_Flag |
2754 Modifiers::kBuffer_Flag))) {
ethannicholasd598f792016-07-25 10:08:54 -07002755 // variable is dead and not an input / output var (the Vulkan debug layers complain if
2756 // we elide an interface var, even if it's dead)
ethannicholasb3058bd2016-07-01 08:22:01 -07002757 continue;
2758 }
2759 SpvStorageClass_ storageClass;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002760 if (var->fModifiers.fFlags & Modifiers::kIn_Flag) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002761 storageClass = SpvStorageClassInput;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002762 } else if (var->fModifiers.fFlags & Modifiers::kOut_Flag) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002763 storageClass = SpvStorageClassOutput;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002764 } else if (var->fModifiers.fFlags & Modifiers::kUniform_Flag) {
2765 if (var->fType.kind() == Type::kSampler_Kind) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002766 storageClass = SpvStorageClassUniformConstant;
2767 } else {
2768 storageClass = SpvStorageClassUniform;
2769 }
2770 } else {
2771 storageClass = SpvStorageClassPrivate;
2772 }
2773 SpvId id = this->nextId();
ethannicholas14fe8cc2016-09-07 13:37:16 -07002774 fVariableMap[var] = id;
Ethan Nicholas5226b772018-05-03 16:20:41 -04002775 SpvId type;
2776 if (var->fModifiers.fLayout.fBuiltin == SK_IN_BUILTIN) {
2777 type = this->getPointerType(Type("sk_in", Type::kArray_Kind,
2778 var->fType.componentType(), fSkInCount),
2779 storageClass);
2780 } else {
2781 type = this->getPointerType(var->fType, storageClass);
2782 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002783 this->writeInstruction(SpvOpVariable, type, id, storageClass, fConstantBuffer);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002784 this->writeInstruction(SpvOpName, id, var->fName, fNameBuffer);
Ethan Nicholas858fecc2019-03-07 13:19:18 -05002785 this->writePrecisionModifier(var->fType, id);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002786 if (varDecl.fValue) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002787 SkASSERT(!fCurrentBlock);
ethannicholasf789b382016-08-03 12:43:36 -07002788 fCurrentBlock = -1;
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002789 SpvId value = this->writeExpression(*varDecl.fValue, fGlobalInitializersBuffer);
ethannicholasb3058bd2016-07-01 08:22:01 -07002790 this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuffer);
ethannicholasf789b382016-08-03 12:43:36 -07002791 fCurrentBlock = 0;
ethannicholasb3058bd2016-07-01 08:22:01 -07002792 }
ethannicholas14fe8cc2016-09-07 13:37:16 -07002793 this->writeLayout(var->fModifiers.fLayout, id);
Ethan Nicholas45b0f152017-07-24 14:36:40 -04002794 if (var->fModifiers.fFlags & Modifiers::kFlat_Flag) {
2795 this->writeInstruction(SpvOpDecorate, id, SpvDecorationFlat, fDecorationBuffer);
2796 }
2797 if (var->fModifiers.fFlags & Modifiers::kNoPerspective_Flag) {
2798 this->writeInstruction(SpvOpDecorate, id, SpvDecorationNoPerspective,
2799 fDecorationBuffer);
2800 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002801 }
2802}
2803
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002804void SPIRVCodeGenerator::writeVarDeclarations(const VarDeclarations& decl, OutputStream& out) {
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002805 for (const auto& stmt : decl.fVars) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002806 SkASSERT(stmt->fKind == Statement::kVarDeclaration_Kind);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002807 VarDeclaration& varDecl = (VarDeclaration&) *stmt;
2808 const Variable* var = varDecl.fVar;
Brian Salomonf9f45122016-11-29 11:59:17 -05002809 // These haven't been implemented in our SPIR-V generator yet and we only currently use them
2810 // in the OpenGL backend.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04002811 SkASSERT(!(var->fModifiers.fFlags & (Modifiers::kReadOnly_Flag |
Brian Salomonf9f45122016-11-29 11:59:17 -05002812 Modifiers::kWriteOnly_Flag |
2813 Modifiers::kCoherent_Flag |
2814 Modifiers::kVolatile_Flag |
2815 Modifiers::kRestrict_Flag)));
ethannicholasb3058bd2016-07-01 08:22:01 -07002816 SpvId id = this->nextId();
ethannicholas14fe8cc2016-09-07 13:37:16 -07002817 fVariableMap[var] = id;
2818 SpvId type = this->getPointerType(var->fType, SpvStorageClassFunction);
ethannicholasb3058bd2016-07-01 08:22:01 -07002819 this->writeInstruction(SpvOpVariable, type, id, SpvStorageClassFunction, fVariableBuffer);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002820 this->writeInstruction(SpvOpName, id, var->fName, fNameBuffer);
Ethan Nicholas82a62d22017-11-07 14:42:10 +00002821 if (varDecl.fValue) {
2822 SpvId value = this->writeExpression(*varDecl.fValue, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002823 this->writeInstruction(SpvOpStore, id, value, out);
2824 }
2825 }
2826}
2827
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002828void SPIRVCodeGenerator::writeStatement(const Statement& s, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002829 switch (s.fKind) {
Ethan Nicholascb670962017-04-20 19:31:52 -04002830 case Statement::kNop_Kind:
2831 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07002832 case Statement::kBlock_Kind:
2833 this->writeBlock((Block&) s, out);
2834 break;
2835 case Statement::kExpression_Kind:
2836 this->writeExpression(*((ExpressionStatement&) s).fExpression, out);
2837 break;
Greg Daniel64773e62016-11-22 09:44:03 -05002838 case Statement::kReturn_Kind:
ethannicholasb3058bd2016-07-01 08:22:01 -07002839 this->writeReturnStatement((ReturnStatement&) s, out);
2840 break;
ethannicholas14fe8cc2016-09-07 13:37:16 -07002841 case Statement::kVarDeclarations_Kind:
2842 this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002843 break;
2844 case Statement::kIf_Kind:
2845 this->writeIfStatement((IfStatement&) s, out);
2846 break;
2847 case Statement::kFor_Kind:
2848 this->writeForStatement((ForStatement&) s, out);
2849 break;
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002850 case Statement::kWhile_Kind:
2851 this->writeWhileStatement((WhileStatement&) s, out);
2852 break;
2853 case Statement::kDo_Kind:
2854 this->writeDoStatement((DoStatement&) s, out);
2855 break;
Ethan Nicholase92b1b12017-11-13 16:13:21 -05002856 case Statement::kSwitch_Kind:
2857 this->writeSwitchStatement((SwitchStatement&) s, out);
2858 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07002859 case Statement::kBreak_Kind:
2860 this->writeInstruction(SpvOpBranch, fBreakTarget.top(), out);
2861 break;
2862 case Statement::kContinue_Kind:
2863 this->writeInstruction(SpvOpBranch, fContinueTarget.top(), out);
2864 break;
2865 case Statement::kDiscard_Kind:
2866 this->writeInstruction(SpvOpKill, out);
2867 break;
2868 default:
2869 ABORT("unsupported statement: %s", s.description().c_str());
2870 }
2871}
2872
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002873void SPIRVCodeGenerator::writeBlock(const Block& b, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002874 for (size_t i = 0; i < b.fStatements.size(); i++) {
2875 this->writeStatement(*b.fStatements[i], out);
2876 }
2877}
2878
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002879void SPIRVCodeGenerator::writeIfStatement(const IfStatement& stmt, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002880 SpvId test = this->writeExpression(*stmt.fTest, out);
2881 SpvId ifTrue = this->nextId();
2882 SpvId ifFalse = this->nextId();
2883 if (stmt.fIfFalse) {
2884 SpvId end = this->nextId();
2885 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
2886 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out);
2887 this->writeLabel(ifTrue, out);
2888 this->writeStatement(*stmt.fIfTrue, out);
2889 if (fCurrentBlock) {
2890 this->writeInstruction(SpvOpBranch, end, out);
2891 }
2892 this->writeLabel(ifFalse, out);
2893 this->writeStatement(*stmt.fIfFalse, out);
2894 if (fCurrentBlock) {
2895 this->writeInstruction(SpvOpBranch, end, out);
2896 }
2897 this->writeLabel(end, out);
2898 } else {
2899 this->writeInstruction(SpvOpSelectionMerge, ifFalse, SpvSelectionControlMaskNone, out);
2900 this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out);
2901 this->writeLabel(ifTrue, out);
2902 this->writeStatement(*stmt.fIfTrue, out);
2903 if (fCurrentBlock) {
2904 this->writeInstruction(SpvOpBranch, ifFalse, out);
2905 }
2906 this->writeLabel(ifFalse, out);
2907 }
2908}
2909
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002910void SPIRVCodeGenerator::writeForStatement(const ForStatement& f, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07002911 if (f.fInitializer) {
2912 this->writeStatement(*f.fInitializer, out);
2913 }
2914 SpvId header = this->nextId();
2915 SpvId start = this->nextId();
2916 SpvId body = this->nextId();
2917 SpvId next = this->nextId();
2918 fContinueTarget.push(next);
2919 SpvId end = this->nextId();
2920 fBreakTarget.push(end);
2921 this->writeInstruction(SpvOpBranch, header, out);
2922 this->writeLabel(header, out);
2923 this->writeInstruction(SpvOpLoopMerge, end, next, SpvLoopControlMaskNone, out);
ethannicholasf789b382016-08-03 12:43:36 -07002924 this->writeInstruction(SpvOpBranch, start, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07002925 this->writeLabel(start, out);
ethannicholas22f939e2016-10-13 13:25:34 -07002926 if (f.fTest) {
2927 SpvId test = this->writeExpression(*f.fTest, out);
2928 this->writeInstruction(SpvOpBranchConditional, test, body, end, out);
2929 }
ethannicholasb3058bd2016-07-01 08:22:01 -07002930 this->writeLabel(body, out);
2931 this->writeStatement(*f.fStatement, out);
2932 if (fCurrentBlock) {
2933 this->writeInstruction(SpvOpBranch, next, out);
2934 }
2935 this->writeLabel(next, out);
2936 if (f.fNext) {
2937 this->writeExpression(*f.fNext, out);
2938 }
2939 this->writeInstruction(SpvOpBranch, header, out);
2940 this->writeLabel(end, out);
2941 fBreakTarget.pop();
2942 fContinueTarget.pop();
2943}
2944
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002945void SPIRVCodeGenerator::writeWhileStatement(const WhileStatement& w, OutputStream& out) {
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002946 // We believe the while loop code below will work, but Skia doesn't actually use them and
2947 // adequately testing this code in the absence of Skia exercising it isn't straightforward. For
2948 // the time being, we just fail with an error due to the lack of testing. If you encounter this
2949 // message, simply remove the error call below to see whether our while loop support actually
2950 // works.
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002951 fErrors.error(w.fOffset, "internal error: while loop support has been disabled in SPIR-V, "
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002952 "see SkSLSPIRVCodeGenerator.cpp for details");
2953
2954 SpvId header = this->nextId();
2955 SpvId start = this->nextId();
2956 SpvId body = this->nextId();
2957 fContinueTarget.push(start);
2958 SpvId end = this->nextId();
2959 fBreakTarget.push(end);
2960 this->writeInstruction(SpvOpBranch, header, out);
2961 this->writeLabel(header, out);
2962 this->writeInstruction(SpvOpLoopMerge, end, start, SpvLoopControlMaskNone, out);
2963 this->writeInstruction(SpvOpBranch, start, out);
2964 this->writeLabel(start, out);
2965 SpvId test = this->writeExpression(*w.fTest, out);
2966 this->writeInstruction(SpvOpBranchConditional, test, body, end, out);
2967 this->writeLabel(body, out);
2968 this->writeStatement(*w.fStatement, out);
2969 if (fCurrentBlock) {
2970 this->writeInstruction(SpvOpBranch, start, out);
2971 }
2972 this->writeLabel(end, out);
2973 fBreakTarget.pop();
2974 fContinueTarget.pop();
2975}
2976
Ethan Nicholas0df1b042017-03-31 13:56:23 -04002977void SPIRVCodeGenerator::writeDoStatement(const DoStatement& d, OutputStream& out) {
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002978 // We believe the do loop code below will work, but Skia doesn't actually use them and
2979 // adequately testing this code in the absence of Skia exercising it isn't straightforward. For
2980 // the time being, we just fail with an error due to the lack of testing. If you encounter this
2981 // message, simply remove the error call below to see whether our do loop support actually
2982 // works.
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07002983 fErrors.error(d.fOffset, "internal error: do loop support has been disabled in SPIR-V, see "
Ethan Nicholasfd146aa2017-01-13 16:40:35 -05002984 "SkSLSPIRVCodeGenerator.cpp for details");
2985
2986 SpvId header = this->nextId();
2987 SpvId start = this->nextId();
2988 SpvId next = this->nextId();
2989 fContinueTarget.push(next);
2990 SpvId end = this->nextId();
2991 fBreakTarget.push(end);
2992 this->writeInstruction(SpvOpBranch, header, out);
2993 this->writeLabel(header, out);
2994 this->writeInstruction(SpvOpLoopMerge, end, start, SpvLoopControlMaskNone, out);
2995 this->writeInstruction(SpvOpBranch, start, out);
2996 this->writeLabel(start, out);
2997 this->writeStatement(*d.fStatement, out);
2998 if (fCurrentBlock) {
2999 this->writeInstruction(SpvOpBranch, next, out);
3000 }
3001 this->writeLabel(next, out);
3002 SpvId test = this->writeExpression(*d.fTest, out);
3003 this->writeInstruction(SpvOpBranchConditional, test, start, end, out);
3004 this->writeLabel(end, out);
3005 fBreakTarget.pop();
3006 fContinueTarget.pop();
3007}
3008
Ethan Nicholase92b1b12017-11-13 16:13:21 -05003009void SPIRVCodeGenerator::writeSwitchStatement(const SwitchStatement& s, OutputStream& out) {
3010 SpvId value = this->writeExpression(*s.fValue, out);
3011 std::vector<SpvId> labels;
3012 SpvId end = this->nextId();
3013 SpvId defaultLabel = end;
3014 fBreakTarget.push(end);
3015 int size = 3;
3016 for (const auto& c : s.fCases) {
3017 SpvId label = this->nextId();
3018 labels.push_back(label);
3019 if (c->fValue) {
3020 size += 2;
3021 } else {
3022 defaultLabel = label;
3023 }
3024 }
3025 labels.push_back(end);
3026 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
3027 this->writeOpCode(SpvOpSwitch, size, out);
3028 this->writeWord(value, out);
3029 this->writeWord(defaultLabel, out);
3030 for (size_t i = 0; i < s.fCases.size(); ++i) {
3031 if (!s.fCases[i]->fValue) {
3032 continue;
3033 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003034 SkASSERT(s.fCases[i]->fValue->fKind == Expression::kIntLiteral_Kind);
Ethan Nicholase92b1b12017-11-13 16:13:21 -05003035 this->writeWord(((IntLiteral&) *s.fCases[i]->fValue).fValue, out);
3036 this->writeWord(labels[i], out);
3037 }
3038 for (size_t i = 0; i < s.fCases.size(); ++i) {
3039 this->writeLabel(labels[i], out);
3040 for (const auto& stmt : s.fCases[i]->fStatements) {
3041 this->writeStatement(*stmt, out);
3042 }
3043 if (fCurrentBlock) {
3044 this->writeInstruction(SpvOpBranch, labels[i + 1], out);
3045 }
3046 }
3047 this->writeLabel(end, out);
3048 fBreakTarget.pop();
3049}
3050
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003051void SPIRVCodeGenerator::writeReturnStatement(const ReturnStatement& r, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07003052 if (r.fExpression) {
Greg Daniel64773e62016-11-22 09:44:03 -05003053 this->writeInstruction(SpvOpReturnValue, this->writeExpression(*r.fExpression, out),
ethannicholasb3058bd2016-07-01 08:22:01 -07003054 out);
3055 } else {
3056 this->writeInstruction(SpvOpReturn, out);
3057 }
3058}
3059
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003060void SPIRVCodeGenerator::writeGeometryShaderExecutionMode(SpvId entryPoint, OutputStream& out) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003061 SkASSERT(fProgram.fKind == Program::kGeometry_Kind);
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003062 int invocations = 1;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003063 for (const auto& e : fProgram) {
3064 if (e.fKind == ProgramElement::kModifiers_Kind) {
3065 const Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003066 if (m.fFlags & Modifiers::kIn_Flag) {
3067 if (m.fLayout.fInvocations != -1) {
3068 invocations = m.fLayout.fInvocations;
3069 }
3070 SpvId input;
3071 switch (m.fLayout.fPrimitive) {
3072 case Layout::kPoints_Primitive:
3073 input = SpvExecutionModeInputPoints;
3074 break;
3075 case Layout::kLines_Primitive:
3076 input = SpvExecutionModeInputLines;
3077 break;
3078 case Layout::kLinesAdjacency_Primitive:
3079 input = SpvExecutionModeInputLinesAdjacency;
3080 break;
3081 case Layout::kTriangles_Primitive:
3082 input = SpvExecutionModeTriangles;
3083 break;
3084 case Layout::kTrianglesAdjacency_Primitive:
3085 input = SpvExecutionModeInputTrianglesAdjacency;
3086 break;
3087 default:
3088 input = 0;
3089 break;
3090 }
Ethan Nicholas81d15112018-07-13 12:48:50 -04003091 update_sk_in_count(m, &fSkInCount);
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003092 if (input) {
3093 this->writeInstruction(SpvOpExecutionMode, entryPoint, input, out);
3094 }
3095 } else if (m.fFlags & Modifiers::kOut_Flag) {
3096 SpvId output;
3097 switch (m.fLayout.fPrimitive) {
3098 case Layout::kPoints_Primitive:
3099 output = SpvExecutionModeOutputPoints;
3100 break;
3101 case Layout::kLineStrip_Primitive:
3102 output = SpvExecutionModeOutputLineStrip;
3103 break;
3104 case Layout::kTriangleStrip_Primitive:
3105 output = SpvExecutionModeOutputTriangleStrip;
3106 break;
3107 default:
3108 output = 0;
3109 break;
3110 }
3111 if (output) {
3112 this->writeInstruction(SpvOpExecutionMode, entryPoint, output, out);
3113 }
3114 if (m.fLayout.fMaxVertices != -1) {
3115 this->writeInstruction(SpvOpExecutionMode, entryPoint,
3116 SpvExecutionModeOutputVertices, m.fLayout.fMaxVertices,
3117 out);
3118 }
3119 }
3120 }
3121 }
3122 this->writeInstruction(SpvOpExecutionMode, entryPoint, SpvExecutionModeInvocations,
3123 invocations, out);
3124}
3125
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003126void SPIRVCodeGenerator::writeInstructions(const Program& program, OutputStream& out) {
ethannicholasb3058bd2016-07-01 08:22:01 -07003127 fGLSLExtendedInstructions = this->nextId();
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003128 StringStream body;
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003129 std::set<SpvId> interfaceVars;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003130 // assign IDs to functions, determine sk_in size
3131 int skInSize = -1;
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003132 for (const auto& e : program) {
3133 switch (e.fKind) {
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003134 case ProgramElement::kFunction_Kind: {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003135 FunctionDefinition& f = (FunctionDefinition&) e;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003136 fFunctionMap[&f.fDeclaration] = this->nextId();
3137 break;
3138 }
3139 case ProgramElement::kModifiers_Kind: {
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003140 Modifiers& m = ((ModifiersDeclaration&) e).fModifiers;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003141 if (m.fFlags & Modifiers::kIn_Flag) {
3142 switch (m.fLayout.fPrimitive) {
3143 case Layout::kPoints_Primitive: // break
3144 case Layout::kLines_Primitive:
3145 skInSize = 1;
3146 break;
3147 case Layout::kLinesAdjacency_Primitive: // break
3148 skInSize = 2;
3149 break;
3150 case Layout::kTriangles_Primitive: // break
3151 case Layout::kTrianglesAdjacency_Primitive:
3152 skInSize = 3;
3153 break;
3154 default:
3155 break;
3156 }
3157 }
3158 break;
3159 }
3160 default:
3161 break;
ethannicholasb3058bd2016-07-01 08:22:01 -07003162 }
3163 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003164 for (const auto& e : program) {
3165 if (e.fKind == ProgramElement::kInterfaceBlock_Kind) {
3166 InterfaceBlock& intf = (InterfaceBlock&) e;
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003167 if (SK_IN_BUILTIN == intf.fVariable.fModifiers.fLayout.fBuiltin) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003168 SkASSERT(skInSize != -1);
Ethan Nicholasb6ba82c2018-01-17 15:21:50 -05003169 intf.fSizes.emplace_back(new IntLiteral(fContext, -1, skInSize));
3170 }
ethannicholasb3058bd2016-07-01 08:22:01 -07003171 SpvId id = this->writeInterfaceBlock(intf);
Ethan Nicholas16c11962018-03-16 12:20:54 -04003172 if (((intf.fVariable.fModifiers.fFlags & Modifiers::kIn_Flag) ||
3173 (intf.fVariable.fModifiers.fFlags & Modifiers::kOut_Flag)) &&
3174 intf.fVariable.fModifiers.fLayout.fBuiltin == -1) {
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003175 interfaceVars.insert(id);
ethannicholasb3058bd2016-07-01 08:22:01 -07003176 }
3177 }
3178 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003179 for (const auto& e : program) {
3180 if (e.fKind == ProgramElement::kVar_Kind) {
3181 this->writeGlobalVars(program.fKind, ((VarDeclarations&) e), body);
ethannicholasb3058bd2016-07-01 08:22:01 -07003182 }
3183 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003184 for (const auto& e : program) {
3185 if (e.fKind == ProgramElement::kFunction_Kind) {
3186 this->writeFunction(((FunctionDefinition&) e), body);
ethannicholasb3058bd2016-07-01 08:22:01 -07003187 }
3188 }
ethannicholasd598f792016-07-25 10:08:54 -07003189 const FunctionDeclaration* main = nullptr;
ethannicholasb3058bd2016-07-01 08:22:01 -07003190 for (auto entry : fFunctionMap) {
ethannicholasf789b382016-08-03 12:43:36 -07003191 if (entry.first->fName == "main") {
ethannicholasb3058bd2016-07-01 08:22:01 -07003192 main = entry.first;
3193 }
3194 }
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003195 SkASSERT(main);
ethannicholasb3058bd2016-07-01 08:22:01 -07003196 for (auto entry : fVariableMap) {
ethannicholasd598f792016-07-25 10:08:54 -07003197 const Variable* var = entry.first;
Greg Daniel64773e62016-11-22 09:44:03 -05003198 if (var->fStorage == Variable::kGlobal_Storage &&
Ethan Nicholas16c11962018-03-16 12:20:54 -04003199 ((var->fModifiers.fFlags & Modifiers::kIn_Flag) ||
Ethan Nicholasd23c8192018-09-26 17:01:24 -04003200 (var->fModifiers.fFlags & Modifiers::kOut_Flag))) {
Ethan Nicholas8e48c1e2017-03-02 14:33:31 -05003201 interfaceVars.insert(entry.second);
ethannicholasb3058bd2016-07-01 08:22:01 -07003202 }
3203 }
3204 this->writeCapabilities(out);
3205 this->writeInstruction(SpvOpExtInstImport, fGLSLExtendedInstructions, "GLSL.std.450", out);
3206 this->writeInstruction(SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemoryModelGLSL450, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07003207 this->writeOpCode(SpvOpEntryPoint, (SpvId) (3 + (main->fName.fLength + 4) / 4) +
ethannicholasb3058bd2016-07-01 08:22:01 -07003208 (int32_t) interfaceVars.size(), out);
3209 switch (program.fKind) {
3210 case Program::kVertex_Kind:
3211 this->writeWord(SpvExecutionModelVertex, out);
3212 break;
3213 case Program::kFragment_Kind:
3214 this->writeWord(SpvExecutionModelFragment, out);
3215 break;
Ethan Nicholas52cad152017-02-16 16:37:32 -05003216 case Program::kGeometry_Kind:
3217 this->writeWord(SpvExecutionModelGeometry, out);
3218 break;
Ethan Nicholas762466e2017-06-29 10:03:38 -04003219 default:
3220 ABORT("cannot write this kind of program to SPIR-V\n");
ethannicholasb3058bd2016-07-01 08:22:01 -07003221 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003222 SpvId entryPoint = fFunctionMap[main];
3223 this->writeWord(entryPoint, out);
Ethan Nicholas5b5f0962017-09-11 13:50:14 -07003224 this->writeString(main->fName.fChars, main->fName.fLength, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003225 for (int var : interfaceVars) {
3226 this->writeWord(var, out);
3227 }
Ethan Nicholasbb155e22017-07-24 10:05:09 -04003228 if (program.fKind == Program::kGeometry_Kind) {
3229 this->writeGeometryShaderExecutionMode(entryPoint, out);
3230 }
ethannicholasb3058bd2016-07-01 08:22:01 -07003231 if (program.fKind == Program::kFragment_Kind) {
Greg Daniel64773e62016-11-22 09:44:03 -05003232 this->writeInstruction(SpvOpExecutionMode,
3233 fFunctionMap[main],
ethannicholasb3058bd2016-07-01 08:22:01 -07003234 SpvExecutionModeOriginUpperLeft,
3235 out);
3236 }
Ethan Nicholas3c6ae622018-04-24 13:06:09 -04003237 for (const auto& e : program) {
3238 if (e.fKind == ProgramElement::kExtension_Kind) {
3239 this->writeInstruction(SpvOpSourceExtension, ((Extension&) e).fName.c_str(), out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003240 }
3241 }
Greg Daniel64773e62016-11-22 09:44:03 -05003242
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003243 write_stringstream(fExtraGlobalsBuffer, out);
3244 write_stringstream(fNameBuffer, out);
3245 write_stringstream(fDecorationBuffer, out);
3246 write_stringstream(fConstantBuffer, out);
3247 write_stringstream(fExternalFunctionsBuffer, out);
3248 write_stringstream(body, out);
ethannicholasb3058bd2016-07-01 08:22:01 -07003249}
3250
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003251bool SPIRVCodeGenerator::generateCode() {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -04003252 SkASSERT(!fErrors.errorCount());
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003253 this->writeWord(SpvMagicNumber, *fOut);
3254 this->writeWord(SpvVersion, *fOut);
3255 this->writeWord(SKSL_MAGIC, *fOut);
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003256 StringStream buffer;
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003257 this->writeInstructions(fProgram, buffer);
3258 this->writeWord(fIdCount, *fOut);
3259 this->writeWord(0, *fOut); // reserved, always zero
Ethan Nicholas0df1b042017-03-31 13:56:23 -04003260 write_stringstream(buffer, *fOut);
Ethan Nicholas941e7e22016-12-12 15:33:30 -05003261 return 0 == fErrors.errorCount();
ethannicholasb3058bd2016-07-01 08:22:01 -07003262}
3263
3264}