1COPYRIGHT = """\
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
27import xml.etree.ElementTree as et
28
29from mako.template import Template
30
31# Mesa-local imports must be declared in meson variable
32# '{file_without_suffix}_depend_files'.
33from vk_extensions import *
34
35_TEMPLATE_H = Template(COPYRIGHT + """
36
37#ifndef ${driver.upper()}_EXTENSIONS_H
38#define ${driver.upper()}_EXTENSIONS_H
39
40#include <stdbool.h>
41
42%for include in includes:
43#include "${include}"
44%endfor
45
46%if driver == 'vk':
47#define VK_INSTANCE_EXTENSION_COUNT ${len(instance_extensions)}
48
49extern const VkExtensionProperties vk_instance_extensions[];
50
51struct vk_instance_extension_table {
52   union {
53      bool extensions[VK_INSTANCE_EXTENSION_COUNT];
54      struct {
55%for ext in instance_extensions:
56         bool ${ext.name[3:]};
57%endfor
58      };
59   };
60};
61
62
63#define VK_DEVICE_EXTENSION_COUNT ${len(device_extensions)}
64
65extern const VkExtensionProperties vk_device_extensions[];
66
67struct vk_device_extension_table {
68   union {
69      bool extensions[VK_DEVICE_EXTENSION_COUNT];
70      struct {
71%for ext in device_extensions:
72        bool ${ext.name[3:]};
73%endfor
74      };
75   };
76};
77%else:
78#include "vk_extensions.h"
79%endif
80
81struct ${driver}_physical_device;
82
83%if driver == 'vk':
84#ifdef ANDROID
85extern const struct vk_instance_extension_table vk_android_allowed_instance_extensions;
86extern const struct vk_device_extension_table vk_android_allowed_device_extensions;
87#endif
88%else:
89extern const struct vk_instance_extension_table ${driver}_instance_extensions_supported;
90
91void
92${driver}_physical_device_get_supported_extensions(const struct ${driver}_physical_device *device,
93                                             struct vk_device_extension_table *extensions);
94%endif
95
96#endif /* ${driver.upper()}_EXTENSIONS_H */
97""")
98
99_TEMPLATE_C = Template(COPYRIGHT + """
100%if driver == 'vk':
101#include "vk_object.h"
102%else:
103#include "${driver}_private.h"
104%endif
105
106#include "${driver}_extensions.h"
107
108%if driver == 'vk':
109const VkExtensionProperties ${driver}_instance_extensions[${driver.upper()}_INSTANCE_EXTENSION_COUNT] = {
110%for ext in instance_extensions:
111   {"${ext.name}", ${ext.ext_version}},
112%endfor
113};
114
115const VkExtensionProperties ${driver}_device_extensions[${driver.upper()}_DEVICE_EXTENSION_COUNT] = {
116%for ext in device_extensions:
117   {"${ext.name}", ${ext.ext_version}},
118%endfor
119};
120
121#ifdef ANDROID
122const struct vk_instance_extension_table vk_android_allowed_instance_extensions = {
123%for ext in instance_extensions:
124   .${ext.name[3:]} = ${ext.c_android_condition()},
125%endfor
126};
127
128extern const struct vk_device_extension_table vk_android_allowed_device_extensions = {
129%for ext in device_extensions:
130   .${ext.name[3:]} = ${ext.c_android_condition()},
131%endfor
132};
133#endif
134%endif
135
136%if driver != 'vk':
137#include "vk_util.h"
138
139/* Convert the VK_USE_PLATFORM_* defines to booleans */
140%for platform_define in platform_defines:
141#ifdef ${platform_define}
142#   undef ${platform_define}
143#   define ${platform_define} true
144#else
145#   define ${platform_define} false
146#endif
147%endfor
148
149/* And ANDROID too */
150#ifdef ANDROID
151#   undef ANDROID
152#   define ANDROID true
153#else
154#   define ANDROID false
155#   define ANDROID_API_LEVEL 0
156#endif
157
158#define ${driver.upper()}_HAS_SURFACE (VK_USE_PLATFORM_WIN32_KHR || \\
159                                       VK_USE_PLATFORM_WAYLAND_KHR || \\
160                                       VK_USE_PLATFORM_XCB_KHR || \\
161                                       VK_USE_PLATFORM_XLIB_KHR || \\
162                                       VK_USE_PLATFORM_DISPLAY_KHR)
163
164static const uint32_t MAX_API_VERSION = ${MAX_API_VERSION.c_vk_version()};
165
166VKAPI_ATTR VkResult VKAPI_CALL ${driver}_EnumerateInstanceVersion(
167    uint32_t*                                   pApiVersion)
168{
169    *pApiVersion = MAX_API_VERSION;
170    return VK_SUCCESS;
171}
172
173const struct vk_instance_extension_table ${driver}_instance_extensions_supported = {
174%for ext in instance_extensions:
175   .${ext.name[3:]} = ${ext.enable},
176%endfor
177};
178
179uint32_t
180${driver}_physical_device_api_version(struct ${driver}_physical_device *device)
181{
182    uint32_t version = 0;
183
184    uint32_t override = vk_get_version_override();
185    if (override)
186        return MIN2(override, MAX_API_VERSION);
187
188%for version in API_VERSIONS:
189    if (!(${version.enable}))
190        return version;
191    version = ${version.version.c_vk_version()};
192
193%endfor
194    return version;
195}
196
197void
198${driver}_physical_device_get_supported_extensions(const struct ${driver}_physical_device *device,
199                                                   struct vk_device_extension_table *extensions)
200{
201   *extensions = (struct vk_device_extension_table) {
202%for ext in device_extensions:
203      .${ext.name[3:]} = ${ext.enable},
204%endfor
205   };
206}
207%endif
208""")
209
210def gen_extensions(driver, xml_files, api_versions, max_api_version,
211                   extensions, out_c, out_h, includes = []):
212    platform_defines = []
213    for filename in xml_files:
214        init_exts_from_xml(filename, extensions, platform_defines)
215
216    for ext in extensions:
217        assert ext.type == 'instance' or ext.type == 'device'
218
219    template_env = {
220        'driver': driver,
221        'API_VERSIONS': api_versions,
222        'MAX_API_VERSION': max_api_version,
223        'instance_extensions': [e for e in extensions if e.type == 'instance'],
224        'device_extensions': [e for e in extensions if e.type == 'device'],
225        'platform_defines': platform_defines,
226        'includes': includes,
227    }
228
229    if out_h:
230        with open(out_h, 'w') as f:
231            f.write(_TEMPLATE_H.render(**template_env))
232
233    if out_c:
234        with open(out_c, 'w') as f:
235            f.write(_TEMPLATE_C.render(**template_env))
236
237
238if __name__ == '__main__':
239    parser = argparse.ArgumentParser()
240    parser.add_argument('--out-c', help='Output C file.')
241    parser.add_argument('--out-h', help='Output H file.')
242    parser.add_argument('--xml',
243                        help='Vulkan API XML file.',
244                        required=True,
245                        action='append',
246                        dest='xml_files')
247    args = parser.parse_args()
248
249    extensions = []
250    for filename in args.xml_files:
251        extensions += get_all_exts_from_xml(filename)
252
253    gen_extensions('vk', args.xml_files, None, None,
254                   extensions, args.out_c, args.out_h, [])
255