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 def __le__(self, other): 108 return not self.__gt__(other) 109 110# Sort the extension list the way we expect: KHR, then EXT, then vendors 111# alphabetically. For digits, read them as a whole number sort that. 112# eg.: VK_KHR_8bit_storage < VK_KHR_16bit_storage < VK_EXT_acquire_xlib_display 113def extension_order(ext): 114 order = [] 115 for substring in re.split('(KHR|EXT|[0-9]+)', ext.name): 116 if substring == 'KHR': 117 order.append(1) 118 if substring == 'EXT': 119 order.append(2) 120 elif substring.isdigit(): 121 order.append(int(substring)) 122 else: 123 order.append(substring) 124 return order 125 126def get_all_exts_from_xml(xml, api='vulkan'): 127 """ Get a list of all Vulkan extensions. """ 128 129 xml = et.parse(xml) 130 131 extensions = [] 132 for ext_elem in xml.findall('.extensions/extension'): 133 ext = Extension.from_xml(ext_elem) 134 if api in ext.supported: 135 extensions.append(ext) 136 137 return sorted(extensions, key=extension_order) 138 139def init_exts_from_xml(xml, extensions, platform_defines): 140 """ Walk the Vulkan XML and fill out extra extension information. """ 141 142 xml = et.parse(xml) 143 144 ext_name_map = {} 145 for ext in extensions: 146 ext_name_map[ext.name] = ext 147 148 # KHR_display is missing from the list. 149 platform_defines.append('VK_USE_PLATFORM_DISPLAY_KHR') 150 for platform in xml.findall('./platforms/platform'): 151 platform_defines.append(platform.attrib['protect']) 152 153 for ext_elem in xml.findall('.extensions/extension'): 154 ext_name = ext_elem.attrib['name'] 155 if ext_name not in ext_name_map: 156 continue 157 158 ext = ext_name_map[ext_name] 159 ext.type = ext_elem.attrib['type'] 160 161class Requirements: 162 def __init__(self, core_version=None): 163 self.core_version = core_version 164 self.extensions = [] 165 self.guard = None 166 167 def add_extension(self, ext): 168 for e in self.extensions: 169 if e == ext: 170 return; 171 assert e.name != ext.name 172 173 self.extensions.append(ext) 174 175def filter_api(elem, api): 176 if 'api' not in elem.attrib: 177 return True 178 179 return api in elem.attrib['api'].split(',') 180 181def get_alias(aliases, name): 182 if name in aliases: 183 # in case the spec registry adds an alias chain later 184 return get_alias(aliases, aliases[name]) 185 return name 186 187def get_all_required(xml, thing, api, beta): 188 things = {} 189 aliases = {} 190 for struct in xml.findall('./types/type[@category="struct"][@alias]'): 191 if not filter_api(struct, api): 192 continue 193 194 name = struct.attrib['name'] 195 alias = struct.attrib['alias'] 196 aliases[name] = alias 197 198 for feature in xml.findall('./feature'): 199 if not filter_api(feature, api): 200 continue 201 202 version = VkVersion(feature.attrib['number']) 203 for t in feature.findall('./require/' + thing): 204 name = t.attrib['name'] 205 if name in things: 206 assert things[name].core_version <= version 207 else: 208 things[name] = Requirements(core_version=version) 209 210 for extension in xml.findall('.extensions/extension'): 211 ext = Extension.from_xml(extension) 212 if api not in ext.supported: 213 continue 214 215 if beta != 'true' and ext.provisional: 216 continue 217 218 for require in extension.findall('./require'): 219 if not filter_api(require, api): 220 continue 221 222 for t in require.findall('./' + thing): 223 name = get_alias(aliases, t.attrib['name']) 224 r = things.setdefault(name, Requirements()) 225 r.add_extension(ext) 226 227 platform_defines = {} 228 for platform in xml.findall('./platforms/platform'): 229 name = platform.attrib['name'] 230 define = platform.attrib['protect'] 231 platform_defines[name] = define 232 233 for req in things.values(): 234 if req.core_version is not None: 235 continue 236 237 for ext in req.extensions: 238 if ext.platform in platform_defines: 239 req.guard = platform_defines[ext.platform] 240 break 241 242 return things 243 244# Mapping between extension name and the android version in which the extension 245# was whitelisted in Android CTS's dEQP-VK.info.device_extensions and 246# dEQP-VK.api.info.android.no_unknown_extensions, excluding those blocked by 247# android.graphics.cts.VulkanFeaturesTest#testVulkanBlockedExtensions. 248ALLOWED_ANDROID_VERSION = { 249 # checkInstanceExtensions on oreo-cts-release 250 "VK_KHR_surface": 26, 251 "VK_KHR_display": 26, 252 "VK_KHR_android_surface": 26, 253 "VK_KHR_mir_surface": 26, 254 "VK_KHR_wayland_surface": 26, 255 "VK_KHR_win32_surface": 26, 256 "VK_KHR_xcb_surface": 26, 257 "VK_KHR_xlib_surface": 26, 258 "VK_KHR_get_physical_device_properties2": 26, 259 "VK_KHR_get_surface_capabilities2": 26, 260 "VK_KHR_external_memory_capabilities": 26, 261 "VK_KHR_external_semaphore_capabilities": 26, 262 "VK_KHR_external_fence_capabilities": 26, 263 # on pie-cts-release 264 "VK_KHR_device_group_creation": 28, 265 "VK_KHR_get_display_properties2": 28, 266 # on android10-tests-release 267 "VK_KHR_surface_protected_capabilities": 29, 268 # on android13-tests-release 269 "VK_KHR_portability_enumeration": 33, 270 271 # checkDeviceExtensions on oreo-cts-release 272 "VK_KHR_swapchain": 26, 273 "VK_KHR_display_swapchain": 26, 274 "VK_KHR_sampler_mirror_clamp_to_edge": 26, 275 "VK_KHR_shader_draw_parameters": 26, 276 "VK_KHR_maintenance1": 26, 277 "VK_KHR_push_descriptor": 26, 278 "VK_KHR_descriptor_update_template": 26, 279 "VK_KHR_incremental_present": 26, 280 "VK_KHR_shared_presentable_image": 26, 281 "VK_KHR_storage_buffer_storage_class": 26, 282 "VK_KHR_16bit_storage": 26, 283 "VK_KHR_get_memory_requirements2": 26, 284 "VK_KHR_external_memory": 26, 285 "VK_KHR_external_memory_fd": 26, 286 "VK_KHR_external_memory_win32": 26, 287 "VK_KHR_external_semaphore": 26, 288 "VK_KHR_external_semaphore_fd": 26, 289 "VK_KHR_external_semaphore_win32": 26, 290 "VK_KHR_external_fence": 26, 291 "VK_KHR_external_fence_fd": 26, 292 "VK_KHR_external_fence_win32": 26, 293 "VK_KHR_win32_keyed_mutex": 26, 294 "VK_KHR_dedicated_allocation": 26, 295 "VK_KHR_variable_pointers": 26, 296 "VK_KHR_relaxed_block_layout": 26, 297 "VK_KHR_bind_memory2": 26, 298 "VK_KHR_maintenance2": 26, 299 "VK_KHR_image_format_list": 26, 300 "VK_KHR_sampler_ycbcr_conversion": 26, 301 # on oreo-mr1-cts-release 302 "VK_KHR_draw_indirect_count": 27, 303 # on pie-cts-release 304 "VK_KHR_device_group": 28, 305 "VK_KHR_multiview": 28, 306 "VK_KHR_maintenance3": 28, 307 "VK_KHR_create_renderpass2": 28, 308 "VK_KHR_driver_properties": 28, 309 # on android10-tests-release 310 "VK_KHR_shader_float_controls": 29, 311 "VK_KHR_shader_float16_int8": 29, 312 "VK_KHR_8bit_storage": 29, 313 "VK_KHR_depth_stencil_resolve": 29, 314 "VK_KHR_swapchain_mutable_format": 29, 315 "VK_KHR_shader_atomic_int64": 29, 316 "VK_KHR_vulkan_memory_model": 29, 317 "VK_KHR_swapchain_mutable_format": 29, 318 "VK_KHR_uniform_buffer_standard_layout": 29, 319 # on android11-tests-release 320 "VK_KHR_imageless_framebuffer": 30, 321 "VK_KHR_shader_subgroup_extended_types": 30, 322 "VK_KHR_buffer_device_address": 30, 323 "VK_KHR_separate_depth_stencil_layouts": 30, 324 "VK_KHR_timeline_semaphore": 30, 325 "VK_KHR_spirv_1_4": 30, 326 "VK_KHR_pipeline_executable_properties": 30, 327 "VK_KHR_shader_clock": 30, 328 # blocked by testVulkanBlockedExtensions 329 # "VK_KHR_performance_query": 30, 330 "VK_KHR_shader_non_semantic_info": 30, 331 "VK_KHR_copy_commands2": 30, 332 # on android12-tests-release 333 "VK_KHR_shader_terminate_invocation": 31, 334 "VK_KHR_ray_tracing_pipeline": 31, 335 "VK_KHR_ray_query": 31, 336 "VK_KHR_acceleration_structure": 31, 337 "VK_KHR_pipeline_library": 31, 338 "VK_KHR_deferred_host_operations": 31, 339 "VK_KHR_fragment_shading_rate": 31, 340 "VK_KHR_zero_initialize_workgroup_memory": 31, 341 "VK_KHR_workgroup_memory_explicit_layout": 31, 342 "VK_KHR_synchronization2": 31, 343 "VK_KHR_shader_integer_dot_product": 31, 344 # on android13-tests-release 345 "VK_KHR_dynamic_rendering": 33, 346 "VK_KHR_format_feature_flags2": 33, 347 "VK_KHR_global_priority": 33, 348 "VK_KHR_maintenance4": 33, 349 "VK_KHR_portability_subset": 33, 350 "VK_KHR_present_id": 33, 351 "VK_KHR_present_wait": 33, 352 "VK_KHR_shader_subgroup_uniform_control_flow": 33, 353 354 # testNoUnknownExtensions on oreo-cts-release 355 "VK_GOOGLE_display_timing": 26, 356 # on pie-cts-release 357 "VK_ANDROID_external_memory_android_hardware_buffer": 28, 358 # on android11-tests-release 359 "VK_GOOGLE_decorate_string": 30, 360 "VK_GOOGLE_hlsl_functionality1": 30, 361 # on android13-tests-release 362 "VK_GOOGLE_surfaceless_query": 33, 363 364 # this HAL extension is always allowed and will be filtered out by the 365 # loader 366 "VK_ANDROID_native_buffer": 26, 367} 368 369# Extensions with these prefixes are checked in Android CTS, and thus must be 370# whitelisted per the preceding dict. 371ANDROID_EXTENSION_WHITELIST_PREFIXES = ( 372 "VK_KHX", 373 "VK_KHR", 374 "VK_GOOGLE", 375 "VK_ANDROID" 376) 377