def diminish_array_type(fields, i): field_type, field_name = fields[i] if not random_ubo.isarray(field_type): return False if random_ubo.array_elements(field_type) == 1: smaller_type = random_ubo.array_base_type(field_type) else: smaller_type = random_ubo.array_base_type(field_type) + "[1]" print("{} => {}".format(field_type, smaller_type)) fields[i] = (smaller_type, field_name) return True
def stringify_array_dimensions(type): s = "" base_type = type while random_ubo.isarray(base_type): s += "@{}".format(random_ubo.array_elements(base_type)) base_type = random_ubo.array_base_type(base_type) return s
def remove_empty_structure(s, do_remove = True): global struct_types removed = [s] for x in struct_types: # Delete the empty structure at the end, and the structure # cannot contain itself. if s == x: continue # A previous caller may be in the proces of deleting this structure # type, so just skip it for now. if len(struct_types[x]) == 0: continue i = 0 while i < len(struct_types[x]): field_type, field_name = struct_types[x][i] if random_ubo.isarray(field_type): field_type = random_ubo.array_base_type(field_type) if field_type == s: del struct_types[x][i] else: i = i + 1 # Now x could be empty, so possibly remove it too. if len(struct_types[x]) == 0: removed.extend(remove_empty_structure(x, False)) if do_remove: for x in removed: del struct_types[x] return removed
def remove_empty_structure(s, do_remove = True): global struct_types removed = [s] for x in struct_types: # Delete the empty structure at the end, and the structure # cannot contain itself. if s == x: continue # A previous caller may be in the process of deleting this structure # type, so just skip it for now. if len(struct_types[x]) == 0: continue i = 0 while i < len(struct_types[x]): field_type, field_name = struct_types[x][i] if random_ubo.isarray(field_type): field_type = random_ubo.array_base_type(field_type) if field_type == s: del struct_types[x][i] else: i = i + 1 # Now x could be empty, so possibly remove it too. if len(struct_types[x]) == 0: removed.extend(remove_empty_structure(x, False)) if do_remove: for x in removed: del struct_types[x] return removed
def remove_random_field(blocks): global struct_types potential_kill_list = [] for b in blocks: potential_kill_list.extend([(b[0], i) for i in xrange(len(b[4]))]) for s in struct_types: potential_kill_list.extend([(s, i) for i in xrange(len(struct_types[s]))]) if len(potential_kill_list) == 0: return False owner, i = random.choice(potential_kill_list) print("{} field index {}:".format(owner, i), end="") if owner in struct_types: if diminish_array_type(struct_types[owner], i): return True print("remove {}".format(struct_types[owner][i])) del struct_types[owner][i] if len(struct_types[owner]) == 0: removed = remove_empty_structure(owner) # Update the UBOs to note that some structures are gone. if len(removed) != 0: for (block_name, instance_name, global_layout, block_layout, fields, field_layouts) in blocks: j = 0 while j < len(fields): field_type, field_name = fields[j] if random_ubo.isarray(field_type): field_type = random_ubo.array_base_type(field_type) if field_type in removed: del fields[j] del field_layouts[j] else: j = j + 1 else: for b in blocks: if b[0] == owner: if not diminish_array_type(b[4], i): print("remove {}".format(b[4][i])) # Delete the field del b[4][i] # Delete the layout del b[5][i] # Remove any potentially empty UBOs i = 0 while i < len(blocks): if len(blocks[i][4]) == 0: del blocks[i] else: i = i + 1 return True
def array_base_type(varying_type): t = copy.copy(varying_type) t.type = random_ubo.array_base_type(t.type) t.name = None return t