| 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 |
|
| 11 |
#include "include/core/SkTypes.h" |
| 12 |
#include "src/base/SkFixedArray.h" |
| 13 |
#include "src/sksl/SkSLPosition.h" |
| 14 |
#include "src/sksl/ir/SkSLExpression.h" |
| 15 |
#include "src/sksl/ir/SkSLIRNode.h" |
| 16 |
#include "src/sksl/ir/SkSLType.h" |
| 17 |
|
| 18 |
#include <cstdint> |
| 19 |
#include <initializer_list> |
| 20 |
#include <memory> |
| 21 |
#include <string> |
| 22 |
#include <string_view> |
| 23 |
#include <utility> |
| 24 |
|
| 25 |
namespace SkSL { |
| 26 |
|
| 27 |
class Context; |
| 28 |
enum class OperatorPrecedence : uint8_t; |
| 29 |
|
| 30 |
namespace SwizzleComponent { |
| 31 |
|
| 32 |
enum Type : int8_t { |
| 33 |
X = 0, Y = 1, Z = 2, W = 3, |
| 34 |
R = 4, G = 5, B = 6, A = 7, |
| 35 |
S = 8, T = 9, P = 10, Q = 11, |
| 36 |
UL = 12, UT = 13, UR = 14, UB = 15, |
| 37 |
ZERO, |
| 38 |
ONE |
| 39 |
}; |
| 40 |
|
| 41 |
} // namespace SwizzleComponent |
| 42 |
|
| 43 |
/** |
| 44 |
* Represents the components of a vector swizzle. |
| 45 |
*/ |
| 46 |
using ComponentArray = skia_private::FixedArray<4, int8_t>; |
| 47 |
|
| 48 |
/** |
| 49 |
* Represents a vector swizzle operation such as 'float3(1, 2, 3).zyx'. |
| 50 |
*/ |
| 51 |
class Swizzle final : public Expression { |
| 52 |
public: |
| 53 |
inline static constexpr Kind kIRNodeKind = Kind::kSwizzle; |
| 54 |
|
| 55 |
using Component = SwizzleComponent::Type; |
| 56 |
|
| 57 |
Swizzle(const Context& context, Position pos, std::unique_ptr<Expression> base, |
| 58 |
const ComponentArray& components) |
| 59 |
: INHERITED(pos, kIRNodeKind, |
| 60 |
&base->type().componentType().toCompound(context, components.size(), 1)) |
| 61 |
, fBase(std::move(base)) |
| 62 |
, fComponents(components) { |
| 63 |
SkASSERT(this->components().size() >= 1 && this->components().size() <= 4); |
| 64 |
} |
| 65 |
|
| 66 |
// Swizzle::Convert permits component strings containing ZERO or ONE, does typechecking, reports |
| 67 |
// errors via ErrorReporter, and returns an expression that combines constructors and native |
| 68 |
// swizzles (comprised solely of X/Y/W/Z). |
| 69 |
static std::unique_ptr<Expression> Convert(const Context& context, |
| 70 |
Position pos, |
| 71 |
Position maskPos, |
| 72 |
std::unique_ptr<Expression> base, |
| 73 |
std::string_view componentString); |
| 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, |
| 78 |
Position pos, |
| 79 |
std::unique_ptr<Expression> expr, |
| 80 |
ComponentArray components); |
| 81 |
|
| 82 |
// Swizzle::MakeExact creates the swizzle as requested, without simplification. |
| 83 |
static std::unique_ptr<Expression> MakeExact(const Context& context, |
| 84 |
Position pos, |
| 85 |
std::unique_ptr<Expression> expr, |
| 86 |
ComponentArray components); |
| 87 |
|
| 88 |
std::unique_ptr<Expression>& base() { |
| 89 |
return fBase; |
| 90 |
} |
| 91 |
|
| 92 |
const std::unique_ptr<Expression>& base() const { |
| 93 |
return fBase; |
| 94 |
} |
| 95 |
|
| 96 |
const ComponentArray& components() const { |
| 97 |
return fComponents; |
| 98 |
} |
| 99 |
|
| 100 |
std::unique_ptr<Expression> clone(Position pos) const override { |
| 101 |
return std::unique_ptr<Expression>(new Swizzle(pos, &this->type(), this->base()->clone(), |
| 102 |
this->components())); |
| 103 |
} |
| 104 |
|
| 105 |
std::string description(OperatorPrecedence) const override; |
| 106 |
|
| 107 |
// Converts an array of swizzle components into a string. |
| 108 |
static std::string MaskString(const ComponentArray& inComponents); |
| 109 |
|
| 110 |
// Returns true if a swizzle keeps each component in its original position. |
| 111 |
static bool IsIdentity(const ComponentArray& components); |
| 112 |
|
| 113 |
private: |
| 114 |
Swizzle(Position pos, const Type* type, std::unique_ptr<Expression> base, |
| 115 |
const ComponentArray& components) |
| 116 |
: INHERITED(pos, kIRNodeKind, type) |
| 117 |
, fBase(std::move(base)) |
| 118 |
, fComponents(components) { |
| 119 |
SkASSERT(this->components().size() >= 1 && this->components().size() <= 4); |
| 120 |
} |
| 121 |
|
| 122 |
std::unique_ptr<Expression> fBase; |
| 123 |
ComponentArray fComponents; |
| 124 |
|
| 125 |
using INHERITED = Expression; |
| 126 |
}; |
| 127 |
|
| 128 |
} // namespace SkSL |
| 129 |
|
| 130 |
#endif |