• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/codegen/SkSLSPIRVCodeGenerator.h"
9 
10 #include "src/sksl/GLSL.std.450.h"
11 
12 #include "include/sksl/DSLCore.h"
13 #include "src/sksl/SkSLCompiler.h"
14 #include "src/sksl/SkSLOperators.h"
15 #include "src/sksl/SkSLThreadContext.h"
16 #include "src/sksl/ir/SkSLBinaryExpression.h"
17 #include "src/sksl/ir/SkSLBlock.h"
18 #include "src/sksl/ir/SkSLConstructorArrayCast.h"
19 #include "src/sksl/ir/SkSLConstructorCompound.h"
20 #include "src/sksl/ir/SkSLConstructorCompoundCast.h"
21 #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
22 #include "src/sksl/ir/SkSLConstructorMatrixResize.h"
23 #include "src/sksl/ir/SkSLConstructorScalarCast.h"
24 #include "src/sksl/ir/SkSLConstructorSplat.h"
25 #include "src/sksl/ir/SkSLDoStatement.h"
26 #include "src/sksl/ir/SkSLExpressionStatement.h"
27 #include "src/sksl/ir/SkSLExtension.h"
28 #include "src/sksl/ir/SkSLField.h"
29 #include "src/sksl/ir/SkSLFieldAccess.h"
30 #include "src/sksl/ir/SkSLForStatement.h"
31 #include "src/sksl/ir/SkSLFunctionCall.h"
32 #include "src/sksl/ir/SkSLFunctionDeclaration.h"
33 #include "src/sksl/ir/SkSLFunctionDefinition.h"
34 #include "src/sksl/ir/SkSLIfStatement.h"
35 #include "src/sksl/ir/SkSLIndexExpression.h"
36 #include "src/sksl/ir/SkSLInterfaceBlock.h"
37 #include "src/sksl/ir/SkSLPostfixExpression.h"
38 #include "src/sksl/ir/SkSLPrefixExpression.h"
39 #include "src/sksl/ir/SkSLReturnStatement.h"
40 #include "src/sksl/ir/SkSLSwitchStatement.h"
41 #include "src/sksl/ir/SkSLSwizzle.h"
42 #include "src/sksl/ir/SkSLTernaryExpression.h"
43 #include "src/sksl/ir/SkSLVarDeclarations.h"
44 #include "src/sksl/ir/SkSLVariableReference.h"
45 
46 #ifdef SK_VULKAN
47 #include "src/gpu/vk/GrVkCaps.h"
48 #endif
49 
50 #ifdef SKSL_EXT
51 #include "src/sksl/SkSLConstantFolder.h"
52 #endif
53 
54 #define kLast_Capability SpvCapabilityMultiViewport
55 
56 constexpr int DEVICE_FRAGCOORDS_BUILTIN = -1000;
57 constexpr int DEVICE_CLOCKWISE_BUILTIN  = -1001;
58 
59 namespace SkSL {
60 
61 // Skia's magic number is 31 and goes in the top 16 bits. We can use the lower bits to version the
62 // sksl generator if we want.
63 // https://github.com/KhronosGroup/SPIRV-Headers/blob/master/include/spirv/spir-v.xml#L84
64 static const int32_t SKSL_MAGIC  = 0x001F0000;
65 
setupIntrinsics()66 void SPIRVCodeGenerator::setupIntrinsics() {
67 #define ALL_GLSL(x) std::make_tuple(kGLSL_STD_450_IntrinsicOpcodeKind, GLSLstd450 ## x, \
68                                     GLSLstd450 ## x, GLSLstd450 ## x, GLSLstd450 ## x)
69 #define BY_TYPE_GLSL(ifFloat, ifInt, ifUInt) std::make_tuple(kGLSL_STD_450_IntrinsicOpcodeKind, \
70                                                              GLSLstd450 ## ifFloat,             \
71                                                              GLSLstd450 ## ifInt,               \
72                                                              GLSLstd450 ## ifUInt,              \
73                                                              SpvOpUndef)
74 #define ALL_SPIRV(x) std::make_tuple(kSPIRV_IntrinsicOpcodeKind, \
75                                      SpvOp ## x, SpvOp ## x, SpvOp ## x, SpvOp ## x)
76 #define SPECIAL(x) std::make_tuple(kSpecial_IntrinsicOpcodeKind, k ## x ## _SpecialIntrinsic, \
77                                    k ## x ## _SpecialIntrinsic, k ## x ## _SpecialIntrinsic,  \
78                                    k ## x ## _SpecialIntrinsic)
79     fIntrinsicMap[k_round_IntrinsicKind]         = ALL_GLSL(Round);
80     fIntrinsicMap[k_roundEven_IntrinsicKind]     = ALL_GLSL(RoundEven);
81     fIntrinsicMap[k_trunc_IntrinsicKind]         = ALL_GLSL(Trunc);
82     fIntrinsicMap[k_abs_IntrinsicKind]           = BY_TYPE_GLSL(FAbs, SAbs, SAbs);
83     fIntrinsicMap[k_sign_IntrinsicKind]          = BY_TYPE_GLSL(FSign, SSign, SSign);
84     fIntrinsicMap[k_floor_IntrinsicKind]         = ALL_GLSL(Floor);
85     fIntrinsicMap[k_ceil_IntrinsicKind]          = ALL_GLSL(Ceil);
86     fIntrinsicMap[k_fract_IntrinsicKind]         = ALL_GLSL(Fract);
87     fIntrinsicMap[k_radians_IntrinsicKind]       = ALL_GLSL(Radians);
88     fIntrinsicMap[k_degrees_IntrinsicKind]       = ALL_GLSL(Degrees);
89     fIntrinsicMap[k_sin_IntrinsicKind]           = ALL_GLSL(Sin);
90     fIntrinsicMap[k_cos_IntrinsicKind]           = ALL_GLSL(Cos);
91     fIntrinsicMap[k_tan_IntrinsicKind]           = ALL_GLSL(Tan);
92     fIntrinsicMap[k_asin_IntrinsicKind]          = ALL_GLSL(Asin);
93     fIntrinsicMap[k_acos_IntrinsicKind]          = ALL_GLSL(Acos);
94     fIntrinsicMap[k_atan_IntrinsicKind]          = SPECIAL(Atan);
95     fIntrinsicMap[k_sinh_IntrinsicKind]          = ALL_GLSL(Sinh);
96     fIntrinsicMap[k_cosh_IntrinsicKind]          = ALL_GLSL(Cosh);
97     fIntrinsicMap[k_tanh_IntrinsicKind]          = ALL_GLSL(Tanh);
98     fIntrinsicMap[k_asinh_IntrinsicKind]         = ALL_GLSL(Asinh);
99     fIntrinsicMap[k_acosh_IntrinsicKind]         = ALL_GLSL(Acosh);
100     fIntrinsicMap[k_atanh_IntrinsicKind]         = ALL_GLSL(Atanh);
101     fIntrinsicMap[k_pow_IntrinsicKind]           = ALL_GLSL(Pow);
102     fIntrinsicMap[k_exp_IntrinsicKind]           = ALL_GLSL(Exp);
103     fIntrinsicMap[k_log_IntrinsicKind]           = ALL_GLSL(Log);
104     fIntrinsicMap[k_exp2_IntrinsicKind]          = ALL_GLSL(Exp2);
105     fIntrinsicMap[k_log2_IntrinsicKind]          = ALL_GLSL(Log2);
106     fIntrinsicMap[k_sqrt_IntrinsicKind]          = ALL_GLSL(Sqrt);
107     fIntrinsicMap[k_inverse_IntrinsicKind]       = ALL_GLSL(MatrixInverse);
108     fIntrinsicMap[k_outerProduct_IntrinsicKind]  = ALL_SPIRV(OuterProduct);
109     fIntrinsicMap[k_transpose_IntrinsicKind]     = ALL_SPIRV(Transpose);
110     fIntrinsicMap[k_isinf_IntrinsicKind]         = ALL_SPIRV(IsInf);
111     fIntrinsicMap[k_isnan_IntrinsicKind]         = ALL_SPIRV(IsNan);
112     fIntrinsicMap[k_inversesqrt_IntrinsicKind]   = ALL_GLSL(InverseSqrt);
113     fIntrinsicMap[k_determinant_IntrinsicKind]   = ALL_GLSL(Determinant);
114     fIntrinsicMap[k_matrixCompMult_IntrinsicKind] = SPECIAL(MatrixCompMult);
115     fIntrinsicMap[k_matrixInverse_IntrinsicKind] = ALL_GLSL(MatrixInverse);
116     fIntrinsicMap[k_mod_IntrinsicKind]           = SPECIAL(Mod);
117     fIntrinsicMap[k_modf_IntrinsicKind]          = ALL_GLSL(Modf);
118     fIntrinsicMap[k_min_IntrinsicKind]           = SPECIAL(Min);
119     fIntrinsicMap[k_max_IntrinsicKind]           = SPECIAL(Max);
120     fIntrinsicMap[k_clamp_IntrinsicKind]         = SPECIAL(Clamp);
121     fIntrinsicMap[k_saturate_IntrinsicKind]      = SPECIAL(Saturate);
122     fIntrinsicMap[k_dot_IntrinsicKind]           = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
123                                                       SpvOpDot, SpvOpUndef, SpvOpUndef, SpvOpUndef);
124     fIntrinsicMap[k_mix_IntrinsicKind]           = SPECIAL(Mix);
125     fIntrinsicMap[k_step_IntrinsicKind]          = SPECIAL(Step);
126     fIntrinsicMap[k_smoothstep_IntrinsicKind]    = SPECIAL(SmoothStep);
127     fIntrinsicMap[k_fma_IntrinsicKind]           = ALL_GLSL(Fma);
128     fIntrinsicMap[k_frexp_IntrinsicKind]         = ALL_GLSL(Frexp);
129     fIntrinsicMap[k_ldexp_IntrinsicKind]         = ALL_GLSL(Ldexp);
130 
131 #define PACK(type) fIntrinsicMap[k_pack##type##_IntrinsicKind] = ALL_GLSL(Pack##type); \
132                    fIntrinsicMap[k_unpack##type##_IntrinsicKind] = ALL_GLSL(Unpack##type)
133     PACK(Snorm4x8);
134     PACK(Unorm4x8);
135     PACK(Snorm2x16);
136     PACK(Unorm2x16);
137     PACK(Half2x16);
138     PACK(Double2x32);
139 #undef PACK
140     fIntrinsicMap[k_length_IntrinsicKind]      = ALL_GLSL(Length);
141     fIntrinsicMap[k_distance_IntrinsicKind]    = ALL_GLSL(Distance);
142     fIntrinsicMap[k_cross_IntrinsicKind]       = ALL_GLSL(Cross);
143     fIntrinsicMap[k_normalize_IntrinsicKind]   = ALL_GLSL(Normalize);
144     fIntrinsicMap[k_faceforward_IntrinsicKind] = ALL_GLSL(FaceForward);
145     fIntrinsicMap[k_reflect_IntrinsicKind]     = ALL_GLSL(Reflect);
146     fIntrinsicMap[k_refract_IntrinsicKind]     = ALL_GLSL(Refract);
147     fIntrinsicMap[k_bitCount_IntrinsicKind]    = ALL_SPIRV(BitCount);
148     fIntrinsicMap[k_findLSB_IntrinsicKind]     = ALL_GLSL(FindILsb);
149     fIntrinsicMap[k_findMSB_IntrinsicKind]     = BY_TYPE_GLSL(FindSMsb, FindSMsb, FindUMsb);
150     fIntrinsicMap[k_dFdx_IntrinsicKind]        = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
151                                                                  SpvOpDPdx, SpvOpUndef,
152                                                                  SpvOpUndef, SpvOpUndef);
153     fIntrinsicMap[k_dFdy_IntrinsicKind]        = SPECIAL(DFdy);
154     fIntrinsicMap[k_fwidth_IntrinsicKind]      = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
155                                                                  SpvOpFwidth, SpvOpUndef,
156                                                                  SpvOpUndef, SpvOpUndef);
157     fIntrinsicMap[k_makeSampler2D_IntrinsicKind] = SPECIAL(SampledImage);
158 
159     fIntrinsicMap[k_sample_IntrinsicKind]      = SPECIAL(Texture);
160     fIntrinsicMap[k_subpassLoad_IntrinsicKind] = SPECIAL(SubpassLoad);
161 
162     fIntrinsicMap[k_floatBitsToInt_IntrinsicKind]  = ALL_SPIRV(Bitcast);
163     fIntrinsicMap[k_floatBitsToUint_IntrinsicKind] = ALL_SPIRV(Bitcast);
164     fIntrinsicMap[k_intBitsToFloat_IntrinsicKind]  = ALL_SPIRV(Bitcast);
165     fIntrinsicMap[k_uintBitsToFloat_IntrinsicKind] = ALL_SPIRV(Bitcast);
166 
167     fIntrinsicMap[k_any_IntrinsicKind]        = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
168                                                                 SpvOpUndef, SpvOpUndef,
169                                                                 SpvOpUndef, SpvOpAny);
170     fIntrinsicMap[k_all_IntrinsicKind]        = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
171                                                                 SpvOpUndef, SpvOpUndef,
172                                                                 SpvOpUndef, SpvOpAll);
173     fIntrinsicMap[k_not_IntrinsicKind]        = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
174                                                                 SpvOpUndef, SpvOpUndef, SpvOpUndef,
175                                                                 SpvOpLogicalNot);
176     fIntrinsicMap[k_equal_IntrinsicKind]      = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
177                                                                 SpvOpFOrdEqual, SpvOpIEqual,
178                                                                 SpvOpIEqual, SpvOpLogicalEqual);
179 #ifdef SKSL_EXT
180     fIntrinsicMap[k_notEqual_IntrinsicKind]   = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
181                                                                 SpvOpFUnordNotEqual, SpvOpINotEqual,
182                                                                 SpvOpINotEqual,
183                                                                 SpvOpLogicalNotEqual);
184 #else
185     fIntrinsicMap[k_notEqual_IntrinsicKind]   = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
186                                                                 SpvOpFOrdNotEqual, SpvOpINotEqual,
187                                                                 SpvOpINotEqual,
188                                                                 SpvOpLogicalNotEqual);
189 #endif
190     fIntrinsicMap[k_lessThan_IntrinsicKind]         = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
191                                                                       SpvOpFOrdLessThan,
192                                                                       SpvOpSLessThan,
193                                                                       SpvOpULessThan,
194                                                                       SpvOpUndef);
195     fIntrinsicMap[k_lessThanEqual_IntrinsicKind]    = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
196                                                                       SpvOpFOrdLessThanEqual,
197                                                                       SpvOpSLessThanEqual,
198                                                                       SpvOpULessThanEqual,
199                                                                       SpvOpUndef);
200     fIntrinsicMap[k_greaterThan_IntrinsicKind]      = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
201                                                                       SpvOpFOrdGreaterThan,
202                                                                       SpvOpSGreaterThan,
203                                                                       SpvOpUGreaterThan,
204                                                                       SpvOpUndef);
205     fIntrinsicMap[k_greaterThanEqual_IntrinsicKind] = std::make_tuple(kSPIRV_IntrinsicOpcodeKind,
206                                                                       SpvOpFOrdGreaterThanEqual,
207                                                                       SpvOpSGreaterThanEqual,
208                                                                       SpvOpUGreaterThanEqual,
209                                                                       SpvOpUndef);
210 #ifdef SKSL_EXT
211     fIntrinsicMap[k_textureSize_IntrinsicKind] = SPECIAL(TextureSize);
212     fIntrinsicMap[k_nonuniformEXT_IntrinsicKind] = SPECIAL(NonuniformEXT);
213 #endif
214 // interpolateAt* not yet supported...
215 }
216 
writeWord(int32_t word,OutputStream & out)217 void SPIRVCodeGenerator::writeWord(int32_t word, OutputStream& out) {
218     out.write((const char*) &word, sizeof(word));
219 }
220 
is_float(const Context & context,const Type & type)221 static bool is_float(const Context& context, const Type& type) {
222     return (type.isScalar() || type.isVector() || type.isMatrix()) &&
223            type.componentType().isFloat();
224 }
225 
is_signed(const Context & context,const Type & type)226 static bool is_signed(const Context& context, const Type& type) {
227     return (type.isScalar() || type.isVector()) && type.componentType().isSigned();
228 }
229 
is_unsigned(const Context & context,const Type & type)230 static bool is_unsigned(const Context& context, const Type& type) {
231     return (type.isScalar() || type.isVector()) && type.componentType().isUnsigned();
232 }
233 
is_bool(const Context & context,const Type & type)234 static bool is_bool(const Context& context, const Type& type) {
235     return (type.isScalar() || type.isVector()) && type.componentType().isBoolean();
236 }
237 
is_out(const Modifiers & m)238 static bool is_out(const Modifiers& m) {
239     return (m.fFlags & Modifiers::kOut_Flag) != 0;
240 }
241 
is_in(const Modifiers & m)242 static bool is_in(const Modifiers& m) {
243     switch (m.fFlags & (Modifiers::kOut_Flag | Modifiers::kIn_Flag)) {
244         case Modifiers::kOut_Flag:                       // out
245             return false;
246 
247         case 0:                                          // implicit in
248         case Modifiers::kIn_Flag:                        // explicit in
249         case Modifiers::kOut_Flag | Modifiers::kIn_Flag: // inout
250             return true;
251 
252         default: SkUNREACHABLE;
253     }
254 }
255 
writeOpCode(SpvOp_ opCode,int length,OutputStream & out)256 void SPIRVCodeGenerator::writeOpCode(SpvOp_ opCode, int length, OutputStream& out) {
257     SkASSERT(opCode != SpvOpLoad || &out != &fConstantBuffer);
258     SkASSERT(opCode != SpvOpUndef);
259     switch (opCode) {
260         case SpvOpReturn:      // fall through
261         case SpvOpReturnValue: // fall through
262         case SpvOpKill:        // fall through
263         case SpvOpSwitch:      // fall through
264         case SpvOpBranch:      // fall through
265         case SpvOpBranchConditional:
266             if (fCurrentBlock == 0) {
267                 // We just encountered dead code--instructions that don't have an associated block.
268                 // Synthesize a label if this happens; this is necessary to satisfy the validator.
269                 this->writeLabel(this->nextId(nullptr), out);
270             }
271             fCurrentBlock = 0;
272             break;
273         case SpvOpConstant:          // fall through
274         case SpvOpConstantTrue:      // fall through
275         case SpvOpConstantFalse:     // fall through
276         case SpvOpConstantComposite: // fall through
277         case SpvOpTypeVoid:          // fall through
278         case SpvOpTypeInt:           // fall through
279         case SpvOpTypeFloat:         // fall through
280         case SpvOpTypeBool:          // fall through
281         case SpvOpTypeVector:        // fall through
282         case SpvOpTypeMatrix:        // fall through
283         case SpvOpTypeArray:         // fall through
284         case SpvOpTypePointer:       // fall through
285         case SpvOpTypeFunction:      // fall through
286         case SpvOpTypeRuntimeArray:  // fall through
287         case SpvOpTypeStruct:        // fall through
288         case SpvOpTypeImage:         // fall through
289         case SpvOpTypeSampledImage:  // fall through
290         case SpvOpTypeSampler:       // fall through
291         case SpvOpVariable:          // fall through
292         case SpvOpFunction:          // fall through
293         case SpvOpFunctionParameter: // fall through
294         case SpvOpFunctionEnd:       // fall through
295         case SpvOpExecutionMode:     // fall through
296         case SpvOpMemoryModel:       // fall through
297         case SpvOpCapability:        // fall through
298         case SpvOpExtInstImport:     // fall through
299         case SpvOpEntryPoint:        // fall through
300         case SpvOpSource:            // fall through
301         case SpvOpSourceExtension:   // fall through
302         case SpvOpName:              // fall through
303         case SpvOpMemberName:        // fall through
304         case SpvOpDecorate:          // fall through
305         case SpvOpMemberDecorate:
306 #ifdef SKSL_EXT
307         case SpvOpExtension:
308         case SpvOpSpecConstant:
309         case SpvOpSpecConstantOp:
310 #endif
311             break;
312         default:
313             // We may find ourselves with dead code--instructions that don't have an associated
314             // block. This should be a rare event, but if it happens, synthesize a label; this is
315             // necessary to satisfy the validator.
316             if (fCurrentBlock == 0) {
317                 this->writeLabel(this->nextId(nullptr), out);
318             }
319             break;
320     }
321     this->writeWord((length << 16) | opCode, out);
322 }
323 
writeLabel(SpvId label,OutputStream & out)324 void SPIRVCodeGenerator::writeLabel(SpvId label, OutputStream& out) {
325     SkASSERT(!fCurrentBlock);
326     fCurrentBlock = label;
327     this->writeInstruction(SpvOpLabel, label, out);
328 }
329 
writeInstruction(SpvOp_ opCode,OutputStream & out)330 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, OutputStream& out) {
331     this->writeOpCode(opCode, 1, out);
332 }
333 
writeInstruction(SpvOp_ opCode,int32_t word1,OutputStream & out)334 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out) {
335     this->writeOpCode(opCode, 2, out);
336     this->writeWord(word1, out);
337 }
338 
writeString(skstd::string_view s,OutputStream & out)339 void SPIRVCodeGenerator::writeString(skstd::string_view s, OutputStream& out) {
340     out.write(s.data(), s.length());
341     switch (s.length() % 4) {
342         case 1:
343             out.write8(0);
344             [[fallthrough]];
345         case 2:
346             out.write8(0);
347             [[fallthrough]];
348         case 3:
349             out.write8(0);
350             break;
351         default:
352             this->writeWord(0, out);
353             break;
354     }
355 }
356 
writeInstruction(SpvOp_ opCode,skstd::string_view string,OutputStream & out)357 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, skstd::string_view string,
358                                           OutputStream& out) {
359     this->writeOpCode(opCode, 1 + (string.length() + 4) / 4, out);
360     this->writeString(string, out);
361 }
362 
363 
writeInstruction(SpvOp_ opCode,int32_t word1,skstd::string_view string,OutputStream & out)364 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, skstd::string_view string,
365                                           OutputStream& out) {
366     this->writeOpCode(opCode, 2 + (string.length() + 4) / 4, out);
367     this->writeWord(word1, out);
368     this->writeString(string, out);
369 }
370 
writeInstruction(SpvOp_ opCode,int32_t word1,int32_t word2,skstd::string_view string,OutputStream & out)371 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
372                                           skstd::string_view string, OutputStream& out) {
373     this->writeOpCode(opCode, 3 + (string.length() + 4) / 4, out);
374     this->writeWord(word1, out);
375     this->writeWord(word2, out);
376     this->writeString(string, out);
377 }
378 
writeInstruction(SpvOp_ opCode,int32_t word1,int32_t word2,OutputStream & out)379 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
380                                           OutputStream& out) {
381     this->writeOpCode(opCode, 3, out);
382     this->writeWord(word1, out);
383     this->writeWord(word2, out);
384 }
385 
writeInstruction(SpvOp_ opCode,int32_t word1,int32_t word2,int32_t word3,OutputStream & out)386 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
387                                           int32_t word3, OutputStream& out) {
388     this->writeOpCode(opCode, 4, out);
389     this->writeWord(word1, out);
390     this->writeWord(word2, out);
391     this->writeWord(word3, out);
392 }
393 
writeInstruction(SpvOp_ opCode,int32_t word1,int32_t word2,int32_t word3,int32_t word4,OutputStream & out)394 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
395                                           int32_t word3, int32_t word4, OutputStream& out) {
396     this->writeOpCode(opCode, 5, out);
397     this->writeWord(word1, out);
398     this->writeWord(word2, out);
399     this->writeWord(word3, out);
400     this->writeWord(word4, out);
401 }
402 
writeInstruction(SpvOp_ opCode,int32_t word1,int32_t word2,int32_t word3,int32_t word4,int32_t word5,OutputStream & out)403 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
404                                           int32_t word3, int32_t word4, int32_t word5,
405                                           OutputStream& out) {
406     this->writeOpCode(opCode, 6, out);
407     this->writeWord(word1, out);
408     this->writeWord(word2, out);
409     this->writeWord(word3, out);
410     this->writeWord(word4, out);
411     this->writeWord(word5, out);
412 }
413 
writeInstruction(SpvOp_ opCode,int32_t word1,int32_t word2,int32_t word3,int32_t word4,int32_t word5,int32_t word6,OutputStream & out)414 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
415                                           int32_t word3, int32_t word4, int32_t word5,
416                                           int32_t word6, OutputStream& out) {
417     this->writeOpCode(opCode, 7, out);
418     this->writeWord(word1, out);
419     this->writeWord(word2, out);
420     this->writeWord(word3, out);
421     this->writeWord(word4, out);
422     this->writeWord(word5, out);
423     this->writeWord(word6, out);
424 }
425 
writeInstruction(SpvOp_ opCode,int32_t word1,int32_t word2,int32_t word3,int32_t word4,int32_t word5,int32_t word6,int32_t word7,OutputStream & out)426 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
427                                           int32_t word3, int32_t word4, int32_t word5,
428                                           int32_t word6, int32_t word7, OutputStream& out) {
429     this->writeOpCode(opCode, 8, out);
430     this->writeWord(word1, out);
431     this->writeWord(word2, out);
432     this->writeWord(word3, out);
433     this->writeWord(word4, out);
434     this->writeWord(word5, out);
435     this->writeWord(word6, out);
436     this->writeWord(word7, out);
437 }
438 
writeInstruction(SpvOp_ opCode,int32_t word1,int32_t word2,int32_t word3,int32_t word4,int32_t word5,int32_t word6,int32_t word7,int32_t word8,OutputStream & out)439 void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
440                                           int32_t word3, int32_t word4, int32_t word5,
441                                           int32_t word6, int32_t word7, int32_t word8,
442                                           OutputStream& out) {
443     this->writeOpCode(opCode, 9, out);
444     this->writeWord(word1, out);
445     this->writeWord(word2, out);
446     this->writeWord(word3, out);
447     this->writeWord(word4, out);
448     this->writeWord(word5, out);
449     this->writeWord(word6, out);
450     this->writeWord(word7, out);
451     this->writeWord(word8, out);
452 }
453 
454 #ifdef SKSL_EXT
writeOpLoad(SpvId type,Precision precision,SpvId pointer,OutputStream & out)455 SpvId SPIRVCodeGenerator::writeOpLoad(SpvId type,
456                                       Precision precision,
457                                       SpvId pointer,
458                                       OutputStream& out) {
459     // write the requested OpLoad instruction.
460     SpvId result = -1;
461     if (fNonUniformSpvId.find(pointer) != fNonUniformSpvId.end()) {
462         result = this->nextId(nullptr);
463         this->writeInstruction(SpvOpDecorate, result, SpvDecorationNonUniform, fDecorationBuffer);
464     } else {
465         result = this->nextId(precision);
466     }
467     this->writeInstruction(SpvOpLoad, type, result, pointer, out);
468     return result;
469 }
470 
writeExtensions(OutputStream & out)471 void SPIRVCodeGenerator::writeExtensions(OutputStream& out) {
472     for (const auto& ext : fExtensions) {
473         this->writeInstruction(SpvOpExtension, ext, out);
474     }
475 }
476 
writePrecisionDecoration(SpvId var,const Type & type)477 void SPIRVCodeGenerator::writePrecisionDecoration(SpvId var, const Type& type) {
478     if (type.hasPrecision() && !type.highPrecision() &&
479         !fProgram.fConfig->fSettings.fForceHighPrecision) {
480         this->writeInstruction(SpvOpDecorate, var, SpvDecorationRelaxedPrecision, fDecorationBuffer);
481     }
482 }
483 #endif
484 
writeCapabilities(OutputStream & out)485 void SPIRVCodeGenerator::writeCapabilities(OutputStream& out) {
486     for (uint64_t i = 0, bit = 1; i <= kLast_Capability; i++, bit <<= 1) {
487         if (fCapabilities & bit) {
488             this->writeInstruction(SpvOpCapability, (SpvId) i, out);
489         }
490     }
491     this->writeInstruction(SpvOpCapability, SpvCapabilityShader, out);
492 #ifdef SKSL_EXT
493     for (auto i : fCapabilitiesExt) {
494         this->writeInstruction(SpvOpCapability, (SpvId) i, out);
495     }
496 #endif
497 }
498 
nextId(const Type * type)499 SpvId SPIRVCodeGenerator::nextId(const Type* type) {
500     return this->nextId(type && type->hasPrecision() && !type->highPrecision()
501                 ? Precision::kRelaxed
502                 : Precision::kDefault);
503 }
504 
nextId(Precision precision)505 SpvId SPIRVCodeGenerator::nextId(Precision precision) {
506     if (precision == Precision::kRelaxed && !fProgram.fConfig->fSettings.fForceHighPrecision) {
507         this->writeInstruction(SpvOpDecorate, fIdCount, SpvDecorationRelaxedPrecision,
508                                fDecorationBuffer);
509     }
510     return fIdCount++;
511 }
512 
writeStruct(const Type & type,const MemoryLayout & memoryLayout,SpvId resultId)513 void SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& memoryLayout,
514                                      SpvId resultId) {
515     this->writeInstruction(SpvOpName, resultId, String(type.name()).c_str(), fNameBuffer);
516     // go ahead and write all of the field types, so we don't inadvertently write them while we're
517     // in the middle of writing the struct instruction
518     std::vector<SpvId> types;
519     for (const auto& f : type.fields()) {
520         types.push_back(this->getType(*f.fType, memoryLayout));
521     }
522     this->writeOpCode(SpvOpTypeStruct, 2 + (int32_t) types.size(), fConstantBuffer);
523     this->writeWord(resultId, fConstantBuffer);
524     for (SpvId id : types) {
525         this->writeWord(id, fConstantBuffer);
526     }
527     size_t offset = 0;
528     for (int32_t i = 0; i < (int32_t) type.fields().size(); i++) {
529         const Type::Field& field = type.fields()[i];
530         if (!MemoryLayout::LayoutIsSupported(*field.fType)) {
531             fContext.fErrors->error(type.fLine, "type '" + field.fType->name() +
532                                     "' is not permitted here");
533             return;
534         }
535         size_t size = memoryLayout.size(*field.fType);
536         size_t alignment = memoryLayout.alignment(*field.fType);
537         const Layout& fieldLayout = field.fModifiers.fLayout;
538         if (fieldLayout.fOffset >= 0) {
539             if (fieldLayout.fOffset < (int) offset) {
540                 fContext.fErrors->error(type.fLine,
541                                         "offset of field '" + field.fName + "' must be at "
542                                         "least " + to_string((int) offset));
543             }
544             if (fieldLayout.fOffset % alignment) {
545                 fContext.fErrors->error(type.fLine,
546                                         "offset of field '" + field.fName + "' must be a multiple"
547                                         " of " + to_string((int) alignment));
548             }
549             offset = fieldLayout.fOffset;
550         } else {
551             size_t mod = offset % alignment;
552             if (mod) {
553                 offset += alignment - mod;
554             }
555         }
556         this->writeInstruction(SpvOpMemberName, resultId, i, field.fName, fNameBuffer);
557         this->writeLayout(fieldLayout, resultId, i);
558         if (field.fModifiers.fLayout.fBuiltin < 0) {
559             this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, SpvDecorationOffset,
560                                    (SpvId) offset, fDecorationBuffer);
561         }
562         if (field.fType->isMatrix()) {
563             this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationColMajor,
564                                    fDecorationBuffer);
565             this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationMatrixStride,
566                                    (SpvId) memoryLayout.stride(*field.fType),
567                                    fDecorationBuffer);
568         }
569 #ifdef SKSL_EXT
570         if (field.fType->isArray()) {
571             if (field.fType->componentType().isMatrix()) {
572                 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationColMajor,
573                                        fDecorationBuffer);
574                 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationMatrixStride,
575                                        (SpvId) memoryLayout.stride(field.fType->componentType()),
576                                        fDecorationBuffer);
577             }
578         }
579 #endif
580         if (!field.fType->highPrecision()) {
581             this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i,
582                                    SpvDecorationRelaxedPrecision, fDecorationBuffer);
583         }
584         offset += size;
585         if ((field.fType->isArray() || field.fType->isStruct()) && offset % alignment != 0) {
586             offset += alignment - offset % alignment;
587         }
588     }
589 }
590 
getActualType(const Type & type)591 const Type& SPIRVCodeGenerator::getActualType(const Type& type) {
592     if (type.isFloat()) {
593         return *fContext.fTypes.fFloat;
594     }
595     if (type.isSigned()) {
596         return *fContext.fTypes.fInt;
597     }
598     if (type.isUnsigned()) {
599         return *fContext.fTypes.fUInt;
600     }
601     if (type.isMatrix() || type.isVector()) {
602         if (type.componentType() == *fContext.fTypes.fHalf) {
603             return fContext.fTypes.fFloat->toCompound(fContext, type.columns(), type.rows());
604         }
605         if (type.componentType() == *fContext.fTypes.fShort) {
606             return fContext.fTypes.fInt->toCompound(fContext, type.columns(), type.rows());
607         }
608         if (type.componentType() == *fContext.fTypes.fUShort) {
609             return fContext.fTypes.fUInt->toCompound(fContext, type.columns(), type.rows());
610         }
611     }
612     return type;
613 }
614 
getType(const Type & type)615 SpvId SPIRVCodeGenerator::getType(const Type& type) {
616     return this->getType(type, fDefaultLayout);
617 }
618 
getType(const Type & rawType,const MemoryLayout & layout)619 SpvId SPIRVCodeGenerator::getType(const Type& rawType, const MemoryLayout& layout) {
620     const Type* type;
621     std::unique_ptr<Type> arrayType;
622     String arrayName;
623 
624     if (rawType.isArray()) {
625         // For arrays, we need to synthesize a temporary Array type using the "actual" component
626         // type. That is, if `short[10]` is passed in, we need to synthesize a `int[10]` Type.
627         // Otherwise, we can end up with two different SpvIds for the same array type.
628         const Type& component = this->getActualType(rawType.componentType());
629         arrayName = component.getArrayName(rawType.columns());
630         arrayType = Type::MakeArrayType(arrayName, component, rawType.columns());
631         type = arrayType.get();
632     } else {
633         // For non-array types, we can simply look up the "actual" type and use it.
634         type = &this->getActualType(rawType);
635     }
636 
637     String key(type->name());
638     if (type->isStruct() || type->isArray()) {
639         key += to_string((int)layout.fStd);
640 #ifdef SK_DEBUG
641         SkASSERT(layout.fStd == MemoryLayout::Standard::k140_Standard ||
642                  layout.fStd == MemoryLayout::Standard::k430_Standard);
643         MemoryLayout::Standard otherStd = layout.fStd == MemoryLayout::Standard::k140_Standard
644                                                   ? MemoryLayout::Standard::k430_Standard
645                                                   : MemoryLayout::Standard::k140_Standard;
646         String otherKey = type->name() + to_string((int)otherStd);
647         SkASSERT(fTypeMap.find(otherKey) == fTypeMap.end());
648 #endif
649     }
650     auto entry = fTypeMap.find(key);
651     if (entry == fTypeMap.end()) {
652         SpvId result = this->nextId(nullptr);
653         switch (type->typeKind()) {
654             case Type::TypeKind::kScalar:
655                 if (type->isBoolean()) {
656                     this->writeInstruction(SpvOpTypeBool, result, fConstantBuffer);
657                 } else if (type->isSigned()) {
658                     this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstantBuffer);
659                 } else if (type->isUnsigned()) {
660                     this->writeInstruction(SpvOpTypeInt, result, 32, 0, fConstantBuffer);
661                 } else if (type->isFloat()) {
662                     this->writeInstruction(SpvOpTypeFloat, result, 32, fConstantBuffer);
663                 } else {
664                     SkDEBUGFAILF("unrecognized scalar type '%s'", type->description().c_str());
665                 }
666                 break;
667             case Type::TypeKind::kVector:
668                 this->writeInstruction(SpvOpTypeVector, result,
669                                        this->getType(type->componentType(), layout),
670                                        type->columns(), fConstantBuffer);
671                 break;
672             case Type::TypeKind::kMatrix:
673                 this->writeInstruction(
674                         SpvOpTypeMatrix,
675                         result,
676                         this->getType(IndexExpression::IndexType(fContext, *type), layout),
677                         type->columns(),
678                         fConstantBuffer);
679                 break;
680             case Type::TypeKind::kStruct:
681                 this->writeStruct(*type, layout, result);
682                 break;
683             case Type::TypeKind::kArray: {
684                 if (!MemoryLayout::LayoutIsSupported(*type)) {
685                     fContext.fErrors->error(type->fLine,
686                                             "type '" + type->name() + "' is not permitted here");
687                     return this->nextId(nullptr);
688                 }
689                 if (type->columns() > 0) {
690                     SpvId typeId = this->getType(type->componentType(), layout);
691                     SpvId countId = this->writeLiteral(type->columns(), *fContext.fTypes.fInt);
692                     this->writeInstruction(SpvOpTypeArray, result, typeId, countId,
693                                            fConstantBuffer);
694                     this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride,
695                                            (int32_t) layout.stride(*type),
696                                            fDecorationBuffer);
697                 } else {
698 #ifdef SKSL_EXT
699                     if (type->componentType().typeKind() == Type::TypeKind::kSampler) {
700                         fCapabilitiesExt.insert(SpvCapabilityRuntimeDescriptorArray);
701                         fExtensions.insert("SPV_EXT_descriptor_indexing");
702                     }
703 #else
704                     // We shouldn't have any runtime-sized arrays right now
705                     fContext.fErrors->error(type->fLine,
706                                             "runtime-sized arrays are not supported in SPIR-V");
707 #endif
708                     this->writeInstruction(SpvOpTypeRuntimeArray, result,
709                                            this->getType(type->componentType(), layout),
710                                            fConstantBuffer);
711                     this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride,
712                                            (int32_t) layout.stride(*type),
713                                            fDecorationBuffer);
714                 }
715                 break;
716             }
717             case Type::TypeKind::kSampler: {
718                 SpvId image = result;
719                 if (SpvDimSubpassData != type->dimensions()) {
720                     image = this->getType(type->textureType(), layout);
721                 }
722                 if (SpvDimBuffer == type->dimensions()) {
723                     fCapabilities |= (((uint64_t) 1) << SpvCapabilitySampledBuffer);
724                 }
725                 if (SpvDimSubpassData != type->dimensions()) {
726                     this->writeInstruction(SpvOpTypeSampledImage, result, image, fConstantBuffer);
727                 }
728                 break;
729             }
730             case Type::TypeKind::kSeparateSampler: {
731                 this->writeInstruction(SpvOpTypeSampler, result, fConstantBuffer);
732                 break;
733             }
734             case Type::TypeKind::kTexture: {
735                 this->writeInstruction(SpvOpTypeImage, result,
736                                        this->getType(*fContext.fTypes.fFloat, layout),
737                                        type->dimensions(), type->isDepth(),
738                                        type->isArrayedTexture(), type->isMultisampled(),
739                                        type->isSampled() ? 1 : 2, SpvImageFormatUnknown,
740                                        fConstantBuffer);
741                 fImageTypeMap[key] = result;
742                 break;
743             }
744             default:
745                 if (type->isVoid()) {
746                     this->writeInstruction(SpvOpTypeVoid, result, fConstantBuffer);
747                 } else {
748                     SkDEBUGFAILF("invalid type: %s", type->description().c_str());
749                 }
750                 break;
751         }
752         fTypeMap[key] = result;
753         return result;
754     }
755     return entry->second;
756 }
757 
getImageType(const Type & type)758 SpvId SPIRVCodeGenerator::getImageType(const Type& type) {
759     SkASSERT(type.typeKind() == Type::TypeKind::kSampler);
760     this->getType(type);
761     String key = type.name() + to_string((int) fDefaultLayout.fStd);
762     SkASSERT(fImageTypeMap.find(key) != fImageTypeMap.end());
763     return fImageTypeMap[key];
764 }
765 
getFunctionType(const FunctionDeclaration & function)766 SpvId SPIRVCodeGenerator::getFunctionType(const FunctionDeclaration& function) {
767     String key = to_string(this->getType(function.returnType())) + "(";
768     String separator;
769     const std::vector<const Variable*>& parameters = function.parameters();
770     for (size_t i = 0; i < parameters.size(); i++) {
771         key += separator;
772         separator = ", ";
773         key += to_string(this->getType(parameters[i]->type()));
774     }
775     key += ")";
776     auto entry = fTypeMap.find(key);
777     if (entry == fTypeMap.end()) {
778         SpvId result = this->nextId(nullptr);
779         int32_t length = 3 + (int32_t) parameters.size();
780         SpvId returnType = this->getType(function.returnType());
781         std::vector<SpvId> parameterTypes;
782         for (size_t i = 0; i < parameters.size(); i++) {
783             // glslang seems to treat all function arguments as pointers whether they need to be or
784             // not. I  was initially puzzled by this until I ran bizarre failures with certain
785             // patterns of function calls and control constructs, as exemplified by this minimal
786             // failure case:
787             //
788             // void sphere(float x) {
789             // }
790             //
791             // void map() {
792             //     sphere(1.0);
793             // }
794             //
795             // void main() {
796             //     for (int i = 0; i < 1; i++) {
797             //         map();
798             //     }
799             // }
800             //
801             // As of this writing, compiling this in the "obvious" way (with sphere taking a float)
802             // crashes. Making it take a float* and storing the argument in a temporary variable,
803             // as glslang does, fixes it. It's entirely possible I simply missed whichever part of
804             // the spec makes this make sense.
805             parameterTypes.push_back(this->getPointerType(parameters[i]->type(),
806                                                           SpvStorageClassFunction));
807         }
808         this->writeOpCode(SpvOpTypeFunction, length, fConstantBuffer);
809         this->writeWord(result, fConstantBuffer);
810         this->writeWord(returnType, fConstantBuffer);
811         for (SpvId id : parameterTypes) {
812             this->writeWord(id, fConstantBuffer);
813         }
814         fTypeMap[key] = result;
815         return result;
816     }
817     return entry->second;
818 }
819 
getPointerType(const Type & type,SpvStorageClass_ storageClass)820 SpvId SPIRVCodeGenerator::getPointerType(const Type& type, SpvStorageClass_ storageClass) {
821     return this->getPointerType(type, fDefaultLayout, storageClass);
822 }
823 
getPointerType(const Type & rawType,const MemoryLayout & layout,SpvStorageClass_ storageClass)824 SpvId SPIRVCodeGenerator::getPointerType(const Type& rawType, const MemoryLayout& layout,
825                                          SpvStorageClass_ storageClass) {
826     const Type& type = this->getActualType(rawType);
827     String key = type.displayName() + "*" + to_string(layout.fStd) + to_string(storageClass);
828     auto entry = fTypeMap.find(key);
829     if (entry == fTypeMap.end()) {
830         SpvId result = this->nextId(nullptr);
831         this->writeInstruction(SpvOpTypePointer, result, storageClass,
832                                this->getType(type), fConstantBuffer);
833         fTypeMap[key] = result;
834         return result;
835     }
836     return entry->second;
837 }
838 
writeExpression(const Expression & expr,OutputStream & out)839 SpvId SPIRVCodeGenerator::writeExpression(const Expression& expr, OutputStream& out) {
840     switch (expr.kind()) {
841         case Expression::Kind::kBinary:
842             return this->writeBinaryExpression(expr.as<BinaryExpression>(), out);
843         case Expression::Kind::kConstructorArrayCast:
844             return this->writeExpression(*expr.as<ConstructorArrayCast>().argument(), out);
845         case Expression::Kind::kConstructorArray:
846         case Expression::Kind::kConstructorStruct:
847             return this->writeCompositeConstructor(expr.asAnyConstructor(), out);
848         case Expression::Kind::kConstructorDiagonalMatrix:
849             return this->writeConstructorDiagonalMatrix(expr.as<ConstructorDiagonalMatrix>(), out);
850         case Expression::Kind::kConstructorMatrixResize:
851             return this->writeConstructorMatrixResize(expr.as<ConstructorMatrixResize>(), out);
852         case Expression::Kind::kConstructorScalarCast:
853             return this->writeConstructorScalarCast(expr.as<ConstructorScalarCast>(), out);
854         case Expression::Kind::kConstructorSplat:
855             return this->writeConstructorSplat(expr.as<ConstructorSplat>(), out);
856         case Expression::Kind::kConstructorCompound:
857             return this->writeConstructorCompound(expr.as<ConstructorCompound>(), out);
858         case Expression::Kind::kConstructorCompoundCast:
859             return this->writeConstructorCompoundCast(expr.as<ConstructorCompoundCast>(), out);
860         case Expression::Kind::kFieldAccess:
861             return this->writeFieldAccess(expr.as<FieldAccess>(), out);
862         case Expression::Kind::kFunctionCall:
863             return this->writeFunctionCall(expr.as<FunctionCall>(), out);
864         case Expression::Kind::kLiteral:
865             return this->writeLiteral(expr.as<Literal>());
866         case Expression::Kind::kPrefix:
867             return this->writePrefixExpression(expr.as<PrefixExpression>(), out);
868         case Expression::Kind::kPostfix:
869             return this->writePostfixExpression(expr.as<PostfixExpression>(), out);
870         case Expression::Kind::kSwizzle:
871             return this->writeSwizzle(expr.as<Swizzle>(), out);
872         case Expression::Kind::kVariableReference:
873             return this->writeVariableReference(expr.as<VariableReference>(), out);
874         case Expression::Kind::kTernary:
875             return this->writeTernaryExpression(expr.as<TernaryExpression>(), out);
876         case Expression::Kind::kIndex:
877             return this->writeIndexExpression(expr.as<IndexExpression>(), out);
878         default:
879             SkDEBUGFAILF("unsupported expression: %s", expr.description().c_str());
880             break;
881     }
882     return -1;
883 }
884 
writeIntrinsicCall(const FunctionCall & c,OutputStream & out)885 SpvId SPIRVCodeGenerator::writeIntrinsicCall(const FunctionCall& c, OutputStream& out) {
886     const FunctionDeclaration& function = c.function();
887     auto intrinsic = fIntrinsicMap.find(function.intrinsicKind());
888     if (intrinsic == fIntrinsicMap.end()) {
889         fContext.fErrors->error(c.fLine, "unsupported intrinsic '" + function.description() + "'");
890         return -1;
891     }
892     int32_t intrinsicId;
893     const ExpressionArray& arguments = c.arguments();
894     if (arguments.size() > 0) {
895         const Type& type = arguments[0]->type();
896         if (std::get<0>(intrinsic->second) == kSpecial_IntrinsicOpcodeKind ||
897             is_float(fContext, type)) {
898             intrinsicId = std::get<1>(intrinsic->second);
899         } else if (is_signed(fContext, type)) {
900             intrinsicId = std::get<2>(intrinsic->second);
901         } else if (is_unsigned(fContext, type)) {
902             intrinsicId = std::get<3>(intrinsic->second);
903         } else if (is_bool(fContext, type)) {
904             intrinsicId = std::get<4>(intrinsic->second);
905         } else {
906             intrinsicId = std::get<1>(intrinsic->second);
907         }
908     } else {
909         intrinsicId = std::get<1>(intrinsic->second);
910     }
911     switch (std::get<0>(intrinsic->second)) {
912         case kGLSL_STD_450_IntrinsicOpcodeKind: {
913             SpvId result = this->nextId(&c.type());
914             std::vector<SpvId> argumentIds;
915             std::vector<TempVar> tempVars;
916             argumentIds.reserve(arguments.size());
917             for (size_t i = 0; i < arguments.size(); i++) {
918                 if (is_out(function.parameters()[i]->modifiers())) {
919                     argumentIds.push_back(
920                             this->writeFunctionCallArgument(*arguments[i],
921                                                             function.parameters()[i]->modifiers(),
922                                                             &tempVars,
923                                                             out));
924                 } else {
925                     argumentIds.push_back(this->writeExpression(*arguments[i], out));
926                 }
927             }
928             this->writeOpCode(SpvOpExtInst, 5 + (int32_t) argumentIds.size(), out);
929             this->writeWord(this->getType(c.type()), out);
930             this->writeWord(result, out);
931             this->writeWord(fGLSLExtendedInstructions, out);
932             this->writeWord(intrinsicId, out);
933             for (SpvId id : argumentIds) {
934                 this->writeWord(id, out);
935             }
936             this->copyBackTempVars(tempVars, out);
937             return result;
938         }
939         case kSPIRV_IntrinsicOpcodeKind: {
940             // GLSL supports dot(float, float), but SPIR-V does not. Convert it to FMul
941             if (intrinsicId == SpvOpDot && arguments[0]->type().isScalar()) {
942                 intrinsicId = SpvOpFMul;
943             }
944             SpvId result = this->nextId(&c.type());
945             std::vector<SpvId> argumentIds;
946             std::vector<TempVar> tempVars;
947             argumentIds.reserve(arguments.size());
948             for (size_t i = 0; i < arguments.size(); i++) {
949                 if (is_out(function.parameters()[i]->modifiers())) {
950                     argumentIds.push_back(
951                             this->writeFunctionCallArgument(*arguments[i],
952                                                             function.parameters()[i]->modifiers(),
953                                                             &tempVars,
954                                                             out));
955                 } else {
956                     argumentIds.push_back(this->writeExpression(*arguments[i], out));
957                 }
958             }
959             if (!c.type().isVoid()) {
960                 this->writeOpCode((SpvOp_) intrinsicId, 3 + (int32_t) arguments.size(), out);
961                 this->writeWord(this->getType(c.type()), out);
962                 this->writeWord(result, out);
963             } else {
964                 this->writeOpCode((SpvOp_) intrinsicId, 1 + (int32_t) arguments.size(), out);
965             }
966             for (SpvId id : argumentIds) {
967                 this->writeWord(id, out);
968             }
969             this->copyBackTempVars(tempVars, out);
970             return result;
971         }
972         case kSpecial_IntrinsicOpcodeKind:
973             return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId, out);
974         default:
975             fContext.fErrors->error(c.fLine, "unsupported intrinsic '" + function.description() +
976                                              "'");
977             return -1;
978     }
979 }
980 
vectorize(const Expression & arg,int vectorSize,OutputStream & out)981 SpvId SPIRVCodeGenerator::vectorize(const Expression& arg, int vectorSize, OutputStream& out) {
982     SkASSERT(vectorSize >= 1 && vectorSize <= 4);
983     const Type& argType = arg.type();
984     SpvId raw = this->writeExpression(arg, out);
985     if (argType.isScalar()) {
986         if (vectorSize == 1) {
987             return raw;
988         }
989         SpvId vector = this->nextId(&argType);
990         this->writeOpCode(SpvOpCompositeConstruct, 3 + vectorSize, out);
991         this->writeWord(this->getType(argType.toCompound(fContext, vectorSize, 1)), out);
992         this->writeWord(vector, out);
993         for (int i = 0; i < vectorSize; i++) {
994             this->writeWord(raw, out);
995         }
996         return vector;
997     } else {
998         SkASSERT(vectorSize == argType.columns());
999         return raw;
1000     }
1001 }
1002 
vectorize(const ExpressionArray & args,OutputStream & out)1003 std::vector<SpvId> SPIRVCodeGenerator::vectorize(const ExpressionArray& args, OutputStream& out) {
1004     int vectorSize = 1;
1005     for (const auto& a : args) {
1006         if (a->type().isVector()) {
1007             if (vectorSize > 1) {
1008                 SkASSERT(a->type().columns() == vectorSize);
1009             } else {
1010                 vectorSize = a->type().columns();
1011             }
1012         }
1013     }
1014     std::vector<SpvId> result;
1015     result.reserve(args.size());
1016     for (const auto& arg : args) {
1017         result.push_back(this->vectorize(*arg, vectorSize, out));
1018     }
1019     return result;
1020 }
1021 
writeGLSLExtendedInstruction(const Type & type,SpvId id,SpvId floatInst,SpvId signedInst,SpvId unsignedInst,const std::vector<SpvId> & args,OutputStream & out)1022 void SPIRVCodeGenerator::writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
1023                                                       SpvId signedInst, SpvId unsignedInst,
1024                                                       const std::vector<SpvId>& args,
1025                                                       OutputStream& out) {
1026     this->writeOpCode(SpvOpExtInst, 5 + args.size(), out);
1027     this->writeWord(this->getType(type), out);
1028     this->writeWord(id, out);
1029     this->writeWord(fGLSLExtendedInstructions, out);
1030 
1031     if (is_float(fContext, type)) {
1032         this->writeWord(floatInst, out);
1033     } else if (is_signed(fContext, type)) {
1034         this->writeWord(signedInst, out);
1035     } else if (is_unsigned(fContext, type)) {
1036         this->writeWord(unsignedInst, out);
1037     } else {
1038         SkASSERT(false);
1039     }
1040     for (SpvId a : args) {
1041         this->writeWord(a, out);
1042     }
1043 }
1044 
writeSpecialIntrinsic(const FunctionCall & c,SpecialIntrinsic kind,OutputStream & out)1045 SpvId SPIRVCodeGenerator::writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind,
1046                                                 OutputStream& out) {
1047     const ExpressionArray& arguments = c.arguments();
1048     const Type& callType = c.type();
1049     SpvId result = this->nextId(nullptr);
1050     switch (kind) {
1051         case kAtan_SpecialIntrinsic: {
1052             std::vector<SpvId> argumentIds;
1053             argumentIds.reserve(arguments.size());
1054             for (const std::unique_ptr<Expression>& arg : arguments) {
1055                 argumentIds.push_back(this->writeExpression(*arg, out));
1056             }
1057             this->writeOpCode(SpvOpExtInst, 5 + (int32_t) argumentIds.size(), out);
1058             this->writeWord(this->getType(callType), out);
1059             this->writeWord(result, out);
1060             this->writeWord(fGLSLExtendedInstructions, out);
1061             this->writeWord(argumentIds.size() == 2 ? GLSLstd450Atan2 : GLSLstd450Atan, out);
1062             for (SpvId id : argumentIds) {
1063                 this->writeWord(id, out);
1064             }
1065             break;
1066         }
1067         case kSampledImage_SpecialIntrinsic: {
1068             SkASSERT(arguments.size() == 2);
1069             SpvId img = this->writeExpression(*arguments[0], out);
1070             SpvId sampler = this->writeExpression(*arguments[1], out);
1071             this->writeInstruction(SpvOpSampledImage,
1072                                    this->getType(callType),
1073                                    result,
1074                                    img,
1075                                    sampler,
1076                                    out);
1077             break;
1078         }
1079         case kSubpassLoad_SpecialIntrinsic: {
1080             SpvId img = this->writeExpression(*arguments[0], out);
1081             ExpressionArray args;
1082             args.reserve_back(2);
1083             args.push_back(Literal::MakeInt(fContext, /*line=*/-1, /*value=*/0));
1084             args.push_back(Literal::MakeInt(fContext, /*line=*/-1, /*value=*/0));
1085             ConstructorCompound ctor(/*line=*/-1, *fContext.fTypes.fInt2, std::move(args));
1086             SpvId coords = this->writeConstantVector(ctor);
1087             if (arguments.size() == 1) {
1088                 this->writeInstruction(SpvOpImageRead,
1089                                        this->getType(callType),
1090                                        result,
1091                                        img,
1092                                        coords,
1093                                        out);
1094             } else {
1095                 SkASSERT(arguments.size() == 2);
1096                 SpvId sample = this->writeExpression(*arguments[1], out);
1097                 this->writeInstruction(SpvOpImageRead,
1098                                        this->getType(callType),
1099                                        result,
1100                                        img,
1101                                        coords,
1102                                        SpvImageOperandsSampleMask,
1103                                        sample,
1104                                        out);
1105             }
1106             break;
1107         }
1108         case kTexture_SpecialIntrinsic: {
1109 #ifdef SKSL_EXT
1110             this->writePrecisionDecoration(result, callType);
1111 #endif
1112             SpvOp_ op = SpvOpImageSampleImplicitLod;
1113             const Type& arg1Type = arguments[1]->type();
1114             switch (arguments[0]->type().dimensions()) {
1115                 case SpvDim1D:
1116                     if (arg1Type == *fContext.fTypes.fFloat2) {
1117                         op = SpvOpImageSampleProjImplicitLod;
1118                     } else {
1119                         SkASSERT(arg1Type == *fContext.fTypes.fFloat);
1120                     }
1121                     break;
1122                 case SpvDim2D:
1123                     if (arg1Type == *fContext.fTypes.fFloat3) {
1124                         op = SpvOpImageSampleProjImplicitLod;
1125                     } else {
1126                         SkASSERT(arg1Type == *fContext.fTypes.fFloat2);
1127                     }
1128                     break;
1129                 case SpvDim3D:
1130                     if (arg1Type == *fContext.fTypes.fFloat4) {
1131                         op = SpvOpImageSampleProjImplicitLod;
1132                     } else {
1133                         SkASSERT(arg1Type == *fContext.fTypes.fFloat3);
1134                     }
1135                     break;
1136                 case SpvDimCube:   // fall through
1137                 case SpvDimRect:   // fall through
1138                 case SpvDimBuffer: // fall through
1139                 case SpvDimSubpassData:
1140                     break;
1141             }
1142             SpvId type = this->getType(callType);
1143             SpvId sampler = this->writeExpression(*arguments[0], out);
1144             SpvId uv = this->writeExpression(*arguments[1], out);
1145             if (arguments.size() == 3) {
1146                 this->writeInstruction(op, type, result, sampler, uv,
1147                                        SpvImageOperandsBiasMask,
1148                                        this->writeExpression(*arguments[2], out),
1149                                        out);
1150             } else {
1151                 SkASSERT(arguments.size() == 2);
1152                 if (fProgram.fConfig->fSettings.fSharpenTextures) {
1153                     SpvId lodBias = this->writeLiteral(-0.5, *fContext.fTypes.fFloat);
1154                     this->writeInstruction(op, type, result, sampler, uv,
1155                                            SpvImageOperandsBiasMask, lodBias, out);
1156                 } else {
1157                     this->writeInstruction(op, type, result, sampler, uv,
1158                                            out);
1159                 }
1160             }
1161             break;
1162         }
1163         case kMod_SpecialIntrinsic: {
1164             std::vector<SpvId> args = this->vectorize(arguments, out);
1165             SkASSERT(args.size() == 2);
1166             const Type& operandType = arguments[0]->type();
1167             SpvOp_ op;
1168             if (is_float(fContext, operandType)) {
1169                 op = SpvOpFMod;
1170             } else if (is_signed(fContext, operandType)) {
1171                 op = SpvOpSMod;
1172             } else if (is_unsigned(fContext, operandType)) {
1173                 op = SpvOpUMod;
1174             } else {
1175                 SkASSERT(false);
1176                 return 0;
1177             }
1178             this->writeOpCode(op, 5, out);
1179             this->writeWord(this->getType(operandType), out);
1180             this->writeWord(result, out);
1181             this->writeWord(args[0], out);
1182             this->writeWord(args[1], out);
1183             break;
1184         }
1185         case kDFdy_SpecialIntrinsic: {
1186             SpvId fn = this->writeExpression(*arguments[0], out);
1187             this->writeOpCode(SpvOpDPdy, 4, out);
1188             this->writeWord(this->getType(callType), out);
1189             this->writeWord(result, out);
1190             this->writeWord(fn, out);
1191 #ifdef SKSL_EXT
1192             if (fProgram.fConfig->fSettings.fForceNoRTFlip) {
1193                 break;
1194             }
1195 #endif
1196             this->addRTFlipUniform(c.fLine);
1197             using namespace dsl;
1198             DSLExpression rtFlip(ThreadContext::Compiler().convertIdentifier(/*line=*/-1,
1199                     SKSL_RTFLIP_NAME));
1200             SpvId rtFlipY = this->vectorize(*rtFlip.y().release(), callType.columns(), out);
1201             SpvId flipped = this->nextId(&callType);
1202             this->writeInstruction(SpvOpFMul, this->getType(callType), flipped, result, rtFlipY,
1203                                    out);
1204             result = flipped;
1205             break;
1206         }
1207         case kClamp_SpecialIntrinsic: {
1208 #ifdef SKSL_EXT
1209             this->writePrecisionDecoration(result, callType);
1210 #endif
1211             std::vector<SpvId> args = this->vectorize(arguments, out);
1212             SkASSERT(args.size() == 3);
1213             this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FClamp, GLSLstd450SClamp,
1214                                                GLSLstd450UClamp, args, out);
1215             break;
1216         }
1217         case kMax_SpecialIntrinsic: {
1218             std::vector<SpvId> args = this->vectorize(arguments, out);
1219             SkASSERT(args.size() == 2);
1220             this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMax, GLSLstd450SMax,
1221                                                GLSLstd450UMax, args, out);
1222             break;
1223         }
1224         case kMin_SpecialIntrinsic: {
1225             std::vector<SpvId> args = this->vectorize(arguments, out);
1226             SkASSERT(args.size() == 2);
1227             this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMin, GLSLstd450SMin,
1228                                                GLSLstd450UMin, args, out);
1229             break;
1230         }
1231         case kMix_SpecialIntrinsic: {
1232 #ifdef SKSL_EXT
1233             this->writePrecisionDecoration(result, callType);
1234 #endif
1235             std::vector<SpvId> args = this->vectorize(arguments, out);
1236             SkASSERT(args.size() == 3);
1237             if (arguments[2]->type().componentType().isBoolean()) {
1238                 // Use OpSelect to implement Boolean mix().
1239                 SpvId falseId     = this->writeExpression(*arguments[0], out);
1240                 SpvId trueId      = this->writeExpression(*arguments[1], out);
1241                 SpvId conditionId = this->writeExpression(*arguments[2], out);
1242                 this->writeInstruction(SpvOpSelect, this->getType(arguments[0]->type()), result,
1243                                        conditionId, trueId, falseId, out);
1244             } else {
1245                 this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMix, SpvOpUndef,
1246                                                    SpvOpUndef, args, out);
1247             }
1248             break;
1249         }
1250         case kSaturate_SpecialIntrinsic: {
1251             SkASSERT(arguments.size() == 1);
1252             ExpressionArray finalArgs;
1253             finalArgs.reserve_back(3);
1254             finalArgs.push_back(arguments[0]->clone());
1255             finalArgs.push_back(Literal::MakeFloat(fContext, /*line=*/-1, /*value=*/0));
1256             finalArgs.push_back(Literal::MakeFloat(fContext, /*line=*/-1, /*value=*/1));
1257             std::vector<SpvId> spvArgs = this->vectorize(finalArgs, out);
1258             this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FClamp, GLSLstd450SClamp,
1259                                                GLSLstd450UClamp, spvArgs, out);
1260             break;
1261         }
1262         case kSmoothStep_SpecialIntrinsic: {
1263             std::vector<SpvId> args = this->vectorize(arguments, out);
1264             SkASSERT(args.size() == 3);
1265             this->writeGLSLExtendedInstruction(callType, result, GLSLstd450SmoothStep, SpvOpUndef,
1266                                                SpvOpUndef, args, out);
1267             break;
1268         }
1269         case kStep_SpecialIntrinsic: {
1270             std::vector<SpvId> args = this->vectorize(arguments, out);
1271             SkASSERT(args.size() == 2);
1272             this->writeGLSLExtendedInstruction(callType, result, GLSLstd450Step, SpvOpUndef,
1273                                                SpvOpUndef, args, out);
1274             break;
1275         }
1276         case kMatrixCompMult_SpecialIntrinsic: {
1277             SkASSERT(arguments.size() == 2);
1278             SpvId lhs = this->writeExpression(*arguments[0], out);
1279             SpvId rhs = this->writeExpression(*arguments[1], out);
1280             result = this->writeComponentwiseMatrixBinary(callType, lhs, rhs, SpvOpFMul, out);
1281             break;
1282         }
1283 #ifdef SKSL_EXT
1284         case kTextureSize_SpecialIntrinsic: {
1285             SkASSERT(arguments[0]->type().dimensions() == SpvDim2D);
1286 
1287             fCapabilities |= 1ULL << SpvCapabilityImageQuery;
1288 
1289             SpvId dimsType = this->getType(*fContext.fTypes.fInt2);
1290             SpvId sampledImage = this->writeExpression(*arguments[0], out);
1291             SpvId image = this->nextId(nullptr);
1292             SpvId imageType = this->getType(arguments[0]->type().textureType());
1293             SpvId lod = this->writeExpression(*arguments[1], out);
1294             this->writeInstruction(SpvOpImage, imageType, image, sampledImage, out);
1295             this->writeInstruction(SpvOpImageQuerySizeLod, dimsType, result, image, lod, out);
1296             break;
1297         }
1298         case kNonuniformEXT_SpecialIntrinsic: {
1299             fCapabilitiesExt.insert(SpvCapabilityShaderNonUniform);
1300             SpvId dimsType = this->getType(*fContext.fTypes.fUInt);
1301             this->writeInstruction(SpvOpDecorate, result, SpvDecorationNonUniform, fDecorationBuffer);
1302             SpvId lod = this->writeExpression(*arguments[0], out);
1303             fNonUniformSpvId.insert(result);
1304             this->writeInstruction(SpvOpCopyObject, dimsType, result, lod, out);
1305             break;
1306         }
1307 #endif
1308     }
1309     return result;
1310 }
1311 
writeFunctionCallArgument(const Expression & arg,const Modifiers & paramModifiers,std::vector<TempVar> * tempVars,OutputStream & out)1312 SpvId SPIRVCodeGenerator::writeFunctionCallArgument(const Expression& arg,
1313                                                     const Modifiers& paramModifiers,
1314                                                     std::vector<TempVar>* tempVars,
1315                                                     OutputStream& out) {
1316     // ID of temporary variable that we will use to hold this argument, or 0 if it is being
1317     // passed directly
1318     SpvId tmpVar;
1319     // if we need a temporary var to store this argument, this is the value to store in the var
1320     SpvId tmpValueId = -1;
1321 
1322     if (is_out(paramModifiers)) {
1323         std::unique_ptr<LValue> lv = this->getLValue(arg, out);
1324         SpvId ptr = lv->getPointer();
1325         if (ptr != (SpvId) -1 && lv->isMemoryObjectPointer()) {
1326             return ptr;
1327         }
1328 
1329         // lvalue cannot simply be read and written via a pointer (e.g. it's a swizzle). We need to
1330         // to use a temp variable.
1331         if (is_in(paramModifiers)) {
1332             tmpValueId = lv->load(out);
1333         }
1334         tmpVar = this->nextId(&arg.type());
1335         tempVars->push_back(TempVar{tmpVar, &arg.type(), std::move(lv)});
1336     } else {
1337         // See getFunctionType for an explanation of why we're always using pointer parameters.
1338         tmpValueId = this->writeExpression(arg, out);
1339         tmpVar = this->nextId(nullptr);
1340     }
1341     this->writeInstruction(SpvOpVariable,
1342                            this->getPointerType(arg.type(), SpvStorageClassFunction),
1343                            tmpVar,
1344                            SpvStorageClassFunction,
1345                            fVariableBuffer);
1346     if (tmpValueId != (SpvId)-1) {
1347         this->writeInstruction(SpvOpStore, tmpVar, tmpValueId, out);
1348     }
1349     return tmpVar;
1350 }
1351 
copyBackTempVars(const std::vector<TempVar> & tempVars,OutputStream & out)1352 void SPIRVCodeGenerator::copyBackTempVars(const std::vector<TempVar>& tempVars, OutputStream& out) {
1353     for (const TempVar& tempVar : tempVars) {
1354         SpvId load = this->nextId(tempVar.type);
1355         this->writeInstruction(SpvOpLoad, this->getType(*tempVar.type), load, tempVar.spvId, out);
1356         tempVar.lvalue->store(load, out);
1357     }
1358 }
1359 
writeFunctionCall(const FunctionCall & c,OutputStream & out)1360 SpvId SPIRVCodeGenerator::writeFunctionCall(const FunctionCall& c, OutputStream& out) {
1361     const FunctionDeclaration& function = c.function();
1362     if (function.isIntrinsic() && !function.definition()) {
1363         return this->writeIntrinsicCall(c, out);
1364     }
1365     const ExpressionArray& arguments = c.arguments();
1366     const auto& entry = fFunctionMap.find(&function);
1367     if (entry == fFunctionMap.end()) {
1368         fContext.fErrors->error(c.fLine, "function '" + function.description() +
1369                                          "' is not defined");
1370         return -1;
1371     }
1372     // Temp variables are used to write back out-parameters after the function call is complete.
1373     std::vector<TempVar> tempVars;
1374     std::vector<SpvId> argumentIds;
1375     argumentIds.reserve(arguments.size());
1376     for (size_t i = 0; i < arguments.size(); i++) {
1377         argumentIds.push_back(this->writeFunctionCallArgument(*arguments[i],
1378                                                               function.parameters()[i]->modifiers(),
1379                                                               &tempVars,
1380                                                               out));
1381     }
1382     SpvId result = this->nextId(nullptr);
1383     this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t) arguments.size(), out);
1384     this->writeWord(this->getType(c.type()), out);
1385     this->writeWord(result, out);
1386     this->writeWord(entry->second, out);
1387     for (SpvId id : argumentIds) {
1388         this->writeWord(id, out);
1389     }
1390     // Now that the call is complete, we copy temp out-variables back to their real lvalues.
1391     this->copyBackTempVars(tempVars, out);
1392     return result;
1393 }
1394 
writeConstantVector(const AnyConstructor & c)1395 SpvId SPIRVCodeGenerator::writeConstantVector(const AnyConstructor& c) {
1396     const Type& type = c.type();
1397     SkASSERT(type.isVector() && c.isCompileTimeConstant());
1398 
1399     // Get each of the constructor components as SPIR-V constants.
1400     SPIRVVectorConstant key{this->getType(type),
1401                             /*fValueId=*/{SpvId(-1), SpvId(-1), SpvId(-1), SpvId(-1)}};
1402 
1403     const Type& scalarType = type.componentType();
1404     for (int n = 0; n < type.columns(); n++) {
1405         skstd::optional<double> slotVal = c.getConstantValue(n);
1406         if (!slotVal.has_value()) {
1407             SkDEBUGFAILF("writeConstantVector: %s not actually constant", c.description().c_str());
1408             return (SpvId)-1;
1409         }
1410         key.fValueId[n] = this->writeLiteral(*slotVal, scalarType);
1411     }
1412 
1413     // Check to see if we've already synthesized this vector constant.
1414     auto [iter, newlyCreated] = fVectorConstants.insert({key, (SpvId)-1});
1415     if (newlyCreated) {
1416         // Emit an OpConstantComposite instruction for this constant.
1417         SpvId result = this->nextId(&type);
1418         this->writeOpCode(SpvOpConstantComposite, 3 + type.columns(), fConstantBuffer);
1419         this->writeWord(key.fTypeId, fConstantBuffer);
1420         this->writeWord(result, fConstantBuffer);
1421         for (int i = 0; i < type.columns(); i++) {
1422             this->writeWord(key.fValueId[i], fConstantBuffer);
1423         }
1424         iter->second = result;
1425     }
1426     return iter->second;
1427 }
1428 
castScalarToType(SpvId inputExprId,const Type & inputType,const Type & outputType,OutputStream & out)1429 SpvId SPIRVCodeGenerator::castScalarToType(SpvId inputExprId,
1430                                            const Type& inputType,
1431                                            const Type& outputType,
1432                                            OutputStream& out) {
1433     if (outputType.isFloat()) {
1434         return this->castScalarToFloat(inputExprId, inputType, outputType, out);
1435     }
1436     if (outputType.isSigned()) {
1437         return this->castScalarToSignedInt(inputExprId, inputType, outputType, out);
1438     }
1439     if (outputType.isUnsigned()) {
1440         return this->castScalarToUnsignedInt(inputExprId, inputType, outputType, out);
1441     }
1442     if (outputType.isBoolean()) {
1443         return this->castScalarToBoolean(inputExprId, inputType, outputType, out);
1444     }
1445 
1446     fContext.fErrors->error(-1, "unsupported cast: " + inputType.description() +
1447                                 " to " + outputType.description());
1448     return inputExprId;
1449 }
1450 
writeFloatConstructor(const AnyConstructor & c,OutputStream & out)1451 SpvId SPIRVCodeGenerator::writeFloatConstructor(const AnyConstructor& c, OutputStream& out) {
1452     SkASSERT(c.argumentSpan().size() == 1);
1453     SkASSERT(c.type().isFloat());
1454     const Expression& ctorExpr = *c.argumentSpan().front();
1455     SpvId expressionId = this->writeExpression(ctorExpr, out);
1456     return this->castScalarToFloat(expressionId, ctorExpr.type(), c.type(), out);
1457 }
1458 
castScalarToFloat(SpvId inputId,const Type & inputType,const Type & outputType,OutputStream & out)1459 SpvId SPIRVCodeGenerator::castScalarToFloat(SpvId inputId, const Type& inputType,
1460                                             const Type& outputType, OutputStream& out) {
1461     // Casting a float to float is a no-op.
1462     if (inputType.isFloat()) {
1463         return inputId;
1464     }
1465 
1466     // Given the input type, generate the appropriate instruction to cast to float.
1467     SpvId result = this->nextId(&outputType);
1468     if (inputType.isBoolean()) {
1469         // Use OpSelect to convert the boolean argument to a literal 1.0 or 0.0.
1470         const SpvId oneID = this->writeLiteral(1.0, *fContext.fTypes.fFloat);
1471         const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fFloat);
1472         this->writeInstruction(SpvOpSelect, this->getType(outputType), result,
1473                                inputId, oneID, zeroID, out);
1474     } else if (inputType.isSigned()) {
1475         this->writeInstruction(SpvOpConvertSToF, this->getType(outputType), result, inputId, out);
1476     } else if (inputType.isUnsigned()) {
1477         this->writeInstruction(SpvOpConvertUToF, this->getType(outputType), result, inputId, out);
1478     } else {
1479         SkDEBUGFAILF("unsupported type for float typecast: %s", inputType.description().c_str());
1480         return (SpvId)-1;
1481     }
1482     return result;
1483 }
1484 
writeIntConstructor(const AnyConstructor & c,OutputStream & out)1485 SpvId SPIRVCodeGenerator::writeIntConstructor(const AnyConstructor& c, OutputStream& out) {
1486     SkASSERT(c.argumentSpan().size() == 1);
1487     SkASSERT(c.type().isSigned());
1488     const Expression& ctorExpr = *c.argumentSpan().front();
1489     SpvId expressionId = this->writeExpression(ctorExpr, out);
1490     return this->castScalarToSignedInt(expressionId, ctorExpr.type(), c.type(), out);
1491 }
1492 
castScalarToSignedInt(SpvId inputId,const Type & inputType,const Type & outputType,OutputStream & out)1493 SpvId SPIRVCodeGenerator::castScalarToSignedInt(SpvId inputId, const Type& inputType,
1494                                                 const Type& outputType, OutputStream& out) {
1495     // Casting a signed int to signed int is a no-op.
1496     if (inputType.isSigned()) {
1497         return inputId;
1498     }
1499 
1500     // Given the input type, generate the appropriate instruction to cast to signed int.
1501     SpvId result = this->nextId(&outputType);
1502     if (inputType.isBoolean()) {
1503         // Use OpSelect to convert the boolean argument to a literal 1 or 0.
1504         const SpvId oneID = this->writeLiteral(1.0, *fContext.fTypes.fInt);
1505         const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fInt);
1506         this->writeInstruction(SpvOpSelect, this->getType(outputType), result,
1507                                inputId, oneID, zeroID, out);
1508     } else if (inputType.isFloat()) {
1509         this->writeInstruction(SpvOpConvertFToS, this->getType(outputType), result, inputId, out);
1510     } else if (inputType.isUnsigned()) {
1511         this->writeInstruction(SpvOpBitcast, this->getType(outputType), result, inputId, out);
1512     } else {
1513         SkDEBUGFAILF("unsupported type for signed int typecast: %s",
1514                      inputType.description().c_str());
1515         return (SpvId)-1;
1516     }
1517 #ifdef SKSL_EXT
1518     if (fNonUniformSpvId.find(inputId) != fNonUniformSpvId.end()) {
1519         fNonUniformSpvId.insert(result);
1520         this->writeInstruction(SpvOpDecorate, result, SpvDecorationNonUniform, fDecorationBuffer);
1521     }
1522 #endif
1523     return result;
1524 }
1525 
writeUIntConstructor(const AnyConstructor & c,OutputStream & out)1526 SpvId SPIRVCodeGenerator::writeUIntConstructor(const AnyConstructor& c, OutputStream& out) {
1527     SkASSERT(c.argumentSpan().size() == 1);
1528     SkASSERT(c.type().isUnsigned());
1529     const Expression& ctorExpr = *c.argumentSpan().front();
1530     SpvId expressionId = this->writeExpression(ctorExpr, out);
1531     return this->castScalarToUnsignedInt(expressionId, ctorExpr.type(), c.type(), out);
1532 }
1533 
castScalarToUnsignedInt(SpvId inputId,const Type & inputType,const Type & outputType,OutputStream & out)1534 SpvId SPIRVCodeGenerator::castScalarToUnsignedInt(SpvId inputId, const Type& inputType,
1535                                                   const Type& outputType, OutputStream& out) {
1536     // Casting an unsigned int to unsigned int is a no-op.
1537     if (inputType.isUnsigned()) {
1538         return inputId;
1539     }
1540 
1541     // Given the input type, generate the appropriate instruction to cast to unsigned int.
1542     SpvId result = this->nextId(&outputType);
1543     if (inputType.isBoolean()) {
1544         // Use OpSelect to convert the boolean argument to a literal 1u or 0u.
1545         const SpvId oneID = this->writeLiteral(1.0, *fContext.fTypes.fUInt);
1546         const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fUInt);
1547         this->writeInstruction(SpvOpSelect, this->getType(outputType), result,
1548                                inputId, oneID, zeroID, out);
1549     } else if (inputType.isFloat()) {
1550         this->writeInstruction(SpvOpConvertFToU, this->getType(outputType), result, inputId, out);
1551     } else if (inputType.isSigned()) {
1552         this->writeInstruction(SpvOpBitcast, this->getType(outputType), result, inputId, out);
1553     } else {
1554         SkDEBUGFAILF("unsupported type for unsigned int typecast: %s",
1555                      inputType.description().c_str());
1556         return (SpvId)-1;
1557     }
1558 #ifdef SKSL_EXT
1559     if (fNonUniformSpvId.find(inputId) != fNonUniformSpvId.end()) {
1560         fNonUniformSpvId.insert(result);
1561         this->writeInstruction(SpvOpDecorate, result, SpvDecorationNonUniform, fDecorationBuffer);
1562     }
1563 #endif
1564     return result;
1565 }
1566 
writeBooleanConstructor(const AnyConstructor & c,OutputStream & out)1567 SpvId SPIRVCodeGenerator::writeBooleanConstructor(const AnyConstructor& c, OutputStream& out) {
1568     SkASSERT(c.argumentSpan().size() == 1);
1569     SkASSERT(c.type().isBoolean());
1570     const Expression& ctorExpr = *c.argumentSpan().front();
1571     SpvId expressionId = this->writeExpression(ctorExpr, out);
1572     return this->castScalarToBoolean(expressionId, ctorExpr.type(), c.type(), out);
1573 }
1574 
castScalarToBoolean(SpvId inputId,const Type & inputType,const Type & outputType,OutputStream & out)1575 SpvId SPIRVCodeGenerator::castScalarToBoolean(SpvId inputId, const Type& inputType,
1576                                               const Type& outputType, OutputStream& out) {
1577     // Casting a bool to bool is a no-op.
1578     if (inputType.isBoolean()) {
1579         return inputId;
1580     }
1581 
1582     // Given the input type, generate the appropriate instruction to cast to bool.
1583     SpvId result = this->nextId(nullptr);
1584     if (inputType.isSigned()) {
1585         // Synthesize a boolean result by comparing the input against a signed zero literal.
1586         const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fInt);
1587         this->writeInstruction(SpvOpINotEqual, this->getType(outputType), result,
1588                                inputId, zeroID, out);
1589     } else if (inputType.isUnsigned()) {
1590         // Synthesize a boolean result by comparing the input against an unsigned zero literal.
1591         const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fUInt);
1592         this->writeInstruction(SpvOpINotEqual, this->getType(outputType), result,
1593                                inputId, zeroID, out);
1594     } else if (inputType.isFloat()) {
1595         // Synthesize a boolean result by comparing the input against a floating-point zero literal.
1596         const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fFloat);
1597         this->writeInstruction(SpvOpFUnordNotEqual, this->getType(outputType), result,
1598                                inputId, zeroID, out);
1599     } else {
1600         SkDEBUGFAILF("unsupported type for boolean typecast: %s", inputType.description().c_str());
1601         return (SpvId)-1;
1602     }
1603     return result;
1604 }
1605 
writeUniformScaleMatrix(SpvId id,SpvId diagonal,const Type & type,OutputStream & out)1606 void SPIRVCodeGenerator::writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type,
1607                                                  OutputStream& out) {
1608     SpvId zeroId = this->writeLiteral(0.0, *fContext.fTypes.fFloat);
1609     std::vector<SpvId> columnIds;
1610     columnIds.reserve(type.columns());
1611     for (int column = 0; column < type.columns(); column++) {
1612         this->writeOpCode(SpvOpCompositeConstruct, 3 + type.rows(),
1613                           out);
1614         this->writeWord(this->getType(type.componentType().toCompound(
1615                                 fContext, /*columns=*/type.rows(), /*rows=*/1)),
1616                         out);
1617         SpvId columnId = this->nextId(&type);
1618         this->writeWord(columnId, out);
1619         columnIds.push_back(columnId);
1620         for (int row = 0; row < type.rows(); row++) {
1621             this->writeWord(row == column ? diagonal : zeroId, out);
1622         }
1623     }
1624     this->writeOpCode(SpvOpCompositeConstruct, 3 + type.columns(),
1625                       out);
1626     this->writeWord(this->getType(type), out);
1627     this->writeWord(id, out);
1628     for (SpvId columnId : columnIds) {
1629         this->writeWord(columnId, out);
1630     }
1631 }
1632 
writeMatrixCopy(SpvId src,const Type & srcType,const Type & dstType,OutputStream & out)1633 SpvId SPIRVCodeGenerator::writeMatrixCopy(SpvId src, const Type& srcType, const Type& dstType,
1634                                           OutputStream& out) {
1635     SkASSERT(srcType.isMatrix());
1636     SkASSERT(dstType.isMatrix());
1637     SkASSERT(srcType.componentType() == dstType.componentType());
1638     SpvId id = this->nextId(&dstType);
1639     SpvId srcColumnType = this->getType(srcType.componentType().toCompound(fContext,
1640                                                                            srcType.rows(),
1641                                                                            1));
1642     SpvId dstColumnType = this->getType(dstType.componentType().toCompound(fContext,
1643                                                                            dstType.rows(),
1644                                                                            1));
1645     SkASSERT(dstType.componentType().isFloat());
1646     const SpvId zeroId = this->writeLiteral(0.0, dstType.componentType());
1647     const SpvId oneId = this->writeLiteral(1.0, dstType.componentType());
1648 
1649     SpvId columns[4];
1650     for (int i = 0; i < dstType.columns(); i++) {
1651         if (i < srcType.columns()) {
1652             // we're still inside the src matrix, copy the column
1653             SpvId srcColumn = this->nextId(&dstType);
1654             this->writeInstruction(SpvOpCompositeExtract, srcColumnType, srcColumn, src, i, out);
1655             SpvId dstColumn;
1656             if (srcType.rows() == dstType.rows()) {
1657                 // columns are equal size, don't need to do anything
1658                 dstColumn = srcColumn;
1659             }
1660             else if (dstType.rows() > srcType.rows()) {
1661                 // dst column is bigger, need to zero-pad it
1662                 dstColumn = this->nextId(&dstType);
1663                 int delta = dstType.rows() - srcType.rows();
1664                 this->writeOpCode(SpvOpCompositeConstruct, 4 + delta, out);
1665                 this->writeWord(dstColumnType, out);
1666                 this->writeWord(dstColumn, out);
1667                 this->writeWord(srcColumn, out);
1668                 for (int j = srcType.rows(); j < dstType.rows(); ++j) {
1669                     this->writeWord((i == j) ? oneId : zeroId, out);
1670                 }
1671             }
1672             else {
1673                 // dst column is smaller, need to swizzle the src column
1674                 dstColumn = this->nextId(&dstType);
1675                 this->writeOpCode(SpvOpVectorShuffle, 5 + dstType.rows(), out);
1676                 this->writeWord(dstColumnType, out);
1677                 this->writeWord(dstColumn, out);
1678                 this->writeWord(srcColumn, out);
1679                 this->writeWord(srcColumn, out);
1680                 for (int j = 0; j < dstType.rows(); j++) {
1681                     this->writeWord(j, out);
1682                 }
1683             }
1684             columns[i] = dstColumn;
1685         } else {
1686             // we're past the end of the src matrix, need to synthesize an identity-matrix column
1687             SpvId identityColumn = this->nextId(&dstType);
1688             this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.rows(), out);
1689             this->writeWord(dstColumnType, out);
1690             this->writeWord(identityColumn, out);
1691             for (int j = 0; j < dstType.rows(); ++j) {
1692                 this->writeWord((i == j) ? oneId : zeroId, out);
1693             }
1694             columns[i] = identityColumn;
1695         }
1696     }
1697     this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.columns(), out);
1698     this->writeWord(this->getType(dstType), out);
1699     this->writeWord(id, out);
1700     for (int i = 0; i < dstType.columns(); i++) {
1701         this->writeWord(columns[i], out);
1702     }
1703     return id;
1704 }
1705 
addColumnEntry(const Type & columnType,std::vector<SpvId> * currentColumn,std::vector<SpvId> * columnIds,int rows,SpvId entry,OutputStream & out)1706 void SPIRVCodeGenerator::addColumnEntry(const Type& columnType,
1707                                         std::vector<SpvId>* currentColumn,
1708                                         std::vector<SpvId>* columnIds,
1709                                         int rows,
1710                                         SpvId entry,
1711                                         OutputStream& out) {
1712     SkASSERT((int)currentColumn->size() < rows);
1713     currentColumn->push_back(entry);
1714     if ((int)currentColumn->size() == rows) {
1715         // Synthesize this column into a vector.
1716         SpvId columnId = this->writeComposite(*currentColumn, columnType, out);
1717         columnIds->push_back(columnId);
1718         currentColumn->clear();
1719     }
1720 }
1721 
writeMatrixConstructor(const ConstructorCompound & c,OutputStream & out)1722 SpvId SPIRVCodeGenerator::writeMatrixConstructor(const ConstructorCompound& c, OutputStream& out) {
1723     const Type& type = c.type();
1724     SkASSERT(type.isMatrix());
1725     SkASSERT(!c.arguments().empty());
1726     const Type& arg0Type = c.arguments()[0]->type();
1727     // go ahead and write the arguments so we don't try to write new instructions in the middle of
1728     // an instruction
1729     std::vector<SpvId> arguments;
1730     arguments.reserve(c.arguments().size());
1731     for (const std::unique_ptr<Expression>& arg : c.arguments()) {
1732         arguments.push_back(this->writeExpression(*arg, out));
1733     }
1734 
1735     if (arguments.size() == 1 && arg0Type.isVector()) {
1736         // Special-case handling of float4 -> mat2x2.
1737         SkASSERT(type.rows() == 2 && type.columns() == 2);
1738         SkASSERT(arg0Type.columns() == 4);
1739         SpvId componentType = this->getType(type.componentType());
1740         SpvId v[4];
1741         for (int i = 0; i < 4; ++i) {
1742             v[i] = this->nextId(&type);
1743             this->writeInstruction(SpvOpCompositeExtract, componentType, v[i], arguments[0], i,
1744                                    out);
1745         }
1746         const Type& vecType = type.componentType().toCompound(fContext, /*columns=*/2, /*rows=*/1);
1747         SpvId v0v1 = this->writeComposite({v[0], v[1]}, vecType, out);
1748         SpvId v2v3 = this->writeComposite({v[2], v[3]}, vecType, out);
1749         return this->writeComposite({v0v1, v2v3}, type, out);
1750     }
1751 
1752     int rows = type.rows();
1753     const Type& columnType = type.componentType().toCompound(fContext,
1754                                                              /*columns=*/rows, /*rows=*/1);
1755     // SpvIds of completed columns of the matrix.
1756     std::vector<SpvId> columnIds;
1757     // SpvIds of scalars we have written to the current column so far.
1758     std::vector<SpvId> currentColumn;
1759     for (size_t i = 0; i < arguments.size(); i++) {
1760         const Type& argType = c.arguments()[i]->type();
1761         if (currentColumn.empty() && argType.isVector() && argType.columns() == rows) {
1762             // This vector is a complete matrix column by itself and can be used as-is.
1763             columnIds.push_back(arguments[i]);
1764         } else if (argType.columns() == 1) {
1765             // This argument is a lone scalar and can be added to the current column as-is.
1766             this->addColumnEntry(columnType, &currentColumn, &columnIds, rows, arguments[i], out);
1767         } else {
1768             // This argument needs to be decomposed into its constituent scalars.
1769             SpvId componentType = this->getType(argType.componentType());
1770             for (int j = 0; j < argType.columns(); ++j) {
1771                 SpvId swizzle = this->nextId(&argType);
1772                 this->writeInstruction(SpvOpCompositeExtract, componentType, swizzle,
1773                                        arguments[i], j, out);
1774                 this->addColumnEntry(columnType, &currentColumn, &columnIds, rows, swizzle, out);
1775             }
1776         }
1777     }
1778     SkASSERT(columnIds.size() == (size_t) type.columns());
1779     return this->writeComposite(columnIds, type, out);
1780 }
1781 
writeConstructorCompound(const ConstructorCompound & c,OutputStream & out)1782 SpvId SPIRVCodeGenerator::writeConstructorCompound(const ConstructorCompound& c,
1783                                                    OutputStream& out) {
1784     return c.type().isMatrix() ? this->writeMatrixConstructor(c, out)
1785                                : this->writeVectorConstructor(c, out);
1786 }
1787 
writeVectorConstructor(const ConstructorCompound & c,OutputStream & out)1788 SpvId SPIRVCodeGenerator::writeVectorConstructor(const ConstructorCompound& c, OutputStream& out) {
1789     const Type& type = c.type();
1790     const Type& componentType = type.componentType();
1791     SkASSERT(type.isVector());
1792 
1793     if (c.isCompileTimeConstant()) {
1794         return this->writeConstantVector(c);
1795     }
1796 
1797     std::vector<SpvId> arguments;
1798     arguments.reserve(c.arguments().size());
1799     for (size_t i = 0; i < c.arguments().size(); i++) {
1800         const Type& argType = c.arguments()[i]->type();
1801         SkASSERT(componentType == argType.componentType());
1802 
1803         SpvId arg = this->writeExpression(*c.arguments()[i], out);
1804         if (argType.isMatrix()) {
1805             // CompositeConstruct cannot take a 2x2 matrix as an input, so we need to extract out
1806             // each scalar separately.
1807             SkASSERT(argType.rows() == 2);
1808             SkASSERT(argType.columns() == 2);
1809             for (int j = 0; j < 4; ++j) {
1810                 SpvId componentId = this->nextId(&componentType);
1811                 this->writeInstruction(SpvOpCompositeExtract, this->getType(componentType),
1812                                        componentId, arg, j / 2, j % 2, out);
1813                 arguments.push_back(componentId);
1814             }
1815         } else if (argType.isVector()) {
1816             // There's a bug in the Intel Vulkan driver where OpCompositeConstruct doesn't handle
1817             // vector arguments at all, so we always extract each vector component and pass them
1818             // into OpCompositeConstruct individually.
1819             for (int j = 0; j < argType.columns(); j++) {
1820                 SpvId componentId = this->nextId(&componentType);
1821                 this->writeInstruction(SpvOpCompositeExtract, this->getType(componentType),
1822                                        componentId, arg, j, out);
1823                 arguments.push_back(componentId);
1824             }
1825         } else {
1826             arguments.push_back(arg);
1827         }
1828     }
1829 
1830     return this->writeComposite(arguments, type, out);
1831 }
1832 
writeComposite(const std::vector<SpvId> & arguments,const Type & type,OutputStream & out)1833 SpvId SPIRVCodeGenerator::writeComposite(const std::vector<SpvId>& arguments,
1834                                          const Type& type,
1835                                          OutputStream& out) {
1836     SkASSERT(arguments.size() == (type.isStruct() ? type.fields().size() : (size_t)type.columns()));
1837 
1838     SpvId result = this->nextId(&type);
1839 #ifdef SKSL_EXT
1840     this->writeOpCode(fEmittingGlobalConstConstructor ? SpvOpConstantComposite : SpvOpCompositeConstruct,
1841         3 + (int32_t) arguments.size(), out);
1842 #else
1843     this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) arguments.size(), out);
1844 #endif
1845     this->writeWord(this->getType(type), out);
1846     this->writeWord(result, out);
1847     for (SpvId id : arguments) {
1848         this->writeWord(id, out);
1849     }
1850     return result;
1851 }
1852 
writeConstructorSplat(const ConstructorSplat & c,OutputStream & out)1853 SpvId SPIRVCodeGenerator::writeConstructorSplat(const ConstructorSplat& c, OutputStream& out) {
1854     // Use writeConstantVector to deduplicate constant splats.
1855     if (c.isCompileTimeConstant()) {
1856         return this->writeConstantVector(c);
1857     }
1858 
1859     // Write the splat argument.
1860     SpvId argument = this->writeExpression(*c.argument(), out);
1861 
1862     // Generate a OpCompositeConstruct which repeats the argument N times.
1863     std::vector<SpvId> arguments(/*count*/ c.type().columns(), /*value*/ argument);
1864     return this->writeComposite(arguments, c.type(), out);
1865 }
1866 
1867 
writeCompositeConstructor(const AnyConstructor & c,OutputStream & out)1868 SpvId SPIRVCodeGenerator::writeCompositeConstructor(const AnyConstructor& c, OutputStream& out) {
1869     SkASSERT(c.type().isArray() || c.type().isStruct());
1870     auto ctorArgs = c.argumentSpan();
1871 
1872     std::vector<SpvId> arguments;
1873     arguments.reserve(ctorArgs.size());
1874     for (const std::unique_ptr<Expression>& arg : ctorArgs) {
1875         arguments.push_back(this->writeExpression(*arg, out));
1876     }
1877 
1878     return this->writeComposite(arguments, c.type(), out);
1879 }
1880 
writeConstructorScalarCast(const ConstructorScalarCast & c,OutputStream & out)1881 SpvId SPIRVCodeGenerator::writeConstructorScalarCast(const ConstructorScalarCast& c,
1882                                                      OutputStream& out) {
1883     const Type& type = c.type();
1884     if (this->getActualType(type) == this->getActualType(c.argument()->type())) {
1885         return this->writeExpression(*c.argument(), out);
1886     }
1887 
1888     const Expression& ctorExpr = *c.argument();
1889     SpvId expressionId = this->writeExpression(ctorExpr, out);
1890     return this->castScalarToType(expressionId, ctorExpr.type(), type, out);
1891 }
1892 
writeConstructorCompoundCast(const ConstructorCompoundCast & c,OutputStream & out)1893 SpvId SPIRVCodeGenerator::writeConstructorCompoundCast(const ConstructorCompoundCast& c,
1894                                                        OutputStream& out) {
1895     const Type& ctorType = c.type();
1896     const Type& argType = c.argument()->type();
1897     SkASSERT(ctorType.isVector() || ctorType.isMatrix());
1898 
1899     // Write the composite that we are casting. If the actual type matches, we are done.
1900     SpvId compositeId = this->writeExpression(*c.argument(), out);
1901     if (this->getActualType(ctorType) == this->getActualType(argType)) {
1902         return compositeId;
1903     }
1904 
1905     // writeMatrixCopy can cast matrices to a different type.
1906     if (ctorType.isMatrix()) {
1907         return this->writeMatrixCopy(compositeId, argType, ctorType, out);
1908     }
1909 
1910     // SPIR-V doesn't support vector(vector-of-different-type) directly, so we need to extract the
1911     // components and convert each one manually.
1912     const Type& srcType = argType.componentType();
1913     const Type& dstType = ctorType.componentType();
1914 
1915     std::vector<SpvId> arguments;
1916     arguments.reserve(argType.columns());
1917     for (int index = 0; index < argType.columns(); ++index) {
1918         SpvId componentId = this->nextId(&srcType);
1919         this->writeInstruction(SpvOpCompositeExtract, this->getType(srcType), componentId,
1920                                compositeId, index, out);
1921         arguments.push_back(this->castScalarToType(componentId, srcType, dstType, out));
1922     }
1923 
1924     return this->writeComposite(arguments, ctorType, out);
1925 }
1926 
writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix & c,OutputStream & out)1927 SpvId SPIRVCodeGenerator::writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c,
1928                                                          OutputStream& out) {
1929     const Type& type = c.type();
1930     SkASSERT(type.isMatrix());
1931     SkASSERT(c.argument()->type().isScalar());
1932 
1933     // Write out the scalar argument.
1934     SpvId argument = this->writeExpression(*c.argument(), out);
1935 
1936     // Build the diagonal matrix.
1937     SpvId result = this->nextId(&type);
1938     this->writeUniformScaleMatrix(result, argument, type, out);
1939     return result;
1940 }
1941 
writeConstructorMatrixResize(const ConstructorMatrixResize & c,OutputStream & out)1942 SpvId SPIRVCodeGenerator::writeConstructorMatrixResize(const ConstructorMatrixResize& c,
1943                                                        OutputStream& out) {
1944     // Write the input matrix.
1945     SpvId argument = this->writeExpression(*c.argument(), out);
1946 
1947     // Use matrix-copy to resize the input matrix to its new size.
1948     return this->writeMatrixCopy(argument, c.argument()->type(), c.type(), out);
1949 }
1950 
get_storage_class(const Variable & var,SpvStorageClass_ fallbackStorageClass)1951 static SpvStorageClass_ get_storage_class(const Variable& var,
1952                                           SpvStorageClass_ fallbackStorageClass) {
1953     const Modifiers& modifiers = var.modifiers();
1954 #ifdef SKSL_EXT
1955     if (var.type().typeKind() == Type::TypeKind::kSampler ||
1956         var.type().typeKind() == Type::TypeKind::kSeparateSampler ||
1957         var.type().typeKind() == Type::TypeKind::kTexture ||
1958         (var.type().typeKind() == Type::TypeKind::kArray &&
1959             var.type().componentType().typeKind() == Type::TypeKind::kSampler)) {
1960         return SpvStorageClassUniformConstant;
1961     }
1962     if (modifiers.fFlags & Modifiers::kBuffer_Flag) {
1963         return SpvStorageClassStorageBuffer;
1964     }
1965     if (!(modifiers.fFlags & Modifiers::kUniform_Flag) &&
1966         var.storage() == Variable::Storage::kGlobal &&
1967         modifiers.fFlags & Modifiers::kConst_Flag) {
1968         return SpvStorageClassFunction;
1969     }
1970 #endif
1971     if (modifiers.fFlags & Modifiers::kIn_Flag) {
1972         SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag));
1973         return SpvStorageClassInput;
1974     }
1975     if (modifiers.fFlags & Modifiers::kOut_Flag) {
1976         SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag));
1977         return SpvStorageClassOutput;
1978     }
1979     if (modifiers.fFlags & Modifiers::kUniform_Flag) {
1980         if (modifiers.fLayout.fFlags & Layout::kPushConstant_Flag) {
1981             return SpvStorageClassPushConstant;
1982         }
1983         if (var.type().typeKind() == Type::TypeKind::kSampler ||
1984             var.type().typeKind() == Type::TypeKind::kSeparateSampler ||
1985             var.type().typeKind() == Type::TypeKind::kTexture) {
1986             return SpvStorageClassUniformConstant;
1987         }
1988         return SpvStorageClassUniform;
1989     }
1990     return fallbackStorageClass;
1991 }
1992 
get_storage_class(const Expression & expr)1993 static SpvStorageClass_ get_storage_class(const Expression& expr) {
1994     switch (expr.kind()) {
1995         case Expression::Kind::kVariableReference: {
1996             const Variable& var = *expr.as<VariableReference>().variable();
1997             if (var.storage() != Variable::Storage::kGlobal) {
1998                 return SpvStorageClassFunction;
1999             }
2000             return get_storage_class(var, SpvStorageClassPrivate);
2001         }
2002         case Expression::Kind::kFieldAccess:
2003             return get_storage_class(*expr.as<FieldAccess>().base());
2004         case Expression::Kind::kIndex:
2005             return get_storage_class(*expr.as<IndexExpression>().base());
2006         default:
2007             return SpvStorageClassFunction;
2008     }
2009 }
2010 
getAccessChain(const Expression & expr,OutputStream & out)2011 std::vector<SpvId> SPIRVCodeGenerator::getAccessChain(const Expression& expr, OutputStream& out) {
2012     std::vector<SpvId> chain;
2013     switch (expr.kind()) {
2014         case Expression::Kind::kIndex: {
2015             const IndexExpression& indexExpr = expr.as<IndexExpression>();
2016             chain = this->getAccessChain(*indexExpr.base(), out);
2017             chain.push_back(this->writeExpression(*indexExpr.index(), out));
2018             break;
2019         }
2020         case Expression::Kind::kFieldAccess: {
2021             const FieldAccess& fieldExpr = expr.as<FieldAccess>();
2022             chain = this->getAccessChain(*fieldExpr.base(), out);
2023             chain.push_back(this->writeLiteral(fieldExpr.fieldIndex(), *fContext.fTypes.fInt));
2024             break;
2025         }
2026         default: {
2027             SpvId id = this->getLValue(expr, out)->getPointer();
2028             SkASSERT(id != (SpvId) -1);
2029             chain.push_back(id);
2030             break;
2031         }
2032     }
2033     return chain;
2034 }
2035 
2036 class PointerLValue : public SPIRVCodeGenerator::LValue {
2037 public:
PointerLValue(SPIRVCodeGenerator & gen,SpvId pointer,bool isMemoryObject,SpvId type,SPIRVCodeGenerator::Precision precision)2038     PointerLValue(SPIRVCodeGenerator& gen, SpvId pointer, bool isMemoryObject, SpvId type,
2039                   SPIRVCodeGenerator::Precision precision)
2040     : fGen(gen)
2041     , fPointer(pointer)
2042     , fIsMemoryObject(isMemoryObject)
2043     , fType(type)
2044     , fPrecision(precision) {}
2045 
getPointer()2046     SpvId getPointer() override {
2047         return fPointer;
2048     }
2049 
isMemoryObjectPointer() const2050     bool isMemoryObjectPointer() const override {
2051         return fIsMemoryObject;
2052     }
2053 
load(OutputStream & out)2054     SpvId load(OutputStream& out) override {
2055 #ifdef SKSL_EXT
2056         return fGen.writeOpLoad(fType, fPrecision, fPointer, out);
2057 #endif
2058         SpvId result = fGen.nextId(fPrecision);
2059         fGen.writeInstruction(SpvOpLoad, fType, result, fPointer, out);
2060         return result;
2061     }
2062 
store(SpvId value,OutputStream & out)2063     void store(SpvId value, OutputStream& out) override {
2064         fGen.writeInstruction(SpvOpStore, fPointer, value, out);
2065     }
2066 
2067 private:
2068     SPIRVCodeGenerator& fGen;
2069     const SpvId fPointer;
2070     const bool fIsMemoryObject;
2071     const SpvId fType;
2072     const SPIRVCodeGenerator::Precision fPrecision;
2073 };
2074 
2075 class SwizzleLValue : public SPIRVCodeGenerator::LValue {
2076 public:
SwizzleLValue(SPIRVCodeGenerator & gen,SpvId vecPointer,const ComponentArray & components,const Type & baseType,const Type & swizzleType)2077     SwizzleLValue(SPIRVCodeGenerator& gen, SpvId vecPointer, const ComponentArray& components,
2078                   const Type& baseType, const Type& swizzleType)
2079     : fGen(gen)
2080     , fVecPointer(vecPointer)
2081     , fComponents(components)
2082     , fBaseType(&baseType)
2083     , fSwizzleType(&swizzleType) {}
2084 
applySwizzle(const ComponentArray & components,const Type & newType)2085     bool applySwizzle(const ComponentArray& components, const Type& newType) override {
2086         ComponentArray updatedSwizzle;
2087         for (int8_t component : components) {
2088             if (component < 0 || component >= fComponents.count()) {
2089                 SkDEBUGFAILF("swizzle accessed nonexistent component %d", (int)component);
2090                 return false;
2091             }
2092             updatedSwizzle.push_back(fComponents[component]);
2093         }
2094         fComponents = updatedSwizzle;
2095         fSwizzleType = &newType;
2096         return true;
2097     }
2098 
load(OutputStream & out)2099     SpvId load(OutputStream& out) override {
2100         SpvId base = fGen.nextId(fBaseType);
2101         fGen.writeInstruction(SpvOpLoad, fGen.getType(*fBaseType), base, fVecPointer, out);
2102         SpvId result = fGen.nextId(fBaseType);
2103         fGen.writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) fComponents.size(), out);
2104         fGen.writeWord(fGen.getType(*fSwizzleType), out);
2105         fGen.writeWord(result, out);
2106         fGen.writeWord(base, out);
2107         fGen.writeWord(base, out);
2108         for (int component : fComponents) {
2109             fGen.writeWord(component, out);
2110         }
2111         return result;
2112     }
2113 
store(SpvId value,OutputStream & out)2114     void store(SpvId value, OutputStream& out) override {
2115         // use OpVectorShuffle to mix and match the vector components. We effectively create
2116         // a virtual vector out of the concatenation of the left and right vectors, and then
2117         // select components from this virtual vector to make the result vector. For
2118         // instance, given:
2119         // float3L = ...;
2120         // float3R = ...;
2121         // L.xz = R.xy;
2122         // we end up with the virtual vector (L.x, L.y, L.z, R.x, R.y, R.z). Then we want
2123         // our result vector to look like (R.x, L.y, R.y), so we need to select indices
2124         // (3, 1, 4).
2125         SpvId base = fGen.nextId(fBaseType);
2126         fGen.writeInstruction(SpvOpLoad, fGen.getType(*fBaseType), base, fVecPointer, out);
2127         SpvId shuffle = fGen.nextId(fBaseType);
2128         fGen.writeOpCode(SpvOpVectorShuffle, 5 + fBaseType->columns(), out);
2129         fGen.writeWord(fGen.getType(*fBaseType), out);
2130         fGen.writeWord(shuffle, out);
2131         fGen.writeWord(base, out);
2132         fGen.writeWord(value, out);
2133         for (int i = 0; i < fBaseType->columns(); i++) {
2134             // current offset into the virtual vector, defaults to pulling the unmodified
2135             // value from the left side
2136             int offset = i;
2137             // check to see if we are writing this component
2138             for (size_t j = 0; j < fComponents.size(); j++) {
2139                 if (fComponents[j] == i) {
2140                     // we're writing to this component, so adjust the offset to pull from
2141                     // the correct component of the right side instead of preserving the
2142                     // value from the left
2143                     offset = (int) (j + fBaseType->columns());
2144                     break;
2145                 }
2146             }
2147             fGen.writeWord(offset, out);
2148         }
2149         fGen.writeInstruction(SpvOpStore, fVecPointer, shuffle, out);
2150     }
2151 
2152 private:
2153     SPIRVCodeGenerator& fGen;
2154     const SpvId fVecPointer;
2155     ComponentArray fComponents;
2156     const Type* fBaseType;
2157     const Type* fSwizzleType;
2158 };
2159 
findUniformFieldIndex(const Variable & var) const2160 int SPIRVCodeGenerator::findUniformFieldIndex(const Variable& var) const {
2161     auto iter = fTopLevelUniformMap.find(&var);
2162     return (iter != fTopLevelUniformMap.end()) ? iter->second : -1;
2163 }
2164 
getLValue(const Expression & expr,OutputStream & out)2165 std::unique_ptr<SPIRVCodeGenerator::LValue> SPIRVCodeGenerator::getLValue(const Expression& expr,
2166                                                                           OutputStream& out) {
2167     const Type& type = expr.type();
2168     Precision precision = type.highPrecision() ? Precision::kDefault : Precision::kRelaxed;
2169     switch (expr.kind()) {
2170         case Expression::Kind::kVariableReference: {
2171             const Variable& var = *expr.as<VariableReference>().variable();
2172             int uniformIdx = this->findUniformFieldIndex(var);
2173             if (uniformIdx >= 0) {
2174                 SpvId memberId = this->nextId(nullptr);
2175                 SpvId typeId = this->getPointerType(type, SpvStorageClassUniform);
2176                 SpvId uniformIdxId = this->writeLiteral((double)uniformIdx, *fContext.fTypes.fInt);
2177                 this->writeInstruction(SpvOpAccessChain, typeId, memberId, fUniformBufferId,
2178                                        uniformIdxId, out);
2179                 return std::make_unique<PointerLValue>(*this, memberId,
2180                                                        /*isMemoryObjectPointer=*/true,
2181                                                        this->getType(type), precision);
2182             }
2183 #ifdef SKSL_EXT
2184             if (fGlobalConstVariableValueMap.find(&var) != fGlobalConstVariableValueMap.end()) {
2185                 SpvId id = this->nextId(&type);
2186                 fVariableMap[&var] = id;
2187                 SpvId typeId = this->getPointerType(type, SpvStorageClassFunction);
2188                 this->writeInstruction(SpvOpVariable, typeId, id, SpvStorageClassFunction, fVariableBuffer);
2189                 this->writeInstruction(SpvOpName, id, var.name(), fNameBuffer);
2190                 this->writeInstruction(SpvOpStore, id, fGlobalConstVariableValueMap[&var], out);
2191             }
2192 #endif
2193             SpvId typeId = this->getType(type, this->memoryLayoutForVariable(var));
2194             auto entry = fVariableMap.find(&var);
2195             SkASSERTF(entry != fVariableMap.end(), "%s", expr.description().c_str());
2196             return std::make_unique<PointerLValue>(*this, entry->second,
2197                                                    /*isMemoryObjectPointer=*/true,
2198                                                    typeId, precision);
2199         }
2200         case Expression::Kind::kIndex: // fall through
2201         case Expression::Kind::kFieldAccess: {
2202             std::vector<SpvId> chain = this->getAccessChain(expr, out);
2203             SpvId member = this->nextId(nullptr);
2204             this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out);
2205             this->writeWord(this->getPointerType(type, get_storage_class(expr)), out);
2206             this->writeWord(member, out);
2207 #ifdef SKSL_EXT
2208             bool needDecorate = false;
2209             for (SpvId idx : chain) {
2210                 this->writeWord(idx, out);
2211                 needDecorate |= fNonUniformSpvId.find(idx) != fNonUniformSpvId.end();
2212             }
2213             if (needDecorate) {
2214                 fNonUniformSpvId.insert(member);
2215                 this->writeInstruction(SpvOpDecorate, member, SpvDecorationNonUniform, fDecorationBuffer);
2216             }
2217 #else
2218             for (SpvId idx : chain) {
2219                 this->writeWord(idx, out);
2220             }
2221 #endif
2222             return std::make_unique<PointerLValue>(*this, member, /*isMemoryObjectPointer=*/false,
2223                                                    this->getType(type), precision);
2224         }
2225         case Expression::Kind::kSwizzle: {
2226             const Swizzle& swizzle = expr.as<Swizzle>();
2227             std::unique_ptr<LValue> lvalue = this->getLValue(*swizzle.base(), out);
2228             if (lvalue->applySwizzle(swizzle.components(), type)) {
2229                 return lvalue;
2230             }
2231             SpvId base = lvalue->getPointer();
2232             if (base == (SpvId) -1) {
2233                 fContext.fErrors->error(swizzle.fLine, "unable to retrieve lvalue from swizzle");
2234             }
2235             if (swizzle.components().size() == 1) {
2236                 SpvId member = this->nextId(nullptr);
2237                 SpvId typeId = this->getPointerType(type, get_storage_class(*swizzle.base()));
2238                 SpvId indexId = this->writeLiteral(swizzle.components()[0], *fContext.fTypes.fInt);
2239                 this->writeInstruction(SpvOpAccessChain, typeId, member, base, indexId, out);
2240                 return std::make_unique<PointerLValue>(*this,
2241                                                        member,
2242                                                        /*isMemoryObjectPointer=*/false,
2243                                                        this->getType(type),
2244                                                        precision);
2245             } else {
2246                 return std::make_unique<SwizzleLValue>(*this, base, swizzle.components(),
2247                                                        swizzle.base()->type(), type);
2248             }
2249         }
2250         default: {
2251             // expr isn't actually an lvalue, create a placeholder variable for it. This case
2252             // happens due to the need to store values in temporary variables during function
2253             // calls (see comments in getFunctionType); erroneous uses of rvalues as lvalues
2254             // should have been caught before code generation
2255             SpvId result = this->nextId(nullptr);
2256             SpvId pointerType = this->getPointerType(type, SpvStorageClassFunction);
2257             this->writeInstruction(SpvOpVariable, pointerType, result, SpvStorageClassFunction,
2258                                    fVariableBuffer);
2259             this->writeInstruction(SpvOpStore, result, this->writeExpression(expr, out), out);
2260             return std::make_unique<PointerLValue>(*this, result, /*isMemoryObjectPointer=*/true,
2261                                                    this->getType(type), precision);
2262         }
2263     }
2264 }
2265 
writeVariableReference(const VariableReference & ref,OutputStream & out)2266 SpvId SPIRVCodeGenerator::writeVariableReference(const VariableReference& ref, OutputStream& out) {
2267     const Variable* variable = ref.variable();
2268     if (variable->modifiers().fLayout.fBuiltin == DEVICE_FRAGCOORDS_BUILTIN) {
2269         // Down below, we rewrite raw references to sk_FragCoord with expressions that reference
2270         // DEVICE_FRAGCOORDS_BUILTIN. This is a fake variable that means we need to directly access
2271         // the fragcoord; do so now.
2272         dsl::DSLGlobalVar fragCoord("sk_FragCoord");
2273         return this->getLValue(*dsl::DSLExpression(fragCoord).release(), out)->load(out);
2274     }
2275     if (variable->modifiers().fLayout.fBuiltin == DEVICE_CLOCKWISE_BUILTIN) {
2276         // Down below, we rewrite raw references to sk_Clockwise with expressions that reference
2277         // DEVICE_CLOCKWISE_BUILTIN. This is a fake variable that means we need to directly
2278         // access front facing; do so now.
2279         dsl::DSLGlobalVar clockwise("sk_Clockwise");
2280         return this->getLValue(*dsl::DSLExpression(clockwise).release(), out)->load(out);
2281     }
2282 
2283     // Handle inserting use of uniform to flip y when referencing sk_FragCoord.
2284     if (variable->modifiers().fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN) {
2285 #ifdef SKSL_EXT
2286         if (fProgram.fConfig->fSettings.fForceNoRTFlip) {
2287             const Symbol* symbol = (*ThreadContext::SymbolTable())["sk_FragCoord"];
2288             const Variable& var = symbol->as<Variable>();
2289             auto varRef = VariableReference::Make(-1, &var);
2290             return this->getLValue(*varRef, out)->load(out);
2291         }
2292 #endif
2293         this->addRTFlipUniform(ref.fLine);
2294         // Use sk_RTAdjust to compute the flipped coordinate
2295         using namespace dsl;
2296         const char* DEVICE_COORDS_NAME = "__device_FragCoords";
2297         SymbolTable& symbols = *ThreadContext::SymbolTable();
2298         // Use a uniform to flip the Y coordinate. The new expression will be written in
2299         // terms of __device_FragCoords, which is a fake variable that means "access the
2300         // underlying fragcoords directly without flipping it".
2301         DSLExpression rtFlip(ThreadContext::Compiler().convertIdentifier(/*line=*/-1,
2302                 SKSL_RTFLIP_NAME));
2303         if (!symbols[DEVICE_COORDS_NAME]) {
2304             AutoAttachPoolToThread attach(fProgram.fPool.get());
2305             Modifiers modifiers;
2306             modifiers.fLayout.fBuiltin = DEVICE_FRAGCOORDS_BUILTIN;
2307             auto coordsVar = std::make_unique<Variable>(/*line=*/-1,
2308                                                         fContext.fModifiersPool->add(modifiers),
2309                                                         DEVICE_COORDS_NAME,
2310                                                         fContext.fTypes.fFloat4.get(),
2311                                                         true,
2312                                                         Variable::Storage::kGlobal);
2313             fSPIRVBonusVariables.insert(coordsVar.get());
2314             symbols.add(std::move(coordsVar));
2315         }
2316         DSLGlobalVar deviceCoord(DEVICE_COORDS_NAME);
2317         std::unique_ptr<Expression> rtFlipSkSLExpr = rtFlip.release();
2318         DSLExpression x = DSLExpression(rtFlipSkSLExpr->clone()).x();
2319         DSLExpression y = DSLExpression(std::move(rtFlipSkSLExpr)).y();
2320         return this->writeExpression(*dsl::Float4(deviceCoord.x(),
2321                                                   std::move(x) + std::move(y) * deviceCoord.y(),
2322                                                   deviceCoord.z(),
2323                                                   deviceCoord.w()).release(),
2324                                      out);
2325     }
2326 
2327     // Handle flipping sk_Clockwise.
2328     if (variable->modifiers().fLayout.fBuiltin == SK_CLOCKWISE_BUILTIN) {
2329 #ifdef SKSL_EXT
2330         if (fProgram.fConfig->fSettings.fForceNoRTFlip) {
2331             const Symbol* symbol = (*ThreadContext::SymbolTable())["sk_Clockwise"];
2332             const Variable& var = symbol->as<Variable>();
2333             auto varRef = VariableReference::Make(-1, &var);
2334             return this->getLValue(*varRef, out)->load(out);
2335         }
2336 #endif
2337         this->addRTFlipUniform(ref.fLine);
2338         using namespace dsl;
2339         const char* DEVICE_CLOCKWISE_NAME = "__device_Clockwise";
2340         SymbolTable& symbols = *ThreadContext::SymbolTable();
2341         // Use a uniform to flip the Y coordinate. The new expression will be written in
2342         // terms of __device_Clockwise, which is a fake variable that means "access the
2343         // underlying FrontFacing directly".
2344         DSLExpression rtFlip(ThreadContext::Compiler().convertIdentifier(/*line=*/-1,
2345                 SKSL_RTFLIP_NAME));
2346         if (!symbols[DEVICE_CLOCKWISE_NAME]) {
2347             AutoAttachPoolToThread attach(fProgram.fPool.get());
2348             Modifiers modifiers;
2349             modifiers.fLayout.fBuiltin = DEVICE_CLOCKWISE_BUILTIN;
2350             auto clockwiseVar = std::make_unique<Variable>(/*line=*/-1,
2351                                                            fContext.fModifiersPool->add(modifiers),
2352                                                            DEVICE_CLOCKWISE_NAME,
2353                                                            fContext.fTypes.fBool.get(),
2354                                                            true,
2355                                                            Variable::Storage::kGlobal);
2356             fSPIRVBonusVariables.insert(clockwiseVar.get());
2357             symbols.add(std::move(clockwiseVar));
2358         }
2359         DSLGlobalVar deviceClockwise(DEVICE_CLOCKWISE_NAME);
2360         // FrontFacing in Vulkan is defined in terms of a top-down render target. In skia,
2361         // we use the default convention of "counter-clockwise face is front".
2362         return this->writeExpression(*dsl::Bool(Select(rtFlip.y() > 0,
2363                                                        !deviceClockwise,
2364                                                        deviceClockwise)).release(),
2365                                      out);
2366     }
2367 #ifdef SKSL_EXT
2368     const Variable* var = ref.as<VariableReference>().variable();
2369     if (var && (var->modifiers().fLayout.fFlags & Layout::Flag::kConstantId_Flag)) {
2370         return fVariableMap[var];
2371     }
2372 #endif
2373     return this->getLValue(ref, out)->load(out);
2374 }
2375 
writeIndexExpression(const IndexExpression & expr,OutputStream & out)2376 SpvId SPIRVCodeGenerator::writeIndexExpression(const IndexExpression& expr, OutputStream& out) {
2377     if (expr.base()->type().isVector()) {
2378         SpvId base = this->writeExpression(*expr.base(), out);
2379         SpvId index = this->writeExpression(*expr.index(), out);
2380         SpvId result = this->nextId(nullptr);
2381         this->writeInstruction(SpvOpVectorExtractDynamic, this->getType(expr.type()), result, base,
2382                                index, out);
2383         return result;
2384     }
2385     return getLValue(expr, out)->load(out);
2386 }
2387 
writeFieldAccess(const FieldAccess & f,OutputStream & out)2388 SpvId SPIRVCodeGenerator::writeFieldAccess(const FieldAccess& f, OutputStream& out) {
2389     return getLValue(f, out)->load(out);
2390 }
2391 
writeSwizzle(const Swizzle & swizzle,OutputStream & out)2392 SpvId SPIRVCodeGenerator::writeSwizzle(const Swizzle& swizzle, OutputStream& out) {
2393     SpvId base = this->writeExpression(*swizzle.base(), out);
2394     SpvId result = this->nextId(&swizzle.type());
2395     size_t count = swizzle.components().size();
2396     if (count == 1) {
2397         this->writeInstruction(SpvOpCompositeExtract, this->getType(swizzle.type()), result, base,
2398                                swizzle.components()[0], out);
2399     } else {
2400         this->writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) count, out);
2401         this->writeWord(this->getType(swizzle.type()), out);
2402         this->writeWord(result, out);
2403         this->writeWord(base, out);
2404         this->writeWord(base, out);
2405         for (int component : swizzle.components()) {
2406             this->writeWord(component, out);
2407         }
2408     }
2409     return result;
2410 }
2411 
writeBinaryOperation(const Type & resultType,const Type & operandType,SpvId lhs,SpvId rhs,SpvOp_ ifFloat,SpvOp_ ifInt,SpvOp_ ifUInt,SpvOp_ ifBool,OutputStream & out)2412 SpvId SPIRVCodeGenerator::writeBinaryOperation(const Type& resultType,
2413                                                const Type& operandType, SpvId lhs,
2414                                                SpvId rhs, SpvOp_ ifFloat, SpvOp_ ifInt,
2415                                                SpvOp_ ifUInt, SpvOp_ ifBool, OutputStream& out) {
2416     SpvId result = this->nextId(&resultType);
2417     if (is_float(fContext, operandType)) {
2418         this->writeInstruction(ifFloat, this->getType(resultType), result, lhs, rhs, out);
2419     } else if (is_signed(fContext, operandType)) {
2420         this->writeInstruction(ifInt, this->getType(resultType), result, lhs, rhs, out);
2421     } else if (is_unsigned(fContext, operandType)) {
2422         this->writeInstruction(ifUInt, this->getType(resultType), result, lhs, rhs, out);
2423     } else if (is_bool(fContext, operandType)) {
2424         this->writeInstruction(ifBool, this->getType(resultType), result, lhs, rhs, out);
2425     } else {
2426         fContext.fErrors->error(operandType.fLine,
2427                 "unsupported operand for binary expression: " + operandType.description());
2428     }
2429     return result;
2430 }
2431 
foldToBool(SpvId id,const Type & operandType,SpvOp op,OutputStream & out)2432 SpvId SPIRVCodeGenerator::foldToBool(SpvId id, const Type& operandType, SpvOp op,
2433                                      OutputStream& out) {
2434     if (operandType.isVector()) {
2435         SpvId result = this->nextId(nullptr);
2436         this->writeInstruction(op, this->getType(*fContext.fTypes.fBool), result, id, out);
2437         return result;
2438     }
2439     return id;
2440 }
2441 
writeMatrixComparison(const Type & operandType,SpvId lhs,SpvId rhs,SpvOp_ floatOperator,SpvOp_ intOperator,SpvOp_ vectorMergeOperator,SpvOp_ mergeOperator,OutputStream & out)2442 SpvId SPIRVCodeGenerator::writeMatrixComparison(const Type& operandType, SpvId lhs, SpvId rhs,
2443                                                 SpvOp_ floatOperator, SpvOp_ intOperator,
2444                                                 SpvOp_ vectorMergeOperator, SpvOp_ mergeOperator,
2445                                                 OutputStream& out) {
2446     SpvOp_ compareOp = is_float(fContext, operandType) ? floatOperator : intOperator;
2447     SkASSERT(operandType.isMatrix());
2448     SpvId columnType = this->getType(operandType.componentType().toCompound(fContext,
2449                                                                             operandType.rows(),
2450                                                                             1));
2451     SpvId bvecType = this->getType(fContext.fTypes.fBool->toCompound(fContext,
2452                                                                     operandType.rows(),
2453                                                                     1));
2454     SpvId boolType = this->getType(*fContext.fTypes.fBool);
2455     SpvId result = 0;
2456     for (int i = 0; i < operandType.columns(); i++) {
2457         SpvId columnL = this->nextId(&operandType);
2458         this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out);
2459         SpvId columnR = this->nextId(&operandType);
2460         this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out);
2461         SpvId compare = this->nextId(&operandType);
2462         this->writeInstruction(compareOp, bvecType, compare, columnL, columnR, out);
2463         SpvId merge = this->nextId(nullptr);
2464         this->writeInstruction(vectorMergeOperator, boolType, merge, compare, out);
2465         if (result != 0) {
2466             SpvId next = this->nextId(nullptr);
2467             this->writeInstruction(mergeOperator, boolType, next, result, merge, out);
2468             result = next;
2469         }
2470         else {
2471             result = merge;
2472         }
2473     }
2474     return result;
2475 }
2476 
writeComponentwiseMatrixBinary(const Type & operandType,SpvId lhs,SpvId rhs,SpvOp_ op,OutputStream & out)2477 SpvId SPIRVCodeGenerator::writeComponentwiseMatrixBinary(const Type& operandType, SpvId lhs,
2478                                                          SpvId rhs, SpvOp_ op, OutputStream& out) {
2479     SkASSERT(operandType.isMatrix());
2480     SpvId columnType = this->getType(operandType.componentType().toCompound(fContext,
2481                                                                             operandType.rows(),
2482                                                                             1));
2483     std::vector<SpvId> columns;
2484     columns.reserve(operandType.columns());
2485     for (int i = 0; i < operandType.columns(); i++) {
2486         SpvId columnL = this->nextId(&operandType);
2487         this->writeInstruction(SpvOpCompositeExtract, columnType, columnL, lhs, i, out);
2488         SpvId columnR = this->nextId(&operandType);
2489         this->writeInstruction(SpvOpCompositeExtract, columnType, columnR, rhs, i, out);
2490         columns.push_back(this->nextId(&operandType));
2491         this->writeInstruction(op, columnType, columns[i], columnL, columnR, out);
2492     }
2493     return this->writeComposite(columns, operandType, out);
2494 }
2495 
writeReciprocal(const Type & type,SpvId value,OutputStream & out)2496 SpvId SPIRVCodeGenerator::writeReciprocal(const Type& type, SpvId value, OutputStream& out) {
2497     SkASSERT(type.isFloat());
2498     SpvId one = this->writeLiteral(1.0, type);
2499     SpvId reciprocal = this->nextId(&type);
2500     this->writeInstruction(SpvOpFDiv, this->getType(type), reciprocal, one, value, out);
2501     return reciprocal;
2502 }
2503 
writeScalarToMatrixSplat(const Type & matrixType,SpvId scalarId,OutputStream & out)2504 SpvId SPIRVCodeGenerator::writeScalarToMatrixSplat(const Type& matrixType,
2505                                                    SpvId scalarId,
2506                                                    OutputStream& out) {
2507     // Splat the scalar into a vector.
2508     const Type& vectorType = matrixType.componentType().toCompound(fContext,
2509                                                                    /*columns=*/matrixType.rows(),
2510                                                                    /*rows=*/1);
2511     std::vector<SpvId> vecArguments(/*count*/ matrixType.rows(), /*value*/ scalarId);
2512     SpvId vectorId = this->writeComposite(vecArguments, vectorType, out);
2513 
2514     // Splat the vector into a matrix.
2515     std::vector<SpvId> matArguments(/*count*/ matrixType.columns(), /*value*/ vectorId);
2516     return this->writeComposite(matArguments, matrixType, out);
2517 }
2518 
writeBinaryExpression(const Type & leftType,SpvId lhs,Operator op,const Type & rightType,SpvId rhs,const Type & resultType,OutputStream & out)2519 SpvId SPIRVCodeGenerator::writeBinaryExpression(const Type& leftType, SpvId lhs, Operator op,
2520                                                 const Type& rightType, SpvId rhs,
2521                                                 const Type& resultType, OutputStream& out) {
2522     // The comma operator ignores the type of the left-hand side entirely.
2523     if (op.kind() == Token::Kind::TK_COMMA) {
2524         return rhs;
2525     }
2526     // overall type we are operating on: float2, int, uint4...
2527     const Type* operandType;
2528     // IR allows mismatched types in expressions (e.g. float2 * float), but they need special
2529     // handling in SPIR-V
2530     if (this->getActualType(leftType) != this->getActualType(rightType)) {
2531         if (leftType.isVector() && rightType.isNumber()) {
2532             if (resultType.componentType().isFloat()) {
2533                 switch (op.kind()) {
2534                     case Token::Kind::TK_SLASH: {
2535                         rhs = this->writeReciprocal(rightType, rhs, out);
2536                         [[fallthrough]];
2537                     }
2538                     case Token::Kind::TK_STAR: {
2539                         SpvId result = this->nextId(&resultType);
2540                         this->writeInstruction(SpvOpVectorTimesScalar, this->getType(resultType),
2541                                                result, lhs, rhs, out);
2542                         return result;
2543                     }
2544                     default:
2545                         break;
2546                 }
2547             }
2548             // promote number to vector
2549             const Type& vecType = leftType;
2550             SpvId vec = this->nextId(&vecType);
2551             this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out);
2552             this->writeWord(this->getType(vecType), out);
2553             this->writeWord(vec, out);
2554             for (int i = 0; i < vecType.columns(); i++) {
2555                 this->writeWord(rhs, out);
2556             }
2557             rhs = vec;
2558             operandType = &leftType;
2559         } else if (rightType.isVector() && leftType.isNumber()) {
2560             if (resultType.componentType().isFloat()) {
2561                 if (op.kind() == Token::Kind::TK_STAR) {
2562                     SpvId result = this->nextId(&resultType);
2563                     this->writeInstruction(SpvOpVectorTimesScalar, this->getType(resultType),
2564                                            result, rhs, lhs, out);
2565                     return result;
2566                 }
2567             }
2568             // promote number to vector
2569             const Type& vecType = rightType;
2570             SpvId vec = this->nextId(&vecType);
2571             this->writeOpCode(SpvOpCompositeConstruct, 3 + vecType.columns(), out);
2572             this->writeWord(this->getType(vecType), out);
2573             this->writeWord(vec, out);
2574             for (int i = 0; i < vecType.columns(); i++) {
2575                 this->writeWord(lhs, out);
2576             }
2577             lhs = vec;
2578             operandType = &rightType;
2579         } else if (leftType.isMatrix()) {
2580             if (op.kind() == Token::Kind::TK_STAR) {
2581                 // Matrix-times-vector and matrix-times-scalar have dedicated ops in SPIR-V.
2582                 SpvOp_ spvop;
2583                 if (rightType.isMatrix()) {
2584                     spvop = SpvOpMatrixTimesMatrix;
2585                 } else if (rightType.isVector()) {
2586                     spvop = SpvOpMatrixTimesVector;
2587                 } else {
2588                     SkASSERT(rightType.isScalar());
2589                     spvop = SpvOpMatrixTimesScalar;
2590                 }
2591                 SpvId result = this->nextId(&resultType);
2592                 this->writeInstruction(spvop, this->getType(resultType), result, lhs, rhs, out);
2593                 return result;
2594             } else {
2595                 // Matrix-op-vector is not supported in GLSL/SkSL for non-multiplication ops; we
2596                 // expect to have a scalar here.
2597                 SkASSERT(rightType.isScalar());
2598 
2599                 // Splat rhs across an entire matrix so we can reuse the matrix-op-matrix path.
2600                 SpvId rhsMatrix = this->writeScalarToMatrixSplat(leftType, rhs, out);
2601 
2602                 // Perform this operation as matrix-op-matrix.
2603                 return this->writeBinaryExpression(leftType, lhs, op, leftType, rhsMatrix,
2604                                                    resultType, out);
2605             }
2606         } else if (rightType.isMatrix()) {
2607             if (op.kind() == Token::Kind::TK_STAR) {
2608                 // Matrix-times-vector and matrix-times-scalar have dedicated ops in SPIR-V.
2609                 SpvId result = this->nextId(&resultType);
2610                 if (leftType.isVector()) {
2611                     this->writeInstruction(SpvOpVectorTimesMatrix, this->getType(resultType),
2612                                            result, lhs, rhs, out);
2613                 } else {
2614                     SkASSERT(leftType.isScalar());
2615                     this->writeInstruction(SpvOpMatrixTimesScalar, this->getType(resultType),
2616                                            result, rhs, lhs, out);
2617                 }
2618                 return result;
2619             } else {
2620                 // Vector-op-matrix is not supported in GLSL/SkSL for non-multiplication ops; we
2621                 // expect to have a scalar here.
2622                 SkASSERT(leftType.isScalar());
2623 
2624                 // Splat lhs across an entire matrix so we can reuse the matrix-op-matrix path.
2625                 SpvId lhsMatrix = this->writeScalarToMatrixSplat(rightType, lhs, out);
2626 
2627                 // Perform this operation as matrix-op-matrix.
2628                 return this->writeBinaryExpression(rightType, lhsMatrix, op, rightType, rhs,
2629                                                    resultType, out);
2630             }
2631         } else {
2632             fContext.fErrors->error(leftType.fLine, "unsupported mixed-type expression");
2633             return -1;
2634         }
2635     } else {
2636         operandType = &this->getActualType(leftType);
2637         SkASSERT(*operandType == this->getActualType(rightType));
2638     }
2639     switch (op.kind()) {
2640         case Token::Kind::TK_EQEQ: {
2641             if (operandType->isMatrix()) {
2642                 return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdEqual,
2643                                                    SpvOpIEqual, SpvOpAll, SpvOpLogicalAnd, out);
2644             }
2645             if (operandType->isStruct()) {
2646                 return this->writeStructComparison(*operandType, lhs, op, rhs, out);
2647             }
2648             if (operandType->isArray()) {
2649                 return this->writeArrayComparison(*operandType, lhs, op, rhs, out);
2650             }
2651             SkASSERT(resultType.isBoolean());
2652             const Type* tmpType;
2653             if (operandType->isVector()) {
2654                 tmpType = &fContext.fTypes.fBool->toCompound(fContext,
2655                                                              operandType->columns(),
2656                                                              operandType->rows());
2657             } else {
2658                 tmpType = &resultType;
2659             }
2660             return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs,
2661                                                                SpvOpFOrdEqual, SpvOpIEqual,
2662                                                                SpvOpIEqual, SpvOpLogicalEqual, out),
2663                                     *operandType, SpvOpAll, out);
2664         }
2665         case Token::Kind::TK_NEQ:
2666             if (operandType->isMatrix()) {
2667 #ifdef SKSL_EXT
2668                 return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFUnordNotEqual,
2669                                                    SpvOpINotEqual, SpvOpAny, SpvOpLogicalOr, out);
2670 #else
2671                 return this->writeMatrixComparison(*operandType, lhs, rhs, SpvOpFOrdNotEqual,
2672                                                    SpvOpINotEqual, SpvOpAny, SpvOpLogicalOr, out);
2673 #endif
2674             }
2675             if (operandType->isStruct()) {
2676                 return this->writeStructComparison(*operandType, lhs, op, rhs, out);
2677             }
2678             if (operandType->isArray()) {
2679                 return this->writeArrayComparison(*operandType, lhs, op, rhs, out);
2680             }
2681             [[fallthrough]];
2682         case Token::Kind::TK_LOGICALXOR:
2683             SkASSERT(resultType.isBoolean());
2684             const Type* tmpType;
2685             if (operandType->isVector()) {
2686                 tmpType = &fContext.fTypes.fBool->toCompound(fContext,
2687                                                              operandType->columns(),
2688                                                              operandType->rows());
2689             } else {
2690                 tmpType = &resultType;
2691             }
2692 #ifdef SKSL_EXT
2693             return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs,
2694                                                                SpvOpFUnordNotEqual, SpvOpINotEqual,
2695                                                                SpvOpINotEqual, SpvOpLogicalNotEqual,
2696                                                                out),
2697                                     *operandType, SpvOpAny, out);
2698 #else
2699             return this->foldToBool(this->writeBinaryOperation(*tmpType, *operandType, lhs, rhs,
2700                                                                SpvOpFOrdNotEqual, SpvOpINotEqual,
2701                                                                SpvOpINotEqual, SpvOpLogicalNotEqual,
2702                                                                out),
2703                                     *operandType, SpvOpAny, out);
2704 #endif
2705         case Token::Kind::TK_GT:
2706             SkASSERT(resultType.isBoolean());
2707             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2708                                               SpvOpFOrdGreaterThan, SpvOpSGreaterThan,
2709                                               SpvOpUGreaterThan, SpvOpUndef, out);
2710         case Token::Kind::TK_LT:
2711             SkASSERT(resultType.isBoolean());
2712             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFOrdLessThan,
2713                                               SpvOpSLessThan, SpvOpULessThan, SpvOpUndef, out);
2714         case Token::Kind::TK_GTEQ:
2715             SkASSERT(resultType.isBoolean());
2716             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2717                                               SpvOpFOrdGreaterThanEqual, SpvOpSGreaterThanEqual,
2718                                               SpvOpUGreaterThanEqual, SpvOpUndef, out);
2719         case Token::Kind::TK_LTEQ:
2720             SkASSERT(resultType.isBoolean());
2721             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs,
2722                                               SpvOpFOrdLessThanEqual, SpvOpSLessThanEqual,
2723                                               SpvOpULessThanEqual, SpvOpUndef, out);
2724         case Token::Kind::TK_PLUS:
2725             if (leftType.isMatrix() && rightType.isMatrix()) {
2726                 SkASSERT(leftType == rightType);
2727                 return this->writeComponentwiseMatrixBinary(leftType, lhs, rhs, SpvOpFAdd, out);
2728             }
2729             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFAdd,
2730                                               SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
2731         case Token::Kind::TK_MINUS:
2732             if (leftType.isMatrix() && rightType.isMatrix()) {
2733                 SkASSERT(leftType == rightType);
2734                 return this->writeComponentwiseMatrixBinary(leftType, lhs, rhs, SpvOpFSub, out);
2735             }
2736             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFSub,
2737                                               SpvOpISub, SpvOpISub, SpvOpUndef, out);
2738         case Token::Kind::TK_STAR:
2739             if (leftType.isMatrix() && rightType.isMatrix()) {
2740                 // matrix multiply
2741                 SpvId result = this->nextId(&resultType);
2742                 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(resultType), result,
2743                                        lhs, rhs, out);
2744                 return result;
2745             }
2746             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMul,
2747                                               SpvOpIMul, SpvOpIMul, SpvOpUndef, out);
2748         case Token::Kind::TK_SLASH:
2749             if (leftType.isMatrix() && rightType.isMatrix()) {
2750                 SkASSERT(leftType == rightType);
2751                 return this->writeComponentwiseMatrixBinary(leftType, lhs, rhs, SpvOpFDiv, out);
2752             }
2753             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFDiv,
2754                                               SpvOpSDiv, SpvOpUDiv, SpvOpUndef, out);
2755         case Token::Kind::TK_PERCENT:
2756             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpFMod,
2757                                               SpvOpSMod, SpvOpUMod, SpvOpUndef, out);
2758         case Token::Kind::TK_SHL:
2759             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2760                                               SpvOpShiftLeftLogical, SpvOpShiftLeftLogical,
2761                                               SpvOpUndef, out);
2762         case Token::Kind::TK_SHR:
2763             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2764                                               SpvOpShiftRightArithmetic, SpvOpShiftRightLogical,
2765                                               SpvOpUndef, out);
2766         case Token::Kind::TK_BITWISEAND:
2767             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2768                                               SpvOpBitwiseAnd, SpvOpBitwiseAnd, SpvOpUndef, out);
2769         case Token::Kind::TK_BITWISEOR:
2770             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2771                                               SpvOpBitwiseOr, SpvOpBitwiseOr, SpvOpUndef, out);
2772         case Token::Kind::TK_BITWISEXOR:
2773             return this->writeBinaryOperation(resultType, *operandType, lhs, rhs, SpvOpUndef,
2774                                               SpvOpBitwiseXor, SpvOpBitwiseXor, SpvOpUndef, out);
2775         default:
2776             fContext.fErrors->error(0, "unsupported token");
2777             return -1;
2778     }
2779 }
2780 
writeArrayComparison(const Type & arrayType,SpvId lhs,Operator op,SpvId rhs,OutputStream & out)2781 SpvId SPIRVCodeGenerator::writeArrayComparison(const Type& arrayType, SpvId lhs, Operator op,
2782                                                SpvId rhs, OutputStream& out) {
2783     // The inputs must be arrays, and the op must be == or !=.
2784     SkASSERT(op.kind() == Token::Kind::TK_EQEQ || op.kind() == Token::Kind::TK_NEQ);
2785     SkASSERT(arrayType.isArray());
2786     const Type& componentType = arrayType.componentType();
2787     const SpvId componentTypeId = this->getType(componentType);
2788     const int arraySize = arrayType.columns();
2789     SkASSERT(arraySize > 0);
2790 
2791     // Synthesize equality checks for each item in the array.
2792     const Type& boolType = *fContext.fTypes.fBool;
2793     SpvId allComparisons = (SpvId)-1;
2794     for (int index = 0; index < arraySize; ++index) {
2795         // Get the left and right item in the array.
2796         SpvId itemL = this->nextId(&componentType);
2797         this->writeInstruction(SpvOpCompositeExtract, componentTypeId, itemL, lhs, index, out);
2798         SpvId itemR = this->nextId(&componentType);
2799         this->writeInstruction(SpvOpCompositeExtract, componentTypeId, itemR, rhs, index, out);
2800         // Use `writeBinaryExpression` with the requested == or != operator on these items.
2801         SpvId comparison = this->writeBinaryExpression(componentType, itemL, op,
2802                                                        componentType, itemR, boolType, out);
2803         // Merge this comparison result with all the other comparisons we've done.
2804         allComparisons = this->mergeComparisons(comparison, allComparisons, op, out);
2805     }
2806     return allComparisons;
2807 }
2808 
writeStructComparison(const Type & structType,SpvId lhs,Operator op,SpvId rhs,OutputStream & out)2809 SpvId SPIRVCodeGenerator::writeStructComparison(const Type& structType, SpvId lhs, Operator op,
2810                                                 SpvId rhs, OutputStream& out) {
2811     // The inputs must be structs containing fields, and the op must be == or !=.
2812     SkASSERT(op.kind() == Token::Kind::TK_EQEQ || op.kind() == Token::Kind::TK_NEQ);
2813     SkASSERT(structType.isStruct());
2814     const std::vector<Type::Field>& fields = structType.fields();
2815     SkASSERT(!fields.empty());
2816 
2817     // Synthesize equality checks for each field in the struct.
2818     const Type& boolType = *fContext.fTypes.fBool;
2819     SpvId allComparisons = (SpvId)-1;
2820     for (int index = 0; index < (int)fields.size(); ++index) {
2821         // Get the left and right versions of this field.
2822         const Type& fieldType = *fields[index].fType;
2823         const SpvId fieldTypeId = this->getType(fieldType);
2824 
2825         SpvId fieldL = this->nextId(&fieldType);
2826         this->writeInstruction(SpvOpCompositeExtract, fieldTypeId, fieldL, lhs, index, out);
2827         SpvId fieldR = this->nextId(&fieldType);
2828         this->writeInstruction(SpvOpCompositeExtract, fieldTypeId, fieldR, rhs, index, out);
2829         // Use `writeBinaryExpression` with the requested == or != operator on these fields.
2830         SpvId comparison = this->writeBinaryExpression(fieldType, fieldL, op, fieldType, fieldR,
2831                                                        boolType, out);
2832         // Merge this comparison result with all the other comparisons we've done.
2833         allComparisons = this->mergeComparisons(comparison, allComparisons, op, out);
2834     }
2835     return allComparisons;
2836 }
2837 
mergeComparisons(SpvId comparison,SpvId allComparisons,Operator op,OutputStream & out)2838 SpvId SPIRVCodeGenerator::mergeComparisons(SpvId comparison, SpvId allComparisons, Operator op,
2839                                            OutputStream& out) {
2840     // If this is the first entry, we don't need to merge comparison results with anything.
2841     if (allComparisons == (SpvId)-1) {
2842         return comparison;
2843     }
2844     // Use LogicalAnd or LogicalOr to combine the comparison with all the other comparisons.
2845     const Type& boolType = *fContext.fTypes.fBool;
2846     SpvId boolTypeId = this->getType(boolType);
2847     SpvId logicalOp = this->nextId(&boolType);
2848     switch (op.kind()) {
2849         case Token::Kind::TK_EQEQ:
2850             this->writeInstruction(SpvOpLogicalAnd, boolTypeId, logicalOp,
2851                                    comparison, allComparisons, out);
2852             break;
2853         case Token::Kind::TK_NEQ:
2854             this->writeInstruction(SpvOpLogicalOr, boolTypeId, logicalOp,
2855                                    comparison, allComparisons, out);
2856             break;
2857         default:
2858             SkDEBUGFAILF("mergeComparisons only supports == and !=, not %s", op.operatorName());
2859             return (SpvId)-1;
2860     }
2861     return logicalOp;
2862 }
2863 
division_by_literal_value(Operator op,const Expression & right)2864 static float division_by_literal_value(Operator op, const Expression& right) {
2865     // If this is a division by a literal value, returns that literal value. Otherwise, returns 0.
2866     if (op.kind() == Token::Kind::TK_SLASH && right.isFloatLiteral()) {
2867         float rhsValue = right.as<Literal>().floatValue();
2868         if (std::isfinite(rhsValue)) {
2869             return rhsValue;
2870         }
2871     }
2872     return 0.0f;
2873 }
2874 
2875 #ifdef SKSL_EXT
writeSpecConstBinaryExpression(const BinaryExpression & b,const Operator & op,SpvId lhs,SpvId rhs)2876 SpvId SPIRVCodeGenerator::writeSpecConstBinaryExpression(const BinaryExpression& b, const Operator& op,
2877                                                          SpvId lhs, SpvId rhs) {
2878     SpvId result = this->nextId(&(b.type()));
2879     switch (op.removeAssignment().kind()) {
2880         case Operator::Kind::TK_EQEQ:
2881             this->writeInstruction(SpvOpSpecConstantOp, this->getType(b.type()), result,
2882                                    SpvOpIEqual, lhs, rhs, fConstantBuffer);
2883             break;
2884         case Operator::Kind::TK_NEQ:
2885             this->writeInstruction(SpvOpSpecConstantOp, this->getType(b.type()), result,
2886                                    SpvOpINotEqual, lhs, rhs, fConstantBuffer);
2887             break;
2888         case Operator::Kind::TK_LT:
2889             this->writeInstruction(SpvOpSpecConstantOp, this->getType(b.type()), result,
2890                                    SpvOpULessThan, lhs, rhs, fConstantBuffer);
2891             break;
2892         case Operator::Kind::TK_LTEQ:
2893             this->writeInstruction(SpvOpSpecConstantOp, this->getType(b.type()), result,
2894                                    SpvOpULessThanEqual, lhs, rhs, fConstantBuffer);
2895             break;
2896         case Operator::Kind::TK_GT:
2897             this->writeInstruction(SpvOpSpecConstantOp, this->getType(b.type()), result,
2898                                    SpvOpUGreaterThan, lhs, rhs, fConstantBuffer);
2899             break;
2900         case Operator::Kind::TK_GTEQ:
2901             this->writeInstruction(SpvOpSpecConstantOp, this->getType(b.type()), result,
2902                                    SpvOpUGreaterThanEqual, lhs, rhs, fConstantBuffer);
2903             break;
2904         default:
2905             fContext.fErrors->error(b.fLine, "spec constant does not support operator: " +
2906                                     String(op.operatorName()));
2907             return -1;
2908     }
2909     return result;
2910 }
2911 #endif
2912 
writeBinaryExpression(const BinaryExpression & b,OutputStream & out)2913 SpvId SPIRVCodeGenerator::writeBinaryExpression(const BinaryExpression& b, OutputStream& out) {
2914     const Expression* left = b.left().get();
2915     const Expression* right = b.right().get();
2916     Operator op = b.getOperator();
2917 
2918     switch (op.kind()) {
2919         case Token::Kind::TK_EQ: {
2920             // Handles assignment.
2921             SpvId rhs = this->writeExpression(*right, out);
2922             this->getLValue(*left, out)->store(rhs, out);
2923             return rhs;
2924         }
2925         case Token::Kind::TK_LOGICALAND:
2926             // Handles short-circuiting; we don't necessarily evaluate both LHS and RHS.
2927             return this->writeLogicalAnd(*b.left(), *b.right(), out);
2928 
2929         case Token::Kind::TK_LOGICALOR:
2930             // Handles short-circuiting; we don't necessarily evaluate both LHS and RHS.
2931             return this->writeLogicalOr(*b.left(), *b.right(), out);
2932 
2933         default:
2934             break;
2935     }
2936 
2937     std::unique_ptr<LValue> lvalue;
2938     SpvId lhs;
2939     if (op.isAssignment()) {
2940         lvalue = this->getLValue(*left, out);
2941         lhs = lvalue->load(out);
2942     } else {
2943         lvalue = nullptr;
2944         lhs = this->writeExpression(*left, out);
2945     }
2946 
2947     SpvId rhs;
2948     float rhsValue = division_by_literal_value(op, *right);
2949     if (rhsValue != 0.0f) {
2950         // Rewrite floating-point division by a literal into multiplication by the reciprocal.
2951         // This converts `expr / 2` into `expr * 0.5`
2952         // This improves codegen, especially for certain types of divides (e.g. vector/scalar).
2953         op = Operator(Token::Kind::TK_STAR);
2954         rhs = this->writeLiteral(1.0 / rhsValue, right->type());
2955     } else {
2956         // Write the right-hand side expression normally.
2957         rhs = this->writeExpression(*right, out);
2958     }
2959 
2960 #ifdef SKSL_EXT
2961     if (left->kind() == Expression::Kind::kVariableReference) {
2962         VariableReference* rightRef = (VariableReference*) right;
2963         const Expression* expr = ConstantFolder::GetConstantValueForVariable(*rightRef);
2964         if (expr != rightRef) {
2965             VariableReference* ref = (VariableReference*) left;
2966             const Variable* var = ref->variable();
2967             if (var && (var->modifiers().fLayout.fFlags & Layout::Flag::kConstantId_Flag)) {
2968                 return writeSpecConstBinaryExpression(b, op, lhs, rhs);
2969             }
2970         }
2971     }
2972     if (right->kind() == Expression::Kind::kVariableReference) {
2973         VariableReference* leftRef = (VariableReference*) left;
2974         const Expression* expr = ConstantFolder::GetConstantValueForVariable(*leftRef);
2975         if (expr != leftRef) {
2976             VariableReference* ref = (VariableReference*) right;
2977             const Variable* var = ref->variable();
2978             if (var && (var->modifiers().fLayout.fFlags & Layout::Flag::kConstantId_Flag)) {
2979                 return writeSpecConstBinaryExpression(b, op, lhs, rhs);
2980             }
2981         }
2982     }
2983 #endif
2984 
2985     SpvId result = this->writeBinaryExpression(left->type(), lhs, op.removeAssignment(),
2986                                                right->type(), rhs, b.type(), out);
2987     if (lvalue) {
2988         lvalue->store(result, out);
2989     }
2990     return result;
2991 }
2992 
writeLogicalAnd(const Expression & left,const Expression & right,OutputStream & out)2993 SpvId SPIRVCodeGenerator::writeLogicalAnd(const Expression& left, const Expression& right,
2994                                           OutputStream& out) {
2995     SpvId falseConstant = this->writeLiteral(0.0, *fContext.fTypes.fBool);
2996     SpvId lhs = this->writeExpression(left, out);
2997     SpvId rhsLabel = this->nextId(nullptr);
2998     SpvId end = this->nextId(nullptr);
2999     SpvId lhsBlock = fCurrentBlock;
3000     this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
3001     this->writeInstruction(SpvOpBranchConditional, lhs, rhsLabel, end, out);
3002     this->writeLabel(rhsLabel, out);
3003     SpvId rhs = this->writeExpression(right, out);
3004     SpvId rhsBlock = fCurrentBlock;
3005     this->writeInstruction(SpvOpBranch, end, out);
3006     this->writeLabel(end, out);
3007     SpvId result = this->nextId(nullptr);
3008     this->writeInstruction(SpvOpPhi, this->getType(*fContext.fTypes.fBool), result, falseConstant,
3009                            lhsBlock, rhs, rhsBlock, out);
3010     return result;
3011 }
3012 
writeLogicalOr(const Expression & left,const Expression & right,OutputStream & out)3013 SpvId SPIRVCodeGenerator::writeLogicalOr(const Expression& left, const Expression& right,
3014                                          OutputStream& out) {
3015     SpvId trueConstant = this->writeLiteral(1.0, *fContext.fTypes.fBool);
3016     SpvId lhs = this->writeExpression(left, out);
3017     SpvId rhsLabel = this->nextId(nullptr);
3018     SpvId end = this->nextId(nullptr);
3019     SpvId lhsBlock = fCurrentBlock;
3020     this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
3021     this->writeInstruction(SpvOpBranchConditional, lhs, end, rhsLabel, out);
3022     this->writeLabel(rhsLabel, out);
3023     SpvId rhs = this->writeExpression(right, out);
3024     SpvId rhsBlock = fCurrentBlock;
3025     this->writeInstruction(SpvOpBranch, end, out);
3026     this->writeLabel(end, out);
3027     SpvId result = this->nextId(nullptr);
3028     this->writeInstruction(SpvOpPhi, this->getType(*fContext.fTypes.fBool), result, trueConstant,
3029                            lhsBlock, rhs, rhsBlock, out);
3030     return result;
3031 }
3032 
writeTernaryExpression(const TernaryExpression & t,OutputStream & out)3033 SpvId SPIRVCodeGenerator::writeTernaryExpression(const TernaryExpression& t, OutputStream& out) {
3034     const Type& type = t.type();
3035     SpvId test = this->writeExpression(*t.test(), out);
3036     if (t.ifTrue()->type().columns() == 1 &&
3037         t.ifTrue()->isCompileTimeConstant() &&
3038         t.ifFalse()->isCompileTimeConstant()) {
3039         // both true and false are constants, can just use OpSelect
3040         SpvId result = this->nextId(nullptr);
3041         SpvId trueId = this->writeExpression(*t.ifTrue(), out);
3042         SpvId falseId = this->writeExpression(*t.ifFalse(), out);
3043         this->writeInstruction(SpvOpSelect, this->getType(type), result, test, trueId, falseId,
3044                                out);
3045         return result;
3046     }
3047     // was originally using OpPhi to choose the result, but for some reason that is crashing on
3048     // Adreno. Switched to storing the result in a temp variable as glslang does.
3049     SpvId var = this->nextId(nullptr);
3050     this->writeInstruction(SpvOpVariable, this->getPointerType(type, SpvStorageClassFunction),
3051                            var, SpvStorageClassFunction, fVariableBuffer);
3052     SpvId trueLabel = this->nextId(nullptr);
3053     SpvId falseLabel = this->nextId(nullptr);
3054     SpvId end = this->nextId(nullptr);
3055     this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
3056     this->writeInstruction(SpvOpBranchConditional, test, trueLabel, falseLabel, out);
3057     this->writeLabel(trueLabel, out);
3058     this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.ifTrue(), out), out);
3059     this->writeInstruction(SpvOpBranch, end, out);
3060     this->writeLabel(falseLabel, out);
3061     this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.ifFalse(), out), out);
3062     this->writeInstruction(SpvOpBranch, end, out);
3063     this->writeLabel(end, out);
3064     SpvId result = this->nextId(&type);
3065     this->writeInstruction(SpvOpLoad, this->getType(type), result, var, out);
3066     return result;
3067 }
3068 
writePrefixExpression(const PrefixExpression & p,OutputStream & out)3069 SpvId SPIRVCodeGenerator::writePrefixExpression(const PrefixExpression& p, OutputStream& out) {
3070     const Type& type = p.type();
3071     if (p.getOperator().kind() == Token::Kind::TK_MINUS) {
3072         SpvId result = this->nextId(&type);
3073         SpvId typeId = this->getType(type);
3074         SpvId expr = this->writeExpression(*p.operand(), out);
3075         if (is_float(fContext, type)) {
3076             this->writeInstruction(SpvOpFNegate, typeId, result, expr, out);
3077         } else if (is_signed(fContext, type) || is_unsigned(fContext, type)) {
3078             this->writeInstruction(SpvOpSNegate, typeId, result, expr, out);
3079         } else {
3080             SkDEBUGFAILF("unsupported prefix expression %s", p.description().c_str());
3081         }
3082         return result;
3083     }
3084     switch (p.getOperator().kind()) {
3085         case Token::Kind::TK_PLUS:
3086             return this->writeExpression(*p.operand(), out);
3087         case Token::Kind::TK_PLUSPLUS: {
3088             std::unique_ptr<LValue> lv = this->getLValue(*p.operand(), out);
3089             SpvId one = this->writeLiteral(1.0, type);
3090             SpvId result = this->writeBinaryOperation(type, type, lv->load(out), one,
3091                                                       SpvOpFAdd, SpvOpIAdd, SpvOpIAdd, SpvOpUndef,
3092                                                       out);
3093             lv->store(result, out);
3094             return result;
3095         }
3096         case Token::Kind::TK_MINUSMINUS: {
3097             std::unique_ptr<LValue> lv = this->getLValue(*p.operand(), out);
3098             SpvId one = this->writeLiteral(1.0, type);
3099             SpvId result = this->writeBinaryOperation(type, type, lv->load(out), one, SpvOpFSub,
3100                                                       SpvOpISub, SpvOpISub, SpvOpUndef, out);
3101             lv->store(result, out);
3102             return result;
3103         }
3104         case Token::Kind::TK_LOGICALNOT: {
3105             SkASSERT(p.operand()->type().isBoolean());
3106             SpvId result = this->nextId(nullptr);
3107             this->writeInstruction(SpvOpLogicalNot, this->getType(type), result,
3108                                    this->writeExpression(*p.operand(), out), out);
3109             return result;
3110         }
3111         case Token::Kind::TK_BITWISENOT: {
3112             SpvId result = this->nextId(nullptr);
3113             this->writeInstruction(SpvOpNot, this->getType(type), result,
3114                                    this->writeExpression(*p.operand(), out), out);
3115             return result;
3116         }
3117         default:
3118             SkDEBUGFAILF("unsupported prefix expression: %s", p.description().c_str());
3119             return -1;
3120     }
3121 }
3122 
writePostfixExpression(const PostfixExpression & p,OutputStream & out)3123 SpvId SPIRVCodeGenerator::writePostfixExpression(const PostfixExpression& p, OutputStream& out) {
3124     const Type& type = p.type();
3125     std::unique_ptr<LValue> lv = this->getLValue(*p.operand(), out);
3126     SpvId result = lv->load(out);
3127     SpvId one = this->writeLiteral(1.0, type);
3128     switch (p.getOperator().kind()) {
3129         case Token::Kind::TK_PLUSPLUS: {
3130             SpvId temp = this->writeBinaryOperation(type, type, result, one, SpvOpFAdd,
3131                                                     SpvOpIAdd, SpvOpIAdd, SpvOpUndef, out);
3132             lv->store(temp, out);
3133             return result;
3134         }
3135         case Token::Kind::TK_MINUSMINUS: {
3136             SpvId temp = this->writeBinaryOperation(type, type, result, one, SpvOpFSub,
3137                                                     SpvOpISub, SpvOpISub, SpvOpUndef, out);
3138             lv->store(temp, out);
3139             return result;
3140         }
3141         default:
3142             SkDEBUGFAILF("unsupported postfix expression %s", p.description().c_str());
3143             return -1;
3144     }
3145 }
3146 
writeLiteral(const Literal & l)3147 SpvId SPIRVCodeGenerator::writeLiteral(const Literal& l) {
3148     return this->writeLiteral(l.value(), l.type());
3149 }
3150 
writeLiteral(double value,const Type & type)3151 SpvId SPIRVCodeGenerator::writeLiteral(double value, const Type& type) {
3152     int32_t valueBits;
3153     if (type.isFloat()) {
3154         float fValue = value;
3155         memcpy(&valueBits, &fValue, sizeof(valueBits));
3156     } else {
3157         SKSL_INT iValue = value;
3158         valueBits = iValue;
3159     }
3160 
3161     SPIRVNumberConstant key{valueBits, type.numberKind()};
3162     auto [iter, newlyCreated] = fNumberConstants.insert({key, (SpvId)-1});
3163     if (newlyCreated) {
3164         SpvId result = this->nextId(nullptr);
3165         iter->second = result;
3166 
3167         if (type.isBoolean()) {
3168             this->writeInstruction(valueBits ? SpvOpConstantTrue : SpvOpConstantFalse,
3169                                    this->getType(type), result, fConstantBuffer);
3170         } else {
3171             this->writeInstruction(SpvOpConstant, this->getType(type), result,
3172                                    (SpvId)valueBits, fConstantBuffer);
3173         }
3174     }
3175 
3176     return iter->second;
3177 }
3178 
writeFunctionStart(const FunctionDeclaration & f,OutputStream & out)3179 SpvId SPIRVCodeGenerator::writeFunctionStart(const FunctionDeclaration& f, OutputStream& out) {
3180     SpvId result = fFunctionMap[&f];
3181     SpvId returnTypeId = this->getType(f.returnType());
3182     SpvId functionTypeId = this->getFunctionType(f);
3183     this->writeInstruction(SpvOpFunction, returnTypeId, result,
3184                            SpvFunctionControlMaskNone, functionTypeId, out);
3185     String mangledName = f.mangledName();
3186     this->writeInstruction(SpvOpName,
3187                            result,
3188                            skstd::string_view(mangledName.c_str(), mangledName.size()),
3189                            fNameBuffer);
3190     for (const Variable* parameter : f.parameters()) {
3191         SpvId id = this->nextId(nullptr);
3192         fVariableMap[parameter] = id;
3193         SpvId type = this->getPointerType(parameter->type(), SpvStorageClassFunction);
3194         this->writeInstruction(SpvOpFunctionParameter, type, id, out);
3195     }
3196     return result;
3197 }
3198 
writeFunction(const FunctionDefinition & f,OutputStream & out)3199 SpvId SPIRVCodeGenerator::writeFunction(const FunctionDefinition& f, OutputStream& out) {
3200     fVariableBuffer.reset();
3201     SpvId result = this->writeFunctionStart(f.declaration(), out);
3202     fCurrentBlock = 0;
3203     this->writeLabel(this->nextId(nullptr), out);
3204     StringStream bodyBuffer;
3205     this->writeBlock(f.body()->as<Block>(), bodyBuffer);
3206     write_stringstream(fVariableBuffer, out);
3207     if (f.declaration().isMain()) {
3208         write_stringstream(fGlobalInitializersBuffer, out);
3209     }
3210     write_stringstream(bodyBuffer, out);
3211     if (fCurrentBlock) {
3212         if (f.declaration().returnType().isVoid()) {
3213             this->writeInstruction(SpvOpReturn, out);
3214         } else {
3215             this->writeInstruction(SpvOpUnreachable, out);
3216         }
3217     }
3218     this->writeInstruction(SpvOpFunctionEnd, out);
3219     return result;
3220 }
3221 
writeLayout(const Layout & layout,SpvId target)3222 void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target) {
3223     if (layout.fLocation >= 0) {
3224         this->writeInstruction(SpvOpDecorate, target, SpvDecorationLocation, layout.fLocation,
3225                                fDecorationBuffer);
3226     }
3227     if (layout.fBinding >= 0) {
3228         this->writeInstruction(SpvOpDecorate, target, SpvDecorationBinding, layout.fBinding,
3229                                fDecorationBuffer);
3230     }
3231     if (layout.fIndex >= 0) {
3232         this->writeInstruction(SpvOpDecorate, target, SpvDecorationIndex, layout.fIndex,
3233                                fDecorationBuffer);
3234     }
3235     if (layout.fSet >= 0) {
3236         this->writeInstruction(SpvOpDecorate, target, SpvDecorationDescriptorSet, layout.fSet,
3237                                fDecorationBuffer);
3238     }
3239     if (layout.fInputAttachmentIndex >= 0) {
3240         this->writeInstruction(SpvOpDecorate, target, SpvDecorationInputAttachmentIndex,
3241                                layout.fInputAttachmentIndex, fDecorationBuffer);
3242         fCapabilities |= (((uint64_t) 1) << SpvCapabilityInputAttachment);
3243     }
3244     if (layout.fBuiltin >= 0 && layout.fBuiltin != SK_FRAGCOLOR_BUILTIN) {
3245         this->writeInstruction(SpvOpDecorate, target, SpvDecorationBuiltIn, layout.fBuiltin,
3246                                fDecorationBuffer);
3247     }
3248 }
3249 
writeLayout(const Layout & layout,SpvId target,int member)3250 void SPIRVCodeGenerator::writeLayout(const Layout& layout, SpvId target, int member) {
3251     if (layout.fLocation >= 0) {
3252         this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationLocation,
3253                                layout.fLocation, fDecorationBuffer);
3254     }
3255     if (layout.fBinding >= 0) {
3256         this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBinding,
3257                                layout.fBinding, fDecorationBuffer);
3258     }
3259     if (layout.fIndex >= 0) {
3260         this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationIndex,
3261                                layout.fIndex, fDecorationBuffer);
3262     }
3263     if (layout.fSet >= 0) {
3264         this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationDescriptorSet,
3265                                layout.fSet, fDecorationBuffer);
3266     }
3267     if (layout.fInputAttachmentIndex >= 0) {
3268         this->writeInstruction(SpvOpDecorate, target, member, SpvDecorationInputAttachmentIndex,
3269                                layout.fInputAttachmentIndex, fDecorationBuffer);
3270     }
3271     if (layout.fBuiltin >= 0) {
3272         this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecorationBuiltIn,
3273                                layout.fBuiltin, fDecorationBuffer);
3274     }
3275 }
3276 
memoryLayoutForVariable(const Variable & v) const3277 MemoryLayout SPIRVCodeGenerator::memoryLayoutForVariable(const Variable& v) const {
3278     bool pushConstant = ((v.modifiers().fLayout.fFlags & Layout::kPushConstant_Flag) != 0);
3279     return pushConstant ? MemoryLayout(MemoryLayout::k430_Standard) : fDefaultLayout;
3280 }
3281 
writeInterfaceBlock(const InterfaceBlock & intf,bool appendRTFlip)3282 SpvId SPIRVCodeGenerator::writeInterfaceBlock(const InterfaceBlock& intf, bool appendRTFlip) {
3283     MemoryLayout memoryLayout = this->memoryLayoutForVariable(intf.variable());
3284     SpvId result = this->nextId(nullptr);
3285     const Variable& intfVar = intf.variable();
3286     const Type& type = intfVar.type();
3287     if (!MemoryLayout::LayoutIsSupported(type)) {
3288         fContext.fErrors->error(type.fLine, "type '" + type.name() + "' is not permitted here");
3289         return this->nextId(nullptr);
3290     }
3291     SpvStorageClass_ storageClass = get_storage_class(intf.variable(), SpvStorageClassFunction);
3292     if (fProgram.fInputs.fUseFlipRTUniform && appendRTFlip && type.isStruct()) {
3293         // We can only have one interface block (because we use push_constant and that is limited
3294         // to one per program), so we need to append rtflip to this one rather than synthesize an
3295         // entirely new block when the variable is referenced. And we can't modify the existing
3296         // block, so we instead create a modified copy of it and write that.
3297         std::vector<Type::Field> fields = type.fields();
3298         fields.emplace_back(Modifiers(Layout(/*flags=*/0,
3299                                              /*location=*/-1,
3300                                              fProgram.fConfig->fSettings.fRTFlipOffset,
3301                                              /*binding=*/-1,
3302                                              /*index=*/-1,
3303                                              /*set=*/-1,
3304                                              /*builtin=*/-1,
3305                                              /*inputAttachmentIndex=*/-1),
3306                                       /*flags=*/0),
3307                             SKSL_RTFLIP_NAME,
3308                             fContext.fTypes.fFloat2.get());
3309         {
3310             AutoAttachPoolToThread attach(fProgram.fPool.get());
3311             const Type* rtFlipStructType = fProgram.fSymbols->takeOwnershipOfSymbol(
3312                     Type::MakeStructType(type.fLine, type.name(), std::move(fields)));
3313             const Variable* modifiedVar = fProgram.fSymbols->takeOwnershipOfSymbol(
3314                     std::make_unique<Variable>(intfVar.fLine,
3315                                                &intfVar.modifiers(),
3316                                                intfVar.name(),
3317                                                rtFlipStructType,
3318                                                intfVar.isBuiltin(),
3319                                                intfVar.storage()));
3320             fSPIRVBonusVariables.insert(modifiedVar);
3321             InterfaceBlock modifiedCopy(intf.fLine,
3322                                         *modifiedVar,
3323                                         intf.typeName(),
3324                                         intf.instanceName(),
3325                                         intf.arraySize(),
3326                                         intf.typeOwner());
3327             result = this->writeInterfaceBlock(modifiedCopy, false);
3328             fProgram.fSymbols->add(std::make_unique<Field>(
3329                     /*line=*/-1, modifiedVar, rtFlipStructType->fields().size() - 1));
3330         }
3331         fVariableMap[&intfVar] = result;
3332         fWroteRTFlip = true;
3333         return result;
3334     }
3335     const Modifiers& intfModifiers = intfVar.modifiers();
3336     SpvId typeId = this->getType(type, memoryLayout);
3337     if (intfModifiers.fLayout.fBuiltin == -1) {
3338         this->writeInstruction(SpvOpDecorate, typeId, SpvDecorationBlock, fDecorationBuffer);
3339     }
3340     SpvId ptrType = this->nextId(nullptr);
3341     this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, typeId, fConstantBuffer);
3342     this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConstantBuffer);
3343     Layout layout = intfModifiers.fLayout;
3344     if (intfModifiers.fFlags & Modifiers::kUniform_Flag && layout.fSet == -1) {
3345         layout.fSet = 0;
3346     }
3347 #ifdef SKSL_EXT
3348     if (intfModifiers.fFlags & Modifiers::kBuffer_Flag && layout.fSet == -1) {
3349         layout.fSet = 0;
3350     }
3351 #endif
3352     this->writeLayout(layout, result);
3353     fVariableMap[&intfVar] = result;
3354     return result;
3355 }
3356 
isDead(const Variable & var) const3357 bool SPIRVCodeGenerator::isDead(const Variable& var) const {
3358     // During SPIR-V code generation, we synthesize some extra bonus variables that don't actually
3359     // exist in the Program at all and aren't tracked by the ProgramUsage. They aren't dead, though.
3360     if (fSPIRVBonusVariables.count(&var)) {
3361         return false;
3362     }
3363     ProgramUsage::VariableCounts counts = fProgram.usage()->get(var);
3364     if (counts.fRead || counts.fWrite) {
3365         return false;
3366     }
3367     // It's not entirely clear what the rules are for eliding interface variables. Generally, it
3368     // causes problems to elide them, even when they're dead.
3369     return !(var.modifiers().fFlags &
3370              (Modifiers::kIn_Flag | Modifiers::kOut_Flag | Modifiers::kUniform_Flag));
3371 }
3372 
writeGlobalVar(ProgramKind kind,const VarDeclaration & varDecl)3373 void SPIRVCodeGenerator::writeGlobalVar(ProgramKind kind, const VarDeclaration& varDecl) {
3374     const Variable& var = varDecl.var();
3375     if (var.modifiers().fLayout.fBuiltin == SK_FRAGCOLOR_BUILTIN &&
3376         kind != ProgramKind::kFragment) {
3377         SkASSERT(!fProgram.fConfig->fSettings.fFragColorIsInOut);
3378         return;
3379     }
3380     if (var.modifiers().fLayout.fBuiltin == SK_SECONDARYFRAGCOLOR_BUILTIN) {
3381         return;
3382     }
3383     if (this->isDead(var)) {
3384         return;
3385     }
3386     SpvStorageClass_ storageClass = get_storage_class(var, SpvStorageClassPrivate);
3387     if (storageClass == SpvStorageClassUniform) {
3388         // Top-level uniforms are emitted in writeUniformBuffer.
3389         fTopLevelUniforms.push_back(&varDecl);
3390         return;
3391     }
3392 #ifdef SKSL_EXT
3393     if (var.modifiers().fLayout.fFlags & Layout::Flag::kConstantId_Flag) {
3394         Layout layout = var.modifiers().fLayout;
3395         const Type& type = var.type();
3396         SpvId id = this->nextId(&type);
3397         fVariableMap[&var] = id;
3398         SpvId typeId = this->getType(type);
3399         if (type.isInteger() && varDecl.value()) {
3400             int tmp = (*varDecl.value()).as<Literal>().intValue();
3401             this->writeInstruction(SpvOpSpecConstant, typeId, id, tmp, fConstantBuffer);
3402         } else {
3403             fContext.fErrors->error(var.fLine, "spec const '" + var.name() +
3404                 "' must be an integer literal");
3405             return;
3406         }
3407         this->writeInstruction(SpvOpName, id, var.name(), fNameBuffer);
3408         this->writeInstruction(SpvOpDecorate, id, SpvDecorationSpecId, layout.fConstantId,
3409                                fDecorationBuffer);
3410         return;
3411     }
3412 #endif
3413     const Type& type = var.type();
3414     Layout layout = var.modifiers().fLayout;
3415     if (layout.fSet < 0 && storageClass == SpvStorageClassUniformConstant) {
3416         layout.fSet = fProgram.fConfig->fSettings.fDefaultUniformSet;
3417     }
3418 #ifdef SKSL_EXT
3419     if (storageClass == SpvStorageClassFunction) {
3420         SkASSERT(varDecl.value());
3421         this->getPointerType(type, storageClass);
3422         fEmittingGlobalConstConstructor = true;
3423         SpvId value = this->writeExpression(*varDecl.value(), fConstantBuffer);
3424         fEmittingGlobalConstConstructor = false;
3425         fGlobalConstVariableValueMap[&var] = value;
3426     } else {
3427 #endif
3428         SpvId id = this->nextId(&type);
3429         fVariableMap[&var] = id;
3430         SpvId typeId = this->getPointerType(type, storageClass);
3431         this->writeInstruction(SpvOpVariable, typeId, id, storageClass, fConstantBuffer);
3432         this->writeInstruction(SpvOpName, id, var.name(), fNameBuffer);
3433         if (varDecl.value()) {
3434             SkASSERT(!fCurrentBlock);
3435             fCurrentBlock = -1;
3436             SpvId value = this->writeExpression(*varDecl.value(), fGlobalInitializersBuffer);
3437             this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuffer);
3438             fCurrentBlock = 0;
3439         }
3440         this->writeLayout(layout, id);
3441         if (var.modifiers().fFlags & Modifiers::kFlat_Flag) {
3442             this->writeInstruction(SpvOpDecorate, id, SpvDecorationFlat, fDecorationBuffer);
3443         }
3444         if (var.modifiers().fFlags & Modifiers::kNoPerspective_Flag) {
3445             this->writeInstruction(SpvOpDecorate, id, SpvDecorationNoPerspective,
3446                                     fDecorationBuffer);
3447         }
3448 #ifdef SKSL_EXT
3449     }
3450 #endif
3451 }
3452 
writeVarDeclaration(const VarDeclaration & varDecl,OutputStream & out)3453 void SPIRVCodeGenerator::writeVarDeclaration(const VarDeclaration& varDecl, OutputStream& out) {
3454     const Variable& var = varDecl.var();
3455     SpvId id = this->nextId(&var.type());
3456     fVariableMap[&var] = id;
3457     SpvId type = this->getPointerType(var.type(), SpvStorageClassFunction);
3458     this->writeInstruction(SpvOpVariable, type, id, SpvStorageClassFunction, fVariableBuffer);
3459     this->writeInstruction(SpvOpName, id, var.name(), fNameBuffer);
3460     if (varDecl.value()) {
3461         SpvId value = this->writeExpression(*varDecl.value(), out);
3462         this->writeInstruction(SpvOpStore, id, value, out);
3463     }
3464 }
3465 
writeStatement(const Statement & s,OutputStream & out)3466 void SPIRVCodeGenerator::writeStatement(const Statement& s, OutputStream& out) {
3467     switch (s.kind()) {
3468         case Statement::Kind::kInlineMarker:
3469         case Statement::Kind::kNop:
3470             break;
3471         case Statement::Kind::kBlock:
3472             this->writeBlock(s.as<Block>(), out);
3473             break;
3474         case Statement::Kind::kExpression:
3475             this->writeExpression(*s.as<ExpressionStatement>().expression(), out);
3476             break;
3477         case Statement::Kind::kReturn:
3478             this->writeReturnStatement(s.as<ReturnStatement>(), out);
3479             break;
3480         case Statement::Kind::kVarDeclaration:
3481             this->writeVarDeclaration(s.as<VarDeclaration>(), out);
3482             break;
3483         case Statement::Kind::kIf:
3484             this->writeIfStatement(s.as<IfStatement>(), out);
3485             break;
3486         case Statement::Kind::kFor:
3487             this->writeForStatement(s.as<ForStatement>(), out);
3488             break;
3489         case Statement::Kind::kDo:
3490             this->writeDoStatement(s.as<DoStatement>(), out);
3491             break;
3492         case Statement::Kind::kSwitch:
3493             this->writeSwitchStatement(s.as<SwitchStatement>(), out);
3494             break;
3495         case Statement::Kind::kBreak:
3496             this->writeInstruction(SpvOpBranch, fBreakTarget.top(), out);
3497             break;
3498         case Statement::Kind::kContinue:
3499             this->writeInstruction(SpvOpBranch, fContinueTarget.top(), out);
3500             break;
3501         case Statement::Kind::kDiscard:
3502             this->writeInstruction(SpvOpKill, out);
3503             break;
3504         default:
3505             SkDEBUGFAILF("unsupported statement: %s", s.description().c_str());
3506             break;
3507     }
3508 }
3509 
writeBlock(const Block & b,OutputStream & out)3510 void SPIRVCodeGenerator::writeBlock(const Block& b, OutputStream& out) {
3511     for (const std::unique_ptr<Statement>& stmt : b.children()) {
3512         this->writeStatement(*stmt, out);
3513     }
3514 }
3515 
writeIfStatement(const IfStatement & stmt,OutputStream & out)3516 void SPIRVCodeGenerator::writeIfStatement(const IfStatement& stmt, OutputStream& out) {
3517     SpvId test = this->writeExpression(*stmt.test(), out);
3518     SpvId ifTrue = this->nextId(nullptr);
3519     SpvId ifFalse = this->nextId(nullptr);
3520     if (stmt.ifFalse()) {
3521         SpvId end = this->nextId(nullptr);
3522         this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
3523         this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out);
3524         this->writeLabel(ifTrue, out);
3525         this->writeStatement(*stmt.ifTrue(), out);
3526         if (fCurrentBlock) {
3527             this->writeInstruction(SpvOpBranch, end, out);
3528         }
3529         this->writeLabel(ifFalse, out);
3530         this->writeStatement(*stmt.ifFalse(), out);
3531         if (fCurrentBlock) {
3532             this->writeInstruction(SpvOpBranch, end, out);
3533         }
3534         this->writeLabel(end, out);
3535     } else {
3536         this->writeInstruction(SpvOpSelectionMerge, ifFalse, SpvSelectionControlMaskNone, out);
3537         this->writeInstruction(SpvOpBranchConditional, test, ifTrue, ifFalse, out);
3538         this->writeLabel(ifTrue, out);
3539         this->writeStatement(*stmt.ifTrue(), out);
3540         if (fCurrentBlock) {
3541             this->writeInstruction(SpvOpBranch, ifFalse, out);
3542         }
3543         this->writeLabel(ifFalse, out);
3544     }
3545 }
3546 
writeForStatement(const ForStatement & f,OutputStream & out)3547 void SPIRVCodeGenerator::writeForStatement(const ForStatement& f, OutputStream& out) {
3548     if (f.initializer()) {
3549         this->writeStatement(*f.initializer(), out);
3550     }
3551     SpvId header = this->nextId(nullptr);
3552     SpvId start = this->nextId(nullptr);
3553     SpvId body = this->nextId(nullptr);
3554     SpvId next = this->nextId(nullptr);
3555     fContinueTarget.push(next);
3556     SpvId end = this->nextId(nullptr);
3557     fBreakTarget.push(end);
3558     this->writeInstruction(SpvOpBranch, header, out);
3559     this->writeLabel(header, out);
3560     this->writeInstruction(SpvOpLoopMerge, end, next, SpvLoopControlMaskNone, out);
3561     this->writeInstruction(SpvOpBranch, start, out);
3562     this->writeLabel(start, out);
3563     if (f.test()) {
3564         SpvId test = this->writeExpression(*f.test(), out);
3565         this->writeInstruction(SpvOpBranchConditional, test, body, end, out);
3566     } else {
3567         this->writeInstruction(SpvOpBranch, body, out);
3568     }
3569     this->writeLabel(body, out);
3570     this->writeStatement(*f.statement(), out);
3571     if (fCurrentBlock) {
3572         this->writeInstruction(SpvOpBranch, next, out);
3573     }
3574     this->writeLabel(next, out);
3575     if (f.next()) {
3576         this->writeExpression(*f.next(), out);
3577     }
3578     this->writeInstruction(SpvOpBranch, header, out);
3579     this->writeLabel(end, out);
3580     fBreakTarget.pop();
3581     fContinueTarget.pop();
3582 }
3583 
writeDoStatement(const DoStatement & d,OutputStream & out)3584 void SPIRVCodeGenerator::writeDoStatement(const DoStatement& d, OutputStream& out) {
3585     SpvId header = this->nextId(nullptr);
3586     SpvId start = this->nextId(nullptr);
3587     SpvId next = this->nextId(nullptr);
3588     SpvId continueTarget = this->nextId(nullptr);
3589     fContinueTarget.push(continueTarget);
3590     SpvId end = this->nextId(nullptr);
3591     fBreakTarget.push(end);
3592     this->writeInstruction(SpvOpBranch, header, out);
3593     this->writeLabel(header, out);
3594     this->writeInstruction(SpvOpLoopMerge, end, continueTarget, SpvLoopControlMaskNone, out);
3595     this->writeInstruction(SpvOpBranch, start, out);
3596     this->writeLabel(start, out);
3597     this->writeStatement(*d.statement(), out);
3598     if (fCurrentBlock) {
3599         this->writeInstruction(SpvOpBranch, next, out);
3600     }
3601     this->writeLabel(next, out);
3602     this->writeInstruction(SpvOpBranch, continueTarget, out);
3603     this->writeLabel(continueTarget, out);
3604     SpvId test = this->writeExpression(*d.test(), out);
3605     this->writeInstruction(SpvOpBranchConditional, test, header, end, out);
3606     this->writeLabel(end, out);
3607     fBreakTarget.pop();
3608     fContinueTarget.pop();
3609 }
3610 
writeSwitchStatement(const SwitchStatement & s,OutputStream & out)3611 void SPIRVCodeGenerator::writeSwitchStatement(const SwitchStatement& s, OutputStream& out) {
3612     SpvId value = this->writeExpression(*s.value(), out);
3613     std::vector<SpvId> labels;
3614     SpvId end = this->nextId(nullptr);
3615     SpvId defaultLabel = end;
3616     fBreakTarget.push(end);
3617     int size = 3;
3618     auto& cases = s.cases();
3619     for (const std::unique_ptr<Statement>& stmt : cases) {
3620         const SwitchCase& c = stmt->as<SwitchCase>();
3621         SpvId label = this->nextId(nullptr);
3622         labels.push_back(label);
3623         if (c.value()) {
3624             size += 2;
3625         } else {
3626             defaultLabel = label;
3627         }
3628     }
3629     labels.push_back(end);
3630     this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone, out);
3631     this->writeOpCode(SpvOpSwitch, size, out);
3632     this->writeWord(value, out);
3633     this->writeWord(defaultLabel, out);
3634     for (size_t i = 0; i < cases.size(); ++i) {
3635         const SwitchCase& c = cases[i]->as<SwitchCase>();
3636         if (!c.value()) {
3637             continue;
3638         }
3639         this->writeWord(c.value()->as<Literal>().intValue(), out);
3640         this->writeWord(labels[i], out);
3641     }
3642     for (size_t i = 0; i < cases.size(); ++i) {
3643         const SwitchCase& c = cases[i]->as<SwitchCase>();
3644         this->writeLabel(labels[i], out);
3645         this->writeStatement(*c.statement(), out);
3646         if (fCurrentBlock) {
3647             this->writeInstruction(SpvOpBranch, labels[i + 1], out);
3648         }
3649     }
3650     this->writeLabel(end, out);
3651     fBreakTarget.pop();
3652 }
3653 
writeReturnStatement(const ReturnStatement & r,OutputStream & out)3654 void SPIRVCodeGenerator::writeReturnStatement(const ReturnStatement& r, OutputStream& out) {
3655     if (r.expression()) {
3656         this->writeInstruction(SpvOpReturnValue, this->writeExpression(*r.expression(), out),
3657                                out);
3658     } else {
3659         this->writeInstruction(SpvOpReturn, out);
3660     }
3661 }
3662 
3663 // Given any function, returns the top-level symbol table (OUTSIDE of the function's scope).
get_top_level_symbol_table(const FunctionDeclaration & anyFunc)3664 static std::shared_ptr<SymbolTable> get_top_level_symbol_table(const FunctionDeclaration& anyFunc) {
3665     return anyFunc.definition()->body()->as<Block>().symbolTable()->fParent;
3666 }
3667 
writeEntrypointAdapter(const FunctionDeclaration & main)3668 SPIRVCodeGenerator::EntrypointAdapter SPIRVCodeGenerator::writeEntrypointAdapter(
3669         const FunctionDeclaration& main) {
3670     // Our goal is to synthesize a tiny helper function which looks like this:
3671     //     void _entrypoint() { sk_FragColor = main(); }
3672 
3673     // Fish a symbol table out of main().
3674     std::shared_ptr<SymbolTable> symbolTable = get_top_level_symbol_table(main);
3675 
3676     // Get `sk_FragColor` as a writable reference.
3677     const Symbol* skFragColorSymbol = (*symbolTable)["sk_FragColor"];
3678     SkASSERT(skFragColorSymbol);
3679     const Variable& skFragColorVar = skFragColorSymbol->as<Variable>();
3680     auto skFragColorRef = std::make_unique<VariableReference>(/*line=*/-1, &skFragColorVar,
3681                                                               VariableReference::RefKind::kWrite);
3682     // Synthesize a call to the `main()` function.
3683     if (main.returnType() != skFragColorRef->type()) {
3684         fContext.fErrors->error(main.fLine, "SPIR-V does not support returning '" +
3685                                             main.returnType().description() + "' from main()");
3686         return {};
3687     }
3688     ExpressionArray args;
3689     if (main.parameters().size() == 1) {
3690         if (main.parameters()[0]->type() != *fContext.fTypes.fFloat2) {
3691             fContext.fErrors->error(main.fLine,
3692                     "SPIR-V does not support parameter of type '" +
3693                     main.parameters()[0]->type().description() + "' to main()");
3694             return {};
3695         }
3696         args.push_back(dsl::Float2(0).release());
3697     }
3698     auto callMainFn = std::make_unique<FunctionCall>(/*line=*/-1, &main.returnType(), &main,
3699                                                      std::move(args));
3700 
3701     // Synthesize `skFragColor = main()` as a BinaryExpression.
3702     auto assignmentStmt = std::make_unique<ExpressionStatement>(std::make_unique<BinaryExpression>(
3703             /*line=*/-1,
3704             std::move(skFragColorRef),
3705             Token::Kind::TK_EQ,
3706             std::move(callMainFn),
3707             &main.returnType()));
3708 
3709     // Function bodies are always wrapped in a Block.
3710     StatementArray entrypointStmts;
3711     entrypointStmts.push_back(std::move(assignmentStmt));
3712     auto entrypointBlock = Block::Make(/*line=*/-1, std::move(entrypointStmts),
3713                                        symbolTable, /*isScope=*/true);
3714     // Declare an entrypoint function.
3715     EntrypointAdapter adapter;
3716     adapter.fLayout = {};
3717     adapter.fModifiers = Modifiers{adapter.fLayout, Modifiers::kHasSideEffects_Flag};
3718     adapter.entrypointDecl =
3719             std::make_unique<FunctionDeclaration>(/*line=*/-1,
3720                                                   &adapter.fModifiers,
3721                                                   "_entrypoint",
3722                                                   /*parameters=*/std::vector<const Variable*>{},
3723                                                   /*returnType=*/fContext.fTypes.fVoid.get(),
3724                                                   /*builtin=*/false);
3725     // Define it.
3726     adapter.entrypointDef = FunctionDefinition::Convert(fContext,
3727                                                         /*line=*/-1,
3728                                                         *adapter.entrypointDecl,
3729                                                         std::move(entrypointBlock),
3730                                                         /*builtin=*/false);
3731 
3732     adapter.entrypointDecl->setDefinition(adapter.entrypointDef.get());
3733     return adapter;
3734 }
3735 
writeUniformBuffer(std::shared_ptr<SymbolTable> topLevelSymbolTable)3736 void SPIRVCodeGenerator::writeUniformBuffer(std::shared_ptr<SymbolTable> topLevelSymbolTable) {
3737     SkASSERT(!fTopLevelUniforms.empty());
3738     static constexpr char kUniformBufferName[] = "_UniformBuffer";
3739 
3740     // Convert the list of top-level uniforms into a matching struct named _UniformBuffer, and build
3741     // a lookup table of variables to UniformBuffer field indices.
3742     std::vector<Type::Field> fields;
3743     fields.reserve(fTopLevelUniforms.size());
3744     fTopLevelUniformMap.reserve(fTopLevelUniforms.size());
3745     for (const VarDeclaration* topLevelUniform : fTopLevelUniforms) {
3746         const Variable* var = &topLevelUniform->var();
3747         fTopLevelUniformMap[var] = (int)fields.size();
3748         fields.emplace_back(var->modifiers(), var->name(), &var->type());
3749     }
3750     fUniformBuffer.fStruct = Type::MakeStructType(/*line=*/-1, kUniformBufferName,
3751                                                  std::move(fields));
3752 
3753     // Create a global variable to contain this struct.
3754     Layout layout;
3755     layout.fBinding = fProgram.fConfig->fSettings.fDefaultUniformBinding;
3756     layout.fSet     = fProgram.fConfig->fSettings.fDefaultUniformSet;
3757     Modifiers modifiers{layout, Modifiers::kUniform_Flag};
3758 
3759     fUniformBuffer.fInnerVariable = std::make_unique<Variable>(
3760             /*line=*/-1, fProgram.fModifiers->add(modifiers), kUniformBufferName,
3761             fUniformBuffer.fStruct.get(), /*builtin=*/false, Variable::Storage::kGlobal);
3762 
3763     // Create an interface block object for this global variable.
3764     fUniformBuffer.fInterfaceBlock = std::make_unique<InterfaceBlock>(
3765             /*offset=*/-1, *fUniformBuffer.fInnerVariable, kUniformBufferName,
3766             kUniformBufferName, /*arraySize=*/0, topLevelSymbolTable);
3767 
3768     // Generate an interface block and hold onto its ID.
3769     fUniformBufferId = this->writeInterfaceBlock(*fUniformBuffer.fInterfaceBlock);
3770 }
3771 
addRTFlipUniform(int line)3772 void SPIRVCodeGenerator::addRTFlipUniform(int line) {
3773     if (fWroteRTFlip) {
3774         return;
3775     }
3776     // Flip variable hasn't been written yet. This means we don't have an existing
3777     // interface block, so we're free to just synthesize one.
3778     fWroteRTFlip = true;
3779     std::vector<Type::Field> fields;
3780     if (fProgram.fConfig->fSettings.fRTFlipOffset < 0) {
3781         fContext.fErrors->error(line, "RTFlipOffset is negative");
3782     }
3783     fields.emplace_back(Modifiers(Layout(/*flags=*/0,
3784                                          /*location=*/-1,
3785                                          fProgram.fConfig->fSettings.fRTFlipOffset,
3786                                          /*binding=*/-1,
3787                                          /*index=*/-1,
3788                                          /*set=*/-1,
3789                                          /*builtin=*/-1,
3790                                          /*inputAttachmentIndex=*/-1),
3791                                   /*flags=*/0),
3792                         SKSL_RTFLIP_NAME,
3793                         fContext.fTypes.fFloat2.get());
3794     skstd::string_view name = "sksl_synthetic_uniforms";
3795     const Type* intfStruct =
3796             fSynthetics.takeOwnershipOfSymbol(Type::MakeStructType(/*line=*/-1, name, fields));
3797     int binding = fProgram.fConfig->fSettings.fRTFlipBinding;
3798     if (binding == -1) {
3799         fContext.fErrors->error(line, "layout(binding=...) is required in SPIR-V");
3800     }
3801     int set = fProgram.fConfig->fSettings.fRTFlipSet;
3802     if (set == -1) {
3803         fContext.fErrors->error(line, "layout(set=...) is required in SPIR-V");
3804     }
3805     bool usePushConstants = fProgram.fConfig->fSettings.fUsePushConstants;
3806     int flags = usePushConstants ? Layout::Flag::kPushConstant_Flag : 0;
3807     const Modifiers* modsPtr;
3808     {
3809         AutoAttachPoolToThread attach(fProgram.fPool.get());
3810         Modifiers modifiers(Layout(flags,
3811                                    /*location=*/-1,
3812                                    /*offset=*/-1,
3813                                    binding,
3814                                    /*index=*/-1,
3815                                    set,
3816                                    /*builtin=*/-1,
3817                                    /*inputAttachmentIndex=*/-1),
3818                             Modifiers::kUniform_Flag);
3819         modsPtr = fProgram.fModifiers->add(modifiers);
3820     }
3821     const Variable* intfVar = fSynthetics.takeOwnershipOfSymbol(
3822             std::make_unique<Variable>(/*line=*/-1,
3823                                        modsPtr,
3824                                        name,
3825                                        intfStruct,
3826                                        /*builtin=*/false,
3827                                        Variable::Storage::kGlobal));
3828     fSPIRVBonusVariables.insert(intfVar);
3829     {
3830         AutoAttachPoolToThread attach(fProgram.fPool.get());
3831         fProgram.fSymbols->add(std::make_unique<Field>(/*line=*/-1, intfVar, /*field=*/0));
3832     }
3833     InterfaceBlock intf(/*line=*/-1,
3834                         *intfVar,
3835                         name,
3836                         /*instanceName=*/"",
3837                         /*arraySize=*/0,
3838                         std::make_shared<SymbolTable>(fContext, /*builtin=*/false));
3839 
3840     this->writeInterfaceBlock(intf, false);
3841 }
3842 
writeInstructions(const Program & program,OutputStream & out)3843 void SPIRVCodeGenerator::writeInstructions(const Program& program, OutputStream& out) {
3844     fGLSLExtendedInstructions = this->nextId(nullptr);
3845     StringStream body;
3846     // Assign SpvIds to functions.
3847     const FunctionDeclaration* main = nullptr;
3848     for (const ProgramElement* e : program.elements()) {
3849         if (e->is<FunctionDefinition>()) {
3850             const FunctionDefinition& funcDef = e->as<FunctionDefinition>();
3851             const FunctionDeclaration& funcDecl = funcDef.declaration();
3852             fFunctionMap[&funcDecl] = this->nextId(nullptr);
3853             if (funcDecl.isMain()) {
3854                 main = &funcDecl;
3855             }
3856         }
3857     }
3858     // Make sure we have a main() function.
3859     if (!main) {
3860         fContext.fErrors->error(/*line=*/-1, "program does not contain a main() function");
3861         return;
3862     }
3863     // Emit interface blocks.
3864     std::set<SpvId> interfaceVars;
3865     for (const ProgramElement* e : program.elements()) {
3866         if (e->is<InterfaceBlock>()) {
3867             const InterfaceBlock& intf = e->as<InterfaceBlock>();
3868             SpvId id = this->writeInterfaceBlock(intf);
3869 
3870             const Modifiers& modifiers = intf.variable().modifiers();
3871             if ((modifiers.fFlags & (Modifiers::kIn_Flag | Modifiers::kOut_Flag)) &&
3872                 modifiers.fLayout.fBuiltin == -1 && !this->isDead(intf.variable())) {
3873                 interfaceVars.insert(id);
3874             }
3875         }
3876     }
3877     // Emit global variable declarations.
3878     for (const ProgramElement* e : program.elements()) {
3879         if (e->is<GlobalVarDeclaration>()) {
3880             this->writeGlobalVar(program.fConfig->fKind,
3881                                  e->as<GlobalVarDeclaration>().declaration()->as<VarDeclaration>());
3882         }
3883     }
3884     // Emit top-level uniforms into a dedicated uniform buffer.
3885     if (!fTopLevelUniforms.empty()) {
3886         this->writeUniformBuffer(get_top_level_symbol_table(*main));
3887     }
3888     // If main() returns a half4, synthesize a tiny entrypoint function which invokes the real
3889     // main() and stores the result into sk_FragColor.
3890     EntrypointAdapter adapter;
3891     if (main->returnType() == *fContext.fTypes.fHalf4) {
3892         adapter = this->writeEntrypointAdapter(*main);
3893         if (adapter.entrypointDecl) {
3894             fFunctionMap[adapter.entrypointDecl.get()] = this->nextId(nullptr);
3895             this->writeFunction(*adapter.entrypointDef, body);
3896             main = adapter.entrypointDecl.get();
3897         }
3898     }
3899     // Emit all the functions.
3900     for (const ProgramElement* e : program.elements()) {
3901         if (e->is<FunctionDefinition>()) {
3902             this->writeFunction(e->as<FunctionDefinition>(), body);
3903         }
3904     }
3905     // Add global in/out variables to the list of interface variables.
3906     for (auto entry : fVariableMap) {
3907         const Variable* var = entry.first;
3908         if (var->storage() == Variable::Storage::kGlobal &&
3909 #ifdef SKSL_EXT
3910             !(var->modifiers().fLayout.fFlags & Layout::Flag::kConstantId_Flag) &&
3911             ((var->modifiers().fFlags == Modifiers::Flag::kNo_Flag) ||
3912                 (var->modifiers().fFlags & (
3913                     Modifiers::Flag::kIn_Flag |
3914                     Modifiers::Flag::kOut_Flag |
3915                     Modifiers::Flag::kUniform_Flag |
3916                     Modifiers::Flag::kBuffer_Flag))) &&
3917 #else
3918             (var->modifiers().fFlags & (Modifiers::kIn_Flag | Modifiers::kOut_Flag)) &&
3919 #endif
3920             !this->isDead(*var)) {
3921             interfaceVars.insert(entry.second);
3922         }
3923     }
3924     this->writeCapabilities(out);
3925 #ifdef SKSL_EXT
3926     this->writeExtensions(out);
3927 #endif
3928     this->writeInstruction(SpvOpExtInstImport, fGLSLExtendedInstructions, "GLSL.std.450", out);
3929     this->writeInstruction(SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemoryModelGLSL450, out);
3930     this->writeOpCode(SpvOpEntryPoint, (SpvId) (3 + (main->name().length() + 4) / 4) +
3931                       (int32_t) interfaceVars.size(), out);
3932     switch (program.fConfig->fKind) {
3933         case ProgramKind::kVertex:
3934             this->writeWord(SpvExecutionModelVertex, out);
3935             break;
3936         case ProgramKind::kFragment:
3937             this->writeWord(SpvExecutionModelFragment, out);
3938             break;
3939         default:
3940             SK_ABORT("cannot write this kind of program to SPIR-V\n");
3941     }
3942     SpvId entryPoint = fFunctionMap[main];
3943     this->writeWord(entryPoint, out);
3944     this->writeString(main->name(), out);
3945     for (int var : interfaceVars) {
3946         this->writeWord(var, out);
3947     }
3948     if (program.fConfig->fKind == ProgramKind::kFragment) {
3949         this->writeInstruction(SpvOpExecutionMode,
3950                                fFunctionMap[main],
3951                                SpvExecutionModeOriginUpperLeft,
3952                                out);
3953     }
3954     for (const ProgramElement* e : program.elements()) {
3955         if (e->is<Extension>()) {
3956             this->writeInstruction(SpvOpSourceExtension, e->as<Extension>().name(), out);
3957         }
3958     }
3959 
3960     write_stringstream(fExtraGlobalsBuffer, out);
3961     write_stringstream(fNameBuffer, out);
3962     write_stringstream(fDecorationBuffer, out);
3963     write_stringstream(fConstantBuffer, out);
3964     write_stringstream(body, out);
3965 }
3966 
generateCode()3967 bool SPIRVCodeGenerator::generateCode() {
3968     SkASSERT(!fContext.fErrors->errorCount());
3969     this->writeWord(SpvMagicNumber, *fOut);
3970     this->writeWord(SpvVersion, *fOut);
3971     this->writeWord(SKSL_MAGIC, *fOut);
3972     StringStream buffer;
3973     this->writeInstructions(fProgram, buffer);
3974     this->writeWord(fIdCount, *fOut);
3975     this->writeWord(0, *fOut); // reserved, always zero
3976     write_stringstream(buffer, *fOut);
3977     fContext.fErrors->reportPendingErrors(PositionInfo());
3978     return fContext.fErrors->errorCount() == 0;
3979 }
3980 
3981 }  // namespace SkSL
3982