| 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 |  | 
|  | 58 | namespace null_driver { | 
|  | 59 |  | 
|  | 60 | PFN_vkVoidFunction GetGlobalProcAddr(const char* name); | 
|  | 61 | PFN_vkVoidFunction GetInstanceProcAddr(const char* name); | 
|  | 62 |  | 
| Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 63 | // clang-format off\n""") | 
| Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 64 |  | 
| Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 65 | for cmd in gencom.command_list: | 
|  | 66 | if _is_driver_function(cmd): | 
|  | 67 | param_list = [''.join(i) for i in gencom.param_dict[cmd]] | 
|  | 68 | f.write('VKAPI_ATTR ' + gencom.return_type_dict[cmd] + ' ' + | 
|  | 69 | gencom.base_name(cmd) + '(' + ', '.join(param_list) + ');\n') | 
| Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 70 |  | 
| Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 71 | f.write("""\ | 
|  | 72 | // clang-format on | 
|  | 73 |  | 
|  | 74 | }  // namespace null_driver | 
|  | 75 |  | 
|  | 76 | #endif  // NULLDRV_NULL_DRIVER_H\n""") | 
|  | 77 |  | 
| Adithya Srinivasan | 8dce9d7 | 2019-07-11 14:26:04 -0700 | [diff] [blame] | 78 | f.close() | 
| Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 79 | gencom.run_clang_format(genfile) | 
| Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 80 |  | 
| Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 81 |  | 
|  | 82 | def gen_cpp(): | 
| Yiwei Zhang | 6ca5d0c | 2019-10-11 17:15:02 -0700 | [diff] [blame] | 83 | """Generates the null_driver_gen.cpp file. | 
|  | 84 | """ | 
| Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 85 | genfile = os.path.join(os.path.dirname(__file__), | 
|  | 86 | '..', 'nulldrv', 'null_driver_gen.cpp') | 
|  | 87 |  | 
|  | 88 | with open(genfile, 'w') as f: | 
|  | 89 | f.write(gencom.copyright_and_warning(2015)) | 
|  | 90 |  | 
|  | 91 | f.write("""\ | 
|  | 92 | #include <algorithm> | 
| Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 93 |  | 
|  | 94 | #include "null_driver_gen.h" | 
|  | 95 |  | 
|  | 96 | using namespace null_driver; | 
|  | 97 |  | 
|  | 98 | namespace { | 
|  | 99 |  | 
|  | 100 | struct NameProc { | 
|  | 101 | const char* name; | 
|  | 102 | PFN_vkVoidFunction proc; | 
|  | 103 | }; | 
|  | 104 |  | 
|  | 105 | PFN_vkVoidFunction Lookup(const char* name, | 
|  | 106 | const NameProc* begin, | 
|  | 107 | const NameProc* end) { | 
|  | 108 | const auto& entry = std::lower_bound( | 
|  | 109 | begin, end, name, | 
|  | 110 | [](const NameProc& e, const char* n) { return strcmp(e.name, n) < 0; }); | 
|  | 111 | if (entry == end || strcmp(entry->name, name) != 0) | 
|  | 112 | return nullptr; | 
|  | 113 | return entry->proc; | 
|  | 114 | } | 
|  | 115 |  | 
|  | 116 | template <size_t N> | 
|  | 117 | PFN_vkVoidFunction Lookup(const char* name, const NameProc (&procs)[N]) { | 
|  | 118 | return Lookup(name, procs, procs + N); | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | const NameProc kGlobalProcs[] = { | 
| Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 122 | // clang-format off\n""") | 
| Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 123 |  | 
| Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 124 | sorted_command_list = sorted(gencom.command_list) | 
|  | 125 | for cmd in sorted_command_list: | 
|  | 126 | if (_is_driver_function(cmd) and | 
|  | 127 | gencom.get_dispatch_table_type(cmd) == 'Global'): | 
|  | 128 | f.write(gencom.indent(1) + '{\"' + cmd + | 
|  | 129 | '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' + | 
|  | 130 | cmd + '>(' + gencom.base_name(cmd) + '))},\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 | f.write("""\ | 
|  | 133 | // clang-format on | 
|  | 134 | }; | 
| Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 135 |  | 
| Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 136 | const NameProc kInstanceProcs[] = { | 
|  | 137 | // clang-format off\n""") | 
|  | 138 |  | 
|  | 139 | for cmd in sorted_command_list: | 
|  | 140 | if _is_driver_function(cmd): | 
|  | 141 | f.write(gencom.indent(1) + '{\"' + cmd + | 
|  | 142 | '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' + | 
|  | 143 | cmd + '>(' + gencom.base_name(cmd) + '))},\n') | 
|  | 144 |  | 
|  | 145 | f.write("""\ | 
|  | 146 | // clang-format on | 
|  | 147 | }; | 
|  | 148 |  | 
|  | 149 | }  // namespace | 
|  | 150 |  | 
|  | 151 | namespace null_driver { | 
| Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 152 |  | 
|  | 153 | PFN_vkVoidFunction GetGlobalProcAddr(const char* name) { | 
|  | 154 | return Lookup(name, kGlobalProcs); | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | PFN_vkVoidFunction GetInstanceProcAddr(const char* name) { | 
|  | 158 | return Lookup(name, kInstanceProcs); | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | }  // namespace null_driver\n""") | 
| Adithya Srinivasan | 6a9b16e | 2019-07-10 17:49:49 -0700 | [diff] [blame] | 162 |  | 
| Yiwei Zhang | 1ca59c1 | 2019-10-10 12:54:42 -0700 | [diff] [blame] | 163 | f.close() | 
|  | 164 | gencom.run_clang_format(genfile) |