anv: replace hard-coded platform list with vk.xml parse
[mesa.git] / src / intel / vulkan / anv_extensions_gen.py
1 COPYRIGHT = """\
2 /*
3 * Copyright 2017 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 """
26
27 import argparse
28 import xml.etree.cElementTree as et
29
30 from mako.template import Template
31
32 from anv_extensions import *
33
34 platform_defines = []
35
36 def _init_exts_from_xml(xml):
37 """ Walk the Vulkan XML and fill out extra extension information. """
38
39 xml = et.parse(xml)
40
41 ext_name_map = {}
42 for ext in EXTENSIONS:
43 ext_name_map[ext.name] = ext
44
45 for platform in xml.findall('./platforms/platform'):
46 platform_defines.append(platform.attrib['protect'])
47
48 for ext_elem in xml.findall('.extensions/extension'):
49 ext_name = ext_elem.attrib['name']
50 if ext_name not in ext_name_map:
51 continue
52
53 ext = ext_name_map[ext_name]
54 ext.type = ext_elem.attrib['type']
55
56 _TEMPLATE_H = Template(COPYRIGHT + """
57
58 #ifndef ANV_EXTENSIONS_H
59 #define ANV_EXTENSIONS_H
60
61 #include "stdbool.h"
62
63 #define ANV_INSTANCE_EXTENSION_COUNT ${len(instance_extensions)}
64
65 extern const VkExtensionProperties anv_instance_extensions[];
66
67 struct anv_instance_extension_table {
68 union {
69 bool extensions[ANV_INSTANCE_EXTENSION_COUNT];
70 struct {
71 %for ext in instance_extensions:
72 bool ${ext.name[3:]};
73 %endfor
74 };
75 };
76 };
77
78 extern const struct anv_instance_extension_table anv_instance_extensions_supported;
79
80
81 #define ANV_DEVICE_EXTENSION_COUNT ${len(device_extensions)}
82
83 extern const VkExtensionProperties anv_device_extensions[];
84
85 struct anv_device_extension_table {
86 union {
87 bool extensions[ANV_DEVICE_EXTENSION_COUNT];
88 struct {
89 %for ext in device_extensions:
90 bool ${ext.name[3:]};
91 %endfor
92 };
93 };
94 };
95
96 struct anv_physical_device;
97
98 void
99 anv_physical_device_get_supported_extensions(const struct anv_physical_device *device,
100 struct anv_device_extension_table *extensions);
101
102 #endif /* ANV_EXTENSIONS_H */
103 """)
104
105 _TEMPLATE_C = Template(COPYRIGHT + """
106 #include "anv_private.h"
107
108 #include "vk_util.h"
109
110 /* Convert the VK_USE_PLATFORM_* defines to booleans */
111 %for platform_define in platform_defines:
112 #ifdef ${platform_define}
113 # undef ${platform_define}
114 # define ${platform_define} true
115 #else
116 # define ${platform_define} false
117 #endif
118 %endfor
119
120 /* And ANDROID too */
121 #ifdef ANDROID
122 # undef ANDROID
123 # define ANDROID true
124 #else
125 # define ANDROID false
126 #endif
127
128 #define ANV_HAS_SURFACE (VK_USE_PLATFORM_WAYLAND_KHR || \\
129 VK_USE_PLATFORM_XCB_KHR || \\
130 VK_USE_PLATFORM_XLIB_KHR || \\
131 VK_USE_PLATFORM_DISPLAY_KHR)
132
133 static const uint32_t MAX_API_VERSION = ${MAX_API_VERSION.c_vk_version()};
134
135 VkResult anv_EnumerateInstanceVersion(
136 uint32_t* pApiVersion)
137 {
138 *pApiVersion = MAX_API_VERSION;
139 return VK_SUCCESS;
140 }
141
142 const VkExtensionProperties anv_instance_extensions[ANV_INSTANCE_EXTENSION_COUNT] = {
143 %for ext in instance_extensions:
144 {"${ext.name}", ${ext.ext_version}},
145 %endfor
146 };
147
148 const struct anv_instance_extension_table anv_instance_extensions_supported = {
149 %for ext in instance_extensions:
150 .${ext.name[3:]} = ${ext.enable},
151 %endfor
152 };
153
154 uint32_t
155 anv_physical_device_api_version(struct anv_physical_device *device)
156 {
157 uint32_t version = 0;
158
159 uint32_t override = vk_get_version_override();
160 if (override)
161 return MIN2(override, MAX_API_VERSION);
162
163 %for version in API_VERSIONS:
164 if (!(${version.enable}))
165 return version;
166 version = ${version.version.c_vk_version()};
167
168 %endfor
169 return version;
170 }
171
172 const VkExtensionProperties anv_device_extensions[ANV_DEVICE_EXTENSION_COUNT] = {
173 %for ext in device_extensions:
174 {"${ext.name}", ${ext.ext_version}},
175 %endfor
176 };
177
178 void
179 anv_physical_device_get_supported_extensions(const struct anv_physical_device *device,
180 struct anv_device_extension_table *extensions)
181 {
182 *extensions = (struct anv_device_extension_table) {
183 %for ext in device_extensions:
184 .${ext.name[3:]} = ${ext.enable},
185 %endfor
186 };
187 }
188 """)
189
190 if __name__ == '__main__':
191 parser = argparse.ArgumentParser()
192 parser.add_argument('--out-c', help='Output C file.')
193 parser.add_argument('--out-h', help='Output H file.')
194 parser.add_argument('--xml',
195 help='Vulkan API XML file.',
196 required=True,
197 action='append',
198 dest='xml_files')
199 args = parser.parse_args()
200
201 for filename in args.xml_files:
202 _init_exts_from_xml(filename)
203
204 for ext in EXTENSIONS:
205 assert ext.type == 'instance' or ext.type == 'device'
206
207 template_env = {
208 'API_VERSIONS': API_VERSIONS,
209 'MAX_API_VERSION': MAX_API_VERSION,
210 'instance_extensions': [e for e in EXTENSIONS if e.type == 'instance'],
211 'device_extensions': [e for e in EXTENSIONS if e.type == 'device'],
212 'platform_defines': platform_defines,
213 }
214
215 if args.out_h:
216 with open(args.out_h, 'w') as f:
217 f.write(_TEMPLATE_H.render(**template_env))
218
219 if args.out_c:
220 with open(args.out_c, 'w') as f:
221 f.write(_TEMPLATE_C.render(**template_env))