Line data Source code
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 : #include "SkSLType.h"
9 : #include "SkSLContext.h"
10 :
11 : namespace SkSL {
12 :
13 0 : bool Type::determineCoercionCost(const Type& other, int* outCost) const {
14 0 : if (*this == other) {
15 0 : *outCost = 0;
16 0 : return true;
17 : }
18 0 : if (this->kind() == kVector_Kind && other.kind() == kVector_Kind) {
19 0 : if (this->columns() == other.columns()) {
20 0 : return this->componentType().determineCoercionCost(other.componentType(), outCost);
21 : }
22 0 : return false;
23 : }
24 0 : if (this->kind() == kMatrix_Kind) {
25 0 : if (this->columns() == other.columns() &&
26 0 : this->rows() == other.rows()) {
27 0 : return this->componentType().determineCoercionCost(other.componentType(), outCost);
28 : }
29 0 : return false;
30 : }
31 0 : for (size_t i = 0; i < fCoercibleTypes.size(); i++) {
32 0 : if (*fCoercibleTypes[i] == other) {
33 0 : *outCost = (int) i + 1;
34 0 : return true;
35 : }
36 : }
37 0 : return false;
38 : }
39 :
40 0 : const Type& Type::toCompound(const Context& context, int columns, int rows) const {
41 0 : ASSERT(this->kind() == Type::kScalar_Kind);
42 0 : if (columns == 1 && rows == 1) {
43 0 : return *this;
44 : }
45 0 : if (*this == *context.fFloat_Type) {
46 0 : switch (rows) {
47 : case 1:
48 0 : switch (columns) {
49 0 : case 2: return *context.fVec2_Type;
50 0 : case 3: return *context.fVec3_Type;
51 0 : case 4: return *context.fVec4_Type;
52 0 : default: ABORT("unsupported vector column count (%d)", columns);
53 : }
54 : case 2:
55 0 : switch (columns) {
56 0 : case 2: return *context.fMat2x2_Type;
57 0 : case 3: return *context.fMat3x2_Type;
58 0 : case 4: return *context.fMat4x2_Type;
59 0 : default: ABORT("unsupported matrix column count (%d)", columns);
60 : }
61 : case 3:
62 0 : switch (columns) {
63 0 : case 2: return *context.fMat2x3_Type;
64 0 : case 3: return *context.fMat3x3_Type;
65 0 : case 4: return *context.fMat4x3_Type;
66 0 : default: ABORT("unsupported matrix column count (%d)", columns);
67 : }
68 : case 4:
69 0 : switch (columns) {
70 0 : case 2: return *context.fMat2x4_Type;
71 0 : case 3: return *context.fMat3x4_Type;
72 0 : case 4: return *context.fMat4x4_Type;
73 0 : default: ABORT("unsupported matrix column count (%d)", columns);
74 : }
75 0 : default: ABORT("unsupported row count (%d)", rows);
76 : }
77 0 : } else if (*this == *context.fDouble_Type) {
78 0 : switch (rows) {
79 : case 1:
80 0 : switch (columns) {
81 0 : case 2: return *context.fDVec2_Type;
82 0 : case 3: return *context.fDVec3_Type;
83 0 : case 4: return *context.fDVec4_Type;
84 0 : default: ABORT("unsupported vector column count (%d)", columns);
85 : }
86 : case 2:
87 0 : switch (columns) {
88 0 : case 2: return *context.fDMat2x2_Type;
89 0 : case 3: return *context.fDMat3x2_Type;
90 0 : case 4: return *context.fDMat4x2_Type;
91 0 : default: ABORT("unsupported matrix column count (%d)", columns);
92 : }
93 : case 3:
94 0 : switch (columns) {
95 0 : case 2: return *context.fDMat2x3_Type;
96 0 : case 3: return *context.fDMat3x3_Type;
97 0 : case 4: return *context.fDMat4x3_Type;
98 0 : default: ABORT("unsupported matrix column count (%d)", columns);
99 : }
100 : case 4:
101 0 : switch (columns) {
102 0 : case 2: return *context.fDMat2x4_Type;
103 0 : case 3: return *context.fDMat3x4_Type;
104 0 : case 4: return *context.fDMat4x4_Type;
105 0 : default: ABORT("unsupported matrix column count (%d)", columns);
106 : }
107 0 : default: ABORT("unsupported row count (%d)", rows);
108 : }
109 0 : } else if (*this == *context.fInt_Type) {
110 0 : switch (rows) {
111 : case 1:
112 0 : switch (columns) {
113 0 : case 2: return *context.fIVec2_Type;
114 0 : case 3: return *context.fIVec3_Type;
115 0 : case 4: return *context.fIVec4_Type;
116 0 : default: ABORT("unsupported vector column count (%d)", columns);
117 : }
118 0 : default: ABORT("unsupported row count (%d)", rows);
119 : }
120 0 : } else if (*this == *context.fUInt_Type) {
121 0 : switch (rows) {
122 : case 1:
123 0 : switch (columns) {
124 0 : case 2: return *context.fUVec2_Type;
125 0 : case 3: return *context.fUVec3_Type;
126 0 : case 4: return *context.fUVec4_Type;
127 0 : default: ABORT("unsupported vector column count (%d)", columns);
128 : }
129 0 : default: ABORT("unsupported row count (%d)", rows);
130 : }
131 : }
132 0 : ABORT("unsupported scalar_to_compound type %s", this->description().c_str());
133 : }
134 :
135 : } // namespace
|