summaryrefslogtreecommitdiff
path: root/vulkan/scripts/null_generator.py
blob: e9faef663b93363ce739d5a719c2f3b43a6d1458 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python3
#
# Copyright 2019 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Generates the null_driver_gen.h and null_driver_gen.cpp.
"""

import os
import generator_common as gencom

# Extensions implemented by the driver.
_DRIVER_EXTENSION_DICT = {
    'VK_ANDROID_native_buffer',
    'VK_EXT_debug_report',
    'VK_KHR_get_physical_device_properties2'
}


def _is_driver_function(cmd):
  """Returns true if the function is implemented by the driver.

  Args:
    cmd: Vulkan function name.
  """
  if cmd in gencom.extension_dict:
    return gencom.extension_dict[cmd] in _DRIVER_EXTENSION_DICT
  return True


def gen_h():
  """Generates the null_driver_gen.h file.
  """
  genfile = os.path.join(os.path.dirname(__file__),
                         '..', 'nulldrv', 'null_driver_gen.h')

  with open(genfile, 'w') as f:
    f.write(gencom.copyright_and_warning(2015))

    f.write("""\
#ifndef NULLDRV_NULL_DRIVER_H
#define NULLDRV_NULL_DRIVER_H 1

#include <vulkan/vk_android_native_buffer.h>
#include <vulkan/vulkan.h>

namespace null_driver {

PFN_vkVoidFunction GetGlobalProcAddr(const char* name);
PFN_vkVoidFunction GetInstanceProcAddr(const char* name);

// clang-format off\n""")

    for cmd in gencom.command_list:
      if _is_driver_function(cmd):
        param_list = [''.join(i) for i in gencom.param_dict[cmd]]
        f.write('VKAPI_ATTR ' + gencom.return_type_dict[cmd] + ' ' +
                gencom.base_name(cmd) + '(' + ', '.join(param_list) + ');\n')

    f.write("""\
// clang-format on

}  // namespace null_driver

#endif  // NULLDRV_NULL_DRIVER_H\n""")

    f.close()
  gencom.run_clang_format(genfile)


def gen_cpp():
  """Generates the null_driver_gen.cpp file.
  """
  genfile = os.path.join(os.path.dirname(__file__),
                         '..', 'nulldrv', 'null_driver_gen.cpp')

  with open(genfile, 'w') as f:
    f.write(gencom.copyright_and_warning(2015))

    f.write("""\
#include <algorithm>

#include "null_driver_gen.h"

using namespace null_driver;

namespace {

struct NameProc {
    const char* name;
    PFN_vkVoidFunction proc;
};

PFN_vkVoidFunction Lookup(const char* name,
                          const NameProc* begin,
                          const NameProc* end) {
    const auto& entry = std::lower_bound(
        begin, end, name,
        [](const NameProc& e, const char* n) { return strcmp(e.name, n) < 0; });
    if (entry == end || strcmp(entry->name, name) != 0)
        return nullptr;
    return entry->proc;
}

template <size_t N>
PFN_vkVoidFunction Lookup(const char* name, const NameProc (&procs)[N]) {
    return Lookup(name, procs, procs + N);
}

const NameProc kGlobalProcs[] = {
    // clang-format off\n""")

    sorted_command_list = sorted(gencom.command_list)
    for cmd in sorted_command_list:
      if (_is_driver_function(cmd) and
          gencom.get_dispatch_table_type(cmd) == 'Global'):
        f.write(gencom.indent(1) + '{\"' + cmd +
                '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' +
                cmd + '>(' + gencom.base_name(cmd) + '))},\n')

    f.write("""\
    // clang-format on
};

const NameProc kInstanceProcs[] = {
    // clang-format off\n""")

    for cmd in sorted_command_list:
      if _is_driver_function(cmd):
        f.write(gencom.indent(1) + '{\"' + cmd +
                '\", reinterpret_cast<PFN_vkVoidFunction>(static_cast<PFN_' +
                cmd + '>(' + gencom.base_name(cmd) + '))},\n')

    f.write("""\
    // clang-format on
};

}  // namespace

namespace null_driver {

PFN_vkVoidFunction GetGlobalProcAddr(const char* name) {
    return Lookup(name, kGlobalProcs);
}

PFN_vkVoidFunction GetInstanceProcAddr(const char* name) {
    return Lookup(name, kInstanceProcs);
}

}  // namespace null_driver\n""")

    f.close()
  gencom.run_clang_format(genfile)