forked from ValveSoftware/steamos-compositor
-
Notifications
You must be signed in to change notification settings - Fork 181
/
rendervulkan.cpp
1747 lines (1301 loc) · 44.1 KB
/
rendervulkan.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
// Initialize Vulkan and composite stuff with a compute queue
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "rendervulkan.hpp"
#include "main.hpp"
#include "steamcompmgr.hpp"
#include "composite.h"
PFN_vkGetMemoryFdKHR dyn_vkGetMemoryFdKHR;
PFN_vkGetFenceFdKHR dyn_vkGetFenceFdKHR;
const VkApplicationInfo appInfo = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pApplicationName = "gamescope",
.applicationVersion = VK_MAKE_VERSION(1, 0, 0),
.pEngineName = "just some code",
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
.apiVersion = VK_API_VERSION_1_0,
};
std::vector< const char * > g_vecSDLInstanceExts;
VkInstance instance;
#define k_nMaxSets 8 // should only need one or two per output tops
struct VulkanOutput_t
{
VkSurfaceKHR surface;
VkSurfaceCapabilitiesKHR surfaceCaps;
std::vector< VkSurfaceFormatKHR > surfaceFormats;
std::vector< VkPresentModeKHR > presentModes;
uint32_t nSwapChainImageIndex;
VkSwapchainKHR swapChain;
std::vector< VkImage > swapChainImages;
std::vector< VkImageView > swapChainImageViews;
// If no swapchain, use our own images
int nOutImage; // ping/pong between two RTs
CVulkanTexture outputImage[2];
int nCurCmdBuffer;
VkCommandBuffer commandBuffers[2]; // ping/pong command buffers as well
VkBuffer constantBuffer;
VkDeviceMemory bufferMemory;
Composite_t *pCompositeBuffer;
VkFence fence;
int fenceFD;
};
VkPhysicalDevice physicalDevice;
uint32_t queueFamilyIndex;
VkQueue queue;
VkShaderModule shaderModule;
VkDevice device;
VkCommandPool commandPool;
VkDescriptorPool descriptorPool;
VkDescriptorSetLayout descriptorSetLayout;
VkPipelineLayout pipelineLayout;
VkDescriptorSet descriptorSet;
VkPipeline pipeline;
VkBuffer uploadBuffer;
VkDeviceMemory uploadBufferMemory;
void *pUploadBuffer;
const uint32_t k_nScratchCmdBufferCount = 1000;
struct scratchCmdBuffer_t
{
VkCommandBuffer cmdBuf;
VkFence fence;
std::vector<CVulkanTexture *> refs;
std::atomic<bool> haswaiter;
std::atomic<bool> busy;
};
scratchCmdBuffer_t g_scratchCommandBuffers[ k_nScratchCmdBufferCount ];
struct VkPhysicalDeviceMemoryProperties memoryProperties;
VulkanOutput_t g_output;
std::unordered_map<VulkanTexture_t, CVulkanTexture *> g_mapVulkanTextures;
std::atomic<VulkanTexture_t> g_nMaxVulkanTexHandle;
struct VulkanSamplerCacheEntry_t
{
VulkanPipeline_t::LayerBinding_t key;
VkSampler sampler;
};
std::vector< VulkanSamplerCacheEntry_t > g_vecVulkanSamplerCache;
VulkanTexture_t g_emptyTex;
#define MAX_DEVICE_COUNT 8
#define MAX_QUEUE_COUNT 8
#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;
};
struct {
uint32_t DRMFormat;
VkFormat vkFormat;
bool bNeedsSwizzle;
bool bHasAlpha;
} s_DRMVKFormatTable[] = {
{ DRM_FORMAT_XRGB8888, VK_FORMAT_A8B8G8R8_UNORM_PACK32, true, false },
{ DRM_FORMAT_ARGB8888, VK_FORMAT_A8B8G8R8_UNORM_PACK32, true, true },
{ DRM_FORMAT_ARGB8888, VK_FORMAT_R8G8B8A8_UNORM, false, true }, // TODO: figure out why the cursor surface didn't like DRM_FORMAT_RGBA8888
{ DRM_FORMAT_INVALID, VK_FORMAT_UNDEFINED, false, false },
};
static inline uint32_t VulkanFormatToDRM( VkFormat vkFormat )
{
for ( int i = 0; s_DRMVKFormatTable[i].vkFormat != VK_FORMAT_UNDEFINED; i++ )
{
if ( s_DRMVKFormatTable[i].vkFormat == vkFormat )
{
return s_DRMVKFormatTable[i].DRMFormat;
}
}
return DRM_FORMAT_INVALID;
}
static inline VkFormat DRMFormatToVulkan( 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].vkFormat;
}
}
return VK_FORMAT_UNDEFINED;
}
static inline bool DRMFormatNeedsSwizzle( 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].bNeedsSwizzle;
}
}
return false;
}
static inline 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;
}
int32_t findMemoryType( VkMemoryPropertyFlags properties, uint32_t requiredTypeBits )
{
for ( uint32_t i = 0; i < memoryProperties.memoryTypeCount; i++ )
{
if ( ( ( 1 << i ) & requiredTypeBits ) == 0 )
continue;
if ( ( properties & memoryProperties.memoryTypes[ i ].propertyFlags ) != properties )
continue;
return i;
}
return -1;
}
bool CVulkanTexture::BInit( uint32_t width, uint32_t height, VkFormat format, bool bFlippable, bool bTextureable, wlr_dmabuf_attributes *pDMA /* = nullptr */ )
{
VkResult res = VK_ERROR_INITIALIZATION_FAILED;
VkImageTiling tiling = VK_IMAGE_TILING_OPTIMAL;
VkImageUsageFlags usage = bTextureable ? VK_IMAGE_USAGE_SAMPLED_BIT : VK_IMAGE_USAGE_STORAGE_BIT;
if ( bTextureable == true && pDMA == nullptr )
{
// If we're not importing it, we'll need to copy bits into it later
usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT;
}
VkMemoryPropertyFlags properties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
// Possible extensions for below
wsi_image_create_info wsiImageCreateInfo = {};
VkExternalMemoryImageCreateInfo externalImageCreateInfo = {};
VkImageCreateInfo imageInfo = {};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.extent.width = width;
imageInfo.extent.height = height;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 1;
imageInfo.format = format;
imageInfo.tiling = tiling;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageInfo.usage = usage;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if ( pDMA != nullptr )
{
assert( format == DRMFormatToVulkan( pDMA->format ) );
}
if ( bFlippable == true || pDMA != nullptr )
{
// Either we're scanning out the image, or if we're importing them, they got
// allocated with scanout in mind by their original WSI.
wsiImageCreateInfo.sType = VK_STRUCTURE_TYPE_WSI_IMAGE_CREATE_INFO_MESA;
wsiImageCreateInfo.scanout = VK_TRUE;
wsiImageCreateInfo.pNext = imageInfo.pNext;
imageInfo.pNext = &wsiImageCreateInfo;
}
if ( pDMA != nullptr )
{
externalImageCreateInfo.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO;
externalImageCreateInfo.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
externalImageCreateInfo.pNext = imageInfo.pNext;
imageInfo.pNext = &externalImageCreateInfo;
}
if (vkCreateImage(device, &imageInfo, nullptr, &m_vkImage) != VK_SUCCESS) {
return false;
}
VkMemoryRequirements memRequirements;
vkGetImageMemoryRequirements(device, m_vkImage, &memRequirements);
// Possible pNexts
wsi_memory_allocate_info wsiAllocInfo = {};
VkImportMemoryFdInfoKHR importMemoryInfo = {};
VkExportMemoryAllocateInfo memory_export_info = {};
VkMemoryDedicatedAllocateInfo memory_dedicated_info = {};
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryType(properties, memRequirements.memoryTypeBits );
if ( bFlippable == true || pDMA != nullptr )
{
memory_dedicated_info.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO;
memory_dedicated_info.image = m_vkImage;
memory_dedicated_info.buffer = VK_NULL_HANDLE;
memory_dedicated_info.pNext = allocInfo.pNext;
allocInfo.pNext = &memory_dedicated_info;
}
if ( bFlippable == true && pDMA == nullptr )
{
// We'll export it to DRM
memory_export_info.sType = VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO;
memory_export_info.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
memory_export_info.pNext = allocInfo.pNext;
allocInfo.pNext = &memory_export_info;
}
if ( pDMA != nullptr )
{
assert( pDMA->n_planes == 1 );
// Importing memory from a FD transfers ownership of the FD
int fd = dup( pDMA->fd[0] );
if ( fd < 0 )
{
perror( "dup failed" );
return false;
}
// We're importing WSI buffers from GL or Vulkan, set implicit_sync
wsiAllocInfo.sType = VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA;
wsiAllocInfo.implicit_sync = true;
wsiAllocInfo.pNext = allocInfo.pNext;
allocInfo.pNext = &wsiAllocInfo;
// Memory already provided by pDMA
importMemoryInfo.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR;
importMemoryInfo.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
importMemoryInfo.fd = fd;
importMemoryInfo.pNext = allocInfo.pNext;
allocInfo.pNext = &importMemoryInfo;
}
if (vkAllocateMemory(device, &allocInfo, nullptr, &m_vkImageMemory) != VK_SUCCESS) {
return false;
}
res = vkBindImageMemory(device, m_vkImage, m_vkImageMemory, 0);
if ( res != VK_SUCCESS )
return false;
if ( bFlippable == true )
{
// We assume we own the memory when doing this right now.
// We could support the import scenario as well if needed
// assert( bTextureable == false );
m_DMA.modifier = DRM_FORMAT_MOD_INVALID;
m_DMA.n_planes = 1;
m_DMA.width = width;
m_DMA.height = height;
m_DMA.format = VulkanFormatToDRM( format );
const VkMemoryGetFdInfoKHR memory_get_fd_info = {
.sType = VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR,
.pNext = NULL,
.memory = m_vkImageMemory,
.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
};
res = dyn_vkGetMemoryFdKHR(device, &memory_get_fd_info, &m_DMA.fd[0]);
if ( res != VK_SUCCESS )
return false;
const VkImageSubresource image_subresource = {
.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
.mipLevel = 0,
.arrayLayer = 0,
};
VkSubresourceLayout image_layout;
vkGetImageSubresourceLayout(device, m_vkImage, &image_subresource, &image_layout);
m_DMA.stride[0] = image_layout.rowPitch;
m_FBID = drm_fbid_from_dmabuf( &g_DRM, &m_DMA );
if ( m_FBID == 0 )
return false;
}
bool bSwapChannels = pDMA ? DRMFormatNeedsSwizzle( pDMA->format ) : false;
bool bHasAlpha = pDMA ? DRMFormatHasAlpha( pDMA->format ) : true;
if ( bSwapChannels || !bHasAlpha )
{
// Right now this implies no storage bit - check it now as that's incompatible with swizzle
assert ( bTextureable == true );
}
VkImageViewCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = m_vkImage;
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = format;
createInfo.components.r = bSwapChannels ? VK_COMPONENT_SWIZZLE_B : VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.b = bSwapChannels ? VK_COMPONENT_SWIZZLE_R : VK_COMPONENT_SWIZZLE_IDENTITY;
// createInfo.components.a = bHasAlpha ? VK_COMPONENT_SWIZZLE_IDENTITY : VK_COMPONENT_SWIZZLE_ONE;
createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;
res = vkCreateImageView(device, &createInfo, nullptr, &m_vkImageView);
if ( res != VK_SUCCESS )
return false;
m_bInitialized = true;
m_bFlippable = bFlippable;
return true;
}
CVulkanTexture::~CVulkanTexture( void )
{
if ( m_vkImageView != VK_NULL_HANDLE )
{
vkDestroyImageView( device, m_vkImageView, nullptr );
m_vkImageView = VK_NULL_HANDLE;
}
if ( m_FBID != 0 )
{
drm_drop_fbid( &g_DRM, m_FBID );
m_FBID = 0;
}
if ( m_vkImage != VK_NULL_HANDLE )
{
vkDestroyImage( device, m_vkImage, nullptr );
m_vkImage = VK_NULL_HANDLE;
}
if ( m_vkImageMemory != VK_NULL_HANDLE )
{
vkFreeMemory( device, m_vkImageMemory, nullptr );
m_vkImageMemory = VK_NULL_HANDLE;
}
m_bInitialized = false;
}
int init_device()
{
uint32_t physicalDeviceCount = 0;
VkPhysicalDevice deviceHandles[MAX_DEVICE_COUNT];
VkQueueFamilyProperties queueFamilyProperties[MAX_QUEUE_COUNT];
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, 0);
physicalDeviceCount = physicalDeviceCount > MAX_DEVICE_COUNT ? MAX_DEVICE_COUNT : physicalDeviceCount;
vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, deviceHandles);
for (uint32_t i = 0; i < physicalDeviceCount; ++i)
{
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(deviceHandles[i], &queueFamilyCount, NULL);
queueFamilyCount = queueFamilyCount > MAX_QUEUE_COUNT ? MAX_QUEUE_COUNT : queueFamilyCount;
vkGetPhysicalDeviceQueueFamilyProperties(deviceHandles[i], &queueFamilyCount, queueFamilyProperties);
for (uint32_t j = 0; j < queueFamilyCount; ++j) {
if ( queueFamilyProperties[j].queueFlags & VK_QUEUE_COMPUTE_BIT &&
!(queueFamilyProperties[j].queueFlags & VK_QUEUE_GRAPHICS_BIT ) )
{
queueFamilyIndex = j;
physicalDevice = deviceHandles[i];
}
}
if (physicalDevice)
{
break;
}
}
if (!physicalDevice)
{
fprintf(stderr, "Failed to find physical device\n");
return false;
}
vkGetPhysicalDeviceMemoryProperties( physicalDevice, &memoryProperties );
float queuePriorities = 1.0f;
VkDeviceQueueCreateInfo queueCreateInfo = {
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.queueFamilyIndex = queueFamilyIndex,
.queueCount = 1,
.pQueuePriorities = &queuePriorities
};
std::vector< const char * > vecEnabledDeviceExtensions;
if ( BIsNested() == true )
{
vecEnabledDeviceExtensions.push_back( VK_KHR_SWAPCHAIN_EXTENSION_NAME );
}
vecEnabledDeviceExtensions.push_back( VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME );
vecEnabledDeviceExtensions.push_back( VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME );
vecEnabledDeviceExtensions.push_back( VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME );
vecEnabledDeviceExtensions.push_back( VK_KHR_EXTERNAL_FENCE_EXTENSION_NAME );
vecEnabledDeviceExtensions.push_back( VK_KHR_EXTERNAL_FENCE_FD_EXTENSION_NAME );
VkDeviceCreateInfo deviceCreateInfo = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &queueCreateInfo,
.enabledLayerCount = 0,
.ppEnabledLayerNames = 0,
.enabledExtensionCount = (uint32_t)vecEnabledDeviceExtensions.size(),
.ppEnabledExtensionNames = vecEnabledDeviceExtensions.data(),
.pEnabledFeatures = 0,
};
VkResult result = vkCreateDevice(physicalDevice, &deviceCreateInfo, NULL, &device);
if ( result != VK_SUCCESS )
{
return false;
}
vkGetDeviceQueue(device, queueFamilyIndex, 0, &queue);
if ( queue == VK_NULL_HANDLE )
return false;
VkShaderModuleCreateInfo shaderModuleCreateInfo = {};
shaderModuleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
shaderModuleCreateInfo.codeSize = sizeof(composite_spv);
shaderModuleCreateInfo.pCode = (const uint32_t*)composite_spv;
VkResult res = vkCreateShaderModule( device, &shaderModuleCreateInfo, nullptr, &shaderModule );
if ( res != VK_SUCCESS )
{
return false;
}
VkCommandPoolCreateInfo commandPoolCreateInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
.pNext = nullptr,
.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
.queueFamilyIndex = queueFamilyIndex,
};
res = vkCreateCommandPool(device, &commandPoolCreateInfo, 0, &commandPool);
if ( res != VK_SUCCESS )
{
return false;
}
VkDescriptorPoolSize descriptorPoolSize[] = {
{
VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
k_nMaxSets * 1,
},
{
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
k_nMaxSets * 1,
},
{
VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
k_nMaxSets * k_nMaxLayers,
},
{
VK_DESCRIPTOR_TYPE_SAMPLER,
k_nMaxSets * k_nMaxLayers,
},
};
VkDescriptorPoolCreateInfo descriptorPoolCreateInfo = {
VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
nullptr,
.flags = 0,
.maxSets = k_nMaxSets,
.poolSizeCount = 4,
descriptorPoolSize
};
res = vkCreateDescriptorPool(device, &descriptorPoolCreateInfo, 0, &descriptorPool);
if ( res != VK_SUCCESS )
{
return false;
}
std::vector< VkDescriptorSetLayoutBinding > vecLayoutBindings;
VkDescriptorSetLayoutBinding descriptorSetLayoutBindings =
{
.binding = 0,
.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.descriptorCount = 1,
.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
};
vecLayoutBindings.push_back( descriptorSetLayoutBindings ); // first binding is target storage image
descriptorSetLayoutBindings.binding = 1;
descriptorSetLayoutBindings.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
vecLayoutBindings.push_back( descriptorSetLayoutBindings ); // second binding is composite description buffer
for ( uint32_t i = 0; i < k_nMaxLayers; i++ )
{
descriptorSetLayoutBindings.binding = 2 + ( i * 2 );
descriptorSetLayoutBindings.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
vecLayoutBindings.push_back( descriptorSetLayoutBindings );
descriptorSetLayoutBindings.binding = 2 + ( i * 2 ) + 1;
descriptorSetLayoutBindings.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER;
vecLayoutBindings.push_back( descriptorSetLayoutBindings );
}
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo =
{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.bindingCount = 2 + ( k_nMaxLayers * 2 ),
vecLayoutBindings.data()
};
res = vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCreateInfo, 0, &descriptorSetLayout);
if ( res != VK_SUCCESS )
{
return false;
}
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = {
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
0,
0,
1,
&descriptorSetLayout,
0,
0
};
res = vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, 0, &pipelineLayout);
if ( res != VK_SUCCESS )
{
return false;
}
VkComputePipelineCreateInfo computePipelineCreateInfo = {
VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
0,
0,
{
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
0,
0,
VK_SHADER_STAGE_COMPUTE_BIT,
shaderModule,
"main",
0
},
pipelineLayout,
0,
0
};
res = vkCreateComputePipelines(device, 0, 1, &computePipelineCreateInfo, 0, &pipeline);
if ( res != VK_SUCCESS )
{
return false;
}
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo = {
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
nullptr,
descriptorPool,
1,
&descriptorSetLayout
};
res = vkAllocateDescriptorSets(device, &descriptorSetAllocateInfo, &descriptorSet);
if ( res != VK_SUCCESS || descriptorSet == VK_NULL_HANDLE )
{
return false;
}
// Make and map upload buffer
VkBufferCreateInfo bufferCreateInfo = {};
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferCreateInfo.pNext = nullptr;
bufferCreateInfo.size = 512 * 512 * 4;
bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
result = vkCreateBuffer( device, &bufferCreateInfo, nullptr, &uploadBuffer );
if ( result != VK_SUCCESS )
{
return false;
}
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, uploadBuffer, &memRequirements);
int memTypeIndex = findMemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT|VK_MEMORY_PROPERTY_HOST_COHERENT_BIT|VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, memRequirements.memoryTypeBits );
if ( memTypeIndex == -1 )
{
return false;
}
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = memTypeIndex;
vkAllocateMemory( device, &allocInfo, nullptr, &uploadBufferMemory);
vkBindBufferMemory( device, uploadBuffer, uploadBufferMemory, 0 );
vkMapMemory( device, uploadBufferMemory, 0, VK_WHOLE_SIZE, 0, (void**)&pUploadBuffer );
if ( pUploadBuffer == nullptr )
{
return false;
}
VkCommandBufferAllocateInfo commandBufferAllocateInfo = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.pNext = nullptr,
.commandPool = commandPool,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
.commandBufferCount = 1
};
for ( uint32_t i = 0; i < k_nScratchCmdBufferCount; i++ )
{
result = vkAllocateCommandBuffers( device, &commandBufferAllocateInfo, &g_scratchCommandBuffers[ i ].cmdBuf );
if ( result != VK_SUCCESS )
{
return false;
}
VkFenceCreateInfo fenceCreateInfo =
{
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
};
result = vkCreateFence( device, &fenceCreateInfo, nullptr, &g_scratchCommandBuffers[ i ].fence );
if ( result != VK_SUCCESS )
{
return false;
}
g_scratchCommandBuffers[ i ].busy = false;
}
return true;
}
void fini_device()
{
vkDestroyDevice(device, 0);
}
void acquire_next_image( void )
{
vkAcquireNextImageKHR( device, g_output.swapChain, UINT64_MAX, VK_NULL_HANDLE, VK_NULL_HANDLE, &g_output.nSwapChainImageIndex );
}
void vulkan_present_to_window( void )
{
VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
// presentInfo.waitSemaphoreCount = 1;
// presentInfo.pWaitSemaphores = signalSemaphores;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &g_output.swapChain;
presentInfo.pImageIndices = &g_output.nSwapChainImageIndex;
vkQueuePresentKHR( queue, &presentInfo );
acquire_next_image();
}
bool vulkan_make_swapchain( VulkanOutput_t *pOutput )
{
uint32_t imageCount = pOutput->surfaceCaps.minImageCount + 1;
uint32_t surfaceFormat = 0;
uint32_t formatCount = pOutput->surfaceFormats.size();
VkResult result = VK_SUCCESS;
for ( surfaceFormat = 0; surfaceFormat < formatCount; surfaceFormat++ )
{
if ( pOutput->surfaceFormats[ surfaceFormat ].format == VK_FORMAT_B8G8R8A8_UNORM )
break;
}
if ( surfaceFormat == formatCount )
return false;
VkSwapchainCreateInfoKHR createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.surface = pOutput->surface;
createInfo.minImageCount = imageCount;
createInfo.imageFormat = pOutput->surfaceFormats[ surfaceFormat ].format;
createInfo.imageColorSpace = pOutput->surfaceFormats[surfaceFormat ].colorSpace;
createInfo.imageExtent = { g_nOutputWidth, g_nOutputHeight };
createInfo.imageArrayLayers = 1;
createInfo.imageUsage = VK_IMAGE_USAGE_STORAGE_BIT;
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
createInfo.preTransform = pOutput->surfaceCaps.currentTransform;
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR;
createInfo.clipped = VK_TRUE;
createInfo.oldSwapchain = VK_NULL_HANDLE;
if (vkCreateSwapchainKHR( device, &createInfo, nullptr, &pOutput->swapChain) != VK_SUCCESS ) {
return 0;
}
vkGetSwapchainImagesKHR( device, pOutput->swapChain, &imageCount, nullptr );
pOutput->swapChainImages.resize( imageCount );
pOutput->swapChainImageViews.resize( imageCount );
vkGetSwapchainImagesKHR( device, pOutput->swapChain, &imageCount, pOutput->swapChainImages.data() );
for ( uint32_t i = 0; i < pOutput->swapChainImages.size(); i++ )
{
VkImageViewCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = pOutput->swapChainImages[ i ];
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = pOutput->surfaceFormats[ surfaceFormat ].format;
createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;
result = vkCreateImageView(device, &createInfo, nullptr, &pOutput->swapChainImageViews[ i ]);
if ( result != VK_SUCCESS )
return false;
}
acquire_next_image();
return true;
}
bool vulkan_remake_swapchain( void )
{
VulkanOutput_t *pOutput = &g_output;
vkQueueWaitIdle( queue );
for ( uint32_t i = 0; i < pOutput->swapChainImages.size(); i++ )
{
vkDestroyImageView( device, pOutput->swapChainImageViews[ i ], nullptr );
pOutput->swapChainImageViews[ i ] = VK_NULL_HANDLE;
pOutput->swapChainImages[ i ] = VK_NULL_HANDLE;
}
vkDestroySwapchainKHR( device, pOutput->swapChain, nullptr );
pOutput->nSwapChainImageIndex = 0;
return ( vulkan_make_swapchain( &g_output ) );
}
bool vulkan_make_output( VulkanOutput_t *pOutput )
{
VkResult result;
if ( BIsNested() == true )
{
SDL_Vulkan_CreateSurface( window, instance, &pOutput->surface );
if ( pOutput->surface == VK_NULL_HANDLE )
return false;
result = vkGetPhysicalDeviceSurfaceCapabilitiesKHR( physicalDevice, pOutput->surface, &pOutput->surfaceCaps );
if ( result != VK_SUCCESS )
return false;
uint32_t formatCount = 0;
result = vkGetPhysicalDeviceSurfaceFormatsKHR( physicalDevice, pOutput->surface, &formatCount, nullptr );
if ( result != VK_SUCCESS )
return false;
if ( formatCount != 0 ) {
pOutput->surfaceFormats.resize( formatCount );
vkGetPhysicalDeviceSurfaceFormatsKHR( physicalDevice, pOutput->surface, &formatCount, pOutput->surfaceFormats.data() );
if ( result != VK_SUCCESS )
return false;
}
uint32_t presentModeCount = false;
result = vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, pOutput->surface, &presentModeCount, nullptr );
if ( result != VK_SUCCESS )
return false;
if ( presentModeCount != 0 ) {
pOutput->presentModes.resize(presentModeCount);
result = vkGetPhysicalDeviceSurfacePresentModesKHR( physicalDevice, pOutput->surface, &presentModeCount, pOutput->presentModes.data() );
if ( result != VK_SUCCESS )
return false;
}
bool bRet = vulkan_make_swapchain( pOutput );
assert( bRet == true );
}
else
{
VkFormat imageFormat = DRMFormatToVulkan( g_nDRMFormat );
if ( imageFormat == VK_FORMAT_UNDEFINED )
{
return false;
}
bool bSuccess = pOutput->outputImage[0].BInit( g_nOutputWidth, g_nOutputHeight, imageFormat, true, false );
if ( bSuccess != true )
return false;
bSuccess = pOutput->outputImage[1].BInit( g_nOutputWidth, g_nOutputHeight, imageFormat, true, false );
if ( bSuccess != true )
return false;
}
// Make and map constant buffer
VkBufferCreateInfo bufferCreateInfo = {};
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferCreateInfo.pNext = nullptr;
bufferCreateInfo.size = sizeof( Composite_t );
bufferCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
result = vkCreateBuffer( device, &bufferCreateInfo, nullptr, &pOutput->constantBuffer );
if ( result != VK_SUCCESS )
{
return false;
}
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, pOutput->constantBuffer, &memRequirements);
int memTypeIndex = findMemoryType(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT|VK_MEMORY_PROPERTY_HOST_COHERENT_BIT|VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, memRequirements.memoryTypeBits );
if ( memTypeIndex == -1 )
{
return false;
}
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = memTypeIndex;
vkAllocateMemory( device, &allocInfo, nullptr, &pOutput->bufferMemory );
vkBindBufferMemory( device, pOutput->constantBuffer, pOutput->bufferMemory, 0 );
vkMapMemory( device, pOutput->bufferMemory, 0, VK_WHOLE_SIZE, 0, (void**)&pOutput->pCompositeBuffer );
if ( pOutput->pCompositeBuffer == nullptr )