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
31 from random_ubo
import struct_types
33 def remove_empty_structure(s
, do_remove
= True):
38 for x
in struct_types
:
39 # Delete the empty structure at the end, and the structure
40 # cannot contain itself.
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:
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
)
57 del struct_types
[x
][i
]
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))
73 def diminish_array_type(fields
, i
):
74 field_type
, field_name
= fields
[i
]
76 if not random_ubo
.isarray(field_type
):
79 if random_ubo
.array_elements(field_type
) == 1:
80 smaller_type
= random_ubo
.array_base_type(field_type
)
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
)
89 def remove_random_field(blocks
):
92 potential_kill_list
= []
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:
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
):
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:
127 field_layouts
) in blocks
:
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
:
144 if not diminish_array_type(b
[4], i
):
145 print("remove {}".format(b
[4][i
]))
152 # Remove any potentially empty UBOs
154 while i
< len(blocks
):
155 if len(blocks
[i
][4]) == 0:
162 if len(sys
.argv
) <= 2:
163 print("Usage: {} input output".format(sys
.argv
[0]))
166 file_in
= open(sys
.argv
[1], "r", 0)
167 file_out
= open(sys
.argv
[2], "w", 0)
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()
190 print("Invalid packing string '{}'.".format(packing_str
))
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"):
204 if not remove_random_field(blocks
):
210 file_out
.write(random_ubo
.emit_shader_test(