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_SWIZZLE
9#define SKSL_SWIZZLE
10
11#include "src/sksl/SkSLContext.h"
12#include "src/sksl/SkSLIRGenerator.h"
13#include "src/sksl/SkSLUtil.h"
14#include "src/sksl/ir/SkSLConstructor.h"
15#include "src/sksl/ir/SkSLExpression.h"
16
17namespace SkSL {
18
19// represents a swizzle component of constant 0, as in x.rgb0
20const int SKSL_SWIZZLE_0 = -2;
21
22// represents a swizzle component of constant 1, as in x.rgb1
23const int SKSL_SWIZZLE_1 = -1;
24
25/**
26 * Given a type and a swizzle component count, returns the type that will result from swizzling. For
27 * instance, swizzling a float3 with two components will result in a float2. It is possible to
28 * swizzle with more components than the source vector, as in 'float2(1).xxxx'.
29 */
30static const Type& get_type(const Context& context, Expression& value, size_t count) {
31 const Type& base = value.fType.componentType();
32 if (count == 1) {
33 return base;
34 }
35 if (base == *context.fFloat_Type) {
36 switch (count) {
37 case 2: return *context.fFloat2_Type;
38 case 3: return *context.fFloat3_Type;
39 case 4: return *context.fFloat4_Type;
40 }
41 } else if (base == *context.fHalf_Type) {
42 switch (count) {
43 case 2: return *context.fHalf2_Type;
44 case 3: return *context.fHalf3_Type;
45 case 4: return *context.fHalf4_Type;
46 }
47 } else if (base == *context.fInt_Type) {
48 switch (count) {
49 case 2: return *context.fInt2_Type;
50 case 3: return *context.fInt3_Type;
51 case 4: return *context.fInt4_Type;
52 }
53 } 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 }
59 } 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 }
65 } else if (base == *context.fUInt_Type) {
66 switch (count) {
67 case 2: return *context.fUInt2_Type;
68 case 3: return *context.fUInt3_Type;
69 case 4: return *context.fUInt4_Type;
70 }
71 } 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 }
77 } 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 }
83 } else if (base == *context.fBool_Type) {
84 switch (count) {
85 case 2: return *context.fBool2_Type;
86 case 3: return *context.fBool3_Type;
87 case 4: return *context.fBool4_Type;
88 }
89 }
90#ifdef SK_DEBUG
91 ABORT("cannot swizzle %s\n", value.description().c_str());
92#endif
93 return value.fType;
94}
95
96/**
97 * Represents a vector swizzle operation such as 'float2(1, 2, 3).zyx'.
98 */
99struct Swizzle : public Expression {
100 Swizzle(const Context& context, std::unique_ptr<Expression> base, std::vector<int> components)
101 : INHERITED(base->fOffset, kSwizzle_Kind, get_type(context, *base, components.size()))
102 , fBase(std::move(base))
103 , fComponents(std::move(components)) {
104 SkASSERT(fComponents.size() >= 1 && fComponents.size() <= 4);
105 }
106
107 std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
108 const DefinitionMap& definitions) override {
109 if (fBase->fKind == Expression::kConstructor_Kind && fBase->isCompileTimeConstant()) {
110 // we're swizzling a constant vector, e.g. float4(1).x. Simplify it.
111 SkASSERT(fBase->fKind == Expression::kConstructor_Kind);
112 if (fType.isInteger()) {
113 SkASSERT(fComponents.size() == 1);
114 int64_t value = ((Constructor&) *fBase).getIVecComponent(fComponents[0]);
115 return std::unique_ptr<Expression>(new IntLiteral(irGenerator.fContext,
116 -1,
117 value));
118 } else if (fType.isFloat()) {
119 SkASSERT(fComponents.size() == 1);
120 double value = ((Constructor&) *fBase).getFVecComponent(fComponents[0]);
121 return std::unique_ptr<Expression>(new FloatLiteral(irGenerator.fContext,
122 -1,
123 value));
124 }
125 }
126 return nullptr;
127 }
128
129 bool hasProperty(Property property) const override {
130 return fBase->hasProperty(property);
131 }
132
133 int nodeCount() const override {
134 return 1 + fBase->nodeCount();
135 }
136
137 std::unique_ptr<Expression> clone() const override {
138 return std::unique_ptr<Expression>(new Swizzle(fType, fBase->clone(), fComponents));
139 }
140
141 String description() const override {
142 String result = fBase->description() + ".";
143 for (int x : fComponents) {
144 result += "01xyzw"[x + 2];
145 }
146 return result;
147 }
148
149 std::unique_ptr<Expression> fBase;
150 std::vector<int> fComponents;
151
152 typedef Expression INHERITED;
153
154private:
155 Swizzle(const Type& type, std::unique_ptr<Expression> base, std::vector<int> components)
156 : INHERITED(base->fOffset, kSwizzle_Kind, type)
157 , fBase(std::move(base))
158 , fComponents(std::move(components)) {
159 SkASSERT(fComponents.size() >= 1 && fComponents.size() <= 4);
160 }
161
162
163};
164
165} // namespace SkSL
166
167#endif
168