Refactor project
Crealned up some TODOs, FIXMEs etc. Added better error handling. Looks better, feels better.
This commit is contained in:
1 parent
bd4dd74881
commit
42dc4d6fcd
29 files changed
+750
-645
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "utilities/file_utils.h"
|
||||
#include "utilities/logger.h"
|
||||
#include "utilities/rse_errors_common.h"
|
||||
#include "locale_vulkan.h"
|
||||
#include "vulkan_errors.h"
|
||||
#include "rse_vulkan_commands.h"
|
||||
@@ -42,7 +43,7 @@ struct rse_vulkan_image_t g_depth_image;
|
||||
VkSampler g_sampler;
|
||||
|
||||
|
||||
static uint8_t create_sampler()
|
||||
static rse_err_t create_sampler()
|
||||
{
|
||||
VkSamplerCreateInfo create_info = {0};
|
||||
|
||||
@@ -73,17 +74,14 @@ static uint8_t create_sampler()
|
||||
return 1;
|
||||
}
|
||||
|
||||
return VULKAN_ERROR_NO_ERROR;
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
// static uint8_t has_stencil_component(VkFormat format) {
|
||||
// return format == VK_FORMAT_D32_SFLOAT_S8_UINT || format == VK_FORMAT_D24_UNORM_S8_UINT;
|
||||
// }
|
||||
|
||||
static VkFormat find_supported_format(const VkFormat* candidates,
|
||||
static rse_err_t find_supported_format(const VkFormat* candidates,
|
||||
size_t candidates_count,
|
||||
VkImageTiling tiling,
|
||||
VkFormatFeatureFlags features)
|
||||
VkFormatFeatureFlags features,
|
||||
VkFormat* found_format)
|
||||
{
|
||||
size_t i = 0U;
|
||||
|
||||
@@ -92,20 +90,21 @@ static VkFormat find_supported_format(const VkFormat* candidates,
|
||||
vkGetPhysicalDeviceFormatProperties(g_vulkan_state.physical_device, candidates[i], &props);
|
||||
|
||||
if (tiling == VK_IMAGE_TILING_LINEAR && (props.linearTilingFeatures & features) == features) {
|
||||
return candidates[i];
|
||||
*found_format = candidates[i];
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
} else if (tiling == VK_IMAGE_TILING_OPTIMAL && (props.optimalTilingFeatures & features) == features) {
|
||||
return candidates[i];
|
||||
*found_format = candidates[i];
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
LOGE("Failed to find correct format");
|
||||
//FIXME: Add error handling
|
||||
return 0;
|
||||
LOGE(vulkan_messages[VULKAN_FORMAT_NOT_FOUND]);
|
||||
return VULKAN_ERROR_FORMAT_NOT_FOUND;
|
||||
}
|
||||
|
||||
static uint8_t create_depth_resources()
|
||||
static rse_err_t create_depth_resources()
|
||||
{
|
||||
uint8_t status = VULKAN_ERROR_NO_ERROR;
|
||||
rse_err_t status = RSE_ERROR_NO_ERROR;
|
||||
VkFormat depth_format = {0};
|
||||
VkExtent3D image_extent = {0};
|
||||
VkImageCreateInfo image_info = {0};
|
||||
@@ -114,7 +113,7 @@ static uint8_t create_depth_resources()
|
||||
VkSurfaceCapabilitiesKHR physical_device_surface_capabilities;
|
||||
|
||||
|
||||
depth_format = find_depth_format();
|
||||
STATUS_CHECK(find_depth_format(&depth_format));
|
||||
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(g_vulkan_state.physical_device,
|
||||
g_vulkan_state.surface,
|
||||
&physical_device_surface_capabilities);
|
||||
@@ -156,8 +155,8 @@ static uint8_t create_depth_resources()
|
||||
&g_depth_image.image,
|
||||
&g_depth_image.allocation,
|
||||
&g_depth_image.allocation_info)) {
|
||||
LOGF("Failed to create image");
|
||||
return -1;
|
||||
LOGF(vulkan_messages[VULKAN_IMAGE_NOT_CREATED]);
|
||||
return VULKAN_ERROR_IMAGE_NOT_CREATED;
|
||||
}
|
||||
|
||||
image_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||
@@ -180,19 +179,20 @@ static uint8_t create_depth_resources()
|
||||
&image_view_create_info,
|
||||
NULL,
|
||||
&g_depth_image.image_view)) {
|
||||
LOGF(vulkan_error_messages[VULKAN_SWAPCHAIN_IMVIEW_CREATION_FAILED]);
|
||||
LOGF(vulkan_messages[VULKAN_SWAPCHAIN_IMVIEW_CREATION_FAILED]);
|
||||
return VULKAN_ERROR_SWAPCHAIN_IMVIEW_CREATION_FAILED;
|
||||
}
|
||||
|
||||
return status;
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
static VkDeviceSize format_to_pixel_size(VkFormat format)
|
||||
static rse_err_t format_to_pixel_size(VkFormat format,
|
||||
VkDeviceSize* format_size)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
case VK_FORMAT_R8G8B8A8_SRGB:
|
||||
return 4;
|
||||
*format_size = 4;
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -200,12 +200,17 @@ static VkDeviceSize format_to_pixel_size(VkFormat format)
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
|
||||
static int8_t create_vulkan_image(uint32_t width, uint32_t height, VkFormat format, unsigned char* pixels)
|
||||
static rse_err_t create_vulkan_image(uint32_t width,
|
||||
uint32_t height,
|
||||
VkFormat format,
|
||||
unsigned char* pixels,
|
||||
uint8_t* texture_id)
|
||||
{
|
||||
rse_err_t status = RSE_ERROR_NO_ERROR;
|
||||
struct rse_vulkan_buffer_t staging_buffer = {0};
|
||||
VkDeviceSize staging_buffer_size = 0U;
|
||||
VkImageCreateInfo image_info = {0};
|
||||
@@ -217,23 +222,26 @@ static int8_t create_vulkan_image(uint32_t width, uint32_t height, VkFormat form
|
||||
VkExtent3D image_extent = {0};
|
||||
VkImageViewCreateInfo image_view_create_info = {0};
|
||||
struct rse_vulkan_image_t* free_texture_image = NULL;
|
||||
size_t i = 0U;
|
||||
*texture_id = 0U;
|
||||
|
||||
while(free_texture_image == NULL || i > RSE_MAX_IMAGE_COUNT) {
|
||||
if(g_texture_images[i].id_taken == IMAGE_FREE) {
|
||||
free_texture_image = &g_texture_images[i];
|
||||
while(free_texture_image == NULL || *texture_id > RSE_MAX_IMAGE_COUNT) {
|
||||
if(g_texture_images[*texture_id].id_taken == IMAGE_FREE) {
|
||||
free_texture_image = &g_texture_images[*texture_id];
|
||||
} else {
|
||||
++i;
|
||||
*texture_id += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check if we found free image handle */
|
||||
if (free_texture_image == NULL) {
|
||||
LOGE("Failed to find free texture image handle");
|
||||
return -1;
|
||||
LOGE(vulkan_messages[VULKAN_MAX_TEXTURE_COUNT_REACHED]);
|
||||
*texture_id = -1;
|
||||
return VULKAN_ERROR_MAX_TEXTURE_COUNT_REACHED;
|
||||
}
|
||||
|
||||
staging_buffer_size = width * height * format_to_pixel_size(format);
|
||||
STATUS_CHECK(format_to_pixel_size(format, &staging_buffer_size));
|
||||
|
||||
staging_buffer_size = staging_buffer_size * width * height;
|
||||
|
||||
image_extent.width = width;
|
||||
image_extent.height = height;
|
||||
@@ -270,8 +278,8 @@ static int8_t create_vulkan_image(uint32_t width, uint32_t height, VkFormat form
|
||||
&free_texture_image->image,
|
||||
&free_texture_image->allocation,
|
||||
&free_texture_image->allocation_info)) {
|
||||
LOGF("Failed to create image");
|
||||
return -1;
|
||||
LOGF(vulkan_messages[VULKAN_IMAGE_NOT_CREATED]);
|
||||
return VULKAN_ERROR_IMAGE_NOT_CREATED;
|
||||
}
|
||||
|
||||
/* Put image into correct layout to copy pixels from buffer to image memory */
|
||||
@@ -306,15 +314,12 @@ static int8_t create_vulkan_image(uint32_t width, uint32_t height, VkFormat form
|
||||
rse_end_single_time_comands(command_buffer);
|
||||
|
||||
/* Copy pixel data to GPU memory */
|
||||
if (VK_SUCCESS != create_buffer(staging_buffer_size,
|
||||
STATUS_CHECK(create_buffer(staging_buffer_size,
|
||||
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||
VMA_MEMORY_USAGE_AUTO_PREFER_HOST,
|
||||
VMA_ALLOCATION_CREATE_MAPPED_BIT
|
||||
| VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
|
||||
&staging_buffer)) {
|
||||
LOGF(vulkan_error_messages[VULKAN_BUFFER_CREATION_FAILED]);
|
||||
return -1;
|
||||
}
|
||||
&staging_buffer));
|
||||
|
||||
memcpy(staging_buffer.allocation_info.pMappedData, pixels, staging_buffer_size);
|
||||
|
||||
@@ -376,17 +381,17 @@ static int8_t create_vulkan_image(uint32_t width, uint32_t height, VkFormat form
|
||||
&image_view_create_info,
|
||||
NULL,
|
||||
&free_texture_image->image_view)) {
|
||||
LOGF("Failed to create image view");
|
||||
return -1;
|
||||
LOGF(vulkan_messages[VULKAN_IMAGE_VIEW_NOT_CREATED]);
|
||||
return VULKAN_ERROR_IMAGE_VIEW_NOT_CREATED;
|
||||
}
|
||||
|
||||
free_texture_image->id_taken = IMAGE_TAKEN;
|
||||
return i;
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
uint8_t init_vulkan_images()
|
||||
rse_err_t init_vulkan_images()
|
||||
{
|
||||
uint8_t status = VULKAN_ERROR_NO_ERROR;
|
||||
rse_err_t status = RSE_ERROR_NO_ERROR;
|
||||
|
||||
STATUS_CHECK(create_sampler());
|
||||
STATUS_CHECK(create_depth_resources());
|
||||
@@ -394,16 +399,14 @@ uint8_t init_vulkan_images()
|
||||
return status;
|
||||
}
|
||||
|
||||
int8_t rse_load_texture(const char* file_path)
|
||||
rse_err_t rse_load_texture(const char* file_path, uint8_t* texture_id)
|
||||
{
|
||||
rse_err_t status = RSE_ERROR_NO_ERROR;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int channels = 0;
|
||||
stbi_uc* pixel_buffer = NULL;
|
||||
|
||||
int8_t ret;
|
||||
|
||||
|
||||
pixel_buffer = stbi_load(file_path, &width, &height, &channels, STBI_rgb_alpha);
|
||||
|
||||
if (pixel_buffer == NULL) {
|
||||
@@ -412,14 +415,14 @@ int8_t rse_load_texture(const char* file_path)
|
||||
}
|
||||
|
||||
/* Create actual Vulkan Image */
|
||||
ret = create_vulkan_image(width,
|
||||
status = create_vulkan_image(width,
|
||||
height,
|
||||
VK_FORMAT_R8G8B8A8_SRGB,
|
||||
pixel_buffer);
|
||||
pixel_buffer, texture_id);
|
||||
|
||||
stbi_image_free(pixel_buffer);
|
||||
|
||||
return ret;
|
||||
return status;
|
||||
|
||||
}
|
||||
|
||||
@@ -440,7 +443,7 @@ void rse_destroy_textures()
|
||||
vmaDestroyImage(g_vulkan_state.allocator, g_depth_image.image, g_depth_image.allocation);
|
||||
}
|
||||
|
||||
int8_t image_exists(uint8_t image_id) {
|
||||
uint8_t image_exists(uint8_t image_id) {
|
||||
return g_texture_images[image_id].id_taken == IMAGE_TAKEN;
|
||||
}
|
||||
|
||||
@@ -474,16 +477,20 @@ size_t get_textures_count()
|
||||
return count;
|
||||
}
|
||||
|
||||
VkFormat find_depth_format()
|
||||
rse_err_t find_depth_format(VkFormat* found_format)
|
||||
{
|
||||
#define formats_count 3
|
||||
rse_err_t status;
|
||||
VkFormat formats[formats_count] = {VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT};
|
||||
|
||||
return find_supported_format(
|
||||
STATUS_CHECK(find_supported_format(
|
||||
formats,
|
||||
formats_count,
|
||||
VK_IMAGE_TILING_OPTIMAL,
|
||||
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
|
||||
);
|
||||
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT,
|
||||
found_format
|
||||
));
|
||||
|
||||
return status;
|
||||
#undef formats_count
|
||||
}
|
||||
Reference in new issue
Block a user