Path: blob/21.2-virgl/src/vulkan/util/vk_extensions_gen.py
7233 views
COPYRIGHT = """\1/*2* Copyright 2017 Intel Corporation3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the6* "Software"), to deal in the Software without restriction, including7* without limitation the rights to use, copy, modify, merge, publish,8* distribute, sub license, and/or sell copies of the Software, and to9* permit persons to whom the Software is furnished to do so, subject to10* the following conditions:11*12* The above copyright notice and this permission notice (including the13* next paragraph) shall be included in all copies or substantial portions14* of the Software.15*16* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS17* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.19* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR20* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,21* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE22* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23*/24"""2526import xml.etree.ElementTree as et2728from mako.template import Template2930# Mesa-local imports must be declared in meson variable31# '{file_without_suffix}_depend_files'.32from vk_extensions import *3334_TEMPLATE_H = Template(COPYRIGHT + """3536#ifndef ${driver.upper()}_EXTENSIONS_H37#define ${driver.upper()}_EXTENSIONS_H3839#include <stdbool.h>4041%for include in includes:42#include "${include}"43%endfor4445%if driver == 'vk':46#define VK_INSTANCE_EXTENSION_COUNT ${len(instance_extensions)}4748extern const VkExtensionProperties vk_instance_extensions[];4950struct vk_instance_extension_table {51union {52bool extensions[VK_INSTANCE_EXTENSION_COUNT];53struct {54%for ext in instance_extensions:55bool ${ext.name[3:]};56%endfor57};58};59};606162#define VK_DEVICE_EXTENSION_COUNT ${len(device_extensions)}6364extern const VkExtensionProperties vk_device_extensions[];6566struct vk_device_extension_table {67union {68bool extensions[VK_DEVICE_EXTENSION_COUNT];69struct {70%for ext in device_extensions:71bool ${ext.name[3:]};72%endfor73};74};75};76%else:77#include "vk_extensions.h"78%endif7980struct ${driver}_physical_device;8182%if driver == 'vk':83#ifdef ANDROID84extern const struct vk_instance_extension_table vk_android_allowed_instance_extensions;85extern const struct vk_device_extension_table vk_android_allowed_device_extensions;86#endif87%else:88extern const struct vk_instance_extension_table ${driver}_instance_extensions_supported;8990void91${driver}_physical_device_get_supported_extensions(const struct ${driver}_physical_device *device,92struct vk_device_extension_table *extensions);93%endif9495#endif /* ${driver.upper()}_EXTENSIONS_H */96""")9798_TEMPLATE_C = Template(COPYRIGHT + """99%if driver == 'vk':100#include "vk_object.h"101%else:102#include "${driver}_private.h"103%endif104105#include "${driver}_extensions.h"106107%if driver == 'vk':108const VkExtensionProperties ${driver}_instance_extensions[${driver.upper()}_INSTANCE_EXTENSION_COUNT] = {109%for ext in instance_extensions:110{"${ext.name}", ${ext.ext_version}},111%endfor112};113114const VkExtensionProperties ${driver}_device_extensions[${driver.upper()}_DEVICE_EXTENSION_COUNT] = {115%for ext in device_extensions:116{"${ext.name}", ${ext.ext_version}},117%endfor118};119120#ifdef ANDROID121const struct vk_instance_extension_table vk_android_allowed_instance_extensions = {122%for ext in instance_extensions:123.${ext.name[3:]} = ${ext.c_android_condition()},124%endfor125};126127extern const struct vk_device_extension_table vk_android_allowed_device_extensions = {128%for ext in device_extensions:129.${ext.name[3:]} = ${ext.c_android_condition()},130%endfor131};132#endif133%endif134135%if driver != 'vk':136#include "vk_util.h"137138/* Convert the VK_USE_PLATFORM_* defines to booleans */139%for platform_define in platform_defines:140#ifdef ${platform_define}141# undef ${platform_define}142# define ${platform_define} true143#else144# define ${platform_define} false145#endif146%endfor147148/* And ANDROID too */149#ifdef ANDROID150# undef ANDROID151# define ANDROID true152#else153# define ANDROID false154# define ANDROID_API_LEVEL 0155#endif156157#define ${driver.upper()}_HAS_SURFACE (VK_USE_PLATFORM_WIN32_KHR || \\158VK_USE_PLATFORM_WAYLAND_KHR || \\159VK_USE_PLATFORM_XCB_KHR || \\160VK_USE_PLATFORM_XLIB_KHR || \\161VK_USE_PLATFORM_DISPLAY_KHR)162163static const uint32_t MAX_API_VERSION = ${MAX_API_VERSION.c_vk_version()};164165VKAPI_ATTR VkResult VKAPI_CALL ${driver}_EnumerateInstanceVersion(166uint32_t* pApiVersion)167{168*pApiVersion = MAX_API_VERSION;169return VK_SUCCESS;170}171172const struct vk_instance_extension_table ${driver}_instance_extensions_supported = {173%for ext in instance_extensions:174.${ext.name[3:]} = ${ext.enable},175%endfor176};177178uint32_t179${driver}_physical_device_api_version(struct ${driver}_physical_device *device)180{181uint32_t version = 0;182183uint32_t override = vk_get_version_override();184if (override)185return MIN2(override, MAX_API_VERSION);186187%for version in API_VERSIONS:188if (!(${version.enable}))189return version;190version = ${version.version.c_vk_version()};191192%endfor193return version;194}195196void197${driver}_physical_device_get_supported_extensions(const struct ${driver}_physical_device *device,198struct vk_device_extension_table *extensions)199{200*extensions = (struct vk_device_extension_table) {201%for ext in device_extensions:202.${ext.name[3:]} = ${ext.enable},203%endfor204};205}206%endif207""")208209def gen_extensions(driver, xml_files, api_versions, max_api_version,210extensions, out_c, out_h, includes = []):211platform_defines = []212for filename in xml_files:213init_exts_from_xml(filename, extensions, platform_defines)214215for ext in extensions:216assert ext.type == 'instance' or ext.type == 'device'217218template_env = {219'driver': driver,220'API_VERSIONS': api_versions,221'MAX_API_VERSION': max_api_version,222'instance_extensions': [e for e in extensions if e.type == 'instance'],223'device_extensions': [e for e in extensions if e.type == 'device'],224'platform_defines': platform_defines,225'includes': includes,226}227228if out_h:229with open(out_h, 'w') as f:230f.write(_TEMPLATE_H.render(**template_env))231232if out_c:233with open(out_c, 'w') as f:234f.write(_TEMPLATE_C.render(**template_env))235236237if __name__ == '__main__':238parser = argparse.ArgumentParser()239parser.add_argument('--out-c', help='Output C file.')240parser.add_argument('--out-h', help='Output H file.')241parser.add_argument('--xml',242help='Vulkan API XML file.',243required=True,244action='append',245dest='xml_files')246args = parser.parse_args()247248extensions = []249for filename in args.xml_files:250extensions += get_all_exts_from_xml(filename)251252gen_extensions('vk', args.xml_files, None, None,253extensions, args.out_c, args.out_h, [])254255256