Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 1 | #!/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 Zhang | 6ca5d0c | 2019-10-11 17:15:02 -0700 | [diff] [blame] | 16 | |
| 17 | """Generates the null_driver_gen.h and null_driver_gen.cpp. |
| 18 | """ |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 19 | |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 20 | import os |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 21 | import generator_common as gencom |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 22 | |
Yiwei Zhang | 6ca5d0c | 2019-10-11 17:15:02 -0700 | [diff] [blame] | 23 | # Extensions implemented by the driver. |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 24 | _DRIVER_EXTENSION_DICT = { |
| 25 | 'VK_ANDROID_native_buffer', |
| 26 | 'VK_EXT_debug_report', |
| 27 | 'VK_KHR_get_physical_device_properties2' |
| 28 | } |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 29 | |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 30 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 31 | def _is_driver_function(cmd): |
Yiwei Zhang | 6ca5d0c | 2019-10-11 17:15:02 -0700 | [diff] [blame] | 32 | """Returns true if the function is implemented by the driver. |
| 33 | |
| 34 | Args: |
| 35 | cmd: Vulkan function name. |
| 36 | """ |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 37 | if cmd in gencom.extension_dict: |
| 38 | return gencom.extension_dict[cmd] in _DRIVER_EXTENSION_DICT |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 39 | return True |
| 40 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 41 | |
| 42 | def gen_h(): |
Yiwei Zhang | 6ca5d0c | 2019-10-11 17:15:02 -0700 | [diff] [blame] | 43 | """Generates the null_driver_gen.h file. |
| 44 | """ |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 45 | genfile = os.path.join(os.path.dirname(__file__), |
| 46 | '..', 'nulldrv', 'null_driver_gen.h') |
| 47 | |
| 48 | with open(genfile, 'w') as f: |
| 49 | f.write(gencom.copyright_and_warning(2015)) |
| 50 | |
| 51 | f.write("""\ |
| 52 | #ifndef NULLDRV_NULL_DRIVER_H |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 53 | #define NULLDRV_NULL_DRIVER_H 1 |
| 54 | |
| 55 | #include <vulkan/vk_android_native_buffer.h> |
| 56 | #include <vulkan/vulkan.h> |
| 57 | |
Tom Murphy | ea32184 | 2024-06-14 18:26:58 +0000 | [diff] [blame] | 58 | /* |
| 59 | * This file is autogenerated by null_generator.py. Do not edit directly. |
| 60 | */ |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 61 | namespace null_driver { |
| 62 | |
| 63 | PFN_vkVoidFunction GetGlobalProcAddr(const char* name); |
| 64 | PFN_vkVoidFunction GetInstanceProcAddr(const char* name); |
| 65 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 66 | // clang-format off\n""") |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 67 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 68 | for cmd in gencom.command_list: |
| 69 | if _is_driver_function(cmd): |
| 70 | param_list = [''.join(i) for i in gencom.param_dict[cmd]] |
| 71 | f.write('VKAPI_ATTR ' + gencom.return_type_dict[cmd] + ' ' + |
| 72 | gencom.base_name(cmd) + '(' + ', '.join(param_list) + ');\n') |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 73 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 74 | f.write("""\ |
| 75 | // clang-format on |
| 76 | |
| 77 | } // namespace null_driver |
| 78 | |
| 79 | #endif // NULLDRV_NULL_DRIVER_H\n""") |
| 80 | |
Adithya Srinivasan | 8dce9d7 | 2019-07-11 14:26:04 -0700 | [diff] [blame] | 81 | f.close() |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 82 | gencom.run_clang_format(genfile) |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 83 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 84 | |
| 85 | def gen_cpp(): |
Yiwei Zhang | 6ca5d0c | 2019-10-11 17:15:02 -0700 | [diff] [blame] | 86 | """Generates the null_driver_gen.cpp file. |
| 87 | """ |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 88 | genfile = os.path.join(os.path.dirname(__file__), |
| 89 | '..', 'nulldrv', 'null_driver_gen.cpp') |
| 90 | |
| 91 | with open(genfile, 'w') as f: |
| 92 | f.write(gencom.copyright_and_warning(2015)) |
| 93 | |
| 94 | f.write("""\ |
Chris Forbes | 9d84eae | 2024-06-12 12:06:28 +1200 | [diff] [blame] | 95 | #include <android/hardware_buffer.h> |
| 96 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 97 | #include <algorithm> |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 98 | |
| 99 | #include "null_driver_gen.h" |
| 100 | |
| 101 | using namespace null_driver; |
| 102 | |
Tom Murphy | ea32184 | 2024-06-14 18:26:58 +0000 | [diff] [blame] | 103 | /* |
| 104 | * This file is autogenerated by null_generator.py. Do not edit directly. |
| 105 | */ |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 106 | namespace { |
| 107 | |
| 108 | struct NameProc { |
| 109 | const char* name; |
| 110 | PFN_vkVoidFunction proc; |
| 111 | }; |
| 112 | |
| 113 | PFN_vkVoidFunction Lookup(const char* name, |
| 114 | const NameProc* begin, |
| 115 | const NameProc* end) { |
| 116 | const auto& entry = std::lower_bound( |
| 117 | begin, end, name, |
| 118 | [](const NameProc& e, const char* n) { return strcmp(e.name, n) < 0; }); |
| 119 | if (entry == end || strcmp(entry->name, name) != 0) |
| 120 | return nullptr; |
| 121 | return entry->proc; |
| 122 | } |
| 123 | |
| 124 | template <size_t N> |
| 125 | PFN_vkVoidFunction Lookup(const char* name, const NameProc (&procs)[N]) { |
| 126 | return Lookup(name, procs, procs + N); |
| 127 | } |
| 128 | |
| 129 | const NameProc kGlobalProcs[] = { |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 130 | // clang-format off\n""") |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 131 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 132 | sorted_command_list = sorted(gencom.command_list) |
| 133 | for cmd in sorted_command_list: |
| 134 | if (_is_driver_function(cmd) and |
| 135 | gencom.get_dispatch_table_type(cmd) == 'Global'): |
| 136 | f.write(gencom.indent(1) + '{\"' + cmd + |
| 137 | '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' + |
| 138 | cmd + '>(' + gencom.base_name(cmd) + '))},\n') |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 139 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 140 | f.write("""\ |
| 141 | // clang-format on |
| 142 | }; |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 143 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 144 | const NameProc kInstanceProcs[] = { |
| 145 | // clang-format off\n""") |
| 146 | |
| 147 | for cmd in sorted_command_list: |
| 148 | if _is_driver_function(cmd): |
| 149 | f.write(gencom.indent(1) + '{\"' + cmd + |
| 150 | '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' + |
| 151 | cmd + '>(' + gencom.base_name(cmd) + '))},\n') |
| 152 | |
| 153 | f.write("""\ |
| 154 | // clang-format on |
| 155 | }; |
| 156 | |
| 157 | } // namespace |
| 158 | |
| 159 | namespace null_driver { |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 160 | |
| 161 | PFN_vkVoidFunction GetGlobalProcAddr(const char* name) { |
| 162 | return Lookup(name, kGlobalProcs); |
| 163 | } |
| 164 | |
| 165 | PFN_vkVoidFunction GetInstanceProcAddr(const char* name) { |
| 166 | return Lookup(name, kInstanceProcs); |
| 167 | } |
| 168 | |
| 169 | } // namespace null_driver\n""") |
Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 170 | |
Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 171 | f.close() |
| 172 | gencom.run_clang_format(genfile) |