xref: /aosp_15_r20/external/deqp/modules/gles2/scripts/gen-conversions.py (revision 35238bce31c2a825756842865a792f8cf7f89930)
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
23import sys
24import random
25import operator
26import itertools
27
28from genutil import *
29
30random.seed(1234567)
31indices = 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
77def 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
87def 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
93def combineBVec(comps):
94    res = []
95    for ndx in range(len(comps[0])):
96        res.append(Vec.fromScalarList(reduce(operator.add, [x[ndx].toBool().getScalars() for x in comps])))
97    return res
98
99def combineMat(numCols, numRows, comps):
100    res = []
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))
104    return res
105
106def combineMat2(comps): return combineMat(2, 2, comps)
107def combineMat3(comps): return combineMat(3, 3, comps)
108def combineMat4(comps): return combineMat(4, 4, comps)
109
110# 0 \+ [f*f for f in lst]
111# r = 0 \+ [f in lst -> f*f]
112# r = 0 \+ lst
113
114# Templates.
115
116s_simpleCaseTemplate = """
117case ${{NAME}}
118    values
119    {
120        ${{VALUES}}
121    }
122
123    both ""
124        precision mediump float;
125        precision mediump int;
126
127        ${DECLARATIONS}
128
129        void main()
130        {
131            ${SETUP}
132            ${{OP}}
133            ${OUTPUT}
134        }
135    ""
136end
137"""[1:]
138
139s_simpleIllegalCaseTemplate = """
140case ${{NAME}}
141    expect compile_fail
142    values {}
143
144    both ""
145        precision mediump float;
146        precision mediump int;
147
148        ${DECLARATIONS}
149
150        void main()
151        {
152            ${SETUP}
153            ${{OP}}
154            ${OUTPUT}
155        }
156    ""
157end
158"""[1:]
159
160class SimpleCase(ShaderCase):
161    def __init__(self, name, inputs, outputs, op):
162        self.name = name
163        self.inputs = inputs
164        self.outputs = outputs
165        self.op = op
166
167    def __str__(self):
168        params = {
169            "NAME": self.name,
170            "VALUES": genValues(self.inputs, self.outputs),
171            "OP": self.op
172        }
173        return fillTemplate(s_simpleCaseTemplate, params)
174
175class 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)]
184
185    def __str__(self):
186        params = {
187            "NAME": self.name,
188            "VALUES": genValues(self.inputs, self.outputs),
189            "OP": self.op
190        }
191        return fillTemplate(s_simpleCaseTemplate, params)
192
193class 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)
199        self.inType = inType
200        self.outType = outType
201
202    def __str__(self):
203        params = {
204            "NAME": self.name,
205            "OP": self.op
206        }
207        return fillTemplate(s_simpleIllegalCaseTemplate, params)
208
209class 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))]))
219
220    def __str__(self):
221        params = {
222            "NAME": self.name,
223            "VALUES": genValues(self.inputs, self.outputs),
224            "OP": self.op
225        }
226        return fillTemplate(s_simpleCaseTemplate, params)
227
228# CASE DECLARATIONS
229
230inFloat = [Scalar(x) for x in [0.0, 1.0, 2.0, 3.5, -0.5, -8.25, -20.125, 36.8125]]
231inInt = [Scalar(x) for x in [0, 1, 2, 5, 8, 11, -12, -66, -192, 255]]
232inBool = [Scalar(x) for x in [True, False]]
233
234inVec4 = [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)]
237inVec3 = toVec3(inVec4)
238inVec2 = toVec2(inVec4)
239inIVec4 = toIVec4(inVec4)
240inIVec3 = toIVec3(inVec4)
241inIVec2 = toIVec2(inVec4)
242inBVec4 = [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)]
243inBVec3 = toBVec3(inBVec4)
244inBVec2 = toBVec2(inBVec4)
245
246# \todo [petri] Enable large values when epsilon adapts to the values.
247inMat4 = [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)
250           ]
251inMat3 = [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)
255           ]
256inMat2 = [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)
264           ]
265
266def genConversionCases(inValueList, convFuncList):
267    combinations = list(itertools.product(inValueList, convFuncList))
268    return [ConversionCase(inValues, convFunc) for (inValues, convFunc) in combinations]
269
270def 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]
275
276def shuffleSubLists(outer):
277    return [shuffled(inner) for inner in outer]
278
279# Generate all combinations of CombineCases.
280# inTupleList    a list of tuples of value-lists
281# combFuncList    a list of comb* functions to combine
282def genComponentCases(inCompLists, combFuncList):
283    res = []
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)]
290    return res
291
292allConversionCases = []
293
294# Scalar-to-scalar conversions.
295allConversionCases.append(CaseGroup("scalar_to_scalar", "Scalar to Scalar Conversions",
296    genConversionCases([inFloat, inInt, inBool], [toFloat, toInt, toBool])))
297
298# Scalar-to-vector conversions.
299allConversionCases.append(CaseGroup("scalar_to_vector", "Scalar to Vector Conversions",
300    genConversionCases([inFloat, inInt, inBool], [toVec2, toVec3, toVec4, toIVec2, toIVec3, toIVec4, toBVec2, toBVec3, toBVec4])))
301
302# Vector-to-scalar conversions.
303allConversionCases.append(CaseGroup("vector_to_scalar", "Vector to Scalar Conversions",
304    genConversionCases([inVec2, inVec3, inVec4, inIVec2, inIVec3, inIVec4, inBVec2, inBVec3, inBVec4], [toFloat, toInt, toBool])))
305
306# Illegal vector-to-vector conversions (to longer vec).
307allConversionCases.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])))
310
311# Vector-to-vector conversions (type conversions, downcasts).
312allConversionCases.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])))
316
317# Scalar-to-matrix.
318allConversionCases.append(CaseGroup("scalar_to_matrix", "Scalar to Matrix Conversions",
319    genConversionCases([inFloat, inInt, inBool], [toMat4, toMat3, toMat2])))
320
321# Vector-to-matrix.
322#allConversionCases += genConversionCases([inVec4, inIVec4, inBVec4], [toMat4])
323#allConversionCases += genConversionCases([inVec3, inIVec3, inBVec3], [toMat3])
324#allConversionCases += genConversionCases([inVec2, inIVec2, inBVec2], [toMat2])
325
326# Matrix-to-matrix.
327allConversionCases.append(CaseGroup("matrix_to_matrix", "Matrix to Matrix Conversions",
328    genConversionCases([inMat4, inMat3, inMat2], [toMat4, toMat3, toMat2])))
329
330# Vector-from-components, matrix-from-components.
331in2Comp = [[inFloat, inFloat], [inInt, inInt], [inBool, inBool], [inFloat, inInt], [inFloat, inBool], [inInt, inBool]]
332in3Comp = [[inFloat, inFloat, inFloat], [inInt, inInt, inInt], [inBool, inBool, inBool], [inBool, inFloat, inInt], [inVec2, inBool], [inBVec2, inFloat], [inBVec2, inInt], [inBool, inIVec2]]
333in4Comp = [[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]]
334in9Comp = [[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]]
335in16Comp = [[inVec4, inVec4, inVec4, inVec4], [inIVec4, inIVec4, inIVec4, inIVec4], [inBVec4, inBVec4, inBVec4, inBVec4], [inFloat, inIVec3, inBVec3, inVec4, inIVec2, inFloat, inVec2]]
336
337allConversionCases.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])))
341
342allConversionCases.append(CaseGroup("matrix_combine", "Matrix Combine Constructors",
343    genComponentCases(in4Comp, [combineMat2]) +\
344    genComponentCases(in9Comp, [combineMat3]) +\
345    genComponentCases(in16Comp, [combineMat4])
346    ))
347
348# Main program.
349
350if __name__ == "__main__":
351    print("Generating shader case files.")
352    writeAllCases("conversions.test", allConversionCases)
353