1 // Copyright 2017 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "dawn_native/vulkan/FencedDeleter.h" 16 17 #include "dawn_native/vulkan/DeviceVk.h" 18 19 namespace dawn_native { namespace vulkan { 20 FencedDeleter(Device * device)21 FencedDeleter::FencedDeleter(Device* device) : mDevice(device) { 22 } 23 ~FencedDeleter()24 FencedDeleter::~FencedDeleter() { 25 ASSERT(mBuffersToDelete.Empty()); 26 ASSERT(mDescriptorPoolsToDelete.Empty()); 27 ASSERT(mFramebuffersToDelete.Empty()); 28 ASSERT(mImagesToDelete.Empty()); 29 ASSERT(mImageViewsToDelete.Empty()); 30 ASSERT(mMemoriesToDelete.Empty()); 31 ASSERT(mPipelinesToDelete.Empty()); 32 ASSERT(mPipelineLayoutsToDelete.Empty()); 33 ASSERT(mQueryPoolsToDelete.Empty()); 34 ASSERT(mRenderPassesToDelete.Empty()); 35 ASSERT(mSamplersToDelete.Empty()); 36 ASSERT(mSemaphoresToDelete.Empty()); 37 ASSERT(mShaderModulesToDelete.Empty()); 38 ASSERT(mSurfacesToDelete.Empty()); 39 ASSERT(mSwapChainsToDelete.Empty()); 40 } 41 DeleteWhenUnused(VkBuffer buffer)42 void FencedDeleter::DeleteWhenUnused(VkBuffer buffer) { 43 mBuffersToDelete.Enqueue(buffer, mDevice->GetPendingCommandSerial()); 44 } 45 DeleteWhenUnused(VkDescriptorPool pool)46 void FencedDeleter::DeleteWhenUnused(VkDescriptorPool pool) { 47 mDescriptorPoolsToDelete.Enqueue(pool, mDevice->GetPendingCommandSerial()); 48 } 49 DeleteWhenUnused(VkDeviceMemory memory)50 void FencedDeleter::DeleteWhenUnused(VkDeviceMemory memory) { 51 mMemoriesToDelete.Enqueue(memory, mDevice->GetPendingCommandSerial()); 52 } 53 DeleteWhenUnused(VkFramebuffer framebuffer)54 void FencedDeleter::DeleteWhenUnused(VkFramebuffer framebuffer) { 55 mFramebuffersToDelete.Enqueue(framebuffer, mDevice->GetPendingCommandSerial()); 56 } 57 DeleteWhenUnused(VkImage image)58 void FencedDeleter::DeleteWhenUnused(VkImage image) { 59 mImagesToDelete.Enqueue(image, mDevice->GetPendingCommandSerial()); 60 } 61 DeleteWhenUnused(VkImageView view)62 void FencedDeleter::DeleteWhenUnused(VkImageView view) { 63 mImageViewsToDelete.Enqueue(view, mDevice->GetPendingCommandSerial()); 64 } 65 DeleteWhenUnused(VkPipeline pipeline)66 void FencedDeleter::DeleteWhenUnused(VkPipeline pipeline) { 67 mPipelinesToDelete.Enqueue(pipeline, mDevice->GetPendingCommandSerial()); 68 } 69 DeleteWhenUnused(VkPipelineLayout layout)70 void FencedDeleter::DeleteWhenUnused(VkPipelineLayout layout) { 71 mPipelineLayoutsToDelete.Enqueue(layout, mDevice->GetPendingCommandSerial()); 72 } 73 DeleteWhenUnused(VkQueryPool querypool)74 void FencedDeleter::DeleteWhenUnused(VkQueryPool querypool) { 75 mQueryPoolsToDelete.Enqueue(querypool, mDevice->GetPendingCommandSerial()); 76 } 77 DeleteWhenUnused(VkRenderPass renderPass)78 void FencedDeleter::DeleteWhenUnused(VkRenderPass renderPass) { 79 mRenderPassesToDelete.Enqueue(renderPass, mDevice->GetPendingCommandSerial()); 80 } 81 DeleteWhenUnused(VkSampler sampler)82 void FencedDeleter::DeleteWhenUnused(VkSampler sampler) { 83 mSamplersToDelete.Enqueue(sampler, mDevice->GetPendingCommandSerial()); 84 } 85 DeleteWhenUnused(VkSemaphore semaphore)86 void FencedDeleter::DeleteWhenUnused(VkSemaphore semaphore) { 87 mSemaphoresToDelete.Enqueue(semaphore, mDevice->GetPendingCommandSerial()); 88 } 89 DeleteWhenUnused(VkShaderModule module)90 void FencedDeleter::DeleteWhenUnused(VkShaderModule module) { 91 mShaderModulesToDelete.Enqueue(module, mDevice->GetPendingCommandSerial()); 92 } 93 DeleteWhenUnused(VkSurfaceKHR surface)94 void FencedDeleter::DeleteWhenUnused(VkSurfaceKHR surface) { 95 mSurfacesToDelete.Enqueue(surface, mDevice->GetPendingCommandSerial()); 96 } 97 DeleteWhenUnused(VkSwapchainKHR swapChain)98 void FencedDeleter::DeleteWhenUnused(VkSwapchainKHR swapChain) { 99 mSwapChainsToDelete.Enqueue(swapChain, mDevice->GetPendingCommandSerial()); 100 } 101 Tick(ExecutionSerial completedSerial)102 void FencedDeleter::Tick(ExecutionSerial completedSerial) { 103 VkDevice vkDevice = mDevice->GetVkDevice(); 104 VkInstance instance = mDevice->GetVkInstance(); 105 106 // Buffers and images must be deleted before memories because it is invalid to free memory 107 // that still have resources bound to it. 108 for (VkBuffer buffer : mBuffersToDelete.IterateUpTo(completedSerial)) { 109 mDevice->fn.DestroyBuffer(vkDevice, buffer, nullptr); 110 } 111 mBuffersToDelete.ClearUpTo(completedSerial); 112 for (VkImage image : mImagesToDelete.IterateUpTo(completedSerial)) { 113 mDevice->fn.DestroyImage(vkDevice, image, nullptr); 114 } 115 mImagesToDelete.ClearUpTo(completedSerial); 116 117 for (VkDeviceMemory memory : mMemoriesToDelete.IterateUpTo(completedSerial)) { 118 mDevice->fn.FreeMemory(vkDevice, memory, nullptr); 119 } 120 mMemoriesToDelete.ClearUpTo(completedSerial); 121 122 for (VkPipelineLayout layout : mPipelineLayoutsToDelete.IterateUpTo(completedSerial)) { 123 mDevice->fn.DestroyPipelineLayout(vkDevice, layout, nullptr); 124 } 125 mPipelineLayoutsToDelete.ClearUpTo(completedSerial); 126 127 for (VkRenderPass renderPass : mRenderPassesToDelete.IterateUpTo(completedSerial)) { 128 mDevice->fn.DestroyRenderPass(vkDevice, renderPass, nullptr); 129 } 130 mRenderPassesToDelete.ClearUpTo(completedSerial); 131 132 for (VkFramebuffer framebuffer : mFramebuffersToDelete.IterateUpTo(completedSerial)) { 133 mDevice->fn.DestroyFramebuffer(vkDevice, framebuffer, nullptr); 134 } 135 mFramebuffersToDelete.ClearUpTo(completedSerial); 136 137 for (VkImageView view : mImageViewsToDelete.IterateUpTo(completedSerial)) { 138 mDevice->fn.DestroyImageView(vkDevice, view, nullptr); 139 } 140 mImageViewsToDelete.ClearUpTo(completedSerial); 141 142 for (VkShaderModule module : mShaderModulesToDelete.IterateUpTo(completedSerial)) { 143 mDevice->fn.DestroyShaderModule(vkDevice, module, nullptr); 144 } 145 mShaderModulesToDelete.ClearUpTo(completedSerial); 146 147 for (VkPipeline pipeline : mPipelinesToDelete.IterateUpTo(completedSerial)) { 148 mDevice->fn.DestroyPipeline(vkDevice, pipeline, nullptr); 149 } 150 mPipelinesToDelete.ClearUpTo(completedSerial); 151 152 // Vulkan swapchains must be destroyed before their corresponding VkSurface 153 for (VkSwapchainKHR swapChain : mSwapChainsToDelete.IterateUpTo(completedSerial)) { 154 mDevice->fn.DestroySwapchainKHR(vkDevice, swapChain, nullptr); 155 } 156 mSwapChainsToDelete.ClearUpTo(completedSerial); 157 for (VkSurfaceKHR surface : mSurfacesToDelete.IterateUpTo(completedSerial)) { 158 mDevice->fn.DestroySurfaceKHR(instance, surface, nullptr); 159 } 160 mSurfacesToDelete.ClearUpTo(completedSerial); 161 162 for (VkSemaphore semaphore : mSemaphoresToDelete.IterateUpTo(completedSerial)) { 163 mDevice->fn.DestroySemaphore(vkDevice, semaphore, nullptr); 164 } 165 mSemaphoresToDelete.ClearUpTo(completedSerial); 166 167 for (VkDescriptorPool pool : mDescriptorPoolsToDelete.IterateUpTo(completedSerial)) { 168 mDevice->fn.DestroyDescriptorPool(vkDevice, pool, nullptr); 169 } 170 mDescriptorPoolsToDelete.ClearUpTo(completedSerial); 171 172 for (VkQueryPool pool : mQueryPoolsToDelete.IterateUpTo(completedSerial)) { 173 mDevice->fn.DestroyQueryPool(vkDevice, pool, nullptr); 174 } 175 mQueryPoolsToDelete.ClearUpTo(completedSerial); 176 177 for (VkSampler sampler : mSamplersToDelete.IterateUpTo(completedSerial)) { 178 mDevice->fn.DestroySampler(vkDevice, sampler, nullptr); 179 } 180 mSamplersToDelete.ClearUpTo(completedSerial); 181 } 182 183 }} // namespace dawn_native::vulkan 184