cmake: move first batch of option() at the beggining of the file
[piglit.git] / generated_tests / gen_inout_64bit_tests.py
blob5677eb27e2c16ff55f8641a475df975755b50e6f
1 #!/usr/bin/env python3
2 # coding=utf-8
4 # Copyright (c) 2019 Intel Corporation
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 # SOFTWARE.
24 """ Generate in/out tests for 64 bit types. """
26 import os
27 import errno
28 import collections
29 import copy
30 import random_ubo
32 from textwrap import dedent
33 from mako.template import Template
36 class VaryingType(object):
37 __slots__ = ['type', 'members', 'name']
39 def __init__(self, type, members=None):
40 self.type = type
41 self.members = members
42 self.name = None
45 VaryingInstance = collections.namedtuple(
46 'VaryingInstance', ['varying_type', 'setters', 'checkers', 'full_name'])
49 def scalar_assignment(type, name, data):
50 """Return a GLSL code string to assign a scalar to its expected value."""
51 if type == "bool":
52 if int(data) == 0:
53 return "{} = {};".format(name,
54 'false' if int(data) == 0 else 'true')
55 elif type == "uint":
56 return "{} = {}u;".format(name, data)
57 elif type == "int":
58 return "{} = {};".format(name, data)
59 elif type == "uint64_t":
60 return "{} = {}ul;".format(name, data)
61 elif type == "int64_t":
62 return "{} = {}l;".format(name, data)
63 elif type == "float":
64 # Not all implementations support the bit-cast operators that are used
65 # to do bit-exact comparisons. For this reason float_match needs the
66 # float data and the bit-exact data.
68 bits = random_ubo.bit_exact_data(data, "float")
69 return "{} = float_get({}, {}u);".format(name, data, bits)
70 elif type == "double":
71 bits = random_ubo.bit_exact_data(data, "double")
73 # 0xHHHHHHHHLLLLLLLL
74 # 012345678901234567
76 hi = "0x" + bits[2:10]
77 lo = "0x" + bits[10:18]
79 return "{} = double_get(uvec2({}, {}));".format(name, lo, hi)
80 else:
81 raise Exception("Unknown scalar type {}".format(type))
84 def vector_assignment(type, name, data):
85 """Return a list of GLSL code strings that assign each field of a vector
86 to its expected value.
87 """
88 scalar = random_ubo.vector_base_type(type)
89 components = ["x", "y", "z", "w"]
91 return [scalar_assignment(scalar,
92 "{}.{}".format(name, "xyzw"[i]),
93 data[i])
94 for i in range(random_ubo.vector_size(type))]
97 def matrix_assignment(type, name, data):
98 """Return a list of GLSL code strings that assign each field of a matrix
99 its expected value.
101 c, r = random_ubo.matrix_dimensions(type)
103 if type[0] == 'd':
104 column_type = "dvec{}".format(r)
105 else:
106 column_type = "vec{}".format(r)
108 data_pairs = []
110 for i in range(c):
111 data_pairs.extend(vector_assignment(
112 column_type,
113 "{}[{}]".format(name, i),
114 data[(i * r):(i * r) + r]))
116 return data_pairs
119 def create_array(base_type, size):
120 return "{}[{}]".format(base_type, size)
123 def array_base_type(varying_type):
124 t = copy.copy(varying_type)
125 t.type = random_ubo.array_base_type(t.type)
126 t.name = None
127 return t
130 def create_getters_and_setters(varying_type, full_name, instances):
131 if not full_name:
132 full_name = varying_type.name
133 elif varying_type.name is not None:
134 full_name += '.' + varying_type.name
136 if random_ubo.isarray(varying_type.type):
137 base_type = array_base_type(varying_type)
138 for i in range(random_ubo.array_elements(varying_type.type)):
139 indexed_name = full_name + '[' + str(i) + ']'
140 create_getters_and_setters(base_type, indexed_name, instances)
142 elif random_ubo.isstructure(varying_type.type):
143 for m in varying_type.members:
144 create_getters_and_setters(m, full_name, instances)
146 else:
147 v = VaryingInstance(varying_type, [], [], full_name)
149 raw_data = random_ubo.random_data(varying_type.type, full_name, 0)
150 data = raw_data.split(" ")
152 if random_ubo.isscalar(varying_type.type):
153 v.checkers.append(random_ubo.scalar_derp(varying_type.type,
154 full_name,
155 data[0]))
156 v.setters.append(
157 scalar_assignment(varying_type.type, full_name, data[0]))
158 elif random_ubo.isvector(varying_type.type):
159 v.checkers.extend(random_ubo.vector_derp(varying_type.type,
160 full_name,
161 data))
162 v.setters.extend(
163 vector_assignment(varying_type.type, full_name, data))
164 elif random_ubo.ismatrix(varying_type.type):
165 v.checkers.extend(random_ubo.matrix_derp(varying_type.type,
166 full_name,
167 data))
168 v.setters.extend(
169 matrix_assignment(varying_type.type, full_name, data))
171 instances.append(v)
174 def assign_names(varying_type, names):
175 if random_ubo.isarray(varying_type.type):
176 varying_type.name = names.get_name(
177 random_ubo.without_array(varying_type.type))
178 else:
179 varying_type.name = names.get_name(varying_type.type)
181 if varying_type.members:
182 for m in varying_type.members:
183 assign_names(m, names)
186 def gather_structs(varying_type, structs):
187 if random_ubo.isarray(varying_type.type):
188 base_type = array_base_type(varying_type)
189 gather_structs(base_type, structs)
190 elif random_ubo.isstructure(varying_type.type):
191 for m in varying_type.members:
192 gather_structs(m, structs)
194 structs.append(varying_type)
197 def stringify_array_dimensions(type):
198 s = ""
199 base_type = type
200 while random_ubo.isarray(base_type):
201 s += "@{}".format(random_ubo.array_elements(base_type))
202 base_type = random_ubo.array_base_type(base_type)
203 return s
206 def stringify_varying_type(varying_type):
207 if random_ubo.isarray(varying_type.type):
208 name = "{}{}".format(random_ubo.without_array(varying_type.type),
209 stringify_array_dimensions(varying_type.type))
210 else:
211 name = varying_type.type
213 if varying_type.members:
214 for m in varying_type.members:
215 name += "-" + stringify_varying_type(m)
217 return name
220 def generate_file_name(root_types, explicit_locations):
221 name = "vs-out-fs-in-"
222 name += "-and-".join(
223 [stringify_varying_type(t) for t in root_types])
224 if explicit_locations:
225 name += "-location-0"
226 return name + ".shader_test"
229 def do_test(root_types, explicit_locations, glsl_version, extensions,
230 tests_path):
231 if explicit_locations and len(root_types) > 1:
232 return
234 basename = generate_file_name(root_types, explicit_locations)
235 fullname = os.path.join(tests_path, basename)
237 print(fullname)
239 shader_file = open(fullname, "w", buffering=1)
241 names = random_ubo.unique_name_dict()
243 instances = []
244 structs = []
246 for type in root_types:
247 assign_names(type, names)
248 create_getters_and_setters(type, None, instances)
249 gather_structs(type, structs)
251 t = Template(dedent("""\
252 [require]
253 GLSL >= ${glsl_version // 100}.${glsl_version % 100}
254 % for ext in extensions:
255 ${ext}
256 % endfor
258 % if explicit_locations:
259 GL_ARB_explicit_attrib_location
260 % endif
262 [vertex shader]
263 % for ext in extensions:
264 #extension ${ext}: require
265 % endfor
266 #extension GL_ARB_shader_bit_encoding: enable
267 #extension GL_ARB_gpu_shader5: enable
269 % if explicit_locations:
270 #extension GL_ARB_explicit_attrib_location : require
271 % endif
273 precision highp float;
274 % for s in structures:
276 struct ${s.type} {
277 % for m in s.members:
278 ${"{:<15}".format(m.type)} ${m.name};
279 % endfor
281 % endfor
283 % for r in root_types:
284 % if explicit_locations:
285 layout(location = 0)
286 % endif
287 flat out ${"{:<10}".format(r.type)} ${r.name};
288 % endfor
290 in vec4 piglit_vertex;
292 #if defined(GL_ARB_shader_bit_encoding) || defined(GL_ARB_gpu_shader5) || __VERSION__ >= 430
293 float float_get(float f, uint bits) { return uintBitsToFloat(bits); }
294 #else
295 float float_get(float f, uint bits) { return f; }
296 #endif
297 % if glsl_version >= 400 or "GL_ARB_gpu_shader_fp64" in extensions:
298 double double_get(uvec2 bits) { return packDouble2x32(bits); }
299 %endif
301 void main()
303 % for inst in instances:
304 % for s in inst.setters:
305 ${s}
306 % endfor
307 % endfor
309 gl_Position = piglit_vertex;
312 [fragment shader]
313 % for ext in extensions:
314 #extension ${ext}: require
315 % endfor
316 #extension GL_ARB_shader_bit_encoding: enable
317 #extension GL_ARB_gpu_shader5: enable
319 % if explicit_locations:
320 #extension GL_ARB_explicit_attrib_location : require
321 % endif
323 precision highp float;
325 % for s in structures:
326 struct ${s.type} {
327 % for m in s.members:
328 ${"{:<15}".format(m.type)} ${m.name};
329 % endfor
331 % endfor
333 % for r in root_types:
334 % if explicit_locations:
335 layout(location = 0)
336 % endif
337 flat in ${"{:<11}".format(r.type)} ${r.name};
338 % endfor
340 out vec4 piglit_fragcolor;
342 #if defined(GL_ARB_shader_bit_encoding) || defined(GL_ARB_gpu_shader5) || __VERSION__ >= 430
343 bool float_match(float u, float f, uint bits) { return floatBitsToUint(u) == bits; }
344 #else
345 bool float_match(float u, float f, uint bits) { return u == f; }
346 #endif
347 % if glsl_version >= 400 or "GL_ARB_gpu_shader_fp64" in extensions:
348 bool double_match(double u, uvec2 bits) { return unpackDouble2x32(u) == bits; }
349 %endif
351 void main()
353 bool pass = true;
355 % for inst in instances:
356 % for s in inst.checkers:
357 if (${s})
358 pass = false;
359 % endfor
360 % endfor
362 piglit_fragcolor = pass ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);
365 [test]
366 link success
368 draw rect -1 -1 2 2
369 probe all rgba 0.0 1.0 0.0 1.0"""))
371 shader = t.render(glsl_version=glsl_version,
372 root_types=root_types,
373 explicit_locations=explicit_locations,
374 structures=structs,
375 extensions=extensions,
376 instances=instances)
378 shader_file.write(shader)
379 shader_file.close()
382 def do_test_permutations(root_types, glsl_version, extensions, tests_path):
383 without_outer_struct = []
384 for type in root_types:
385 if random_ubo.isstructure(type.type):
386 without_outer_struct.extend(m for m in type.members)
387 else:
388 without_outer_struct.append(type)
390 do_test(root_types, False, glsl_version, extensions, tests_path)
391 do_test(root_types, True, glsl_version,
392 extensions + ["GL_ARB_separate_shader_objects"], tests_path)
393 do_test(without_outer_struct, False, glsl_version, extensions, tests_path)
396 fp64_ext_path = os.path.join("spec", "arb_gpu_shader_fp64", "execution",
397 "inout")
398 fp64_core_path = os.path.join("spec", "glsl-4.00", "execution",
399 "inout")
400 int64_path = os.path.join("spec", "arb_gpu_shader_int64", "execution",
401 "inout")
403 for path in [fp64_ext_path, fp64_core_path, int64_path]:
404 try:
405 os.makedirs(path)
406 except OSError as exc:
407 if exc.errno != errno.EEXIST or not os.path.isdir(path):
408 raise
410 INT64_VEC_TYPES = [None, "int64_t", "i64vec2", "i64vec3", "i64vec4"]
411 UINT64_VEC_TYPES = [None, "uint64_t", "u64vec2", "u64vec3", "u64vec4"]
412 FLOAT_VEC_TYPES = [None, "float", "vec2", "vec3", "vec4"]
413 DOUBLE_VEC_TYPES = [None, "double", "dvec2", "dvec3", "dvec4"]
415 DOUBLE_MAT_TYPES = ["dmat2x2", "dmat2x3", "dmat2x4", "dmat3x2", "dmat3x3",
416 "dmat3x4", "dmat4x2", "dmat4x3", "dmat4x4"]
418 VT = VaryingType
421 def do_common_64bit_tests(glsl_version, extensions, name_arr, tests_path):
422 tests = []
423 for i in range(1, 4):
424 tests.append([VT("S1", [VT(name_arr[i])])])
426 tests.append([VT("S1", [VT(name_arr[i] + "[4]")])])
428 for j in range(1, 4):
429 tests.append([VT("S1", [VT(FLOAT_VEC_TYPES[j]), VT(name_arr[i])])])
431 tests.append([VaryingType("S1",
432 [VT("float"), VT("float"), VT("float"),
433 VT(name_arr[i])])])
435 for j in range(2, 5):
436 tests.append([VT("S1",
437 [VT("float[{}]".format(j)),
438 VT(name_arr[i])])])
440 tests.append([VT("S1", [VT(name_arr[i] + "[3]")])])
442 tests.append([VT("S1",
443 [VT("S2[3]", [VT(name_arr[i]), VT("float")])])])
445 tests.append([VT("S1",
446 [VT("S2", [VT(name_arr[i])])])])
448 for i in range(1, 2):
449 tests.append([VT("S1",
450 [VT("S2[2]",
451 [VT("S3[2]", [
452 VT("float"),
453 VT(name_arr[i])])])])])
455 tests.append([VT("S1",
456 [VT("S2[2]",
457 [VT("S3[2]", [
458 VT("vec3"),
459 VT(name_arr[i])])])])])
461 tests.append([VT("S1[2]",
462 [VT("S2[2]",
463 [VT("S3[2]", [
464 VT(name_arr[i])])])])])
466 for t in tests:
467 do_test_permutations(t, glsl_version, extensions, tests_path)
469 tests = []
471 for i in range(1, 2):
472 tests.append([VT("S1",
473 [VT(name_arr[i] + "[3][2]")])])
474 for i in range(1, 2):
475 for j in range(1, 4):
476 tests.append([VT("S1",
477 [VT(FLOAT_VEC_TYPES[j]),
478 VT(name_arr[i] + "[3][2]")])])
480 for i in range(1, 2):
481 for j in range(1, 4):
482 tests.append([VT("S1", [VT("S2[2][2]",
483 [VT(FLOAT_VEC_TYPES[j]),
484 VT(name_arr[i])])])])
486 for i in range(3, 4):
487 tests.append([VT("S1", [VT(name_arr[i] + "[2][2]")])])
489 for t in tests:
490 do_test_permutations(t, glsl_version,
491 extensions + ["GL_ARB_arrays_of_arrays"],
492 tests_path)
495 def do_fp64_specific_tests():
496 tests = []
498 for t in DOUBLE_MAT_TYPES:
499 tests.append([VT("S1", [VT(t)])])
501 for t in ["dmat2x2", "dmat2x3", "dmat2x4", "dmat3x2", "dmat3x3"]:
502 for j in range(1, 4):
503 tests.append([VT("S1", [VT(FLOAT_VEC_TYPES[j]), VT(t)])])
505 for j in range(1, 7):
506 tests.append([VT("S1",
507 [VT("float[{}]".format(j)), VT(t)])])
509 for j in range(1, 4):
510 tests.append([VT("S1", [VT("S2[2]",
511 [VT(FLOAT_VEC_TYPES[j]),
512 VT("dmat2x2")])])])
514 tests.append([VT("S1",
515 [VT("double"), VT("float"), VT("double[2]"),
516 VT("float[3]"), VT("dmat2x2")])])
518 tests.append([VT("S1",
519 [VT("S2", [VT("double")]),
520 VT("S3", [VT("float")]),
521 VT("S4", [VT("dmat3x3")])])])
523 for t in tests:
524 do_test_permutations(t, 150, ["GL_ARB_gpu_shader_fp64"], fp64_ext_path)
525 do_test_permutations(t, 400, [], fp64_core_path)
528 do_common_64bit_tests(400, [], DOUBLE_VEC_TYPES, fp64_core_path)
529 do_common_64bit_tests(150, ["GL_ARB_gpu_shader_fp64"], DOUBLE_VEC_TYPES,
530 fp64_ext_path)
531 do_common_64bit_tests(150, ["GL_ARB_gpu_shader_int64"], INT64_VEC_TYPES,
532 int64_path)
533 do_common_64bit_tests(150, ["GL_ARB_gpu_shader_int64"], UINT64_VEC_TYPES,
534 int64_path)
536 do_fp64_specific_tests()