Flutter Engine
The Flutter Engine
SkSLRewriteIndexedSwizzle.cpp
Go to the documentation of this file.
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
16
17#include <memory>
18#include <utility>
19
20namespace SkSL {
21
22std::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]]`.
48 context, indexExpr.fPosition, swizzle.base()->clone(), std::move(innerExpr));
49}
50
51} // namespace SkSL
const std::unique_ptr< Type > fInt
static std::unique_ptr< Expression > MakeFromConstants(const Context &context, Position pos, const Type &type, const double values[])
const BuiltinTypes & fTypes
Definition: SkSLContext.h:30
Position fPosition
Definition: SkSLIRNode.h:109
static std::unique_ptr< Expression > Make(const Context &context, Position pos, std::unique_ptr< Expression > base, std::unique_ptr< Expression > index)
std::unique_ptr< Expression > & base()
std::unique_ptr< Expression > & index()
std::unique_ptr< Expression > & base()
Definition: SkSLSwizzle.h:82
const ComponentArray & components() const
Definition: SkSLSwizzle.h:90
std::unique_ptr< Expression > RewriteIndexedSwizzle(const Context &context, const IndexExpression &swizzle)