static void cleanup_vulkan() { vkDestroyDescriptorPool(g_Device, g_DescriptorPool, g_Allocator); for (int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++) { vkDestroyFence(g_Device, g_Fence[i], g_Allocator); vkFreeCommandBuffers(g_Device, g_CommandPool[i], 1, &g_CommandBuffer[i]); vkDestroyCommandPool(g_Device, g_CommandPool[i], g_Allocator); vkDestroySemaphore(g_Device, g_PresentCompleteSemaphore[i], g_Allocator); vkDestroySemaphore(g_Device, g_RenderCompleteSemaphore[i], g_Allocator); } for (uint32_t i = 0; i < g_BackBufferCount; i++) { vkDestroyImageView(g_Device, g_BackBufferView[i], g_Allocator); vkDestroyFramebuffer(g_Device, g_Framebuffer[i], g_Allocator); } vkDestroyRenderPass(g_Device, g_RenderPass, g_Allocator); vkDestroySwapchainKHR(g_Device, g_Swapchain, g_Allocator); vkDestroySurfaceKHR(g_Instance, g_Surface, g_Allocator); #ifdef IMGUI_VULKAN_DEBUG_REPORT // get the proc address of the function pointer, required for used extensions auto vkDestroyDebugReportCallbackEXT = (PFN_vkDestroyDebugReportCallbackEXT)vkGetInstanceProcAddr(g_Instance, "vkDestroyDebugReportCallbackEXT"); vkDestroyDebugReportCallbackEXT(g_Instance, g_Debug_Report, g_Allocator); #endif // IMGUI_VULKAN_DEBUG_REPORT vkDestroyDevice(g_Device, g_Allocator); vkDestroyInstance(g_Instance, g_Allocator); }
/** * Finish command buffer recording and submit it to a queue * * @param commandBuffer Command buffer to flush * @param queue Queue to submit the command buffer to * @param free (Optional) Free the command buffer once it has been submitted (Defaults to true) * * @note The queue that the command buffer is submitted to must be from the same family index as the pool it was allocated from * @note Uses a fence to ensure command buffer has finished executing */ void flushCommandBuffer(VkCommandBuffer commandBuffer, VkQueue queue, bool free = true) { if (commandBuffer == VK_NULL_HANDLE) { return; } VK_CHECK_RESULT(vkEndCommandBuffer(commandBuffer)); VkSubmitInfo submitInfo = vkTools::initializers::submitInfo(); submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &commandBuffer; // Create fence to ensure that the command buffer has finished executing VkFenceCreateInfo fenceInfo = vkTools::initializers::fenceCreateInfo(VK_FLAGS_NONE); VkFence fence; VK_CHECK_RESULT(vkCreateFence(logicalDevice, &fenceInfo, nullptr, &fence)); // Submit to the queue VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, fence)); // Wait for the fence to signal that command buffer has finished executing VK_CHECK_RESULT(vkWaitForFences(logicalDevice, 1, &fence, VK_TRUE, DEFAULT_FENCE_TIMEOUT)); vkDestroyFence(logicalDevice, fence, nullptr); if (free) { vkFreeCommandBuffers(logicalDevice, commandPool, 1, &commandBuffer); } }
void op3d::Engine::createCommandBuffers() { if (commandBuffers.size() > 0) { vkFreeCommandBuffers(device, commandBufferManager.getCommandPool(), commandBuffers.size(), commandBuffers.data()); } commandBuffers.resize(swapChainFramebuffers.size()); VkCommandBufferAllocateInfo allocInfo = {}; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocInfo.commandPool = commandBufferManager.getCommandPool(); allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocInfo.commandBufferCount = (uint32_t)commandBuffers.size(); if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) { throw std::runtime_error("failed to allocate command buffers!"); } for (std::size_t i = 0; i < commandBuffers.size(); i++) { VkCommandBufferBeginInfo beginInfo = {}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT; vkBeginCommandBuffer(commandBuffers[i], &beginInfo); VkRenderPassBeginInfo renderPassInfo = {}; renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassInfo.renderPass = renderPass; renderPassInfo.framebuffer = swapChainFramebuffers[i]; renderPassInfo.renderArea.offset = {0, 0}; renderPassInfo.renderArea.extent = swapChain.getExtent(); std::array <VkClearValue, 2> clearValues = {}; clearValues[0].color = { 0.0f, 0.0f, 0.0f, 1.0f }; clearValues[1].depthStencil = { 1.0f, 0 }; renderPassInfo.clearValueCount = clearValues.size(); renderPassInfo.pClearValues = clearValues.data(); vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline); VkBuffer vertexBuffers[] = {vertexBuffer}; VkDeviceSize offsets[] = {0}; vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets); vkCmdBindIndexBuffer(commandBuffers[i], indexBuffer, 0, VK_INDEX_TYPE_UINT16); vkCmdBindDescriptorSets(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr); vkCmdDrawIndexed(commandBuffers[i], indices.size(), 1, 0, 0, 0); vkCmdEndRenderPass(commandBuffers[i]); if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) { throw std::runtime_error("failed to record command buffer!"); } } }
void Tutorial03::ChildClear() { if( GetDevice() != VK_NULL_HANDLE ) { vkDeviceWaitIdle( GetDevice() ); if( (Vulkan.GraphicsCommandBuffers.size() > 0) && (Vulkan.GraphicsCommandBuffers[0] != VK_NULL_HANDLE) ) { vkFreeCommandBuffers( GetDevice(), Vulkan.GraphicsCommandPool, static_cast<uint32_t>(Vulkan.GraphicsCommandBuffers.size()), &Vulkan.GraphicsCommandBuffers[0] ); Vulkan.GraphicsCommandBuffers.clear(); } if( Vulkan.GraphicsCommandPool != VK_NULL_HANDLE ) { vkDestroyCommandPool( GetDevice(), Vulkan.GraphicsCommandPool, nullptr ); Vulkan.GraphicsCommandPool = VK_NULL_HANDLE; } if( Vulkan.GraphicsPipeline != VK_NULL_HANDLE ) { vkDestroyPipeline( GetDevice(), Vulkan.GraphicsPipeline, nullptr ); Vulkan.GraphicsPipeline = VK_NULL_HANDLE; } if( Vulkan.RenderPass != VK_NULL_HANDLE ) { vkDestroyRenderPass( GetDevice(), Vulkan.RenderPass, nullptr ); Vulkan.RenderPass = VK_NULL_HANDLE; } for( size_t i = 0; i < Vulkan.Framebuffers.size(); ++i ) { if( Vulkan.Framebuffers[i] != VK_NULL_HANDLE ) { vkDestroyFramebuffer( GetDevice(), Vulkan.Framebuffers[i], nullptr ); Vulkan.Framebuffers[i] = VK_NULL_HANDLE; } } Vulkan.Framebuffers.clear(); } }
void CommandBufferManager::DestroyCommandBuffers() { VkDevice device = g_vulkan_context->GetDevice(); for (FrameResources& resources : m_frame_resources) { for (auto& it : resources.cleanup_resources) it(); resources.cleanup_resources.clear(); if (resources.fence != VK_NULL_HANDLE) { vkDestroyFence(device, resources.fence, nullptr); resources.fence = VK_NULL_HANDLE; } if (resources.descriptor_pool != VK_NULL_HANDLE) { vkDestroyDescriptorPool(device, resources.descriptor_pool, nullptr); resources.descriptor_pool = VK_NULL_HANDLE; } if (resources.command_buffers[0] != VK_NULL_HANDLE) { vkFreeCommandBuffers(device, resources.command_pool, static_cast<u32>(resources.command_buffers.size()), resources.command_buffers.data()); resources.command_buffers.fill(VK_NULL_HANDLE); } if (resources.command_pool != VK_NULL_HANDLE) { vkDestroyCommandPool(device, resources.command_pool, nullptr); resources.command_pool = VK_NULL_HANDLE; } } }
static void _free_command_buffer (GstVulkanDevice * device, VkCommandBuffer * cmd) { g_assert (cmd); vkFreeCommandBuffers (device->device, device->cmd_pool, 1, cmd); g_free (cmd); }
VulkanExampleBase::~VulkanExampleBase() { // Clean up Vulkan resources swapChain.cleanup(); if (descriptorPool != VK_NULL_HANDLE) { vkDestroyDescriptorPool(device, descriptorPool, nullptr); } if (setupCmdBuffer != VK_NULL_HANDLE) { vkFreeCommandBuffers(device, cmdPool, 1, &setupCmdBuffer); } destroyCommandBuffers(); vkDestroyRenderPass(device, renderPass, nullptr); for (uint32_t i = 0; i < frameBuffers.size(); i++) { vkDestroyFramebuffer(device, frameBuffers[i], nullptr); } for (auto& shaderModule : shaderModules) { vkDestroyShaderModule(device, shaderModule, nullptr); } vkDestroyImageView(device, depthStencil.view, nullptr); vkDestroyImage(device, depthStencil.image, nullptr); vkFreeMemory(device, depthStencil.mem, nullptr); vkDestroyPipelineCache(device, pipelineCache, nullptr); if (textureLoader) { delete textureLoader; } vkDestroyCommandPool(device, cmdPool, nullptr); vkDestroySemaphore(device, semaphores.presentComplete, nullptr); vkDestroySemaphore(device, semaphores.renderComplete, nullptr); vkDestroyDevice(device, nullptr); if (enableValidation) { vkDebug::freeDebugCallback(instance); } vkDestroyInstance(instance, nullptr); #if defined(__linux) #if defined(__ANDROID__) // todo : android cleanup (if required) #else xcb_destroy_window(connection, window); xcb_disconnect(connection); #endif #endif }
VulkanRenderManager::~VulkanRenderManager() { ILOG("VulkanRenderManager destructor"); StopThread(); vulkan_->WaitUntilQueueIdle(); VkDevice device = vulkan_->GetDevice(); vkDestroySemaphore(device, acquireSemaphore_, nullptr); vkDestroySemaphore(device, renderingCompleteSemaphore_, nullptr); for (int i = 0; i < vulkan_->GetInflightFrames(); i++) { VkCommandBuffer cmdBuf[2]{ frameData_[i].mainCmd, frameData_[i].initCmd }; vkFreeCommandBuffers(device, frameData_[i].cmdPoolInit, 1, &frameData_[i].initCmd); vkFreeCommandBuffers(device, frameData_[i].cmdPoolMain, 1, &frameData_[i].mainCmd); vkDestroyCommandPool(device, frameData_[i].cmdPoolInit, nullptr); vkDestroyCommandPool(device, frameData_[i].cmdPoolMain, nullptr); vkDestroyFence(device, frameData_[i].fence, nullptr); } queueRunner_.DestroyDeviceObjects(); }
void CommandBuffers::destroy() { if (allCommandBuffers.size() > 0) { vkFreeCommandBuffers(device, commandBufferAllocateInfo.commandPool, (uint32_t)allCommandBuffers.size(), &allCommandBuffers[0]); allCommandBuffers.clear(); } }
void VKDevice::free_cmd_buffer (VkCommandPool pool, VkCommandBuffer buf) { XCAM_ASSERT (XCAM_IS_VALID_VK_ID (_dev_id)); XCAM_ASSERT (XCAM_IS_VALID_VK_ID (pool)); XCAM_ASSERT (XCAM_IS_VALID_VK_ID (buf)); vkFreeCommandBuffers (_dev_id, pool, 1, &buf); }
static void vulkan_raster_font_flush(vulkan_raster_t *font) { const struct vk_draw_triangles call = { font->vk->pipelines.font, &font->texture_optimal, font->vk->samplers.mipmap_linear, &font->vk->mvp, sizeof(font->vk->mvp), &font->range, font->vertices, }; if(font->needs_update) { VkCommandBuffer staging; VkSubmitInfo submit_info = { VK_STRUCTURE_TYPE_SUBMIT_INFO }; VkCommandBufferAllocateInfo cmd_info = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO }; VkCommandBufferBeginInfo begin_info = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO }; cmd_info.commandPool = font->vk->staging_pool; cmd_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; cmd_info.commandBufferCount = 1; vkAllocateCommandBuffers(font->vk->context->device, &cmd_info, &staging); begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; vkBeginCommandBuffer(staging, &begin_info); vulkan_copy_staging_to_dynamic(font->vk, staging, &font->texture_optimal, &font->texture); vkEndCommandBuffer(staging); #ifdef HAVE_THREADS slock_lock(font->vk->context->queue_lock); #endif submit_info.commandBufferCount = 1; submit_info.pCommandBuffers = &staging; vkQueueSubmit(font->vk->context->queue, 1, &submit_info, VK_NULL_HANDLE); vkQueueWaitIdle(font->vk->context->queue); #ifdef HAVE_THREADS slock_unlock(font->vk->context->queue_lock); #endif vkFreeCommandBuffers(font->vk->context->device, font->vk->staging_pool, 1, &staging); font->needs_update = false; } vulkan_draw_triangles(font->vk, &call); }
/** * Reallocate command buffers for the text overlay * @note Frees the existing command buffers */ void reallocateCommandBuffers() { vkFreeCommandBuffers(vulkanDevice->logicalDevice, commandPool, static_cast<uint32_t>(cmdBuffers.size()), cmdBuffers.data()); VkCommandBufferAllocateInfo cmdBufAllocateInfo = vks::initializers::commandBufferAllocateInfo( commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, static_cast<uint32_t>(cmdBuffers.size())); VK_CHECK_RESULT(vkAllocateCommandBuffers(vulkanDevice->logicalDevice, &cmdBufAllocateInfo, cmdBuffers.data())); }
void VkApp::endSingleTimeCommands(VkCommandBuffer commandBuffer){ vkEndCommandBuffer(commandBuffer); VkSubmitInfo submitInfo = {}; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &commandBuffer; vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); vkQueueWaitIdle(graphicsQueue); vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer); }
void Renderer::_EndSingleTimeCommands(VkCommandPool pool, VkCommandBuffer commandBuffer) { ErrorCheck(vkEndCommandBuffer(commandBuffer)); VkSubmitInfo submit_info {}; submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submit_info.commandBufferCount = 1; submit_info.pCommandBuffers = &commandBuffer; ErrorCheck(vkQueueSubmit(_queue, 1, &submit_info, VK_NULL_HANDLE)); ErrorCheck(vkQueueWaitIdle(_queue)); vkFreeCommandBuffers(_device, pool, 1, &commandBuffer); }
/*! * \brief Finishes initializing all previously recorded objects * * Submits internal vulkan command buffer */ bool rSceneBase::endInitObject() { if ( !vInitializingObjects ) { eLOG( "beginInitObject was NOT called on secene ", vName_str ); return false; } vkEndCommandBuffer( vInitBuff_vk ); VkSubmitInfo lInfo = {}; lInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; lInfo.pNext = nullptr; lInfo.waitSemaphoreCount = 0; lInfo.pWaitSemaphores = nullptr; lInfo.pWaitDstStageMask = nullptr; lInfo.commandBufferCount = 1; lInfo.pCommandBuffers = &vInitBuff_vk; lInfo.signalSemaphoreCount = 0; lInfo.pSignalSemaphores = nullptr; auto lFence = vWorldPtr->createFence(); VkResult lRes; { std::lock_guard<std::mutex> lLock( vWorldPtr->getInitPtr()->getQueueMutex( vInitQueue_vk ) ); lRes = vkQueueSubmit( vInitQueue_vk, 1, &lInfo, lFence ); } if ( lRes ) { eLOG( "'vkQueueSubmit' returned ", uEnum2Str::toStr( lRes ) ); vInitObjs.clear(); vObjectsInit_MUT.unlock(); return false; } lRes = vkWaitForFences( vWorldPtr->getDevice(), 1, &lFence, VK_TRUE, UINT64_MAX ); if ( lRes ) { eLOG( "'vkQueueSubmit' returned ", uEnum2Str::toStr( lRes ) ); } for ( auto &i : vInitObjs ) i->finishData(); vkDestroyFence( vWorldPtr->getDevice(), lFence, nullptr ); vkFreeCommandBuffers( vWorldPtr->getDevice(), vInitPool_vk, 1, &vInitBuff_vk ); vInitObjs.clear(); vInitializingObjects = false; vObjectsInit_MUT.unlock(); return true; }
void VkContext::Destroy() { vkDeviceWaitIdle(dev); // Free command buffers if (drawCmdBuffers.size() > 0) { vkFreeCommandBuffers(dev, cmdPool, (u32)drawCmdBuffers.size(), &drawCmdBuffers[0]); } // Destroy command pools vkDestroyCommandPool(dev, cmdPool, nullptr); // Destroy semaphores vkDestroySemaphore(dev, acquireCompleteSemaphore, nullptr); vkDestroySemaphore(dev, renderCompleteSemaphore, nullptr); // Destroy swapchain image views for (u32 i = 0; i < swapchainImageViews.size(); i++) { vkDestroyImageView(dev, swapchainImageViews[i], nullptr); } // Destroy swapchain if (swapchain) { vkDestroySwapchainKHR(dev, swapchain, nullptr); } // Destroy surface if (surf) { vkDestroySurfaceKHR(instance, surf, nullptr); } // Destroy device if (dev) { vkDestroyDevice(dev, nullptr); } #ifdef VOXL_DEBUG // Destroy debug report callback if (msgCallback != VK_NULL_HANDLE) { vkDestroyDebugReportCallbackEXT(instance, msgCallback, nullptr); } #endif // Destroy instance if (instance) { vkDestroyInstance(instance, nullptr); } // Terminate GLFW glfwTerminate(); }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class vkDestroyPipeline(device, pipelines.phong, nullptr); vkDestroyPipeline(device, pipelines.starsphere, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkFreeCommandBuffers(device, cmdPool, 1, &primaryCommandBuffer); vkFreeCommandBuffers(device, cmdPool, 1, &secondaryCommandBuffer); vkMeshLoader::freeMeshBufferResources(device, &meshes.ufo); vkMeshLoader::freeMeshBufferResources(device, &meshes.skysphere); for (auto& thread : threadData) { vkFreeCommandBuffers(device, thread.commandPool, thread.commandBuffer.size(), thread.commandBuffer.data()); vkDestroyCommandPool(device, thread.commandPool, nullptr); } vkDestroyFence(device, renderFence, nullptr); }
int sample_main() { VkResult U_ASSERT_ONLY res; struct sample_info info = {}; char sample_title[] = "Command Buffer Sample"; init_global_layer_properties(info); init_instance_extension_names(info); init_device_extension_names(info); init_instance(info, sample_title); init_enumerate_device(info); init_window_size(info, 500, 500); init_connection(info); init_window(info); init_swapchain_extension(info); init_device(info); /* VULKAN_KEY_START */ /* Create a command pool to allocate our command buffer from */ VkCommandPoolCreateInfo cmd_pool_info = {}; cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; cmd_pool_info.pNext = NULL; cmd_pool_info.queueFamilyIndex = info.graphics_queue_family_index; cmd_pool_info.flags = 0; res = vkCreateCommandPool(info.device, &cmd_pool_info, NULL, &info.cmd_pool); assert(res == VK_SUCCESS); /* Create the command buffer from the command pool */ VkCommandBufferAllocateInfo cmd = {}; cmd.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; cmd.pNext = NULL; cmd.commandPool = info.cmd_pool; cmd.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; cmd.commandBufferCount = 1; res = vkAllocateCommandBuffers(info.device, &cmd, &info.cmd); assert(res == VK_SUCCESS); /* VULKAN_KEY_END */ VkCommandBuffer cmd_bufs[1] = {info.cmd}; vkFreeCommandBuffers(info.device, info.cmd_pool, 1, cmd_bufs); vkDestroyCommandPool(info.device, info.cmd_pool, NULL); destroy_window(info); destroy_device(info); destroy_instance(info); return 0; }
ICommandBuffersSP VKTS_APIENTRY commandBuffersCreate(const VkDevice device, const VkCommandPool commandPool, const VkCommandBufferLevel level, const uint32_t commandBufferCount) { if (!device) { return ICommandBuffersSP(); } VkResult result; VkCommandBufferAllocateInfo commandBufferAllocateInfo; memset(&commandBufferAllocateInfo, 0, sizeof(VkCommandBufferAllocateInfo)); commandBufferAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; commandBufferAllocateInfo.commandPool = commandPool; commandBufferAllocateInfo.level = level; commandBufferAllocateInfo.commandBufferCount = commandBufferCount; std::vector<VkCommandBuffer> allCommandBuffers; VkCommandBuffer dummy; for (uint32_t i = 0; i < commandBufferCount; i++) { allCommandBuffers.push_back(dummy); } result = vkAllocateCommandBuffers(device, &commandBufferAllocateInfo, &allCommandBuffers[0]); if (result != VK_SUCCESS) { logPrint(VKTS_LOG_ERROR, "CommandBuffer: Could not create command buffer."); return ICommandBuffersSP(); } // auto newInstance = new CommandBuffers(device, commandPool, level, commandBufferCount, &allCommandBuffers[0]); if (!newInstance) { vkFreeCommandBuffers(device, commandPool, (uint32_t)allCommandBuffers.size(), &allCommandBuffers[0]); return ICommandBuffersSP(); } return ICommandBuffersSP(newInstance); }
void flush_command_buffer() { VkResult err; if (_vulkan_command_buffer == VK_NULL_HANDLE) { return; } err = vkEndCommandBuffer(_vulkan_command_buffer); assert(!err); const VkCommandBuffer command_buffers[] = { _vulkan_command_buffer }; VkFence nullFence = VK_NULL_HANDLE; /* typedef struct VkSubmitInfo { VkStructureType sType; const void* pNext; uint32_t waitSemaphoreCount; const VkSemaphore* pWaitSemaphores; const VkPipelineStageFlags* pWaitDstStageMask; uint32_t commandBufferCount; const VkCommandBuffer* pCommandBuffers; uint32_t signalSemaphoreCount; const VkSemaphore* pSignalSemaphores; } VkSubmitInfo; */ VkSubmitInfo submit_info = { VK_STRUCTURE_TYPE_SUBMIT_INFO, NULL, 0, NULL, NULL, 1, command_buffers, 0, NULL }; err = vkQueueSubmit(_vulkan_queue, 1, &submit_info, nullFence); assert(!err); err = vkQueueWaitIdle(_vulkan_queue); assert(!err); vkFreeCommandBuffers(_vulkan_device, _vulkan_command_pool, 1, command_buffers); _vulkan_command_buffer = VK_NULL_HANDLE; }
RHI_CommandList::~RHI_CommandList() { auto cmd_pool_vk = static_cast<VkCommandPool>(m_cmd_pool); for (unsigned int i = 0; i < g_max_frames_in_flight; i++) { Vulkan_Common::fence::destroy(m_rhi_device, m_fences_in_flight[i]); Vulkan_Common::semaphore::destroy(m_rhi_device, m_semaphores_render_finished[i]); auto cmd_buffer = static_cast<VkCommandBuffer>(m_cmd_buffers[i]); vkFreeCommandBuffers(m_rhi_device->GetContext()->device, cmd_pool_vk, 1, &cmd_buffer); } m_cmd_buffers.clear(); m_semaphores_render_finished.clear(); m_fences_in_flight.clear(); vkDestroyCommandPool(m_rhi_device->GetContext()->device, cmd_pool_vk, nullptr); m_cmd_pool = nullptr; }
void vkeGameRendererDynamic::initTerrainCommand(){ VulkanDC *dc = VulkanDC::Get(); VulkanDC::Device *device = dc->getDefaultDevice(); VulkanDC::Device::Queue *queue = dc->getDefaultQueue(); uint32_t cmdID = 1021; VkResult rslt; for (uint32_t i = 0; i < 2; ++i){ if (m_terrain_command[i] != VK_NULL_HANDLE){ vkFreeCommandBuffers(device->getVKDevice(), queue->getCommandPool(), 1, &m_terrain_command[i]); m_terrain_command[i] = VK_NULL_HANDLE; } { VkCommandBufferAllocateInfo cmdBufInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO }; cmdBufInfo.commandBufferCount = 1; cmdBufInfo.commandPool = queue->getCommandPool(); cmdBufInfo.level = VK_COMMAND_BUFFER_LEVEL_SECONDARY; rslt = vkAllocateCommandBuffers(device->getVKDevice(), &cmdBufInfo, &m_terrain_command[i]); VkCommandBufferBeginInfo cmdBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO }; cmdBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT; rslt = vkBeginCommandBuffer(m_terrain_command[i], &cmdBeginInfo); vkCmdBindPipeline(m_terrain_command[i], VK_PIPELINE_BIND_POINT_GRAPHICS, m_quad_pipeline); vkCmdBindDescriptorSets(m_terrain_command[i], VK_PIPELINE_BIND_POINT_GRAPHICS, m_quad_pipeline_layout, 0, 1, &m_quad_descriptor_set, 0, NULL); m_screen_quad.bind(&m_terrain_command[i]); m_screen_quad.draw(&m_terrain_command[i]); vkCmdBindPipeline(m_terrain_command[i], VK_PIPELINE_BIND_POINT_GRAPHICS, m_terrain_pipeline); vkCmdBindDescriptorSets(m_terrain_command[i], VK_PIPELINE_BIND_POINT_GRAPHICS, m_terrain_pipeline_layout, 0, 1, &m_terrain_descriptor_set, 0, NULL); m_terrain_quad.bind(&m_terrain_command[i]); m_terrain_quad.draw(&m_terrain_command[i]);/**/ vkEndCommandBuffer(m_terrain_command[i]); } } }
void VulkanBase::flushSetupCommandBuffer() { if (setupCmdBuffer == VK_NULL_HANDLE) return; vkTools::checkResult(vkEndCommandBuffer(setupCmdBuffer)); VkSubmitInfo submitInfo = {}; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &setupCmdBuffer; vkTools::checkResult(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE)); vkTools::checkResult(vkQueueWaitIdle(queue)); vkFreeCommandBuffers(device, cmdPool, 1, &setupCmdBuffer); setupCmdBuffer = VK_NULL_HANDLE; }
/** * Default destructor, frees up all Vulkan resources acquired by the text overlay */ ~VulkanTextOverlay() { // Free up all Vulkan resources requested by the text overlay vertexBuffer.destroy(); vkDestroySampler(vulkanDevice->logicalDevice, sampler, nullptr); vkDestroyImage(vulkanDevice->logicalDevice, image, nullptr); vkDestroyImageView(vulkanDevice->logicalDevice, view, nullptr); vkFreeMemory(vulkanDevice->logicalDevice, imageMemory, nullptr); vkDestroyDescriptorSetLayout(vulkanDevice->logicalDevice, descriptorSetLayout, nullptr); vkDestroyDescriptorPool(vulkanDevice->logicalDevice, descriptorPool, nullptr); vkDestroyPipelineLayout(vulkanDevice->logicalDevice, pipelineLayout, nullptr); vkDestroyPipelineCache(vulkanDevice->logicalDevice, pipelineCache, nullptr); vkDestroyPipeline(vulkanDevice->logicalDevice, pipeline, nullptr); vkDestroyRenderPass(vulkanDevice->logicalDevice, renderPass, nullptr); vkFreeCommandBuffers(vulkanDevice->logicalDevice, commandPool, static_cast<uint32_t>(cmdBuffers.size()), cmdBuffers.data()); vkDestroyCommandPool(vulkanDevice->logicalDevice, commandPool, nullptr); vkDestroyFence(vulkanDevice->logicalDevice, fence, nullptr); }
void VkHelper::endSingleTimeCommandBuffer(VkDevice device, VkCommandPool cmdPool, VkQueue queue, VkCommandBuffer cmdBuffer) { if (vkEndCommandBuffer(cmdBuffer) != VK_SUCCESS) std::runtime_error("ERROR: Command buffer end failed."); VkSubmitInfo submitInfo{}; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.pNext = VK_NULL_HANDLE; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &cmdBuffer; if (vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE) != VK_SUCCESS) std::runtime_error("ERROR: Submission to the queue failed."); vkQueueWaitIdle(queue); vkFreeCommandBuffers(device, cmdPool, 1, &cmdBuffer); }
void VulkanBase::createSetupCommandBuffer() { if (setupCmdBuffer != VK_NULL_HANDLE){ vkFreeCommandBuffers(device, cmdPool, 1, &setupCmdBuffer); setupCmdBuffer = VK_NULL_HANDLE; // todo : check if still necessary } VkCommandBufferAllocateInfo cmdBufAllocateInfo = vkTools::initializers::commandBufferAllocateInfo( cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1); vkTools::checkResult(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &setupCmdBuffer)); VkCommandBufferBeginInfo cmdBufInfo = {}; cmdBufInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; vkTools::checkResult(vkBeginCommandBuffer(setupCmdBuffer, &cmdBufInfo)); }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class vkDestroyPipeline(device, pipelines.phong, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkTools::destroyUniformData(device, &uniformData.vsScene); vkMeshLoader::freeMeshBufferResources(device, &meshes.ufo); for (auto& thread : renderThreads) { vkFreeCommandBuffers(device, thread.cmdPool, thread.cmdBuffers.size(), thread.cmdBuffers.data()); vkDestroyCommandPool(device, thread.cmdPool, nullptr); } }
void cleanupVulkan() { prepared = false; vkDestroyPipeline(device, pipelines.solid, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyPipelineLayout(device, computePipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(device, computeDescriptorSetLayout, nullptr); vkDestroyBuffer(device, uniformDataCompute.buffer, nullptr); vkFreeMemory(device, uniformDataCompute.memory, nullptr); vkDestroyBuffer(device, computeStorageBuffer.buffer, nullptr); vkFreeMemory(device, computeStorageBuffer.memory, nullptr); destroyTextureImage(&texture); vkFreeCommandBuffers(device, cmdPool, 1, &computeCmdBuffer); VulkanExample::cleanUpVulkan(); }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class vkDestroyPipeline(device, pipelines.postCompute, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkDestroyBuffer(device, computeStorageBuffer.buffer, nullptr); vkFreeMemory(device, computeStorageBuffer.memory, nullptr); vkTools::destroyUniformData(device, &uniformData.computeShader.ubo); vkFreeCommandBuffers(device, cmdPool, 1, &computeCmdBuffer); vkDestroyPipelineLayout(device, computePipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, computeDescriptorSetLayout, nullptr); vkDestroyPipeline(device, pipelines.compute, nullptr); textureLoader->destroyTexture(textureColorMap); }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class // Texture target textureLoader->destroyTexture(offScreenFrameBuf.textureTarget); // Frame buffer // Color attachment vkDestroyImageView(device, offScreenFrameBuf.color.view, nullptr); vkDestroyImage(device, offScreenFrameBuf.color.image, nullptr); vkFreeMemory(device, offScreenFrameBuf.color.mem, nullptr); // Depth attachment vkDestroyImageView(device, offScreenFrameBuf.depth.view, nullptr); vkDestroyImage(device, offScreenFrameBuf.depth.image, nullptr); vkFreeMemory(device, offScreenFrameBuf.depth.mem, nullptr); vkDestroyFramebuffer(device, offScreenFrameBuf.frameBuffer, nullptr); vkDestroyPipeline(device, pipelines.quad, nullptr); vkDestroyPipeline(device, pipelines.offscreen, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.quad, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.offscreen, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); // Meshes vkMeshLoader::freeMeshBufferResources(device, &meshes.scene); vkMeshLoader::freeMeshBufferResources(device, &meshes.quad); // Uniform buffers vkTools::destroyUniformData(device, &uniformDataVS); vkTools::destroyUniformData(device, &uniformDataOffscreenVS); vkFreeCommandBuffers(device, cmdPool, 1, &offScreenCmdBuffer); }