Add vkb::destroy_surface helper function

This commit is contained in:
Charles Giessen 2021-06-06 14:02:02 -06:00 committed by Charles Giessen
parent 10d88ae840
commit b3fdd554b4
5 changed files with 63 additions and 44 deletions

View File

@ -547,7 +547,7 @@ void cleanup (Init& init, RenderData& data) {
vkb::destroy_swapchain (init.swapchain); vkb::destroy_swapchain (init.swapchain);
vkb::destroy_device (init.device); vkb::destroy_device (init.device);
init->vkDestroySurfaceKHR (init.instance.instance, init.surface, nullptr); vkb::destroy_surface(init.instance, init.surface);
vkb::destroy_instance (init.instance); vkb::destroy_instance (init.instance);
destroy_window_glfw (init.window); destroy_window_glfw (init.window);
} }

View File

@ -558,7 +558,16 @@ bool SystemInfo::is_layer_available(const char* layer_name) const {
if (!layer_name) return false; if (!layer_name) return false;
return detail::check_layer_supported(available_layers, layer_name); return detail::check_layer_supported(available_layers, layer_name);
} }
void destroy_surface(Instance instance, VkSurfaceKHR surface) {
if (instance.instance != VK_NULL_HANDLE && surface != VK_NULL_HANDLE) {
detail::vulkan_functions().fp_vkDestroySurfaceKHR(instance.instance, surface, instance.allocation_callbacks);
}
}
void destroy_surface(VkInstance instance, VkSurfaceKHR surface, VkAllocationCallbacks* callbacks) {
if (instance != VK_NULL_HANDLE && surface != VK_NULL_HANDLE) {
detail::vulkan_functions().fp_vkDestroySurfaceKHR(instance, surface, callbacks);
}
}
void destroy_instance(Instance instance) { void destroy_instance(Instance instance) {
if (instance.instance != VK_NULL_HANDLE) { if (instance.instance != VK_NULL_HANDLE) {
if (instance.debug_messenger != VK_NULL_HANDLE) if (instance.debug_messenger != VK_NULL_HANDLE)
@ -945,7 +954,8 @@ uint32_t get_separate_queue_index(std::vector<VkQueueFamilyProperties> const& fa
VkQueueFlags undesired_flags) { VkQueueFlags undesired_flags) {
uint32_t index = QUEUE_INDEX_MAX_VALUE; uint32_t index = QUEUE_INDEX_MAX_VALUE;
for (uint32_t i = 0; i < static_cast<uint32_t>(families.size()); i++) { for (uint32_t i = 0; i < static_cast<uint32_t>(families.size()); i++) {
if ((families[i].queueFlags & desired_flags) && ((families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) == 0)) { if ((families[i].queueFlags & desired_flags) &&
((families[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) == 0)) {
if ((families[i].queueFlags & undesired_flags) == 0) { if ((families[i].queueFlags & undesired_flags) == 0) {
return i; return i;
} else { } else {
@ -1376,9 +1386,7 @@ detail::Result<VkQueue> Device::get_dedicated_queue(QueueType type) const {
// ---- Dispatch ---- // // ---- Dispatch ---- //
DispatchTable Device::make_table() const { DispatchTable Device::make_table() const { return { device, fp_vkGetDeviceProcAddr }; }
return {device, fp_vkGetDeviceProcAddr};
}
// ---- Device ---- // // ---- Device ---- //

View File

@ -225,6 +225,8 @@ struct Instance {
friend class PhysicalDeviceSelector; friend class PhysicalDeviceSelector;
}; };
void destroy_surface(Instance instance, VkSurfaceKHR surface); // release surface handle
void destroy_surface(VkInstance instance, VkSurfaceKHR surface, VkAllocationCallbacks* callbacks = nullptr); // release surface handle
void destroy_instance(Instance instance); // release instance resources void destroy_instance(Instance instance); // release instance resources
class InstanceBuilder { class InstanceBuilder {

View File

@ -5,12 +5,6 @@
// TODO // TODO
// changing present modes and/or image formats // changing present modes and/or image formats
void destroy_surface(vkb::detail::Result<vkb::Instance> instance_ret, VkSurfaceKHR surface) {
PFN_vkDestroySurfaceKHR fp_vkDestroySurfaceKHR = reinterpret_cast<PFN_vkDestroySurfaceKHR>(
instance_ret->fp_vkGetInstanceProcAddr(instance_ret->instance, "vkDestroySurfaceKHR"));
fp_vkDestroySurfaceKHR(instance_ret->instance, surface, nullptr);
}
TEST_CASE("Instance with surface", "[VkBootstrap.bootstrap]") { TEST_CASE("Instance with surface", "[VkBootstrap.bootstrap]") {
GIVEN("A window and a vulkan instance") { GIVEN("A window and a vulkan instance") {
@ -54,7 +48,7 @@ TEST_CASE("Instance with surface", "[VkBootstrap.bootstrap]") {
REQUIRE(phys_dev_ret.has_value()); REQUIRE(phys_dev_ret.has_value());
} }
destroy_surface(instance_ret, surface); vkb::destroy_surface(instance, surface);
vkb::destroy_instance(instance); vkb::destroy_instance(instance);
destroy_window_glfw(window); destroy_window_glfw(window);
} }
@ -106,7 +100,8 @@ TEST_CASE("instance configuration", "[VkBootstrap.bootstrap]") {
TEST_CASE("Headless Vulkan", "[VkBootstrap.bootstrap]") { TEST_CASE("Headless Vulkan", "[VkBootstrap.bootstrap]") {
vkb::InstanceBuilder builder; vkb::InstanceBuilder builder;
auto instance_ret = builder.request_validation_layers().set_headless().use_default_debug_messenger().build(); auto instance_ret =
builder.request_validation_layers().set_headless().use_default_debug_messenger().build();
REQUIRE(instance_ret.has_value()); REQUIRE(instance_ret.has_value());
vkb::PhysicalDeviceSelector phys_device_selector(instance_ret.value()); vkb::PhysicalDeviceSelector phys_device_selector(instance_ret.value());
@ -127,7 +122,8 @@ TEST_CASE("Device Configuration", "[VkBootstrap.bootstrap]") {
auto window = create_window_glfw("Device Configuration"); auto window = create_window_glfw("Device Configuration");
vkb::InstanceBuilder builder; vkb::InstanceBuilder builder;
auto instance_ret = builder.request_validation_layers().require_api_version(1, 1).use_default_debug_messenger().build(); auto instance_ret =
builder.request_validation_layers().require_api_version(1, 1).use_default_debug_messenger().build();
REQUIRE(instance_ret.has_value()); REQUIRE(instance_ret.has_value());
auto surface = create_surface_glfw(instance_ret.value().instance, window); auto surface = create_surface_glfw(instance_ret.value().instance, window);
@ -183,15 +179,18 @@ TEST_CASE("Device Configuration", "[VkBootstrap.bootstrap]") {
vkb::destroy_device(device_ret.value()); vkb::destroy_device(device_ret.value());
} }
destroy_surface(instance_ret, surface); vkb::destroy_surface(instance_ret.value(), surface);
vkb::destroy_instance(instance_ret.value()); vkb::destroy_instance(instance_ret.value());
} }
TEST_CASE("Loading Dispatch Table", "[VkBootstrap.bootstrap]") { TEST_CASE("Loading Dispatch Table", "[VkBootstrap.bootstrap]") {
vkb::InstanceBuilder builder; vkb::InstanceBuilder builder;
auto instance_ret = auto instance_ret = builder.request_validation_layers()
builder.request_validation_layers().require_api_version(1, 0).set_headless().use_default_debug_messenger().build(); .require_api_version(1, 0)
.set_headless()
.use_default_debug_messenger()
.build();
REQUIRE(instance_ret.has_value()); REQUIRE(instance_ret.has_value());
{ {
@ -325,7 +324,7 @@ TEST_CASE("Swapchain", "[VkBootstrap.bootstrap]") {
} }
vkb::destroy_device(device_ret.value()); vkb::destroy_device(device_ret.value());
destroy_surface(instance_ret, surface); vkb::destroy_surface(instance_ret.value(), surface);
vkb::destroy_instance(instance_ret.value()); vkb::destroy_instance(instance_ret.value());
} }
} }
@ -350,10 +349,12 @@ TEST_CASE("Allocation Callbacks", "[VkBootstrap.bootstrap]") {
auto window = create_window_glfw("Allocation Callbacks"); auto window = create_window_glfw("Allocation Callbacks");
vkb::InstanceBuilder builder; vkb::InstanceBuilder builder;
auto instance_ret = auto instance_ret = builder.request_validation_layers()
builder.request_validation_layers().set_allocation_callbacks(&allocation_callbacks).use_default_debug_messenger().build(); .set_allocation_callbacks(&allocation_callbacks)
.use_default_debug_messenger()
.build();
REQUIRE(instance_ret.has_value()); REQUIRE(instance_ret.has_value());
auto surface = create_surface_glfw(instance_ret.value().instance, window); auto surface = create_surface_glfw(instance_ret.value().instance, window, &allocation_callbacks);
vkb::PhysicalDeviceSelector phys_device_selector(instance_ret.value()); vkb::PhysicalDeviceSelector phys_device_selector(instance_ret.value());
@ -374,7 +375,7 @@ TEST_CASE("Allocation Callbacks", "[VkBootstrap.bootstrap]") {
vkb::destroy_swapchain(swapchain_ret.value()); vkb::destroy_swapchain(swapchain_ret.value());
vkb::destroy_device(device_ret.value()); vkb::destroy_device(device_ret.value());
destroy_surface(instance_ret, surface); vkb::destroy_surface(instance_ret.value(), surface);
vkb::destroy_instance(instance_ret.value()); vkb::destroy_instance(instance_ret.value());
} }
@ -447,8 +448,11 @@ TEST_CASE("Querying Required Extension Features", "[VkBootstrap.version]") {
GIVEN("A working instance") { GIVEN("A working instance") {
vkb::InstanceBuilder builder; vkb::InstanceBuilder builder;
auto instance_ret = auto instance_ret = builder.request_validation_layers()
builder.request_validation_layers().require_api_version(1, 2).set_headless().use_default_debug_messenger().build(); .require_api_version(1, 2)
.set_headless()
.use_default_debug_messenger()
.build();
REQUIRE(instance_ret.has_value()); REQUIRE(instance_ret.has_value());
// Requires a device that supports runtime descriptor arrays via descriptor indexing extension. // Requires a device that supports runtime descriptor arrays via descriptor indexing extension.
{ {
@ -458,8 +462,9 @@ TEST_CASE("Querying Required Extension Features", "[VkBootstrap.version]") {
vkb::PhysicalDeviceSelector selector(instance_ret.value()); vkb::PhysicalDeviceSelector selector(instance_ret.value());
auto phys_dev_ret = auto phys_dev_ret =
selector.add_required_extension(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME). selector.add_required_extension(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME)
add_required_extension_features(descriptor_indexing_features).select(); .add_required_extension_features(descriptor_indexing_features)
.select();
// Ignore if hardware support isn't true // Ignore if hardware support isn't true
REQUIRE(phys_dev_ret.has_value()); REQUIRE(phys_dev_ret.has_value());
@ -479,8 +484,11 @@ TEST_CASE("Querying Vulkan 1.1 and 1.2 features", "[VkBootstrap.version]") {
GIVEN("A working instance") { GIVEN("A working instance") {
vkb::InstanceBuilder builder; vkb::InstanceBuilder builder;
auto instance_ret = auto instance_ret = builder.request_validation_layers()
builder.request_validation_layers().require_api_version(1, 2).set_headless().use_default_debug_messenger().build(); .require_api_version(1, 2)
.set_headless()
.use_default_debug_messenger()
.build();
REQUIRE(instance_ret.has_value()); REQUIRE(instance_ret.has_value());
// Requires a device that supports multiview and bufferDeviceAddress // Requires a device that supports multiview and bufferDeviceAddress
{ {

View File

@ -31,9 +31,10 @@ void destroy_window_glfw(GLFWwindow* window) {
glfwDestroyWindow(window); glfwDestroyWindow(window);
glfwTerminate(); glfwTerminate();
} }
VkSurfaceKHR create_surface_glfw(VkInstance instance, GLFWwindow* window) { VkSurfaceKHR create_surface_glfw(
VkInstance instance, GLFWwindow* window, VkAllocationCallbacks* allocator = nullptr) {
VkSurfaceKHR surface = VK_NULL_HANDLE; VkSurfaceKHR surface = VK_NULL_HANDLE;
VkResult err = glfwCreateWindowSurface(instance, window, NULL, &surface); VkResult err = glfwCreateWindowSurface(instance, window, allocator, &surface);
if (err) { if (err) {
const char* error_msg; const char* error_msg;
int ret = glfwGetError(&error_msg); int ret = glfwGetError(&error_msg);