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