Merge vk-gl-cts/opengl-cts-4.6.0 into vk-gl-cts/master
[platform/upstream/VK-GL-CTS.git] / modules / gles2 / scripts / gen-swizzles.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 # Swizzles:
34 # - vector components
35 #   * int, float, bool vectors
36 #   * .xyzw, .rgba, .stpq
37 #   * illegal to mix
38 #   * not allowed for scalar types
39 #   * legal to chain: vec4.rgba.xyzw.stpq
40 #   * illegal to select more than 4 components
41 #
42 # Subscripts:
43 # - array-like indexing with [] operator
44 #   * vectors, matrices
45 # - read & write
46 # - vectors components
47 #   * [] accessor
48 # - matrix columns
49 #   * [] accessor
50 #   * note: mat4[0].x = 1.0; vs mat4[0][0] = 1.0; ??
51 #   * out-of-bounds accesses
52
53 #
54 # - vector swizzles
55 #   * all vector types (bvec2..4, ivec2..4, vec2..4)
56 #   * all precisions (lowp, mediump, highp)
57 #   * all component names (xyzw, rgba, stpq)
58 #   * broadcast each, reverse, N random
59 # - component-masked writes
60 #   * all vector types (bvec2..4, ivec2..4, vec2..4)
61 #   * all precisions (lowp, mediump, highp)
62 #   * all component names (xyzw, rgba, stpq)
63 #   * all possible subsets
64 #   * all input types (attribute, varying, uniform, tmp)
65 #   -> a few hundred cases
66 # - concatenated swizzles
67
68 #
69 VECTOR_TYPES    = [ "vec2", "vec3", "vec4", "ivec2", "ivec3", "ivec4", "bvec2", "bvec3", "bvec4" ]
70 PRECISION_TYPES = [ "lowp", "mediump", "highp" ]
71 INPUT_TYPES             = [ "uniform", "varying", "attribute", "tmp" ]
72 SWIZZLE_NAMES   = [ "xyzw", "stpq", "rgba" ]
73
74 def getDataTypeScalarSize (dt):
75         return {
76                 "float":        1,
77                 "vec2":         2,
78                 "vec3":         3,
79                 "vec4":         4,
80                 "int":          1,
81                 "ivec2":        2,
82                 "ivec3":        3,
83                 "ivec4":        4,
84                 "bool":         1,
85                 "bvec2":        2,
86                 "bvec3":        3,
87                 "bvec4":        4,
88                 "mat2":         4,
89                 "mat3":         9,
90                 "mat4":         16
91         }[dt]
92
93 if False:
94         class Combinations:
95                 def __init__(self, *args):
96                         self.lists                              = list(args)
97                         self.numLists                   = len(args)
98                         self.numCombinations    = reduce(operator.mul, map(len, self.lists), 1)
99                         print self.lists
100                         print self.numCombinations
101
102                 def iterate(self):
103                         return [tuple(map(lambda x: x[0], self.lists))]
104
105         combinations = Combinations(INPUT_TYPES, VECTOR_TYPES, PRECISION_TYPES)
106         print combinations.iterate()
107         for (inputType, dataType, precision) in combinations.iterate():
108                 scalarSize = getDataTypeScalarSize(dataType)
109                 print inputType, precision, dataType
110
111 def getSwizzlesForWidth(width):
112         if (width == 2):
113                 return [(0,), (0,0), (0,1), (1,0), (1,0,1), (0,1,0,0), (1,1,1,1)]
114         elif (width == 3):
115                 return [(0,), (2,), (0,2), (2,2), (0,1,2), (2,1,0), (0,0,0), (2,2,2), (2,2,1), (1,0,1), (0,2,0), (0,1,1,0), (2,2,2,2)]
116         elif (width == 4):
117                 return [(0,), (3,), (3,0), (3,2), (3,3,3), (1,1,3), (3,2,1), (0,1,2,3), (3,2,1,0), (0,0,0,0), (1,1,1,1), (3,3,3,3), (3,2,2,3), (3,3,3,1), (0,1,0,0), (2,2,3,2)]
118         else:
119                 assert False
120
121 # Templates.
122
123 s_swizzleCaseTemplate = """
124 case ${{NAME}}
125         values
126         {
127                 ${{VALUES}}
128         }
129
130         both ""
131                 precision mediump float;
132
133                 ${DECLARATIONS}
134
135                 void main()
136                 {
137                         ${SETUP}
138                         ${{OP}}
139                         ${OUTPUT}
140                 }
141         ""
142 end
143 """[1:]
144
145 s_simpleIllegalCaseTemplate = """
146 case ${{NAME}}
147         expect compile_fail
148         values {}
149
150         both ""
151                 precision mediump float;
152                 precision mediump int;
153
154                 ${DECLARATIONS}
155
156                 void main()
157                 {
158                         ${SETUP}
159                         ${{OP}}
160                         ${OUTPUT}
161                 }
162         ""
163 end
164 """[1:]
165
166 class SwizzleCase(ShaderCase):
167         def __init__(self, name, precision, dataType, swizzle, inputs, outputs):
168                 self.name               = name
169                 self.precision  = precision
170                 self.dataType   = dataType
171                 self.swizzle    = swizzle
172                 self.inputs             = inputs
173                 self.outputs    = outputs
174                 self.op                 = "out0 = in0.%s;" % swizzle
175
176         def __str__(self):
177                 params = {
178                         "NAME":         self.name,
179                         "VALUES":       genValues(self.inputs, self.outputs),
180                         "OP":           self.op
181                 }
182                 return fillTemplate(s_swizzleCaseTemplate, params)
183
184 # CASE DECLARATIONS
185
186 inFloat = [Scalar(x) for x in [0.0, 1.0, 2.0, 3.5, -0.5, -20.125, 36.8125]]
187 inInt   = [Scalar(x) for x in [0, 1, 2, 5, 8, 11, -12, -66, -192, 255]]
188 inBool  = [Scalar(x) for x in [True, False]]
189
190 inVec4  = [Vec4(0.0, 0.5, 0.75, 0.825), Vec4(1.0, 1.25, 1.125, 1.75),
191                    Vec4(-0.5, -2.25, -4.875, 9.0), Vec4(-32.0, 64.0, -51.0, 24.0),
192                    Vec4(-0.75, -1.0/31.0, 1.0/19.0, 1.0/4.0)]
193 inVec3  = toVec3(inVec4)
194 inVec2  = toVec2(inVec4)
195 inIVec4 = toIVec4(inVec4)
196 inIVec3 = toIVec3(inVec4)
197 inIVec2 = toIVec2(inVec4)
198 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)]
199 inBVec3 = toBVec3(inBVec4)
200 inBVec2 = toBVec2(inBVec4)
201
202 # \todo [petri] Enable large values when epsilon adapts to the values.
203 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),
204                    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),
205                    #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)
206                    ]
207 inMat3  = [Mat3(1.0, 0.0, 0.0,  0.0, 1.0, 0.0,  0.0, 0.0, 1.0),
208                    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),
209                    #Mat3(-18.725, -0.5, -0.0125,  19.975, -0.25, -17.75,  9.25, 65.125, -21.425),
210                    #Mat3(128.0, -4096.0, -8192.0,  192.0, 768.0, -1536.0,  8192.0, 6144.0, -6144.0)
211                    ]
212 inMat2  = [Mat2(1.0, 0.0,  0.0, 1.0),
213                    Mat2(6.5, 12.5,  -0.75, 9.975),
214                    Mat2(6.5, 12.5,  -0.75, 9.975),
215                    Mat2(8.0, 16.0,  -24.0, -16.0),
216                    Mat2(1.0/8.0, 1.0/16.0,  1.0/32.0, 1.0/64.0),
217                    Mat2(-18.725, -0.5,  -0.0125, 19.975),
218                    #Mat2(128.0, -4096.0,  192.0, -1536.0),
219                    #Mat2(-1536.0, 8192.0,  6144.0, -6144.0)
220                    ]
221
222 INPUTS = {
223         "float":        inFloat,
224         "vec2":         inVec2,
225         "vec3":         inVec3,
226         "vec4":         inVec4,
227         "int":          inInt,
228         "ivec2":        inIVec2,
229         "ivec3":        inIVec3,
230         "ivec4":        inIVec4,
231         "bool":         inBool,
232         "bvec2":        inBVec2,
233         "bvec3":        inBVec3,
234         "bvec4":        inBVec4,
235         "mat2":         inMat2,
236         "mat3":         inMat3,
237         "mat4":         inMat4
238 }
239
240 def genConversionCases(inValueList, convFuncList):
241         combinations = list(itertools.product(inValueList, convFuncList))
242         return [ConversionCase(inValues, convFunc) for (inValues, convFunc) in combinations]
243
244 allCases = []
245
246 # Vector swizzles.
247
248 vectorSwizzleCases = []
249
250 # \todo [petri] Uses fixed precision.
251 for dataType in VECTOR_TYPES:
252         scalarSize = getDataTypeScalarSize(dataType)
253         precision = "mediump"
254         for swizzleComponents in SWIZZLE_NAMES:
255                 for swizzleIndices in getSwizzlesForWidth(scalarSize):
256                         swizzle = "".join(map(lambda x: swizzleComponents[x], swizzleIndices))
257                         #print "%s %s .%s" % (precision, dataType, swizzle)
258                         caseName = "%s_%s_%s" % (precision, dataType, swizzle)
259                         inputs = INPUTS[dataType]
260                         outputs = map(lambda x: x.swizzle(swizzleIndices), inputs)
261                         outType = outputs[0].typeString()
262                         vectorSwizzleCases.append(SwizzleCase(caseName, precision, dataType, swizzle, [("%s in0" % dataType, inputs)], [("%s out0" % outType, outputs)]))
263
264 # ??
265 #for dataType in VECTOR_TYPES:
266 #       scalarSize = getDataTypeScalarSize(dataType)
267 #       for precision in PRECISION_TYPES:
268 #               for swizzleIndices in getSwizzlesForWidth(scalarSize):
269 #                       swizzle = "".join(map(lambda x: "xyzw"[x], swizzleIndices))
270 #                       #print "%s %s .%s" % (precision, dataType, swizzle)
271 #                       caseName = "%s_%s_%s" % (precision, dataType, swizzle)
272 #                       inputs = INPUTS[dataType]
273 #                       outputs = map(lambda x: x.swizzle(swizzleIndices), inputs)
274 #                       vectorSwizzleCases.append(SwizzleCase(caseName, precision, dataType, swizzle, [("in0", inputs)], [("out0", outputs)]))
275
276 allCases.append(CaseGroup("vector_swizzles", "Vector Swizzles", vectorSwizzleCases))
277
278 # Swizzles:
279 # - vector components
280 #   * int, float, bool vectors
281 #   * .xyzw, .rgba, .stpq
282 #   * illegal to mix
283 #   * not allowed for scalar types
284 #   * legal to chain: vec4.rgba.xyzw.stpq
285 #   * illegal to select more than 4 components
286
287 # TODO: precisions!!
288
289 #allCases.append(CaseGroup("vector_swizzles", "Vector Swizzles",
290 #       genSwizzleCase([inVec2, inVec3, inVec4],
291
292 # Main program.
293
294 if __name__ == "__main__":
295         print "Generating shader case files."
296         writeAllCases("swizzles.test", allCases)