blob: 0eb4a00dcab1ccdf22204ff5068c81389d12aa34 [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 */
7
8#ifndef SKSL_SWIZZLE
9#define SKSL_SWIZZLE
10
11#include "SkSLExpression.h"
12#include "SkSLUtil.h"
13
14namespace 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 possible to swizzle
19 * with more components than the source vector, as in 'vec2(1).xxxx'.
20 */
ethannicholasd598f792016-07-25 10:08:54 -070021static const Type& get_type(const Context& context, Expression& value, size_t count) {
22 const Type& base = value.fType.componentType();
ethannicholasb3058bd2016-07-01 08:22:01 -070023 if (count == 1) {
24 return base;
25 }
ethannicholasd598f792016-07-25 10:08:54 -070026 if (base == *context.fFloat_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070027 switch (count) {
ethannicholasd598f792016-07-25 10:08:54 -070028 case 2: return *context.fVec2_Type;
29 case 3: return *context.fVec3_Type;
30 case 4: return *context.fVec4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070031 }
ethannicholasd598f792016-07-25 10:08:54 -070032 } else if (base == *context.fDouble_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070033 switch (count) {
ethannicholasd598f792016-07-25 10:08:54 -070034 case 2: return *context.fDVec2_Type;
35 case 3: return *context.fDVec3_Type;
36 case 4: return *context.fDVec4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070037 }
ethannicholasd598f792016-07-25 10:08:54 -070038 } else if (base == *context.fInt_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070039 switch (count) {
ethannicholasd598f792016-07-25 10:08:54 -070040 case 2: return *context.fIVec2_Type;
41 case 3: return *context.fIVec3_Type;
42 case 4: return *context.fIVec4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070043 }
ethannicholasd598f792016-07-25 10:08:54 -070044 } else if (base == *context.fUInt_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070045 switch (count) {
ethannicholasd598f792016-07-25 10:08:54 -070046 case 2: return *context.fUVec2_Type;
47 case 3: return *context.fUVec3_Type;
48 case 4: return *context.fUVec4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070049 }
ethannicholasd598f792016-07-25 10:08:54 -070050 } else if (base == *context.fBool_Type) {
ethannicholasb3058bd2016-07-01 08:22:01 -070051 switch (count) {
ethannicholasd598f792016-07-25 10:08:54 -070052 case 2: return *context.fBVec2_Type;
53 case 3: return *context.fBVec3_Type;
54 case 4: return *context.fBVec4_Type;
ethannicholasb3058bd2016-07-01 08:22:01 -070055 }
56 }
57 ABORT("cannot swizzle %s\n", value.description().c_str());
58}
59
60/**
61 * Represents a vector swizzle operation such as 'vec2(1, 2, 3).zyx'.
62 */
63struct Swizzle : public Expression {
ethannicholasd598f792016-07-25 10:08:54 -070064 Swizzle(const Context& context, std::unique_ptr<Expression> base, std::vector<int> components)
65 : INHERITED(base->fPosition, kSwizzle_Kind, get_type(context, *base, components.size()))
ethannicholasb3058bd2016-07-01 08:22:01 -070066 , fBase(std::move(base))
67 , fComponents(std::move(components)) {
68 ASSERT(fComponents.size() >= 1 && fComponents.size() <= 4);
69 }
70
71 std::string description() const override {
72 std::string result = fBase->description() + ".";
73 for (int x : fComponents) {
74 result += "xyzw"[x];
75 }
76 return result;
77 }
78
79 const std::unique_ptr<Expression> fBase;
80 const std::vector<int> fComponents;
81
82 typedef Expression INHERITED;
83};
84
85} // namespace
86
87#endif