From 1f6538bcaf379cb3fe257a0053e4e7567f8ddb98 Mon Sep 17 00:00:00 2001 From: gramanas Date: Sun, 26 May 2024 23:28:28 +0300 Subject: Begin vksetup.h --- src/render.c | 144 +++++++++-------------------------------------------------- 1 file changed, 20 insertions(+), 124 deletions(-) (limited to 'src/render.c') 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); -- cgit v1.2.3