blob: d9f97e176030d4b85aac46056ebe991923d60b59 [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
130 if len(paramDict[functionName])>0:
131 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
153clang_off_spaces = ' '*4
154
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()
169 protoset = False
170 fnName = ""
171 fnType = ""
172 for commands in root.iter('commands'):
173 for command in commands:
174 if command.tag == 'command':
175 if protoset == True:
176 paramDict[fnName] = parametersList.copy()
177 parametersList.clear()
178 protoset = False
179 if command.get('alias') != None:
180 alias = command.get('alias')
181 fnName = command.get('name')
182 aliasDict[fnName] = alias
183 allCommandsList.append(fnName)
184 paramDict[fnName] = paramDict[alias].copy()
Adithya Srinivasan01364142019-07-02 15:52:49 -0700185 returnTypeDict[fnName] = returnTypeDict[alias]
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700186 for params in command:
187 if(params.tag == 'param'):
188 paramtype = ""
189 if params.text!=None:
190 paramtype = params.text
191 typeval = params.find('type')
192 paramtype = paramtype + typeval.text
193 if typeval.tail!=None:
194 paramtype = paramtype + typeval.tail
195 pname = params.find('name')
196 paramname = pname.text
197 if pname.tail != None:
198 parametersList.append((paramtype,paramname,pname.tail))
199 else:
200 parametersList.append((paramtype,paramname))
201 if params.tag == 'proto':
202 for c in params:
203 if c.tag == 'type':
204 fnType = c.text
205 if c.tag == 'name':
206 fnName = c.text
207 protoset = True
208 allCommandsList.append(fnName)
209 returnTypeDict[fnName] = fnType
210
211 for exts in root.iter('extensions'):
212 for extension in exts:
213 apiversion = ""
214 if extension.tag == 'extension':
215 extname = extension.get('name')
216 for req in extension:
217 if req.get('feature')!=None:
218 apiversion = req.get('feature')
219 for commands in req:
220 if commands.tag == 'command':
221 commandname = commands.get('name')
222 if commandname not in extensionsDict:
223 extensionsDict[commandname] = extname
224 if apiversion != "":
225 versionDict[commandname] = apiversion
226
Adithya Srinivasan01364142019-07-02 15:52:49 -0700227 # TODO(adsrini): http://b/136570819
228 extensionsDict['vkGetSwapchainGrallocUsage2ANDROID'] = 'VK_ANDROID_native_buffer'
229 allCommandsList.append('vkGetSwapchainGrallocUsage2ANDROID')
230 returnTypeDict['vkGetSwapchainGrallocUsage2ANDROID'] = 'VkResult'
231 paramDict['vkGetSwapchainGrallocUsage2ANDROID'] = [
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700232 ('VkDevice ', 'device'),
233 ('VkFormat ', 'format'),
234 ('VkImageUsageFlags ', 'imageUsage'),
235 ('VkSwapchainImageUsageFlagsANDROID ', 'swapchainImageUsage'),
236 ('uint64_t* ', 'grallocConsumerUsage'),
237 ('uint64_t* ', 'grallocProducerUsage')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700238 ]
239
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700240 for feature in root.iter('feature'):
241 apiversion = feature.get('name')
242 for req in feature:
243 for command in req:
244 if command.tag == 'command':
245 cmdName = command.get('name')
246 if cmdName in allCommandsList:
247 versionDict[cmdName] = apiversion
248
249
250def initProc(name, f):
251 if name in extensionsDict:
252 f.write (' INIT_PROC_EXT(' + extensionsDict[name][3:] + ', ')
253 else:
254 f.write (' INIT_PROC(')
255
256 if name in versionDict and versionDict[name] == 'VK_VERSION_1_1':
257 f.write('false, ')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700258 elif name == 'vkGetSwapchainGrallocUsageANDROID' or name == 'vkGetSwapchainGrallocUsage2ANDROID': # optional in vulkan.api
259 f.write('false, ')
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700260 else:
261 f.write('true, ')
262
263 if isInstanceDispatched(name):
264 f.write('instance, ')
265 else:
266 f.write('dev, ')
267
268 f.write(name[2:] + ');\n')
269