anv: Make some bits of anv_extensions module-private
[mesa.git] / src / intel / vulkan / anv_extensions.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 class Extension:
33 def __init__(self, name, ext_version, enable):
34 self.name = name
35 self.ext_version = int(ext_version)
36 if enable is True:
37 self.enable = 'true';
38 elif enable is False:
39 self.enable = 'false';
40 else:
41 self.enable = enable;
42
43 EXTENSIONS = [
44 Extension('VK_KHR_dedicated_allocation', 1, True),
45 Extension('VK_KHR_descriptor_update_template', 1, True),
46 Extension('VK_KHR_external_memory', 1, True),
47 Extension('VK_KHR_external_memory_capabilities', 1, True),
48 Extension('VK_KHR_external_memory_fd', 1, True),
49 Extension('VK_KHR_get_memory_requirements2', 1, True),
50 Extension('VK_KHR_get_physical_device_properties2', 1, True),
51 Extension('VK_KHR_get_surface_capabilities2', 1, True),
52 Extension('VK_KHR_incremental_present', 1, True),
53 Extension('VK_KHR_maintenance1', 1, True),
54 Extension('VK_KHR_push_descriptor', 1, True),
55 Extension('VK_KHR_sampler_mirror_clamp_to_edge', 1, True),
56 Extension('VK_KHR_shader_draw_parameters', 1, True),
57 Extension('VK_KHR_storage_buffer_storage_class', 1, True),
58 Extension('VK_KHR_surface', 25, True),
59 Extension('VK_KHR_swapchain', 68, True),
60 Extension('VK_KHR_variable_pointers', 1, True),
61 Extension('VK_KHR_wayland_surface', 6, 'VK_USE_PLATFORM_WAYLAND_KHR'),
62 Extension('VK_KHR_xcb_surface', 6, 'VK_USE_PLATFORM_XCB_KHR'),
63 Extension('VK_KHR_xlib_surface', 6, 'VK_USE_PLATFORM_XLIB_KHR'),
64 Extension('VK_KHX_multiview', 1, True),
65 ]
66
67 def _init_exts_from_xml(xml):
68 """ Walk the Vulkan XML and fill out extra extension information. """
69
70 xml = et.parse(xml)
71
72 ext_name_map = {}
73 for ext in EXTENSIONS:
74 ext_name_map[ext.name] = ext
75
76 for ext_elem in xml.findall('.extensions/extension'):
77 ext_name = ext_elem.attrib['name']
78 if ext_name not in ext_name_map:
79 continue
80 ext = ext_name_map[ext_name]
81
82 ext.type = ext_elem.attrib['type']
83
84 for ext in EXTENSIONS:
85 assert ext.type == 'instance' or ext.type == 'device'
86
87 _TEMPLATE = Template(COPYRIGHT + """
88 #include "anv_private.h"
89
90 #include "vk_util.h"
91
92 /* Convert the VK_USE_PLATFORM_* defines to booleans */
93 %for platform in ['ANDROID', 'WAYLAND', 'XCB', 'XLIB']:
94 #ifdef VK_USE_PLATFORM_${platform}_KHR
95 # undef VK_USE_PLATFORM_${platform}_KHR
96 # define VK_USE_PLATFORM_${platform}_KHR true
97 #else
98 # define VK_USE_PLATFORM_${platform}_KHR false
99 #endif
100 %endfor
101
102 bool
103 anv_instance_extension_supported(const char *name)
104 {
105 %for ext in instance_extensions:
106 if (strcmp(name, "${ext.name}") == 0)
107 return ${ext.enable};
108 %endfor
109 return false;
110 }
111
112 VkResult anv_EnumerateInstanceExtensionProperties(
113 const char* pLayerName,
114 uint32_t* pPropertyCount,
115 VkExtensionProperties* pProperties)
116 {
117 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
118
119 %for ext in instance_extensions:
120 if (${ext.enable}) {
121 vk_outarray_append(&out, prop) {
122 *prop = (VkExtensionProperties) {
123 .extensionName = "${ext.name}",
124 .specVersion = ${ext.ext_version},
125 };
126 }
127 }
128 %endfor
129
130 return vk_outarray_status(&out);
131 }
132
133 bool
134 anv_physical_device_extension_supported(struct anv_physical_device *device,
135 const char *name)
136 {
137 %for ext in device_extensions:
138 if (strcmp(name, "${ext.name}") == 0)
139 return ${ext.enable};
140 %endfor
141 return false;
142 }
143
144 VkResult anv_EnumerateDeviceExtensionProperties(
145 VkPhysicalDevice physicalDevice,
146 const char* pLayerName,
147 uint32_t* pPropertyCount,
148 VkExtensionProperties* pProperties)
149 {
150 ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
151 VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
152 (void)device;
153
154 %for ext in device_extensions:
155 if (${ext.enable}) {
156 vk_outarray_append(&out, prop) {
157 *prop = (VkExtensionProperties) {
158 .extensionName = "${ext.name}",
159 .specVersion = ${ext.ext_version},
160 };
161 }
162 }
163 %endfor
164
165 return vk_outarray_status(&out);
166 }
167 """)
168
169 if __name__ == '__main__':
170 parser = argparse.ArgumentParser()
171 parser.add_argument('--out', help='Output C file.', required=True)
172 parser.add_argument('--xml', help='Vulkan API XML file.', required=True)
173 args = parser.parse_args()
174
175 _init_exts_from_xml(args.xml)
176
177 template_env = {
178 'instance_extensions': [e for e in EXTENSIONS if e.type == 'instance'],
179 'device_extensions': [e for e in EXTENSIONS if e.type == 'device'],
180 }
181
182 with open(args.out, 'w') as f:
183 f.write(_TEMPLATE.render(**template_env))