Skip to content

Commit

Permalink
[code] Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmNotHanni committed May 17, 2020
1 parent 730adb2 commit 46c3078
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 45 deletions.
8 changes: 1 addition & 7 deletions include/inexor/vulkan-renderer/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,9 @@ class VulkanRenderer {
/// @param graphics_card The regarded graphics card.
VkResult create_physical_device(const VkPhysicalDevice &graphics_card, const bool enable_debug_markers = true);

/// @brief Creates an instance of VulkanDebugMarkerManager
/// @brief Creates an instance of VulkanDebugMarkerManager.
VkResult initialise_debug_marker_manager(const bool enable_debug_markers = true);

/// @brief Initialises glTF 2.0 model manager.
VkResult initialise_glTF2_model_manager();

VkResult update_cameras();

/// @brief Create depth image.
Expand All @@ -183,9 +180,6 @@ class VulkanRenderer {
/// @brief Creates the semaphores neccesary for synchronisation.
VkResult create_synchronisation_objects();

/// @brief Creates the swapchain.
VkResult create_swapchain();

/// @brief Cleans the swapchain.
VkResult cleanup_swapchain();

Expand Down
15 changes: 7 additions & 8 deletions include/inexor/vulkan-renderer/wrapper/swapchain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ class Swapchain {
VkPhysicalDevice graphics_card;
VkSurfaceKHR surface;
VkSwapchainKHR swapchain;
VkSwapchainKHR old_swapchain;
VkSurfaceFormatKHR surface_format;
VkExtent2D extent;

std::vector<VkImage> swapchain_images;
std::vector<VkImageView> swapchain_image_views;
std::uint32_t images_in_swapchain_count;
std::uint32_t swapchain_image_count;
bool vsync_enabled;

/// @brief (Re)creates the swapchain.
/// @param window_width [in] The requested width of the window.
/// @param window_height [in] The requested height of the window.
/// @note We are passing width and height of the window as reference since the API
/// needs to check if the swapchain can support the requested resolution.
void setup_swapchain(std::uint32_t &window_width, std::uint32_t &window_height);
void setup_swapchain(const VkSwapchainKHR old_swapchain, std::uint32_t window_width, std::uint32_t window_height);

public:
/// Delete the copy constructor so swapchains are move-only objects.
Expand All @@ -45,17 +44,17 @@ class Swapchain {

/// @brief
/// @note We must pass width and height as call by reference!
Swapchain(const VkDevice device, const VkPhysicalDevice graphics_card, const VkSurfaceKHR surface, std::uint32_t &window_width,
std::uint32_t &window_height, const bool enable_vsync);
Swapchain(const VkDevice device, const VkPhysicalDevice graphics_card, const VkSurfaceKHR surface, std::uint32_t window_width, std::uint32_t window_height,
const bool enable_vsync);

~Swapchain();

/// @brief The swapchain needs to be recreated if it has been invalidated.
/// @note We must pass width and height as call by reference!
/// This happens for example when the window gets resized.
void recreate(std::uint32_t &window_width, std::uint32_t &window_height);
void recreate(std::uint32_t window_width, std::uint32_t window_height);

[[nodiscard]] const VkSwapchainKHR* get_swapchain_ptr() const {
[[nodiscard]] const VkSwapchainKHR *get_swapchain_ptr() const {
return &swapchain;
}

Expand All @@ -64,7 +63,7 @@ class Swapchain {
}

[[nodiscard]] std::uint32_t get_image_count() const {
return images_in_swapchain_count;
return swapchain_image_count;
}

[[nodiscard]] VkFormat get_image_format() const {
Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/vulkan-renderer/bezier_curve.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "inexor/vulkan-renderer/bezier_curve.h"
#include "inexor/vulkan-renderer/bezier_curve.hpp"

namespace inexor::vulkan_renderer {

Expand Down
51 changes: 22 additions & 29 deletions src/vulkan-renderer/wrapper/swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ namespace inexor::vulkan_renderer::wrapper {

Swapchain::Swapchain(Swapchain &&other) noexcept
: device(other.device), graphics_card(std::exchange(other.graphics_card, nullptr)), surface(std::exchange(other.surface, nullptr)),
swapchain(std::exchange(other.swapchain, nullptr)), old_swapchain(std::exchange(other.old_swapchain, nullptr)), surface_format(other.surface_format),
extent(other.extent), swapchain_images(std::move(other.swapchain_images)), swapchain_image_views(std::move(other.swapchain_image_views)),
images_in_swapchain_count(other.images_in_swapchain_count), vsync_enabled(other.vsync_enabled) {}
swapchain(std::exchange(other.swapchain, nullptr)), surface_format(other.surface_format), extent(other.extent),
swapchain_images(std::move(other.swapchain_images)), swapchain_image_views(std::move(other.swapchain_image_views)),
swapchain_image_count(other.swapchain_image_count), vsync_enabled(other.vsync_enabled) {}

void Swapchain::setup_swapchain(std::uint32_t &window_width, std::uint32_t &window_height) {
void Swapchain::setup_swapchain(const VkSwapchainKHR old_swapchain, std::uint32_t window_width, std::uint32_t window_height) {
VulkanSettingsDecisionMaker settings_decision_maker;

settings_decision_maker.decide_width_and_height_of_swapchain_extent(graphics_card, surface, window_width, window_height, extent);
Expand All @@ -19,13 +19,7 @@ void Swapchain::setup_swapchain(std::uint32_t &window_width, std::uint32_t &wind
throw std::runtime_error("Error: Could not find a suitable present mode!");
}

images_in_swapchain_count = settings_decision_maker.decide_how_many_images_in_swapchain_to_use(graphics_card, surface);

// Find the transformation of the surface.
auto pre_transform = settings_decision_maker.decide_which_image_transformation_to_use(graphics_card, surface);

// Find a supported composite alpha format (not all devices support alpha opaque).
auto composite_alpha_format = settings_decision_maker.find_composite_alpha_format(graphics_card, surface);
swapchain_image_count = settings_decision_maker.decide_how_many_images_in_swapchain_to_use(graphics_card, surface);

auto surface_format_candidate = settings_decision_maker.decide_which_surface_color_format_in_swapchain_to_use(graphics_card, surface);

Expand All @@ -38,13 +32,14 @@ void Swapchain::setup_swapchain(std::uint32_t &window_width, std::uint32_t &wind
VkSwapchainCreateInfoKHR swapchain_create_info = {};
swapchain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchain_create_info.surface = surface;
swapchain_create_info.minImageCount = images_in_swapchain_count;
swapchain_create_info.minImageCount = swapchain_image_count;
swapchain_create_info.imageFormat = surface_format.format;
swapchain_create_info.imageColorSpace = surface_format.colorSpace;
swapchain_create_info.imageExtent.width = extent.width;
swapchain_create_info.imageExtent.height = extent.height;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_create_info.preTransform = (VkSurfaceTransformFlagBitsKHR)pre_transform;
swapchain_create_info.preTransform =
(VkSurfaceTransformFlagBitsKHR)settings_decision_maker.decide_which_image_transformation_to_use(graphics_card, surface);
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.queueFamilyIndexCount = 0;
Expand All @@ -57,7 +52,7 @@ void Swapchain::setup_swapchain(std::uint32_t &window_width, std::uint32_t &wind

// Setting clipped to VK_TRUE allows the implementation to discard rendering outside of the surface area.
swapchain_create_info.clipped = VK_TRUE;
swapchain_create_info.compositeAlpha = composite_alpha_format;
swapchain_create_info.compositeAlpha = settings_decision_maker.find_composite_alpha_format(graphics_card, surface);

// Set additional usage flag for blitting from the swapchain images if supported.
VkFormatProperties formatProps;
Expand All @@ -72,21 +67,21 @@ void Swapchain::setup_swapchain(std::uint32_t &window_width, std::uint32_t &wind
throw std::runtime_error("Error: vkCreateSwapchainKHR failed!");
}

if (vkGetSwapchainImagesKHR(device, swapchain, &images_in_swapchain_count, nullptr) != VK_SUCCESS) {
if (vkGetSwapchainImagesKHR(device, swapchain, &swapchain_image_count, nullptr) != VK_SUCCESS) {
throw std::runtime_error("Error: vkGetSwapchainImagesKHR failed!");
}

swapchain_images.resize(images_in_swapchain_count);
swapchain_images.resize(swapchain_image_count);

if (vkGetSwapchainImagesKHR(device, swapchain, &images_in_swapchain_count, swapchain_images.data())) {
if (vkGetSwapchainImagesKHR(device, swapchain, &swapchain_image_count, swapchain_images.data())) {
throw std::runtime_error("Error: vkGetSwapchainImagesKHR failed!");
}

// TODO: Assign an appropriate debug marker name to the swapchain images.

spdlog::debug("Creating {} swapchain image views.", images_in_swapchain_count);
spdlog::debug("Creating {} swapchain image views.", swapchain_image_count);

swapchain_image_views.resize(images_in_swapchain_count);
swapchain_image_views.resize(swapchain_image_count);

VkImageViewCreateInfo image_view_create_info = {};
image_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
Expand All @@ -102,7 +97,7 @@ void Swapchain::setup_swapchain(std::uint32_t &window_width, std::uint32_t &wind
image_view_create_info.subresourceRange.baseArrayLayer = 0;
image_view_create_info.subresourceRange.layerCount = 1;

for (std::size_t i = 0; i < images_in_swapchain_count; i++) {
for (std::size_t i = 0; i < swapchain_image_count; i++) {
spdlog::debug("Creating swapchain image #{}.", i);

image_view_create_info.image = swapchain_images[i];
Expand All @@ -114,25 +109,23 @@ void Swapchain::setup_swapchain(std::uint32_t &window_width, std::uint32_t &wind
// TODO: Use Vulkan debug markers to assign an appropriate name to this swapchain image view.
}

spdlog::debug("Created {} swapchain image views successfully.", images_in_swapchain_count);
spdlog::debug("Created {} swapchain image views successfully.", swapchain_image_count);
}

Swapchain::Swapchain(const VkDevice device, const VkPhysicalDevice graphics_card, const VkSurfaceKHR surface, std::uint32_t &window_width,
std::uint32_t &window_height, const bool enable_vsync)
Swapchain::Swapchain(const VkDevice device, const VkPhysicalDevice graphics_card, const VkSurfaceKHR surface, std::uint32_t window_width,
std::uint32_t window_height, const bool enable_vsync)
: device(device), graphics_card(graphics_card), surface(surface), vsync_enabled(enable_vsync) {

assert(device);
assert(graphics_card);
assert(surface);

old_swapchain = VK_NULL_HANDLE;

setup_swapchain(window_width, window_height);
setup_swapchain(VK_NULL_HANDLE, window_width, window_height);
}

void Swapchain::recreate(std::uint32_t &window_width, std::uint32_t &window_height) {
void Swapchain::recreate(std::uint32_t window_width, std::uint32_t window_height) {
// Store the old swapchain. This allows us to pass it to VkSwapchainCreateInfoKHR::oldSwapchain to speed up swapchain recreation.
old_swapchain = swapchain;
VkSwapchainKHR old_swapchain = swapchain;

// When swapchain needs to be recreated, all the old swapchain images need to be destroyed.

Expand All @@ -146,7 +139,7 @@ void Swapchain::recreate(std::uint32_t &window_width, std::uint32_t &window_heig

swapchain_images.clear();

setup_swapchain(window_width, window_height);
setup_swapchain(old_swapchain, window_width, window_height);
}

Swapchain::~Swapchain() {
Expand Down
Empty file.

0 comments on commit 46c3078

Please sign in to comment.