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