ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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 | */ |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 7 | |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 8 | #ifndef SKSL_INDEX |
| 9 | #define SKSL_INDEX |
| 10 | |
ethannicholas | 22f939e | 2016-10-13 13:25:34 -0700 | [diff] [blame] | 11 | #include "SkSLContext.h" |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 12 | #include "SkSLExpression.h" |
| 13 | #include "SkSLUtil.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 | */ |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 20 | static const Type& index_type(const Context& context, const Type& type) { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 21 | if (type.kind() == Type::kMatrix_Kind) { |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 22 | if (type.componentType() == *context.fFloat_Type) { |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 23 | switch (type.rows()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 24 | case 2: return *context.fFloat2_Type; |
| 25 | case 3: return *context.fFloat3_Type; |
| 26 | case 4: return *context.fFloat4_Type; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame^] | 27 | default: SkASSERT(false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 28 | } |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 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; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame^] | 34 | default: SkASSERT(false); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 35 | } |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 36 | } else { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame^] | 37 | SkASSERT(type.componentType() == *context.fDouble_Type); |
ethannicholas | 5961bc9 | 2016-10-12 06:39:56 -0700 | [diff] [blame] | 38 | switch (type.rows()) { |
Ethan Nicholas | 5af9ea3 | 2017-07-28 15:19:46 -0400 | [diff] [blame] | 39 | case 2: return *context.fDouble2_Type; |
| 40 | case 3: return *context.fDouble3_Type; |
| 41 | case 4: return *context.fDouble4_Type; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame^] | 42 | default: SkASSERT(false); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 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 { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 53 | IndexExpression(const Context& context, std::unique_ptr<Expression> base, |
ethannicholas | d598f79 | 2016-07-25 10:08:54 -0700 | [diff] [blame] | 54 | std::unique_ptr<Expression> index) |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 55 | : INHERITED(base->fOffset, kIndex_Kind, index_type(context, base->fType)) |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 56 | , fBase(std::move(base)) |
| 57 | , fIndex(std::move(index)) { |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame^] | 58 | SkASSERT(fIndex->fType == *context.fInt_Type || fIndex->fType == *context.fUInt_Type); |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Ethan Nicholas | cb67096 | 2017-04-20 19:31:52 -0400 | [diff] [blame] | 61 | bool hasSideEffects() const override { |
| 62 | return fBase->hasSideEffects() || fIndex->hasSideEffects(); |
| 63 | } |
| 64 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 65 | String description() const override { |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 66 | return fBase->description() + "[" + fIndex->description() + "]"; |
| 67 | } |
| 68 | |
Ethan Nicholas | 86a4340 | 2017-01-19 13:32:00 -0500 | [diff] [blame] | 69 | std::unique_ptr<Expression> fBase; |
| 70 | std::unique_ptr<Expression> fIndex; |
ethannicholas | b3058bd | 2016-07-01 08:22:01 -0700 | [diff] [blame] | 71 | |
| 72 | typedef Expression INHERITED; |
| 73 | }; |
| 74 | |
| 75 | } // namespace |
| 76 | |
| 77 | #endif |