Path: blob/21.2-virgl/src/vulkan/util/vk_dispatch_table_gen.py
7136 views
# coding=utf-81COPYRIGHT = """\2/*3* Copyright 2020 Intel Corporation4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*/25"""2627import argparse28import math29import os30import xml.etree.ElementTree as et3132from collections import OrderedDict, namedtuple33from mako.template import Template3435# Mesa-local imports must be declared in meson variable36# '{file_without_suffix}_depend_files'.37from vk_extensions import *3839# We generate a static hash table for entry point lookup40# (vkGetProcAddress). We use a linear congruential generator for our hash41# function and a power-of-two size table. The prime numbers are determined42# experimentally.4344TEMPLATE_H = Template(COPYRIGHT + """\45/* This file generated from ${filename}, don't edit directly. */4647#ifndef VK_DISPATCH_TABLE_H48#define VK_DISPATCH_TABLE_H4950#include "vulkan/vulkan.h"51#include "vulkan/vk_android_native_buffer.h"5253#include "vk_extensions.h"5455/* Windows api conflict */56#ifdef _WIN3257#include <windows.h>58#ifdef CreateSemaphore59#undef CreateSemaphore60#endif61#ifdef CreateEvent62#undef CreateEvent63#endif64#endif6566#ifdef __cplusplus67extern "C" {68#endif6970<%def name="dispatch_table(entrypoints)">71% for e in entrypoints:72% if e.alias:73<% continue %>74% endif75% if e.guard is not None:76#ifdef ${e.guard}77% endif78% if e.aliases:79union {80PFN_vk${e.name} ${e.name};81% for a in e.aliases:82PFN_vk${a.name} ${a.name};83% endfor84};85% else:86PFN_vk${e.name} ${e.name};87% endif88% if e.guard is not None:89#else90% if e.aliases:91union {92PFN_vkVoidFunction ${e.name};93% for a in e.aliases:94PFN_vkVoidFunction ${a.name};95% endfor96};97% else:98PFN_vkVoidFunction ${e.name};99% endif100#endif101% endif102% endfor103</%def>104105<%def name="entrypoint_table(type, entrypoints)">106struct vk_${type}_entrypoint_table {107% for e in entrypoints:108% if e.guard is not None:109#ifdef ${e.guard}110% endif111PFN_vk${e.name} ${e.name};112% if e.guard is not None:113#else114PFN_vkVoidFunction ${e.name};115# endif116% endif117% endfor118};119</%def>120121struct vk_instance_dispatch_table {122${dispatch_table(instance_entrypoints)}123};124125struct vk_physical_device_dispatch_table {126${dispatch_table(physical_device_entrypoints)}127};128129struct vk_device_dispatch_table {130${dispatch_table(device_entrypoints)}131};132133struct vk_dispatch_table {134union {135struct {136struct vk_instance_dispatch_table instance;137struct vk_physical_device_dispatch_table physical_device;138struct vk_device_dispatch_table device;139};140141struct {142${dispatch_table(instance_entrypoints)}143${dispatch_table(physical_device_entrypoints)}144${dispatch_table(device_entrypoints)}145};146};147};148149${entrypoint_table('instance', instance_entrypoints)}150${entrypoint_table('physical_device', physical_device_entrypoints)}151${entrypoint_table('device', device_entrypoints)}152153void154vk_instance_dispatch_table_load(struct vk_instance_dispatch_table *table,155PFN_vkGetInstanceProcAddr gpa,156VkInstance instance);157void158vk_physical_device_dispatch_table_load(struct vk_physical_device_dispatch_table *table,159PFN_vkGetInstanceProcAddr gpa,160VkInstance instance);161void162vk_device_dispatch_table_load(struct vk_device_dispatch_table *table,163PFN_vkGetDeviceProcAddr gpa,164VkDevice device);165166void vk_instance_dispatch_table_from_entrypoints(167struct vk_instance_dispatch_table *dispatch_table,168const struct vk_instance_entrypoint_table *entrypoint_table,169bool overwrite);170171void vk_physical_device_dispatch_table_from_entrypoints(172struct vk_physical_device_dispatch_table *dispatch_table,173const struct vk_physical_device_entrypoint_table *entrypoint_table,174bool overwrite);175176void vk_device_dispatch_table_from_entrypoints(177struct vk_device_dispatch_table *dispatch_table,178const struct vk_device_entrypoint_table *entrypoint_table,179bool overwrite);180181PFN_vkVoidFunction182vk_instance_dispatch_table_get(const struct vk_instance_dispatch_table *table,183const char *name);184185PFN_vkVoidFunction186vk_physical_device_dispatch_table_get(const struct vk_physical_device_dispatch_table *table,187const char *name);188189PFN_vkVoidFunction190vk_device_dispatch_table_get(const struct vk_device_dispatch_table *table,191const char *name);192193PFN_vkVoidFunction194vk_instance_dispatch_table_get_if_supported(195const struct vk_instance_dispatch_table *table,196const char *name,197uint32_t core_version,198const struct vk_instance_extension_table *instance_exts);199200PFN_vkVoidFunction201vk_physical_device_dispatch_table_get_if_supported(202const struct vk_physical_device_dispatch_table *table,203const char *name,204uint32_t core_version,205const struct vk_instance_extension_table *instance_exts);206207PFN_vkVoidFunction208vk_device_dispatch_table_get_if_supported(209const struct vk_device_dispatch_table *table,210const char *name,211uint32_t core_version,212const struct vk_instance_extension_table *instance_exts,213const struct vk_device_extension_table *device_exts);214215extern struct vk_physical_device_dispatch_table vk_physical_device_trampolines;216extern struct vk_device_dispatch_table vk_device_trampolines;217218#ifdef __cplusplus219}220#endif221222#endif /* VK_DISPATCH_TABLE_H */223""", output_encoding='utf-8')224225TEMPLATE_C = Template(COPYRIGHT + """\226/* This file generated from ${filename}, don't edit directly. */227228#include "vk_device.h"229#include "vk_dispatch_table.h"230#include "vk_instance.h"231#include "vk_object.h"232#include "vk_physical_device.h"233234#include "util/macros.h"235#include "string.h"236237<%def name="load_dispatch_table(type, VkType, ProcAddr, entrypoints)">238void239vk_${type}_dispatch_table_load(struct vk_${type}_dispatch_table *table,240PFN_vk${ProcAddr} gpa,241${VkType} obj)242{243% if type != 'physical_device':244table->${ProcAddr} = gpa;245% endif246% for e in entrypoints:247% if e.alias or e.name == '${ProcAddr}':248<% continue %>249% endif250% if e.guard is not None:251#ifdef ${e.guard}252% endif253table->${e.name} = (PFN_vk${e.name}) gpa(obj, "vk${e.name}");254% for a in e.aliases:255if (table->${e.name} == NULL) {256table->${e.name} = (PFN_vk${e.name}) gpa(obj, "vk${a.name}");257}258% endfor259% if e.guard is not None:260#endif261% endif262% endfor263}264</%def>265266${load_dispatch_table('instance', 'VkInstance', 'GetInstanceProcAddr',267instance_entrypoints)}268269${load_dispatch_table('physical_device', 'VkInstance', 'GetInstanceProcAddr',270physical_device_entrypoints)}271272${load_dispatch_table('device', 'VkDevice', 'GetDeviceProcAddr',273device_entrypoints)}274275276struct string_map_entry {277uint32_t name;278uint32_t hash;279uint32_t num;280};281282/* We use a big string constant to avoid lots of reloctions from the entry283* point table to lots of little strings. The entries in the entry point table284* store the index into this big string.285*/286287<%def name="strmap(strmap, prefix)">288static const char ${prefix}_strings[] =289% for s in strmap.sorted_strings:290"${s.string}\\0"291% endfor292;293294static const struct string_map_entry ${prefix}_string_map_entries[] = {295% for s in strmap.sorted_strings:296{ ${s.offset}, ${'{:0=#8x}'.format(s.hash)}, ${s.num} }, /* ${s.string} */297% endfor298};299300/* Hash table stats:301* size ${len(strmap.sorted_strings)} entries302* collisions entries:303% for i in range(10):304* ${i}${'+' if i == 9 else ' '} ${strmap.collisions[i]}305% endfor306*/307308#define none 0xffff309static const uint16_t ${prefix}_string_map[${strmap.hash_size}] = {310% for e in strmap.mapping:311${ '{:0=#6x}'.format(e) if e >= 0 else 'none' },312% endfor313};314315static int316${prefix}_string_map_lookup(const char *str)317{318static const uint32_t prime_factor = ${strmap.prime_factor};319static const uint32_t prime_step = ${strmap.prime_step};320const struct string_map_entry *e;321uint32_t hash, h;322uint16_t i;323const char *p;324325hash = 0;326for (p = str; *p; p++)327hash = hash * prime_factor + *p;328329h = hash;330while (1) {331i = ${prefix}_string_map[h & ${strmap.hash_mask}];332if (i == none)333return -1;334e = &${prefix}_string_map_entries[i];335if (e->hash == hash && strcmp(str, ${prefix}_strings + e->name) == 0)336return e->num;337h += prime_step;338}339340return -1;341}342</%def>343344${strmap(instance_strmap, 'instance')}345${strmap(physical_device_strmap, 'physical_device')}346${strmap(device_strmap, 'device')}347348<% assert len(instance_entrypoints) < 2**8 %>349static const uint8_t instance_compaction_table[] = {350% for e in instance_entrypoints:351${e.disp_table_index},352% endfor353};354355<% assert len(physical_device_entrypoints) < 2**8 %>356static const uint8_t physical_device_compaction_table[] = {357% for e in physical_device_entrypoints:358${e.disp_table_index},359% endfor360};361362<% assert len(device_entrypoints) < 2**16 %>363static const uint16_t device_compaction_table[] = {364% for e in device_entrypoints:365${e.disp_table_index},366% endfor367};368369static bool370vk_instance_entrypoint_is_enabled(int index, uint32_t core_version,371const struct vk_instance_extension_table *instance)372{373switch (index) {374% for e in instance_entrypoints:375case ${e.entry_table_index}:376/* ${e.name} */377% if e.core_version:378return ${e.core_version.c_vk_version()} <= core_version;379% elif e.extensions:380% for ext in e.extensions:381% if ext.type == 'instance':382if (instance->${ext.name[3:]}) return true;383% else:384/* All device extensions are considered enabled at the instance level */385return true;386% endif387% endfor388return false;389% else:390return true;391% endif392% endfor393default:394return false;395}396}397398/** Return true if the core version or extension in which the given entrypoint399* is defined is enabled.400*401* If device is NULL, all device extensions are considered enabled.402*/403static bool404vk_physical_device_entrypoint_is_enabled(int index, uint32_t core_version,405const struct vk_instance_extension_table *instance)406{407switch (index) {408% for e in physical_device_entrypoints:409case ${e.entry_table_index}:410/* ${e.name} */411% if e.core_version:412return ${e.core_version.c_vk_version()} <= core_version;413% elif e.extensions:414% for ext in e.extensions:415% if ext.type == 'instance':416if (instance->${ext.name[3:]}) return true;417% else:418/* All device extensions are considered enabled at the instance level */419return true;420% endif421% endfor422return false;423% else:424return true;425% endif426% endfor427default:428return false;429}430}431432/** Return true if the core version or extension in which the given entrypoint433* is defined is enabled.434*435* If device is NULL, all device extensions are considered enabled.436*/437static bool438vk_device_entrypoint_is_enabled(int index, uint32_t core_version,439const struct vk_instance_extension_table *instance,440const struct vk_device_extension_table *device)441{442switch (index) {443% for e in device_entrypoints:444case ${e.entry_table_index}:445/* ${e.name} */446% if e.core_version:447return ${e.core_version.c_vk_version()} <= core_version;448% elif e.extensions:449% for ext in e.extensions:450% if ext.type == 'instance':451if (instance->${ext.name[3:]}) return true;452% else:453if (!device || device->${ext.name[3:]}) return true;454% endif455% endfor456return false;457% else:458return true;459% endif460% endfor461default:462return false;463}464}465466<%def name="dispatch_table_from_entrypoints(type)">467void vk_${type}_dispatch_table_from_entrypoints(468struct vk_${type}_dispatch_table *dispatch_table,469const struct vk_${type}_entrypoint_table *entrypoint_table,470bool overwrite)471{472PFN_vkVoidFunction *disp = (PFN_vkVoidFunction *)dispatch_table;473PFN_vkVoidFunction *entry = (PFN_vkVoidFunction *)entrypoint_table;474475if (overwrite) {476memset(dispatch_table, 0, sizeof(*dispatch_table));477for (unsigned i = 0; i < ARRAY_SIZE(${type}_compaction_table); i++) {478#ifdef _MSC_VER479const uintptr_t zero = 0;480if (entry[i] == NULL || memcmp(entry[i], &zero, sizeof(zero)) == 0)481#else482if (entry[i] == NULL)483#endif484continue;485unsigned disp_index = ${type}_compaction_table[i];486assert(disp[disp_index] == NULL);487disp[disp_index] = entry[i];488}489} else {490for (unsigned i = 0; i < ARRAY_SIZE(${type}_compaction_table); i++) {491unsigned disp_index = ${type}_compaction_table[i];492if (disp[disp_index] == NULL)493disp[disp_index] = entry[i];494}495}496}497</%def>498499${dispatch_table_from_entrypoints('instance')}500${dispatch_table_from_entrypoints('physical_device')}501${dispatch_table_from_entrypoints('device')}502503<%def name="lookup_funcs(type)">504static PFN_vkVoidFunction505vk_${type}_dispatch_table_get_for_entry_index(506const struct vk_${type}_dispatch_table *table, int entry_index)507{508assert(entry_index < ARRAY_SIZE(${type}_compaction_table));509int disp_index = ${type}_compaction_table[entry_index];510return ((PFN_vkVoidFunction *)table)[disp_index];511}512513PFN_vkVoidFunction514vk_${type}_dispatch_table_get(515const struct vk_${type}_dispatch_table *table, const char *name)516{517int entry_index = ${type}_string_map_lookup(name);518if (entry_index < 0)519return NULL;520521return vk_${type}_dispatch_table_get_for_entry_index(table, entry_index);522}523</%def>524525${lookup_funcs('instance')}526${lookup_funcs('physical_device')}527${lookup_funcs('device')}528529PFN_vkVoidFunction530vk_instance_dispatch_table_get_if_supported(531const struct vk_instance_dispatch_table *table,532const char *name,533uint32_t core_version,534const struct vk_instance_extension_table *instance_exts)535{536int entry_index = instance_string_map_lookup(name);537if (entry_index < 0)538return NULL;539540if (!vk_instance_entrypoint_is_enabled(entry_index, core_version,541instance_exts))542return NULL;543544return vk_instance_dispatch_table_get_for_entry_index(table, entry_index);545}546547PFN_vkVoidFunction548vk_physical_device_dispatch_table_get_if_supported(549const struct vk_physical_device_dispatch_table *table,550const char *name,551uint32_t core_version,552const struct vk_instance_extension_table *instance_exts)553{554int entry_index = physical_device_string_map_lookup(name);555if (entry_index < 0)556return NULL;557558if (!vk_physical_device_entrypoint_is_enabled(entry_index, core_version,559instance_exts))560return NULL;561562return vk_physical_device_dispatch_table_get_for_entry_index(table, entry_index);563}564565PFN_vkVoidFunction566vk_device_dispatch_table_get_if_supported(567const struct vk_device_dispatch_table *table,568const char *name,569uint32_t core_version,570const struct vk_instance_extension_table *instance_exts,571const struct vk_device_extension_table *device_exts)572{573int entry_index = device_string_map_lookup(name);574if (entry_index < 0)575return NULL;576577if (!vk_device_entrypoint_is_enabled(entry_index, core_version,578instance_exts, device_exts))579return NULL;580581return vk_device_dispatch_table_get_for_entry_index(table, entry_index);582}583584% for e in physical_device_entrypoints:585% if e.alias:586<% continue %>587% endif588% if e.guard is not None:589#ifdef ${e.guard}590% endif591static VKAPI_ATTR ${e.return_type} VKAPI_CALL592${e.prefixed_name('vk_tramp')}(${e.decl_params()})593{594<% assert e.params[0].type == 'VkPhysicalDevice' %>595VK_FROM_HANDLE(vk_physical_device, vk_physical_device, ${e.params[0].name});596% if e.return_type == 'void':597vk_physical_device->dispatch_table.${e.name}(${e.call_params()});598% else:599return vk_physical_device->dispatch_table.${e.name}(${e.call_params()});600% endif601}602% if e.guard is not None:603#endif604% endif605% endfor606607struct vk_physical_device_dispatch_table vk_physical_device_trampolines = {608% for e in physical_device_entrypoints:609% if e.alias:610<% continue %>611% endif612% if e.guard is not None:613#ifdef ${e.guard}614% endif615.${e.name} = ${e.prefixed_name('vk_tramp')},616% if e.guard is not None:617#endif618% endif619% endfor620};621622% for e in device_entrypoints:623% if e.alias:624<% continue %>625% endif626% if e.guard is not None:627#ifdef ${e.guard}628% endif629static VKAPI_ATTR ${e.return_type} VKAPI_CALL630${e.prefixed_name('vk_tramp')}(${e.decl_params()})631{632% if e.params[0].type == 'VkDevice':633VK_FROM_HANDLE(vk_device, vk_device, ${e.params[0].name});634% if e.return_type == 'void':635vk_device->dispatch_table.${e.name}(${e.call_params()});636% else:637return vk_device->dispatch_table.${e.name}(${e.call_params()});638% endif639% elif e.params[0].type in ('VkCommandBuffer', 'VkQueue'):640struct vk_object_base *vk_object = (struct vk_object_base *)${e.params[0].name};641% if e.return_type == 'void':642vk_object->device->dispatch_table.${e.name}(${e.call_params()});643% else:644return vk_object->device->dispatch_table.${e.name}(${e.call_params()});645% endif646% else:647assert(!"Unhandled device child trampoline case: ${e.params[0].type}");648% endif649}650% if e.guard is not None:651#endif652% endif653% endfor654655struct vk_device_dispatch_table vk_device_trampolines = {656% for e in device_entrypoints:657% if e.alias:658<% continue %>659% endif660% if e.guard is not None:661#ifdef ${e.guard}662% endif663.${e.name} = ${e.prefixed_name('vk_tramp')},664% if e.guard is not None:665#endif666% endif667% endfor668};669""", output_encoding='utf-8')670671U32_MASK = 2**32 - 1672673PRIME_FACTOR = 5024183674PRIME_STEP = 19675676class StringIntMapEntry(object):677def __init__(self, string, num):678self.string = string679self.num = num680681# Calculate the same hash value that we will calculate in C.682h = 0683for c in string:684h = ((h * PRIME_FACTOR) + ord(c)) & U32_MASK685self.hash = h686687self.offset = None688689def round_to_pow2(x):690return 2**int(math.ceil(math.log(x, 2)))691692class StringIntMap(object):693def __init__(self):694self.baked = False695self.strings = dict()696697def add_string(self, string, num):698assert not self.baked699assert string not in self.strings700assert 0 <= num < 2**31701self.strings[string] = StringIntMapEntry(string, num)702703def bake(self):704self.sorted_strings = \705sorted(self.strings.values(), key=lambda x: x.string)706offset = 0707for entry in self.sorted_strings:708entry.offset = offset709offset += len(entry.string) + 1710711# Save off some values that we'll need in C712self.hash_size = round_to_pow2(len(self.strings) * 1.25)713self.hash_mask = self.hash_size - 1714self.prime_factor = PRIME_FACTOR715self.prime_step = PRIME_STEP716717self.mapping = [-1] * self.hash_size718self.collisions = [0] * 10719for idx, s in enumerate(self.sorted_strings):720level = 0721h = s.hash722while self.mapping[h & self.hash_mask] >= 0:723h = h + PRIME_STEP724level = level + 1725self.collisions[min(level, 9)] += 1726self.mapping[h & self.hash_mask] = idx727728EntrypointParam = namedtuple('EntrypointParam', 'type name decl')729730class EntrypointBase(object):731def __init__(self, name):732assert name.startswith('vk')733self.name = name[2:]734self.alias = None735self.guard = None736self.entry_table_index = None737# Extensions which require this entrypoint738self.core_version = None739self.extensions = []740741def prefixed_name(self, prefix):742return prefix + '_' + self.name743744class Entrypoint(EntrypointBase):745def __init__(self, name, return_type, params, guard=None):746super(Entrypoint, self).__init__(name)747self.return_type = return_type748self.params = params749self.guard = guard750self.aliases = []751self.disp_table_index = None752753def is_physical_device_entrypoint(self):754return self.params[0].type in ('VkPhysicalDevice', )755756def is_device_entrypoint(self):757return self.params[0].type in ('VkDevice', 'VkCommandBuffer', 'VkQueue')758759def decl_params(self):760return ', '.join(p.decl for p in self.params)761762def call_params(self):763return ', '.join(p.name for p in self.params)764765class EntrypointAlias(EntrypointBase):766def __init__(self, name, entrypoint):767super(EntrypointAlias, self).__init__(name)768self.alias = entrypoint769entrypoint.aliases.append(self)770771def is_physical_device_entrypoint(self):772return self.alias.is_physical_device_entrypoint()773774def is_device_entrypoint(self):775return self.alias.is_device_entrypoint()776777def prefixed_name(self, prefix):778return self.alias.prefixed_name(prefix)779780@property781def params(self):782return self.alias.params783784@property785def return_type(self):786return self.alias.return_type787788@property789def disp_table_index(self):790return self.alias.disp_table_index791792def decl_params(self):793return self.alias.decl_params()794795def call_params(self):796return self.alias.call_params()797798def get_entrypoints(doc, entrypoints_to_defines):799"""Extract the entry points from the registry."""800entrypoints = OrderedDict()801802for command in doc.findall('./commands/command'):803if 'alias' in command.attrib:804alias = command.attrib['name']805target = command.attrib['alias']806entrypoints[alias] = EntrypointAlias(alias, entrypoints[target])807else:808name = command.find('./proto/name').text809ret_type = command.find('./proto/type').text810params = [EntrypointParam(811type=p.find('./type').text,812name=p.find('./name').text,813decl=''.join(p.itertext())814) for p in command.findall('./param')]815guard = entrypoints_to_defines.get(name)816# They really need to be unique817assert name not in entrypoints818entrypoints[name] = Entrypoint(name, ret_type, params, guard)819820for feature in doc.findall('./feature'):821assert feature.attrib['api'] == 'vulkan'822version = VkVersion(feature.attrib['number'])823for command in feature.findall('./require/command'):824e = entrypoints[command.attrib['name']]825assert e.core_version is None826e.core_version = version827828for extension in doc.findall('.extensions/extension'):829if extension.attrib['supported'] != 'vulkan':830continue831832ext_name = extension.attrib['name']833834ext = Extension(ext_name, 1, True)835ext.type = extension.attrib['type']836837for command in extension.findall('./require/command'):838e = entrypoints[command.attrib['name']]839assert e.core_version is None840e.extensions.append(ext)841842return entrypoints.values()843844845def get_entrypoints_defines(doc):846"""Maps entry points to extension defines."""847entrypoints_to_defines = {}848849platform_define = {}850for platform in doc.findall('./platforms/platform'):851name = platform.attrib['name']852define = platform.attrib['protect']853platform_define[name] = define854855for extension in doc.findall('./extensions/extension[@platform]'):856platform = extension.attrib['platform']857define = platform_define[platform]858859for entrypoint in extension.findall('./require/command'):860fullname = entrypoint.attrib['name']861entrypoints_to_defines[fullname] = define862863return entrypoints_to_defines864865def get_entrypoints_from_xml(xml_files):866entrypoints = []867868for filename in xml_files:869doc = et.parse(filename)870entrypoints += get_entrypoints(doc, get_entrypoints_defines(doc))871872return entrypoints873874def main():875parser = argparse.ArgumentParser()876parser.add_argument('--out-c', help='Output C file.')877parser.add_argument('--out-h', help='Output H file.')878parser.add_argument('--xml',879help='Vulkan API XML file.',880required=True,881action='append',882dest='xml_files')883args = parser.parse_args()884885entrypoints = get_entrypoints_from_xml(args.xml_files)886887device_entrypoints = []888physical_device_entrypoints = []889instance_entrypoints = []890for e in entrypoints:891if e.is_device_entrypoint():892device_entrypoints.append(e)893elif e.is_physical_device_entrypoint():894physical_device_entrypoints.append(e)895else:896instance_entrypoints.append(e)897898for i, e in enumerate(e for e in device_entrypoints if not e.alias):899e.disp_table_index = i900901device_strmap = StringIntMap()902for i, e in enumerate(device_entrypoints):903e.entry_table_index = i904device_strmap.add_string("vk" + e.name, e.entry_table_index)905device_strmap.bake()906907for i, e in enumerate(e for e in physical_device_entrypoints if not e.alias):908e.disp_table_index = i909910physical_device_strmap = StringIntMap()911for i, e in enumerate(physical_device_entrypoints):912e.entry_table_index = i913physical_device_strmap.add_string("vk" + e.name, e.entry_table_index)914physical_device_strmap.bake()915916for i, e in enumerate(e for e in instance_entrypoints if not e.alias):917e.disp_table_index = i918919instance_strmap = StringIntMap()920for i, e in enumerate(instance_entrypoints):921e.entry_table_index = i922instance_strmap.add_string("vk" + e.name, e.entry_table_index)923instance_strmap.bake()924925# For outputting entrypoints.h we generate a anv_EntryPoint() prototype926# per entry point.927try:928if args.out_h:929with open(args.out_h, 'wb') as f:930f.write(TEMPLATE_H.render(instance_entrypoints=instance_entrypoints,931physical_device_entrypoints=physical_device_entrypoints,932device_entrypoints=device_entrypoints,933filename=os.path.basename(__file__)))934if args.out_c:935with open(args.out_c, 'wb') as f:936f.write(TEMPLATE_C.render(instance_entrypoints=instance_entrypoints,937physical_device_entrypoints=physical_device_entrypoints,938device_entrypoints=device_entrypoints,939instance_strmap=instance_strmap,940physical_device_strmap=physical_device_strmap,941device_strmap=device_strmap,942filename=os.path.basename(__file__)))943except Exception:944# In the event there's an error, this imports some helpers from mako945# to print a useful stack trace and prints it, then exits with946# status 1, if python is run with debug; otherwise it just raises947# the exception948if __debug__:949import sys950from mako import exceptions951sys.stderr.write(exceptions.text_error_template().render() + '\n')952sys.exit(1)953raise954955956if __name__ == '__main__':957main()958959960