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
+149
-152
@@ -11,23 +11,26 @@
|
||||
|
||||
#include "vulkan_base.h"
|
||||
|
||||
#include "vulkan_errors.h"
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-function"
|
||||
#pragma GCC diagnostic ignored "-Wunused-variable"
|
||||
#pragma GCC diagnostic ignored "-Wpedantic"
|
||||
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
|
||||
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
||||
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
|
||||
#pragma GCC diagnostic ignored "-Wswitch"
|
||||
#pragma GCC diagnostic ignored "-Wparentheses"
|
||||
/* We are building with -Werror flag on, but there are some warning in VMA implementation. Thus diagnostic push */
|
||||
// #pragma GCC diagnostic push
|
||||
// #pragma GCC diagnostic ignored "-Wunused-function"
|
||||
// #pragma GCC diagnostic ignored "-Wunused-variable"
|
||||
// #pragma GCC diagnostic ignored "-Wpedantic"
|
||||
// #pragma GCC diagnostic ignored "-Wignored-qualifiers"
|
||||
// #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
||||
// #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
|
||||
// #pragma GCC diagnostic ignored "-Wswitch"
|
||||
// #pragma GCC diagnostic ignored "-Wparentheses"
|
||||
#include "vk_mem_alloc.h"
|
||||
#pragma GCC diagnostic pop
|
||||
// #pragma GCC diagnostic pop
|
||||
|
||||
#include "locale_vulkan.h"
|
||||
#include "utilities/locale_common.h"
|
||||
#include "utilities/rse_errors_common.h"
|
||||
#include "utilities/logger.h"
|
||||
#include "vulkan_errors.h"
|
||||
#include "window.h"
|
||||
|
||||
#include "vulkan_commons.h"
|
||||
@@ -73,60 +76,7 @@ VkSemaphore g_render_finished_semaphores[SWAP_BUFFER_COUNT] = {};
|
||||
VkFence g_in_flight_fences[SWAP_BUFFER_COUNT] = {};
|
||||
|
||||
|
||||
|
||||
#ifndef NDEBUG
|
||||
static void setup_debug_messenger();
|
||||
static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
|
||||
VkDebugUtilsMessageTypeFlagsEXT message_type,
|
||||
const VkDebugUtilsMessengerCallbackDataEXT* callback_data,
|
||||
void* user_data);
|
||||
|
||||
static VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* create_info,
|
||||
const VkAllocationCallbacks* allocator,
|
||||
VkDebugUtilsMessengerEXT* debug_messenger);
|
||||
|
||||
static void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debug_messenger,
|
||||
const VkAllocationCallbacks* allocator);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
static void set_enabled_extension();
|
||||
static uint8_t create_instance();
|
||||
static uint8_t create_surface();
|
||||
static uint8_t pick_physical_device();
|
||||
static uint8_t create_device();
|
||||
static uint8_t create_memory_allocator();
|
||||
|
||||
static uint8_t create_sync_objects();
|
||||
|
||||
|
||||
|
||||
#ifndef NDEBUG
|
||||
/**
|
||||
* @brief Setup debugging system for Vulkan API
|
||||
*
|
||||
*/
|
||||
void setup_debug_messenger()
|
||||
{
|
||||
VkDebugUtilsMessengerCreateInfoEXT create_info;
|
||||
|
||||
create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
||||
create_info.pNext = NULL;
|
||||
create_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
|
||||
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
|
||||
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
||||
create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
|
||||
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
|
||||
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
||||
create_info.pfnUserCallback = debug_callback;
|
||||
create_info.pUserData = NULL; // Optional
|
||||
create_info.flags = 0U;
|
||||
|
||||
if (CreateDebugUtilsMessengerEXT(g_vulkan_instance, &create_info, NULL, &g_debug_messenger) != VK_SUCCESS) {
|
||||
fprintf(stderr, "failed to set up debug messenger!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Calback for debug messenger
|
||||
@@ -137,15 +87,29 @@ void setup_debug_messenger()
|
||||
* @param user_data Data provided by the user
|
||||
* @return VkBool32 Should always return VK_FALSE. The VK_TRUE value is reserved for use in layer development
|
||||
*/
|
||||
VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
|
||||
static VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
|
||||
VkDebugUtilsMessageTypeFlagsEXT message_type,
|
||||
const VkDebugUtilsMessengerCallbackDataEXT* callback_data, void* user_data)
|
||||
{
|
||||
enum log_level_t log_level = LOGLEVEL_TRACE;
|
||||
(void)message_severity;
|
||||
(void)message_type;
|
||||
(void)user_data;
|
||||
|
||||
LOGD(callback_data->pMessage);
|
||||
/* There is no guarantee, that the error message will have single severity flag set, so we can't use switch()
|
||||
* here. We have to check flags and set severity to most important one */
|
||||
|
||||
if (message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
|
||||
log_level = LOGLEVEL_ERROR;
|
||||
} else if (message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
|
||||
log_level = LOGLEVEL_WARNING;
|
||||
} else if (message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
|
||||
log_level = LOGLEVEL_INFO;
|
||||
} else if (message_severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) {
|
||||
log_level = LOGLEVEL_TRACE;
|
||||
}
|
||||
|
||||
LOGGER(log_level, callback_data->pMessage);
|
||||
|
||||
return VK_FALSE;
|
||||
}
|
||||
@@ -159,7 +123,7 @@ VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback(VkDebugUtilsMessageSeverityFlagBit
|
||||
* @param debug_messenger Debug messanger handle
|
||||
* @return VkResult VK_SUCCESS on success
|
||||
*/
|
||||
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* create_info,
|
||||
static VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* create_info,
|
||||
const VkAllocationCallbacks* allocator,
|
||||
VkDebugUtilsMessengerEXT* debug_messenger)
|
||||
{
|
||||
@@ -175,6 +139,34 @@ VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMes
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Setup debugging system for Vulkan API
|
||||
*
|
||||
*/
|
||||
static rse_err_t setup_debug_messenger()
|
||||
{
|
||||
VkDebugUtilsMessengerCreateInfoEXT create_info = {0};
|
||||
|
||||
create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
|
||||
create_info.pNext = NULL;
|
||||
create_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
|
||||
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
|
||||
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
||||
create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
|
||||
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
|
||||
VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
||||
create_info.pfnUserCallback = debug_callback;
|
||||
create_info.pUserData = NULL; // Optional
|
||||
create_info.flags = 0U;
|
||||
|
||||
if (CreateDebugUtilsMessengerEXT(g_vulkan_instance, &create_info, NULL, &g_debug_messenger) != VK_SUCCESS) {
|
||||
LOGF(vulkan_messages[VULKAN_DEBUG_MESSENGER_SETUP_FAILED]);
|
||||
return VULKAN_ERROR_DEBUG_MESSENGER_CREATION_FAILED;
|
||||
}
|
||||
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Proxy for vkDestroyDebugUtilsMessengerEXT
|
||||
*
|
||||
@@ -182,7 +174,7 @@ VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMes
|
||||
* @param debug_messenger Debug messanger handle
|
||||
* @param allocator Memory allocator
|
||||
*/
|
||||
void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debug_messenger,
|
||||
static void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debug_messenger,
|
||||
const VkAllocationCallbacks* allocator)
|
||||
{
|
||||
PFN_vkDestroyDebugUtilsMessengerEXT func =
|
||||
@@ -192,13 +184,13 @@ void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif /* DEBUG */
|
||||
|
||||
/**
|
||||
* @brief Enable instance extensions, based on system and requirements
|
||||
*
|
||||
*/
|
||||
void set_enabled_extension()
|
||||
static void set_enabled_extension()
|
||||
{
|
||||
g_enabled_instance_extensions_names[0] = "VK_KHR_surface";
|
||||
g_enabled_instance_extensions_names[1] = "VK_KHR_device_group_creation";
|
||||
@@ -210,49 +202,54 @@ void set_enabled_extension()
|
||||
g_enabled_instance_extensions_names[2] = "VK_KHR_xcb_surface";
|
||||
}
|
||||
#elif defined(_WIN32) || defined(WIN32)
|
||||
g_enabled_instance_extensions_names.push_back("VK_KHR_win32_surface");
|
||||
g_enabled_instance_extensions_names[2] = "VK_KHR_win32_surface";
|
||||
#endif
|
||||
|
||||
/* Add custom extensions here and make VK_EXT_debug_utils last */
|
||||
|
||||
/* ----------------------------------------------------------- */
|
||||
|
||||
#ifndef NDEBUG
|
||||
g_enabled_instance_extensions_names[3] = "VK_EXT_debug_utils";
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Create the Vulkan Instance object
|
||||
*
|
||||
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
|
||||
* @return rse_err_t RSE_ERROR_NO_ERROR on success. Possible errors:
|
||||
* VULKAN_ERROR_INSTANCE_INIT_FAILED
|
||||
*/
|
||||
uint8_t create_instance()
|
||||
static rse_err_t create_instance()
|
||||
{
|
||||
size_t i;
|
||||
size_t j;
|
||||
uint8_t supported = 0;
|
||||
uint32_t extensionCount = 0;
|
||||
uint32_t layersCount = 0;
|
||||
VkApplicationInfo application_info = {};
|
||||
VkInstanceCreateInfo create_info = {};
|
||||
size_t i = 0U;
|
||||
size_t j = 0U;
|
||||
uint8_t supported = 0U;
|
||||
uint32_t extensionCount = 0U;
|
||||
uint32_t layersCount = 0U;
|
||||
VkApplicationInfo application_info = {0};
|
||||
VkInstanceCreateInfo create_info = {0};
|
||||
VkLayerProperties* layer_properties;
|
||||
VkExtensionProperties* extensions_properties;
|
||||
|
||||
vkEnumerateInstanceLayerProperties(&layersCount, NULL);
|
||||
layer_properties = malloc(sizeof(VkLayerProperties) * layersCount);
|
||||
rse_malloc(layer_properties, sizeof(VkLayerProperties) * layersCount);
|
||||
vkEnumerateInstanceLayerProperties(&layersCount, layer_properties);
|
||||
|
||||
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, NULL);
|
||||
extensions_properties = malloc(sizeof(VkExtensionProperties) * extensionCount);
|
||||
rse_malloc(extensions_properties, sizeof(VkExtensionProperties) * extensionCount);
|
||||
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, extensions_properties);
|
||||
|
||||
#ifndef NDEBUG
|
||||
LOGD("Supported Layers");
|
||||
LOGD("Supported Layers: \n");
|
||||
for (i = 0; i < layersCount; i++) {
|
||||
LOGD("%s", layer_properties[i].layerName);
|
||||
LOGD("\t-%s\n", layer_properties[i].layerName);
|
||||
}
|
||||
|
||||
LOGD("Supported Extensions");
|
||||
LOGD("Supported Extensions: \n");
|
||||
for (size_t i = 0; i < extensionCount; i++) {
|
||||
LOGD("%s", extensions_properties[i].extensionName);
|
||||
LOGD("\t-%s\n", extensions_properties[i].extensionName);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -264,14 +261,13 @@ uint8_t create_instance()
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(supported == 0) {
|
||||
LOGF(vulkan_messages[VULKAN_LAYER_NOT_SUPPORTED], layer_properties[j].layerName);
|
||||
return VULKAN_ERROR_LAYER_NOT_SUPPORTED;
|
||||
}
|
||||
supported = 0;
|
||||
}
|
||||
|
||||
if(supported == 0) {
|
||||
LOGF(vulkan_error_messages[VULKAN_LAYER_NOT_SUPPORTED], layer_properties[j].layerName);
|
||||
return VULKAN_ERROR_LAYER_NOT_SUPPORTED;
|
||||
}
|
||||
supported = 0;
|
||||
|
||||
/* Check if selected extensions are supported*/
|
||||
for (i = 0; i < ENABLED_EXTENSIONS_COUT; ++i) {
|
||||
for(j = 0; j < extensionCount; ++j) {
|
||||
@@ -280,10 +276,11 @@ uint8_t create_instance()
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(supported == 0) {
|
||||
LOGF(vulkan_error_messages[VULKAN_EXTENSION_NOT_SUPPORTED], extensions_properties[j].extensionName);
|
||||
return VULKAN_ERROR_EXTENSION_NOT_SUPPORTED;
|
||||
if(supported == 0) {
|
||||
LOGF(vulkan_messages[VULKAN_EXTENSION_NOT_SUPPORTED], extensions_properties[j].extensionName);
|
||||
return VULKAN_ERROR_EXTENSION_NOT_SUPPORTED;
|
||||
}
|
||||
supported = 0;
|
||||
}
|
||||
|
||||
application_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
||||
@@ -309,36 +306,35 @@ uint8_t create_instance()
|
||||
|
||||
VkResult result = vkCreateInstance(&create_info, NULL, &g_vulkan_instance);
|
||||
if (VK_SUCCESS != result) {
|
||||
LOGF(vulkan_error_messages[VULKAN_INTANCE_INIT_FAILED]);
|
||||
LOGF(vulkan_messages[VULKAN_INTANCE_INIT_FAILED]);
|
||||
return VULKAN_ERROR_INSTANCE_INIT_FAILED;
|
||||
}
|
||||
|
||||
return VULKAN_ERROR_NO_ERROR;
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create a Surface, connection between Vulkan and actual window
|
||||
*
|
||||
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
|
||||
* @return rse_err_t RSE_ERROR_NO_ERROR on success. Possible errors:
|
||||
*/
|
||||
uint8_t create_surface()
|
||||
static rse_err_t create_surface()
|
||||
{
|
||||
if (VK_SUCCESS != glfwCreateWindowSurface(g_vulkan_instance, get_window_handle(), NULL, &g_vulkan_state.surface)) {
|
||||
LOGF(vulkan_error_messages[VULKAN_SURFACE_CREATION_FAILED]);
|
||||
LOGF(vulkan_messages[VULKAN_SURFACE_CREATION_FAILED]);
|
||||
return VULKAN_ERROR_SURFACE_CREATION_FAILED;
|
||||
}
|
||||
|
||||
return VULKAN_ERROR_NO_ERROR;
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Choose physical device. Used later for vkDevice
|
||||
*
|
||||
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
|
||||
* @return rse_err_t RSE_ERROR_NO_ERROR on success. Possible errors:
|
||||
* VULKAN_ERROR_NO_PHYSICAL_DEVICE_FOUND
|
||||
* VULKAN_ERROR_NO_SUITABLE_PHYSICAL_DEVICE_FOUND
|
||||
*/
|
||||
uint8_t pick_physical_device()
|
||||
static rse_err_t pick_physical_device()
|
||||
{
|
||||
uint32_t physical_device_count = 0U;
|
||||
VkPhysicalDevice* physical_devices = NULL;
|
||||
@@ -349,11 +345,12 @@ uint8_t pick_physical_device()
|
||||
vkEnumeratePhysicalDevices(g_vulkan_instance, &physical_device_count, NULL);
|
||||
|
||||
if (0 == physical_device_count) {
|
||||
LOGF(vulkan_error_messages[VULKAN_ERROR_NO_PHYSICAL_DEVICE_FOUND]);
|
||||
LOGF(vulkan_messages[VULKAN_ERROR_NO_PHYSICAL_DEVICE_FOUND]);
|
||||
return VULKAN_ERROR_NO_PHYSICAL_DEVICE_FOUND;
|
||||
}
|
||||
|
||||
physical_devices = malloc(sizeof(VkPhysicalDevice) * physical_device_count);
|
||||
rse_malloc(physical_devices, sizeof(VkPhysicalDevice) * physical_device_count);
|
||||
|
||||
vkEnumeratePhysicalDevices(g_vulkan_instance, &physical_device_count, physical_devices);
|
||||
|
||||
/* Check for suitability */
|
||||
@@ -373,35 +370,36 @@ uint8_t pick_physical_device()
|
||||
|
||||
if (VK_NULL_HANDLE == g_vulkan_state.physical_device) {
|
||||
/* No suitable device found */
|
||||
LOGF(vulkan_error_messages[VULKAN_NO_SUITABLE_PHYSICAL_DEVICES]);
|
||||
LOGF(vulkan_messages[VULKAN_NO_SUITABLE_PHYSICAL_DEVICES]);
|
||||
return VULKAN_ERROR_NO_SUITABLE_PHYSICAL_DEVICE_FOUND;
|
||||
}
|
||||
|
||||
return VULKAN_ERROR_NO_ERROR;
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create vkDevice object
|
||||
*
|
||||
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
|
||||
* @return rse_err_t RSE_ERROR_NO_ERROR on success. Possible errors:
|
||||
* VULKAN_ERROR_QUEUE_NOT_SUPPORTED
|
||||
* VULKAN_ERROR_DEVICE_CREATION_FAILED
|
||||
*/
|
||||
uint8_t create_device()
|
||||
static rse_err_t create_device()
|
||||
{
|
||||
size_t device_queues_count = 0;
|
||||
uint32_t queue_families_property_count = 0U;
|
||||
ssize_t graphics_family_idx = -1;
|
||||
ssize_t presentation_familiy_idx = -1;
|
||||
VkQueueFamilyProperties* queue_family_properties = NULL;
|
||||
VkDeviceCreateInfo device_create_info = {};
|
||||
VkDeviceQueueCreateInfo device_queue_createinfos[2] = {}; /* TODO: Change when we want more queues */
|
||||
VkDeviceCreateInfo device_create_info = {0};
|
||||
VkDeviceQueueCreateInfo device_queue_createinfos[2] = {0};
|
||||
float* graphics_familiy_queue_priorities = NULL;
|
||||
float* presentation_familiy_queue_priorities = NULL;
|
||||
VkPhysicalDeviceFeatures2 physical_features2 = {0};
|
||||
VkPhysicalDeviceDescriptorIndexingFeatures indexing_features = {0};
|
||||
VkBool32 bindless_supported = VK_FALSE;
|
||||
|
||||
/* Enable descriptori indexing for bindless textures */
|
||||
indexing_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT;
|
||||
|
||||
physical_features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
|
||||
@@ -411,13 +409,13 @@ uint8_t create_device()
|
||||
|
||||
bindless_supported = indexing_features.descriptorBindingPartiallyBound && indexing_features.runtimeDescriptorArray;
|
||||
if (bindless_supported == VK_FALSE) {
|
||||
LOGE("This engine requires bindless support");
|
||||
return 1;
|
||||
LOGF(vulkan_messages[VULKAN_BINDLESS_DESIGN_NOT_SUPPORTED]);
|
||||
return VULKAN_ERROR_MISSING_FEATURES_FOR_BINDLESS_DESIGN;
|
||||
}
|
||||
|
||||
/* Get information about supported queue families */
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(g_vulkan_state.physical_device, &queue_families_property_count, NULL);
|
||||
queue_family_properties = malloc(sizeof(VkQueueFamilyProperties) * queue_families_property_count);
|
||||
rse_malloc(queue_family_properties, sizeof(VkQueueFamilyProperties) * queue_families_property_count);
|
||||
vkGetPhysicalDeviceQueueFamilyProperties(g_vulkan_state.physical_device, &queue_families_property_count,
|
||||
queue_family_properties);
|
||||
|
||||
@@ -442,11 +440,9 @@ uint8_t create_device()
|
||||
if (presentSupport) {
|
||||
presentation_familiy_idx = familyIdx;
|
||||
/* If this is the same queue family as in graphics family, don't increase queue count */
|
||||
//TODO: This is still ugly, but fixes an error. Change later.
|
||||
if (presentation_familiy_idx != graphics_family_idx) {
|
||||
device_queues_count++;
|
||||
}
|
||||
LOGI("Setting present family idx to %d", familyIdx);
|
||||
}
|
||||
|
||||
if (graphics_family_idx > 0 && presentation_familiy_idx > 0) {
|
||||
@@ -457,21 +453,22 @@ uint8_t create_device()
|
||||
|
||||
/* This is not magic number ;P. We want to support exact number of required queues flags */
|
||||
if (0 > graphics_family_idx) {
|
||||
LOGF(vulkan_error_messages[VULKAN_QUEUE_NOT_SUPPORTED]);
|
||||
LOGF(vulkan_messages[VULKAN_QUEUE_NOT_SUPPORTED]);
|
||||
return VULKAN_ERROR_QUEUE_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
g_vulkan_state.queue_family_indices[0] = graphics_family_idx; /* TODO: Change when more families are needed */
|
||||
g_vulkan_state.queue_family_indices[0] = graphics_family_idx;
|
||||
|
||||
rse_malloc(graphics_familiy_queue_priorities, sizeof(float) * queue_family_properties[graphics_family_idx].queueCount);
|
||||
|
||||
graphics_familiy_queue_priorities = malloc(sizeof(float) * queue_family_properties[graphics_family_idx].queueCount);
|
||||
for (size_t i = 0; i < queue_family_properties[graphics_family_idx].queueCount; i++) {
|
||||
graphics_familiy_queue_priorities[i] = 1.0f; /* TODO: When queues have different tasks, change this if needed */
|
||||
graphics_familiy_queue_priorities[i] = 1.0f;
|
||||
}
|
||||
|
||||
presentation_familiy_queue_priorities = malloc(sizeof(float) * queue_family_properties[presentation_familiy_idx].queueCount);
|
||||
rse_malloc(presentation_familiy_queue_priorities, sizeof(float) * queue_family_properties[presentation_familiy_idx].queueCount);
|
||||
for (size_t i = 0; i < queue_family_properties[presentation_familiy_idx].queueCount; i++) {
|
||||
presentation_familiy_queue_priorities[i] =
|
||||
1.0f; /* TODO: When queues have different tasks, change this if needed */
|
||||
1.0f;
|
||||
}
|
||||
|
||||
device_queue_createinfos[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
||||
@@ -497,27 +494,26 @@ uint8_t create_device()
|
||||
device_create_info.ppEnabledLayerNames = NULL;
|
||||
device_create_info.enabledExtensionCount = ENABLED_DEVICE_EXTENSIONS_COUT;
|
||||
device_create_info.ppEnabledExtensionNames = g_enabled_device_extensions_names;
|
||||
device_create_info.pEnabledFeatures = NULL; /* TODO: Enable features, when needed */
|
||||
|
||||
device_create_info.pEnabledFeatures = NULL;
|
||||
|
||||
if (VK_SUCCESS != vkCreateDevice(g_vulkan_state.physical_device, &device_create_info, NULL, &g_vulkan_state.device)) {
|
||||
LOGF(vulkan_error_messages[VULKAN_FAILED_TO_CREATE_DEVICE]);
|
||||
LOGF(vulkan_messages[VULKAN_FAILED_TO_CREATE_DEVICE]);
|
||||
return VULKAN_ERROR_DEVICE_CREATION_FAILED;
|
||||
}
|
||||
|
||||
vkGetDeviceQueue(g_vulkan_state.device, graphics_family_idx, 0, &g_vulkan_state.graphics_queue);
|
||||
vkGetDeviceQueue(g_vulkan_state.device, presentation_familiy_idx, 0, &g_present_queue);
|
||||
|
||||
return VULKAN_ERROR_NO_ERROR;
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Create a Memory Allocator object
|
||||
*
|
||||
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
|
||||
* @return rse_err_t RSE_ERROR_NO_ERROR on success. Possible errors:
|
||||
* VULKAN_ERROR_ALLOCATOR_CREATION_FAILED
|
||||
*/
|
||||
uint8_t create_memory_allocator()
|
||||
static rse_err_t create_memory_allocator()
|
||||
{
|
||||
VmaVulkanFunctions vulkan_functions = {0};
|
||||
vulkan_functions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr;
|
||||
@@ -531,21 +527,21 @@ uint8_t create_memory_allocator()
|
||||
allocator_create_info.pVulkanFunctions = &vulkan_functions;
|
||||
|
||||
if (VK_SUCCESS != vmaCreateAllocator(&allocator_create_info, &g_vulkan_state.allocator)) {
|
||||
LOGF(vulkan_error_messages[VULKAN_ERROR_ALLOCATOR_CREATION_FAILED]);
|
||||
LOGF(vulkan_messages[VULKAN_ERROR_ALLOCATOR_CREATION_FAILED]);
|
||||
return VULKAN_ERROR_ALLOCATOR_CREATION_FAILED;
|
||||
}
|
||||
|
||||
return VULKAN_ERROR_NO_ERROR;
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Create a Sync Objects
|
||||
*
|
||||
* @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors:
|
||||
* @return rse_err_t RSE_ERROR_NO_ERROR on success. Possible errors:
|
||||
* VULKAN_ERROR_SYNCOBJCTS_CREATION_FAILED
|
||||
*/
|
||||
uint8_t create_sync_objects()
|
||||
static rse_err_t create_sync_objects()
|
||||
{
|
||||
VkSemaphoreCreateInfo semaphore_info = {};
|
||||
semaphore_info.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
||||
@@ -558,23 +554,23 @@ uint8_t create_sync_objects()
|
||||
if (VK_SUCCESS != vkCreateSemaphore(g_vulkan_state.device, &semaphore_info, NULL, &g_image_available_semaphores[i]) ||
|
||||
VK_SUCCESS != vkCreateSemaphore(g_vulkan_state.device, &semaphore_info, NULL, &g_render_finished_semaphores[i]) ||
|
||||
VK_SUCCESS != vkCreateFence(g_vulkan_state.device, &fenceInfo, NULL, &g_in_flight_fences[i])) {
|
||||
LOGF(vulkan_error_messages[VULKAN_SYNCOBJCTS_CREATION_FAILED]);
|
||||
LOGF(vulkan_messages[VULKAN_SYNCOBJCTS_CREATION_FAILED]);
|
||||
return VULKAN_ERROR_SYNCOBJCTS_CREATION_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
return VULKAN_ERROR_NO_ERROR;
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
|
||||
uint8_t preinit_vulkan()
|
||||
rse_err_t preinit_vulkan()
|
||||
{
|
||||
uint8_t status = VULKAN_ERROR_NO_ERROR;
|
||||
rse_err_t status = RSE_ERROR_NO_ERROR;
|
||||
|
||||
set_enabled_extension();
|
||||
STATUS_CHECK(create_instance());
|
||||
#ifndef NDEBUG
|
||||
setup_debug_messenger();
|
||||
STATUS_CHECK(setup_debug_messenger());
|
||||
#endif
|
||||
STATUS_CHECK(create_surface());
|
||||
STATUS_CHECK(pick_physical_device());
|
||||
@@ -587,27 +583,28 @@ uint8_t preinit_vulkan()
|
||||
return status;
|
||||
}
|
||||
|
||||
uint8_t init_vulkan()
|
||||
rse_err_t init_vulkan()
|
||||
{
|
||||
uint8_t status = VULKAN_ERROR_NO_ERROR;
|
||||
rse_err_t status = RSE_ERROR_NO_ERROR;
|
||||
|
||||
STATUS_CHECK(create_pipeline());
|
||||
STATUS_CHECK(create_descriptors());
|
||||
STATUS_CHECK(create_sync_objects());
|
||||
|
||||
rse_init_time();
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
void draw_frame()
|
||||
rse_err_t draw_frame()
|
||||
{
|
||||
uint32_t image_index = 0U;
|
||||
VkResult result = VK_FALSE;
|
||||
VkSubmitInfo submit_info = {};
|
||||
VkSemaphore wait_semaphores[1]; //TODO: Increase when needed
|
||||
VkSubmitInfo submit_info = {0};
|
||||
VkSemaphore wait_semaphores[1];
|
||||
VkPipelineStageFlags wait_stages[1];
|
||||
VkSemaphore signal_semaphores[1];
|
||||
VkPresentInfoKHR present_info = {};
|
||||
VkPresentInfoKHR present_info = {0};
|
||||
VkSwapchainKHR swap_chains[1];
|
||||
|
||||
vkWaitForFences(g_vulkan_state.device, 1, &g_in_flight_fences[g_vulkan_state.current_frame], VK_TRUE, UINT64_MAX);
|
||||
@@ -619,10 +616,10 @@ void draw_frame()
|
||||
|
||||
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) {
|
||||
recreate_swapchain();
|
||||
return;
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
} else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
|
||||
// throw std::runtime_error("failed to acquire swap chain image!");
|
||||
return;
|
||||
LOGF(vulkan_messages[VULKAN_ACQUIRE_SWAPCHAIN_FAILED]);
|
||||
return VULKAN_ERROR_ACQUIRE_SWAPCHAIN_FAILED;
|
||||
}
|
||||
|
||||
update_uniform_buffers();
|
||||
@@ -633,7 +630,6 @@ void draw_frame()
|
||||
|
||||
record_command_buffer(image_index);
|
||||
|
||||
|
||||
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
|
||||
wait_semaphores[0] = g_image_available_semaphores[g_vulkan_state.current_frame];
|
||||
@@ -650,11 +646,10 @@ void draw_frame()
|
||||
submit_info.pSignalSemaphores = signal_semaphores;
|
||||
|
||||
if (vkQueueSubmit(g_vulkan_state.graphics_queue, 1, &submit_info, g_in_flight_fences[g_vulkan_state.current_frame]) != VK_SUCCESS) {
|
||||
LOGF(vulkan_error_messages[VULAKN_DRAW_FAILED]);
|
||||
return;
|
||||
LOGF(vulkan_messages[VULAKN_DRAW_FAILED]);
|
||||
return VULKAN_ERROR_DRAW_FAILED;
|
||||
}
|
||||
|
||||
|
||||
present_info.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
|
||||
|
||||
present_info.waitSemaphoreCount = 1;
|
||||
@@ -669,6 +664,8 @@ void draw_frame()
|
||||
vkQueuePresentKHR(g_present_queue, &present_info);
|
||||
|
||||
g_vulkan_state.current_frame = (g_vulkan_state.current_frame + 1) % SWAP_BUFFER_COUNT;
|
||||
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
void deinit_vulkan()
|
||||
|
||||
Reference in new issue
Block a user