-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathffx_fsr2_vk.cpp
More file actions
1950 lines (1595 loc) · 88 KB
/
ffx_fsr2_vk.cpp
File metadata and controls
1950 lines (1595 loc) · 88 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
// This file is part of the FidelityFX SDK.
//
// Copyright (c) 2022-2023 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "../ffx_fsr2.h"
#include "ffx_fsr2_vk.h"
#include "shaders/ffx_fsr2_shaders_vk.h" // include all the precompiled VK shaders for the FSR2 passes
#include "../ffx_fsr2_private.h"
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <codecvt>
#include <locale>
// prototypes for functions in the interface
FfxErrorCode GetDeviceCapabilitiesVK(FfxFsr2Interface* backendInterface, FfxDeviceCapabilities* deviceCapabilities, FfxDevice device);
FfxErrorCode CreateBackendContextVK(FfxFsr2Interface* backendInterface, FfxDevice device);
FfxErrorCode DestroyBackendContextVK(FfxFsr2Interface* backendInterface);
FfxErrorCode CreateResourceVK(FfxFsr2Interface* backendInterface, const FfxCreateResourceDescription* desc, FfxResourceInternal* outResource);
FfxErrorCode RegisterResourceVK(FfxFsr2Interface* backendInterface, const FfxResource* inResource, FfxResourceInternal* outResourceInternal);
FfxErrorCode UnregisterResourcesVK(FfxFsr2Interface* backendInterface);
FfxResourceDescription GetResourceDescriptorVK(FfxFsr2Interface* backendInterface, FfxResourceInternal resource);
FfxErrorCode DestroyResourceVK(FfxFsr2Interface* backendInterface, FfxResourceInternal resource);
FfxErrorCode CreatePipelineVK(FfxFsr2Interface* backendInterface, FfxFsr2Pass passId, const FfxPipelineDescription* desc, FfxPipelineState* outPass);
FfxErrorCode DestroyPipelineVK(FfxFsr2Interface* backendInterface, FfxPipelineState* pipeline);
FfxErrorCode ScheduleGpuJobVK(FfxFsr2Interface* backendInterface, const FfxGpuJobDescription* job);
FfxErrorCode ExecuteGpuJobsVK(FfxFsr2Interface* backendInterface, FfxCommandList commandList);
#define FSR2_MAX_QUEUED_FRAMES ( 4)
#define FSR2_MAX_RESOURCE_COUNT (64)
#define FSR2_MAX_STAGING_RESOURCE_COUNT ( 8)
#define FSR2_MAX_BARRIERS (16)
#define FSR2_MAX_GPU_JOBS (32)
#define FSR2_MAX_IMAGE_COPY_MIPS (32)
#define FSR2_MAX_SAMPLERS ( 2)
#define FSR2_MAX_UNIFORM_BUFFERS ( 4)
#define FSR2_MAX_IMAGE_VIEWS (32)
#define FSR2_MAX_BUFFERED_DESCRIPTORS (FFX_FSR2_PASS_COUNT * FSR2_MAX_QUEUED_FRAMES)
#define FSR2_UBO_RING_BUFFER_SIZE (FSR2_MAX_BUFFERED_DESCRIPTORS * FSR2_MAX_UNIFORM_BUFFERS)
#define FSR2_UBO_MEMORY_BLOCK_SIZE (FSR2_UBO_RING_BUFFER_SIZE * 256)
typedef struct BackendContext_VK {
// store for resources and resourceViews
typedef struct Resource
{
#ifdef _DEBUG
char resourceName[64] = {};
#endif
VkImage imageResource;
VkImageAspectFlags aspectFlags;
VkBuffer bufferResource;
VkDeviceMemory deviceMemory;
VkMemoryPropertyFlags memoryProperties;
FfxResourceDescription resourceDescription;
FfxResourceStates state;
VkImageView allMipsImageView;
VkImageView singleMipImageViews[FSR2_MAX_IMAGE_VIEWS];
bool undefined;
} Resource;
typedef struct UniformBuffer
{
VkBuffer bufferResource;
uint8_t* pData;
} UniformBuffer;
typedef struct PipelineLayout
{
VkDescriptorSetLayout descriptorSetLayout;
VkDescriptorSet descriptorSets[FSR2_MAX_QUEUED_FRAMES];
uint32_t descriptorSetIndex;
VkPipelineLayout pipelineLayout;
} PipelineLayout;
typedef struct VKFunctionTable
{
PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr = 0;
PFN_vkSetDebugUtilsObjectNameEXT vkSetDebugUtilsObjectNameEXT = 0;
PFN_vkCreateDescriptorPool vkCreateDescriptorPool = 0;
PFN_vkCreateSampler vkCreateSampler = 0;
PFN_vkCreateDescriptorSetLayout vkCreateDescriptorSetLayout = 0;
PFN_vkCreateBuffer vkCreateBuffer = 0;
PFN_vkCreateImage vkCreateImage = 0;
PFN_vkCreateImageView vkCreateImageView = 0;
PFN_vkCreateShaderModule vkCreateShaderModule = 0;
PFN_vkCreatePipelineLayout vkCreatePipelineLayout = 0;
PFN_vkCreateComputePipelines vkCreateComputePipelines = 0;
PFN_vkDestroyPipelineLayout vkDestroyPipelineLayout = 0;
PFN_vkDestroyPipeline vkDestroyPipeline = 0;
PFN_vkDestroyImage vkDestroyImage = 0;
PFN_vkDestroyImageView vkDestroyImageView = 0;
PFN_vkDestroyBuffer vkDestroyBuffer = 0;
PFN_vkDestroyDescriptorSetLayout vkDestroyDescriptorSetLayout = 0;
PFN_vkDestroyDescriptorPool vkDestroyDescriptorPool = 0;
PFN_vkDestroySampler vkDestroySampler = 0;
PFN_vkDestroyShaderModule vkDestroyShaderModule = 0;
PFN_vkGetBufferMemoryRequirements vkGetBufferMemoryRequirements = 0;
PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements = 0;
PFN_vkAllocateDescriptorSets vkAllocateDescriptorSets = 0;
PFN_vkAllocateMemory vkAllocateMemory = 0;
PFN_vkFreeMemory vkFreeMemory = 0;
PFN_vkMapMemory vkMapMemory = 0;
PFN_vkUnmapMemory vkUnmapMemory = 0;
PFN_vkBindBufferMemory vkBindBufferMemory = 0;
PFN_vkBindImageMemory vkBindImageMemory = 0;
PFN_vkUpdateDescriptorSets vkUpdateDescriptorSets = 0;
PFN_vkFlushMappedMemoryRanges vkFlushMappedMemoryRanges = 0;
PFN_vkCmdPipelineBarrier vkCmdPipelineBarrier = 0;
PFN_vkCmdBindPipeline vkCmdBindPipeline = 0;
PFN_vkCmdBindDescriptorSets vkCmdBindDescriptorSets = 0;
PFN_vkCmdDispatch vkCmdDispatch = 0;
PFN_vkCmdCopyBuffer vkCmdCopyBuffer = 0;
PFN_vkCmdCopyImage vkCmdCopyImage = 0;
PFN_vkCmdCopyBufferToImage vkCmdCopyBufferToImage = 0;
PFN_vkCmdClearColorImage vkCmdClearColorImage = 0;
} VkFunctionTable;
VkPhysicalDevice physicalDevice = nullptr;
VkDevice device = nullptr;
VkFunctionTable vkFunctionTable = {};
uint32_t gpuJobCount = 0;
FfxGpuJobDescription gpuJobs[FSR2_MAX_GPU_JOBS] = {};
uint32_t nextStaticResource = 0;
uint32_t nextDynamicResource = 0;
uint32_t stagingResourceCount = 0;
Resource resources[FSR2_MAX_RESOURCE_COUNT] = {};
FfxResourceInternal stagingResources[FSR2_MAX_STAGING_RESOURCE_COUNT] = {};
VkDescriptorPool descPool = nullptr;
VkDescriptorSetLayout samplerDescriptorSetLayout = nullptr;
VkDescriptorSet samplerDescriptorSet = nullptr;
uint32_t allocatedPipelineLayoutCount = 0;
PipelineLayout pipelineLayouts[FFX_FSR2_PASS_COUNT] = {};
VkSampler pointSampler = nullptr;
VkSampler linearSampler = nullptr;
VkDeviceMemory uboMemory = nullptr;
VkMemoryPropertyFlags uboMemoryProperties = 0;
UniformBuffer uboRingBuffer[FSR2_UBO_RING_BUFFER_SIZE] = {};
uint32_t uboRingBufferIndex = 0;
VkImageMemoryBarrier imageMemoryBarriers[FSR2_MAX_BARRIERS] = {};
VkBufferMemoryBarrier bufferMemoryBarriers[FSR2_MAX_BARRIERS] = {};
uint32_t scheduledImageBarrierCount = 0;
uint32_t scheduledBufferBarrierCount = 0;
VkPipelineStageFlags srcStageMask = 0;
VkPipelineStageFlags dstStageMask = 0;
uint32_t numDeviceExtensions = 0;
VkExtensionProperties* extensionProperties = nullptr;
} BackendContext_VK;
FFX_API size_t ffxFsr2GetScratchMemorySizeVK(VkPhysicalDevice physicalDevice)
{
uint32_t numExtensions = 0;
if (physicalDevice)
vkEnumerateDeviceExtensionProperties(physicalDevice, nullptr, &numExtensions, nullptr);
return FFX_ALIGN_UP(sizeof(BackendContext_VK) + sizeof(VkExtensionProperties) * numExtensions, sizeof(uint64_t));
}
FfxErrorCode ffxFsr2GetInterfaceVK(
FfxFsr2Interface* outInterface,
void* scratchBuffer,
size_t scratchBufferSize,
VkPhysicalDevice physicalDevice,
PFN_vkGetDeviceProcAddr getDeviceProcAddr)
{
FFX_RETURN_ON_ERROR(
outInterface,
FFX_ERROR_INVALID_POINTER);
FFX_RETURN_ON_ERROR(
scratchBuffer,
FFX_ERROR_INVALID_POINTER);
FFX_RETURN_ON_ERROR(
scratchBufferSize >= ffxFsr2GetScratchMemorySizeVK(physicalDevice),
FFX_ERROR_INSUFFICIENT_MEMORY);
outInterface->fpGetDeviceCapabilities = GetDeviceCapabilitiesVK;
outInterface->fpCreateBackendContext = CreateBackendContextVK;
outInterface->fpDestroyBackendContext = DestroyBackendContextVK;
outInterface->fpCreateResource = CreateResourceVK;
outInterface->fpRegisterResource = RegisterResourceVK;
outInterface->fpUnregisterResources = UnregisterResourcesVK;
outInterface->fpGetResourceDescription = GetResourceDescriptorVK;
outInterface->fpDestroyResource = DestroyResourceVK;
outInterface->fpCreatePipeline = CreatePipelineVK;
outInterface->fpDestroyPipeline = DestroyPipelineVK;
outInterface->fpScheduleGpuJob = ScheduleGpuJobVK;
outInterface->fpExecuteGpuJobs = ExecuteGpuJobsVK;
outInterface->scratchBuffer = scratchBuffer;
outInterface->scratchBufferSize = scratchBufferSize;
BackendContext_VK* context = (BackendContext_VK*)scratchBuffer;
context->physicalDevice = physicalDevice;
context->vkFunctionTable.vkGetDeviceProcAddr = getDeviceProcAddr;
return FFX_OK;
}
void loadVKFunctions(BackendContext_VK* backendContext, PFN_vkGetDeviceProcAddr getDeviceProcAddr)
{
FFX_ASSERT(NULL != backendContext);
backendContext->vkFunctionTable.vkSetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT)getDeviceProcAddr(backendContext->device, "vkSetDebugUtilsObjectNameEXT");
backendContext->vkFunctionTable.vkFlushMappedMemoryRanges = (PFN_vkFlushMappedMemoryRanges)getDeviceProcAddr(backendContext->device, "vkFlushMappedMemoryRanges");
backendContext->vkFunctionTable.vkCreateDescriptorPool = (PFN_vkCreateDescriptorPool)getDeviceProcAddr(backendContext->device, "vkCreateDescriptorPool");
backendContext->vkFunctionTable.vkCreateSampler = (PFN_vkCreateSampler)getDeviceProcAddr(backendContext->device, "vkCreateSampler");
backendContext->vkFunctionTable.vkCreateDescriptorSetLayout = (PFN_vkCreateDescriptorSetLayout)getDeviceProcAddr(backendContext->device, "vkCreateDescriptorSetLayout");
backendContext->vkFunctionTable.vkCreateBuffer = (PFN_vkCreateBuffer)getDeviceProcAddr(backendContext->device, "vkCreateBuffer");
backendContext->vkFunctionTable.vkCreateImage = (PFN_vkCreateImage)getDeviceProcAddr(backendContext->device, "vkCreateImage");
backendContext->vkFunctionTable.vkCreateImageView = (PFN_vkCreateImageView)getDeviceProcAddr(backendContext->device, "vkCreateImageView");
backendContext->vkFunctionTable.vkCreateShaderModule = (PFN_vkCreateShaderModule)getDeviceProcAddr(backendContext->device, "vkCreateShaderModule");
backendContext->vkFunctionTable.vkCreatePipelineLayout = (PFN_vkCreatePipelineLayout)getDeviceProcAddr(backendContext->device, "vkCreatePipelineLayout");
backendContext->vkFunctionTable.vkCreateComputePipelines = (PFN_vkCreateComputePipelines)getDeviceProcAddr(backendContext->device, "vkCreateComputePipelines");
backendContext->vkFunctionTable.vkDestroyPipelineLayout = (PFN_vkDestroyPipelineLayout)getDeviceProcAddr(backendContext->device, "vkDestroyPipelineLayout");
backendContext->vkFunctionTable.vkDestroyPipeline = (PFN_vkDestroyPipeline)getDeviceProcAddr(backendContext->device, "vkDestroyPipeline");
backendContext->vkFunctionTable.vkDestroyImage = (PFN_vkDestroyImage)getDeviceProcAddr(backendContext->device, "vkDestroyImage");
backendContext->vkFunctionTable.vkDestroyImageView = (PFN_vkDestroyImageView)getDeviceProcAddr(backendContext->device, "vkDestroyImageView");
backendContext->vkFunctionTable.vkDestroyBuffer = (PFN_vkDestroyBuffer)getDeviceProcAddr(backendContext->device, "vkDestroyBuffer");
backendContext->vkFunctionTable.vkDestroyDescriptorSetLayout = (PFN_vkDestroyDescriptorSetLayout)getDeviceProcAddr(backendContext->device, "vkDestroyDescriptorSetLayout");
backendContext->vkFunctionTable.vkDestroyDescriptorPool = (PFN_vkDestroyDescriptorPool)getDeviceProcAddr(backendContext->device, "vkDestroyDescriptorPool");
backendContext->vkFunctionTable.vkDestroySampler = (PFN_vkDestroySampler)getDeviceProcAddr(backendContext->device, "vkDestroySampler");
backendContext->vkFunctionTable.vkDestroyShaderModule = (PFN_vkDestroyShaderModule)getDeviceProcAddr(backendContext->device, "vkDestroyShaderModule");
backendContext->vkFunctionTable.vkGetBufferMemoryRequirements = (PFN_vkGetBufferMemoryRequirements)getDeviceProcAddr(backendContext->device, "vkGetBufferMemoryRequirements");
backendContext->vkFunctionTable.vkGetImageMemoryRequirements = (PFN_vkGetImageMemoryRequirements)getDeviceProcAddr(backendContext->device, "vkGetImageMemoryRequirements");
backendContext->vkFunctionTable.vkAllocateDescriptorSets = (PFN_vkAllocateDescriptorSets)getDeviceProcAddr(backendContext->device, "vkAllocateDescriptorSets");
backendContext->vkFunctionTable.vkAllocateMemory = (PFN_vkAllocateMemory)getDeviceProcAddr(backendContext->device, "vkAllocateMemory");
backendContext->vkFunctionTable.vkFreeMemory = (PFN_vkFreeMemory)getDeviceProcAddr(backendContext->device, "vkFreeMemory");
backendContext->vkFunctionTable.vkMapMemory = (PFN_vkMapMemory)getDeviceProcAddr(backendContext->device, "vkMapMemory");
backendContext->vkFunctionTable.vkUnmapMemory = (PFN_vkUnmapMemory)getDeviceProcAddr(backendContext->device, "vkUnmapMemory");
backendContext->vkFunctionTable.vkBindBufferMemory = (PFN_vkBindBufferMemory)getDeviceProcAddr(backendContext->device, "vkBindBufferMemory");
backendContext->vkFunctionTable.vkBindImageMemory = (PFN_vkBindImageMemory)getDeviceProcAddr(backendContext->device, "vkBindImageMemory");
backendContext->vkFunctionTable.vkUpdateDescriptorSets = (PFN_vkUpdateDescriptorSets)getDeviceProcAddr(backendContext->device, "vkUpdateDescriptorSets");
backendContext->vkFunctionTable.vkCmdPipelineBarrier = (PFN_vkCmdPipelineBarrier)getDeviceProcAddr(backendContext->device, "vkCmdPipelineBarrier");
backendContext->vkFunctionTable.vkCmdBindPipeline = (PFN_vkCmdBindPipeline)getDeviceProcAddr(backendContext->device, "vkCmdBindPipeline");
backendContext->vkFunctionTable.vkCmdBindDescriptorSets = (PFN_vkCmdBindDescriptorSets)getDeviceProcAddr(backendContext->device, "vkCmdBindDescriptorSets");
backendContext->vkFunctionTable.vkCmdDispatch = (PFN_vkCmdDispatch)getDeviceProcAddr(backendContext->device, "vkCmdDispatch");
backendContext->vkFunctionTable.vkCmdCopyBuffer = (PFN_vkCmdCopyBuffer)getDeviceProcAddr(backendContext->device, "vkCmdCopyBuffer");
backendContext->vkFunctionTable.vkCmdCopyImage = (PFN_vkCmdCopyImage)getDeviceProcAddr(backendContext->device, "vkCmdCopyImage");
backendContext->vkFunctionTable.vkCmdCopyBufferToImage = (PFN_vkCmdCopyBufferToImage)getDeviceProcAddr(backendContext->device, "vkCmdCopyBufferToImage");
backendContext->vkFunctionTable.vkCmdClearColorImage = (PFN_vkCmdClearColorImage)getDeviceProcAddr(backendContext->device, "vkCmdClearColorImage");
}
void setVKObjectName(BackendContext_VK::VKFunctionTable& vkFunctionTable, VkDevice device, VkObjectType objectType, uint64_t object, char* name)
{
VkDebugUtilsObjectNameInfoEXT s{ VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT, nullptr, objectType, object, name };
if (vkFunctionTable.vkSetDebugUtilsObjectNameEXT)
vkFunctionTable.vkSetDebugUtilsObjectNameEXT(device, &s);
}
VkFormat getVKFormatFromSurfaceFormat(FfxSurfaceFormat fmt)
{
switch (fmt) {
case(FFX_SURFACE_FORMAT_R32G32B32A32_TYPELESS):
return VK_FORMAT_R32G32B32A32_SFLOAT;
case(FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT):
return VK_FORMAT_R32G32B32A32_SFLOAT;
case(FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT):
return VK_FORMAT_R16G16B16A16_SFLOAT;
case(FFX_SURFACE_FORMAT_R16G16B16A16_UNORM):
return VK_FORMAT_R16G16B16A16_UNORM;
case(FFX_SURFACE_FORMAT_R32G32_FLOAT):
return VK_FORMAT_R32G32_SFLOAT;
case(FFX_SURFACE_FORMAT_R32_UINT):
return VK_FORMAT_R32_UINT;
case(FFX_SURFACE_FORMAT_R8G8B8A8_TYPELESS):
return VK_FORMAT_R8G8B8A8_UNORM;
case(FFX_SURFACE_FORMAT_R8G8B8A8_UNORM):
return VK_FORMAT_R8G8B8A8_UNORM;
case(FFX_SURFACE_FORMAT_R11G11B10_FLOAT):
return VK_FORMAT_B10G11R11_UFLOAT_PACK32;
case(FFX_SURFACE_FORMAT_R16G16_FLOAT):
return VK_FORMAT_R16G16_SFLOAT;
case(FFX_SURFACE_FORMAT_R16G16_UINT):
return VK_FORMAT_R16G16_UINT;
case(FFX_SURFACE_FORMAT_R16_FLOAT):
return VK_FORMAT_R16_SFLOAT;
case(FFX_SURFACE_FORMAT_R16_UINT):
return VK_FORMAT_R16_UINT;
case(FFX_SURFACE_FORMAT_R16_UNORM):
return VK_FORMAT_R16_UNORM;
case(FFX_SURFACE_FORMAT_R16_SNORM):
return VK_FORMAT_R16_SNORM;
case(FFX_SURFACE_FORMAT_R8_UNORM):
return VK_FORMAT_R8_UNORM;
case(FFX_SURFACE_FORMAT_R8G8_UNORM):
return VK_FORMAT_R8G8_UNORM;
case(FFX_SURFACE_FORMAT_R32_FLOAT):
return VK_FORMAT_R32_SFLOAT;
case(FFX_SURFACE_FORMAT_R8_UINT):
return VK_FORMAT_R8_UINT;
default:
return VK_FORMAT_UNDEFINED;
}
}
VkImageUsageFlags getVKImageUsageFlagsFromResourceUsage(FfxResourceUsage flags)
{
VkImageUsageFlags ret = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
if (flags & FFX_RESOURCE_USAGE_RENDERTARGET) ret |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
if (flags & FFX_RESOURCE_USAGE_UAV) ret |= (VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT);
return ret;
}
VkBufferUsageFlags getVKBufferUsageFlagsFromResourceUsage(FfxResourceUsage flags)
{
if (flags & FFX_RESOURCE_USAGE_UAV)
return VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
else
return VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
}
VkImageType getVKImageTypeFromResourceType(FfxResourceType type)
{
switch (type) {
case(FFX_RESOURCE_TYPE_TEXTURE1D):
return VK_IMAGE_TYPE_1D;
case(FFX_RESOURCE_TYPE_TEXTURE2D):
return VK_IMAGE_TYPE_2D;
case(FFX_RESOURCE_TYPE_TEXTURE3D):
return VK_IMAGE_TYPE_3D;
default:
return VK_IMAGE_TYPE_MAX_ENUM;
}
}
VkImageLayout getVKImageLayoutFromResourceState(FfxResourceStates state)
{
switch (state) {
case(FFX_RESOURCE_STATE_GENERIC_READ):
return VK_IMAGE_LAYOUT_GENERAL;
case(FFX_RESOURCE_STATE_UNORDERED_ACCESS):
return VK_IMAGE_LAYOUT_GENERAL;
case(FFX_RESOURCE_STATE_COMPUTE_READ):
return VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
case FFX_RESOURCE_STATE_COPY_SRC:
return VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
case FFX_RESOURCE_STATE_COPY_DEST:
return VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
default:
return VK_IMAGE_LAYOUT_GENERAL;
}
}
VkPipelineStageFlags getVKPipelineStageFlagsFromResourceState(FfxResourceStates state)
{
switch (state) {
case(FFX_RESOURCE_STATE_GENERIC_READ):
case(FFX_RESOURCE_STATE_UNORDERED_ACCESS):
case(FFX_RESOURCE_STATE_COMPUTE_READ):
return VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
case FFX_RESOURCE_STATE_COPY_SRC:
case FFX_RESOURCE_STATE_COPY_DEST:
return VK_PIPELINE_STAGE_TRANSFER_BIT;
default:
return VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
}
}
VkAccessFlags getVKAccessFlagsFromResourceState(FfxResourceStates state)
{
switch (state) {
case(FFX_RESOURCE_STATE_GENERIC_READ):
return VK_ACCESS_SHADER_READ_BIT;
case(FFX_RESOURCE_STATE_UNORDERED_ACCESS):
return VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
case(FFX_RESOURCE_STATE_COMPUTE_READ):
return VK_ACCESS_SHADER_READ_BIT;
case FFX_RESOURCE_STATE_COPY_SRC:
return VK_ACCESS_TRANSFER_READ_BIT;
case FFX_RESOURCE_STATE_COPY_DEST:
return VK_ACCESS_TRANSFER_WRITE_BIT;
default:
return VK_ACCESS_SHADER_READ_BIT;
}
}
FfxSurfaceFormat ffxGetSurfaceFormatVK(VkFormat fmt)
{
switch (fmt) {
case(VK_FORMAT_R32G32B32A32_SFLOAT):
return FFX_SURFACE_FORMAT_R32G32B32A32_FLOAT;
case(VK_FORMAT_R16G16B16A16_SFLOAT):
return FFX_SURFACE_FORMAT_R16G16B16A16_FLOAT;
case(VK_FORMAT_R16G16B16A16_UNORM):
return FFX_SURFACE_FORMAT_R16G16B16A16_UNORM;
case(VK_FORMAT_R32G32_SFLOAT):
return FFX_SURFACE_FORMAT_R32G32_FLOAT;
case(VK_FORMAT_R32_UINT):
return FFX_SURFACE_FORMAT_R32_UINT;
case(VK_FORMAT_R8G8B8A8_UNORM):
return FFX_SURFACE_FORMAT_R8G8B8A8_UNORM;
case(VK_FORMAT_B10G11R11_UFLOAT_PACK32):
return FFX_SURFACE_FORMAT_R11G11B10_FLOAT;
case(VK_FORMAT_R16G16_SFLOAT):
return FFX_SURFACE_FORMAT_R16G16_FLOAT;
case(VK_FORMAT_R16G16_UINT):
return FFX_SURFACE_FORMAT_R16G16_UINT;
case(VK_FORMAT_R16_SFLOAT):
return FFX_SURFACE_FORMAT_R16_FLOAT;
case(VK_FORMAT_R16_UINT):
return FFX_SURFACE_FORMAT_R16_UINT;
case(VK_FORMAT_R16_UNORM):
return FFX_SURFACE_FORMAT_R16_UNORM;
case(VK_FORMAT_R16_SNORM):
return FFX_SURFACE_FORMAT_R16_SNORM;
case(VK_FORMAT_R8_UNORM):
return FFX_SURFACE_FORMAT_R8_UNORM;
case(VK_FORMAT_R32_SFLOAT):
return FFX_SURFACE_FORMAT_R32_FLOAT;
case(VK_FORMAT_R8_UINT):
return FFX_SURFACE_FORMAT_R8_UINT;
default:
return FFX_SURFACE_FORMAT_UNKNOWN;
}
}
uint32_t findMemoryTypeIndex(VkPhysicalDevice physicalDevice, VkMemoryRequirements memRequirements, VkMemoryPropertyFlags requestedProperties, VkMemoryPropertyFlags& outProperties)
{
FFX_ASSERT(NULL != physicalDevice);
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
uint32_t bestCandidate = UINT32_MAX;
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
if ((memRequirements.memoryTypeBits & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & requestedProperties)) {
// if just device-local memory is requested, make sure this is the invisible heap to prevent over-subscribing the local heap
if (requestedProperties == VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT && (memProperties.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
continue;
bestCandidate = i;
outProperties = memProperties.memoryTypes[i].propertyFlags;
// if host-visible memory is requested, check for host coherency as well and if available, return immediately
if ((requestedProperties & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) && (memProperties.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
return bestCandidate;
}
}
return bestCandidate;
}
VkDescriptorBufferInfo accquireDynamicUBO(BackendContext_VK* backendContext, uint32_t size, void* pData)
{
// the ubo ring buffer is pre-populated with VkBuffer objects of 256-bytes to prevent creating buffers at runtime
FFX_ASSERT(size <= 256);
BackendContext_VK::UniformBuffer& ubo = backendContext->uboRingBuffer[backendContext->uboRingBufferIndex];
VkDescriptorBufferInfo bufferInfo = {};
bufferInfo.buffer = ubo.bufferResource;
bufferInfo.offset = 0;
bufferInfo.range = size;
if (pData)
{
memcpy(ubo.pData, pData, size);
// flush mapped range if memory type is not coherant
if ((backendContext->uboMemoryProperties & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) == 0)
{
VkMappedMemoryRange memoryRange;
memset(&memoryRange, 0, sizeof(memoryRange));
memoryRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
memoryRange.memory = backendContext->uboMemory;
memoryRange.offset = 256 * backendContext->uboRingBufferIndex;
memoryRange.size = size;
backendContext->vkFunctionTable.vkFlushMappedMemoryRanges(backendContext->device, 1, &memoryRange);
}
}
backendContext->uboRingBufferIndex++;
if (backendContext->uboRingBufferIndex >= FSR2_UBO_RING_BUFFER_SIZE)
backendContext->uboRingBufferIndex = 0;
return bufferInfo;
}
static uint32_t getDefaultSubgroupSize(const BackendContext_VK* backendContext)
{
VkPhysicalDeviceVulkan11Properties vulkan11Properties = {};
vulkan11Properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES;
VkPhysicalDeviceProperties2 deviceProperties2 = {};
deviceProperties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
deviceProperties2.pNext = &vulkan11Properties;
vkGetPhysicalDeviceProperties2(backendContext->physicalDevice, &deviceProperties2);
FFX_ASSERT(vulkan11Properties.subgroupSize == 32 || vulkan11Properties.subgroupSize == 64); // current desktop market
return vulkan11Properties.subgroupSize;
}
// Create a FfxFsr2Device from a VkDevice
FfxDevice ffxGetDeviceVK(VkDevice vkDevice)
{
FFX_ASSERT(NULL != vkDevice);
return reinterpret_cast<FfxDevice>(vkDevice);
}
FfxCommandList ffxGetCommandListVK(VkCommandBuffer cmdBuf)
{
FFX_ASSERT(NULL != cmdBuf);
return reinterpret_cast<FfxCommandList>(cmdBuf);
}
FfxResource ffxGetTextureResourceVK(FfxFsr2Context* context, VkImage imgVk, VkImageView imageView, uint32_t width, uint32_t height, VkFormat imgFormat, const wchar_t* name, FfxResourceStates state)
{
FfxResource resource = {};
resource.resource = reinterpret_cast<void*>(imgVk);
resource.state = state;
resource.descriptorData = reinterpret_cast<uint64_t>(imageView);
resource.description.flags = FFX_RESOURCE_FLAGS_NONE;
resource.description.type = FFX_RESOURCE_TYPE_TEXTURE2D;
resource.description.width = width;
resource.description.height = height;
resource.description.depth = 1;
resource.description.mipCount = 1;
resource.description.format = ffxGetSurfaceFormatVK(imgFormat);
switch (imgFormat)
{
case VK_FORMAT_D16_UNORM:
case VK_FORMAT_D32_SFLOAT:
case VK_FORMAT_D16_UNORM_S8_UINT:
case VK_FORMAT_D24_UNORM_S8_UINT:
case VK_FORMAT_D32_SFLOAT_S8_UINT:
{
resource.isDepth = true;
break;
}
default:
{
resource.isDepth = false;
break;
}
}
#ifdef _DEBUG
if (name) {
wcscpy(resource.name, name);
}
#endif
return resource;
}
FfxResource ffxGetBufferResourceVK(FfxFsr2Context* context, VkBuffer bufVk, uint32_t size, const wchar_t* name, FfxResourceStates state)
{
FfxResource resource = {};
resource.resource = reinterpret_cast<void*>(bufVk);
resource.state = state;
resource.descriptorData = 0;
resource.description.flags = FFX_RESOURCE_FLAGS_NONE;
resource.description.type = FFX_RESOURCE_TYPE_BUFFER;
resource.description.width = size;
resource.description.height = 1;
resource.description.depth = 1;
resource.description.mipCount = 1;
resource.description.format = FFX_SURFACE_FORMAT_UNKNOWN;
resource.isDepth = false;
#ifdef _DEBUG
if (name) {
wcscpy(resource.name, name);
}
#endif
return resource;
}
VkImage ffxGetVkImage(FfxFsr2Context* context, uint32_t resId)
{
FFX_ASSERT(NULL != context);
FfxFsr2Context_Private* contextPrivate = (FfxFsr2Context_Private*)(context);
BackendContext_VK* backendContext = (BackendContext_VK*)(contextPrivate->contextDescription.callbacks.scratchBuffer);
int32_t internalIndex = contextPrivate->uavResources[resId].internalIndex;
return (internalIndex == -1) ? nullptr : backendContext->resources[internalIndex].imageResource;
}
VkImageView ffxGetVkImageView(FfxFsr2Context* context, uint32_t resId)
{
FFX_ASSERT(NULL != context);
FfxFsr2Context_Private* contextPrivate = (FfxFsr2Context_Private*)(context);
BackendContext_VK* backendContext = (BackendContext_VK*)(contextPrivate->contextDescription.callbacks.scratchBuffer);
BackendContext_VK::Resource& internalRes = backendContext->resources[contextPrivate->uavResources[resId].internalIndex];
return internalRes.allMipsImageView;
}
VkImageLayout ffxGetVkImageLayout(FfxFsr2Context* context, uint32_t resId)
{
FfxFsr2Context_Private* contextPrivate = (FfxFsr2Context_Private*)(context);
BackendContext_VK* backendContext = (BackendContext_VK*)(contextPrivate->contextDescription.callbacks.scratchBuffer);
BackendContext_VK::Resource& internalRes = backendContext->resources[contextPrivate->uavResources[resId].internalIndex];
return getVKImageLayoutFromResourceState(internalRes.state);
}
FfxErrorCode RegisterResourceVK(
FfxFsr2Interface* backendInterface,
const FfxResource* inFfxResource,
FfxResourceInternal* outFfxResourceInternal
)
{
FFX_ASSERT(NULL != backendInterface);
BackendContext_VK* backendContext = (BackendContext_VK*)(backendInterface->scratchBuffer);
if (inFfxResource->resource == nullptr) {
outFfxResourceInternal->internalIndex = FFX_FSR2_RESOURCE_IDENTIFIER_NULL;
return FFX_OK;
}
FFX_ASSERT(backendContext->nextDynamicResource > backendContext->nextStaticResource);
outFfxResourceInternal->internalIndex = backendContext->nextDynamicResource--;
BackendContext_VK::Resource* backendResource = &backendContext->resources[outFfxResourceInternal->internalIndex];
backendResource->resourceDescription = inFfxResource->description;
backendResource->state = inFfxResource->state;
backendResource->undefined = false;
#ifdef _DEBUG
size_t retval = 0;
wcstombs_s(&retval, backendResource->resourceName, sizeof(backendResource->resourceName), inFfxResource->name, sizeof(backendResource->resourceName));
if (retval >= 64) backendResource->resourceName[63] = '\0';
#endif
if (inFfxResource->description.type == FFX_RESOURCE_TYPE_BUFFER)
{
VkBuffer buffer = reinterpret_cast<VkBuffer>(inFfxResource->resource);
backendResource->bufferResource = buffer;
}
else
{
VkImage image = reinterpret_cast<VkImage>(inFfxResource->resource);
VkImageView imageView = reinterpret_cast<VkImageView>(inFfxResource->descriptorData);
backendResource->imageResource = image;
if (image) {
if (imageView) {
if (inFfxResource->isDepth)
backendResource->aspectFlags = VK_IMAGE_ASPECT_DEPTH_BIT;
else
backendResource->aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT;
backendResource->allMipsImageView = imageView;
backendResource->singleMipImageViews[0] = imageView;
}
}
}
return FFX_OK;
}
// dispose dynamic resources: This should be called at the end of the frame
FfxErrorCode UnregisterResourcesVK(FfxFsr2Interface* backendInterface)
{
FFX_ASSERT(NULL != backendInterface);
BackendContext_VK* backendContext = (BackendContext_VK*)(backendInterface->scratchBuffer);
backendContext->nextDynamicResource = FSR2_MAX_RESOURCE_COUNT - 1;
return FFX_OK;
}
FfxErrorCode GetDeviceCapabilitiesVK(FfxFsr2Interface* backendInterface, FfxDeviceCapabilities* deviceCapabilities, FfxDevice device)
{
BackendContext_VK* backendContext = (BackendContext_VK*)backendInterface->scratchBuffer;
const uint32_t defaultSubgroupSize = getDefaultSubgroupSize(backendContext);
// no shader model in vulkan so assume the minimum
deviceCapabilities->minimumSupportedShaderModel = FFX_SHADER_MODEL_5_1;
deviceCapabilities->waveLaneCountMin = defaultSubgroupSize;
deviceCapabilities->waveLaneCountMax = defaultSubgroupSize;
deviceCapabilities->fp16Supported = false;
deviceCapabilities->raytracingSupported = false;
// check if extensions are enabled
for (uint32_t i = 0; i < backendContext->numDeviceExtensions; i++)
{
if (strcmp(backendContext->extensionProperties[i].extensionName, VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME) == 0)
{
// check if we the max subgroup size allows us to use wave64
VkPhysicalDeviceSubgroupSizeControlProperties subgroupSizeControlProperties = {};
subgroupSizeControlProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES;
VkPhysicalDeviceProperties2 deviceProperties2 = {};
deviceProperties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
deviceProperties2.pNext = &subgroupSizeControlProperties;
vkGetPhysicalDeviceProperties2(backendContext->physicalDevice, &deviceProperties2);
// NOTE: It's important to check requiredSubgroupSizeStages flags (and it's required by the spec).
// As of August 2022, AMD's Vulkan drivers do not support subgroup size selection through Vulkan API
// and this information is reported through requiredSubgroupSizeStages flags.
if (subgroupSizeControlProperties.requiredSubgroupSizeStages & VK_SHADER_STAGE_COMPUTE_BIT)
{
deviceCapabilities->waveLaneCountMin = subgroupSizeControlProperties.minSubgroupSize;
deviceCapabilities->waveLaneCountMax = subgroupSizeControlProperties.maxSubgroupSize;
}
}
if (strcmp(backendContext->extensionProperties[i].extensionName, VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME) == 0)
{
// check for fp16 support
VkPhysicalDeviceShaderFloat16Int8Features shaderFloat18Int8Features = {};
shaderFloat18Int8Features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES;
VkPhysicalDeviceFeatures2 physicalDeviceFeatures2 = {};
physicalDeviceFeatures2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
physicalDeviceFeatures2.pNext = &shaderFloat18Int8Features;
vkGetPhysicalDeviceFeatures2(backendContext->physicalDevice, &physicalDeviceFeatures2);
deviceCapabilities->fp16Supported = (bool)shaderFloat18Int8Features.shaderFloat16;
}
if (strcmp(backendContext->extensionProperties[i].extensionName, VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME) == 0)
{
// check for ray tracing support
VkPhysicalDeviceAccelerationStructureFeaturesKHR accelerationStructureFeatures = {};
accelerationStructureFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ACCELERATION_STRUCTURE_FEATURES_KHR;
VkPhysicalDeviceFeatures2 physicalDeviceFeatures2 = {};
physicalDeviceFeatures2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
physicalDeviceFeatures2.pNext = &accelerationStructureFeatures;
vkGetPhysicalDeviceFeatures2(backendContext->physicalDevice, &physicalDeviceFeatures2);
deviceCapabilities->raytracingSupported = (bool)accelerationStructureFeatures.accelerationStructure;
}
}
return FFX_OK;
}
FfxErrorCode CreateBackendContextVK(FfxFsr2Interface* backendInterface, FfxDevice device)
{
FFX_ASSERT(NULL != backendInterface);
VkDevice vkDevice = reinterpret_cast<VkDevice>(device);
// set up some internal resources we need (space for resource views and constant buffers)
BackendContext_VK* backendContext = (BackendContext_VK*)backendInterface->scratchBuffer;
backendContext->extensionProperties = (VkExtensionProperties*)(backendContext + 1);
// make sure the extra parameters were already passed in
FFX_ASSERT(backendContext->physicalDevice != NULL);
// if vkGetDeviceProcAddr is NULL, use the one from the vulkan header
if (backendContext->vkFunctionTable.vkGetDeviceProcAddr == NULL)
backendContext->vkFunctionTable.vkGetDeviceProcAddr = vkGetDeviceProcAddr;
if (vkDevice != NULL) {
backendContext->device = vkDevice;
}
backendContext->nextStaticResource = 0;
backendContext->nextDynamicResource = FSR2_MAX_RESOURCE_COUNT - 1;
// load vulkan functions
loadVKFunctions(backendContext, backendContext->vkFunctionTable.vkGetDeviceProcAddr);
// enumerate all the device extensions
backendContext->numDeviceExtensions = 0;
vkEnumerateDeviceExtensionProperties(backendContext->physicalDevice, nullptr, &backendContext->numDeviceExtensions, nullptr);
vkEnumerateDeviceExtensionProperties(backendContext->physicalDevice, nullptr, &backendContext->numDeviceExtensions, backendContext->extensionProperties);
// create descriptor pool
VkDescriptorPoolCreateInfo descriptorPoolCreateInfo = {};
VkDescriptorPoolSize poolSizes[] = {
{ VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, FSR2_MAX_IMAGE_VIEWS * FSR2_MAX_BUFFERED_DESCRIPTORS },
{ VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, FSR2_MAX_IMAGE_VIEWS * FSR2_MAX_BUFFERED_DESCRIPTORS },
{ VK_DESCRIPTOR_TYPE_SAMPLER, FSR2_MAX_SAMPLERS * FSR2_MAX_BUFFERED_DESCRIPTORS },
{ VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, FSR2_MAX_UNIFORM_BUFFERS * FSR2_MAX_BUFFERED_DESCRIPTORS },
};
descriptorPoolCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
descriptorPoolCreateInfo.maxSets = (FSR2_MAX_BUFFERED_DESCRIPTORS * FSR2_MAX_QUEUED_FRAMES);
descriptorPoolCreateInfo.poolSizeCount = 4;
descriptorPoolCreateInfo.pPoolSizes = poolSizes;
if (backendContext->vkFunctionTable.vkCreateDescriptorPool(backendContext->device, &descriptorPoolCreateInfo, nullptr, &backendContext->descPool) != VK_SUCCESS) {
return FFX_ERROR_BACKEND_API_ERROR;
}
VkSamplerCreateInfo samplerCreateInfo = {};
samplerCreateInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerCreateInfo.magFilter = VK_FILTER_NEAREST;
samplerCreateInfo.minFilter = VK_FILTER_NEAREST;
samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
samplerCreateInfo.minLod = -1000;
samplerCreateInfo.maxLod = 1000;
samplerCreateInfo.maxAnisotropy = 1.0f;
if (backendContext->vkFunctionTable.vkCreateSampler(backendContext->device, &samplerCreateInfo, nullptr, &backendContext->pointSampler) != VK_SUCCESS) {
return FFX_ERROR_BACKEND_API_ERROR;
}
samplerCreateInfo.magFilter = VK_FILTER_LINEAR;
samplerCreateInfo.minFilter = VK_FILTER_LINEAR;
if (backendContext->vkFunctionTable.vkCreateSampler(backendContext->device, &samplerCreateInfo, nullptr, &backendContext->linearSampler) != VK_SUCCESS) {
return FFX_ERROR_BACKEND_API_ERROR;
}
{
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo = {};
VkDescriptorSetLayoutBinding bindings[] = {
{ 0, VK_DESCRIPTOR_TYPE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT, &backendContext->pointSampler },
{ 1, VK_DESCRIPTOR_TYPE_SAMPLER, 1, VK_SHADER_STAGE_COMPUTE_BIT, &backendContext->linearSampler },
};
descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptorSetLayoutCreateInfo.bindingCount = 2;
descriptorSetLayoutCreateInfo.pBindings = bindings;
if (backendContext->vkFunctionTable.vkCreateDescriptorSetLayout(backendContext->device, &descriptorSetLayoutCreateInfo, NULL, &backendContext->samplerDescriptorSetLayout) != VK_SUCCESS) {
return FFX_ERROR_BACKEND_API_ERROR;
}
}
{
VkDescriptorSetAllocateInfo allocateInfo = {};
allocateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocateInfo.descriptorPool = backendContext->descPool;
allocateInfo.descriptorSetCount = 1;
allocateInfo.pSetLayouts = &backendContext->samplerDescriptorSetLayout;
backendContext->vkFunctionTable.vkAllocateDescriptorSets(backendContext->device, &allocateInfo, &backendContext->samplerDescriptorSet);
}
// allocate ring buffer of uniform buffers
{
for (uint32_t i = 0; i < FSR2_UBO_RING_BUFFER_SIZE; i++)
{
BackendContext_VK::UniformBuffer& ubo = backendContext->uboRingBuffer[i];
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = 256;
bufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (backendContext->vkFunctionTable.vkCreateBuffer(backendContext->device, &bufferInfo, NULL, &ubo.bufferResource) != VK_SUCCESS) {
return FFX_ERROR_BACKEND_API_ERROR;
}
}
// allocate memory block for all uniform buffers
VkMemoryRequirements memRequirements = {};
backendContext->vkFunctionTable.vkGetBufferMemoryRequirements(backendContext->device, backendContext->uboRingBuffer[0].bufferResource, &memRequirements);
VkMemoryPropertyFlags requiredMemoryProperties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = FSR2_UBO_MEMORY_BLOCK_SIZE;
allocInfo.memoryTypeIndex = findMemoryTypeIndex(backendContext->physicalDevice, memRequirements, requiredMemoryProperties, backendContext->uboMemoryProperties);
if (allocInfo.memoryTypeIndex == UINT32_MAX) {
requiredMemoryProperties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
allocInfo.memoryTypeIndex = findMemoryTypeIndex(backendContext->physicalDevice, memRequirements, requiredMemoryProperties, backendContext->uboMemoryProperties);
if (allocInfo.memoryTypeIndex == UINT32_MAX) {
return FFX_ERROR_BACKEND_API_ERROR;
}
}
VkResult result = backendContext->vkFunctionTable.vkAllocateMemory(backendContext->device, &allocInfo, nullptr, &backendContext->uboMemory);
if (result != VK_SUCCESS) {
switch (result) {
case(VK_ERROR_OUT_OF_HOST_MEMORY):
case(VK_ERROR_OUT_OF_DEVICE_MEMORY):
return FFX_ERROR_OUT_OF_MEMORY;
default:
return FFX_ERROR_BACKEND_API_ERROR;
}
}
// map the memory block
uint8_t* pData = nullptr;
if (backendContext->vkFunctionTable.vkMapMemory(backendContext->device, backendContext->uboMemory, 0, FSR2_UBO_MEMORY_BLOCK_SIZE, 0, reinterpret_cast<void**>(&pData)) != VK_SUCCESS) {
return FFX_ERROR_BACKEND_API_ERROR;
}
// bind each 256-byte block to the ubos
for (uint32_t i = 0; i < FSR2_UBO_RING_BUFFER_SIZE; i++)
{
BackendContext_VK::UniformBuffer& ubo = backendContext->uboRingBuffer[i];
// get the buffer memory requirements for each buffer object to silence validation errors
VkMemoryRequirements memRequirements = {};
backendContext->vkFunctionTable.vkGetBufferMemoryRequirements(backendContext->device, ubo.bufferResource, &memRequirements);
ubo.pData = pData + 256 * i;
if (backendContext->vkFunctionTable.vkBindBufferMemory(backendContext->device, ubo.bufferResource, backendContext->uboMemory, 256 * i) != VK_SUCCESS) {
return FFX_ERROR_BACKEND_API_ERROR;
}
}
}
backendContext->gpuJobCount = 0;
backendContext->scheduledImageBarrierCount = 0;
backendContext->scheduledBufferBarrierCount = 0;
backendContext->stagingResourceCount = 0;
backendContext->allocatedPipelineLayoutCount = 0;
backendContext->srcStageMask = 0;
backendContext->dstStageMask = 0;
backendContext->uboRingBufferIndex = 0;
return FFX_OK;
}
FfxErrorCode DestroyBackendContextVK(FfxFsr2Interface* backendInterface)
{
FFX_ASSERT(NULL != backendInterface);
BackendContext_VK* backendContext = (BackendContext_VK*)backendInterface->scratchBuffer;
for (uint32_t i = 0; i < backendContext->stagingResourceCount; i++)
DestroyResourceVK(backendInterface, backendContext->stagingResources[i]);
for (uint32_t i = 0; i < FSR2_UBO_RING_BUFFER_SIZE; i++)
{
BackendContext_VK::UniformBuffer& ubo = backendContext->uboRingBuffer[i];
backendContext->vkFunctionTable.vkDestroyBuffer(backendContext->device, ubo.bufferResource, nullptr);
ubo.bufferResource = nullptr;
ubo.pData = nullptr;
}
backendContext->vkFunctionTable.vkUnmapMemory(backendContext->device, backendContext->uboMemory);
backendContext->vkFunctionTable.vkFreeMemory(backendContext->device, backendContext->uboMemory, nullptr);
backendContext->uboMemory = nullptr;
backendContext->vkFunctionTable.vkDestroyDescriptorPool(backendContext->device, backendContext->descPool, nullptr);
backendContext->descPool = nullptr;
backendContext->vkFunctionTable.vkDestroyDescriptorSetLayout(backendContext->device, backendContext->samplerDescriptorSetLayout, nullptr);
backendContext->samplerDescriptorSet = nullptr;