Merge pull request #23 from LesleyLai/tests

Migrate to Catch2 & CMake file structural change
This commit is contained in:
Charles Giessen 2020-03-26 11:21:28 -06:00 committed by GitHub
commit e906cfc093
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 141 additions and 196 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
*.vscode *.vscode
*.vs *.vs
*.idea
cmake-build-*

View File

@ -6,15 +6,9 @@ find_package(Vulkan REQUIRED)
add_library(vk-bootstrap src/VkBootstrap.h src/VkBootstrap.cpp) add_library(vk-bootstrap src/VkBootstrap.h src/VkBootstrap.cpp)
add_library(vk-bootstrap::vk-bootstrap ALIAS vk-bootstrap) add_library(vk-bootstrap::vk-bootstrap ALIAS vk-bootstrap)
target_include_directories(vk-bootstrap PUBLIC src) add_library(vk-bootstrap-compiler-warnings INTERFACE)
target_compile_options(vk-bootstrap-compiler-warnings
target_include_directories(vk-bootstrap PRIVATE ${Vulkan_INCLUDE_DIR}) INTERFACE
target_link_libraries(vk-bootstrap PRIVATE ${Vulkan_LIBRARY})
target_compile_features(vk-bootstrap PUBLIC cxx_std_11)
target_compile_options(
vk-bootstrap
PRIVATE
$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>: $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:
-Wall -Wall
-Wextra -Wextra
@ -23,40 +17,20 @@ target_compile_options(
-Wsign-conversion> -Wsign-conversion>
$<$<CXX_COMPILER_ID:MSVC>: $<$<CXX_COMPILER_ID:MSVC>:
/WX /WX
/W4>) /W4>
)
target_include_directories(vk-bootstrap PUBLIC src)
target_include_directories(vk-bootstrap PUBLIC ${Vulkan_INCLUDE_DIR})
target_link_libraries(vk-bootstrap
PUBLIC ${Vulkan_LIBRARY}
PRIVATE
vk-bootstrap-compiler-warnings)
option(VK_BOOTSTRAP_TEST "Test Vk-Bootstrap with glfw and Catch2" OFF) option(VK_BOOTSTRAP_TEST "Test Vk-Bootstrap with glfw and Catch2" OFF)
if (VK_BOOTSTRAP_TEST) if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR VK_BOOTSTRAP_TEST)
set(GLFW_BUILD_TESTS add_subdirectory(ext)
OFF add_subdirectory(tests)
CACHE BOOL "" FORCE) add_subdirectory(example)
set(GLFW_BUILD_DOCS
OFF
CACHE BOOL "" FORCE)
set(GLFW_INSTALL
OFF
CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES
OFF
CACHE BOOL "" FORCE)
add_subdirectory(ext/glfw)
add_subdirectory(ext/Catch2)
add_executable(vk-bootstrap-test tests/run_tests.cpp)
target_link_libraries(vk-bootstrap-test vk-bootstrap)
target_link_libraries(vk-bootstrap-test glfw)
target_link_libraries(vk-bootstrap-test Catch2)
add_executable(vk-bootstrap-triangle example/triangle.cpp)
target_link_libraries(vk-bootstrap-triangle vk-bootstrap)
target_link_libraries(vk-bootstrap-triangle glfw)
add_custom_command(
TARGET vk-bootstrap-triangle
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/example/shaders ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${CMAKE_CURRENT_BINARY_DIR})
endif () endif ()

View File

@ -110,10 +110,11 @@ In the project directory, run the following to get the required dependencies to
git submodule update --init git submodule update --init
``` ```
In the build directory, enable tests by adding `-DVK_BOOTSTRAP_TEST` to the cmake command line arguments Tests will be enabled by default if you open this project standalone. If you include this project as a subdirectory,
you can force enable tests by adding `-DVK_BOOTSTRAP_TEST` to the cmake command line arguments
```bash ```bash
cmake ../path/to/vk-bootstrap/ -DVK_BOOTSTRAP_TEST=ON cmake ../path/to/your-project/ -DVK_BOOTSTRAP_TEST=ON
``` ```
## Todo's ## Todo's

14
example/CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
add_executable(vk-bootstrap-triangle triangle.cpp)
target_link_libraries(vk-bootstrap-triangle
PRIVATE
glfw
vk-bootstrap
vk-bootstrap-compiler-warnings)
add_custom_command(
TARGET vk-bootstrap-triangle
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy_directory
${CMAKE_SOURCE_DIR}/example/shaders ${CMAKE_CURRENT_BINARY_DIR}
DEPENDS ${CMAKE_CURRENT_BINARY_DIR})

View File

@ -150,7 +150,7 @@ std::vector<char> readFile (const std::string& filename) {
std::vector<char> buffer (file_size); std::vector<char> buffer (file_size);
file.seekg (0); file.seekg (0);
file.read (buffer.data (), file_size); file.read (buffer.data (), static_cast<std::streamsize> (file_size));
file.close (); file.close ();

14
ext/CMakeLists.txt Normal file
View File

@ -0,0 +1,14 @@
set(GLFW_BUILD_TESTS
OFF
CACHE BOOL "" FORCE)
set(GLFW_BUILD_DOCS
OFF
CACHE BOOL "" FORCE)
set(GLFW_INSTALL
OFF
CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES
OFF
CACHE BOOL "" FORCE)
add_subdirectory(glfw)
add_subdirectory(Catch2)

View File

@ -284,7 +284,7 @@ void destroy_debug_utils_messenger (VkInstance instance,
VkDebugUtilsMessengerEXT debugMessenger, VkDebugUtilsMessengerEXT debugMessenger,
VkAllocationCallbacks* allocation_callbacks = VK_NULL_HANDLE); VkAllocationCallbacks* allocation_callbacks = VK_NULL_HANDLE);
static VKAPI_ATTR VkBool32 VKAPI_CALL default_debug_callback (VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VKAPI_ATTR VkBool32 VKAPI_CALL default_debug_callback (VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType, VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
void* pUserData); void* pUserData);

8
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,8 @@
add_executable(vk-bootstrap-test main.cpp run_tests.cpp)
target_link_libraries(vk-bootstrap-test
PRIVATE
vk-bootstrap
vk-bootstrap-compiler-warnings
glfw
Catch2
)

2
tests/main.cpp Normal file
View File

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

View File

@ -1,46 +1,61 @@
#include "common.h" #include "common.h"
int test_happy_path () { #include <catch2/catch.hpp>
printf ("happy path\n");
// TODO
// Getting queues
// get dedicated vs distinct compute queues
// Swapchain creation
// Swapchain recreation
// changing present modes and/or image formats
TEST_CASE ("Instance with surface") {
GIVEN ("A window and a vulkan instance") {
auto window = create_window_glfw (); auto window = create_window_glfw ();
vkb::InstanceBuilder instance_builder; vkb::InstanceBuilder instance_builder;
auto instance_ret = instance_builder.use_default_debug_messenger ().build (); auto instance_ret = instance_builder.use_default_debug_messenger ().build ();
if (!instance_ret) { REQUIRE (instance_ret);
std::cout << static_cast<uint32_t> (instance_ret.error ().type) << "\n";
return -1; // couldn't make instance
}
vkb::Instance instance = instance_ret.value (); vkb::Instance instance = instance_ret.value ();
printf ("made instance\n");
auto surface = create_surface_glfw (instance.instance, window); auto surface = create_surface_glfw (instance.instance, window);
GIVEN ("A default selected physical device") {
vkb::PhysicalDeviceSelector phys_device_selector (instance); vkb::PhysicalDeviceSelector phys_device_selector (instance);
auto phys_device_ret = phys_device_selector.set_surface (surface).select (); auto phys_device_ret = phys_device_selector.set_surface (surface).select ();
if (!phys_device_ret) return -2; // couldn't select physical device REQUIRE (phys_device_ret);
vkb::PhysicalDevice physical_device = phys_device_ret.value (); vkb::PhysicalDevice physical_device = phys_device_ret.value ();
printf ("made physical device\n");
GIVEN ("A device created with default parameters") {
vkb::DeviceBuilder device_builder (physical_device); vkb::DeviceBuilder device_builder (physical_device);
auto device_ret = device_builder.build (); auto device_ret = device_builder.build ();
if (!device_ret) return -1; // couldn't create device REQUIRE (device_ret);
vkb::Device device = device_ret.value (); vkb::Device device = device_ret.value ();
printf ("made device\n");
// possible swapchain creation... // possible swapchain creation...
vkb::destroy_device (device); vkb::destroy_device (device);
}
}
THEN ("Can select physical device with customized requirements") {
vkb::PhysicalDeviceSelector selector (instance);
auto phys_dev_ret = selector.set_surface (surface)
.add_desired_extension (VK_KHR_MULTIVIEW_EXTENSION_NAME)
.add_required_extension (VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME)
.set_minimum_version (1, 0)
.set_desired_version (1, 1)
.select ();
REQUIRE (phys_dev_ret.has_value ());
}
destroy_surface (instance.instance, surface); destroy_surface (instance.instance, surface);
vkb::destroy_instance (instance); vkb::destroy_instance (instance);
destroy_window_glfw (window); destroy_window_glfw (window);
return 0; }
} }
TEST_CASE ("basic instance") {
int test_instance_basic () {
printf ("\nbasic instance\n");
vkb::InstanceBuilder builder; vkb::InstanceBuilder builder;
auto instance_ret = auto instance_ret =
@ -49,7 +64,8 @@ int test_instance_basic () {
.set_debug_callback ([] (VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, .set_debug_callback ([] (VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType, VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
void* pUserData) -> VkBool32 { void *
/*pUserData*/) -> VkBool32 {
auto ms = vkb::to_string_message_severity (messageSeverity); auto ms = vkb::to_string_message_severity (messageSeverity);
auto mt = vkb::to_string_message_type (messageType); auto mt = vkb::to_string_message_type (messageType);
printf ("[%s: %s](user defined)\n%s\n", ms, mt, pCallbackData->pMessage); printf ("[%s: %s](user defined)\n%s\n", ms, mt, pCallbackData->pMessage);
@ -57,17 +73,13 @@ int test_instance_basic () {
}) })
.require_api_version (1, 2, 111) .require_api_version (1, 2, 111)
.build (); .build ();
if (!instance_ret.has_value ()) {
return 1; REQUIRE (instance_ret.has_value ());
}
vkb::destroy_instance (instance_ret.value ()); vkb::destroy_instance (instance_ret.value ());
return 0;
} }
int test_instance_headless () { TEST_CASE ("headless instance") {
printf ("\nheadless instance\n");
vkb::InstanceBuilder builder; vkb::InstanceBuilder builder;
auto instance_ret = auto instance_ret =
@ -82,88 +94,6 @@ int test_instance_headless () {
.add_validation_feature_disable (VkValidationFeatureDisableEXT::VK_VALIDATION_FEATURE_DISABLE_OBJECT_LIFETIMES_EXT) .add_validation_feature_disable (VkValidationFeatureDisableEXT::VK_VALIDATION_FEATURE_DISABLE_OBJECT_LIFETIMES_EXT)
.add_validation_disable (VkValidationCheckEXT::VK_VALIDATION_CHECK_SHADERS_EXT) .add_validation_disable (VkValidationCheckEXT::VK_VALIDATION_CHECK_SHADERS_EXT)
.build (); .build ();
if (!instance_ret.has_value ()) { REQUIRE (instance_ret.has_value ());
return 1;
}
vkb::destroy_instance (instance_ret.value ()); vkb::destroy_instance (instance_ret.value ());
return 0;
}
int test_physical_device_selection () {
printf ("\nphysical device selection\n");
vkb::InstanceBuilder instance_builder;
auto instance_ret = instance_builder.use_default_debug_messenger ().build ();
if(!instance_ret.has_value()){
printf("\n%s",vkb::to_string(instance_ret.error().type));
return -1;
}
auto instance = instance_ret.value ();
auto window = create_window_glfw ();
auto surface = create_surface_glfw (instance.instance, window);
vkb::PhysicalDeviceSelector selector (instance);
auto phys_dev_ret = selector.set_surface (surface)
.add_desired_extension (VK_KHR_MULTIVIEW_EXTENSION_NAME)
.add_required_extension (VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME)
.set_minimum_version (1, 0)
.set_desired_version (1, 1)
.select ();
if (!phys_dev_ret.has_value ()) {
return -1;
}
destroy_surface (instance.instance, surface);
vkb::destroy_instance (instance);
destroy_window_glfw (window);
return 0;
}
int test_device_creation () {
printf ("\ndevice creation\n");
vkb::InstanceBuilder instance_builder;
auto instance_ret = instance_builder.use_default_debug_messenger ().build ();
if (!instance_ret.has_value ()) {
printf ("couldn't create instance %s\n", vkb::to_string (instance_ret.error ().type));
return -1;
}
auto instance = instance_ret.value ();
auto window = create_window_glfw ();
auto surface = create_surface_glfw (instance.instance, window);
vkb::PhysicalDeviceSelector selector (instance);
auto phys_dev_ret = selector.set_surface (surface).select ();
if (!phys_dev_ret.has_value ()) {
printf ("couldn't select device %s\n", vkb::to_string (phys_dev_ret.error ().type));
return -1;
}
auto phys_dev = phys_dev_ret.value ();
vkb::DeviceBuilder device_builder (phys_dev);
auto dev_ret = device_builder.build ();
if (!dev_ret.has_value ()) {
printf ("couldn't create device %s\n", vkb::to_string (dev_ret.error ().type));
return -1;
}
vkb::destroy_device (dev_ret.value ());
destroy_surface (instance.instance, surface);
vkb::destroy_instance (instance);
destroy_window_glfw (window);
return 0;
}
// TODO
// Migrate to Catch2
// Getting queues
// get dedicated vs distinct compute queues
// Swapchain creation
// Swapchain recreation
// changing present modes and/or image formats
int main () {
test_happy_path ();
test_instance_basic ();
test_instance_headless ();
test_physical_device_selection ();
test_device_creation ();
} }