17ec681f3Smrgimport argparse
27ec681f3Smrgimport copy
37ec681f3Smrgimport re
47ec681f3Smrgimport xml.etree.ElementTree as et
57ec681f3Smrg
67ec681f3Smrgdef _bool_to_c_expr(b):
77ec681f3Smrg    if b is True:
87ec681f3Smrg        return 'true'
97ec681f3Smrg    if b is False:
107ec681f3Smrg        return 'false'
117ec681f3Smrg    return b
127ec681f3Smrg
137ec681f3Smrgclass Extension:
147ec681f3Smrg    def __init__(self, name, ext_version, enable):
157ec681f3Smrg        self.name = name
167ec681f3Smrg        self.ext_version = int(ext_version)
177ec681f3Smrg        self.enable = _bool_to_c_expr(enable)
187ec681f3Smrg
197ec681f3Smrg    def c_android_condition(self):
207ec681f3Smrg        # if it's an EXT or vendor extension, it's allowed
217ec681f3Smrg        if not self.name.startswith(ANDROID_EXTENSION_WHITELIST_PREFIXES):
227ec681f3Smrg            return 'true'
237ec681f3Smrg
247ec681f3Smrg        allowed_version = ALLOWED_ANDROID_VERSION.get(self.name, None)
257ec681f3Smrg        if allowed_version is None:
267ec681f3Smrg            return 'false'
277ec681f3Smrg
287ec681f3Smrg        return 'ANDROID_API_LEVEL >= %d' % (allowed_version)
297ec681f3Smrg
307ec681f3Smrgclass ApiVersion:
317ec681f3Smrg    def __init__(self, version, enable):
327ec681f3Smrg        self.version = version
337ec681f3Smrg        self.enable = _bool_to_c_expr(enable)
347ec681f3Smrg
357ec681f3Smrgclass VkVersion:
367ec681f3Smrg    def __init__(self, string):
377ec681f3Smrg        split = string.split('.')
387ec681f3Smrg        self.major = int(split[0])
397ec681f3Smrg        self.minor = int(split[1])
407ec681f3Smrg        if len(split) > 2:
417ec681f3Smrg            assert len(split) == 3
427ec681f3Smrg            self.patch = int(split[2])
437ec681f3Smrg        else:
447ec681f3Smrg            self.patch = None
457ec681f3Smrg
467ec681f3Smrg        # Sanity check.  The range bits are required by the definition of the
477ec681f3Smrg        # VK_MAKE_VERSION macro
487ec681f3Smrg        assert self.major < 1024 and self.minor < 1024
497ec681f3Smrg        assert self.patch is None or self.patch < 4096
507ec681f3Smrg        assert(str(self) == string)
517ec681f3Smrg
527ec681f3Smrg    def __str__(self):
537ec681f3Smrg        ver_list = [str(self.major), str(self.minor)]
547ec681f3Smrg        if self.patch is not None:
557ec681f3Smrg            ver_list.append(str(self.patch))
567ec681f3Smrg        return '.'.join(ver_list)
577ec681f3Smrg
587ec681f3Smrg    def c_vk_version(self):
597ec681f3Smrg        patch = self.patch if self.patch is not None else 0
607ec681f3Smrg        ver_list = [str(self.major), str(self.minor), str(patch)]
617ec681f3Smrg        return 'VK_MAKE_VERSION(' + ', '.join(ver_list) + ')'
627ec681f3Smrg
637ec681f3Smrg    def __int_ver(self):
647ec681f3Smrg        # This is just an expansion of VK_VERSION
657ec681f3Smrg        patch = self.patch if self.patch is not None else 0
667ec681f3Smrg        return (self.major << 22) | (self.minor << 12) | patch
677ec681f3Smrg
687ec681f3Smrg    def __gt__(self, other):
697ec681f3Smrg        # If only one of them has a patch version, "ignore" it by making
707ec681f3Smrg        # other's patch version match self.
717ec681f3Smrg        if (self.patch is None) != (other.patch is None):
727ec681f3Smrg            other = copy.copy(other)
737ec681f3Smrg            other.patch = self.patch
747ec681f3Smrg
757ec681f3Smrg        return self.__int_ver() > other.__int_ver()
767ec681f3Smrg
777ec681f3Smrg# Sort the extension list the way we expect: KHR, then EXT, then vendors
787ec681f3Smrg# alphabetically. For digits, read them as a whole number sort that.
797ec681f3Smrg# eg.: VK_KHR_8bit_storage < VK_KHR_16bit_storage < VK_EXT_acquire_xlib_display
807ec681f3Smrgdef extension_order(ext):
817ec681f3Smrg    order = []
827ec681f3Smrg    for substring in re.split('(KHR|EXT|[0-9]+)', ext.name):
837ec681f3Smrg        if substring == 'KHR':
847ec681f3Smrg            order.append(1)
857ec681f3Smrg        if substring == 'EXT':
867ec681f3Smrg            order.append(2)
877ec681f3Smrg        elif substring.isdigit():
887ec681f3Smrg            order.append(int(substring))
897ec681f3Smrg        else:
907ec681f3Smrg            order.append(substring)
917ec681f3Smrg    return order
927ec681f3Smrg
937ec681f3Smrgdef get_all_exts_from_xml(xml):
947ec681f3Smrg    """ Get a list of all Vulkan extensions. """
957ec681f3Smrg
967ec681f3Smrg    xml = et.parse(xml)
977ec681f3Smrg
987ec681f3Smrg    extensions = []
997ec681f3Smrg    for ext_elem in xml.findall('.extensions/extension'):
1007ec681f3Smrg        supported = ext_elem.attrib['supported'] == 'vulkan'
1017ec681f3Smrg        name = ext_elem.attrib['name']
1027ec681f3Smrg        if not supported and name != 'VK_ANDROID_native_buffer':
1037ec681f3Smrg            continue
1047ec681f3Smrg        version = None
1057ec681f3Smrg        for enum_elem in ext_elem.findall('.require/enum'):
1067ec681f3Smrg            if enum_elem.attrib['name'].endswith('_SPEC_VERSION'):
1077ec681f3Smrg                # Skip alias SPEC_VERSIONs
1087ec681f3Smrg                if 'value' in enum_elem.attrib:
1097ec681f3Smrg                    assert version is None
1107ec681f3Smrg                    version = int(enum_elem.attrib['value'])
1117ec681f3Smrg        ext = Extension(name, version, True)
1127ec681f3Smrg        extensions.append(Extension(name, version, True))
1137ec681f3Smrg
1147ec681f3Smrg    return sorted(extensions, key=extension_order)
1157ec681f3Smrg
1167ec681f3Smrgdef init_exts_from_xml(xml, extensions, platform_defines):
1177ec681f3Smrg    """ Walk the Vulkan XML and fill out extra extension information. """
1187ec681f3Smrg
1197ec681f3Smrg    xml = et.parse(xml)
1207ec681f3Smrg
1217ec681f3Smrg    ext_name_map = {}
1227ec681f3Smrg    for ext in extensions:
1237ec681f3Smrg        ext_name_map[ext.name] = ext
1247ec681f3Smrg
1257ec681f3Smrg    # KHR_display is missing from the list.
1267ec681f3Smrg    platform_defines.append('VK_USE_PLATFORM_DISPLAY_KHR')
1277ec681f3Smrg    for platform in xml.findall('./platforms/platform'):
1287ec681f3Smrg        platform_defines.append(platform.attrib['protect'])
1297ec681f3Smrg
1307ec681f3Smrg    for ext_elem in xml.findall('.extensions/extension'):
1317ec681f3Smrg        ext_name = ext_elem.attrib['name']
1327ec681f3Smrg        if ext_name not in ext_name_map:
1337ec681f3Smrg            continue
1347ec681f3Smrg
1357ec681f3Smrg        ext = ext_name_map[ext_name]
1367ec681f3Smrg        ext.type = ext_elem.attrib['type']
1377ec681f3Smrg
1387ec681f3Smrg# Mapping between extension name and the android version in which the extension
1397ec681f3Smrg# was whitelisted in Android CTS.
1407ec681f3SmrgALLOWED_ANDROID_VERSION = {
1417ec681f3Smrg    # Allowed Instance KHR Extensions
1427ec681f3Smrg    "VK_KHR_surface": 26,
1437ec681f3Smrg    "VK_KHR_display": 26,
1447ec681f3Smrg    "VK_KHR_android_surface": 26,
1457ec681f3Smrg    "VK_KHR_mir_surface": 26,
1467ec681f3Smrg    "VK_KHR_wayland_surface": 26,
1477ec681f3Smrg    "VK_KHR_win32_surface": 26,
1487ec681f3Smrg    "VK_KHR_xcb_surface": 26,
1497ec681f3Smrg    "VK_KHR_xlib_surface": 26,
1507ec681f3Smrg    "VK_KHR_get_physical_device_properties2": 26,
1517ec681f3Smrg    "VK_KHR_get_surface_capabilities2": 26,
1527ec681f3Smrg    "VK_KHR_external_memory_capabilities": 28,
1537ec681f3Smrg    "VK_KHR_external_semaphore_capabilities": 28,
1547ec681f3Smrg    "VK_KHR_external_fence_capabilities": 28,
1557ec681f3Smrg    "VK_KHR_device_group_creation": 28,
1567ec681f3Smrg    "VK_KHR_get_display_properties2": 29,
1577ec681f3Smrg    "VK_KHR_surface_protected_capabilities": 29,
1587ec681f3Smrg
1597ec681f3Smrg    # Allowed Device KHR Extensions
1607ec681f3Smrg    "VK_KHR_swapchain": 26,
1617ec681f3Smrg    "VK_KHR_display_swapchain": 26,
1627ec681f3Smrg    "VK_KHR_sampler_mirror_clamp_to_edge": 26,
1637ec681f3Smrg    "VK_KHR_shader_draw_parameters": 26,
1647ec681f3Smrg    "VK_KHR_shader_float_controls": 29,
1657ec681f3Smrg    "VK_KHR_shader_float16_int8": 29,
1667ec681f3Smrg    "VK_KHR_maintenance1": 26,
1677ec681f3Smrg    "VK_KHR_push_descriptor": 26,
1687ec681f3Smrg    "VK_KHR_descriptor_update_template": 26,
1697ec681f3Smrg    "VK_KHR_incremental_present": 26,
1707ec681f3Smrg    "VK_KHR_shared_presentable_image": 26,
1717ec681f3Smrg    "VK_KHR_storage_buffer_storage_class": 28,
1727ec681f3Smrg    "VK_KHR_8bit_storage": 29,
1737ec681f3Smrg    "VK_KHR_16bit_storage": 28,
1747ec681f3Smrg    "VK_KHR_get_memory_requirements2": 28,
1757ec681f3Smrg    "VK_KHR_external_memory": 28,
1767ec681f3Smrg    "VK_KHR_external_memory_fd": 28,
1777ec681f3Smrg    "VK_KHR_external_memory_win32": 28,
1787ec681f3Smrg    "VK_KHR_external_semaphore": 28,
1797ec681f3Smrg    "VK_KHR_external_semaphore_fd": 28,
1807ec681f3Smrg    "VK_KHR_external_semaphore_win32": 28,
1817ec681f3Smrg    "VK_KHR_external_fence": 28,
1827ec681f3Smrg    "VK_KHR_external_fence_fd": 28,
1837ec681f3Smrg    "VK_KHR_external_fence_win32": 28,
1847ec681f3Smrg    "VK_KHR_win32_keyed_mutex": 28,
1857ec681f3Smrg    "VK_KHR_dedicated_allocation": 28,
1867ec681f3Smrg    "VK_KHR_variable_pointers": 28,
1877ec681f3Smrg    "VK_KHR_relaxed_block_layout": 28,
1887ec681f3Smrg    "VK_KHR_bind_memory2": 28,
1897ec681f3Smrg    "VK_KHR_maintenance2": 28,
1907ec681f3Smrg    "VK_KHR_image_format_list": 28,
1917ec681f3Smrg    "VK_KHR_sampler_ycbcr_conversion": 28,
1927ec681f3Smrg    "VK_KHR_device_group": 28,
1937ec681f3Smrg    "VK_KHR_multiview": 28,
1947ec681f3Smrg    "VK_KHR_maintenance3": 28,
1957ec681f3Smrg    "VK_KHR_draw_indirect_count": 28,
1967ec681f3Smrg    "VK_KHR_create_renderpass2": 28,
1977ec681f3Smrg    "VK_KHR_depth_stencil_resolve": 29,
1987ec681f3Smrg    "VK_KHR_driver_properties": 28,
1997ec681f3Smrg    "VK_KHR_swapchain_mutable_format": 29,
2007ec681f3Smrg    "VK_KHR_shader_atomic_int64": 29,
2017ec681f3Smrg    "VK_KHR_vulkan_memory_model": 29,
2027ec681f3Smrg    "VK_KHR_performance_query": 30,
2037ec681f3Smrg
2047ec681f3Smrg    "VK_GOOGLE_display_timing": 26,
2057ec681f3Smrg    "VK_ANDROID_native_buffer": 26,
2067ec681f3Smrg    "VK_ANDROID_external_memory_android_hardware_buffer": 28,
2077ec681f3Smrg}
2087ec681f3Smrg
2097ec681f3Smrg# Extensions with these prefixes are checked in Android CTS, and thus must be
2107ec681f3Smrg# whitelisted per the preceding dict.
2117ec681f3SmrgANDROID_EXTENSION_WHITELIST_PREFIXES = (
2127ec681f3Smrg    "VK_KHX",
2137ec681f3Smrg    "VK_KHR",
2147ec681f3Smrg    "VK_GOOGLE",
2157ec681f3Smrg    "VK_ANDROID"
2167ec681f3Smrg)
217