anv: Split anv_extensions.py into two files
[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 = Template(COPYRIGHT + """
62 #include "anv_private.h"
63
64 #include "vk_util.h"
65
66 /* Convert the VK_USE_PLATFORM_* defines to booleans */
67 %for platform in ['ANDROID', 'WAYLAND', 'XCB', 'XLIB']:
68 #ifdef VK_USE_PLATFORM_${platform}_KHR
69 # undef VK_USE_PLATFORM_${platform}_KHR
70 # define VK_USE_PLATFORM_${platform}_KHR true
71 #else
72 # define VK_USE_PLATFORM_${platform}_KHR false
73 #endif
74 %endfor
75
76 /* And ANDROID too */
77 #ifdef ANDROID
78 # undef ANDROID
79 # define ANDROID true
80 #else
81 # define ANDROID false
82 #endif
83
84 #define ANV_HAS_SURFACE (VK_USE_PLATFORM_WAYLAND_KHR || \\
85 VK_USE_PLATFORM_XCB_KHR || \\
86 VK_USE_PLATFORM_XLIB_KHR)
87
88 bool
89 anv_instance_extension_supported(const char *name)
90 {
91 %for ext in instance_extensions:
92 if (strcmp(name, "${ext.name}") == 0)
93 return ${ext.enable};
94 %endfor
95 return false;
96 }
97
98 VkResult anv_EnumerateInstanceExtensionProperties(
99 const char* pLayerName,
100 uint32_t* pPropertyCount,
101 VkExtensionProperties* pProperties)
102 {
103 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
104
105 %for ext in instance_extensions:
106 if (${ext.enable}) {
107 vk_outarray_append(&out, prop) {
108 *prop = (VkExtensionProperties) {
109 .extensionName = "${ext.name}",
110 .specVersion = ${ext.ext_version},
111 };
112 }
113 }
114 %endfor
115
116 return vk_outarray_status(&out);
117 }
118
119 uint32_t
120 anv_physical_device_api_version(struct anv_physical_device *dev)
121 {
122 return ${MAX_API_VERSION.c_vk_version()};
123 }
124
125 bool
126 anv_physical_device_extension_supported(struct anv_physical_device *device,
127 const char *name)
128 {
129 %for ext in device_extensions:
130 if (strcmp(name, "${ext.name}") == 0)
131 return ${ext.enable};
132 %endfor
133 return false;
134 }
135
136 VkResult anv_EnumerateDeviceExtensionProperties(
137 VkPhysicalDevice physicalDevice,
138 const char* pLayerName,
139 uint32_t* pPropertyCount,
140 VkExtensionProperties* pProperties)
141 {
142 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
143 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
144 (void)device;
145
146 %for ext in device_extensions:
147 if (${ext.enable}) {
148 vk_outarray_append(&out, prop) {
149 *prop = (VkExtensionProperties) {
150 .extensionName = "${ext.name}",
151 .specVersion = ${ext.ext_version},
152 };
153 }
154 }
155 %endfor
156
157 return vk_outarray_status(&out);
158 }
159 """)
160
161 if __name__ == '__main__':
162 parser = argparse.ArgumentParser()
163 parser.add_argument('--out', help='Output C file.', required=True)
164 parser.add_argument('--xml',
165 help='Vulkan API XML file.',
166 required=True,
167 action='append',
168 dest='xml_files')
169 args = parser.parse_args()
170
171 for filename in args.xml_files:
172 _init_exts_from_xml(filename)
173
174 for ext in EXTENSIONS:
175 assert ext.type == 'instance' or ext.type == 'device'
176
177 template_env = {
178 'MAX_API_VERSION': MAX_API_VERSION,
179 'instance_extensions': [e for e in EXTENSIONS if e.type == 'instance'],
180 'device_extensions': [e for e in EXTENSIONS if e.type == 'device'],
181 }
182
183 with open(args.out, 'w') as f:
184 f.write(_TEMPLATE.render(**template_env))