dadf20a687e8b1d6e090d50dd83b1a05afc437fb
[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 def _init_exts_from_xml(xml):
35 """ Walk the Vulkan XML and fill out extra extension information. """
36
37 xml = et.parse(xml)
38
39 ext_name_map = {}
40 for ext in EXTENSIONS:
41 ext_name_map[ext.name] = ext
42
43 for ext_elem in xml.findall('.extensions/extension'):
44 ext_name = ext_elem.attrib['name']
45 if ext_name not in ext_name_map:
46 continue
47
48 # Workaround for VK_ANDROID_native_buffer. Its <extension> element in
49 # vk.xml lists it as supported="disabled" and provides only a stub
50 # definition. Its <extension> element in Mesa's custom
51 # vk_android_native_buffer.xml, though, lists it as
52 # supported='android-vendor' and fully defines the extension. We want
53 # to skip the <extension> element in vk.xml.
54 if ext_elem.attrib['supported'] == 'disabled':
55 assert ext_name == 'VK_ANDROID_native_buffer'
56 continue
57
58 ext = ext_name_map[ext_name]
59 ext.type = ext_elem.attrib['type']
60
61 _TEMPLATE_H = Template(COPYRIGHT + """
62
63 #ifndef ANV_EXTENSIONS_H
64 #define ANV_EXTENSIONS_H
65
66 #include "stdbool.h"
67
68 #define ANV_INSTANCE_EXTENSION_COUNT ${len(instance_extensions)}
69
70 extern const VkExtensionProperties anv_instance_extensions[];
71
72 struct anv_instance_extension_table {
73 union {
74 bool extensions[ANV_INSTANCE_EXTENSION_COUNT];
75 struct {
76 %for ext in instance_extensions:
77 bool ${ext.name[3:]};
78 %endfor
79 };
80 };
81 };
82
83
84 #define ANV_DEVICE_EXTENSION_COUNT ${len(device_extensions)}
85
86 extern const VkExtensionProperties anv_device_extensions[];
87
88 struct anv_device_extension_table {
89 union {
90 bool extensions[ANV_DEVICE_EXTENSION_COUNT];
91 struct {
92 %for ext in device_extensions:
93 bool ${ext.name[3:]};
94 %endfor
95 };
96 };
97 };
98
99 #endif /* ANV_EXTENSIONS_H */
100 """)
101
102 _TEMPLATE_C = Template(COPYRIGHT + """
103 #include "anv_private.h"
104
105 #include "vk_util.h"
106
107 /* Convert the VK_USE_PLATFORM_* defines to booleans */
108 %for platform in ['ANDROID', 'WAYLAND', 'XCB', 'XLIB']:
109 #ifdef VK_USE_PLATFORM_${platform}_KHR
110 # undef VK_USE_PLATFORM_${platform}_KHR
111 # define VK_USE_PLATFORM_${platform}_KHR true
112 #else
113 # define VK_USE_PLATFORM_${platform}_KHR false
114 #endif
115 %endfor
116
117 /* And ANDROID too */
118 #ifdef ANDROID
119 # undef ANDROID
120 # define ANDROID true
121 #else
122 # define ANDROID false
123 #endif
124
125 #define ANV_HAS_SURFACE (VK_USE_PLATFORM_WAYLAND_KHR || \\
126 VK_USE_PLATFORM_XCB_KHR || \\
127 VK_USE_PLATFORM_XLIB_KHR)
128
129 const VkExtensionProperties anv_instance_extensions[ANV_INSTANCE_EXTENSION_COUNT] = {
130 %for ext in instance_extensions:
131 {"${ext.name}", ${ext.ext_version}},
132 %endfor
133 };
134
135 bool
136 anv_instance_extension_supported(const char *name)
137 {
138 %for ext in instance_extensions:
139 if (strcmp(name, "${ext.name}") == 0)
140 return ${ext.enable};
141 %endfor
142 return false;
143 }
144
145 VkResult anv_EnumerateInstanceExtensionProperties(
146 const char* pLayerName,
147 uint32_t* pPropertyCount,
148 VkExtensionProperties* pProperties)
149 {
150 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
151
152 %for ext in instance_extensions:
153 if (${ext.enable}) {
154 vk_outarray_append(&out, prop) {
155 *prop = (VkExtensionProperties) {
156 .extensionName = "${ext.name}",
157 .specVersion = ${ext.ext_version},
158 };
159 }
160 }
161 %endfor
162
163 return vk_outarray_status(&out);
164 }
165
166 uint32_t
167 anv_physical_device_api_version(struct anv_physical_device *dev)
168 {
169 return ${MAX_API_VERSION.c_vk_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 bool
179 anv_physical_device_extension_supported(struct anv_physical_device *device,
180 const char *name)
181 {
182 %for ext in device_extensions:
183 if (strcmp(name, "${ext.name}") == 0)
184 return ${ext.enable};
185 %endfor
186 return false;
187 }
188
189 VkResult anv_EnumerateDeviceExtensionProperties(
190 VkPhysicalDevice physicalDevice,
191 const char* pLayerName,
192 uint32_t* pPropertyCount,
193 VkExtensionProperties* pProperties)
194 {
195 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
196 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
197 (void)device;
198
199 %for ext in device_extensions:
200 if (${ext.enable}) {
201 vk_outarray_append(&out, prop) {
202 *prop = (VkExtensionProperties) {
203 .extensionName = "${ext.name}",
204 .specVersion = ${ext.ext_version},
205 };
206 }
207 }
208 %endfor
209
210 return vk_outarray_status(&out);
211 }
212 """)
213
214 if __name__ == '__main__':
215 parser = argparse.ArgumentParser()
216 parser.add_argument('--out-c', help='Output C file.')
217 parser.add_argument('--out-h', help='Output H file.')
218 parser.add_argument('--xml',
219 help='Vulkan API XML file.',
220 required=True,
221 action='append',
222 dest='xml_files')
223 args = parser.parse_args()
224
225 for filename in args.xml_files:
226 _init_exts_from_xml(filename)
227
228 for ext in EXTENSIONS:
229 assert ext.type == 'instance' or ext.type == 'device'
230
231 template_env = {
232 'MAX_API_VERSION': MAX_API_VERSION,
233 'instance_extensions': [e for e in EXTENSIONS if e.type == 'instance'],
234 'device_extensions': [e for e in EXTENSIONS if e.type == 'device'],
235 }
236
237 if args.out_h:
238 with open(args.out_h, 'w') as f:
239 f.write(_TEMPLATE_H.render(**template_env))
240
241 if args.out_c:
242 with open(args.out_c, 'w') as f:
243 f.write(_TEMPLATE_C.render(**template_env))