blob: 5e78c62df8c876848f80720ee4199ae5ccb2ad7d [file] [log] [blame]
Adithya Srinivasan01364142019-07-02 15:52:49 -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.
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -070016
17"""Generates the driver_gen.h and driver_gen.cpp.
18"""
Adithya Srinivasan01364142019-07-02 15:52:49 -070019
Adithya Srinivasan01364142019-07-02 15:52:49 -070020import os
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070021import generator_common as gencom
Adithya Srinivasan01364142019-07-02 15:52:49 -070022
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -070023# Extensions intercepted at vulkan::driver level.
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070024_INTERCEPTED_EXTENSIONS = [
25 'VK_ANDROID_native_buffer',
26 'VK_EXT_debug_report',
27 'VK_EXT_hdr_metadata',
28 'VK_EXT_swapchain_colorspace',
29 'VK_GOOGLE_display_timing',
30 'VK_KHR_android_surface',
31 'VK_KHR_get_surface_capabilities2',
32 'VK_KHR_incremental_present',
33 'VK_KHR_shared_presentable_image',
34 'VK_KHR_surface',
35 'VK_KHR_swapchain',
Adithya Srinivasan01364142019-07-02 15:52:49 -070036]
37
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -070038# Extensions known to vulkan::driver level.
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070039_KNOWN_EXTENSIONS = _INTERCEPTED_EXTENSIONS + [
40 'VK_ANDROID_external_memory_android_hardware_buffer',
41 'VK_KHR_bind_memory2',
42 'VK_KHR_get_physical_device_properties2',
Adithya Srinivasan01364142019-07-02 15:52:49 -070043]
44
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -070045# Functions needed at vulkan::driver level.
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070046_NEEDED_COMMANDS = [
47 # Create functions of dispatchable objects
48 'vkCreateDevice',
49 'vkGetDeviceQueue',
50 'vkGetDeviceQueue2',
51 'vkAllocateCommandBuffers',
52
53 # Destroy functions of dispatchable objects
54 'vkDestroyInstance',
55 'vkDestroyDevice',
56
57 # Enumeration of extensions
58 'vkEnumerateDeviceExtensionProperties',
59
60 # We cache physical devices in loader.cpp
61 'vkEnumeratePhysicalDevices',
62 'vkEnumeratePhysicalDeviceGroups',
63
64 'vkGetInstanceProcAddr',
65 'vkGetDeviceProcAddr',
66
67 'vkQueueSubmit',
68
69 # VK_KHR_swapchain->VK_ANDROID_native_buffer translation
70 'vkCreateImage',
71 'vkDestroyImage',
72
73 'vkGetPhysicalDeviceProperties',
74 'vkGetPhysicalDeviceProperties2',
75 'vkGetPhysicalDeviceProperties2KHR',
76
77 # VK_KHR_swapchain v69 requirement
78 'vkBindImageMemory2',
79 'vkBindImageMemory2KHR',
80]
81
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -070082# Functions intercepted at vulkan::driver level.
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070083_INTERCEPTED_COMMANDS = [
84 # Create functions of dispatchable objects
85 'vkCreateInstance',
86 'vkCreateDevice',
87 'vkEnumeratePhysicalDevices',
88 'vkEnumeratePhysicalDeviceGroups',
89 'vkGetDeviceQueue',
90 'vkGetDeviceQueue2',
91 'vkAllocateCommandBuffers',
92
93 # Destroy functions of dispatchable objects
94 'vkDestroyInstance',
95 'vkDestroyDevice',
96
97 # Enumeration of extensions
98 'vkEnumerateInstanceExtensionProperties',
99 'vkEnumerateDeviceExtensionProperties',
100
101 'vkGetInstanceProcAddr',
102 'vkGetDeviceProcAddr',
103
104 'vkQueueSubmit',
105
106 # VK_KHR_swapchain v69 requirement
107 'vkBindImageMemory2',
108 'vkBindImageMemory2KHR',
109]
110
111
112def _is_driver_table_entry(cmd):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700113 """Returns true if a function is needed by vulkan::driver.
114
115 Args:
116 cmd: Vulkan function name.
117 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700118 if gencom.is_function_supported(cmd):
119 if cmd in _NEEDED_COMMANDS:
120 return True
121 if cmd in gencom.extension_dict:
122 if (gencom.extension_dict[cmd] == 'VK_ANDROID_native_buffer' or
123 gencom.extension_dict[cmd] == 'VK_EXT_debug_report'):
124 return True
125 return False
126
127
128def _is_instance_driver_table_entry(cmd):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700129 """Returns true if a instance-dispatched function is needed by vulkan::driver.
130
131 Args:
132 cmd: Vulkan function name.
133 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700134 return (_is_driver_table_entry(cmd) and
135 gencom.is_instance_dispatched(cmd))
136
137
138def _is_device_driver_table_entry(cmd):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700139 """Returns true if a device-dispatched function is needed by vulkan::driver.
140
141 Args:
142 cmd: Vulkan function name.
143 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700144 return (_is_driver_table_entry(cmd) and
145 gencom.is_device_dispatched(cmd))
146
147
148def gen_h():
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700149 """Generates the driver_gen.h file.
150 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700151 genfile = os.path.join(os.path.dirname(__file__),
152 '..', 'libvulkan', 'driver_gen.h')
153
154 with open(genfile, 'w') as f:
155 f.write(gencom.copyright_and_warning(2016))
156
157 f.write("""\
158#ifndef LIBVULKAN_DRIVER_GEN_H
159#define LIBVULKAN_DRIVER_GEN_H
160
161#include <vulkan/vk_android_native_buffer.h>
162#include <vulkan/vulkan.h>
163
164#include <bitset>
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700165#include <optional>
166#include <vector>
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700167
168namespace vulkan {
169namespace driver {
170
171struct ProcHook {
Adithya Srinivasan01364142019-07-02 15:52:49 -0700172 enum Type {
173 GLOBAL,
174 INSTANCE,
175 DEVICE,
176 };
177 enum Extension {\n""")
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700178
Yiwei Zhang5365a7b2019-10-11 17:26:44 -0700179 for ext in _KNOWN_EXTENSIONS:
180 f.write(gencom.indent(2) + gencom.base_ext_name(ext) + ',\n')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700181
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700182 f.write('\n')
183 for version in gencom.version_code_list:
184 f.write(gencom.indent(2) + 'EXTENSION_CORE_' + version + ',\n')
185
186 # EXTENSION_COUNT must be the next enum after the highest API version.
187 f.write("""\
Adithya Srinivasan01364142019-07-02 15:52:49 -0700188 EXTENSION_COUNT,
189 EXTENSION_UNKNOWN,
190 };
191
192 const char* name;
193 Type type;
194 Extension extension;
195
196 PFN_vkVoidFunction proc;
197 PFN_vkVoidFunction checked_proc; // always nullptr for non-device hooks
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700198};
Adithya Srinivasan01364142019-07-02 15:52:49 -0700199
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700200struct InstanceDriverTable {
201 // clang-format off\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700202
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700203 for cmd in gencom.command_list:
204 if _is_instance_driver_table_entry(cmd):
205 f.write(gencom.indent(1) + 'PFN_' + cmd + ' ' +
206 gencom.base_name(cmd) + ';\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700207
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700208 f.write("""\
209 // clang-format on
210};
Adithya Srinivasan01364142019-07-02 15:52:49 -0700211
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700212struct DeviceDriverTable {
213 // clang-format off\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700214
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700215 for cmd in gencom.command_list:
216 if _is_device_driver_table_entry(cmd):
217 f.write(gencom.indent(1) + 'PFN_' + cmd + ' ' +
218 gencom.base_name(cmd) + ';\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700219
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700220 f.write("""\
221 // clang-format on
222};
Adithya Srinivasan01364142019-07-02 15:52:49 -0700223
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700224const ProcHook* GetProcHook(const char* name);
Adithya Srinivasan01364142019-07-02 15:52:49 -0700225ProcHook::Extension GetProcHookExtension(const char* name);
226
227bool InitDriverTable(VkInstance instance,
228 PFN_vkGetInstanceProcAddr get_proc,
229 const std::bitset<ProcHook::EXTENSION_COUNT>& extensions);
230bool InitDriverTable(VkDevice dev,
231 PFN_vkGetDeviceProcAddr get_proc,
232 const std::bitset<ProcHook::EXTENSION_COUNT>& extensions);
233
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700234std::optional<uint32_t> GetInstanceExtensionPromotedVersion(const char* name);
235uint32_t CountPromotedInstanceExtensions(uint32_t begin_version,
236 uint32_t end_version);
237std::vector<const char*> GetPromotedInstanceExtensions(uint32_t begin_version,
238 uint32_t end_version);
239
Adithya Srinivasan01364142019-07-02 15:52:49 -0700240} // namespace driver
241} // namespace vulkan
242
243#endif // LIBVULKAN_DRIVER_TABLE_H\n""")
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700244
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -0700245 f.close()
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700246 gencom.run_clang_format(genfile)
Adithya Srinivasan01364142019-07-02 15:52:49 -0700247
Adithya Srinivasan01364142019-07-02 15:52:49 -0700248
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700249def _is_intercepted(cmd):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700250 """Returns true if a function is intercepted by vulkan::driver.
251
252 Args:
253 cmd: Vulkan function name.
254 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700255 if gencom.is_function_supported(cmd):
256 if cmd in _INTERCEPTED_COMMANDS:
257 return True
Adithya Srinivasan01364142019-07-02 15:52:49 -0700258
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700259 if cmd in gencom.extension_dict:
260 return gencom.extension_dict[cmd] in _INTERCEPTED_EXTENSIONS
Adithya Srinivasan01364142019-07-02 15:52:49 -0700261 return False
262
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700263
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700264def _get_proc_hook_enum(cmd):
265 """Returns the ProcHook enumeration for the corresponding core function.
266
267 Args:
268 cmd: Vulkan function name.
269 """
270 assert cmd in gencom.version_dict
271 for version in gencom.version_code_list:
272 if gencom.version_dict[cmd] == 'VK_VERSION_' + version:
273 return 'ProcHook::EXTENSION_CORE_' + version
274
275
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700276def _need_proc_hook_stub(cmd):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700277 """Returns true if a function needs a ProcHook stub.
278
279 Args:
280 cmd: Vulkan function name.
281 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700282 if _is_intercepted(cmd) and gencom.is_device_dispatched(cmd):
283 if cmd in gencom.extension_dict:
284 if not gencom.is_extension_internal(gencom.extension_dict[cmd]):
Adithya Srinivasan01364142019-07-02 15:52:49 -0700285 return True
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700286 elif gencom.version_dict[cmd] != 'VK_VERSION_1_0':
287 return True
Adithya Srinivasan01364142019-07-02 15:52:49 -0700288 return False
289
Adithya Srinivasan01364142019-07-02 15:52:49 -0700290
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700291def _define_proc_hook_stub(cmd, f):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700292 """Emits a stub for ProcHook::checked_proc.
293
294 Args:
295 cmd: Vulkan function name.
296 f: Output file handle.
297 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700298 if _need_proc_hook_stub(cmd):
299 return_type = gencom.return_type_dict[cmd]
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700300
301 ext_name = ''
302 ext_hook = ''
303 if cmd in gencom.extension_dict:
304 ext_name = gencom.extension_dict[cmd]
305 ext_hook = 'ProcHook::' + gencom.base_ext_name(ext_name)
306 else:
307 ext_name = gencom.version_dict[cmd]
308 ext_hook = _get_proc_hook_enum(cmd)
309
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700310 handle = gencom.param_dict[cmd][0][1]
311 param_types = ', '.join([''.join(i) for i in gencom.param_dict[cmd]])
312 param_names = ', '.join([''.join(i[1]) for i in gencom.param_dict[cmd]])
Adithya Srinivasan01364142019-07-02 15:52:49 -0700313
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700314 f.write('VKAPI_ATTR ' + return_type + ' checked' + gencom.base_name(cmd) +
315 '(' + param_types + ') {\n')
316 f.write(gencom.indent(1) + 'if (GetData(' + handle + ').hook_extensions[' +
317 ext_hook + ']) {\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700318
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700319 f.write(gencom.indent(2))
320 if gencom.return_type_dict[cmd] != 'void':
321 f.write('return ')
322 f.write(gencom.base_name(cmd) + '(' + param_names + ');\n')
323
324 f.write(gencom.indent(1) + '} else {\n')
325 f.write(gencom.indent(2) + 'Logger(' + handle + ').Err(' + handle + ', \"' +
326 ext_name + ' not enabled. ' + cmd + ' not executed.\");\n')
327 if gencom.return_type_dict[cmd] != 'void':
328 f.write(gencom.indent(2) + 'return VK_SUCCESS;\n')
329 f.write(gencom.indent(1) + '}\n}\n\n')
330
331
332def _define_global_proc_hook(cmd, f):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700333 """Emits definition of a global ProcHook.
334
335 Args:
336 cmd: Vulkan function name.
337 f: Output file handle.
338 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700339 assert cmd not in gencom.extension_dict
340
341 f.write(gencom.indent(1) + '{\n')
342 f.write(gencom.indent(2) + '\"' + cmd + '\",\n')
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700343 f.write(gencom.indent(2) + 'ProcHook::GLOBAL,\n')
344 f.write(gencom.indent(2) + _get_proc_hook_enum(cmd) + ',\n')
345 f.write(gencom.indent(2) + 'reinterpret_cast<PFN_vkVoidFunction>(' +
346 gencom.base_name(cmd) + '),\n')
347 f.write(gencom.indent(2) + 'nullptr,\n')
348 f.write(gencom.indent(1) + '},\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700349
Adithya Srinivasan01364142019-07-02 15:52:49 -0700350
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700351def _define_instance_proc_hook(cmd, f):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700352 """Emits definition of a instance ProcHook.
353
354 Args:
355 cmd: Vulkan function name.
356 f: Output file handle.
357 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700358 f.write(gencom.indent(1) + '{\n')
359 f.write(gencom.indent(2) + '\"' + cmd + '\",\n')
360 f.write(gencom.indent(2) + 'ProcHook::INSTANCE,\n')
361
362 if cmd in gencom.extension_dict:
363 ext_name = gencom.extension_dict[cmd]
Yiwei Zhangaeaa8672019-10-16 18:59:41 -0700364 f.write(gencom.indent(2) + 'ProcHook::' +
365 gencom.base_ext_name(ext_name) + ',\n')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700366
367 if gencom.is_extension_internal(ext_name):
368 f.write("""\
369 nullptr,
370 nullptr,\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700371 else:
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700372 f.write("""\
373 reinterpret_cast<PFN_vkVoidFunction>(""" + gencom.base_name(cmd) + """),
374 nullptr,\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700375 else:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700376 f.write(gencom.indent(2) + _get_proc_hook_enum(cmd) + ',\n')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700377 f.write("""\
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700378 reinterpret_cast<PFN_vkVoidFunction>(""" + gencom.base_name(cmd) + """),
Adithya Srinivasan01364142019-07-02 15:52:49 -0700379 nullptr,\n""")
380
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700381 f.write(gencom.indent(1) + '},\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700382
Adithya Srinivasan01364142019-07-02 15:52:49 -0700383
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700384def _define_device_proc_hook(cmd, f):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700385 """Emits definition of a device ProcHook.
386
387 Args:
388 cmd: Vulkan function name.
389 f: Output file handle.
390 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700391 f.write(gencom.indent(1) + '{\n')
392 f.write(gencom.indent(2) + '\"' + cmd + '\",\n')
393 f.write(gencom.indent(2) + 'ProcHook::DEVICE,\n')
394
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700395 if (cmd in gencom.extension_dict or
396 gencom.version_dict[cmd] != 'VK_VERSION_1_0'):
397 ext_name = ''
398 ext_hook = ''
399 if cmd in gencom.extension_dict:
400 ext_name = gencom.extension_dict[cmd]
401 ext_hook = 'ProcHook::' + gencom.base_ext_name(ext_name)
402 else:
403 ext_name = gencom.version_dict[cmd]
404 ext_hook = _get_proc_hook_enum(cmd)
405 f.write(gencom.indent(2) + ext_hook + ',\n')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700406
407 if gencom.is_extension_internal(ext_name):
408 f.write("""\
409 nullptr,
410 nullptr,\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700411 else:
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700412 f.write("""\
413 reinterpret_cast<PFN_vkVoidFunction>(""" + gencom.base_name(cmd) + """),
414 reinterpret_cast<PFN_vkVoidFunction>(checked""" +
415 gencom.base_name(cmd) + '),\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700416
417 else:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700418 f.write(gencom.indent(2) + _get_proc_hook_enum(cmd) + ',\n')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700419 f.write("""\
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700420 reinterpret_cast<PFN_vkVoidFunction>(""" + gencom.base_name(cmd) + """),
Adithya Srinivasan01364142019-07-02 15:52:49 -0700421 nullptr,\n""")
422
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700423 f.write(gencom.indent(1) + '},\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700424
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700425
426def gen_cpp():
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700427 """Generates the driver_gen.cpp file.
428 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700429 genfile = os.path.join(os.path.dirname(__file__),
430 '..', 'libvulkan', 'driver_gen.cpp')
431
432 with open(genfile, 'w') as f:
433 f.write(gencom.copyright_and_warning(2016))
434 f.write("""\
435#include <log/log.h>
Adithya Srinivasan01364142019-07-02 15:52:49 -0700436#include <string.h>
437
438#include <algorithm>
439
440#include "driver.h"
441
442namespace vulkan {
443namespace driver {
444
445namespace {
446
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700447// clang-format off\n\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700448
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700449 for cmd in gencom.command_list:
450 _define_proc_hook_stub(cmd, f)
Adithya Srinivasan01364142019-07-02 15:52:49 -0700451
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700452 f.write("""\
453// clang-format on
Adithya Srinivasan01364142019-07-02 15:52:49 -0700454
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700455const ProcHook g_proc_hooks[] = {
456 // clang-format off\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700457
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700458 sorted_command_list = sorted(gencom.command_list)
459 for cmd in sorted_command_list:
460 if _is_intercepted(cmd):
461 if gencom.is_globally_dispatched(cmd):
462 _define_global_proc_hook(cmd, f)
463 elif gencom.is_instance_dispatched(cmd):
464 _define_instance_proc_hook(cmd, f)
465 elif gencom.is_device_dispatched(cmd):
466 _define_device_proc_hook(cmd, f)
Adithya Srinivasan01364142019-07-02 15:52:49 -0700467
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700468 f.write("""\
469 // clang-format on
470};
471
472} // namespace
473
474const ProcHook* GetProcHook(const char* name) {
Adithya Srinivasan01364142019-07-02 15:52:49 -0700475 const auto& begin = g_proc_hooks;
476 const auto& end =
477 g_proc_hooks + sizeof(g_proc_hooks) / sizeof(g_proc_hooks[0]);
478 const auto hook = std::lower_bound(
479 begin, end, name,
480 [](const ProcHook& e, const char* n) { return strcmp(e.name, n) < 0; });
481 return (hook < end && strcmp(hook->name, name) == 0) ? hook : nullptr;
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700482}
Adithya Srinivasan01364142019-07-02 15:52:49 -0700483
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700484ProcHook::Extension GetProcHookExtension(const char* name) {
485 // clang-format off\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700486
Yiwei Zhang5365a7b2019-10-11 17:26:44 -0700487 for ext in _KNOWN_EXTENSIONS:
488 f.write(gencom.indent(1) + 'if (strcmp(name, \"' + ext +
489 '\") == 0) return ProcHook::' + gencom.base_ext_name(ext) + ';\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700490
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700491 f.write("""\
492 // clang-format on
493 return ProcHook::EXTENSION_UNKNOWN;
494}
495
496#define UNLIKELY(expr) __builtin_expect((expr), 0)
497
498#define INIT_PROC(required, obj, proc) \\
499 do { \\
500 data.driver.proc = \\
501 reinterpret_cast<PFN_vk##proc>(get_proc(obj, "vk" #proc)); \\
502 if (UNLIKELY(required && !data.driver.proc)) { \\
503 ALOGE("missing " #obj " proc: vk" #proc); \\
504 success = false; \\
505 } \\
506 } while (0)
507
508#define INIT_PROC_EXT(ext, required, obj, proc) \\
509 do { \\
510 if (extensions[ProcHook::ext]) \\
511 INIT_PROC(required, obj, proc); \\
512 } while (0)
513
514bool InitDriverTable(VkInstance instance,
Adithya Srinivasan01364142019-07-02 15:52:49 -0700515 PFN_vkGetInstanceProcAddr get_proc,
516 const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
517 auto& data = GetData(instance);
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700518 bool success = true;
Adithya Srinivasan01364142019-07-02 15:52:49 -0700519
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700520 // clang-format off\n""")
521
522 for cmd in gencom.command_list:
523 if _is_instance_driver_table_entry(cmd):
524 gencom.init_proc(cmd, f)
525
526 f.write("""\
527 // clang-format on
528
529 return success;
530}
531
532bool InitDriverTable(VkDevice dev,
Adithya Srinivasan01364142019-07-02 15:52:49 -0700533 PFN_vkGetDeviceProcAddr get_proc,
534 const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
535 auto& data = GetData(dev);
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700536 bool success = true;
537
538 // clang-format off\n""")
539
540 for cmd in gencom.command_list:
541 if _is_device_driver_table_entry(cmd):
542 gencom.init_proc(cmd, f)
543
544 f.write("""\
545 // clang-format on
546
547 return success;
548}
549
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700550const std::pair<const char*, uint32_t> g_promoted_instance_extensions[] = {
551 // clang-format off\n""")
552
553 for key, value in sorted(gencom.promoted_inst_ext_dict.items()):
554 f.write(gencom.indent(1) + 'std::make_pair("' + key + '", ' + value + '),\n')
555
556 f.write("""\
557 // clang-format on
558};
559
560std::optional<uint32_t> GetInstanceExtensionPromotedVersion(const char* name) {
561 auto begin = std::cbegin(g_promoted_instance_extensions);
562 auto end = std::cend(g_promoted_instance_extensions);
563 auto iter =
564 std::lower_bound(begin, end, name,
565 [](const std::pair<const char*, uint32_t>& e,
566 const char* n) { return strcmp(e.first, n) < 0; });
567 return (iter < end && strcmp(iter->first, name) == 0)
568 ? std::optional<uint32_t>(iter->second)
569 : std::nullopt;
570}
571
572uint32_t CountPromotedInstanceExtensions(uint32_t begin_version,
573 uint32_t end_version) {
574 auto begin = std::cbegin(g_promoted_instance_extensions);
575 auto end = std::cend(g_promoted_instance_extensions);
576 uint32_t count = 0;
577
578 for (auto iter = begin; iter != end; iter++)
579 if (iter->second > begin_version && iter->second <= end_version)
580 count++;
581
582 return count;
583}
584
585std::vector<const char*> GetPromotedInstanceExtensions(uint32_t begin_version,
586 uint32_t end_version) {
587 auto begin = std::cbegin(g_promoted_instance_extensions);
588 auto end = std::cend(g_promoted_instance_extensions);
589 std::vector<const char*> extensions;
590
591 for (auto iter = begin; iter != end; iter++)
592 if (iter->second > begin_version && iter->second <= end_version)
593 extensions.emplace_back(iter->first);
594
595 return extensions;
596}
597
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700598} // namespace driver
599} // namespace vulkan\n""")
600
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -0700601 f.close()
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700602 gencom.run_clang_format(genfile)