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 Swizzle(const Context & context,Position pos,std::unique_ptr<Expression> base,const ComponentArray & components)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 inComponents); 81 base()82 std::unique_ptr<Expression>& base() { 83 return fBase; 84 } 85 base()86 const std::unique_ptr<Expression>& base() const { 87 return fBase; 88 } 89 components()90 const ComponentArray& components() const { 91 return fComponents; 92 } 93 clone(Position pos)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 104 // Returns true if a swizzle keeps each component in its original position. 105 static bool IsIdentity(const ComponentArray& components); 106 107 private: Swizzle(Position pos,const Type * type,std::unique_ptr<Expression> base,const ComponentArray & components)108 Swizzle(Position pos, const Type* type, std::unique_ptr<Expression> base, 109 const ComponentArray& components) 110 : INHERITED(pos, kIRNodeKind, type) 111 , fBase(std::move(base)) 112 , fComponents(components) { 113 SkASSERT(this->components().size() >= 1 && this->components().size() <= 4); 114 } 115 116 std::unique_ptr<Expression> fBase; 117 ComponentArray fComponents; 118 119 using INHERITED = Expression; 120 }; 121 122 } // namespace SkSL 123 124 #endif 125