~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class vkDestroyImageView(device, attachments.color.view, nullptr); vkDestroyImage(device, attachments.color.image, nullptr); vkFreeMemory(device, attachments.color.memory, nullptr); vkDestroyImageView(device, attachments.depth.view, nullptr); vkDestroyImage(device, attachments.depth.image, nullptr); vkFreeMemory(device, attachments.depth.memory, nullptr); vkDestroyPipeline(device, pipelines.attachmentRead, nullptr); vkDestroyPipeline(device, pipelines.attachmentWrite, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.attachmentWrite, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.attachmentRead, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.attachmentWrite, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.attachmentRead, nullptr); vkDestroyRenderPass(device, uiRenderPass, nullptr); scene.destroy(); uniformBuffers.matrices.destroy(); uniformBuffers.params.destroy(); }
VKRenderPass::~VKRenderPass() { VKRenderer* renderer = VKRenderer::RendererInstance; //Free input descriptor sets vkFreeDescriptorSets(m_device, m_descriptorPool, static_cast<uint32_t>(m_inputTargetDescriptorSets.size()), m_inputTargetDescriptorSets.data()); //Destroy framebuffer images for (size_t i = 0; i < m_colorImages.size(); i++) { Image_vk image = m_colorImages[i]; vkDestroyImageView(m_device, image.view, nullptr); vkDestroyImage(m_device, image.image, nullptr); vkFreeMemory(m_device, image.memory, nullptr); } //Destroy depth image vkDestroyImageView(m_device, m_depthImage.view, nullptr); vkDestroyImage(m_device, m_depthImage.image, nullptr); vkFreeMemory(m_device, m_depthImage.memory, nullptr); //Free instance texel buffers if(m_instanceBlock.buffer != VK_NULL_HANDLE) DeleteUniformBuffer(m_device, m_instanceBlock); //Destroy framebuffer vkDestroyFramebuffer(m_device, m_framebuffer, nullptr); //Destroy the render pass vkDestroyRenderPass(m_device, m_renderPass, nullptr); }
void VulkanTexturedQuad::ShutdownImpl() { vkDestroyPipeline(device_, pipeline_, nullptr); vkDestroyPipelineLayout(device_, pipelineLayout_, nullptr); vkDestroyBuffer(device_, vertexBuffer_, nullptr); vkDestroyBuffer(device_, indexBuffer_, nullptr); vkFreeMemory(device_, deviceBufferMemory_, nullptr); vkDestroyImageView (device_, rubyImageView_, nullptr); vkDestroyImage (device_, rubyImage_, nullptr); vkFreeMemory (device_, deviceImageMemory_, nullptr); vkDestroyBuffer (device_, uploadImageBuffer_, nullptr); vkFreeMemory (device_, uploadImageMemory_, nullptr); vkDestroyBuffer (device_, uploadBufferBuffer_, nullptr); vkFreeMemory (device_, uploadBufferMemory_, nullptr); vkDestroyDescriptorSetLayout (device_, descriptorSetLayout_, nullptr); vkDestroyDescriptorPool (device_, descriptorPool_, nullptr); vkDestroySampler (device_, sampler_, nullptr); vkDestroyShaderModule(device_, vertexShader_, nullptr); vkDestroyShaderModule(device_, fragmentShader_, nullptr); VulkanSample::ShutdownImpl(); }
void vkImageBase::cleanup() { VulkanCore * instance = VulkanCore::getInstance(); VkDevice device = instance->getDevice(); if(imageView != 0) { vkDestroyImageView(device, imageView, nullptr); imageView = 0; } if(imageHandle != 0 ) { vkDestroyImage(device, imageHandle, nullptr); imageHandle = 0; } if(device_memory != 0) { vkFreeMemory(device, device_memory, nullptr); device_memory = 0; } if(cascadeImageView.size() != 0) { for(int i = 0; i < cascadeImageView.size(); i++) vkDestroyImageView(device, cascadeImageView[i], nullptr); cascadeImageView.clear(); } }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class // Clean up texture resources vkDestroyImageView(device, cubeMap.view, nullptr); vkDestroyImage(device, cubeMap.image, nullptr); vkDestroySampler(device, cubeMap.sampler, nullptr); vkFreeMemory(device, cubeMap.deviceMemory, nullptr); vkDestroyPipeline(device, pipelines.skybox, nullptr); vkDestroyPipeline(device, pipelines.reflect, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); for (auto& model : models.objects) { model.destroy(); } models.skybox.destroy(); uniformBuffers.object.destroy(); uniformBuffers.skybox.destroy(); }
BaseImage::~BaseImage() { if(m_Image) { vkDestroyImage(g_hDevice, m_Image, nullptr); } }
void ImGui_ImplGlfwVulkan_InvalidateDeviceObjects() { ImGui_ImplGlfwVulkan_InvalidateFontUploadObjects(); for (int i=0; i<IMGUI_VK_QUEUED_FRAMES; i++) { if (g_VertexBuffer[i]) vkDestroyBuffer(g_Device, g_VertexBuffer[i], g_Allocator); if (g_VertexBufferMemory[i]) vkFreeMemory(g_Device, g_VertexBufferMemory[i], g_Allocator); if (g_IndexBuffer[i]) vkDestroyBuffer(g_Device, g_IndexBuffer[i], g_Allocator); if (g_IndexBufferMemory[i]) vkFreeMemory(g_Device, g_IndexBufferMemory[i], g_Allocator); } if (g_FontView) vkDestroyImageView(g_Device, g_FontView, g_Allocator); if (g_FontImage) vkDestroyImage(g_Device, g_FontImage, g_Allocator); if (g_FontMemory) vkFreeMemory(g_Device, g_FontMemory, g_Allocator); if (g_FontSampler) vkDestroySampler(g_Device, g_FontSampler, g_Allocator); if (g_DescriptorSetLayout) vkDestroyDescriptorSetLayout(g_Device, g_DescriptorSetLayout, g_Allocator); if (g_PipelineLayout) vkDestroyPipelineLayout(g_Device, g_PipelineLayout, g_Allocator); if (g_Pipeline) vkDestroyPipeline(g_Device, g_Pipeline, g_Allocator); }
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 }
void vkeGameRendererDynamic::releaseFramebuffer(){ VulkanDC *dc = VulkanDC::Get(); VulkanDC::Device *device = dc->getDefaultDevice(); device->waitIdle(); vkDestroyImageView(device->getVKDevice(), m_depth_attachment.view, NULL); vkDestroyImageView(device->getVKDevice(), m_color_attachment.view, NULL); vkDestroyImageView(device->getVKDevice(), m_resolve_attachment[0].view, NULL); vkDestroyImageView(device->getVKDevice(), m_resolve_attachment[1].view, NULL); m_depth_attachment.view = NULL; m_color_attachment.view = NULL; m_resolve_attachment[0].view = NULL; m_resolve_attachment[1].view = NULL; vkDestroyImage(device->getVKDevice(), m_depth_attachment.image, NULL); vkDestroyImage(device->getVKDevice(), m_color_attachment.image, NULL); vkDestroyImage(device->getVKDevice(), m_resolve_attachment[0].image, NULL); vkDestroyImage(device->getVKDevice(), m_resolve_attachment[1].image, NULL); m_depth_attachment.image = NULL; m_color_attachment.image = NULL; m_resolve_attachment[0].image = NULL; m_resolve_attachment[1].image = NULL; vkFreeMemory(device->getVKDevice(), m_depth_attachment.memory, NULL); vkFreeMemory(device->getVKDevice(), m_color_attachment.memory, NULL); vkFreeMemory(device->getVKDevice(), m_resolve_attachment[0].memory, NULL); vkFreeMemory(device->getVKDevice(), m_resolve_attachment[1].memory, NULL); m_depth_attachment.memory = NULL; m_color_attachment.memory = NULL; m_resolve_attachment[0].memory = NULL; m_resolve_attachment[1].memory = NULL; vkDestroyFramebuffer(device->getVKDevice(), m_framebuffers[0], NULL); vkDestroyFramebuffer(device->getVKDevice(), m_framebuffers[1], NULL); m_framebuffers[0] = NULL; m_framebuffers[1] = NULL; }
void Image::destroy() { if (image) { vkDestroyImage(device, image, nullptr); image = VK_NULL_HANDLE; } }
// Free all Vulkan resources used a texture object void destroyTextureImage(Texture texture) { if (texture.view != VK_NULL_HANDLE) vkDestroyImageView(device, texture.view, nullptr); if (texture.image != VK_NULL_HANDLE) vkDestroyImage(device, texture.image, nullptr); if (texture.sampler != VK_NULL_HANDLE) vkDestroySampler(device, texture.sampler, nullptr); if (texture.deviceMemory != VK_NULL_HANDLE) vkFreeMemory(device, texture.deviceMemory, nullptr); }
~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); }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class // Frame buffer // Color attachment vkDestroyImageView(device, offscreenPass.color.view, nullptr); vkDestroyImage(device, offscreenPass.color.image, nullptr); vkFreeMemory(device, offscreenPass.color.mem, nullptr); // Depth attachment vkDestroyImageView(device, offscreenPass.depth.view, nullptr); vkDestroyImage(device, offscreenPass.depth.image, nullptr); vkFreeMemory(device, offscreenPass.depth.mem, nullptr); vkDestroyRenderPass(device, offscreenPass.renderPass, nullptr); vkDestroySampler(device, offscreenPass.sampler, nullptr); vkDestroyFramebuffer(device, offscreenPass.frameBuffer, nullptr); vkDestroyPipeline(device, pipelines.radialBlur, nullptr); vkDestroyPipeline(device, pipelines.phongPass, nullptr); vkDestroyPipeline(device, pipelines.colorPass, nullptr); vkDestroyPipeline(device, pipelines.offscreenDisplay, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.radialBlur, nullptr); vkDestroyPipelineLayout(device, pipelineLayouts.scene, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.scene, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.radialBlur, nullptr); models.example.destroy(); uniformBuffers.scene.destroy(); uniformBuffers.blurParams.destroy(); textures.gradient.destroy(); }
void Image::destroy() { if (view) { vkDestroyImageView(vk, view, NULL); view = NULL; } if (mem) { vkFreeMemory(vk, mem, NULL); mem = NULL; if (image) vkDestroyImage(vk, image, NULL); } image = NULL; }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class vkDestroyPipeline(device, pipelines.solid, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkMeshLoader::freeMeshBufferResources(device, &meshes.example); // Destroy MSAA target vkDestroyImage(device, multisampleTarget.color.image, nullptr); vkDestroyImageView(device, multisampleTarget.color.view, nullptr); vkFreeMemory(device, multisampleTarget.color.memory, nullptr); vkDestroyImage(device, multisampleTarget.depth.image, nullptr); vkDestroyImageView(device, multisampleTarget.depth.view, nullptr); vkFreeMemory(device, multisampleTarget.depth.memory, nullptr); textureLoader->destroyTexture(textures.colorMap); vkTools::destroyUniformData(device, &uniformData.vsScene); }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class vkDestroyPipeline(device, pipelines.MSAA, nullptr); vkDestroyPipeline(device, pipelines.MSAASampleShading, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); models.example.destroy(); // Destroy MSAA target vkDestroyImage(device, multisampleTarget.color.image, nullptr); vkDestroyImageView(device, multisampleTarget.color.view, nullptr); vkFreeMemory(device, multisampleTarget.color.memory, nullptr); vkDestroyImage(device, multisampleTarget.depth.image, nullptr); vkDestroyImageView(device, multisampleTarget.depth.view, nullptr); vkFreeMemory(device, multisampleTarget.depth.memory, nullptr); textures.colorMap.destroy(); uniformBuffer.destroy(); }
void destroy_vulkan_texture(vulkan_texture& texture) { if (texture.sampler != VK_NULL_HANDLE) { vkDestroySampler(_vulkan_device, texture.sampler, NULL); } if (texture.image != VK_NULL_HANDLE) { vkDestroyImage(_vulkan_device, texture.image, NULL); } if (texture.view != VK_NULL_HANDLE) { vkDestroyImageView(_vulkan_device, texture.view, NULL); } if (texture.device_memory != VK_NULL_HANDLE) { vkFreeMemory(_vulkan_device, texture.device_memory, NULL); } }
void WImage::_DestroyResources() { VkDevice device = m_app->GetVulkanDevice(); if (m_stagingBuffer) vkDestroyBuffer(device, m_stagingBuffer, nullptr); if (m_stagingMemory) vkFreeMemory(device, m_stagingMemory, nullptr); if (m_image) vkDestroyImage(device, m_image, nullptr); if (m_deviceMemory) vkFreeMemory(device, m_deviceMemory, nullptr); if (m_view) vkDestroyImageView(device, m_view, nullptr); m_stagingBuffer = VK_NULL_HANDLE; m_stagingMemory = VK_NULL_HANDLE; m_image = VK_NULL_HANDLE; m_deviceMemory = VK_NULL_HANDLE; m_view = 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 VulkanExampleBase::windowResize() { if (!prepared) { return; } prepared = false; // Recreate swap chain width = destWidth; height = destHeight; createSetupCommandBuffer(); setupSwapChain(); // Recreate the frame buffers vkDestroyImageView(device, depthStencil.view, nullptr); vkDestroyImage(device, depthStencil.image, nullptr); vkFreeMemory(device, depthStencil.mem, nullptr); setupDepthStencil(); for (uint32_t i = 0; i < frameBuffers.size(); i++) { vkDestroyFramebuffer(device, frameBuffers[i], nullptr); } setupFrameBuffer(); flushSetupCommandBuffer(); // Command buffers need to be recreated as they may store // references to the recreated frame buffer destroyCommandBuffers(); createCommandBuffers(); buildCommandBuffers(); vkQueueWaitIdle(queue); vkDeviceWaitIdle(device); // Notify derived class windowResized(); viewChanged(); prepared = true; }
void VulkanWindow::FinalizeDepthStencil() { if ( mVkDepthStencilImageView != VK_NULL_HANDLE ) { vkDestroyImageView ( mVulkanRenderer.GetDevice(), mVkDepthStencilImageView, nullptr ); mVkDepthStencilImageView = VK_NULL_HANDLE; } if ( mVkDepthStencilImageMemory != VK_NULL_HANDLE ) { vkFreeMemory ( mVulkanRenderer.GetDevice(), mVkDepthStencilImageMemory, nullptr ); mVkDepthStencilImageMemory = VK_NULL_HANDLE; } if ( mVkDepthStencilImage != VK_NULL_HANDLE ) { vkDestroyImage ( mVulkanRenderer.GetDevice(), mVkDepthStencilImage, nullptr ); mVkDepthStencilImage = VK_NULL_HANDLE; } }
void ModelLoader::CleanupModels(VkDevice device) { for (auto& model : mModelMap) { // Free vertex and index buffers vkDestroyBuffer(device, model.second->vertices.buffer, nullptr); vkFreeMemory(device, model.second->vertices.memory, nullptr); vkDestroyBuffer(device, model.second->indices.buffer, nullptr); vkFreeMemory(device, model.second->indices.memory, nullptr); // Free the texture (NOTE: not sure if this is the right place to delete them, texture loader maybe?) if (model.second->texture != nullptr) { vkDestroyImageView(device, model.second->texture->view, nullptr); // NOTE: Ugly vkDestroyImage(device, model.second->texture->image, nullptr); vkDestroySampler(device, model.second->texture->sampler, nullptr); vkFreeMemory(device, model.second->texture->deviceMemory, nullptr); } delete model.second; } }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class // Clean up texture resources vkDestroyImageView(device, textureArray.view, nullptr); vkDestroyImage(device, textureArray.image, nullptr); vkDestroySampler(device, textureArray.sampler, nullptr); vkFreeMemory(device, textureArray.deviceMemory, nullptr); vkDestroyPipeline(device, pipelines.solid, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkMeshLoader::freeMeshBufferResources(device, &meshes.quad); vkTools::destroyUniformData(device, &uniformData.vertexShader); delete[] uboVS.instance; }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class // Clean up texture resources vkDestroyImageView(device, cubeMap.view, nullptr); vkDestroyImage(device, cubeMap.image, nullptr); vkDestroySampler(device, cubeMap.sampler, nullptr); vkFreeMemory(device, cubeMap.deviceMemory, nullptr); vkDestroyPipeline(device, pipelines.skybox, nullptr); vkDestroyPipeline(device, pipelines.reflect, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkMeshLoader::freeMeshBufferResources(device, &meshes.object); vkMeshLoader::freeMeshBufferResources(device, &meshes.skybox); vkTools::destroyUniformData(device, &uniformData.objectVS); vkTools::destroyUniformData(device, &uniformData.skyboxVS); }
VulkanBase::~VulkanBase() { swapChain.cleanup(); 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); }
std::unique_ptr<StagingTexture2D> StagingTexture2DLinear::Create(STAGING_BUFFER_TYPE type, u32 width, u32 height, VkFormat format) { VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; VkImageCreateInfo create_info = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // VkStructureType sType nullptr, // const void* pNext 0, // VkImageCreateFlags flags VK_IMAGE_TYPE_2D, // VkImageType imageType format, // VkFormat format {width, height, 1}, // VkExtent3D extent 1, // uint32_t mipLevels 1, // uint32_t arrayLayers VK_SAMPLE_COUNT_1_BIT, // VkSampleCountFlagBits samples VK_IMAGE_TILING_LINEAR, // VkImageTiling tiling usage, // VkImageUsageFlags usage VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode sharingMode 0, // uint32_t queueFamilyIndexCount nullptr, // const uint32_t* pQueueFamilyIndices VK_IMAGE_LAYOUT_PREINITIALIZED // VkImageLayout initialLayout }; VkImage image; VkResult res = vkCreateImage(g_vulkan_context->GetDevice(), &create_info, nullptr, &image); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreateImage failed: "); return nullptr; } VkMemoryRequirements memory_requirements; vkGetImageMemoryRequirements(g_vulkan_context->GetDevice(), image, &memory_requirements); bool is_coherent; u32 memory_type_index; if (type == STAGING_BUFFER_TYPE_READBACK) { memory_type_index = g_vulkan_context->GetReadbackMemoryType(memory_requirements.memoryTypeBits, &is_coherent); } else { memory_type_index = g_vulkan_context->GetUploadMemoryType(memory_requirements.memoryTypeBits, &is_coherent); } VkMemoryAllocateInfo memory_allocate_info = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, // VkStructureType sType nullptr, // const void* pNext memory_requirements.size, // VkDeviceSize allocationSize memory_type_index // uint32_t memoryTypeIndex }; VkDeviceMemory memory; res = vkAllocateMemory(g_vulkan_context->GetDevice(), &memory_allocate_info, nullptr, &memory); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkAllocateMemory failed: "); vkDestroyImage(g_vulkan_context->GetDevice(), image, nullptr); return nullptr; } res = vkBindImageMemory(g_vulkan_context->GetDevice(), image, memory, 0); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkBindImageMemory failed: "); vkDestroyImage(g_vulkan_context->GetDevice(), image, nullptr); vkFreeMemory(g_vulkan_context->GetDevice(), memory, nullptr); return nullptr; } // Assume tight packing. Is this correct? u32 stride = width * Util::GetTexelSize(format); return std::make_unique<StagingTexture2DLinear>(type, width, height, format, stride, image, memory, memory_requirements.size, is_coherent); }
void VulkanDeleteList::PerformDeletes(VkDevice device) { for (auto &cmdPool : cmdPools_) { vkDestroyCommandPool(device, cmdPool, nullptr); } cmdPools_.clear(); for (auto &descPool : descPools_) { vkDestroyDescriptorPool(device, descPool, nullptr); } descPools_.clear(); for (auto &module : modules_) { vkDestroyShaderModule(device, module, nullptr); } modules_.clear(); for (auto &buf : buffers_) { vkDestroyBuffer(device, buf, nullptr); } buffers_.clear(); for (auto &bufView : bufferViews_) { vkDestroyBufferView(device, bufView, nullptr); } bufferViews_.clear(); for (auto &image : images_) { vkDestroyImage(device, image, nullptr); } images_.clear(); for (auto &imageView : imageViews_) { vkDestroyImageView(device, imageView, nullptr); } imageViews_.clear(); for (auto &mem : deviceMemory_) { vkFreeMemory(device, mem, nullptr); } deviceMemory_.clear(); for (auto &sampler : samplers_) { vkDestroySampler(device, sampler, nullptr); } samplers_.clear(); for (auto &pipeline : pipelines_) { vkDestroyPipeline(device, pipeline, nullptr); } pipelines_.clear(); for (auto &pcache : pipelineCaches_) { vkDestroyPipelineCache(device, pcache, nullptr); } pipelineCaches_.clear(); for (auto &renderPass : renderPasses_) { vkDestroyRenderPass(device, renderPass, nullptr); } renderPasses_.clear(); for (auto &framebuffer : framebuffers_) { vkDestroyFramebuffer(device, framebuffer, nullptr); } framebuffers_.clear(); for (auto &pipeLayout : pipelineLayouts_) { vkDestroyPipelineLayout(device, pipeLayout, nullptr); } pipelineLayouts_.clear(); for (auto &descSetLayout : descSetLayouts_) { vkDestroyDescriptorSetLayout(device, descSetLayout, nullptr); } descSetLayouts_.clear(); for (auto &callback : callbacks_) { callback.func(callback.userdata); } callbacks_.clear(); }
int sample_main(int argc, char *argv[]) { VkResult U_ASSERT_ONLY res; struct sample_info info = {}; char sample_title[] = "Separate Image Sampler"; const bool depthPresent = true; process_command_line_args(info, argc, argv); 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); init_command_pool(info); init_command_buffer(info); execute_begin_command_buffer(info); init_device_queue(info); init_swap_chain(info); init_depth_buffer(info); init_uniform_buffer(info); init_renderpass(info, depthPresent); init_shaders(info, vertShaderText, fragShaderText); init_framebuffers(info, depthPresent); init_vertex_buffer(info, g_vb_texture_Data, sizeof(g_vb_texture_Data), sizeof(g_vb_texture_Data[0]), true); /* VULKAN_KEY_START */ // Sample from a green texture to easily see that we've pulled correct texel // value // Create our separate image struct texture_object texObj; const char *textureName = "green.ppm"; init_image(info, texObj, textureName); info.textures.push_back(texObj); info.texture_data.image_info.sampler = 0; info.texture_data.image_info.imageView = info.textures[0].view; info.texture_data.image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; // Create our separate sampler VkSampler separateSampler = {}; init_sampler(info, separateSampler); VkDescriptorImageInfo samplerInfo = {}; samplerInfo.sampler = separateSampler; // Set up one descriptor set static const unsigned descriptor_set_count = 1; static const unsigned resource_count = 3; static const unsigned resource_type_count = 3; // Create binding and layout for the following, matching contents of shader // binding 0 = uniform buffer (MVP) // binding 1 = texture2D // binding 2 = sampler VkDescriptorSetLayoutBinding resource_binding[resource_count] = {}; resource_binding[0].binding = 0; resource_binding[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; resource_binding[0].descriptorCount = 1; resource_binding[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT; resource_binding[0].pImmutableSamplers = NULL; resource_binding[1].binding = 1; resource_binding[1].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; resource_binding[1].descriptorCount = 1; resource_binding[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; resource_binding[1].pImmutableSamplers = NULL; resource_binding[2].binding = 2; resource_binding[2].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER; resource_binding[2].descriptorCount = 1; resource_binding[2].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT; resource_binding[2].pImmutableSamplers = NULL; VkDescriptorSetLayoutCreateInfo resource_layout_info[1] = {}; resource_layout_info[0].sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; resource_layout_info[0].pNext = NULL; resource_layout_info[0].bindingCount = resource_count; resource_layout_info[0].pBindings = resource_binding; VkDescriptorSetLayout descriptor_layouts[1] = {}; res = vkCreateDescriptorSetLayout(info.device, resource_layout_info, NULL, &descriptor_layouts[0]); assert(res == VK_SUCCESS); // Create pipeline layout VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo[1] = {}; pipelineLayoutCreateInfo[0].sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipelineLayoutCreateInfo[0].pNext = NULL; pipelineLayoutCreateInfo[0].pushConstantRangeCount = 0; pipelineLayoutCreateInfo[0].pPushConstantRanges = NULL; pipelineLayoutCreateInfo[0].setLayoutCount = descriptor_set_count; pipelineLayoutCreateInfo[0].pSetLayouts = descriptor_layouts; res = vkCreatePipelineLayout(info.device, pipelineLayoutCreateInfo, NULL, &info.pipeline_layout); assert(res == VK_SUCCESS); // Create a single pool to contain data for our descriptor set VkDescriptorPoolSize pool_sizes[resource_type_count] = {}; pool_sizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; pool_sizes[0].descriptorCount = 1; pool_sizes[1].type = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; pool_sizes[1].descriptorCount = 1; pool_sizes[2].type = VK_DESCRIPTOR_TYPE_SAMPLER; pool_sizes[2].descriptorCount = 1; VkDescriptorPoolCreateInfo pool_info[1] = {}; pool_info[0].sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; pool_info[0].pNext = NULL; pool_info[0].maxSets = descriptor_set_count; pool_info[0].poolSizeCount = resource_type_count; pool_info[0].pPoolSizes = pool_sizes; VkDescriptorPool descriptor_pool[1] = {}; res = vkCreateDescriptorPool(info.device, pool_info, NULL, descriptor_pool); assert(res == VK_SUCCESS); VkDescriptorSetAllocateInfo alloc_info[1]; alloc_info[0].sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; alloc_info[0].pNext = NULL; alloc_info[0].descriptorPool = descriptor_pool[0]; alloc_info[0].descriptorSetCount = descriptor_set_count; alloc_info[0].pSetLayouts = descriptor_layouts; // Populate descriptor sets VkDescriptorSet descriptor_sets[descriptor_set_count] = {}; res = vkAllocateDescriptorSets(info.device, alloc_info, descriptor_sets); assert(res == VK_SUCCESS); VkWriteDescriptorSet descriptor_writes[resource_count]; // Populate with info about our uniform buffer for MVP descriptor_writes[0] = {}; descriptor_writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; descriptor_writes[0].pNext = NULL; descriptor_writes[0].dstSet = descriptor_sets[0]; descriptor_writes[0].descriptorCount = 1; descriptor_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; descriptor_writes[0].pBufferInfo = &info.uniform_data.buffer_info; // populated by init_uniform_buffer() descriptor_writes[0].dstArrayElement = 0; descriptor_writes[0].dstBinding = 0; // Populate with info about our image descriptor_writes[1] = {}; descriptor_writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; descriptor_writes[1].pNext = NULL; descriptor_writes[1].dstSet = descriptor_sets[0]; descriptor_writes[1].descriptorCount = 1; descriptor_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; descriptor_writes[1].pImageInfo = &info.texture_data.image_info; // populated by init_texture() descriptor_writes[1].dstArrayElement = 0; descriptor_writes[1].dstBinding = 1; // Populate with info about our sampler descriptor_writes[2] = {}; descriptor_writes[2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; descriptor_writes[2].pNext = NULL; descriptor_writes[2].dstSet = descriptor_sets[0]; descriptor_writes[2].descriptorCount = 1; descriptor_writes[2].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER; descriptor_writes[2].pImageInfo = &samplerInfo; descriptor_writes[2].dstArrayElement = 0; descriptor_writes[2].dstBinding = 2; vkUpdateDescriptorSets(info.device, resource_count, descriptor_writes, 0, NULL); /* VULKAN_KEY_END */ init_pipeline_cache(info); init_pipeline(info, depthPresent); init_presentable_image(info); VkClearValue clear_values[2]; init_clear_color_and_depth(info, clear_values); VkRenderPassBeginInfo rp_begin; init_render_pass_begin_info(info, rp_begin); rp_begin.clearValueCount = 2; rp_begin.pClearValues = clear_values; vkCmdBeginRenderPass(info.cmd, &rp_begin, VK_SUBPASS_CONTENTS_INLINE); vkCmdBindPipeline(info.cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipeline); vkCmdBindDescriptorSets(info.cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipeline_layout, 0, NUM_DESCRIPTOR_SETS, descriptor_sets, 0, NULL); const VkDeviceSize offsets[1] = {0}; vkCmdBindVertexBuffers(info.cmd, 0, 1, &info.vertex_buffer.buf, offsets); init_viewports(info); init_scissors(info); vkCmdDraw(info.cmd, 12 * 3, 1, 0, 0); vkCmdEndRenderPass(info.cmd); res = vkEndCommandBuffer(info.cmd); assert(res == VK_SUCCESS); VkFence drawFence = {}; init_fence(info, drawFence); VkPipelineStageFlags pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; VkSubmitInfo submit_info = {}; init_submit_info(info, submit_info, pipe_stage_flags); /* Queue the command buffer for execution */ res = vkQueueSubmit(info.graphics_queue, 1, &submit_info, drawFence); assert(res == VK_SUCCESS); /* Now present the image in the window */ VkPresentInfoKHR present = {}; init_present_info(info, present); /* Make sure command buffer is finished before presenting */ do { res = vkWaitForFences(info.device, 1, &drawFence, VK_TRUE, FENCE_TIMEOUT); } while (res == VK_TIMEOUT); assert(res == VK_SUCCESS); res = vkQueuePresentKHR(info.present_queue, &present); assert(res == VK_SUCCESS); wait_seconds(1); if (info.save_images) write_ppm(info, "separate_image_sampler"); vkDestroyFence(info.device, drawFence, NULL); vkDestroySemaphore(info.device, info.imageAcquiredSemaphore, NULL); destroy_pipeline(info); destroy_pipeline_cache(info); vkDestroySampler(info.device, separateSampler, NULL); vkDestroyImageView(info.device, info.textures[0].view, NULL); vkDestroyImage(info.device, info.textures[0].image, NULL); vkFreeMemory(info.device, info.textures[0].mem, NULL); // instead of destroy_descriptor_pool(info); vkDestroyDescriptorPool(info.device, descriptor_pool[0], NULL); destroy_vertex_buffer(info); destroy_framebuffers(info); destroy_shaders(info); destroy_renderpass(info); // instead of destroy_descriptor_and_pipeline_layouts(info); for (int i = 0; i < descriptor_set_count; i++) vkDestroyDescriptorSetLayout(info.device, descriptor_layouts[i], NULL); vkDestroyPipelineLayout(info.device, info.pipeline_layout, NULL); destroy_uniform_buffer(info); destroy_depth_buffer(info); destroy_swap_chain(info); destroy_command_buffer(info); destroy_command_pool(info); destroy_device(info); destroy_window(info); destroy_instance(info); return 0; }
std::unique_ptr<Texture2D> Texture2D::Create(u32 width, u32 height, u32 levels, u32 layers, VkFormat format, VkSampleCountFlagBits samples, VkImageViewType view_type, VkImageTiling tiling, VkImageUsageFlags usage) { VkImageCreateInfo image_info = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, nullptr, 0, VK_IMAGE_TYPE_2D, format, {width, height, 1}, levels, layers, samples, tiling, usage, VK_SHARING_MODE_EXCLUSIVE, 0, nullptr, VK_IMAGE_LAYOUT_UNDEFINED}; VkImage image = VK_NULL_HANDLE; VkResult res = vkCreateImage(g_vulkan_context->GetDevice(), &image_info, nullptr, &image); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreateImage failed: "); return nullptr; } // Allocate memory to back this texture, we want device local memory in this case VkMemoryRequirements memory_requirements; vkGetImageMemoryRequirements(g_vulkan_context->GetDevice(), image, &memory_requirements); VkMemoryAllocateInfo memory_info = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, nullptr, memory_requirements.size, g_vulkan_context->GetMemoryType(memory_requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)}; VkDeviceMemory device_memory; res = vkAllocateMemory(g_vulkan_context->GetDevice(), &memory_info, nullptr, &device_memory); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkAllocateMemory failed: "); vkDestroyImage(g_vulkan_context->GetDevice(), image, nullptr); return nullptr; } res = vkBindImageMemory(g_vulkan_context->GetDevice(), image, device_memory, 0); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkBindImageMemory failed: "); vkDestroyImage(g_vulkan_context->GetDevice(), image, nullptr); vkFreeMemory(g_vulkan_context->GetDevice(), device_memory, nullptr); return nullptr; } VkImageViewCreateInfo view_info = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, nullptr, 0, image, view_type, format, {VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY, VK_COMPONENT_SWIZZLE_IDENTITY}, {Util::IsDepthFormat(format) ? static_cast<VkImageAspectFlags>(VK_IMAGE_ASPECT_DEPTH_BIT) : static_cast<VkImageAspectFlags>(VK_IMAGE_ASPECT_COLOR_BIT), 0, levels, 0, layers}}; VkImageView view = VK_NULL_HANDLE; res = vkCreateImageView(g_vulkan_context->GetDevice(), &view_info, nullptr, &view); if (res != VK_SUCCESS) { LOG_VULKAN_ERROR(res, "vkCreateImageView failed: "); vkDestroyImage(g_vulkan_context->GetDevice(), image, nullptr); vkFreeMemory(g_vulkan_context->GetDevice(), device_memory, nullptr); return nullptr; } return std::make_unique<Texture2D>(width, height, levels, layers, format, samples, view_type, image, device_memory, view); }
void CommandBufferManager::DeferImageDestruction(VkImage object) { FrameResources& resources = m_frame_resources[m_current_frame]; resources.cleanup_resources.push_back( [object]() { vkDestroyImage(g_vulkan_context->GetDevice(), object, nullptr); }); }