blob: b492abc22be2e921e8bb142ed3adb1be8de8b364 [file] [log] [blame]
Brian Osman1298bc42020-06-30 13:39:35 -04001/*
2 * Copyright 2020 Google LLC
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#include "include/private/SkSLSampleUsage.h"
9
10#include "src/sksl/ir/SkSLBinaryExpression.h"
11#include "src/sksl/ir/SkSLConstructor.h"
12#include "src/sksl/ir/SkSLDoStatement.h"
13#include "src/sksl/ir/SkSLExpression.h"
14#include "src/sksl/ir/SkSLExpressionStatement.h"
15#include "src/sksl/ir/SkSLFieldAccess.h"
16#include "src/sksl/ir/SkSLForStatement.h"
17#include "src/sksl/ir/SkSLFunctionCall.h"
18#include "src/sksl/ir/SkSLIfStatement.h"
19#include "src/sksl/ir/SkSLIndexExpression.h"
20#include "src/sksl/ir/SkSLPostfixExpression.h"
21#include "src/sksl/ir/SkSLPrefixExpression.h"
22#include "src/sksl/ir/SkSLProgram.h"
23#include "src/sksl/ir/SkSLReturnStatement.h"
24#include "src/sksl/ir/SkSLSwitchStatement.h"
25#include "src/sksl/ir/SkSLSwizzle.h"
26#include "src/sksl/ir/SkSLTernaryExpression.h"
Brian Osman1298bc42020-06-30 13:39:35 -040027#include "src/sksl/ir/SkSLVariable.h"
Brian Osman1298bc42020-06-30 13:39:35 -040028
29namespace SkSL {
30
31SampleUsage SampleUsage::merge(const SampleUsage& other) {
32 if (other.fExplicitCoords) { fExplicitCoords = true; }
33 if (other.fPassThrough) { fPassThrough = true; }
34 if (other.fHasPerspective) { fHasPerspective = true; }
35
36 if (other.fKind == Kind::kVariable) {
37 fKind = Kind::kVariable;
38 fExpression.clear();
39 } else if (other.fKind == Kind::kUniform) {
40 if (fKind == Kind::kUniform) {
41 if (fExpression != other.fExpression) {
42 fKind = Kind::kVariable;
43 fExpression.clear();
44 } else {
45 // Identical uniform expressions, so leave things as-is
46 }
47 } else if (fKind == Kind::kNone) {
48 fKind = Kind::kUniform;
49 fExpression = other.fExpression;
50 } else {
51 // We were already variable, so leave things as-is
52 SkASSERT(fKind == Kind::kVariable);
53 }
54 } else {
55 // other had no matrix information, so we're done
56 }
57
58 return *this;
59}
60
61std::string SampleUsage::constructor(std::string perspectiveExpression) const {
62 SkASSERT(this->hasMatrix() || perspectiveExpression.empty());
63 if (perspectiveExpression.empty()) {
64 perspectiveExpression = fHasPerspective ? "true" : "false";
65 }
66
67 // Check for special cases where we can use our factories:
68 if (!this->hasMatrix()) {
69 if (fExplicitCoords && !fPassThrough) {
70 return "SkSL::SampleUsage::Explicit()";
71 } else if (fPassThrough && !fExplicitCoords) {
72 return "SkSL::SampleUsage::PassThrough()";
73 }
74 }
75 if (!fExplicitCoords && !fPassThrough) {
76 if (fKind == Kind::kVariable) {
77 return "SkSL::SampleUsage::VariableMatrix(" + perspectiveExpression + ")";
78 } else if (fKind == Kind::kUniform) {
79 return "SkSL::SampleUsage::UniformMatrix(\"" + fExpression + "\", " +
80 perspectiveExpression + ")";
81 }
82 }
83
84 // For more complex scenarios (mixed sampling), fall back to our universal constructor
85 std::string result = "SkSL::SampleUsage(SkSL::SampleUsage::Kind::";
86 switch (fKind) {
87 case Kind::kNone: result += "kNone"; break;
88 case Kind::kUniform: result += "kUniform"; break;
89 case Kind::kVariable: result += "kVariable"; break;
90 }
91 result += ", \"";
92 result += fExpression;
93 result += "\", ";
94 result += perspectiveExpression;
95 result += ", ";
96 result += fExplicitCoords ? "true, " : "false, ";
97 result += fPassThrough ? "true)" : "false)";
98
99 return result;
100}
101
John Stilesa6841be2020-08-06 14:11:56 -0400102} // namespace SkSL