blob: fe9dab4ed613b0e23bf7373bab2cc23cd2667c07 [file] [log] [blame]
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -07001#!/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 Srinivasan8dce9d72019-07-11 14:26:04 -070020from subprocess import check_call
21
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070022copyright = """/*
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
40warning = '// WARNING: This file is generated. See ../README.md for instructions.\n\n'
41
42blacklistedExtensions = [
43 'VK_KHR_display',
44 'VK_KHR_display_swapchain',
45 'VK_KHR_mir_surface',
46 'VK_KHR_xcb_surface',
47 'VK_KHR_xlib_surface',
48 'VK_KHR_wayland_surface',
49 'VK_KHR_win32_surface',
50 'VK_KHR_external_memory_win32',
51 'VK_KHR_win32_keyed_mutex',
52 'VK_KHR_external_semaphore_win32',
53 'VK_KHR_external_fence_win32',
54 'VK_EXT_acquire_xlib_display',
55 'VK_EXT_direct_mode_display',
56 'VK_EXT_display_surface_counter',
57 'VK_EXT_display_control',
58 'VK_FUCHSIA_imagepipe_surface',
59 'VK_MVK_ios_surface',
60 'VK_MVK_macos_surface',
61 'VK_NN_vi_surface',
62 'VK_NV_external_memory_win32',
63 'VK_NV_win32_keyed_mutex',
64 'VK_EXT_metal_surface', #not present in vulkan.api
65 'VK_NVX_image_view_handle', #not present in vulkan.api
Adithya Srinivasan01364142019-07-02 15:52:49 -070066 'VK_NV_cooperative_matrix', #not present in vulkan.api
67 'VK_EXT_headless_surface', #not present in vulkan.api
68 'VK_GGP_stream_descriptor_surface', #not present in vulkan.api
69 'VK_NV_coverage_reduction_mode', #not present in vulkan.api
70 'VK_EXT_full_screen_exclusive' #not present in vulkan.api
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070071]
72
73exportedExtensions = [
74 'VK_KHR_surface',
75 'VK_KHR_swapchain',
76 'VK_KHR_android_surface',
77 'VK_ANDROID_external_memory_android_hardware_buffer'
78]
79
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -070080def runClangFormat(args):
81 clang_call = ["clang-format", "--style", "file", "-i", args]
82 check_call (clang_call)
83
Adithya Srinivasan01364142019-07-02 15:52:49 -070084def isExtensionInternal(extensionName):
85 if extensionName == 'VK_ANDROID_native_buffer':
86 return True
87 return False
88
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070089def isFunctionSupported(functionName):
90 if functionName not in extensionsDict:
91 return True
92 else:
93 if extensionsDict[functionName] not in blacklistedExtensions:
94 return True
95 return False
96
97def isInstanceDispatched(functionName):
98 return isFunctionSupported(functionName) and getDispatchTableType(functionName) == 'Instance'
99
100def isDeviceDispatched(functionName):
101 return isFunctionSupported(functionName) and getDispatchTableType(functionName) == 'Device'
102
103def isGloballyDispatched(functionName):
104 return isFunctionSupported(functionName) and getDispatchTableType(functionName) == 'Global'
105
106def isExtensionExported(extensionName):
107 if extensionName in exportedExtensions:
108 return True
109 return False
110
111def isFunctionExported(functionName):
112 if isFunctionSupported(functionName):
113 if functionName in extensionsDict:
114 return isExtensionExported(extensionsDict[functionName])
115 return True
116 return False
117
118def getDispatchTableType(functionName):
119 if functionName not in paramDict:
120 return None
121
122 switchCase = {
123 'VkInstance ' : 'Instance',
124 'VkPhysicalDevice ' : 'Instance',
125 'VkDevice ' : 'Device',
126 'VkQueue ' : 'Device',
127 'VkCommandBuffer ' : 'Device'
128 }
129
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700130 if len(paramDict[functionName]) > 0:
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700131 return switchCase.get(paramDict[functionName][0][0], 'Global')
132 return 'Global'
133
134def isInstanceDispatchTableEntry(functionName):
135 if functionName == 'vkEnumerateDeviceLayerProperties': # deprecated, unused internally - @dbd33bc
136 return False
137 if isFunctionExported(functionName) and isInstanceDispatched(functionName):
138 return True
139 return False
140
141def isDeviceDispatchTableEntry(functionName):
142 if isFunctionExported(functionName) and isDeviceDispatched(functionName):
143 return True
144 return False
145
146
147def clang_on(f, indent):
148 f.write (clang_off_spaces * indent + '// clang-format on\n')
149
150def clang_off(f, indent):
151 f.write (clang_off_spaces * indent + '// clang-format off\n')
152
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700153clang_off_spaces = ' ' * 4
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700154
155parametersList = []
156paramDict = {}
157allCommandsList = []
158extensionsDict = {}
159returnTypeDict = {}
160versionDict = {}
161aliasDict = {}
162
163def parseVulkanRegistry():
164 import xml.etree.ElementTree as ET
165 import os
166 vulkan_registry = os.path.join(os.path.dirname(__file__),'..','..','..','..','external','vulkan-headers','registry','vk.xml')
167 tree = ET.parse(vulkan_registry)
168 root = tree.getroot()
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700169 for commands in root.iter('commands'):
170 for command in commands:
171 if command.tag == 'command':
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700172 parametersList.clear()
173 protoset = False
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700174 fnName = ""
175 fnType = ""
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700176 if command.get('alias') != None:
177 alias = command.get('alias')
178 fnName = command.get('name')
179 aliasDict[fnName] = alias
180 allCommandsList.append(fnName)
181 paramDict[fnName] = paramDict[alias].copy()
Adithya Srinivasan01364142019-07-02 15:52:49 -0700182 returnTypeDict[fnName] = returnTypeDict[alias]
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700183 for params in command:
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700184 if params.tag == 'param':
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700185 paramtype = ""
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700186 if params.text != None and params.text.strip() != '':
187 paramtype = params.text.strip() + ' '
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700188 typeval = params.find('type')
189 paramtype = paramtype + typeval.text
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700190 if typeval.tail != None:
191 paramtype += typeval.tail.strip() + ' '
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700192 pname = params.find('name')
193 paramname = pname.text
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700194 if pname.tail != None and pname.tail.strip() != '':
195 parametersList.append((paramtype, paramname, pname.tail.strip()))
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700196 else:
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700197 parametersList.append((paramtype, paramname))
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700198 if params.tag == 'proto':
199 for c in params:
200 if c.tag == 'type':
201 fnType = c.text
202 if c.tag == 'name':
203 fnName = c.text
204 protoset = True
205 allCommandsList.append(fnName)
206 returnTypeDict[fnName] = fnType
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700207 if protoset == True:
208 paramDict[fnName] = parametersList.copy()
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700209
210 for exts in root.iter('extensions'):
211 for extension in exts:
212 apiversion = ""
213 if extension.tag == 'extension':
214 extname = extension.get('name')
215 for req in extension:
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700216 if req.get('feature') != None:
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700217 apiversion = req.get('feature')
218 for commands in req:
219 if commands.tag == 'command':
220 commandname = commands.get('name')
221 if commandname not in extensionsDict:
222 extensionsDict[commandname] = extname
223 if apiversion != "":
224 versionDict[commandname] = apiversion
225
226 for feature in root.iter('feature'):
227 apiversion = feature.get('name')
228 for req in feature:
229 for command in req:
230 if command.tag == 'command':
231 cmdName = command.get('name')
232 if cmdName in allCommandsList:
233 versionDict[cmdName] = apiversion
234
235
236def initProc(name, f):
237 if name in extensionsDict:
238 f.write (' INIT_PROC_EXT(' + extensionsDict[name][3:] + ', ')
239 else:
240 f.write (' INIT_PROC(')
241
242 if name in versionDict and versionDict[name] == 'VK_VERSION_1_1':
243 f.write('false, ')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700244 elif name == 'vkGetSwapchainGrallocUsageANDROID' or name == 'vkGetSwapchainGrallocUsage2ANDROID': # optional in vulkan.api
245 f.write('false, ')
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700246 else:
247 f.write('true, ')
248
249 if isInstanceDispatched(name):
250 f.write('instance, ')
251 else:
252 f.write('dev, ')
253
254 f.write(name[2:] + ');\n')
255