1import copy 2import re 3import xml.etree.ElementTree as et 4 5def get_api_list(s): 6 apis = [] 7 for a in s.split(','): 8 if a == 'disabled': 9 continue 10 assert a in ('vulkan', 'vulkansc') 11 apis.append(a) 12 return apis 13 14class Extension: 15 def __init__(self, name, number, ext_version): 16 self.name = name 17 self.type = None 18 self.number = number 19 self.platform = None 20 self.provisional = False 21 self.ext_version = int(ext_version) 22 self.supported = [] 23 24 def from_xml(ext_elem): 25 name = ext_elem.attrib['name'] 26 number = int(ext_elem.attrib['number']) 27 supported = get_api_list(ext_elem.attrib['supported']) 28 if name == 'VK_ANDROID_native_buffer': 29 assert not supported 30 supported = ['vulkan'] 31 32 if not supported: 33 return Extension(name, number, 0) 34 35 version = None 36 for enum_elem in ext_elem.findall('.require/enum'): 37 if enum_elem.attrib['name'].endswith('_SPEC_VERSION'): 38 # Skip alias SPEC_VERSIONs 39 if 'value' in enum_elem.attrib: 40 assert version is None 41 version = int(enum_elem.attrib['value']) 42 43 assert version is not None 44 ext = Extension(name, number, version) 45 ext.type = ext_elem.attrib['type'] 46 ext.platform = ext_elem.attrib.get('platform', None) 47 ext.provisional = ext_elem.attrib.get('provisional', False) 48 ext.supported = supported 49 50 return ext 51 52 def c_android_condition(self): 53 # if it's an EXT or vendor extension, it's allowed 54 if not self.name.startswith(ANDROID_EXTENSION_WHITELIST_PREFIXES): 55 return 'true' 56 57 allowed_version = ALLOWED_ANDROID_VERSION.get(self.name, None) 58 if allowed_version is None: 59 return 'false' 60 61 return 'ANDROID_API_LEVEL >= %d' % (allowed_version) 62 63class ApiVersion: 64 def __init__(self, version): 65 self.version = version 66 67class VkVersion: 68 def __init__(self, string): 69 split = string.split('.') 70 self.major = int(split[0]) 71 self.minor = int(split[1]) 72 if len(split) > 2: 73 assert len(split) == 3 74 self.patch = int(split[2]) 75 else: 76 self.patch = None 77 78 # Sanity check. The range bits are required by the definition of the 79 # VK_MAKE_VERSION macro 80 assert self.major < 1024 and self.minor < 1024 81 assert self.patch is None or self.patch < 4096 82 assert str(self) == string 83 84 def __str__(self): 85 ver_list = [str(self.major), str(self.minor)] 86 if self.patch is not None: 87 ver_list.append(str(self.patch)) 88 return '.'.join(ver_list) 89 90 def c_vk_version(self): 91 ver_list = [str(self.major), str(self.minor), str(self.patch or 0)] 92 return 'VK_MAKE_VERSION(' + ', '.join(ver_list) + ')' 93 94 def __int_ver(self): 95 # This is just an expansion of VK_VERSION 96 return (self.major << 22) | (self.minor << 12) | (self.patch or 0) 97 98 def __gt__(self, other): 99 # If only one of them has a patch version, "ignore" it by making 100 # other's patch version match self. 101 if (self.patch is None) != (other.patch is None): 102 other = copy.copy(other) 103 other.patch = self.patch 104 105 return self.__int_ver() > other.__int_ver() 106 107# Sort the extension list the way we expect: KHR, then EXT, then vendors 108# alphabetically. For digits, read them as a whole number sort that. 109# eg.: VK_KHR_8bit_storage < VK_KHR_16bit_storage < VK_EXT_acquire_xlib_display 110def extension_order(ext): 111 order = [] 112 for substring in re.split('(KHR|EXT|[0-9]+)', ext.name): 113 if substring == 'KHR': 114 order.append(1) 115 if substring == 'EXT': 116 order.append(2) 117 elif substring.isdigit(): 118 order.append(int(substring)) 119 else: 120 order.append(substring) 121 return order 122 123def get_all_exts_from_xml(xml, api='vulkan'): 124 """ Get a list of all Vulkan extensions. """ 125 126 xml = et.parse(xml) 127 128 extensions = [] 129 for ext_elem in xml.findall('.extensions/extension'): 130 ext = Extension.from_xml(ext_elem) 131 if api in ext.supported: 132 extensions.append(ext) 133 134 return sorted(extensions, key=extension_order) 135 136def init_exts_from_xml(xml, extensions, platform_defines): 137 """ Walk the Vulkan XML and fill out extra extension information. """ 138 139 xml = et.parse(xml) 140 141 ext_name_map = {} 142 for ext in extensions: 143 ext_name_map[ext.name] = ext 144 145 # KHR_display is missing from the list. 146 platform_defines.append('VK_USE_PLATFORM_DISPLAY_KHR') 147 for platform in xml.findall('./platforms/platform'): 148 platform_defines.append(platform.attrib['protect']) 149 150 for ext_elem in xml.findall('.extensions/extension'): 151 ext_name = ext_elem.attrib['name'] 152 if ext_name not in ext_name_map: 153 continue 154 155 ext = ext_name_map[ext_name] 156 ext.type = ext_elem.attrib['type'] 157 158class Requirements: 159 def __init__(self, core_version=None): 160 self.core_version = core_version 161 self.extensions = [] 162 self.guard = None 163 164 def add_extension(self, ext): 165 for e in self.extensions: 166 if e == ext: 167 return; 168 assert e.name != ext.name 169 170 self.extensions.append(ext) 171 172def filter_api(elem, api): 173 if 'api' not in elem.attrib: 174 return True 175 176 return api in elem.attrib['api'].split(',') 177 178def get_alias(aliases, name): 179 if name in aliases: 180 # in case the spec registry adds an alias chain later 181 return get_alias(aliases, aliases[name]) 182 return name 183 184def get_all_required(xml, thing, api, beta): 185 things = {} 186 aliases = {} 187 for struct in xml.findall('./types/type[@category="struct"][@alias]'): 188 if not filter_api(struct, api): 189 continue 190 191 name = struct.attrib['name'] 192 alias = struct.attrib['alias'] 193 aliases[name] = alias 194 195 for feature in xml.findall('./feature'): 196 if not filter_api(feature, api): 197 continue 198 199 version = VkVersion(feature.attrib['number']) 200 for t in feature.findall('./require/' + thing): 201 name = t.attrib['name'] 202 assert name not in things 203 things[name] = Requirements(core_version=version) 204 205 for extension in xml.findall('.extensions/extension'): 206 ext = Extension.from_xml(extension) 207 if api not in ext.supported: 208 continue 209 210 if beta != 'true' and ext.provisional: 211 continue 212 213 for require in extension.findall('./require'): 214 if not filter_api(require, api): 215 continue 216 217 for t in require.findall('./' + thing): 218 name = get_alias(aliases, t.attrib['name']) 219 r = things.setdefault(name, Requirements()) 220 r.add_extension(ext) 221 222 platform_defines = {} 223 for platform in xml.findall('./platforms/platform'): 224 name = platform.attrib['name'] 225 define = platform.attrib['protect'] 226 platform_defines[name] = define 227 228 for req in things.values(): 229 if req.core_version is not None: 230 continue 231 232 for ext in req.extensions: 233 if ext.platform in platform_defines: 234 req.guard = platform_defines[ext.platform] 235 break 236 237 return things 238 239# Mapping between extension name and the android version in which the extension 240# was whitelisted in Android CTS's dEQP-VK.info.device_extensions and 241# dEQP-VK.api.info.android.no_unknown_extensions, excluding those blocked by 242# android.graphics.cts.VulkanFeaturesTest#testVulkanBlockedExtensions. 243ALLOWED_ANDROID_VERSION = { 244 # checkInstanceExtensions on oreo-cts-release 245 "VK_KHR_surface": 26, 246 "VK_KHR_display": 26, 247 "VK_KHR_android_surface": 26, 248 "VK_KHR_mir_surface": 26, 249 "VK_KHR_wayland_surface": 26, 250 "VK_KHR_win32_surface": 26, 251 "VK_KHR_xcb_surface": 26, 252 "VK_KHR_xlib_surface": 26, 253 "VK_KHR_get_physical_device_properties2": 26, 254 "VK_KHR_get_surface_capabilities2": 26, 255 "VK_KHR_external_memory_capabilities": 26, 256 "VK_KHR_external_semaphore_capabilities": 26, 257 "VK_KHR_external_fence_capabilities": 26, 258 # on pie-cts-release 259 "VK_KHR_device_group_creation": 28, 260 "VK_KHR_get_display_properties2": 28, 261 # on android10-tests-release 262 "VK_KHR_surface_protected_capabilities": 29, 263 # on android13-tests-release 264 "VK_KHR_portability_enumeration": 33, 265 266 # checkDeviceExtensions on oreo-cts-release 267 "VK_KHR_swapchain": 26, 268 "VK_KHR_display_swapchain": 26, 269 "VK_KHR_sampler_mirror_clamp_to_edge": 26, 270 "VK_KHR_shader_draw_parameters": 26, 271 "VK_KHR_maintenance1": 26, 272 "VK_KHR_push_descriptor": 26, 273 "VK_KHR_descriptor_update_template": 26, 274 "VK_KHR_incremental_present": 26, 275 "VK_KHR_shared_presentable_image": 26, 276 "VK_KHR_storage_buffer_storage_class": 26, 277 "VK_KHR_16bit_storage": 26, 278 "VK_KHR_get_memory_requirements2": 26, 279 "VK_KHR_external_memory": 26, 280 "VK_KHR_external_memory_fd": 26, 281 "VK_KHR_external_memory_win32": 26, 282 "VK_KHR_external_semaphore": 26, 283 "VK_KHR_external_semaphore_fd": 26, 284 "VK_KHR_external_semaphore_win32": 26, 285 "VK_KHR_external_fence": 26, 286 "VK_KHR_external_fence_fd": 26, 287 "VK_KHR_external_fence_win32": 26, 288 "VK_KHR_win32_keyed_mutex": 26, 289 "VK_KHR_dedicated_allocation": 26, 290 "VK_KHR_variable_pointers": 26, 291 "VK_KHR_relaxed_block_layout": 26, 292 "VK_KHR_bind_memory2": 26, 293 "VK_KHR_maintenance2": 26, 294 "VK_KHR_image_format_list": 26, 295 "VK_KHR_sampler_ycbcr_conversion": 26, 296 # on oreo-mr1-cts-release 297 "VK_KHR_draw_indirect_count": 27, 298 # on pie-cts-release 299 "VK_KHR_device_group": 28, 300 "VK_KHR_multiview": 28, 301 "VK_KHR_maintenance3": 28, 302 "VK_KHR_create_renderpass2": 28, 303 "VK_KHR_driver_properties": 28, 304 # on android10-tests-release 305 "VK_KHR_shader_float_controls": 29, 306 "VK_KHR_shader_float16_int8": 29, 307 "VK_KHR_8bit_storage": 29, 308 "VK_KHR_depth_stencil_resolve": 29, 309 "VK_KHR_swapchain_mutable_format": 29, 310 "VK_KHR_shader_atomic_int64": 29, 311 "VK_KHR_vulkan_memory_model": 29, 312 "VK_KHR_swapchain_mutable_format": 29, 313 "VK_KHR_uniform_buffer_standard_layout": 29, 314 # on android11-tests-release 315 "VK_KHR_imageless_framebuffer": 30, 316 "VK_KHR_shader_subgroup_extended_types": 30, 317 "VK_KHR_buffer_device_address": 30, 318 "VK_KHR_separate_depth_stencil_layouts": 30, 319 "VK_KHR_timeline_semaphore": 30, 320 "VK_KHR_spirv_1_4": 30, 321 "VK_KHR_pipeline_executable_properties": 30, 322 "VK_KHR_shader_clock": 30, 323 # blocked by testVulkanBlockedExtensions 324 # "VK_KHR_performance_query": 30, 325 "VK_KHR_shader_non_semantic_info": 30, 326 "VK_KHR_copy_commands2": 30, 327 # on android12-tests-release 328 "VK_KHR_shader_terminate_invocation": 31, 329 "VK_KHR_ray_tracing_pipeline": 31, 330 "VK_KHR_ray_query": 31, 331 "VK_KHR_acceleration_structure": 31, 332 "VK_KHR_pipeline_library": 31, 333 "VK_KHR_deferred_host_operations": 31, 334 "VK_KHR_fragment_shading_rate": 31, 335 "VK_KHR_zero_initialize_workgroup_memory": 31, 336 "VK_KHR_workgroup_memory_explicit_layout": 31, 337 "VK_KHR_synchronization2": 31, 338 "VK_KHR_shader_integer_dot_product": 31, 339 # on android13-tests-release 340 "VK_KHR_dynamic_rendering": 33, 341 "VK_KHR_format_feature_flags2": 33, 342 "VK_KHR_global_priority": 33, 343 "VK_KHR_maintenance4": 33, 344 "VK_KHR_portability_subset": 33, 345 "VK_KHR_present_id": 33, 346 "VK_KHR_present_wait": 33, 347 "VK_KHR_shader_subgroup_uniform_control_flow": 33, 348 349 # testNoUnknownExtensions on oreo-cts-release 350 "VK_GOOGLE_display_timing": 26, 351 # on pie-cts-release 352 "VK_ANDROID_external_memory_android_hardware_buffer": 28, 353 # on android11-tests-release 354 "VK_GOOGLE_decorate_string": 30, 355 "VK_GOOGLE_hlsl_functionality1": 30, 356 # on android13-tests-release 357 "VK_GOOGLE_surfaceless_query": 33, 358 359 # this HAL extension is always allowed and will be filtered out by the 360 # loader 361 "VK_ANDROID_native_buffer": 26, 362} 363 364# Extensions with these prefixes are checked in Android CTS, and thus must be 365# whitelisted per the preceding dict. 366ANDROID_EXTENSION_WHITELIST_PREFIXES = ( 367 "VK_KHX", 368 "VK_KHR", 369 "VK_GOOGLE", 370 "VK_ANDROID" 371) 372