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 2: return kVec2_Type; |
| 30 case 3: return kVec3_Type; |
| 31 case 4: return kVec4_Type; |
| 32 } |
| 33 } else if (base == kDouble_Type) { |
| 34 switch (count) { |
| 35 case 2: return kDVec2_Type; |
| 36 case 3: return kDVec3_Type; |
| 37 case 4: return kDVec4_Type; |
| 38 } |
| 39 } else if (base == kInt_Type) { |
| 40 switch (count) { |
| 41 case 2: return kIVec2_Type; |
| 42 case 3: return kIVec3_Type; |
| 43 case 4: return kIVec4_Type; |
| 44 } |
| 45 } else if (base == kUInt_Type) { |
| 46 switch (count) { |
| 47 case 2: return kUVec2_Type; |
| 48 case 3: return kUVec3_Type; |
| 49 case 4: return kUVec4_Type; |
| 50 } |
| 51 } else if (base == kBool_Type) { |
| 52 switch (count) { |
| 53 case 2: return kBVec2_Type; |
| 54 case 3: return kBVec3_Type; |
| 55 case 4: return kBVec4_Type; |
| 56 } |
| 57 } |
| 58 ABORT("cannot swizzle %s\n", value.description().c_str()); |
| 59 } |
| 60 |
| 61 /** |
| 62 * Represents a vector swizzle operation such as 'vec2(1, 2, 3).zyx'. |
| 63 */ |
| 64 struct Swizzle : public Expression { |
| 65 Swizzle(std::unique_ptr<Expression> base, std::vector<int> components) |
| 66 : INHERITED(base->fPosition, kSwizzle_Kind, get_type(*base, components.size(
))) |
| 67 , fBase(std::move(base)) |
| 68 , fComponents(std::move(components)) { |
| 69 ASSERT(fComponents.size() >= 1 && fComponents.size() <= 4); |
| 70 } |
| 71 |
| 72 std::string description() const override { |
| 73 std::string result = fBase->description() + "."; |
| 74 for (int x : fComponents) { |
| 75 result += "xyzw"[x]; |
| 76 } |
| 77 return result; |
| 78 } |
| 79 |
| 80 const std::unique_ptr<Expression> fBase; |
| 81 const std::vector<int> fComponents; |
| 82 |
| 83 typedef Expression INHERITED; |
| 84 }; |
| 85 |
| 86 } // namespace |
| 87 |
| 88 #endif |
OLD | NEW |