-
Notifications
You must be signed in to change notification settings - Fork 42
/
gen_vk_structs.py
executable file
·111 lines (84 loc) · 3.33 KB
/
gen_vk_structs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python3
import sys
import argparse
# import ipdb
try:
import gnureadline as readline
except ImportError:
import readline
# allow copy-paste
readline.parse_and_bind('bind_key("Control-v", "paste")')
parser = argparse.ArgumentParser(description='Generate Vulkan struct templates based on user input.')
parser.add_argument("--vk_path", default="/usr/share/vulkan/registry", help='absolute path to vulkan registry')
parser.add_argument("--registry", default="vk.xml", help='registry file to load (try video.xml for video)')
args = parser.parse_args()
vk_registry_path = args.vk_path
# we want to have access to all python modules from `vk_registry_path`
sys.path.append(vk_registry_path)
from reg import Registry
reg = Registry()
reg.loadFile(vk_registry_path + "/" + args.registry)
def generate_struct(struct_name):
type_info = reg.typedict.get(struct_name)
if type_info is None:
print("Key `{}` not found.".format(struct_name))
return ""
members = type_info.elem.findall('.//member')
body = "{} = {{\n".format(struct_name)
longest_name_len = 0
for member in members:
for elem in member:
if elem.tag == 'name':
name_len = len(elem.text)
if longest_name_len < name_len:
longest_name_len = name_len
for member in members:
member_name = ""
member_value = member.get('values') or '0'
member_optional = member.get('optional') == 'true'
member_type = ''
for elem in member:
if elem.tag == 'name':
member_name = elem.text
if elem.tag == 'type':
if elem.text == 'void':
member_value = 'nullptr'
member_type = 'void *'
else:
member_type = ((elem.text or '').strip() + ' ' ) + ((member.text or '').strip() + ' ') + ((elem.tail or '').strip())
member_type = member_type.strip()
if '*' in elem.tail:
member_value = 'nullptr'
if member_optional:
txt = "\t.{: <" + str(longest_name_len + 2) + "} = {}, // {}, optional\n"
else:
txt = "\t.{: <" + str(longest_name_len + 2) + "} = {}, // {} \n"
# print(txt)
body += txt.format(member_name, member_value, member_type)
# ipdb.set_trace()
body += "};\n"
return body
def str2bool(v) -> bool:
"""evaluate anything that is not true(ish) as false"""
return str(v).lower() in ("yes", "true", "t", "1")
print("// Enter a Vulkan struct name to begin, Ctl+D to end interactive session:")
while True:
body = ""
try:
line = input("('q' to quit): ")
except EOFError:
exit(0)
line = line.split("#")[0] # remove any comment lines
if line in ["q", "quit", "exit"]:
break
else:
# only continue if there is anything left of the line to process (comment lines will be empty at this point)
params = line.split(",")
for i, p in enumerate(params):
# remove any extra whitespace around our input
params[i] = p.strip()
if len(params) > 0 and params[0]:
body += "\n// " + "-" * 70 + "\n\n"
body += generate_struct(params[0])
body += "\n// " + "-" * 70 + "\n\n"
print(body)