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