/
VkGraphicsDevice.cs
1177 lines (1030 loc) · 47.9 KB
/
VkGraphicsDevice.cs
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Vulkan;
using static Veldrid.Vk.VulkanUtil;
using static Vulkan.VulkanNative;
namespace Veldrid.Vk
{
internal unsafe class VkGraphicsDevice : GraphicsDevice
{
private static readonly FixedUtf8String s_name = "Veldrid-VkGraphicsDevice";
private VkInstance _instance;
private VkPhysicalDevice _physicalDevice;
private VkDeviceMemoryManager _memoryManager;
private VkPhysicalDeviceProperties _physicalDeviceProperties;
private VkPhysicalDeviceFeatures _physicalDeviceFeatures;
private VkPhysicalDeviceMemoryProperties _physicalDeviceMemProperties;
private VkDevice _device;
private uint _graphicsQueueIndex;
private uint _presentQueueIndex;
private VkCommandPool _graphicsCommandPool;
private readonly object _graphicsCommandPoolLock = new object();
private VkQueue _graphicsQueue;
private readonly object _graphicsQueueLock = new object();
private VkDebugReportCallbackEXT _debugCallbackHandle;
private PFN_vkDebugReportCallbackEXT _debugCallbackFunc;
private readonly List<VkCommandList> _commandListsToDispose = new List<VkCommandList>();
private bool _debugMarkerEnabled;
private vkDebugMarkerSetObjectNameEXT_d _setObjectNameDelegate;
private const int SharedCommandPoolCount = 4;
private ConcurrentStack<SharedCommandPool> _sharedGraphicsCommandPools = new ConcurrentStack<SharedCommandPool>();
private VkDescriptorPoolManager _descriptorPoolManager;
// Staging Resources
private const uint MinStagingBufferSize = 64;
private const uint MaxStagingBufferSize = 512;
private readonly object _stagingResourcesLock = new object();
private readonly List<VkTexture> _availableStagingTextures = new List<VkTexture>();
private readonly List<VkBuffer> _availableStagingBuffers = new List<VkBuffer>();
private readonly Dictionary<VkCommandBuffer, VkTexture> _submittedStagingTextures
= new Dictionary<VkCommandBuffer, VkTexture>();
private readonly Dictionary<VkCommandBuffer, VkBuffer> _submittedStagingBuffers
= new Dictionary<VkCommandBuffer, VkBuffer>();
private readonly Dictionary<VkCommandBuffer, SharedCommandPool> _submittedSharedCommandPools
= new Dictionary<VkCommandBuffer, SharedCommandPool>();
public override GraphicsBackend BackendType => GraphicsBackend.Vulkan;
public override Swapchain MainSwapchain => _mainSwapchain;
public VkInstance Instance => _instance;
public VkDevice Device => _device;
public VkPhysicalDevice PhysicalDevice => _physicalDevice;
public VkPhysicalDeviceMemoryProperties PhysicalDeviceMemProperties => _physicalDeviceMemProperties;
public VkQueue GraphicsQueue => _graphicsQueue;
public uint GraphicsQueueIndex => _graphicsQueueIndex;
public uint PresentQueueIndex => _presentQueueIndex;
public VkDeviceMemoryManager MemoryManager => _memoryManager;
public VkDescriptorPoolManager DescriptorPoolManager => _descriptorPoolManager;
private readonly object _submittedFencesLock = new object();
private readonly Queue<Vulkan.VkFence> _availableSubmissionFences = new Queue<Vulkan.VkFence>();
private readonly Dictionary<Vulkan.VkFence, (VkCommandList, VkCommandBuffer)> _submittedFences
= new Dictionary<Vulkan.VkFence, (VkCommandList, VkCommandBuffer)>();
private readonly List<KeyValuePair<Vulkan.VkFence, (VkCommandList, VkCommandBuffer)>> _completedFences
= new List<KeyValuePair<Vulkan.VkFence, (VkCommandList, VkCommandBuffer)>>();
private readonly VkSwapchain _mainSwapchain;
public VkGraphicsDevice(GraphicsDeviceOptions options, SwapchainDescription? scDesc)
{
CreateInstance(options.Debug);
VkSurfaceKHR surface = VkSurfaceKHR.Null;
if (scDesc != null)
{
surface = VkSurfaceSource.CreateFromSwapchainSource(scDesc.Value.Source).CreateSurface(_instance);
}
CreatePhysicalDevice();
CreateLogicalDevice(surface);
_memoryManager = new VkDeviceMemoryManager(_device, _physicalDevice);
ResourceFactory = new VkResourceFactory(this);
if (scDesc != null)
{
SwapchainDescription desc = scDesc.Value;
_mainSwapchain = new VkSwapchain(this, ref desc, surface);
}
CreateDescriptorPool();
CreateGraphicsCommandPool();
for (int i = 0; i < SharedCommandPoolCount; i++)
{
_sharedGraphicsCommandPools.Push(new SharedCommandPool(this, true));
}
PostDeviceCreated();
}
public override ResourceFactory ResourceFactory { get; }
protected override void SubmitCommandsCore(
CommandList cl,
Fence fence)
{
SubmitCommandList(cl, 0, null, 0, null, fence);
}
private void SubmitCommandList(
CommandList cl,
uint waitSemaphoreCount,
VkSemaphore* waitSemaphoresPtr,
uint signalSemaphoreCount,
VkSemaphore* signalSemaphoresPtr,
Fence fence)
{
VkCommandList vkCL = Util.AssertSubtype<CommandList, VkCommandList>(cl);
VkCommandBuffer vkCB = vkCL.CommandBuffer;
SubmitCommandBuffer(vkCL, vkCB, waitSemaphoreCount, waitSemaphoresPtr, signalSemaphoreCount, signalSemaphoresPtr, fence);
vkCL.CommandBufferSubmitted();
}
private void SubmitCommandBuffer(
VkCommandList vkCL,
VkCommandBuffer vkCB,
uint waitSemaphoreCount,
VkSemaphore* waitSemaphoresPtr,
uint signalSemaphoreCount,
VkSemaphore* signalSemaphoresPtr,
Fence fence)
{
CheckSubmittedFences();
bool useExtraFence = fence != null;
VkSubmitInfo si = VkSubmitInfo.New();
si.commandBufferCount = 1;
si.pCommandBuffers = &vkCB;
VkPipelineStageFlags waitDstStageMask = VkPipelineStageFlags.ColorAttachmentOutput;
si.pWaitDstStageMask = &waitDstStageMask;
si.pWaitSemaphores = waitSemaphoresPtr;
si.waitSemaphoreCount = waitSemaphoreCount;
si.pSignalSemaphores = signalSemaphoresPtr;
si.signalSemaphoreCount = signalSemaphoreCount;
Vulkan.VkFence vkFence = Vulkan.VkFence.Null;
Vulkan.VkFence submissionFence = Vulkan.VkFence.Null;
if (useExtraFence)
{
vkFence = Util.AssertSubtype<Fence, VkFence>(fence).DeviceFence;
submissionFence = GetFreeSubmissionFence();
}
else
{
vkFence = GetFreeSubmissionFence();
submissionFence = vkFence;
}
lock (_graphicsQueueLock)
{
vkQueueSubmit(_graphicsQueue, 1, ref si, vkFence);
_submittedFences.Add(submissionFence, (vkCL, vkCB));
if (useExtraFence)
{
vkQueueSubmit(_graphicsQueue, 0, null, submissionFence);
}
}
}
private void CheckSubmittedFences()
{
lock (_submittedFencesLock)
{
Debug.Assert(_completedFences.Count == 0);
foreach (KeyValuePair<Vulkan.VkFence, (VkCommandList, VkCommandBuffer)> kvp in _submittedFences)
{
if (vkGetFenceStatus(_device, kvp.Key) == VkResult.Success)
{
_completedFences.Add(kvp);
}
}
foreach (KeyValuePair<Vulkan.VkFence, (VkCommandList, VkCommandBuffer)> kvp in _completedFences)
{
Vulkan.VkFence fence = kvp.Key;
VkCommandBuffer completedCB = kvp.Value.Item2;
kvp.Value.Item1?.CommandBufferCompleted(completedCB);
bool result = _submittedFences.Remove(fence);
Debug.Assert(result);
VkResult resetResult = vkResetFences(_device, 1, ref fence);
CheckResult(resetResult);
_availableSubmissionFences.Enqueue(fence);
lock (_stagingResourcesLock)
{
if (_submittedStagingTextures.TryGetValue(completedCB, out VkTexture stagingTex))
{
_submittedStagingTextures.Remove(completedCB);
_availableStagingTextures.Add(stagingTex);
}
if (_submittedStagingBuffers.TryGetValue(completedCB, out VkBuffer stagingBuffer))
{
_submittedStagingBuffers.Remove(completedCB);
if (stagingBuffer.SizeInBytes <= MaxStagingBufferSize)
{
_availableStagingBuffers.Add(stagingBuffer);
}
else
{
stagingBuffer.Dispose();
}
}
if (_submittedSharedCommandPools.TryGetValue(completedCB, out SharedCommandPool sharedPool))
{
_submittedSharedCommandPools.Remove(completedCB);
lock (_graphicsCommandPoolLock)
{
if (sharedPool.IsCached)
{
sharedPool.Reset();
_sharedGraphicsCommandPools.Push(sharedPool);
}
else
{
sharedPool.Destroy();
}
}
}
}
VkCommandList cl = kvp.Value.Item1;
if (cl != null)
{
lock (_commandListsToDispose)
{
if (cl.SubmittedCommandBufferCount == 0)
{
if (_commandListsToDispose.Remove(cl))
{
cl.DestroyCommandPool();
}
}
}
}
}
_completedFences.Clear();
}
}
private Vulkan.VkFence GetFreeSubmissionFence()
{
if (_availableSubmissionFences.Count > 0)
{
return _availableSubmissionFences.Dequeue();
}
else
{
VkFenceCreateInfo fenceCI = VkFenceCreateInfo.New();
VkResult result = vkCreateFence(_device, ref fenceCI, null, out Vulkan.VkFence ret);
CheckResult(result);
return ret;
}
}
public void EnqueueDisposedCommandBuffer(VkCommandList vkCL)
{
lock (_commandListsToDispose)
{
_commandListsToDispose.Add(vkCL);
}
}
protected override void SwapBuffersCore(Swapchain swapchain)
{
VkSwapchain vkSC = Util.AssertSubtype<Swapchain, VkSwapchain>(swapchain);
VkPresentInfoKHR presentInfo = VkPresentInfoKHR.New();
VkSwapchainKHR deviceSwapchain = vkSC.DeviceSwapchain;
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = &deviceSwapchain;
uint imageIndex = vkSC.ImageIndex;
presentInfo.pImageIndices = &imageIndex;
vkQueuePresentKHR(vkSC.PresentQueue, ref presentInfo);
if (vkSC.AcquireNextImage(_device, VkSemaphore.Null, vkSC.ImageAvailableFence))
{
Vulkan.VkFence fence = vkSC.ImageAvailableFence;
vkWaitForFences(_device, 1, ref fence, true, ulong.MaxValue);
vkResetFences(_device, 1, ref fence);
}
}
internal void SetResourceName(DeviceResource resource, string name)
{
if (_debugMarkerEnabled)
{
switch (resource)
{
case VkBuffer buffer:
SetDebugMarkerName(VkDebugReportObjectTypeEXT.BufferEXT, buffer.DeviceBuffer.Handle, name);
break;
case VkCommandList commandList:
SetDebugMarkerName(
VkDebugReportObjectTypeEXT.CommandBufferEXT,
(ulong)commandList.CommandBuffer.Handle,
string.Format("{0}_CommandBuffer", name));
SetDebugMarkerName(
VkDebugReportObjectTypeEXT.CommandPoolEXT,
commandList.CommandPool.Handle,
string.Format("{0}_CommandPool", name));
break;
case VkFramebuffer framebuffer:
SetDebugMarkerName(
VkDebugReportObjectTypeEXT.FramebufferEXT,
framebuffer.CurrentFramebuffer.Handle,
name);
break;
case VkPipeline pipeline:
SetDebugMarkerName(VkDebugReportObjectTypeEXT.PipelineEXT, pipeline.DevicePipeline.Handle, name);
SetDebugMarkerName(VkDebugReportObjectTypeEXT.PipelineLayoutEXT, pipeline.PipelineLayout.Handle, name);
break;
case VkResourceLayout resourceLayout:
SetDebugMarkerName(
VkDebugReportObjectTypeEXT.DescriptorSetLayoutEXT,
resourceLayout.DescriptorSetLayout.Handle,
name);
break;
case VkResourceSet resourceSet:
SetDebugMarkerName(VkDebugReportObjectTypeEXT.DescriptorSetEXT, resourceSet.DescriptorSet.Handle, name);
break;
case VkSampler sampler:
SetDebugMarkerName(VkDebugReportObjectTypeEXT.SamplerEXT, sampler.DeviceSampler.Handle, name);
break;
case VkShader shader:
SetDebugMarkerName(VkDebugReportObjectTypeEXT.ShaderModuleEXT, shader.ShaderModule.Handle, name);
break;
case VkTexture tex:
SetDebugMarkerName(VkDebugReportObjectTypeEXT.ImageEXT, tex.OptimalDeviceImage.Handle, name);
break;
case VkTextureView texView:
SetDebugMarkerName(VkDebugReportObjectTypeEXT.ImageViewEXT, texView.ImageView.Handle, name);
break;
case VkFence fence:
SetDebugMarkerName(VkDebugReportObjectTypeEXT.FenceEXT, fence.DeviceFence.Handle, name);
break;
case VkSwapchain sc:
SetDebugMarkerName(VkDebugReportObjectTypeEXT.SwapchainKHREXT, sc.DeviceSwapchain.Handle, name);
break;
default:
break;
}
}
}
private void SetDebugMarkerName(VkDebugReportObjectTypeEXT type, ulong target, string name)
{
Debug.Assert(_setObjectNameDelegate != null);
VkDebugMarkerObjectNameInfoEXT nameInfo = VkDebugMarkerObjectNameInfoEXT.New();
nameInfo.objectType = type;
nameInfo.@object = target;
int byteCount = Encoding.UTF8.GetByteCount(name);
byte* utf8Ptr = stackalloc byte[byteCount];
fixed (char* namePtr = name)
{
Encoding.UTF8.GetBytes(namePtr, name.Length, utf8Ptr, byteCount);
nameInfo.pObjectName = utf8Ptr;
VkResult result = _setObjectNameDelegate(_device, &nameInfo);
CheckResult(result);
}
}
private void FlushQueuedDisposables()
{
lock (_commandListsToDispose)
{
foreach (VkCommandList vkCB in _commandListsToDispose)
{
vkCB.DestroyCommandPool();
}
_commandListsToDispose.Clear();
}
}
private void CreateInstance(bool debug)
{
HashSet<string> availableInstanceLayers = new HashSet<string>(EnumerateInstanceLayers());
HashSet<string> availableInstanceExtensions = new HashSet<string>(GetInstanceExtensions());
VkInstanceCreateInfo instanceCI = VkInstanceCreateInfo.New();
VkApplicationInfo applicationInfo = new VkApplicationInfo();
applicationInfo.apiVersion = new VkVersion(1, 0, 0);
applicationInfo.applicationVersion = new VkVersion(1, 0, 0);
applicationInfo.engineVersion = new VkVersion(1, 0, 0);
applicationInfo.pApplicationName = s_name;
applicationInfo.pEngineName = s_name;
instanceCI.pApplicationInfo = &applicationInfo;
StackList<IntPtr, Size64Bytes> instanceExtensions = new StackList<IntPtr, Size64Bytes>();
StackList<IntPtr, Size64Bytes> instanceLayers = new StackList<IntPtr, Size64Bytes>();
if (!availableInstanceExtensions.Contains(CommonStrings.VK_KHR_SURFACE_EXTENSION_NAME))
{
throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_KHR_SURFACE_EXTENSION_NAME}");
}
instanceExtensions.Add(CommonStrings.VK_KHR_SURFACE_EXTENSION_NAME);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
if (!availableInstanceExtensions.Contains(CommonStrings.VK_KHR_WIN32_SURFACE_EXTENSION_NAME))
{
throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_KHR_WIN32_SURFACE_EXTENSION_NAME}");
}
instanceExtensions.Add(CommonStrings.VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
if (!availableInstanceExtensions.Contains(CommonStrings.VK_KHR_XLIB_SURFACE_EXTENSION_NAME))
{
throw new VeldridException($"The required instance extension was not available: {CommonStrings.VK_KHR_XLIB_SURFACE_EXTENSION_NAME}");
}
instanceExtensions.Add(CommonStrings.VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
}
else
{
throw new NotSupportedException("This platform does not support Vulkan.");
}
bool debugReportExtensionAvailable = false;
if (debug)
{
if (availableInstanceExtensions.Contains(CommonStrings.VK_EXT_DEBUG_REPORT_EXTENSION_NAME))
{
debugReportExtensionAvailable = true;
instanceExtensions.Add(CommonStrings.VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
}
if (availableInstanceLayers.Contains(CommonStrings.StandardValidationLayerName))
{
instanceLayers.Add(CommonStrings.StandardValidationLayerName);
}
}
instanceCI.enabledExtensionCount = instanceExtensions.Count;
instanceCI.ppEnabledExtensionNames = (byte**)instanceExtensions.Data;
instanceCI.enabledLayerCount = instanceLayers.Count;
instanceCI.ppEnabledLayerNames = (byte**)instanceLayers.Data;
VkResult result = vkCreateInstance(ref instanceCI, null, out _instance);
CheckResult(result);
if (debug && debugReportExtensionAvailable)
{
EnableDebugCallback();
}
}
public void EnableDebugCallback(VkDebugReportFlagsEXT flags = VkDebugReportFlagsEXT.WarningEXT | VkDebugReportFlagsEXT.ErrorEXT)
{
Debug.WriteLine("Enabling Vulkan Debug callbacks.");
_debugCallbackFunc = DebugCallback;
IntPtr debugFunctionPtr = Marshal.GetFunctionPointerForDelegate(_debugCallbackFunc);
VkDebugReportCallbackCreateInfoEXT debugCallbackCI = VkDebugReportCallbackCreateInfoEXT.New();
debugCallbackCI.flags = flags;
debugCallbackCI.pfnCallback = debugFunctionPtr;
IntPtr createFnPtr;
using (FixedUtf8String debugExtFnName = "vkCreateDebugReportCallbackEXT")
{
createFnPtr = vkGetInstanceProcAddr(_instance, debugExtFnName);
}
vkCreateDebugReportCallbackEXT_d createDelegate = Marshal.GetDelegateForFunctionPointer<vkCreateDebugReportCallbackEXT_d>(createFnPtr);
VkResult result = createDelegate(_instance, &debugCallbackCI, IntPtr.Zero, out _debugCallbackHandle);
CheckResult(result);
}
private uint DebugCallback(
uint flags,
VkDebugReportObjectTypeEXT objectType,
ulong @object,
UIntPtr location,
int messageCode,
byte* pLayerPrefix,
byte* pMessage,
void* pUserData)
{
string message = Util.GetString(pMessage);
VkDebugReportFlagsEXT debugReportFlags = (VkDebugReportFlagsEXT)flags;
#if DEBUG
if (Debugger.IsAttached)
{
Debugger.Break();
}
#endif
string fullMessage = $"[{debugReportFlags}] ({objectType}) {message}";
if (debugReportFlags == VkDebugReportFlagsEXT.ErrorEXT)
{
throw new VeldridException("A Vulkan validation error was encountered: " + fullMessage);
}
Console.WriteLine(fullMessage);
return 0;
}
private void CreatePhysicalDevice()
{
uint deviceCount = 0;
vkEnumeratePhysicalDevices(_instance, ref deviceCount, null);
if (deviceCount == 0)
{
throw new InvalidOperationException("No physical devices exist.");
}
VkPhysicalDevice[] physicalDevices = new VkPhysicalDevice[deviceCount];
vkEnumeratePhysicalDevices(_instance, ref deviceCount, ref physicalDevices[0]);
// Just use the first one.
_physicalDevice = physicalDevices[0];
vkGetPhysicalDeviceProperties(_physicalDevice, out _physicalDeviceProperties);
string deviceName;
fixed (byte* utf8NamePtr = _physicalDeviceProperties.deviceName)
{
deviceName = Encoding.UTF8.GetString(utf8NamePtr, (int)MaxPhysicalDeviceNameSize);
}
vkGetPhysicalDeviceFeatures(_physicalDevice, out _physicalDeviceFeatures);
vkGetPhysicalDeviceMemoryProperties(_physicalDevice, out _physicalDeviceMemProperties);
}
private void CreateLogicalDevice(VkSurfaceKHR surface)
{
GetQueueFamilyIndices(surface);
HashSet<uint> familyIndices = new HashSet<uint> { _graphicsQueueIndex, _presentQueueIndex };
VkDeviceQueueCreateInfo* queueCreateInfos = stackalloc VkDeviceQueueCreateInfo[familyIndices.Count];
uint queueCreateInfosCount = (uint)familyIndices.Count;
int i = 0;
foreach (uint index in familyIndices)
{
VkDeviceQueueCreateInfo queueCreateInfo = VkDeviceQueueCreateInfo.New();
queueCreateInfo.queueFamilyIndex = _graphicsQueueIndex;
queueCreateInfo.queueCount = 1;
float priority = 1f;
queueCreateInfo.pQueuePriorities = &priority;
queueCreateInfos[i] = queueCreateInfo;
i += 1;
}
VkPhysicalDeviceFeatures deviceFeatures = new VkPhysicalDeviceFeatures();
deviceFeatures.samplerAnisotropy = true;
deviceFeatures.fillModeNonSolid = true;
deviceFeatures.geometryShader = true;
deviceFeatures.depthClamp = true;
deviceFeatures.multiViewport = true;
deviceFeatures.textureCompressionBC = true;
bool debugMarkerSupported = false;
uint propertyCount = 0;
VkResult result = vkEnumerateDeviceExtensionProperties(_physicalDevice, (byte*)null, &propertyCount, null);
CheckResult(result);
VkExtensionProperties* properties = stackalloc VkExtensionProperties[(int)propertyCount];
result = vkEnumerateDeviceExtensionProperties(_physicalDevice, (byte*)null, &propertyCount, properties);
CheckResult(result);
for (int property = 0; property < propertyCount; property++)
{
if (Util.GetString(properties[property].extensionName) == "VK_EXT_debug_marker")
{
debugMarkerSupported = true;
break;
}
}
VkDeviceCreateInfo deviceCreateInfo = VkDeviceCreateInfo.New();
deviceCreateInfo.queueCreateInfoCount = queueCreateInfosCount;
deviceCreateInfo.pQueueCreateInfos = queueCreateInfos;
deviceCreateInfo.pEnabledFeatures = &deviceFeatures;
StackList<IntPtr> layerNames = new StackList<IntPtr>();
layerNames.Add(CommonStrings.StandardValidationLayerName);
deviceCreateInfo.enabledLayerCount = layerNames.Count;
deviceCreateInfo.ppEnabledLayerNames = (byte**)layerNames.Data;
StackList<IntPtr> extensionNames = new StackList<IntPtr>();
extensionNames.Add(CommonStrings.VK_KHR_SWAPCHAIN_EXTENSION_NAME);
if (debugMarkerSupported)
{
extensionNames.Add(CommonStrings.VK_EXT_DEBUG_MARKER_EXTENSION_NAME);
_debugMarkerEnabled = true;
}
deviceCreateInfo.enabledExtensionCount = extensionNames.Count;
deviceCreateInfo.ppEnabledExtensionNames = (byte**)extensionNames.Data;
result = vkCreateDevice(_physicalDevice, ref deviceCreateInfo, null, out _device);
CheckResult(result);
vkGetDeviceQueue(_device, _graphicsQueueIndex, 0, out _graphicsQueue);
if (debugMarkerSupported)
{
IntPtr setObjectNamePtr;
using (FixedUtf8String debugExtFnName = "vkDebugMarkerSetObjectNameEXT")
{
setObjectNamePtr = vkGetInstanceProcAddr(_instance, debugExtFnName);
}
_setObjectNameDelegate = Marshal.GetDelegateForFunctionPointer<vkDebugMarkerSetObjectNameEXT_d>(setObjectNamePtr);
}
}
private void GetQueueFamilyIndices(VkSurfaceKHR surface)
{
uint queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(_physicalDevice, ref queueFamilyCount, null);
VkQueueFamilyProperties[] qfp = new VkQueueFamilyProperties[queueFamilyCount];
vkGetPhysicalDeviceQueueFamilyProperties(_physicalDevice, ref queueFamilyCount, out qfp[0]);
bool foundGraphics = false;
bool foundPresent = surface == VkSurfaceKHR.Null;
for (uint i = 0; i < qfp.Length; i++)
{
if ((qfp[i].queueFlags & VkQueueFlags.Graphics) != 0)
{
_graphicsQueueIndex = i;
}
if (!foundPresent)
{
vkGetPhysicalDeviceSurfaceSupportKHR(_physicalDevice, i, surface, out VkBool32 presentSupported);
if (presentSupported)
{
_presentQueueIndex = i;
foundPresent = true;
}
}
if (foundGraphics && foundPresent)
{
return;
}
}
}
private void CreateDescriptorPool()
{
_descriptorPoolManager = new VkDescriptorPoolManager(this);
}
private void CreateGraphicsCommandPool()
{
VkCommandPoolCreateInfo commandPoolCI = VkCommandPoolCreateInfo.New();
commandPoolCI.flags = VkCommandPoolCreateFlags.ResetCommandBuffer;
commandPoolCI.queueFamilyIndex = _graphicsQueueIndex;
VkResult result = vkCreateCommandPool(_device, ref commandPoolCI, null, out _graphicsCommandPool);
CheckResult(result);
}
protected override MappedResource MapCore(MappableResource resource, MapMode mode, uint subresource)
{
VkMemoryBlock memoryBlock = null;
IntPtr mappedPtr = IntPtr.Zero;
uint sizeInBytes;
uint offset = 0;
uint rowPitch = 0;
uint depthPitch = 0;
if (resource is VkBuffer buffer)
{
memoryBlock = buffer.Memory;
sizeInBytes = buffer.SizeInBytes;
}
else
{
VkTexture texture = Util.AssertSubtype<MappableResource, VkTexture>(resource);
VkSubresourceLayout layout = texture.GetSubresourceLayout(subresource);
memoryBlock = texture.Memory;
sizeInBytes = (uint)layout.size;
offset = (uint)layout.offset;
rowPitch = (uint)layout.rowPitch;
depthPitch = (uint)layout.depthPitch;
}
if (memoryBlock != null)
{
if (memoryBlock.IsPersistentMapped)
{
mappedPtr = (IntPtr)memoryBlock.BlockMappedPointer;
}
else
{
mappedPtr = _memoryManager.Map(memoryBlock);
}
}
byte* dataPtr = (byte*)mappedPtr.ToPointer() + offset;
return new MappedResource(
resource,
mode,
(IntPtr)dataPtr,
sizeInBytes,
subresource,
rowPitch,
depthPitch);
}
protected override void UnmapCore(MappableResource resource, uint subresource)
{
VkMemoryBlock memoryBlock = null;
if (resource is VkBuffer buffer)
{
memoryBlock = buffer.Memory;
}
else
{
VkTexture tex = Util.AssertSubtype<MappableResource, VkTexture>(resource);
memoryBlock = tex.Memory;
}
if (memoryBlock != null && !memoryBlock.IsPersistentMapped)
{
vkUnmapMemory(_device, memoryBlock.DeviceMemory);
}
}
protected override void PlatformDispose()
{
Debug.Assert(_submittedFences.Count == 0);
foreach (Vulkan.VkFence fence in _availableSubmissionFences)
{
vkDestroyFence(_device, fence, null);
}
_mainSwapchain?.Dispose();
if (_debugCallbackFunc != null)
{
_debugCallbackFunc = null;
FixedUtf8String debugExtFnName = "vkDestroyDebugReportCallbackEXT";
IntPtr destroyFuncPtr = vkGetInstanceProcAddr(_instance, debugExtFnName);
vkDestroyDebugReportCallbackEXT_d destroyDel
= Marshal.GetDelegateForFunctionPointer<vkDestroyDebugReportCallbackEXT_d>(destroyFuncPtr);
VkResult debugDestroyResult = destroyDel(_instance, _debugCallbackHandle, null);
CheckResult(debugDestroyResult);
}
_descriptorPoolManager.DestroyAll();
vkDestroyCommandPool(_device, _graphicsCommandPool, null);
Debug.Assert(_submittedStagingTextures.Count == 0);
foreach (VkTexture tex in _availableStagingTextures)
{
tex.Dispose();
}
Debug.Assert(_submittedStagingBuffers.Count == 0);
foreach (VkBuffer buffer in _availableStagingBuffers)
{
buffer.Dispose();
}
while (_sharedGraphicsCommandPools.TryPop(out SharedCommandPool sharedPool))
{
sharedPool.Destroy();
}
_memoryManager.Dispose();
VkResult result = vkDeviceWaitIdle(_device);
CheckResult(result);
vkDestroyDevice(_device, null);
vkDestroyInstance(_instance, null);
}
protected override void WaitForIdleCore()
{
lock (_graphicsQueueLock)
{
vkQueueWaitIdle(_graphicsQueue);
}
CheckSubmittedFences();
FlushQueuedDisposables();
}
public override TextureSampleCount GetSampleCountLimit(PixelFormat format, bool depthFormat)
{
VkImageUsageFlags usageFlags = VkImageUsageFlags.Sampled;
usageFlags |= depthFormat ? VkImageUsageFlags.DepthStencilAttachment : VkImageUsageFlags.ColorAttachment;
vkGetPhysicalDeviceImageFormatProperties(
_physicalDevice,
VkFormats.VdToVkPixelFormat(format),
VkImageType.Image2D,
VkImageTiling.Optimal,
usageFlags,
VkImageCreateFlags.None,
out VkImageFormatProperties formatProperties);
VkSampleCountFlags vkSampleCounts = formatProperties.sampleCounts;
if ((vkSampleCounts & VkSampleCountFlags.Count32) == VkSampleCountFlags.Count32)
{
return TextureSampleCount.Count32;
}
else if ((vkSampleCounts & VkSampleCountFlags.Count16) == VkSampleCountFlags.Count16)
{
return TextureSampleCount.Count16;
}
else if ((vkSampleCounts & VkSampleCountFlags.Count8) == VkSampleCountFlags.Count8)
{
return TextureSampleCount.Count8;
}
else if ((vkSampleCounts & VkSampleCountFlags.Count4) == VkSampleCountFlags.Count4)
{
return TextureSampleCount.Count4;
}
else if ((vkSampleCounts & VkSampleCountFlags.Count2) == VkSampleCountFlags.Count2)
{
return TextureSampleCount.Count2;
}
return TextureSampleCount.Count1;
}
protected override void UpdateBufferCore(DeviceBuffer buffer, uint bufferOffsetInBytes, IntPtr source, uint sizeInBytes)
{
VkBuffer vkBuffer = Util.AssertSubtype<DeviceBuffer, VkBuffer>(buffer);
VkBuffer copySrcVkBuffer = null;
IntPtr mappedPtr;
bool isPersistentMapped = vkBuffer.Memory.IsPersistentMapped;
if (isPersistentMapped)
{
mappedPtr = (IntPtr)vkBuffer.Memory.BlockMappedPointer;
}
else
{
copySrcVkBuffer = GetFreeStagingBuffer(sizeInBytes);
mappedPtr = (IntPtr)copySrcVkBuffer.Memory.BlockMappedPointer;
}
byte* destPtr = (byte*)mappedPtr + bufferOffsetInBytes;
Unsafe.CopyBlock(destPtr, source.ToPointer(), sizeInBytes);
if (!isPersistentMapped)
{
SharedCommandPool pool = GetFreeCommandPool();
VkCommandBuffer cb = pool.BeginNewCommandBuffer();
VkBufferCopy copyRegion = new VkBufferCopy { size = vkBuffer.BufferMemoryRequirements.size };
vkCmdCopyBuffer(cb, copySrcVkBuffer.DeviceBuffer, vkBuffer.DeviceBuffer, 1, ref copyRegion);
pool.EndAndSubmit(cb);
lock (_stagingResourcesLock)
{
_submittedStagingBuffers.Add(cb, copySrcVkBuffer);
}
}
}
private SharedCommandPool GetFreeCommandPool()
{
if (!_sharedGraphicsCommandPools.TryPop(out SharedCommandPool sharedPool))
{
sharedPool = new SharedCommandPool(this, false);
}
return sharedPool;
}
private IntPtr MapBuffer(VkBuffer buffer, uint numBytes)
{
if (buffer.Memory.IsPersistentMapped)
{
return (IntPtr)buffer.Memory.BlockMappedPointer;
}
else
{
void* mappedPtr;
VkResult result = vkMapMemory(Device, buffer.Memory.DeviceMemory, buffer.Memory.Offset, numBytes, 0, &mappedPtr);
CheckResult(result);
return (IntPtr)mappedPtr;
}
}
private void UnmapBuffer(VkBuffer buffer)
{
if (!buffer.Memory.IsPersistentMapped)
{
vkUnmapMemory(Device, buffer.Memory.DeviceMemory);
}
}
protected override void UpdateTextureCore(
Texture texture,
IntPtr source,
uint sizeInBytes,
uint x,
uint y,
uint z,
uint width,
uint height,
uint depth,
uint mipLevel,
uint arrayLayer)
{
VkTexture vkTex = Util.AssertSubtype<Texture, VkTexture>(texture);
bool isStaging = (vkTex.Usage & TextureUsage.Staging) != 0;
if (isStaging)
{
VkMemoryBlock memBlock = vkTex.Memory;
uint subresource = texture.CalculateSubresource(mipLevel, arrayLayer);
VkSubresourceLayout layout = vkTex.GetSubresourceLayout(subresource);
byte* imageBasePtr = (byte*)memBlock.BlockMappedPointer + layout.offset;
uint srcRowPitch = FormatHelpers.GetRowPitch(width, texture.Format);
uint srcDepthPitch = FormatHelpers.GetDepthPitch(srcRowPitch, height, texture.Format);
Util.CopyTextureRegion(
source.ToPointer(),
0, 0, 0,
srcRowPitch, srcDepthPitch,
imageBasePtr,
x, y, z,
(uint)layout.rowPitch, (uint)layout.depthPitch,
width, height, depth,
texture.Format);
}
else
{
VkTexture stagingTex = GetFreeStagingTexture(width, height, depth, texture.Format);
UpdateTexture(stagingTex, source, sizeInBytes, 0, 0, 0, width, height, depth, 0, 0);
SharedCommandPool pool = GetFreeCommandPool();
VkCommandBuffer cb = pool.BeginNewCommandBuffer();
VkCommandList.CopyTextureCore_VkCommandBuffer(
cb,
stagingTex, 0, 0, 0, 0, 0,
texture, x, y, z, mipLevel, arrayLayer,
width, height, depth, 1);
pool.EndAndSubmit(cb);
lock (_stagingResourcesLock)
{
_submittedStagingTextures.Add(cb, stagingTex);
}
}
}
private VkTexture GetFreeStagingTexture(uint width, uint height, uint depth, PixelFormat format)
{
uint pixelSize = FormatHelpers.GetSizeInBytes(format);
uint totalSize = width * height * depth * pixelSize;
lock (_stagingResourcesLock)
{
for (int i = 0; i < _availableStagingTextures.Count; i++)
{
VkTexture tex = _availableStagingTextures[i];
if (tex.Memory.Size >= totalSize)
{
_availableStagingTextures.RemoveAt(i);
tex.SetStagingDimensions(width, height, depth);
return tex;
}
}
}
uint texWidth = Math.Max(256, width);
uint texHeight = Math.Max(256, height);
VkTexture newTex = (VkTexture)ResourceFactory.CreateTexture(TextureDescription.Texture3D(
texWidth, texHeight, depth, 1, format, TextureUsage.Staging));
newTex.SetStagingDimensions(width, height, depth);
return newTex;
}
private VkBuffer GetFreeStagingBuffer(uint size)
{
lock (_stagingResourcesLock)
{