From 6781c1cff144afb4773de0e1a13a85cd48a9b5d1 Mon Sep 17 00:00:00 2001 From: Piotr Krygier Date: Tue, 4 Apr 2023 15:27:42 +0200 Subject: [PATCH] Remove VulkanState dependency Passing VulkanState everywhere (including windowLoop) made no sense. I know that "globals are evil", but this is the best solution for now. --- graphics/CMakeLists.txt | 1 + graphics/src/mesh_controller.cpp | 10 +- graphics/src/mesh_controller.hpp | 6 +- graphics/src/rse_graphics.cpp | 10 +- graphics/src/vulkan_base.cpp | 120 ++++++++++---------- graphics/src/vulkan_base.hpp | 6 +- graphics/src/vulkan_buffers.cpp | 167 +++++++++++++++------------- graphics/src/vulkan_buffers.hpp | 18 ++- graphics/src/vulkan_commons.cpp | 7 ++ graphics/src/vulkan_commons.hpp | 3 + graphics/src/vulkan_descriptors.cpp | 34 +++--- graphics/src/vulkan_descriptors.hpp | 4 +- graphics/src/vulkan_pipeline.cpp | 156 +++++++++++++------------- graphics/src/vulkan_pipeline.hpp | 8 +- graphics/src/window.cpp | 4 +- graphics/src/window.hpp | 3 +- 16 files changed, 285 insertions(+), 272 deletions(-) create mode 100644 graphics/src/vulkan_commons.cpp diff --git a/graphics/CMakeLists.txt b/graphics/CMakeLists.txt index 0757aaca..c7897ec5 100644 --- a/graphics/CMakeLists.txt +++ b/graphics/CMakeLists.txt @@ -52,6 +52,7 @@ target_sources(rse_graphics PRIVATE src/vulkan_descriptors.cpp src/rse_graphics.cpp src/mesh_controller.cpp + src/vulkan_commons.cpp ) target_link_libraries(rse_graphics PRIVATE diff --git a/graphics/src/mesh_controller.cpp b/graphics/src/mesh_controller.cpp index f05948c1..5492f3ce 100644 --- a/graphics/src/mesh_controller.cpp +++ b/graphics/src/mesh_controller.cpp @@ -14,8 +14,7 @@ std::queue free_ids; std::map> instance_free_ids; -uint16_t create_mesh(rse::graphics::vulkanbase::VulkanState& vulkan_state, - std::vector vertices, +uint16_t create_mesh(std::vector vertices, std::vector indices) { static uint16_t last_id = 0U; @@ -34,13 +33,12 @@ uint16_t create_mesh(rse::graphics::vulkanbase::VulkanState& vulkan_state, indices )); - rse::graphics::vulkanbase::update_mesh_buffers(vulkan_state, vertices, indices); + rse::graphics::vulkanbase::update_mesh_buffers(vertices, indices); return assignable_id; } -void create_mesh_instance(rse::graphics::vulkanbase::VulkanState& vulkan_state, - uint16_t mesh_id, +void create_mesh_instance(uint16_t mesh_id, InstanceData instance_data) { uint16_t instance_id = 0U; @@ -59,7 +57,7 @@ void create_mesh_instance(rse::graphics::vulkanbase::VulkanState& vulkan_state, instance_data ); - rse::graphics::vulkanbase::update_mesh_instances(vulkan_state, instance_data); + rse::graphics::vulkanbase::update_mesh_instances(instance_data); } //TODO: Replace with something else. It will take to much time to do this each time we draw frame for all the objects diff --git a/graphics/src/mesh_controller.hpp b/graphics/src/mesh_controller.hpp index 847b29ef..60a4bf21 100644 --- a/graphics/src/mesh_controller.hpp +++ b/graphics/src/mesh_controller.hpp @@ -47,8 +47,7 @@ struct Mesh * @param indices indices for mesh * @return uint16_t Mesh identifier. Can be useful for getting vertices, indices and instances */ -uint16_t create_mesh(rse::graphics::vulkanbase::VulkanState& vulkan_state, - std::vector vertices, +uint16_t create_mesh(std::vector vertices, std::vector indices); /** @@ -57,8 +56,7 @@ uint16_t create_mesh(rse::graphics::vulkanbase::VulkanState& vulkan_state, * @param mesh_id Mesh id * @param instance_data Transformation info */ -void create_mesh_instance(rse::graphics::vulkanbase::VulkanState& vulkan_state, - uint16_t mesh_id, +void create_mesh_instance(uint16_t mesh_id, InstanceData instance_data); std::vector get_mesh_vertices(uint16_t mesh_id); diff --git a/graphics/src/rse_graphics.cpp b/graphics/src/rse_graphics.cpp index 4b25e605..3f9413a4 100644 --- a/graphics/src/rse_graphics.cpp +++ b/graphics/src/rse_graphics.cpp @@ -36,7 +36,7 @@ uint8_t RseGraphics::graphics_run(void) - if (0 != vulkanbase::initVulkan(vulkan_state)) + if (0 != vulkanbase::initVulkan()) return -1; // FIXME: Temporary array of vertices, for testing purposes @@ -48,8 +48,8 @@ uint8_t RseGraphics::graphics_run(void) // FIXME: Temporary array of vertices, for testing purposes std::vector indices = {0, 1, 2, 2, 3, 0}; - uint16_t mesh_id = rse::graphics::mesh::create_mesh(vulkan_state, vertices, indices); - rse::graphics::mesh::create_mesh_instance(vulkan_state, mesh_id, rse::graphics::mesh::InstanceData( + uint16_t mesh_id = rse::graphics::mesh::create_mesh(vertices, indices); + rse::graphics::mesh::create_mesh_instance(mesh_id, rse::graphics::mesh::InstanceData( glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, glm::radians(45.0f)), 1.0f @@ -61,9 +61,9 @@ uint8_t RseGraphics::graphics_run(void) // 1.0f // )); - window::windowLoop(vulkan_state); + window::windowLoop(); - vulkanbase::deinitVulkan(vulkan_state); + vulkanbase::deinitVulkan(); window::windowTerminate(); diff --git a/graphics/src/vulkan_base.cpp b/graphics/src/vulkan_base.cpp index 8f21a227..3bb15983 100644 --- a/graphics/src/vulkan_base.cpp +++ b/graphics/src/vulkan_base.cpp @@ -90,12 +90,12 @@ static void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMesse static void setEnabledExtension(); static uint8_t createInstance(); -static uint8_t createSurface(VulkanState& vulkan_state); -static uint8_t pickPhysicalDevice(VulkanState& vulkan_state); -static uint8_t createDevice(VulkanState& vulkan_state); -static uint8_t createMemoryAllocator(VulkanState& vulkan_state); +static uint8_t createSurface(); +static uint8_t pickPhysicalDevice(); +static uint8_t createDevice(); +static uint8_t createMemoryAllocator(); -static uint8_t createSyncObjects(VulkanState& vulkan_state); +static uint8_t createSyncObjects(); @@ -306,9 +306,9 @@ uint8_t createInstance() * * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: */ -uint8_t createSurface(VulkanState& vulkan_state) +uint8_t createSurface() { - if (VK_SUCCESS != glfwCreateWindowSurface(gVulkanInstance, window::getWindowHandle(), nullptr, &vulkan_state.surface)) { + if (VK_SUCCESS != glfwCreateWindowSurface(gVulkanInstance, window::getWindowHandle(), nullptr, &g_vulkan_state.surface)) { LOGF << vulkanErrorMesssages[VULKAN_SURFACE_CREATION_FAILED]; return VULKAN_ERROR_SURFACE_CREATION_FAILED; } @@ -323,7 +323,7 @@ uint8_t createSurface(VulkanState& vulkan_state) * VULKAN_ERROR_NO_PHYSICAL_DEVICE_FOUND * VULKAN_ERROR_NO_SUITABLE_PHYSICAL_DEVICE_FOUND */ -uint8_t pickPhysicalDevice(VulkanState& vulkan_state) +uint8_t pickPhysicalDevice() { uint32_t physicalDeviceCount = 0U; std::vector pPhysicalDevices; @@ -351,12 +351,12 @@ uint8_t pickPhysicalDevice(VulkanState& vulkan_state) deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU) { /* Application MUST have geometry shader */ if (deviceFeatures.geometryShader) { - vulkan_state.physicalDevice = pPhysicalDevices[physicalDeviceIdx]; + g_vulkan_state.physicalDevice = pPhysicalDevices[physicalDeviceIdx]; } } } - if (VK_NULL_HANDLE == vulkan_state.physicalDevice) { + if (VK_NULL_HANDLE == g_vulkan_state.physicalDevice) { /* No suitable device found */ LOGF << vulkanErrorMesssages[VULKAN_NO_SUITABLE_PHYSICAL_DEVICES]; return VULKAN_ERROR_NO_SUITABLE_PHYSICAL_DEVICE_FOUND; @@ -372,7 +372,7 @@ uint8_t pickPhysicalDevice(VulkanState& vulkan_state) * VULKAN_ERROR_QUEUE_NOT_SUPPORTED * VULKAN_ERROR_DEVICE_CREATION_FAILED */ -uint8_t createDevice(VulkanState& vulkan_state) +uint8_t createDevice() { uint32_t queueFamiliesPropertyCount = 0U; ssize_t graphicsFamilyIdx = -1; @@ -384,9 +384,9 @@ uint8_t createDevice(VulkanState& vulkan_state) std::vector pPresentationFamiliyQueuePriorities; /* Get information about supported queue families */ - vkGetPhysicalDeviceQueueFamilyProperties(vulkan_state.physicalDevice, &queueFamiliesPropertyCount, nullptr); + vkGetPhysicalDeviceQueueFamilyProperties(g_vulkan_state.physicalDevice, &queueFamiliesPropertyCount, nullptr); pQueueFamilyProperties.resize(queueFamiliesPropertyCount); - vkGetPhysicalDeviceQueueFamilyProperties(vulkan_state.physicalDevice, &queueFamiliesPropertyCount, + vkGetPhysicalDeviceQueueFamilyProperties(g_vulkan_state.physicalDevice, &queueFamiliesPropertyCount, pQueueFamilyProperties.data()); /* Get queue families that support graphics operations AND have most @@ -402,7 +402,7 @@ uint8_t createDevice(VulkanState& vulkan_state) } /* Check for family with surface support */ VkBool32 presentSupport = false; - vkGetPhysicalDeviceSurfaceSupportKHR(vulkan_state.physicalDevice, familyIdx, vulkan_state.surface, &presentSupport); + vkGetPhysicalDeviceSurfaceSupportKHR(g_vulkan_state.physicalDevice, familyIdx, g_vulkan_state.surface, &presentSupport); if (presentSupport) { presentationFamiliyIdx = familyIdx; } @@ -419,7 +419,7 @@ uint8_t createDevice(VulkanState& vulkan_state) return VULKAN_ERROR_QUEUE_NOT_SUPPORTED; } - vulkan_state.queueFamilyIndices[0] = graphicsFamilyIdx; /* TODO: Change when more families are needed */ + g_vulkan_state.queueFamilyIndices[0] = graphicsFamilyIdx; /* TODO: Change when more families are needed */ pGraphicsFamiliyQueuePriorities.resize(pQueueFamilyProperties[graphicsFamilyIdx].queueCount); for (size_t i = 0; i < pQueueFamilyProperties[graphicsFamilyIdx].queueCount; i++) { @@ -457,13 +457,13 @@ uint8_t createDevice(VulkanState& vulkan_state) deviceCreateInfo.ppEnabledExtensionNames = gEnabledDeviceExtensionsNames.data(); deviceCreateInfo.pEnabledFeatures = nullptr; /* TODO: Enable features, when needed */ - if (VK_SUCCESS != vkCreateDevice(vulkan_state.physicalDevice, &deviceCreateInfo, nullptr, &vulkan_state.device)) { + if (VK_SUCCESS != vkCreateDevice(g_vulkan_state.physicalDevice, &deviceCreateInfo, nullptr, &g_vulkan_state.device)) { LOGF << vulkanErrorMesssages[VULKAN_FAILED_TO_CREATE_DEVICE]; return VULKAN_ERROR_DEVICE_CREATION_FAILED; } - vkGetDeviceQueue(vulkan_state.device, graphicsFamilyIdx, 0, &vulkan_state.graphicsQueue); - vkGetDeviceQueue(vulkan_state.device, presentationFamiliyIdx, 0, &gPresentQueue); + vkGetDeviceQueue(g_vulkan_state.device, graphicsFamilyIdx, 0, &g_vulkan_state.graphicsQueue); + vkGetDeviceQueue(g_vulkan_state.device, presentationFamiliyIdx, 0, &gPresentQueue); return VULKAN_ERROR_NO_ERROR; } @@ -474,7 +474,7 @@ uint8_t createDevice(VulkanState& vulkan_state) * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_ALLOCATOR_CREATION_FAILED */ -uint8_t createMemoryAllocator(VulkanState& vulkan_state) +uint8_t createMemoryAllocator() { VmaVulkanFunctions vulkanFunctions = {0}; vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr; @@ -482,12 +482,12 @@ uint8_t createMemoryAllocator(VulkanState& vulkan_state) VmaAllocatorCreateInfo allocatorCreateInfo = {0}; allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_3; - allocatorCreateInfo.physicalDevice = vulkan_state.physicalDevice; - allocatorCreateInfo.device = vulkan_state.device; + allocatorCreateInfo.physicalDevice = g_vulkan_state.physicalDevice; + allocatorCreateInfo.device = g_vulkan_state.device; allocatorCreateInfo.instance = gVulkanInstance; allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions; - if (VK_SUCCESS != vmaCreateAllocator(&allocatorCreateInfo, &vulkan_state.allocator)) { + if (VK_SUCCESS != vmaCreateAllocator(&allocatorCreateInfo, &g_vulkan_state.allocator)) { LOGF << vulkanErrorMesssages[VULKAN_ERROR_ALLOCATOR_CREATION_FAILED]; return VULKAN_ERROR_ALLOCATOR_CREATION_FAILED; } @@ -502,7 +502,7 @@ uint8_t createMemoryAllocator(VulkanState& vulkan_state) * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_SYNCOBJCTS_CREATION_FAILED */ -uint8_t createSyncObjects(VulkanState& vulkan_state) +uint8_t createSyncObjects() { gImageAvailableSemaphores.resize(SWAP_BUFFER_COUNT); gRenderFinishedSemaphores.resize(SWAP_BUFFER_COUNT); @@ -516,9 +516,9 @@ uint8_t createSyncObjects(VulkanState& vulkan_state) fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; for (size_t i = 0; i < SWAP_BUFFER_COUNT; ++i) { - if (VK_SUCCESS != vkCreateSemaphore(vulkan_state.device, &semaphoreInfo, nullptr, &gImageAvailableSemaphores[i]) || - VK_SUCCESS != vkCreateSemaphore(vulkan_state.device, &semaphoreInfo, nullptr, &gRenderFinishedSemaphores[i]) || - VK_SUCCESS != vkCreateFence(vulkan_state.device, &fenceInfo, nullptr, &gInFlightFences[i])) { + if (VK_SUCCESS != vkCreateSemaphore(g_vulkan_state.device, &semaphoreInfo, nullptr, &gImageAvailableSemaphores[i]) || + VK_SUCCESS != vkCreateSemaphore(g_vulkan_state.device, &semaphoreInfo, nullptr, &gRenderFinishedSemaphores[i]) || + VK_SUCCESS != vkCreateFence(g_vulkan_state.device, &fenceInfo, nullptr, &gInFlightFences[i])) { LOGF << vulkanErrorMesssages[VULKAN_SYNCOBJCTS_CREATION_FAILED]; return VULKAN_ERROR_SYNCOBJCTS_CREATION_FAILED; } @@ -528,7 +528,7 @@ uint8_t createSyncObjects(VulkanState& vulkan_state) } -uint8_t initVulkan(VulkanState& vulkan_state) +uint8_t initVulkan() { uint8_t status = VULKAN_ERROR_NO_ERROR; @@ -537,62 +537,62 @@ uint8_t initVulkan(VulkanState& vulkan_state) #ifndef NDEBUG setupDebugMessenger(); #endif - STATUS_CHECK(createSurface(vulkan_state)); - STATUS_CHECK(pickPhysicalDevice(vulkan_state)); - STATUS_CHECK(createDevice(vulkan_state)); - STATUS_CHECK(createMemoryAllocator(vulkan_state)); + STATUS_CHECK(createSurface()); + STATUS_CHECK(pickPhysicalDevice()); + STATUS_CHECK(createDevice()); + STATUS_CHECK(createMemoryAllocator()); - STATUS_CHECK(create_pipeline(vulkan_state)); + STATUS_CHECK(create_pipeline()); - STATUS_CHECK(create_buffers(vulkan_state)); + STATUS_CHECK(create_buffers()); - STATUS_CHECK(create_descriptors(vulkan_state)); + STATUS_CHECK(create_descriptors()); - STATUS_CHECK(createSyncObjects(vulkan_state)); + STATUS_CHECK(createSyncObjects()); return status; } -void drawFrame(VulkanState& vulkan_state) +void drawFrame() { - vkWaitForFences(vulkan_state.device, 1, &gInFlightFences[vulkan_state.currentFrame], VK_TRUE, UINT64_MAX); + vkWaitForFences(g_vulkan_state.device, 1, &gInFlightFences[g_vulkan_state.currentFrame], VK_TRUE, UINT64_MAX); uint32_t imageIndex; - VkResult result = vkAcquireNextImageKHR(vulkan_state.device, vulkan_state.swapchain, UINT64_MAX, gImageAvailableSemaphores[vulkan_state.currentFrame], + VkResult result = vkAcquireNextImageKHR(g_vulkan_state.device, g_vulkan_state.swapchain, UINT64_MAX, gImageAvailableSemaphores[g_vulkan_state.currentFrame], VK_NULL_HANDLE, &imageIndex); if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { - recreateSwapchain(vulkan_state); + recreateSwapchain(); return; } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) { // throw std::runtime_error("failed to acquire swap chain image!"); return; } - updateUniformBuffer(vulkan_state); + updateUniformBuffer(); /* Only reset the fence if we are submitting work */ - vkResetFences(vulkan_state.device, 1, &gInFlightFences[vulkan_state.currentFrame]); - reset_command_buffer(vulkan_state); + vkResetFences(g_vulkan_state.device, 1, &gInFlightFences[g_vulkan_state.currentFrame]); + reset_command_buffer(); - recordCommandBuffer(vulkan_state, imageIndex); + recordCommandBuffer(imageIndex); VkSubmitInfo submitInfo{}; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - VkSemaphore waitSemaphores[] = {gImageAvailableSemaphores[vulkan_state.currentFrame]}; + VkSemaphore waitSemaphores[] = {gImageAvailableSemaphores[g_vulkan_state.currentFrame]}; VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT}; submitInfo.waitSemaphoreCount = 1; submitInfo.pWaitSemaphores = waitSemaphores; submitInfo.pWaitDstStageMask = waitStages; submitInfo.commandBufferCount = 1; - submitInfo.pCommandBuffers = &vulkan_state.pCommandBuffers[vulkan_state.currentFrame]; + submitInfo.pCommandBuffers = &g_vulkan_state.pCommandBuffers[g_vulkan_state.currentFrame]; - VkSemaphore signalSemaphores[] = {gRenderFinishedSemaphores[vulkan_state.currentFrame]}; + VkSemaphore signalSemaphores[] = {gRenderFinishedSemaphores[g_vulkan_state.currentFrame]}; submitInfo.signalSemaphoreCount = 1; submitInfo.pSignalSemaphores = signalSemaphores; - if (vkQueueSubmit(vulkan_state.graphicsQueue, 1, &submitInfo, gInFlightFences[vulkan_state.currentFrame]) != VK_SUCCESS) { + if (vkQueueSubmit(g_vulkan_state.graphicsQueue, 1, &submitInfo, gInFlightFences[g_vulkan_state.currentFrame]) != VK_SUCCESS) { throw std::runtime_error("failed to submit draw command buffer!"); } @@ -602,7 +602,7 @@ void drawFrame(VulkanState& vulkan_state) presentInfo.waitSemaphoreCount = 1; presentInfo.pWaitSemaphores = signalSemaphores; - VkSwapchainKHR swapChains[] = {vulkan_state.swapchain}; + VkSwapchainKHR swapChains[] = {g_vulkan_state.swapchain}; presentInfo.swapchainCount = 1; presentInfo.pSwapchains = swapChains; @@ -610,28 +610,28 @@ void drawFrame(VulkanState& vulkan_state) vkQueuePresentKHR(gPresentQueue, &presentInfo); - vulkan_state.currentFrame = (vulkan_state.currentFrame + 1) % SWAP_BUFFER_COUNT; + g_vulkan_state.currentFrame = (g_vulkan_state.currentFrame + 1) % SWAP_BUFFER_COUNT; } -void deinitVulkan(VulkanState& vulkan_state) +void deinitVulkan() { - cleanup_swapchain(vulkan_state); - destroy_descriptors(vulkan_state); - destroy_buffers(vulkan_state); + cleanup_swapchain(); + destroy_descriptors(); + destroy_buffers(); for (size_t i = 0; i < SWAP_BUFFER_COUNT; ++i) { - vkDestroySemaphore(vulkan_state.device, gImageAvailableSemaphores[i], nullptr); - vkDestroySemaphore(vulkan_state.device, gRenderFinishedSemaphores[i], nullptr); - vkDestroyFence(vulkan_state.device, gInFlightFences[i], nullptr); + vkDestroySemaphore(g_vulkan_state.device, gImageAvailableSemaphores[i], nullptr); + vkDestroySemaphore(g_vulkan_state.device, gRenderFinishedSemaphores[i], nullptr); + vkDestroyFence(g_vulkan_state.device, gInFlightFences[i], nullptr); } - destroy_pipeline(vulkan_state); + destroy_pipeline(); - vmaDestroyAllocator(vulkan_state.allocator); - vkDestroyDevice(vulkan_state.device, nullptr); + vmaDestroyAllocator(g_vulkan_state.allocator); + vkDestroyDevice(g_vulkan_state.device, nullptr); #ifndef NDEBUG DestroyDebugUtilsMessengerEXT(gVulkanInstance, gDebugMessenger, nullptr); #endif - vkDestroySurfaceKHR(gVulkanInstance, vulkan_state.surface, nullptr); + vkDestroySurfaceKHR(gVulkanInstance, g_vulkan_state.surface, nullptr); vkDestroyInstance(gVulkanInstance, nullptr); } diff --git a/graphics/src/vulkan_base.hpp b/graphics/src/vulkan_base.hpp index d629649d..da3e867e 100644 --- a/graphics/src/vulkan_base.hpp +++ b/graphics/src/vulkan_base.hpp @@ -29,21 +29,21 @@ namespace rse::graphics::vulkanbase * @param state * @return uint8_t 0 on success. Status code on failure */ -uint8_t initVulkan(VulkanState& state); +uint8_t initVulkan(); /** * @brief Deinitialize Vulkan backend * * @param state */ -void deinitVulkan(VulkanState& state); +void deinitVulkan(); /** * @brief Draw frame on the screen. * * @param state */ -void drawFrame(VulkanState& state); +void drawFrame(); } /* namespace rse::graphics::vulkanbase */ diff --git a/graphics/src/vulkan_buffers.cpp b/graphics/src/vulkan_buffers.cpp index 5b2c34f7..78f8f9af 100644 --- a/graphics/src/vulkan_buffers.cpp +++ b/graphics/src/vulkan_buffers.cpp @@ -36,15 +36,21 @@ std::vector gInstanceBuffers = {}; VkCommandPool gCommandPool = VK_NULL_HANDLE; -static uint8_t createBuffer(VulkanState& vulkan_state, const VkDeviceSize size, VkBufferUsageFlags bufferUsage, VmaMemoryUsage memoryUsage, - const VmaAllocationCreateFlags allocationFlags, Buffer& buffer); -static uint8_t copyBuffer(VulkanState& vulkan_state, VkBuffer src, VkBuffer dst, VkDeviceSize size, VkDeviceSize dest_offset = 0); +static uint8_t createBuffer(const VkDeviceSize size, + VkBufferUsageFlags bufferUsage, + VmaMemoryUsage memoryUsage, + const VmaAllocationCreateFlags allocationFlags, + Buffer& buffer); +static uint8_t copyBuffer(VkBuffer src, + VkBuffer dst, + VkDeviceSize size, + VkDeviceSize dest_offset = 0); -static uint8_t createCommandPools(VulkanState& vulkan_state); -static uint8_t createVertexBuffer(VulkanState& vulkan_state); -static uint8_t createIndexBuffer(VulkanState& vulkan_state); -static uint8_t createUniformBuffers(VulkanState& vulkan_state); -static uint8_t allocateCommandBuffers(VulkanState& vulkan_state); +static uint8_t createCommandPools(); +static uint8_t createVertexBuffer(); +static uint8_t createIndexBuffer(); +static uint8_t createUniformBuffers(); +static uint8_t allocateCommandBuffers(); /** * @brief Helper function. Creates a buffer object @@ -57,7 +63,7 @@ static uint8_t allocateCommandBuffers(VulkanState& vulkan_state); * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED */ -uint8_t createBuffer(VulkanState& vulkan_state, const VkDeviceSize size, VkBufferUsageFlags bufferUsage, +uint8_t createBuffer(const VkDeviceSize size, VkBufferUsageFlags bufferUsage, VmaMemoryUsage memoryUsage, const VmaAllocationCreateFlags allocationFlags, Buffer& buffer) { VkBufferCreateInfo vertexBufferInfo; @@ -82,7 +88,11 @@ uint8_t createBuffer(VulkanState& vulkan_state, const VkDeviceSize size, VkBuffe createInfo.pUserData = VK_NULL_HANDLE; createInfo.priority = 0.0f; - if (VK_SUCCESS != vmaCreateBuffer(vulkan_state.allocator, &vertexBufferInfo, &createInfo, &buffer.buffer, &buffer.allocation, + if (VK_SUCCESS != vmaCreateBuffer(g_vulkan_state.allocator, + &vertexBufferInfo, + &createInfo, + &buffer.buffer, + &buffer.allocation, &buffer.allocationInfo)) { LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED]; return VULKAN_ERROR_BUFFER_CREATION_FAILED; @@ -99,7 +109,7 @@ uint8_t createBuffer(VulkanState& vulkan_state, const VkDeviceSize size, VkBuffe * @param size Size of buffer to copy * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: */ -uint8_t copyBuffer(VulkanState& vulkan_state, VkBuffer src, VkBuffer dst, VkDeviceSize size, VkDeviceSize dest_offset) +uint8_t copyBuffer(VkBuffer src, VkBuffer dst, VkDeviceSize size, VkDeviceSize dest_offset) { /* Vulkan buffers can only be copied using command buffers */ VkBufferCopy copyRegion; @@ -135,14 +145,14 @@ uint8_t copyBuffer(VulkanState& vulkan_state, VkBuffer src, VkBuffer dst, VkDevi // FIXME: At result checks VkResult status; - status = vkAllocateCommandBuffers(vulkan_state.device, &allocInfo, ©CommandBuffer); + status = vkAllocateCommandBuffers(g_vulkan_state.device, &allocInfo, ©CommandBuffer); status = vkBeginCommandBuffer(copyCommandBuffer, &beginInfo); vkCmdCopyBuffer(copyCommandBuffer, src, dst, 1, ©Region); status = vkEndCommandBuffer(copyCommandBuffer); - status = vkQueueSubmit(vulkan_state.graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); + status = vkQueueSubmit(g_vulkan_state.graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); /* TODO: Use fences to wait instead of idle */ - status = vkQueueWaitIdle(vulkan_state.graphicsQueue); - vkFreeCommandBuffers(vulkan_state.device, gCommandPool, 1, ©CommandBuffer); + status = vkQueueWaitIdle(g_vulkan_state.graphicsQueue); + vkFreeCommandBuffers(g_vulkan_state.device, gCommandPool, 1, ©CommandBuffer); if (status != VK_SUCCESS) { return VULKAN_ERROR_NO_ERROR; @@ -158,16 +168,16 @@ uint8_t copyBuffer(VulkanState& vulkan_state, VkBuffer src, VkBuffer dst, VkDevi * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_COMMAND_POOL_CREATION_FAILED */ -uint8_t createCommandPools(VulkanState& vulkan_state) +uint8_t createCommandPools() { VkCommandPoolCreateInfo createInfo; createInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; createInfo.pNext = nullptr; createInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; - createInfo.queueFamilyIndex = vulkan_state.queueFamilyIndices[0]; /* TODO: Change when more families are needed */ + createInfo.queueFamilyIndex = g_vulkan_state.queueFamilyIndices[0]; /* TODO: Change when more families are needed */ - if (VK_SUCCESS != vkCreateCommandPool(vulkan_state.device, &createInfo, nullptr, &gCommandPool)) { + if (VK_SUCCESS != vkCreateCommandPool(g_vulkan_state.device, &createInfo, nullptr, &gCommandPool)) { LOGF << vulkanErrorMesssages[VULKAN_COMMAND_POOL_CREATION_FAILED]; return VULKAN_ERROR_COMMAND_POOL_CREATION_FAILED; } @@ -183,7 +193,7 @@ uint8_t createCommandPools(VulkanState& vulkan_state) * VULKAN_ERROR_BUFFER_CREATION_FAILED * VULKAN_ERROR_VERTEX_BUFFER_MAPPING_FAILED */ -uint8_t createVertexBuffer(VulkanState& vulkan_state) +uint8_t createVertexBuffer() { VkDeviceSize bufferSize; Buffer vertex_buffer; @@ -191,8 +201,10 @@ uint8_t createVertexBuffer(VulkanState& vulkan_state) bufferSize = MAX_VERTEX_BUFFER_SIZE; /* Create Vertex Buffer*/ - if (VK_SUCCESS != createBuffer(vulkan_state, bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, - VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0, /* Will not be mapped with vmaMapMemory */ + if (VK_SUCCESS != createBuffer(bufferSize, + VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, + VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, + 0, /* Will not be mapped with vmaMapMemory */ vertex_buffer)) { LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED]; return VULKAN_ERROR_BUFFER_CREATION_FAILED; @@ -209,7 +221,7 @@ uint8_t createVertexBuffer(VulkanState& vulkan_state) * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_BUFFER_CREATION_FAILED */ -uint8_t createIndexBuffer(VulkanState& vulkan_state) +uint8_t createIndexBuffer() { VkDeviceSize bufferSize; Buffer index_buffer; @@ -217,8 +229,9 @@ uint8_t createIndexBuffer(VulkanState& vulkan_state) bufferSize = MAX_VERTEX_BUFFER_SIZE; /* Create Index Buffer*/ - if (VK_SUCCESS != createBuffer(vulkan_state, bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, - VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0, /* Will not be mapped with vmaMapMemory */ + if (VK_SUCCESS != createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, + VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, + 0, /* Will not be mapped with vmaMapMemory */ index_buffer)) { LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED]; return VULKAN_ERROR_BUFFER_CREATION_FAILED; @@ -229,7 +242,7 @@ uint8_t createIndexBuffer(VulkanState& vulkan_state) return VULKAN_ERROR_NO_ERROR; } -uint8_t createInstanceBuffer(VulkanState& vulkan_state) +uint8_t createInstanceBuffer() { VkDeviceSize bufferSize; Buffer instance_buffer; @@ -238,7 +251,8 @@ uint8_t createInstanceBuffer(VulkanState& vulkan_state) bufferSize = MAX_VERTEX_BUFFER_SIZE; /* Create Instance Buffer*/ - if (VK_SUCCESS != createBuffer(vulkan_state, bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, + if (VK_SUCCESS != createBuffer(bufferSize, + VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0, /* Will not be mapped with vmaMapMemory */ instance_buffer)) { LOGF << vulkanErrorMesssages[VULKAN_BUFFER_CREATION_FAILED]; @@ -257,16 +271,16 @@ uint8_t createInstanceBuffer(VulkanState& vulkan_state) * * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: */ -uint8_t createUniformBuffers(VulkanState& vulkan_state) +uint8_t createUniformBuffers() { VkDeviceSize bufferSize = sizeof(UniformBufferObject); - vulkan_state.uniformBuffers.resize(SWAP_BUFFER_COUNT); + g_vulkan_state.uniformBuffers.resize(SWAP_BUFFER_COUNT); for (size_t i = 0; i < SWAP_BUFFER_COUNT; i++) { - createBuffer(vulkan_state, bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_HOST, + createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_HOST, VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT, - vulkan_state.uniformBuffers[i]); + g_vulkan_state.uniformBuffers[i]); } return VULKAN_ERROR_NO_ERROR; @@ -278,21 +292,21 @@ uint8_t createUniformBuffers(VulkanState& vulkan_state) * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED */ -uint8_t allocateCommandBuffers(VulkanState& vulkan_state) +uint8_t allocateCommandBuffers() { VkCommandBufferAllocateInfo allocateInfo; /* Allocate memory for command buffers */ - vulkan_state.pCommandBuffers.resize(SWAP_BUFFER_COUNT); + g_vulkan_state.pCommandBuffers.resize(SWAP_BUFFER_COUNT); allocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocateInfo.pNext = nullptr; allocateInfo.commandPool = gCommandPool; allocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; /* TODO: When needed, add secondary command buffer */ allocateInfo.commandBufferCount = SWAP_BUFFER_COUNT; - if (VK_SUCCESS != vkAllocateCommandBuffers(vulkan_state.device, + if (VK_SUCCESS != vkAllocateCommandBuffers(g_vulkan_state.device, &allocateInfo, - vulkan_state.pCommandBuffers.data())) { + g_vulkan_state.pCommandBuffers.data())) { LOGF << vulkanErrorMesssages[VULKAN_COMMAND_BUFFER_ALLOCATION_FAILED]; return VULKAN_ERROR_COMMAND_BUFFER_ALLOCATION_FAILED; } @@ -304,22 +318,21 @@ uint8_t allocateCommandBuffers(VulkanState& vulkan_state) * * @param currentImage Current image index */ -void updateUniformBuffer(VulkanState& vulkan_state) +void updateUniformBuffer() { UniformBufferObject ubo{}; // ubo.model = glm::translate(glm::mat4(1.0f), glm::vec3(1.0f, 0.0f, 0.0f)) * glm::rotate(glm::mat4(1.0f), glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f)) * glm::scale(glm::mat4(1.0f), glm::vec3(2, 1, 1)); ubo.model = glm::mat4(1.0f); ubo.view = glm::lookAt(glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); ubo.proj = glm::perspective(glm::radians(45.0f), - vulkan_state.swapchainExtent.width / (float)vulkan_state.swapchainExtent.height, + g_vulkan_state.swapchainExtent.width / (float)g_vulkan_state.swapchainExtent.height, 0.1f, 20.0f); ubo.proj[1][1] *= -1; /* GLM was developed for OpenGL, where Y coordinate of the clip is inverted */ - memcpy(vulkan_state.uniformBuffers[vulkan_state.currentFrame].allocationInfo.pMappedData, &ubo, sizeof(ubo)); + memcpy(g_vulkan_state.uniformBuffers[g_vulkan_state.currentFrame].allocationInfo.pMappedData, &ubo, sizeof(ubo)); } -uint8_t update_mesh_buffers(VulkanState& vulkan_state, - const std::vector& vertices, +uint8_t update_mesh_buffers( const std::vector& vertices, const std::vector& indices) { // void* mapped_data; @@ -331,8 +344,7 @@ uint8_t update_mesh_buffers(VulkanState& vulkan_state, auto last_index_buffer = &gIndexBuffers[gIndexBuffers.size() - 1]; /* Creating staging buffer*/ - if (VK_SUCCESS != createBuffer(vulkan_state, - MAX_VERTEX_BUFFER_SIZE, + if (VK_SUCCESS != createBuffer(MAX_VERTEX_BUFFER_SIZE, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_HOST, VMA_ALLOCATION_CREATE_MAPPED_BIT @@ -345,8 +357,7 @@ uint8_t update_mesh_buffers(VulkanState& vulkan_state, /* Fill staging buffer with vertex data */ memset(staging_buffer.allocationInfo.pMappedData, 0, MAX_VERTEX_BUFFER_SIZE); memcpy(staging_buffer.allocationInfo.pMappedData, vertices.data(), vertices_size); - copyBuffer(vulkan_state, - staging_buffer.buffer, + copyBuffer(staging_buffer.buffer, last_vertex_buffer->buffer, vertices_size, last_vertex_buffer->allocated_size); @@ -354,19 +365,17 @@ uint8_t update_mesh_buffers(VulkanState& vulkan_state, /* Fill staging buffer with index data */ memset(staging_buffer.allocationInfo.pMappedData, 0, MAX_VERTEX_BUFFER_SIZE); memcpy(staging_buffer.allocationInfo.pMappedData, indices.data(), indices_size); - copyBuffer(vulkan_state, - staging_buffer.buffer, + copyBuffer(staging_buffer.buffer, last_index_buffer->buffer, indices_size, last_index_buffer->allocated_size); - vmaDestroyBuffer(vulkan_state.allocator, staging_buffer.buffer, staging_buffer.allocation); + vmaDestroyBuffer(g_vulkan_state.allocator, staging_buffer.buffer, staging_buffer.allocation); return VULKAN_ERROR_NO_ERROR; } -uint8_t update_mesh_instances(VulkanState& vulkan_state, - rse::graphics::mesh::InstanceData& instance_data) +uint8_t update_mesh_instances( rse::graphics::mesh::InstanceData& instance_data) { Buffer staging_buffer; @@ -374,8 +383,7 @@ uint8_t update_mesh_instances(VulkanState& vulkan_state, auto instance_size = sizeof(instance_data); /* Creating staging buffer*/ - if (VK_SUCCESS != createBuffer(vulkan_state, - MAX_VERTEX_BUFFER_SIZE, + if (VK_SUCCESS != createBuffer(MAX_VERTEX_BUFFER_SIZE, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_AUTO_PREFER_HOST, VMA_ALLOCATION_CREATE_MAPPED_BIT @@ -386,13 +394,12 @@ uint8_t update_mesh_instances(VulkanState& vulkan_state, } memcpy(staging_buffer.allocationInfo.pMappedData, &instance_data, instance_size); - copyBuffer(vulkan_state, - staging_buffer.buffer, + copyBuffer(staging_buffer.buffer, last_instance_buffer->buffer, instance_size, last_instance_buffer->allocated_size); - vmaDestroyBuffer(vulkan_state.allocator, staging_buffer.buffer, staging_buffer.allocation); + vmaDestroyBuffer(g_vulkan_state.allocator, staging_buffer.buffer, staging_buffer.allocation); last_instance_buffer->allocated_size += instance_size; @@ -407,9 +414,9 @@ uint8_t update_mesh_instances(VulkanState& vulkan_state, * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_RECORD_COMMAND_BUFFER_FAILED */ -uint8_t recordCommandBuffer(VulkanState& vulkan_state, uint32_t imageIndex) +uint8_t recordCommandBuffer(uint32_t imageIndex) { - VkCommandBuffer commandBuffer = vulkan_state.pCommandBuffers[vulkan_state.currentFrame]; + VkCommandBuffer commandBuffer = g_vulkan_state.pCommandBuffers[g_vulkan_state.currentFrame]; VkCommandBufferBeginInfo beginInfo{}; VkClearValue clearColor = {{{0.0f, 0.0f, 0.0f, 1.0f}}}; VkRenderPassBeginInfo renderPassInfo{}; @@ -430,26 +437,26 @@ uint8_t recordCommandBuffer(VulkanState& vulkan_state, uint32_t imageIndex) } renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; - renderPassInfo.renderPass = vulkan_state.renderPass; - renderPassInfo.framebuffer = vulkan_state.swapchainFramebuffers[imageIndex]; + renderPassInfo.renderPass = g_vulkan_state.renderPass; + renderPassInfo.framebuffer = g_vulkan_state.swapchainFramebuffers[imageIndex]; renderPassInfo.renderArea.offset = {0, 0}; - renderPassInfo.renderArea.extent = vulkan_state.swapchainExtent; + renderPassInfo.renderArea.extent = g_vulkan_state.swapchainExtent; renderPassInfo.clearValueCount = 1; renderPassInfo.pClearValues = &clearColor; vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); - vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, vulkan_state.graphicsPipeline); + vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_vulkan_state.graphicsPipeline); viewport.x = 0.0f; viewport.y = 0.0f; - viewport.width = static_cast(vulkan_state.swapchainExtent.width); - viewport.height = static_cast(vulkan_state.swapchainExtent.height); + viewport.width = static_cast(g_vulkan_state.swapchainExtent.width); + viewport.height = static_cast(g_vulkan_state.swapchainExtent.height); viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; vkCmdSetViewport(commandBuffer, 0, 1, &viewport); scissor.offset = {0, 0}; - scissor.extent = vulkan_state.swapchainExtent; + scissor.extent = g_vulkan_state.swapchainExtent; vkCmdSetScissor(commandBuffer, 0, 1, &scissor); for (size_t i = 0; i < gVertexBuffers.size(); ++i) { @@ -461,8 +468,8 @@ uint8_t recordCommandBuffer(VulkanState& vulkan_state, uint32_t imageIndex) vkCmdBindVertexBuffers(commandBuffer, 0, 1, vertexBuffers, offsets); vkCmdBindVertexBuffers(commandBuffer, 1, 1, instanceBuffers, offsets); vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16); - vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, vulkan_state.pipelineLayout, 0, 1, - &vulkan_state.descriptorSets[vulkan_state.currentFrame], 0, nullptr); + vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_vulkan_state.pipelineLayout, 0, 1, + &g_vulkan_state.descriptorSets[g_vulkan_state.currentFrame], 0, nullptr); vkCmdDrawIndexed(commandBuffer, static_cast(indices.size()), instances.size(), 0, 0, 0); } @@ -477,37 +484,39 @@ uint8_t recordCommandBuffer(VulkanState& vulkan_state, uint32_t imageIndex) return VULKAN_ERROR_NO_ERROR; } -uint8_t create_buffers(VulkanState& vulkan_state) +uint8_t create_buffers() { uint8_t status = VULKAN_ERROR_NO_ERROR;\ - STATUS_CHECK(createCommandPools(vulkan_state)); - STATUS_CHECK(createVertexBuffer(vulkan_state)); - STATUS_CHECK(createIndexBuffer(vulkan_state)); - STATUS_CHECK(createInstanceBuffer(vulkan_state)); - STATUS_CHECK(createUniformBuffers(vulkan_state)); - STATUS_CHECK(allocateCommandBuffers(vulkan_state)); + STATUS_CHECK(createCommandPools()); + STATUS_CHECK(createVertexBuffer()); + STATUS_CHECK(createIndexBuffer()); + STATUS_CHECK(createInstanceBuffer()); + STATUS_CHECK(createUniformBuffers()); + STATUS_CHECK(allocateCommandBuffers()); return status; } -void reset_command_buffer(VulkanState& vulkan_state) +void reset_command_buffer() { - vkResetCommandBuffer(vulkan_state.pCommandBuffers[vulkan_state.currentFrame], /*VkCommandBufferResetFlagBits*/ 0); + vkResetCommandBuffer(g_vulkan_state.pCommandBuffers[g_vulkan_state.currentFrame], /*VkCommandBufferResetFlagBits*/ 0); } -void destroy_buffers(VulkanState& vulkan_state) +void destroy_buffers() { for (size_t i = 0; i < SWAP_BUFFER_COUNT; ++i) { - vmaDestroyBuffer(vulkan_state.allocator, vulkan_state.uniformBuffers[i].buffer, vulkan_state.uniformBuffers[i].allocation); + vmaDestroyBuffer(g_vulkan_state.allocator, + g_vulkan_state.uniformBuffers[i].buffer, + g_vulkan_state.uniformBuffers[i].allocation); } for (size_t i = 0; i < gVertexBuffers.size(); ++i) { - vmaDestroyBuffer(vulkan_state.allocator, gVertexBuffers[i].buffer, gVertexBuffers[i].allocation); - vmaDestroyBuffer(vulkan_state.allocator, gIndexBuffers[i].buffer, gIndexBuffers[i].allocation); - vmaDestroyBuffer(vulkan_state.allocator, gInstanceBuffers[i].buffer, gInstanceBuffers[i].allocation); + vmaDestroyBuffer(g_vulkan_state.allocator, gVertexBuffers[i].buffer, gVertexBuffers[i].allocation); + vmaDestroyBuffer(g_vulkan_state.allocator, gIndexBuffers[i].buffer, gIndexBuffers[i].allocation); + vmaDestroyBuffer(g_vulkan_state.allocator, gInstanceBuffers[i].buffer, gInstanceBuffers[i].allocation); } - vkDestroyCommandPool(vulkan_state.device, gCommandPool, nullptr); + vkDestroyCommandPool(g_vulkan_state.device, gCommandPool, nullptr); } } \ No newline at end of file diff --git a/graphics/src/vulkan_buffers.hpp b/graphics/src/vulkan_buffers.hpp index 61b13c15..9c4b7bd4 100644 --- a/graphics/src/vulkan_buffers.hpp +++ b/graphics/src/vulkan_buffers.hpp @@ -18,21 +18,19 @@ namespace rse::graphics::vulkanbase * @param vulkan_state * @return uint8_t */ -uint8_t create_buffers(VulkanState& vulkan_state); +uint8_t create_buffers(); /** * @brief Update uniform buffers * * @param vulkan_state */ -void updateUniformBuffer(VulkanState& vulkan_state); +void updateUniformBuffer(); -uint8_t update_mesh_buffers(VulkanState& vulkan_state, - const std::vector& vertices, - const std::vector& indices); +uint8_t update_mesh_buffers(const std::vector& vertices, + const std::vector& indices); -uint8_t update_mesh_instances(VulkanState& vulkan_state, - rse::graphics::mesh::InstanceData& instance_data); +uint8_t update_mesh_instances(rse::graphics::mesh::InstanceData& instance_data); /** * @brief Record commands for given image index * @@ -40,21 +38,21 @@ uint8_t update_mesh_instances(VulkanState& vulkan_state, * @param imageIndex * @return uint8_t */ -uint8_t recordCommandBuffer(VulkanState& vulkan_state, uint32_t imageIndex); +uint8_t recordCommandBuffer(uint32_t imageIndex); /** * @brief Reset command buffer for current frame. * * @param vulkan_state */ -void reset_command_buffer(VulkanState& vulkan_state); +void reset_command_buffer(); /** * @brief Destroy ALL previously allocated buffers * * @param vulkan_state */ -void destroy_buffers(VulkanState& vulkan_state); +void destroy_buffers(); } // namespace rse::graphics::vulkanbase diff --git a/graphics/src/vulkan_commons.cpp b/graphics/src/vulkan_commons.cpp new file mode 100644 index 00000000..bfa38333 --- /dev/null +++ b/graphics/src/vulkan_commons.cpp @@ -0,0 +1,7 @@ +#include "vulkan_commons.hpp" + +namespace rse::graphics::vulkanbase { + +VulkanState g_vulkan_state; + +} \ No newline at end of file diff --git a/graphics/src/vulkan_commons.hpp b/graphics/src/vulkan_commons.hpp index b728d2de..053b7463 100644 --- a/graphics/src/vulkan_commons.hpp +++ b/graphics/src/vulkan_commons.hpp @@ -100,6 +100,9 @@ struct VulkanState VkSwapchainKHR swapchain; }; + +extern VulkanState g_vulkan_state; + } // namespace rse::graphics::vulkanbase namespace rse::graphics::mesh diff --git a/graphics/src/vulkan_descriptors.cpp b/graphics/src/vulkan_descriptors.cpp index d35efe8d..b946891e 100644 --- a/graphics/src/vulkan_descriptors.cpp +++ b/graphics/src/vulkan_descriptors.cpp @@ -11,8 +11,8 @@ using namespace locale::vulkan; VkDescriptorPool gDescriptorPool = {}; -static uint8_t createDescriptorPool(VulkanState& vulkan_state); -static uint8_t createDescriptorSets(VulkanState& vulkan_state); +static uint8_t createDescriptorPool(); +static uint8_t createDescriptorSets(); /** * @brief Create a Descriptor Pool @@ -20,7 +20,7 @@ static uint8_t createDescriptorSets(VulkanState& vulkan_state); * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_DESCRIPTOR_POOL_CREATION_FAILED */ -uint8_t createDescriptorPool(VulkanState& vulkan_state) +uint8_t createDescriptorPool() { VkDescriptorPoolSize poolSize{}; poolSize.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; @@ -32,7 +32,7 @@ uint8_t createDescriptorPool(VulkanState& vulkan_state) poolInfo.pPoolSizes = &poolSize; poolInfo.maxSets = static_cast(SWAP_BUFFER_COUNT); - if (vkCreateDescriptorPool(vulkan_state.device, &poolInfo, nullptr, &gDescriptorPool) != VK_SUCCESS) { + if (vkCreateDescriptorPool(g_vulkan_state.device, &poolInfo, nullptr, &gDescriptorPool) != VK_SUCCESS) { LOGF << vulkanErrorMesssages[VULKAN_DESCRIPTOR_POOL_CREATION_FAILED]; return VULKAN_ERROR_DESCRIPTOR_POOL_CREATION_FAILED; } @@ -45,56 +45,56 @@ uint8_t createDescriptorPool(VulkanState& vulkan_state) * * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_DESCRIPTOR_SETS_ALLOCATION_FAILED */ -uint8_t createDescriptorSets(VulkanState& vulkan_state) +uint8_t createDescriptorSets() { - std::vector layouts(SWAP_BUFFER_COUNT, vulkan_state.descriptorSetLayout); + std::vector layouts(SWAP_BUFFER_COUNT, g_vulkan_state.descriptorSetLayout); VkDescriptorSetAllocateInfo allocInfo{}; allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; allocInfo.descriptorPool = gDescriptorPool; allocInfo.descriptorSetCount = static_cast(SWAP_BUFFER_COUNT); allocInfo.pSetLayouts = layouts.data(); - vulkan_state.descriptorSets.resize(SWAP_BUFFER_COUNT); - if (vkAllocateDescriptorSets(vulkan_state.device, &allocInfo, vulkan_state.descriptorSets.data()) != VK_SUCCESS) { + g_vulkan_state.descriptorSets.resize(SWAP_BUFFER_COUNT); + if (vkAllocateDescriptorSets(g_vulkan_state.device, &allocInfo, g_vulkan_state.descriptorSets.data()) != VK_SUCCESS) { LOGF << vulkanErrorMesssages[VULKAN_DESCRIPTOR_SETS_ALLOCATION_FAILED]; return VULKAN_ERROR_DESCRIPTOR_SETS_ALLOCATION_FAILED; } for (size_t i = 0; i < SWAP_BUFFER_COUNT; i++) { VkDescriptorBufferInfo bufferInfo{}; - bufferInfo.buffer = vulkan_state.uniformBuffers[i].buffer; + bufferInfo.buffer = g_vulkan_state.uniformBuffers[i].buffer; bufferInfo.offset = 0; bufferInfo.range = sizeof(UniformBufferObject); VkWriteDescriptorSet descriptorWrite{}; descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - descriptorWrite.dstSet = vulkan_state.descriptorSets[i]; + descriptorWrite.dstSet = g_vulkan_state.descriptorSets[i]; descriptorWrite.dstBinding = 0; descriptorWrite.dstArrayElement = 0; descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; descriptorWrite.descriptorCount = 1; descriptorWrite.pBufferInfo = &bufferInfo; - vkUpdateDescriptorSets(vulkan_state.device, 1, &descriptorWrite, 0, nullptr); + vkUpdateDescriptorSets(g_vulkan_state.device, 1, &descriptorWrite, 0, nullptr); } return VULKAN_ERROR_NO_ERROR; } -uint8_t create_descriptors(VulkanState& vulkan_state) +uint8_t create_descriptors() { uint8_t status = VULKAN_ERROR_NO_ERROR; - STATUS_CHECK(createDescriptorPool(vulkan_state)); - STATUS_CHECK(createDescriptorSets(vulkan_state)); + STATUS_CHECK(createDescriptorPool()); + STATUS_CHECK(createDescriptorSets()); return status; } -void destroy_descriptors(VulkanState& vulkan_state) +void destroy_descriptors() { - vkDestroyDescriptorPool(vulkan_state.device, gDescriptorPool, nullptr); - vkDestroyDescriptorSetLayout(vulkan_state.device, vulkan_state.descriptorSetLayout, nullptr); + vkDestroyDescriptorPool(g_vulkan_state.device, gDescriptorPool, nullptr); + vkDestroyDescriptorSetLayout(g_vulkan_state.device, g_vulkan_state.descriptorSetLayout, nullptr); } } // namespace rse::graphics::vulkanbase diff --git a/graphics/src/vulkan_descriptors.hpp b/graphics/src/vulkan_descriptors.hpp index 64d456fd..43a8dec7 100644 --- a/graphics/src/vulkan_descriptors.hpp +++ b/graphics/src/vulkan_descriptors.hpp @@ -16,14 +16,14 @@ namespace rse::graphics::vulkanbase * @param vulkan_state * @return uint8_t */ -uint8_t create_descriptors(VulkanState& vulkan_state); +uint8_t create_descriptors(); /** * @brief Destroy all descriptors * * @param vulkan_state */ -void destroy_descriptors(VulkanState& vulkan_state); +void destroy_descriptors(); } // namespace rse::graphics::vulkanbase diff --git a/graphics/src/vulkan_pipeline.cpp b/graphics/src/vulkan_pipeline.cpp index 1b1b072f..891d89c8 100644 --- a/graphics/src/vulkan_pipeline.cpp +++ b/graphics/src/vulkan_pipeline.cpp @@ -41,23 +41,23 @@ const std::map gShaderPaths = "../graphics/shaders/shader.frag.num", }}; -static std::optional createShaderModule(VulkanState& state, const std::vector& binaryData); +static std::optional createShaderModule(const std::vector& binaryData); -static uint8_t createSwapchain(VulkanState& vulkan_state); -static uint8_t createSwapchainImageViews(VulkanState& vulkan_state); -static uint8_t createRenderPass(VulkanState& vulkan_state); -static uint8_t createDescriptorSetLayout(VulkanState& vulkan_state); -static uint8_t createPipelineLayout(VulkanState& vulkan_state); -static uint8_t createGraphicsPipeline(VulkanState& vulkan_state); -static uint8_t createFrambuffers(VulkanState& vulkan_state); +static uint8_t createSwapchain(); +static uint8_t createSwapchainImageViews(); +static uint8_t createRenderPass(); +static uint8_t createDescriptorSetLayout(); +static uint8_t createPipelineLayout(); +static uint8_t createGraphicsPipeline(); +static uint8_t createFrambuffers(); -static uint8_t fillShaderStages(VulkanState& vulkan_state, std::vector* shaderStages); +static uint8_t fillShaderStages(std::vector* shaderStages); static void fillDynamicPipelineStateInfo(VkPipelineDynamicStateCreateInfo* dynamicState); static void freeDynamicPipelineStateInfo(VkPipelineDynamicStateCreateInfo* dynamicState); static void fillVertexInputInfo(VkPipelineVertexInputStateCreateInfo* vertexInputInfo); static void freeVertexInputInfo(VkPipelineVertexInputStateCreateInfo* vertexInputInfo); static void fillInputAssemblyInfo(VkPipelineInputAssemblyStateCreateInfo* inputAssembly); -static void fillViewportStateInfo(VulkanState& vulkan_state, VkPipelineViewportStateCreateInfo* viewportState); +static void fillViewportStateInfo(VkPipelineViewportStateCreateInfo* viewportState); static void freeViewportStateInfo(VkPipelineViewportStateCreateInfo* viewportState); static void fillRasterizationInfo(VkPipelineRasterizationStateCreateInfo* rasterizer); static void fillMultisamplingStateInfo(VkPipelineMultisampleStateCreateInfo* multisampling); @@ -70,7 +70,7 @@ static void freeColorBlendInfo(VkPipelineColorBlendStateCreateInfo* colorBlendin * @param binaryData Shader compiled code * @return std::optional Handle to compiled shader module or nullopt */ -std::optional createShaderModule(VulkanState& vulkan_state, const std::vector& binaryData) +std::optional createShaderModule(const std::vector& binaryData) { VkShaderModule shaderModule; VkShaderModuleCreateInfo createInfo{}; @@ -80,7 +80,7 @@ std::optional createShaderModule(VulkanState& vulkan_state, cons createInfo.codeSize = binaryData.size(); createInfo.pCode = reinterpret_cast(binaryData.data()); - if (VK_SUCCESS != vkCreateShaderModule(vulkan_state.device, &createInfo, nullptr, &shaderModule)) { + if (VK_SUCCESS != vkCreateShaderModule(g_vulkan_state.device, &createInfo, nullptr, &shaderModule)) { LOGF << vulkanErrorMesssages[VULKAN_SHADER_UNABLE_TO_CREATE_SHADER]; return std::nullopt; } @@ -94,48 +94,48 @@ std::optional createShaderModule(VulkanState& vulkan_state, cons * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_SWAPCHAIN_CREATION_FAILED */ -uint8_t createSwapchain(VulkanState& vulkan_state) +uint8_t createSwapchain() { VkSwapchainCreateInfoKHR createInfo; VkSurfaceCapabilitiesKHR physicalDeviceSurfaceCapabilities; VkBool32 surfaceSupported; - vkGetPhysicalDeviceSurfaceCapabilitiesKHR(vulkan_state.physicalDevice, vulkan_state.surface, &physicalDeviceSurfaceCapabilities); + vkGetPhysicalDeviceSurfaceCapabilitiesKHR(g_vulkan_state.physicalDevice, g_vulkan_state.surface, &physicalDeviceSurfaceCapabilities); /* Store extent in global variable, for later use */ - vulkan_state.swapchainExtent = physicalDeviceSurfaceCapabilities.currentExtent; + g_vulkan_state.swapchainExtent = physicalDeviceSurfaceCapabilities.currentExtent; /* Check if device supports surface for presentation */ - vkGetPhysicalDeviceSurfaceSupportKHR(vulkan_state.physicalDevice, vulkan_state.queueFamilyIndices[0], vulkan_state.surface, &surfaceSupported); + vkGetPhysicalDeviceSurfaceSupportKHR(g_vulkan_state.physicalDevice, g_vulkan_state.queueFamilyIndices[0], g_vulkan_state.surface, &surfaceSupported); createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; createInfo.pNext = nullptr; createInfo.flags = 0U; - createInfo.surface = vulkan_state.surface; + createInfo.surface = g_vulkan_state.surface; createInfo.minImageCount = SWAP_BUFFER_COUNT + 1; createInfo.imageFormat = IMAGE_FORMAT; /* TODO: Check for supported formats */ createInfo.imageColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; /* TODO: Check for supported color space */ - createInfo.imageExtent = vulkan_state.swapchainExtent; + createInfo.imageExtent = g_vulkan_state.swapchainExtent; createInfo.imageArrayLayers = 1U; /* For non-stereoscopic-3D applications, this value is 1. */ createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; /* TODO: Probably change later? Dunno */ createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; /* Using only one queue family, so this is ok */ createInfo.queueFamilyIndexCount = 1U; - createInfo.pQueueFamilyIndices = vulkan_state.queueFamilyIndices; + createInfo.pQueueFamilyIndices = g_vulkan_state.queueFamilyIndices; createInfo.preTransform = physicalDeviceSurfaceCapabilities.currentTransform; createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; createInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR; /* TODO: Choose present mode from VkPresentModeKHR */ createInfo.clipped = VK_TRUE; createInfo.oldSwapchain = VK_NULL_HANDLE; - if (VK_SUCCESS != vkCreateSwapchainKHR(vulkan_state.device, &createInfo, nullptr, &vulkan_state.swapchain)) { + if (VK_SUCCESS != vkCreateSwapchainKHR(g_vulkan_state.device, &createInfo, nullptr, &g_vulkan_state.swapchain)) { LOGF << vulkanErrorMesssages[VULKAN_SWAPCHAIN_CREATION_FAILED]; return VULKAN_ERROR_SWAPCHAIN_CREATION_FAILED; } /* Obtain swapchain images */ - vkGetSwapchainImagesKHR(vulkan_state.device, vulkan_state.swapchain, &gSwapchainImagesCount, nullptr); + vkGetSwapchainImagesKHR(g_vulkan_state.device, g_vulkan_state.swapchain, &gSwapchainImagesCount, nullptr); gSwapChainImages.resize(gSwapchainImagesCount); - vkGetSwapchainImagesKHR(vulkan_state.device, vulkan_state.swapchain, &gSwapchainImagesCount, gSwapChainImages.data()); + vkGetSwapchainImagesKHR(g_vulkan_state.device, g_vulkan_state.swapchain, &gSwapchainImagesCount, gSwapChainImages.data()); return VULKAN_ERROR_NO_ERROR; } @@ -145,9 +145,9 @@ uint8_t createSwapchain(VulkanState& vulkan_state) * * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: */ -uint8_t createSwapchainImageViews(VulkanState& vulkan_state) +uint8_t createSwapchainImageViews() { - vulkan_state.swapChainImageViews.resize(gSwapchainImagesCount); + g_vulkan_state.swapChainImageViews.resize(gSwapchainImagesCount); for (size_t i = 0; i < gSwapchainImagesCount; i++) { VkImageViewCreateInfo createInfo; @@ -167,7 +167,7 @@ uint8_t createSwapchainImageViews(VulkanState& vulkan_state) createInfo.subresourceRange.baseArrayLayer = 0U; createInfo.subresourceRange.layerCount = 1U; - if (VK_SUCCESS != vkCreateImageView(vulkan_state.device, &createInfo, nullptr, &vulkan_state.swapChainImageViews[i])) { + if (VK_SUCCESS != vkCreateImageView(g_vulkan_state.device, &createInfo, nullptr, &g_vulkan_state.swapChainImageViews[i])) { LOGF << vulkanErrorMesssages[VULKAN_SWAPCHAIN_IMVIEW_CREATION_FAILED]; return VULKAN_ERROR_SWAPCHAIN_IMVIEW_CREATION_FAILED; } @@ -182,7 +182,7 @@ uint8_t createSwapchainImageViews(VulkanState& vulkan_state) * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_RENDERPASS_CREATION_FAILED */ -uint8_t createRenderPass(VulkanState& vulkan_state) +uint8_t createRenderPass() { VkAttachmentDescription colorAttachment{}; colorAttachment.flags = 0U; @@ -211,7 +211,7 @@ uint8_t createRenderPass(VulkanState& vulkan_state) renderPassInfo.subpassCount = 1; renderPassInfo.pSubpasses = &subpass; - if (VK_SUCCESS != vkCreateRenderPass(vulkan_state.device, &renderPassInfo, nullptr, &vulkan_state.renderPass)) { + if (VK_SUCCESS != vkCreateRenderPass(g_vulkan_state.device, &renderPassInfo, nullptr, &g_vulkan_state.renderPass)) { LOGF << vulkanErrorMesssages[VULKAN_RENDERPASS_CREATION_FAILED]; return VULKAN_ERROR_RENDERPASS_CREATION_FAILED; } @@ -224,7 +224,7 @@ uint8_t createRenderPass(VulkanState& vulkan_state) * * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: */ -uint8_t createDescriptorSetLayout(VulkanState& vulkan_state) +uint8_t createDescriptorSetLayout() { VkDescriptorSetLayoutCreateInfo layoutInfo; VkDescriptorSetLayoutBinding uboLayoutBinding; @@ -241,7 +241,7 @@ uint8_t createDescriptorSetLayout(VulkanState& vulkan_state) layoutInfo.bindingCount = 1U; layoutInfo.pBindings = &uboLayoutBinding; - if (vkCreateDescriptorSetLayout(vulkan_state.device, &layoutInfo, nullptr, &vulkan_state.descriptorSetLayout) != VK_SUCCESS) { + if (vkCreateDescriptorSetLayout(g_vulkan_state.device, &layoutInfo, nullptr, &g_vulkan_state.descriptorSetLayout) != VK_SUCCESS) { LOGF << vulkanErrorMesssages[VULKAN_DESCRIPTORSETLAYOUT_CREATION_FAILED]; return VULKAN_ERROR_DESCRIPTORSETLAYOUT_CREATION_FAILED; } @@ -254,18 +254,18 @@ uint8_t createDescriptorSetLayout(VulkanState& vulkan_state) * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_PIPELINE_LAYOUT_CREATION_FAILED */ -uint8_t createPipelineLayout(VulkanState& vulkan_state) +uint8_t createPipelineLayout() { VkPipelineLayoutCreateInfo createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; createInfo.pNext = nullptr; createInfo.flags = 0U; createInfo.setLayoutCount = 1U; - createInfo.pSetLayouts = &vulkan_state.descriptorSetLayout; + createInfo.pSetLayouts = &g_vulkan_state.descriptorSetLayout; createInfo.pushConstantRangeCount = 0U; createInfo.pPushConstantRanges = nullptr; - if (VK_SUCCESS != vkCreatePipelineLayout(vulkan_state.device, &createInfo, nullptr, &vulkan_state.pipelineLayout)) { + if (VK_SUCCESS != vkCreatePipelineLayout(g_vulkan_state.device, &createInfo, nullptr, &g_vulkan_state.pipelineLayout)) { LOGF << vulkanErrorMesssages[VULKAN_PIPELAYOUT_CREATION_FAILED]; return VULKAN_ERROR_PIPELINE_LAYOUT_CREATION_FAILED; } @@ -279,7 +279,7 @@ uint8_t createPipelineLayout(VulkanState& vulkan_state) * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: * VULKAN_ERROR_GRAPHICSPIPELINE_CREATION_FAILED */ -uint8_t createGraphicsPipeline(VulkanState& vulkan_state) +uint8_t createGraphicsPipeline() { uint8_t status = VULKAN_ERROR_NO_ERROR; @@ -292,10 +292,10 @@ uint8_t createGraphicsPipeline(VulkanState& vulkan_state) VkPipelineColorBlendStateCreateInfo colorBlending{}; VkPipelineDynamicStateCreateInfo dynamicState{}; - STATUS_CHECK(fillShaderStages(vulkan_state, &shaderStages)); + STATUS_CHECK(fillShaderStages(&shaderStages)); fillVertexInputInfo(&vertexInputInfo); fillInputAssemblyInfo(&inputAssembly); - fillViewportStateInfo(vulkan_state, &viewportState); + fillViewportStateInfo(&viewportState); fillRasterizationInfo(&rasterizer); fillMultisamplingStateInfo(&multisampling); fillColorBlendStateInfo(&colorBlending); @@ -316,14 +316,14 @@ uint8_t createGraphicsPipeline(VulkanState& vulkan_state) pipelineInfo.pDepthStencilState = nullptr; pipelineInfo.pColorBlendState = &colorBlending; pipelineInfo.pDynamicState = &dynamicState; - pipelineInfo.layout = vulkan_state.pipelineLayout; - pipelineInfo.renderPass = vulkan_state.renderPass; + pipelineInfo.layout = g_vulkan_state.pipelineLayout; + pipelineInfo.renderPass = g_vulkan_state.renderPass; pipelineInfo.subpass = 0; pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; pipelineInfo.basePipelineIndex = -1; if (VK_SUCCESS != - vkCreateGraphicsPipelines(vulkan_state.device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &vulkan_state.graphicsPipeline)) { + vkCreateGraphicsPipelines(g_vulkan_state.device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &g_vulkan_state.graphicsPipeline)) { LOGF << vulkanErrorMesssages[VULKAN_GRAPHICSPIPELINE_CREATION_FAILED]; status = VULKAN_ERROR_GRAPHICSPIPELINE_CREATION_FAILED; } @@ -341,23 +341,23 @@ uint8_t createGraphicsPipeline(VulkanState& vulkan_state) * * @return uint8_t VULKAN_ERROR_NO_ERROR on success. Possible errors: */ -uint8_t createFrambuffers(VulkanState& vulkan_state) +uint8_t createFrambuffers() { - vulkan_state.swapchainFramebuffers.resize(vulkan_state.swapChainImageViews.size()); + g_vulkan_state.swapchainFramebuffers.resize(g_vulkan_state.swapChainImageViews.size()); - for (size_t i = 0; i < vulkan_state.swapChainImageViews.size(); i++) { - VkImageView attachments[] = {vulkan_state.swapChainImageViews[i]}; + for (size_t i = 0; i < g_vulkan_state.swapChainImageViews.size(); i++) { + VkImageView attachments[] = {g_vulkan_state.swapChainImageViews[i]}; VkFramebufferCreateInfo framebufferInfo{}; framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; - framebufferInfo.renderPass = vulkan_state.renderPass; + framebufferInfo.renderPass = g_vulkan_state.renderPass; framebufferInfo.attachmentCount = 1; framebufferInfo.pAttachments = attachments; - framebufferInfo.width = vulkan_state.swapchainExtent.width; - framebufferInfo.height = vulkan_state.swapchainExtent.height; + framebufferInfo.width = g_vulkan_state.swapchainExtent.width; + framebufferInfo.height = g_vulkan_state.swapchainExtent.height; framebufferInfo.layers = 1; - if (vkCreateFramebuffer(vulkan_state.device, &framebufferInfo, nullptr, &vulkan_state.swapchainFramebuffers[i]) != VK_SUCCESS) { + if (vkCreateFramebuffer(g_vulkan_state.device, &framebufferInfo, nullptr, &g_vulkan_state.swapchainFramebuffers[i]) != VK_SUCCESS) { LOGF << vulkanErrorMesssages[VULKAN_FRAMEBUFFERS_CREATION_FAILED]; return VULKAN_ERROR_FRAMEBUFFERS_CREATION_FAILED; } @@ -371,30 +371,30 @@ uint8_t createFrambuffers(VulkanState& vulkan_state) * @brief Recreate swapchain * */ -void recreateSwapchain(VulkanState& vulkan_state) +void recreateSwapchain() { - cleanup_swapchain(vulkan_state); - createSwapchain(vulkan_state); - createSwapchainImageViews(vulkan_state); - createFrambuffers(vulkan_state); + cleanup_swapchain(); + createSwapchain(); + createSwapchainImageViews(); + createFrambuffers(); } /** * @brief Cleanup swapchain * */ -void cleanup_swapchain(VulkanState& vulkan_state) +void cleanup_swapchain() { - vkDeviceWaitIdle(vulkan_state.device); - for (size_t i = 0; i < vulkan_state.swapchainFramebuffers.size(); i++) { - vkDestroyFramebuffer(vulkan_state.device, vulkan_state.swapchainFramebuffers[i], nullptr); + vkDeviceWaitIdle(g_vulkan_state.device); + for (size_t i = 0; i < g_vulkan_state.swapchainFramebuffers.size(); i++) { + vkDestroyFramebuffer(g_vulkan_state.device, g_vulkan_state.swapchainFramebuffers[i], nullptr); } - for (size_t i = 0; i < vulkan_state.swapChainImageViews.size(); i++) { - vkDestroyImageView(vulkan_state.device, vulkan_state.swapChainImageViews[i], nullptr); + for (size_t i = 0; i < g_vulkan_state.swapChainImageViews.size(); i++) { + vkDestroyImageView(g_vulkan_state.device, g_vulkan_state.swapChainImageViews[i], nullptr); } - vkDestroySwapchainKHR(vulkan_state.device, vulkan_state.swapchain, nullptr); + vkDestroySwapchainKHR(g_vulkan_state.device, g_vulkan_state.swapchain, nullptr); } /** @@ -405,7 +405,7 @@ void cleanup_swapchain(VulkanState& vulkan_state) * VULKAN_ERROR_SHADER_FILE_OPEN_FAILED * VULKAN_ERROR_SHADER_CREATION_FAILED */ -uint8_t fillShaderStages(VulkanState& vulkan_state, std::vector* shaderStages) +uint8_t fillShaderStages(std::vector* shaderStages) { auto vertexShaderCode = rse::utils::file::read_file(gShaderPaths.at(ShaderStage::VERTEX)); auto fragmentsShaderCode = rse::utils::file::read_file(gShaderPaths.at(ShaderStage::FRAGMENTS)); @@ -414,8 +414,8 @@ uint8_t fillShaderStages(VulkanState& vulkan_state, std::vector vertexShaderModule = createShaderModule(vulkan_state, vertexShaderCode); - std::optional fragmentsShaderModule = createShaderModule(vulkan_state, fragmentsShaderCode); + std::optional vertexShaderModule = createShaderModule(vertexShaderCode); + std::optional fragmentsShaderModule = createShaderModule(fragmentsShaderCode); if (!vertexShaderModule.has_value() || !fragmentsShaderModule.has_value()) { return VULKAN_ERROR_SHADER_CREATION_FAILED; @@ -570,20 +570,20 @@ void fillInputAssemblyInfo(VkPipelineInputAssemblyStateCreateInfo* inputAssembly * * @param viewportState structure to be filled */ -void fillViewportStateInfo(VulkanState& vulkan_state, VkPipelineViewportStateCreateInfo* viewportState) +void fillViewportStateInfo(VkPipelineViewportStateCreateInfo* viewportState) { VkViewport viewport{}; viewport.x = 0.0f; viewport.y = 0.0f; - viewport.width = static_cast(vulkan_state.swapchainExtent.width); - viewport.height = static_cast(vulkan_state.swapchainExtent.height); + viewport.width = static_cast(g_vulkan_state.swapchainExtent.width); + viewport.height = static_cast(g_vulkan_state.swapchainExtent.height); viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; /* Setting Scissors to fill entire window*/ VkRect2D scissor{}; scissor.offset = {0, 0}; - scissor.extent = vulkan_state.swapchainExtent; + scissor.extent = g_vulkan_state.swapchainExtent; viewportState->sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewportState->viewportCount = 1; @@ -684,29 +684,29 @@ void freeColorBlendInfo(VkPipelineColorBlendStateCreateInfo* colorBlending) delete colorBlending->pAttachments; } -uint8_t create_pipeline(VulkanState& vulkan_state) +uint8_t create_pipeline() { uint8_t status = VULKAN_ERROR_NO_ERROR; - STATUS_CHECK(createSwapchain(vulkan_state)); - STATUS_CHECK(createSwapchainImageViews(vulkan_state)); - STATUS_CHECK(createRenderPass(vulkan_state)); - STATUS_CHECK(createDescriptorSetLayout(vulkan_state)); - STATUS_CHECK(createPipelineLayout(vulkan_state)); - STATUS_CHECK(createGraphicsPipeline(vulkan_state)); - STATUS_CHECK(createFrambuffers(vulkan_state)); + STATUS_CHECK(createSwapchain()); + STATUS_CHECK(createSwapchainImageViews()); + STATUS_CHECK(createRenderPass()); + STATUS_CHECK(createDescriptorSetLayout()); + STATUS_CHECK(createPipelineLayout()); + STATUS_CHECK(createGraphicsPipeline()); + STATUS_CHECK(createFrambuffers()); return status; } -void destroy_pipeline(VulkanState& vulkan_state) +void destroy_pipeline() { - vkDestroyPipeline(vulkan_state.device, vulkan_state.graphicsPipeline, nullptr); - vkDestroyPipelineLayout(vulkan_state.device, vulkan_state.pipelineLayout, nullptr); - vkDestroyRenderPass(vulkan_state.device, vulkan_state.renderPass, nullptr); + vkDestroyPipeline(g_vulkan_state.device, g_vulkan_state.graphicsPipeline, nullptr); + vkDestroyPipelineLayout(g_vulkan_state.device, g_vulkan_state.pipelineLayout, nullptr); + vkDestroyRenderPass(g_vulkan_state.device, g_vulkan_state.renderPass, nullptr); for (size_t i = 0; i < gShaderModules.size(); i++) { - vkDestroyShaderModule(vulkan_state.device, gShaderModules[i], nullptr); + vkDestroyShaderModule(g_vulkan_state.device, gShaderModules[i], nullptr); } } diff --git a/graphics/src/vulkan_pipeline.hpp b/graphics/src/vulkan_pipeline.hpp index 3fa28821..2f102145 100644 --- a/graphics/src/vulkan_pipeline.hpp +++ b/graphics/src/vulkan_pipeline.hpp @@ -15,28 +15,28 @@ namespace rse::graphics::vulkanbase * @param vulkan_state * @return uint8_t */ -uint8_t create_pipeline(VulkanState& vulkan_state); +uint8_t create_pipeline(); /** * @brief Cleanup swapchain * * @param vulkan_state */ -void cleanup_swapchain(VulkanState& vulkan_state); +void cleanup_swapchain(); /** * @brief Recreates swapchain * * @param vulkan_state */ -void recreateSwapchain(VulkanState& vulkan_state); +void recreateSwapchain(); /** * @brief Destroy all vulkan pipeline objects * * @param vulkan_state */ -void destroy_pipeline(VulkanState& vulkan_state); +void destroy_pipeline(); } diff --git a/graphics/src/window.cpp b/graphics/src/window.cpp index 6b517be6..32512050 100644 --- a/graphics/src/window.cpp +++ b/graphics/src/window.cpp @@ -96,11 +96,11 @@ uint16_t windowInit() return WINDOW_ERROR_NO_ERROR; } -uint16_t windowLoop(rse::graphics::vulkanbase::VulkanState& vulkan_state) +uint16_t windowLoop() { while (!glfwWindowShouldClose(gpWindowHandle)) { glfwPollEvents(); - rse::graphics::vulkanbase::drawFrame(vulkan_state); + rse::graphics::vulkanbase::drawFrame(); } return WINDOW_ERROR_NO_ERROR; diff --git a/graphics/src/window.hpp b/graphics/src/window.hpp index c3dafe8f..f595a411 100644 --- a/graphics/src/window.hpp +++ b/graphics/src/window.hpp @@ -16,7 +16,6 @@ /* Vulkan header MUST be included before glfw */ #include -#include "vulkan_commons.hpp" namespace rse::graphics::window { @@ -37,7 +36,7 @@ uint16_t windowInit(); * @param vulkan_state * @return uint16_t WINDOW_SUCCESS or error code */ -uint16_t windowLoop(rse::graphics::vulkanbase::VulkanState& vulkan_state); +uint16_t windowLoop(); /** * @brief Closes window and terminates GLFW