From 4af0c038cfe38e144e4d83e03c0d3a9372767886 Mon Sep 17 00:00:00 2001 From: Jason Ekstrand Date: Fri, 1 Oct 2021 17:26:29 -0500 Subject: [PATCH] vulkan: Move trampoline code-gen to its own file This way we sepaprate the raw tables from the code that actively depends on vk_device and friends. Reviewed-by: Dylan Baker Part-of: --- src/vulkan/util/meson.build | 19 +- src/vulkan/util/vk_dispatch_table_gen.py | 93 --------- .../util/vk_dispatch_trampolines_gen.py | 192 ++++++++++++++++++ src/vulkan/util/vk_instance.c | 1 + 4 files changed, 211 insertions(+), 94 deletions(-) create mode 100644 src/vulkan/util/vk_dispatch_trampolines_gen.py diff --git a/src/vulkan/util/meson.build b/src/vulkan/util/meson.build index d3040b60794..fd90ebbb4ab 100644 --- a/src/vulkan/util/meson.build +++ b/src/vulkan/util/meson.build @@ -34,6 +34,10 @@ vk_dispatch_table_gen_depend_files = [ files('vk_entrypoints.py'), vk_entrypoints_depend_files, ] +vk_dispatch_trampolines_gen_depend_files = [ + files('vk_entrypoints.py'), + vk_entrypoints_depend_files, +] vk_entrypoints_gen_depend_files = [ files('vk_entrypoints.py'), vk_entrypoints_depend_files, @@ -113,6 +117,17 @@ vk_dispatch_table = custom_target( depend_files : vk_dispatch_table_gen_depend_files, ) +vk_dispatch_trampolines = custom_target( + 'vk_dispatch_trampolines', + input : ['vk_dispatch_trampolines_gen.py', vk_api_xml], + output : ['vk_dispatch_trampolines.c', 'vk_dispatch_trampolines.h'], + command : [ + prog_python, '@INPUT0@', '--xml', '@INPUT1@', + '--out-c', '@OUTPUT0@', '--out-h', '@OUTPUT1@' + ], + depend_files : vk_dispatch_trampolines_gen_depend_files, +) + vk_enum_to_str = custom_target( 'vk_enum_to_str', input : ['gen_enum_to_str.py', vk_api_xml], @@ -159,6 +174,7 @@ vk_physical_device_features = custom_target( libvulkan_util = static_library( 'vulkan_util', [files_vulkan_util, vk_common_entrypoints, vk_dispatch_table, + vk_dispatch_trampolines, vk_enum_to_str, vk_extensions, vk_cmd_queue, vk_physical_device_features], include_directories : [inc_include, inc_src, inc_gallium], dependencies : [vulkan_wsi_deps, idep_mesautil, idep_nir_headers], @@ -170,7 +186,8 @@ libvulkan_util = static_library( ) idep_vulkan_util_headers = declare_dependency( - sources : [vk_dispatch_table[1], vk_enum_to_str[1], vk_extensions[1]], + sources : [vk_dispatch_table[1], vk_dispatch_trampolines[1], + vk_enum_to_str[1], vk_extensions[1]], include_directories : include_directories('.') ) diff --git a/src/vulkan/util/vk_dispatch_table_gen.py b/src/vulkan/util/vk_dispatch_table_gen.py index 9fef55141aa..6073a80654d 100644 --- a/src/vulkan/util/vk_dispatch_table_gen.py +++ b/src/vulkan/util/vk_dispatch_table_gen.py @@ -215,9 +215,6 @@ vk_device_dispatch_table_get_if_supported( const struct vk_instance_extension_table *instance_exts, const struct vk_device_extension_table *device_exts); -extern struct vk_physical_device_dispatch_table vk_physical_device_trampolines; -extern struct vk_device_dispatch_table vk_device_trampolines; - #ifdef __cplusplus } #endif @@ -228,11 +225,7 @@ extern struct vk_device_dispatch_table vk_device_trampolines; TEMPLATE_C = Template(COPYRIGHT + """\ /* This file generated from ${filename}, don't edit directly. */ -#include "vk_device.h" #include "vk_dispatch_table.h" -#include "vk_instance.h" -#include "vk_object.h" -#include "vk_physical_device.h" #include "util/macros.h" #include "string.h" @@ -595,92 +588,6 @@ vk_device_dispatch_table_get_if_supported( return vk_device_dispatch_table_get_for_entry_index(table, entry_index); } - -% for e in physical_device_entrypoints: - % if e.alias: - <% continue %> - % endif - % if e.guard is not None: -#ifdef ${e.guard} - % endif -static VKAPI_ATTR ${e.return_type} VKAPI_CALL -${e.prefixed_name('vk_tramp')}(${e.decl_params()}) -{ - <% assert e.params[0].type == 'VkPhysicalDevice' %> - VK_FROM_HANDLE(vk_physical_device, vk_physical_device, ${e.params[0].name}); - % if e.return_type == 'void': - vk_physical_device->dispatch_table.${e.name}(${e.call_params()}); - % else: - return vk_physical_device->dispatch_table.${e.name}(${e.call_params()}); - % endif -} - % if e.guard is not None: -#endif - % endif -% endfor - -struct vk_physical_device_dispatch_table vk_physical_device_trampolines = { -% for e in physical_device_entrypoints: - % if e.alias: - <% continue %> - % endif - % if e.guard is not None: -#ifdef ${e.guard} - % endif - .${e.name} = ${e.prefixed_name('vk_tramp')}, - % if e.guard is not None: -#endif - % endif -% endfor -}; - -% for e in device_entrypoints: - % if e.alias: - <% continue %> - % endif - % if e.guard is not None: -#ifdef ${e.guard} - % endif -static VKAPI_ATTR ${e.return_type} VKAPI_CALL -${e.prefixed_name('vk_tramp')}(${e.decl_params()}) -{ - % if e.params[0].type == 'VkDevice': - VK_FROM_HANDLE(vk_device, vk_device, ${e.params[0].name}); - % if e.return_type == 'void': - vk_device->dispatch_table.${e.name}(${e.call_params()}); - % else: - return vk_device->dispatch_table.${e.name}(${e.call_params()}); - % endif - % elif e.params[0].type in ('VkCommandBuffer', 'VkQueue'): - struct vk_object_base *vk_object = (struct vk_object_base *)${e.params[0].name}; - % if e.return_type == 'void': - vk_object->device->dispatch_table.${e.name}(${e.call_params()}); - % else: - return vk_object->device->dispatch_table.${e.name}(${e.call_params()}); - % endif - % else: - assert(!"Unhandled device child trampoline case: ${e.params[0].type}"); - % endif -} - % if e.guard is not None: -#endif - % endif -% endfor - -struct vk_device_dispatch_table vk_device_trampolines = { -% for e in device_entrypoints: - % if e.alias: - <% continue %> - % endif - % if e.guard is not None: -#ifdef ${e.guard} - % endif - .${e.name} = ${e.prefixed_name('vk_tramp')}, - % if e.guard is not None: -#endif - % endif -% endfor -}; """) U32_MASK = 2**32 - 1 diff --git a/src/vulkan/util/vk_dispatch_trampolines_gen.py b/src/vulkan/util/vk_dispatch_trampolines_gen.py new file mode 100644 index 00000000000..fe999d4b141 --- /dev/null +++ b/src/vulkan/util/vk_dispatch_trampolines_gen.py @@ -0,0 +1,192 @@ +# coding=utf-8 +COPYRIGHT = """\ +/* + * Copyright 2020 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +""" + +import argparse +import os + +from mako.template import Template + +# Mesa-local imports must be declared in meson variable +# '{file_without_suffix}_depend_files'. +from vk_entrypoints import get_entrypoints_from_xml + +TEMPLATE_H = Template(COPYRIGHT + """\ +/* This file generated from ${filename}, don't edit directly. */ + +#ifndef VK_DISPATCH_TRAMPOLINES_H +#define VK_DISPATCH_TRAMPOLINES_H + +#include "vulkan/util/vk_dispatch_table.h" + +#ifdef __cplusplus +extern "C" { +#endif + +extern struct vk_physical_device_dispatch_table vk_physical_device_trampolines; +extern struct vk_device_dispatch_table vk_device_trampolines; + +#ifdef __cplusplus +} +#endif + +#endif /* VK_DISPATCH_TRAMPOLINES_H */ +""") + +TEMPLATE_C = Template(COPYRIGHT + """\ +/* This file generated from ${filename}, don't edit directly. */ + +#include "vk_device.h" +#include "vk_dispatch_trampolines.h" +#include "vk_object.h" +#include "vk_physical_device.h" + +% for e in entrypoints: + % if not e.is_physical_device_entrypoint() or e.alias: + <% continue %> + % endif + % if e.guard is not None: +#ifdef ${e.guard} + % endif +static VKAPI_ATTR ${e.return_type} VKAPI_CALL +${e.prefixed_name('vk_tramp')}(${e.decl_params()}) +{ + <% assert e.params[0].type == 'VkPhysicalDevice' %> + VK_FROM_HANDLE(vk_physical_device, vk_physical_device, ${e.params[0].name}); + % if e.return_type == 'void': + vk_physical_device->dispatch_table.${e.name}(${e.call_params()}); + % else: + return vk_physical_device->dispatch_table.${e.name}(${e.call_params()}); + % endif +} + % if e.guard is not None: +#endif + % endif +% endfor + +struct vk_physical_device_dispatch_table vk_physical_device_trampolines = { +% for e in entrypoints: + % if not e.is_physical_device_entrypoint() or e.alias: + <% continue %> + % endif + % if e.guard is not None: +#ifdef ${e.guard} + % endif + .${e.name} = ${e.prefixed_name('vk_tramp')}, + % if e.guard is not None: +#endif + % endif +% endfor +}; + +% for e in entrypoints: + % if not e.is_device_entrypoint() or e.alias: + <% continue %> + % endif + % if e.guard is not None: +#ifdef ${e.guard} + % endif +static VKAPI_ATTR ${e.return_type} VKAPI_CALL +${e.prefixed_name('vk_tramp')}(${e.decl_params()}) +{ + % if e.params[0].type == 'VkDevice': + VK_FROM_HANDLE(vk_device, vk_device, ${e.params[0].name}); + % if e.return_type == 'void': + vk_device->dispatch_table.${e.name}(${e.call_params()}); + % else: + return vk_device->dispatch_table.${e.name}(${e.call_params()}); + % endif + % elif e.params[0].type in ('VkCommandBuffer', 'VkQueue'): + struct vk_object_base *vk_object = (struct vk_object_base *)${e.params[0].name}; + % if e.return_type == 'void': + vk_object->device->dispatch_table.${e.name}(${e.call_params()}); + % else: + return vk_object->device->dispatch_table.${e.name}(${e.call_params()}); + % endif + % else: + assert(!"Unhandled device child trampoline case: ${e.params[0].type}"); + % endif +} + % if e.guard is not None: +#endif + % endif +% endfor + +struct vk_device_dispatch_table vk_device_trampolines = { +% for e in entrypoints: + % if not e.is_device_entrypoint() or e.alias: + <% continue %> + % endif + % if e.guard is not None: +#ifdef ${e.guard} + % endif + .${e.name} = ${e.prefixed_name('vk_tramp')}, + % if e.guard is not None: +#endif + % endif +% endfor +}; +""") + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--out-c', help='Output C file.') + parser.add_argument('--out-h', help='Output H file.') + parser.add_argument('--xml', + help='Vulkan API XML file.', + required=True, + action='append', + dest='xml_files') + args = parser.parse_args() + + entrypoints = get_entrypoints_from_xml(args.xml_files) + + # For outputting entrypoints.h we generate a anv_EntryPoint() prototype + # per entry point. + try: + if args.out_h: + with open(args.out_h, 'w') as f: + f.write(TEMPLATE_H.render(entrypoints=entrypoints, + filename=os.path.basename(__file__))) + if args.out_c: + with open(args.out_c, 'w') as f: + f.write(TEMPLATE_C.render(entrypoints=entrypoints, + filename=os.path.basename(__file__))) + except Exception: + # In the event there's an error, this imports some helpers from mako + # to print a useful stack trace and prints it, then exits with + # status 1, if python is run with debug; otherwise it just raises + # the exception + if __debug__: + import sys + from mako import exceptions + sys.stderr.write(exceptions.text_error_template().render() + '\n') + sys.exit(1) + raise + + +if __name__ == '__main__': + main() diff --git a/src/vulkan/util/vk_instance.c b/src/vulkan/util/vk_instance.c index 93107150913..a247bc3f096 100644 --- a/src/vulkan/util/vk_instance.c +++ b/src/vulkan/util/vk_instance.c @@ -25,6 +25,7 @@ #include "vk_alloc.h" #include "vk_common_entrypoints.h" +#include "vk_dispatch_trampolines.h" #include "vk_log.h" #include "vk_util.h" #include "vk_debug_utils.h"