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