1 //
2 // Copyright (c) 2022 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #ifndef _vulkan_list_map_hpp_
18 #define _vulkan_list_map_hpp_
19
20 #include <functional>
21 #include "vulkan_wrapper_types.hpp"
22 #include "vulkan_utility.hpp"
23 #include <iostream>
24 template <class VulkanWrapper, class VulkanNative> class VulkanList {
25 protected:
26 std::vector<std::reference_wrapper<VulkanWrapper>> m_wrapperList;
27 std::vector<std::reference_wrapper<const VulkanWrapper>> m_constWrapperList;
28 std::vector<VulkanNative> m_nativeList;
29
30 VulkanList(const VulkanList &list);
31 VulkanList();
32 virtual ~VulkanList();
33 virtual void add(VulkanWrapper &wrapper);
34
35 public:
36 virtual void add(const VulkanWrapper &wrapper);
37 virtual size_t size() const;
38 virtual const VulkanWrapper &operator[](size_t idx) const;
39 virtual VulkanWrapper &operator[](size_t idx);
40 virtual const VulkanNative *operator()() const;
41 };
42
43 template <class VulkanKey, class VulkanValue> class VulkanMap {
44 protected:
45 std::map<VulkanKey, VulkanValue> m_map;
46
47 VulkanMap(const VulkanMap &map);
48 VulkanMap();
49 virtual ~VulkanMap();
50
51 public:
52 void insert(const VulkanKey &key, VulkanValue &value);
53 const VulkanValue &operator[](const VulkanKey &key) const;
54 VulkanValue &operator[](const VulkanKey &key);
55 };
56
57 class VulkanPhysicalDeviceList
58 : public VulkanList<VulkanPhysicalDevice, VkPhysicalDevice> {
59 friend class VulkanInstance;
60
61 protected:
62 VulkanPhysicalDeviceList(
63 const VulkanPhysicalDeviceList &physicalDeviceList);
64
65 public:
66 VulkanPhysicalDeviceList();
67 virtual ~VulkanPhysicalDeviceList();
68 };
69
70 class VulkanQueueFamilyList : public VulkanList<VulkanQueueFamily, uint32_t> {
71 friend class VulkanPhysicalDevice;
72
73 protected:
74 VulkanQueueFamilyList(const VulkanQueueFamilyList &queueFamilyList);
75
76 public:
77 VulkanQueueFamilyList();
78 virtual ~VulkanQueueFamilyList();
79 };
80
81 class VulkanMemoryHeapList : public VulkanList<VulkanMemoryHeap, uint32_t> {
82 friend class VulkanPhysicalDevice;
83
84 protected:
85 VulkanMemoryHeapList(const VulkanMemoryHeapList &memoryHeapList);
86
87 public:
88 VulkanMemoryHeapList();
89 virtual ~VulkanMemoryHeapList();
90 };
91
92 class VulkanMemoryTypeList : public VulkanList<VulkanMemoryType, uint32_t> {
93 friend class VulkanPhysicalDevice;
94 friend class VulkanBuffer;
95 friend class VulkanImage;
96
97 protected:
98 VulkanMemoryTypeList(const VulkanMemoryTypeList &memoryTypeList);
99
100 public:
101 VulkanMemoryTypeList();
102 virtual ~VulkanMemoryTypeList();
103 };
104
105 class VulkanQueueFamilyToQueueCountMap : public VulkanMap<uint32_t, uint32_t> {
106 protected:
107 VulkanQueueFamilyToQueueCountMap(
108 const VulkanQueueFamilyToQueueCountMap &queueFamilyToQueueCountMap);
109
110 public:
111 VulkanQueueFamilyToQueueCountMap(uint32_t numQueuesPerFamily = 0);
112 virtual ~VulkanQueueFamilyToQueueCountMap();
113 };
114
115 class VulkanQueueList : public VulkanList<VulkanQueue, VkQueue> {
116 friend class VulkanDevice;
117
118 protected:
119 VulkanQueueList(const VulkanQueueList &queueList);
120
121 public:
122 VulkanQueueList();
123 virtual ~VulkanQueueList();
124 };
125
126 class VulkanQueueFamilyToQueueListMap
127 : public VulkanMap<uint32_t, std::reference_wrapper<VulkanQueueList>> {
128 protected:
129 VulkanQueueFamilyToQueueListMap(
130 const VulkanQueueFamilyToQueueListMap &queueFamilyToQueueMap);
131
132 public:
133 VulkanQueueFamilyToQueueListMap();
134 virtual ~VulkanQueueFamilyToQueueListMap();
135 void insert(uint32_t key, VulkanQueueList &queueList);
136 VulkanQueueList &operator[](uint32_t key);
137 };
138
139 class VulkanDescriptorSetLayoutBindingList
140 : public VulkanList<VulkanDescriptorSetLayoutBinding,
141 VkDescriptorSetLayoutBinding> {
142 protected:
143 VulkanDescriptorSetLayoutBindingList(
144 const VulkanDescriptorSetLayoutBindingList
145 &descriptorSetLayoutBindingList);
146
147 public:
148 VulkanDescriptorSetLayoutBindingList();
149 VulkanDescriptorSetLayoutBindingList(
150 size_t numDescriptorSetLayoutBindings,
151 VulkanDescriptorType descriptorType, uint32_t descriptorCount = 1,
152 VulkanShaderStage shaderStage = VULKAN_SHADER_STAGE_COMPUTE);
153 VulkanDescriptorSetLayoutBindingList(
154 VulkanDescriptorType descriptorType0, uint32_t descriptorCount0,
155 VulkanDescriptorType descriptorType1, uint32_t descriptorCount1,
156 VulkanShaderStage shaderStage = VULKAN_SHADER_STAGE_COMPUTE);
157 void
158 addBinding(size_t binding, VulkanDescriptorType descriptorType,
159 uint32_t descriptorCount,
160 VulkanShaderStage shaderStage = VULKAN_SHADER_STAGE_COMPUTE);
161 virtual ~VulkanDescriptorSetLayoutBindingList();
162 };
163
164 class VulkanDescriptorSetLayoutList
165 : public VulkanList<VulkanDescriptorSetLayout, VkDescriptorSetLayout> {
166 protected:
167 VulkanDescriptorSetLayoutList(
168 const VulkanDescriptorSetLayoutList &descriptorSetLayoutList);
169
170 public:
171 VulkanDescriptorSetLayoutList();
172 virtual ~VulkanDescriptorSetLayoutList();
173 };
174
175 class VulkanCommandBufferList
176 : public VulkanList<VulkanCommandBuffer, VkCommandBuffer> {
177 protected:
178 VulkanCommandBufferList(const VulkanCommandBufferList &commandBufferList);
179
180 public:
181 VulkanCommandBufferList();
182 VulkanCommandBufferList(size_t numCommandBuffers,
183 const VulkanDevice &device,
184 const VulkanCommandPool &commandPool);
185 virtual ~VulkanCommandBufferList();
186 };
187
188 class VulkanBufferList : public VulkanList<VulkanBuffer, VkBuffer> {
189 protected:
190 VulkanBufferList(const VulkanBufferList &bufferList);
191
192 public:
193 VulkanBufferList(
194 size_t numBuffers, const VulkanDevice &device, uint64_t size,
195 VulkanExternalMemoryHandleType externalMemoryHandleType =
196 VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE,
197 VulkanBufferUsage bufferUsage =
198 VULKAN_BUFFER_USAGE_STORAGE_BUFFER_TRANSFER_SRC_DST,
199 VulkanSharingMode sharingMode = VULKAN_SHARING_MODE_EXCLUSIVE,
200 const VulkanQueueFamilyList &queueFamilyList =
201 getEmptyVulkanQueueFamilyList());
202 virtual ~VulkanBufferList();
203 };
204
205 class VulkanImage2DList : public VulkanList<VulkanImage2D, VkImage> {
206 protected:
207 VulkanImage2DList(const VulkanImage2DList &image2DList);
208
209 public:
210 VulkanImage2DList(
211 size_t numImages, std::vector<VulkanDeviceMemory *> &deviceMemory,
212 uint64_t baseOffset, uint64_t interImageOffset,
213 const VulkanDevice &device, VulkanFormat format, uint32_t width,
214 uint32_t height, uint32_t mipLevels,
215 VulkanImageTiling vulkanImageTiling,
216 VulkanExternalMemoryHandleType externalMemoryHandleType =
217 VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE,
218 VulkanImageCreateFlag imageCreateFlag = VULKAN_IMAGE_CREATE_FLAG_NONE,
219 VulkanImageUsage imageUsage =
220 VULKAN_IMAGE_USAGE_SAMPLED_STORAGE_TRANSFER_SRC_DST,
221 VulkanSharingMode sharingMode = VULKAN_SHARING_MODE_EXCLUSIVE);
222 VulkanImage2DList(
223 size_t numImages, const VulkanDevice &device, VulkanFormat format,
224 uint32_t width, uint32_t height, VulkanImageTiling vulkanImageTiling,
225 uint32_t mipLevels = 1,
226 VulkanExternalMemoryHandleType externalMemoryHandleType =
227 VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE,
228 VulkanImageCreateFlag imageCreateFlag = VULKAN_IMAGE_CREATE_FLAG_NONE,
229 VulkanImageUsage imageUsage =
230 VULKAN_IMAGE_USAGE_SAMPLED_STORAGE_TRANSFER_SRC_DST,
231 VulkanSharingMode sharingMode = VULKAN_SHARING_MODE_EXCLUSIVE);
232 virtual ~VulkanImage2DList();
233 };
234
235 class VulkanImageViewList : public VulkanList<VulkanImageView, VkImageView> {
236 protected:
237 VulkanImageViewList(const VulkanImageViewList &imageViewList);
238
239 public:
240 VulkanImageViewList(const VulkanDevice &device,
241 const VulkanImage2DList &image2DList,
242 bool createImageViewPerMipLevel = true);
243 virtual ~VulkanImageViewList();
244 };
245
246 class VulkanDeviceMemoryList
247 : public VulkanList<VulkanDeviceMemory, VkDeviceMemory> {
248 protected:
249 VulkanDeviceMemoryList(const VulkanDeviceMemoryList &deviceMemoryList);
250
251 public:
252 VulkanDeviceMemoryList(
253 size_t numImages, const VulkanImage2DList &image2DList,
254 const VulkanDevice &device, const VulkanMemoryType &memoryType,
255 VulkanExternalMemoryHandleType externalMemoryHandleType =
256 VULKAN_EXTERNAL_MEMORY_HANDLE_TYPE_NONE);
257 virtual ~VulkanDeviceMemoryList();
258 };
259
260 class VulkanSemaphoreList : public VulkanList<VulkanSemaphore, VkSemaphore> {
261 protected:
262 VulkanSemaphoreList(const VulkanSemaphoreList &semaphoreList);
263
264 public:
265 VulkanSemaphoreList();
266 VulkanSemaphoreList(
267 size_t numSemaphores, const VulkanDevice &device,
268 VulkanExternalSemaphoreHandleType externalSemaphoreHandleType =
269 VULKAN_EXTERNAL_SEMAPHORE_HANDLE_TYPE_NONE,
270 const std::wstring namePrefix = L"");
271 virtual ~VulkanSemaphoreList();
272 };
273
274 ///////////////////////////////
275 // VulkanList implementation //
276 ///////////////////////////////
277
278 template <class VulkanWrapper, class VulkanNative>
VulkanList(const VulkanList & list)279 VulkanList<VulkanWrapper, VulkanNative>::VulkanList(const VulkanList &list)
280 : m_wrapperList(list.m_wrapperList),
281 m_constWrapperList(list.m_constWrapperList),
282 m_nativeList(list.m_nativeList)
283 {}
284
285 template <class VulkanWrapper, class VulkanNative>
VulkanList()286 VulkanList<VulkanWrapper, VulkanNative>::VulkanList()
287 {}
288
289 template <class VulkanWrapper, class VulkanNative>
~VulkanList()290 VulkanList<VulkanWrapper, VulkanNative>::~VulkanList()
291 {}
292
293 template <class VulkanWrapper, class VulkanNative>
add(VulkanWrapper & wrapper)294 void VulkanList<VulkanWrapper, VulkanNative>::add(VulkanWrapper &wrapper)
295 {
296
297 if (m_constWrapperList.size() != size_t(0))
298 {
299 std::cout << "This list can only contain externally allocated objects"
300 << std::endl;
301 return;
302 }
303 m_wrapperList.push_back(std::reference_wrapper<VulkanWrapper>(wrapper));
304 m_nativeList.push_back((VulkanNative)wrapper);
305 }
306
307 template <class VulkanWrapper, class VulkanNative>
add(const VulkanWrapper & wrapper)308 void VulkanList<VulkanWrapper, VulkanNative>::add(const VulkanWrapper &wrapper)
309 {
310 if (m_wrapperList.size() != size_t(0))
311 {
312 std::cout << "This list cannot contain externally allocated objects"
313 << std::endl;
314 return;
315 }
316
317 m_constWrapperList.push_back(
318 std::reference_wrapper<const VulkanWrapper>(wrapper));
319 m_nativeList.push_back((VulkanNative)wrapper);
320 }
321
322 template <class VulkanWrapper, class VulkanNative>
size() const323 size_t VulkanList<VulkanWrapper, VulkanNative>::size() const
324 {
325 return (m_wrapperList.size() > 0) ? m_wrapperList.size()
326 : m_constWrapperList.size();
327 }
328
329 template <class VulkanWrapper, class VulkanNative>
330 const VulkanWrapper &
operator [](size_t idx) const331 VulkanList<VulkanWrapper, VulkanNative>::operator[](size_t idx) const
332 {
333 if (idx < size())
334 {
335 // CHECK_LT(idx, size());
336 return (m_wrapperList.size() > 0) ? m_wrapperList[idx].get()
337 : m_constWrapperList[idx].get();
338 }
339 }
340
341 template <class VulkanWrapper, class VulkanNative>
operator [](size_t idx)342 VulkanWrapper &VulkanList<VulkanWrapper, VulkanNative>::operator[](size_t idx)
343 {
344 // CHECK_LT(idx, m_wrapperList.size());
345 return m_wrapperList[idx].get();
346 }
347
348 template <class VulkanWrapper, class VulkanNative>
operator ()() const349 const VulkanNative *VulkanList<VulkanWrapper, VulkanNative>::operator()() const
350 {
351 return m_nativeList.data();
352 }
353
354 //////////////////////////////
355 // VulkanMap implementation //
356 //////////////////////////////
357
358 template <class VulkanKey, class VulkanValue>
VulkanMap(const VulkanMap & map)359 VulkanMap<VulkanKey, VulkanValue>::VulkanMap(const VulkanMap &map)
360 : m_map(map.m_map)
361 {}
362
363 template <class VulkanKey, class VulkanValue>
VulkanMap()364 VulkanMap<VulkanKey, VulkanValue>::VulkanMap()
365 {}
366
367 template <class VulkanKey, class VulkanValue>
~VulkanMap()368 VulkanMap<VulkanKey, VulkanValue>::~VulkanMap()
369 {}
370
371 template <class VulkanKey, class VulkanValue>
insert(const VulkanKey & key,VulkanValue & value)372 void VulkanMap<VulkanKey, VulkanValue>::insert(const VulkanKey &key,
373 VulkanValue &value)
374 {
375 m_map.insert(std::pair<VulkanKey, std::reference_wrapper<VulkanValue>>(
376 key, std::reference_wrapper<VulkanValue>(value)));
377 }
378
379 template <class VulkanKey, class VulkanValue>
380 const VulkanValue &
operator [](const VulkanKey & key) const381 VulkanMap<VulkanKey, VulkanValue>::operator[](const VulkanKey &key) const
382 {
383 return m_map.at(key);
384 }
385
386 template <class VulkanKey, class VulkanValue>
operator [](const VulkanKey & key)387 VulkanValue &VulkanMap<VulkanKey, VulkanValue>::operator[](const VulkanKey &key)
388 {
389 return m_map.at(key);
390 }
391
392 #endif // _vulkan_list_map_hpp_
393