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 "SkSLConstructor.h" 12 #include "SkSLContext.h" 13 #include "SkSLExpression.h" 14 #include "SkSLIRGenerator.h" 15 #include "SkSLUtil.h" 16 17 namespace SkSL { 18 19 /** 20 * Given a type and a swizzle component count, returns the type that will result from swizzling. For 21 * instance, swizzling a float3with two components will result in a float2 It is possible to swizzle 22 * with more components than the source vector, as in 'float21).xxxx'. 23 */ 24 static const Type& get_type(const Context& context, Expression& value, size_t count) { 25 const Type& base = value.fType.componentType(); 26 if (count == 1) { 27 return base; 28 } 29 if (base == *context.fFloat_Type) { 30 switch (count) { 31 case 2: return *context.fFloat2_Type; 32 case 3: return *context.fFloat3_Type; 33 case 4: return *context.fFloat4_Type; 34 } 35 } else if (base == *context.fHalf_Type) { 36 switch (count) { 37 case 2: return *context.fHalf2_Type; 38 case 3: return *context.fHalf3_Type; 39 case 4: return *context.fHalf4_Type; 40 } 41 } else if (base == *context.fDouble_Type) { 42 switch (count) { 43 case 2: return *context.fDouble2_Type; 44 case 3: return *context.fDouble3_Type; 45 case 4: return *context.fDouble4_Type; 46 } 47 } else if (base == *context.fInt_Type) { 48 switch (count) { 49 case 2: return *context.fInt2_Type; 50 case 3: return *context.fInt3_Type; 51 case 4: return *context.fInt4_Type; 52 } 53 } else if (base == *context.fShort_Type) { 54 switch (count) { 55 case 2: return *context.fShort2_Type; 56 case 3: return *context.fShort3_Type; 57 case 4: return *context.fShort4_Type; 58 } 59 } else if (base == *context.fUInt_Type) { 60 switch (count) { 61 case 2: return *context.fUInt2_Type; 62 case 3: return *context.fUInt3_Type; 63 case 4: return *context.fUInt4_Type; 64 } 65 } else if (base == *context.fUShort_Type) { 66 switch (count) { 67 case 2: return *context.fUShort2_Type; 68 case 3: return *context.fUShort3_Type; 69 case 4: return *context.fUShort4_Type; 70 } 71 } else if (base == *context.fBool_Type) { 72 switch (count) { 73 case 2: return *context.fBool2_Type; 74 case 3: return *context.fBool3_Type; 75 case 4: return *context.fBool4_Type; 76 } 77 } 78 ABORT("cannot swizzle %s\n", value.description().c_str()); 79 } 80 81 /** 82 * Represents a vector swizzle operation such as 'float21, 2, 3).zyx'. 83 */ 84 struct Swizzle : public Expression { 85 Swizzle(const Context& context, std::unique_ptr<Expression> base, std::vector<int> components) 86 : INHERITED(base->fOffset, kSwizzle_Kind, get_type(context, *base, components.size())) 87 , fBase(std::move(base)) 88 , fComponents(std::move(components)) { 89 ASSERT(fComponents.size() >= 1 && fComponents.size() <= 4); 90 } 91 92 std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator, 93 const DefinitionMap& definitions) override { 94 if (fBase->fKind == Expression::kConstructor_Kind && fBase->isConstant()) { 95 // we're swizzling a constant vector, e.g. float4(1).x. Simplify it. 96 ASSERT(fBase->fKind == Expression::kConstructor_Kind); 97 if (fType == *irGenerator.fContext.fInt_Type) { 98 ASSERT(fComponents.size() == 1); 99 int64_t value = ((Constructor&) *fBase).getIVecComponent(fComponents[0]); 100 return std::unique_ptr<Expression>(new IntLiteral(irGenerator.fContext, 101 -1, 102 value)); 103 } else if (fType == *irGenerator.fContext.fFloat_Type) { 104 ASSERT(fComponents.size() == 1); 105 double value = ((Constructor&) *fBase).getFVecComponent(fComponents[0]); 106 return std::unique_ptr<Expression>(new FloatLiteral(irGenerator.fContext, 107 -1, 108 value)); 109 } 110 } 111 return nullptr; 112 } 113 114 bool hasSideEffects() const override { 115 return fBase->hasSideEffects(); 116 } 117 118 String description() const override { 119 String result = fBase->description() + "."; 120 for (int x : fComponents) { 121 result += "xyzw"[x]; 122 } 123 return result; 124 } 125 126 std::unique_ptr<Expression> fBase; 127 const std::vector<int> fComponents; 128 129 typedef Expression INHERITED; 130 }; 131 132 } // namespace 133 134 #endif 135