1b8e80941SmrgCOPYRIGHT = """\
2b8e80941Smrg/*
3b8e80941Smrg * Copyright 2017 Intel Corporation
4b8e80941Smrg *
5b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6b8e80941Smrg * copy of this software and associated documentation files (the
7b8e80941Smrg * "Software"), to deal in the Software without restriction, including
8b8e80941Smrg * without limitation the rights to use, copy, modify, merge, publish,
9b8e80941Smrg * distribute, sub license, and/or sell copies of the Software, and to
10b8e80941Smrg * permit persons to whom the Software is furnished to do so, subject to
11b8e80941Smrg * the following conditions:
12b8e80941Smrg *
13b8e80941Smrg * The above copyright notice and this permission notice (including the
14b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions
15b8e80941Smrg * of the Software.
16b8e80941Smrg *
17b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18b8e80941Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19b8e80941Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20b8e80941Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21b8e80941Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22b8e80941Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23b8e80941Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24b8e80941Smrg */
25b8e80941Smrg"""
26b8e80941Smrg
27b8e80941Smrgimport argparse
28b8e80941Smrgimport copy
29b8e80941Smrgimport re
30b8e80941Smrgimport xml.etree.cElementTree as et
31b8e80941Smrg
32b8e80941Smrgfrom mako.template import Template
33b8e80941Smrg
34b8e80941SmrgMAX_API_VERSION = '1.1.82'
35b8e80941Smrg
36b8e80941Smrgclass Extension:
37b8e80941Smrg    def __init__(self, name, ext_version, enable):
38b8e80941Smrg        self.name = name
39b8e80941Smrg        self.ext_version = int(ext_version)
40b8e80941Smrg        if enable is True:
41b8e80941Smrg            self.enable = 'true';
42b8e80941Smrg        elif enable is False:
43b8e80941Smrg            self.enable = 'false';
44b8e80941Smrg        else:
45b8e80941Smrg            self.enable = enable;
46b8e80941Smrg
47b8e80941Smrg# On Android, we disable all surface and swapchain extensions. Android's Vulkan
48b8e80941Smrg# loader implements VK_KHR_surface and VK_KHR_swapchain, and applications
49b8e80941Smrg# cannot access the driver's implementation. Moreoever, if the driver exposes
50b8e80941Smrg# the those extension strings, then tests dEQP-VK.api.info.instance.extensions
51b8e80941Smrg# and dEQP-VK.api.info.device fail due to the duplicated strings.
52b8e80941SmrgEXTENSIONS = [
53b8e80941Smrg    Extension('VK_KHR_bind_memory2',                      1, True),
54b8e80941Smrg    Extension('VK_KHR_create_renderpass2',                1, True),
55b8e80941Smrg    Extension('VK_KHR_dedicated_allocation',              1, True),
56b8e80941Smrg    Extension('VK_KHR_get_display_properties2',           1, 'VK_USE_PLATFORM_DISPLAY_KHR'),
57b8e80941Smrg    Extension('VK_KHR_get_memory_requirements2',          1, True),
58b8e80941Smrg    Extension('VK_KHR_get_physical_device_properties2',   1, True),
59b8e80941Smrg    Extension('VK_KHR_get_surface_capabilities2',         1, 'TU_HAS_SURFACE'),
60b8e80941Smrg    Extension('VK_KHR_maintenance1',                      1, True),
61b8e80941Smrg    Extension('VK_KHR_maintenance2',                      1, True),
62b8e80941Smrg    Extension('VK_KHR_maintenance3',                      1, True),
63b8e80941Smrg    Extension('VK_KHR_surface',                          25, 'TU_HAS_SURFACE'),
64b8e80941Smrg    Extension('VK_KHR_swapchain',                        68, 'TU_HAS_SURFACE'),
65b8e80941Smrg    Extension('VK_KHR_wayland_surface',                   6, 'VK_USE_PLATFORM_WAYLAND_KHR'),
66b8e80941Smrg    Extension('VK_KHR_xcb_surface',                       6, 'VK_USE_PLATFORM_XCB_KHR'),
67b8e80941Smrg    Extension('VK_KHR_xlib_surface',                      6, 'VK_USE_PLATFORM_XLIB_KHR'),
68b8e80941Smrg    Extension('VK_KHR_display',                          23, 'VK_USE_PLATFORM_DISPLAY_KHR'),
69b8e80941Smrg    Extension('VK_EXT_direct_mode_display',               1, 'VK_USE_PLATFORM_DISPLAY_KHR'),
70b8e80941Smrg    Extension('VK_EXT_acquire_xlib_display',              1, 'VK_USE_PLATFORM_XLIB_XRANDR_EXT'),
71b8e80941Smrg    Extension('VK_EXT_display_surface_counter',           1, 'VK_USE_PLATFORM_DISPLAY_KHR'),
72b8e80941Smrg    Extension('VK_EXT_display_control',                   1, 'VK_USE_PLATFORM_DISPLAY_KHR'),
73b8e80941Smrg    Extension('VK_EXT_debug_report',                      9, True),
74b8e80941Smrg    Extension('VK_KHR_external_memory_capabilities',      1, True),
75b8e80941Smrg    Extension('VK_KHR_external_memory',                   1, True),
76b8e80941Smrg    Extension('VK_KHR_external_memory_fd',                1, True),
77b8e80941Smrg    Extension('VK_EXT_external_memory_dma_buf',           1, True),
78b8e80941Smrg]
79b8e80941Smrg
80b8e80941Smrgclass VkVersion:
81b8e80941Smrg    def __init__(self, string):
82b8e80941Smrg        split = string.split('.')
83b8e80941Smrg        self.major = int(split[0])
84b8e80941Smrg        self.minor = int(split[1])
85b8e80941Smrg        if len(split) > 2:
86b8e80941Smrg            assert len(split) == 3
87b8e80941Smrg            self.patch = int(split[2])
88b8e80941Smrg        else:
89b8e80941Smrg            self.patch = None
90b8e80941Smrg
91b8e80941Smrg        # Sanity check.  The range bits are required by the definition of the
92b8e80941Smrg        # VK_MAKE_VERSION macro
93b8e80941Smrg        assert self.major < 1024 and self.minor < 1024
94b8e80941Smrg        assert self.patch is None or self.patch < 4096
95b8e80941Smrg        assert(str(self) == string)
96b8e80941Smrg
97b8e80941Smrg    def __str__(self):
98b8e80941Smrg        ver_list = [str(self.major), str(self.minor)]
99b8e80941Smrg        if self.patch is not None:
100b8e80941Smrg            ver_list.append(str(self.patch))
101b8e80941Smrg        return '.'.join(ver_list)
102b8e80941Smrg
103b8e80941Smrg    def c_vk_version(self):
104b8e80941Smrg        patch = self.patch if self.patch is not None else 0
105b8e80941Smrg        ver_list = [str(self.major), str(self.minor), str(patch)]
106b8e80941Smrg        return 'VK_MAKE_VERSION(' + ', '.join(ver_list) + ')'
107b8e80941Smrg
108b8e80941Smrg    def __int_ver(self):
109b8e80941Smrg        # This is just an expansion of VK_VERSION
110b8e80941Smrg        patch = self.patch if self.patch is not None else 0
111b8e80941Smrg        return (self.major << 22) | (self.minor << 12) | patch
112b8e80941Smrg
113b8e80941Smrg    def __gt__(self, other):
114b8e80941Smrg        # If only one of them has a patch version, "ignore" it by making
115b8e80941Smrg        # other's patch version match self.
116b8e80941Smrg        if (self.patch is None) != (other.patch is None):
117b8e80941Smrg            other = copy.copy(other)
118b8e80941Smrg            other.patch = self.patch
119b8e80941Smrg
120b8e80941Smrg        return self.__int_ver() > other.__int_ver()
121b8e80941Smrg
122b8e80941SmrgMAX_API_VERSION = VkVersion(MAX_API_VERSION)
123b8e80941Smrg
124b8e80941Smrgdef _init_exts_from_xml(xml):
125b8e80941Smrg    """ Walk the Vulkan XML and fill out extra extension information. """
126b8e80941Smrg
127b8e80941Smrg    xml = et.parse(xml)
128b8e80941Smrg
129b8e80941Smrg    ext_name_map = {}
130b8e80941Smrg    for ext in EXTENSIONS:
131b8e80941Smrg        ext_name_map[ext.name] = ext
132b8e80941Smrg
133b8e80941Smrg    for ext_elem in xml.findall('.extensions/extension'):
134b8e80941Smrg        ext_name = ext_elem.attrib['name']
135b8e80941Smrg        if ext_name not in ext_name_map:
136b8e80941Smrg            continue
137b8e80941Smrg
138b8e80941Smrg        ext = ext_name_map[ext_name]
139b8e80941Smrg        ext.type = ext_elem.attrib['type']
140b8e80941Smrg
141b8e80941Smrg_TEMPLATE_H = Template(COPYRIGHT + """
142b8e80941Smrg#ifndef TU_EXTENSIONS_H
143b8e80941Smrg#define TU_EXTENSIONS_H
144b8e80941Smrg
145b8e80941Smrgenum {
146b8e80941Smrg   TU_INSTANCE_EXTENSION_COUNT = ${len(instance_extensions)},
147b8e80941Smrg   TU_DEVICE_EXTENSION_COUNT = ${len(device_extensions)},
148b8e80941Smrg};
149b8e80941Smrg
150b8e80941Smrgstruct tu_instance_extension_table {
151b8e80941Smrg   union {
152b8e80941Smrg      bool extensions[TU_INSTANCE_EXTENSION_COUNT];
153b8e80941Smrg      struct {
154b8e80941Smrg%for ext in instance_extensions:
155b8e80941Smrg        bool ${ext.name[3:]};
156b8e80941Smrg%endfor
157b8e80941Smrg      };
158b8e80941Smrg   };
159b8e80941Smrg};
160b8e80941Smrg
161b8e80941Smrgstruct tu_device_extension_table {
162b8e80941Smrg   union {
163b8e80941Smrg      bool extensions[TU_DEVICE_EXTENSION_COUNT];
164b8e80941Smrg      struct {
165b8e80941Smrg%for ext in device_extensions:
166b8e80941Smrg        bool ${ext.name[3:]};
167b8e80941Smrg%endfor
168b8e80941Smrg      };
169b8e80941Smrg   };
170b8e80941Smrg};
171b8e80941Smrg
172b8e80941Smrgextern const VkExtensionProperties tu_instance_extensions[TU_INSTANCE_EXTENSION_COUNT];
173b8e80941Smrgextern const VkExtensionProperties tu_device_extensions[TU_DEVICE_EXTENSION_COUNT];
174b8e80941Smrgextern const struct tu_instance_extension_table tu_supported_instance_extensions;
175b8e80941Smrg
176b8e80941Smrg
177b8e80941Smrgstruct tu_physical_device;
178b8e80941Smrg
179b8e80941Smrgvoid tu_fill_device_extension_table(const struct tu_physical_device *device,
180b8e80941Smrg                                      struct tu_device_extension_table* table);
181b8e80941Smrg#endif
182b8e80941Smrg""")
183b8e80941Smrg
184b8e80941Smrg_TEMPLATE_C = Template(COPYRIGHT + """
185b8e80941Smrg#include "tu_private.h"
186b8e80941Smrg
187b8e80941Smrg#include "vk_util.h"
188b8e80941Smrg
189b8e80941Smrg/* Convert the VK_USE_PLATFORM_* defines to booleans */
190b8e80941Smrg%for platform in ['ANDROID_KHR', 'WAYLAND_KHR', 'XCB_KHR', 'XLIB_KHR', 'DISPLAY_KHR', 'XLIB_XRANDR_EXT']:
191b8e80941Smrg#ifdef VK_USE_PLATFORM_${platform}
192b8e80941Smrg#   undef VK_USE_PLATFORM_${platform}
193b8e80941Smrg#   define VK_USE_PLATFORM_${platform} true
194b8e80941Smrg#else
195b8e80941Smrg#   define VK_USE_PLATFORM_${platform} false
196b8e80941Smrg#endif
197b8e80941Smrg%endfor
198b8e80941Smrg
199b8e80941Smrg/* And ANDROID too */
200b8e80941Smrg#ifdef ANDROID
201b8e80941Smrg#   undef ANDROID
202b8e80941Smrg#   define ANDROID true
203b8e80941Smrg#else
204b8e80941Smrg#   define ANDROID false
205b8e80941Smrg#endif
206b8e80941Smrg
207b8e80941Smrg#define TU_HAS_SURFACE (VK_USE_PLATFORM_WAYLAND_KHR || \\
208b8e80941Smrg                         VK_USE_PLATFORM_XCB_KHR || \\
209b8e80941Smrg                         VK_USE_PLATFORM_XLIB_KHR || \\
210b8e80941Smrg                         VK_USE_PLATFORM_DISPLAY_KHR)
211b8e80941Smrg
212b8e80941Smrg
213b8e80941Smrgconst VkExtensionProperties tu_instance_extensions[TU_INSTANCE_EXTENSION_COUNT] = {
214b8e80941Smrg%for ext in instance_extensions:
215b8e80941Smrg   {"${ext.name}", ${ext.ext_version}},
216b8e80941Smrg%endfor
217b8e80941Smrg};
218b8e80941Smrg
219b8e80941Smrgconst VkExtensionProperties tu_device_extensions[TU_DEVICE_EXTENSION_COUNT] = {
220b8e80941Smrg%for ext in device_extensions:
221b8e80941Smrg   {"${ext.name}", ${ext.ext_version}},
222b8e80941Smrg%endfor
223b8e80941Smrg};
224b8e80941Smrg
225b8e80941Smrgconst struct tu_instance_extension_table tu_supported_instance_extensions = {
226b8e80941Smrg%for ext in instance_extensions:
227b8e80941Smrg   .${ext.name[3:]} = ${ext.enable},
228b8e80941Smrg%endfor
229b8e80941Smrg};
230b8e80941Smrg
231b8e80941Smrgvoid tu_fill_device_extension_table(const struct tu_physical_device *device,
232b8e80941Smrg                                      struct tu_device_extension_table* table)
233b8e80941Smrg{
234b8e80941Smrg%for ext in device_extensions:
235b8e80941Smrg   table->${ext.name[3:]} = ${ext.enable};
236b8e80941Smrg%endfor
237b8e80941Smrg}
238b8e80941Smrg
239b8e80941SmrgVkResult tu_EnumerateInstanceVersion(
240b8e80941Smrg    uint32_t*                                   pApiVersion)
241b8e80941Smrg{
242b8e80941Smrg    *pApiVersion = ${MAX_API_VERSION.c_vk_version()};
243b8e80941Smrg    return VK_SUCCESS;
244b8e80941Smrg}
245b8e80941Smrg
246b8e80941Smrguint32_t
247b8e80941Smrgtu_physical_device_api_version(struct tu_physical_device *dev)
248b8e80941Smrg{
249b8e80941Smrg    return VK_MAKE_VERSION(1, 1, 82);
250b8e80941Smrg}
251b8e80941Smrg""")
252b8e80941Smrg
253b8e80941Smrgif __name__ == '__main__':
254b8e80941Smrg    parser = argparse.ArgumentParser()
255b8e80941Smrg    parser.add_argument('--out-c', help='Output C file.', required=True)
256b8e80941Smrg    parser.add_argument('--out-h', help='Output H file.', required=True)
257b8e80941Smrg    parser.add_argument('--xml',
258b8e80941Smrg                        help='Vulkan API XML file.',
259b8e80941Smrg                        required=True,
260b8e80941Smrg                        action='append',
261b8e80941Smrg                        dest='xml_files')
262b8e80941Smrg    args = parser.parse_args()
263b8e80941Smrg
264b8e80941Smrg    for filename in args.xml_files:
265b8e80941Smrg        _init_exts_from_xml(filename)
266b8e80941Smrg
267b8e80941Smrg    for ext in EXTENSIONS:
268b8e80941Smrg        assert ext.type == 'instance' or ext.type == 'device'
269b8e80941Smrg
270b8e80941Smrg    template_env = {
271b8e80941Smrg        'MAX_API_VERSION': MAX_API_VERSION,
272b8e80941Smrg        'instance_extensions': [e for e in EXTENSIONS if e.type == 'instance'],
273b8e80941Smrg        'device_extensions': [e for e in EXTENSIONS if e.type == 'device'],
274b8e80941Smrg    }
275b8e80941Smrg
276b8e80941Smrg    with open(args.out_c, 'w') as f:
277b8e80941Smrg        f.write(_TEMPLATE_C.render(**template_env))
278b8e80941Smrg    with open(args.out_h, 'w') as f:
279b8e80941Smrg        f.write(_TEMPLATE_H.render(**template_env))
280