1 # -*- coding: utf-8 -*-
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
7 # Copyright 2015 The Android Open Source Project
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
21 #-------------------------------------------------------------------------
31 indices = xrange(sys.maxint)
36 # * int <-> float <-> bool (also float(float) etc.)
37 # * to bool: zero means false, others true
38 # * from bool: false==0, true==1
39 # * \todo [petri] float<->int rounding rules?
40 # - scalar type from vector
41 # * choose the first component
42 # - vectors & matrices
43 # * vector from scalar: broadcast to all components
44 # * matrix from scalar: broadcast scalar to diagonal, other components zero
45 # * vector from vector: copy existing components
46 # + illegal: vector from smaller vector
47 # * mat from mat: copy existing components, other components from identity matrix
48 # * from components: consumed by-component in column-major order, must have same
49 # number of components,
50 # + note: vec4(mat2) valid
51 # \todo [petri] Implement!
53 # * type conversions are always allowed: mat3(ivec3, bvec3, bool, int, float) is valid!
58 # * .xyzw, .rgba, .stpq
60 # * now allowed for scalar types
61 # * legal to chain: vec4.rgba.xyzw.stpq
62 # * illegal to select more than 4 components
63 # * array indexing with [] operator
67 # * note: mat4[0].x = 1.0; vs mat4[0][0] = 1.0; ??
68 # * out-of-bounds accesses
70 # \todo [petri] Accessors!
74 # - constructing larger vector from smaller: vec3(vec2) ?
75 # - base type and size conversion at same time: vec4(bool), int(vec3) allowed?
77 def combineVec(comps):
79 for ndx in range(len(comps[0])):
81 # print x[ndx].toFloat().getScalars() ,
82 scalars = reduce(operator.add, [x[ndx].toFloat().getScalars() for x in comps])
84 res.append(Vec.fromScalarList(scalars))
87 def combineIVec(comps):
89 for ndx in range(len(comps[0])):
90 res.append(Vec.fromScalarList(reduce(operator.add, [x[ndx].toInt().getScalars() for x in comps])))
93 def combineBVec(comps):
95 for ndx in range(len(comps[0])):
96 res.append(Vec.fromScalarList(reduce(operator.add, [x[ndx].toBool().getScalars() for x in comps])))
99 def combineMat(numCols, numRows, comps):
101 for ndx in range(len(comps[0])):
102 scalars = reduce(operator.add, [x[ndx].toFloat().getScalars() for x in comps])
103 res.append(Mat(numCols, numRows, scalars))
106 def combineMat2(comps): return combineMat(2, 2, comps)
107 def combineMat3(comps): return combineMat(3, 3, comps)
108 def combineMat4(comps): return combineMat(4, 4, comps)
110 # 0 \+ [f*f for f in lst]
111 # r = 0 \+ [f in lst -> f*f]
116 s_simpleCaseTemplate = """
124 precision mediump float;
125 precision mediump int;
139 s_simpleIllegalCaseTemplate = """
145 precision mediump float;
146 precision mediump int;
160 class SimpleCase(ShaderCase):
161 def __init__(self, name, inputs, outputs, op):
164 self.outputs = outputs
170 "VALUES": genValues(self.inputs, self.outputs),
173 return fillTemplate(s_simpleCaseTemplate, params)
175 class ConversionCase(ShaderCase):
176 def __init__(self, inValues, convFunc):
177 outValues = convFunc(inValues)
178 inType = inValues[0].typeString()
179 outType = outValues[0].typeString()
180 self.name = "%s_to_%s" % (inType, outType)
181 self.op = "out0 = %s(in0);" % outType
182 self.inputs = [("%s in0" % inType, inValues)]
183 self.outputs = [("%s out0" % outType, outValues)]
188 "VALUES": genValues(self.inputs, self.outputs),
191 return fillTemplate(s_simpleCaseTemplate, params)
193 class IllegalConversionCase(ShaderCase):
194 def __init__(self, inValue, outValue):
195 inType = inValue.typeString()
196 outType = outValue.typeString()
197 self.name = "%s_to_%s" % (inType, outType)
198 self.op = "%s in0 = %s;\n%s out0 = %s(in0);" % (inType, str(inValue), outType, outType)
200 self.outType = outType
207 return fillTemplate(s_simpleIllegalCaseTemplate, params)
209 class CombineCase(ShaderCase):
210 def __init__(self, inComps, combFunc):
211 self.inComps = inComps
212 self.outValues = combFunc(inComps)
213 self.outType = self.outValues[0].typeString()
214 inTypes = [values[0].typeString() for values in inComps]
215 self.name = "%s_to_%s" % ("_".join(inTypes), self.outType)
216 self.inputs = [("%s in%s" % (comp[0].typeString(), ndx), comp) for (comp, ndx) in zip(inComps, indices)]
217 self.outputs = [("%s out0" % self.outType, self.outValues)]
218 self.op = "out0 = %s(%s);" % (self.outType, ", ".join(["in%d" % x for x in range(len(inComps))]))
223 "VALUES": genValues(self.inputs, self.outputs),
226 return fillTemplate(s_simpleCaseTemplate, params)
230 inFloat = [Scalar(x) for x in [0.0, 1.0, 2.0, 3.5, -0.5, -8.25, -20.125, 36.8125]]
231 inInt = [Scalar(x) for x in [0, 1, 2, 5, 8, 11, -12, -66, -192, 255]]
232 inBool = [Scalar(x) for x in [True, False]]
234 inVec4 = [Vec4(0.0, 0.5, 0.75, 0.825), Vec4(1.0, 1.25, 1.125, 1.75),
235 Vec4(-0.5, -2.25, -4.875, 9.0), Vec4(-32.0, 64.0, -51.0, 24.0),
236 Vec4(-0.75, -1.0/31.0, 1.0/19.0, 1.0/4.0)]
237 inVec3 = toVec3(inVec4)
238 inVec2 = toVec2(inVec4)
239 inIVec4 = toIVec4(inVec4)
240 inIVec3 = toIVec3(inVec4)
241 inIVec2 = toIVec2(inVec4)
242 inBVec4 = [Vec4(True, False, False, True), Vec4(False, False, False, True), Vec4(False, True, False, False), Vec4(True, True, True, True), Vec4(False, False, False, False)]
243 inBVec3 = toBVec3(inBVec4)
244 inBVec2 = toBVec2(inBVec4)
246 # \todo [petri] Enable large values when epsilon adapts to the values.
247 inMat4 = [Mat4(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0),
248 Mat4(6.5, 12.5, -0.75, 9.975, 32.0, 1.0/48.0, -8.425, -6.542, 1.0/8.0, 1.0/16.0, 1.0/32.0, 1.0/64.0, -6.725, -0.5, -0.0125, 9.975),
249 #Mat4(128.0, 256.0, -512.0, -1024.0, 2048.0, -4096.0, 8192.0, -8192.0, 192.0, -384.0, 768.0, -1536.0, 8192.0, -8192.0, 6144.0, -6144.0)
251 inMat3 = [Mat3(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0),
252 Mat3(6.5, 12.5, -0.75, 32.0, 1.0/32.0, 1.0/64.0, 1.0/8.0, 1.0/16.0, 1.0/32.0),
253 #Mat3(-18.725, -0.5, -0.0125, 19.975, -0.25, -17.75, 9.25, 65.125, -21.425),
254 #Mat3(128.0, -4096.0, -8192.0, 192.0, 768.0, -1536.0, 8192.0, 6144.0, -6144.0)
256 inMat2 = [Mat2(1.0, 0.0, 0.0, 1.0),
257 Mat2(6.5, 12.5, -0.75, 9.975),
258 Mat2(6.5, 12.5, -0.75, 9.975),
259 Mat2(8.0, 16.0, -24.0, -16.0),
260 Mat2(1.0/8.0, 1.0/16.0, 1.0/32.0, 1.0/64.0),
261 Mat2(-18.725, -0.5, -0.0125, 19.975),
262 #Mat2(128.0, -4096.0, 192.0, -1536.0),
263 #Mat2(-1536.0, 8192.0, 6144.0, -6144.0)
266 def genConversionCases(inValueList, convFuncList):
267 combinations = list(itertools.product(inValueList, convFuncList))
268 return [ConversionCase(inValues, convFunc) for (inValues, convFunc) in combinations]
270 def genIllegalConversionCases(inValueList, outValueList):
271 inValues = [x[0] for x in inValueList]
272 outValues = [x[0] for x in outValueList]
273 combinations = list(itertools.product(inValues, outValues))
274 return [IllegalConversionCase(inVal, outVal) for (inVal, outVal) in combinations]
276 def shuffleSubLists(outer):
277 return [shuffled(inner) for inner in outer]
279 # Generate all combinations of CombineCases.
280 # inTupleList a list of tuples of value-lists
281 # combFuncList a list of comb* functions to combine
282 def genComponentCases(inCompLists, combFuncList):
284 for comps in inCompLists:
285 maxLen = reduce(max, [len(values) for values in comps])
286 comps = [repeatToLength(values, maxLen) for values in comps]
287 comps = [shuffled(values) for values in comps]
288 for combFunc in combFuncList:
289 res += [CombineCase(comps, combFunc)]
292 allConversionCases = []
294 # Scalar-to-scalar conversions.
295 allConversionCases.append(CaseGroup("scalar_to_scalar", "Scalar to Scalar Conversions",
296 genConversionCases([inFloat, inInt, inBool], [toFloat, toInt, toBool])))
298 # Scalar-to-vector conversions.
299 allConversionCases.append(CaseGroup("scalar_to_vector", "Scalar to Vector Conversions",
300 genConversionCases([inFloat, inInt, inBool], [toVec2, toVec3, toVec4, toIVec2, toIVec3, toIVec4, toBVec2, toBVec3, toBVec4])))
302 # Vector-to-scalar conversions.
303 allConversionCases.append(CaseGroup("vector_to_scalar", "Vector to Scalar Conversions",
304 genConversionCases([inVec2, inVec3, inVec4, inIVec2, inIVec3, inIVec4, inBVec2, inBVec3, inBVec4], [toFloat, toInt, toBool])))
306 # Illegal vector-to-vector conversions (to longer vec).
307 allConversionCases.append(CaseGroup("vector_illegal", "Illegal Vector Conversions",
308 genIllegalConversionCases([inVec2, inIVec2, inBVec2], [inVec3, inIVec3, inBVec3, inVec4, inIVec4, inBVec4]) +\
309 genIllegalConversionCases([inVec3, inIVec3, inBVec3], [inVec4, inIVec4, inBVec4])))
311 # Vector-to-vector conversions (type conversions, downcasts).
312 allConversionCases.append(CaseGroup("vector_to_vector", "Vector to Vector Conversions",
313 genConversionCases([inVec4, inIVec4, inBVec4], [toVec4, toVec3, toVec2, toIVec4, toIVec3, toIVec2, toBVec4, toBVec3, toBVec2]) +\
314 genConversionCases([inVec3, inIVec3, inBVec3], [toVec3, toVec2, toIVec3, toIVec2, toBVec3, toBVec2]) +\
315 genConversionCases([inVec2, inIVec2, inBVec2], [toVec2, toIVec2, toBVec2])))
318 allConversionCases.append(CaseGroup("scalar_to_matrix", "Scalar to Matrix Conversions",
319 genConversionCases([inFloat, inInt, inBool], [toMat4, toMat3, toMat2])))
322 #allConversionCases += genConversionCases([inVec4, inIVec4, inBVec4], [toMat4])
323 #allConversionCases += genConversionCases([inVec3, inIVec3, inBVec3], [toMat3])
324 #allConversionCases += genConversionCases([inVec2, inIVec2, inBVec2], [toMat2])
327 allConversionCases.append(CaseGroup("matrix_to_matrix", "Matrix to Matrix Conversions",
328 genConversionCases([inMat4, inMat3, inMat2], [toMat4, toMat3, toMat2])))
330 # Vector-from-components, matrix-from-components.
331 in2Comp = [[inFloat, inFloat], [inInt, inInt], [inBool, inBool], [inFloat, inInt], [inFloat, inBool], [inInt, inBool]]
332 in3Comp = [[inFloat, inFloat, inFloat], [inInt, inInt, inInt], [inBool, inBool, inBool], [inBool, inFloat, inInt], [inVec2, inBool], [inBVec2, inFloat], [inBVec2, inInt], [inBool, inIVec2]]
333 in4Comp = [[inVec2, inVec2], [inBVec2, inBVec2], [inFloat, inFloat, inFloat, inFloat], [inInt, inInt, inInt, inInt], [inBool, inBool, inBool, inBool], [inBool, inFloat, inInt, inBool], [inVec2, inIVec2], [inVec2, inBVec2], [inBVec3, inFloat], [inVec3, inFloat], [inInt, inIVec2, inInt], [inBool, inFloat, inIVec2]]
334 in9Comp = [[inVec3, inVec3, inVec3], [inIVec3, inIVec3, inIVec3], [inVec2, inIVec2, inFloat, inFloat, inInt, inBool, inBool], [inBool, inFloat, inInt, inVec2, inBool, inBVec2, inFloat], [inBool, inBVec2, inInt, inVec4, inBool], [inFloat, inBVec4, inIVec2, inBool, inBool]]
335 in16Comp = [[inVec4, inVec4, inVec4, inVec4], [inIVec4, inIVec4, inIVec4, inIVec4], [inBVec4, inBVec4, inBVec4, inBVec4], [inFloat, inIVec3, inBVec3, inVec4, inIVec2, inFloat, inVec2]]
337 allConversionCases.append(CaseGroup("vector_combine", "Vector Combine Constructors",
338 genComponentCases(in4Comp, [combineVec, combineIVec, combineBVec]) +\
339 genComponentCases(in3Comp, [combineVec, combineIVec, combineBVec]) +\
340 genComponentCases(in2Comp, [combineVec, combineIVec, combineBVec])))
342 allConversionCases.append(CaseGroup("matrix_combine", "Matrix Combine Constructors",
343 genComponentCases(in4Comp, [combineMat2]) +\
344 genComponentCases(in9Comp, [combineMat3]) +\
345 genComponentCases(in16Comp, [combineMat4])
350 if __name__ == "__main__":
351 print "Generating shader case files."
352 writeAllCases("conversions.test", allConversionCases)