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, | |
dogben
2016/06/22 17:43:57
nit: I'm used to all references being const, but I
ethannicholas
2016/06/24 21:23:10
Normally I would have used const, but all of these
dogben
2016/06/26 03:57:53
Nope. :-)
The style I'm used to is that everythin
| |
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; | |
dogben
2016/06/22 17:43:57
nit: Aren't all the "case 1"s in this function red
ethannicholas
2016/06/24 21:23:10
What do you mean?
dogben
2016/06/26 03:57:53
Sorry for being unclear. The "count == 1" case is
ethannicholas
2016/06/27 20:35:03
Derp. Yes, it is.
| |
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) { | |
dogben
2016/06/22 17:43:58
nit: std::move
| |
74 ASSERT(components.size() >= 1 && components.size() <= 4); | |
75 } | |
76 | |
77 virtual std::string description() const override { | |
dogben
2016/06/22 17:43:58
nit: remove virtual
| |
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 |