Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright 2019 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | # This script provides the common functions for generating the |
| 18 | # vulkan framework directly from the vulkan registry (vk.xml). |
| 19 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 20 | import os |
| 21 | import subprocess |
| 22 | import xml.etree.ElementTree as element_tree |
Adithya Srinivasan | 8dce9d7 | 2019-07-11 14:26:04 -0700 | [diff] [blame] | 23 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 24 | _BLACKLISTED_EXTENSIONS = [ |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 25 | 'VK_EXT_acquire_xlib_display', |
| 26 | 'VK_EXT_direct_mode_display', |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 27 | 'VK_EXT_display_control', |
Yiwei Zhang | dc792f5 | 2019-10-10 16:29:42 -0700 | [diff] [blame] | 28 | 'VK_EXT_display_surface_counter', |
| 29 | 'VK_EXT_full_screen_exclusive', |
| 30 | 'VK_EXT_headless_surface', |
| 31 | 'VK_EXT_metal_surface', |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 32 | 'VK_FUCHSIA_imagepipe_surface', |
Yiwei Zhang | dc792f5 | 2019-10-10 16:29:42 -0700 | [diff] [blame] | 33 | 'VK_GGP_stream_descriptor_surface', |
| 34 | 'VK_KHR_display', |
| 35 | 'VK_KHR_display_swapchain', |
| 36 | 'VK_KHR_external_fence_win32', |
| 37 | 'VK_KHR_external_memory_win32', |
| 38 | 'VK_KHR_external_semaphore_win32', |
| 39 | 'VK_KHR_mir_surface', |
| 40 | 'VK_KHR_wayland_surface', |
| 41 | 'VK_KHR_win32_keyed_mutex', |
| 42 | 'VK_KHR_win32_surface', |
| 43 | 'VK_KHR_xcb_surface', |
| 44 | 'VK_KHR_xlib_surface', |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 45 | 'VK_MVK_ios_surface', |
| 46 | 'VK_MVK_macos_surface', |
| 47 | 'VK_NN_vi_surface', |
Yiwei Zhang | dc792f5 | 2019-10-10 16:29:42 -0700 | [diff] [blame] | 48 | 'VK_NV_cooperative_matrix', |
| 49 | 'VK_NV_coverage_reduction_mode', |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 50 | 'VK_NV_external_memory_win32', |
| 51 | 'VK_NV_win32_keyed_mutex', |
Yiwei Zhang | dc792f5 | 2019-10-10 16:29:42 -0700 | [diff] [blame] | 52 | 'VK_NVX_image_view_handle', |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 53 | ] |
| 54 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 55 | _EXPORTED_EXTENSIONS = [ |
Yiwei Zhang | dc792f5 | 2019-10-10 16:29:42 -0700 | [diff] [blame] | 56 | 'VK_ANDROID_external_memory_android_hardware_buffer', |
| 57 | 'VK_KHR_android_surface', |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 58 | 'VK_KHR_surface', |
| 59 | 'VK_KHR_swapchain', |
Yiwei Zhang | dc792f5 | 2019-10-10 16:29:42 -0700 | [diff] [blame] | 60 | ] |
| 61 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 62 | _OPTIONAL_COMMANDS = [ |
Yiwei Zhang | dc792f5 | 2019-10-10 16:29:42 -0700 | [diff] [blame] | 63 | 'vkGetSwapchainGrallocUsageANDROID', |
| 64 | 'vkGetSwapchainGrallocUsage2ANDROID', |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 65 | ] |
| 66 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 67 | _DISPATCH_TYPE_DICT = { |
| 68 | 'VkInstance ': 'Instance', |
| 69 | 'VkPhysicalDevice ': 'Instance', |
| 70 | 'VkDevice ': 'Device', |
| 71 | 'VkQueue ': 'Device', |
| 72 | 'VkCommandBuffer ': 'Device' |
| 73 | } |
Adithya Srinivasan | 8dce9d7 | 2019-07-11 14:26:04 -0700 | [diff] [blame] | 74 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 75 | alias_dict = {} |
| 76 | command_list = [] |
| 77 | extension_dict = {} |
| 78 | param_dict = {} |
| 79 | return_type_dict = {} |
| 80 | version_dict = {} |
Adithya Srinivasan | 0136414 | 2019-07-02 15:52:49 -0700 | [diff] [blame] | 81 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 82 | |
| 83 | def indent(num): |
| 84 | return ' ' * num |
| 85 | |
| 86 | |
| 87 | def copyright_and_warning(year): |
| 88 | return """\ |
| 89 | /* |
| 90 | * Copyright """ + str(year) + """ The Android Open Source Project |
| 91 | * |
| 92 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 93 | * you may not use this file except in compliance with the License. |
| 94 | * You may obtain a copy of the License at |
| 95 | * |
| 96 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 97 | * |
| 98 | * Unless required by applicable law or agreed to in writing, software |
| 99 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 100 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 101 | * See the License for the specific language governing permissions and |
| 102 | * limitations under the License. |
| 103 | */ |
| 104 | |
| 105 | // WARNING: This file is generated. See ../README.md for instructions. |
| 106 | |
| 107 | """ |
| 108 | |
| 109 | |
| 110 | def run_clang_format(args): |
| 111 | clang_call = ['clang-format', '--style', 'file', '-i', args] |
| 112 | subprocess.check_call(clang_call) |
| 113 | |
| 114 | |
| 115 | def is_extension_internal(extension_name): |
| 116 | return extension_name == 'VK_ANDROID_native_buffer' |
| 117 | |
| 118 | |
| 119 | def base_name(cmd): |
| 120 | return cmd[2:] |
| 121 | |
| 122 | |
| 123 | def is_function_supported(cmd): |
| 124 | if cmd not in extension_dict: |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 125 | return True |
| 126 | else: |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 127 | if extension_dict[cmd] not in _BLACKLISTED_EXTENSIONS: |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 128 | return True |
| 129 | return False |
| 130 | |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 131 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 132 | def get_dispatch_table_type(cmd): |
| 133 | if cmd not in param_dict: |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 134 | return None |
| 135 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 136 | if param_dict[cmd]: |
| 137 | return _DISPATCH_TYPE_DICT.get(param_dict[cmd][0][0], 'Global') |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 138 | return 'Global' |
| 139 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 140 | |
| 141 | def is_globally_dispatched(cmd): |
| 142 | return is_function_supported(cmd) and get_dispatch_table_type(cmd) == 'Global' |
| 143 | |
| 144 | |
| 145 | def is_instance_dispatched(cmd): |
| 146 | return (is_function_supported(cmd) and |
| 147 | get_dispatch_table_type(cmd) == 'Instance') |
| 148 | |
| 149 | |
| 150 | def is_device_dispatched(cmd): |
| 151 | return is_function_supported(cmd) and get_dispatch_table_type(cmd) == 'Device' |
| 152 | |
| 153 | |
| 154 | def is_extension_exported(extension_name): |
| 155 | return extension_name in _EXPORTED_EXTENSIONS |
| 156 | |
| 157 | |
| 158 | def is_function_exported(cmd): |
| 159 | if is_function_supported(cmd): |
| 160 | if cmd in extension_dict: |
| 161 | return is_extension_exported(extension_dict[cmd]) |
| 162 | return True |
| 163 | return False |
| 164 | |
| 165 | |
| 166 | def is_instance_dispatch_table_entry(cmd): |
| 167 | if cmd == 'vkEnumerateDeviceLayerProperties': |
| 168 | # deprecated, unused internally - @dbd33bc |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 169 | return False |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 170 | return is_function_exported(cmd) and is_instance_dispatched(cmd) |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 171 | |
| 172 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 173 | def is_device_dispatch_table_entry(cmd): |
| 174 | return is_function_exported(cmd) and is_device_dispatched(cmd) |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 175 | |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 176 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 177 | def init_proc(name, f): |
| 178 | f.write(indent(1)) |
| 179 | if name in extension_dict: |
| 180 | f.write('INIT_PROC_EXT(' + extension_dict[name][3:] + ', ') |
| 181 | else: |
| 182 | f.write('INIT_PROC(') |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 183 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 184 | if name in version_dict and version_dict[name] == 'VK_VERSION_1_1': |
| 185 | f.write('false, ') |
| 186 | elif name in _OPTIONAL_COMMANDS: |
| 187 | f.write('false, ') |
| 188 | else: |
| 189 | f.write('true, ') |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 190 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 191 | if is_instance_dispatched(name): |
| 192 | f.write('instance, ') |
| 193 | else: |
| 194 | f.write('dev, ') |
| 195 | |
| 196 | f.write(base_name(name) + ');\n') |
| 197 | |
| 198 | |
| 199 | def parse_vulkan_registry(): |
| 200 | registry = os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', |
| 201 | 'external', 'vulkan-headers', 'registry', 'vk.xml') |
| 202 | tree = element_tree.parse(registry) |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 203 | root = tree.getroot() |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 204 | for commands in root.iter('commands'): |
| 205 | for command in commands: |
| 206 | if command.tag == 'command': |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 207 | parameter_list = [] |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 208 | protoset = False |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 209 | cmd_name = '' |
| 210 | cmd_type = '' |
| 211 | if command.get('alias') is not None: |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 212 | alias = command.get('alias') |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 213 | cmd_name = command.get('name') |
| 214 | alias_dict[cmd_name] = alias |
| 215 | command_list.append(cmd_name) |
| 216 | param_dict[cmd_name] = param_dict[alias].copy() |
| 217 | return_type_dict[cmd_name] = return_type_dict[alias] |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 218 | for params in command: |
Yiwei Zhang | 4bc489b | 2019-09-23 15:17:22 -0700 | [diff] [blame] | 219 | if params.tag == 'param': |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 220 | param_type = '' |
| 221 | if params.text is not None and params.text.strip(): |
| 222 | param_type = params.text.strip() + ' ' |
| 223 | type_val = params.find('type') |
| 224 | param_type = param_type + type_val.text |
| 225 | if type_val.tail is not None: |
| 226 | param_type += type_val.tail.strip() + ' ' |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 227 | pname = params.find('name') |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 228 | param_name = pname.text |
| 229 | if pname.tail is not None and pname.tail.strip(): |
| 230 | parameter_list.append( |
| 231 | (param_type, param_name, pname.tail.strip())) |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 232 | else: |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 233 | parameter_list.append((param_type, param_name)) |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 234 | if params.tag == 'proto': |
| 235 | for c in params: |
| 236 | if c.tag == 'type': |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 237 | cmd_type = c.text |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 238 | if c.tag == 'name': |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 239 | cmd_name = c.text |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 240 | protoset = True |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 241 | command_list.append(cmd_name) |
| 242 | return_type_dict[cmd_name] = cmd_type |
| 243 | if protoset: |
| 244 | param_dict[cmd_name] = parameter_list.copy() |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 245 | |
| 246 | for exts in root.iter('extensions'): |
| 247 | for extension in exts: |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 248 | apiversion = '' |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 249 | if extension.tag == 'extension': |
| 250 | extname = extension.get('name') |
| 251 | for req in extension: |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 252 | if req.get('feature') is not None: |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 253 | apiversion = req.get('feature') |
| 254 | for commands in req: |
| 255 | if commands.tag == 'command': |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 256 | cmd_name = commands.get('name') |
| 257 | if cmd_name not in extension_dict: |
| 258 | extension_dict[cmd_name] = extname |
| 259 | if apiversion: |
| 260 | version_dict[cmd_name] = apiversion |
Adithya Srinivasan | 751a7dc | 2019-07-02 17:17:25 -0700 | [diff] [blame] | 261 | |
| 262 | for feature in root.iter('feature'): |
| 263 | apiversion = feature.get('name') |
| 264 | for req in feature: |
| 265 | for command in req: |
| 266 | if command.tag == 'command': |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame^] | 267 | cmd_name = command.get('name') |
| 268 | if cmd_name in command_list: |
| 269 | version_dict[cmd_name] = apiversion |