diff options
Diffstat (limited to 'render/vulkan/context.c')
-rw-r--r-- | render/vulkan/context.c | 113 |
1 files changed, 81 insertions, 32 deletions
diff --git a/render/vulkan/context.c b/render/vulkan/context.c index eaf4cf3..5d78dd4 100644 --- a/render/vulkan/context.c +++ b/render/vulkan/context.c @@ -1,7 +1,29 @@ +/* + * Rune Game Engine + * Copyright 2024 Danny Holman <dholman@gymli.org> + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ + #include "context.h" #include "vkassert.h" #include <rune/core/logging.h> #include <rune/core/alloc.h> +#include <rune/core/config.h> #include <stdlib.h> #include <string.h> @@ -29,18 +51,28 @@ VKAPI_ATTR VkBool32 VKAPI_CALL _vulkan_db_callback( return VK_FALSE; } -int _init_vkdebugger(struct vkcontext *context) { +int _init_vkdebugger(vkcontext_t *context) { log_output(LOG_INFO, "Initializing Vulkan debugger"); - uint32_t log_severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; - VkDebugUtilsMessengerCreateInfoEXT dbinfo = {VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT}; - dbinfo.messageSeverity = log_severity; + + VkDebugUtilsMessengerCreateInfoEXT dbinfo; + dbinfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; + dbinfo.pNext = NULL; + dbinfo.flags = 0; + dbinfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | + VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | + VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT | + VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT; dbinfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT; dbinfo.pfnUserCallback = _vulkan_db_callback; dbinfo.pUserData = NULL; - PFN_vkCreateDebugUtilsMessengerEXT func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(context->instance, "vkCreateDebugUtilsMessengerEXT"); + + PFN_vkCreateDebugUtilsMessengerEXT func = + (PFN_vkCreateDebugUtilsMessengerEXT) + vkGetInstanceProcAddr(context->instance, + "vkCreateDebugUtilsMessengerEXT"); + if (func == NULL) { log_output(LOG_ERROR, "Failed to create Vulkan debugger"); return -1; @@ -50,63 +82,80 @@ int _init_vkdebugger(struct vkcontext *context) { res = func(context->instance, &dbinfo, NULL, &context->db_messenger); if (res != VK_SUCCESS) { char *err_str = get_vkerr_str(res); - log_output(LOG_ERROR, "Cannot create a Vulkan debug session: %s", err_str); + log_output(LOG_ERROR, "Debug session error: %s", err_str); free(err_str); return -1; } return 0; } -struct vkcontext* create_vkcontext(struct vklayer_container *vklayers, struct ext_container *ext) { - log_output(LOG_DEBUG, "Initializing Vulkan"); - VkApplicationInfo app_info = {VK_STRUCTURE_TYPE_APPLICATION_INFO}; +vkcontext_t* create_vkcontext(vklayer_container_t *vklayers, ext_container_t *ext) { + vkcontext_t *ret = rune_calloc(0, sizeof(vkcontext_t)); + ret->surface = rune_alloc(sizeof(vksurface_t)); + + VkApplicationInfo app_info; + app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; + app_info.pNext = NULL; app_info.apiVersion = VK_API_VERSION_1_2; - app_info.pApplicationName = "RuneClient"; - app_info.applicationVersion = VK_MAKE_VERSION(1, 0, 0); + app_info.pApplicationName = rune_get_app_name(); + int *app_ver = rune_get_app_ver(); + app_info.applicationVersion = VK_MAKE_VERSION(app_ver[0], + app_ver[1], + app_ver[2]); app_info.pEngineName = "RuneEngine"; - app_info.engineVersion = VK_MAKE_VERSION(1, 0, 0); + app_info.engineVersion = VK_MAKE_VERSION(RUNE_VER_MAJOR, + RUNE_VER_MINOR, + RUNE_VER_PATCH); - VkInstanceCreateInfo cinfo = {VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO}; + VkInstanceCreateInfo cinfo; + cinfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + cinfo.pNext = NULL; + cinfo.flags = 0; cinfo.pApplicationInfo = &app_info; cinfo.enabledExtensionCount = ext->ext_count; cinfo.ppEnabledExtensionNames = ext->extensions; cinfo.pNext = NULL; - struct vkcontext *ret = rune_calloc(0, sizeof(struct vkcontext)); - ret->surface = rune_alloc(sizeof(struct vksurface)); - VkResult res; - res = vkCreateInstance(&cinfo, NULL, &ret->instance); - if (res != VK_SUCCESS) { - char *err_str = get_vkerr_str(res); - log_output(LOG_FATAL, "Cannot create a Vulkan instance: %s", err_str); - free(err_str); - rune_free(ret); - ret = NULL; - } - if (vklayers != NULL) { log_output(LOG_INFO, "Validation layers enabled"); - _init_vkdebugger(ret); cinfo.enabledLayerCount = vklayers->vklayer_count; cinfo.ppEnabledLayerNames = vklayers->vklayer_names; } else { log_output(LOG_INFO, "Validation layers disabled"); + cinfo.enabledLayerCount = 0; + cinfo.ppEnabledLayerNames = NULL; + } + + VkResult res; + res = vkCreateInstance(&cinfo, NULL, &ret->instance); + if (res != VK_SUCCESS) { + char *err_str = get_vkerr_str(res); + log_output(LOG_FATAL, "Cannot create a Vulkan instance: %s", err_str); + rune_abort(); } + + if (vklayers != NULL) + _init_vkdebugger(ret); return ret; } -void destroy_vkcontext(struct vkcontext *context) { +void destroy_vkcontext(vkcontext_t *context) { log_output(LOG_DEBUG, "Closing Vulkan instance"); - PFN_vkDestroyDebugUtilsMessengerEXT func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(context->instance, "vkDestroyDebugUtilsMessengerEXT"); - func(context->instance, context->db_messenger, NULL); + if (rune_get_vk_debug() == 1) { + PFN_vkDestroyDebugUtilsMessengerEXT func = + (PFN_vkDestroyDebugUtilsMessengerEXT) + vkGetInstanceProcAddr(context->instance, + "vkDestroyDebugUtilsMessengerEXT"); + func(context->instance, context->db_messenger, NULL); + } vkDestroySurfaceKHR(context->instance, context->surface->handle, NULL); vkDestroyInstance(context->instance, NULL); rune_free(context->surface); rune_free(context); } -struct vklayer_container* init_vklayers(struct ext_container *ext) { +vklayer_container_t* init_vklayers(ext_container_t *ext) { const char** new_extensions = rune_alloc(sizeof(char*) * ext->ext_count++); for (uint32_t i = 0; i < ext->ext_count-1; i++) new_extensions[i] = ext->extensions[i]; @@ -118,7 +167,7 @@ struct vklayer_container* init_vklayers(struct ext_container *ext) { VkLayerProperties layer_props[layer_count]; vkEnumerateInstanceLayerProperties(&layer_count, layer_props); - struct vklayer_container *ret = rune_alloc(sizeof(struct vklayer_container)); + vklayer_container_t *ret = rune_alloc(sizeof(vklayer_container_t)); ret->vklayer_count = 1; ret->vklayer_names = rune_alloc(sizeof(char*) * ret->vklayer_count); ret->vklayer_names[0] = "VK_LAYER_KHRONOS_validation"; |