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