]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/destroyqueue.cpp
Initial implementation of Vulkan backend
[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(VkImageView handle)
34 {
35         destroy<VkImageView, &VulkanFunctions::DestroyImageView>(handle);
36 }
37
38 void DestroyQueue::destroy(VkSemaphore handle)
39 {
40         destroy<VkSemaphore, &VulkanFunctions::DestroySemaphore>(handle);
41 }
42
43 template<typename T, void (VulkanFunctions::*destroy_func)(T) const>
44 void DestroyQueue::destroy(T handle, unsigned mem_id)
45 {
46         Entry entry;
47         entry.handle = handle;
48         entry.destroy_func = [](const VulkanFunctions &vk, void *h){ (vk.*destroy_func)(static_cast<T>(h)); };
49         entry.memory_id = mem_id;
50         entry.on_frame = current_frame+MAX_FRAMES_IN_FLIGHT;
51         queue.push_back(entry);
52 }
53
54 void DestroyQueue::tick()
55 {
56         const VulkanFunctions &vk = device.get_functions();
57         MemoryAllocator &allocator = device.get_allocator();
58
59         ++current_frame;
60         while(!queue.empty() && current_frame>=queue.front().on_frame)
61         {
62                 const Entry &e = queue.front();
63                 e.destroy_func(vk, e.handle);
64                 if(e.memory_id)
65                         allocator.release(e.memory_id);
66                 queue.pop_front();
67         }
68 }
69
70 } // namespace GL
71 } // namespace Msp