diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2024-05-26 23:28:28 +0300 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2024-05-26 23:28:28 +0300 |
commit | 1f6538bcaf379cb3fe257a0053e4e7567f8ddb98 (patch) | |
tree | 24e7ec078b9618b596d6265ccfe45137ad37d497 | |
parent | d56a036fd406ca63b07888b2dbb91b56cad7e40c (diff) | |
download | cgame-1f6538bcaf379cb3fe257a0053e4e7567f8ddb98.tar.gz cgame-1f6538bcaf379cb3fe257a0053e4e7567f8ddb98.tar.bz2 cgame-1f6538bcaf379cb3fe257a0053e4e7567f8ddb98.zip |
Begin vksetup.h
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/render.c | 144 | ||||
-rw-r--r-- | src/shader.vert | 2 | ||||
-rw-r--r-- | src/vksetup.h | 216 |
4 files changed, 236 insertions, 127 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index b7e2182..30ca37f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,6 @@ bin_PROGRAMS = game common_sources = state.h - AM_LDFLAGS = -fuse-ld=lld -flto # -fwhole-program allows cross-file inlining, but only works when you put all diff --git a/src/render.c b/src/render.c index 32ffb7c..b6383c4 100644 --- a/src/render.c +++ b/src/render.c @@ -19,8 +19,8 @@ #define STB_IMAGE_IMPLEMENTATION #include "../lib/stb_image.h" -/* #define CIMGUI_DEFINE_ENUMS_AND_STRUCTS */ -/* #include "../lib/cimgui/cimgui.h" */ +#define VKSETUP_IMPLEMENTATION +#include "vksetup.h" //#include "cplusplus.h" #include "vkutil.h" @@ -174,33 +174,6 @@ closeSDL() SDL_Quit(); } -bool -checkValidationLayerSupport() -{ - uint32_t layerCount; - vkEnumerateInstanceLayerProperties(&layerCount, NULL); - - VkLayerProperties availableLayers[layerCount]; - vkEnumerateInstanceLayerProperties(&layerCount, availableLayers); - - 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; -} - static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT messageType, @@ -235,86 +208,6 @@ vulkan_setup_debug_messenger() } } -void -vulkan_create_instance() -{ - if (enableValidationLayers && !checkValidationLayerSupport()) { - vk_log(VK_ERROR, "validation layers requested, but not available!\n"); - } - - uint32_t instanceVersion; - VkResult result = vkEnumerateInstanceVersion(&instanceVersion); - if (result == VK_SUCCESS) { - if (instanceVersion < VK_MAKE_API_VERSION(0, 1, 3, 0)) { - vk_log(VK_ERROR, "Vulkan version 1.3 or greater required!\n"); - exit(1); - } - vk_log(VK_INFO, "Vulkan version found (%d) %d.%d.%d\n", - VK_API_VERSION_VARIANT(instanceVersion), - VK_API_VERSION_MAJOR(instanceVersion), - VK_API_VERSION_MINOR(instanceVersion), - VK_API_VERSION_PATCH(instanceVersion)); - } else { - vk_log(VK_ERROR, "Failed to retrieve vulkan version, is vulkan supported in this system?\n"); - exit(1); - } - - // Load Vulkan and create instance - VkApplicationInfo appInfo = { - .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, - .pApplicationName = "Vulkan Application", - .applicationVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), - .pEngineName = NULL, - .engineVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), - .apiVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), - }; - - uint32_t sdlExtensionCount = 0; - - if (SDL_Vulkan_GetInstanceExtensions(s.sdl_window, &sdlExtensionCount, NULL) == SDL_FALSE) { - vk_log(VK_ERROR, "SDL_Vulkan_GetInstanceExtensions failed: %s\n", SDL_GetError()); - } - - // make space for debug extenetion - if (enableValidationLayers) { - sdlExtensionCount++; - } - - const char* sdlExtensions[sdlExtensionCount]; - - if (SDL_Vulkan_GetInstanceExtensions(s.sdl_window, &sdlExtensionCount, sdlExtensions) == SDL_FALSE) { - vk_log(VK_ERROR, "SDL_Vulkan_GetInstanceExtensions failed: %s\n", SDL_GetError()); - } - - // add debug extenetion - if (enableValidationLayers) { - sdlExtensions[sdlExtensionCount] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME; - } - - vk_log(VK_INFO, "The sdl extensions:\n"); - for (uint32_t i = 0; i < sdlExtensionCount; i++) { - vk_log(VK_INFO, "\t%s\n", sdlExtensions[i]); - } - - VkInstanceCreateInfo createInfo = { - .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, - .pApplicationInfo = &appInfo, - .enabledExtensionCount = sdlExtensionCount, - .ppEnabledExtensionNames = sdlExtensions, - .enabledLayerCount = 0, - }; - - if (enableValidationLayers) { - createInfo.enabledLayerCount = validation_layer_count; - createInfo.ppEnabledLayerNames = validation_layers; - } - - if (vkCreateInstance(&createInfo, NULL, &s.vk_instance) != VK_SUCCESS) { - vk_log(VK_ERROR, "Can't start vulkan instance\n"); - } - vk_log(VK_INFO, "Vulkan instance created\n"); -} - VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR * capabilities) { @@ -546,7 +439,7 @@ vulkan_create_logical_device() VkPhysicalDeviceFeatures deviceFeatures = {0}; vkGetPhysicalDeviceFeatures(s.vk_physical_device, &deviceFeatures); deviceFeatures.samplerAnisotropy = VK_TRUE; -#ifndef NDEBUG +#ifndef VKDEBUG /* Disable robust buffer access when building without debug */ deviceFeatures.robustBufferAccess = VK_FALSE; #endif @@ -592,21 +485,20 @@ void move_relative(vec3 position, vec3 front, float step, int x) { glm_vec3_cross(GLM_ZUP, direction_vec, right); glm_normalize(right); - // Calculate the up vector - vec3 up; - glm_vec3_cross(right, direction_vec, up); - glm_normalize(up); - // Move front according to the specified direction - if (x) { + if (x) { front[0] += right[0] * step; front[1] += right[1] * step; front[2] += right[2] * step; - } else { + } else { + // Calculate the up vector + vec3 up; + glm_vec3_cross(right, direction_vec, up); + glm_normalize(up); front[0] += up[0] * step; front[1] += up[1] * step; front[2] += up[2] * step; - } + } } void @@ -615,8 +507,6 @@ update_camera(float xoffset, float yoffset) s.camera.yaw += xoffset; s.camera.pitch += yoffset; - vk_log(VK_WARN, "yaw: %f pitch: %f\n", s.camera.yaw, s.camera.pitch); - // Make sure that when pitch is out of bounds, the screen doesn't get flipped /* if (s.camera.pitch > 89.0f) */ /* s.camera.pitch = 89.0f; */ @@ -1194,7 +1084,7 @@ vulkan_create_graphics_pipeline(VkPolygonMode polygon_mode) pipelineInfo.stageCount = 2; pipelineInfo.pStages = shaderStages; - pipelineInfo.pNext = &pipeline_rendering_create_info, + pipelineInfo.pNext = &pipeline_rendering_create_info; pipelineInfo.pVertexInputState = &vertexInputInfo; pipelineInfo.pInputAssemblyState = &inputAssembly; pipelineInfo.pViewportState = &viewportState; @@ -1231,6 +1121,7 @@ vulkan_create_command_pool() VkCommandPoolCreateInfo poolInfo = {0}; poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; + // AMD doesn't like this flag for some reason poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; poolInfo.queueFamilyIndex = queueFamilyIndices.graphicsFamily; @@ -1811,7 +1702,10 @@ init_vulkan() vk_log(VK_WARN, " DEBUG ON \n"); vk_log(VK_WARN, "====================================\n"); - vulkan_create_instance(); + //vulkan_create_instance(); + s.vk_instance = + vksetup_create_instance(enableValidationLayers, validation_layers, + validation_layer_count, s.sdl_window); // vulkan_setup_debug_messenger(); vulkan_create_surface(); vulkan_pick_physical_device(); @@ -1916,7 +1810,7 @@ updateUniformBuffer(uint32_t currentImage) glm_mat4_identity(ubo.model); if (!s.rotate) - glm_rotate(ubo.model, glm_rad(5 * time * glm_rad(90.0f)), GLM_ZUP); + glm_rotate(ubo.model, glm_rad(50 * time * glm_rad(90.0f)), GLM_ZUP); vec3 eye = GLM_VEC3_ONE_INIT; vec3 center = GLM_VEC3_ZERO_INIT; @@ -1928,7 +1822,7 @@ updateUniformBuffer(uint32_t currentImage) /* glm_lookat(eye, center, GLM_ZUP, ubo.view); */ float aspect = s.vk_swap_chain_extent.width / (float)s.vk_swap_chain_extent.height; - glm_perspective(glm_rad(45.0f ), aspect, 0.1f, 100.0f, ubo.proj); + glm_perspective(glm_rad(45.0f + s.zoom ), aspect, 0.1f, 100.0f, ubo.proj); // Inverting the Y axis for Vulkan ubo.proj[1][1] *= -1; @@ -1954,6 +1848,8 @@ draw_frame() { vkResetFences(s.vk_device, 1, &s.frames[currentFrame].in_flight_fence); + // both could work + //vkResetCommandPool(s.vk_device, s.vk_command_pool, VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT); vkResetCommandBuffer(s.frames[currentFrame].vk_command_buffer, 0); recordCommandBuffer(s.frames[currentFrame].vk_command_buffer, imageIndex); diff --git a/src/shader.vert b/src/shader.vert index 6c050ac..dd50683 100644 --- a/src/shader.vert +++ b/src/shader.vert @@ -22,5 +22,3 @@ void main() { fragColor = inColor; fragTexCoord = inTexCoord; } - - diff --git a/src/vksetup.h b/src/vksetup.h new file mode 100644 index 0000000..2feb533 --- /dev/null +++ b/src/vksetup.h @@ -0,0 +1,216 @@ +#ifndef _VKSETUP_H +#define _VKSETUP_H +/* Start header file */ + +/** + vulkan setup and basic vector math + + Single header file with included implementation in the spirit of + stb_* <https://github.com/nothings/stb> + + ASSUMPTIONS: + ~~~~~~~~~~~~ + - Using SDL2 for the window + - Using cglm for maths + - Using shaderc for compiling glsl + - Using vulkan(!) + + USAGE: + ~~~~~~ + Do this: + #define VKSETUP_IMPLEMENTATION + before you include this file in *one* C or C++ file to create the implementation. + + // i.e. it should look like this: + #include ... + #include ... + #include ... + #define VKSETUP_IMPLEMENTATION + #include "vksetup.h" + + */ + +#define SDL_MAIN_HANDLED +#define VK_USE_PLATFORM_XCB_KHR +#include <SDL2/SDL.h> +#include <SDL2/SDL_vulkan.h> + +#include <vulkan/vulkan.h> + +#include <shaderc/shaderc.h> + +#include "../lib/cglm/include/cglm/cglm.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef VKSETUPDEF +#ifdef VKSETUP_STATIC +#define VKSETUPDEF static +#else +#define VKSETUPDEF 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 + + +/** + 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(); */ + +#ifdef __cplusplus +} +#endif + +/* End header file */ +#endif /* _VKSETUP_H */ + +#ifdef VKSETUP_IMPLEMENTATION + +#include <stddef.h> +#include <stdlib.h> +#include <time.h> +#include "vkutil.h" + +bool +_checkValidationLayerSupport(const char * const validation_layers[], uint32_t validation_layer_count) +{ + uint32_t layerCount; + vkEnumerateInstanceLayerProperties(&layerCount, NULL); + + VkLayerProperties availableLayers[layerCount]; + vkEnumerateInstanceLayerProperties(&layerCount, availableLayers); + + 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; +} + +VKSETUPDEF VkInstance +vksetup_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)) { + vk_log(VK_ERROR, "validation layers requested, but not available!\n"); + } + + uint32_t instanceVersion; + VkResult result = vkEnumerateInstanceVersion(&instanceVersion); + if (result == VK_SUCCESS) { + if (instanceVersion < VK_MAKE_API_VERSION(0, 1, 3, 0)) { + vk_log(VK_ERROR, "Vulkan version 1.3 or greater required!\n"); + exit(1); + } + vk_log(VK_INFO, "Vulkan version found (%d) %d.%d.%d\n", + VK_API_VERSION_VARIANT(instanceVersion), + VK_API_VERSION_MAJOR(instanceVersion), + VK_API_VERSION_MINOR(instanceVersion), + VK_API_VERSION_PATCH(instanceVersion)); + } else { + vk_log(VK_ERROR, "Failed to retrieve vulkan version, is vulkan supported in this system?\n"); + exit(1); + } + + // Load Vulkan and create instance + VkApplicationInfo appInfo = { + .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, + .pApplicationName = "Vulkan Application", + .applicationVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), + .pEngineName = NULL, + .engineVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), + .apiVersion = VK_MAKE_API_VERSION(0, 1, 3, 0), + }; + + uint32_t sdlExtensionCount = 0; + + if (SDL_Vulkan_GetInstanceExtensions(window, &sdlExtensionCount, NULL) == SDL_FALSE) { + vk_log(VK_ERROR, "SDL_Vulkan_GetInstanceExtensions failed: %s\n", SDL_GetError()); + } + + // make space for debug extenetion + if (validation_layers_toggle) { + sdlExtensionCount++; + } + + const char* sdlExtensions[sdlExtensionCount]; + + if (SDL_Vulkan_GetInstanceExtensions(window, &sdlExtensionCount, sdlExtensions) == SDL_FALSE) { + vk_log(VK_ERROR, "SDL_Vulkan_GetInstanceExtensions failed: %s\n", SDL_GetError()); + } + + // add debug extenetion + if (validation_layers_toggle) { + sdlExtensions[sdlExtensionCount] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME; + } + + vk_log(VK_INFO, "The sdl extensions:\n"); + for (uint32_t i = 0; i < sdlExtensionCount; i++) { + vk_log(VK_INFO, "\t%s\n", sdlExtensions[i]); + } + + VkInstanceCreateInfo createInfo = { + .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, + .pApplicationInfo = &appInfo, + .enabledExtensionCount = sdlExtensionCount, + .ppEnabledExtensionNames = sdlExtensions, + .enabledLayerCount = 0, + }; + + if (validation_layers_toggle) { + createInfo.enabledLayerCount = validation_layer_count; + createInfo.ppEnabledLayerNames = validation_layers; + } + + VkInstance instance; + + if (vkCreateInstance(&createInfo, NULL, &instance) != VK_SUCCESS) { + vk_log(VK_ERROR, "Can't start vulkan instance\n"); + } + vk_log(VK_INFO, "Vulkan instance created\n"); + + return instance; +} + +#endif /* VKSETUP_IMPLEMENTATION */ + |