blob: f1f09d59e54c8818d37f4c1443b08e62d9255140 [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 = [
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070043 'VK_EXT_acquire_xlib_display',
44 'VK_EXT_direct_mode_display',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070045 'VK_EXT_display_control',
Yiwei Zhangdc792f52019-10-10 16:29:42 -070046 'VK_EXT_display_surface_counter',
47 'VK_EXT_full_screen_exclusive',
48 'VK_EXT_headless_surface',
49 'VK_EXT_metal_surface',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070050 'VK_FUCHSIA_imagepipe_surface',
Yiwei Zhangdc792f52019-10-10 16:29:42 -070051 '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 Srinivasan751a7dc2019-07-02 17:17:25 -070063 'VK_MVK_ios_surface',
64 'VK_MVK_macos_surface',
65 'VK_NN_vi_surface',
Yiwei Zhangdc792f52019-10-10 16:29:42 -070066 'VK_NV_cooperative_matrix',
67 'VK_NV_coverage_reduction_mode',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070068 'VK_NV_external_memory_win32',
69 'VK_NV_win32_keyed_mutex',
Yiwei Zhangdc792f52019-10-10 16:29:42 -070070 'VK_NVX_image_view_handle',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070071]
72
73exportedExtensions = [
Yiwei Zhangdc792f52019-10-10 16:29:42 -070074 'VK_ANDROID_external_memory_android_hardware_buffer',
75 'VK_KHR_android_surface',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070076 'VK_KHR_surface',
77 'VK_KHR_swapchain',
Yiwei Zhangdc792f52019-10-10 16:29:42 -070078]
79
80optionalCommands = [
81 'vkGetSwapchainGrallocUsageANDROID',
82 'vkGetSwapchainGrallocUsage2ANDROID',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070083]
84
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -070085def runClangFormat(args):
86 clang_call = ["clang-format", "--style", "file", "-i", args]
87 check_call (clang_call)
88
Adithya Srinivasan01364142019-07-02 15:52:49 -070089def isExtensionInternal(extensionName):
90 if extensionName == 'VK_ANDROID_native_buffer':
91 return True
92 return False
93
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070094def 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
102def isInstanceDispatched(functionName):
103 return isFunctionSupported(functionName) and getDispatchTableType(functionName) == 'Instance'
104
105def isDeviceDispatched(functionName):
106 return isFunctionSupported(functionName) and getDispatchTableType(functionName) == 'Device'
107
108def isGloballyDispatched(functionName):
109 return isFunctionSupported(functionName) and getDispatchTableType(functionName) == 'Global'
110
111def isExtensionExported(extensionName):
112 if extensionName in exportedExtensions:
113 return True
114 return False
115
116def isFunctionExported(functionName):
117 if isFunctionSupported(functionName):
118 if functionName in extensionsDict:
119 return isExtensionExported(extensionsDict[functionName])
120 return True
121 return False
122
123def 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 Zhang4bc489b2019-09-23 15:17:22 -0700135 if len(paramDict[functionName]) > 0:
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700136 return switchCase.get(paramDict[functionName][0][0], 'Global')
137 return 'Global'
138
139def 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
146def isDeviceDispatchTableEntry(functionName):
147 if isFunctionExported(functionName) and isDeviceDispatched(functionName):
148 return True
149 return False
150
151
152def clang_on(f, indent):
153 f.write (clang_off_spaces * indent + '// clang-format on\n')
154
155def clang_off(f, indent):
156 f.write (clang_off_spaces * indent + '// clang-format off\n')
157
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700158clang_off_spaces = ' ' * 4
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700159
160parametersList = []
161paramDict = {}
162allCommandsList = []
163extensionsDict = {}
164returnTypeDict = {}
165versionDict = {}
166aliasDict = {}
167
168def 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 Srinivasan751a7dc2019-07-02 17:17:25 -0700174 for commands in root.iter('commands'):
175 for command in commands:
176 if command.tag == 'command':
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700177 parametersList.clear()
178 protoset = False
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700179 fnName = ""
180 fnType = ""
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700181 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 Srinivasan01364142019-07-02 15:52:49 -0700187 returnTypeDict[fnName] = returnTypeDict[alias]
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700188 for params in command:
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700189 if params.tag == 'param':
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700190 paramtype = ""
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700191 if params.text != None and params.text.strip() != '':
192 paramtype = params.text.strip() + ' '
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700193 typeval = params.find('type')
194 paramtype = paramtype + typeval.text
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700195 if typeval.tail != None:
196 paramtype += typeval.tail.strip() + ' '
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700197 pname = params.find('name')
198 paramname = pname.text
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700199 if pname.tail != None and pname.tail.strip() != '':
200 parametersList.append((paramtype, paramname, pname.tail.strip()))
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700201 else:
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700202 parametersList.append((paramtype, paramname))
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700203 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 Zhang4bc489b2019-09-23 15:17:22 -0700212 if protoset == True:
213 paramDict[fnName] = parametersList.copy()
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700214
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 Zhang4bc489b2019-09-23 15:17:22 -0700221 if req.get('feature') != None:
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700222 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
241def 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 Zhangdc792f52019-10-10 16:29:42 -0700249 elif name in optionalCommands:
Adithya Srinivasan01364142019-07-02 15:52:49 -0700250 f.write('false, ')
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700251 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