-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_vkfunctions.py
executable file
·188 lines (157 loc) · 5.73 KB
/
gen_vkfunctions.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
# Generate core/vk_functions.cc and include/VKFW/vk_functions.h
# Copyright (C) 2024 dbstream
#
# Based on generate.py from https://github.com/zeux/volk at commit f51cfb6.
#
# This file is licensed under the MIT license.
import re
import sys
import xml.etree.ElementTree as etree
from collections import OrderedDict
# Generate vk_functions.cc and vk_functions.h from vk.xml.
cmdversions = {
'vkCmdSetDiscardRectangleEnableEXT': 2,
'vkCmdSetDiscardRectangleModeEXT': 2,
'vkCmdSetExclusiveScissorEnableNV': 2
}
def is_descendant (types, name, base):
if name == base:
return True
typ = types.get (name)
if not typ:
return False
parents = typ.get ('parent')
if not parents:
return False
return any ([is_descendant (types, parent, base) for parent in parents.split (',')])
def cdepends (key):
key = re.sub (r'[a-zA-Z0-9_]+', lambda m: 'defined(' + m.group(0) + ')', key)
key = key.replace (',', ' || ')
key = key.replace ('+', ' && ')
return key
def patch_file (path, blocks):
result = []
block = None
with open (path, 'r') as filp:
for line in filp.readlines ():
if block:
if line.strip () == block.strip ():
result.append (line)
block = None
else:
result.append (line)
if line.strip ().startswith ('/* VKFW_GEN_'):
block = line
result.append (blocks[line.strip ()[12:-3]])
with open (path, 'w', newline='\n') as filp:
for line in result:
filp.write (line)
def main (argv):
spec_path = '/usr/share/vulkan/registry/vk.xml'
if len (argv) > 1:
spec_path = argv[1]
with open (spec_path, 'r') as fd:
spec = etree.parse (fd)
command_groups = OrderedDict ()
for feature in spec.findall ('feature'):
if 'vulkan' not in feature.get ('api').split (','):
continue
key = 'defined(' + feature.get ('name') + ')'
cmdrefs = feature.findall ('require/command')
command_groups[key] = [cmdref.get ('name') for cmdref in cmdrefs]
instance_commands = set ()
for ext in sorted (spec.findall ('extensions/extension'), key=lambda ext: ext.get ('name')):
if 'vulkan' not in ext.get ('supported').split (','):
continue
name = ext.get ('name')
typ = ext.get ('type')
for req in ext.findall ('require'):
key = 'defined(' + name + ')'
if req.get ('feature'):
for i in req.get ('feature').split (','):
key += ' && defined(' + i + ')'
if req.get ('extension'):
for i in req.get ('extension').split (','):
key += ' && defined(' + i + ')'
if req.get ('depends'):
dep = cdepends (req.get ('depends'))
if '||' in dep:
key += ' && (' + dep + ')'
else:
key += ' && ' + dep
cmdrefs = req.findall ('command')
for cmdref in cmdrefs:
ver = cmdversions.get (cmdref.get ('name'))
real_key = key
if ver:
real_key += ' && ' + name.upper() + '_SPEC_VERSION >= ' + str (ver)
command_groups.setdefault (real_key, []).append (cmdref.get ('name'))
if typ == 'instance':
for cmdref in cmdrefs:
instance_commands.add (cmdref.get ('name'))
command_to_groups = OrderedDict ()
for (group, cmdnames) in command_groups.items ():
for name in cmdnames:
command_to_groups.setdefault (name, []).append (group)
for (group, cmdnames) in command_groups.items ():
command_groups[group] = [name for name in cmdnames if len (command_to_groups[name]) == 1]
for (name, groups) in command_to_groups.items ():
if len (groups) == 1:
continue
key = ' || '.join (['(' + group + ')' for group in groups])
command_groups.setdefault (key, []).append (name)
commands = {}
for cmd in spec.findall ('commands/command'):
if not cmd.get ('alias'):
name = cmd.findtext ('proto/name')
commands[name] = cmd
for cmd in spec.findall ('commands/command'):
if cmd.get ('alias'):
name = cmd.get ('name')
commands[name] = commands[cmd.get ('alias')]
types = {}
for typ in spec.findall ('types/type'):
name = typ.findtext ('name')
if name:
types[name] = typ
blocks = {
'PROTOTYPES_H': '',
'PROTOTYPES_C': '',
'PFNS': '',
'LOAD_LOADER': '',
'LOAD_INSTANCE': '',
'LOAD_DEVICE': ''
}
for (group, cmdnames) in command_groups.items ():
ifdef = '#if ' + group + '\n'
for key in blocks.keys ():
blocks[key] += ifdef
for name in sorted (cmdnames):
cmd = commands[name]
proto = cmd.findtext ('proto/type')
params = cmd.findall ('param')
params = [p for p in params if (not p.get ('api')) or 'vulkan' in p.get ('api').split (',')]
param_names = [param.findtext ('name') for param in params]
typ = params[0].findtext ('type')
if name == 'vkGetDeviceProcAddr':
typ = 'VkInstance'
decl = 'VKFWAPI VKAPI_ATTR ' + proto + ' VKAPI_CALL\nvkfw_' + name + ' (' + ', '.join ([' '.join (param.itertext ()) for param in params]) + ')'
blocks['PROTOTYPES_H'] += decl + ';\n#define ' + name + ' vkfw_' + name + '\n'
blocks['PROTOTYPES_C'] += 'extern "C"\n' + decl + '\n{\n\t' + ('return ' if proto != 'void' else '') + 'pfn_' + name + ' (' + ', '.join(param_names) + ');\n}\n'
blocks['PFNS'] += 'PFN_' + name + ' pfn_' + name + ';\n'
if is_descendant (types, typ, 'VkDevice') and name not in instance_commands:
blocks['LOAD_DEVICE'] += '\tpfn_' + name + ' = (PFN_' + name + ') load (context, "' + name + '");\n'
elif is_descendant (types, typ, 'VkInstance'):
blocks['LOAD_INSTANCE'] += '\tpfn_' + name + ' = (PFN_' + name + ') load (context, "' + name + '");\n'
elif name != 'vkGetInstanceProcAddr':
blocks['LOAD_LOADER'] += '\tpfn_' + name + ' = (PFN_' + name + ') load (context, "' + name + '");\n'
for key in blocks.keys ():
if blocks[key].endswith (ifdef):
blocks[key] = blocks[key][:-len(ifdef)]
else:
blocks[key] += '#endif /* ' + group + ' */\n'
patch_file ('core/vk_functions.cc', blocks)
patch_file ('include/VKFW/vk_functions.h', blocks)
if __name__ == '__main__':
main (sys.argv)