From 22f658239f63df9661a01a34193860d283f0f34f Mon Sep 17 00:00:00 2001 From: Benau Date: Sun, 17 Apr 2022 14:37:52 +0800 Subject: [PATCH] Use GEVulkanCommandLoader for all --- .../include/ge_vulkan_driver.hpp | 13 ---- lib/graphics_engine/src/ge_vulkan_driver.cpp | 69 +++---------------- lib/graphics_engine/src/ge_vulkan_texture.cpp | 13 ++-- 3 files changed, 17 insertions(+), 78 deletions(-) diff --git a/lib/graphics_engine/include/ge_vulkan_driver.hpp b/lib/graphics_engine/include/ge_vulkan_driver.hpp index 071e6f2fb..43add979c 100644 --- a/lib/graphics_engine/include/ge_vulkan_driver.hpp +++ b/lib/graphics_engine/include/ge_vulkan_driver.hpp @@ -291,8 +291,6 @@ namespace GE { return m_features; } const VkPhysicalDeviceProperties& getPhysicalDeviceProperties() const { return m_properties; } - VkCommandBuffer beginSingleTimeCommands(); - void endSingleTimeCommands(VkCommandBuffer command_buffer); io::IFileSystem* getFileSystem() const { return FileSystem; } VkExtent2D getSwapChainExtent() const { return m_swap_chain_extent; } size_t getSwapChainImagesCount() const @@ -378,7 +376,6 @@ namespace GE std::vector image_available_semaphores; std::vector render_finished_semaphores; std::vector in_flight_fences; - VkCommandPool command_pool; std::vector command_buffers; std::array samplers; VkRenderPass render_pass; @@ -389,7 +386,6 @@ namespace GE surface = VK_NULL_HANDLE; device = VK_NULL_HANDLE; swap_chain = VK_NULL_HANDLE; - command_pool = VK_NULL_HANDLE; samplers = {{}}; render_pass = VK_NULL_HANDLE; } @@ -401,14 +397,6 @@ namespace GE vkDestroyRenderPass(device, render_pass, NULL); for (unsigned i = 0; i < GVS_COUNT; i++) vkDestroySampler(device, samplers[i], NULL); - if (!command_buffers.empty()) - { - vkFreeCommandBuffers(device, command_pool, - (uint32_t)(command_buffers.size()), - &command_buffers[0]); - } - if (command_pool != VK_NULL_HANDLE) - vkDestroyCommandPool(device, command_pool, NULL); for (VkSemaphore& semaphore : image_available_semaphores) vkDestroySemaphore(device, semaphore, NULL); for (VkSemaphore& semaphore : render_finished_semaphores) @@ -468,7 +456,6 @@ namespace GE void createDevice(); void createSwapChain(); void createSyncObjects(); - void createCommandPool(); void createCommandBuffers(); void createSamplers(); void createRenderPass(); diff --git a/lib/graphics_engine/src/ge_vulkan_driver.cpp b/lib/graphics_engine/src/ge_vulkan_driver.cpp index 0546005cd..a0ec9f989 100644 --- a/lib/graphics_engine/src/ge_vulkan_driver.cpp +++ b/lib/graphics_engine/src/ge_vulkan_driver.cpp @@ -529,7 +529,6 @@ GEVulkanDriver::GEVulkanDriver(const SIrrlichtCreationParameters& params, createSwapChain(); createSyncObjects(); - createCommandPool(); createSamplers(); createRenderPass(); createFramebuffers(); @@ -574,6 +573,13 @@ void GEVulkanDriver::destroyVulkan() GEVulkan2dRenderer::destroy(); GEVulkanShaderManager::destroy(); + if (!m_vk->command_buffers.empty()) + { + vkFreeCommandBuffers(m_vk->device, + GEVulkanCommandLoader::getCurrentCommandPool(), + (uint32_t)(m_vk->command_buffers.size()), + &m_vk->command_buffers[0]); + } GEVulkanCommandLoader::destroy(); for (std::mutex* m : m_graphics_queue_mutexes) delete m; @@ -1172,21 +1178,6 @@ void GEVulkanDriver::createSyncObjects() } } // createSyncObjects -// ---------------------------------------------------------------------------- -void GEVulkanDriver::createCommandPool() -{ - VkCommandPoolCreateInfo pool_info = {}; - pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; - pool_info.queueFamilyIndex = m_graphics_family; - pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; - - VkResult result = vkCreateCommandPool(m_vk->device, &pool_info, NULL, - &m_vk->command_pool); - - if (result != VK_SUCCESS) - throw std::runtime_error("vkCreateCommandPool failed"); -} // createCommandPool - // ---------------------------------------------------------------------------- void GEVulkanDriver::createCommandBuffers() { @@ -1194,7 +1185,7 @@ void GEVulkanDriver::createCommandBuffers() VkCommandBufferAllocateInfo alloc_info = {}; alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; - alloc_info.commandPool = m_vk->command_pool; + alloc_info.commandPool = GEVulkanCommandLoader::getCurrentCommandPool(); alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; alloc_info.commandBufferCount = (uint32_t)buffers.size(); @@ -1371,55 +1362,15 @@ bool GEVulkanDriver::createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, void GEVulkanDriver::copyBuffer(VkBuffer src_buffer, VkBuffer dst_buffer, VkDeviceSize size) { - VkCommandBuffer command_buffer = beginSingleTimeCommands(); + VkCommandBuffer command_buffer = GEVulkanCommandLoader::beginSingleTimeCommands(); VkBufferCopy copy_region = {}; copy_region.size = size; vkCmdCopyBuffer(command_buffer, src_buffer, dst_buffer, 1, ©_region); - endSingleTimeCommands(command_buffer); + GEVulkanCommandLoader::endSingleTimeCommands(command_buffer); } // copyBuffer -// ---------------------------------------------------------------------------- -VkCommandBuffer GEVulkanDriver::beginSingleTimeCommands() -{ - VkCommandBufferAllocateInfo alloc_info = {}; - alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; - alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; - alloc_info.commandPool = m_vk->command_pool; - alloc_info.commandBufferCount = 1; - - VkCommandBuffer command_buffer; - vkAllocateCommandBuffers(m_vk->device, &alloc_info, &command_buffer); - - VkCommandBufferBeginInfo begin_info = {}; - begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; - begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; - - vkBeginCommandBuffer(command_buffer, &begin_info); - - return command_buffer; -} // beginSingleTimeCommands - -// ---------------------------------------------------------------------------- -void GEVulkanDriver::endSingleTimeCommands(VkCommandBuffer command_buffer) -{ - vkEndCommandBuffer(command_buffer); - - VkSubmitInfo submit_info = {}; - submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - submit_info.commandBufferCount = 1; - submit_info.pCommandBuffers = &command_buffer; - - VkQueue queue = VK_NULL_HANDLE; - std::unique_lock ul = getGraphicsQueue(&queue); - vkQueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE); - vkQueueWaitIdle(queue); - ul.unlock(); - - vkFreeCommandBuffers(m_vk->device, m_vk->command_pool, 1, &command_buffer); -} // beginSingleTimeCommands - // ---------------------------------------------------------------------------- void GEVulkanDriver::OnResize(const core::dimension2d& size) { diff --git a/lib/graphics_engine/src/ge_vulkan_texture.cpp b/lib/graphics_engine/src/ge_vulkan_texture.cpp index 92cd1ce9d..4f2347d2c 100644 --- a/lib/graphics_engine/src/ge_vulkan_texture.cpp +++ b/lib/graphics_engine/src/ge_vulkan_texture.cpp @@ -1,5 +1,6 @@ #include "ge_vulkan_texture.hpp" +#include "ge_vulkan_command_loader.hpp" #include "ge_vulkan_driver.hpp" #include "ge_gl_utils.hpp" #include "ge_main.hpp" @@ -106,7 +107,7 @@ bool GEVulkanTexture::createTextureImage(uint8_t* texture_data) goto destroy; } - command_buffer = getVKDriver()->beginSingleTimeCommands(); + command_buffer = GEVulkanCommandLoader::beginSingleTimeCommands(); transitionImageLayout(command_buffer, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); @@ -117,7 +118,7 @@ bool GEVulkanTexture::createTextureImage(uint8_t* texture_data) transitionImageLayout(command_buffer, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); - getVKDriver()->endSingleTimeCommands(command_buffer); + GEVulkanCommandLoader::endSingleTimeCommands(command_buffer); destroy: vkDestroyBuffer(m_vulkan_device, staging_buffer, NULL); @@ -413,7 +414,7 @@ uint8_t* GEVulkanTexture::getTextureData() VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, buffer, buffer_memory)) return NULL; - VkCommandBuffer command_buffer = getVKDriver()->beginSingleTimeCommands(); + VkCommandBuffer command_buffer = GEVulkanCommandLoader::beginSingleTimeCommands(); transitionImageLayout(command_buffer, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, @@ -436,7 +437,7 @@ uint8_t* GEVulkanTexture::getTextureData() transitionImageLayout(command_buffer, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); - getVKDriver()->endSingleTimeCommands(command_buffer); + GEVulkanCommandLoader::endSingleTimeCommands(command_buffer); uint8_t* texture_data = new uint8_t[image_size]; void* mapped_data; @@ -530,14 +531,14 @@ void GEVulkanTexture::updateTexture(void* data, video::ECOLOR_FORMAT format, } } - VkCommandBuffer command_buffer = getVKDriver()->beginSingleTimeCommands(); + VkCommandBuffer command_buffer = GEVulkanCommandLoader::beginSingleTimeCommands(); transitionImageLayout(command_buffer, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); copyBufferToImage(command_buffer, staging_buffer, w, h, x, y); transitionImageLayout(command_buffer, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); - getVKDriver()->endSingleTimeCommands(command_buffer); + GEVulkanCommandLoader::endSingleTimeCommands(command_buffer); vkDestroyBuffer(m_vulkan_device, staging_buffer, NULL); vkFreeMemory(m_vulkan_device, staging_buffer_memory, NULL);