OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkSLType.h" | 8 #include "SkSLType.h" |
| 9 #include "SkSLContext.h" |
9 | 10 |
10 namespace SkSL { | 11 namespace SkSL { |
11 | 12 |
12 bool Type::determineCoercionCost(std::shared_ptr<Type> other, int* outCost) cons
t { | 13 bool Type::determineCoercionCost(const Type& other, int* outCost) const { |
13 if (this == other.get()) { | 14 if (*this == other) { |
14 *outCost = 0; | 15 *outCost = 0; |
15 return true; | 16 return true; |
16 } | 17 } |
17 if (this->kind() == kVector_Kind && other->kind() == kVector_Kind) { | 18 if (this->kind() == kVector_Kind && other.kind() == kVector_Kind) { |
18 if (this->columns() == other->columns()) { | 19 if (this->columns() == other.columns()) { |
19 return this->componentType()->determineCoercionCost(other->component
Type(), outCost); | 20 return this->componentType().determineCoercionCost(other.componentTy
pe(), outCost); |
20 } | 21 } |
21 return false; | 22 return false; |
22 } | 23 } |
23 if (this->kind() == kMatrix_Kind) { | 24 if (this->kind() == kMatrix_Kind) { |
24 if (this->columns() == other->columns() && | 25 if (this->columns() == other.columns() && |
25 this->rows() == other->rows()) { | 26 this->rows() == other.rows()) { |
26 return this->componentType()->determineCoercionCost(other->component
Type(), outCost); | 27 return this->componentType().determineCoercionCost(other.componentTy
pe(), outCost); |
27 } | 28 } |
28 return false; | 29 return false; |
29 } | 30 } |
30 for (size_t i = 0; i < fCoercibleTypes.size(); i++) { | 31 for (size_t i = 0; i < fCoercibleTypes.size(); i++) { |
31 if (fCoercibleTypes[i] == other) { | 32 if (*fCoercibleTypes[i] == other) { |
32 *outCost = (int) i + 1; | 33 *outCost = (int) i + 1; |
33 return true; | 34 return true; |
34 } | 35 } |
35 } | 36 } |
36 return false; | 37 return false; |
37 } | 38 } |
38 | 39 |
39 std::shared_ptr<Type> Type::toCompound(int columns, int rows) { | 40 const Type& Type::toCompound(const Context& context, int columns, int rows) cons
t { |
40 ASSERT(this->kind() == Type::kScalar_Kind); | 41 ASSERT(this->kind() == Type::kScalar_Kind); |
41 if (columns == 1 && rows == 1) { | 42 if (columns == 1 && rows == 1) { |
42 return std::shared_ptr<Type>(this); | 43 return *this; |
43 } | 44 } |
44 if (*this == *kFloat_Type) { | 45 if (*this == *context.fFloat_Type) { |
45 switch (rows) { | 46 switch (rows) { |
46 case 1: | 47 case 1: |
47 switch (columns) { | 48 switch (columns) { |
48 case 2: return kVec2_Type; | 49 case 2: return *context.fVec2_Type; |
49 case 3: return kVec3_Type; | 50 case 3: return *context.fVec3_Type; |
50 case 4: return kVec4_Type; | 51 case 4: return *context.fVec4_Type; |
51 default: ABORT("unsupported vector column count (%d)", colum
ns); | 52 default: ABORT("unsupported vector column count (%d)", colum
ns); |
52 } | 53 } |
53 case 2: | 54 case 2: |
54 switch (columns) { | 55 switch (columns) { |
55 case 2: return kMat2x2_Type; | 56 case 2: return *context.fMat2x2_Type; |
56 case 3: return kMat3x2_Type; | 57 case 3: return *context.fMat3x2_Type; |
57 case 4: return kMat4x2_Type; | 58 case 4: return *context.fMat4x2_Type; |
58 default: ABORT("unsupported matrix column count (%d)", colum
ns); | 59 default: ABORT("unsupported matrix column count (%d)", colum
ns); |
59 } | 60 } |
60 case 3: | 61 case 3: |
61 switch (columns) { | 62 switch (columns) { |
62 case 2: return kMat2x3_Type; | 63 case 2: return *context.fMat2x3_Type; |
63 case 3: return kMat3x3_Type; | 64 case 3: return *context.fMat3x3_Type; |
64 case 4: return kMat4x3_Type; | 65 case 4: return *context.fMat4x3_Type; |
65 default: ABORT("unsupported matrix column count (%d)", colum
ns); | 66 default: ABORT("unsupported matrix column count (%d)", colum
ns); |
66 } | 67 } |
67 case 4: | 68 case 4: |
68 switch (columns) { | 69 switch (columns) { |
69 case 2: return kMat2x4_Type; | 70 case 2: return *context.fMat2x4_Type; |
70 case 3: return kMat3x4_Type; | 71 case 3: return *context.fMat3x4_Type; |
71 case 4: return kMat4x4_Type; | 72 case 4: return *context.fMat4x4_Type; |
72 default: ABORT("unsupported matrix column count (%d)", colum
ns); | 73 default: ABORT("unsupported matrix column count (%d)", colum
ns); |
73 } | 74 } |
74 default: ABORT("unsupported row count (%d)", rows); | 75 default: ABORT("unsupported row count (%d)", rows); |
75 } | 76 } |
76 } else if (*this == *kDouble_Type) { | 77 } else if (*this == *context.fDouble_Type) { |
77 switch (rows) { | 78 switch (rows) { |
78 case 1: | 79 case 1: |
79 switch (columns) { | 80 switch (columns) { |
80 case 2: return kDVec2_Type; | 81 case 2: return *context.fDVec2_Type; |
81 case 3: return kDVec3_Type; | 82 case 3: return *context.fDVec3_Type; |
82 case 4: return kDVec4_Type; | 83 case 4: return *context.fDVec4_Type; |
83 default: ABORT("unsupported vector column count (%d)", colum
ns); | 84 default: ABORT("unsupported vector column count (%d)", colum
ns); |
84 } | 85 } |
85 case 2: | 86 case 2: |
86 switch (columns) { | 87 switch (columns) { |
87 case 2: return kDMat2x2_Type; | 88 case 2: return *context.fDMat2x2_Type; |
88 case 3: return kDMat3x2_Type; | 89 case 3: return *context.fDMat3x2_Type; |
89 case 4: return kDMat4x2_Type; | 90 case 4: return *context.fDMat4x2_Type; |
90 default: ABORT("unsupported matrix column count (%d)", colum
ns); | 91 default: ABORT("unsupported matrix column count (%d)", colum
ns); |
91 } | 92 } |
92 case 3: | 93 case 3: |
93 switch (columns) { | 94 switch (columns) { |
94 case 2: return kDMat2x3_Type; | 95 case 2: return *context.fDMat2x3_Type; |
95 case 3: return kDMat3x3_Type; | 96 case 3: return *context.fDMat3x3_Type; |
96 case 4: return kDMat4x3_Type; | 97 case 4: return *context.fDMat4x3_Type; |
97 default: ABORT("unsupported matrix column count (%d)", colum
ns); | 98 default: ABORT("unsupported matrix column count (%d)", colum
ns); |
98 } | 99 } |
99 case 4: | 100 case 4: |
100 switch (columns) { | 101 switch (columns) { |
101 case 2: return kDMat2x4_Type; | 102 case 2: return *context.fDMat2x4_Type; |
102 case 3: return kDMat3x4_Type; | 103 case 3: return *context.fDMat3x4_Type; |
103 case 4: return kDMat4x4_Type; | 104 case 4: return *context.fDMat4x4_Type; |
104 default: ABORT("unsupported matrix column count (%d)", colum
ns); | 105 default: ABORT("unsupported matrix column count (%d)", colum
ns); |
105 } | 106 } |
106 default: ABORT("unsupported row count (%d)", rows); | 107 default: ABORT("unsupported row count (%d)", rows); |
107 } | 108 } |
108 } else if (*this == *kInt_Type) { | 109 } else if (*this == *context.fInt_Type) { |
109 switch (rows) { | 110 switch (rows) { |
110 case 1: | 111 case 1: |
111 switch (columns) { | 112 switch (columns) { |
112 case 2: return kIVec2_Type; | 113 case 2: return *context.fIVec2_Type; |
113 case 3: return kIVec3_Type; | 114 case 3: return *context.fIVec3_Type; |
114 case 4: return kIVec4_Type; | 115 case 4: return *context.fIVec4_Type; |
115 default: ABORT("unsupported vector column count (%d)", colum
ns); | 116 default: ABORT("unsupported vector column count (%d)", colum
ns); |
116 } | 117 } |
117 default: ABORT("unsupported row count (%d)", rows); | 118 default: ABORT("unsupported row count (%d)", rows); |
118 } | 119 } |
119 } else if (*this == *kUInt_Type) { | 120 } else if (*this == *context.fUInt_Type) { |
120 switch (rows) { | 121 switch (rows) { |
121 case 1: | 122 case 1: |
122 switch (columns) { | 123 switch (columns) { |
123 case 2: return kUVec2_Type; | 124 case 2: return *context.fUVec2_Type; |
124 case 3: return kUVec3_Type; | 125 case 3: return *context.fUVec3_Type; |
125 case 4: return kUVec4_Type; | 126 case 4: return *context.fUVec4_Type; |
126 default: ABORT("unsupported vector column count (%d)", colum
ns); | 127 default: ABORT("unsupported vector column count (%d)", colum
ns); |
127 } | 128 } |
128 default: ABORT("unsupported row count (%d)", rows); | 129 default: ABORT("unsupported row count (%d)", rows); |
129 } | 130 } |
130 } | 131 } |
131 ABORT("unsupported scalar_to_compound type %s", this->description().c_str())
; | 132 ABORT("unsupported scalar_to_compound type %s", this->description().c_str())
; |
132 } | 133 } |
133 | 134 |
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 const std::shared_ptr<Type> kFloat_Type(new Type("float", true, { kDouble_Type }
)); | |
142 const std::shared_ptr<Type> kVec2_Type(new Type("vec2", kFloat_Type, 2)); | |
143 const std::shared_ptr<Type> kVec3_Type(new Type("vec3", kFloat_Type, 3)); | |
144 const std::shared_ptr<Type> kVec4_Type(new Type("vec4", kFloat_Type, 4)); | |
145 | |
146 const std::shared_ptr<Type> kUInt_Type(new Type("uint", true, { kFloat_Type, kDo
uble_Type })); | |
147 const std::shared_ptr<Type> kUVec2_Type(new Type("uvec2", kUInt_Type, 2)); | |
148 const std::shared_ptr<Type> kUVec3_Type(new Type("uvec3", kUInt_Type, 3)); | |
149 const std::shared_ptr<Type> kUVec4_Type(new Type("uvec4", kUInt_Type, 4)); | |
150 | |
151 const std::shared_ptr<Type> kInt_Type(new Type("int", true, { kUInt_Type, kFloat
_Type, | |
152 kDouble_Type })); | |
153 const std::shared_ptr<Type> kIVec2_Type(new Type("ivec2", kInt_Type, 2)); | |
154 const std::shared_ptr<Type> kIVec3_Type(new Type("ivec3", kInt_Type, 3)); | |
155 const std::shared_ptr<Type> kIVec4_Type(new Type("ivec4", kInt_Type, 4)); | |
156 | |
157 const std::shared_ptr<Type> kBool_Type(new Type("bool", false)); | |
158 const std::shared_ptr<Type> kBVec2_Type(new Type("bvec2", kBool_Type, 2)); | |
159 const std::shared_ptr<Type> kBVec3_Type(new Type("bvec3", kBool_Type, 3)); | |
160 const std::shared_ptr<Type> kBVec4_Type(new Type("bvec4", kBool_Type, 4)); | |
161 | |
162 const std::shared_ptr<Type> kMat2x2_Type(new Type("mat2", kFloat_Type, 2, 2)); | |
163 const std::shared_ptr<Type> kMat2x3_Type(new Type("mat2x3", kFloat_Type, 2, 3)); | |
164 const std::shared_ptr<Type> kMat2x4_Type(new Type("mat2x4", kFloat_Type, 2, 4)); | |
165 const std::shared_ptr<Type> kMat3x2_Type(new Type("mat3x2", kFloat_Type, 3, 2)); | |
166 const std::shared_ptr<Type> kMat3x3_Type(new Type("mat3", kFloat_Type, 3, 3)); | |
167 const std::shared_ptr<Type> kMat3x4_Type(new Type("mat3x4", kFloat_Type, 3, 4)); | |
168 const std::shared_ptr<Type> kMat4x2_Type(new Type("mat4x2", kFloat_Type, 4, 2)); | |
169 const std::shared_ptr<Type> kMat4x3_Type(new Type("mat4x3", kFloat_Type, 4, 3)); | |
170 const std::shared_ptr<Type> kMat4x4_Type(new Type("mat4", kFloat_Type, 4, 4)); | |
171 | |
172 const std::shared_ptr<Type> kDMat2x2_Type(new Type("dmat2", kFloat_Type, 2, 2)
); | |
173 const std::shared_ptr<Type> kDMat2x3_Type(new Type("dmat2x3", kFloat_Type, 2, 3)
); | |
174 const std::shared_ptr<Type> kDMat2x4_Type(new Type("dmat2x4", kFloat_Type, 2, 4)
); | |
175 const std::shared_ptr<Type> kDMat3x2_Type(new Type("dmat3x2", kFloat_Type, 3, 2)
); | |
176 const std::shared_ptr<Type> kDMat3x3_Type(new Type("dmat3", kFloat_Type, 3, 3)
); | |
177 const std::shared_ptr<Type> kDMat3x4_Type(new Type("dmat3x4", kFloat_Type, 3, 4)
); | |
178 const std::shared_ptr<Type> kDMat4x2_Type(new Type("dmat4x2", kFloat_Type, 4, 2)
); | |
179 const std::shared_ptr<Type> kDMat4x3_Type(new Type("dmat4x3", kFloat_Type, 4, 3)
); | |
180 const std::shared_ptr<Type> kDMat4x4_Type(new Type("dmat4", kFloat_Type, 4, 4)
); | |
181 | |
182 const std::shared_ptr<Type> kSampler1D_Type(new Type("sampler1D", SpvDim1D, fals
e, false, false, true)); | |
183 const std::shared_ptr<Type> kSampler2D_Type(new Type("sampler2D", SpvDim2D, fals
e, false, false, true)); | |
184 const std::shared_ptr<Type> kSampler3D_Type(new Type("sampler3D", SpvDim3D, fals
e, false, false, true)); | |
185 const std::shared_ptr<Type> kSamplerCube_Type(new Type("samplerCube")); | |
186 const std::shared_ptr<Type> kSampler2DRect_Type(new Type("sampler2DRect")); | |
187 const std::shared_ptr<Type> kSampler1DArray_Type(new Type("sampler1DArray")); | |
188 const std::shared_ptr<Type> kSampler2DArray_Type(new Type("sampler2DArray")); | |
189 const std::shared_ptr<Type> kSamplerCubeArray_Type(new Type("samplerCubeArray"))
; | |
190 const std::shared_ptr<Type> kSamplerBuffer_Type(new Type("samplerBuffer")); | |
191 const std::shared_ptr<Type> kSampler2DMS_Type(new Type("sampler2DMS")); | |
192 const std::shared_ptr<Type> kSampler2DMSArray_Type(new Type("sampler2DMSArray"))
; | |
193 const std::shared_ptr<Type> kSampler1DShadow_Type(new Type("sampler1DShadow")); | |
194 const std::shared_ptr<Type> kSampler2DShadow_Type(new Type("sampler2DShadow")); | |
195 const std::shared_ptr<Type> kSamplerCubeShadow_Type(new Type("samplerCubeShadow"
)); | |
196 const std::shared_ptr<Type> kSampler2DRectShadow_Type(new Type("sampler2DRectSha
dow")); | |
197 const std::shared_ptr<Type> kSampler1DArrayShadow_Type(new Type("sampler1DArrayS
hadow")); | |
198 const std::shared_ptr<Type> kSampler2DArrayShadow_Type(new Type("sampler2DArrayS
hadow")); | |
199 const std::shared_ptr<Type> kSamplerCubeArrayShadow_Type(new Type("samplerCubeAr
rayShadow")); | |
200 | |
201 static std::vector<std::shared_ptr<Type>> type(std::shared_ptr<Type> t) { | |
202 return { t, t, t, t }; | |
203 } | |
204 | |
205 // FIXME figure out what we're supposed to do with the gsampler et al. types | |
206 const std::shared_ptr<Type> kGSampler1D_Type(new Type("$gsampler1D", type(kSampl
er1D_Type))); | |
207 const std::shared_ptr<Type> kGSampler2D_Type(new Type("$gsampler2D", type(kSampl
er2D_Type))); | |
208 const std::shared_ptr<Type> kGSampler3D_Type(new Type("$gsampler3D", type(kSampl
er3D_Type))); | |
209 const std::shared_ptr<Type> kGSamplerCube_Type(new Type("$gsamplerCube", type(kS
amplerCube_Type))); | |
210 const std::shared_ptr<Type> kGSampler2DRect_Type(new Type("$gsampler2DRect", | |
211 type(kSampler2DRect_Type))); | |
212 const std::shared_ptr<Type> kGSampler1DArray_Type(new Type("$gsampler1DArray", | |
213 type(kSampler1DArray_Type))); | |
214 const std::shared_ptr<Type> kGSampler2DArray_Type(new Type("$gsampler2DArray", | |
215 type(kSampler2DArray_Type))); | |
216 const std::shared_ptr<Type> kGSamplerCubeArray_Type(new Type("$gsamplerCubeArray
", | |
217 type(kSamplerCubeArray_Type)
)); | |
218 const std::shared_ptr<Type> kGSamplerBuffer_Type(new Type("$gsamplerBuffer", | |
219 type(kSamplerBuffer_Type))); | |
220 const std::shared_ptr<Type> kGSampler2DMS_Type(new Type("$gsampler2DMS", | |
221 type(kSampler2DMS_Type))); | |
222 const std::shared_ptr<Type> kGSampler2DMSArray_Type(new Type("$gsampler2DMSArray
", | |
223 type(kSampler2DMSArray_Type)
)); | |
224 const std::shared_ptr<Type> kGSampler2DArrayShadow_Type(new Type("$gsampler2DArr
ayShadow", | |
225 type(kSampler2DArrayShad
ow_Type))); | |
226 const std::shared_ptr<Type> kGSamplerCubeArrayShadow_Type(new Type("$gsamplerCub
eArrayShadow", | |
227 type(kSamplerCubeArray
Shadow_Type))); | |
228 | |
229 const std::shared_ptr<Type> kGenType_Type(new Type("$genType", { kFloat_Type, kV
ec2_Type, | |
230 kVec3_Type, kVe
c4_Type })); | |
231 const std::shared_ptr<Type> kGenDType_Type(new Type("$genDType", { kDouble_Type,
kDVec2_Type, | |
232 kDVec3_Type,
kDVec4_Type })); | |
233 const std::shared_ptr<Type> kGenIType_Type(new Type("$genIType", { kInt_Type, kI
Vec2_Type, | |
234 kIVec3_Type,
kIVec4_Type })); | |
235 const std::shared_ptr<Type> kGenUType_Type(new Type("$genUType", { kUInt_Type, k
UVec2_Type, | |
236 kUVec3_Type,
kUVec4_Type })); | |
237 const std::shared_ptr<Type> kGenBType_Type(new Type("$genBType", { kBool_Type, k
BVec2_Type, | |
238 kBVec3_Type,
kBVec4_Type })); | |
239 | |
240 const std::shared_ptr<Type> kMat_Type(new Type("$mat")); | |
241 | |
242 const std::shared_ptr<Type> kVec_Type(new Type("$vec", { kVec2_Type, kVec2_Type,
kVec3_Type, | |
243 kVec4_Type })); | |
244 | |
245 const std::shared_ptr<Type> kGVec_Type(new Type("$gvec")); | |
246 const std::shared_ptr<Type> kGVec2_Type(new Type("$gvec2")); | |
247 const std::shared_ptr<Type> kGVec3_Type(new Type("$gvec3")); | |
248 const std::shared_ptr<Type> kGVec4_Type(new Type("$gvec4", type(kVec4_Type))); | |
249 const std::shared_ptr<Type> kDVec_Type(new Type("$dvec")); | |
250 const std::shared_ptr<Type> kIVec_Type(new Type("$ivec")); | |
251 const std::shared_ptr<Type> kUVec_Type(new Type("$uvec")); | |
252 | |
253 const std::shared_ptr<Type> kBVec_Type(new Type("$bvec", { kBVec2_Type, kBVec2_T
ype, | |
254 kBVec3_Type, kBVec4_T
ype })); | |
255 | |
256 const std::shared_ptr<Type> kInvalid_Type(new Type("<INVALID>")); | |
257 | |
258 } // namespace | 135 } // namespace |
OLD | NEW |