b65bc7f7ef64f166130fce073b7becc6b9be8d5d
[mesa.git] / src / vulkan / util / gen_enum_to_str.py
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.cElementTree 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 "util/macros.h"
64 #include "vk_enum_to_str.h"
65
66 % for enum in enums:
67
68 % if enum.guard:
69 #ifdef ${enum.guard}
70 % endif
71 const char *
72 vk_${enum.name[2:]}_to_str(${enum.name} input)
73 {
74 #pragma GCC diagnostic push
75 #pragma GCC diagnostic ignored "-Wswitch"
76 switch(input) {
77 % for v in sorted(enum.values.keys()):
78 case ${v}:
79 return "${enum.values[v]}";
80 % endfor
81 }
82 #pragma GCC diagnostic pop
83 unreachable("Undefined enum value.");
84 }
85
86 % if enum.guard:
87 #endif
88 % endif
89 %endfor
90
91 size_t vk_structure_type_size(const struct VkBaseInStructure *item)
92 {
93 #pragma GCC diagnostic push
94 #pragma GCC diagnostic ignored "-Wswitch"
95 switch(item->sType) {
96 % for struct in structs:
97 % if struct.extension is not None and struct.extension.define is not None:
98 #ifdef ${struct.extension.define}
99 case ${struct.stype}: return sizeof(${struct.name});
100 #endif
101 % else:
102 case ${struct.stype}: return sizeof(${struct.name});
103 % endif
104 %endfor
105 }
106 #pragma GCC diagnostic pop
107 unreachable("Undefined struct type.");
108 }
109
110 void vk_load_instance_commands(VkInstance instance,
111 PFN_vkGetInstanceProcAddr gpa,
112 struct vk_instance_dispatch_table *table)
113 {
114 memset(table, 0, sizeof(*table));
115 table->GetInstanceProcAddr = gpa;
116 % for cmd in commands:
117 % if not cmd.device_entrypoint and cmd.name != 'vkGetInstanceProcAddr':
118 % if cmd.extension is not None and cmd.extension.define is not None:
119 #ifdef ${cmd.extension.define}
120 table->${cmd.name[2:]} = (PFN_${cmd.name}) gpa(instance, "${cmd.name}");
121 #endif
122 % else:
123 table->${cmd.name[2:]} = (PFN_${cmd.name}) gpa(instance, "${cmd.name}");
124 % endif
125 % endif
126 %endfor
127 }
128
129 void vk_load_device_commands(VkDevice device,
130 PFN_vkGetDeviceProcAddr gpa,
131 struct vk_device_dispatch_table *table)
132 {
133 memset(table, 0, sizeof(*table));
134 table->GetDeviceProcAddr = gpa;
135 % for cmd in commands:
136 % if cmd.device_entrypoint and cmd.name != 'vkGetDeviceProcAddr':
137 % if cmd.extension is not None and cmd.extension.define is not None:
138 #ifdef ${cmd.extension.define}
139 table->${cmd.name[2:]} = (PFN_${cmd.name}) gpa(device, "${cmd.name}");
140 #endif
141 % else:
142 table->${cmd.name[2:]} = (PFN_${cmd.name}) gpa(device, "${cmd.name}");
143 % endif
144 % endif
145 %endfor
146 }
147 """),
148 output_encoding='utf-8')
149
150 H_TEMPLATE = Template(textwrap.dedent(u"""\
151 /* Autogenerated file -- do not edit
152 * generated by ${file}
153 *
154 ${copyright}
155 */
156
157 #ifndef MESA_VK_ENUM_TO_STR_H
158 #define MESA_VK_ENUM_TO_STR_H
159
160 #include <vulkan/vulkan.h>
161 #include <vulkan/vk_android_native_buffer.h>
162
163 #ifdef __cplusplus
164 extern "C" {
165 #endif
166
167 % for ext in extensions:
168 #define _${ext.name}_number (${ext.number})
169 % endfor
170
171 % for enum in enums:
172 % if enum.guard:
173 #ifdef ${enum.guard}
174 % endif
175 const char * vk_${enum.name[2:]}_to_str(${enum.name} input);
176 % if enum.guard:
177 #endif
178 % endif
179 % endfor
180
181 size_t vk_structure_type_size(const struct VkBaseInStructure *item);
182
183 struct vk_instance_dispatch_table {
184 PFN_vkGetInstanceProcAddr GetInstanceProcAddr;
185 % for cmd in commands:
186 % if not cmd.device_entrypoint and cmd.name != 'vkGetInstanceProcAddr':
187 % if cmd.extension is not None and cmd.extension.define is not None:
188 #ifdef ${cmd.extension.define}
189 PFN_${cmd.name} ${cmd.name[2:]};
190 #endif
191 % else:
192 PFN_${cmd.name} ${cmd.name[2:]};
193 % endif
194 % endif
195 %endfor
196 };
197
198 struct vk_device_dispatch_table {
199 PFN_vkGetDeviceProcAddr GetDeviceProcAddr;
200 % for cmd in commands:
201 % if cmd.device_entrypoint and cmd.name != 'vkGetDeviceProcAddr':
202 % if cmd.extension is not None and cmd.extension.define is not None:
203 #ifdef ${cmd.extension.define}
204 PFN_${cmd.name} ${cmd.name[2:]};
205 #endif
206 % else:
207 PFN_${cmd.name} ${cmd.name[2:]};
208 % endif
209 % endif
210 %endfor
211 };
212
213 void vk_load_instance_commands(VkInstance instance, PFN_vkGetInstanceProcAddr gpa, struct vk_instance_dispatch_table *table);
214 void vk_load_device_commands(VkDevice device, PFN_vkGetDeviceProcAddr gpa, struct vk_device_dispatch_table *table);
215
216 #ifdef __cplusplus
217 } /* extern "C" */
218 #endif
219
220 #endif"""),
221 output_encoding='utf-8')
222
223
224 class NamedFactory(object):
225 """Factory for creating enums."""
226
227 def __init__(self, type_):
228 self.registry = {}
229 self.type = type_
230
231 def __call__(self, name, **kwargs):
232 try:
233 return self.registry[name]
234 except KeyError:
235 n = self.registry[name] = self.type(name, **kwargs)
236 return n
237
238 def get(self, name):
239 return self.registry.get(name)
240
241
242 class VkExtension(object):
243 """Simple struct-like class representing extensions"""
244
245 def __init__(self, name, number=None, define=None):
246 self.name = name
247 self.number = number
248 self.define = define
249
250
251 class VkEnum(object):
252 """Simple struct-like class representing a single Vulkan Enum."""
253
254 def __init__(self, name, values=None):
255 self.name = name
256 self.extension = None
257 # Maps numbers to names
258 self.values = values or dict()
259 self.name_to_value = dict()
260 self.guard = None
261
262 def add_value(self, name, value=None,
263 extnum=None, offset=None,
264 error=False):
265 assert value is not None or extnum is not None
266 if value is None:
267 value = 1000000000 + (extnum - 1) * 1000 + offset
268 if error:
269 value = -value
270
271 self.name_to_value[name] = value
272 if value not in self.values:
273 self.values[value] = name
274 elif len(self.values[value]) > len(name):
275 self.values[value] = name
276
277 def add_value_from_xml(self, elem, extension=None):
278 self.extension = extension
279 if 'value' in elem.attrib:
280 self.add_value(elem.attrib['name'],
281 value=int(elem.attrib['value'], base=0))
282 elif 'alias' in elem.attrib:
283 self.add_value(elem.attrib['name'],
284 value=self.name_to_value[elem.attrib['alias']])
285 else:
286 error = 'dir' in elem.attrib and elem.attrib['dir'] == '-'
287 if 'extnumber' in elem.attrib:
288 extnum = int(elem.attrib['extnumber'])
289 else:
290 extnum = extension.number
291 self.add_value(elem.attrib['name'],
292 extnum=extnum,
293 offset=int(elem.attrib['offset']),
294 error=error)
295
296 def set_guard(self, g):
297 self.guard = g
298
299
300 class VkCommand(object):
301 """Simple struct-like class representing a single Vulkan command"""
302
303 def __init__(self, name, device_entrypoint=False):
304 self.name = name
305 self.device_entrypoint = device_entrypoint
306 self.extension = None
307
308
309 class VkChainStruct(object):
310 """Simple struct-like class representing a single Vulkan struct identified with a VkStructureType"""
311 def __init__(self, name, stype):
312 self.name = name
313 self.stype = stype
314 self.extension = None
315
316
317 def struct_get_stype(xml_node):
318 for member in xml_node.findall('./member'):
319 name = member.findall('./name')
320 if len(name) > 0 and name[0].text == "sType":
321 return member.get('values')
322 return None
323
324
325 def parse_xml(cmd_factory, enum_factory, ext_factory, struct_factory, filename):
326 """Parse the XML file. Accumulate results into the factories.
327
328 This parser is a memory efficient iterative XML parser that returns a list
329 of VkEnum objects.
330 """
331
332 xml = et.parse(filename)
333
334 for enum_type in xml.findall('./enums[@type="enum"]'):
335 enum = enum_factory(enum_type.attrib['name'])
336 for value in enum_type.findall('./enum'):
337 enum.add_value_from_xml(value)
338
339 for value in xml.findall('./feature/require/enum[@extends]'):
340 enum = enum_factory.get(value.attrib['extends'])
341 if enum is not None:
342 enum.add_value_from_xml(value)
343
344 for command in xml.findall('./commands/command'):
345 name = command.find('./proto/name')
346 first_arg = command.find('./param/type')
347 # Some commands are alias KHR -> nonKHR, ignore those
348 if name is not None:
349 cmd_factory(name.text,
350 device_entrypoint=(first_arg.text in ('VkDevice', 'VkCommandBuffer', 'VkQueue')))
351
352 for struct_type in xml.findall('./types/type[@category="struct"]'):
353 name = struct_type.attrib['name']
354 stype = struct_get_stype(struct_type)
355 if stype is not None:
356 struct_factory(name, stype=stype)
357
358 platform_define = {}
359 for platform in xml.findall('./platforms/platform'):
360 name = platform.attrib['name']
361 define = platform.attrib['protect']
362 platform_define[name] = define
363
364 for ext_elem in xml.findall('./extensions/extension[@supported="vulkan"]'):
365 define = None
366 if "platform" in ext_elem.attrib:
367 define = platform_define[ext_elem.attrib['platform']]
368 extension = ext_factory(ext_elem.attrib['name'],
369 number=int(ext_elem.attrib['number']),
370 define=define)
371
372 for value in ext_elem.findall('./require/enum[@extends]'):
373 enum = enum_factory.get(value.attrib['extends'])
374 if enum is not None:
375 enum.add_value_from_xml(value, extension)
376 for t in ext_elem.findall('./require/type'):
377 struct = struct_factory.get(t.attrib['name'])
378 if struct is not None:
379 struct.extension = extension
380
381 if define:
382 for value in ext_elem.findall('./require/type[@name]'):
383 enum = enum_factory.get(value.attrib['name'])
384 if enum is not None:
385 enum.set_guard(define)
386
387 for t in ext_elem.findall('./require/command'):
388 command = cmd_factory.get(t.attrib['name'])
389 if command is not None:
390 command.extension = extension
391
392
393 def main():
394 parser = argparse.ArgumentParser()
395 parser.add_argument('--xml', required=True,
396 help='Vulkan API XML files',
397 action='append',
398 dest='xml_files')
399 parser.add_argument('--outdir',
400 help='Directory to put the generated files in',
401 required=True)
402
403 args = parser.parse_args()
404
405 command_factory = NamedFactory(VkCommand)
406 enum_factory = NamedFactory(VkEnum)
407 ext_factory = NamedFactory(VkExtension)
408 struct_factory = NamedFactory(VkChainStruct)
409 for filename in args.xml_files:
410 parse_xml(command_factory, enum_factory, ext_factory, struct_factory, filename)
411 commands = sorted(command_factory.registry.values(), key=lambda e: e.name)
412 enums = sorted(enum_factory.registry.values(), key=lambda e: e.name)
413 extensions = sorted(ext_factory.registry.values(), key=lambda e: e.name)
414 structs = sorted(struct_factory.registry.values(), key=lambda e: e.name)
415
416 for template, file_ in [(C_TEMPLATE, os.path.join(args.outdir, 'vk_enum_to_str.c')),
417 (H_TEMPLATE, os.path.join(args.outdir, 'vk_enum_to_str.h'))]:
418 with open(file_, 'wb') as f:
419 f.write(template.render(
420 file=os.path.basename(__file__),
421 commands=commands,
422 enums=enums,
423 extensions=extensions,
424 structs=structs,
425 copyright=COPYRIGHT))
426
427
428 if __name__ == '__main__':
429 main()