forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphicsDevice.Vulkan.cs
More file actions
1018 lines (847 loc) · 38.6 KB
/
GraphicsDevice.Vulkan.cs
File metadata and controls
1018 lines (847 loc) · 38.6 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
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
#if STRIDE_GRAPHICS_API_VULKAN
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Vortice.Vulkan;
using static Vortice.Vulkan.Vulkan;
using Stride.Core;
using Stride.Core.Threading;
using System.Text;
namespace Stride.Graphics
{
public partial class GraphicsDevice
{
internal int ConstantBufferDataPlacementAlignment;
internal readonly ConcurrentPool<List<VkDescriptorPool>> DescriptorPoolLists = new ConcurrentPool<List<VkDescriptorPool>>(() => new List<VkDescriptorPool>());
internal readonly ConcurrentPool<List<Texture>> StagingResourceLists = new ConcurrentPool<List<Texture>>(() => new List<Texture>());
private const GraphicsPlatform GraphicPlatform = GraphicsPlatform.Vulkan;
internal GraphicsProfile RequestedProfile;
private bool simulateReset = false;
private string rendererName;
private VkDevice nativeDevice;
internal VkQueue NativeCommandQueue;
internal object QueueLock = new object();
internal ThreadLocal<VkCommandPool> NativeCopyCommandPools;
private NativeResourceCollector nativeResourceCollector;
private GraphicsResourceLinkCollector graphicsResourceLinkCollector;
private VkBuffer nativeUploadBuffer;
private VkDeviceMemory nativeUploadBufferMemory;
private IntPtr nativeUploadBufferStart;
private int nativeUploadBufferSize;
private int nativeUploadBufferOffset;
private object nativeUploadBufferLock = new();
private Queue<KeyValuePair<long, VkFence>> nativeFences = new Queue<KeyValuePair<long, VkFence>>();
private long lastCompletedFence;
internal long NextFenceValue = 1;
internal HeapPool DescriptorPools;
internal const uint MaxDescriptorSetCount = 256;
internal readonly uint[] MaxDescriptorTypeCounts =
[
256, // Sampler
0, // CombinedImageSampler
512, // SampledImage
64, // StorageImage
64, // UniformTexelBuffer
64, // StorageTexelBuffer
512, // UniformBuffer
64, // StorageBuffer
0, // UniformBufferDynamic
0, // StorageBufferDynamic
0 // InputAttachment
];
internal Buffer EmptyTexelBufferInt, EmptyTexelBufferFloat;
internal Texture EmptyTexture;
internal VkPhysicalDevice NativePhysicalDevice => Adapter.GetPhysicalDevice(IsDebugMode);
internal VkInstance NativeInstance => GraphicsAdapterFactory.GetInstance(IsDebugMode).NativeInstance;
internal struct BufferInfo
{
public long FenceValue;
public VkBuffer Buffer;
public VkDeviceMemory Memory;
public BufferInfo(long fenceValue, VkBuffer buffer, VkDeviceMemory memory)
{
FenceValue = fenceValue;
Buffer = buffer;
Memory = memory;
}
}
/// <summary>
/// The tick frquency of timestamp queries in Hertz.
/// </summary>
public long TimestampFrequency { get; private set; }
/// <summary>
/// Gets the status of this device.
/// </summary>
/// <value>The graphics device status.</value>
public GraphicsDeviceStatus GraphicsDeviceStatus
{
get
{
if (simulateReset)
{
simulateReset = false;
return GraphicsDeviceStatus.Reset;
}
//var result = NativeDevice.DeviceRemovedReason;
//if (result == SharpDX.DXGI.ResultCode.DeviceRemoved)
//{
// return GraphicsDeviceStatus.Removed;
//}
//if (result == SharpDX.DXGI.ResultCode.DeviceReset)
//{
// return GraphicsDeviceStatus.Reset;
//}
//if (result == SharpDX.DXGI.ResultCode.DeviceHung)
//{
// return GraphicsDeviceStatus.Hung;
//}
//if (result == SharpDX.DXGI.ResultCode.DriverInternalError)
//{
// return GraphicsDeviceStatus.InternalError;
//}
//if (result == SharpDX.DXGI.ResultCode.InvalidCall)
//{
// return GraphicsDeviceStatus.InvalidCall;
//}
//if (result.Code < 0)
//{
// return GraphicsDeviceStatus.Reset;
//}
return GraphicsDeviceStatus.Normal;
}
}
/// <summary>
/// Gets the native device.
/// </summary>
/// <value>The native device.</value>
internal VkDevice NativeDevice
{
get { return nativeDevice; }
}
/// <summary>
/// Marks context as active on the current thread.
/// </summary>
public void Begin()
{
FrameTriangleCount = 0;
FrameDrawCalls = 0;
}
/// <summary>
/// Enables profiling.
/// </summary>
/// <param name="enabledFlag">if set to <c>true</c> [enabled flag].</param>
public void EnableProfile(bool enabledFlag)
{
}
/// <summary>
/// Unmarks context as active on the current thread.
/// </summary>
public void End()
{
}
/// <summary>
/// Executes a deferred command list.
/// </summary>
/// <param name="commandList">The deferred command list.</param>
public void ExecuteCommandList(CompiledCommandList commandList)
{
ExecuteCommandListInternal(commandList);
}
/// <summary>
/// Executes multiple deferred command lists.
/// </summary>
/// <param name="count">Number of command lists to execute.</param>
/// <param name="commandLists">The deferred command lists.</param>
public unsafe void ExecuteCommandLists(int count, CompiledCommandList[] commandLists)
{
if (commandLists == null) throw new ArgumentNullException(nameof(commandLists));
if (count > commandLists.Length) throw new ArgumentOutOfRangeException(nameof(count));
var fenceValue = NextFenceValue++;
// Create a fence
var fenceCreateInfo = new VkFenceCreateInfo { sType = VkStructureType.FenceCreateInfo };
vkCreateFence(nativeDevice, &fenceCreateInfo, null, out var fence);
nativeFences.Enqueue(new KeyValuePair<long, VkFence>(fenceValue, fence));
// Collect resources
var commandBuffers = stackalloc VkCommandBuffer[count];
for (int i = 0; i < count; i++)
{
commandBuffers[i] = commandLists[i].NativeCommandBuffer;
RecycleCommandListResources(commandLists[i], fenceValue);
}
// Submit commands
var pipelineStageFlags = VkPipelineStageFlags.BottomOfPipe;
var presentSemaphoreCopy = presentSemaphore;
var submitInfo = new VkSubmitInfo
{
sType = VkStructureType.SubmitInfo,
commandBufferCount = (uint)count,
pCommandBuffers = commandBuffers,
waitSemaphoreCount = presentSemaphore != VkSemaphore.Null ? 1U : 0U,
pWaitSemaphores = &presentSemaphoreCopy,
pWaitDstStageMask = &pipelineStageFlags,
};
lock (QueueLock)
{
vkQueueSubmit(NativeCommandQueue, 1, &submitInfo, fence);
}
presentSemaphore = VkSemaphore.Null;
nativeResourceCollector.Release();
graphicsResourceLinkCollector.Release();
}
private void InitializePostFeatures()
{
}
private string GetRendererName()
{
return rendererName;
}
public void SimulateReset()
{
simulateReset = true;
}
/// <summary>
/// Initializes the specified device.
/// </summary>
/// <param name="graphicsProfiles">The graphics profiles.</param>
/// <param name="deviceCreationFlags">The device creation flags.</param>
/// <param name="windowHandle">The window handle.</param>
private unsafe void InitializePlatformDevice(GraphicsProfile[] graphicsProfiles, DeviceCreationFlags deviceCreationFlags, object windowHandle)
{
if (nativeDevice != VkDevice.Null)
{
// Destroy previous device
ReleaseDevice();
}
rendererName = Adapter.Description;
vkGetPhysicalDeviceProperties(NativePhysicalDevice, out var physicalDeviceProperties);
ConstantBufferDataPlacementAlignment = (int)physicalDeviceProperties.limits.minUniformBufferOffsetAlignment;
TimestampFrequency = (long)(1.0e9 / physicalDeviceProperties.limits.timestampPeriod); // Resolution in nanoseconds
// Configure descriptor type max counts
void SetMaxDescriptorTypeCount(VkDescriptorType type, uint limit)
=> MaxDescriptorTypeCounts[(int)type] = Math.Min(MaxDescriptorTypeCounts[(int)type], limit);
SetMaxDescriptorTypeCount(VkDescriptorType.Sampler, physicalDeviceProperties.limits.maxDescriptorSetSamplers);
SetMaxDescriptorTypeCount(VkDescriptorType.CombinedImageSampler, 0); // Not defined.
SetMaxDescriptorTypeCount(VkDescriptorType.SampledImage, physicalDeviceProperties.limits.maxDescriptorSetSampledImages);
SetMaxDescriptorTypeCount(VkDescriptorType.StorageImage, physicalDeviceProperties.limits.maxDescriptorSetStorageImages);
SetMaxDescriptorTypeCount(VkDescriptorType.UniformTexelBuffer, physicalDeviceProperties.limits.maxDescriptorSetSampledImages); // No individual limit
SetMaxDescriptorTypeCount(VkDescriptorType.StorageTexelBuffer, physicalDeviceProperties.limits.maxDescriptorSetStorageImages); // No individual limit
SetMaxDescriptorTypeCount(VkDescriptorType.UniformBuffer, physicalDeviceProperties.limits.maxDescriptorSetUniformBuffers);
SetMaxDescriptorTypeCount(VkDescriptorType.StorageBuffer, physicalDeviceProperties.limits.maxDescriptorSetStorageBuffers);
SetMaxDescriptorTypeCount(VkDescriptorType.UniformBufferDynamic, physicalDeviceProperties.limits.maxDescriptorSetUniformBuffersDynamic);
SetMaxDescriptorTypeCount(VkDescriptorType.StorageBufferDynamic, physicalDeviceProperties.limits.maxDescriptorSetStorageBuffersDynamic);
SetMaxDescriptorTypeCount(VkDescriptorType.InputAttachment, physicalDeviceProperties.limits.maxDescriptorSetInputAttachments);
RequestedProfile = graphicsProfiles.First();
var queueProperties = vkGetPhysicalDeviceQueueFamilyProperties(NativePhysicalDevice);
//IsProfilingSupported = queueProperties[0].TimestampValidBits > 0;
// Command lists are thread-safe and execute deferred
IsDeferred = true;
// TODO VULKAN
// Create Vulkan device based on profile
float queuePriorities = 0;
var queueCreateInfo = new VkDeviceQueueCreateInfo
{
sType = VkStructureType.DeviceQueueCreateInfo,
queueFamilyIndex = 0,
queueCount = 1,
pQueuePriorities = &queuePriorities,
};
var enabledFeature = new VkPhysicalDeviceFeatures
{
fillModeNonSolid = true,
shaderClipDistance = true,
shaderCullDistance = true,
samplerAnisotropy = true,
depthClamp = true,
};
vkGetPhysicalDeviceFeatures(NativePhysicalDevice, out var deviceFeatures);
if (deviceFeatures.shaderStorageImageReadWithoutFormat)
{
enabledFeature.shaderStorageImageReadWithoutFormat = true;
}
if (deviceFeatures.shaderStorageImageWriteWithoutFormat)
{
enabledFeature.shaderStorageImageWriteWithoutFormat = true;
}
Span<VkUtf8String> supportedExtensionProperties = stackalloc VkUtf8String[]
{
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
VK_EXT_DEBUG_MARKER_EXTENSION_NAME,
};
var availableExtensionProperties = GetAvailableExtensionProperties(supportedExtensionProperties);
ValidateExtensionPropertiesAvailability(availableExtensionProperties);
var desiredExtensionProperties = new HashSet<VkUtf8String>
{
VK_KHR_SWAPCHAIN_EXTENSION_NAME
};
if (availableExtensionProperties.Contains(VK_EXT_DEBUG_MARKER_EXTENSION_NAME) && IsDebugMode)
{
desiredExtensionProperties.Add(VK_EXT_DEBUG_MARKER_EXTENSION_NAME);
IsProfilingSupported = true;
}
using VkStringArray ppEnabledExtensionNames = new(desiredExtensionProperties);
var deviceCreateInfo = new VkDeviceCreateInfo
{
sType = VkStructureType.DeviceCreateInfo,
queueCreateInfoCount = 1,
pQueueCreateInfos = &queueCreateInfo,
enabledExtensionCount = ppEnabledExtensionNames.Length,
ppEnabledExtensionNames = ppEnabledExtensionNames,
pEnabledFeatures = &enabledFeature,
};
vkCreateDevice(NativePhysicalDevice, in deviceCreateInfo, null, out nativeDevice);
vkLoadDevice(nativeDevice);
vkGetDeviceQueue(nativeDevice, 0, 0, out NativeCommandQueue);
NativeCopyCommandPools = new ThreadLocal<VkCommandPool>(() =>
{
//// Prepare copy command list (start it closed, so that every new use start with a Reset)
var commandPoolCreateInfo = new VkCommandPoolCreateInfo
{
sType = VkStructureType.CommandPoolCreateInfo,
queueFamilyIndex = 0, //device.NativeCommandQueue.FamilyIndex
flags = VkCommandPoolCreateFlags.ResetCommandBuffer
};
vkCreateCommandPool(NativeDevice, &commandPoolCreateInfo, null, out var result);
return result;
}, true);
DescriptorPools = new HeapPool(this);
nativeResourceCollector = new NativeResourceCollector(this);
graphicsResourceLinkCollector = new GraphicsResourceLinkCollector(this);
EmptyTexelBufferInt = Buffer.Typed.New(this, 1, PixelFormat.R32G32B32A32_UInt);
EmptyTexelBufferFloat = Buffer.Typed.New(this, 1, PixelFormat.R32G32B32A32_Float);
EmptyTexture = Texture.New2D(this, 1, 1, PixelFormat.R8G8B8A8_UNorm_SRgb, TextureFlags.ShaderResource);
}
private unsafe HashSet<VkUtf8String> GetAvailableExtensionProperties(Span<VkUtf8String> supportedExtensionProperties)
{
var availableExtensionProperties = new HashSet<VkUtf8String>();
var extensionProperties = vkEnumerateDeviceExtensionProperties(NativePhysicalDevice);
for (int index = 0; index < extensionProperties.Length; index++)
{
var properties = extensionProperties[index];
var name = new VkUtf8String(properties.extensionName);
var indexOfExtensionName = supportedExtensionProperties.IndexOf(name);
if (indexOfExtensionName >= 0)
availableExtensionProperties.Add(supportedExtensionProperties[indexOfExtensionName]);
}
return availableExtensionProperties;
}
private static void ValidateExtensionPropertiesAvailability(HashSet<VkUtf8String> availableExtensionProperties)
{
if (!availableExtensionProperties.Contains(VK_KHR_SWAPCHAIN_EXTENSION_NAME))
{
string extensionName = Encoding.UTF8.GetString(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
throw new NotSupportedException($"Required Vulkan extension {extensionName} is not supported by the current physical device.");
}
}
internal unsafe IntPtr AllocateUploadBuffer(int size, out VkBuffer resource, out int offset)
{
lock (nativeUploadBufferLock)
{
if (nativeUploadBuffer == VkBuffer.Null || nativeUploadBufferOffset + size > nativeUploadBufferSize)
{
if (nativeUploadBuffer != VkBuffer.Null)
{
vkUnmapMemory(NativeDevice, nativeUploadBufferMemory);
Collect(nativeUploadBuffer);
Collect(nativeUploadBufferMemory);
}
// Allocate new buffer
// TODO D3D12 recycle old ones (using fences to know when GPU is done with them)
// TODO D3D12 ResourceStates.CopySource not working?
nativeUploadBufferSize = Math.Max(4 * 1024 * 1024, size);
var bufferCreateInfo = new VkBufferCreateInfo
{
sType = VkStructureType.BufferCreateInfo,
size = (ulong)nativeUploadBufferSize,
flags = VkBufferCreateFlags.None,
usage = VkBufferUsageFlags.TransferSrc,
};
vkCreateBuffer(NativeDevice, &bufferCreateInfo, null, out nativeUploadBuffer);
AllocateMemory(VkMemoryPropertyFlags.HostVisible | VkMemoryPropertyFlags.HostCoherent);
fixed (IntPtr* nativeUploadBufferStartPtr = &nativeUploadBufferStart)
vkMapMemory(NativeDevice, nativeUploadBufferMemory, 0, (ulong)nativeUploadBufferSize, VkMemoryMapFlags.None, (void**)nativeUploadBufferStartPtr);
nativeUploadBufferOffset = 0;
}
// Bump allocate
resource = nativeUploadBuffer;
offset = nativeUploadBufferOffset;
nativeUploadBufferOffset += size;
return nativeUploadBufferStart + offset;
}
}
protected unsafe void AllocateMemory(VkMemoryPropertyFlags memoryProperties)
{
vkGetBufferMemoryRequirements(nativeDevice, nativeUploadBuffer, out var memoryRequirements);
if (memoryRequirements.size == 0)
return;
var allocateInfo = new VkMemoryAllocateInfo
{
sType = VkStructureType.MemoryAllocateInfo,
allocationSize = memoryRequirements.size,
};
vkGetPhysicalDeviceMemoryProperties(NativePhysicalDevice, out var physicalDeviceMemoryProperties);
var typeBits = memoryRequirements.memoryTypeBits;
for (uint i = 0; i < physicalDeviceMemoryProperties.memoryTypeCount; i++)
{
if ((typeBits & 1) == 1)
{
// Type is available, does it match user properties?
var memoryType = *(&physicalDeviceMemoryProperties.memoryTypes[0] + i);
if ((memoryType.propertyFlags & memoryProperties) == memoryProperties)
{
allocateInfo.memoryTypeIndex = i;
break;
}
}
typeBits >>= 1;
}
vkAllocateMemory(NativeDevice, &allocateInfo, null, out nativeUploadBufferMemory);
vkBindBufferMemory(NativeDevice, nativeUploadBuffer, nativeUploadBufferMemory, 0);
}
private void AdjustDefaultPipelineStateDescription(ref PipelineStateDescription pipelineStateDescription)
{
}
protected void DestroyPlatformDevice()
{
ReleaseDevice();
}
private unsafe void ReleaseDevice()
{
EmptyTexelBufferInt.Dispose();
EmptyTexelBufferInt = null;
EmptyTexelBufferFloat.Dispose();
EmptyTexelBufferFloat = null;
EmptyTexture.Dispose();
EmptyTexture = null;
// Wait for all queues to be idle
vkDeviceWaitIdle(nativeDevice);
// Destroy all remaining fences
GetCompletedValue();
// Mark upload buffer for destruction
if (nativeUploadBuffer != VkBuffer.Null)
{
vkUnmapMemory(NativeDevice, nativeUploadBufferMemory);
nativeResourceCollector.Add(lastCompletedFence, nativeUploadBuffer);
nativeResourceCollector.Add(lastCompletedFence, nativeUploadBufferMemory);
nativeUploadBuffer = VkBuffer.Null;
nativeUploadBufferMemory = VkDeviceMemory.Null;
}
// Release fenced resources
nativeResourceCollector.Dispose();
DescriptorPools.Dispose();
foreach (var nativeCopyCommandPool in NativeCopyCommandPools.Values)
vkDestroyCommandPool(nativeDevice, nativeCopyCommandPool, null);
NativeCopyCommandPools.Dispose();
NativeCopyCommandPools = null;
vkDestroyDevice(nativeDevice, null);
}
internal void OnDestroyed()
{
}
internal unsafe long ExecuteCommandListInternal(CompiledCommandList commandList)
{
//if (nativeUploadBuffer != VkBuffer.Null)
//{
// NativeDevice.UnmapMemory(nativeUploadBufferMemory);
// TemporaryResources.Enqueue(new BufferInfo(NextFenceValue, nativeUploadBuffer, nativeUploadBufferMemory));
// nativeUploadBuffer = VkBuffer.Null;
// nativeUploadBufferMemory = VkDeviceMemory.Null;
//}
var fenceValue = NextFenceValue++;
// Create new fence
var fenceCreateInfo = new VkFenceCreateInfo { sType = VkStructureType.FenceCreateInfo };
vkCreateFence(nativeDevice, &fenceCreateInfo, null, out var fence);
nativeFences.Enqueue(new KeyValuePair<long, VkFence>(fenceValue, fence));
// Collect resources
RecycleCommandListResources(commandList, fenceValue);
// Submit commands
var nativeCommandBufferCopy = commandList.NativeCommandBuffer;
var pipelineStageFlags = VkPipelineStageFlags.BottomOfPipe;
var presentSemaphoreCopy = presentSemaphore;
var submitInfo = new VkSubmitInfo
{
sType = VkStructureType.SubmitInfo,
commandBufferCount = 1,
pCommandBuffers = &nativeCommandBufferCopy,
waitSemaphoreCount = presentSemaphore != VkSemaphore.Null ? 1U : 0U,
pWaitSemaphores = &presentSemaphoreCopy,
pWaitDstStageMask = &pipelineStageFlags,
};
lock (QueueLock)
{
vkQueueSubmit(NativeCommandQueue, 1, &submitInfo, fence);
}
presentSemaphore = VkSemaphore.Null;
nativeResourceCollector.Release();
graphicsResourceLinkCollector.Release();
return fenceValue;
}
private void RecycleCommandListResources(CompiledCommandList commandList, long fenceValue)
{
// Set fence on staging textures
foreach (var stagingResource in commandList.StagingResources)
{
stagingResource.StagingFenceValue = fenceValue;
}
StagingResourceLists.Release(commandList.StagingResources);
commandList.StagingResources.Clear();
// Recycle all resources
foreach (var descriptorPool in commandList.DescriptorPools)
{
DescriptorPools.RecycleObject(fenceValue, descriptorPool);
}
DescriptorPoolLists.Release(commandList.DescriptorPools);
commandList.DescriptorPools.Clear();
commandList.Builder.CommandBufferPool.RecycleObject(fenceValue, commandList.NativeCommandBuffer);
}
internal bool IsFenceCompleteInternal(long fenceValue)
{
// Try to avoid checking the fence if possible
if (fenceValue > lastCompletedFence)
{
GetCompletedValue();
}
return fenceValue <= lastCompletedFence;
}
private SpinLock spinLock = new SpinLock();
internal unsafe long GetCompletedValue()
{
bool lockTaken = false;
try
{
spinLock.Enter(ref lockTaken);
while (nativeFences.Count > 0 && vkGetFenceStatus(NativeDevice, nativeFences.Peek().Value) == VkResult.Success)
{
var fence = nativeFences.Dequeue();
vkDestroyFence(NativeDevice, fence.Value, null);
lastCompletedFence = Math.Max(lastCompletedFence, fence.Key);
}
return lastCompletedFence;
}
finally
{
if (lockTaken)
spinLock.Exit(false);
}
}
internal unsafe void WaitForFenceInternal(long fenceValue)
{
if (IsFenceCompleteInternal(fenceValue))
return;
// TODO D3D12 in case of concurrency, this lock could end up blocking too long a second thread with lower fenceValue then first one
lock (nativeFences)
{
while (nativeFences.Count > 0 && nativeFences.Peek().Key <= fenceValue)
{
var fence = nativeFences.Dequeue();
var fenceCopy = fence.Value;
vkWaitForFences(NativeDevice, 1, &fenceCopy, true, ulong.MaxValue);
vkDestroyFence(NativeDevice, fence.Value, null);
lastCompletedFence = fenceValue;
}
}
}
private VkSemaphore presentSemaphore;
public unsafe VkSemaphore GetNextPresentSemaphore()
{
var createInfo = new VkSemaphoreCreateInfo { sType = VkStructureType.SemaphoreCreateInfo };
vkCreateSemaphore(NativeDevice, &createInfo, null, out presentSemaphore);
Collect(presentSemaphore);
return presentSemaphore;
}
internal void Collect(NativeResource nativeResource)
{
nativeResourceCollector.Add(NextFenceValue, nativeResource);
}
internal void TagResource(GraphicsResourceLink resourceLink)
{
switch (resourceLink.Resource)
{
case Texture texture:
if (texture.Usage == GraphicsResourceUsage.Dynamic)
{
// Increase the reference count until GPU is done with the resource
resourceLink.ReferenceCount++;
graphicsResourceLinkCollector.Add(NextFenceValue, resourceLink);
}
break;
case Buffer buffer:
if (buffer.Usage == GraphicsResourceUsage.Dynamic)
{
// Increase the reference count until GPU is done with the resource
resourceLink.ReferenceCount++;
graphicsResourceLinkCollector.Add(NextFenceValue, resourceLink);
}
break;
case QueryPool _:
resourceLink.ReferenceCount++;
graphicsResourceLinkCollector.Add(NextFenceValue, resourceLink);
break;
}
}
}
internal abstract class ResourcePool<T> : ComponentBase
{
protected readonly GraphicsDevice GraphicsDevice;
private readonly Queue<KeyValuePair<long, T>> liveObjects = new Queue<KeyValuePair<long, T>>();
protected ResourcePool(GraphicsDevice graphicsDevice)
{
GraphicsDevice = graphicsDevice;
}
public T GetObject()
{
lock (liveObjects)
{
// Check if first allocator is ready for reuse
if (liveObjects.Count > 0)
{
var firstAllocator = liveObjects.Peek();
if (firstAllocator.Key <= GraphicsDevice.GetCompletedValue())
{
liveObjects.Dequeue();
ResetObject(firstAllocator.Value);
return firstAllocator.Value;
}
}
return CreateObject();
}
}
public void RecycleObject(long fenceValue, T obj)
{
lock (liveObjects)
{
liveObjects.Enqueue(new KeyValuePair<long, T>(fenceValue, obj));
}
}
protected abstract T CreateObject();
protected abstract void ResetObject(T obj);
protected virtual void DestroyObject(T obj)
{
}
protected override void Destroy()
{
lock (liveObjects)
{
foreach (var item in liveObjects)
{
DestroyObject(item.Value);
}
}
base.Destroy();
}
}
internal class CommandBufferPool : ResourcePool<VkCommandBuffer>
{
private readonly VkCommandPool commandPool;
public unsafe CommandBufferPool(GraphicsDevice graphicsDevice) : base(graphicsDevice)
{
var commandPoolCreateInfo = new VkCommandPoolCreateInfo
{
sType = VkStructureType.CommandPoolCreateInfo,
queueFamilyIndex = 0, //device.NativeCommandQueue.FamilyIndex
flags = VkCommandPoolCreateFlags.ResetCommandBuffer
};
vkCreateCommandPool(graphicsDevice.NativeDevice, &commandPoolCreateInfo, null, out commandPool);
}
protected override unsafe VkCommandBuffer CreateObject()
{
// No allocator ready to be used, let's create a new one
var commandBufferAllocationInfo = new VkCommandBufferAllocateInfo
{
sType = VkStructureType.CommandBufferAllocateInfo,
level = VkCommandBufferLevel.Primary,
commandPool = commandPool,
commandBufferCount = 1,
};
VkCommandBuffer commandBuffer;
vkAllocateCommandBuffers(GraphicsDevice.NativeDevice, &commandBufferAllocationInfo, &commandBuffer);
return commandBuffer;
}
protected override void ResetObject(VkCommandBuffer obj)
{
vkResetCommandBuffer(obj, VkCommandBufferResetFlags.None);
}
protected override unsafe void Destroy()
{
base.Destroy();
vkDestroyCommandPool(GraphicsDevice.NativeDevice, commandPool, null);
}
}
internal class HeapPool : ResourcePool<VkDescriptorPool>
{
public HeapPool(GraphicsDevice graphicsDevice) : base(graphicsDevice)
{
}
protected override unsafe VkDescriptorPool CreateObject()
{
// No allocator ready to be used, let's create a new one
var poolSizes = GraphicsDevice.MaxDescriptorTypeCounts
.Select((count, index) => new VkDescriptorPoolSize { type = (VkDescriptorType)index, descriptorCount = count })
.Where(size => size.descriptorCount > 0)
.ToArray();
fixed (VkDescriptorPoolSize* fPoolSizes = poolSizes) { // null if array is empty or null
var descriptorPoolCreateInfo = new VkDescriptorPoolCreateInfo
{
sType = VkStructureType.DescriptorPoolCreateInfo,
poolSizeCount = (uint)poolSizes.Length,
pPoolSizes = fPoolSizes,
maxSets = GraphicsDevice.MaxDescriptorSetCount,
};
vkCreateDescriptorPool(GraphicsDevice.NativeDevice, &descriptorPoolCreateInfo, null, out var descriptorPool);
return descriptorPool;
}
}
protected override void ResetObject(VkDescriptorPool obj)
{
vkResetDescriptorPool(GraphicsDevice.NativeDevice, obj, VkDescriptorPoolResetFlags.None);
}
protected override unsafe void DestroyObject(VkDescriptorPool obj)
{
vkDestroyDescriptorPool(GraphicsDevice.NativeDevice, obj, null);
}
}
internal struct NativeResource
{
public VkDebugReportObjectTypeEXT type;
public ulong handle;
public NativeResource(VkDebugReportObjectTypeEXT type, ulong handle)
{
this.type = type;
this.handle = handle;
}
public static unsafe implicit operator NativeResource(VkBuffer handle)
{
return new NativeResource(VkDebugReportObjectTypeEXT.Buffer, *(ulong*)&handle);
}
public static unsafe implicit operator NativeResource(VkBufferView handle)
{
return new NativeResource(VkDebugReportObjectTypeEXT.BufferView, *(ulong*)&handle);
}
public static unsafe implicit operator NativeResource(VkImage handle)
{
return new NativeResource(VkDebugReportObjectTypeEXT.Image, *(ulong*)&handle);
}
public static unsafe implicit operator NativeResource(VkImageView handle)
{
return new NativeResource(VkDebugReportObjectTypeEXT.ImageView, *(ulong*)&handle);
}
public static unsafe implicit operator NativeResource(VkDeviceMemory handle)
{
return new NativeResource(VkDebugReportObjectTypeEXT.DeviceMemory, *(ulong*)&handle);
}
public static unsafe implicit operator NativeResource(VkSampler handle)
{
return new NativeResource(VkDebugReportObjectTypeEXT.Sampler, *(ulong*)&handle);
}
public static unsafe implicit operator NativeResource(VkFramebuffer handle)
{
return new NativeResource(VkDebugReportObjectTypeEXT.Framebuffer, *(ulong*)&handle);
}
public static unsafe implicit operator NativeResource(VkSemaphore handle)
{
return new NativeResource(VkDebugReportObjectTypeEXT.Semaphore, *(ulong*)&handle);
}
public static unsafe implicit operator NativeResource(VkFence handle)
{
return new NativeResource(VkDebugReportObjectTypeEXT.Fence, *(ulong*)&handle);
}
public static unsafe implicit operator NativeResource(VkQueryPool handle)
{
return new NativeResource(VkDebugReportObjectTypeEXT.QueryPool, *(ulong*)&handle);
}
public unsafe void Destroy(GraphicsDevice device)
{
var handleCopy = handle;
switch (type)
{
case VkDebugReportObjectTypeEXT.Buffer:
vkDestroyBuffer(device.NativeDevice, *(VkBuffer*)&handleCopy, null);
break;
case VkDebugReportObjectTypeEXT.BufferView:
vkDestroyBufferView(device.NativeDevice, *(VkBufferView*)&handleCopy, null);
break;
case VkDebugReportObjectTypeEXT.Image:
vkDestroyImage(device.NativeDevice, *(VkImage*)&handleCopy, null);
break;
case VkDebugReportObjectTypeEXT.ImageView:
vkDestroyImageView(device.NativeDevice, *(VkImageView*)&handleCopy, null);
break;
case VkDebugReportObjectTypeEXT.DeviceMemory:
vkFreeMemory(device.NativeDevice, *(VkDeviceMemory*)&handleCopy, null);
break;
case VkDebugReportObjectTypeEXT.Sampler:
vkDestroySampler(device.NativeDevice, *(VkSampler*)&handleCopy, null);
break;
case VkDebugReportObjectTypeEXT.Framebuffer:
vkDestroyFramebuffer(device.NativeDevice, *(VkFramebuffer*)&handleCopy, null);
break;
case VkDebugReportObjectTypeEXT.Semaphore:
vkDestroySemaphore(device.NativeDevice, *(VkSemaphore*)&handleCopy, null);
break;
case VkDebugReportObjectTypeEXT.Fence:
vkDestroyFence(device.NativeDevice, *(VkFence*)&handleCopy, null);
break;
case VkDebugReportObjectTypeEXT.QueryPool:
vkDestroyQueryPool(device.NativeDevice, *(VkQueryPool*)&handleCopy, null);
break;
}
}
}
internal class GraphicsResourceLinkCollector : TemporaryResourceCollector<GraphicsResourceLink>
{
public GraphicsResourceLinkCollector(GraphicsDevice graphicsDevice) : base(graphicsDevice)
{
}
protected override void ReleaseObject(GraphicsResourceLink item)
{
item.ReferenceCount--;
}
}
internal class NativeResourceCollector : TemporaryResourceCollector<NativeResource>
{
public NativeResourceCollector(GraphicsDevice graphicsDevice) : base(graphicsDevice)
{
}
protected override void ReleaseObject(NativeResource item)
{
item.Destroy(GraphicsDevice);
}
}
internal abstract class TemporaryResourceCollector<T> : IDisposable
{
protected readonly GraphicsDevice GraphicsDevice;
private readonly Queue<KeyValuePair<long, T>> items = new Queue<KeyValuePair<long, T>>();
protected TemporaryResourceCollector(GraphicsDevice graphicsDevice)
{
GraphicsDevice = graphicsDevice;
}
public void Add(long fenceValue, T item)
{
lock (items)
{
items.Enqueue(new KeyValuePair<long, T>(fenceValue, item));
}
}
public void Release()
{
lock (items)
{
while (items.Count > 0 && GraphicsDevice.IsFenceCompleteInternal(items.Peek().Key))