blob: 7c5c1290b8aac28bb4ba9ac0d761dc3e8a4813bf [file] [log] [blame]
ethannicholasb3058bd2016-07-01 08:22:01 -07001/*
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 Nicholas0df1b042017-03-31 13:56:23 -04007
ethannicholasb3058bd2016-07-01 08:22:01 -07008#ifndef SKSL_INDEX
9#define SKSL_INDEX
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/sksl/SkSLContext.h"
12#include "src/sksl/SkSLUtil.h"
13#include "src/sksl/ir/SkSLExpression.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070014
15namespace SkSL {
16
17/**
18 * Given a type, returns the type that will result from extracting an array value from it.
19 */
ethannicholasd598f792016-07-25 10:08:54 -070020static const Type& index_type(const Context& context, const Type& type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070021 if (type.kind() == Type::kMatrix_Kind) {
ethannicholasd598f792016-07-25 10:08:54 -070022 if (type.componentType() == *context.fFloat_Type) {
ethannicholas5961bc92016-10-12 06:39:56 -070023 switch (type.rows()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040024 case 2: return *context.fFloat2_Type;
25 case 3: return *context.fFloat3_Type;
26 case 4: return *context.fFloat4_Type;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040027 default: SkASSERT(false);
ethannicholasb3058bd2016-07-01 08:22:01 -070028 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -040029 } 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 Nicholasd9d33c32018-06-12 11:05:59 -040034 default: SkASSERT(false);
Ethan Nicholasf7b88202017-09-18 14:10:39 -040035 }
ethannicholasb3058bd2016-07-01 08:22:01 -070036 } else {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040037 SkASSERT(type.componentType() == *context.fDouble_Type);
ethannicholas5961bc92016-10-12 06:39:56 -070038 switch (type.rows()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040039 case 2: return *context.fDouble2_Type;
40 case 3: return *context.fDouble3_Type;
41 case 4: return *context.fDouble4_Type;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040042 default: SkASSERT(false);
ethannicholasb3058bd2016-07-01 08:22:01 -070043 }
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 */
52struct IndexExpression : public Expression {
Ethan Nicholas0df1b042017-03-31 13:56:23 -040053 IndexExpression(const Context& context, std::unique_ptr<Expression> base,
ethannicholasd598f792016-07-25 10:08:54 -070054 std::unique_ptr<Expression> index)
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070055 : INHERITED(base->fOffset, kIndex_Kind, index_type(context, base->fType))
ethannicholasb3058bd2016-07-01 08:22:01 -070056 , fBase(std::move(base))
57 , fIndex(std::move(index)) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -040058 SkASSERT(fIndex->fType == *context.fInt_Type || fIndex->fType == *context.fUInt_Type);
ethannicholasb3058bd2016-07-01 08:22:01 -070059 }
60
Ethan Nicholascb670962017-04-20 19:31:52 -040061 bool hasSideEffects() const override {
62 return fBase->hasSideEffects() || fIndex->hasSideEffects();
63 }
64
Ethan Nicholas00543112018-07-31 09:44:36 -040065 std::unique_ptr<Expression> clone() const override {
66 return std::unique_ptr<Expression>(new IndexExpression(fBase->clone(), fIndex->clone(),
67 &fType));
68 }
69
Ethan Nicholas0df1b042017-03-31 13:56:23 -040070 String description() const override {
ethannicholasb3058bd2016-07-01 08:22:01 -070071 return fBase->description() + "[" + fIndex->description() + "]";
72 }
73
Ethan Nicholas86a43402017-01-19 13:32:00 -050074 std::unique_ptr<Expression> fBase;
75 std::unique_ptr<Expression> fIndex;
ethannicholasb3058bd2016-07-01 08:22:01 -070076
77 typedef Expression INHERITED;
Ethan Nicholas00543112018-07-31 09:44:36 -040078
79private:
80 IndexExpression(std::unique_ptr<Expression> base, std::unique_ptr<Expression> index,
81 const Type* type)
82 : INHERITED(base->fOffset, kIndex_Kind, *type)
83 , fBase(std::move(base))
84 , fIndex(std::move(index)) {}
ethannicholasb3058bd2016-07-01 08:22:01 -070085};
86
87} // namespace
88
89#endif