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 argparse
28import xml.etree.cElementTree as et
29
30from mako.template import Template
31
32from anv_extensions import *
33
34def _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        ext = ext_name_map[ext_name]
49        ext.type = ext_elem.attrib['type']
50
51_TEMPLATE_H = Template(COPYRIGHT + """
52
53#ifndef ANV_EXTENSIONS_H
54#define ANV_EXTENSIONS_H
55
56#include "stdbool.h"
57
58#define ANV_INSTANCE_EXTENSION_COUNT ${len(instance_extensions)}
59
60extern const VkExtensionProperties anv_instance_extensions[];
61
62struct anv_instance_extension_table {
63   union {
64      bool extensions[ANV_INSTANCE_EXTENSION_COUNT];
65      struct {
66%for ext in instance_extensions:
67         bool ${ext.name[3:]};
68%endfor
69      };
70   };
71};
72
73extern const struct anv_instance_extension_table anv_instance_extensions_supported;
74
75
76#define ANV_DEVICE_EXTENSION_COUNT ${len(device_extensions)}
77
78extern const VkExtensionProperties anv_device_extensions[];
79
80struct anv_device_extension_table {
81   union {
82      bool extensions[ANV_DEVICE_EXTENSION_COUNT];
83      struct {
84%for ext in device_extensions:
85        bool ${ext.name[3:]};
86%endfor
87      };
88   };
89};
90
91struct anv_physical_device;
92
93void
94anv_physical_device_get_supported_extensions(const struct anv_physical_device *device,
95                                             struct anv_device_extension_table *extensions);
96
97#endif /* ANV_EXTENSIONS_H */
98""")
99
100_TEMPLATE_C = Template(COPYRIGHT + """
101#include "anv_private.h"
102
103#include "vk_util.h"
104
105/* Convert the VK_USE_PLATFORM_* defines to booleans */
106%for platform in ['ANDROID_KHR', 'WAYLAND_KHR', 'XCB_KHR', 'XLIB_KHR', 'DISPLAY_KHR', 'XLIB_XRANDR_EXT']:
107#ifdef VK_USE_PLATFORM_${platform}
108#   undef VK_USE_PLATFORM_${platform}
109#   define VK_USE_PLATFORM_${platform} true
110#else
111#   define VK_USE_PLATFORM_${platform} false
112#endif
113%endfor
114
115/* And ANDROID too */
116#ifdef ANDROID
117#   undef ANDROID
118#   define ANDROID true
119#else
120#   define ANDROID false
121#endif
122
123#define ANV_HAS_SURFACE (VK_USE_PLATFORM_WAYLAND_KHR || \\
124                         VK_USE_PLATFORM_XCB_KHR || \\
125                         VK_USE_PLATFORM_XLIB_KHR || \\
126                         VK_USE_PLATFORM_DISPLAY_KHR)
127
128static const uint32_t MAX_API_VERSION = ${MAX_API_VERSION.c_vk_version()};
129
130VkResult anv_EnumerateInstanceVersion(
131    uint32_t*                                   pApiVersion)
132{
133    *pApiVersion = MAX_API_VERSION;
134    return VK_SUCCESS;
135}
136
137const VkExtensionProperties anv_instance_extensions[ANV_INSTANCE_EXTENSION_COUNT] = {
138%for ext in instance_extensions:
139   {"${ext.name}", ${ext.ext_version}},
140%endfor
141};
142
143const struct anv_instance_extension_table anv_instance_extensions_supported = {
144%for ext in instance_extensions:
145   .${ext.name[3:]} = ${ext.enable},
146%endfor
147};
148
149uint32_t
150anv_physical_device_api_version(struct anv_physical_device *device)
151{
152    uint32_t version = 0;
153
154    uint32_t override = vk_get_version_override();
155    if (override)
156        return MIN2(override, MAX_API_VERSION);
157
158%for version in API_VERSIONS:
159    if (!(${version.enable}))
160        return version;
161    version = ${version.version.c_vk_version()};
162
163%endfor
164    return version;
165}
166
167const VkExtensionProperties anv_device_extensions[ANV_DEVICE_EXTENSION_COUNT] = {
168%for ext in device_extensions:
169   {"${ext.name}", ${ext.ext_version}},
170%endfor
171};
172
173void
174anv_physical_device_get_supported_extensions(const struct anv_physical_device *device,
175                                             struct anv_device_extension_table *extensions)
176{
177   *extensions = (struct anv_device_extension_table) {
178%for ext in device_extensions:
179      .${ext.name[3:]} = ${ext.enable},
180%endfor
181   };
182}
183""")
184
185if __name__ == '__main__':
186    parser = argparse.ArgumentParser()
187    parser.add_argument('--out-c', help='Output C file.')
188    parser.add_argument('--out-h', help='Output H file.')
189    parser.add_argument('--xml',
190                        help='Vulkan API XML file.',
191                        required=True,
192                        action='append',
193                        dest='xml_files')
194    args = parser.parse_args()
195
196    for filename in args.xml_files:
197        _init_exts_from_xml(filename)
198
199    for ext in EXTENSIONS:
200        assert ext.type == 'instance' or ext.type == 'device'
201
202    template_env = {
203        'API_VERSIONS': API_VERSIONS,
204        'MAX_API_VERSION': MAX_API_VERSION,
205        'instance_extensions': [e for e in EXTENSIONS if e.type == 'instance'],
206        'device_extensions': [e for e in EXTENSIONS if e.type == 'device'],
207    }
208
209    if args.out_h:
210        with open(args.out_h, 'w') as f:
211            f.write(_TEMPLATE_H.render(**template_env))
212
213    if args.out_c:
214        with open(args.out_c, 'w') as f:
215            f.write(_TEMPLATE_C.render(**template_env))
216