vulkan/util: use the platform defines in vk.xml instead of hard-coding them
[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, define=None):
225 self.name = name
226 self.number = number
227 self.define = define
228
229
230 class VkEnum(object):
231 """Simple struct-like class representing a single Vulkan Enum."""
232
233 def __init__(self, name, values=None):
234 self.name = name
235 # Maps numbers to names
236 self.values = values or dict()
237 self.name_to_value = dict()
238
239 def add_value(self, name, value=None,
240 extnum=None, offset=None,
241 error=False):
242 assert value is not None or extnum is not None
243 if value is None:
244 value = 1000000000 + (extnum - 1) * 1000 + offset
245 if error:
246 value = -value
247
248 self.name_to_value[name] = value
249 if value not in self.values:
250 self.values[value] = name
251 elif len(self.values[value]) > len(name):
252 self.values[value] = name
253
254 def add_value_from_xml(self, elem, extension=None):
255 if 'value' in elem.attrib:
256 self.add_value(elem.attrib['name'],
257 value=int(elem.attrib['value'], base=0))
258 elif 'alias' in elem.attrib:
259 self.add_value(elem.attrib['name'],
260 value=self.name_to_value[elem.attrib['alias']])
261 else:
262 error = 'dir' in elem.attrib and elem.attrib['dir'] == '-'
263 if 'extnumber' in elem.attrib:
264 extnum = int(elem.attrib['extnumber'])
265 else:
266 extnum = extension.number
267 self.add_value(elem.attrib['name'],
268 extnum=extnum,
269 offset=int(elem.attrib['offset']),
270 error=error)
271
272
273 class VkCommand(object):
274 """Simple struct-like class representing a single Vulkan command"""
275
276 def __init__(self, name, device_entrypoint=False):
277 self.name = name
278 self.device_entrypoint = device_entrypoint
279 self.extension = None
280
281
282 def parse_xml(cmd_factory, enum_factory, ext_factory, filename):
283 """Parse the XML file. Accumulate results into the factories.
284
285 This parser is a memory efficient iterative XML parser that returns a list
286 of VkEnum objects.
287 """
288
289 xml = et.parse(filename)
290
291 for enum_type in xml.findall('./enums[@type="enum"]'):
292 enum = enum_factory(enum_type.attrib['name'])
293 for value in enum_type.findall('./enum'):
294 enum.add_value_from_xml(value)
295
296 for value in xml.findall('./feature/require/enum[@extends]'):
297 enum = enum_factory.get(value.attrib['extends'])
298 if enum is not None:
299 enum.add_value_from_xml(value)
300
301 for command in xml.findall('./commands/command'):
302 name = command.find('./proto/name')
303 first_arg = command.find('./param/type')
304 # Some commands are alias KHR -> nonKHR, ignore those
305 if name is not None:
306 cmd_factory(name.text,
307 device_entrypoint=(first_arg.text in ('VkDevice', 'VkCommandBuffer', 'VkQueue')))
308
309 platform_define = {}
310 for platform in xml.findall('./platforms/platform'):
311 name = platform.attrib['name']
312 define = platform.attrib['protect']
313 platform_define[name] = define
314
315 for ext_elem in xml.findall('./extensions/extension[@supported="vulkan"]'):
316 platform = None
317 if "platform" in ext_elem.attrib:
318 define = platform_define[ext_elem.attrib['platform']]
319 extension = ext_factory(ext_elem.attrib['name'],
320 number=int(ext_elem.attrib['number']),
321 define=define)
322
323 for value in ext_elem.findall('./require/enum[@extends]'):
324 enum = enum_factory.get(value.attrib['extends'])
325 if enum is not None:
326 enum.add_value_from_xml(value, extension)
327
328 for t in ext_elem.findall('./require/command'):
329 command = cmd_factory.get(t.attrib['name'])
330 if command is not None:
331 command.extension = extension
332
333
334 def main():
335 parser = argparse.ArgumentParser()
336 parser.add_argument('--xml', required=True,
337 help='Vulkan API XML files',
338 action='append',
339 dest='xml_files')
340 parser.add_argument('--outdir',
341 help='Directory to put the generated files in',
342 required=True)
343
344 args = parser.parse_args()
345
346 command_factory = NamedFactory(VkCommand)
347 enum_factory = NamedFactory(VkEnum)
348 ext_factory = NamedFactory(VkExtension)
349 for filename in args.xml_files:
350 parse_xml(command_factory, enum_factory, ext_factory, filename)
351 commands = sorted(command_factory.registry.values(), key=lambda e: e.name)
352 enums = sorted(enum_factory.registry.values(), key=lambda e: e.name)
353 extensions = sorted(ext_factory.registry.values(), key=lambda e: e.name)
354
355 for template, file_ in [(C_TEMPLATE, os.path.join(args.outdir, 'vk_enum_to_str.c')),
356 (H_TEMPLATE, os.path.join(args.outdir, 'vk_enum_to_str.h'))]:
357 with open(file_, 'wb') as f:
358 f.write(template.render(
359 file=os.path.basename(__file__),
360 commands=commands,
361 enums=enums,
362 extensions=extensions,
363 copyright=COPYRIGHT,
364 FOREIGN_ENUM_VALUES=FOREIGN_ENUM_VALUES))
365
366
367 if __name__ == '__main__':
368 main()