forked from ValveSoftware/steamos-compositor
-
Notifications
You must be signed in to change notification settings - Fork 325
Expand file tree
/
Copy pathrendervulkan.cpp
More file actions
4402 lines (3659 loc) · 133 KB
/
rendervulkan.cpp
File metadata and controls
4402 lines (3659 loc) · 133 KB
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
// Initialize Vulkan and composite stuff with a compute queue
#include <cassert>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <thread>
#include <dlfcn.h>
#include "vulkan_include.h"
#include "Utils/Algorithm.h"
#if defined(__linux__)
#include <sys/sysmacros.h>
#endif
// Used to remove the config struct alignment specified by the NIS header
#define NIS_ALIGNED(x)
// NIS_Config needs to be included before the X11 headers because of conflicting defines introduced by X11
#include "shaders/NVIDIAImageScaling/NIS/NIS_Config.h"
#include <drm_fourcc.h>
#include "hdmi.h"
#if HAVE_DRM
#include "drm_include.h"
#endif
#include "wlr_begin.hpp"
#include <wlr/render/drm_format_set.h>
#include "wlr_end.hpp"
#include "rendervulkan.hpp"
#include "main.hpp"
#include "steamcompmgr.hpp"
#include "log.hpp"
#include "Utils/Process.h"
#include "cs_composite_blit.h"
#include "cs_composite_blur.h"
#include "cs_composite_blur_cond.h"
#include "cs_composite_rcas.h"
#include "cs_easu.h"
#include "cs_easu_fp16.h"
#include "cs_gaussian_blur_horizontal.h"
#include "cs_nis.h"
#include "cs_nis_fp16.h"
#include "cs_rgb_to_nv12.h"
#define A_CPU
#include "shaders/ffx_a.h"
#include "shaders/ffx_fsr1.h"
#include "reshade_effect_manager.hpp"
extern bool g_bWasPartialComposite;
extern bool g_bAllowDeferredBackend;
static constexpr mat3x4 g_rgb2yuv_srgb_to_bt601_limited = {{
{ 0.257f, 0.504f, 0.098f, 0.0625f },
{ -0.148f, -0.291f, 0.439f, 0.5f },
{ 0.439f, -0.368f, -0.071f, 0.5f },
}};
static constexpr mat3x4 g_rgb2yuv_srgb_to_bt601 = {{
{ 0.299f, 0.587f, 0.114f, 0.0f },
{ -0.169f, -0.331f, 0.500f, 0.5f },
{ 0.500f, -0.419f, -0.081f, 0.5f },
}};
static constexpr mat3x4 g_rgb2yuv_srgb_to_bt709_limited = {{
{ 0.1826f, 0.6142f, 0.0620f, 0.0625f },
{ -0.1006f, -0.3386f, 0.4392f, 0.5f },
{ 0.4392f, -0.3989f, -0.0403f, 0.5f },
}};
static constexpr mat3x4 g_rgb2yuv_srgb_to_bt709_full = {{
{ 0.2126f, 0.7152f, 0.0722f, 0.0f },
{ -0.1146f, -0.3854f, 0.5000f, 0.5f },
{ 0.5000f, -0.4542f, -0.0458f, 0.5f },
}};
static const mat3x4& colorspace_to_conversion_from_srgb_matrix(EStreamColorspace colorspace) {
switch (colorspace) {
default:
case k_EStreamColorspace_BT601: return g_rgb2yuv_srgb_to_bt601_limited;
case k_EStreamColorspace_BT601_Full: return g_rgb2yuv_srgb_to_bt601;
case k_EStreamColorspace_BT709: return g_rgb2yuv_srgb_to_bt709_limited;
case k_EStreamColorspace_BT709_Full: return g_rgb2yuv_srgb_to_bt709_full;
}
}
PFN_vkGetInstanceProcAddr g_pfn_vkGetInstanceProcAddr;
PFN_vkCreateInstance g_pfn_vkCreateInstance;
static VkResult vulkan_load_module()
{
static VkResult s_result = []()
{
void* pModule = dlopen( "libvulkan.so.1", RTLD_NOW | RTLD_LOCAL );
if ( !pModule )
pModule = dlopen( "libvulkan.so", RTLD_NOW | RTLD_LOCAL );
if ( !pModule )
return VK_ERROR_INITIALIZATION_FAILED;
g_pfn_vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)dlsym( pModule, "vkGetInstanceProcAddr" );
if ( !g_pfn_vkGetInstanceProcAddr )
return VK_ERROR_INITIALIZATION_FAILED;
g_pfn_vkCreateInstance = (PFN_vkCreateInstance) g_pfn_vkGetInstanceProcAddr( nullptr, "vkCreateInstance" );
if ( !g_pfn_vkCreateInstance )
return VK_ERROR_INITIALIZATION_FAILED;
return VK_SUCCESS;
}();
return s_result;
}
VulkanOutput_t g_output;
uint32_t g_uCompositeDebug = 0u;
gamescope::ConVar<uint32_t> cv_composite_debug{ "composite_debug", 0, "Debug composition flags" };
static std::map< VkFormat, std::map< uint64_t, VkDrmFormatModifierPropertiesEXT > > DRMModifierProps = {};
static std::unordered_map<uint32_t, std::vector<uint64_t>> s_SampledModifierFormats = {};
static struct wlr_drm_format_set sampledShmFormats = {};
static struct wlr_drm_format_set sampledDRMFormats = {};
std::span<const uint64_t> GetSupportedSampleModifiers( uint32_t uDrmFormat )
{
auto iter = s_SampledModifierFormats.find( uDrmFormat );
if ( iter == s_SampledModifierFormats.end() )
return std::span<const uint64_t>{};
return std::span<const uint64_t>{ iter->second.begin(), iter->second.end() };
}
static LogScope vk_log("vulkan");
static void vk_errorf(VkResult result, const char *fmt, ...) {
static char buf[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
vk_log.errorf("%s (VkResult: %d)", buf, result);
}
// For when device is up and it would be totally fatal to fail
#define vk_check( x ) \
do \
{ \
VkResult check_res = VK_SUCCESS; \
if ( ( check_res = ( x ) ) != VK_SUCCESS ) \
{ \
vk_errorf( check_res, #x " failed!" ); \
abort(); \
} \
} while ( 0 )
template<typename Target, typename Base>
Target *pNextFind(const Base *base, VkStructureType sType)
{
for ( ; base; base = (const Base *)base->pNext )
{
if (base->sType == sType)
return (Target *) base;
}
return nullptr;
}
#define VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA (VkStructureType)1000001002
#define VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA (VkStructureType)1000001003
struct wsi_image_create_info {
VkStructureType sType;
const void *pNext;
bool scanout;
uint32_t modifier_count;
const uint64_t *modifiers;
};
struct wsi_memory_allocate_info {
VkStructureType sType;
const void *pNext;
bool implicit_sync;
};
// DRM doesn't have 32bit floating point formats, so add our own
#define DRM_FORMAT_ABGR32323232F fourcc_code('A', 'B', '8', 'F')
#define DRM_FORMAT_R16F fourcc_code('R', '1', '6', 'F')
#define DRM_FORMAT_R32F fourcc_code('R', '3', '2', 'F')
struct {
uint32_t DRMFormat;
VkFormat vkFormat;
VkFormat vkFormatSrgb;
uint32_t bpp;
bool bHasAlpha;
bool internal;
} s_DRMVKFormatTable[] = {
{ DRM_FORMAT_ARGB8888, VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_SRGB, 4, true, false },
{ DRM_FORMAT_XRGB8888, VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_B8G8R8A8_SRGB, 4, false, false },
{ DRM_FORMAT_ABGR8888, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB, 4, true, false },
{ DRM_FORMAT_XBGR8888, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_R8G8B8A8_SRGB, 4, false, false },
{ DRM_FORMAT_RGB565, VK_FORMAT_R5G6B5_UNORM_PACK16, VK_FORMAT_R5G6B5_UNORM_PACK16, 1, false, false },
{ DRM_FORMAT_NV12, VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, 0, false, false },
{ DRM_FORMAT_ABGR16161616F, VK_FORMAT_R16G16B16A16_SFLOAT, VK_FORMAT_R16G16B16A16_SFLOAT, 8, true, false },
{ DRM_FORMAT_XBGR16161616F, VK_FORMAT_R16G16B16A16_SFLOAT, VK_FORMAT_R16G16B16A16_SFLOAT, 8, false, false },
{ DRM_FORMAT_ABGR16161616, VK_FORMAT_R16G16B16A16_UNORM, VK_FORMAT_R16G16B16A16_UNORM, 8, true, false },
{ DRM_FORMAT_XBGR16161616, VK_FORMAT_R16G16B16A16_UNORM, VK_FORMAT_R16G16B16A16_UNORM, 8, false, false },
{ DRM_FORMAT_ABGR2101010, VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_FORMAT_A2B10G10R10_UNORM_PACK32, 4, true, false },
{ DRM_FORMAT_XBGR2101010, VK_FORMAT_A2B10G10R10_UNORM_PACK32, VK_FORMAT_A2B10G10R10_UNORM_PACK32, 4, false, false },
{ DRM_FORMAT_ARGB2101010, VK_FORMAT_A2R10G10B10_UNORM_PACK32, VK_FORMAT_A2R10G10B10_UNORM_PACK32, 4, true, false },
{ DRM_FORMAT_XRGB2101010, VK_FORMAT_A2R10G10B10_UNORM_PACK32, VK_FORMAT_A2R10G10B10_UNORM_PACK32, 4, false, false },
{ DRM_FORMAT_R8, VK_FORMAT_R8_UNORM, VK_FORMAT_R8_UNORM, 1, false, true },
{ DRM_FORMAT_R16, VK_FORMAT_R16_UNORM, VK_FORMAT_R16_UNORM, 2, false, true },
{ DRM_FORMAT_GR88, VK_FORMAT_R8G8_UNORM, VK_FORMAT_R8G8_UNORM, 2, false, true },
{ DRM_FORMAT_GR1616, VK_FORMAT_R16G16_UNORM, VK_FORMAT_R16G16_UNORM, 4, false, true },
{ DRM_FORMAT_ABGR32323232F, VK_FORMAT_R32G32B32A32_SFLOAT, VK_FORMAT_R32G32B32A32_SFLOAT, 16,true, true },
{ DRM_FORMAT_R16F, VK_FORMAT_R16_SFLOAT, VK_FORMAT_R16_SFLOAT, 2, false, true },
{ DRM_FORMAT_R32F, VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32_SFLOAT, 4, false, true },
{ DRM_FORMAT_INVALID, VK_FORMAT_UNDEFINED, VK_FORMAT_UNDEFINED, false, true },
};
uint32_t VulkanFormatToDRM( VkFormat vkFormat, std::optional<bool> obHasAlphaOverride )
{
for ( int i = 0; s_DRMVKFormatTable[i].vkFormat != VK_FORMAT_UNDEFINED; i++ )
{
if ( ( s_DRMVKFormatTable[i].vkFormat == vkFormat || s_DRMVKFormatTable[i].vkFormatSrgb == vkFormat ) && ( !obHasAlphaOverride || s_DRMVKFormatTable[i].bHasAlpha == *obHasAlphaOverride ) )
{
return s_DRMVKFormatTable[i].DRMFormat;
}
}
return DRM_FORMAT_INVALID;
}
VkFormat DRMFormatToVulkan( uint32_t nDRMFormat, bool bSrgb )
{
for ( int i = 0; s_DRMVKFormatTable[i].vkFormat != VK_FORMAT_UNDEFINED; i++ )
{
if ( s_DRMVKFormatTable[i].DRMFormat == nDRMFormat )
{
return bSrgb ? s_DRMVKFormatTable[i].vkFormatSrgb : s_DRMVKFormatTable[i].vkFormat;
}
}
return VK_FORMAT_UNDEFINED;
}
bool DRMFormatHasAlpha( uint32_t nDRMFormat )
{
for ( int i = 0; s_DRMVKFormatTable[i].vkFormat != VK_FORMAT_UNDEFINED; i++ )
{
if ( s_DRMVKFormatTable[i].DRMFormat == nDRMFormat )
{
return s_DRMVKFormatTable[i].bHasAlpha;
}
}
return false;
}
uint32_t DRMFormatGetBPP( uint32_t nDRMFormat )
{
for ( int i = 0; s_DRMVKFormatTable[i].vkFormat != VK_FORMAT_UNDEFINED; i++ )
{
if ( s_DRMVKFormatTable[i].DRMFormat == nDRMFormat )
{
return s_DRMVKFormatTable[i].bpp;
}
}
return false;
}
bool CVulkanDevice::BInit(VkInstance instance, VkSurfaceKHR surface)
{
assert(instance);
assert(!m_bInitialized);
g_output.surface = surface;
m_instance = instance;
#define VK_FUNC(x) vk.x = (PFN_vk##x) g_pfn_vkGetInstanceProcAddr(instance, "vk"#x);
VULKAN_INSTANCE_FUNCTIONS
#undef VK_FUNC
if (!selectPhysDev(surface))
return false;
if (!createDevice())
return false;
if (!createLayouts())
return false;
if (!createPools())
return false;
if (!createShaders())
return false;
if (!createScratchResources())
return false;
m_bInitialized = true;
std::thread piplelineThread([this](){compileAllPipelines();});
piplelineThread.detach();
g_reshadeManager.init(this);
return true;
}
extern bool env_to_bool(const char *env);
bool CVulkanDevice::selectPhysDev(VkSurfaceKHR surface)
{
uint32_t deviceCount = 0;
vk.EnumeratePhysicalDevices(instance(), &deviceCount, nullptr);
std::vector<VkPhysicalDevice> physDevs(deviceCount);
vk.EnumeratePhysicalDevices(instance(), &deviceCount, physDevs.data());
if (deviceCount < physDevs.size())
physDevs.resize(deviceCount);
bool bTryComputeOnly = true;
// In theory vkBasalt might want to filter out compute-only queue families to force our hand here
const char *pchEnableVkBasalt = getenv( "ENABLE_VKBASALT" );
if ( pchEnableVkBasalt != nullptr && pchEnableVkBasalt[0] == '1' )
{
bTryComputeOnly = false;
}
for (auto cphysDev : physDevs)
{
VkPhysicalDeviceProperties deviceProperties;
vk.GetPhysicalDeviceProperties(cphysDev, &deviceProperties);
if (deviceProperties.apiVersion < VK_API_VERSION_1_2)
continue;
uint32_t queueFamilyCount = 0;
vk.GetPhysicalDeviceQueueFamilyProperties(cphysDev, &queueFamilyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilyProperties(queueFamilyCount);
vk.GetPhysicalDeviceQueueFamilyProperties(cphysDev, &queueFamilyCount, queueFamilyProperties.data());
uint32_t generalIndex = ~0u;
uint32_t computeOnlyIndex = ~0u;
for (uint32_t i = 0; i < queueFamilyCount; ++i) {
const VkQueueFlags generalBits = VK_QUEUE_COMPUTE_BIT | VK_QUEUE_GRAPHICS_BIT;
if ((queueFamilyProperties[i].queueFlags & generalBits) == generalBits )
generalIndex = std::min(generalIndex, i);
else if (bTryComputeOnly && queueFamilyProperties[i].queueFlags & VK_QUEUE_COMPUTE_BIT)
computeOnlyIndex = std::min(computeOnlyIndex, i);
}
if (generalIndex != ~0u || computeOnlyIndex != ~0u)
{
// Select the device if it's the first one or the preferred one
if (!m_physDev ||
(g_preferVendorID == deviceProperties.vendorID && g_preferDeviceID == deviceProperties.deviceID))
{
// if we have a surface, check that the queue family can actually present on it
if (surface) {
VkBool32 canPresent = false;
vk.GetPhysicalDeviceSurfaceSupportKHR( cphysDev, generalIndex, surface, &canPresent );
if ( !canPresent )
{
vk_log.infof( "physical device %04x:%04x queue doesn't support presenting on our surface, testing next one..", deviceProperties.vendorID, deviceProperties.deviceID );
continue;
}
if (computeOnlyIndex != ~0u)
{
vk.GetPhysicalDeviceSurfaceSupportKHR( cphysDev, computeOnlyIndex, surface, &canPresent );
if ( !canPresent )
{
vk_log.infof( "physical device %04x:%04x compute queue doesn't support presenting on our surface, using graphics queue", deviceProperties.vendorID, deviceProperties.deviceID );
computeOnlyIndex = ~0u;
}
}
}
m_queueFamily = computeOnlyIndex == ~0u ? generalIndex : computeOnlyIndex;
m_generalQueueFamily = generalIndex;
m_physDev = cphysDev;
/* When Intel uses compute-only queue for Gamescope composition, some games
* experience performance loss. Using the general queue alleviates the issue
* for now.
* See: https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/4452
*
* TODO: Remove vendorID check for Intel once issue is resolved.
*/
if (deviceProperties.vendorID == 0x8086) /* Intel */
{
vk_log.infof("Intel device detected, forcing general queue family instead of compute-only queue");
m_queueFamily = generalIndex;
}
else if ( env_to_bool( getenv( "GAMESCOPE_FORCE_GENERAL_QUEUE" ) ) )
m_queueFamily = generalIndex;
}
}
}
if (!m_physDev)
{
vk_log.errorf("failed to find physical device");
return false;
}
VkPhysicalDeviceProperties props;
vk.GetPhysicalDeviceProperties( m_physDev, &props );
vk_log.infof( "selecting physical device '%s': queue family %x (general queue family %x)", props.deviceName, m_queueFamily, m_generalQueueFamily );
return true;
}
bool CVulkanDevice::createDevice()
{
vk.GetPhysicalDeviceMemoryProperties( physDev(), &m_memoryProperties );
uint32_t supportedExtensionCount;
vk.EnumerateDeviceExtensionProperties( physDev(), NULL, &supportedExtensionCount, NULL );
std::vector<VkExtensionProperties> supportedExts(supportedExtensionCount);
vk.EnumerateDeviceExtensionProperties( physDev(), NULL, &supportedExtensionCount, supportedExts.data() );
bool hasDrmProps = false;
bool supportsForeignQueue = false;
bool supportsHDRMetadata = false;
for ( uint32_t i = 0; i < supportedExtensionCount; ++i )
{
if ( strcmp(supportedExts[i].extensionName,
VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME) == 0 )
m_bSupportsModifiers = true;
if ( strcmp(supportedExts[i].extensionName,
VK_EXT_PHYSICAL_DEVICE_DRM_EXTENSION_NAME) == 0 )
hasDrmProps = true;
if ( strcmp(supportedExts[i].extensionName,
VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME) == 0 )
supportsForeignQueue = true;
if ( strcmp(supportedExts[i].extensionName,
VK_EXT_HDR_METADATA_EXTENSION_NAME) == 0 )
supportsHDRMetadata = true;
}
vk_log.infof( "physical device %s DRM format modifiers", m_bSupportsModifiers ? "supports" : "does not support" );
if ( !GetBackend()->ValidPhysicalDevice( physDev() ) )
return false;
#if HAVE_DRM
// XXX(JoshA): Move this to ValidPhysicalDevice.
// We need to refactor some Vulkan stuff to do that though.
if ( hasDrmProps )
{
VkPhysicalDeviceDrmPropertiesEXT drmProps = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRM_PROPERTIES_EXT,
};
VkPhysicalDeviceProperties2 props2 = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
.pNext = &drmProps,
};
vk.GetPhysicalDeviceProperties2( physDev(), &props2 );
if ( !GetBackend()->UsesVulkanSwapchain() && !drmProps.hasPrimary ) {
vk_log.errorf( "physical device has no primary node" );
return false;
}
if ( !drmProps.hasRender ) {
vk_log.errorf( "physical device has no render node" );
return false;
}
dev_t renderDevId = makedev( drmProps.renderMajor, drmProps.renderMinor );
drmDevice *drmDev = nullptr;
if (drmGetDeviceFromDevId(renderDevId, 0, &drmDev) != 0) {
vk_log.errorf( "drmGetDeviceFromDevId() failed" );
return false;
}
assert(drmDev->available_nodes & (1 << DRM_NODE_RENDER));
const char *drmRenderName = drmDev->nodes[DRM_NODE_RENDER];
m_drmRendererFd = open( drmRenderName, O_RDWR | O_CLOEXEC );
drmFreeDevice(&drmDev);
if ( m_drmRendererFd < 0 ) {
vk_log.errorf_errno( "failed to open DRM render node" );
return false;
}
if ( drmProps.hasPrimary ) {
m_bHasDrmPrimaryDevId = true;
m_drmPrimaryDevId = makedev( drmProps.primaryMajor, drmProps.primaryMinor );
}
}
else
#endif
{
vk_log.errorf( "physical device doesn't support VK_EXT_physical_device_drm" );
return false;
}
if ( m_bSupportsModifiers && !supportsForeignQueue ) {
vk_log.infof( "The vulkan driver does not support foreign queues,"
" disabling modifier support.");
m_bSupportsModifiers = false;
}
{
VkPhysicalDeviceVulkan12Features vulkan12Features = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
};
VkPhysicalDeviceFeatures2 features2 = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
.pNext = &vulkan12Features,
};
vk.GetPhysicalDeviceFeatures2( physDev(), &features2 );
m_bSupportsFp16 = vulkan12Features.shaderFloat16 && features2.features.shaderInt16;
}
float queuePriorities = 1.0f;
VkDeviceQueueGlobalPriorityCreateInfoEXT queueCreateInfoEXT = {
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT,
.pNext = nullptr,
.globalPriority = VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT
};
VkDeviceQueueCreateInfo queueCreateInfos[2] =
{
{
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.pNext = gamescope::Process::HasCapSysNice() ? &queueCreateInfoEXT : nullptr,
.queueFamilyIndex = m_queueFamily,
.queueCount = 1,
.pQueuePriorities = &queuePriorities
},
{
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.pNext = gamescope::Process::HasCapSysNice() ? &queueCreateInfoEXT : nullptr,
.queueFamilyIndex = m_generalQueueFamily,
.queueCount = 1,
.pQueuePriorities = &queuePriorities
},
};
std::vector< const char * > enabledExtensions;
if ( GetBackend()->UsesVulkanSwapchain() )
{
enabledExtensions.push_back( VK_KHR_SWAPCHAIN_EXTENSION_NAME );
enabledExtensions.push_back( VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME );
enabledExtensions.push_back( VK_KHR_PRESENT_ID_EXTENSION_NAME );
enabledExtensions.push_back( VK_KHR_PRESENT_WAIT_EXTENSION_NAME );
}
if ( m_bSupportsModifiers )
{
enabledExtensions.push_back( VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME );
enabledExtensions.push_back( VK_EXT_QUEUE_FAMILY_FOREIGN_EXTENSION_NAME );
}
enabledExtensions.push_back( VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME );
enabledExtensions.push_back( VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME );
enabledExtensions.push_back( VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME );
enabledExtensions.push_back( VK_EXT_ROBUSTNESS_2_EXTENSION_NAME );
#if 0
enabledExtensions.push_back( VK_KHR_MAINTENANCE_5_EXTENSION_NAME );
#endif
if ( supportsHDRMetadata )
enabledExtensions.push_back( VK_EXT_HDR_METADATA_EXTENSION_NAME );
for ( auto& extension : GetBackend()->GetDeviceExtensions( physDev() ) )
enabledExtensions.push_back( extension );
uint32_t devExtPropCount = 0;
vk.EnumerateDeviceExtensionProperties( physDev(), nullptr, &devExtPropCount, nullptr );
std::vector<VkExtensionProperties> devExtProp( devExtPropCount );
vk.EnumerateDeviceExtensionProperties( physDev(), nullptr, &devExtPropCount, devExtProp.data() );
bool anyMissing = false;
for ( auto& requiredExt : enabledExtensions ) {
bool extFound = false;
for ( auto & availableExt : devExtProp ) {
if ( strcmp( requiredExt, availableExt.extensionName ) == 0 ) {
extFound = true;
break;
}
}
if ( !extFound ) {
vk_log.errorf( "Missing required extension: %s", requiredExt );
anyMissing = true;
}
}
if ( anyMissing )
return false;
#if 0
VkPhysicalDeviceMaintenance5FeaturesKHR maintenance5 = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES_KHR,
.maintenance5 = VK_TRUE,
};
#endif
VkPhysicalDeviceVulkan13Features features13 = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES,
#if 0
.pNext = &maintenance5,
#endif
.dynamicRendering = VK_TRUE,
};
VkPhysicalDevicePresentWaitFeaturesKHR presentWaitFeatures = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_WAIT_FEATURES_KHR,
.pNext = &features13,
.presentWait = VK_TRUE,
};
VkPhysicalDevicePresentIdFeaturesKHR presentIdFeatures = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_ID_FEATURES_KHR,
.pNext = &presentWaitFeatures,
.presentId = VK_TRUE,
};
VkPhysicalDeviceFeatures2 features2 = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
.pNext = &presentIdFeatures,
.features = {
.shaderInt16 = m_bSupportsFp16,
},
};
VkDeviceCreateInfo deviceCreateInfo = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.pNext = &features2,
.queueCreateInfoCount = m_queueFamily == m_generalQueueFamily ? 1u : 2u,
.pQueueCreateInfos = queueCreateInfos,
.enabledExtensionCount = (uint32_t)enabledExtensions.size(),
.ppEnabledExtensionNames = enabledExtensions.data(),
};
VkPhysicalDeviceVulkan12Features vulkan12Features = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
.pNext = std::exchange(features2.pNext, &vulkan12Features),
.shaderFloat16 = m_bSupportsFp16,
.scalarBlockLayout = VK_TRUE,
.timelineSemaphore = VK_TRUE,
};
VkPhysicalDeviceSamplerYcbcrConversionFeatures ycbcrFeatures = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES,
.pNext = std::exchange(features2.pNext, &ycbcrFeatures),
.samplerYcbcrConversion = VK_TRUE,
};
VkPhysicalDeviceRobustness2FeaturesEXT robustness2Features = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT,
.pNext = std::exchange(features2.pNext, &robustness2Features),
.nullDescriptor = VK_TRUE,
};
VkResult res = vk.CreateDevice(physDev(), &deviceCreateInfo, nullptr, &m_device);
if ( res == VK_ERROR_NOT_PERMITTED_KHR && gamescope::Process::HasCapSysNice() )
{
fprintf(stderr, "vkCreateDevice failed with a high-priority queue (general + compute). Falling back to regular priority (general).\n");
queueCreateInfos[1].pNext = nullptr;
res = vk.CreateDevice(physDev(), &deviceCreateInfo, nullptr, &m_device);
if ( res == VK_ERROR_NOT_PERMITTED_KHR && gamescope::Process::HasCapSysNice() )
{
fprintf(stderr, "vkCreateDevice failed with a high-priority queue (compute). Falling back to regular priority (all).\n");
queueCreateInfos[0].pNext = nullptr;
res = vk.CreateDevice(physDev(), &deviceCreateInfo, nullptr, &m_device);
}
}
if ( res != VK_SUCCESS )
{
vk_errorf( res, "vkCreateDevice failed" );
return false;
}
#define VK_FUNC(x) vk.x = (PFN_vk##x) vk.GetDeviceProcAddr(device(), "vk"#x);
VULKAN_DEVICE_FUNCTIONS
#undef VK_FUNC
vk.GetDeviceQueue(device(), m_queueFamily, 0, &m_queue);
if ( m_queueFamily == m_generalQueueFamily )
m_generalQueue = m_queue;
else
vk.GetDeviceQueue(device(), m_generalQueueFamily, 0, &m_generalQueue);
return true;
}
static VkSamplerYcbcrModelConversion colorspaceToYCBCRModel( EStreamColorspace colorspace )
{
switch (colorspace)
{
default:
case k_EStreamColorspace_Unknown:
return VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709;
case k_EStreamColorspace_BT601:
case k_EStreamColorspace_BT601_Full:
return VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_601;
case k_EStreamColorspace_BT709:
case k_EStreamColorspace_BT709_Full:
return VK_SAMPLER_YCBCR_MODEL_CONVERSION_YCBCR_709;
}
}
static VkSamplerYcbcrRange colorspaceToYCBCRRange( EStreamColorspace colorspace )
{
switch (colorspace)
{
default:
case k_EStreamColorspace_Unknown:
return VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
case k_EStreamColorspace_BT709:
case k_EStreamColorspace_BT601:
return VK_SAMPLER_YCBCR_RANGE_ITU_NARROW;
case k_EStreamColorspace_BT601_Full:
case k_EStreamColorspace_BT709_Full:
return VK_SAMPLER_YCBCR_RANGE_ITU_FULL;
}
}
bool CVulkanDevice::createLayouts()
{
VkFormatProperties nv12Properties;
vk.GetPhysicalDeviceFormatProperties(physDev(), VK_FORMAT_G8_B8R8_2PLANE_420_UNORM, &nv12Properties);
bool cosited = nv12Properties.optimalTilingFeatures & VK_FORMAT_FEATURE_COSITED_CHROMA_SAMPLES_BIT;
VkSamplerYcbcrConversionCreateInfo ycbcrSamplerConversionCreateInfo =
{
.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_CREATE_INFO,
.format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM,
.ycbcrModel = colorspaceToYCBCRModel( g_ForcedNV12ColorSpace ),
.ycbcrRange = colorspaceToYCBCRRange( g_ForcedNV12ColorSpace ),
.xChromaOffset = cosited ? VK_CHROMA_LOCATION_COSITED_EVEN : VK_CHROMA_LOCATION_MIDPOINT,
.yChromaOffset = cosited ? VK_CHROMA_LOCATION_COSITED_EVEN : VK_CHROMA_LOCATION_MIDPOINT,
.chromaFilter = VK_FILTER_LINEAR,
.forceExplicitReconstruction = VK_FALSE,
};
vk.CreateSamplerYcbcrConversion( device(), &ycbcrSamplerConversionCreateInfo, nullptr, &m_ycbcrConversion );
VkSamplerYcbcrConversionInfo ycbcrSamplerConversionInfo = {
.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_INFO,
.conversion = m_ycbcrConversion,
};
VkSamplerCreateInfo ycbcrSamplerInfo = {
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
.pNext = &ycbcrSamplerConversionInfo,
.magFilter = VK_FILTER_LINEAR,
.minFilter = VK_FILTER_LINEAR,
.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK,
};
vk.CreateSampler( device(), &ycbcrSamplerInfo, nullptr, &m_ycbcrSampler );
// Create an array of our ycbcrSampler to fill up
std::array<VkSampler, VKR_SAMPLER_SLOTS> ycbcrSamplers;
for (auto& sampler : ycbcrSamplers)
sampler = m_ycbcrSampler;
std::array<VkDescriptorSetLayoutBinding, 7 > layoutBindings = {
VkDescriptorSetLayoutBinding {
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
},
VkDescriptorSetLayoutBinding {
.binding = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
},
VkDescriptorSetLayoutBinding {
.binding = 2,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
},
VkDescriptorSetLayoutBinding {
.binding = 3,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = VKR_SAMPLER_SLOTS,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
},
VkDescriptorSetLayoutBinding {
.binding = 4,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = VKR_SAMPLER_SLOTS,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
.pImmutableSamplers = ycbcrSamplers.data(),
},
VkDescriptorSetLayoutBinding {
.binding = 5,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = VKR_LUT3D_COUNT,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
},
VkDescriptorSetLayoutBinding {
.binding = 6,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = VKR_LUT3D_COUNT,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
},
};
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo =
{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.bindingCount = (uint32_t)layoutBindings.size(),
.pBindings = layoutBindings.data()
};
VkResult res = vk.CreateDescriptorSetLayout(device(), &descriptorSetLayoutCreateInfo, 0, &m_descriptorSetLayout);
if ( res != VK_SUCCESS )
{
vk_errorf( res, "vkCreateDescriptorSetLayout failed" );
return false;
}
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = {
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.setLayoutCount = 1,
.pSetLayouts = &m_descriptorSetLayout,
};
res = vk.CreatePipelineLayout(device(), &pipelineLayoutCreateInfo, nullptr, &m_pipelineLayout);
if ( res != VK_SUCCESS )
{
vk_errorf( res, "vkCreatePipelineLayout failed" );
return false;
}
return true;
}
bool CVulkanDevice::createPools()
{
VkCommandPoolCreateInfo commandPoolCreateInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
.queueFamilyIndex = m_queueFamily,
};
VkResult res = vk.CreateCommandPool(device(), &commandPoolCreateInfo, nullptr, &m_commandPool);
if ( res != VK_SUCCESS )
{
vk_errorf( res, "vkCreateCommandPool failed" );
return false;
}
VkCommandPoolCreateInfo generalCommandPoolCreateInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
.queueFamilyIndex = m_generalQueueFamily,
};
res = vk.CreateCommandPool(device(), &generalCommandPoolCreateInfo, nullptr, &m_generalCommandPool);
if ( res != VK_SUCCESS )
{
vk_errorf( res, "vkCreateCommandPool failed" );
return false;
}
VkPhysicalDeviceImageFormatInfo2 imageFormatInfo = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2,
.format = VK_FORMAT_G8_B8R8_2PLANE_420_UNORM,
.type = VK_IMAGE_TYPE_2D,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = VK_IMAGE_USAGE_SAMPLED_BIT,
};
VkSamplerYcbcrConversionImageFormatProperties ycbcrProps = {
.sType = VK_STRUCTURE_TYPE_SAMPLER_YCBCR_CONVERSION_IMAGE_FORMAT_PROPERTIES,
};
VkImageFormatProperties2 imageFormatProps = {
.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2,
.pNext = &ycbcrProps,
};
res = vk.GetPhysicalDeviceImageFormatProperties2( physDev(), &imageFormatInfo, &imageFormatProps );
VkDescriptorPoolSize poolSizes[3] {
{
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
uint32_t(m_descriptorSets.size()),
},
{
VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
uint32_t(m_descriptorSets.size()) * 2,
},
{
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
uint32_t(m_descriptorSets.size()) * (((ycbcrProps.combinedImageSamplerDescriptorCount + 1) * VKR_SAMPLER_SLOTS) + (2 * VKR_LUT3D_COUNT)),
},
};
VkDescriptorPoolCreateInfo descriptorPoolCreateInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.maxSets = uint32_t(m_descriptorSets.size()),
.poolSizeCount = sizeof(poolSizes) / sizeof(poolSizes[0]),
.pPoolSizes = poolSizes,
};
res = vk.CreateDescriptorPool(device(), &descriptorPoolCreateInfo, nullptr, &m_descriptorPool);
if ( res != VK_SUCCESS )
{
vk_errorf( res, "vkCreateDescriptorPool failed" );
return false;
}
return true;
}
bool CVulkanDevice::createShaders()
{
struct ShaderInfo_t
{
const uint32_t* spirv;
uint32_t size;
};
std::array<ShaderInfo_t, SHADER_TYPE_COUNT> shaderInfos;
#define SHADER(type, array) shaderInfos[SHADER_TYPE_##type] = {array , sizeof(array)}
SHADER(BLIT, cs_composite_blit);
SHADER(BLUR, cs_composite_blur);
SHADER(BLUR_COND, cs_composite_blur_cond);
SHADER(BLUR_FIRST_PASS, cs_gaussian_blur_horizontal);
SHADER(RCAS, cs_composite_rcas);
if (m_bSupportsFp16)
{
SHADER(EASU, cs_easu_fp16);
SHADER(NIS, cs_nis_fp16);
}
else
{
SHADER(EASU, cs_easu);
SHADER(NIS, cs_nis);
}
SHADER(RGB_TO_NV12, cs_rgb_to_nv12);
#undef SHADER
for (uint32_t i = 0; i < shaderInfos.size(); i++)
{
VkShaderModuleCreateInfo shaderCreateInfo = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = shaderInfos[i].size,
.pCode = shaderInfos[i].spirv,
};
VkResult res = vk.CreateShaderModule(device(), &shaderCreateInfo, nullptr, &m_shaderModules[i]);
if ( res != VK_SUCCESS )
{
vk_errorf( res, "vkCreateShaderModule failed" );
return false;
}
}
return true;
}
bool CVulkanDevice::createScratchResources()
{
std::vector<VkDescriptorSetLayout> descriptorSetLayouts(m_descriptorSets.size(), m_descriptorSetLayout);
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.descriptorPool = m_descriptorPool,
.descriptorSetCount = (uint32_t)descriptorSetLayouts.size(),
.pSetLayouts = descriptorSetLayouts.data(),
};