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