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