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
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.
26 from templates
import template_dir
27 from modules
import utils
29 TEMPLATES
= template_dir(os
.path
.splitext(os
.path
.basename(__file__
))[0])
31 # These are a set of pseudo random values used by the number sequence
32 # generator. See get_value above.
33 RANDOM_NUMBERS
= (0.78685, 0.89828, 0.36590, 0.92504, 0.48998, 0.27989,
34 0.08693, 0.48144, 0.87644, 0.18080, 0.95147, 0.18892,
35 0.45851, 0.76423, 0.78659, 0.97998, 0.24352, 0.60922,
36 0.45241, 0.33045, 0.27233, 0.92331, 0.63593, 0.67826,
37 0.12195, 0.24853, 0.35977, 0.41759, 0.79119, 0.54281,
38 0.04089, 0.03877, 0.58445, 0.43017, 0.58635, 0.48151,
39 0.58778, 0.37033, 0.47464, 0.17470, 0.18308, 0.49466,
40 0.45838, 0.30337, 0.71273, 0.45083, 0.88339, 0.47350,
41 0.86539, 0.48355, 0.92923, 0.79107, 0.77266, 0.71677,
42 0.79860, 0.95149, 0.05604, 0.16863, 0.14072, 0.29028,
43 0.57637, 0.13572, 0.36011, 0.65431, 0.38951, 0.73245,
44 0.69497, 0.76041, 0.31016, 0.48708, 0.96677, 0.58732,
45 0.33741, 0.73691, 0.24445, 0.35686, 0.72645, 0.65438,
46 0.00824, 0.00923, 0.87650, 0.43315, 0.67256, 0.66939,
47 0.87706, 0.73880, 0.96248, 0.24148, 0.24126, 0.24673,
48 0.18999, 0.10330, 0.78826, 0.23209, 0.59548, 0.23134,
49 0.72414, 0.88036, 0.54498, 0.32668, 0.02967, 0.12643)
54 "-set-by-other-stage")
57 def get_value(type_
, idx
):
58 """Get a string representing a number in the specified GLSL type"""
60 value
= RANDOM_NUMBERS
[idx
% len(RANDOM_NUMBERS
)]
68 return str(int(value
* 100) - 50)
70 return str(int(value
* 50))
72 return str((value
* 20.0) - 10.0)
75 def generate_tests(type_list
, base_name
, major
, minor
):
76 dirname
= os
.path
.join('spec',
77 'glsl-{0}.{1}'.format(major
, minor
),
79 'uniform-initializer')
80 utils
.safe_makedirs(dirname
)
82 for target
in ("vs", "fs"):
83 for t
in ALL_TEMPLATES
:
84 template
= TEMPLATES
.get_template(
85 "{0}-initializer{1}.shader_test.mako".format(target
, t
))
87 test_file_name
= os
.path
.join(
89 '{0}-{1}{2}.shader_test'.format(target
, base_name
, t
))
92 # Generate the test vectors. This is a list of tuples. Each
93 # tuple is a type name paired with a value. The value is
94 # formatted as a GLSL constructor.
96 # A set of types and values is also generated that can be set via
97 # the OpenGL API. Some of the tests use this information.
100 for i
, (type_
, num_values
) in enumerate(type_list
):
103 for j
in range(num_values
):
104 numbers
.append(get_value(type_
, i
+ j
))
105 alt_numbers
.append(get_value(type_
, i
+ j
+ 7))
107 value
= "{0}({1})".format(type_
, ", ".join(numbers
))
112 elif type_
[0] == 'b':
113 api_type
= "ivec{0}".format(type_
[-1])
115 if type_
[-1] in ["2", "3", "4"]:
116 name
= 'u{0}{1}'.format(type_
[0], type_
[-1])
118 name
= 'u{0}'.format(type_
[0])
120 test_vectors
.append((type_
, name
, value
))
121 api_vectors
.append((api_type
, name
, alt_numbers
))
123 with
open(test_file_name
, "w") as f
:
124 f
.write(template
.render_unicode(type_list
=test_vectors
,
125 api_types
=api_vectors
,
130 def generate_array_tests(type_list
, base_name
, major
, minor
):
131 dirname
= os
.path
.join('spec',
132 'glsl-{0}.{1}'.format(major
, minor
),
134 'uniform-initializer')
135 utils
.safe_makedirs(dirname
)
138 """Generate parts."""
139 # pylint: disable=undefined-loop-variable
142 for k
in range(num_values
):
143 numbers
.append(get_value(type_
, i
+ j
+ k
))
145 yield '{0}({1})'.format(type_
, ', '.join(numbers
))
146 # pylint: enable=undefined-loop-variable
149 for i
, (type_
, num_values
) in enumerate(type_list
):
150 if type_
[-1] in ["2", "3", "4"]:
151 name
= 'u{0}{1}'.format(type_
[0], type_
[-1])
153 name
= 'u{0}'.format(type_
[0])
155 array_type
= '{0}[2]'.format(type_
)
156 value
= "{0}({1})".format(array_type
, ", ".join(parts()))
158 vecs
.append((array_type
, name
, value
))
160 for target
in ("vs", "fs"):
161 template
= TEMPLATES
.get_template(
162 '{0}-initializer.shader_test.mako'.format(target
))
164 test_file_name
= os
.path
.join(
166 '{0}-{1}-array.shader_test'.format(target
, base_name
))
167 print(test_file_name
)
169 with
open(test_file_name
, "w") as f
:
170 f
.write(template
.render_unicode(type_list
=vecs
,
177 bool_types
= [("bool", 1), ("bvec2", 2), ("bvec3", 3), ("bvec4", 4)]
178 int_types
= [("int", 1), ("ivec2", 2), ("ivec3", 3), ("ivec4", 4)]
179 float_types
= [("float", 1), ("vec2", 2), ("vec3", 3), ("vec4", 4)]
180 uint_types
= [("uint", 1), ("uvec2", 2), ("uvec3", 3), ("uvec4", 4)]
181 mat2_types
= [("mat2x2", 4), ("mat2x3", 6), ("mat2x4", 8)]
182 mat3_types
= [("mat3x2", 6), ("mat3x3", 9), ("mat3x4", 12)]
183 mat4_types
= [("mat4x2", 8), ("mat4x3", 12), ("mat4x4", 16)]
185 for types
, base_name
, major
, minor
in [(bool_types
, "bool", 1, 20),
186 (int_types
, "int", 1, 20),
187 (float_types
, "float", 1, 20),
188 (mat2_types
, "mat2", 1, 20),
189 (mat3_types
, "mat3", 1, 20),
190 (mat4_types
, "mat4", 1, 20),
191 (uint_types
, "uint", 1, 30)]:
192 generate_tests(types
, base_name
, major
, minor
)
193 generate_array_tests(types
, base_name
, major
, minor
)
196 if __name__
== '__main__':