9bcb631b1243830f85bd38941479ad7a8f250452
[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 extern const struct anv_instance_extension_table anv_instance_extensions_supported;
84
85
86 #define ANV_DEVICE_EXTENSION_COUNT ${len(device_extensions)}
87
88 extern const VkExtensionProperties anv_device_extensions[];
89
90 struct anv_device_extension_table {
91 union {
92 bool extensions[ANV_DEVICE_EXTENSION_COUNT];
93 struct {
94 %for ext in device_extensions:
95 bool ${ext.name[3:]};
96 %endfor
97 };
98 };
99 };
100
101 struct anv_physical_device;
102
103 void
104 anv_physical_device_get_supported_extensions(const struct anv_physical_device *device,
105 struct anv_device_extension_table *extensions);
106
107 #endif /* ANV_EXTENSIONS_H */
108 """)
109
110 _TEMPLATE_C = Template(COPYRIGHT + """
111 #include "anv_private.h"
112
113 #include "vk_util.h"
114
115 /* Convert the VK_USE_PLATFORM_* defines to booleans */
116 %for platform in ['ANDROID', 'WAYLAND', 'XCB', 'XLIB']:
117 #ifdef VK_USE_PLATFORM_${platform}_KHR
118 # undef VK_USE_PLATFORM_${platform}_KHR
119 # define VK_USE_PLATFORM_${platform}_KHR true
120 #else
121 # define VK_USE_PLATFORM_${platform}_KHR false
122 #endif
123 %endfor
124
125 /* And ANDROID too */
126 #ifdef ANDROID
127 # undef ANDROID
128 # define ANDROID true
129 #else
130 # define ANDROID false
131 #endif
132
133 #define ANV_HAS_SURFACE (VK_USE_PLATFORM_WAYLAND_KHR || \\
134 VK_USE_PLATFORM_XCB_KHR || \\
135 VK_USE_PLATFORM_XLIB_KHR)
136
137 static const uint32_t MAX_API_VERSION = ${MAX_API_VERSION.c_vk_version()};
138
139 VkResult anv_EnumerateInstanceVersion(
140 uint32_t* pApiVersion)
141 {
142 *pApiVersion = MAX_API_VERSION;
143 return VK_SUCCESS;
144 }
145
146 const VkExtensionProperties anv_instance_extensions[ANV_INSTANCE_EXTENSION_COUNT] = {
147 %for ext in instance_extensions:
148 {"${ext.name}", ${ext.ext_version}},
149 %endfor
150 };
151
152 const struct anv_instance_extension_table anv_instance_extensions_supported = {
153 %for ext in instance_extensions:
154 .${ext.name[3:]} = ${ext.enable},
155 %endfor
156 };
157
158 uint32_t
159 anv_physical_device_api_version(struct anv_physical_device *device)
160 {
161 uint32_t version = 0;
162
163 uint32_t override = vk_get_version_override();
164 if (override)
165 return MIN2(override, MAX_API_VERSION);
166
167 %for version in API_VERSIONS:
168 if (!(${version.enable}))
169 return version;
170 version = ${version.max_patch_version.c_vk_version()};
171
172 %endfor
173 return version;
174 }
175
176 const VkExtensionProperties anv_device_extensions[ANV_DEVICE_EXTENSION_COUNT] = {
177 %for ext in device_extensions:
178 {"${ext.name}", ${ext.ext_version}},
179 %endfor
180 };
181
182 void
183 anv_physical_device_get_supported_extensions(const struct anv_physical_device *device,
184 struct anv_device_extension_table *extensions)
185 {
186 *extensions = (struct anv_device_extension_table) {
187 %for ext in device_extensions:
188 .${ext.name[3:]} = ${ext.enable},
189 %endfor
190 };
191 }
192 """)
193
194 if __name__ == '__main__':
195 parser = argparse.ArgumentParser()
196 parser.add_argument('--out-c', help='Output C file.')
197 parser.add_argument('--out-h', help='Output H file.')
198 parser.add_argument('--xml',
199 help='Vulkan API XML file.',
200 required=True,
201 action='append',
202 dest='xml_files')
203 args = parser.parse_args()
204
205 for filename in args.xml_files:
206 _init_exts_from_xml(filename)
207
208 for ext in EXTENSIONS:
209 assert ext.type == 'instance' or ext.type == 'device'
210
211 template_env = {
212 'API_VERSIONS': API_VERSIONS,
213 'MAX_API_VERSION': MAX_API_VERSION,
214 'instance_extensions': [e for e in EXTENSIONS if e.type == 'instance'],
215 'device_extensions': [e for e in EXTENSIONS if e.type == 'device'],
216 }
217
218 if args.out_h:
219 with open(args.out_h, 'w') as f:
220 f.write(_TEMPLATE_H.render(**template_env))
221
222 if args.out_c:
223 with open(args.out_c, 'w') as f:
224 f.write(_TEMPLATE_C.render(**template_env))