OLD | NEW |
(Empty) | |
| 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 "SkSLExpression.h" |
| 12 #include "SkSLUtil.h" |
| 13 |
| 14 namespace SkSL { |
| 15 |
| 16 /** |
| 17 * Given a type and a swizzle component count, returns the type that will result
from swizzling. For |
| 18 * instance, swizzling a vec3 with two components will result in a vec2. It is p
ossible to swizzle |
| 19 * with more components than the source vector, as in 'vec2(1).xxxx'. |
| 20 */ |
| 21 static std::shared_ptr<Type> get_type(Expression& value, |
| 22 size_t count) { |
| 23 std::shared_ptr<Type> base = value.fType->componentType(); |
| 24 if (count == 1) { |
| 25 return base; |
| 26 } |
| 27 if (base == kFloat_Type) { |
| 28 switch (count) { |
| 29 case 1: return kFloat_Type; |
| 30 case 2: return kVec2_Type; |
| 31 case 3: return kVec3_Type; |
| 32 case 4: return kVec4_Type; |
| 33 } |
| 34 } else if (base == kDouble_Type) { |
| 35 switch (count) { |
| 36 case 1: return kDouble_Type; |
| 37 case 2: return kDVec2_Type; |
| 38 case 3: return kDVec3_Type; |
| 39 case 4: return kDVec4_Type; |
| 40 } |
| 41 } else if (base == kInt_Type) { |
| 42 switch (count) { |
| 43 case 1: return kInt_Type; |
| 44 case 2: return kIVec2_Type; |
| 45 case 3: return kIVec3_Type; |
| 46 case 4: return kIVec4_Type; |
| 47 } |
| 48 } else if (base == kUInt_Type) { |
| 49 switch (count) { |
| 50 case 1: return kUInt_Type; |
| 51 case 2: return kUVec2_Type; |
| 52 case 3: return kUVec3_Type; |
| 53 case 4: return kUVec4_Type; |
| 54 } |
| 55 } else if (base == kBool_Type) { |
| 56 switch (count) { |
| 57 case 1: return kBool_Type; |
| 58 case 2: return kBVec2_Type; |
| 59 case 3: return kBVec3_Type; |
| 60 case 4: return kBVec4_Type; |
| 61 } |
| 62 } |
| 63 ABORT("cannot swizzle %s\n", value.description().c_str()); |
| 64 } |
| 65 |
| 66 /** |
| 67 * Represents a vector swizzle operation such as 'vec2(1, 2, 3).zyx'. |
| 68 */ |
| 69 struct Swizzle : public Expression { |
| 70 Swizzle(std::unique_ptr<Expression> base, std::vector<int> components) |
| 71 : INHERITED(base->fPosition, kSwizzle_Kind, get_type(*base, components.size(
))) |
| 72 , fBase(std::move(base)) |
| 73 , fComponents(components) { |
| 74 ASSERT(components.size() >= 1 && components.size() <= 4); |
| 75 } |
| 76 |
| 77 virtual std::string description() const override { |
| 78 std::string result = fBase->description() + "."; |
| 79 for (int x : fComponents) { |
| 80 result += "xyzw"[x]; |
| 81 } |
| 82 return result; |
| 83 } |
| 84 |
| 85 const std::unique_ptr<Expression> fBase; |
| 86 const std::vector<int> fComponents; |
| 87 |
| 88 typedef Expression INHERITED; |
| 89 }; |
| 90 |
| 91 } // namespace |
| 92 |
| 93 #endif |
OLD | NEW |