glsl-1.10: test mesa bug conflict between globals
[piglit.git] / generated_tests / random_ubo_trim.py
blobd920bb0c1fedf7e82a273601ca520463c49c95ee
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 import os
25 import sys
26 import errno
27 import ast
28 import random
29 import random_ubo
31 from random_ubo import struct_types
33 def remove_empty_structure(s, do_remove = True):
34 global struct_types
36 removed = [s]
38 for x in struct_types:
39 # Delete the empty structure at the end, and the structure
40 # cannot contain itself.
41 if s == x:
42 continue
44 # A previous caller may be in the process of deleting this structure
45 # type, so just skip it for now.
46 if len(struct_types[x]) == 0:
47 continue
49 i = 0
50 while i < len(struct_types[x]):
51 field_type, field_name = struct_types[x][i]
53 if random_ubo.isarray(field_type):
54 field_type = random_ubo.array_base_type(field_type)
56 if field_type == s:
57 del struct_types[x][i]
58 else:
59 i = i + 1
61 # Now x could be empty, so possibly remove it too.
62 if len(struct_types[x]) == 0:
63 removed.extend(remove_empty_structure(x, False))
66 if do_remove:
67 for x in removed:
68 del struct_types[x]
70 return removed
73 def diminish_array_type(fields, i):
74 field_type, field_name = fields[i]
76 if not random_ubo.isarray(field_type):
77 return False
79 if random_ubo.array_elements(field_type) == 1:
80 smaller_type = random_ubo.array_base_type(field_type)
81 else:
82 smaller_type = random_ubo.array_base_type(field_type) + "[1]"
84 print("{} => {}".format(field_type, smaller_type))
85 fields[i] = (smaller_type, field_name)
86 return True
89 def remove_random_field(blocks):
90 global struct_types
92 potential_kill_list = []
94 for b in blocks:
95 potential_kill_list.extend([(b[0], i)
96 for i in xrange(len(b[4]))])
98 for s in struct_types:
99 potential_kill_list.extend([(s, i)
100 for i in xrange(len(struct_types[s]))])
102 if len(potential_kill_list) == 0:
103 return False
105 owner, i = random.choice(potential_kill_list)
107 print("{} field index {}:".format(owner, i), end="")
109 if owner in struct_types:
110 if diminish_array_type(struct_types[owner], i):
111 return True
113 print("remove {}".format(struct_types[owner][i]))
114 del struct_types[owner][i]
116 if len(struct_types[owner]) == 0:
117 removed = remove_empty_structure(owner)
119 # Update the UBOs to note that some structures are gone.
121 if len(removed) != 0:
122 for (block_name,
123 instance_name,
124 global_layout,
125 block_layout,
126 fields,
127 field_layouts) in blocks:
128 j = 0
129 while j < len(fields):
130 field_type, field_name = fields[j]
132 if random_ubo.isarray(field_type):
133 field_type = random_ubo.array_base_type(field_type)
135 if field_type in removed:
136 del fields[j]
137 del field_layouts[j]
138 else:
139 j = j + 1
141 else:
142 for b in blocks:
143 if b[0] == owner:
144 if not diminish_array_type(b[4], i):
145 print("remove {}".format(b[4][i]))
147 # Delete the field
148 del b[4][i]
149 # Delete the layout
150 del b[5][i]
152 # Remove any potentially empty UBOs
153 i = 0
154 while i < len(blocks):
155 if len(blocks[i][4]) == 0:
156 del blocks[i]
157 else:
158 i = i + 1
160 return True
162 if len(sys.argv) <= 2:
163 print("Usage: {} input output".format(sys.argv[0]))
164 sys.exit(1)
166 file_in = open(sys.argv[1], "r", 0)
167 file_out = open(sys.argv[2], "w", 0)
169 glsl_version = None
170 extensions = None
171 packing = None
172 blocks = []
174 for line in file_in:
175 if line[0] != '#':
176 continue
178 if line.startswith("# GLSL"):
179 glsl_version = int(line.split(" ")[2])
180 elif line.startswith("# EXTENSIONS"):
181 extensions = ast.literal_eval(line[12:].strip())
182 elif line.startswith("# PACKING"):
183 packing_str = line.split(" ")[2].strip()
185 if packing_str == "shared":
186 packing = random_ubo.shared_packing_rules()
187 elif packing_str == "std140":
188 packing = random_ubo.std140_packing_rules()
189 else:
190 print("Invalid packing string '{}'.".format(packing_str))
191 sys.exit(1)
192 elif line.startswith("# STRUCT"):
193 struct_name, struct_fields = ast.literal_eval(line[8:].strip())
194 struct_types[struct_name] = struct_fields
195 elif line.startswith("# UBO"):
196 blocks.append(ast.literal_eval(line[5:].strip()))
197 elif line.startswith("# DATA END"):
198 break
199 else:
200 pass
202 file_in.close()
204 if not remove_random_field(blocks):
205 sys.exit(1)
207 if len(blocks) == 0:
208 sys.exit(1)
210 file_out.write(random_ubo.emit_shader_test(
211 blocks,
212 packing,
213 glsl_version,
214 extensions))
215 file_out.write("\n")
216 file_out.close()