blob: 2a99a1dda4f22c25f0b521b3cc0c431ac4f1bab3 [file] [log] [blame]
Adithya Srinivasan6a9b16e2019-07-10 17:49: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.
16#
17# This script provides the functions for generating the null driver
18# framework directly from the vulkan registry (vk.xml).
19
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070020import os
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070021import generator_common as gencom
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070022
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070023_DRIVER_EXTENSION_DICT = {
24 'VK_ANDROID_native_buffer',
25 'VK_EXT_debug_report',
26 'VK_KHR_get_physical_device_properties2'
27}
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070028
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070029
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070030def _is_driver_function(cmd):
31 if cmd in gencom.extension_dict:
32 return gencom.extension_dict[cmd] in _DRIVER_EXTENSION_DICT
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070033 return True
34
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070035
36def 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 Srinivasan6a9b16e2019-07-10 17:49:49 -070045#define NULLDRV_NULL_DRIVER_H 1
46
47#include <vulkan/vk_android_native_buffer.h>
48#include <vulkan/vulkan.h>
49
50namespace null_driver {
51
52PFN_vkVoidFunction GetGlobalProcAddr(const char* name);
53PFN_vkVoidFunction GetInstanceProcAddr(const char* name);
54
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070055// clang-format off\n""")
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070056
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070057 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 Srinivasan6a9b16e2019-07-10 17:49:49 -070062
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070063 f.write("""\
64// clang-format on
65
66} // namespace null_driver
67
68#endif // NULLDRV_NULL_DRIVER_H\n""")
69
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -070070 f.close()
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070071 gencom.run_clang_format(genfile)
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070072
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070073
74def 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 Srinivasan6a9b16e2019-07-10 17:49:49 -070083
84#include "null_driver_gen.h"
85
86using namespace null_driver;
87
88namespace {
89
90struct NameProc {
91 const char* name;
92 PFN_vkVoidFunction proc;
93};
94
95PFN_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
106template <size_t N>
107PFN_vkVoidFunction Lookup(const char* name, const NameProc (&procs)[N]) {
108 return Lookup(name, procs, procs + N);
109}
110
111const NameProc kGlobalProcs[] = {
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700112 // clang-format off\n""")
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700113
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700114 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 Srinivasan6a9b16e2019-07-10 17:49:49 -0700121
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700122 f.write("""\
123 // clang-format on
124};
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700125
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700126const 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
141namespace null_driver {
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700142
143PFN_vkVoidFunction GetGlobalProcAddr(const char* name) {
144 return Lookup(name, kGlobalProcs);
145}
146
147PFN_vkVoidFunction GetInstanceProcAddr(const char* name) {
148 return Lookup(name, kInstanceProcs);
149}
150
151} // namespace null_driver\n""")
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700152
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700153 f.close()
154 gencom.run_clang_format(genfile)