glsl-1.10: test mesa bug conflict between globals
[piglit.git] / generated_tests / gen_uniform_initializer_tests.py
blobdb52129d9bbeec64ab45ec0da81fda1136a3d98f
1 # coding=utf-8
3 # Copyright © 2012, 2014 Intel Corporation
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
14 # 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
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 # DEALINGS IN THE SOFTWARE.
24 import os
25 import sys
27 from mako import exceptions
29 from templates import template_dir
30 from modules import utils
32 TEMPLATES = template_dir(os.path.splitext(os.path.basename(__file__))[0])
34 # These are a set of pseudo random values used by the number sequence
35 # generator. See get_value above.
36 RANDOM_NUMBERS = (0.78685, 0.89828, 0.36590, 0.92504, 0.48998, 0.27989,
37 0.08693, 0.48144, 0.87644, 0.18080, 0.95147, 0.18892,
38 0.45851, 0.76423, 0.78659, 0.97998, 0.24352, 0.60922,
39 0.45241, 0.33045, 0.27233, 0.92331, 0.63593, 0.67826,
40 0.12195, 0.24853, 0.35977, 0.41759, 0.79119, 0.54281,
41 0.04089, 0.03877, 0.58445, 0.43017, 0.58635, 0.48151,
42 0.58778, 0.37033, 0.47464, 0.17470, 0.18308, 0.49466,
43 0.45838, 0.30337, 0.71273, 0.45083, 0.88339, 0.47350,
44 0.86539, 0.48355, 0.92923, 0.79107, 0.77266, 0.71677,
45 0.79860, 0.95149, 0.05604, 0.16863, 0.14072, 0.29028,
46 0.57637, 0.13572, 0.36011, 0.65431, 0.38951, 0.73245,
47 0.69497, 0.76041, 0.31016, 0.48708, 0.96677, 0.58732,
48 0.33741, 0.73691, 0.24445, 0.35686, 0.72645, 0.65438,
49 0.00824, 0.00923, 0.87650, 0.43315, 0.67256, 0.66939,
50 0.87706, 0.73880, 0.96248, 0.24148, 0.24126, 0.24673,
51 0.18999, 0.10330, 0.78826, 0.23209, 0.59548, 0.23134,
52 0.72414, 0.88036, 0.54498, 0.32668, 0.02967, 0.12643)
54 ALL_TEMPLATES = ("",
55 "-from-const",
56 "-set-by-API",
57 "-set-by-other-stage")
60 def get_value(type_, idx):
61 """Get a string representing a number in the specified GLSL type"""
63 value = RANDOM_NUMBERS[idx % len(RANDOM_NUMBERS)]
65 if type_[0] == 'b':
66 if (value * 10) > 5:
67 return "1"
68 else:
69 return "0"
70 elif type_[0] == 'i':
71 return str(int(value * 100) - 50)
72 elif type_[0] == 'u':
73 return str(int(value * 50))
74 else:
75 return str((value * 20.0) - 10.0)
78 def generate_tests(type_list, base_name, major, minor):
79 dirname = os.path.join('spec',
80 'glsl-{0}.{1}'.format(major, minor),
81 'execution',
82 'uniform-initializer')
83 utils.safe_makedirs(dirname)
85 for target in ("vs", "fs"):
86 for t in ALL_TEMPLATES:
87 template = TEMPLATES.get_template(
88 "{0}-initializer{1}.shader_test.mako".format(target, t))
90 test_file_name = os.path.join(
91 dirname,
92 '{0}-{1}{2}.shader_test'.format(target, base_name, t))
93 print(test_file_name)
95 # Generate the test vectors. This is a list of tuples. Each
96 # tuple is a type name paired with a value. The value is
97 # formatted as a GLSL constructor.
99 # A set of types and values is also generated that can be set via
100 # the OpenGL API. Some of the tests use this information.
101 test_vectors = []
102 api_vectors = []
103 for i, (type_, num_values) in enumerate(type_list):
104 numbers = []
105 alt_numbers = []
106 for j in range(num_values):
107 numbers.append(get_value(type_, i + j))
108 alt_numbers.append(get_value(type_, i + j + 7))
110 value = "{0}({1})".format(type_, ", ".join(numbers))
112 api_type = type_
113 if type_ == "bool":
114 api_type = "int"
115 elif type_[0] == 'b':
116 api_type = "ivec{0}".format(type_[-1])
118 if type_[-1] in ["2", "3", "4"]:
119 name = 'u{0}{1}'.format(type_[0], type_[-1])
120 else:
121 name = 'u{0}'.format(type_[0])
123 test_vectors.append((type_, name, value))
124 api_vectors.append((api_type, name, alt_numbers))
126 with open(test_file_name, "w") as f:
127 try:
128 f.write(template.render_unicode(type_list=test_vectors,
129 api_types=api_vectors,
130 major=major,
131 minor=minor))
132 except:
133 print(exceptions.text_error_template().render(), file=sys.stderr)
134 raise
137 def generate_array_tests(type_list, base_name, major, minor):
138 dirname = os.path.join('spec',
139 'glsl-{0}.{1}'.format(major, minor),
140 'execution',
141 'uniform-initializer')
142 utils.safe_makedirs(dirname)
144 def parts():
145 """Generate parts."""
146 # pylint: disable=undefined-loop-variable
147 for j in range(2):
148 numbers = []
149 for k in range(num_values):
150 numbers.append(get_value(type_, i + j + k))
152 yield '{0}({1})'.format(type_, ', '.join(numbers))
153 # pylint: enable=undefined-loop-variable
155 vecs = []
156 for i, (type_, num_values) in enumerate(type_list):
157 if type_[-1] in ["2", "3", "4"]:
158 name = 'u{0}{1}'.format(type_[0], type_[-1])
159 else:
160 name = 'u{0}'.format(type_[0])
162 array_type = '{0}[2]'.format(type_)
163 value = "{0}({1})".format(array_type, ", ".join(parts()))
165 vecs.append((array_type, name, value))
167 for target in ("vs", "fs"):
168 template = TEMPLATES.get_template(
169 '{0}-initializer.shader_test.mako'.format(target))
171 test_file_name = os.path.join(
172 dirname,
173 '{0}-{1}-array.shader_test'.format(target, base_name))
174 print(test_file_name)
176 with open(test_file_name, "w") as f:
177 try:
178 f.write(template.render_unicode(type_list=vecs,
179 major=major,
180 minor=minor))
181 except:
182 print(exceptions.text_error_template().render(), file=sys.stderr)
183 raise
186 def main():
187 """Main function."""
188 bool_types = [("bool", 1), ("bvec2", 2), ("bvec3", 3), ("bvec4", 4)]
189 int_types = [("int", 1), ("ivec2", 2), ("ivec3", 3), ("ivec4", 4)]
190 float_types = [("float", 1), ("vec2", 2), ("vec3", 3), ("vec4", 4)]
191 uint_types = [("uint", 1), ("uvec2", 2), ("uvec3", 3), ("uvec4", 4)]
192 mat2_types = [("mat2x2", 4), ("mat2x3", 6), ("mat2x4", 8)]
193 mat3_types = [("mat3x2", 6), ("mat3x3", 9), ("mat3x4", 12)]
194 mat4_types = [("mat4x2", 8), ("mat4x3", 12), ("mat4x4", 16)]
196 for types, base_name, major, minor in [(bool_types, "bool", 1, 20),
197 (int_types, "int", 1, 20),
198 (float_types, "float", 1, 20),
199 (mat2_types, "mat2", 1, 20),
200 (mat3_types, "mat3", 1, 20),
201 (mat4_types, "mat4", 1, 20),
202 (uint_types, "uint", 1, 30)]:
203 generate_tests(types, base_name, major, minor)
204 generate_array_tests(types, base_name, major, minor)
207 if __name__ == '__main__':
208 main()