-
Notifications
You must be signed in to change notification settings - Fork 56
/
DDGIVolume_VK.cpp
1859 lines (1540 loc) · 96.6 KB
/
DDGIVolume_VK.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#include "rtxgi/ddgi/gfx/DDGIVolume_VK.h"
#include "rtxgi/VulkanExtensions.h"
#include <cstring>
#include <random>
#include <string>
#include <vector>
#define VKFAILED(x) (x != VK_SUCCESS)
#ifdef RTXGI_GFX_NAME_OBJECTS
/**
* Sets a debug name for an object.
*/
void SetObjectName(VkDevice device, uint64_t handle, const char* name, VkObjectType type)
{
VkDebugUtilsObjectNameInfoEXT objectNameInfo = {};
objectNameInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
objectNameInfo.pNext = nullptr;
objectNameInfo.objectType = type;
objectNameInfo.objectHandle = handle;
objectNameInfo.pObjectName = name;
vkSetDebugUtilsObjectNameEXT(device, &objectNameInfo);
}
#endif
/**
* Add a performance marker to the command buffer.
*/
void AddPerfMarker(VkCommandBuffer cmdBuffer, uint8_t r, uint8_t g, uint8_t b, std::string name)
{
VkDebugUtilsLabelEXT label = {};
label.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_LABEL_EXT;
label.pLabelName = name.c_str();
label.color[0] = (float)r / 255.f;
label.color[1] = (float)g / 255.f;
label.color[2] = (float)b / 255.f;
label.color[3] = 1.f;
vkCmdBeginDebugUtilsLabelEXT(cmdBuffer, &label);
}
namespace rtxgi
{
namespace vulkan
{
//------------------------------------------------------------------------
// Private RTXGI Namespace Helper Functions
//------------------------------------------------------------------------
ERTXGIStatus ValidateManagedResourcesDesc(const DDGIVolumeManagedResourcesDesc& desc)
{
// Vulkan devices and descriptor pool
if (desc.device == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_DEVICE;
if (desc.physicalDevice == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_PHYSICAL_DEVICE;
if (desc.descriptorPool == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_DESCRIPTOR_POOL;
// Shader bytecode
if (!ValidateShaderBytecode(desc.probeBlendingIrradianceCS)) return ERTXGIStatus::ERROR_DDGI_INVALID_BYTECODE_PROBE_BLENDING_IRRADIANCE;
if (!ValidateShaderBytecode(desc.probeBlendingDistanceCS)) return ERTXGIStatus::ERROR_DDGI_INVALID_BYTECODE_PROBE_BLENDING_DISTANCE;
if (!ValidateShaderBytecode(desc.probeRelocation.updateCS)) return ERTXGIStatus::ERROR_DDGI_INVALID_BYTECODE_PROBE_RELOCATION;
if (!ValidateShaderBytecode(desc.probeRelocation.resetCS)) return ERTXGIStatus::ERROR_DDGI_INVALID_BYTECODE_PROBE_RELOCATION_RESET;
if (!ValidateShaderBytecode(desc.probeClassification.updateCS)) return ERTXGIStatus::ERROR_DDGI_INVALID_BYTECODE_PROBE_CLASSIFICATION;
if (!ValidateShaderBytecode(desc.probeClassification.resetCS)) return ERTXGIStatus::ERROR_DDGI_INVALID_BYTECODE_PROBE_CLASSIFICATION_RESET;
if (!ValidateShaderBytecode(desc.probeVariability.reductionCS)) return ERTXGIStatus::ERROR_DDGI_INVALID_BYTECODE_PROBE_CLASSIFICATION_RESET;
if (!ValidateShaderBytecode(desc.probeVariability.extraReductionCS)) return ERTXGIStatus::ERROR_DDGI_INVALID_BYTECODE_PROBE_VARIABILITY_EXTRA_REDUCTION;
return ERTXGIStatus::OK;
}
ERTXGIStatus ValidateUnmanagedResourcesDesc(const DDGIVolumeUnmanagedResourcesDesc& desc)
{
// Pipeline Layout and Descriptor Set
if (desc.pipelineLayout == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_PIPELINE_LAYOUT;
if (desc.descriptorSet == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_DESCRIPTOR_SET;
// Texture Arrays
if (desc.probeRayData == nullptr) return ERTXGIStatus::ERROR_DDGI_INVALID_TEXTURE_PROBE_RAY_DATA;
if (desc.probeIrradiance == nullptr) return ERTXGIStatus::ERROR_DDGI_INVALID_TEXTURE_PROBE_IRRADIANCE;
if (desc.probeDistance == nullptr) return ERTXGIStatus::ERROR_DDGI_INVALID_TEXTURE_PROBE_DISTANCE;
if (desc.probeData == nullptr) return ERTXGIStatus::ERROR_DDGI_INVALID_TEXTURE_PROBE_DATA;
if (desc.probeVariability == nullptr) return ERTXGIStatus::ERROR_DDGI_INVALID_TEXTURE_PROBE_VARIABILITY;
if (desc.probeVariabilityAverage == nullptr) return ERTXGIStatus::ERROR_DDGI_INVALID_TEXTURE_PROBE_VARIABILITY_AVERAGE;
if (desc.probeVariabilityReadback == nullptr) return ERTXGIStatus::ERROR_DDGI_INVALID_TEXTURE_PROBE_VARIABILITY_READBACK;
// Texture Array Memory
if (desc.probeRayDataMemory == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_MEMORY_PROBE_RAY_DATA;
if (desc.probeIrradianceMemory == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_MEMORY_PROBE_IRRADIANCE;
if (desc.probeDistanceMemory == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_MEMORY_PROBE_DISTANCE;
if (desc.probeDataMemory == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_MEMORY_PROBE_DATA;
if (desc.probeVariabilityMemory == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_MEMORY_PROBE_VARIABILITY;
if (desc.probeVariabilityAverageMemory == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_MEMORY_PROBE_VARIABILITY_AVERAGE;
if (desc.probeVariabilityReadbackMemory == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_MEMORY_PROBE_VARIABILITY_READBACK;
// Texture Array Views
if (desc.probeRayDataView == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_VIEW_PROBE_RAY_DATA;
if (desc.probeIrradianceView == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_VIEW_PROBE_IRRADIANCE;
if (desc.probeDistanceView == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_VIEW_PROBE_DISTANCE;
if (desc.probeDataView == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_VIEW_PROBE_DATA;
if (desc.probeVariabilityView == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_VIEW_PROBE_VARIABILITY;
if (desc.probeVariabilityAverageView == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_IMAGE_VIEW_PROBE_VARIABILITY_AVERAGE;
// Shader Modules
if (desc.probeBlendingIrradianceModule == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_SHADER_MODULE_PROBE_BLENDING_IRRADIANCE;
if (desc.probeBlendingDistanceModule == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_SHADER_MODULE_PROBE_BLENDING_DISTANCE;
if (desc.probeRelocation.updateModule == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_SHADER_MODULE_PROBE_RELOCATION;
if (desc.probeRelocation.resetModule == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_SHADER_MODULE_PROBE_RELOCATION_RESET;
if (desc.probeClassification.updateModule == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_SHADER_MODULE_PROBE_CLASSIFICATION;
if (desc.probeClassification.resetModule == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_SHADER_MODULE_PROBE_CLASSIFICATION_RESET;
if (desc.probeVariabilityPipelines.reductionModule == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_SHADER_MODULE_PROBE_VARIABILITY_REDUCTION;
if (desc.probeVariabilityPipelines.extraReductionModule == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_SHADER_MODULE_PROBE_VARIABILITY_EXTRA_REDUCTION;
// Pipelines
if (desc.probeBlendingIrradiancePipeline == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_PIPELINE_PROBE_BLENDING_IRRADIANCE;
if (desc.probeBlendingDistancePipeline == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_PIPELINE_PROBE_BLENDING_DISTANCE;
if (desc.probeRelocation.updatePipeline == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_PIPELINE_PROBE_RELOCATION;
if (desc.probeRelocation.resetPipeline == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_PIPELINE_PROBE_RELOCATION_RESET;
if (desc.probeClassification.updatePipeline == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_PIPELINE_PROBE_CLASSIFICATION;
if (desc.probeClassification.resetPipeline == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_PIPELINE_PROBE_CLASSIFICATION_RESET;
if (desc.probeVariabilityPipelines.reductionPipeline == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_PIPELINE_PROBE_VARIABILITY_REDUCTION;
if (desc.probeVariabilityPipelines.extraReductionPipeline == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_PIPELINE_PROBE_VARIABILITY_EXTRA_REDUCTION;
return ERTXGIStatus::OK;
}
//------------------------------------------------------------------------
// Public RTXGI Namespace DDGI Functions
//------------------------------------------------------------------------
VkFormat GetDDGIVolumeTextureFormat(EDDGIVolumeTextureType type, EDDGIVolumeTextureFormat format)
{
if (type == EDDGIVolumeTextureType::RayData)
{
if (format == EDDGIVolumeTextureFormat::F32x2) return VK_FORMAT_R32G32_SFLOAT;
else if (format == EDDGIVolumeTextureFormat::F32x4) return VK_FORMAT_R32G32B32A32_SFLOAT;
}
else if (type == EDDGIVolumeTextureType::Irradiance)
{
if (format == EDDGIVolumeTextureFormat::U32) return VK_FORMAT_A2B10G10R10_UNORM_PACK32;
else if (format == EDDGIVolumeTextureFormat::F16x4) return VK_FORMAT_R16G16B16A16_SFLOAT;
else if (format == EDDGIVolumeTextureFormat::F32x4) return VK_FORMAT_R32G32B32A32_SFLOAT;
}
else if (type == EDDGIVolumeTextureType::Distance)
{
if (format == EDDGIVolumeTextureFormat::F16x2) return VK_FORMAT_R16G16_SFLOAT; // Note: in large environments FP16 may not be sufficient
else if (format == EDDGIVolumeTextureFormat::F32x2) return VK_FORMAT_R32G32_SFLOAT;
}
else if (type == EDDGIVolumeTextureType::Data)
{
if (format == EDDGIVolumeTextureFormat::F16x4) return VK_FORMAT_R16G16B16A16_SFLOAT;
else if (format == EDDGIVolumeTextureFormat::F32x4) return VK_FORMAT_R32G32B32A32_SFLOAT;
}
else if (type == EDDGIVolumeTextureType::Variability)
{
if (format == EDDGIVolumeTextureFormat::F16) return VK_FORMAT_R16_SFLOAT;
else if (format == EDDGIVolumeTextureFormat::F32) return VK_FORMAT_R32_SFLOAT;
}
else if (type == EDDGIVolumeTextureType::VariabilityAverage)
{
return VK_FORMAT_R32G32_SFLOAT;
}
return VK_FORMAT_UNDEFINED;
}
uint32_t GetDDGIVolumeLayoutBindingCount() { return 7; }
void GetDDGIVolumeLayoutDescs(
VkDescriptorSetLayoutCreateInfo& descriptorSetLayoutCreateInfo,
VkPushConstantRange& pushConstantRange,
VkPipelineLayoutCreateInfo& pipelineLayoutCreateInfo,
VkDescriptorSetLayoutBinding* bindings)
{
// Descriptor set layout bindings
// 1 SRV constants structured buffer (0)
// 1 UAV for ray data texture array (1)
// 1 UAV probe irradiance texture array (2)
// 1 UAV probe distance texture array (3)
// 1 UAV probe data texture array (4)
// 1 UAV probe variation texture array (5)
// 1 UAV probe variation average array (6)
// 0: Volume Constants Structured Buffer
VkDescriptorSetLayoutBinding& bind0 = bindings[0];
bind0.binding = static_cast<uint32_t>(EDDGIVolumeBindings::Constants);
bind0.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
bind0.descriptorCount = 1;
bind0.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
// 1: Ray Data Texture Array UAV
VkDescriptorSetLayoutBinding& bind1 = bindings[1];
bind1.binding = static_cast<uint32_t>(EDDGIVolumeBindings::RayData);
bind1.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
bind1.descriptorCount = 1;
bind1.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
// 2: Probe Irradiance Texture Array UAV
VkDescriptorSetLayoutBinding& bind2 = bindings[2];
bind2.binding = static_cast<uint32_t>(EDDGIVolumeBindings::ProbeIrradiance);
bind2.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
bind2.descriptorCount = 1;
bind2.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
// 3: Probe Distance Texture Array UAV
VkDescriptorSetLayoutBinding& bind3 = bindings[3];
bind3.binding = static_cast<uint32_t>(EDDGIVolumeBindings::ProbeDistance);
bind3.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
bind3.descriptorCount = 1;
bind3.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
// 4: Probe Data Texture Array UAV
VkDescriptorSetLayoutBinding& bind4 = bindings[4];
bind4.binding = static_cast<uint32_t>(EDDGIVolumeBindings::ProbeData);
bind4.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
bind4.descriptorCount = 1;
bind4.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
// 5: Probe Variability
VkDescriptorSetLayoutBinding& bind5 = bindings[5];
bind5.binding = static_cast<uint32_t>(EDDGIVolumeBindings::ProbeVariability);
bind5.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
bind5.descriptorCount = 1;
bind5.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
// 6: Probe Variability
VkDescriptorSetLayoutBinding& bind6 = bindings[6];
bind6.binding = static_cast<uint32_t>(EDDGIVolumeBindings::ProbeVariabilityAverage);
bind6.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
bind6.descriptorCount = 1;
bind6.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
// Describe the descriptor set layout
descriptorSetLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
descriptorSetLayoutCreateInfo.bindingCount = GetDDGIVolumeLayoutBindingCount();
descriptorSetLayoutCreateInfo.pBindings = bindings;
// Describe the push constants
pushConstantRange.stageFlags = VK_SHADER_STAGE_ALL;
pushConstantRange.offset = 0;
pushConstantRange.size = DDGIRootConstants::GetAlignedSizeInBytes();
// Describe the pipeline layout
pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutCreateInfo.setLayoutCount = 1;
pipelineLayoutCreateInfo.pushConstantRangeCount = 1;
pipelineLayoutCreateInfo.pPushConstantRanges = &pushConstantRange;
}
ERTXGIStatus UploadDDGIVolumeResourceIndices(VkDevice device, VkCommandBuffer cmdBuffer, uint32_t bufferingIndex, uint32_t numVolumes, DDGIVolume** volumes)
{
// Copy the resource indices for each volume
for (uint32_t volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
// Get the volume
const DDGIVolume* volume = volumes[volumeIndex];
// Validate the upload and device buffers
if (volume->GetResourceIndicesBuffer() == nullptr) return ERTXGIStatus::ERROR_DDGI_INVALID_RESOURCE_INDICES_BUFFER;
if (volume->GetResourceIndicesBufferUpload() == nullptr) return ERTXGIStatus::ERROR_DDGI_INVALID_RESOURCE_INDICES_UPLOAD_BUFFER;
if (volume->GetResourceIndicesBufferUploadMemory() == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_RESOURCE_INDICES_UPLOAD_MEMORY;
// Offset to the resource indices data to write to (e.g. double buffering)
uint64_t bufferOffset = volume->GetResourceIndicesBufferSizeInBytes() * bufferingIndex;
// Offset to the volume in current resource indices buffer
uint32_t volumeOffset = (volume->GetIndex() * (uint32_t)sizeof(DDGIVolumeResourceIndices));
// Offset to the volume resource indices in the upload buffer
uint64_t srcOffset = (bufferOffset + volumeOffset);
// Map the resource indices buffer and update it
void* pData = nullptr;
VkResult result = vkMapMemory(device, volume->GetResourceIndicesBufferUploadMemory(), srcOffset, sizeof(DDGIVolumeResourceIndices), 0, &pData);
if (VKFAILED(result)) return ERTXGIStatus::ERROR_DDGI_MAP_FAILURE_RESOURCE_INDICES_UPLOAD_BUFFER;
// Get the DDGIVolume's bindless resource indices
const DDGIVolumeResourceIndices gpuDesc = volume->GetResourceIndices();
memcpy(pData, &gpuDesc, sizeof(DDGIVolumeResourceIndices));
vkUnmapMemory(device, volume->GetResourceIndicesBufferUploadMemory());
// Schedule a copy of the upload buffer to the device buffer
VkBufferCopy bufferCopy = {};
bufferCopy.size = sizeof(DDGIVolumeResourceIndices);
bufferCopy.srcOffset = srcOffset;
bufferCopy.dstOffset = volumeOffset;
vkCmdCopyBuffer(cmdBuffer, volume->GetResourceIndicesBufferUpload(), volume->GetResourceIndicesBuffer(), 1, &bufferCopy);
}
return ERTXGIStatus::OK;
}
ERTXGIStatus UploadDDGIVolumeConstants(VkDevice device, VkCommandBuffer cmdBuffer, uint32_t bufferingIndex, uint32_t numVolumes, DDGIVolume** volumes)
{
// Copy the constants for each volume
for (uint32_t volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
// Get the volume
const DDGIVolume* volume = volumes[volumeIndex];
// Validate the upload and device buffers
if (volume->GetConstantsBuffer() == nullptr) return ERTXGIStatus::ERROR_DDGI_INVALID_CONSTANTS_BUFFER;
if (volume->GetConstantsBufferUpload() == nullptr) return ERTXGIStatus::ERROR_DDGI_INVALID_CONSTANTS_UPLOAD_BUFFER;
if (volume->GetConstantsBufferUploadMemory() == nullptr) return ERTXGIStatus::ERROR_DDGI_VK_INVALID_CONSTANTS_UPLOAD_MEMORY;
// Offset to the constants data to write to (e.g. double buffering)
uint64_t bufferOffset = volume->GetConstantsBufferSizeInBytes() * bufferingIndex;
// Offset to the volume in current constants buffer
uint32_t volumeOffset = (volume->GetIndex() * (uint32_t)sizeof(DDGIVolumeDescGPUPacked));
// Offset to the volume constants in the upload buffer
uint64_t srcOffset = (bufferOffset + volumeOffset);
// Map the constants buffer and update it
void* pData = nullptr;
VkResult result = vkMapMemory(device, volume->GetConstantsBufferUploadMemory(), srcOffset, sizeof(DDGIVolumeDescGPUPacked), 0, &pData);
if (VKFAILED(result)) return ERTXGIStatus::ERROR_DDGI_MAP_FAILURE_CONSTANTS_UPLOAD_BUFFER;
// Get the packed DDGIVolume GPU descriptor
const DDGIVolumeDescGPUPacked gpuDesc = volume->GetDescGPUPacked();
#if _DEBUG
volume->ValidatePackedData(gpuDesc);
#endif
memcpy(pData, &gpuDesc, sizeof(DDGIVolumeDescGPUPacked));
vkUnmapMemory(device, volume->GetConstantsBufferUploadMemory());
// Schedule a copy of the upload buffer to the device buffer
VkBufferCopy bufferCopy = {};
bufferCopy.size = sizeof(DDGIVolumeDescGPUPacked);
bufferCopy.srcOffset = srcOffset;
bufferCopy.dstOffset = volumeOffset;
vkCmdCopyBuffer(cmdBuffer, volume->GetConstantsBufferUpload(), volume->GetConstantsBuffer(), 1, &bufferCopy);
}
return ERTXGIStatus::OK;
}
ERTXGIStatus UpdateDDGIVolumeProbes(VkCommandBuffer cmdBuffer, uint32_t numVolumes, DDGIVolume** volumes)
{
if (bInsertPerfMarkers) AddPerfMarker(cmdBuffer, RTXGI_PERF_MARKER_GREEN, "RTXGI DDGI Update Probes");
uint32_t volumeIndex;
std::vector<VkImageMemoryBarrier> barriers;
VkImageMemoryBarrier barrier = {};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
barrier.oldLayout = barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
barrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
// Irradiance Blending
if (bInsertPerfMarkers) AddPerfMarker(cmdBuffer, RTXGI_PERF_MARKER_GREEN, "Probe Irradiance");
for (volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
// Get the volume
const DDGIVolume* volume = volumes[volumeIndex];
// Bind the descriptor set and push constants
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetPipelineLayout(), 0, 1, volume->GetDescriptorSetConstPtr(), 0, nullptr);
// Update the push constants
vkCmdPushConstants(cmdBuffer, volume->GetPipelineLayout(), VK_SHADER_STAGE_ALL, volume->GetPushConstantsOffset(), DDGIRootConstants::GetSizeInBytes(), volume->GetPushConstants().GetData());
// Get the number of probes on each axis
uint32_t probeCountX, probeCountY, probeCountZ;
GetDDGIVolumeProbeCounts(volume->GetDesc(), probeCountX, probeCountY, probeCountZ);
// Probe irradiance blending
{
if (bInsertPerfMarkers && volume->GetInsertPerfMarkers())
{
std::string msg = "Irradiance, DDGIVolume[" + std::to_string(volume->GetIndex()) + "] - \"" + volume->GetName() + "\"";
AddPerfMarker(cmdBuffer, RTXGI_PERF_MARKER_GREEN, msg.c_str());
}
// Bind the pipeline and dispatch threads
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetProbeBlendingIrradiancePipeline());
vkCmdDispatch(cmdBuffer, probeCountX, probeCountY, probeCountZ);
if (bInsertPerfMarkers && volume->GetInsertPerfMarkers()) vkCmdEndDebugUtilsLabelEXT(cmdBuffer);
}
// Add a barrier
barrier.image = volume->GetProbeIrradiance();
barriers.push_back(barrier);
barrier.image = volume->GetProbeVariability();
barriers.push_back(barrier);
}
if (bInsertPerfMarkers) vkCmdEndDebugUtilsLabelEXT(cmdBuffer);
// Distance Blending
if (bInsertPerfMarkers) AddPerfMarker(cmdBuffer, RTXGI_PERF_MARKER_GREEN, "Probe Distance");
for (volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
// Get the volume
const DDGIVolume* volume = volumes[volumeIndex];
// Bind the descriptor set and push constants
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetPipelineLayout(), 0, 1, volume->GetDescriptorSetConstPtr(), 0, nullptr);
// Update the push constants
vkCmdPushConstants(cmdBuffer, volume->GetPipelineLayout(), VK_SHADER_STAGE_ALL, volume->GetPushConstantsOffset(), DDGIRootConstants::GetSizeInBytes(), volume->GetPushConstants().GetData());
// Get the number of probes on the X and Y dimensions of the texture
uint32_t probeCountX, probeCountY, probeCountZ;
GetDDGIVolumeProbeCounts(volume->GetDesc(), probeCountX, probeCountY, probeCountZ);
// Probe distance blending
{
if (bInsertPerfMarkers && volume->GetInsertPerfMarkers())
{
std::string msg = "Distance, DDGIVolume[" + std::to_string(volume->GetIndex()) + "] - \"" + volume->GetName() + "\"";
AddPerfMarker(cmdBuffer, RTXGI_PERF_MARKER_GREEN, msg.c_str());
}
// Bind the pipeline and dispatch threads
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetProbeBlendingDistancePipeline());
vkCmdDispatch(cmdBuffer, probeCountX, probeCountY, probeCountZ);
if (bInsertPerfMarkers && volume->GetInsertPerfMarkers()) vkCmdEndDebugUtilsLabelEXT(cmdBuffer);
}
// Add a barrier
barrier.image = volume->GetProbeDistance();
barriers.push_back(barrier);
}
if (bInsertPerfMarkers) vkCmdEndDebugUtilsLabelEXT(cmdBuffer);
// Irradiance pass must finish generating variability before possible reduction pass
// Also ensures that irradiance and distance complete before border update after reduction
if (!barriers.empty())
{
vkCmdPipelineBarrier(
cmdBuffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0,
0, nullptr,
0, nullptr,
static_cast<uint32_t>(barriers.size()), barriers.data());
}
// Remove previous barriers
barriers.clear();
if (bInsertPerfMarkers) vkCmdEndDebugUtilsLabelEXT(cmdBuffer);
return ERTXGIStatus::OK;
}
ERTXGIStatus RelocateDDGIVolumeProbes(VkCommandBuffer cmdBuffer, uint32_t numVolumes, DDGIVolume** volumes)
{
if (bInsertPerfMarkers) AddPerfMarker(cmdBuffer, RTXGI_PERF_MARKER_GREEN, "RTXGI DDGI Relocate Probes");
uint32_t volumeIndex;
std::vector<VkImageMemoryBarrier> barriers;
VkImageMemoryBarrier barrier = {};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
barrier.oldLayout = barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
barrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
// Probe Relocation Reset
for (volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
// Get the volume
const DDGIVolume* volume = volumes[volumeIndex];
if (!volume->GetProbeRelocationNeedsReset()) continue; // Skip if the volume doesn't need to be reset
// Bind descriptor set and push constants
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetPipelineLayout(), 0, 1, volume->GetDescriptorSetConstPtr(), 0, nullptr);
// Update the push constants
vkCmdPushConstants(cmdBuffer, volume->GetPipelineLayout(), VK_SHADER_STAGE_ALL, volume->GetPushConstantsOffset(), DDGIRootConstants::GetSizeInBytes(), volume->GetPushConstants().GetData());
// Reset all probe offsets to zero
const float groupSizeX = 32.f;
uint32_t numGroupsX = (uint32_t)ceil((float)volume->GetNumProbes() / groupSizeX);
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetProbeRelocationResetPipeline());
vkCmdDispatch(cmdBuffer, numGroupsX, 1, 1);
// Update the reset flag
volumes[volumeIndex]->SetProbeRelocationNeedsReset(false);
// Add a barrier
barrier.image = volume->GetProbeData();
barriers.push_back(barrier);
}
// Probe Relocation Reset Barrier(s)
if(!barriers.empty())
{
// Wait for the compute pass to complete
vkCmdPipelineBarrier(
cmdBuffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0,
0, nullptr,
0, nullptr,
static_cast<uint32_t>(barriers.size()), barriers.data());
}
barriers.clear();
// Probe Relocation
for (volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
// Get the volume
const DDGIVolume* volume = volumes[volumeIndex];
if (!volume->GetProbeRelocationEnabled()) continue; // Skip if relocation is not enabled for this volume
// Bind descriptor set and push constants
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetPipelineLayout(), 0, 1, volume->GetDescriptorSetConstPtr(), 0, nullptr);
// Update the push constants
vkCmdPushConstants(cmdBuffer, volume->GetPipelineLayout(), VK_SHADER_STAGE_ALL, volume->GetPushConstantsOffset(), DDGIRootConstants::GetSizeInBytes(), volume->GetPushConstants().GetData());
// Probe relocation
float groupSizeX = 32.f;
uint32_t numGroupsX = (uint32_t)ceil((float)volume->GetNumProbes() / groupSizeX);
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetProbeRelocationPipeline());
vkCmdDispatch(cmdBuffer, numGroupsX, 1, 1);
// Add a barrier
barrier.image = volume->GetProbeData();
barriers.push_back(barrier);
}
// Probe Relocation Barrier(s)
if (!barriers.empty())
{
// Wait for the compute pass to complete
vkCmdPipelineBarrier(
cmdBuffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0,
0, nullptr,
0, nullptr,
static_cast<uint32_t>(barriers.size()), barriers.data());
}
if (bInsertPerfMarkers) vkCmdEndDebugUtilsLabelEXT(cmdBuffer);
return ERTXGIStatus::OK;
}
ERTXGIStatus ClassifyDDGIVolumeProbes(VkCommandBuffer cmdBuffer, uint32_t numVolumes, DDGIVolume** volumes)
{
if (bInsertPerfMarkers) AddPerfMarker(cmdBuffer, RTXGI_PERF_MARKER_GREEN, "RTXGI DDGI Classify Probes");
uint32_t volumeIndex;
std::vector<VkImageMemoryBarrier> barriers;
VkImageMemoryBarrier barrier = {};
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
barrier.oldLayout = barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
barrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
// Probe Classification Reset
for (volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
// Get the volume
const DDGIVolume* volume = volumes[volumeIndex];
if (!volume->GetProbeClassificationNeedsReset()) continue; // Skip if the volume doesn't need to be reset
// Bind descriptor set and push constants
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetPipelineLayout(), 0, 1, volume->GetDescriptorSetConstPtr(), 0, nullptr);
// Update the push constants
vkCmdPushConstants(cmdBuffer, volume->GetPipelineLayout(), VK_SHADER_STAGE_ALL, volume->GetPushConstantsOffset(), DDGIRootConstants::GetSizeInBytes(), volume->GetPushConstants().GetData());
// Reset all probe states to the ACTIVE state
const float groupSizeX = 32.f;
uint32_t numGroupsX = (uint32_t)ceil((float)volume->GetNumProbes() / groupSizeX);
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetProbeClassificationResetPipeline());
vkCmdDispatch(cmdBuffer, numGroupsX, 1, 1);
// Update the reset flag
volumes[volumeIndex]->SetProbeClassificationNeedsReset(false);
// Add a barrier
barrier.image = volume->GetProbeData();
barriers.push_back(barrier);
}
// Probe Classification Reset Barrier(s)
if (!barriers.empty())
{
// Wait for the compute pass to complete
vkCmdPipelineBarrier(
cmdBuffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0,
0, nullptr,
0, nullptr,
static_cast<uint32_t>(barriers.size()), barriers.data());
}
barriers.clear();
// Probe Classification
for (volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
// Get the volume
const DDGIVolume* volume = volumes[volumeIndex];
if (!volume->GetProbeClassificationEnabled()) continue; // Skip if classification is not enabled for this volume
// Bind descriptor set and push constants
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetPipelineLayout(), 0, 1, volume->GetDescriptorSetConstPtr(), 0, nullptr);
// Update the push constants
vkCmdPushConstants(cmdBuffer, volume->GetPipelineLayout(), VK_SHADER_STAGE_ALL, volume->GetPushConstantsOffset(), DDGIRootConstants::GetSizeInBytes(), volume->GetPushConstants().GetData());
// Probe classification
const float groupSizeX = 32.f;
uint32_t numGroupsX = (uint32_t)ceil((float)volume->GetNumProbes() / groupSizeX);
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetProbeClassificationPipeline());
vkCmdDispatch(cmdBuffer, numGroupsX, 1, 1);
// Add a barrier
barrier.image = volume->GetProbeData();
barriers.push_back(barrier);
}
// Probe Classification Barrier(s)
if (!barriers.empty())
{
// Wait for the compute pass to complete
vkCmdPipelineBarrier(
cmdBuffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0,
0, nullptr,
0, nullptr,
static_cast<uint32_t>(barriers.size()), barriers.data());
}
if (bInsertPerfMarkers) vkCmdEndDebugUtilsLabelEXT(cmdBuffer);
return ERTXGIStatus::OK;
}
ERTXGIStatus CalculateDDGIVolumeVariability(VkCommandBuffer cmdBuffer, uint32_t numVolumes, DDGIVolume** volumes)
{
if (bInsertPerfMarkers) AddPerfMarker(cmdBuffer, RTXGI_PERF_MARKER_GREEN, "Probe Variability Calculation");
uint32_t volumeIndex;
std::vector<VkImageMemoryBarrier> barriers;
// Reduction
for (volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
const DDGIVolume* volume = volumes[volumeIndex];
if (!volume->GetProbeVariabilityEnabled()) continue; // Skip if the volume is not calculating variability
// Bind the descriptor set and push constants
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetPipelineLayout(), 0, 1, volume->GetDescriptorSetConstPtr(), 0, nullptr);
// Get the number of probes on the XYZ dimensions of the texture
uint32_t probeCountX, probeCountY, probeCountZ;
GetDDGIVolumeProbeCounts(volume->GetDesc(), probeCountX, probeCountY, probeCountZ);
// Initially, the reduction input is the full variability size (same as irradiance texture)
uint32_t inputTexelsX = probeCountX * volume->GetDesc().probeNumIrradianceInteriorTexels;
uint32_t inputTexelsY = probeCountY * volume->GetDesc().probeNumIrradianceInteriorTexels;
uint32_t inputTexelsZ = probeCountZ;
const uint3 NumThreadsInGroup = { 4, 8, 4 }; // Each thread group will have 8x8x8 threads
constexpr uint2 ThreadSampleFootprint = { 4, 2 }; // Each thread will sample 4x2 texels
// Set push constants
DDGIRootConstants consts = volume->GetPushConstants();
// First pass reduction takes probe irradiance data and calculates variability, reduces as much as possible
{
if (bInsertPerfMarkers && volume->GetInsertPerfMarkers())
{
std::string msg = "Reduction, DDGIVolume[" + std::to_string(volume->GetIndex()) + "] - \"" + volume->GetName() + "\"";
AddPerfMarker(cmdBuffer, RTXGI_PERF_MARKER_GREEN, msg.c_str());
}
// Set the PSO and dispatch threads
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetProbeVariabilityReductionPipeline());
// One thread group per output texel
uint32_t outputTexelsX = (uint32_t)ceil((float)inputTexelsX / (float)(NumThreadsInGroup.x * ThreadSampleFootprint.x));
uint32_t outputTexelsY = (uint32_t)ceil((float)inputTexelsY / (float)(NumThreadsInGroup.y * ThreadSampleFootprint.y));
uint32_t outputTexelsZ = (uint32_t)ceil((float)inputTexelsZ / (float)NumThreadsInGroup.z);
consts.reductionInputSizeX = inputTexelsX;
consts.reductionInputSizeY = inputTexelsY;
consts.reductionInputSizeZ = inputTexelsZ;
vkCmdPushConstants(cmdBuffer, volume->GetPipelineLayout(), VK_SHADER_STAGE_ALL, volume->GetPushConstantsOffset(), DDGIRootConstants::GetSizeInBytes(), consts.GetData());
vkCmdDispatch(cmdBuffer, outputTexelsX, outputTexelsY, outputTexelsZ);
if (bInsertPerfMarkers && volume->GetInsertPerfMarkers()) vkCmdEndDebugUtilsLabelEXT(cmdBuffer);
// Each thread group will write out a value to the averaging texture
// If there is more than one thread group, we will need to do extra averaging passes
inputTexelsX = outputTexelsX;
inputTexelsY = outputTexelsY;
inputTexelsZ = outputTexelsZ;
}
// UAV barrier needed after each reduction pass
VkImageMemoryBarrier reductionBarrier = {};
reductionBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
reductionBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
reductionBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
reductionBarrier.oldLayout = reductionBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
reductionBarrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
reductionBarrier.image = volume->GetProbeVariabilityAverage();
vkCmdPipelineBarrier(
cmdBuffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0,
0, nullptr,
0, nullptr,
1, &reductionBarrier);
// Future extra passes (if they run) will re-use the reductionBarrier struct, so update srcAcessMask to match
reductionBarrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
// Extra reduction passes average values in variability texture down to single value
while (inputTexelsX > 1 || inputTexelsY > 1 || inputTexelsZ > 1)
{
if (bInsertPerfMarkers && volume->GetInsertPerfMarkers())
{
std::string msg = "Extra Reduction, DDGIVolume[" + std::to_string(volume->GetIndex()) + "] - \"" + volume->GetName() + "\"";
AddPerfMarker(cmdBuffer, RTXGI_PERF_MARKER_GREEN, msg.c_str());
}
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, volume->GetProbeVariabilityExtraReductionPipeline());
// One thread group per output texel
uint32_t outputTexelsX = (uint32_t)ceil((float)inputTexelsX / (float)(NumThreadsInGroup.x * ThreadSampleFootprint.x));
uint32_t outputTexelsY = (uint32_t)ceil((float)inputTexelsY / (float)(NumThreadsInGroup.y * ThreadSampleFootprint.y));
uint32_t outputTexelsZ = (uint32_t)ceil((float)inputTexelsZ / (float)NumThreadsInGroup.z);
consts.reductionInputSizeX = inputTexelsX;
consts.reductionInputSizeY = inputTexelsY;
consts.reductionInputSizeZ = inputTexelsZ;
vkCmdPushConstants(cmdBuffer, volume->GetPipelineLayout(), VK_SHADER_STAGE_ALL, volume->GetPushConstantsOffset(), DDGIRootConstants::GetSizeInBytes(), consts.GetData());
vkCmdDispatch(cmdBuffer, outputTexelsX, outputTexelsY, outputTexelsZ);
if (bInsertPerfMarkers && volume->GetInsertPerfMarkers()) vkCmdEndDebugUtilsLabelEXT(cmdBuffer);
inputTexelsX = outputTexelsX;
inputTexelsY = outputTexelsY;
inputTexelsZ = outputTexelsZ;
// Need a barrier in between each reduction pass
vkCmdPipelineBarrier(
cmdBuffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0,
0, nullptr,
0, nullptr,
1, &reductionBarrier);
}
}
if (bInsertPerfMarkers) vkCmdEndDebugUtilsLabelEXT(cmdBuffer);
// Copy readback buffer
if (bInsertPerfMarkers) AddPerfMarker(cmdBuffer, RTXGI_PERF_MARKER_GREEN, "Probe Variability Readback");
{
VkImageMemoryBarrier beforeBarrier = {};
beforeBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
beforeBarrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
beforeBarrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
beforeBarrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
beforeBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
beforeBarrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
VkImageMemoryBarrier afterBarrier = beforeBarrier;
afterBarrier.srcAccessMask = beforeBarrier.dstAccessMask;
afterBarrier.dstAccessMask = beforeBarrier.srcAccessMask;
afterBarrier.oldLayout = beforeBarrier.newLayout;
afterBarrier.newLayout = beforeBarrier.oldLayout;
for (volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
const DDGIVolume* volume = volumes[volumeIndex];
if (!volume->GetProbeVariabilityEnabled()) continue; // Skip if the volume is not calculating variability
beforeBarrier.image = volume->GetProbeVariabilityAverage();
barriers.push_back(beforeBarrier);
}
if (!barriers.empty())
{
vkCmdPipelineBarrier(
cmdBuffer,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
0,
0, nullptr,
0, nullptr,
static_cast<uint32_t>(barriers.size()), barriers.data());
barriers.clear();
}
for (volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
const DDGIVolume* volume = volumes[volumeIndex];
if (!volume->GetProbeVariabilityEnabled()) continue; // Skip if the volume is not calculating variability
VkBufferImageCopy copy = {};
copy.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 };
copy.imageExtent = { 1, 1, 1 };
vkCmdCopyImageToBuffer(cmdBuffer,
volume->GetProbeVariabilityAverage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
volume->GetProbeVariabilityReadback(),
1, ©);
afterBarrier.image = volume->GetProbeVariabilityAverage();
barriers.push_back(afterBarrier);
}
if (!barriers.empty())
{
vkCmdPipelineBarrier(
cmdBuffer,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
0,
0, nullptr,
0, nullptr,
static_cast<uint32_t>(barriers.size()), barriers.data());
barriers.clear();
}
}
if (bInsertPerfMarkers) vkCmdEndDebugUtilsLabelEXT(cmdBuffer);
return ERTXGIStatus::OK;
}
ERTXGIStatus ReadbackDDGIVolumeVariability(VkDevice device, uint32_t numVolumes, DDGIVolume** volumes)
{
for (uint32_t volumeIndex = 0; volumeIndex < numVolumes; volumeIndex++)
{
// Get the volume
DDGIVolume* volume = volumes[volumeIndex];
if (!volume->GetProbeVariabilityEnabled()) continue; // Skip if the volume is not calculating variability
// Get the probe variability readback buffer
VkDeviceMemory readback = volume->GetProbeVariabilityReadbackMemory();
// Read the first 32-bits of the readback buffer
float* pMappedMemory = nullptr;
VkResult result = vkMapMemory(device, readback, 0, sizeof(float), 0, (void**)&pMappedMemory);
if (VKFAILED(result)) return ERTXGIStatus::ERROR_DDGI_MAP_FAILURE_VARIABILITY_READBACK_BUFFER;
float value = pMappedMemory[0];
vkUnmapMemory(device, readback);
volume->SetVolumeAverageVariability(value);
}
return ERTXGIStatus::OK;
}
//------------------------------------------------------------------------
// Private DDGIVolume Functions
//------------------------------------------------------------------------
#if RTXGI_DDGI_RESOURCE_MANAGEMENT
void DDGIVolume::ReleaseManagedResources()
{
// Release the pipeline layout and descriptor set layout
vkDestroyDescriptorSetLayout(m_device, m_descriptorSetLayout, nullptr);
vkDestroyPipelineLayout(m_device, m_pipelineLayout, nullptr);
// Release the existing shader modules
vkDestroyShaderModule(m_device, m_probeBlendingIrradianceModule, nullptr);
vkDestroyShaderModule(m_device, m_probeBlendingDistanceModule, nullptr);
vkDestroyShaderModule(m_device, m_probeRelocationModule, nullptr);
vkDestroyShaderModule(m_device, m_probeRelocationResetModule, nullptr);
vkDestroyShaderModule(m_device, m_probeClassificationModule, nullptr);
vkDestroyShaderModule(m_device, m_probeClassificationResetModule, nullptr);
vkDestroyShaderModule(m_device, m_probeVariabilityReductionModule, nullptr);
vkDestroyShaderModule(m_device, m_probeVariabilityExtraReductionModule, nullptr);
// Release the existing compute pipelines
vkDestroyPipeline(m_device, m_probeBlendingIrradiancePipeline, nullptr);
vkDestroyPipeline(m_device, m_probeBlendingDistancePipeline, nullptr);
vkDestroyPipeline(m_device, m_probeRelocationPipeline, nullptr);
vkDestroyPipeline(m_device, m_probeRelocationResetPipeline, nullptr);
vkDestroyPipeline(m_device, m_probeClassificationPipeline, nullptr);
vkDestroyPipeline(m_device, m_probeClassificationResetPipeline, nullptr);
vkDestroyPipeline(m_device, m_probeVariabilityReductionPipeline, nullptr);
vkDestroyPipeline(m_device, m_probeVariabilityExtraReductionPipeline, nullptr);
}
ERTXGIStatus DDGIVolume::CreateManagedResources(const DDGIVolumeDesc& desc, const DDGIVolumeManagedResourcesDesc& managed)
{
bool deviceChanged = IsDeviceChanged(managed);
// Create the descriptor set layout, pipeline layout, and pipelines
if (deviceChanged)
{
// The device may have changed, release resources on that device
if (m_device != nullptr) ReleaseManagedResources();
// Store the handle to the new device and descriptor pool
m_device = managed.device;
m_physicalDevice = managed.physicalDevice;
m_descriptorPool = managed.descriptorPool;
// Create the descriptor set layout and pipeline layout
if(!CreateLayouts()) return ERTXGIStatus::ERROR_DDGI_VK_CREATE_FAILURE_LAYOUTS;
// Create the compute pipelines
if (!CreateComputePipeline(
managed.probeBlendingIrradianceCS,
"DDGIProbeBlendingCS",
&m_probeBlendingIrradianceModule,
&m_probeBlendingIrradiancePipeline,
"Probe Irradiance Blending")) return ERTXGIStatus::ERROR_DDGI_VK_CREATE_FAILURE_PIPELINE;
if (!CreateComputePipeline(
managed.probeBlendingDistanceCS,
"DDGIProbeBlendingCS",
&m_probeBlendingDistanceModule,
&m_probeBlendingDistancePipeline,
"Probe Distance Blending")) return ERTXGIStatus::ERROR_DDGI_VK_CREATE_FAILURE_PIPELINE;
if (!CreateComputePipeline(
managed.probeRelocation.updateCS,
"DDGIProbeRelocationCS",
&m_probeRelocationModule,
&m_probeRelocationPipeline,
"Probe Relocation")) return ERTXGIStatus::ERROR_DDGI_VK_CREATE_FAILURE_PIPELINE;
if (!CreateComputePipeline(
managed.probeRelocation.resetCS,
"DDGIProbeRelocationResetCS",
&m_probeRelocationResetModule,
&m_probeRelocationResetPipeline,
"Probe Relocation Reset")) return ERTXGIStatus::ERROR_DDGI_VK_CREATE_FAILURE_PIPELINE;
if (!CreateComputePipeline(
managed.probeClassification.updateCS,
"DDGIProbeClassificationCS",
&m_probeClassificationModule,
&m_probeClassificationPipeline,
"Probe Classification")) return ERTXGIStatus::ERROR_DDGI_VK_CREATE_FAILURE_PIPELINE;
if (!CreateComputePipeline(
managed.probeClassification.resetCS,
"DDGIProbeClassificationResetCS",
&m_probeClassificationResetModule,
&m_probeClassificationResetPipeline,
"Probe Classification Reset")) return ERTXGIStatus::ERROR_DDGI_VK_CREATE_FAILURE_PIPELINE;
if (!CreateComputePipeline(
managed.probeVariability.reductionCS,
"DDGIReductionCS",
&m_probeVariabilityReductionModule,
&m_probeVariabilityReductionPipeline,
"Probe Variability Reduction")) return ERTXGIStatus::ERROR_DDGI_VK_CREATE_FAILURE_PIPELINE;
if (!CreateComputePipeline(
managed.probeVariability.extraReductionCS,
"DDGIExtraReductionCS",
&m_probeVariabilityExtraReductionModule,
&m_probeVariabilityExtraReductionPipeline,
"Probe Variability Extra Reduction")) return ERTXGIStatus::ERROR_DDGI_VK_CREATE_FAILURE_PIPELINE;
}