Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkSLSwizzle.h
Go to the documentation of this file.
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#ifndef SKSL_SWIZZLE
9#define SKSL_SWIZZLE
10
17
18#include <cstdint>
19#include <memory>
20#include <string>
21#include <string_view>
22#include <utility>
23
24namespace SkSL {
25
26class Context;
27enum class OperatorPrecedence : uint8_t;
28
29namespace SwizzleComponent {
30
31enum Type : int8_t {
32 X = 0, Y = 1, Z = 2, W = 3,
33 R = 4, G = 5, B = 6, A = 7,
34 S = 8, T = 9, P = 10, Q = 11,
35 UL = 12, UT = 13, UR = 14, UB = 15,
37 ONE
38};
39
40} // namespace SwizzleComponent
41
42/**
43 * Represents a vector swizzle operation such as 'float3(1, 2, 3).zyx'.
44 */
45class Swizzle final : public Expression {
46public:
47 inline static constexpr Kind kIRNodeKind = Kind::kSwizzle;
48
50
51 Swizzle(const Context& context, Position pos, std::unique_ptr<Expression> base,
54 &base->type().componentType().toCompound(context, components.size(), 1))
55 , fBase(std::move(base))
56 , fComponents(components) {
57 SkASSERT(this->components().size() >= 1 && this->components().size() <= 4);
58 }
59
60 // Swizzle::Convert permits component arrays containing ZERO or ONE, does typechecking, reports
61 // errors via ErrorReporter, and returns an expression that combines constructors and native
62 // swizzles (comprised solely of X/Y/W/Z).
63 static std::unique_ptr<Expression> Convert(const Context& context,
65 Position maskPos,
66 std::unique_ptr<Expression> base,
67 ComponentArray inComponents);
68
69 static std::unique_ptr<Expression> Convert(const Context& context,
71 Position maskPos,
72 std::unique_ptr<Expression> base,
73 std::string_view maskString);
74
75 // Swizzle::Make does not permit ZERO or ONE in the component array, just X/Y/Z/W; errors are
76 // reported via ASSERT.
77 static std::unique_ptr<Expression> Make(const Context& context,
79 std::unique_ptr<Expression> expr,
80 ComponentArray inComponents);
81
82 std::unique_ptr<Expression>& base() {
83 return fBase;
84 }
85
86 const std::unique_ptr<Expression>& base() const {
87 return fBase;
88 }
89
90 const ComponentArray& components() const {
91 return fComponents;
92 }
93
94 std::unique_ptr<Expression> clone(Position pos) const override {
95 return std::unique_ptr<Expression>(new Swizzle(pos, &this->type(), this->base()->clone(),
96 this->components()));
97 }
98
99 std::string description(OperatorPrecedence) const override;
100
101 // Converts an array of swizzle components into a string.
102 static std::string MaskString(const ComponentArray& inComponents);
103
104private:
105 Swizzle(Position pos, const Type* type, std::unique_ptr<Expression> base,
107 : INHERITED(pos, kIRNodeKind, type)
108 , fBase(std::move(base))
109 , fComponents(components) {
110 SkASSERT(this->components().size() >= 1 && this->components().size() <= 4);
111 }
112
113 std::unique_ptr<Expression> fBase;
114 ComponentArray fComponents;
115
116 using INHERITED = Expression;
117};
118
119} // namespace SkSL
120
121#endif
SkPoint pos
#define SkASSERT(cond)
Definition SkAssert.h:116
virtual const Type & type() const
std::unique_ptr< Expression > clone() const
std::string description() const final
Swizzle(const Context &context, Position pos, std::unique_ptr< Expression > base, const ComponentArray &components)
Definition SkSLSwizzle.h:51
std::unique_ptr< Expression > & base()
Definition SkSLSwizzle.h:82
std::unique_ptr< Expression > clone(Position pos) const override
Definition SkSLSwizzle.h:94
const std::unique_ptr< Expression > & base() const
Definition SkSLSwizzle.h:86
static constexpr Kind kIRNodeKind
Definition SkSLSwizzle.h:47
static std::unique_ptr< Expression > Convert(const Context &context, Position pos, Position maskPos, std::unique_ptr< Expression > base, ComponentArray inComponents)
const ComponentArray & components() const
Definition SkSLSwizzle.h:90
static std::unique_ptr< Expression > Make(const Context &context, Position pos, std::unique_ptr< Expression > expr, ComponentArray inComponents)
static std::string MaskString(const ComponentArray &inComponents)
skia_private::STArray< 4, int8_t > ComponentArray
Definition SkSLDefines.h:24
OperatorPrecedence
ExpressionKind
Definition SkSLIRNode.h:62
Definition ref_ptr.h:256
Definition SkMD5.cpp:125