]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/destroyqueue.cpp
0c3e17c1a1831ecc2ab88534f33b8a0ca8bc7169
[libs/gl.git] / source / backends / vulkan / destroyqueue.cpp
1 #include "destroyqueue.h"
2 #include "device.h"
3 #include "vulkan.h"
4
5 namespace Msp {
6 namespace GL {
7
8 DestroyQueue::DestroyQueue(Device &d):
9         device(d)
10 { }
11
12 DestroyQueue::~DestroyQueue()
13 {
14         while(!queue.empty())
15                 tick();
16 }
17
18 void DestroyQueue::destroy(VkBuffer handle, unsigned mem_id)
19 {
20         destroy<VkBuffer, &VulkanFunctions::DestroyBuffer>(handle, mem_id);
21 }
22
23 void DestroyQueue::destroy(VkFence handle)
24 {
25         destroy<VkFence, &VulkanFunctions::DestroyFence>(handle);
26 }
27
28 void DestroyQueue::destroy(VkFramebuffer handle)
29 {
30         destroy<VkFramebuffer, &VulkanFunctions::DestroyFramebuffer>(handle);
31 }
32
33 void DestroyQueue::destroy(VkImage handle, unsigned mem_id)
34 {
35         destroy<VkImage, &VulkanFunctions::DestroyImage>(handle, mem_id);
36 }
37
38 void DestroyQueue::destroy(VkImageView handle)
39 {
40         destroy<VkImageView, &VulkanFunctions::DestroyImageView>(handle);
41 }
42
43 void DestroyQueue::destroy(VkSampler handle)
44 {
45         destroy<VkSampler, &VulkanFunctions::DestroySampler>(handle);
46 }
47
48 void DestroyQueue::destroy(VkSemaphore handle)
49 {
50         destroy<VkSemaphore, &VulkanFunctions::DestroySemaphore>(handle);
51 }
52
53 template<typename T, void (VulkanFunctions::*destroy_func)(T) const>
54 void DestroyQueue::destroy(T handle, unsigned mem_id)
55 {
56         Entry entry;
57         entry.handle = handle;
58         entry.destroy_func = [](const VulkanFunctions &vk, void *h){ (vk.*destroy_func)(static_cast<T>(h)); };
59         entry.memory_id = mem_id;
60         entry.on_frame = current_frame+MAX_FRAMES_IN_FLIGHT;
61         queue.push_back(entry);
62 }
63
64 void DestroyQueue::tick()
65 {
66         const VulkanFunctions &vk = device.get_functions();
67         MemoryAllocator &allocator = device.get_allocator();
68
69         ++current_frame;
70         while(!queue.empty() && current_frame>=queue.front().on_frame)
71         {
72                 const Entry &e = queue.front();
73                 e.destroy_func(vk, e.handle);
74                 if(e.memory_id)
75                         allocator.release(e.memory_id);
76                 queue.pop_front();
77         }
78 }
79
80 } // namespace GL
81 } // namespace Msp