ext_semaphore: add check for glGetIntegerv(GL_NUM_DEVICE_UUIDS_EXT)
[piglit.git] / generated_tests / random_ubo_trim.py
blob5ae21bf0610b69c9c91a267e9b95970c175296ee
1 #!/usr/bin/env python2
2 # coding=utf-8
4 # Copyright (c) 2014 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 from __future__ import print_function
25 import os
26 import sys
27 import errno
28 import ast
29 import random
30 import random_ubo
32 from random_ubo import struct_types
34 def remove_empty_structure(s, do_remove = True):
35 global struct_types
37 removed = [s]
39 for x in struct_types:
40 # Delete the empty structure at the end, and the structure
41 # cannot contain itself.
42 if s == x:
43 continue
45 # A previous caller may be in the process of deleting this structure
46 # type, so just skip it for now.
47 if len(struct_types[x]) == 0:
48 continue
50 i = 0
51 while i < len(struct_types[x]):
52 field_type, field_name = struct_types[x][i]
54 if random_ubo.isarray(field_type):
55 field_type = random_ubo.array_base_type(field_type)
57 if field_type == s:
58 del struct_types[x][i]
59 else:
60 i = i + 1
62 # Now x could be empty, so possibly remove it too.
63 if len(struct_types[x]) == 0:
64 removed.extend(remove_empty_structure(x, False))
67 if do_remove:
68 for x in removed:
69 del struct_types[x]
71 return removed
74 def diminish_array_type(fields, i):
75 field_type, field_name = fields[i]
77 if not random_ubo.isarray(field_type):
78 return False
80 if random_ubo.array_elements(field_type) == 1:
81 smaller_type = random_ubo.array_base_type(field_type)
82 else:
83 smaller_type = random_ubo.array_base_type(field_type) + "[1]"
85 print("{} => {}".format(field_type, smaller_type))
86 fields[i] = (smaller_type, field_name)
87 return True
90 def remove_random_field(blocks):
91 global struct_types
93 potential_kill_list = []
95 for b in blocks:
96 potential_kill_list.extend([(b[0], i)
97 for i in xrange(len(b[4]))])
99 for s in struct_types:
100 potential_kill_list.extend([(s, i)
101 for i in xrange(len(struct_types[s]))])
103 if len(potential_kill_list) == 0:
104 return False
106 owner, i = random.choice(potential_kill_list)
108 print("{} field index {}:".format(owner, i), end="")
110 if owner in struct_types:
111 if diminish_array_type(struct_types[owner], i):
112 return True
114 print("remove {}".format(struct_types[owner][i]))
115 del struct_types[owner][i]
117 if len(struct_types[owner]) == 0:
118 removed = remove_empty_structure(owner)
120 # Update the UBOs to note that some structures are gone.
122 if len(removed) != 0:
123 for (block_name,
124 instance_name,
125 global_layout,
126 block_layout,
127 fields,
128 field_layouts) in blocks:
129 j = 0
130 while j < len(fields):
131 field_type, field_name = fields[j]
133 if random_ubo.isarray(field_type):
134 field_type = random_ubo.array_base_type(field_type)
136 if field_type in removed:
137 del fields[j]
138 del field_layouts[j]
139 else:
140 j = j + 1
142 else:
143 for b in blocks:
144 if b[0] == owner:
145 if not diminish_array_type(b[4], i):
146 print("remove {}".format(b[4][i]))
148 # Delete the field
149 del b[4][i]
150 # Delete the layout
151 del b[5][i]
153 # Remove any potentially empty UBOs
154 i = 0
155 while i < len(blocks):
156 if len(blocks[i][4]) == 0:
157 del blocks[i]
158 else:
159 i = i + 1
161 return True
163 if len(sys.argv) <= 2:
164 print("Usage: {} input output".format(sys.argv[0]))
165 sys.exit(1)
167 file_in = open(sys.argv[1], "r", 0)
168 file_out = open(sys.argv[2], "w", 0)
170 glsl_version = None
171 extensions = None
172 packing = None
173 blocks = []
175 for line in file_in:
176 if line[0] != '#':
177 continue
179 if line.startswith("# GLSL"):
180 glsl_version = int(line.split(" ")[2])
181 elif line.startswith("# EXTENSIONS"):
182 extensions = ast.literal_eval(line[12:].strip())
183 elif line.startswith("# PACKING"):
184 packing_str = line.split(" ")[2].strip()
186 if packing_str == "shared":
187 packing = random_ubo.shared_packing_rules()
188 elif packing_str == "std140":
189 packing = random_ubo.std140_packing_rules()
190 else:
191 print("Invalid packing string '{}'.".format(packing_str))
192 sys.exit(1)
193 elif line.startswith("# STRUCT"):
194 struct_name, struct_fields = ast.literal_eval(line[8:].strip())
195 struct_types[struct_name] = struct_fields
196 elif line.startswith("# UBO"):
197 blocks.append(ast.literal_eval(line[5:].strip()))
198 elif line.startswith("# DATA END"):
199 break
200 else:
201 pass
203 file_in.close()
205 if not remove_random_field(blocks):
206 sys.exit(1)
208 if len(blocks) == 0:
209 sys.exit(1)
211 file_out.write(random_ubo.emit_shader_test(
212 blocks,
213 packing,
214 glsl_version,
215 extensions))
216 file_out.write("\n")
217 file_out.close()