blob: 3624c1d0329914cbdff54c1784b6d1316f3c4789 [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.
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -070016
17"""Generates the null_driver_gen.h and null_driver_gen.cpp.
18"""
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070019
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 Zhang6ca5d0c2019-10-11 17:15:02 -070023# Extensions implemented by the driver.
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070024_DRIVER_EXTENSION_DICT = {
25 'VK_ANDROID_native_buffer',
26 'VK_EXT_debug_report',
27 'VK_KHR_get_physical_device_properties2'
28}
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070029
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070030
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070031def _is_driver_function(cmd):
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -070032 """Returns true if the function is implemented by the driver.
33
34 Args:
35 cmd: Vulkan function name.
36 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070037 if cmd in gencom.extension_dict:
38 return gencom.extension_dict[cmd] in _DRIVER_EXTENSION_DICT
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070039 return True
40
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070041
42def gen_h():
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -070043 """Generates the null_driver_gen.h file.
44 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070045 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 Srinivasan6a9b16e2019-07-10 17:49:49 -070053#define NULLDRV_NULL_DRIVER_H 1
54
55#include <vulkan/vk_android_native_buffer.h>
56#include <vulkan/vulkan.h>
57
58namespace null_driver {
59
60PFN_vkVoidFunction GetGlobalProcAddr(const char* name);
61PFN_vkVoidFunction GetInstanceProcAddr(const char* name);
62
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070063// clang-format off\n""")
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070064
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070065 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 Srinivasan6a9b16e2019-07-10 17:49:49 -070070
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070071 f.write("""\
72// clang-format on
73
74} // namespace null_driver
75
76#endif // NULLDRV_NULL_DRIVER_H\n""")
77
Adithya Srinivasan8dce9d72019-07-11 14:26:04 -070078 f.close()
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070079 gencom.run_clang_format(genfile)
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070080
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070081
82def gen_cpp():
Yiwei Zhang6ca5d0c2019-10-11 17:15:02 -070083 """Generates the null_driver_gen.cpp file.
84 """
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070085 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("""\
Chris Forbes9d84eae2024-06-12 12:06:28 +120092#include <android/hardware_buffer.h>
93
Yiwei Zhang1ca59c12019-10-10 12:54:42 -070094#include <algorithm>
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -070095
96#include "null_driver_gen.h"
97
98using namespace null_driver;
99
100namespace {
101
102struct NameProc {
103 const char* name;
104 PFN_vkVoidFunction proc;
105};
106
107PFN_vkVoidFunction Lookup(const char* name,
108 const NameProc* begin,
109 const NameProc* end) {
110 const auto& entry = std::lower_bound(
111 begin, end, name,
112 [](const NameProc& e, const char* n) { return strcmp(e.name, n) < 0; });
113 if (entry == end || strcmp(entry->name, name) != 0)
114 return nullptr;
115 return entry->proc;
116}
117
118template <size_t N>
119PFN_vkVoidFunction Lookup(const char* name, const NameProc (&procs)[N]) {
120 return Lookup(name, procs, procs + N);
121}
122
123const NameProc kGlobalProcs[] = {
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700124 // clang-format off\n""")
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700125
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700126 sorted_command_list = sorted(gencom.command_list)
127 for cmd in sorted_command_list:
128 if (_is_driver_function(cmd) and
129 gencom.get_dispatch_table_type(cmd) == 'Global'):
130 f.write(gencom.indent(1) + '{\"' + cmd +
131 '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' +
132 cmd + '>(' + gencom.base_name(cmd) + '))},\n')
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700133
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700134 f.write("""\
135 // clang-format on
136};
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700137
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700138const NameProc kInstanceProcs[] = {
139 // clang-format off\n""")
140
141 for cmd in sorted_command_list:
142 if _is_driver_function(cmd):
143 f.write(gencom.indent(1) + '{\"' + cmd +
144 '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' +
145 cmd + '>(' + gencom.base_name(cmd) + '))},\n')
146
147 f.write("""\
148 // clang-format on
149};
150
151} // namespace
152
153namespace null_driver {
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700154
155PFN_vkVoidFunction GetGlobalProcAddr(const char* name) {
156 return Lookup(name, kGlobalProcs);
157}
158
159PFN_vkVoidFunction GetInstanceProcAddr(const char* name) {
160 return Lookup(name, kInstanceProcs);
161}
162
163} // namespace null_driver\n""")
Adithya Srinivasan6a9b16e2019-07-10 17:49:49 -0700164
Yiwei Zhang1ca59c12019-10-10 12:54:42 -0700165 f.close()
166 gencom.run_clang_format(genfile)