blob: 1db1a3715b19e2884ce02c32220acd42be17a053 [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',
Yiwei Zhange4f64172020-07-05 15:17:32 -070043 'VK_KHR_device_group_creation',
Adithya Srinivasan01364142019-07-02 15:52:49 -070044]
45
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -070046# Functions needed at vulkan::driver level.
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070047_NEEDED_COMMANDS = [
48 # Create functions of dispatchable objects
49 'vkCreateDevice',
50 'vkGetDeviceQueue',
51 'vkGetDeviceQueue2',
52 'vkAllocateCommandBuffers',
53
54 # Destroy functions of dispatchable objects
55 'vkDestroyInstance',
56 'vkDestroyDevice',
57
58 # Enumeration of extensions
59 'vkEnumerateDeviceExtensionProperties',
60
61 # We cache physical devices in loader.cpp
62 'vkEnumeratePhysicalDevices',
63 'vkEnumeratePhysicalDeviceGroups',
64
65 'vkGetInstanceProcAddr',
66 'vkGetDeviceProcAddr',
67
68 'vkQueueSubmit',
69
70 # VK_KHR_swapchain->VK_ANDROID_native_buffer translation
71 'vkCreateImage',
72 'vkDestroyImage',
73
74 'vkGetPhysicalDeviceProperties',
75 'vkGetPhysicalDeviceProperties2',
76 'vkGetPhysicalDeviceProperties2KHR',
77
78 # VK_KHR_swapchain v69 requirement
79 'vkBindImageMemory2',
80 'vkBindImageMemory2KHR',
Yiwei Zhange4f64172020-07-05 15:17:32 -070081
82 # For promoted VK_KHR_device_group_creation
83 'vkEnumeratePhysicalDeviceGroupsKHR',
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070084]
85
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -070086# Functions intercepted at vulkan::driver level.
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070087_INTERCEPTED_COMMANDS = [
88 # Create functions of dispatchable objects
89 'vkCreateInstance',
90 'vkCreateDevice',
91 'vkEnumeratePhysicalDevices',
92 'vkEnumeratePhysicalDeviceGroups',
93 'vkGetDeviceQueue',
94 'vkGetDeviceQueue2',
95 'vkAllocateCommandBuffers',
96
97 # Destroy functions of dispatchable objects
98 'vkDestroyInstance',
99 'vkDestroyDevice',
100
101 # Enumeration of extensions
102 'vkEnumerateInstanceExtensionProperties',
103 'vkEnumerateDeviceExtensionProperties',
104
105 'vkGetInstanceProcAddr',
106 'vkGetDeviceProcAddr',
107
108 'vkQueueSubmit',
109
110 # VK_KHR_swapchain v69 requirement
111 'vkBindImageMemory2',
112 'vkBindImageMemory2KHR',
113]
114
115
116def _is_driver_table_entry(cmd):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700117 """Returns true if a function is needed by vulkan::driver.
118
119 Args:
120 cmd: Vulkan function name.
121 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700122 if gencom.is_function_supported(cmd):
123 if cmd in _NEEDED_COMMANDS:
124 return True
125 if cmd in gencom.extension_dict:
126 if (gencom.extension_dict[cmd] == 'VK_ANDROID_native_buffer' or
127 gencom.extension_dict[cmd] == 'VK_EXT_debug_report'):
128 return True
129 return False
130
131
132def _is_instance_driver_table_entry(cmd):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700133 """Returns true if a instance-dispatched function is needed by vulkan::driver.
134
135 Args:
136 cmd: Vulkan function name.
137 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700138 return (_is_driver_table_entry(cmd) and
139 gencom.is_instance_dispatched(cmd))
140
141
142def _is_device_driver_table_entry(cmd):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700143 """Returns true if a device-dispatched function is needed by vulkan::driver.
144
145 Args:
146 cmd: Vulkan function name.
147 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700148 return (_is_driver_table_entry(cmd) and
149 gencom.is_device_dispatched(cmd))
150
151
152def gen_h():
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700153 """Generates the driver_gen.h file.
154 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700155 genfile = os.path.join(os.path.dirname(__file__),
156 '..', 'libvulkan', 'driver_gen.h')
157
158 with open(genfile, 'w') as f:
159 f.write(gencom.copyright_and_warning(2016))
160
161 f.write("""\
162#ifndef LIBVULKAN_DRIVER_GEN_H
163#define LIBVULKAN_DRIVER_GEN_H
164
165#include <vulkan/vk_android_native_buffer.h>
166#include <vulkan/vulkan.h>
167
168#include <bitset>
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700169#include <optional>
170#include <vector>
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700171
172namespace vulkan {
173namespace driver {
174
175struct ProcHook {
Adithya Srinivasan01364142019-07-02 15:52:49 -0700176 enum Type {
177 GLOBAL,
178 INSTANCE,
179 DEVICE,
180 };
181 enum Extension {\n""")
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700182
Yiwei Zhang5365a7b2019-10-11 17:26:44 -0700183 for ext in _KNOWN_EXTENSIONS:
184 f.write(gencom.indent(2) + gencom.base_ext_name(ext) + ',\n')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700185
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700186 f.write('\n')
187 for version in gencom.version_code_list:
188 f.write(gencom.indent(2) + 'EXTENSION_CORE_' + version + ',\n')
189
190 # EXTENSION_COUNT must be the next enum after the highest API version.
191 f.write("""\
Adithya Srinivasan01364142019-07-02 15:52:49 -0700192 EXTENSION_COUNT,
193 EXTENSION_UNKNOWN,
194 };
195
196 const char* name;
197 Type type;
198 Extension extension;
199
200 PFN_vkVoidFunction proc;
201 PFN_vkVoidFunction checked_proc; // always nullptr for non-device hooks
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700202};
Adithya Srinivasan01364142019-07-02 15:52:49 -0700203
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700204struct InstanceDriverTable {
205 // clang-format off\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700206
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700207 for cmd in gencom.command_list:
208 if _is_instance_driver_table_entry(cmd):
209 f.write(gencom.indent(1) + 'PFN_' + cmd + ' ' +
210 gencom.base_name(cmd) + ';\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700211
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700212 f.write("""\
213 // clang-format on
214};
Adithya Srinivasan01364142019-07-02 15:52:49 -0700215
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700216struct DeviceDriverTable {
217 // clang-format off\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700218
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700219 for cmd in gencom.command_list:
220 if _is_device_driver_table_entry(cmd):
221 f.write(gencom.indent(1) + 'PFN_' + cmd + ' ' +
222 gencom.base_name(cmd) + ';\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700223
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700224 f.write("""\
225 // clang-format on
226};
Adithya Srinivasan01364142019-07-02 15:52:49 -0700227
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700228const ProcHook* GetProcHook(const char* name);
Adithya Srinivasan01364142019-07-02 15:52:49 -0700229ProcHook::Extension GetProcHookExtension(const char* name);
230
231bool InitDriverTable(VkInstance instance,
232 PFN_vkGetInstanceProcAddr get_proc,
233 const std::bitset<ProcHook::EXTENSION_COUNT>& extensions);
234bool InitDriverTable(VkDevice dev,
235 PFN_vkGetDeviceProcAddr get_proc,
236 const std::bitset<ProcHook::EXTENSION_COUNT>& extensions);
237
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700238std::optional<uint32_t> GetInstanceExtensionPromotedVersion(const char* name);
239uint32_t CountPromotedInstanceExtensions(uint32_t begin_version,
240 uint32_t end_version);
241std::vector<const char*> GetPromotedInstanceExtensions(uint32_t begin_version,
242 uint32_t end_version);
243
Adithya Srinivasan01364142019-07-02 15:52:49 -0700244} // namespace driver
245} // namespace vulkan
246
247#endif // LIBVULKAN_DRIVER_TABLE_H\n""")
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700248
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -0700249 f.close()
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700250 gencom.run_clang_format(genfile)
Adithya Srinivasan01364142019-07-02 15:52:49 -0700251
Adithya Srinivasan01364142019-07-02 15:52:49 -0700252
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700253def _is_intercepted(cmd):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700254 """Returns true if a function is intercepted by vulkan::driver.
255
256 Args:
257 cmd: Vulkan function name.
258 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700259 if gencom.is_function_supported(cmd):
260 if cmd in _INTERCEPTED_COMMANDS:
261 return True
Adithya Srinivasan01364142019-07-02 15:52:49 -0700262
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700263 if cmd in gencom.extension_dict:
264 return gencom.extension_dict[cmd] in _INTERCEPTED_EXTENSIONS
Adithya Srinivasan01364142019-07-02 15:52:49 -0700265 return False
266
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700267
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700268def _get_proc_hook_enum(cmd):
269 """Returns the ProcHook enumeration for the corresponding core function.
270
271 Args:
272 cmd: Vulkan function name.
273 """
274 assert cmd in gencom.version_dict
275 for version in gencom.version_code_list:
276 if gencom.version_dict[cmd] == 'VK_VERSION_' + version:
277 return 'ProcHook::EXTENSION_CORE_' + version
278
279
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700280def _need_proc_hook_stub(cmd):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700281 """Returns true if a function needs a ProcHook stub.
282
283 Args:
284 cmd: Vulkan function name.
285 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700286 if _is_intercepted(cmd) and gencom.is_device_dispatched(cmd):
287 if cmd in gencom.extension_dict:
288 if not gencom.is_extension_internal(gencom.extension_dict[cmd]):
Adithya Srinivasan01364142019-07-02 15:52:49 -0700289 return True
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700290 elif gencom.version_dict[cmd] != 'VK_VERSION_1_0':
291 return True
Adithya Srinivasan01364142019-07-02 15:52:49 -0700292 return False
293
Adithya Srinivasan01364142019-07-02 15:52:49 -0700294
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700295def _define_proc_hook_stub(cmd, f):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700296 """Emits a stub for ProcHook::checked_proc.
297
298 Args:
299 cmd: Vulkan function name.
300 f: Output file handle.
301 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700302 if _need_proc_hook_stub(cmd):
303 return_type = gencom.return_type_dict[cmd]
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700304
305 ext_name = ''
306 ext_hook = ''
307 if cmd in gencom.extension_dict:
308 ext_name = gencom.extension_dict[cmd]
309 ext_hook = 'ProcHook::' + gencom.base_ext_name(ext_name)
310 else:
311 ext_name = gencom.version_dict[cmd]
312 ext_hook = _get_proc_hook_enum(cmd)
313
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700314 handle = gencom.param_dict[cmd][0][1]
315 param_types = ', '.join([''.join(i) for i in gencom.param_dict[cmd]])
316 param_names = ', '.join([''.join(i[1]) for i in gencom.param_dict[cmd]])
Adithya Srinivasan01364142019-07-02 15:52:49 -0700317
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700318 f.write('VKAPI_ATTR ' + return_type + ' checked' + gencom.base_name(cmd) +
319 '(' + param_types + ') {\n')
320 f.write(gencom.indent(1) + 'if (GetData(' + handle + ').hook_extensions[' +
321 ext_hook + ']) {\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700322
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700323 f.write(gencom.indent(2))
324 if gencom.return_type_dict[cmd] != 'void':
325 f.write('return ')
326 f.write(gencom.base_name(cmd) + '(' + param_names + ');\n')
327
328 f.write(gencom.indent(1) + '} else {\n')
329 f.write(gencom.indent(2) + 'Logger(' + handle + ').Err(' + handle + ', \"' +
330 ext_name + ' not enabled. ' + cmd + ' not executed.\");\n')
331 if gencom.return_type_dict[cmd] != 'void':
332 f.write(gencom.indent(2) + 'return VK_SUCCESS;\n')
333 f.write(gencom.indent(1) + '}\n}\n\n')
334
335
336def _define_global_proc_hook(cmd, f):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700337 """Emits definition of a global ProcHook.
338
339 Args:
340 cmd: Vulkan function name.
341 f: Output file handle.
342 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700343 assert cmd not in gencom.extension_dict
344
345 f.write(gencom.indent(1) + '{\n')
346 f.write(gencom.indent(2) + '\"' + cmd + '\",\n')
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700347 f.write(gencom.indent(2) + 'ProcHook::GLOBAL,\n')
348 f.write(gencom.indent(2) + _get_proc_hook_enum(cmd) + ',\n')
349 f.write(gencom.indent(2) + 'reinterpret_cast<PFN_vkVoidFunction>(' +
350 gencom.base_name(cmd) + '),\n')
351 f.write(gencom.indent(2) + 'nullptr,\n')
352 f.write(gencom.indent(1) + '},\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700353
Adithya Srinivasan01364142019-07-02 15:52:49 -0700354
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700355def _define_instance_proc_hook(cmd, f):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700356 """Emits definition of a instance ProcHook.
357
358 Args:
359 cmd: Vulkan function name.
360 f: Output file handle.
361 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700362 f.write(gencom.indent(1) + '{\n')
363 f.write(gencom.indent(2) + '\"' + cmd + '\",\n')
364 f.write(gencom.indent(2) + 'ProcHook::INSTANCE,\n')
365
366 if cmd in gencom.extension_dict:
367 ext_name = gencom.extension_dict[cmd]
Yiwei Zhangaeaa8672019-10-16 18:59:41 -0700368 f.write(gencom.indent(2) + 'ProcHook::' +
369 gencom.base_ext_name(ext_name) + ',\n')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700370
371 if gencom.is_extension_internal(ext_name):
372 f.write("""\
373 nullptr,
374 nullptr,\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700375 else:
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700376 f.write("""\
377 reinterpret_cast<PFN_vkVoidFunction>(""" + gencom.base_name(cmd) + """),
378 nullptr,\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700379 else:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700380 f.write(gencom.indent(2) + _get_proc_hook_enum(cmd) + ',\n')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700381 f.write("""\
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700382 reinterpret_cast<PFN_vkVoidFunction>(""" + gencom.base_name(cmd) + """),
Adithya Srinivasan01364142019-07-02 15:52:49 -0700383 nullptr,\n""")
384
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700385 f.write(gencom.indent(1) + '},\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700386
Adithya Srinivasan01364142019-07-02 15:52:49 -0700387
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700388def _define_device_proc_hook(cmd, f):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700389 """Emits definition of a device ProcHook.
390
391 Args:
392 cmd: Vulkan function name.
393 f: Output file handle.
394 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700395 f.write(gencom.indent(1) + '{\n')
396 f.write(gencom.indent(2) + '\"' + cmd + '\",\n')
397 f.write(gencom.indent(2) + 'ProcHook::DEVICE,\n')
398
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700399 if (cmd in gencom.extension_dict or
400 gencom.version_dict[cmd] != 'VK_VERSION_1_0'):
401 ext_name = ''
402 ext_hook = ''
403 if cmd in gencom.extension_dict:
404 ext_name = gencom.extension_dict[cmd]
405 ext_hook = 'ProcHook::' + gencom.base_ext_name(ext_name)
406 else:
407 ext_name = gencom.version_dict[cmd]
408 ext_hook = _get_proc_hook_enum(cmd)
409 f.write(gencom.indent(2) + ext_hook + ',\n')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700410
411 if gencom.is_extension_internal(ext_name):
412 f.write("""\
413 nullptr,
414 nullptr,\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700415 else:
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700416 f.write("""\
417 reinterpret_cast<PFN_vkVoidFunction>(""" + gencom.base_name(cmd) + """),
418 reinterpret_cast<PFN_vkVoidFunction>(checked""" +
419 gencom.base_name(cmd) + '),\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700420
421 else:
Yiwei Zhang7cc36a52019-10-11 19:02:09 -0700422 f.write(gencom.indent(2) + _get_proc_hook_enum(cmd) + ',\n')
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700423 f.write("""\
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700424 reinterpret_cast<PFN_vkVoidFunction>(""" + gencom.base_name(cmd) + """),
Adithya Srinivasan01364142019-07-02 15:52:49 -0700425 nullptr,\n""")
426
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700427 f.write(gencom.indent(1) + '},\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700428
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700429
430def gen_cpp():
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -0700431 """Generates the driver_gen.cpp file.
432 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700433 genfile = os.path.join(os.path.dirname(__file__),
434 '..', 'libvulkan', 'driver_gen.cpp')
435
436 with open(genfile, 'w') as f:
437 f.write(gencom.copyright_and_warning(2016))
438 f.write("""\
439#include <log/log.h>
Adithya Srinivasan01364142019-07-02 15:52:49 -0700440#include <string.h>
441
442#include <algorithm>
443
444#include "driver.h"
445
446namespace vulkan {
447namespace driver {
448
449namespace {
450
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700451// clang-format off\n\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700452
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700453 for cmd in gencom.command_list:
454 _define_proc_hook_stub(cmd, f)
Adithya Srinivasan01364142019-07-02 15:52:49 -0700455
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700456 f.write("""\
457// clang-format on
Adithya Srinivasan01364142019-07-02 15:52:49 -0700458
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700459const ProcHook g_proc_hooks[] = {
460 // clang-format off\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700461
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700462 sorted_command_list = sorted(gencom.command_list)
463 for cmd in sorted_command_list:
464 if _is_intercepted(cmd):
465 if gencom.is_globally_dispatched(cmd):
466 _define_global_proc_hook(cmd, f)
467 elif gencom.is_instance_dispatched(cmd):
468 _define_instance_proc_hook(cmd, f)
469 elif gencom.is_device_dispatched(cmd):
470 _define_device_proc_hook(cmd, f)
Adithya Srinivasan01364142019-07-02 15:52:49 -0700471
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700472 f.write("""\
473 // clang-format on
474};
475
476} // namespace
477
478const ProcHook* GetProcHook(const char* name) {
Adithya Srinivasan01364142019-07-02 15:52:49 -0700479 const auto& begin = g_proc_hooks;
480 const auto& end =
481 g_proc_hooks + sizeof(g_proc_hooks) / sizeof(g_proc_hooks[0]);
482 const auto hook = std::lower_bound(
483 begin, end, name,
484 [](const ProcHook& e, const char* n) { return strcmp(e.name, n) < 0; });
485 return (hook < end && strcmp(hook->name, name) == 0) ? hook : nullptr;
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700486}
Adithya Srinivasan01364142019-07-02 15:52:49 -0700487
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700488ProcHook::Extension GetProcHookExtension(const char* name) {
489 // clang-format off\n""")
Adithya Srinivasan01364142019-07-02 15:52:49 -0700490
Yiwei Zhang5365a7b2019-10-11 17:26:44 -0700491 for ext in _KNOWN_EXTENSIONS:
492 f.write(gencom.indent(1) + 'if (strcmp(name, \"' + ext +
493 '\") == 0) return ProcHook::' + gencom.base_ext_name(ext) + ';\n')
Adithya Srinivasan01364142019-07-02 15:52:49 -0700494
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700495 f.write("""\
496 // clang-format on
497 return ProcHook::EXTENSION_UNKNOWN;
498}
499
500#define UNLIKELY(expr) __builtin_expect((expr), 0)
501
502#define INIT_PROC(required, obj, proc) \\
503 do { \\
504 data.driver.proc = \\
505 reinterpret_cast<PFN_vk##proc>(get_proc(obj, "vk" #proc)); \\
506 if (UNLIKELY(required && !data.driver.proc)) { \\
507 ALOGE("missing " #obj " proc: vk" #proc); \\
508 success = false; \\
509 } \\
510 } while (0)
511
512#define INIT_PROC_EXT(ext, required, obj, proc) \\
513 do { \\
514 if (extensions[ProcHook::ext]) \\
515 INIT_PROC(required, obj, proc); \\
516 } while (0)
517
518bool InitDriverTable(VkInstance instance,
Adithya Srinivasan01364142019-07-02 15:52:49 -0700519 PFN_vkGetInstanceProcAddr get_proc,
520 const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
521 auto& data = GetData(instance);
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700522 bool success = true;
Adithya Srinivasan01364142019-07-02 15:52:49 -0700523
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700524 // clang-format off\n""")
525
526 for cmd in gencom.command_list:
527 if _is_instance_driver_table_entry(cmd):
528 gencom.init_proc(cmd, f)
529
530 f.write("""\
531 // clang-format on
532
533 return success;
534}
535
536bool InitDriverTable(VkDevice dev,
Adithya Srinivasan01364142019-07-02 15:52:49 -0700537 PFN_vkGetDeviceProcAddr get_proc,
538 const std::bitset<ProcHook::EXTENSION_COUNT>& extensions) {
539 auto& data = GetData(dev);
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700540 bool success = true;
541
542 // clang-format off\n""")
543
544 for cmd in gencom.command_list:
545 if _is_device_driver_table_entry(cmd):
546 gencom.init_proc(cmd, f)
547
548 f.write("""\
549 // clang-format on
550
551 return success;
552}
553
Yiwei Zhang7c0c07c2020-07-04 23:49:47 -0700554const std::pair<const char*, uint32_t> g_promoted_instance_extensions[] = {
555 // clang-format off\n""")
556
557 for key, value in sorted(gencom.promoted_inst_ext_dict.items()):
558 f.write(gencom.indent(1) + 'std::make_pair("' + key + '", ' + value + '),\n')
559
560 f.write("""\
561 // clang-format on
562};
563
564std::optional<uint32_t> GetInstanceExtensionPromotedVersion(const char* name) {
565 auto begin = std::cbegin(g_promoted_instance_extensions);
566 auto end = std::cend(g_promoted_instance_extensions);
567 auto iter =
568 std::lower_bound(begin, end, name,
569 [](const std::pair<const char*, uint32_t>& e,
570 const char* n) { return strcmp(e.first, n) < 0; });
571 return (iter < end && strcmp(iter->first, name) == 0)
572 ? std::optional<uint32_t>(iter->second)
573 : std::nullopt;
574}
575
576uint32_t CountPromotedInstanceExtensions(uint32_t begin_version,
577 uint32_t end_version) {
578 auto begin = std::cbegin(g_promoted_instance_extensions);
579 auto end = std::cend(g_promoted_instance_extensions);
580 uint32_t count = 0;
581
582 for (auto iter = begin; iter != end; iter++)
583 if (iter->second > begin_version && iter->second <= end_version)
584 count++;
585
586 return count;
587}
588
589std::vector<const char*> GetPromotedInstanceExtensions(uint32_t begin_version,
590 uint32_t end_version) {
591 auto begin = std::cbegin(g_promoted_instance_extensions);
592 auto end = std::cend(g_promoted_instance_extensions);
593 std::vector<const char*> extensions;
594
595 for (auto iter = begin; iter != end; iter++)
596 if (iter->second > begin_version && iter->second <= end_version)
597 extensions.emplace_back(iter->first);
598
599 return extensions;
600}
601
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700602} // namespace driver
603} // namespace vulkan\n""")
604
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -0700605 f.close()
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700606 gencom.run_clang_format(genfile)