1 /*
2 * Copyright 2021 Google LLC
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/ir/SkSLSwizzle.h"
9
10 #include "include/sksl/SkSLErrorReporter.h"
11 #include "src/sksl/SkSLAnalysis.h"
12 #include "src/sksl/SkSLConstantFolder.h"
13 #include "src/sksl/SkSLProgramSettings.h"
14 #include "src/sksl/ir/SkSLConstructor.h"
15 #include "src/sksl/ir/SkSLConstructorScalarCast.h"
16 #include "src/sksl/ir/SkSLConstructorSplat.h"
17 #include "src/sksl/ir/SkSLLiteral.h"
18
19 #include <optional>
20
21 namespace SkSL {
22
validate_swizzle_domain(const ComponentArray & fields)23 static bool validate_swizzle_domain(const ComponentArray& fields) {
24 enum SwizzleDomain {
25 kCoordinate,
26 kColor,
27 kUV,
28 kRectangle,
29 };
30
31 std::optional<SwizzleDomain> domain;
32
33 for (int8_t field : fields) {
34 SwizzleDomain fieldDomain;
35 switch (field) {
36 case SwizzleComponent::X:
37 case SwizzleComponent::Y:
38 case SwizzleComponent::Z:
39 case SwizzleComponent::W:
40 fieldDomain = kCoordinate;
41 break;
42 case SwizzleComponent::R:
43 case SwizzleComponent::G:
44 case SwizzleComponent::B:
45 case SwizzleComponent::A:
46 fieldDomain = kColor;
47 break;
48 case SwizzleComponent::S:
49 case SwizzleComponent::T:
50 case SwizzleComponent::P:
51 case SwizzleComponent::Q:
52 fieldDomain = kUV;
53 break;
54 case SwizzleComponent::UL:
55 case SwizzleComponent::UT:
56 case SwizzleComponent::UR:
57 case SwizzleComponent::UB:
58 fieldDomain = kRectangle;
59 break;
60 case SwizzleComponent::ZERO:
61 case SwizzleComponent::ONE:
62 continue;
63 default:
64 return false;
65 }
66
67 if (!domain.has_value()) {
68 domain = fieldDomain;
69 } else if (domain != fieldDomain) {
70 return false;
71 }
72 }
73
74 return true;
75 }
76
mask_char(int8_t component)77 static char mask_char(int8_t component) {
78 switch (component) {
79 case SwizzleComponent::X: return 'x';
80 case SwizzleComponent::Y: return 'y';
81 case SwizzleComponent::Z: return 'z';
82 case SwizzleComponent::W: return 'w';
83 case SwizzleComponent::R: return 'r';
84 case SwizzleComponent::G: return 'g';
85 case SwizzleComponent::B: return 'b';
86 case SwizzleComponent::A: return 'a';
87 case SwizzleComponent::S: return 's';
88 case SwizzleComponent::T: return 't';
89 case SwizzleComponent::P: return 'p';
90 case SwizzleComponent::Q: return 'q';
91 case SwizzleComponent::UL: return 'L';
92 case SwizzleComponent::UT: return 'T';
93 case SwizzleComponent::UR: return 'R';
94 case SwizzleComponent::UB: return 'B';
95 case SwizzleComponent::ZERO: return '0';
96 case SwizzleComponent::ONE: return '1';
97 default: SkUNREACHABLE;
98 }
99 }
100
mask_string(const ComponentArray & components)101 static std::string mask_string(const ComponentArray& components) {
102 std::string result;
103 for (int8_t component : components) {
104 result += mask_char(component);
105 }
106 return result;
107 }
108
optimize_constructor_swizzle(const Context & context,const AnyConstructor & base,ComponentArray components)109 static std::unique_ptr<Expression> optimize_constructor_swizzle(const Context& context,
110 const AnyConstructor& base,
111 ComponentArray components) {
112 auto baseArguments = base.argumentSpan();
113 std::unique_ptr<Expression> replacement;
114 const Type& exprType = base.type();
115 const Type& componentType = exprType.componentType();
116 int swizzleSize = components.size();
117
118 // Swizzles can duplicate some elements and discard others, e.g.
119 // `half4(1, 2, 3, 4).xxz` --> `half3(1, 1, 3)`. However, there are constraints:
120 // - Expressions with side effects need to occur exactly once, even if they would otherwise be
121 // swizzle-eliminated
122 // - Non-trivial expressions should not be repeated, but elimination is OK.
123 //
124 // Look up the argument for the constructor at each index. This is typically simple but for
125 // weird cases like `half4(bar.yz, half2(foo))`, it can be harder than it seems. This example
126 // would result in:
127 // argMap[0] = {.fArgIndex = 0, .fComponent = 0} (bar.yz .x)
128 // argMap[1] = {.fArgIndex = 0, .fComponent = 1} (bar.yz .y)
129 // argMap[2] = {.fArgIndex = 1, .fComponent = 0} (half2(foo) .x)
130 // argMap[3] = {.fArgIndex = 1, .fComponent = 1} (half2(foo) .y)
131 struct ConstructorArgMap {
132 int8_t fArgIndex;
133 int8_t fComponent;
134 };
135
136 int numConstructorArgs = base.type().columns();
137 ConstructorArgMap argMap[4] = {};
138 int writeIdx = 0;
139 for (int argIdx = 0; argIdx < (int)baseArguments.size(); ++argIdx) {
140 const Expression& arg = *baseArguments[argIdx];
141 const Type& argType = arg.type();
142
143 if (!argType.isScalar() && !argType.isVector()) {
144 return nullptr;
145 }
146
147 int argSlots = argType.slotCount();
148 for (int componentIdx = 0; componentIdx < argSlots; ++componentIdx) {
149 argMap[writeIdx].fArgIndex = argIdx;
150 argMap[writeIdx].fComponent = componentIdx;
151 ++writeIdx;
152 }
153 }
154 SkASSERT(writeIdx == numConstructorArgs);
155
156 // Count up the number of times each constructor argument is used by the swizzle.
157 // `half4(bar.yz, half2(foo)).xwxy` -> { 3, 1 }
158 // - bar.yz is referenced 3 times, by `.x_xy`
159 // - half(foo) is referenced 1 time, by `._w__`
160 int8_t exprUsed[4] = {};
161 for (int8_t c : components) {
162 exprUsed[argMap[c].fArgIndex]++;
163 }
164
165 for (int index = 0; index < numConstructorArgs; ++index) {
166 int8_t constructorArgIndex = argMap[index].fArgIndex;
167 const Expression& baseArg = *baseArguments[constructorArgIndex];
168
169 // Check that non-trivial expressions are not swizzled in more than once.
170 if (exprUsed[constructorArgIndex] > 1 && !Analysis::IsTrivialExpression(baseArg)) {
171 return nullptr;
172 }
173 // Check that side-effect-bearing expressions are swizzled in exactly once.
174 if (exprUsed[constructorArgIndex] != 1 && baseArg.hasSideEffects()) {
175 return nullptr;
176 }
177 }
178
179 struct ReorderedArgument {
180 int8_t fArgIndex;
181 ComponentArray fComponents;
182 };
183 SkSTArray<4, ReorderedArgument> reorderedArgs;
184 for (int8_t c : components) {
185 const ConstructorArgMap& argument = argMap[c];
186 const Expression& baseArg = *baseArguments[argument.fArgIndex];
187
188 if (baseArg.type().isScalar()) {
189 // This argument is a scalar; add it to the list as-is.
190 SkASSERT(argument.fComponent == 0);
191 reorderedArgs.push_back({argument.fArgIndex,
192 ComponentArray{}});
193 } else {
194 // This argument is a component from a vector.
195 SkASSERT(baseArg.type().isVector());
196 SkASSERT(argument.fComponent < baseArg.type().columns());
197 if (reorderedArgs.empty() ||
198 reorderedArgs.back().fArgIndex != argument.fArgIndex) {
199 // This can't be combined with the previous argument. Add a new one.
200 reorderedArgs.push_back({argument.fArgIndex,
201 ComponentArray{argument.fComponent}});
202 } else {
203 // Since we know this argument uses components, it should already have at least one
204 // component set.
205 SkASSERT(!reorderedArgs.back().fComponents.empty());
206 // Build up the current argument with one more component.
207 reorderedArgs.back().fComponents.push_back(argument.fComponent);
208 }
209 }
210 }
211
212 // Convert our reordered argument list to an actual array of expressions, with the new order and
213 // any new inner swizzles that need to be applied.
214 ExpressionArray newArgs;
215 newArgs.reserve_back(swizzleSize);
216 for (const ReorderedArgument& reorderedArg : reorderedArgs) {
217 std::unique_ptr<Expression> newArg =
218 baseArguments[reorderedArg.fArgIndex]->clone();
219
220 if (reorderedArg.fComponents.empty()) {
221 newArgs.push_back(std::move(newArg));
222 } else {
223 newArgs.push_back(Swizzle::Make(context, std::move(newArg),
224 reorderedArg.fComponents));
225 }
226 }
227
228 // Wrap the new argument list in a constructor.
229 return Constructor::Convert(context,
230 base.fLine,
231 componentType.toCompound(context, swizzleSize, /*rows=*/1),
232 std::move(newArgs));
233 }
234
Convert(const Context & context,std::unique_ptr<Expression> base,std::string_view maskString)235 std::unique_ptr<Expression> Swizzle::Convert(const Context& context,
236 std::unique_ptr<Expression> base,
237 std::string_view maskString) {
238 ComponentArray components;
239 for (char field : maskString) {
240 switch (field) {
241 case '0': components.push_back(SwizzleComponent::ZERO); break;
242 case '1': components.push_back(SwizzleComponent::ONE); break;
243 case 'x': components.push_back(SwizzleComponent::X); break;
244 case 'r': components.push_back(SwizzleComponent::R); break;
245 case 's': components.push_back(SwizzleComponent::S); break;
246 case 'L': components.push_back(SwizzleComponent::UL); break;
247 case 'y': components.push_back(SwizzleComponent::Y); break;
248 case 'g': components.push_back(SwizzleComponent::G); break;
249 case 't': components.push_back(SwizzleComponent::T); break;
250 case 'T': components.push_back(SwizzleComponent::UT); break;
251 case 'z': components.push_back(SwizzleComponent::Z); break;
252 case 'b': components.push_back(SwizzleComponent::B); break;
253 case 'p': components.push_back(SwizzleComponent::P); break;
254 case 'R': components.push_back(SwizzleComponent::UR); break;
255 case 'w': components.push_back(SwizzleComponent::W); break;
256 case 'a': components.push_back(SwizzleComponent::A); break;
257 case 'q': components.push_back(SwizzleComponent::Q); break;
258 case 'B': components.push_back(SwizzleComponent::UB); break;
259 default:
260 context.fErrors->error(base->fLine,
261 String::printf("invalid swizzle component '%c'", field));
262 return nullptr;
263 }
264 }
265 return Convert(context, std::move(base), std::move(components));
266 }
267
268 // Swizzles are complicated due to constant components. The most difficult case is a mask like
269 // '.x1w0'. A naive approach might turn that into 'float4(base.x, 1, base.w, 0)', but that evaluates
270 // 'base' twice. We instead group the swizzle mask ('xw') and constants ('1, 0') together and use a
271 // secondary swizzle to put them back into the right order, so in this case we end up with
272 // 'float4(base.xw, 1, 0).xzyw'.
Convert(const Context & context,std::unique_ptr<Expression> base,ComponentArray inComponents)273 std::unique_ptr<Expression> Swizzle::Convert(const Context& context,
274 std::unique_ptr<Expression> base,
275 ComponentArray inComponents) {
276 if (!validate_swizzle_domain(inComponents)) {
277 context.fErrors->error(base->fLine,
278 "invalid swizzle mask '" + mask_string(inComponents) + "'");
279 return nullptr;
280 }
281
282 const int line = base->fLine;
283 const Type& baseType = base->type();
284
285 if (!baseType.isVector() && !baseType.isScalar()) {
286 context.fErrors->error(
287 line, "cannot swizzle value of type '" + baseType.displayName() + "'");
288 return nullptr;
289 }
290
291 if (inComponents.count() > 4) {
292 context.fErrors->error(line,
293 "too many components in swizzle mask '" + mask_string(inComponents) + "'");
294 return nullptr;
295 }
296
297 ComponentArray maskComponents;
298 bool foundXYZW = false;
299 for (int i = 0; i < inComponents.count(); ++i) {
300 switch (inComponents[i]) {
301 case SwizzleComponent::ZERO:
302 case SwizzleComponent::ONE:
303 // Skip over constant fields for now.
304 break;
305 case SwizzleComponent::X:
306 case SwizzleComponent::R:
307 case SwizzleComponent::S:
308 case SwizzleComponent::UL:
309 foundXYZW = true;
310 maskComponents.push_back(SwizzleComponent::X);
311 break;
312 case SwizzleComponent::Y:
313 case SwizzleComponent::G:
314 case SwizzleComponent::T:
315 case SwizzleComponent::UT:
316 foundXYZW = true;
317 if (baseType.columns() >= 2) {
318 maskComponents.push_back(SwizzleComponent::Y);
319 break;
320 }
321 [[fallthrough]];
322 case SwizzleComponent::Z:
323 case SwizzleComponent::B:
324 case SwizzleComponent::P:
325 case SwizzleComponent::UR:
326 foundXYZW = true;
327 if (baseType.columns() >= 3) {
328 maskComponents.push_back(SwizzleComponent::Z);
329 break;
330 }
331 [[fallthrough]];
332 case SwizzleComponent::W:
333 case SwizzleComponent::A:
334 case SwizzleComponent::Q:
335 case SwizzleComponent::UB:
336 foundXYZW = true;
337 if (baseType.columns() >= 4) {
338 maskComponents.push_back(SwizzleComponent::W);
339 break;
340 }
341 [[fallthrough]];
342 default:
343 // The swizzle component references a field that doesn't exist in the base type.
344 context.fErrors->error(line, String::printf("invalid swizzle component '%c'",
345 mask_char(inComponents[i])));
346 return nullptr;
347 }
348 }
349
350 if (!foundXYZW) {
351 context.fErrors->error(line, "swizzle must refer to base expression");
352 return nullptr;
353 }
354
355 // Coerce literals in expressions such as `(12345).xxx` to their actual type.
356 base = baseType.scalarTypeForLiteral().coerceExpression(std::move(base), context);
357 if (!base) {
358 return nullptr;
359 }
360
361 // First, we need a vector expression that is the non-constant portion of the swizzle, packed:
362 // scalar.xxx -> type3(scalar)
363 // scalar.x0x0 -> type2(scalar)
364 // vector.zyx -> vector.zyx
365 // vector.x0y0 -> vector.xy
366 std::unique_ptr<Expression> expr = Swizzle::Make(context, std::move(base), maskComponents);
367
368 // If we have processed the entire swizzle, we're done.
369 if (maskComponents.count() == inComponents.count()) {
370 return expr;
371 }
372
373 // Now we create a constructor that has the correct number of elements for the final swizzle,
374 // with all fields at the start. It's not finished yet; constants we need will be added below.
375 // scalar.x0x0 -> type4(type2(x), ...)
376 // vector.y111 -> type4(vector.y, ...)
377 // vector.z10x -> type4(vector.zx, ...)
378 //
379 // The constructor will have at most three arguments: { base expr, constant 0, constant 1 }
380 ExpressionArray constructorArgs;
381 constructorArgs.reserve_back(3);
382 constructorArgs.push_back(std::move(expr));
383
384 // Apply another swizzle to shuffle the constants into the correct place. Any constant values we
385 // need are also tacked on to the end of the constructor.
386 // scalar.x0x0 -> type4(type2(x), 0).xyxy
387 // vector.y111 -> type4(vector.y, 1).xyyy
388 // vector.z10x -> type4(vector.zx, 1, 0).xzwy
389 const Type* scalarType = &baseType.componentType();
390 ComponentArray swizzleComponents;
391 int maskFieldIdx = 0;
392 int constantFieldIdx = maskComponents.size();
393 int constantZeroIdx = -1, constantOneIdx = -1;
394
395 for (int i = 0; i < inComponents.count(); i++) {
396 switch (inComponents[i]) {
397 case SwizzleComponent::ZERO:
398 if (constantZeroIdx == -1) {
399 // Synthesize a 'type(0)' argument at the end of the constructor.
400 constructorArgs.push_back(ConstructorScalarCast::Make(
401 context, line, *scalarType,
402 Literal::MakeInt(context, line, /*value=*/0)));
403 constantZeroIdx = constantFieldIdx++;
404 }
405 swizzleComponents.push_back(constantZeroIdx);
406 break;
407 case SwizzleComponent::ONE:
408 if (constantOneIdx == -1) {
409 // Synthesize a 'type(1)' argument at the end of the constructor.
410 constructorArgs.push_back(ConstructorScalarCast::Make(
411 context, line, *scalarType,
412 Literal::MakeInt(context, line, /*value=*/1)));
413 constantOneIdx = constantFieldIdx++;
414 }
415 swizzleComponents.push_back(constantOneIdx);
416 break;
417 default:
418 // The non-constant fields are already in the expected order.
419 swizzleComponents.push_back(maskFieldIdx++);
420 break;
421 }
422 }
423
424 expr = Constructor::Convert(context, line,
425 scalarType->toCompound(context, constantFieldIdx, /*rows=*/1),
426 std::move(constructorArgs));
427 if (!expr) {
428 return nullptr;
429 }
430
431 return Swizzle::Make(context, std::move(expr), swizzleComponents);
432 }
433
Make(const Context & context,std::unique_ptr<Expression> expr,ComponentArray components)434 std::unique_ptr<Expression> Swizzle::Make(const Context& context,
435 std::unique_ptr<Expression> expr,
436 ComponentArray components) {
437 const Type& exprType = expr->type();
438 SkASSERTF(exprType.isVector() || exprType.isScalar(),
439 "cannot swizzle type '%s'", exprType.description().c_str());
440 SkASSERT(components.count() >= 1 && components.count() <= 4);
441
442 // Confirm that the component array only contains X/Y/Z/W. (Call MakeWith01 if you want support
443 // for ZERO and ONE. Once initial IR generation is complete, no swizzles should have zeros or
444 // ones in them.)
445 SkASSERT(std::all_of(components.begin(), components.end(), [](int8_t component) {
446 return component >= SwizzleComponent::X &&
447 component <= SwizzleComponent::W;
448 }));
449
450 // SkSL supports splatting a scalar via `scalar.xxxx`, but not all versions of GLSL allow this.
451 // Replace swizzles with equivalent splat constructors (`scalar.xxx` --> `half3(value)`).
452 if (exprType.isScalar()) {
453 int line = expr->fLine;
454 return ConstructorSplat::Make(context, line,
455 exprType.toCompound(context, components.size(), /*rows=*/1),
456 std::move(expr));
457 }
458
459 // Detect identity swizzles like `color.rgba` and return the base-expression as-is.
460 if (components.count() == exprType.columns()) {
461 bool identity = true;
462 for (int i = 0; i < components.count(); ++i) {
463 if (components[i] != i) {
464 identity = false;
465 break;
466 }
467 }
468 if (identity) {
469 return expr;
470 }
471 }
472
473 // Optimize swizzles of swizzles, e.g. replace `foo.argb.rggg` with `foo.arrr`.
474 if (expr->is<Swizzle>()) {
475 Swizzle& base = expr->as<Swizzle>();
476 ComponentArray combined;
477 for (int8_t c : components) {
478 combined.push_back(base.components()[c]);
479 }
480
481 // It may actually be possible to further simplify this swizzle. Go again.
482 // (e.g. `color.abgr.abgr` --> `color.rgba` --> `color`.)
483 return Swizzle::Make(context, std::move(base.base()), combined);
484 }
485
486 // If we are swizzling a constant expression, we can use its value instead here (so that
487 // swizzles like `colorWhite.x` can be simplified to `1`).
488 const Expression* value = ConstantFolder::GetConstantValueForVariable(*expr);
489
490 // `half4(scalar).zyy` can be optimized to `half3(scalar)`, and `half3(scalar).y` can be
491 // optimized to just `scalar`. The swizzle components don't actually matter, as every field
492 // in a splat constructor holds the same value.
493 if (value->is<ConstructorSplat>()) {
494 const ConstructorSplat& splat = value->as<ConstructorSplat>();
495 return ConstructorSplat::Make(
496 context, splat.fLine,
497 splat.type().componentType().toCompound(context, components.size(), /*rows=*/1),
498 splat.argument()->clone());
499 }
500
501 // Optimize swizzles of constructors.
502 if (value->isAnyConstructor()) {
503 const AnyConstructor& ctor = value->asAnyConstructor();
504 if (auto replacement = optimize_constructor_swizzle(context, ctor, components)) {
505 return replacement;
506 }
507 }
508
509 // The swizzle could not be simplified, so apply the requested swizzle to the base expression.
510 return std::make_unique<Swizzle>(context, std::move(expr), components);
511 }
512
513 } // namespace SkSL
514