OLD | NEW |
---|---|
(Empty) | |
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 | |
10 namespace SkSL { | |
11 | |
12 bool Type::determineCoercionCost(std::shared_ptr<Type> other, int* outCost) cons t { | |
13 if (this == other.get()) { | |
14 *outCost = 0; | |
15 return true; | |
16 } | |
17 if (this->kind() == kVector_Kind && other->kind() == kVector_Kind) { | |
18 if (this->columns() == other->columns()) { | |
19 return this->componentType()->determineCoercionCost(other->component Type(), outCost); | |
20 } | |
21 return false; | |
22 } | |
23 if (this->kind() == kMatrix_Kind) { | |
24 if (this->columns() == other->columns() && | |
25 this->rows() == other->rows()) { | |
26 return this->componentType()->determineCoercionCost(other->component Type(), outCost); | |
27 } | |
28 return false; | |
29 } | |
30 for (size_t i = 0; i < fCoercibleTypes.size(); i++) { | |
31 if (fCoercibleTypes[i] == other) { | |
32 *outCost = (int) i + 1; | |
33 return true; | |
34 } | |
35 } | |
36 return false; | |
37 } | |
38 | |
39 std::shared_ptr<Type> Type::toCompound(int columns, int rows) { | |
40 ASSERT(this->kind() == Type::kScalar_Kind); | |
41 if (columns == 1 && rows == 1) { | |
42 return std::shared_ptr<Type>(this); | |
43 } | |
44 if (*this == *kFloat_Type) { | |
45 switch (rows) { | |
46 case 1: | |
47 switch (columns) { | |
48 case 2: return kVec2_Type; | |
49 case 3: return kVec3_Type; | |
50 case 4: return kVec4_Type; | |
51 default: ABORT("unsupported vector column count (%d)", colum ns); | |
52 } | |
53 case 2: | |
54 switch (columns) { | |
55 case 2: return kMat2x2_Type; | |
56 case 3: return kMat3x2_Type; | |
57 case 4: return kMat4x2_Type; | |
58 default: ABORT("unsupported matrix column count (%d)", colum ns); | |
59 } | |
60 case 3: | |
61 switch (columns) { | |
62 case 2: return kMat2x3_Type; | |
63 case 3: return kMat3x3_Type; | |
64 case 4: return kMat4x3_Type; | |
65 default: ABORT("unsupported matrix column count (%d)", colum ns); | |
66 } | |
67 case 4: | |
68 switch (columns) { | |
69 case 2: return kMat2x4_Type; | |
70 case 3: return kMat3x4_Type; | |
71 case 4: return kMat4x4_Type; | |
72 default: ABORT("unsupported matrix column count (%d)", colum ns); | |
73 } | |
74 default: ABORT("unsupported row count (%d)", rows); | |
75 } | |
76 } else if (*this == *kDouble_Type) { | |
77 switch (rows) { | |
78 case 1: | |
79 switch (columns) { | |
80 case 2: return kDVec2_Type; | |
81 case 3: return kDVec3_Type; | |
82 case 4: return kDVec4_Type; | |
83 default: ABORT("unsupported vector column count (%d)", colum ns); | |
84 } | |
85 case 2: | |
86 switch (columns) { | |
87 case 2: return kDMat2x2_Type; | |
88 case 3: return kDMat3x2_Type; | |
89 case 4: return kDMat4x2_Type; | |
90 default: ABORT("unsupported matrix column count (%d)", colum ns); | |
91 } | |
92 case 3: | |
93 switch (columns) { | |
94 case 2: return kDMat2x3_Type; | |
95 case 3: return kDMat3x3_Type; | |
96 case 4: return kDMat4x3_Type; | |
97 default: ABORT("unsupported matrix column count (%d)", colum ns); | |
98 } | |
99 case 4: | |
100 switch (columns) { | |
101 case 2: return kDMat2x4_Type; | |
102 case 3: return kDMat3x4_Type; | |
103 case 4: return kDMat4x4_Type; | |
104 default: ABORT("unsupported matrix column count (%d)", colum ns); | |
105 } | |
106 default: ABORT("unsupported row count (%d)", rows); | |
107 } | |
108 } else if (*this == *kInt_Type) { | |
109 switch (rows) { | |
110 case 1: | |
111 switch (columns) { | |
112 case 2: return kIVec2_Type; | |
113 case 3: return kIVec3_Type; | |
114 case 4: return kIVec4_Type; | |
115 default: ABORT("unsupported vector column count (%d)", colum ns); | |
116 } | |
117 default: ABORT("unsupported row count (%d)", rows); | |
118 } | |
119 } else if (*this == *kUInt_Type) { | |
120 switch (rows) { | |
121 case 1: | |
122 switch (columns) { | |
123 case 2: return kUVec2_Type; | |
124 case 3: return kUVec3_Type; | |
125 case 4: return kUVec4_Type; | |
126 default: ABORT("unsupported vector column count (%d)", colum ns); | |
127 } | |
128 default: ABORT("unsupported row count (%d)", rows); | |
129 } | |
130 } | |
131 ABORT("unsupported scalar_to_compound type %s", this->description().c_str()) ; | |
dogben
2016/06/21 21:52:51
bool not needed?
ethannicholas
2016/06/24 21:23:07
It'll come eventually. Support for int and bool co
| |
132 } | |
133 | |
134 const std::shared_ptr<Type> kVoid_Type(new Type("void")); | |
135 | |
136 const std::shared_ptr<Type> kDouble_Type(new Type("double", true)); | |
137 const std::shared_ptr<Type> kDVec2_Type(new Type("dvec2", kDouble_Type, 2)); | |
138 const std::shared_ptr<Type> kDVec3_Type(new Type("dvec3", kDouble_Type, 3)); | |
139 const std::shared_ptr<Type> kDVec4_Type(new Type("dvec4", kDouble_Type, 4)); | |
140 | |
141 static std::vector<std::shared_ptr<Type>> float_coercible_types() { | |
142 std::vector<std::shared_ptr<Type>> result; | |
143 result.push_back(kDouble_Type); | |
144 return result; | |
145 } | |
146 const std::shared_ptr<Type> kFloat_Type(new Type("float", true, float_coercible_ types())); | |
147 const std::shared_ptr<Type> kVec2_Type(new Type("vec2", kFloat_Type, 2)); | |
148 const std::shared_ptr<Type> kVec3_Type(new Type("vec3", kFloat_Type, 3)); | |
149 const std::shared_ptr<Type> kVec4_Type(new Type("vec4", kFloat_Type, 4)); | |
150 | |
151 static std::vector<std::shared_ptr<Type>> uint_coercible_types() { | |
152 std::vector<std::shared_ptr<Type>> result; | |
153 result.push_back(kFloat_Type); | |
154 result.push_back(kDouble_Type); | |
155 return result; | |
156 } | |
157 const std::shared_ptr<Type> kUInt_Type(new Type("uint", true, uint_coercible_typ es())); | |
158 const std::shared_ptr<Type> kUVec2_Type(new Type("uvec2", kUInt_Type, 2)); | |
159 const std::shared_ptr<Type> kUVec3_Type(new Type("uvec3", kUInt_Type, 3)); | |
160 const std::shared_ptr<Type> kUVec4_Type(new Type("uvec4", kUInt_Type, 4)); | |
161 | |
162 static std::vector<std::shared_ptr<Type>> int_coercible_types() { | |
163 std::vector<std::shared_ptr<Type>> result; | |
164 result.push_back(kUInt_Type); | |
165 result.push_back(kFloat_Type); | |
166 result.push_back(kDouble_Type); | |
167 return result; | |
168 } | |
169 const std::shared_ptr<Type> kInt_Type(new Type("int", true, int_coercible_types( ))); | |
170 const std::shared_ptr<Type> kIVec2_Type(new Type("ivec2", kInt_Type, 2)); | |
171 const std::shared_ptr<Type> kIVec3_Type(new Type("ivec3", kInt_Type, 3)); | |
172 const std::shared_ptr<Type> kIVec4_Type(new Type("ivec4", kInt_Type, 4)); | |
173 | |
174 const std::shared_ptr<Type> kBool_Type(new Type("bool", false)); | |
175 const std::shared_ptr<Type> kBVec2_Type(new Type("bvec2", kBool_Type, 2)); | |
176 const std::shared_ptr<Type> kBVec3_Type(new Type("bvec3", kBool_Type, 3)); | |
177 const std::shared_ptr<Type> kBVec4_Type(new Type("bvec4", kBool_Type, 4)); | |
178 | |
179 const std::shared_ptr<Type> kMat2x2_Type(new Type("mat2", kFloat_Type, 2, 2)); | |
180 const std::shared_ptr<Type> kMat2x3_Type(new Type("mat2x3", kFloat_Type, 2, 3)); | |
181 const std::shared_ptr<Type> kMat2x4_Type(new Type("mat2x4", kFloat_Type, 2, 4)); | |
182 const std::shared_ptr<Type> kMat3x2_Type(new Type("mat3x2", kFloat_Type, 3, 2)); | |
183 const std::shared_ptr<Type> kMat3x3_Type(new Type("mat3", kFloat_Type, 3, 3)); | |
184 const std::shared_ptr<Type> kMat3x4_Type(new Type("mat3x4", kFloat_Type, 3, 4)); | |
185 const std::shared_ptr<Type> kMat4x2_Type(new Type("mat4x2", kFloat_Type, 4, 2)); | |
186 const std::shared_ptr<Type> kMat4x3_Type(new Type("mat4x3", kFloat_Type, 4, 3)); | |
187 const std::shared_ptr<Type> kMat4x4_Type(new Type("mat4", kFloat_Type, 4, 4)); | |
188 | |
189 const std::shared_ptr<Type> kDMat2x2_Type(new Type("dmat2", kFloat_Type, 2, 2) ); | |
190 const std::shared_ptr<Type> kDMat2x3_Type(new Type("dmat2x3", kFloat_Type, 2, 3) ); | |
191 const std::shared_ptr<Type> kDMat2x4_Type(new Type("dmat2x4", kFloat_Type, 2, 4) ); | |
192 const std::shared_ptr<Type> kDMat3x2_Type(new Type("dmat3x2", kFloat_Type, 3, 2) ); | |
193 const std::shared_ptr<Type> kDMat3x3_Type(new Type("dmat3", kFloat_Type, 3, 3) ); | |
194 const std::shared_ptr<Type> kDMat3x4_Type(new Type("dmat3x4", kFloat_Type, 3, 4) ); | |
195 const std::shared_ptr<Type> kDMat4x2_Type(new Type("dmat4x2", kFloat_Type, 4, 2) ); | |
196 const std::shared_ptr<Type> kDMat4x3_Type(new Type("dmat4x3", kFloat_Type, 4, 3) ); | |
197 const std::shared_ptr<Type> kDMat4x4_Type(new Type("dmat4", kFloat_Type, 4, 4) ); | |
198 | |
199 const std::shared_ptr<Type> kSampler1D_Type(new Type("sampler1D", SpvDim1D, fals e, false, false, true)); | |
200 const std::shared_ptr<Type> kSampler2D_Type(new Type("sampler2D", SpvDim2D, fals e, false, false, true)); | |
201 const std::shared_ptr<Type> kSampler3D_Type(new Type("sampler3D", SpvDim3D, fals e, false, false, true)); | |
202 const std::shared_ptr<Type> kSamplerCube_Type(new Type("samplerCube")); | |
203 const std::shared_ptr<Type> kSampler2DRect_Type(new Type("sampler2DRect")); | |
204 const std::shared_ptr<Type> kSampler1DArray_Type(new Type("sampler1DArray")); | |
205 const std::shared_ptr<Type> kSampler2DArray_Type(new Type("sampler2DArray")); | |
206 const std::shared_ptr<Type> kSamplerCubeArray_Type(new Type("samplerCubeArray")) ; | |
207 const std::shared_ptr<Type> kSamplerBuffer_Type(new Type("samplerBuffer")); | |
208 const std::shared_ptr<Type> kSampler2DMS_Type(new Type("sampler2DMS")); | |
209 const std::shared_ptr<Type> kSampler2DMSArray_Type(new Type("sampler2DMSArray")) ; | |
210 const std::shared_ptr<Type> kSampler1DShadow_Type(new Type("sampler1DShadow")); | |
211 const std::shared_ptr<Type> kSampler2DShadow_Type(new Type("sampler2DShadow")); | |
212 const std::shared_ptr<Type> kSamplerCubeShadow_Type(new Type("samplerCubeShadow" )); | |
213 const std::shared_ptr<Type> kSampler2DRectShadow_Type(new Type("sampler2DRectSha dow")); | |
214 const std::shared_ptr<Type> kSampler1DArrayShadow_Type(new Type("sampler1DArrayS hadow")); | |
215 const std::shared_ptr<Type> kSampler2DArrayShadow_Type(new Type("sampler2DArrayS hadow")); | |
216 const std::shared_ptr<Type> kSamplerCubeArrayShadow_Type(new Type("samplerCubeAr rayShadow")); | |
217 | |
218 static std::vector<std::shared_ptr<Type>> type(std::shared_ptr<Type> t) { | |
219 std::vector<std::shared_ptr<Type>> result; | |
220 result.push_back(t); | |
221 result.push_back(t); | |
222 result.push_back(t); | |
223 result.push_back(t); | |
224 return result; | |
225 } | |
226 | |
227 static std::vector<std::shared_ptr<Type>> types(std::shared_ptr<Type> t1, | |
228 std::shared_ptr<Type> t2, | |
229 std::shared_ptr<Type> t3, | |
230 std::shared_ptr<Type> t4) { | |
231 std::vector<std::shared_ptr<Type>> result; | |
232 result.push_back(t1); | |
dogben
2016/06/21 21:52:51
nit: std::move x4
| |
233 result.push_back(t2); | |
234 result.push_back(t3); | |
235 result.push_back(t4); | |
236 return result; | |
237 } | |
238 | |
239 const std::shared_ptr<Type> kGSampler1D_Type(new Type("$gsampler1D", type(kSampl er1D_Type))); | |
dogben
2016/06/21 21:52:51
I don't understand this. We're passing the result
ethannicholas
2016/06/24 21:23:07
Added the missing FIXME comment. Basically, the gs
| |
240 const std::shared_ptr<Type> kGSampler2D_Type(new Type("$gsampler2D", type(kSampl er2D_Type))); | |
241 const std::shared_ptr<Type> kGSampler3D_Type(new Type("$gsampler3D", type(kSampl er3D_Type))); | |
242 const std::shared_ptr<Type> kGSamplerCube_Type(new Type("$gsamplerCube", type(kS amplerCube_Type))); | |
243 const std::shared_ptr<Type> kGSampler2DRect_Type(new Type("$gsampler2DRect", | |
244 type(kSampler2DRect_Type))); | |
dogben
2016/06/21 21:52:51
nit: odd indentation, until genType
| |
245 const std::shared_ptr<Type> kGSampler1DArray_Type(new Type("$gsampler1DArray", | |
246 type(kSampler1DArray_Type))); | |
247 const std::shared_ptr<Type> kGSampler2DArray_Type(new Type("$gsampler2DArray", | |
248 type(kSampler2DArray_Type))); | |
249 const std::shared_ptr<Type> kGSamplerCubeArray_Type(new Type("$gsamplerCubeArray ", | |
250 type(kSamplerCubeArray_Type) )); | |
251 const std::shared_ptr<Type> kGSamplerBuffer_Type(new Type("$gsamplerBuffer", | |
252 type(kSamplerBuffer_Type))); | |
253 const std::shared_ptr<Type> kGSampler2DMS_Type(new Type("$gsampler2DMS", | |
254 type(kSampler2DMS_Type))); | |
255 const std::shared_ptr<Type> kGSampler2DMSArray_Type(new Type("$gsampler2DMSArray ", | |
256 type(kSampler2DMSArray_Type) )); | |
257 const std::shared_ptr<Type> kGSampler2DArrayShadow_Type(new Type("$gsampler2DArr ayShadow", | |
258 type(kSampler2DArrayShad ow_Type))); | |
259 const std::shared_ptr<Type> kGSamplerCubeArrayShadow_Type(new Type("$gsamplerCub eArrayShadow", | |
260 type(kSamplerCubeArray Shadow_Type))); | |
261 | |
262 const std::shared_ptr<Type> kGenType_Type(new Type("$genType", types(kFloat_Type , kVec2_Type, | |
263 kVec3_Type, kVec4_Type))); | |
264 const std::shared_ptr<Type> kGenDType_Type(new Type("$genDType", types(kDouble_T ype, kDVec2_Type, | |
265 kDVec3_Ty pe, kDVec4_Type))); | |
266 const std::shared_ptr<Type> kGenIType_Type(new Type("$genIType", types(kInt_Type , kIVec2_Type, | |
267 kIVec3_Ty pe, kIVec4_Type))); | |
268 const std::shared_ptr<Type> kGenUType_Type(new Type("$genUType", types(kUInt_Typ e, kUVec2_Type, | |
269 kUVec3_Ty pe, kUVec4_Type))); | |
270 const std::shared_ptr<Type> kGenBType_Type(new Type("$genBType", types(kBool_Typ e, kBVec2_Type, | |
271 kBVec3_Ty pe, kBVec4_Type))); | |
272 | |
273 const std::shared_ptr<Type> kMat_Type(new Type("$mat")); | |
274 | |
275 const std::shared_ptr<Type> kVec_Type(new Type("$vec", types(kVec2_Type, kVec2_T ype, kVec3_Type, | |
dogben
2016/06/21 21:52:51
kVec2_Type twice is correct?
ethannicholas
2016/06/24 21:23:07
Was written before kInvalid_Type existed and I for
| |
276 kVec4_Type))); | |
277 | |
278 const std::shared_ptr<Type> kGVec_Type(new Type("$gvec")); | |
279 const std::shared_ptr<Type> kGVec2_Type(new Type("$gvec2")); | |
280 const std::shared_ptr<Type> kGVec3_Type(new Type("$gvec3")); | |
281 const std::shared_ptr<Type> kGVec4_Type(new Type("$gvec4", type(kVec4_Type))); | |
282 const std::shared_ptr<Type> kDVec_Type(new Type("$dvec")); | |
283 const std::shared_ptr<Type> kIVec_Type(new Type("$ivec")); | |
284 const std::shared_ptr<Type> kUVec_Type(new Type("$uvec")); | |
285 | |
286 const std::shared_ptr<Type> kBVec_Type(new Type("$bvec", types(kBVec2_Type, kBVe c2_Type, | |
287 kBVec3_Type, kBVe c4_Type))); | |
288 | |
289 const std::shared_ptr<Type> kInvalid_Type(new Type("<INVALID>")); | |
290 | |
291 } // namespace | |
OLD | NEW |