[WIP] Refactor project
There are many problems with the way the current version is provided and maintained. This commit or series of commits will fix this.
This commit is contained in:
1 parent
f5ab5f3f27
commit
173a82b9d1
63 files changed
+2663
-2239
No files matched your search
@@ -0,0 +1,213 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "utilities/commons.h"
|
||||
#include "utilities/errors_common.h"
|
||||
#include "utilities/logger.h"
|
||||
#include "utilities/localization.h"
|
||||
#include "vulkan/vulkan_core.h"
|
||||
#include "vulkan_commons.h"
|
||||
#include "vulkan_descriptors.h"
|
||||
#include "vulkan_errors.h"
|
||||
#include "vulkan_image.h"
|
||||
#include "vulkan_swapchain.h"
|
||||
|
||||
/**
|
||||
* @brief Create a Swapchain
|
||||
*
|
||||
* @return rse_err_t RSE_ERROR_NO_ERROR on success. Possible errors:
|
||||
* VULKAN_ERROR_SWAPCHAIN_CREATION_FAILED
|
||||
*/
|
||||
static rse_err_t create_swapchain(struct graphics_context_t* context)
|
||||
{
|
||||
VkSwapchainCreateInfoKHR create_info;
|
||||
VkSurfaceCapabilitiesKHR physical_device_surface_capabilities;
|
||||
VkBool32 surfaceSupported;
|
||||
|
||||
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
|
||||
context->physical_device, context->surface,
|
||||
&physical_device_surface_capabilities);
|
||||
|
||||
/* Store extent in global variable, for later use */
|
||||
context->swapchain_extent =
|
||||
physical_device_surface_capabilities.currentExtent;
|
||||
|
||||
/* Check if device supports surface for presentation */
|
||||
vkGetPhysicalDeviceSurfaceSupportKHR(context->physical_device,
|
||||
context->queue_family_indices[0],
|
||||
context->surface, &surfaceSupported);
|
||||
|
||||
create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
||||
create_info.pNext = NULL;
|
||||
create_info.flags = 0U;
|
||||
create_info.surface = context->surface;
|
||||
create_info.minImageCount = SWAP_BUFFER_COUNT + 1;
|
||||
create_info.imageFormat = IMAGE_FORMAT;
|
||||
create_info.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
|
||||
create_info.imageExtent = context->swapchain_extent;
|
||||
create_info.imageArrayLayers =
|
||||
1U; /* For non-stereoscopic-3D applications, this value is 1. */
|
||||
create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
||||
create_info.imageSharingMode =
|
||||
VK_SHARING_MODE_EXCLUSIVE; /* Using only one queue family, so this is ok
|
||||
*/
|
||||
create_info.queueFamilyIndexCount = 1U;
|
||||
create_info.pQueueFamilyIndices = context->queue_family_indices;
|
||||
create_info.preTransform =
|
||||
physical_device_surface_capabilities.currentTransform;
|
||||
create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
||||
create_info.presentMode = VK_PRESENT_MODE_FIFO_KHR;
|
||||
create_info.clipped = VK_TRUE;
|
||||
create_info.oldSwapchain = VK_NULL_HANDLE;
|
||||
|
||||
if (VK_SUCCESS != vkCreateSwapchainKHR(context->device, &create_info, NULL,
|
||||
&context->swapchain)) {
|
||||
LOGF(_("Failed to create swapchain\n"));
|
||||
return VULKAN_ERROR_SWAPCHAIN_CREATION_FAILED;
|
||||
}
|
||||
|
||||
/* Obtain swapchain images */
|
||||
vkGetSwapchainImagesKHR(context->device, context->swapchain,
|
||||
&context->swapchain_images_count, NULL);
|
||||
rse_malloc(context->swapchain_images, sizeof(VkImage) * context->swapchain_images_count);
|
||||
vkGetSwapchainImagesKHR(context->device, context->swapchain,
|
||||
&context->swapchain_images_count, context->swapchain_images);
|
||||
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create a Swapchain Image Views
|
||||
*
|
||||
* @return rse_err_t RSE_ERROR_NO_ERROR on success. Possible errors:
|
||||
*/
|
||||
static rse_err_t create_swapchain_image_views(struct graphics_context_t* context)
|
||||
{
|
||||
rse_malloc(context->swapchain_image_views,
|
||||
sizeof(VkImageView) * context->swapchain_images_count);
|
||||
for (size_t i = 0; i < context->swapchain_images_count; i++) {
|
||||
VkImageViewCreateInfo create_info;
|
||||
|
||||
create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||
create_info.pNext = NULL;
|
||||
create_info.flags = 0U;
|
||||
create_info.image = context->swapchain_images[i];
|
||||
create_info.viewType = VK_IMAGE_VIEW_TYPE_2D; /* 2D Texture */
|
||||
create_info.format = VK_FORMAT_B8G8R8A8_SRGB;
|
||||
create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||
create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||
create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||
create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
|
||||
create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
create_info.subresourceRange.baseMipLevel = 0U;
|
||||
create_info.subresourceRange.levelCount = 1U;
|
||||
create_info.subresourceRange.baseArrayLayer = 0U;
|
||||
create_info.subresourceRange.layerCount = 1U;
|
||||
|
||||
if (VK_SUCCESS !=
|
||||
vkCreateImageView(context->device, &create_info, NULL,
|
||||
&context->swapchain_image_views[i])) {
|
||||
LOGF(_("Failed to create swapchain image view\n"));
|
||||
return VULKAN_ERROR_SWAPCHAIN_IMVIEW_CREATION_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create Frambuffers
|
||||
*
|
||||
* @return rse_err_t RSE_ERROR_NO_ERROR on success. Possible errors:
|
||||
*/
|
||||
static rse_err_t create_framebuffers(struct graphics_context_t* context)
|
||||
{
|
||||
context->swapchain_framebuffers = NULL;
|
||||
|
||||
rse_malloc(context->swapchain_framebuffers,
|
||||
sizeof(VkFramebuffer) * context->swapchain_images_count);
|
||||
|
||||
for (size_t i = 0; i < context->swapchain_images_count; i++) {
|
||||
VkImageView attachments[] = {context->color_image.image_view,
|
||||
context->depth_image.image_view,
|
||||
context->swapchain_image_views[i]};
|
||||
|
||||
VkFramebufferCreateInfo framebufferInfo = {};
|
||||
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
|
||||
framebufferInfo.renderPass = context->render_pass;
|
||||
framebufferInfo.attachmentCount = 3;
|
||||
framebufferInfo.pAttachments = attachments;
|
||||
framebufferInfo.width = context->swapchain_extent.width;
|
||||
framebufferInfo.height = context->swapchain_extent.height;
|
||||
framebufferInfo.layers = 1;
|
||||
|
||||
if (vkCreateFramebuffer(context->device, &framebufferInfo, NULL,
|
||||
&context->swapchain_framebuffers[i]) !=
|
||||
VK_SUCCESS) {
|
||||
LOGF(_("Failed to create framebuffer\n"));
|
||||
return VULKAN_ERROR_FRAMEBUFFERS_CREATION_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Recreate swapchain
|
||||
*
|
||||
*/
|
||||
void recreate_swapchain(struct graphics_context_t* context)
|
||||
{
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
|
||||
glfwGetFramebufferSize(context->window_handle, &width, &height);
|
||||
while (width == 0 || height == 0) {
|
||||
glfwGetFramebufferSize(context->window_handle, &width, &height);
|
||||
glfwWaitEvents();
|
||||
}
|
||||
|
||||
vkDeviceWaitIdle(context->device);
|
||||
|
||||
cleanup_swapchain(context);
|
||||
|
||||
create_swapchain(context);
|
||||
create_swapchain_image_views(context);
|
||||
create_color_resource(context);
|
||||
create_depth_resources(context);
|
||||
create_framebuffers(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Cleanup swapchain
|
||||
*
|
||||
*/
|
||||
void cleanup_swapchain(struct graphics_context_t* context)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
destroy_color_resource(context);
|
||||
destroy_depth_resource(context);
|
||||
for (i = 0; i < context->swapchain_images_count; i++) {
|
||||
vkDestroyFramebuffer(context->device,
|
||||
context->swapchain_framebuffers[i], NULL);
|
||||
}
|
||||
|
||||
for (i = 0; i < context->swapchain_images_count; i++) {
|
||||
vkDestroyImageView(context->device, context->swapchain_image_views[i],
|
||||
NULL);
|
||||
}
|
||||
|
||||
vkDestroySwapchainKHR(context->device, context->swapchain, NULL);
|
||||
}
|
||||
|
||||
rse_err_t create_swapchain_and_framebuffers(struct graphics_context_t* context)
|
||||
{
|
||||
rse_err_t status = RSE_ERROR_NO_ERROR;
|
||||
|
||||
STATUS_CHECK(create_swapchain(context));
|
||||
STATUS_CHECK(create_swapchain_image_views(context));
|
||||
STATUS_CHECK(create_framebuffers(context));
|
||||
|
||||
return status;
|
||||
}
|
||||
Reference in new issue
Block a user