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
24 """ Generate in/out tests for 64 bit types. """
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):
41 self
.members
= members
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."""
53 return "{} = {};".format(name
,
54 'false' if int(data
) == 0 else 'true')
56 return "{} = {}u;".format(name
, data
)
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
)
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")
76 hi
= "0x" + bits
[2:10]
77 lo
= "0x" + bits
[10:18]
79 return "{} = double_get(uvec2({}, {}));".format(name
, lo
, hi
)
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.
88 scalar
= random_ubo
.vector_base_type(type)
89 components
= ["x", "y", "z", "w"]
91 return [scalar_assignment(scalar
,
92 "{}.{}".format(name
, "xyzw"[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
101 c
, r
= random_ubo
.matrix_dimensions(type)
104 column_type
= "dvec{}".format(r
)
106 column_type
= "vec{}".format(r
)
111 data_pairs
.extend(vector_assignment(
113 "{}[{}]".format(name
, i
),
114 data
[(i
* r
):(i
* r
) + r
]))
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)
130 def create_getters_and_setters(varying_type
, full_name
, instances
):
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
)
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,
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,
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,
169 matrix_assignment(varying_type
.type, full_name
, data
))
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))
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):
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
)
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))
211 name
= varying_type
.type
213 if varying_type
.members
:
214 for m
in varying_type
.members
:
215 name
+= "-" + stringify_varying_type(m
)
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
,
231 if explicit_locations
and len(root_types
) > 1:
234 basename
= generate_file_name(root_types
, explicit_locations
)
235 fullname
= os
.path
.join(tests_path
, basename
)
239 shader_file
= open(fullname
, "w", buffering
=1)
241 names
= random_ubo
.unique_name_dict()
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("""\
253 GLSL >= ${glsl_version // 100}.${glsl_version % 100}
254 % for ext in extensions:
258 % if explicit_locations:
259 GL_ARB_explicit_attrib_location
263 % for ext in extensions:
264 #extension ${ext}: require
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
273 precision highp float;
274 % for s in structures:
277 % for m in s.members:
278 ${"{:<15}".format(m.type)} ${m.name};
283 % for r in root_types:
284 % if explicit_locations:
287 flat out ${"{:<10}".format(r.type)} ${r.name};
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); }
295 float float_get(float f, uint bits) { return f; }
297 % if glsl_version >= 400 or "GL_ARB_gpu_shader_fp64" in extensions:
298 double double_get(uvec2 bits) { return packDouble2x32(bits); }
303 % for inst in instances:
304 % for s in inst.setters:
309 gl_Position = piglit_vertex;
313 % for ext in extensions:
314 #extension ${ext}: require
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
323 precision highp float;
325 % for s in structures:
327 % for m in s.members:
328 ${"{:<15}".format(m.type)} ${m.name};
333 % for r in root_types:
334 % if explicit_locations:
337 flat in ${"{:<11}".format(r.type)} ${r.name};
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; }
345 bool float_match(float u, float f, uint bits) { return u == f; }
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; }
355 % for inst in instances:
356 % for s in inst.checkers:
362 piglit_fragcolor = pass ? vec4(0, 1, 0, 1) : vec4(1, 0, 0, 1);
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
,
375 extensions
=extensions
,
378 shader_file
.write(shader
)
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
)
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",
398 fp64_core_path
= os
.path
.join("spec", "glsl-4.00", "execution",
400 int64_path
= os
.path
.join("spec", "arb_gpu_shader_int64", "execution",
403 for path
in [fp64_ext_path
, fp64_core_path
, int64_path
]:
406 except OSError as exc
:
407 if exc
.errno
!= errno
.EEXIST
or not os
.path
.isdir(path
):
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"]
421 def do_common_64bit_tests(glsl_version
, extensions
, name_arr
, tests_path
):
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"),
435 for j
in range(2, 5):
436 tests
.append([VT("S1",
437 [VT("float[{}]".format(j
)),
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",
453 VT(name_arr
[i
])])])])])
455 tests
.append([VT("S1",
459 VT(name_arr
[i
])])])])])
461 tests
.append([VT("S1[2]",
464 VT(name_arr
[i
])])])])])
467 do_test_permutations(t
, glsl_version
, extensions
, tests_path
)
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]")])])
490 do_test_permutations(t
, glsl_version
,
491 extensions
+ ["GL_ARB_arrays_of_arrays"],
495 def do_fp64_specific_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
]),
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")])])])
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
,
531 do_common_64bit_tests(150, ["GL_ARB_gpu_shader_int64"], INT64_VEC_TYPES
,
533 do_common_64bit_tests(150, ["GL_ARB_gpu_shader_int64"], UINT64_VEC_TYPES
,
536 do_fp64_specific_tests()