Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/vulkan/util/gen_enum_to_str.py
7129 views
1
# encoding=utf-8
2
# Copyright © 2017 Intel Corporation
3
4
# Permission is hereby granted, free of charge, to any person obtaining a copy
5
# of this software and associated documentation files (the "Software"), to deal
6
# in the Software without restriction, including without limitation the rights
7
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
# copies of the Software, and to permit persons to whom the Software is
9
# furnished to do so, subject to the following conditions:
10
11
# The above copyright notice and this permission notice shall be included in
12
# all copies or substantial portions of the Software.
13
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
# SOFTWARE.
21
22
"""Create enum to string functions for vulkan using vk.xml."""
23
24
from __future__ import print_function
25
import argparse
26
import os
27
import textwrap
28
import xml.etree.ElementTree as et
29
30
from mako.template import Template
31
32
COPYRIGHT = textwrap.dedent(u"""\
33
* Copyright © 2017 Intel Corporation
34
*
35
* Permission is hereby granted, free of charge, to any person obtaining a copy
36
* of this software and associated documentation files (the "Software"), to deal
37
* in the Software without restriction, including without limitation the rights
38
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
39
* copies of the Software, and to permit persons to whom the Software is
40
* furnished to do so, subject to the following conditions:
41
*
42
* The above copyright notice and this permission notice shall be included in
43
* all copies or substantial portions of the Software.
44
*
45
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
48
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
50
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
51
* SOFTWARE.""")
52
53
C_TEMPLATE = Template(textwrap.dedent(u"""\
54
/* Autogenerated file -- do not edit
55
* generated by ${file}
56
*
57
${copyright}
58
*/
59
60
#include <string.h>
61
#include <vulkan/vulkan.h>
62
#include <vulkan/vk_android_native_buffer.h>
63
#include <vulkan/vk_layer.h>
64
#include "util/macros.h"
65
#include "vk_enum_to_str.h"
66
67
% for enum in enums:
68
69
% if enum.guard:
70
#ifdef ${enum.guard}
71
% endif
72
const char *
73
vk_${enum.name[2:]}_to_str(${enum.name} input)
74
{
75
switch((int64_t)input) {
76
% for v in sorted(enum.values.keys()):
77
case ${v}:
78
return "${enum.values[v]}";
79
% endfor
80
default:
81
unreachable("Undefined enum value.");
82
}
83
}
84
85
% if enum.guard:
86
#endif
87
% endif
88
%endfor
89
90
size_t vk_structure_type_size(const struct VkBaseInStructure *item)
91
{
92
switch((int)item->sType) {
93
% for struct in structs:
94
% if struct.extension is not None and struct.extension.define is not None:
95
#ifdef ${struct.extension.define}
96
case ${struct.stype}: return sizeof(${struct.name});
97
#endif
98
% else:
99
case ${struct.stype}: return sizeof(${struct.name});
100
% endif
101
%endfor
102
case VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO: return sizeof(VkLayerInstanceCreateInfo);
103
case VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO: return sizeof(VkLayerDeviceCreateInfo);
104
default:
105
unreachable("Undefined struct type.");
106
}
107
}
108
"""),
109
output_encoding='utf-8')
110
111
H_TEMPLATE = Template(textwrap.dedent(u"""\
112
/* Autogenerated file -- do not edit
113
* generated by ${file}
114
*
115
${copyright}
116
*/
117
118
#ifndef MESA_VK_ENUM_TO_STR_H
119
#define MESA_VK_ENUM_TO_STR_H
120
121
#include <vulkan/vulkan.h>
122
#include <vulkan/vk_android_native_buffer.h>
123
124
#ifdef __cplusplus
125
extern "C" {
126
#endif
127
128
% for ext in extensions:
129
#define _${ext.name}_number (${ext.number})
130
% endfor
131
132
% for enum in enums:
133
% if enum.guard:
134
#ifdef ${enum.guard}
135
% endif
136
const char * vk_${enum.name[2:]}_to_str(${enum.name} input);
137
% if enum.guard:
138
#endif
139
% endif
140
% endfor
141
142
size_t vk_structure_type_size(const struct VkBaseInStructure *item);
143
144
#ifdef __cplusplus
145
} /* extern "C" */
146
#endif
147
148
#endif"""),
149
output_encoding='utf-8')
150
151
152
class NamedFactory(object):
153
"""Factory for creating enums."""
154
155
def __init__(self, type_):
156
self.registry = {}
157
self.type = type_
158
159
def __call__(self, name, **kwargs):
160
try:
161
return self.registry[name]
162
except KeyError:
163
n = self.registry[name] = self.type(name, **kwargs)
164
return n
165
166
def get(self, name):
167
return self.registry.get(name)
168
169
170
class VkExtension(object):
171
"""Simple struct-like class representing extensions"""
172
173
def __init__(self, name, number=None, define=None):
174
self.name = name
175
self.number = number
176
self.define = define
177
178
179
class VkEnum(object):
180
"""Simple struct-like class representing a single Vulkan Enum."""
181
182
def __init__(self, name, values=None):
183
self.name = name
184
self.extension = None
185
# Maps numbers to names
186
self.values = values or dict()
187
self.name_to_value = dict()
188
self.guard = None
189
self.name_to_alias_list = {}
190
191
def add_value(self, name, value=None,
192
extnum=None, offset=None, alias=None,
193
error=False):
194
if alias is not None:
195
assert value is None and offset is None
196
if alias not in self.name_to_value:
197
# We don't have this alias yet. Just record the alias and
198
# we'll deal with it later.
199
alias_list = self.name_to_alias_list.get(alias, [])
200
alias_list.append(name);
201
return
202
203
# Use the value from the alias
204
value = self.name_to_value[alias]
205
206
assert value is not None or extnum is not None
207
if value is None:
208
value = 1000000000 + (extnum - 1) * 1000 + offset
209
if error:
210
value = -value
211
212
self.name_to_value[name] = value
213
if value not in self.values:
214
self.values[value] = name
215
elif len(self.values[value]) > len(name):
216
self.values[value] = name
217
218
# Now that the value has been fully added, resolve aliases, if any.
219
if name in self.name_to_alias_list:
220
for alias in self.name_to_alias_list[name]:
221
add_value(alias, value)
222
del self.name_to_alias_list[name]
223
224
def add_value_from_xml(self, elem, extension=None):
225
self.extension = extension
226
if 'value' in elem.attrib:
227
self.add_value(elem.attrib['name'],
228
value=int(elem.attrib['value'], base=0))
229
elif 'alias' in elem.attrib:
230
self.add_value(elem.attrib['name'], alias=elem.attrib['alias'])
231
else:
232
error = 'dir' in elem.attrib and elem.attrib['dir'] == '-'
233
if 'extnumber' in elem.attrib:
234
extnum = int(elem.attrib['extnumber'])
235
else:
236
extnum = extension.number
237
self.add_value(elem.attrib['name'],
238
extnum=extnum,
239
offset=int(elem.attrib['offset']),
240
error=error)
241
242
def set_guard(self, g):
243
self.guard = g
244
245
246
class VkCommand(object):
247
"""Simple struct-like class representing a single Vulkan command"""
248
249
def __init__(self, name, device_entrypoint=False):
250
self.name = name
251
self.device_entrypoint = device_entrypoint
252
self.extension = None
253
254
255
class VkChainStruct(object):
256
"""Simple struct-like class representing a single Vulkan struct identified with a VkStructureType"""
257
def __init__(self, name, stype):
258
self.name = name
259
self.stype = stype
260
self.extension = None
261
262
263
def struct_get_stype(xml_node):
264
for member in xml_node.findall('./member'):
265
name = member.findall('./name')
266
if len(name) > 0 and name[0].text == "sType":
267
return member.get('values')
268
return None
269
270
271
def parse_xml(enum_factory, ext_factory, struct_factory, filename):
272
"""Parse the XML file. Accumulate results into the factories.
273
274
This parser is a memory efficient iterative XML parser that returns a list
275
of VkEnum objects.
276
"""
277
278
xml = et.parse(filename)
279
280
for enum_type in xml.findall('./enums[@type="enum"]'):
281
enum = enum_factory(enum_type.attrib['name'])
282
for value in enum_type.findall('./enum'):
283
enum.add_value_from_xml(value)
284
285
for value in xml.findall('./feature/require/enum[@extends]'):
286
enum = enum_factory.get(value.attrib['extends'])
287
if enum is not None:
288
enum.add_value_from_xml(value)
289
290
for struct_type in xml.findall('./types/type[@category="struct"]'):
291
name = struct_type.attrib['name']
292
stype = struct_get_stype(struct_type)
293
if stype is not None:
294
struct_factory(name, stype=stype)
295
296
platform_define = {}
297
for platform in xml.findall('./platforms/platform'):
298
name = platform.attrib['name']
299
define = platform.attrib['protect']
300
platform_define[name] = define
301
302
for ext_elem in xml.findall('./extensions/extension[@supported="vulkan"]'):
303
define = None
304
if "platform" in ext_elem.attrib:
305
define = platform_define[ext_elem.attrib['platform']]
306
extension = ext_factory(ext_elem.attrib['name'],
307
number=int(ext_elem.attrib['number']),
308
define=define)
309
310
for value in ext_elem.findall('./require/enum[@extends]'):
311
enum = enum_factory.get(value.attrib['extends'])
312
if enum is not None:
313
enum.add_value_from_xml(value, extension)
314
for t in ext_elem.findall('./require/type'):
315
struct = struct_factory.get(t.attrib['name'])
316
if struct is not None:
317
struct.extension = extension
318
319
if define:
320
for value in ext_elem.findall('./require/type[@name]'):
321
enum = enum_factory.get(value.attrib['name'])
322
if enum is not None:
323
enum.set_guard(define)
324
325
326
def main():
327
parser = argparse.ArgumentParser()
328
parser.add_argument('--xml', required=True,
329
help='Vulkan API XML files',
330
action='append',
331
dest='xml_files')
332
parser.add_argument('--outdir',
333
help='Directory to put the generated files in',
334
required=True)
335
336
args = parser.parse_args()
337
338
enum_factory = NamedFactory(VkEnum)
339
ext_factory = NamedFactory(VkExtension)
340
struct_factory = NamedFactory(VkChainStruct)
341
for filename in args.xml_files:
342
parse_xml(enum_factory, ext_factory, struct_factory, filename)
343
enums = sorted(enum_factory.registry.values(), key=lambda e: e.name)
344
extensions = sorted(ext_factory.registry.values(), key=lambda e: e.name)
345
structs = sorted(struct_factory.registry.values(), key=lambda e: e.name)
346
347
for template, file_ in [(C_TEMPLATE, os.path.join(args.outdir, 'vk_enum_to_str.c')),
348
(H_TEMPLATE, os.path.join(args.outdir, 'vk_enum_to_str.h'))]:
349
with open(file_, 'wb') as f:
350
f.write(template.render(
351
file=os.path.basename(__file__),
352
enums=enums,
353
extensions=extensions,
354
structs=structs,
355
copyright=COPYRIGHT))
356
357
358
if __name__ == '__main__':
359
main()
360
361