1# coding=utf-8 2COPYRIGHT=u""" 3/* Copyright © 2015-2021 Intel Corporation 4 * Copyright © 2021 Collabora, Ltd. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 23 * IN THE SOFTWARE. 24 */ 25""" 26 27import argparse 28import os 29import re 30import xml.etree.ElementTree as et 31 32from mako.template import Template 33 34# Mesa-local imports must be declared in meson variable 35# '{file_without_suffix}_depend_files'. 36from vk_dispatch_table_gen import get_entrypoints_from_xml, EntrypointParam 37 38MANUAL_COMMANDS = ['CmdPushDescriptorSetKHR', # This script doesn't know how to copy arrays in structs in arrays 39 'CmdPushDescriptorSetWithTemplateKHR', # pData's size cannot be calculated from the xml 40 'CmdDrawMultiEXT', # The size of the elements is specified in a stride param 41 'CmdDrawMultiIndexedEXT', # The size of the elements is specified in a stride param 42 'CmdBindDescriptorSets', # The VkPipelineLayout object could be released before the command is executed 43 'CmdCopyImageToBuffer', # There are wrappers that implement these in terms of the newer variants 44 'CmdCopyImage', 45 'CmdCopyBuffer', 46 'CmdCopyImage', 47 'CmdCopyBufferToImage', 48 'CmdCopyImageToBuffer', 49 'CmdBlitImage', 50 'CmdResolveImage', 51 ] 52 53TEMPLATE_C = Template(COPYRIGHT + """ 54/* This file generated from ${filename}, don't edit directly. */ 55 56#define VK_PROTOTYPES 57#include <vulkan/vulkan.h> 58 59#include "lvp_private.h" 60#include "pipe/p_context.h" 61#include "vk_util.h" 62 63% for c in commands: 64% if c.name in manual_commands: 65<% continue %> 66% endif 67% if c.guard is not None: 68#ifdef ${c.guard} 69% endif 70VKAPI_ATTR ${c.return_type} VKAPI_CALL lvp_${c.name} (VkCommandBuffer commandBuffer 71% for p in c.params[1:]: 72, ${p.decl} 73% endfor 74) 75{ 76 LVP_FROM_HANDLE(lvp_cmd_buffer, cmd_buffer, commandBuffer); 77 78 vk_enqueue_${to_underscore(c.name)}(&cmd_buffer->queue 79% for p in c.params[1:]: 80, ${p.name} 81% endfor 82 ); 83 84% if c.return_type == 'VkResult': 85 return VK_SUCCESS; 86% endif 87} 88% if c.guard is not None: 89#endif // ${c.guard} 90% endif 91% endfor 92 93""", output_encoding='utf-8') 94 95def remove_prefix(text, prefix): 96 if text.startswith(prefix): 97 return text[len(prefix):] 98 return text 99 100def to_underscore(name): 101 return remove_prefix(re.sub('([A-Z]+)', r'_\1', name).lower(), '_') 102 103def main(): 104 parser = argparse.ArgumentParser() 105 parser.add_argument('--out-c', required=True, help='Output C file.') 106 parser.add_argument('--xml', 107 help='Vulkan API XML file.', 108 required=True, action='append', dest='xml_files') 109 parser.add_argument('--prefix', 110 help='Prefix to use for all dispatch tables.', 111 action='append', default=[], dest='prefixes') 112 args = parser.parse_args() 113 114 commands = [] 115 for e in get_entrypoints_from_xml(args.xml_files): 116 if e.name.startswith('Cmd') and \ 117 not e.alias: 118 commands.append(e) 119 120 environment = { 121 'commands': commands, 122 'filename': os.path.basename(__file__), 123 'to_underscore': to_underscore, 124 'manual_commands': MANUAL_COMMANDS, 125 } 126 127 try: 128 with open(args.out_c, 'wb') as f: 129 f.write(TEMPLATE_C.render(**environment)) 130 except Exception: 131 # In the event there's an error, this imports some helpers from mako 132 # to print a useful stack trace and prints it, then exits with 133 # status 1, if python is run with debug; otherwise it just raises 134 # the exception 135 if __debug__: 136 import sys 137 from mako import exceptions 138 sys.stderr.write(exceptions.text_error_template().render() + '\n') 139 sys.exit(1) 140 raise 141 142if __name__ == '__main__': 143 main() 144