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
24 from __future__
import print_function
32 from random_ubo
import struct_types
34 def remove_empty_structure(s
, do_remove
= True):
39 for x
in struct_types
:
40 # Delete the empty structure at the end, and the structure
41 # cannot contain itself.
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:
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
)
58 del struct_types
[x
][i
]
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))
74 def diminish_array_type(fields
, i
):
75 field_type
, field_name
= fields
[i
]
77 if not random_ubo
.isarray(field_type
):
80 if random_ubo
.array_elements(field_type
) == 1:
81 smaller_type
= random_ubo
.array_base_type(field_type
)
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
)
90 def remove_random_field(blocks
):
93 potential_kill_list
= []
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:
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
):
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:
128 field_layouts
) in blocks
:
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
:
145 if not diminish_array_type(b
[4], i
):
146 print("remove {}".format(b
[4][i
]))
153 # Remove any potentially empty UBOs
155 while i
< len(blocks
):
156 if len(blocks
[i
][4]) == 0:
163 if len(sys
.argv
) <= 2:
164 print("Usage: {} input output".format(sys
.argv
[0]))
167 file_in
= open(sys
.argv
[1], "r", 0)
168 file_out
= open(sys
.argv
[2], "w", 0)
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()
191 print("Invalid packing string '{}'.".format(packing_str
))
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"):
205 if not remove_random_field(blocks
):
211 file_out
.write(random_ubo
.emit_shader_test(