Skip to content

Commit

Permalink
[exceptions] Introduced custom base class for exceptions and special …
Browse files Browse the repository at this point in the history
…class for Vulkan API exceptions.
  • Loading branch information
IAmNotHanni committed Jan 2, 2021
1 parent 8d6a115 commit ba7e494
Show file tree
Hide file tree
Showing 28 changed files with 507 additions and 191 deletions.
16 changes: 13 additions & 3 deletions example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "inexor/vulkan-renderer/application.hpp"

#include "inexor/vulkan-renderer/exceptions/vk_exception.hpp"

#include <spdlog/async.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
Expand All @@ -22,8 +24,16 @@ int main(int argc, char *argv[]) {
spdlog::debug("Inexor vulkan-renderer, BUILD " + std::string(__DATE__) + ", " + __TIME__);
spdlog::debug("Parsing command line arguments.");

inexor::vulkan_renderer::Application renderer(argc, argv);
renderer.run();
renderer.calculate_memory_budget();
std::unique_ptr<inexor::vulkan_renderer::Application> renderer;

try {
renderer = std::make_unique<inexor::vulkan_renderer::Application>(argc, argv);
} catch (inexor::vulkan_renderer::exceptions::VulkanException &exception) {
spdlog::critical(exception.what());
std::abort();
}

renderer->run();
renderer->calculate_memory_budget();
spdlog::debug("Window closed");
}
15 changes: 15 additions & 0 deletions include/inexor/vulkan-renderer/exceptions/exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <stdexcept>
#include <string>

namespace inexor::vulkan_renderer::exceptions {

/// @brief A custom base class for exceptions
class Exception : public std::runtime_error {
public:
// No need to define own constructors.
using std::runtime_error::runtime_error;
};

} // namespace inexor::vulkan_renderer::exceptions
29 changes: 29 additions & 0 deletions include/inexor/vulkan-renderer/exceptions/vk_exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "inexor/vulkan-renderer/exceptions/exception.hpp"

#include <vulkan/vulkan_core.h>

namespace inexor::vulkan_renderer::exceptions {

/// @brief
class VulkanException final : public Exception {
private:
///@brief Return a VkResult's description text.
/// @note This function can be used for both VkResult error and success values.
/// @param result The VkResult return value which will be turned into a string.
[[nodiscard]] static std::string get_vkresult_description(const VkResult result);

/// @brief Turn a VkResult into a string.
/// @note This function can be used for both VkResult error and success values.
/// @param result The VkResult return value which will be turned into a string.
[[nodiscard]] static std::string get_vkresult_string(const VkResult result);

public:
/// @brief Default constructor.
/// @param message The exception message.
/// @param result The VkResult value of the Vulkan API call which failed.
VulkanException(const std::string message, const VkResult result);
};

} // namespace inexor::vulkan_renderer::exceptions
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ set(INEXOR_SOURCE_FILES
vulkan-renderer/settings_decision_maker.cpp
vulkan-renderer/time_step.cpp

vulkan-renderer/input/keyboard_mouse_data.cpp
vulkan-renderer/exceptions/vk_exception.cpp

vulkan-renderer/input/keyboard_mouse_data.cpp

vulkan-renderer/io/byte_stream.cpp
vulkan-renderer/io/octree_parser.cpp
Expand Down
10 changes: 6 additions & 4 deletions src/vulkan-renderer/application.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "inexor/vulkan-renderer/application.hpp"

#include "inexor/vulkan-renderer/debug_callback.hpp"
#include "inexor/vulkan-renderer/exceptions/vk_exception.hpp"
#include "inexor/vulkan-renderer/octree_gpu_vertex.hpp"
#include "inexor/vulkan-renderer/standard_ubo.hpp"
#include "inexor/vulkan-renderer/tools/cla_parser.hpp"
Expand Down Expand Up @@ -53,7 +54,7 @@ void Application::load_toml_configuration_file(const std::string &file_name) {

std::ifstream toml_file(file_name, std::ios::in);
if (!toml_file) {
throw std::runtime_error(std::string("Could not open configuration file: " + file_name + "!"));
throw std::runtime_error("Could not open configuration file: " + file_name + "!");
}

toml_file.close();
Expand Down Expand Up @@ -340,9 +341,10 @@ Application::Application(int argc, char **argv) {
vkGetInstanceProcAddr(m_instance->instance(), "vkCreateDebugReportCallbackEXT"));

if (vkCreateDebugReportCallbackEXT) {
if (vkCreateDebugReportCallbackEXT(m_instance->instance(), &debug_report_ci, nullptr,
&m_debug_report_callback) != VK_SUCCESS) {
throw std::runtime_error("Error: vkCreateDebugReportCallbackEXT failed!");
if (const auto result = vkCreateDebugReportCallbackEXT(m_instance->instance(), &debug_report_ci,
nullptr, &m_debug_report_callback);
result != VK_SUCCESS) {
throw exceptions::VulkanException("Error: vkCreateDebugReportCallbackEXT failed!", result);
}
spdlog::debug("Creating Vulkan debug callback.");
m_debug_report_callback_initialised = true;
Expand Down
57 changes: 34 additions & 23 deletions src/vulkan-renderer/availability_checks.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "inexor/vulkan-renderer/availability_checks.hpp"

#include "inexor/vulkan-renderer/exceptions/vk_exception.hpp"

#include <algorithm>
#include <cassert>
#include <cstring>
Expand All @@ -9,8 +11,9 @@ namespace inexor::vulkan_renderer {

void AvailabilityChecksManager::create_instance_extensions_cache() {
// First ask Vulkan how many instance extensions are available on the system.
if (vkEnumerateInstanceExtensionProperties(nullptr, &m_available_instance_extensions, nullptr) != VK_SUCCESS) {
throw std::runtime_error("Error: vkEnumerateInstanceExtensionProperties failed!");
if (const auto result = vkEnumerateInstanceExtensionProperties(nullptr, &m_available_instance_extensions, nullptr);
result != VK_SUCCESS) {
throw exceptions::VulkanException("Error: vkEnumerateInstanceExtensionProperties failed!", result);
}

if (m_available_instance_extensions == 0) {
Expand All @@ -22,9 +25,10 @@ void AvailabilityChecksManager::create_instance_extensions_cache() {
m_instance_extensions_cache.resize(m_available_instance_extensions);

// Get the information about the available instance extensions.
if (vkEnumerateInstanceExtensionProperties(nullptr, &m_available_instance_extensions,
m_instance_extensions_cache.data())) {
throw std::runtime_error("Error: vkEnumerateInstanceExtensionProperties failed!");
if (const auto result = vkEnumerateInstanceExtensionProperties(nullptr, &m_available_instance_extensions,
m_instance_extensions_cache.data());
result != VK_SUCCESS) {
throw exceptions::VulkanException("Error: vkEnumerateInstanceExtensionProperties failed!", result);
}
}
}
Expand All @@ -50,8 +54,9 @@ bool AvailabilityChecksManager::has_instance_extension(const std::string &instan

void AvailabilityChecksManager::create_instance_layers_cache() {
// First ask Vulkan how many instance layers are available on the system.
if (vkEnumerateInstanceLayerProperties(&m_available_instance_layers, nullptr) != VK_SUCCESS) {
throw std::runtime_error("Error: vkEnumerateInstanceLayerProperties failed!");
if (const auto result = vkEnumerateInstanceLayerProperties(&m_available_instance_layers, nullptr);
result != VK_SUCCESS) {
throw exceptions::VulkanException("Error: vkEnumerateInstanceLayerProperties failed!", result);
}

if (m_available_instance_layers == 0) {
Expand All @@ -62,9 +67,10 @@ void AvailabilityChecksManager::create_instance_layers_cache() {
m_instance_layers_cache.resize(m_available_instance_layers);

// Get the information about the available instance layers.
if (vkEnumerateInstanceLayerProperties(&m_available_instance_layers, m_instance_layers_cache.data()) !=
VK_SUCCESS) {
throw std::runtime_error("Error: vkEnumerateInstanceLayerProperties failed!");
if (const auto result =
vkEnumerateInstanceLayerProperties(&m_available_instance_layers, m_instance_layers_cache.data());
result != VK_SUCCESS) {
throw exceptions::VulkanException("Error: vkEnumerateInstanceLayerProperties failed!", result);
}
}

Expand All @@ -88,8 +94,9 @@ bool AvailabilityChecksManager::has_instance_layer(const std::string &instance_l

void AvailabilityChecksManager::create_device_layers_cache(const VkPhysicalDevice &graphics_card) {
// First ask Vulkan how many device layers are available on the system.
if (vkEnumerateDeviceLayerProperties(graphics_card, &m_available_device_layers, nullptr) != VK_SUCCESS) {
throw std::runtime_error("Error: vkEnumerateDeviceLayerProperties failed!");
if (const auto result = vkEnumerateDeviceLayerProperties(graphics_card, &m_available_device_layers, nullptr);
result != VK_SUCCESS) {
throw exceptions::VulkanException("Error: vkEnumerateDeviceLayerProperties failed!", result);
}

if (m_available_device_layers == 0) {
Expand All @@ -100,9 +107,10 @@ void AvailabilityChecksManager::create_device_layers_cache(const VkPhysicalDevic
m_device_layer_properties_cache.resize(m_available_device_layers);

// Get the information about the available device layers.
if (vkEnumerateDeviceLayerProperties(graphics_card, &m_available_device_layers,
m_device_layer_properties_cache.data()) != VK_SUCCESS) {
throw std::runtime_error("Error: vkEnumerateDeviceLayerProperties failed!");
if (const auto result = vkEnumerateDeviceLayerProperties(graphics_card, &m_available_device_layers,
m_device_layer_properties_cache.data());
result != VK_SUCCESS) {
throw exceptions::VulkanException("Error: vkEnumerateDeviceLayerProperties failed!", result);
}
}

Expand All @@ -127,9 +135,10 @@ bool AvailabilityChecksManager::has_device_layer(const VkPhysicalDevice &graphic

void AvailabilityChecksManager::create_device_extensions_cache(const VkPhysicalDevice &graphics_card) {
// First ask Vulkan how many device extensions are available on the system.
if (vkEnumerateDeviceExtensionProperties(graphics_card, nullptr, &m_available_device_extensions, nullptr) !=
VK_SUCCESS) {
throw std::runtime_error("Error: vkEnumerateDeviceExtensionProperties failed!");
if (const auto result =
vkEnumerateDeviceExtensionProperties(graphics_card, nullptr, &m_available_device_extensions, nullptr);
result != VK_SUCCESS) {
throw exceptions::VulkanException("Error: vkEnumerateDeviceExtensionProperties failed!", result);
}

if (m_available_device_extensions == 0) {
Expand All @@ -140,9 +149,10 @@ void AvailabilityChecksManager::create_device_extensions_cache(const VkPhysicalD
m_device_extensions_cache.resize(m_available_device_extensions);

// Get the information about the available device extensions.
if (vkEnumerateDeviceExtensionProperties(graphics_card, nullptr, &m_available_device_extensions,
m_device_extensions_cache.data())) {
throw std::runtime_error("Error: vkEnumerateDeviceExtensionProperties failed!");
if (const auto result = vkEnumerateDeviceExtensionProperties(
graphics_card, nullptr, &m_available_device_extensions, m_device_extensions_cache.data());
result != VK_SUCCESS) {
throw exceptions::VulkanException("Error: vkEnumerateDeviceExtensionProperties failed!", result);
}
}
}
Expand Down Expand Up @@ -172,8 +182,9 @@ bool AvailabilityChecksManager::has_presentation(const VkPhysicalDevice &graphic
VkBool32 presentation_available = false;

// Query if presentation is supported.
if (vkGetPhysicalDeviceSurfaceSupportKHR(graphics_card, 0, surface, &presentation_available) != VK_SUCCESS) {
throw std::runtime_error("Error: vkGetPhysicalDeviceSurfaceSupportKHR failed!");
if (const auto result = vkGetPhysicalDeviceSurfaceSupportKHR(graphics_card, 0, surface, &presentation_available);
result != VK_SUCCESS) {
throw exceptions::VulkanException("Error: vkGetPhysicalDeviceSurfaceSupportKHR failed!", result);
}

return presentation_available;
Expand Down
Loading

0 comments on commit ba7e494

Please sign in to comment.