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