1bf215546Sopenharmony_ciCOPYRIGHT = """\
2bf215546Sopenharmony_ci/*
3bf215546Sopenharmony_ci * Copyright 2017 Intel Corporation
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the
7bf215546Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
8bf215546Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
9bf215546Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
10bf215546Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
11bf215546Sopenharmony_ci * the following conditions:
12bf215546Sopenharmony_ci *
13bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the
14bf215546Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
15bf215546Sopenharmony_ci * of the Software.
16bf215546Sopenharmony_ci *
17bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20bf215546Sopenharmony_ci * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21bf215546Sopenharmony_ci * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22bf215546Sopenharmony_ci * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23bf215546Sopenharmony_ci * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24bf215546Sopenharmony_ci */
25bf215546Sopenharmony_ci"""
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ciimport argparse
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_cifrom mako.template import Template
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci# Mesa-local imports must be declared in meson variable
32bf215546Sopenharmony_ci# '{file_without_suffix}_depend_files'.
33bf215546Sopenharmony_cifrom vk_extensions import get_all_exts_from_xml, init_exts_from_xml
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci_TEMPLATE_H = Template(COPYRIGHT + """
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#ifndef ${driver.upper()}_EXTENSIONS_H
38bf215546Sopenharmony_ci#define ${driver.upper()}_EXTENSIONS_H
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci#include <stdbool.h>
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci%if driver == 'vk':
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci<%def name="extension_table(type, extensions)">
45bf215546Sopenharmony_ci#define VK_${type.upper()}_EXTENSION_COUNT ${len(extensions)}
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ciextern const VkExtensionProperties vk_${type}_extensions[];
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_cistruct vk_${type}_extension_table {
50bf215546Sopenharmony_ci   union {
51bf215546Sopenharmony_ci      bool extensions[VK_${type.upper()}_EXTENSION_COUNT];
52bf215546Sopenharmony_ci      struct {
53bf215546Sopenharmony_ci%for ext in extensions:
54bf215546Sopenharmony_ci         bool ${ext.name[3:]};
55bf215546Sopenharmony_ci%endfor
56bf215546Sopenharmony_ci      };
57bf215546Sopenharmony_ci   };
58bf215546Sopenharmony_ci};
59bf215546Sopenharmony_ci</%def>
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci${extension_table('instance', instance_extensions)}
62bf215546Sopenharmony_ci${extension_table('device', device_extensions)}
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci%else:
65bf215546Sopenharmony_ci#include "vk_extensions.h"
66bf215546Sopenharmony_ci%endif
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_cistruct ${driver}_physical_device;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci%if driver == 'vk':
71bf215546Sopenharmony_ci#ifdef ANDROID
72bf215546Sopenharmony_ciextern const struct vk_instance_extension_table vk_android_allowed_instance_extensions;
73bf215546Sopenharmony_ciextern const struct vk_device_extension_table vk_android_allowed_device_extensions;
74bf215546Sopenharmony_ci#endif
75bf215546Sopenharmony_ci%else:
76bf215546Sopenharmony_ciextern const struct vk_instance_extension_table ${driver}_instance_extensions_supported;
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_civoid
79bf215546Sopenharmony_ci${driver}_physical_device_get_supported_extensions(const struct ${driver}_physical_device *device,
80bf215546Sopenharmony_ci                                             struct vk_device_extension_table *extensions);
81bf215546Sopenharmony_ci%endif
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci#endif /* ${driver.upper()}_EXTENSIONS_H */
84bf215546Sopenharmony_ci""")
85bf215546Sopenharmony_ci
86bf215546Sopenharmony_ci_TEMPLATE_C = Template(COPYRIGHT + """
87bf215546Sopenharmony_ci#include "vulkan/vulkan_core.h"
88bf215546Sopenharmony_ci%if driver != 'vk':
89bf215546Sopenharmony_ci#include "${driver}_private.h"
90bf215546Sopenharmony_ci%endif
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci#include "${driver}_extensions.h"
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci%if driver == 'vk':
95bf215546Sopenharmony_ciconst VkExtensionProperties ${driver}_instance_extensions[${driver.upper()}_INSTANCE_EXTENSION_COUNT] = {
96bf215546Sopenharmony_ci%for ext in instance_extensions:
97bf215546Sopenharmony_ci   {"${ext.name}", ${ext.ext_version}},
98bf215546Sopenharmony_ci%endfor
99bf215546Sopenharmony_ci};
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ciconst VkExtensionProperties ${driver}_device_extensions[${driver.upper()}_DEVICE_EXTENSION_COUNT] = {
102bf215546Sopenharmony_ci%for ext in device_extensions:
103bf215546Sopenharmony_ci   {"${ext.name}", ${ext.ext_version}},
104bf215546Sopenharmony_ci%endfor
105bf215546Sopenharmony_ci};
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci#ifdef ANDROID
108bf215546Sopenharmony_ciconst struct vk_instance_extension_table vk_android_allowed_instance_extensions = {
109bf215546Sopenharmony_ci%for ext in instance_extensions:
110bf215546Sopenharmony_ci   .${ext.name[3:]} = ${ext.c_android_condition()},
111bf215546Sopenharmony_ci%endfor
112bf215546Sopenharmony_ci};
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ciconst struct vk_device_extension_table vk_android_allowed_device_extensions = {
115bf215546Sopenharmony_ci%for ext in device_extensions:
116bf215546Sopenharmony_ci   .${ext.name[3:]} = ${ext.c_android_condition()},
117bf215546Sopenharmony_ci%endfor
118bf215546Sopenharmony_ci};
119bf215546Sopenharmony_ci#endif
120bf215546Sopenharmony_ci%endif
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci%if driver != 'vk':
123bf215546Sopenharmony_ci#include "vk_util.h"
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci/* Convert the VK_USE_PLATFORM_* defines to booleans */
126bf215546Sopenharmony_ci%for platform_define in platform_defines:
127bf215546Sopenharmony_ci#ifdef ${platform_define}
128bf215546Sopenharmony_ci#   undef ${platform_define}
129bf215546Sopenharmony_ci#   define ${platform_define} true
130bf215546Sopenharmony_ci#else
131bf215546Sopenharmony_ci#   define ${platform_define} false
132bf215546Sopenharmony_ci#endif
133bf215546Sopenharmony_ci%endfor
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci/* And ANDROID too */
136bf215546Sopenharmony_ci#ifdef ANDROID
137bf215546Sopenharmony_ci#   undef ANDROID
138bf215546Sopenharmony_ci#   define ANDROID true
139bf215546Sopenharmony_ci#else
140bf215546Sopenharmony_ci#   define ANDROID false
141bf215546Sopenharmony_ci#   define ANDROID_API_LEVEL 0
142bf215546Sopenharmony_ci#endif
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci#define ${driver.upper()}_HAS_SURFACE (VK_USE_PLATFORM_WIN32_KHR || \\
145bf215546Sopenharmony_ci                                       VK_USE_PLATFORM_WAYLAND_KHR || \\
146bf215546Sopenharmony_ci                                       VK_USE_PLATFORM_XCB_KHR || \\
147bf215546Sopenharmony_ci                                       VK_USE_PLATFORM_XLIB_KHR || \\
148bf215546Sopenharmony_ci                                       VK_USE_PLATFORM_DISPLAY_KHR)
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_cistatic const uint32_t MAX_API_VERSION = ${MAX_API_VERSION.c_vk_version()};
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ciVKAPI_ATTR VkResult VKAPI_CALL ${driver}_EnumerateInstanceVersion(
153bf215546Sopenharmony_ci    uint32_t*                                   pApiVersion)
154bf215546Sopenharmony_ci{
155bf215546Sopenharmony_ci    *pApiVersion = MAX_API_VERSION;
156bf215546Sopenharmony_ci    return VK_SUCCESS;
157bf215546Sopenharmony_ci}
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_ciconst struct vk_instance_extension_table ${driver}_instance_extensions_supported = {
160bf215546Sopenharmony_ci%for ext in instance_extensions:
161bf215546Sopenharmony_ci   .${ext.name[3:]} = ${ext.enable},
162bf215546Sopenharmony_ci%endfor
163bf215546Sopenharmony_ci};
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ciuint32_t
166bf215546Sopenharmony_ci${driver}_physical_device_api_version(struct ${driver}_physical_device *device)
167bf215546Sopenharmony_ci{
168bf215546Sopenharmony_ci    uint32_t version = 0;
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci    uint32_t override = vk_get_version_override();
171bf215546Sopenharmony_ci    if (override)
172bf215546Sopenharmony_ci        return MIN2(override, MAX_API_VERSION);
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci%for version in API_VERSIONS:
175bf215546Sopenharmony_ci    if (!(${version.enable}))
176bf215546Sopenharmony_ci        return version;
177bf215546Sopenharmony_ci    version = ${version.version.c_vk_version()};
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci%endfor
180bf215546Sopenharmony_ci    return version;
181bf215546Sopenharmony_ci}
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_civoid
184bf215546Sopenharmony_ci${driver}_physical_device_get_supported_extensions(const struct ${driver}_physical_device *device,
185bf215546Sopenharmony_ci                                                   struct vk_device_extension_table *extensions)
186bf215546Sopenharmony_ci{
187bf215546Sopenharmony_ci   *extensions = (struct vk_device_extension_table) {
188bf215546Sopenharmony_ci%for ext in device_extensions:
189bf215546Sopenharmony_ci      .${ext.name[3:]} = ${ext.enable},
190bf215546Sopenharmony_ci%endfor
191bf215546Sopenharmony_ci   };
192bf215546Sopenharmony_ci}
193bf215546Sopenharmony_ci%endif
194bf215546Sopenharmony_ci""")
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_cidef gen_extensions(driver, xml_files, api_versions, max_api_version,
197bf215546Sopenharmony_ci                   extensions, out_c, out_h):
198bf215546Sopenharmony_ci    platform_defines = []
199bf215546Sopenharmony_ci    for filename in xml_files:
200bf215546Sopenharmony_ci        init_exts_from_xml(filename, extensions, platform_defines)
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci    for ext in extensions:
203bf215546Sopenharmony_ci        assert ext.type in {'instance', 'device'}
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci    template_env = {
206bf215546Sopenharmony_ci        'driver': driver,
207bf215546Sopenharmony_ci        'API_VERSIONS': api_versions,
208bf215546Sopenharmony_ci        'MAX_API_VERSION': max_api_version,
209bf215546Sopenharmony_ci        'instance_extensions': [e for e in extensions if e.type == 'instance'],
210bf215546Sopenharmony_ci        'device_extensions': [e for e in extensions if e.type == 'device'],
211bf215546Sopenharmony_ci        'platform_defines': platform_defines,
212bf215546Sopenharmony_ci    }
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci    if out_h:
215bf215546Sopenharmony_ci        with open(out_h, 'w') as f:
216bf215546Sopenharmony_ci            f.write(_TEMPLATE_H.render(**template_env))
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci    if out_c:
219bf215546Sopenharmony_ci        with open(out_c, 'w') as f:
220bf215546Sopenharmony_ci            f.write(_TEMPLATE_C.render(**template_env))
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_cidef main():
224bf215546Sopenharmony_ci    parser = argparse.ArgumentParser()
225bf215546Sopenharmony_ci    parser.add_argument('--out-c', help='Output C file.')
226bf215546Sopenharmony_ci    parser.add_argument('--out-h', help='Output H file.')
227bf215546Sopenharmony_ci    parser.add_argument('--xml',
228bf215546Sopenharmony_ci                        help='Vulkan API XML file.',
229bf215546Sopenharmony_ci                        required=True,
230bf215546Sopenharmony_ci                        action='append',
231bf215546Sopenharmony_ci                        dest='xml_files')
232bf215546Sopenharmony_ci    args = parser.parse_args()
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci    extensions = []
235bf215546Sopenharmony_ci    for filename in args.xml_files:
236bf215546Sopenharmony_ci        extensions += get_all_exts_from_xml(filename)
237bf215546Sopenharmony_ci
238bf215546Sopenharmony_ci    gen_extensions('vk', args.xml_files, None, None,
239bf215546Sopenharmony_ci                   extensions, args.out_c, args.out_h)
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ciif __name__ == '__main__':
242bf215546Sopenharmony_ci    main()
243