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_CONSTRUCTOR
9#define SKSL_CONSTRUCTOR
10
11#include "src/sksl/SkSLIRGenerator.h"
12#include "src/sksl/ir/SkSLExpression.h"
13#include "src/sksl/ir/SkSLFloatLiteral.h"
14#include "src/sksl/ir/SkSLIntLiteral.h"
15#include "src/sksl/ir/SkSLPrefixExpression.h"
16
17namespace SkSL {
18
19/**
20 * Represents the construction of a compound type, such as "float2(x, y)".
21 *
22 * Vector constructors will always consist of either exactly 1 scalar, or a collection of vectors
23 * and scalars totalling exactly the right number of scalar components.
24 *
25 * Matrix constructors will always consist of either exactly 1 scalar, exactly 1 matrix, or a
26 * collection of vectors and scalars totalling exactly the right number of scalar components.
27 */
28struct Constructor : public Expression {
29 Constructor(int offset, const Type& type, std::vector<std::unique_ptr<Expression>> arguments)
30 : INHERITED(offset, kConstructor_Kind, type)
31 , fArguments(std::move(arguments)) {}
32
33 std::unique_ptr<Expression> constantPropagate(const IRGenerator& irGenerator,
34 const DefinitionMap& definitions) override {
35 if (fArguments.size() == 1 && fArguments[0]->fKind == Expression::kIntLiteral_Kind) {
36 if (fType.isFloat()) {
37 // promote float(1) to 1.0
38 int64_t intValue = ((IntLiteral&) *fArguments[0]).fValue;
39 return std::unique_ptr<Expression>(new FloatLiteral(irGenerator.fContext,
40 fOffset,
41 intValue));
42 } else if (fType.isInteger()) {
43 // promote uint(1) to 1u
44 int64_t intValue = ((IntLiteral&) *fArguments[0]).fValue;
45 return std::unique_ptr<Expression>(new IntLiteral(fOffset,
46 intValue,
47 &fType));
48 }
49 }
50 return nullptr;
51 }
52
53 bool hasProperty(Property property) const override {
54 for (const auto& arg : fArguments) {
55 if (arg->hasProperty(property)) {
56 return true;
57 }
58 }
59 return false;
60 }
61
62 std::unique_ptr<Expression> clone() const override {
63 std::vector<std::unique_ptr<Expression>> cloned;
64 for (const auto& arg : fArguments) {
65 cloned.push_back(arg->clone());
66 }
67 return std::unique_ptr<Expression>(new Constructor(fOffset, fType, std::move(cloned)));
68 }
69
70#ifdef SK_DEBUG
71 String description() const override {
72 String result = fType.description() + "(";
73 String separator;
74 for (size_t i = 0; i < fArguments.size(); i++) {
75 result += separator;
76 result += fArguments[i]->description();
77 separator = ", ";
78 }
79 result += ")";
80 return result;
81 }
82#endif
83
84 bool isConstant() const override {
85 for (size_t i = 0; i < fArguments.size(); i++) {
86 if (!fArguments[i]->isConstant()) {
87 return false;
88 }
89 }
90 return true;
91 }
92
93 bool compareConstant(const Context& context, const Expression& other) const override {
94 SkASSERT(other.fKind == Expression::kConstructor_Kind && other.fType == fType);
95 Constructor& c = (Constructor&) other;
96 if (c.fType.kind() == Type::kVector_Kind) {
97 bool isFloat = c.fType.columns() > 1 ? c.fType.componentType().isFloat()
98 : c.fType.isFloat();
99 for (int i = 0; i < fType.columns(); i++) {
100 if (isFloat) {
101 if (this->getFVecComponent(i) != c.getFVecComponent(i)) {
102 return false;
103 }
104 } else if (this->getIVecComponent(i) != c.getIVecComponent(i)) {
105 return false;
106 }
107 }
108 return true;
109 }
110 // shouldn't be possible to have a constant constructor that isn't a vector or matrix;
111 // a constant scalar constructor should have been collapsed down to the appropriate
112 // literal
113 SkASSERT(fType.kind() == Type::kMatrix_Kind);
114 for (int col = 0; col < fType.columns(); col++) {
115 for (int row = 0; row < fType.rows(); row++) {
116 if (getMatComponent(col, row) != c.getMatComponent(col, row)) {
117 return false;
118 }
119 }
120 }
121 return true;
122 }
123
124 template<typename type>
125 type getVecComponent(int index) const {
126 SkASSERT(fType.kind() == Type::kVector_Kind);
127 if (fArguments.size() == 1 && fArguments[0]->fType.kind() == Type::kScalar_Kind) {
128 if (std::is_floating_point<type>::value) {
129 return fArguments[0]->getConstantFloat();
130 } else {
131 return fArguments[0]->getConstantInt();
132 }
133 }
134 int current = 0;
135 for (const auto& arg : fArguments) {
136 SkASSERT(current <= index);
137 if (arg->fType.kind() == Type::kScalar_Kind) {
138 if (index == current) {
139 if (std::is_floating_point<type>::value) {
140 return arg.get()->getConstantFloat();
141 } else {
142 return arg.get()->getConstantInt();
143 }
144 }
145 current++;
146 } else if (arg->fKind == kConstructor_Kind) {
147 if (current + arg->fType.columns() > index) {
148 return ((const Constructor&) *arg).getVecComponent<type>(index - current);
149 }
150 current += arg->fType.columns();
151 } else {
152 if (current + arg->fType.columns() > index) {
153 SkASSERT(arg->fKind == kPrefix_Kind);
154 const PrefixExpression& p = (PrefixExpression&) *arg;
155 const Constructor& c = (const Constructor&) *p.fOperand;
156 return -c.getVecComponent<type>(index - current);
157 }
158 current += arg->fType.columns();
159 }
160 }
161#ifdef SK_DEBUG
162 ABORT("failed to find vector component %d in %s\n", index, description().c_str());
163#endif
164 return -1;
165 }
166
167 SKSL_FLOAT getFVecComponent(int n) const override {
168 return this->getVecComponent<SKSL_FLOAT>(n);
169 }
170
171 /**
172 * For a literal vector expression, return the integer value of the n'th vector component. It is
173 * an error to call this method on an expression which is not a literal vector.
174 */
175 SKSL_INT getIVecComponent(int n) const override {
176 return this->getVecComponent<SKSL_INT>(n);
177 }
178
179 SKSL_FLOAT getMatComponent(int col, int row) const override {
180 SkASSERT(this->isConstant());
181 SkASSERT(fType.kind() == Type::kMatrix_Kind);
182 SkASSERT(col < fType.columns() && row < fType.rows());
183 if (fArguments.size() == 1) {
184 if (fArguments[0]->fType.kind() == Type::kScalar_Kind) {
185 // single scalar argument, so matrix is of the form:
186 // x 0 0
187 // 0 x 0
188 // 0 0 x
189 // return x if col == row
190 return col == row ? fArguments[0]->getConstantFloat() : 0.0;
191 }
192 if (fArguments[0]->fType.kind() == Type::kMatrix_Kind) {
193 SkASSERT(fArguments[0]->fKind == Expression::kConstructor_Kind);
194 // single matrix argument. make sure we're within the argument's bounds.
195 const Type& argType = ((Constructor&) *fArguments[0]).fType;
196 if (col < argType.columns() && row < argType.rows()) {
197 // within bounds, defer to argument
198 return ((Constructor&) *fArguments[0]).getMatComponent(col, row);
199 }
200 // out of bounds
201 return 0.0;
202 }
203 }
204 int currentIndex = 0;
205 int targetIndex = col * fType.rows() + row;
206 for (const auto& arg : fArguments) {
207 SkASSERT(targetIndex >= currentIndex);
208 SkASSERT(arg->fType.rows() == 1);
209 if (currentIndex + arg->fType.columns() > targetIndex) {
210 if (arg->fType.columns() == 1) {
211 return arg->getConstantFloat();
212 } else {
213 return arg->getFVecComponent(targetIndex - currentIndex);
214 }
215 }
216 currentIndex += arg->fType.columns();
217 }
218 ABORT("can't happen, matrix component out of bounds");
219 }
220
221 std::vector<std::unique_ptr<Expression>> fArguments;
222
223 typedef Expression INHERITED;
224};
225
226} // namespace
227
228#endif
229