Skip to content

Commit

Permalink
fix: Exceptions and headless
Browse files Browse the repository at this point in the history
Return runtime_error directly to avoid slicing issues, and skip window
and surface creation when a headless context is requested.
  • Loading branch information
antonio-lunarg committed Dec 12, 2024
1 parent a576b3e commit df26f7e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
55 changes: 29 additions & 26 deletions test/test_apps/common/test_app_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,11 @@ const char* to_string(SwapchainError err)
}
}

std::exception to_exception(InstanceError error)
std::runtime_error to_exception(InstanceError error)
{
return std::runtime_error(to_string(error));
}
std::exception to_exception(InstanceError error, VkResult result)
std::runtime_error to_exception(InstanceError error, VkResult result)
{
std::string message{};
message.append(to_string(error));
Expand All @@ -571,11 +571,11 @@ std::exception to_exception(InstanceError error, VkResult result)
return std::runtime_error(message);
}

std::exception to_exception(PhysicalDeviceError error)
std::runtime_error to_exception(PhysicalDeviceError error)
{
return std::runtime_error(to_string(error));
}
std::exception to_exception(PhysicalDeviceError error, VkResult result)
std::runtime_error to_exception(PhysicalDeviceError error, VkResult result)
{
std::string message{};
message.append(to_string(error));
Expand All @@ -584,11 +584,11 @@ std::exception to_exception(PhysicalDeviceError error, VkResult result)
return std::runtime_error(message);
}

std::exception to_exception(QueueError error)
std::runtime_error to_exception(QueueError error)
{
return std::runtime_error(to_string(error));
}
std::exception to_exception(QueueError error, VkResult result)
std::runtime_error to_exception(QueueError error, VkResult result)
{
std::string message{};
message.append(to_string(error));
Expand All @@ -597,11 +597,11 @@ std::exception to_exception(QueueError error, VkResult result)
return std::runtime_error(message);
}

std::exception to_exception(DeviceError error)
std::runtime_error to_exception(DeviceError error)
{
return std::runtime_error(to_string(error));
}
std::exception to_exception(DeviceError error, VkResult result)
std::runtime_error to_exception(DeviceError error, VkResult result)
{
std::string message{};
message.append(to_string(error));
Expand All @@ -610,11 +610,11 @@ std::exception to_exception(DeviceError error, VkResult result)
return std::runtime_error(message);
}

std::exception to_exception(SwapchainError error)
std::runtime_error to_exception(SwapchainError error)
{
return std::runtime_error(to_string(error));
}
std::exception to_exception(SwapchainError error, VkResult result)
std::runtime_error to_exception(SwapchainError error, VkResult result)
{
std::string message{};
message.append(to_string(error));
Expand Down Expand Up @@ -2178,11 +2178,11 @@ const char* to_string(SurfaceSupportError err)
}
}

std::exception to_exception(SurfaceSupportError error)
std::runtime_error to_exception(SurfaceSupportError error)
{
return std::runtime_error(to_string(error));
}
std::exception to_exception(SurfaceSupportError error, VkResult result)
std::runtime_error to_exception(SurfaceSupportError error, VkResult result)
{
std::string message{};
message.append(to_string(error));
Expand Down Expand Up @@ -2819,18 +2819,19 @@ std::exception sdl_exception()
return std::runtime_error(SDL_GetError());
}

void device_initialization_phase_1(const std::string& window_name, InitInfo& init)
void device_initialization_phase_2(const std::string& window_name, InitInfo& init)
{
init.window = create_window_sdl(window_name.data(), true, 1024, 1024);
if (!init.instance.headless) {
init.window = create_window_sdl(window_name.data(), true, 1024, 1024);
init.surface = create_surface_sdl(init.instance, init.window);
}
}

void device_initialization_phase_2(InstanceBuilder const& instance_builder, InitInfo& init)
void device_initialization_phase_1(InstanceBuilder const& instance_builder, InitInfo& init)
{
init.instance = instance_builder.build();

init.inst_disp = init.instance.make_table();

init.surface = create_surface_sdl(init.instance, init.window);
}

PhysicalDevice device_initialization_phase_3(PhysicalDeviceSelector& phys_device_selector, InitInfo& init)
Expand All @@ -2857,10 +2858,10 @@ InitInfo device_initialization(const std::string& window_name)
{
InitInfo init;

device_initialization_phase_1(window_name, init);

InstanceBuilder instance_builder;
device_initialization_phase_2(instance_builder, init);
device_initialization_phase_1(instance_builder, init);

device_initialization_phase_2(window_name, init);

PhysicalDeviceSelector phys_device_selector(init.instance);
init.physical_device = device_initialization_phase_3(phys_device_selector, init);
Expand Down Expand Up @@ -2900,11 +2901,11 @@ void recreate_init_swapchain(SwapchainBuilder& swapchain_builder, InitInfo& init

void TestAppBase::run(const std::string& window_name)
{
device_initialization_phase_1(window_name, init);

InstanceBuilder instance_builder;
configure_instance_builder(instance_builder);
device_initialization_phase_2(instance_builder, init);
device_initialization_phase_1(instance_builder, init);

device_initialization_phase_2(window_name, init);

PhysicalDeviceSelector phys_device_selector(init.instance);
configure_physical_device_selector(phys_device_selector);
Expand All @@ -2914,9 +2915,11 @@ void TestAppBase::run(const std::string& window_name)
configure_device_builder(device_builder, init.physical_device);
device_initialization_phase_4(device_builder, init);

SwapchainBuilder swapchain_builder{ init.device };
configure_swapchain_builder(swapchain_builder);
device_initialization_phase_5(swapchain_builder, init);
if (!init.instance.headless) {
SwapchainBuilder swapchain_builder{ init.device };
configure_swapchain_builder(swapchain_builder);
device_initialization_phase_5(swapchain_builder, init);
}

setup();

Expand Down
24 changes: 12 additions & 12 deletions test/test_apps/common/test_app_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <cstring>

#include <optional>
#include <exception>
#include <stdexcept>
#include <vector>
#include <string>

Expand Down Expand Up @@ -176,17 +176,17 @@ const char* to_string(QueueError err);
const char* to_string(DeviceError err);
const char* to_string(SwapchainError err);

std::exception to_exception(InstanceError err);
std::exception to_exception(PhysicalDeviceError err);
std::exception to_exception(QueueError err);
std::exception to_exception(DeviceError err);
std::exception to_exception(SwapchainError err);
std::runtime_error to_exception(InstanceError err);
std::runtime_error to_exception(PhysicalDeviceError err);
std::runtime_error to_exception(QueueError err);
std::runtime_error to_exception(DeviceError err);
std::runtime_error to_exception(SwapchainError err);

std::exception to_exception(InstanceError err, VkResult result);
std::exception to_exception(PhysicalDeviceError err, VkResult result);
std::exception to_exception(QueueError err, VkResult result);
std::exception to_exception(DeviceError err, VkResult result);
std::exception to_exception(SwapchainError err, VkResult result);
std::runtime_error to_exception(InstanceError err, VkResult result);
std::runtime_error to_exception(PhysicalDeviceError err, VkResult result);
std::runtime_error to_exception(QueueError err, VkResult result);
std::runtime_error to_exception(DeviceError err, VkResult result);
std::runtime_error to_exception(SwapchainError err, VkResult result);

// Gathers useful information about the available vulkan capabilities, like layers and instance
// extensions. Use this for enabling features conditionally, ie if you would like an extension but
Expand Down Expand Up @@ -249,8 +249,8 @@ struct Instance
// Return a loaded instance dispatch table
vkb::InstanceDispatchTable make_table() const;

private:
bool headless = false;
private:
bool properties2_ext_enabled = false;
uint32_t instance_version = VKB_VK_API_VERSION_1_0;
uint32_t api_version = VKB_VK_API_VERSION_1_0;
Expand Down

0 comments on commit df26f7e

Please sign in to comment.