Update to latest headers & fix errors & cleanup generation script

VulkanSC was added to vk.xml and broke the generation script. This was an
easy fix, just needed to specify which API the version information should
use. Still, took the time to cleanup the code by running pylint and fixing
anything it warned about.
This commit is contained in:
Charles Giessen 2023-09-19 14:32:22 -06:00 committed by Charles Giessen
parent 61f77612c7
commit 04ec13b48d
9 changed files with 1432 additions and 1679 deletions

View File

@ -7,6 +7,7 @@ option(CATCH_BUILD_TESTING "" OFF)
option(CATCH_ENABLE_WERROR "" OFF) option(CATCH_ENABLE_WERROR "" OFF)
option(CATCH_INSTALL_DOCS "" OFF) option(CATCH_INSTALL_DOCS "" OFF)
option(CATCH_INSTALL_HELPERS "" OFF) option(CATCH_INSTALL_HELPERS "" OFF)
option(CATCH_INSTALL_EXTRAS "" ON)
set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1) #remove Catch2 target spam set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1) #remove Catch2 target spam
include(FetchContent) include(FetchContent)
@ -19,6 +20,7 @@ FetchContent_MakeAvailable(glfw)
FetchContent_Declare( FetchContent_Declare(
Catch2 Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2
GIT_TAG v2.13.7 GIT_TAG v3.4.0
) )
FetchContent_MakeAvailable(Catch2) FetchContent_MakeAvailable(Catch2)

View File

@ -1,2 +1,2 @@
set(VK_BOOTSTRAP_SOURCE_HEADER_VERSION 1.3.240) set(VK_BOOTSTRAP_SOURCE_HEADER_VERSION 1.3.264)
set(VK_BOOTSTRAP_SOURCE_HEADER_VERSION_GIT_TAG v1.3.240) set(VK_BOOTSTRAP_SOURCE_HEADER_VERSION_GIT_TAG v1.3.264)

View File

@ -31,16 +31,27 @@
# Command Line Arguments # Command Line Arguments
# [--auto] Don't ask for input from the command line # [--auto] Don't ask for input from the command line
import sys
import os
import subprocess
import copy
import codecs
import re
from string import Template
import urllib.request
import pkg_resources
# Exclusions # Exclusions
exclusions = [ exclusions = [
'vkGetDeviceProcAddr', 'vkGetDeviceProcAddr',
'vkCreateDevice', 'vkCreateDevice',
'vkDestroyDevice' 'vkDestroyDevice'
] ]
# Excluded extension authors - don't generate anything for these types of extensions # Excluded extension authors - don't generate anything for these types of extensions
excluded_extension_authors = [ excluded_extension_authors = [
'NVX' 'NVX'
] ]
excluded_alias_types = [ excluded_alias_types = [
@ -48,48 +59,38 @@ excluded_alias_types = [
] ]
# Check for/install xmltodict # Check for/install xmltodict
import sys
import os
import subprocess
import pkg_resources
import copy
import codecs
import re
from string import Template
installed = {pkg.key for pkg in pkg_resources.working_set} installed = {pkg.key for pkg in pkg_resources.working_set}
xmltodict_missing = {'xmltodict'} - installed xmltodict_missing = {'xmltodict'} - installed
# Install xmltodict # Install xmltodict
if xmltodict_missing: if xmltodict_missing:
if '--auto' not in sys.argv: if '--auto' not in sys.argv:
val = input("xmltodict is required to run this script. Would you like to install? (y/n): ") val = input('xmltodict is required to run this script. Would you like to install? (y/n): ')
else: else:
val = "y" val = 'y'
if(val.lower() == "y"): if val.lower() == 'y':
try: try:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'xmltodict']) subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'xmltodict'])
except subprocess.CalledProcessError as error: except subprocess.CalledProcessError as error:
print("Failed to install xmltodict due to error:") print('Failed to install xmltodict due to error:')
print(error) print(error)
if '--auto' not in sys.argv: if '--auto' not in sys.argv:
input("Press Enter to continue...") input('Press Enter to continue...')
sys.exit() sys.exit()
else: else:
sys.exit() sys.exit()
# Fetch fresh vk.xml from Khronos repo # Fetch fresh vk.xml from Khronos repo
import urllib.request
import xmltodict import xmltodict
try: try:
response = urllib.request.urlopen('https://raw.githubusercontent.com/KhronosGroup/Vulkan-Headers/main/registry/vk.xml') response = urllib.request.urlopen('https://raw.githubusercontent.com/KhronosGroup/Vulkan-Headers/main/registry/vk.xml')
except urllib.error.URLError as error: except urllib.error.URLError as error:
print("Failed to download vk.xml due to error:") print('Failed to download vk.xml due to error:')
print(error.reason) print(error.reason)
if '-q' not in sys.argv: if '-q' not in sys.argv:
input("Press Enter to continue...") input('Press Enter to continue...')
sys.exit() sys.exit()
vk_xml_raw = response.read() vk_xml_raw = response.read()
vk_xml = xmltodict.parse(vk_xml_raw,process_namespaces=True) vk_xml = xmltodict.parse(vk_xml_raw,process_namespaces=True)
@ -101,124 +102,130 @@ device_commands = {}
aliased_types = {} aliased_types = {}
types_node = vk_xml['registry']['types']['type'] types_node = vk_xml['registry']['types']['type']
for type_node in types_node: for type_node in types_node:
if '@alias' in type_node: if '@alias' in type_node:
aliased_types[type_node['@alias']] = type_node['@name'] aliased_types[type_node['@alias']] = type_node['@name']
# Gather all device functions/aliases for filtering core/extension function fetching # Gather all device functions/aliases for filtering core/extension function fetching
commands_node = vk_xml['registry']['commands']['command'] commands_node = vk_xml['registry']['commands']['command']
aliases = {} aliases = {}
for command_node in commands_node: for command_node in commands_node:
if 'proto' in command_node: if 'proto' in command_node:
command_name = command_node['proto']['name'] command_name = command_node['proto']['name']
new_command_params = copy.deepcopy(command_params) new_command_params = copy.deepcopy(command_params)
new_command_params['return_type'] = command_node['proto']['type'] new_command_params['return_type'] = command_node['proto']['type']
if type(command_node['param']) is list: if isinstance(command_node['param'], list):
new_command_params['args'] = command_node['param'] params = command_node['param']
else: else:
new_command_params['args'] = [command_node['param']] params = [command_node['param']]
if not command_name in exclusions: new_command_params['args'] = []
if new_command_params['args'][0]['type'] in ['VkDevice', 'VkCommandBuffer', 'VkQueue']: for param in params:
device_commands[command_name] = new_command_params # if the api attribute does exist, make sure it is for vulkan
device_commands[command_name]['is_alias'] = False if not '@api' in param or param['@api'] == 'vulkan':
elif '@alias' in command_node: new_command_params['args'].append(param)
aliases[command_node['@alias']] = command_node['@name'] if not command_name in exclusions:
if new_command_params['args'][0]['type'] in ['VkDevice', 'VkCommandBuffer', 'VkQueue']:
device_commands[command_name] = new_command_params
device_commands[command_name]['is_alias'] = False
elif '@alias' in command_node:
aliases[command_node['@alias']] = command_node['@name']
# Push the alias name as a device function if the alias exists in device commands # Push the alias name as a device function if the alias exists in device commands
for alias in aliases: for alias_name, alias in aliases.items():
if alias in device_commands: if alias in device_commands:
device_commands[aliases[alias]] = copy.deepcopy(device_commands[alias]) device_commands[alias] = copy.deepcopy(device_commands[alias_name])
device_commands[aliases[alias]]['is_alias'] = True device_commands[alias]['is_alias'] = True
# Add requirements for core PFN's # Add requirements for core PFN's
features_node = vk_xml['registry']['feature'] features_node = vk_xml['registry']['feature']
for feature_node in features_node: for feature_node in features_node:
if feature_node['@name'] != 'VK_VERSION_1_0': if feature_node['@name'] != 'VK_VERSION_1_0':
for require_node in feature_node['require']: for require_node in feature_node['require']:
for param_node in require_node: for param_node in require_node:
if param_node == 'command': if param_node == 'command':
if type(require_node[param_node]) is not list: if not isinstance(require_node[param_node], list):
require_node[param_node] = [require_node[param_node]] require_node[param_node] = [require_node[param_node]]
for param in require_node[param_node]: for param in require_node[param_node]:
if param['@name'] in device_commands: if param['@name'] in device_commands:
device_commands[param['@name']]['requirements'] += [[feature_node['@name']]] device_commands[param['@name']]['requirements'] += [[feature_node['@name']]]
# Add requirements for extension PFN's # Add requirements for extension PFN's
extensions_node = vk_xml['registry']['extensions']['extension'] extensions_node = vk_xml['registry']['extensions']['extension']
for extension_node in extensions_node: for extension_node in extensions_node:
extension_name = extension_node['@name'] extension_name = extension_node['@name']
if 'require' in extension_node.keys(): if 'require' in extension_node.keys():
require_nodes = extension_node['require'] require_nodes = extension_node['require']
for require_node in require_nodes: for require_node in require_nodes:
requirements = [extension_name] requirements = [extension_name]
if type(require_node) is not str: if not isinstance(require_node, str):
if 'command' in require_node.keys(): if 'command' in require_node.keys():
if '@feature' in require_node.keys(): if '@feature' in require_node.keys():
requirements.append(require_node['@feature']) requirements.append(require_node['@feature'])
if '@extension' in require_node.keys(): if '@extension' in require_node.keys():
requirements.extend(require_node['@extension'].split(',')) requirements.extend(require_node['@extension'].split(','))
if type(require_node['command']) is not list: if not isinstance(require_node['command'], list):
require_node['command'] = [require_node['command']] require_node['command'] = [require_node['command']]
for command_node in require_node['command']: for command_node in require_node['command']:
if command_node['@name'] in device_commands: if command_node['@name'] in device_commands:
if '@author' in extension_node and extension_node['@author'] in excluded_extension_authors: if '@author' in extension_node and extension_node['@author'] in excluded_extension_authors:
device_commands.pop(command_node['@name']) device_commands.pop(command_node['@name'])
else: else:
device_commands[command_node['@name']]['requirements'] += [requirements] device_commands[command_node['@name']]['requirements'] += [requirements]
elif require_node == 'command': elif require_node == 'command':
if type(require_nodes['command']) is not list: if not isinstance(require_nodes['command'], list):
require_nodes['command'] = [require_nodes['command']] require_nodes['command'] = [require_nodes['command']]
for command_node in require_nodes['command']: for command_node in require_nodes['command']:
if command_node['@name'] in device_commands: if command_node['@name'] in device_commands:
if '@author' in extension_node and extension_node['@author'] in excluded_extension_authors: if '@author' in extension_node and extension_node['@author'] in excluded_extension_authors:
device_commands.pop(command_node['@name']) device_commands.pop(command_node['@name'])
else: else:
device_commands[command_node['@name']]['requirements'] += [requirements] device_commands[command_node['@name']]['requirements'] += [requirements]
# Generate macro templates # Generate macro templates
for command in device_commands: for command_name, command in device_commands.items():
macro = '' macro = ''
requirements_collection = device_commands[command]['requirements'] requirements_collection = device_commands[command_name]['requirements']
collection_count = len(requirements_collection) collection_count = len(requirements_collection)
if collection_count > 0: if collection_count > 0:
macro = '#if ' macro = '#if '
while(collection_count > 0): while collection_count > 0:
for requirements in requirements_collection: for requirements in requirements_collection:
requirements_count = len(requirements) requirements_count = len(requirements)
macro += '(' macro += '('
for requirement in requirements: for requirement in requirements:
macro += 'defined({})'.format(requirement) macro += f'defined({requirement})'
requirements_count -= 1 requirements_count -= 1
if requirements_count > 0: if requirements_count > 0:
macro += ' && ' macro += ' && '
macro += ')' macro += ')'
if collection_count > 0: if collection_count > 0:
collection_count -= 1 collection_count -= 1
if collection_count > 0: if collection_count > 0:
macro += ' || ' macro += ' || '
macro += '\n$body#endif\n' macro += '\n$body#endif\n'
else: else:
macro = '$body' macro = '$body'
device_commands[command]['macro_template'] = Template(macro) device_commands[command_name]['macro_template'] = Template(macro)
# License # License
license = '/* \n' dispatch_license = '''/*
license += ' * Copyright © 2021 Cody Goodson (contact@vibimanx.com)\n' * Copyright © 2021 Cody Goodson (contact@vibimanx.com)
license += ' * Copyright © 2022 Charles Giessen (charles@lunarg.com)\n' * Copyright © 2022 Charles Giessen (charles@lunarg.com)
license += ' * \n' *
license += ' * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated\n' * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
license += ' * documentation files (the “Software”), to deal in the Software without restriction, including without\n' * documentation files (the Software), to deal in the Software without restriction, including without
license += ' * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\n' * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
license += ' * of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n' * of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
license += ' * \n' *
license += ' * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n' * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
license += ' * \n' *
license += ' * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT\n' * THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
license += ' * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n' * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
license += ' * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\n' * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
license += ' * 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.\n' * 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.
license += ' * \n' *
license += ' */\n\n' */
'''
# Info # Info
info = '// This file is a part of VkBootstrap\n' info = '// This file is a part of VkBootstrap\n'
@ -239,83 +246,83 @@ proxy_template = Template('\t$return_type $proxy_name($args_full) const noexcept
fp_decl_template = Template('\t$pfn_name $fp_name = nullptr;\n') fp_decl_template = Template('\t$pfn_name $fp_name = nullptr;\n')
pfn_load_template = Template('\t\t$fp_name = reinterpret_cast<$pfn_name>(procAddr(device, "$command_name"));\n') pfn_load_template = Template('\t\t$fp_name = reinterpret_cast<$pfn_name>(procAddr(device, "$command_name"));\n')
for command in device_commands: for command_name, command in device_commands.items():
params = device_commands[command] params = device_commands[command]
# easy stuff out of the way # easy stuff out of the way
return_type = params['return_type'] return_type = params['return_type']
if(return_type != 'void'): if return_type != 'void':
opt_return = 'return ' opt_return = 'return '
else: else:
opt_return = '' opt_return = ''
proxy_name = command[2].lower() + command[3:] proxy_name = command_name[2].lower() + command_name[3:]
fp_name = 'fp_' + command fp_name = 'fp_' + command_name
pfn_name = 'PFN_' + command pfn_name = 'PFN_' + command_name
# Now for args # Now for args
arg_template = Template('$front_mods$arg_type$back_mods$arg_name$array') arg_template = Template('$front_mods$arg_type$back_mods$arg_name$array')
args_full = '' args_full = ''
args_names = '' args_names = ''
args_count = len(params['args']) args_count = len(params['args'])
i = args_count i = args_count
for arg in params['args']: for arg in params['args']:
front_mods = '' front_mods = ''
back_mods = ' ' back_mods = ' '
array = '' array = ''
arg_type = arg['type'] arg_type = arg['type']
arg_name = arg['name'] arg_name = arg['name']
if '#text' in arg: if '#text' in arg:
text = arg['#text'] text = arg['#text']
text = text.replace(' ', '') text = text.replace(' ', '')
array_index = text.find('[') array_index = text.find('[')
if array_index != -1: if array_index != -1:
array = text[array_index:] array = text[array_index:]
text = text[0:array_index] text = text[0:array_index]
if text == '*': if text == '*':
front_mods = '' front_mods = ''
back_mods = '* ' back_mods = '* '
elif text == '**': elif text == '**':
front_mods = '' front_mods = ''
back_mods = '** ' back_mods = '** '
elif text == 'struct**': elif text == 'struct**':
front_mods = 'struct ' front_mods = 'struct '
back_mods = '** ' back_mods = '** '
elif text == 'const*': elif text == 'const*':
front_mods = 'const ' front_mods = 'const '
back_mods = '* ' back_mods = '* '
elif text == 'const**': elif text == 'const**':
front_mods = 'const ' front_mods = 'const '
back_mods = '** ' back_mods = '** '
elif text == 'const*const*': elif text == 'const*const*':
front_mods = 'const ' front_mods = 'const '
back_mods = '* const* ' back_mods = '* const* '
elif text == 'conststruct*': elif text == 'conststruct*':
front_mods = 'const struct ' front_mods = 'const struct '
back_mods = '* ' back_mods = '* '
if i == args_count and arg_type == 'VkDevice': if i == args_count and arg_type == 'VkDevice':
args_names += arg_name args_names += arg_name
if i > 0: if i > 0:
i -= 1 i -= 1
if i > 0: if i > 0:
args_names += ', ' args_names += ', '
else: else:
if arg_type in aliased_types and arg_type not in excluded_alias_types: if arg_type in aliased_types and arg_type not in excluded_alias_types:
arg_type = aliased_types[arg_type] arg_type = aliased_types[arg_type]
args_full += arg_template.substitute(front_mods = front_mods, arg_type = arg_type, back_mods = back_mods, arg_name = arg_name, array = array) args_full += arg_template.substitute(front_mods = front_mods, arg_type = arg_type, back_mods = back_mods, arg_name = arg_name, array = array)
args_names += arg_name args_names += arg_name
if i > 0: if i > 0:
i -= 1 i -= 1
if i > 0: if i > 0:
args_full += ', ' args_full += ', '
args_names += ', ' args_names += ', '
proxy_body = proxy_template.substitute(return_type = return_type, proxy_name = proxy_name, args_full = args_full, opt_return = opt_return, fp_name = fp_name, args_names = args_names) proxy_body = proxy_template.substitute(return_type = return_type, proxy_name = proxy_name, args_full = args_full, opt_return = opt_return, fp_name = fp_name, args_names = args_names)
fp_decl_body = fp_decl_template.substitute(pfn_name = pfn_name, fp_name = fp_name) fp_decl_body = fp_decl_template.substitute(pfn_name = pfn_name, fp_name = fp_name)
pfn_load_body = pfn_load_template.substitute(fp_name = fp_name, pfn_name = pfn_name, command_name = command) pfn_load_body = pfn_load_template.substitute(fp_name = fp_name, pfn_name = pfn_name, command_name = command)
macro_template = params['macro_template'] macro_template = params['macro_template']
proxy_section += macro_template.substitute(body=proxy_body) proxy_section += macro_template.substitute(body=proxy_body)
fp_decl_section += macro_template.substitute(body=fp_decl_body) fp_decl_section += macro_template.substitute(body=fp_decl_body)
pfn_load_section +=macro_template.substitute(body=pfn_load_body) pfn_load_section +=macro_template.substitute(body=pfn_load_body)
body += pfn_load_section body += pfn_load_section
body += '\t}\n' body += '\t}\n'
@ -330,39 +337,39 @@ body += '} // namespace vkb'
# find the version used to generate the code # find the version used to generate the code
for type_node in types_node: for type_node in types_node:
if 'name' in type_node and type_node['name'] == 'VK_HEADER_VERSION_COMPLETE': if '@api' in type_node and type_node['@api'] == 'vulkan' and 'name' in type_node and type_node['name'] == 'VK_HEADER_VERSION_COMPLETE':
complete_header_version = type_node["#text"] complete_header_version = type_node['#text']
if 'name' in type_node and type_node['name'] == 'VK_HEADER_VERSION': if '@api' in type_node and type_node['@api'] == 'vulkan' and 'name' in type_node and type_node['name'] == 'VK_HEADER_VERSION':
vk_header_version = type_node['#text'] vk_header_version = type_node['#text']
find_number_fields = re.compile('[0-9]+') find_number_fields = re.compile('[0-9]+')
version_fields = find_number_fields.findall(complete_header_version) version_fields = find_number_fields.findall(complete_header_version)
header_version_field = find_number_fields.findall(vk_header_version)[0] header_version_field = find_number_fields.findall(vk_header_version)[0]
version_tag = f'{version_fields[1]}.{version_fields[2]}.{header_version_field}' version_tag = f'{version_fields[1]}.{version_fields[2]}.{header_version_field}'
header = license + info + body header = dispatch_license + info + body
path_to_src = os.path.join('src') path_to_src = os.path.join('src')
if not os.path.exists(path_to_src): if not os.path.exists(path_to_src):
path_to_src = os.path.join('..', 'src') path_to_src = os.path.join('..', 'src')
if not os.path.exists(path_to_src): if not os.path.exists(path_to_src):
print("Couldn't find source folder. Is the current directory wrong?") print('Could not find source folder. Is the current directory wrong?')
sys.exit() sys.exit()
header_file = codecs.open(os.path.join(path_to_src,"VkBootstrapDispatch.h"), "w", "utf-8") header_file = codecs.open(os.path.join(path_to_src,'VkBootstrapDispatch.h'), 'w', 'utf-8')
header_file.write(header) header_file.write(header)
header_file.close() header_file.close()
path_to_gen = os.path.join('gen') path_to_gen = os.path.join('gen')
if not os.path.exists(path_to_gen): if not os.path.exists(path_to_gen):
path_to_gen = os.path.join('..', 'gen') path_to_gen = os.path.join('..', 'gen')
if not os.path.exists(path_to_gen): if not os.path.exists(path_to_gen):
print("Couldn't find gen folder. Is the current directory wrong?") print('Could not find gen folder. Is the current directory wrong?')
sys.exit() sys.exit()
# Generate a CMake file that contains the header version used. # Generate a CMake file that contains the header version used.
cmake_version_file = codecs.open(os.path.join(path_to_gen,"CurrentBuildVulkanVersion.cmake"), "w", "utf-8") cmake_version_file = codecs.open(os.path.join(path_to_gen,'CurrentBuildVulkanVersion.cmake'), 'w', 'utf-8')
cmake_version_file.write(f'set(VK_BOOTSTRAP_SOURCE_HEADER_VERSION {version_tag})\n') cmake_version_file.write(f'set(VK_BOOTSTRAP_SOURCE_HEADER_VERSION {version_tag})\n')
cmake_version_file.write(f'set(VK_BOOTSTRAP_SOURCE_HEADER_VERSION_GIT_TAG v{version_tag})\n') cmake_version_file.write(f'set(VK_BOOTSTRAP_SOURCE_HEADER_VERSION_GIT_TAG v{version_tag})\n')
cmake_version_file.close() cmake_version_file.close()
print("Generation finished.") print('Generation finished.')

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,19 @@
add_executable(vk-bootstrap-test main.cpp bootstrap_tests.cpp error_code_tests.cpp unit_tests.cpp) add_executable(vk-bootstrap-test
bootstrap_tests.cpp
error_code_tests.cpp
unit_tests.cpp)
target_link_libraries(vk-bootstrap-test target_link_libraries(vk-bootstrap-test
PRIVATE PRIVATE
vk-bootstrap vk-bootstrap
vk-bootstrap-vulkan-headers vk-bootstrap-vulkan-headers
vk-bootstrap-compiler-warnings vk-bootstrap-compiler-warnings
glfw glfw
Catch2 Catch2::Catch2WithMain
) )
# This should be how to make CTest aware of the tests, but unfortunately it fails to link. The tests run fine on their own, so this is left as a TODO
# list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
# include(CTest)
# include(Catch)
# catch_discover_tests(vk-bootstrap-test)

File diff suppressed because it is too large Load Diff

View File

@ -1,98 +1,88 @@
#include <catch2/catch.hpp> #include <catch2/catch_test_macros.hpp>
#include "VkBootstrap.h" #include "VkBootstrap.h"
TEST_CASE ("is_error_code_enum", "[VkBootstrap.error_code]") { TEST_CASE("is_error_code_enum", "[VkBootstrap.error_code]") {
STATIC_REQUIRE (std::is_error_code_enum<vkb::InstanceError>::value); STATIC_REQUIRE(std::is_error_code_enum<vkb::InstanceError>::value);
STATIC_REQUIRE (std::is_error_code_enum<vkb::PhysicalDeviceError>::value); STATIC_REQUIRE(std::is_error_code_enum<vkb::PhysicalDeviceError>::value);
STATIC_REQUIRE (std::is_error_code_enum<vkb::QueueError>::value); STATIC_REQUIRE(std::is_error_code_enum<vkb::QueueError>::value);
STATIC_REQUIRE (std::is_error_code_enum<vkb::DeviceError>::value); STATIC_REQUIRE(std::is_error_code_enum<vkb::DeviceError>::value);
STATIC_REQUIRE (std::is_error_code_enum<vkb::SwapchainError>::value); STATIC_REQUIRE(std::is_error_code_enum<vkb::SwapchainError>::value);
} }
TEST_CASE ("make_error_code", "[VkBootstrap.error_code]") { TEST_CASE("make_error_code", "[VkBootstrap.error_code]") {
GIVEN ("An InstanceError") { GIVEN("An InstanceError") {
const auto error = vkb::InstanceError::vulkan_unavailable; const auto error = vkb::InstanceError::vulkan_unavailable;
WHEN ("Creating an error code from it") { WHEN("Creating an error code from it") {
std::error_code ec = make_error_code (error); std::error_code ec = make_error_code(error);
THEN ("The error code is equal to the original error") { REQUIRE (ec == error); } THEN("The error code is equal to the original error") { REQUIRE(ec == error); }
THEN ("The error code is not equal to an unrelated error") { THEN("The error code is not equal to an unrelated error") {
REQUIRE (ec != vkb::InstanceError::failed_create_instance); REQUIRE(ec != vkb::InstanceError::failed_create_instance);
} }
THEN ("We can get the error message") { THEN("We can get the error message") { REQUIRE(ec.message() == "vulkan_unavailable"); }
REQUIRE (ec.message () == "vulkan_unavailable"); }
} }
}
}
GIVEN ("A PhysicalDeviceError") { GIVEN("A PhysicalDeviceError") {
const auto error = vkb::PhysicalDeviceError::no_physical_devices_found; const auto error = vkb::PhysicalDeviceError::no_physical_devices_found;
WHEN ("Creating an error code from it") { WHEN("Creating an error code from it") {
std::error_code ec = make_error_code (error); std::error_code ec = make_error_code(error);
THEN ("The error code is equal to the original error") { REQUIRE (ec == error); } THEN("The error code is equal to the original error") { REQUIRE(ec == error); }
THEN ("The error code is not equal to an unrelated error") { THEN("The error code is not equal to an unrelated error") {
REQUIRE (ec != vkb::InstanceError::failed_create_instance); REQUIRE(ec != vkb::InstanceError::failed_create_instance);
} }
THEN ("We can get the error message") { THEN("We can get the error message") { REQUIRE(ec.message() == "no_physical_devices_found"); }
REQUIRE (ec.message () == "no_physical_devices_found"); }
} }
}
}
GIVEN ("A QueueError") { GIVEN("A QueueError") {
const auto error = vkb::QueueError::invalid_queue_family_index; const auto error = vkb::QueueError::invalid_queue_family_index;
WHEN ("Creating an error code from it") { WHEN("Creating an error code from it") {
std::error_code ec = make_error_code (error); std::error_code ec = make_error_code(error);
THEN ("The error code is equal to the original error") { REQUIRE (ec == error); } THEN("The error code is equal to the original error") { REQUIRE(ec == error); }
THEN ("The error code is not equal to an unrelated error") { THEN("The error code is not equal to an unrelated error") {
REQUIRE (ec != vkb::InstanceError::failed_create_instance); REQUIRE(ec != vkb::InstanceError::failed_create_instance);
} }
THEN ("We can get the error message") { THEN("We can get the error message") { REQUIRE(ec.message() == "invalid_queue_family_index"); }
REQUIRE (ec.message () == "invalid_queue_family_index"); }
} }
}
}
GIVEN ("A DeviceError") { GIVEN("A DeviceError") {
const auto error = vkb::DeviceError::failed_create_device; const auto error = vkb::DeviceError::failed_create_device;
WHEN ("Creating an error code from it") { WHEN("Creating an error code from it") {
std::error_code ec = make_error_code (error); std::error_code ec = make_error_code(error);
THEN ("The error code is equal to the original error") { REQUIRE (ec == error); } THEN("The error code is equal to the original error") { REQUIRE(ec == error); }
THEN ("The error code is not equal to an unrelated error") { THEN("The error code is not equal to an unrelated error") {
REQUIRE (ec != vkb::InstanceError::failed_create_instance); REQUIRE(ec != vkb::InstanceError::failed_create_instance);
} }
THEN ("We can get the error message") { THEN("We can get the error message") { REQUIRE(ec.message() == "failed_create_device"); }
REQUIRE (ec.message () == "failed_create_device"); }
} }
}
}
GIVEN ("A SwapchainError") { GIVEN("A SwapchainError") {
const auto error = vkb::SwapchainError::failed_create_swapchain; const auto error = vkb::SwapchainError::failed_create_swapchain;
WHEN ("Creating an error code from it") { WHEN("Creating an error code from it") {
std::error_code ec = make_error_code (error); std::error_code ec = make_error_code(error);
THEN ("The error code is equal to the original error") { REQUIRE (ec == error); } THEN("The error code is equal to the original error") { REQUIRE(ec == error); }
THEN ("The error code is not equal to an unrelated error") { THEN("The error code is not equal to an unrelated error") {
REQUIRE (ec != vkb::InstanceError::failed_create_instance); REQUIRE(ec != vkb::InstanceError::failed_create_instance);
} }
THEN ("We can get the error message") { THEN("We can get the error message") { REQUIRE(ec.message() == "failed_create_swapchain"); }
REQUIRE (ec.message () == "failed_create_swapchain"); }
} }
}
}
} }

View File

@ -1,2 +0,0 @@
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

View File

@ -1,85 +1,85 @@
#include <catch2/catch.hpp> #include <catch2/catch_test_macros.hpp>
#include "VkBootstrap.cpp" #include "VkBootstrap.cpp"
TEST_CASE("Single Queue Device", "[UnitTests.queue_selection_logic]") { TEST_CASE("Single Queue Device", "[UnitTests.queue_selection_logic]") {
std::vector<VkQueueFamilyProperties> families = { VkQueueFamilyProperties{ std::vector<VkQueueFamilyProperties> families = { VkQueueFamilyProperties{
VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } } }; VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } } };
REQUIRE(0 == vkb::detail::get_first_queue_index(families, VK_QUEUE_GRAPHICS_BIT)); REQUIRE(0 == vkb::detail::get_first_queue_index(families, VK_QUEUE_GRAPHICS_BIT));
REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE == REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE ==
vkb::detail::get_separate_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT)); vkb::detail::get_separate_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT));
REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE == REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE ==
vkb::detail::get_separate_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT)); vkb::detail::get_separate_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT));
REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE == REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE ==
vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT)); vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT));
REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE == REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE ==
vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT)); vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT));
} }
TEST_CASE("Dedicated Compute Queue, Separate Transfer", "[UnitTests.queue_selection_logic]") { TEST_CASE("Dedicated Compute Queue, Separate Transfer", "[UnitTests.queue_selection_logic]") {
SECTION("Dedicated Queue First") { SECTION("Dedicated Queue First") {
std::vector<VkQueueFamilyProperties> families = { std::vector<VkQueueFamilyProperties> families = {
VkQueueFamilyProperties{ VkQueueFamilyProperties{
VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_GRAPHICS_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }, VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_GRAPHICS_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } },
VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }, VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } },
VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } } VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }
}; };
REQUIRE(0 == vkb::detail::get_first_queue_index(families, VK_QUEUE_GRAPHICS_BIT)); REQUIRE(0 == vkb::detail::get_first_queue_index(families, VK_QUEUE_GRAPHICS_BIT));
REQUIRE(1 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT)); REQUIRE(1 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT));
REQUIRE(2 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT)); REQUIRE(2 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT));
REQUIRE(1 == vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT)); REQUIRE(1 == vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT));
REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE == REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE ==
vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT)); vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT));
} }
SECTION("Dedicated Queue Last") { SECTION("Dedicated Queue Last") {
std::vector<VkQueueFamilyProperties> families = { std::vector<VkQueueFamilyProperties> families = {
VkQueueFamilyProperties{ VkQueueFamilyProperties{
VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_GRAPHICS_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }, VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_GRAPHICS_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } },
VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }, VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } },
VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } } VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }
}; };
REQUIRE(0 == vkb::detail::get_first_queue_index(families, VK_QUEUE_GRAPHICS_BIT)); REQUIRE(0 == vkb::detail::get_first_queue_index(families, VK_QUEUE_GRAPHICS_BIT));
REQUIRE(2 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT)); REQUIRE(2 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT));
REQUIRE(1 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT)); REQUIRE(1 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT));
REQUIRE(2 == vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT)); REQUIRE(2 == vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT));
REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE == REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE ==
vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT)); vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT));
} }
} }
TEST_CASE("Dedicated Transfer Queue, Separate Compute", "[UnitTests.queue_selection_logic]") { TEST_CASE("Dedicated Transfer Queue, Separate Compute", "[UnitTests.queue_selection_logic]") {
SECTION("Dedicated Queue First") { SECTION("Dedicated Queue First") {
std::vector<VkQueueFamilyProperties> families = { std::vector<VkQueueFamilyProperties> families = {
VkQueueFamilyProperties{ VkQueueFamilyProperties{
VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_GRAPHICS_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }, VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_GRAPHICS_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } },
VkQueueFamilyProperties{ VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }, VkQueueFamilyProperties{ VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } },
VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } } VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }
}; };
REQUIRE(0 == vkb::detail::get_first_queue_index(families, VK_QUEUE_GRAPHICS_BIT)); REQUIRE(0 == vkb::detail::get_first_queue_index(families, VK_QUEUE_GRAPHICS_BIT));
REQUIRE(2 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT)); REQUIRE(2 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT));
REQUIRE(1 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT)); REQUIRE(1 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT));
REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE == REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE ==
vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT)); vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT));
REQUIRE(1 == vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT)); REQUIRE(1 == vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT));
} }
SECTION("Dedicated Queue Last") { SECTION("Dedicated Queue Last") {
std::vector<VkQueueFamilyProperties> families = { std::vector<VkQueueFamilyProperties> families = {
VkQueueFamilyProperties{ VkQueueFamilyProperties{
VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_GRAPHICS_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }, VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_GRAPHICS_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } },
VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }, VkQueueFamilyProperties{ VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } },
VkQueueFamilyProperties{ VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } } VkQueueFamilyProperties{ VK_QUEUE_TRANSFER_BIT, 1, 0, VkExtent3D{ 1, 1, 1 } }
}; };
REQUIRE(0 == vkb::detail::get_first_queue_index(families, VK_QUEUE_GRAPHICS_BIT)); REQUIRE(0 == vkb::detail::get_first_queue_index(families, VK_QUEUE_GRAPHICS_BIT));
REQUIRE(1 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT)); REQUIRE(1 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT));
REQUIRE(2 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT)); REQUIRE(2 == vkb::detail::get_separate_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT));
REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE == REQUIRE(vkb::detail::QUEUE_INDEX_MAX_VALUE ==
vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT)); vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_COMPUTE_BIT, VK_QUEUE_TRANSFER_BIT));
REQUIRE(2 == vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT)); REQUIRE(2 == vkb::detail::get_dedicated_queue_index(families, VK_QUEUE_TRANSFER_BIT, VK_QUEUE_COMPUTE_BIT));
} }
} }