From a5a82709028c475d0ff6cd54f6d07f16375e156e Mon Sep 17 00:00:00 2001 From: gramanas Date: Fri, 31 May 2024 21:00:57 +0300 Subject: Allaboard --- src/vksetup.h | 172 +++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 129 insertions(+), 43 deletions(-) (limited to 'src/vksetup.h') diff --git a/src/vksetup.h b/src/vksetup.h index 2feb533..88f579a 100644 --- a/src/vksetup.h +++ b/src/vksetup.h @@ -15,6 +15,10 @@ - Using shaderc for compiling glsl - Using vulkan(!) + This is not meant to be a generic werapper around vulkan. It is actively + paired and chaned by the currently in development application, whatever that + might be. Think of it as the vulkan setup configuratior and helper. + USAGE: ~~~~~~ Do this: @@ -32,60 +36,141 @@ #define SDL_MAIN_HANDLED #define VK_USE_PLATFORM_XCB_KHR +#include + #include #include #include +#include // for string_VkResult #include #include "../lib/cglm/include/cglm/cglm.h" +#define VK_ARRAY_LEN(arr) sizeof((arr))/sizeof((arr)[0]) + +#ifdef __clang__ +#define __FILENAME__ __FILE_NAME__ +#else +#define __FILENAME__ __FILE__ +#endif + +#define VK_CHECK(x) \ +do { \ + VkResult err = x; \ + if (err < 0) { \ + fprintf(stderr, "src/%s:%d:0: vulkan error: %s \n", \ + __FILENAME__, __LINE__, string_VkResult(err)); \ + abort(); \ + } \ +} while (0) + +typedef enum { + VK_INFO = 0, + VK_WARN, + VK_ERROR, +} log_type; + +static inline void +_vk_log(log_type t, const char * f, ...) +{ +#ifdef VKDEBUG + va_list args; + va_start(args, f); + switch (t) { + case VK_INFO: + printf("INFO: "); + vprintf(f, args); + break; + case VK_WARN: + fprintf(stderr, "WARN: "); + vfprintf(stderr, f, args); + break; + case VK_ERROR: + fprintf(stderr, "ERROR: "); + vfprintf(stderr, f, args); + break; + } + va_end(args); +#else + return; +#endif +} + +#ifdef VKDEBUG +#define vk_log(t, ...) do { \ + fprintf(t == VK_INFO ? stdout : stderr, "src/%s:%d:0: ", \ + __FILENAME__, __LINE__); \ + _vk_log(t, __VA_ARGS__); \ +} while(0) +#else +#define vk_log(t, ...) +#endif + +/**********/ +/* config */ +/**********/ + +const char *const validation_layers[] = { + "VK_LAYER_KHRONOS_validation" +}; +const uint32_t validation_layer_count = VK_ARRAY_LEN(validation_layers); + +const char *const device_extensions[] = { + VK_KHR_SWAPCHAIN_EXTENSION_NAME, +}; +const uint32_t deviceExtensionCount = VK_ARRAY_LEN(device_extensions); + +#ifdef VKDEBUG + const bool enableValidationLayers = true; +#else + const bool enableValidationLayers = false; +#endif + + #ifdef __cplusplus extern "C" { #endif -#ifndef VKSETUPDEF -#ifdef VKSETUP_STATIC -#define VKSETUPDEF static +#ifndef VKSDEF +#ifdef VKS_STATIC +#define VKSDEF static #else -#define VKSETUPDEF extern +#define VKSDEF extern #endif #endif // TODO Create structs for vulkan data -typedef struct {void * a;} vksetup_vulkan; -typedef struct {void * a;} vksetup_image; -typedef struct {void * a;} vksetup_; - -// need abstraction with the - +typedef struct {void * a;} vks_vulkan; +typedef struct {void * a;} vks_image; +typedef struct {void * a;} vks_; /** Create a VkInstance */ -VKSETUPDEF VkInstance vksetup_create_instance(bool validation_layers_toggle, const char * const validation_layers[], uint32_t validation_layer_count, SDL_Window *window); -/* VKSETUPDEF void vulkan_create_surface(); */ -/* VKSETUPDEF void vulkan_pick_physical_device(); */ -/* VKSETUPDEF void vulkan_create_logical_device(); */ -/* VKSETUPDEF void vulkan_create_swap_chain(); */ -/* VKSETUPDEF void vulkan_create_image_views(); */ -/* VKSETUPDEF void vulkan_create_descriptor_set_layout(); */ -/* VKSETUPDEF void vulkan_create_graphics_pipeline(); */ -/* VKSETUPDEF void vulkan_create_command_pool(); */ -/* VKSETUPDEF void vulkan_create_depth_resources(); */ -/* VKSETUPDEF void vulkan_create_texture_image(); */ -/* VKSETUPDEF void vulkan_create_texture_image_view(); */ -/* VKSETUPDEF void vulkan_create_texture_sampler(); */ -/* VKSETUPDEF void vulkan_create_vertex_buffer(); */ -/* VKSETUPDEF void vulkan_create_index_buffer(); */ -/* VKSETUPDEF void vulkan_create_uniform_buffers(); */ -/* VKSETUPDEF void vulkan_create_descriptor_pool(); */ -/* VKSETUPDEF void vulkan_create_descriptor_sets(); */ -/* VKSETUPDEF void vulkan_create_command_buffer(); */ -/* VKSETUPDEF void vulkan_create_sync_objects(); */ +VKSDEF VkInstance vks_create_instance(bool validation_layers_toggle, const char * const validation_layers[], uint32_t validation_layer_count, SDL_Window *window); +/* VKSDEF void vulkan_create_surface(); */ +/* VKSDEF void vulkan_pick_physical_device(); */ +/* VKSDEF void vulkan_create_logical_device(); */ +/* VKSDEF void vulkan_create_swap_chain(); */ +/* VKSDEF void vulkan_create_image_views(); */ +/* VKSDEF void vulkan_create_descriptor_set_layout(); */ +/* VKSDEF void vulkan_create_graphics_pipeline(); */ +/* VKSDEF void vulkan_create_command_pool(); */ +/* VKSDEF void vulkan_create_depth_resources(); */ +/* VKSDEF void vulkan_create_texture_image(); */ +/* VKSDEF void vulkan_create_texture_image_view(); */ +/* VKSDEF void vulkan_create_texture_sampler(); */ +/* VKSDEF void vulkan_create_vertex_buffer(); */ +/* VKSDEF void vulkan_create_index_buffer(); */ +/* VKSDEF void vulkan_create_uniform_buffers(); */ +/* VKSDEF void vulkan_create_descriptor_pool(); */ +/* VKSDEF void vulkan_create_descriptor_sets(); */ +/* VKSDEF void vulkan_create_command_buffer(); */ +/* VKSDEF void vulkan_create_sync_objects(); */ #ifdef __cplusplus } @@ -99,10 +184,11 @@ VKSETUPDEF VkInstance vksetup_create_instance(bool validation_layers_toggle, con #include #include #include -#include "vkutil.h" + +/* Vks helpers */ bool -_checkValidationLayerSupport(const char * const validation_layers[], uint32_t validation_layer_count) +vks_check_validation_layer_support(const char * const validation_layers[], uint32_t validation_layer_count) { uint32_t layerCount; vkEnumerateInstanceLayerProperties(&layerCount, NULL); @@ -110,29 +196,28 @@ _checkValidationLayerSupport(const char * const validation_layers[], uint32_t va VkLayerProperties availableLayers[layerCount]; vkEnumerateInstanceLayerProperties(&layerCount, availableLayers); + bool layerFound = false; for (uint32_t i = 0; i < validation_layer_count; i++) { - bool layerFound = false; - for (uint32_t j = 0; j < layerCount; j++) { if (strcmp(validation_layers[i], availableLayers[j].layerName) == 0) { layerFound = true; break; } } - - if (!layerFound) { - return false; - } } - return true; + return layerFound; } -VKSETUPDEF VkInstance -vksetup_create_instance(bool validation_layers_toggle, const char * const validation_layers[], uint32_t validation_layer_count, SDL_Window *window) +/* Vks API implementation */ + +VKSDEF VkInstance +vks_create_instance(bool validation_layers_toggle, const char * const validation_layers[], + uint32_t validation_layer_count, SDL_Window *window) { - if (validation_layers_toggle && !_checkValidationLayerSupport(validation_layers, validation_layer_count)) { + if (validation_layers_toggle && !vks_check_validation_layer_support(validation_layers, validation_layer_count)) { vk_log(VK_ERROR, "validation layers requested, but not available!\n"); + abort(); } uint32_t instanceVersion; @@ -166,6 +251,7 @@ vksetup_create_instance(bool validation_layers_toggle, const char * const valida if (SDL_Vulkan_GetInstanceExtensions(window, &sdlExtensionCount, NULL) == SDL_FALSE) { vk_log(VK_ERROR, "SDL_Vulkan_GetInstanceExtensions failed: %s\n", SDL_GetError()); + abort(); } // make space for debug extenetion @@ -177,6 +263,7 @@ vksetup_create_instance(bool validation_layers_toggle, const char * const valida if (SDL_Vulkan_GetInstanceExtensions(window, &sdlExtensionCount, sdlExtensions) == SDL_FALSE) { vk_log(VK_ERROR, "SDL_Vulkan_GetInstanceExtensions failed: %s\n", SDL_GetError()); + abort(); } // add debug extenetion @@ -213,4 +300,3 @@ vksetup_create_instance(bool validation_layers_toggle, const char * const valida } #endif /* VKSETUP_IMPLEMENTATION */ - -- cgit v1.2.3