1 /*
2 * Copyright 2023 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/SkSLBuiltinTypes.h"
9 #include "src/sksl/SkSLContext.h"
10 #include "src/sksl/ir/SkSLConstructorCompound.h"
11 #include "src/sksl/ir/SkSLExpression.h"
12 #include "src/sksl/ir/SkSLIndexExpression.h"
13 #include "src/sksl/ir/SkSLSwizzle.h"
14 #include "src/sksl/ir/SkSLType.h"
15 #include "src/sksl/transform/SkSLTransform.h"
16
17 #include <memory>
18 #include <utility>
19
20 namespace SkSL {
21
RewriteIndexedSwizzle(const Context & context,const IndexExpression & indexExpr)22 std::unique_ptr<Expression> Transform::RewriteIndexedSwizzle(const Context& context,
23 const IndexExpression& indexExpr) {
24 // The index expression _must_ have a swizzle base for this transformation to be valid.
25 if (!indexExpr.base()->is<Swizzle>()) {
26 return nullptr;
27 }
28 const Swizzle& swizzle = indexExpr.base()->as<Swizzle>();
29
30 // Convert the swizzle components to a literal array.
31 double vecArray[4];
32 for (int index = 0; index < swizzle.components().size(); ++index) {
33 vecArray[index] = swizzle.components()[index];
34 }
35
36 // Make a compound constructor with the literal array.
37 const Type& vecType =
38 context.fTypes.fInt->toCompound(context, swizzle.components().size(), /*rows=*/1);
39 std::unique_ptr<Expression> vec =
40 ConstructorCompound::MakeFromConstants(context, indexExpr.fPosition, vecType, vecArray);
41
42 // Create a rewritten inner-expression corresponding to `vec(1,2,3)[originalIndex]`.
43 std::unique_ptr<Expression> innerExpr = IndexExpression::Make(
44 context, indexExpr.fPosition, std::move(vec), indexExpr.index()->clone());
45
46 // Return a rewritten outer-expression corresponding to `base[vec(1,2,3)[originalIndex]]`.
47 return IndexExpression::Make(
48 context, indexExpr.fPosition, swizzle.base()->clone(), std::move(innerExpr));
49 }
50
51 } // namespace SkSL
52