ngscopeclient 0.1-dev+51fbda87c
QueueManager.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal v0.1 *
4* *
5* Copyright (c) 2012-2022 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
36#ifndef QueueManager_h
37#define QueueManager_h
38
39#include <map>
40#include <memory>
41#include <mutex>
42#include <tuple>
43#include <vector>
44#include <vulkan/vulkan_raii.hpp>
45
46class QueueLock;
47
53{
54public:
55 QueueHandle(std::shared_ptr<vk::raii::Device> device, size_t family, size_t index, std::string name);
57
59 void AddName(std::string name);
60
62 void Submit(vk::raii::CommandBuffer const& cmdBuf);
64 void SubmitAndBlock(vk::raii::CommandBuffer const& cmdBuf);
65
66 const std::string& GetName() const
67 { return m_name; }
68
69public:
70 //non-copyable
71 QueueHandle(QueueHandle const&) = delete;
72 QueueHandle& operator=(QueueHandle const&) = delete;
73
74protected:
77 void _waitFence();
78
79public:
80 const size_t m_family;
81 const size_t m_index;
82
83protected:
84 friend QueueLock;
85 std::recursive_mutex m_mutex;
86 std::string m_name;
87 std::shared_ptr<vk::raii::Device> m_device;
88 std::unique_ptr<vk::raii::Queue> m_queue;
89 std::unique_ptr<vk::raii::Fence> m_fence;
90};
91
92
100{
101public:
102 QueueLock(std::shared_ptr<QueueHandle> handle)
103 : m_lock(handle->m_mutex)
104 , m_handle(handle)
105 { handle->_waitFence(); }
106
107 vk::raii::Queue& operator*()
108 { return *(m_handle->m_queue); }
109
110public:
111 //non-copyable
112 QueueLock(QueueLock const&) = delete;
113 QueueLock& operator=(QueueLock const&) = delete;
114
115protected:
116 const std::lock_guard<std::recursive_mutex> m_lock;
117 std::shared_ptr<QueueHandle> m_handle;
118};
119
120
129{
130public:
131 QueueManager(vk::raii::PhysicalDevice* phys, std::shared_ptr<vk::raii::Device> device);
132
134 std::shared_ptr<QueueHandle> GetComputeQueue(std::string name)
135 { return GetQueueWithFlags(vk::QueueFlagBits::eCompute | vk::QueueFlagBits::eTransfer, name); }
136
139 std::shared_ptr<QueueHandle> GetRenderQueue(std::string name)
140 { return GetQueueWithFlags(vk::QueueFlagBits::eGraphics | vk::QueueFlagBits::eTransfer, name); }
141
144 std::shared_ptr<QueueHandle> GetTransferQueue(std::string name)
145 { return GetQueueWithFlags(vk::QueueFlagBits::eCompute | vk::QueueFlagBits::eTransfer, name); }
146
149 std::shared_ptr<QueueHandle> GetQueueWithFlags(vk::QueueFlags flags, std::string name);
150
151public:
152 //non-copyable
153 QueueManager(QueueManager const&) = delete;
154 QueueManager& operator=(QueueManager const&) = delete;
155
156protected:
157 vk::raii::PhysicalDevice* m_phys;
158 std::shared_ptr<vk::raii::Device> m_device;
159
161 std::mutex m_mutex;
162
164 {
165 size_t Family;
166 size_t Index;
167 vk::QueueFlags Flags;
168 std::shared_ptr<QueueHandle> Handle;
169 };
170
172 std::vector<QueueInfo> m_queues;
173};
174
175#endif
Wrapper around a Vulkan Queue, protected by mutex for thread safety.
Definition: QueueManager.h:53
void Submit(vk::raii::CommandBuffer const &cmdBuf)
Submit the given command buffer on the queue.
Definition: QueueManager.cpp:85
void SubmitAndBlock(vk::raii::CommandBuffer const &cmdBuf)
Submit the given command buffer on the queue and wait until completion.
Definition: QueueManager.cpp:104
void AddName(std::string name)
Append a name to the queue, used for debugging.
Definition: QueueManager.cpp:67
void _waitFence()
Definition: QueueManager.cpp:124
Obtains exclusive access to a Vulkan Queue for the duration of its existence, similar to a std::lock_...
Definition: QueueManager.h:100
Allocates and hands out std::shared_ptr<QueueHandle> instances for thread-safe access to Vulkan Queue...
Definition: QueueManager.h:129
std::shared_ptr< QueueHandle > GetTransferQueue(std::string name)
Definition: QueueManager.h:144
std::shared_ptr< QueueHandle > GetRenderQueue(std::string name)
Definition: QueueManager.h:139
std::vector< QueueInfo > m_queues
All queues available on the device.
Definition: QueueManager.h:172
std::shared_ptr< QueueHandle > GetComputeQueue(std::string name)
Get a handle to a compute queue.
Definition: QueueManager.h:134
std::mutex m_mutex
Mutex to guard allocations.
Definition: QueueManager.h:161
std::shared_ptr< QueueHandle > GetQueueWithFlags(vk::QueueFlags flags, std::string name)
Definition: QueueManager.cpp:170
Definition: QueueManager.h:164