OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SKSL_SWIZZLE | 8 #ifndef SKSL_SWIZZLE |
9 #define SKSL_SWIZZLE | 9 #define SKSL_SWIZZLE |
10 | 10 |
11 #include "SkSLExpression.h" | 11 #include "SkSLExpression.h" |
12 #include "SkSLUtil.h" | 12 #include "SkSLUtil.h" |
13 | 13 |
14 namespace SkSL { | 14 namespace SkSL { |
15 | 15 |
16 /** | 16 /** |
17 * Given a type and a swizzle component count, returns the type that will result
from swizzling. For | 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 | 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'. | 19 * with more components than the source vector, as in 'vec2(1).xxxx'. |
20 */ | 20 */ |
21 static std::shared_ptr<Type> get_type(Expression& value, | 21 static const Type& get_type(const Context& context, Expression& value, size_t co
unt) { |
22 size_t count) { | 22 const Type& base = value.fType.componentType(); |
23 std::shared_ptr<Type> base = value.fType->componentType(); | |
24 if (count == 1) { | 23 if (count == 1) { |
25 return base; | 24 return base; |
26 } | 25 } |
27 if (base == kFloat_Type) { | 26 if (base == *context.fFloat_Type) { |
28 switch (count) { | 27 switch (count) { |
29 case 2: return kVec2_Type; | 28 case 2: return *context.fVec2_Type; |
30 case 3: return kVec3_Type; | 29 case 3: return *context.fVec3_Type; |
31 case 4: return kVec4_Type; | 30 case 4: return *context.fVec4_Type; |
32 } | 31 } |
33 } else if (base == kDouble_Type) { | 32 } else if (base == *context.fDouble_Type) { |
34 switch (count) { | 33 switch (count) { |
35 case 2: return kDVec2_Type; | 34 case 2: return *context.fDVec2_Type; |
36 case 3: return kDVec3_Type; | 35 case 3: return *context.fDVec3_Type; |
37 case 4: return kDVec4_Type; | 36 case 4: return *context.fDVec4_Type; |
38 } | 37 } |
39 } else if (base == kInt_Type) { | 38 } else if (base == *context.fInt_Type) { |
40 switch (count) { | 39 switch (count) { |
41 case 2: return kIVec2_Type; | 40 case 2: return *context.fIVec2_Type; |
42 case 3: return kIVec3_Type; | 41 case 3: return *context.fIVec3_Type; |
43 case 4: return kIVec4_Type; | 42 case 4: return *context.fIVec4_Type; |
44 } | 43 } |
45 } else if (base == kUInt_Type) { | 44 } else if (base == *context.fUInt_Type) { |
46 switch (count) { | 45 switch (count) { |
47 case 2: return kUVec2_Type; | 46 case 2: return *context.fUVec2_Type; |
48 case 3: return kUVec3_Type; | 47 case 3: return *context.fUVec3_Type; |
49 case 4: return kUVec4_Type; | 48 case 4: return *context.fUVec4_Type; |
50 } | 49 } |
51 } else if (base == kBool_Type) { | 50 } else if (base == *context.fBool_Type) { |
52 switch (count) { | 51 switch (count) { |
53 case 2: return kBVec2_Type; | 52 case 2: return *context.fBVec2_Type; |
54 case 3: return kBVec3_Type; | 53 case 3: return *context.fBVec3_Type; |
55 case 4: return kBVec4_Type; | 54 case 4: return *context.fBVec4_Type; |
56 } | 55 } |
57 } | 56 } |
58 ABORT("cannot swizzle %s\n", value.description().c_str()); | 57 ABORT("cannot swizzle %s\n", value.description().c_str()); |
59 } | 58 } |
60 | 59 |
61 /** | 60 /** |
62 * Represents a vector swizzle operation such as 'vec2(1, 2, 3).zyx'. | 61 * Represents a vector swizzle operation such as 'vec2(1, 2, 3).zyx'. |
63 */ | 62 */ |
64 struct Swizzle : public Expression { | 63 struct Swizzle : public Expression { |
65 Swizzle(std::unique_ptr<Expression> base, std::vector<int> components) | 64 Swizzle(const Context& context, std::unique_ptr<Expression> base, std::vecto
r<int> components) |
66 : INHERITED(base->fPosition, kSwizzle_Kind, get_type(*base, components.size(
))) | 65 : INHERITED(base->fPosition, kSwizzle_Kind, get_type(context, *base, compone
nts.size())) |
67 , fBase(std::move(base)) | 66 , fBase(std::move(base)) |
68 , fComponents(std::move(components)) { | 67 , fComponents(std::move(components)) { |
69 ASSERT(fComponents.size() >= 1 && fComponents.size() <= 4); | 68 ASSERT(fComponents.size() >= 1 && fComponents.size() <= 4); |
70 } | 69 } |
71 | 70 |
72 std::string description() const override { | 71 std::string description() const override { |
73 std::string result = fBase->description() + "."; | 72 std::string result = fBase->description() + "."; |
74 for (int x : fComponents) { | 73 for (int x : fComponents) { |
75 result += "xyzw"[x]; | 74 result += "xyzw"[x]; |
76 } | 75 } |
77 return result; | 76 return result; |
78 } | 77 } |
79 | 78 |
80 const std::unique_ptr<Expression> fBase; | 79 const std::unique_ptr<Expression> fBase; |
81 const std::vector<int> fComponents; | 80 const std::vector<int> fComponents; |
82 | 81 |
83 typedef Expression INHERITED; | 82 typedef Expression INHERITED; |
84 }; | 83 }; |
85 | 84 |
86 } // namespace | 85 } // namespace |
87 | 86 |
88 #endif | 87 #endif |
OLD | NEW |