blob: e713a323b3404a7490dd228c124d4475d64dd6cc [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_SWIZZLE
9#define SKSL_SWIZZLE
10
Hal Canaryce78bad2017-05-04 14:15:40 -040011#include "SkSLConstructor.h"
Hal Canary6b20a552017-02-07 14:09:38 -050012#include "SkSLContext.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070013#include "SkSLExpression.h"
Hal Canaryce78bad2017-05-04 14:15:40 -040014#include "SkSLIRGenerator.h"
ethannicholasb3058bd2016-07-01 08:22:01 -070015#include "SkSLUtil.h"
16
17namespace SkSL {
18
19/**
Ethan Nicholas0df1b042017-03-31 13:56:23 -040020 * Given a type and a swizzle component count, returns the type that will result from swizzling. For
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040021 * instance, swizzling a float3with two components will result in a float2 It is possible to swizzle
22 * with more components than the source vector, as in 'float21).xxxx'.
ethannicholasb3058bd2016-07-01 08:22:01 -070023 */
ethannicholasd598f792016-07-25 10:08:54 -070024static const Type& get_type(const Context& context, Expression& value, size_t count) {
25 const Type& base = value.fType.componentType();
ethannicholasb3058bd2016-07-01 08:22:01 -070026 if (count == 1) {
27 return base;
28 }
ethannicholasd598f792016-07-25 10:08:54 -070029 if (base == *context.fFloat_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070030 switch (count) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040031 case 2: return *context.fFloat2_Type;
32 case 3: return *context.fFloat3_Type;
33 case 4: return *context.fFloat4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070034 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -040035 } else if (base == *context.fHalf_Type) {
36 switch (count) {
37 case 2: return *context.fHalf2_Type;
38 case 3: return *context.fHalf3_Type;
39 case 4: return *context.fHalf4_Type;
40 }
ethannicholasd598f792016-07-25 10:08:54 -070041 } else if (base == *context.fDouble_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070042 switch (count) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040043 case 2: return *context.fDouble2_Type;
44 case 3: return *context.fDouble3_Type;
45 case 4: return *context.fDouble4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070046 }
ethannicholasd598f792016-07-25 10:08:54 -070047 } else if (base == *context.fInt_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070048 switch (count) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040049 case 2: return *context.fInt2_Type;
50 case 3: return *context.fInt3_Type;
51 case 4: return *context.fInt4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070052 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -040053 } else if (base == *context.fShort_Type) {
54 switch (count) {
55 case 2: return *context.fShort2_Type;
56 case 3: return *context.fShort3_Type;
57 case 4: return *context.fShort4_Type;
58 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -040059 } else if (base == *context.fByte_Type) {
60 switch (count) {
61 case 2: return *context.fByte2_Type;
62 case 3: return *context.fByte3_Type;
63 case 4: return *context.fByte4_Type;
64 }
ethannicholasd598f792016-07-25 10:08:54 -070065 } else if (base == *context.fUInt_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070066 switch (count) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040067 case 2: return *context.fUInt2_Type;
68 case 3: return *context.fUInt3_Type;
69 case 4: return *context.fUInt4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070070 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -040071 } else if (base == *context.fUShort_Type) {
72 switch (count) {
73 case 2: return *context.fUShort2_Type;
74 case 3: return *context.fUShort3_Type;
75 case 4: return *context.fUShort4_Type;
76 }
Ruiqi Maob609e6d2018-07-17 10:19:38 -040077 } else if (base == *context.fUByte_Type) {
78 switch (count) {
79 case 2: return *context.fUByte2_Type;
80 case 3: return *context.fUByte3_Type;
81 case 4: return *context.fUByte4_Type;
82 }
ethannicholasd598f792016-07-25 10:08:54 -070083 } else if (base == *context.fBool_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070084 switch (count) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040085 case 2: return *context.fBool2_Type;
86 case 3: return *context.fBool3_Type;
87 case 4: return *context.fBool4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070088 }
89 }
90 ABORT("cannot swizzle %s\n", value.description().c_str());
91}
92
93/**
Ethan Nicholas16c11962018-03-16 12:20:54 -040094 * Represents a vector swizzle operation such as 'float2(1, 2, 3).zyx'.
ethannicholasb3058bd2016-07-01 08:22:01 -070095 */
96struct Swizzle : public Expression {
ethannicholasd598f792016-07-25 10:08:54 -070097 Swizzle(const Context& context, std::unique_ptr<Expression> base, std::vector<int> components)
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070098 : INHERITED(base->fOffset, kSwizzle_Kind, get_type(context, *base, components.size()))
ethannicholasb3058bd2016-07-01 08:22:01 -070099 , fBase(std::move(base))
100 , fComponents(std::move(components)) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400101 SkASSERT(fComponents.size() >= 1 && fComponents.size() <= 4);
ethannicholasb3058bd2016-07-01 08:22:01 -0700102 }
103
Ethan Nicholas762466e2017-06-29 10:03:38 -0400104 std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
105 const DefinitionMap& definitions) override {
Ethan Nicholascb670962017-04-20 19:31:52 -0400106 if (fBase->fKind == Expression::kConstructor_Kind && fBase->isConstant()) {
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400107 // we're swizzling a constant vector, e.g. float4(1).x. Simplify it.
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400108 SkASSERT(fBase->fKind == Expression::kConstructor_Kind);
Brian Salomon23f92272017-08-17 14:58:40 +0000109 if (fType == *irGenerator.fContext.fInt_Type) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400110 SkASSERT(fComponents.size() == 1);
Ethan Nicholascb670962017-04-20 19:31:52 -0400111 int64_t value = ((Constructor&) *fBase).getIVecComponent(fComponents[0]);
112 return std::unique_ptr<Expression>(new IntLiteral(irGenerator.fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700113 -1,
114 value));
Brian Salomon23f92272017-08-17 14:58:40 +0000115 } else if (fType == *irGenerator.fContext.fFloat_Type) {
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400116 SkASSERT(fComponents.size() == 1);
Ethan Nicholascb670962017-04-20 19:31:52 -0400117 double value = ((Constructor&) *fBase).getFVecComponent(fComponents[0]);
118 return std::unique_ptr<Expression>(new FloatLiteral(irGenerator.fContext,
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700119 -1,
Ethan Nicholascb670962017-04-20 19:31:52 -0400120 value));
121 }
122 }
123 return nullptr;
124 }
125
126 bool hasSideEffects() const override {
127 return fBase->hasSideEffects();
128 }
129
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400130 String description() const override {
131 String result = fBase->description() + ".";
ethannicholasb3058bd2016-07-01 08:22:01 -0700132 for (int x : fComponents) {
133 result += "xyzw"[x];
134 }
135 return result;
136 }
137
Ethan Nicholas86a43402017-01-19 13:32:00 -0500138 std::unique_ptr<Expression> fBase;
ethannicholasb3058bd2016-07-01 08:22:01 -0700139 const std::vector<int> fComponents;
140
141 typedef Expression INHERITED;
ethannicholasb3058bd2016-07-01 08:22:01 -0700142};
143
144} // namespace
145
146#endif