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_INDEX |
9 | #define SKSL_INDEX |
10 | |
11 | #include "src/sksl/SkSLContext.h" |
12 | #include "src/sksl/SkSLUtil.h" |
13 | #include "src/sksl/ir/SkSLExpression.h" |
14 | |
15 | namespace SkSL { |
16 | |
17 | /** |
18 | * Given a type, returns the type that will result from extracting an array value from it. |
19 | */ |
20 | static const Type& index_type(const Context& context, const Type& type) { |
21 | if (type.kind() == Type::kMatrix_Kind) { |
22 | if (type.componentType() == *context.fFloat_Type) { |
23 | switch (type.rows()) { |
24 | case 2: return *context.fFloat2_Type; |
25 | case 3: return *context.fFloat3_Type; |
26 | case 4: return *context.fFloat4_Type; |
27 | default: SkASSERT(false); |
28 | } |
29 | } else if (type.componentType() == *context.fHalf_Type) { |
30 | switch (type.rows()) { |
31 | case 2: return *context.fHalf2_Type; |
32 | case 3: return *context.fHalf3_Type; |
33 | case 4: return *context.fHalf4_Type; |
34 | default: SkASSERT(false); |
35 | } |
36 | } else { |
37 | SkASSERT(type.componentType() == *context.fDouble_Type); |
38 | switch (type.rows()) { |
39 | case 2: return *context.fDouble2_Type; |
40 | case 3: return *context.fDouble3_Type; |
41 | case 4: return *context.fDouble4_Type; |
42 | default: SkASSERT(false); |
43 | } |
44 | } |
45 | } |
46 | return type.componentType(); |
47 | } |
48 | |
49 | /** |
50 | * An expression which extracts a value from an array or matrix, as in 'm[2]'. |
51 | */ |
52 | struct IndexExpression : public Expression { |
53 | IndexExpression(const Context& context, std::unique_ptr<Expression> base, |
54 | std::unique_ptr<Expression> index) |
55 | : INHERITED(base->fOffset, kIndex_Kind, index_type(context, base->fType)) |
56 | , fBase(std::move(base)) |
57 | , fIndex(std::move(index)) { |
58 | SkASSERT(fIndex->fType == *context.fInt_Type || fIndex->fType == *context.fUInt_Type); |
59 | } |
60 | |
61 | bool hasProperty(Property property) const override { |
62 | return fBase->hasProperty(property) || fIndex->hasProperty(property); |
63 | } |
64 | |
65 | std::unique_ptr<Expression> clone() const override { |
66 | return std::unique_ptr<Expression>(new IndexExpression(fBase->clone(), fIndex->clone(), |
67 | &fType)); |
68 | } |
69 | |
70 | #ifdef SK_DEBUG |
71 | String description() const override { |
72 | return fBase->description() + "[" + fIndex->description() + "]" ; |
73 | } |
74 | #endif |
75 | |
76 | std::unique_ptr<Expression> fBase; |
77 | std::unique_ptr<Expression> fIndex; |
78 | |
79 | typedef Expression INHERITED; |
80 | |
81 | private: |
82 | IndexExpression(std::unique_ptr<Expression> base, std::unique_ptr<Expression> index, |
83 | const Type* type) |
84 | : INHERITED(base->fOffset, kIndex_Kind, *type) |
85 | , fBase(std::move(base)) |
86 | , fIndex(std::move(index)) {} |
87 | }; |
88 | |
89 | } // namespace |
90 | |
91 | #endif |
92 | |