2 # Copyright © 2017 Intel Corporation
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:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
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
22 """Create enum to string functions for vulking using vk.xml."""
24 from __future__
import print_function
27 import xml
.etree
.cElementTree
as et
29 from mako
.template
import Template
31 VK_XML
= os
.path
.join(os
.path
.dirname(__file__
), '..', 'registry', 'vk.xml')
33 COPYRIGHT
= textwrap
.dedent(u
"""\
34 * Copyright © 2017 Intel Corporation
36 * Permission is hereby granted, free of charge, to any person obtaining a copy
37 * of this software and associated documentation files (the "Software"), to deal
38 * in the Software without restriction, including without limitation the rights
39 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
40 * copies of the Software, and to permit persons to whom the Software is
41 * furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
51 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
54 C_TEMPLATE
= Template(textwrap
.dedent(u
"""\
55 /* Autogenerated file -- do not edit
56 * generated by ${file}
61 #include <vulkan/vulkan.h>
62 #include "util/macros.h"
63 #include "vk_enum_to_str.h"
68 vk_${enum.name[2:]}_to_str(${enum.name} input)
71 % for v in enum.values:
76 unreachable("Undefined enum value.");
80 output_encoding
='utf-8')
82 H_TEMPLATE
= Template(textwrap
.dedent(u
"""\
83 /* Autogenerated file -- do not edit
84 * generated by ${file}
89 #ifndef MESA_VK_ENUM_TO_STR_H
90 #define MESA_VK_ENUM_TO_STR_H
92 #include <vulkan/vulkan.h>
95 const char * vk_${enum.name[2:]}_to_str(${enum.name} input);
99 output_encoding
='utf-8')
102 class EnumFactory(object):
103 """Factory for creating enums."""
105 def __init__(self
, type_
):
109 def __call__(self
, name
):
111 return self
.registry
[name
]
113 n
= self
.registry
[name
] = self
.type(name
)
117 class VkEnum(object):
118 """Simple struct-like class representing a single Vulkan Enum."""
120 def __init__(self
, name
, values
=None):
122 self
.values
= values
or []
125 def xml_parser(filename
):
126 """Parse the XML file and return parsed data.
128 This parser is a memory efficient iterative XML parser that returns a list
131 efactory
= EnumFactory(VkEnum
)
133 with
open(filename
, 'rb') as f
:
134 context
= iter(et
.iterparse(f
, events
=('start', 'end')))
136 # This gives the root element, since goal is to iterate over the
137 # elements without building a tree, this allows the root to be cleared
138 # (erase the elements) after the children have been processed.
139 _
, root
= next(context
)
141 for event
, elem
in context
:
142 if event
== 'end' and elem
.tag
== 'enums':
143 type_
= elem
.attrib
.get('type')
145 enum
= efactory(elem
.attrib
['name'])
146 enum
.values
.extend([e
.attrib
['name'] for e
in elem
148 elif event
== 'end' and elem
.tag
== 'extension':
149 if elem
.attrib
['supported'] != 'vulkan':
151 for e
in elem
.findall('.//enum[@extends][@offset]'):
152 enum
= efactory(e
.attrib
['extends'])
153 enum
.values
.append(e
.attrib
['name'])
157 return efactory
.registry
.values()
161 enums
= xml_parser(VK_XML
)
162 for template
, file_
in [(C_TEMPLATE
, 'util/vk_enum_to_str.c'),
163 (H_TEMPLATE
, 'util/vk_enum_to_str.h')]:
164 with
open(file_
, 'wb') as f
:
165 f
.write(template
.render(
166 file=os
.path
.basename(__file__
),
168 copyright
=COPYRIGHT
))
171 if __name__
== '__main__':