Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/vulkan/util/vk_entrypoints_gen.py
7196 views
1
# coding=utf-8
2
COPYRIGHT=u"""
3
/* Copyright © 2015-2021 Intel Corporation
4
*
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the "Software"),
7
* to deal in the Software without restriction, including without limitation
8
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
* and/or sell copies of the Software, and to permit persons to whom the
10
* Software is furnished to do so, subject to the following conditions:
11
*
12
* The above copyright notice and this permission notice (including the next
13
* paragraph) shall be included in all copies or substantial portions of the
14
* Software.
15
*
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
19
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
* IN THE SOFTWARE.
23
*/
24
"""
25
26
import argparse
27
import os
28
29
from mako.template import Template
30
31
# Mesa-local imports must be declared in meson variable
32
# '{file_without_suffix}_depend_files'.
33
from vk_dispatch_table_gen import get_entrypoints_from_xml
34
35
TEMPLATE_H = Template(COPYRIGHT + """\
36
/* This file generated from ${filename}, don't edit directly. */
37
38
#include "vk_dispatch_table.h"
39
40
#ifndef ${guard}
41
#define ${guard}
42
43
#ifdef __cplusplus
44
extern "C" {
45
#endif
46
47
% for p in instance_prefixes:
48
extern const struct vk_instance_entrypoint_table ${p}_instance_entrypoints;
49
% endfor
50
51
% for p in physical_device_prefixes:
52
extern const struct vk_physical_device_entrypoint_table ${p}_physical_device_entrypoints;
53
% endfor
54
55
% for p in device_prefixes:
56
extern const struct vk_device_entrypoint_table ${p}_device_entrypoints;
57
% endfor
58
59
% if gen_proto:
60
% for e in instance_entrypoints:
61
% if e.guard is not None:
62
#ifdef ${e.guard}
63
% endif
64
% for p in physical_device_prefixes:
65
VKAPI_ATTR ${e.return_type} VKAPI_CALL ${p}_${e.name}(${e.decl_params()});
66
% endfor
67
% if e.guard is not None:
68
#endif // ${e.guard}
69
% endif
70
% endfor
71
72
% for e in physical_device_entrypoints:
73
% if e.guard is not None:
74
#ifdef ${e.guard}
75
% endif
76
% for p in physical_device_prefixes:
77
VKAPI_ATTR ${e.return_type} VKAPI_CALL ${p}_${e.name}(${e.decl_params()});
78
% endfor
79
% if e.guard is not None:
80
#endif // ${e.guard}
81
% endif
82
% endfor
83
84
% for e in device_entrypoints:
85
% if e.guard is not None:
86
#ifdef ${e.guard}
87
% endif
88
% for p in device_prefixes:
89
VKAPI_ATTR ${e.return_type} VKAPI_CALL ${p}_${e.name}(${e.decl_params()});
90
% endfor
91
% if e.guard is not None:
92
#endif // ${e.guard}
93
% endif
94
% endfor
95
% endif
96
97
#ifdef __cplusplus
98
}
99
#endif
100
101
#endif /* ${guard} */
102
""", output_encoding='utf-8')
103
104
TEMPLATE_C = Template(COPYRIGHT + """
105
/* This file generated from ${filename}, don't edit directly. */
106
107
#include "${header}"
108
109
/* Weak aliases for all potential implementations. These will resolve to
110
* NULL if they're not defined, which lets the resolve_entrypoint() function
111
* either pick the correct entry point.
112
*
113
* MSVC uses different decorated names for 32-bit versus 64-bit. Declare
114
* all argument sizes for 32-bit because computing the actual size would be
115
* difficult.
116
*/
117
118
<%def name="entrypoint_table(type, entrypoints, prefixes)">
119
% if gen_weak:
120
% for e in entrypoints:
121
% if e.guard is not None:
122
#ifdef ${e.guard}
123
% endif
124
% for p in prefixes:
125
#ifdef _MSC_VER
126
${e.return_type} (*${p}_${e.name}_Null)(${e.decl_params()}) = 0;
127
#ifdef _M_IX86
128
% for args_size in [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 60, 104]:
129
#pragma comment(linker, "/alternatename:_${p}_${e.name}@${args_size}=_${p}_${e.name}_Null")
130
% endfor
131
#else
132
#pragma comment(linker, "/alternatename:${p}_${e.name}=${p}_${e.name}_Null")
133
#endif
134
#else
135
VKAPI_ATTR ${e.return_type} VKAPI_CALL ${p}_${e.name}(${e.decl_params()}) __attribute__ ((weak));
136
#endif
137
% endfor
138
% if e.guard is not None:
139
#endif // ${e.guard}
140
% endif
141
% endfor
142
% endif
143
144
% for p in prefixes:
145
const struct vk_${type}_entrypoint_table ${p}_${type}_entrypoints = {
146
% for e in entrypoints:
147
% if e.guard is not None:
148
#ifdef ${e.guard}
149
% endif
150
.${e.name} = ${p}_${e.name},
151
% if e.guard is not None:
152
#endif // ${e.guard}
153
% endif
154
% endfor
155
};
156
% endfor
157
</%def>
158
159
${entrypoint_table('instance', instance_entrypoints, instance_prefixes)}
160
${entrypoint_table('physical_device', physical_device_entrypoints, physical_device_prefixes)}
161
${entrypoint_table('device', device_entrypoints, device_prefixes)}
162
""", output_encoding='utf-8')
163
164
def get_entrypoints_defines(doc):
165
"""Maps entry points to extension defines."""
166
entrypoints_to_defines = {}
167
168
platform_define = {}
169
for platform in doc.findall('./platforms/platform'):
170
name = platform.attrib['name']
171
define = platform.attrib['protect']
172
platform_define[name] = define
173
174
for extension in doc.findall('./extensions/extension[@platform]'):
175
platform = extension.attrib['platform']
176
define = platform_define[platform]
177
178
for entrypoint in extension.findall('./require/command'):
179
fullname = entrypoint.attrib['name']
180
entrypoints_to_defines[fullname] = define
181
182
return entrypoints_to_defines
183
184
185
def main():
186
parser = argparse.ArgumentParser()
187
parser.add_argument('--out-c', required=True, help='Output C file.')
188
parser.add_argument('--out-h', required=True, help='Output H file.')
189
parser.add_argument('--xml',
190
help='Vulkan API XML file.',
191
required=True, action='append', dest='xml_files')
192
parser.add_argument('--proto', help='Generate entrypoint prototypes',
193
action='store_true', dest='gen_proto')
194
parser.add_argument('--weak', help='Generate weak entrypoint declarations',
195
action='store_true', dest='gen_weak')
196
parser.add_argument('--prefix',
197
help='Prefix to use for all dispatch tables.',
198
action='append', default=[], dest='prefixes')
199
parser.add_argument('--device-prefix',
200
help='Prefix to use for device dispatch tables.',
201
action='append', default=[], dest='device_prefixes')
202
args = parser.parse_args()
203
204
instance_prefixes = args.prefixes
205
physical_device_prefixes = args.prefixes
206
device_prefixes = args.prefixes + args.device_prefixes
207
208
entrypoints = get_entrypoints_from_xml(args.xml_files)
209
210
device_entrypoints = []
211
physical_device_entrypoints = []
212
instance_entrypoints = []
213
for e in entrypoints:
214
if e.is_device_entrypoint():
215
device_entrypoints.append(e)
216
elif e.is_physical_device_entrypoint():
217
physical_device_entrypoints.append(e)
218
else:
219
instance_entrypoints.append(e)
220
221
assert os.path.dirname(args.out_c) == os.path.dirname(args.out_h)
222
223
environment = {
224
'gen_proto': args.gen_proto,
225
'gen_weak': args.gen_weak,
226
'header': os.path.basename(args.out_h),
227
'instance_entrypoints': instance_entrypoints,
228
'instance_prefixes': instance_prefixes,
229
'physical_device_entrypoints': physical_device_entrypoints,
230
'physical_device_prefixes': physical_device_prefixes,
231
'device_entrypoints': device_entrypoints,
232
'device_prefixes': device_prefixes,
233
'filename': os.path.basename(__file__),
234
}
235
236
# For outputting entrypoints.h we generate a anv_EntryPoint() prototype
237
# per entry point.
238
try:
239
with open(args.out_h, 'wb') as f:
240
guard = os.path.basename(args.out_h).replace('.', '_').upper()
241
f.write(TEMPLATE_H.render(guard=guard, **environment))
242
with open(args.out_c, 'wb') as f:
243
f.write(TEMPLATE_C.render(**environment))
244
245
except Exception:
246
# In the event there's an error, this imports some helpers from mako
247
# to print a useful stack trace and prints it, then exits with
248
# status 1, if python is run with debug; otherwise it just raises
249
# the exception
250
if __debug__:
251
import sys
252
from mako import exceptions
253
sys.stderr.write(exceptions.text_error_template().render() + '\n')
254
sys.exit(1)
255
raise
256
257
if __name__ == '__main__':
258
main()
259
260