am c8a2d38b: am 0d898376: (-s ours) am b24fe40c: Merge "Remove mediump sin/cos/tan...
[platform/upstream/VK-GL-CTS.git] / modules / gles3 / scripts / gen-conversions.py
1 # -*- coding: utf-8 -*-
2
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
6 #
7 # Copyright 2015 The Android Open Source Project
8 #
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
12 #
13 #      http://www.apache.org/licenses/LICENSE-2.0
14 #
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.
20 #
21 #-------------------------------------------------------------------------
22
23 import sys
24 import random
25 import operator
26 import itertools
27
28 from genutil import *
29
30 random.seed(1234567)
31 indices = xrange(sys.maxint)
32
33 # Constructors:
34 #
35 # - scalars types
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!
52 # - notes:
53 #   * type conversions are always allowed: mat3(ivec3, bvec3, bool, int, float) is valid!
54 #
55 # Accessors:
56 #
57 # - vector components
58 #   * .xyzw, .rgba, .stpq
59 #   * illegal to mix
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
64 #   * can also write!
65 # - matrix columns
66 #   * [] accessor
67 #   * note: mat4[0].x = 1.0; vs mat4[0][0] = 1.0; ??
68 #   * out-of-bounds accesses
69 #
70 # \todo [petri] Accessors!
71 #
72 # Spec issues:
73 #
74 # - constructing larger vector from smaller: vec3(vec2) ?
75 # - base type and size conversion at same time: vec4(bool), int(vec3) allowed?
76
77 def combineVec(comps):
78         res = []
79         for ndx in range(len(comps[0])):
80 #               for x in comps:
81 #                       print x[ndx].toFloat().getScalars() ,
82                 scalars = reduce(operator.add, [x[ndx].toFloat().getScalars() for x in comps])
83 #               print "->", scalars
84                 res.append(Vec.fromScalarList(scalars))
85         return res
86
87 def combineIVec(comps):
88         res = []
89         for ndx in range(len(comps[0])):
90                 res.append(Vec.fromScalarList(reduce(operator.add, [x[ndx].toInt().getScalars() for x in comps])))
91         return res
92
93 def combineUVec(comps):
94         return [x.toUint() for x in combineIVec(comps)]
95
96 def combineBVec(comps):
97         res = []
98         for ndx in range(len(comps[0])):
99                 res.append(Vec.fromScalarList(reduce(operator.add, [x[ndx].toBool().getScalars() for x in comps])))
100         return res
101
102 def combineMat(numCols, numRows, comps):
103         res = []
104         for ndx in range(len(comps[0])):
105                 scalars = reduce(operator.add, [x[ndx].toFloat().getScalars() for x in comps])
106                 res.append(Mat(numCols, numRows, scalars))
107         return res
108
109 def combineMat2(comps):         return combineMat(2, 2, comps)
110 def combineMat2x3(comps):       return combineMat(2, 3, comps)
111 def combineMat2x4(comps):       return combineMat(2, 4, comps)
112 def combineMat3x2(comps):       return combineMat(3, 2, comps)
113 def combineMat3(comps):         return combineMat(3, 3, comps)
114 def combineMat3x4(comps):       return combineMat(3, 4, comps)
115 def combineMat4x2(comps):       return combineMat(4, 2, comps)
116 def combineMat4x3(comps):       return combineMat(4, 3, comps)
117 def combineMat4(comps):         return combineMat(4, 4, comps)
118
119 # 0 \+ [f*f for f in lst]
120 # r = 0 \+ [f in lst -> f*f]
121 # r = 0 \+ lst
122
123 # Templates.
124
125 s_simpleCaseTemplate = """
126 case ${{NAME}}
127         version 300 es
128         values
129         {
130                 ${{VALUES}}
131         }
132
133         both ""
134                 #version 300 es
135                 precision mediump float;
136                 precision mediump int;
137
138                 ${DECLARATIONS}
139
140                 void main()
141                 {
142                         ${SETUP}
143                         ${{OP}}
144                         ${OUTPUT}
145                 }
146         ""
147 end
148 """[1:]
149
150 s_simpleIllegalCaseTemplate = """
151 case ${{NAME}}
152         version 300 es
153         expect compile_fail
154         values {}
155
156         both ""
157                 #version 300 es
158                 precision mediump float;
159                 precision mediump int;
160
161                 ${DECLARATIONS}
162
163                 void main()
164                 {
165                         ${SETUP}
166                         ${{OP}}
167                         ${OUTPUT}
168                 }
169         ""
170 end
171 """[1:]
172
173 class SimpleCase(ShaderCase):
174         def __init__(self, name, inputs, outputs, op):
175                 self.name               = name
176                 self.inputs             = inputs
177                 self.outputs    = outputs
178                 self.op                 = op
179
180         def __str__(self):
181                 params = {
182                         "NAME":         self.name,
183                         "VALUES":       genValues(self.inputs, self.outputs),
184                         "OP":           self.op
185                 }
186                 return fillTemplate(s_simpleCaseTemplate, params)
187
188 class ConversionCase(ShaderCase):
189         def __init__(self, inValues, convFunc):
190                 outValues = convFunc(inValues)
191                 inType  = inValues[0].typeString()
192                 outType = outValues[0].typeString()
193                 self.name               = "%s_to_%s" % (inType, outType)
194                 self.op                 = "out0 = %s(in0);" % outType
195                 self.inputs             = [("%s in0" % inType, inValues)]
196                 self.outputs    = [("%s out0" % outType, outValues)]
197
198         def __str__(self):
199                 params = {
200                         "NAME":         self.name,
201                         "VALUES":       genValues(self.inputs, self.outputs),
202                         "OP":           self.op
203                 }
204                 return fillTemplate(s_simpleCaseTemplate, params)
205
206 class IllegalConversionCase(ShaderCase):
207         def __init__(self, inValue, outValue):
208                 inType  = inValue.typeString()
209                 outType = outValue.typeString()
210                 self.name               = "%s_to_%s" % (inType, outType)
211                 self.op                 = "%s in0 = %s;\n%s out0 = %s(in0);" % (inType, str(inValue), outType, outType)
212                 self.inType             = inType
213                 self.outType    = outType
214
215         def __str__(self):
216                 params = {
217                         "NAME":         self.name,
218                         "OP":           self.op
219                 }
220                 return fillTemplate(s_simpleIllegalCaseTemplate, params)
221
222 class CombineCase(ShaderCase):
223         def __init__(self, inComps, combFunc):
224                 self.inComps    = inComps
225                 self.outValues  = combFunc(inComps)
226                 self.outType    = self.outValues[0].typeString()
227                 inTypes = [values[0].typeString() for values in inComps]
228                 self.name               = "%s_to_%s" % ("_".join(inTypes), self.outType)
229                 self.inputs             = [("%s in%s" % (comp[0].typeString(), ndx), comp) for (comp, ndx) in zip(inComps, indices)]
230                 self.outputs    = [("%s out0" % self.outType, self.outValues)]
231                 self.op                 = "out0 = %s(%s);" % (self.outType, ", ".join(["in%d" % x for x in range(len(inComps))]))
232
233         def __str__(self):
234                 params = {
235                         "NAME":         self.name,
236                         "VALUES":       genValues(self.inputs, self.outputs),
237                         "OP":           self.op
238                 }
239                 return fillTemplate(s_simpleCaseTemplate, params)
240
241 # CASE DECLARATIONS
242
243 def toPos (value):
244         if isinstance(value, list):
245                 return [toPos(x) for x in value]
246         else:
247                 return GenMath.abs(value)
248
249 inFloat = [Scalar(x) for x in [0.0, 1.0, 2.0, 3.5, -0.5, -8.25, -20.125, 36.8125]]
250 inInt   = [Scalar(x) for x in [0, 1, 2, 5, 8, 11, -12, -66, -192, 255]]
251 inUint  = [Uint(x) for x in [0, 2, 3, 8, 9, 12, 10, 45, 193, 255]]
252 inBool  = [Scalar(x) for x in [True, False]]
253
254 inVec4  = [Vec4(0.0, 0.5, 0.75, 0.825), Vec4(1.0, 1.25, 1.125, 1.75),
255                    Vec4(-0.5, -2.25, -4.875, 9.0), Vec4(-32.0, 64.0, -51.0, 24.0),
256                    Vec4(-0.75, -1.0/31.0, 1.0/19.0, 1.0/4.0)]
257 inVec3  = toVec3(inVec4)
258 inVec2  = toVec2(inVec4)
259 inIVec4 = toIVec4(inVec4)
260 inIVec3 = toIVec3(inVec4)
261 inIVec2 = toIVec2(inVec4)
262 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)]
263 inBVec3 = toBVec3(inBVec4)
264 inBVec2 = toBVec2(inBVec4)
265 inUVec4 = toUVec4(toPos(inVec4))
266 inUVec3 = toUVec3(toPos(inVec3))
267 inUVec2 = toUVec2(toPos(inVec2))
268
269 # \todo [petri] Enable large values when epsilon adapts to the values.
270 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),
271                    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),
272                    #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)
273                    ]
274 inMat2  = [Mat2(1.0, 0.0,  0.0, 1.0),
275                    Mat2(6.5, 12.5,  -0.75, 9.975),
276                    Mat2(6.5, 12.5,  -0.75, 9.975),
277                    Mat2(8.0, 16.0,  -24.0, -16.0),
278                    Mat2(1.0/8.0, 1.0/16.0,  1.0/32.0, 1.0/64.0),
279                    Mat2(-18.725, -0.5,  -0.0125, 19.975),
280                    #Mat2(128.0, -4096.0,  192.0, -1536.0),
281                    #Mat2(-1536.0, 8192.0,  6144.0, -6144.0)
282                    ]
283
284 inMat4x3        = toMat4x3(inMat4)
285 inMat4x2        = toMat4x2(inMat4)
286 inMat3x4        = toMat3x4(inMat4)
287 inMat3          = toMat3(inMat4)
288 inMat3x2        = toMat3x2(inMat4)
289 inMat2x4        = toMat2x4(inMat4)
290 inMat2x3        = toMat2x3(inMat4)
291
292 def genConversionCases(inValueList, convFuncList):
293         combinations = list(itertools.product(inValueList, convFuncList))
294         return [ConversionCase(inValues, convFunc) for (inValues, convFunc) in combinations]
295
296 def genIllegalConversionCases(inValueList, outValueList):
297         inValues        = [x[0] for x in inValueList]
298         outValues       = [x[0] for x in outValueList]
299         combinations = list(itertools.product(inValues, outValues))
300         return [IllegalConversionCase(inVal, outVal) for (inVal, outVal) in combinations]
301
302 def shuffleSubLists(outer):
303         return [shuffled(inner) for inner in outer]
304
305 # Generate all combinations of CombineCases.
306 # inTupleList   a list of tuples of value-lists
307 # combFuncList  a list of comb* functions to combine
308 def genComponentCases(inCompLists, combFuncList):
309         res = []
310         for comps in inCompLists:
311                 maxLen = reduce(max, [len(values) for values in comps])
312                 comps = [repeatToLength(values, maxLen) for values in comps]
313                 comps = [shuffled(values) for values in comps]
314                 for combFunc in combFuncList:
315                         res += [CombineCase(comps, combFunc)]
316         return res
317
318 allConversionCases = []
319
320 # Scalar-to-scalar conversions.
321 allConversionCases.append(CaseGroup("scalar_to_scalar", "Scalar to Scalar Conversions",
322         genConversionCases([inFloat, inInt, inUint, inBool], [toFloat, toInt, toBool]) +\
323         genConversionCases([toPos(inFloat), toPos(inInt), inUint, inBool], [toUint])))
324
325 # Scalar-to-vector conversions.
326 allConversionCases.append(CaseGroup("scalar_to_vector", "Scalar to Vector Conversions",
327         genConversionCases([inFloat, inInt, inUint, inBool], [toVec2, toVec3, toVec4, toIVec2, toIVec3, toIVec4, toBVec2, toBVec3, toBVec4]) +\
328         genConversionCases([toPos(inFloat), toPos(inInt), inUint, inBool], [toUVec2, toUVec3, toUVec4])))
329
330 # Vector-to-scalar conversions.
331 allConversionCases.append(CaseGroup("vector_to_scalar", "Vector to Scalar Conversions",
332         genConversionCases([inVec2, inVec3, inVec4, inIVec2, inIVec3, inIVec4, inUVec2, inUVec3, inUVec4, inBVec2, inBVec3, inBVec4], [toFloat, toInt, toBool]) +\
333         genConversionCases([toPos(inVec2), toPos(inVec3), toPos(inVec4), toPos(inIVec2), toPos(inIVec3), toPos(inIVec4), inUVec2, inUVec3, inUVec4, inBVec2, inBVec3, inBVec4], [toUint])))
334
335 # Illegal vector-to-vector conversions (to longer vec).
336 allConversionCases.append(CaseGroup("vector_illegal", "Illegal Vector Conversions",
337         genIllegalConversionCases([inVec2, inIVec2, inUVec2, inBVec2], [inVec3, inIVec3, inUVec3, inBVec3, inVec4, inIVec4, inUVec4, inBVec4]) +\
338         genIllegalConversionCases([inVec3, inIVec3, inUVec3, inBVec3], [inVec4, inIVec4, inUVec4, inBVec4])))
339
340 # Vector-to-vector conversions (type conversions, downcasts).
341 allConversionCases.append(CaseGroup("vector_to_vector", "Vector to Vector Conversions",
342         genConversionCases([inVec4, inIVec4, inUVec4, inBVec4], [toVec4, toVec3, toVec2, toIVec4, toIVec3, toIVec2, toBVec4, toBVec3, toBVec2]) +\
343         genConversionCases([toPos(inVec4), toPos(inIVec4), inUVec4, inBVec4], [toUVec4, toUVec3, toUVec2]) +\
344         genConversionCases([inVec3, inIVec3, inUVec3, inBVec3], [toVec3, toVec2, toIVec3, toIVec2, toBVec3, toBVec2]) +\
345         genConversionCases([toPos(inVec3), toPos(inIVec3), inUVec3, inBVec3], [toUVec3, toUVec2]) +\
346         genConversionCases([inVec2, inIVec2, inUVec2, inBVec2], [toVec2, toIVec2, toBVec2]) +\
347         genConversionCases([toPos(inVec2), toPos(inIVec2), inUVec2, inBVec2], [toUVec2])))
348
349 # Scalar-to-matrix.
350 allConversionCases.append(CaseGroup("scalar_to_matrix", "Scalar to Matrix Conversions",
351         genConversionCases([inFloat, inInt, inUint, inBool], [toMat4, toMat4x3, toMat4x2, toMat3x4, toMat3, toMat3x2, toMat2x4, toMat2x3, toMat2])))
352
353 # Vector-to-matrix.
354 #allConversionCases += genConversionCases([inVec4, inIVec4, inBVec4], [toMat4])
355 #allConversionCases += genConversionCases([inVec3, inIVec3, inBVec3], [toMat3])
356 #allConversionCases += genConversionCases([inVec2, inIVec2, inBVec2], [toMat2])
357
358 # Matrix-to-matrix.
359 allConversionCases.append(CaseGroup("matrix_to_matrix", "Matrix to Matrix Conversions",
360         genConversionCases([inMat4, inMat4x3, inMat4x2, inMat3x4, inMat3, inMat3x2, inMat2x4, inMat2x3, inMat2], [toMat4, toMat4x3, toMat4x2, toMat3x4, toMat3, toMat3x2, toMat2x4, toMat2x3, toMat2])))
361
362 # Vector-from-components, matrix-from-components.
363 in2Comp         = [[inFloat, inFloat], [inInt, inInt], [inUint, inUint], [inBool, inBool], [inFloat, inInt], [inFloat, inBool], [inInt, inBool], [inInt, inUint], [inUint, inFloat]]
364 in3Comp         = [[inFloat, inFloat, inFloat], [inInt, inInt, inInt], [inUint, inUint, inUint], [inBool, inBool, inBool], [inBool, inFloat, inInt], [inVec2, inBool], [inBVec2, inFloat], [inBVec2, inInt], [inBool, inIVec2], [inFloat, inUVec2]]
365 in4Comp         = [[inVec2, inVec2], [inBVec2, inBVec2], [inFloat, inFloat, inFloat, inFloat], [inInt, inInt, inInt, inInt], [inUint, inUint, inUint, inUint], [inBool, inBool, inBool, inBool], [inBool, inFloat, inInt, inBool], [inVec2, inIVec2], [inVec2, inBVec2], [inBVec3, inFloat], [inVec3, inFloat], [inInt, inIVec2, inInt], [inBool, inFloat, inIVec2], [inFloat, inUVec3], [inInt, inUVec2, inBool]]
366 in6Comp         = [[inVec3, inVec3], [inBVec3, inBVec3], [inFloat, inFloat, inFloat, inFloat, inFloat, inFloat], [inInt, inInt, inInt, inInt, inInt, inInt], [inBool, inBool, inBool, inBool, inBool, inBool], [inBool, inFloat, inInt, inBool, inFloat, inInt], [inVec3, inIVec3], [inVec2, inBVec4], [inBVec3, inFloat, inIVec2], [inVec3, inFloat, inBVec2]]
367 in8Comp         = [[inVec3, inVec3, inVec2], [inIVec3, inIVec3, inIVec2], [inVec2, inIVec2, inFloat, inFloat, inInt, inBool], [inBool, inFloat, inInt, inVec2, inBool, inBVec2], [inBool, inBVec2, inInt, inVec4], [inFloat, inBVec4, inIVec2, inBool]]
368 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]]
369 in12Comp        = [[inVec4, inVec4, inVec4], [inIVec4, inIVec4, inIVec4], [inVec2, inIVec2, inFloat, inFloat, inFloat, inInt, inInt, inBool, inBool, inBool], [inBool, inFloat, inInt, inVec3, inBool, inBVec3, inFloat, inBool], [inBool, inBVec4, inInt, inVec4, inBool, inFloat], [inFloat, inBVec4, inIVec4, inBool, inBool, inInt]]
370 in16Comp        = [[inVec4, inVec4, inVec4, inVec4], [inIVec4, inIVec4, inIVec4, inIVec4], [inBVec4, inBVec4, inBVec4, inBVec4], [inFloat, inIVec3, inBVec3, inVec4, inIVec2, inFloat, inVec2]]
371
372 allConversionCases.append(CaseGroup("vector_combine", "Vector Combine Constructors",
373         genComponentCases(in4Comp, [combineVec, combineIVec, combineBVec]) +\
374         genComponentCases(toPos(in4Comp), [combineUVec]) +\
375         genComponentCases(in3Comp, [combineVec, combineIVec, combineBVec]) +\
376         genComponentCases(toPos(in3Comp), [combineUVec]) +\
377         genComponentCases(in2Comp, [combineVec, combineIVec, combineBVec]) +\
378         genComponentCases(toPos(in2Comp), [combineUVec])))
379
380 allConversionCases.append(CaseGroup("matrix_combine", "Matrix Combine Constructors",
381         genComponentCases(in4Comp,      [combineMat2])          +\
382         genComponentCases(in6Comp,      [combineMat2x3])        +\
383         genComponentCases(in8Comp,      [combineMat2x4])        +\
384         genComponentCases(in6Comp,      [combineMat3x2])        +\
385         genComponentCases(in9Comp,      [combineMat3])          +\
386         genComponentCases(in12Comp,     [combineMat3x4])        +\
387         genComponentCases(in8Comp,      [combineMat4x2])        +\
388         genComponentCases(in12Comp,     [combineMat4x3])        +\
389         genComponentCases(in16Comp, [combineMat4])
390         ))
391
392 # Main program.
393
394 if __name__ == "__main__":
395         print "Generating shader case files."
396         writeAllCases("conversions.test", allConversionCases)