Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
18
19#include <memory>
20#include <utility>
21
22namespace SkSL {
23
24std::unique_ptr<Expression> Transform::RewriteIndexedSwizzle(const Context& context,
25 const IndexExpression& indexExpr) {
26 // The index expression _must_ have a swizzle base for this transformation to be valid.
27 if (!indexExpr.base()->is<Swizzle>()) {
28 return nullptr;
29 }
30 const Swizzle& swizzle = indexExpr.base()->as<Swizzle>();
31
32 // Convert the swizzle components to a literal array.
33 double vecArray[4];
34 for (int index = 0; index < swizzle.components().size(); ++index) {
35 vecArray[index] = swizzle.components()[index];
36 }
37
38 // Make a compound constructor with the literal array.
39 const Type& vecType =
40 context.fTypes.fInt->toCompound(context, swizzle.components().size(), /*rows=*/1);
41 std::unique_ptr<Expression> vec =
42 ConstructorCompound::MakeFromConstants(context, indexExpr.fPosition, vecType, vecArray);
43
44 // Create a rewritten inner-expression corresponding to `vec(1,2,3)[originalIndex]`.
45 std::unique_ptr<Expression> innerExpr = IndexExpression::Make(
46 context, indexExpr.fPosition, std::move(vec), indexExpr.index()->clone());
47
48 // Return a rewritten outer-expression corresponding to `base[vec(1,2,3)[originalIndex]]`.
50 context, indexExpr.fPosition, swizzle.base()->clone(), std::move(innerExpr));
51}
52
53} // 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
int size() const
Definition SkTArray.h:416
std::unique_ptr< Expression > RewriteIndexedSwizzle(const Context &context, const IndexExpression &swizzle)