Merge changes I5f7e56e3,I7f88e1da into oreo-cts-dev am: 67694b5635 am: aa425927d0...
[platform/upstream/VK-GL-CTS.git] / modules / gles3 / 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         version 300
126         values
127         {
128                 ${{VALUES}}
129         }
130
131         both ""
132                 #version 300 es
133                 precision mediump float;
134
135                 ${DECLARATIONS}
136
137                 void main()
138                 {
139                         ${SETUP}
140                         ${{OP}}
141                         ${OUTPUT}
142                 }
143         ""
144 end
145 """[1:]
146
147 s_simpleIllegalCaseTemplate = """
148 case ${{NAME}}
149         version 300
150         expect compile_fail
151         values {}
152
153         both ""
154                 #version 300 es
155                 precision mediump float;
156                 precision mediump int;
157
158                 ${DECLARATIONS}
159
160                 void main()
161                 {
162                         ${SETUP}
163                         ${{OP}}
164                         ${OUTPUT}
165                 }
166         ""
167 end
168 """[1:]
169
170 class SwizzleCase(ShaderCase):
171         def __init__(self, name, precision, dataType, swizzle, inputs, outputs):
172                 self.name               = name
173                 self.precision  = precision
174                 self.dataType   = dataType
175                 self.swizzle    = swizzle
176                 self.inputs             = inputs
177                 self.outputs    = outputs
178                 self.op                 = "out0 = in0.%s;" % swizzle
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_swizzleCaseTemplate, params)
187
188 # CASE DECLARATIONS
189
190 inFloat = [Scalar(x) for x in [0.0, 1.0, 2.0, 3.5, -0.5, -20.125, 36.8125]]
191 inInt   = [Scalar(x) for x in [0, 1, 2, 5, 8, 11, -12, -66, -192, 255]]
192 inBool  = [Scalar(x) for x in [True, False]]
193
194 inVec4  = [Vec4(0.0, 0.5, 0.75, 0.825), Vec4(1.0, 1.25, 1.125, 1.75),
195                    Vec4(-0.5, -2.25, -4.875, 9.0), Vec4(-32.0, 64.0, -51.0, 24.0),
196                    Vec4(-0.75, -1.0/31.0, 1.0/19.0, 1.0/4.0)]
197 inVec3  = toVec3(inVec4)
198 inVec2  = toVec2(inVec4)
199 inIVec4 = toIVec4(inVec4)
200 inIVec3 = toIVec3(inVec4)
201 inIVec2 = toIVec2(inVec4)
202 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)]
203 inBVec3 = toBVec3(inBVec4)
204 inBVec2 = toBVec2(inBVec4)
205
206 # \todo [petri] Enable large values when epsilon adapts to the values.
207 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),
208                    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),
209                    #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)
210                    ]
211 inMat3  = [Mat3(1.0, 0.0, 0.0,  0.0, 1.0, 0.0,  0.0, 0.0, 1.0),
212                    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),
213                    #Mat3(-18.725, -0.5, -0.0125,  19.975, -0.25, -17.75,  9.25, 65.125, -21.425),
214                    #Mat3(128.0, -4096.0, -8192.0,  192.0, 768.0, -1536.0,  8192.0, 6144.0, -6144.0)
215                    ]
216 inMat2  = [Mat2(1.0, 0.0,  0.0, 1.0),
217                    Mat2(6.5, 12.5,  -0.75, 9.975),
218                    Mat2(6.5, 12.5,  -0.75, 9.975),
219                    Mat2(8.0, 16.0,  -24.0, -16.0),
220                    Mat2(1.0/8.0, 1.0/16.0,  1.0/32.0, 1.0/64.0),
221                    Mat2(-18.725, -0.5,  -0.0125, 19.975),
222                    #Mat2(128.0, -4096.0,  192.0, -1536.0),
223                    #Mat2(-1536.0, 8192.0,  6144.0, -6144.0)
224                    ]
225
226 INPUTS = {
227         "float":        inFloat,
228         "vec2":         inVec2,
229         "vec3":         inVec3,
230         "vec4":         inVec4,
231         "int":          inInt,
232         "ivec2":        inIVec2,
233         "ivec3":        inIVec3,
234         "ivec4":        inIVec4,
235         "bool":         inBool,
236         "bvec2":        inBVec2,
237         "bvec3":        inBVec3,
238         "bvec4":        inBVec4,
239         "mat2":         inMat2,
240         "mat3":         inMat3,
241         "mat4":         inMat4
242 }
243
244 def genConversionCases(inValueList, convFuncList):
245         combinations = list(itertools.product(inValueList, convFuncList))
246         return [ConversionCase(inValues, convFunc) for (inValues, convFunc) in combinations]
247
248 allCases = []
249
250 # Vector swizzles.
251
252 vectorSwizzleCases = []
253
254 # \todo [petri] Uses fixed precision.
255 for dataType in VECTOR_TYPES:
256         scalarSize = getDataTypeScalarSize(dataType)
257         precision = "mediump"
258         for swizzleComponents in SWIZZLE_NAMES:
259                 for swizzleIndices in getSwizzlesForWidth(scalarSize):
260                         swizzle = "".join(map(lambda x: swizzleComponents[x], swizzleIndices))
261                         #print "%s %s .%s" % (precision, dataType, swizzle)
262                         caseName = "%s_%s_%s" % (precision, dataType, swizzle)
263                         inputs = INPUTS[dataType]
264                         outputs = map(lambda x: x.swizzle(swizzleIndices), inputs)
265                         outType = outputs[0].typeString()
266                         vectorSwizzleCases.append(SwizzleCase(caseName, precision, dataType, swizzle, [("%s in0" % dataType, inputs)], [("%s out0" % outType, outputs)]))
267
268 # ??
269 #for dataType in VECTOR_TYPES:
270 #       scalarSize = getDataTypeScalarSize(dataType)
271 #       for precision in PRECISION_TYPES:
272 #               for swizzleIndices in getSwizzlesForWidth(scalarSize):
273 #                       swizzle = "".join(map(lambda x: "xyzw"[x], swizzleIndices))
274 #                       #print "%s %s .%s" % (precision, dataType, swizzle)
275 #                       caseName = "%s_%s_%s" % (precision, dataType, swizzle)
276 #                       inputs = INPUTS[dataType]
277 #                       outputs = map(lambda x: x.swizzle(swizzleIndices), inputs)
278 #                       vectorSwizzleCases.append(SwizzleCase(caseName, precision, dataType, swizzle, [("in0", inputs)], [("out0", outputs)]))
279
280 allCases.append(CaseGroup("vector_swizzles", "Vector Swizzles", vectorSwizzleCases))
281
282 # Swizzles:
283 # - vector components
284 #   * int, float, bool vectors
285 #   * .xyzw, .rgba, .stpq
286 #   * illegal to mix
287 #   * not allowed for scalar types
288 #   * legal to chain: vec4.rgba.xyzw.stpq
289 #   * illegal to select more than 4 components
290
291 # TODO: precisions!!
292
293 #allCases.append(CaseGroup("vector_swizzles", "Vector Swizzles",
294 #       genSwizzleCase([inVec2, inVec3, inVec4],
295
296 # Main program.
297
298 if __name__ == "__main__":
299         print "Generating shader case files."
300         writeAllCases("swizzles.test", allCases)