17ec681f3Smrg# coding=utf-8
27ec681f3SmrgCOPYRIGHT=u"""
37ec681f3Smrg/* Copyright © 2015-2021 Intel Corporation
47ec681f3Smrg * Copyright © 2021 Collabora, Ltd.
57ec681f3Smrg *
67ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
87ec681f3Smrg * to deal in the Software without restriction, including without limitation
97ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
107ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
117ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
127ec681f3Smrg *
137ec681f3Smrg * The above copyright notice and this permission notice (including the next
147ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
157ec681f3Smrg * Software.
167ec681f3Smrg *
177ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
187ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
197ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
207ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
217ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
227ec681f3Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
237ec681f3Smrg * IN THE SOFTWARE.
247ec681f3Smrg */
257ec681f3Smrg"""
267ec681f3Smrg
277ec681f3Smrgimport argparse
287ec681f3Smrgimport os
297ec681f3Smrgimport re
307ec681f3Smrgimport xml.etree.ElementTree as et
317ec681f3Smrg
327ec681f3Smrgfrom mako.template import Template
337ec681f3Smrg
347ec681f3Smrg# Mesa-local imports must be declared in meson variable
357ec681f3Smrg# '{file_without_suffix}_depend_files'.
367ec681f3Smrgfrom vk_dispatch_table_gen import get_entrypoints_from_xml, EntrypointParam
377ec681f3Smrg
387ec681f3SmrgMANUAL_COMMANDS = ['CmdPushDescriptorSetKHR',             # This script doesn't know how to copy arrays in structs in arrays
397ec681f3Smrg                   'CmdPushDescriptorSetWithTemplateKHR', # pData's size cannot be calculated from the xml
407ec681f3Smrg                   'CmdDrawMultiEXT',                     # The size of the elements is specified in a stride param
417ec681f3Smrg                   'CmdDrawMultiIndexedEXT',              # The size of the elements is specified in a stride param
427ec681f3Smrg                   'CmdBindDescriptorSets',               # The VkPipelineLayout object could be released before the command is executed
437ec681f3Smrg                   'CmdCopyImageToBuffer',                # There are wrappers that implement these in terms of the newer variants
447ec681f3Smrg                   'CmdCopyImage',
457ec681f3Smrg                   'CmdCopyBuffer',
467ec681f3Smrg                   'CmdCopyImage',
477ec681f3Smrg                   'CmdCopyBufferToImage',
487ec681f3Smrg                   'CmdCopyImageToBuffer',
497ec681f3Smrg                   'CmdBlitImage',
507ec681f3Smrg                   'CmdResolveImage',
517ec681f3Smrg                  ]
527ec681f3Smrg
537ec681f3SmrgTEMPLATE_C = Template(COPYRIGHT + """
547ec681f3Smrg/* This file generated from ${filename}, don't edit directly. */
557ec681f3Smrg
567ec681f3Smrg#define VK_PROTOTYPES
577ec681f3Smrg#include <vulkan/vulkan.h>
587ec681f3Smrg
597ec681f3Smrg#include "lvp_private.h"
607ec681f3Smrg#include "pipe/p_context.h"
617ec681f3Smrg#include "vk_util.h"
627ec681f3Smrg
637ec681f3Smrg% for c in commands:
647ec681f3Smrg% if c.name in manual_commands:
657ec681f3Smrg<% continue %>
667ec681f3Smrg% endif
677ec681f3Smrg% if c.guard is not None:
687ec681f3Smrg#ifdef ${c.guard}
697ec681f3Smrg% endif
707ec681f3SmrgVKAPI_ATTR ${c.return_type} VKAPI_CALL lvp_${c.name} (VkCommandBuffer commandBuffer
717ec681f3Smrg% for p in c.params[1:]:
727ec681f3Smrg, ${p.decl}
737ec681f3Smrg% endfor
747ec681f3Smrg)
757ec681f3Smrg{
767ec681f3Smrg   LVP_FROM_HANDLE(lvp_cmd_buffer, cmd_buffer, commandBuffer);
777ec681f3Smrg
787ec681f3Smrg   vk_enqueue_${to_underscore(c.name)}(&cmd_buffer->queue
797ec681f3Smrg% for p in c.params[1:]:
807ec681f3Smrg, ${p.name}
817ec681f3Smrg% endfor
827ec681f3Smrg   );
837ec681f3Smrg
847ec681f3Smrg% if c.return_type == 'VkResult':
857ec681f3Smrg   return VK_SUCCESS;
867ec681f3Smrg% endif
877ec681f3Smrg}
887ec681f3Smrg% if c.guard is not None:
897ec681f3Smrg#endif // ${c.guard}
907ec681f3Smrg% endif
917ec681f3Smrg% endfor
927ec681f3Smrg
937ec681f3Smrg""", output_encoding='utf-8')
947ec681f3Smrg
957ec681f3Smrgdef remove_prefix(text, prefix):
967ec681f3Smrg    if text.startswith(prefix):
977ec681f3Smrg        return text[len(prefix):]
987ec681f3Smrg    return text
997ec681f3Smrg
1007ec681f3Smrgdef to_underscore(name):
1017ec681f3Smrg    return remove_prefix(re.sub('([A-Z]+)', r'_\1', name).lower(), '_')
1027ec681f3Smrg
1037ec681f3Smrgdef main():
1047ec681f3Smrg    parser = argparse.ArgumentParser()
1057ec681f3Smrg    parser.add_argument('--out-c', required=True, help='Output C file.')
1067ec681f3Smrg    parser.add_argument('--xml',
1077ec681f3Smrg                        help='Vulkan API XML file.',
1087ec681f3Smrg                        required=True, action='append', dest='xml_files')
1097ec681f3Smrg    parser.add_argument('--prefix',
1107ec681f3Smrg                        help='Prefix to use for all dispatch tables.',
1117ec681f3Smrg                        action='append', default=[], dest='prefixes')
1127ec681f3Smrg    args = parser.parse_args()
1137ec681f3Smrg
1147ec681f3Smrg    commands = []
1157ec681f3Smrg    for e in get_entrypoints_from_xml(args.xml_files):
1167ec681f3Smrg        if e.name.startswith('Cmd') and \
1177ec681f3Smrg           not e.alias:
1187ec681f3Smrg            commands.append(e)
1197ec681f3Smrg
1207ec681f3Smrg    environment = {
1217ec681f3Smrg        'commands': commands,
1227ec681f3Smrg        'filename': os.path.basename(__file__),
1237ec681f3Smrg        'to_underscore': to_underscore,
1247ec681f3Smrg        'manual_commands': MANUAL_COMMANDS,
1257ec681f3Smrg    }
1267ec681f3Smrg
1277ec681f3Smrg    try:
1287ec681f3Smrg        with open(args.out_c, 'wb') as f:
1297ec681f3Smrg            f.write(TEMPLATE_C.render(**environment))
1307ec681f3Smrg    except Exception:
1317ec681f3Smrg        # In the event there's an error, this imports some helpers from mako
1327ec681f3Smrg        # to print a useful stack trace and prints it, then exits with
1337ec681f3Smrg        # status 1, if python is run with debug; otherwise it just raises
1347ec681f3Smrg        # the exception
1357ec681f3Smrg        if __debug__:
1367ec681f3Smrg            import sys
1377ec681f3Smrg            from mako import exceptions
1387ec681f3Smrg            sys.stderr.write(exceptions.text_error_template().render() + '\n')
1397ec681f3Smrg            sys.exit(1)
1407ec681f3Smrg        raise
1417ec681f3Smrg
1427ec681f3Smrgif __name__ == '__main__':
1437ec681f3Smrg    main()
144