blob: 03993e21d798c145dfcd0308877e8e68af2e99a1 [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
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070020import os
21import subprocess
22import xml.etree.ElementTree as element_tree
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -070023
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070024_BLACKLISTED_EXTENSIONS = [
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070025 'VK_EXT_acquire_xlib_display',
26 'VK_EXT_direct_mode_display',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070027 'VK_EXT_display_control',
Yiwei Zhangdc792f52019-10-10 16:29:42 -070028 'VK_EXT_display_surface_counter',
29 'VK_EXT_full_screen_exclusive',
30 'VK_EXT_headless_surface',
31 'VK_EXT_metal_surface',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070032 'VK_FUCHSIA_imagepipe_surface',
Yiwei Zhangdc792f52019-10-10 16:29:42 -070033 'VK_GGP_stream_descriptor_surface',
34 'VK_KHR_display',
35 'VK_KHR_display_swapchain',
36 'VK_KHR_external_fence_win32',
37 'VK_KHR_external_memory_win32',
38 'VK_KHR_external_semaphore_win32',
39 'VK_KHR_mir_surface',
40 'VK_KHR_wayland_surface',
41 'VK_KHR_win32_keyed_mutex',
42 'VK_KHR_win32_surface',
43 'VK_KHR_xcb_surface',
44 'VK_KHR_xlib_surface',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070045 'VK_MVK_ios_surface',
46 'VK_MVK_macos_surface',
47 'VK_NN_vi_surface',
Yiwei Zhangdc792f52019-10-10 16:29:42 -070048 'VK_NV_cooperative_matrix',
49 'VK_NV_coverage_reduction_mode',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070050 'VK_NV_external_memory_win32',
51 'VK_NV_win32_keyed_mutex',
Yiwei Zhangdc792f52019-10-10 16:29:42 -070052 'VK_NVX_image_view_handle',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070053]
54
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070055_EXPORTED_EXTENSIONS = [
Yiwei Zhangdc792f52019-10-10 16:29:42 -070056 'VK_ANDROID_external_memory_android_hardware_buffer',
57 'VK_KHR_android_surface',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070058 'VK_KHR_surface',
59 'VK_KHR_swapchain',
Yiwei Zhangdc792f52019-10-10 16:29:42 -070060]
61
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070062_OPTIONAL_COMMANDS = [
Yiwei Zhangdc792f52019-10-10 16:29:42 -070063 'vkGetSwapchainGrallocUsageANDROID',
64 'vkGetSwapchainGrallocUsage2ANDROID',
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -070065]
66
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070067_DISPATCH_TYPE_DICT = {
68 'VkInstance ': 'Instance',
69 'VkPhysicalDevice ': 'Instance',
70 'VkDevice ': 'Device',
71 'VkQueue ': 'Device',
72 'VkCommandBuffer ': 'Device'
73}
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -070074
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070075alias_dict = {}
76command_list = []
77extension_dict = {}
78param_dict = {}
79return_type_dict = {}
80version_dict = {}
Adithya Srinivasan01364142019-07-02 15:52:49 -070081
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070082
83def indent(num):
84 return ' ' * num
85
86
87def copyright_and_warning(year):
88 return """\
89/*
90 * Copyright """ + str(year) + """ The Android Open Source Project
91 *
92 * Licensed under the Apache License, Version 2.0 (the "License");
93 * you may not use this file except in compliance with the License.
94 * You may obtain a copy of the License at
95 *
96 * http://www.apache.org/licenses/LICENSE-2.0
97 *
98 * Unless required by applicable law or agreed to in writing, software
99 * distributed under the License is distributed on an "AS IS" BASIS,
100 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
101 * See the License for the specific language governing permissions and
102 * limitations under the License.
103 */
104
105// WARNING: This file is generated. See ../README.md for instructions.
106
107"""
108
109
110def run_clang_format(args):
111 clang_call = ['clang-format', '--style', 'file', '-i', args]
112 subprocess.check_call(clang_call)
113
114
115def is_extension_internal(extension_name):
116 return extension_name == 'VK_ANDROID_native_buffer'
117
118
119def base_name(cmd):
120 return cmd[2:]
121
122
123def is_function_supported(cmd):
124 if cmd not in extension_dict:
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700125 return True
126 else:
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700127 if extension_dict[cmd] not in _BLACKLISTED_EXTENSIONS:
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700128 return True
129 return False
130
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700131
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700132def get_dispatch_table_type(cmd):
133 if cmd not in param_dict:
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700134 return None
135
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700136 if param_dict[cmd]:
137 return _DISPATCH_TYPE_DICT.get(param_dict[cmd][0][0], 'Global')
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700138 return 'Global'
139
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700140
141def is_globally_dispatched(cmd):
142 return is_function_supported(cmd) and get_dispatch_table_type(cmd) == 'Global'
143
144
145def is_instance_dispatched(cmd):
146 return (is_function_supported(cmd) and
147 get_dispatch_table_type(cmd) == 'Instance')
148
149
150def is_device_dispatched(cmd):
151 return is_function_supported(cmd) and get_dispatch_table_type(cmd) == 'Device'
152
153
154def is_extension_exported(extension_name):
155 return extension_name in _EXPORTED_EXTENSIONS
156
157
158def is_function_exported(cmd):
159 if is_function_supported(cmd):
160 if cmd in extension_dict:
161 return is_extension_exported(extension_dict[cmd])
162 return True
163 return False
164
165
166def is_instance_dispatch_table_entry(cmd):
167 if cmd == 'vkEnumerateDeviceLayerProperties':
168 # deprecated, unused internally - @dbd33bc
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700169 return False
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700170 return is_function_exported(cmd) and is_instance_dispatched(cmd)
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700171
172
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700173def is_device_dispatch_table_entry(cmd):
174 return is_function_exported(cmd) and is_device_dispatched(cmd)
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700175
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700176
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700177def init_proc(name, f):
178 f.write(indent(1))
179 if name in extension_dict:
180 f.write('INIT_PROC_EXT(' + extension_dict[name][3:] + ', ')
181 else:
182 f.write('INIT_PROC(')
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700183
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700184 if name in version_dict and version_dict[name] == 'VK_VERSION_1_1':
185 f.write('false, ')
186 elif name in _OPTIONAL_COMMANDS:
187 f.write('false, ')
188 else:
189 f.write('true, ')
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700190
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700191 if is_instance_dispatched(name):
192 f.write('instance, ')
193 else:
194 f.write('dev, ')
195
196 f.write(base_name(name) + ');\n')
197
198
199def parse_vulkan_registry():
200 registry = os.path.join(os.path.dirname(__file__), '..', '..', '..', '..',
201 'external', 'vulkan-headers', 'registry', 'vk.xml')
202 tree = element_tree.parse(registry)
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700203 root = tree.getroot()
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700204 for commands in root.iter('commands'):
205 for command in commands:
206 if command.tag == 'command':
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700207 parameter_list = []
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700208 protoset = False
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700209 cmd_name = ''
210 cmd_type = ''
211 if command.get('alias') is not None:
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700212 alias = command.get('alias')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700213 cmd_name = command.get('name')
214 alias_dict[cmd_name] = alias
215 command_list.append(cmd_name)
216 param_dict[cmd_name] = param_dict[alias].copy()
217 return_type_dict[cmd_name] = return_type_dict[alias]
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700218 for params in command:
Yiwei Zhang4bc489b2019-09-23 15:17:22 -0700219 if params.tag == 'param':
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700220 param_type = ''
221 if params.text is not None and params.text.strip():
222 param_type = params.text.strip() + ' '
223 type_val = params.find('type')
224 param_type = param_type + type_val.text
225 if type_val.tail is not None:
226 param_type += type_val.tail.strip() + ' '
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700227 pname = params.find('name')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700228 param_name = pname.text
229 if pname.tail is not None and pname.tail.strip():
230 parameter_list.append(
231 (param_type, param_name, pname.tail.strip()))
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700232 else:
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700233 parameter_list.append((param_type, param_name))
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700234 if params.tag == 'proto':
235 for c in params:
236 if c.tag == 'type':
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700237 cmd_type = c.text
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700238 if c.tag == 'name':
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700239 cmd_name = c.text
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700240 protoset = True
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700241 command_list.append(cmd_name)
242 return_type_dict[cmd_name] = cmd_type
243 if protoset:
244 param_dict[cmd_name] = parameter_list.copy()
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700245
246 for exts in root.iter('extensions'):
247 for extension in exts:
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700248 apiversion = ''
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700249 if extension.tag == 'extension':
250 extname = extension.get('name')
251 for req in extension:
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700252 if req.get('feature') is not None:
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700253 apiversion = req.get('feature')
254 for commands in req:
255 if commands.tag == 'command':
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700256 cmd_name = commands.get('name')
257 if cmd_name not in extension_dict:
258 extension_dict[cmd_name] = extname
259 if apiversion:
260 version_dict[cmd_name] = apiversion
Adithya Srinivasan751a7dc2019-07-02 17:17:25 -0700261
262 for feature in root.iter('feature'):
263 apiversion = feature.get('name')
264 for req in feature:
265 for command in req:
266 if command.tag == 'command':
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700267 cmd_name = command.get('name')
268 if cmd_name in command_list:
269 version_dict[cmd_name] = apiversion