context.cpp 21.8 KB
Newer Older
1
2
3
4
#include <SDL2/SDL.h>
#include <SDL2/SDL_vulkan.h>

#include <sf2/sf2.hpp> // has to be first so he sf2_struct define is set
5
6
7
8
9
10
11
12
13
14
15

#include <mirrage/graphic/context.hpp>
#include <mirrage/graphic/device.hpp>
#include <mirrage/graphic/window.hpp>

#include <mirrage/asset/asset_manager.hpp>
#include <mirrage/utils/log.hpp>
#include <mirrage/utils/template_utils.hpp>

#include <gsl/gsl>

16
#include <cstdio>
17
18
#include <iostream>
#include <sstream>
19
#include <unordered_set>
20
21
22


extern "C" {
23
24
25
VkResult vkCreateDebugReportCallbackEXT(VkInstance                                instance,
                                        const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,
                                        const VkAllocationCallbacks*              pAllocator,
26
27
                                        VkDebugReportCallbackEXT*                 pCallback)
{
Florian Oetke's avatar
Florian Oetke committed
28
29
	auto func = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(
	        vkGetInstanceProcAddr(instance, "vkCreateDebugReportCallbackEXT"));
30
31
32
33
	if(func != nullptr) {
		return func(instance, pCreateInfo, pAllocator, pCallback);
	} else {
		return VK_ERROR_EXTENSION_NOT_PRESENT;
34
	}
35
}
36

37
38
void vkDestroyDebugReportCallbackEXT(VkInstance                   instance,
                                     VkDebugReportCallbackEXT     callback,
39
40
                                     const VkAllocationCallbacks* pAllocator)
{
Florian Oetke's avatar
Florian Oetke committed
41
42
	auto func = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(
	        vkGetInstanceProcAddr(instance, "vkDestroyDebugReportCallbackEXT"));
43
44
	if(func != nullptr) {
		func(instance, callback, pAllocator);
45
46
	}
}
47
}
48

49
namespace mirrage::graphic {
50
51
52
53

	using namespace util::unit_literals;

	namespace {
54
55
		void sdl_error_check()
		{
56
			const char* err = SDL_GetError();
57
58
59
			if(*err != '\0') {
				std::string errorStr(err);
				SDL_ClearError();
60
				MIRRAGE_FAIL("SDL: " << errorStr);
61
62
63
			}
		}

64
65
		void add_presnet_extensions(std::vector<const char*>& extensions, SDL_Window* window)
		{
66
67
68
69
70
71
72
73
74
			auto count = static_cast<unsigned int>(0);
			if(!SDL_Vulkan_GetInstanceExtensions(window, &count, nullptr)) {
				MIRRAGE_FAIL("Unable to determine present extensions: " << SDL_GetError());
			}

			auto begin = extensions.size();
			extensions.resize(begin + count);

			if(!SDL_Vulkan_GetInstanceExtensions(window, &count, extensions.data() + begin)) {
75
				MIRRAGE_FAIL("Unable to determine present extensions: " << SDL_GetError());
76
			}
77
78
79
		}

		void add_present_extensions(std::vector<const char*>&                          extensions,
80
81
		                            const std::unordered_map<std::string, Window_ptr>& windows)
		{
82

83
			extensions.reserve(extensions.size() + windows.size() * 4);
84

85
			for(auto&& [_, window] : windows) {
86
87
				(void) _;
				add_presnet_extensions(extensions, window->window_handle());
88
89
90
			}
		}

91
92
		void sort_and_unique(std::vector<const char*>& extensions)
		{
93
94
95
			std::sort(extensions.begin(), extensions.end(), [](auto lhs, auto rhs) {
				return std::strcmp(lhs, rhs) < 0;
			});
96
97
98
			auto new_end = std::unique(extensions.begin(), extensions.end(), [](auto lhs, auto rhs) {
				return std::strcmp(lhs, rhs) == 0;
			});
99
100
101
102

			extensions.erase(new_end, extensions.end());
		}

103
		auto check_extensions(const std::vector<const char*>& required,
104
105
		                      const std::vector<const char*>& optional) -> std::vector<const char*>
		{
106
107
108
109
110
111
112
113
			auto extensions = std::vector<const char*>();
			extensions.reserve(required.size() + optional.size());

			auto supported_extensions = vk::enumerateInstanceExtensionProperties();

			auto support_confirmed = std::vector<bool>(required.size());

			for(auto& e : supported_extensions) {
114
				for(auto i = 0u; i < required.size(); i++) {
115
116
117
118
119
120
121
					if(!strcmp(e.extensionName, required[i])) {
						support_confirmed[i] = true;
						extensions.push_back(required[i]);
						break;
					}
				}

122
				for(auto i = 0u; i < optional.size(); i++) {
123
124
125
126
127
128
129
130
					if(!strcmp(e.extensionName, optional[i])) {
						extensions.push_back(optional[i]);
						break;
					}
				}
			}

			bool all_supported = true;
131
			for(auto i = 0u; i < support_confirmed.size(); i++) {
132
				if(!support_confirmed[i]) {
133
					LOG(plog::warning) << "Unsupported extension \"" << required[i] << "\"!";
134
135
136
137
138
					all_supported = false;
				}
			}

			if(!all_supported) {
139
				MIRRAGE_FAIL("At least one required extension is not supported (see log for details)!");
140
141
142
143
144
			}

			return extensions;
		}

145
146
		auto check_layers(const std::vector<const char*>& requested) -> std::vector<const char*>
		{
147
148
149
150
151
152
153
			auto validation_layers = std::vector<const char*>();
			validation_layers.reserve(requested.size());

			auto supported_layers = vk::enumerateInstanceLayerProperties();
			for(auto& l : supported_layers) {
				bool layer_requested = false;
				for(auto& req_layer : requested) {
154
					if(0 == strcmp(l.layerName, req_layer)) {
155
156
157
158
159
160
161
						validation_layers.push_back(req_layer);
						layer_requested = true;
						break;
					}
				}

				if(!layer_requested) {
162
163
164
					LOG(plog::debug) << "Additional validation layer is available, that hasn't been "
					                    "requested: "
					                 << l.layerName;
165
166
167
168
				}
			}

			if(validation_layers.size() != requested.size()) {
169
170
				IF_LOG(plog::error)
				{
171
172
173
174
175
176
177
					auto msg = std::stringstream{};
					msg << "Some requested validation layers are not supported: \n";
					for(auto& l : validation_layers) {
						msg << "  - " << l << "\n";
					}

					LOG(plog::error) << msg.str();
178
179
180
181
182
183
				}
			}

			return validation_layers;
		}

184
185
186
187
188
189
190
		VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(VkDebugReportFlagsEXT      flags,
		                                             VkDebugReportObjectTypeEXT objType,
		                                             uint64_t                   obj,
		                                             size_t                     location,
		                                             int32_t                    code,
		                                             const char*                layerPrefix,
		                                             const char*                msg,
191
192
		                                             void*                      userData)
		{
193
194
195
196

			// silences: DescriptorSet 0x3f previously bound as set #1 is incompatible with set
			//             0x1cc99c0 newly bound as set #1 so set #2 and any subsequent sets were
			//             disturbed by newly bound pipelineLayout (0x4f)
197
			if(objType == VK_DEBUG_REPORT_OBJECT_TYPE_DESCRIPTOR_SET_EXT)
198
199
200
201
				return VK_FALSE;


			if(flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) {
202
				LOG(plog::error) << "[VK | " << layerPrefix << "] " << msg;
203
204
			} else if(flags & VK_DEBUG_REPORT_WARNING_BIT_EXT
			          || flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) {
205
				LOG(plog::warning) << "[VK | " << layerPrefix << "] " << msg;
206
			} else {
207
				LOG(plog::info) << "[VK | " << layerPrefix << "] " << msg;
208
209
210
211
			}

			return VK_FALSE;
		}
212
	} // namespace
213
214


215
216
217
218
219
220
	Context::Context(const std::string&    appName,
	                 uint32_t              appVersion,
	                 const std::string&    engineName,
	                 uint32_t              engineVersion,
	                 bool                  debug,
	                 asset::Asset_manager& assets)
221
222
	  : _assets(assets), _name(appName)
	{
223
224
225
226

		auto maybe_settings = assets.load_maybe<Graphics_settings>("cfg:graphics"_aid);
		if(maybe_settings.is_nothing()) {
			if(!settings(default_settings())) {
227
				MIRRAGE_FAIL("Invalid graphics settings");
228
229
			}
		} else {
230
			_settings = maybe_settings.get_or_throw();
231
232
233
		}

		if(!settings(*_settings)) { //< apply actual size/settings
234
			MIRRAGE_FAIL("Couldn't apply graphics settings");
235
236
237
238
		}

		sdl_error_check();

239
		for(auto&& [title, win] : _settings->windows) {
240
241
242
243
244
245
246
247
248
			_windows.emplace(title,
			                 std::make_unique<Window>(title,
			                                          app_name() + " | " + title,
			                                          win.display,
			                                          win.width,
			                                          win.height,
			                                          win.fullscreen));
		}

249
		auto instanceCreateInfo = vk::InstanceCreateInfo{};
250
		auto appInfo            = vk::ApplicationInfo{
251
                appName.c_str(), appVersion, engineName.c_str(), engineVersion, VK_API_VERSION_1_0};
252

253
		auto required_extensions = std::vector<const char*>{VK_KHR_SURFACE_EXTENSION_NAME};
254
		add_present_extensions(required_extensions, _windows);
255
		auto optional_extensions = std::vector<const char*>{};
256
257
258


		if(debug) {
259
			_enabled_layers = check_layers({"VK_LAYER_LUNARG_standard_validation"});
260
261
262
263

			required_extensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
		}

264
265
		sort_and_unique(required_extensions);
		sort_and_unique(optional_extensions);
266
267
		auto extensions = check_extensions(required_extensions, optional_extensions);

268
269
		IF_LOG(plog::info)
		{
270
271
272
273
274
275
276
277
278
279
280
281
			auto msg = std::stringstream{};
			msg << "Initializing vulkan...\n";
			msg << "Enabled extensions:\n";
			for(auto e : extensions) {
				msg << "  - " << e << "\n";
			}
			msg << "Enabled validation layers:\n";
			for(auto l : _enabled_layers) {
				msg << "  - " << l << "\n";
			}

			LOG(plog::info) << msg.str();
282
283
284
285
286
287
288
289
290
291
		}

		instanceCreateInfo.setPApplicationInfo(&appInfo);
		instanceCreateInfo.setEnabledExtensionCount(gsl::narrow<uint32_t>(extensions.size()));
		instanceCreateInfo.setPpEnabledExtensionNames(extensions.data());
		instanceCreateInfo.setEnabledLayerCount(gsl::narrow<uint32_t>(_enabled_layers.size()));
		instanceCreateInfo.setPpEnabledLayerNames(_enabled_layers.data());
		_instance = vk::createInstanceUnique(instanceCreateInfo);

		if(debug) {
292
293
			_debug_callback = _instance->createDebugReportCallbackEXTUnique(
			        {/*	vk::DebugReportFlagBitsEXT::eDebug | vk::DebugReportFlagBitsEXT::eInformation | */
294
			         vk::DebugReportFlagBitsEXT::eError | vk::DebugReportFlagBitsEXT::ePerformanceWarning
295
296
			                 | vk::DebugReportFlagBitsEXT::eWarning,
			         debugCallback});
297
		}
298

299
		for(auto&& [_, window] : _windows) {
300
301
302
			(void) _;
			window->create_surface(*this);
		}
303
304
305
	}
	Context::~Context() = default;

306
307
	bool Context::settings(Graphics_settings new_settings)
	{
308
		_assets.save<Graphics_settings>("cfg:graphics"_aid, new_settings);
309
		_settings = _assets.load<Graphics_settings>("cfg:graphics"_aid);
310
311
312
313

		return true;
	}

314
315
	auto Context::find_window(std::string name) -> util::maybe<Window&>
	{
316
317
		auto iter = _windows.find(name);
		return iter == _windows.end() ? util::nothing : util::justPtr(&*iter->second);
318
	}
319
320
	auto Context::_find_window_settings(const std::string& name, int width, int height) -> Window_settings
	{
321
322
		auto& cfg = settings();

323
324
		auto win_cfg = std::find_if(
		        std::begin(cfg.windows), std::end(cfg.windows), [&](auto& w) { return w.first == name; });
325

326
		if(win_cfg == std::end(cfg.windows)) { // no config create new
327
328
			auto new_settings = cfg;
			auto win_settings = default_window_settings(0);
329
330
331
			if(width > 0)
				win_settings.width = width;
			if(height > 0)
332
				win_settings.height = height;
333
334
335
336
337
338
339
340
341
342
343
			new_settings.windows[name] = win_settings;

			settings(new_settings);

			return win_settings;
		}

		return win_cfg->second;
	}

	namespace {
344
345
		bool supports_present(vk::PhysicalDevice& gpu, const std::vector<Window*>& can_present_to)
		{
346
			auto supported_extensions = gpu.enumerateDeviceExtensionProperties();
347
348
349
			auto sc_ext = std::find_if(supported_extensions.begin(), supported_extensions.end(), [](auto& e) {
				return std::strcmp(e.extensionName, VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0;
			});
350

351
			if(sc_ext == supported_extensions.end()) {
352
353
354
355
356
357
358
359
360
361
362
363
364
				return false;
			}

			for(auto window : can_present_to) {
				auto formats       = gpu.getSurfaceFormatsKHR(window->surface());
				auto present_modes = gpu.getSurfacePresentModesKHR(window->surface());

				if(formats.empty() || present_modes.empty())
					return false;
			}

			return true;
		}
365

366
		auto find_graphics_queue(vk::PhysicalDevice& gpu, const std::vector<Window*>& can_present_to)
367
368
		        -> util::maybe<std::uint32_t>
		{
369
			auto i = 0;
370
371

			for(auto& queue_family : gpu.getQueueFamilyProperties()) {
372
373
				auto can_present =
				        can_present_to.empty()
374
375
376
				        || std::all_of(can_present_to.begin(), can_present_to.end(), [&](auto window) {
					           return gpu.getSurfaceSupportKHR(i, window->surface());
				           });
377
378
379
380

				if(queue_family.queueCount > 0 && can_present
				   && queue_family.timestampValidBits
				              >= 32 // only accept queues with >=32bit timer support, for now
381
382
383
384
385
386
387
388
389
390
				   && (queue_family.queueFlags & vk::QueueFlagBits::eGraphics)) {
					return i;
				}

				i++;
			}

			return util::nothing;
		}

391
392
		auto find_transfer_queue(vk::PhysicalDevice& gpu) -> std::uint32_t
		{
393
			auto families = gpu.getQueueFamilyProperties();
394

395
			auto i = 0;
396

397
398
			// check for transfer-only queue
			for(auto& queue_family : families) {
399
				if(queue_family.queueCount > 0 && (queue_family.queueFlags & vk::QueueFlagBits::eTransfer)
400
				   && !(queue_family.queueFlags & vk::QueueFlagBits::eGraphics)) {
401
402
403
404
405
					return i;
				}

				i++;
			}
406
407

			i = 0;
408
			for(auto& queue_family : families) {
409
				if(queue_family.queueCount > 0
410
				   && ((queue_family.queueFlags & vk::QueueFlagBits::eTransfer)
411
412
				       || (queue_family.queueFlags & vk::QueueFlagBits::eGraphics)
				       || (queue_family.queueFlags & vk::QueueFlagBits::eCompute))) {
413
414
415
416
417
					return i;
				}

				i++;
			}
418

419
			MIRRAGE_FAIL("No queue found, that supports transfer operations!");
420
421
		}

422
423
424
		auto find_surface_format(vk::PhysicalDevice& gpu,
		                         Window&             window,
		                         vk::Format          target_format,
425
426
		                         vk::ColorSpaceKHR   target_space) -> vk::SurfaceFormatKHR
		{
427
428

			auto formats = gpu.getSurfaceFormatsKHR(window.surface());
429
			if(formats.size() == 1 && formats.front().format == vk::Format::eUndefined) {
430
431
432
433
				auto surface_format       = vk::SurfaceFormatKHR{};
				surface_format.format     = target_format;
				surface_format.colorSpace = target_space;
				return surface_format;
434
435
436
			}

			auto opt_found = std::find_if(formats.begin(), formats.end(), [&](auto& f) {
437
				return f.format == target_format && f.colorSpace == target_space;
438
			});
439
			if(opt_found != formats.end()) {
440
441
442
443
				auto surface_format       = vk::SurfaceFormatKHR{};
				surface_format.format     = target_format;
				surface_format.colorSpace = target_space;
				return surface_format;
444
445
			}

446
447
			opt_found = std::find_if(
			        formats.begin(), formats.end(), [&](auto& f) { return f.format == target_format; });
448
			if(opt_found != formats.end()) {
449
450
451
452
				return *opt_found;
			}

			if(!formats.empty()) {
453
454
				LOG(plog::warning) << "Requested format is not supported by the device, fallback to first "
				                      "reported format";
455
456
				return formats.front();
			} else {
457
				MIRRAGE_FAIL("The device doesn't support any surface formats!");
458
459
460
			}
		}

461
462
		auto create_swapchain_create_info(vk::PhysicalDevice          gpu,
		                                  bool                        srgb,
463
464
		                                  const std::vector<Window*>& can_present_to)
		{
465
			auto swapchains = Swapchain_create_infos();
466

467
468
			for(auto window : can_present_to) {
				auto capabilities = gpu.getSurfaceCapabilitiesKHR(window->surface());
469
470


471
472
473
474
475
				auto sc_info = vk::SwapchainCreateInfoKHR{};
				sc_info.setSurface(window->surface());
				sc_info.setClipped(true);
				sc_info.setPreTransform(capabilities.currentTransform);
				sc_info.setImageSharingMode(vk::SharingMode::eExclusive);
476
477
				sc_info.setImageUsage(vk::ImageUsageFlagBits::eColorAttachment
				                      | vk::ImageUsageFlagBits::eTransferDst);
478
				sc_info.setImageArrayLayers(1);
479
				sc_info.setMinImageCount(std::max(3u, capabilities.minImageCount));
480
				if(capabilities.maxImageCount > 0 && capabilities.maxImageCount < sc_info.minImageCount) {
481
482
					sc_info.setMinImageCount(capabilities.maxImageCount);
				}
483

484
485
486
487
				auto present_modes = gpu.getSurfacePresentModesKHR(window->surface());
				auto mailbox_supported =
				        std::find(present_modes.begin(), present_modes.end(), vk::PresentModeKHR::eMailbox)
				        != present_modes.end();
488
489
490
491
492
				if(mailbox_supported) {
					sc_info.setPresentMode(vk::PresentModeKHR::eMailbox);
				} else {
					sc_info.setPresentMode(vk::PresentModeKHR::eFifo);
				}
493

494
				if(capabilities.currentExtent.width == std::numeric_limits<uint32_t>::max()) {
495
496
497
498
499
500
501
502
503
					capabilities.currentExtent.width =
					        std::min(std::max(gsl::narrow<std::uint32_t>(window->width()),
					                          capabilities.minImageExtent.width),
					                 capabilities.maxImageExtent.width);

					capabilities.currentExtent.height =
					        std::min(std::max(gsl::narrow<std::uint32_t>(window->height()),
					                          capabilities.minImageExtent.height),
					                 capabilities.maxImageExtent.height);
504
				}
505

506
				sc_info.setImageExtent(capabilities.currentExtent);
507

508
509
510
511
512
				auto format =
				        find_surface_format(gpu,
				                            *window,
				                            srgb ? vk::Format::eB8G8R8A8Srgb : vk::Format::eB8G8R8A8Unorm,
				                            vk::ColorSpaceKHR::eSrgbNonlinear);
513

514
515
				sc_info.setImageFormat(format.format);
				sc_info.setImageColorSpace(format.colorSpace);
516

517
518
				swapchains.emplace(window->name(), std::make_tuple(window, sc_info));
			}
519

520
521
			return swapchains;
		}
522
	} // namespace
523
524
525
	auto Context::instantiate_device(Device_selector             selector,
	                                 Device_factory              factory,
	                                 const std::vector<Window*>& can_present_to,
526
527
	                                 bool                        srgb) -> Device_ptr
	{
528
529
530
531
532
533
534
		auto top_score = std::numeric_limits<int>::min();
		auto top_gpu   = vk::PhysicalDevice{};

		for(auto& gpu : _instance->enumeratePhysicalDevices()) {
			if(!can_present_to.empty() && !supports_present(gpu, can_present_to)) {
				continue;
			}
535

536
537
538
			auto graphics_queue = find_graphics_queue(gpu, can_present_to);

			auto score = selector(gpu, graphics_queue);
Florian Oetke's avatar
Florian Oetke committed
539
540

			auto gpu_name = std::string(gpu.getProperties().deviceName);
541
			LOG(plog::info) << "Detected GPU: " << gpu_name;
Florian Oetke's avatar
Florian Oetke committed
542
543
544
545
546

			if(!_settings->gpu_preference.empty() && _settings->gpu_preference == gpu_name) {
				score = std::numeric_limits<int>::max();
			}

547
			if(score > top_score) {
548
				top_score = score;
549
				top_gpu   = gpu;
550
551
552
553
			}
		}

		if(!top_gpu) {
554
			MIRRAGE_FAIL("Couldn't find a GPU that supports vulkan and all required features.");
555
556
		}

557
		LOG(plog::info) << "Selected GPU: " << top_gpu.getProperties().deviceName;
Florian Oetke's avatar
Florian Oetke committed
558

559
560
561
562
		auto cfg = vk::DeviceCreateInfo{};
		cfg.setEnabledLayerCount(gsl::narrow<uint32_t>(_enabled_layers.size()));
		cfg.setPpEnabledLayerNames(_enabled_layers.data());

563
		auto extensions           = std::vector<const char*>();
564
		auto supported_extensions = top_gpu.enumerateDeviceExtensionProperties();
565
		auto extension_supported  = [&](const char* e) {
566
            return supported_extensions.end()
567
568
569
                   != std::find_if(supported_extensions.begin(), supported_extensions.end(), [&](auto& se) {
                          return !strcmp(se.extensionName, e);
                      });
570
		};
571

572
		auto dedicated_alloc_supported = false;
573

574
575
576
577
578
579
#ifdef VK_NV_dedicated_allocation
		if(extension_supported(VK_NV_DEDICATED_ALLOCATION_EXTENSION_NAME)) {
			extensions.emplace_back(VK_NV_DEDICATED_ALLOCATION_EXTENSION_NAME);
			dedicated_alloc_supported = true;
		}
#endif
580

581
582
583
		if(!can_present_to.empty()) {
			extensions.emplace_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
		}
584

585
586
		cfg.setEnabledExtensionCount(extensions.size());
		cfg.setPpEnabledExtensionNames(extensions.data());
587

588
589
590
		auto create_info = factory(top_gpu, find_graphics_queue(top_gpu, can_present_to));

		cfg.setPEnabledFeatures(&create_info.features);
591

592
		// familyId => (count, priorities)
593
594
595
		auto queue_families =
		        std::unordered_map<std::uint32_t, std::tuple<std::uint32_t, std::vector<float>>>();

596
		auto available_families = top_gpu.getQueueFamilyProperties();
597

598
599
600
		auto queue_mapping = Queue_family_mapping();

		auto transfer_queue_tag = "_transfer"_strid;
601
602
		create_info.queue_families.emplace(transfer_queue_tag,
		                                   Queue_create_info{find_transfer_queue(top_gpu), 0.2f});
603

604
605
		auto draw_queue_tag = util::maybe<util::Str_id>(util::nothing);

606
		for(auto& qf : create_info.queue_families) {
607
608
			auto tag      = qf.first;
			auto family   = qf.second.family_id;
609
			auto priority = qf.second.priority;
610

611
			auto& entry = queue_families[family];
612

613
614
615
616
617
618
			if(available_families[family].queueFlags & vk::QueueFlagBits::eGraphics) {
				draw_queue_tag = qf.first;
			} else if(draw_queue_tag.is_nothing()
			          && (available_families[family].queueFlags & vk::QueueFlagBits::eCompute))
				draw_queue_tag = qf.first;

619
620
621
622
623
			if(available_families[family].queueCount > 0) {
				available_families[family].queueCount--;
				std::get<0>(entry) += 1;
				std::get<1>(entry).emplace_back(priority);
			} else {
624
625
				LOG(plog::warning) << "More queues requested than are availbalbe from family " << family
				                   << ". Collapsed with previous queue!";
626
627
			}

628
			queue_mapping.emplace(tag, std::make_tuple(family, std::get<1>(entry).size() - 1));
629
630
631
632
633
		}

		auto used_queues = std::vector<vk::DeviceQueueCreateInfo>{};
		used_queues.reserve(queue_families.size());
		for(auto& qf : queue_families) {
634
			MIRRAGE_INVARIANT(std::get<1>(qf.second).size() == std::get<0>(qf.second), "Size mismatch");
635

636
637
638
639
640
			used_queues.emplace_back(vk::DeviceQueueCreateFlags(),
			                         qf.first,
			                         std::get<0>(qf.second),
			                         std::get<1>(qf.second).data());
		}
641

642
643
		cfg.setQueueCreateInfoCount(gsl::narrow<std::uint32_t>(used_queues.size()));
		cfg.setPQueueCreateInfos(used_queues.data());
644

645
646
		auto swapchains = create_swapchain_create_info(top_gpu, srgb, can_present_to);

647
648
649
650
651
652
653
654
655
656
		return std::make_unique<Device>(
		        *this,
		        _assets,
		        top_gpu.createDeviceUnique(cfg),
		        top_gpu,
		        transfer_queue_tag,
		        draw_queue_tag.get_or_throw("No draw or compute queue! That doesn't seem right."),
		        std::move(queue_mapping),
		        std::move(swapchains),
		        dedicated_alloc_supported);
657
	}
658
} // namespace mirrage::graphic