-
-
Notifications
You must be signed in to change notification settings - Fork 584
/
Copy pathvulkan_hooks_device.cpp
3174 lines (2641 loc) · 135 KB
/
vulkan_hooks_device.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014 Patrick Mours
* SPDX-License-Identifier: BSD-3-Clause OR MIT
*/
#include "vulkan_hooks.hpp"
#include "vulkan_impl_device.hpp"
#include "vulkan_impl_command_queue.hpp"
#include "vulkan_impl_swapchain.hpp"
#include "vulkan_impl_type_convert.hpp"
#include "dll_log.hpp"
#include "hook_manager.hpp"
#include "addon_manager.hpp"
#include "runtime_manager.hpp"
#include "lockfree_linear_map.hpp"
#include <cstring> // std::strcmp, std::strncmp
#include <algorithm> // std::fill_n, std::find_if, std::min, std::sort, std::unique
// Set during Vulkan device creation and presentation, to avoid hooking internal D3D devices created e.g. by NVIDIA Ansel, Optimus or layered DXGI swapchain
extern thread_local bool g_in_dxgi_runtime;
lockfree_linear_map<void *, reshade::vulkan::device_impl *, 8> g_vulkan_devices;
extern lockfree_linear_map<void *, instance_dispatch_table, 16> g_vulkan_instances;
extern lockfree_linear_map<VkSurfaceKHR, HWND, 16> g_vulkan_surface_windows;
#define GET_DISPATCH_PTR(name, object) \
GET_DISPATCH_PTR_FROM(name, g_vulkan_devices.at(dispatch_key_from_handle(object)))
#define GET_DISPATCH_PTR_FROM(name, data) \
assert((data) != nullptr); \
PFN_vk##name trampoline = (data)->_dispatch_table.name; \
assert(trampoline != nullptr)
#define INIT_DISPATCH_PTR(name) \
dispatch_table.name = reinterpret_cast<PFN_vk##name>(get_device_proc(device, "vk" #name))
#define INIT_DISPATCH_PTR_ALTERNATIVE(name, suffix) \
if (nullptr == dispatch_table.name) \
dispatch_table.name = reinterpret_cast<PFN_vk##name##suffix>(get_device_proc(device, "vk" #name #suffix))
#if RESHADE_ADDON
static void create_default_view(reshade::vulkan::device_impl *device_impl, VkImage image)
{
if (image == VK_NULL_HANDLE)
return;
const auto data = device_impl->get_private_data_for_object<VK_OBJECT_TYPE_IMAGE>(image);
assert(data->default_view == VK_NULL_HANDLE);
// Need to create a default view that is used in 'vkCmdClearColorImage' and 'vkCmdClearDepthStencilImage'
if ((data->create_info.usage & (VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT)) != 0 &&
(data->create_info.usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) != 0)
{
VkImageViewCreateInfo default_view_info { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO };
default_view_info.image = image;
default_view_info.viewType = static_cast<VkImageViewType>(data->create_info.imageType); // Map 'VK_IMAGE_TYPE_1D' to VK_IMAGE_VIEW_TYPE_1D' and so on
default_view_info.format = data->create_info.format;
default_view_info.subresourceRange.aspectMask = reshade::vulkan::aspect_flags_from_format(data->create_info.format);
default_view_info.subresourceRange.baseMipLevel = 0;
default_view_info.subresourceRange.levelCount = VK_REMAINING_MIP_LEVELS;
default_view_info.subresourceRange.baseArrayLayer = 0;
default_view_info.subresourceRange.layerCount = 1; // Non-array image view types can only contain a single layer
vkCreateImageView(device_impl->_orig, &default_view_info, nullptr, &data->default_view);
}
}
static void destroy_default_view(reshade::vulkan::device_impl *device_impl, VkImage image)
{
if (image == VK_NULL_HANDLE)
return;
const VkImageView default_view = device_impl->get_private_data_for_object<VK_OBJECT_TYPE_IMAGE>(image)->default_view;
if (default_view != VK_NULL_HANDLE)
{
vkDestroyImageView(device_impl->_orig, default_view, nullptr);
}
}
#endif
VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkDevice *pDevice)
{
reshade::log::message(reshade::log::level::info, "Redirecting vkCreateDevice(physicalDevice = %p, pCreateInfo = %p, pAllocator = %p, pDevice = %p) ...", physicalDevice, pCreateInfo, pAllocator, pDevice);
assert(pCreateInfo != nullptr && pDevice != nullptr);
const instance_dispatch_table &instance_dispatch = g_vulkan_instances.at(dispatch_key_from_handle(physicalDevice));
assert(instance_dispatch.instance != VK_NULL_HANDLE);
// Look for layer link info if installed as a layer (provided by the Vulkan loader)
VkLayerDeviceCreateInfo *const link_info = find_layer_info<VkLayerDeviceCreateInfo>(pCreateInfo->pNext, VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO, VK_LAYER_LINK_INFO);
// Get trampoline function pointers
PFN_vkCreateDevice trampoline = nullptr;
PFN_vkGetDeviceProcAddr get_device_proc = nullptr;
PFN_vkGetInstanceProcAddr get_instance_proc = nullptr;
if (link_info != nullptr)
{
assert(link_info->u.pLayerInfo != nullptr);
assert(link_info->u.pLayerInfo->pfnNextGetDeviceProcAddr != nullptr);
assert(link_info->u.pLayerInfo->pfnNextGetInstanceProcAddr != nullptr);
// Look up functions in layer info
get_device_proc = link_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
get_instance_proc = link_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
trampoline = reinterpret_cast<PFN_vkCreateDevice>(get_instance_proc(instance_dispatch.instance, "vkCreateDevice"));
// Advance the link info for the next element on the chain
link_info->u.pLayerInfo = link_info->u.pLayerInfo->pNext;
}
#ifdef RESHADE_TEST_APPLICATION
else
{
trampoline = reshade::hooks::call(vkCreateDevice);
get_device_proc = reshade::hooks::call(vkGetDeviceProcAddr);
get_instance_proc = reshade::hooks::call(vkGetInstanceProcAddr);
}
#endif
if (trampoline == nullptr) // Unable to resolve next 'vkCreateDevice' function in the call chain
return VK_ERROR_INITIALIZATION_FAILED;
reshade::log::message(reshade::log::level::info, "> Dumping enabled device extensions:");
for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; ++i)
reshade::log::message(reshade::log::level::info, " %s", pCreateInfo->ppEnabledExtensionNames[i]);
const auto enum_queue_families = instance_dispatch.GetPhysicalDeviceQueueFamilyProperties;
assert(enum_queue_families != nullptr);
const auto enum_device_extensions = instance_dispatch.EnumerateDeviceExtensionProperties;
assert(enum_device_extensions != nullptr);
uint32_t num_queue_families = 0;
enum_queue_families(physicalDevice, &num_queue_families, nullptr);
std::vector<VkQueueFamilyProperties> queue_families(num_queue_families);
enum_queue_families(physicalDevice, &num_queue_families, queue_families.data());
uint32_t graphics_queue_family_index = std::numeric_limits<uint32_t>::max();
for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; ++i)
{
const uint32_t queue_family_index = pCreateInfo->pQueueCreateInfos[i].queueFamilyIndex;
assert(queue_family_index < num_queue_families);
// Find the first queue family which supports graphics and has at least one queue
if (pCreateInfo->pQueueCreateInfos[i].queueCount > 0 && (queue_families[queue_family_index].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
{
if (pCreateInfo->pQueueCreateInfos[i].pQueuePriorities[0] < 1.0f)
reshade::log::message(reshade::log::level::warning, "Vulkan queue used for rendering has a low priority (%f).", pCreateInfo->pQueueCreateInfos[i].pQueuePriorities[0]);
graphics_queue_family_index = queue_family_index;
break;
}
}
VkPhysicalDeviceFeatures enabled_features = {};
const VkPhysicalDeviceFeatures2 *const features2 = find_in_structure_chain<VkPhysicalDeviceFeatures2>(
pCreateInfo->pNext, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2);
if (features2 != nullptr) // The features from the structure chain take precedence
enabled_features = features2->features;
else if (pCreateInfo->pEnabledFeatures != nullptr)
enabled_features = *pCreateInfo->pEnabledFeatures;
std::vector<const char *> enabled_extensions;
enabled_extensions.reserve(pCreateInfo->enabledExtensionCount);
for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; ++i)
enabled_extensions.push_back(pCreateInfo->ppEnabledExtensionNames[i]);
bool push_descriptor_ext = false;
bool dynamic_rendering_ext = false;
bool timeline_semaphore_ext = false;
bool custom_border_color_ext = false;
bool extended_dynamic_state_ext = false;
bool conservative_rasterization_ext = false;
bool ray_tracing_ext = false;
// Check if the device is used for presenting
if (std::find_if(enabled_extensions.cbegin(), enabled_extensions.cend(),
[](const char *name) { return std::strcmp(name, VK_KHR_SWAPCHAIN_EXTENSION_NAME) == 0; }) == enabled_extensions.cend())
{
reshade::log::message(reshade::log::level::warning, "Skipping device because it is not created with the \"" VK_KHR_SWAPCHAIN_EXTENSION_NAME "\" extension.");
graphics_queue_family_index = std::numeric_limits<uint32_t>::max();
}
// Only have to enable additional features if there is a graphics queue, since ReShade will not run otherwise
else if (graphics_queue_family_index == std::numeric_limits<uint32_t>::max())
{
reshade::log::message(reshade::log::level::warning, "Skipping device because it is not created with a graphics queue.");
}
else
{
// No Man's Sky initializes OpenVR before loading Vulkan (and therefore before loading ReShade), so need to manually install OpenVR hooks now when used
extern void check_and_init_openvr_hooks();
check_and_init_openvr_hooks();
uint32_t num_extensions = 0;
enum_device_extensions(physicalDevice, nullptr, &num_extensions, nullptr);
std::vector<VkExtensionProperties> extensions(num_extensions);
enum_device_extensions(physicalDevice, nullptr, &num_extensions, extensions.data());
// Make sure the driver actually supports the requested extensions
const auto add_extension = [&extensions, &enabled_extensions, &graphics_queue_family_index](const char *name, bool required) {
if (const auto it = std::find_if(extensions.cbegin(), extensions.cend(),
[name](const auto &props) { return std::strncmp(props.extensionName, name, VK_MAX_EXTENSION_NAME_SIZE) == 0; });
it != extensions.cend())
{
enabled_extensions.push_back(name);
return true;
}
if (required)
{
reshade::log::message(reshade::log::level::error, "Required extension \"%s\" is not supported on this device. Initialization failed.", name);
// Reset queue family index to prevent ReShade initialization
graphics_queue_family_index = std::numeric_limits<uint32_t>::max();
}
else
{
reshade::log::message(reshade::log::level::warning, "Optional extension \"%s\" is not supported on this device.", name);
}
return false;
};
// Enable features that ReShade requires
enabled_features.samplerAnisotropy = VK_TRUE;
enabled_features.shaderImageGatherExtended = VK_TRUE;
enabled_features.shaderStorageImageWriteWithoutFormat = VK_TRUE;
// Enable extensions that ReShade requires
if (instance_dispatch.api_version < VK_API_VERSION_1_3 && !add_extension(VK_EXT_PRIVATE_DATA_EXTENSION_NAME, true))
return VK_ERROR_EXTENSION_NOT_PRESENT;
add_extension(VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME, true);
add_extension(VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME, true);
push_descriptor_ext = add_extension(VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME, false);
dynamic_rendering_ext = instance_dispatch.api_version >= VK_API_VERSION_1_3 || add_extension(VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME, false);
// Add extensions that are required by VK_KHR_dynamic_rendering when not using the core variant
if (dynamic_rendering_ext && instance_dispatch.api_version < VK_API_VERSION_1_3)
{
add_extension(VK_KHR_DEPTH_STENCIL_RESOLVE_EXTENSION_NAME, false);
add_extension(VK_KHR_CREATE_RENDERPASS_2_EXTENSION_NAME, false);
}
timeline_semaphore_ext = instance_dispatch.api_version >= VK_API_VERSION_1_2 || add_extension(VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME, false);
custom_border_color_ext = add_extension(VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME, false);
extended_dynamic_state_ext = instance_dispatch.api_version >= VK_API_VERSION_1_3 || add_extension(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME, false);
conservative_rasterization_ext = add_extension(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME, false);
add_extension(VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME, false);
#if 0
ray_tracing_ext =
add_extension(VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME, false) &&
add_extension(VK_KHR_SPIRV_1_4_EXTENSION_NAME, false) &&
add_extension(VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME, false) &&
add_extension(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME, false) &&
add_extension(VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME, false) &&
add_extension(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME, false) &&
add_extension(VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME, false) &&
add_extension(VK_KHR_RAY_TRACING_MAINTENANCE_1_EXTENSION_NAME, false);
#endif
}
VkDeviceCreateInfo create_info = *pCreateInfo;
create_info.enabledExtensionCount = static_cast<uint32_t>(enabled_extensions.size());
create_info.ppEnabledExtensionNames = enabled_extensions.data();
// Patch the enabled features
if (features2 != nullptr)
// This is evil, because overwriting application memory, but whatever (RenderDoc does this too)
const_cast<VkPhysicalDeviceFeatures2 *>(features2)->features = enabled_features;
else
create_info.pEnabledFeatures = &enabled_features;
// Enable private data feature
VkDevicePrivateDataCreateInfo private_data_info { VK_STRUCTURE_TYPE_DEVICE_PRIVATE_DATA_CREATE_INFO };
private_data_info.pNext = create_info.pNext;
private_data_info.privateDataSlotRequestCount = 1;
// Enable Vulkan memory model device scope if it is not, since it is required by atomics in generated SPIR-V code for effects
if (const auto existing_memory_model_features = find_in_structure_chain<VkPhysicalDeviceVulkanMemoryModelFeatures>(
pCreateInfo->pNext, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_MEMORY_MODEL_FEATURES))
{
if (existing_memory_model_features->vulkanMemoryModel)
const_cast<VkPhysicalDeviceVulkanMemoryModelFeatures *>(existing_memory_model_features)->vulkanMemoryModelDeviceScope = VK_TRUE;
}
VkPhysicalDevicePrivateDataFeatures private_data_feature;
VkPhysicalDeviceDynamicRenderingFeatures dynamic_rendering_feature;
VkPhysicalDeviceTimelineSemaphoreFeatures timeline_semaphore_feature;
if (const auto existing_vulkan_13_features = find_in_structure_chain<VkPhysicalDeviceVulkan13Features>(
pCreateInfo->pNext, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES))
{
assert(instance_dispatch.api_version >= VK_API_VERSION_1_3);
create_info.pNext = &private_data_info;
dynamic_rendering_ext = existing_vulkan_13_features->dynamicRendering;
// Force enable private data in Vulkan 1.3, again, evil =)
const_cast<VkPhysicalDeviceVulkan13Features *>(existing_vulkan_13_features)->privateData = VK_TRUE;
}
else
{
private_data_feature = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIVATE_DATA_FEATURES };
private_data_feature.pNext = &private_data_info;
private_data_feature.privateData = VK_TRUE;
create_info.pNext = &private_data_feature;
if (const auto existing_dynamic_rendering_features = find_in_structure_chain<VkPhysicalDeviceDynamicRenderingFeatures>(
pCreateInfo->pNext, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_FEATURES))
{
dynamic_rendering_ext = existing_dynamic_rendering_features->dynamicRendering;
}
else if (dynamic_rendering_ext)
{
dynamic_rendering_feature = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_FEATURES };
dynamic_rendering_feature.pNext = const_cast<void *>(create_info.pNext);
dynamic_rendering_feature.dynamicRendering = VK_TRUE;
create_info.pNext = &dynamic_rendering_feature;
}
}
if (const auto existing_vulkan_12_features = find_in_structure_chain<VkPhysicalDeviceVulkan12Features>(
pCreateInfo->pNext, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES))
{
assert(instance_dispatch.api_version >= VK_API_VERSION_1_2);
// Force enable timeline semaphore support (used for effect runtime present/graphics queue synchronization in case of present from compute, e.g. in Indiana Jones and the Great Circle and DOOM Eternal)
const_cast<VkPhysicalDeviceVulkan12Features *>(existing_vulkan_12_features)->timelineSemaphore = VK_TRUE;
}
else
{
if (const auto existing_timeline_semaphore_features = find_in_structure_chain<VkPhysicalDeviceTimelineSemaphoreFeatures>(
pCreateInfo->pNext, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES))
{
timeline_semaphore_ext = existing_timeline_semaphore_features->timelineSemaphore;
}
else if (timeline_semaphore_ext)
{
timeline_semaphore_feature = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES };
timeline_semaphore_feature.pNext = const_cast<void *>(create_info.pNext);
timeline_semaphore_feature.timelineSemaphore = VK_TRUE;
create_info.pNext = &timeline_semaphore_feature;
}
}
// Optionally enable custom border color feature
VkPhysicalDeviceCustomBorderColorFeaturesEXT custom_border_feature;
if (const auto existing_custom_border_features = find_in_structure_chain<VkPhysicalDeviceCustomBorderColorFeaturesEXT>(
pCreateInfo->pNext, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT))
{
custom_border_color_ext = existing_custom_border_features->customBorderColors;
}
else if (custom_border_color_ext)
{
custom_border_feature = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT };
custom_border_feature.pNext = const_cast<void *>(create_info.pNext);
custom_border_feature.customBorderColors = VK_TRUE;
custom_border_feature.customBorderColorWithoutFormat = VK_TRUE;
create_info.pNext = &custom_border_feature;
}
// Optionally enable extended dynamic state feature
VkPhysicalDeviceExtendedDynamicStateFeaturesEXT extended_dynamic_state_feature;
if (const auto existing_extended_dynamic_state_features = find_in_structure_chain<VkPhysicalDeviceExtendedDynamicStateFeaturesEXT>(
pCreateInfo->pNext, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT))
{
extended_dynamic_state_ext = existing_extended_dynamic_state_features->extendedDynamicState;
}
else if (extended_dynamic_state_ext)
{
extended_dynamic_state_feature = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT };
extended_dynamic_state_feature.pNext = const_cast<void *>(create_info.pNext);
extended_dynamic_state_feature.extendedDynamicState = VK_TRUE;
create_info.pNext = &extended_dynamic_state_feature;
}
// Optionally enable ray tracing feature
VkPhysicalDeviceRayTracingPipelineFeaturesKHR ray_tracing_feature;
VkPhysicalDeviceAccelerationStructureFeaturesKHR acceleration_structure_feature;
VkPhysicalDeviceBufferDeviceAddressFeatures buffer_device_address_feature;
if (const auto existing_ray_tracing_features = find_in_structure_chain<VkPhysicalDeviceRayTracingPipelineFeaturesKHR>(
pCreateInfo->pNext, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR))
{
ray_tracing_ext = existing_ray_tracing_features->rayTracingPipeline;
}
else if (ray_tracing_ext)
{
ray_tracing_feature = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_RAY_TRACING_PIPELINE_FEATURES_KHR };
ray_tracing_feature.pNext = const_cast<void *>(create_info.pNext);
ray_tracing_feature.rayTracingPipeline = VK_TRUE;
create_info.pNext = &ray_tracing_feature;
acceleration_structure_feature = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR };
acceleration_structure_feature.pNext = const_cast<void *>(create_info.pNext);
acceleration_structure_feature.accelerationStructure = VK_TRUE;
create_info.pNext = &acceleration_structure_feature;
buffer_device_address_feature = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES };
buffer_device_address_feature.pNext = const_cast<void *>(create_info.pNext);
buffer_device_address_feature.bufferDeviceAddress = VK_TRUE;
create_info.pNext = &buffer_device_address_feature;
}
// Continue calling down the chain
assert(!g_in_dxgi_runtime);
g_in_dxgi_runtime = true;
const VkResult result = trampoline(physicalDevice, &create_info, pAllocator, pDevice);
g_in_dxgi_runtime = false;
if (result < VK_SUCCESS)
{
reshade::log::message(reshade::log::level::warning, "vkCreateDevice failed with error code %d.", static_cast<int>(result));
return result;
}
VkDevice device = *pDevice;
// Initialize the device dispatch table
VkLayerDispatchTable dispatch_table = {};
dispatch_table.GetDeviceProcAddr = get_device_proc;
// Core 1_0
INIT_DISPATCH_PTR(DestroyDevice);
INIT_DISPATCH_PTR(GetDeviceQueue);
INIT_DISPATCH_PTR(QueueSubmit);
INIT_DISPATCH_PTR(QueueWaitIdle);
INIT_DISPATCH_PTR(DeviceWaitIdle);
INIT_DISPATCH_PTR(AllocateMemory);
INIT_DISPATCH_PTR(FreeMemory);
INIT_DISPATCH_PTR(MapMemory);
INIT_DISPATCH_PTR(UnmapMemory);
INIT_DISPATCH_PTR(FlushMappedMemoryRanges);
INIT_DISPATCH_PTR(InvalidateMappedMemoryRanges);
INIT_DISPATCH_PTR(BindBufferMemory);
INIT_DISPATCH_PTR(BindImageMemory);
INIT_DISPATCH_PTR(GetBufferMemoryRequirements);
INIT_DISPATCH_PTR(GetImageMemoryRequirements);
INIT_DISPATCH_PTR(CreateFence);
INIT_DISPATCH_PTR(DestroyFence);
INIT_DISPATCH_PTR(ResetFences);
INIT_DISPATCH_PTR(GetFenceStatus);
INIT_DISPATCH_PTR(WaitForFences);
INIT_DISPATCH_PTR(CreateSemaphore);
INIT_DISPATCH_PTR(DestroySemaphore);
INIT_DISPATCH_PTR(CreateQueryPool);
INIT_DISPATCH_PTR(DestroyQueryPool);
INIT_DISPATCH_PTR(GetQueryPoolResults);
INIT_DISPATCH_PTR(CreateBuffer);
INIT_DISPATCH_PTR(DestroyBuffer);
INIT_DISPATCH_PTR(CreateBufferView);
INIT_DISPATCH_PTR(DestroyBufferView);
INIT_DISPATCH_PTR(CreateImage);
INIT_DISPATCH_PTR(DestroyImage);
INIT_DISPATCH_PTR(GetImageSubresourceLayout);
INIT_DISPATCH_PTR(CreateImageView);
INIT_DISPATCH_PTR(DestroyImageView);
INIT_DISPATCH_PTR(CreateShaderModule);
INIT_DISPATCH_PTR(DestroyShaderModule);
INIT_DISPATCH_PTR(CreateGraphicsPipelines);
INIT_DISPATCH_PTR(CreateComputePipelines);
INIT_DISPATCH_PTR(DestroyPipeline);
INIT_DISPATCH_PTR(CreatePipelineLayout);
INIT_DISPATCH_PTR(DestroyPipelineLayout);
INIT_DISPATCH_PTR(CreateSampler);
INIT_DISPATCH_PTR(DestroySampler);
INIT_DISPATCH_PTR(CreateDescriptorSetLayout);
INIT_DISPATCH_PTR(DestroyDescriptorSetLayout);
INIT_DISPATCH_PTR(CreateDescriptorPool);
INIT_DISPATCH_PTR(DestroyDescriptorPool);
INIT_DISPATCH_PTR(ResetDescriptorPool);
INIT_DISPATCH_PTR(AllocateDescriptorSets);
INIT_DISPATCH_PTR(FreeDescriptorSets);
INIT_DISPATCH_PTR(UpdateDescriptorSets);
INIT_DISPATCH_PTR(CreateFramebuffer);
INIT_DISPATCH_PTR(DestroyFramebuffer);
INIT_DISPATCH_PTR(CreateRenderPass);
INIT_DISPATCH_PTR(DestroyRenderPass);
INIT_DISPATCH_PTR(CreateCommandPool);
INIT_DISPATCH_PTR(DestroyCommandPool);
INIT_DISPATCH_PTR(ResetCommandPool);
INIT_DISPATCH_PTR(AllocateCommandBuffers);
INIT_DISPATCH_PTR(FreeCommandBuffers);
INIT_DISPATCH_PTR(BeginCommandBuffer);
INIT_DISPATCH_PTR(EndCommandBuffer);
INIT_DISPATCH_PTR(ResetCommandBuffer);
INIT_DISPATCH_PTR(CmdBindPipeline);
INIT_DISPATCH_PTR(CmdSetViewport);
INIT_DISPATCH_PTR(CmdSetScissor);
INIT_DISPATCH_PTR(CmdSetDepthBias);
INIT_DISPATCH_PTR(CmdSetBlendConstants);
INIT_DISPATCH_PTR(CmdSetStencilCompareMask);
INIT_DISPATCH_PTR(CmdSetStencilWriteMask);
INIT_DISPATCH_PTR(CmdSetStencilReference);
INIT_DISPATCH_PTR(CmdBindDescriptorSets);
INIT_DISPATCH_PTR(CmdBindIndexBuffer);
INIT_DISPATCH_PTR(CmdBindVertexBuffers);
INIT_DISPATCH_PTR(CmdDraw);
INIT_DISPATCH_PTR(CmdDrawIndexed);
INIT_DISPATCH_PTR(CmdDrawIndirect);
INIT_DISPATCH_PTR(CmdDrawIndexedIndirect);
INIT_DISPATCH_PTR(CmdDispatch);
INIT_DISPATCH_PTR(CmdDispatchIndirect);
INIT_DISPATCH_PTR(CmdCopyBuffer);
INIT_DISPATCH_PTR(CmdCopyImage);
INIT_DISPATCH_PTR(CmdBlitImage);
INIT_DISPATCH_PTR(CmdCopyBufferToImage);
INIT_DISPATCH_PTR(CmdCopyImageToBuffer);
INIT_DISPATCH_PTR(CmdUpdateBuffer);
INIT_DISPATCH_PTR(CmdClearColorImage);
INIT_DISPATCH_PTR(CmdClearDepthStencilImage);
INIT_DISPATCH_PTR(CmdClearAttachments);
INIT_DISPATCH_PTR(CmdResolveImage);
INIT_DISPATCH_PTR(CmdPipelineBarrier);
INIT_DISPATCH_PTR(CmdBeginQuery);
INIT_DISPATCH_PTR(CmdEndQuery);
INIT_DISPATCH_PTR(CmdResetQueryPool);
INIT_DISPATCH_PTR(CmdWriteTimestamp);
INIT_DISPATCH_PTR(CmdCopyQueryPoolResults);
INIT_DISPATCH_PTR(CmdPushConstants);
INIT_DISPATCH_PTR(CmdBeginRenderPass);
INIT_DISPATCH_PTR(CmdNextSubpass);
INIT_DISPATCH_PTR(CmdEndRenderPass);
INIT_DISPATCH_PTR(CmdExecuteCommands);
// Core 1_1
if (instance_dispatch.api_version >= VK_API_VERSION_1_1)
{
INIT_DISPATCH_PTR(BindBufferMemory2);
INIT_DISPATCH_PTR(BindImageMemory2);
INIT_DISPATCH_PTR(GetBufferMemoryRequirements2);
INIT_DISPATCH_PTR(GetImageMemoryRequirements2);
INIT_DISPATCH_PTR(GetDeviceQueue2);
}
// Core 1_2
if (instance_dispatch.api_version >= VK_API_VERSION_1_2)
{
INIT_DISPATCH_PTR(CmdDrawIndirectCount);
INIT_DISPATCH_PTR(CmdDrawIndexedIndirectCount);
INIT_DISPATCH_PTR(CreateRenderPass2);
INIT_DISPATCH_PTR(CmdBeginRenderPass2);
INIT_DISPATCH_PTR(CmdNextSubpass2);
INIT_DISPATCH_PTR(CmdEndRenderPass2);
INIT_DISPATCH_PTR(GetSemaphoreCounterValue);
INIT_DISPATCH_PTR(WaitSemaphores);
INIT_DISPATCH_PTR(SignalSemaphore);
INIT_DISPATCH_PTR(GetBufferDeviceAddress);
}
// Core 1_3
if (instance_dispatch.api_version >= VK_API_VERSION_1_3)
{
INIT_DISPATCH_PTR(CreatePrivateDataSlot);
INIT_DISPATCH_PTR(DestroyPrivateDataSlot);
INIT_DISPATCH_PTR(GetPrivateData);
INIT_DISPATCH_PTR(SetPrivateData);
INIT_DISPATCH_PTR(CmdPipelineBarrier2);
INIT_DISPATCH_PTR(CmdWriteTimestamp2);
INIT_DISPATCH_PTR(QueueSubmit2);
INIT_DISPATCH_PTR(CmdCopyBuffer2);
INIT_DISPATCH_PTR(CmdCopyImage2);
INIT_DISPATCH_PTR(CmdCopyBufferToImage2);
INIT_DISPATCH_PTR(CmdCopyImageToBuffer2);
INIT_DISPATCH_PTR(CmdBlitImage2);
INIT_DISPATCH_PTR(CmdResolveImage2);
INIT_DISPATCH_PTR(CmdBeginRendering);
INIT_DISPATCH_PTR(CmdEndRendering);
INIT_DISPATCH_PTR(CmdSetCullMode);
INIT_DISPATCH_PTR(CmdSetFrontFace);
INIT_DISPATCH_PTR(CmdSetPrimitiveTopology);
INIT_DISPATCH_PTR(CmdSetViewportWithCount);
INIT_DISPATCH_PTR(CmdSetScissorWithCount);
INIT_DISPATCH_PTR(CmdBindVertexBuffers2);
INIT_DISPATCH_PTR(CmdSetDepthTestEnable);
INIT_DISPATCH_PTR(CmdSetDepthWriteEnable);
INIT_DISPATCH_PTR(CmdSetDepthCompareOp);
INIT_DISPATCH_PTR(CmdSetDepthBoundsTestEnable);
INIT_DISPATCH_PTR(CmdSetStencilTestEnable);
INIT_DISPATCH_PTR(CmdSetStencilOp);
INIT_DISPATCH_PTR(CmdSetRasterizerDiscardEnable);
INIT_DISPATCH_PTR(CmdSetDepthBiasEnable);
INIT_DISPATCH_PTR(CmdSetPrimitiveRestartEnable);
INIT_DISPATCH_PTR(GetDeviceBufferMemoryRequirements);
INIT_DISPATCH_PTR(GetDeviceImageMemoryRequirements);
}
// VK_KHR_swapchain
INIT_DISPATCH_PTR(CreateSwapchainKHR);
INIT_DISPATCH_PTR(DestroySwapchainKHR);
INIT_DISPATCH_PTR(GetSwapchainImagesKHR);
INIT_DISPATCH_PTR(AcquireNextImageKHR);
INIT_DISPATCH_PTR(QueuePresentKHR);
INIT_DISPATCH_PTR(AcquireNextImage2KHR);
// VK_KHR_dynamic_rendering
INIT_DISPATCH_PTR_ALTERNATIVE(CmdBeginRendering, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdEndRendering, KHR);
// VK_KHR_push_descriptor
INIT_DISPATCH_PTR(CmdPushDescriptorSetKHR);
// VK_KHR_create_renderpass2 (try the KHR version if the core version does not exist)
INIT_DISPATCH_PTR_ALTERNATIVE(CreateRenderPass2, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdBeginRenderPass2, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdNextSubpass2, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdEndRenderPass2, KHR);
// VK_KHR_bind_memory2
INIT_DISPATCH_PTR_ALTERNATIVE(BindBufferMemory2, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(BindImageMemory2, KHR);
// VK_KHR_draw_indirect_count
INIT_DISPATCH_PTR_ALTERNATIVE(CmdDrawIndirectCount, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdDrawIndexedIndirectCount, KHR);
// VK_KHR_timeline_semaphore
INIT_DISPATCH_PTR_ALTERNATIVE(GetSemaphoreCounterValue, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(WaitSemaphores, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(SignalSemaphore, KHR);
// VK_KHR_buffer_device_address
INIT_DISPATCH_PTR_ALTERNATIVE(GetBufferDeviceAddress, KHR);
// VK_KHR_synchronization2
INIT_DISPATCH_PTR_ALTERNATIVE(CmdPipelineBarrier2, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdWriteTimestamp2, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(QueueSubmit2, KHR);
// VK_KHR_copy_commands2
INIT_DISPATCH_PTR_ALTERNATIVE(CmdCopyBuffer2, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdCopyImage2, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdCopyBufferToImage2, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdCopyImageToBuffer2, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdBlitImage2, KHR);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdResolveImage2, KHR);
// VK_EXT_transform_feedback
INIT_DISPATCH_PTR(CmdBindTransformFeedbackBuffersEXT);
INIT_DISPATCH_PTR(CmdBeginQueryIndexedEXT);
INIT_DISPATCH_PTR(CmdEndQueryIndexedEXT);
// VK_EXT_debug_utils
INIT_DISPATCH_PTR(SetDebugUtilsObjectNameEXT);
INIT_DISPATCH_PTR(QueueBeginDebugUtilsLabelEXT);
INIT_DISPATCH_PTR(QueueEndDebugUtilsLabelEXT);
INIT_DISPATCH_PTR(QueueInsertDebugUtilsLabelEXT);
INIT_DISPATCH_PTR(CmdBeginDebugUtilsLabelEXT);
INIT_DISPATCH_PTR(CmdEndDebugUtilsLabelEXT);
INIT_DISPATCH_PTR(CmdInsertDebugUtilsLabelEXT);
// VK_EXT_extended_dynamic_state
INIT_DISPATCH_PTR_ALTERNATIVE(CmdSetCullMode, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdSetFrontFace, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdSetPrimitiveTopology, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdSetViewportWithCount, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdSetScissorWithCount, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdBindVertexBuffers2, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdSetDepthTestEnable, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdSetDepthWriteEnable, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdSetDepthCompareOp, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdSetDepthBoundsTestEnable, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdSetStencilTestEnable, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(CmdSetStencilOp, EXT);
// VK_EXT_private_data (try the EXT version if the core version does not exist)
INIT_DISPATCH_PTR_ALTERNATIVE(CreatePrivateDataSlot, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(DestroyPrivateDataSlot, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(GetPrivateData, EXT);
INIT_DISPATCH_PTR_ALTERNATIVE(SetPrivateData, EXT);
// VK_KHR_acceleration_structure
INIT_DISPATCH_PTR(CreateAccelerationStructureKHR);
INIT_DISPATCH_PTR(DestroyAccelerationStructureKHR);
INIT_DISPATCH_PTR(CmdBuildAccelerationStructuresKHR);
INIT_DISPATCH_PTR(CmdBuildAccelerationStructuresIndirectKHR);
INIT_DISPATCH_PTR(CmdCopyAccelerationStructureKHR);
INIT_DISPATCH_PTR(GetAccelerationStructureDeviceAddressKHR);
INIT_DISPATCH_PTR(CmdWriteAccelerationStructuresPropertiesKHR);
INIT_DISPATCH_PTR(GetAccelerationStructureBuildSizesKHR);
// VK_KHR_ray_tracing_pipeline
INIT_DISPATCH_PTR(CmdTraceRaysKHR);
INIT_DISPATCH_PTR(CreateRayTracingPipelinesKHR);
INIT_DISPATCH_PTR(GetRayTracingShaderGroupHandlesKHR);
INIT_DISPATCH_PTR(CmdTraceRaysIndirectKHR);
INIT_DISPATCH_PTR(CmdSetRayTracingPipelineStackSizeKHR);
// VK_KHR_ray_tracing_maintenance1
INIT_DISPATCH_PTR(CmdTraceRaysIndirect2KHR);
// VK_EXT_mesh_shader
INIT_DISPATCH_PTR(CmdDrawMeshTasksEXT);
INIT_DISPATCH_PTR(CmdDrawMeshTasksIndirectEXT);
INIT_DISPATCH_PTR(CmdDrawMeshTasksIndirectCountEXT);
// VK_KHR_external_memory_win32
INIT_DISPATCH_PTR(GetMemoryWin32HandleKHR);
INIT_DISPATCH_PTR(GetMemoryWin32HandlePropertiesKHR);
// VK_KHR_external_semaphore_win32
INIT_DISPATCH_PTR(ImportSemaphoreWin32HandleKHR);
INIT_DISPATCH_PTR(GetSemaphoreWin32HandleKHR);
// Initialize per-device data
const auto device_impl = new reshade::vulkan::device_impl(
device,
physicalDevice,
instance_dispatch.instance,
instance_dispatch.api_version,
static_cast<const VkLayerInstanceDispatchTable &>(instance_dispatch),
dispatch_table,
enabled_features,
push_descriptor_ext,
dynamic_rendering_ext,
timeline_semaphore_ext,
custom_border_color_ext,
extended_dynamic_state_ext,
conservative_rasterization_ext,
ray_tracing_ext);
device_impl->_graphics_queue_family_index = graphics_queue_family_index;
if (!g_vulkan_devices.emplace(dispatch_key_from_handle(device), device_impl))
{
reshade::log::message(reshade::log::level::warning, "Failed to register Vulkan device %p.", device);
}
#if RESHADE_ADDON
reshade::load_addons();
reshade::invoke_addon_event<reshade::addon_event::init_device>(device_impl);
#endif
// Initialize all queues associated with this device
for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; ++i)
{
const VkDeviceQueueCreateInfo &queue_create_info = pCreateInfo->pQueueCreateInfos[i];
for (uint32_t queue_index = 0; queue_index < queue_create_info.queueCount; ++queue_index)
{
VkDeviceQueueInfo2 queue_info = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_INFO_2 };
queue_info.flags = queue_create_info.flags;
queue_info.queueFamilyIndex = queue_create_info.queueFamilyIndex;
queue_info.queueIndex = queue_index;
VkQueue queue = VK_NULL_HANDLE;
// According to the spec, 'vkGetDeviceQueue' must only be used to get queues where 'VkDeviceQueueCreateInfo::flags' is set to zero, so use 'vkGetDeviceQueue2' instead
dispatch_table.GetDeviceQueue2(device, &queue_info, &queue);
assert(VK_NULL_HANDLE != queue);
// Subsequent layers (like the validation layer or the Steam overlay) expect the loader to have set the dispatch pointer, but this does not happen when calling down the layer chain from here, so fix it
// This applies to 'vkGetDeviceQueue', 'vkGetDeviceQueue2' and 'vkAllocateCommandBuffers' (functions that return dispatchable objects)
*reinterpret_cast<void **>(queue) = *reinterpret_cast<void **>(device);
const auto queue_impl = new reshade::vulkan::object_data<VK_OBJECT_TYPE_QUEUE>(
device_impl,
queue_create_info.queueFamilyIndex,
queue_families[queue_create_info.queueFamilyIndex],
queue);
device_impl->register_object<VK_OBJECT_TYPE_QUEUE>(queue, queue_impl);
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::init_command_queue>(queue_impl);
#endif
}
}
#if RESHADE_VERBOSE_LOG
reshade::log::message(reshade::log::level::debug, "Returning Vulkan device %p.", device);
#endif
return result;
}
void VKAPI_CALL vkDestroyDevice(VkDevice device, const VkAllocationCallbacks *pAllocator)
{
reshade::log::message(reshade::log::level::info, "Redirecting vkDestroyDevice(device = %p, pAllocator = %p) ...", device, pAllocator);
if (device == VK_NULL_HANDLE)
return;
// Remove from device dispatch table since this device is being destroyed
reshade::vulkan::device_impl *const device_impl = g_vulkan_devices.erase(dispatch_key_from_handle(device));
GET_DISPATCH_PTR_FROM(DestroyDevice, device_impl);
// Destroy all queues associated with this device
const std::vector<reshade::vulkan::command_queue_impl *> queues = device_impl->_queues;
for (auto queue_it = queues.begin(); queue_it != queues.end(); ++queue_it)
{
const auto queue_impl = static_cast<reshade::vulkan::object_data<VK_OBJECT_TYPE_QUEUE> *>(*queue_it);
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::destroy_command_queue>(queue_impl);
#endif
device_impl->unregister_object<VK_OBJECT_TYPE_QUEUE, false>(queue_impl->_orig);
delete queue_impl; // This will remove the queue from the queue list of the device too (see 'command_queue_impl' destructor)
}
#if RESHADE_ADDON
reshade::invoke_addon_event<reshade::addon_event::destroy_device>(device_impl);
reshade::unload_addons();
#endif
// Finally destroy the device
delete device_impl;
trampoline(device, pAllocator);
}
VkResult VKAPI_CALL vkCreateSwapchainKHR(VkDevice device, const VkSwapchainCreateInfoKHR *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSwapchainKHR *pSwapchain)
{
reshade::log::message(reshade::log::level::info, "Redirecting vkCreateSwapchainKHR(device = %p, pCreateInfo = %p, pAllocator = %p, pSwapchain = %p) ...", device, pCreateInfo, pAllocator, pSwapchain);
reshade::vulkan::device_impl *const device_impl = g_vulkan_devices.at(dispatch_key_from_handle(device));
GET_DISPATCH_PTR_FROM(CreateSwapchainKHR, device_impl);
assert(pCreateInfo != nullptr && pSwapchain != nullptr);
std::vector<VkFormat> format_list;
std::vector<uint32_t> queue_family_list;
VkSwapchainCreateInfoKHR create_info = *pCreateInfo;
VkImageFormatListCreateInfoKHR format_list_info;
// Only have to enable additional features if there is a graphics queue, since ReShade will not run otherwise
if (device_impl->_graphics_queue_family_index != std::numeric_limits<uint32_t>::max())
{
// Add required usage flags to create info
create_info.imageUsage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
// Add required format variants, so e.g. both linear and sRGB views can be created for the swap chain images
format_list.push_back(reshade::vulkan::convert_format(
reshade::api::format_to_default_typed(reshade::vulkan::convert_format(create_info.imageFormat), 0)));
format_list.push_back(reshade::vulkan::convert_format(
reshade::api::format_to_default_typed(reshade::vulkan::convert_format(create_info.imageFormat), 1)));
// Only have to make format mutable if they are actually different
if (format_list[0] != format_list[1])
create_info.flags |= VK_SWAPCHAIN_CREATE_MUTABLE_FORMAT_BIT_KHR;
// Patch the format list in the create info of the application
if (const auto format_list_info2 = find_in_structure_chain<VkImageFormatListCreateInfoKHR>(
pCreateInfo->pNext, VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO_KHR))
{
format_list.insert(format_list.end(),
format_list_info2->pViewFormats, format_list_info2->pViewFormats + format_list_info2->viewFormatCount);
// Remove duplicates from the list (since the new formats may have already been added by the application)
std::sort(format_list.begin(), format_list.end());
format_list.erase(std::unique(format_list.begin(), format_list.end()), format_list.end());
// This is evil, because writing into application memory, but eh =)
const_cast<VkImageFormatListCreateInfoKHR *>(format_list_info2)->viewFormatCount = static_cast<uint32_t>(format_list.size());
const_cast<VkImageFormatListCreateInfoKHR *>(format_list_info2)->pViewFormats = format_list.data();
}
else if (format_list[0] != format_list[1])
{
format_list_info = { VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO_KHR };
format_list_info.pNext = create_info.pNext;
format_list_info.viewFormatCount = static_cast<uint32_t>(format_list.size());
format_list_info.pViewFormats = format_list.data();
create_info.pNext = &format_list_info;
}
// Add required queue family indices, so images can be used on the graphics queue
if (create_info.imageSharingMode == VK_SHARING_MODE_CONCURRENT)
{
queue_family_list.reserve(create_info.queueFamilyIndexCount + 1);
queue_family_list.push_back(device_impl->_graphics_queue_family_index);
for (uint32_t i = 0; i < create_info.queueFamilyIndexCount; ++i)
if (create_info.pQueueFamilyIndices[i] != device_impl->_graphics_queue_family_index)
queue_family_list.push_back(create_info.pQueueFamilyIndices[i]);
create_info.queueFamilyIndexCount = static_cast<uint32_t>(queue_family_list.size());
create_info.pQueueFamilyIndices = queue_family_list.data();
}
}
// Dump swap chain description
{
const char *format_string = nullptr;
switch (create_info.imageFormat)
{
case VK_FORMAT_UNDEFINED:
format_string = "VK_FORMAT_UNDEFINED";
break;
case VK_FORMAT_R8G8B8A8_UNORM:
format_string = "VK_FORMAT_R8G8B8A8_UNORM";
break;
case VK_FORMAT_R8G8B8A8_SRGB:
format_string = "VK_FORMAT_R8G8B8A8_SRGB";
break;
case VK_FORMAT_B8G8R8A8_UNORM:
format_string = "VK_FORMAT_B8G8R8A8_UNORM";
break;
case VK_FORMAT_B8G8R8A8_SRGB:
format_string = "VK_FORMAT_B8G8R8A8_SRGB";
break;
case VK_FORMAT_A2B10G10R10_UNORM_PACK32:
format_string = "VK_FORMAT_A2B10G10R10_UNORM_PACK32";
break;
case VK_FORMAT_A2R10G10B10_UNORM_PACK32:
format_string = "VK_FORMAT_A2R10G10B10_UNORM_PACK32";
break;
case VK_FORMAT_R16G16B16A16_UNORM:
format_string = "VK_FORMAT_R16G16B16A16_UNORM";
break;
case VK_FORMAT_R16G16B16A16_SFLOAT:
format_string = "VK_FORMAT_R16G16B16A16_SFLOAT";
break;
}
const char *color_space_string = nullptr;
switch (create_info.imageColorSpace)
{
case VK_COLOR_SPACE_SRGB_NONLINEAR_KHR:
color_space_string = "VK_COLOR_SPACE_SRGB_NONLINEAR_KHR";
break;
case VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT:
color_space_string = "VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT";
break;
case VK_COLOR_SPACE_BT2020_LINEAR_EXT:
color_space_string = "VK_COLOR_SPACE_BT2020_LINEAR_EXT";
break;
case VK_COLOR_SPACE_HDR10_ST2084_EXT:
color_space_string = "VK_COLOR_SPACE_HDR10_ST2084_EXT";
break;
case VK_COLOR_SPACE_HDR10_HLG_EXT:
color_space_string = "VK_COLOR_SPACE_HDR10_HLG_EXT";
break;
}
reshade::log::message(reshade::log::level::info, "> Dumping swap chain description:");
reshade::log::message(reshade::log::level::info, " +-----------------------------------------+-----------------------------------------+");
reshade::log::message(reshade::log::level::info, " | Parameter | Value |");
reshade::log::message(reshade::log::level::info, " +-----------------------------------------+-----------------------------------------+");
reshade::log::message(reshade::log::level::info, " | flags |" " %-#39x |", static_cast<unsigned int>(create_info.flags));
reshade::log::message(reshade::log::level::info, " | surface |" " %-39p |", create_info.surface);
reshade::log::message(reshade::log::level::info, " | minImageCount |" " %-39u |", create_info.minImageCount);
if (format_string != nullptr)
reshade::log::message(reshade::log::level::info, " | imageFormat |" " %-39s |", format_string);
else
reshade::log::message(reshade::log::level::info, " | imageFormat |" " %-39d |", static_cast<int>(create_info.imageFormat));
if (color_space_string != nullptr)
reshade::log::message(reshade::log::level::info, " | imageColorSpace |" " %-39s |", color_space_string);
else
reshade::log::message(reshade::log::level::info, " | imageColorSpace |" " %-39d |", static_cast<int>(create_info.imageColorSpace));
reshade::log::message(reshade::log::level::info, " | imageExtent |" " %-19u" " %-19u |", create_info.imageExtent.width, create_info.imageExtent.height);
reshade::log::message(reshade::log::level::info, " | imageArrayLayers |" " %-39u |", create_info.imageArrayLayers);
reshade::log::message(reshade::log::level::info, " | imageUsage |" " %-#39x |", static_cast<unsigned int>(create_info.imageUsage));
reshade::log::message(reshade::log::level::info, " | imageSharingMode |" " %-39d |", static_cast<int>(create_info.imageSharingMode));
reshade::log::message(reshade::log::level::info, " | queueFamilyIndexCount |" " %-39u |", create_info.queueFamilyIndexCount);
reshade::log::message(reshade::log::level::info, " | preTransform |" " %-#39x |", static_cast<unsigned int>(create_info.preTransform));
reshade::log::message(reshade::log::level::info, " | compositeAlpha |" " %-#39x |", static_cast<unsigned int>(create_info.compositeAlpha));
reshade::log::message(reshade::log::level::info, " | presentMode |" " %-39d |", static_cast<int>(create_info.presentMode));
reshade::log::message(reshade::log::level::info, " | clipped |" " %-39s |", create_info.clipped ? "true" : "false");
reshade::log::message(reshade::log::level::info, " | oldSwapchain |" " %-39p |", create_info.oldSwapchain);
reshade::log::message(reshade::log::level::info, " +-----------------------------------------+-----------------------------------------+");
}
// Look up window handle from surface
const HWND hwnd = g_vulkan_surface_windows.at(create_info.surface);
#if RESHADE_ADDON
reshade::api::swapchain_desc desc = {};
desc.back_buffer.type = reshade::api::resource_type::texture_2d;
desc.back_buffer.texture.width = create_info.imageExtent.width;
desc.back_buffer.texture.height = create_info.imageExtent.height;
assert(create_info.imageArrayLayers <= std::numeric_limits<uint16_t>::max());
desc.back_buffer.texture.depth_or_layers = static_cast<uint16_t>(create_info.imageArrayLayers);
desc.back_buffer.texture.levels = 1;
desc.back_buffer.texture.format = reshade::vulkan::convert_format(create_info.imageFormat);
desc.back_buffer.texture.samples = 1;
desc.back_buffer.heap = reshade::api::memory_heap::gpu_only;
reshade::vulkan::convert_image_usage_flags_to_usage(create_info.imageUsage, desc.back_buffer.usage);
desc.back_buffer_count = create_info.minImageCount;
desc.present_mode = static_cast<uint32_t>(create_info.presentMode);
desc.present_flags = create_info.flags;
desc.sync_interval = create_info.presentMode == VK_PRESENT_MODE_IMMEDIATE_KHR ? 0 : UINT32_MAX;
// Optionally change fullscreen state
VkSurfaceFullScreenExclusiveInfoEXT fullscreen_info;
if (const auto existing_fullscreen_info = find_in_structure_chain<VkSurfaceFullScreenExclusiveInfoEXT>(
pCreateInfo->pNext, VK_STRUCTURE_TYPE_SURFACE_FULL_SCREEN_EXCLUSIVE_INFO_EXT))
{
fullscreen_info = *existing_fullscreen_info;
desc.fullscreen_state = existing_fullscreen_info->fullScreenExclusive == VK_FULL_SCREEN_EXCLUSIVE_ALLOWED_EXT;
}
else
{