From 155677ec0f989a4224ba28d4d999b43e43905ca8 Mon Sep 17 00:00:00 2001 From: Charles Giessen Date: Thu, 30 Jan 2020 01:15:10 -0700 Subject: [PATCH] Added glfw as git submodule + Catch2 Made the physical device, device, and swapchain builders take the required parameters as constructor arugments. Made tests optional using a cmake bool --- .gitmodules | 6 ++++++ CMakeLists.txt | 16 +++++++++++----- ext/Catch2 | 1 + ext/glfw | 1 + src/Device.cpp | 22 +++++++++------------- src/Device.h | 6 +++--- src/Swapchain.cpp | 8 ++++++++ src/Swapchain.h | 4 ++-- tests/run_tests.cpp | 21 ++++++++++----------- 9 files changed, 51 insertions(+), 34 deletions(-) create mode 100644 .gitmodules create mode 160000 ext/Catch2 create mode 160000 ext/glfw diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..59ccffc --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "ext/glfw"] + path = ext/glfw + url = https://github.com/glfw/glfw +[submodule "ext/Catch2"] + path = ext/Catch2 + url = https://github.com/catchorg/Catch2 diff --git a/CMakeLists.txt b/CMakeLists.txt index e3beb5e..3463bc4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,10 +3,12 @@ project(VulkanBootstrap) find_package(Vulkan REQUIRED) -find_package(glfw3) - add_library(vk-bootstrap src/VkBootstrap.h + src/Util.h + src/Instance.h + src/Device.h + src/Swapchain.h src/Instance.cpp src/Device.cpp src/Swapchain.cpp) @@ -18,12 +20,16 @@ target_link_libraries(vk-bootstrap PRIVATE ${Vulkan_LIBRARIES}) target_compile_features(vk-bootstrap PUBLIC cxx_std_11) -target_compile_options(vk-bootstrap PUBLIC -fsanitize=address) -target_link_options(vk-bootstrap PUBLIC -fsanitize=address) +option(VK_BOOTSTRAP_TEST "Test Vk-Bootstrap with glfw and Catch2" OFF) +if (VK_BOOTSTRAP_TEST) +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) \ No newline at end of file +target_link_libraries(vk-bootstrap-test glfw) + +endif(VK_BOOTSTRAP_TEST) \ No newline at end of file diff --git a/ext/Catch2 b/ext/Catch2 new file mode 160000 index 0000000..2d172dc --- /dev/null +++ b/ext/Catch2 @@ -0,0 +1 @@ +Subproject commit 2d172dc688ffab0867a94928159e93384475a870 diff --git a/ext/glfw b/ext/glfw new file mode 160000 index 0000000..d973acc --- /dev/null +++ b/ext/glfw @@ -0,0 +1 @@ +Subproject commit d973acc123826666ecc9e6fd475682e3d84c54a6 diff --git a/src/Device.cpp b/src/Device.cpp index 9db396f..84ce757 100644 --- a/src/Device.cpp +++ b/src/Device.cpp @@ -276,6 +276,13 @@ PhysicalDeviceSelector::Suitable PhysicalDeviceSelector::is_device_suitable (VkP return suitable; } +PhysicalDeviceSelector::PhysicalDeviceSelector (Instance instance) +{ + info.instance = instance.instance; + info.headless = instance.headless; + criteria.require_present = !instance.headless; +} + detail::Expected PhysicalDeviceSelector::select () { auto physical_devices = detail::get_vector (vkEnumeratePhysicalDevices, info.instance); @@ -311,13 +318,6 @@ detail::Expected PhysicalDeviceSelector::select () return physical_device; } -PhysicalDeviceSelector& PhysicalDeviceSelector::set_instance (Instance instance) -{ - info.instance = instance.instance; - info.headless = instance.headless; - criteria.require_present = !instance.headless; - return *this; -} PhysicalDeviceSelector& PhysicalDeviceSelector::set_surface (VkSurfaceKHR surface) { info.surface = surface; @@ -399,6 +399,8 @@ struct QueueFamily int32_t family; uint32_t count; }; +DeviceBuilder::DeviceBuilder (PhysicalDevice device) { info.physical_device = device; } + detail::Expected DeviceBuilder::build () { auto& queue_properties = info.physical_device.queue_family_properties; @@ -449,12 +451,6 @@ detail::Expected DeviceBuilder::build () return device; } -DeviceBuilder& DeviceBuilder::set_physical_device (PhysicalDevice const& phys_device) -{ - info.physical_device = phys_device; - return *this; -} - template DeviceBuilder& DeviceBuilder::add_pNext (T* structure) { if (info.pNext_chain == nullptr) diff --git a/src/Device.h b/src/Device.h index 109b563..5c90ebd 100644 --- a/src/Device.h +++ b/src/Device.h @@ -67,9 +67,10 @@ void populate_physical_device_details (PhysicalDevice physical_device); struct PhysicalDeviceSelector { public: + PhysicalDeviceSelector (Instance instance); + detail::Expected select (); - PhysicalDeviceSelector& set_instance (Instance instance); PhysicalDeviceSelector& set_surface (VkSurfaceKHR instance); PhysicalDeviceSelector& prefer_discrete (bool prefer_discrete = true); @@ -145,10 +146,9 @@ void destroy_device (Device device); class DeviceBuilder { public: + DeviceBuilder (PhysicalDevice device); detail::Expected build (); - DeviceBuilder& set_physical_device (PhysicalDevice const& phys_device); - template DeviceBuilder& add_pNext (T* structure); private: diff --git a/src/Swapchain.cpp b/src/Swapchain.cpp index ca5ae93..26621a2 100644 --- a/src/Swapchain.cpp +++ b/src/Swapchain.cpp @@ -52,6 +52,14 @@ VkExtent2D choose_swap_extent ( } } } // namespace detail + +SwapchainBuilder::SwapchainBuilder (Device const& device) +{ + info.device = device.device; + info.physical_device = device.physical_device; + info.surface = device.surface; +} + detail::Expected SwapchainBuilder::build () { auto surface_support = diff --git a/src/Swapchain.h b/src/Swapchain.h index 3857f86..450ecb8 100644 --- a/src/Swapchain.h +++ b/src/Swapchain.h @@ -26,12 +26,12 @@ struct Swapchain class SwapchainBuilder { public: + SwapchainBuilder (Device const& device); + detail::Expected build (); detail::Expected recreate (Swapchain const& swapchain); void destroy (Swapchain const& swapchain); - SwapchainBuilder& set_device (Device const& device); - SwapchainBuilder& set_desired_format (VkFormat format); SwapchainBuilder& set_fallback_format (VkFormat format); diff --git a/tests/run_tests.cpp b/tests/run_tests.cpp index eda8664..747bb2a 100644 --- a/tests/run_tests.cpp +++ b/tests/run_tests.cpp @@ -37,13 +37,13 @@ int test_happy_path () auto surface = create_surface_glfw (instance.instance, window); - vkbs::PhysicalDeviceSelector phys_device_selector; - auto phys_device_ret = phys_device_selector.set_instance (instance).set_surface (surface).select (); + vkbs::PhysicalDeviceSelector phys_device_selector (instance); + auto phys_device_ret = phys_device_selector.set_surface (surface).select (); if (!phys_device_ret) return -2; // couldn't select physical device vkbs::PhysicalDevice physical_device = phys_device_ret.value (); - vkbs::DeviceBuilder device_builder; - auto device_ret = device_builder.set_physical_device (physical_device).build (); + vkbs::DeviceBuilder device_builder (physical_device); + auto device_ret = device_builder.build (); if (!device_ret) return -3; // couldn't create device vkbs::Device device = device_ret.value (); @@ -110,9 +110,8 @@ int test_physical_device_selection () auto window = create_window_glfw (); auto surface = create_surface_glfw (instance.instance, window); - vkbs::PhysicalDeviceSelector selector; - auto phys_dev_ret = selector.set_instance (instance) - .set_surface (surface) + vkbs::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) @@ -135,12 +134,12 @@ int test_device_creation () auto window = create_window_glfw (); auto surface = create_surface_glfw (instance.instance, window); - vkbs::PhysicalDeviceSelector selector; - auto phys_dev_ret = selector.set_instance (instance).set_surface (surface).select (); + vkbs::PhysicalDeviceSelector selector (instance); + auto phys_dev_ret = selector.set_surface (surface).select (); auto phys_dev = phys_dev_ret.value (); - vkbs::DeviceBuilder device_builder; - auto dev_ret = device_builder.set_physical_device (phys_dev).build (); + vkbs::DeviceBuilder device_builder (phys_dev); + auto dev_ret = device_builder.build (); if (!dev_ret.has_value ()) { printf ("%s\n", dev_ret.error ().msg);