• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import argparse
2import copy
3import re
4import xml.etree.ElementTree as et
5
6def _bool_to_c_expr(b):
7    if b is True:
8        return 'true'
9    if b is False:
10        return 'false'
11    return b
12
13class Extension:
14    def __init__(self, name, ext_version, enable):
15        self.name = name
16        self.ext_version = int(ext_version)
17        self.enable = _bool_to_c_expr(enable)
18
19    def c_android_condition(self):
20        # if it's an EXT or vendor extension, it's allowed
21        if not self.name.startswith(ANDROID_EXTENSION_WHITELIST_PREFIXES):
22            return 'true'
23
24        allowed_version = ALLOWED_ANDROID_VERSION.get(self.name, None)
25        if allowed_version is None:
26            return 'false'
27
28        return 'ANDROID_API_LEVEL >= %d' % (allowed_version)
29
30class ApiVersion:
31    def __init__(self, version, enable):
32        self.version = version
33        self.enable = _bool_to_c_expr(enable)
34
35class VkVersion:
36    def __init__(self, string):
37        split = string.split('.')
38        self.major = int(split[0])
39        self.minor = int(split[1])
40        if len(split) > 2:
41            assert len(split) == 3
42            self.patch = int(split[2])
43        else:
44            self.patch = None
45
46        # Sanity check.  The range bits are required by the definition of the
47        # VK_MAKE_VERSION macro
48        assert self.major < 1024 and self.minor < 1024
49        assert self.patch is None or self.patch < 4096
50        assert(str(self) == string)
51
52    def __str__(self):
53        ver_list = [str(self.major), str(self.minor)]
54        if self.patch is not None:
55            ver_list.append(str(self.patch))
56        return '.'.join(ver_list)
57
58    def c_vk_version(self):
59        patch = self.patch if self.patch is not None else 0
60        ver_list = [str(self.major), str(self.minor), str(patch)]
61        return 'VK_MAKE_VERSION(' + ', '.join(ver_list) + ')'
62
63    def __int_ver(self):
64        # This is just an expansion of VK_VERSION
65        patch = self.patch if self.patch is not None else 0
66        return (self.major << 22) | (self.minor << 12) | patch
67
68    def __gt__(self, other):
69        # If only one of them has a patch version, "ignore" it by making
70        # other's patch version match self.
71        if (self.patch is None) != (other.patch is None):
72            other = copy.copy(other)
73            other.patch = self.patch
74
75        return self.__int_ver() > other.__int_ver()
76
77# Sort the extension list the way we expect: KHR, then EXT, then vendors
78# alphabetically. For digits, read them as a whole number sort that.
79# eg.: VK_KHR_8bit_storage < VK_KHR_16bit_storage < VK_EXT_acquire_xlib_display
80def extension_order(ext):
81    order = []
82    for substring in re.split('(KHR|EXT|[0-9]+)', ext.name):
83        if substring == 'KHR':
84            order.append(1)
85        if substring == 'EXT':
86            order.append(2)
87        elif substring.isdigit():
88            order.append(int(substring))
89        else:
90            order.append(substring)
91    return order
92
93def get_all_exts_from_xml(xml):
94    """ Get a list of all Vulkan extensions. """
95
96    xml = et.parse(xml)
97
98    extensions = []
99    for ext_elem in xml.findall('.extensions/extension'):
100        supported = ext_elem.attrib['supported'] == 'vulkan'
101        name = ext_elem.attrib['name']
102        if not supported and name != 'VK_ANDROID_native_buffer':
103            continue
104        version = None
105        for enum_elem in ext_elem.findall('.require/enum'):
106            if enum_elem.attrib['name'].endswith('_SPEC_VERSION'):
107                # Skip alias SPEC_VERSIONs
108                if 'value' in enum_elem.attrib:
109                    assert version is None
110                    version = int(enum_elem.attrib['value'])
111        ext = Extension(name, version, True)
112        extensions.append(Extension(name, version, True))
113
114    return sorted(extensions, key=extension_order)
115
116def init_exts_from_xml(xml, extensions, platform_defines):
117    """ Walk the Vulkan XML and fill out extra extension information. """
118
119    xml = et.parse(xml)
120
121    ext_name_map = {}
122    for ext in extensions:
123        ext_name_map[ext.name] = ext
124
125    # KHR_display is missing from the list.
126    platform_defines.append('VK_USE_PLATFORM_DISPLAY_KHR')
127    for platform in xml.findall('./platforms/platform'):
128        platform_defines.append(platform.attrib['protect'])
129
130    for ext_elem in xml.findall('.extensions/extension'):
131        ext_name = ext_elem.attrib['name']
132        if ext_name not in ext_name_map:
133            continue
134
135        ext = ext_name_map[ext_name]
136        ext.type = ext_elem.attrib['type']
137
138# Mapping between extension name and the android version in which the extension
139# was whitelisted in Android CTS.
140ALLOWED_ANDROID_VERSION = {
141    # Allowed Instance KHR Extensions
142    "VK_KHR_surface": 26,
143    "VK_KHR_display": 26,
144    "VK_KHR_android_surface": 26,
145    "VK_KHR_mir_surface": 26,
146    "VK_KHR_wayland_surface": 26,
147    "VK_KHR_win32_surface": 26,
148    "VK_KHR_xcb_surface": 26,
149    "VK_KHR_xlib_surface": 26,
150    "VK_KHR_get_physical_device_properties2": 26,
151    "VK_KHR_get_surface_capabilities2": 26,
152    "VK_KHR_external_memory_capabilities": 28,
153    "VK_KHR_external_semaphore_capabilities": 28,
154    "VK_KHR_external_fence_capabilities": 28,
155    "VK_KHR_device_group_creation": 28,
156    "VK_KHR_get_display_properties2": 29,
157    "VK_KHR_surface_protected_capabilities": 29,
158
159    # Allowed Device KHR Extensions
160    "VK_KHR_swapchain": 26,
161    "VK_KHR_display_swapchain": 26,
162    "VK_KHR_sampler_mirror_clamp_to_edge": 26,
163    "VK_KHR_shader_draw_parameters": 26,
164    "VK_KHR_shader_float_controls": 29,
165    "VK_KHR_shader_float16_int8": 29,
166    "VK_KHR_maintenance1": 26,
167    "VK_KHR_push_descriptor": 26,
168    "VK_KHR_descriptor_update_template": 26,
169    "VK_KHR_incremental_present": 26,
170    "VK_KHR_shared_presentable_image": 26,
171    "VK_KHR_storage_buffer_storage_class": 28,
172    "VK_KHR_8bit_storage": 29,
173    "VK_KHR_16bit_storage": 28,
174    "VK_KHR_get_memory_requirements2": 28,
175    "VK_KHR_external_memory": 28,
176    "VK_KHR_external_memory_fd": 28,
177    "VK_KHR_external_memory_win32": 28,
178    "VK_KHR_external_semaphore": 28,
179    "VK_KHR_external_semaphore_fd": 28,
180    "VK_KHR_external_semaphore_win32": 28,
181    "VK_KHR_external_fence": 28,
182    "VK_KHR_external_fence_fd": 28,
183    "VK_KHR_external_fence_win32": 28,
184    "VK_KHR_win32_keyed_mutex": 28,
185    "VK_KHR_dedicated_allocation": 28,
186    "VK_KHR_variable_pointers": 28,
187    "VK_KHR_relaxed_block_layout": 28,
188    "VK_KHR_bind_memory2": 28,
189    "VK_KHR_maintenance2": 28,
190    "VK_KHR_image_format_list": 28,
191    "VK_KHR_sampler_ycbcr_conversion": 28,
192    "VK_KHR_device_group": 28,
193    "VK_KHR_multiview": 28,
194    "VK_KHR_maintenance3": 28,
195    "VK_KHR_draw_indirect_count": 28,
196    "VK_KHR_create_renderpass2": 28,
197    "VK_KHR_depth_stencil_resolve": 29,
198    "VK_KHR_driver_properties": 28,
199    "VK_KHR_swapchain_mutable_format": 29,
200    "VK_KHR_shader_atomic_int64": 29,
201    "VK_KHR_vulkan_memory_model": 29,
202    "VK_KHR_performance_query": 30,
203
204    "VK_GOOGLE_display_timing": 26,
205    "VK_ANDROID_native_buffer": 26,
206    "VK_ANDROID_external_memory_android_hardware_buffer": 28,
207}
208
209# Extensions with these prefixes are checked in Android CTS, and thus must be
210# whitelisted per the preceding dict.
211ANDROID_EXTENSION_WHITELIST_PREFIXES = (
212    "VK_KHX",
213    "VK_KHR",
214    "VK_GOOGLE",
215    "VK_ANDROID"
216)
217