xref: /aosp_15_r20/external/mesa3d/src/amd/vulkan/layers/radv_annotate_layer_gen.py (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1COPYRIGHT=u"""
2/* Copyright © 2021 Intel Corporation
3 * Copyright © 2024 Valve Corporation
4 *
5 * SPDX-License-Identifier: MIT
6 */
7"""
8
9import argparse
10import os
11import sys
12import xml.etree.ElementTree as et
13
14import mako
15from mako.template import Template
16
17sys.path.append(os.path.join(sys.path[0], '../../../vulkan/util/'))
18
19from vk_entrypoints import get_entrypoints_from_xml
20
21EXCLUDED_COMMANDS = [
22    'CmdBeginRenderPass',
23    'CmdEndRenderPass',
24    'CmdDispatch',
25]
26
27TEMPLATE = Template(COPYRIGHT + """
28/* This file generated from ${filename}, don't edit directly. */
29
30#include "radv_cmd_buffer.h"
31#include "radv_entrypoints.h"
32
33#define ANNOTATE(command, ...) \
34   struct radv_cmd_buffer *cmd_buffer = radv_cmd_buffer_from_handle(commandBuffer); \
35   struct radv_device *device = radv_cmd_buffer_device(cmd_buffer); \
36   radv_cmd_buffer_annotate(cmd_buffer, #command); \
37   device->layer_dispatch.annotate.command(__VA_ARGS__)
38
39% for c in commands:
40% if c.guard is not None:
41#ifdef ${c.guard}
42% endif
43VKAPI_ATTR ${c.return_type} VKAPI_CALL
44annotate_${c.name}(${c.decl_params()})
45{
46   ANNOTATE(${c.name}, ${c.call_params()});
47}
48
49% if c.guard is not None:
50#endif // ${c.guard}
51% endif
52% endfor
53""")
54
55# str.removesuffix requires python 3.9+ so implement our own to not break build
56# on older versions
57def removesuffix(s, suffix):
58    l = len(suffix)
59    if l == 0:
60        return s
61    idx = s.find(suffix)
62    if idx == len(s) - l:
63        return s[:-l]
64    return s
65
66
67def main():
68    parser = argparse.ArgumentParser()
69    parser.add_argument("--out-c", required=True, help="Output C file.")
70    parser.add_argument("--beta", required=True, help="Enable beta extensions.")
71    parser.add_argument("--xml",
72                        help="Vulkan API XML file.",
73                        required=True, action="append", dest="xml_files")
74    args = parser.parse_args()
75
76    commands = []
77    commands_names = []
78    for e in get_entrypoints_from_xml(args.xml_files, args.beta):
79        if not e.name.startswith('Cmd') or e.alias or e.return_type != "void":
80            continue
81
82        stripped_name = removesuffix(removesuffix(removesuffix(e.name, 'EXT'), 'KHR'), '2')
83        if stripped_name in commands_names or stripped_name in EXCLUDED_COMMANDS:
84            continue
85
86        commands.append(e)
87        commands_names.append(stripped_name)
88
89    environment = {
90        "filename": os.path.basename(__file__),
91        "commands": commands,
92    }
93
94    try:
95        with open(args.out_c, "w", encoding='utf-8') as f:
96            f.write(TEMPLATE.render(**environment))
97    except Exception:
98        # In the event there"s an error, this uses some helpers from mako
99        # to print a useful stack trace and prints it, then exits with
100        # status 1, if python is run with debug; otherwise it just raises
101        # the exception
102        print(mako.exceptions.text_error_template().render(), file=sys.stderr)
103        sys.exit(1)
104
105if __name__ == "__main__":
106    main()
107