-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathBindlessDeferred.cpp
More file actions
2424 lines (1964 loc) · 108 KB
/
Copy pathBindlessDeferred.cpp
File metadata and controls
2424 lines (1964 loc) · 108 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
//=================================================================================================
//
// Bindless Deferred Texturing Sample
// by MJP
// http://mynameismjp.wordpress.com/
//
// All code and content licensed under the MIT license
//
//=================================================================================================
#include <PCH.h>
#include <InterfacePointers.h>
#include <Window.h>
#include <Input.h>
#include <Utility.h>
#include <Graphics/SwapChain.h>
#include <Graphics/ShaderCompilation.h>
#include <Graphics/Profiler.h>
#include <Graphics/Textures.h>
#include <Graphics/Sampling.h>
#include <Graphics/DX12.h>
#include <Graphics/DX12_Helpers.h>
#include <EnkiTS/TaskScheduler_c.h>
#include "BindlessDeferred.h"
#include "SharedTypes.h"
using namespace SampleFramework12;
using std::wstring;
// Model filenames
static const wchar* ScenePaths[] =
{
L"..\\Content\\Models\\Sponza\\Sponza.fbx",
};
static const float SceneScales[] = { 0.01f };
static const Float3 SceneCameraPositions[] = { Float3(-11.5f, 1.85f, -0.45f) };
static const Float2 SceneCameraRotations[] = { Float2(0.0f, 1.544f) };
StaticAssert_(ArraySize_(ScenePaths) == uint64(Scenes::NumValues));
StaticAssert_(ArraySize_(SceneScales) == uint64(Scenes::NumValues));
StaticAssert_(ArraySize_(SceneCameraPositions) == uint64(Scenes::NumValues));
StaticAssert_(ArraySize_(SceneCameraRotations) == uint64(Scenes::NumValues));
static const uint64 NumConeSides = 16;
static const float SpotLightIntensityFactor = 25.0f;
static enkiTaskScheduler* taskScheduler = nullptr;
static enkiTaskSet* taskSet = nullptr;
static const bool EnableMultithreadedCompilation = true;
struct PickingData
{
Float3 Position;
Float3 Normal;
};
struct LightConstants
{
SpotLight Lights[AppSettings::MaxSpotLights];
Float4x4 ShadowMatrices[AppSettings::MaxSpotLights];
};
struct ClusterConstants
{
Float4x4 ViewProjection;
Float4x4 InvProjection;
float NearClip = 0.0f;
float FarClip = 0.0f;
float InvClipRange = 0.0f;
uint32 NumXTiles = 0;
uint32 NumYTiles = 0;
uint32 NumXYTiles = 0;
uint32 ElementsPerCluster = 0;
uint32 InstanceOffset = 0;
uint32 NumLights = 0;
uint32 NumDecals = 0;
uint32 BoundsBufferIdx = uint32(-1);
uint32 VertexBufferIdx = uint32(-1);
uint32 InstanceBufferIdx = uint32(-1);
};
struct MSAAMaskConstants
{
uint32 NumXTiles = 0;
uint32 MaterialIDMapIdx = uint32(-1);
uint32 UVMapIdx = uint32(-1);
};
struct DeferredConstants
{
Float4x4 InvViewProj;
Float4x4 Projection;
Float2 RTSize;
uint32 NumComputeTilesX = 0;
};
struct PickingConstants
{
Float4x4 InverseViewProjection;
Uint2 PixelPos;
Float2 RTSize;
uint32 TangentMapIdx = uint32(-1);
uint32 DepthMapIdx = uint32(-1);
};
struct ClusterVisConstants
{
Float4x4 Projection;
Float3 ViewMin;
float NearClip = 0.0f;
Float3 ViewMax;
float InvClipRange = 0.0f;
Float2 DisplaySize;
uint32 NumXTiles = 0;
uint32 NumXYTiles = 0;
uint32 DecalClusterBufferIdx = uint32(-1);
uint32 SpotLightClusterBufferIdx = uint32(-1);
};
enum ClusterRootParams : uint32
{
ClusterParams_StandardDescriptors,
ClusterParams_UAVDescriptors,
ClusterParams_CBuffer,
ClusterParams_AppSettings,
NumClusterRootParams,
};
enum MSAAMaskRootParams : uint32
{
MSAAMaskParams_StandardDescriptors,
MSAAMaskParams_UAVDescriptors,
MSAAMaskParams_CBuffer,
MSAAMaskParams_AppSettings,
NumMSAAMaskRootParams
};
enum DeferredRootParams : uint32
{
DeferredParams_StandardDescriptors,
DeferredParams_PSCBuffer,
DeferredParams_ShadowCBuffer,
DeferredParams_DeferredCBuffer,
DeferredParams_LightCBuffer,
DeferredParams_SRVIndices,
DeferredParams_UAVDescriptors,
DeferredParams_AppSettings,
NumDeferredRootParams
};
enum PickingRootParams : uint32
{
PickingParams_StandardDescriptors,
PickingParams_UAVDescriptors,
PickingParams_CBuffer,
NumPickingRootParams
};
enum ClusterVisRootParams : uint32
{
ClusterVisParams_StandardDescriptors,
ClusterVisParams_CBuffer,
ClusterVisParams_AppSettings,
NumClusterVisRootParams,
};
enum ResolveRootParams : uint32
{
ResolveParams_StandardDescriptors,
ResolveParams_Constants,
ResolveParams_AppSettings,
NumResolveRootParams
};
// Returns true if a sphere intersects a capped cone defined by a direction, height, and angle
static bool SphereConeIntersection(const Float3& coneTip, const Float3& coneDir, float coneHeight,
float coneAngle, const Float3& sphereCenter, float sphereRadius)
{
if(Float3::Dot(sphereCenter - coneTip, coneDir) > coneHeight + sphereRadius)
return false;
float cosHalfAngle = std::cos(coneAngle * 0.5f);
float sinHalfAngle = std::sin(coneAngle * 0.5f);
Float3 v = sphereCenter - coneTip;
float a = Float3::Dot(v, coneDir);
float b = a * sinHalfAngle / cosHalfAngle;
float c = std::sqrt(Float3::Dot(v, v) - a * a);
float d = c - b;
float e = d * cosHalfAngle;
return e < sphereRadius;
}
BindlessDeferred::BindlessDeferred(const wchar* cmdLine) : App(L"Bindless Deferred Texturing", cmdLine)
{
minFeatureLevel = D3D_FEATURE_LEVEL_11_1;
globalHelpText = "Bindless Deferred Texturing\n\n"
"Controls:\n\n"
"Use W/S/A/D/Q/E to move the camera, and hold right-click while dragging the mouse to rotate.";
}
void BindlessDeferred::BeforeReset()
{
}
void BindlessDeferred::AfterReset()
{
float aspect = float(swapChain.Width()) / swapChain.Height();
camera.SetAspectRatio(aspect);
CreateRenderTargets();
}
void BindlessDeferred::Initialize()
{
// Check if the device supports conservative rasterization
D3D12_FEATURE_DATA_D3D12_OPTIONS features = { };
DX12::Device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &features, sizeof(features));
if(features.ResourceBindingTier < D3D12_RESOURCE_BINDING_TIER_2)
throw Exception("This demo requires a GPU that supports FEATURE_LEVEL_11_1 with D3D12_RESOURCE_BINDING_TIER_2");
if(features.ConservativeRasterizationTier == D3D12_CONSERVATIVE_RASTERIZATION_TIER_NOT_SUPPORTED)
{
AppSettings::ClusterRasterizationMode.SetValue(ClusterRasterizationModes::MSAA8x);
AppSettings::ClusterRasterizationMode.ClampNumValues(uint32(ClusterRasterizationModes::NumValues) - 1);
}
ShadowHelper::Initialize(ShadowMapMode::DepthMap, ShadowMSAAMode::MSAA1x);
// Load the scenes
for(uint64 i = 0; i < uint64(Scenes::NumValues); ++i)
{
ModelLoadSettings settings;
settings.FilePath = ScenePaths[i];
settings.ForceSRGB = true;
settings.SceneScale = SceneScales[i];
settings.MergeMeshes = false;
sceneModels[i].CreateWithAssimp(settings);
}
float aspect = float(swapChain.Width()) / swapChain.Height();
camera.Initialize(aspect, Pi_4, 0.1f, 35.0f);
InitializeScene();
skybox.Initialize();
postProcessor.Initialize();
// Load the decal textures
for(uint64 i = 0; i < AppSettings::NumDecalTypes; ++i)
{
LoadTexture(decalTextures[i * 2 + 0], MakeString(L"..\\Content\\Textures\\Decals\\Decal_%02u_Albedo.tga", uint32(i)).c_str(), true);
LoadTexture(decalTextures[i * 2 + 1], MakeString(L"..\\Content\\Textures\\Decals\\Decal_%02u_Normal.png", uint32(i)).c_str(), false);
}
decals.Init(AppSettings::MaxDecals);
{
// Decal buffer
StructuredBufferInit sbInit;
sbInit.Stride = sizeof(Decal);
sbInit.NumElements = AppSettings::MaxDecals;
sbInit.Dynamic = true;
sbInit.CPUAccessible = false;
decalBuffer.Initialize(sbInit);
decalBuffer.Resource()->SetName(L"Decal Buffer");
}
{
// Decal bounds and instance buffers
StructuredBufferInit sbInit;
sbInit.Stride = sizeof(ClusterBounds);
sbInit.NumElements = AppSettings::MaxDecals;
sbInit.Dynamic = true;
sbInit.CPUAccessible = true;
decalBoundsBuffer.Initialize(sbInit);
sbInit.Stride = sizeof(uint32);
decalInstanceBuffer.Initialize(sbInit);
}
{
// Spot light bounds and instance buffers
StructuredBufferInit sbInit;
sbInit.Stride = sizeof(ClusterBounds);
sbInit.NumElements = AppSettings::MaxSpotLights;
sbInit.Dynamic = true;
sbInit.CPUAccessible = true;
spotLightBoundsBuffer.Initialize(sbInit);
sbInit.Stride = sizeof(uint32);
spotLightInstanceBuffer.Initialize(sbInit);
}
{
// Spot light and shadow bounds buffer
ConstantBufferInit cbInit;
cbInit.Size = sizeof(LightConstants);
cbInit.Dynamic = true;
cbInit.CPUAccessible = false;
cbInit.Name = L"Spot Light Buffer";
spotLightBuffer.Initialize(cbInit);
}
{
// Indirect args buffers for deferred rendering
StructuredBufferInit sbInit;
sbInit.NumElements = 1;
sbInit.Stride = sizeof(D3D12_DISPATCH_ARGUMENTS);
uint32 initData[3] = { 0, 1, 1 };
sbInit.InitData = initData;
nonMsaaArgsBuffer.Initialize(sbInit);
msaaArgsBuffer.Initialize(sbInit);
nonMsaaArgsBuffer.Resource()->SetName(L"Non-MSAA Args Buffer");
msaaArgsBuffer.Resource()->SetName(L"MSAA Args Buffer");
}
{
CompileOptions opts;
opts.Add("FrontFace_", 1);
opts.Add("BackFace_", 0);
opts.Add("Intersecting_", 0);
// Clustering shaders
clusterVS = CompileFromFile(L"Clusters.hlsl", "ClusterVS", ShaderType::Vertex, opts);
clusterFrontFacePS = CompileFromFile(L"Clusters.hlsl", "ClusterPS", ShaderType::Pixel, opts);
opts.Reset();
opts.Add("FrontFace_", 0);
opts.Add("BackFace_", 1);
opts.Add("Intersecting_", 0);
clusterBackFacePS = CompileFromFile(L"Clusters.hlsl", "ClusterPS", ShaderType::Pixel, opts);
opts.Reset();
opts.Add("FrontFace_", 0);
opts.Add("BackFace_", 0);
opts.Add("Intersecting_", 1);
clusterIntersectingPS = CompileFromFile(L"Clusters.hlsl", "ClusterPS", ShaderType::Pixel, opts);
}
MakeBoxGeometry(decalClusterVtxBuffer, decalClusterIdxBuffer, 2.0f); // resulting box is [-1, 1]
MakeConeGeometry(NumConeSides, spotLightClusterVtxBuffer, spotLightClusterIdxBuffer, coneVertices);
{
// Picking buffer
StructuredBufferInit sbInit;
sbInit.Stride = sizeof(PickingData);
sbInit.NumElements = 1;
sbInit.CreateUAV = 1;
pickingBuffer.Initialize(sbInit);
}
for(uint64 i = 0; i < DX12::RenderLatency; ++i)
{
pickingReadbackBuffers[i].Initialize(sizeof(PickingData));
pickingReadbackBuffers[i].Resource->SetName(L"Picking Readback Buffer");
}
{
// Compile picking shaders
CompileOptions opts;
opts.Add("MSAA_", 0);
pickingCS[0] = CompileFromFile(L"Picking.hlsl", "PickingCS", ShaderType::Compute, opts);
opts.Reset();
opts.Add("MSAA_", 1);
pickingCS[1] = CompileFromFile(L"Picking.hlsl", "PickingCS", ShaderType::Compute, opts);
}
// Compile MSAA mask generation shaders
for(uint64 msaaMode = 1; msaaMode < NumMSAAModes; ++msaaMode)
{
CompileOptions opts;
opts.Add("MSAASamples_", AppSettings::NumMSAASamples(MSAAModes(msaaMode)));
opts.Add("UseZGradients_", 0);
msaaMaskCS[msaaMode][0] = CompileFromFile(L"MSAAMask.hlsl", "MSAAMaskCS", ShaderType::Compute, opts);
opts.Reset();
opts.Add("MSAASamples_", AppSettings::NumMSAASamples(MSAAModes(msaaMode)));
opts.Add("UseZGradients_", 1);
msaaMaskCS[msaaMode][1] = CompileFromFile(L"MSAAMask.hlsl", "MSAAMaskCS", ShaderType::Compute, opts);
}
// Compile resolve shaders
for(uint64 msaaMode = 1; msaaMode < NumMSAAModes; ++msaaMode)
{
for(uint64 deferred = 0; deferred < 2; ++deferred)
{
CompileOptions opts;
opts.Add("MSAASamples_", AppSettings::NumMSAASamples(MSAAModes(msaaMode)));
opts.Add("Deferred_", uint32(deferred));
resolvePS[msaaMode][deferred] = CompileFromFile(L"Resolve.hlsl", "ResolvePS", ShaderType::Pixel, opts);
}
}
// Compile cluster visualization shaders
clusterVisPS = CompileFromFile(L"ClusterVisualizer.hlsl", "ClusterVisualizerPS", ShaderType::Pixel);
std::wstring fullScreenTriPath = SampleFrameworkDir() + L"Shaders\\FullScreenTriangle.hlsl";
fullScreenTriVS = CompileFromFile(fullScreenTriPath.c_str(), "FullScreenTriangleVS", ShaderType::Vertex);
{
// Clustering root signature
D3D12_DESCRIPTOR_RANGE1 uavRanges[1] = {};
uavRanges[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
uavRanges[0].NumDescriptors = 1;
uavRanges[0].BaseShaderRegister = 0;
uavRanges[0].RegisterSpace = 0;
uavRanges[0].OffsetInDescriptorsFromTableStart = 0;
D3D12_ROOT_PARAMETER1 rootParameters[NumClusterRootParams] = {};
// Standard SRV descriptors
rootParameters[ClusterParams_StandardDescriptors].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rootParameters[ClusterParams_StandardDescriptors].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
rootParameters[ClusterParams_StandardDescriptors].DescriptorTable.pDescriptorRanges = DX12::StandardDescriptorRanges();
rootParameters[ClusterParams_StandardDescriptors].DescriptorTable.NumDescriptorRanges = DX12::NumStandardDescriptorRanges;
// PS UAV descriptors
rootParameters[ClusterParams_UAVDescriptors].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rootParameters[ClusterParams_UAVDescriptors].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
rootParameters[ClusterParams_UAVDescriptors].DescriptorTable.pDescriptorRanges = uavRanges;
rootParameters[ClusterParams_UAVDescriptors].DescriptorTable.NumDescriptorRanges = ArraySize_(uavRanges);
// CBuffer
rootParameters[ClusterParams_CBuffer].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParameters[ClusterParams_CBuffer].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameters[ClusterParams_CBuffer].Descriptor.RegisterSpace = 0;
rootParameters[ClusterParams_CBuffer].Descriptor.ShaderRegister = 0;
rootParameters[ClusterParams_CBuffer].Descriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_DATA_STATIC;
// AppSettings
rootParameters[ClusterParams_AppSettings].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParameters[ClusterParams_AppSettings].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameters[ClusterParams_AppSettings].Descriptor.RegisterSpace = 0;
rootParameters[ClusterParams_AppSettings].Descriptor.ShaderRegister = AppSettings::CBufferRegister;
rootParameters[ClusterParams_AppSettings].Descriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_DATA_STATIC;
D3D12_ROOT_SIGNATURE_DESC1 rootSignatureDesc = {};
rootSignatureDesc.NumParameters = ArraySize_(rootParameters);
rootSignatureDesc.pParameters = rootParameters;
rootSignatureDesc.NumStaticSamplers = 0;
rootSignatureDesc.pStaticSamplers = nullptr;
rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
DX12::CreateRootSignature(&clusterRS, rootSignatureDesc);
}
{
// Picking root signature
D3D12_DESCRIPTOR_RANGE1 descriptorRanges[1] = {};
descriptorRanges[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
descriptorRanges[0].NumDescriptors = 1;
descriptorRanges[0].BaseShaderRegister = 0;
descriptorRanges[0].RegisterSpace = 0;
descriptorRanges[0].OffsetInDescriptorsFromTableStart = 0;
D3D12_ROOT_PARAMETER1 rootParameters[NumPickingRootParams] = {};
rootParameters[PickingParams_StandardDescriptors].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rootParameters[PickingParams_StandardDescriptors].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameters[PickingParams_StandardDescriptors].DescriptorTable.pDescriptorRanges = DX12::StandardDescriptorRanges();
rootParameters[PickingParams_StandardDescriptors].DescriptorTable.NumDescriptorRanges = DX12::NumStandardDescriptorRanges;
rootParameters[PickingParams_UAVDescriptors].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rootParameters[PickingParams_UAVDescriptors].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameters[PickingParams_UAVDescriptors].DescriptorTable.pDescriptorRanges = descriptorRanges;
rootParameters[PickingParams_UAVDescriptors].DescriptorTable.NumDescriptorRanges = ArraySize_(descriptorRanges);
rootParameters[PickingParams_CBuffer].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParameters[PickingParams_CBuffer].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameters[PickingParams_CBuffer].Descriptor.RegisterSpace = 0;
rootParameters[PickingParams_CBuffer].Descriptor.ShaderRegister = 0;
rootParameters[PickingParams_CBuffer].Descriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_DATA_STATIC;
D3D12_ROOT_SIGNATURE_DESC1 rootSignatureDesc = {};
rootSignatureDesc.NumParameters = ArraySize_(rootParameters);
rootSignatureDesc.pParameters = rootParameters;
rootSignatureDesc.NumStaticSamplers = 0;
rootSignatureDesc.pStaticSamplers = nullptr;
rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
DX12::CreateRootSignature(&pickingRS, rootSignatureDesc);
}
{
// MSAA mask root signature
D3D12_DESCRIPTOR_RANGE1 descriptorRanges[1] = {};
descriptorRanges[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_UAV;
descriptorRanges[0].NumDescriptors = 3;
descriptorRanges[0].BaseShaderRegister = 0;
descriptorRanges[0].RegisterSpace = 0;
descriptorRanges[0].OffsetInDescriptorsFromTableStart = 0;
D3D12_ROOT_PARAMETER1 rootParameters[NumMSAAMaskRootParams] = {};
rootParameters[MSAAMaskParams_StandardDescriptors].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rootParameters[MSAAMaskParams_StandardDescriptors].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameters[MSAAMaskParams_StandardDescriptors].DescriptorTable.pDescriptorRanges = DX12::StandardDescriptorRanges();
rootParameters[MSAAMaskParams_StandardDescriptors].DescriptorTable.NumDescriptorRanges = DX12::NumStandardDescriptorRanges;
rootParameters[MSAAMaskParams_UAVDescriptors].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rootParameters[MSAAMaskParams_UAVDescriptors].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameters[MSAAMaskParams_UAVDescriptors].DescriptorTable.pDescriptorRanges = descriptorRanges;
rootParameters[MSAAMaskParams_UAVDescriptors].DescriptorTable.NumDescriptorRanges = ArraySize_(descriptorRanges);
rootParameters[MSAAMaskParams_CBuffer].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParameters[MSAAMaskParams_CBuffer].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameters[MSAAMaskParams_CBuffer].Descriptor.RegisterSpace = 0;
rootParameters[MSAAMaskParams_CBuffer].Descriptor.ShaderRegister = 0;
rootParameters[MSAAMaskParams_CBuffer].Descriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_DATA_STATIC;
rootParameters[MSAAMaskParams_AppSettings].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParameters[MSAAMaskParams_AppSettings].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
rootParameters[MSAAMaskParams_AppSettings].Descriptor.RegisterSpace = 0;
rootParameters[MSAAMaskParams_AppSettings].Descriptor.ShaderRegister = AppSettings::CBufferRegister;
rootParameters[MSAAMaskParams_AppSettings].Descriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_DATA_STATIC;
D3D12_ROOT_SIGNATURE_DESC1 rootSignatureDesc = {};
rootSignatureDesc.NumParameters = ArraySize_(rootParameters);
rootSignatureDesc.pParameters = rootParameters;
rootSignatureDesc.NumStaticSamplers = 0;
rootSignatureDesc.pStaticSamplers = nullptr;
rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
DX12::CreateRootSignature(&msaaMaskRootSignature, rootSignatureDesc);
}
{
// Resolve root signature
D3D12_ROOT_PARAMETER1 rootParameters[NumResolveRootParams] = {};
// Standard SRV descriptors
rootParameters[ResolveParams_StandardDescriptors].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rootParameters[ResolveParams_StandardDescriptors].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
rootParameters[ResolveParams_StandardDescriptors].DescriptorTable.pDescriptorRanges = DX12::StandardDescriptorRanges();
rootParameters[ResolveParams_StandardDescriptors].DescriptorTable.NumDescriptorRanges = DX12::NumStandardDescriptorRanges;
// CBuffer
rootParameters[ResolveParams_Constants].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
rootParameters[ResolveParams_Constants].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
rootParameters[ResolveParams_Constants].Constants.Num32BitValues = 3;
rootParameters[ResolveParams_Constants].Constants.RegisterSpace = 0;
rootParameters[ResolveParams_Constants].Constants.ShaderRegister = 0;
// AppSettings
rootParameters[ResolveParams_AppSettings].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParameters[ResolveParams_AppSettings].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
rootParameters[ResolveParams_AppSettings].Descriptor.RegisterSpace = 0;
rootParameters[ResolveParams_AppSettings].Descriptor.ShaderRegister = AppSettings::CBufferRegister;
rootParameters[ResolveParams_AppSettings].Descriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_DATA_STATIC;
D3D12_ROOT_SIGNATURE_DESC1 rootSignatureDesc = {};
rootSignatureDesc.NumParameters = ArraySize_(rootParameters);
rootSignatureDesc.pParameters = rootParameters;
rootSignatureDesc.NumStaticSamplers = 0;
rootSignatureDesc.pStaticSamplers = nullptr;
rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
DX12::CreateRootSignature(&resolveRootSignature, rootSignatureDesc);
}
{
// Cluster visualization root signature
D3D12_ROOT_PARAMETER1 rootParameters[NumClusterVisRootParams] = {};
// Standard SRV descriptors
rootParameters[ClusterVisParams_StandardDescriptors].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
rootParameters[ClusterVisParams_StandardDescriptors].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
rootParameters[ClusterVisParams_StandardDescriptors].DescriptorTable.pDescriptorRanges = DX12::StandardDescriptorRanges();
rootParameters[ClusterVisParams_StandardDescriptors].DescriptorTable.NumDescriptorRanges = DX12::NumStandardDescriptorRanges;
// CBuffer
rootParameters[ClusterVisParams_CBuffer].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParameters[ClusterVisParams_CBuffer].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
rootParameters[ClusterVisParams_CBuffer].Descriptor.RegisterSpace = 0;
rootParameters[ClusterVisParams_CBuffer].Descriptor.ShaderRegister = 0;
rootParameters[ClusterVisParams_CBuffer].Descriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_DATA_STATIC;
// AppSettings
rootParameters[ClusterVisParams_AppSettings].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParameters[ClusterVisParams_AppSettings].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
rootParameters[ClusterVisParams_AppSettings].Descriptor.RegisterSpace = 0;
rootParameters[ClusterVisParams_AppSettings].Descriptor.ShaderRegister = AppSettings::CBufferRegister;
rootParameters[ClusterVisParams_AppSettings].Descriptor.Flags = D3D12_ROOT_DESCRIPTOR_FLAG_DATA_STATIC;
D3D12_ROOT_SIGNATURE_DESC1 rootSignatureDesc = {};
rootSignatureDesc.NumParameters = ArraySize_(rootParameters);
rootSignatureDesc.pParameters = rootParameters;
rootSignatureDesc.NumStaticSamplers = 0;
rootSignatureDesc.pStaticSamplers = nullptr;
rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_NONE;
DX12::CreateRootSignature(&clusterVisRootSignature, rootSignatureDesc);
}
{
// Command signature for MSAA deferred indirect dispatch
D3D12_INDIRECT_ARGUMENT_DESC argsDescs[1] = { };
argsDescs[0].Type = D3D12_INDIRECT_ARGUMENT_TYPE_DISPATCH;
D3D12_COMMAND_SIGNATURE_DESC cmdSignatureDesc = { };
cmdSignatureDesc.ByteStride = sizeof(D3D12_DISPATCH_ARGUMENTS);
cmdSignatureDesc.NodeMask = 0;
cmdSignatureDesc.NumArgumentDescs = ArraySize_(argsDescs);
cmdSignatureDesc.pArgumentDescs = argsDescs;
DXCall(DX12::Device->CreateCommandSignature(&cmdSignatureDesc, nullptr, IID_PPV_ARGS(&deferredCmdSignature)));
}
}
void BindlessDeferred::Shutdown()
{
ShadowHelper::Shutdown();
for(uint64 i = 0; i < ArraySize_(sceneModels); ++i)
sceneModels[i].Shutdown();
meshRenderer.Shutdown();
skybox.Shutdown();
skyCache.Shutdown();
postProcessor.Shutdown();
decalBuffer.Shutdown();
decalBoundsBuffer.Shutdown();
decalClusterBuffer.Shutdown();
decalInstanceBuffer.Shutdown();
for(uint64 i = 0; i < ArraySize_(decalTextures); ++i)
decalTextures[i].Shutdown();
spotLightBuffer.Shutdown();
spotLightBoundsBuffer.Shutdown();
spotLightClusterBuffer.Shutdown();
spotLightInstanceBuffer.Shutdown();
DX12::Release(clusterRS);
clusterMSAATarget.Shutdown();
decalClusterVtxBuffer.Shutdown();
decalClusterIdxBuffer.Shutdown();
spotLightClusterVtxBuffer.Shutdown();
spotLightClusterIdxBuffer.Shutdown();
DX12::Release(deferredRootSignature);
DX12::Release(deferredCmdSignature);
DX12::Release(msaaMaskRootSignature);
nonMsaaTileBuffer.Shutdown();
msaaTileBuffer.Shutdown();
nonMsaaArgsBuffer.Shutdown();
msaaArgsBuffer.Shutdown();
msaaMaskBuffer.Shutdown();
pickingBuffer.Shutdown();
DX12::Release(pickingRS);
for(uint64 i = 0; i < ArraySize_(pickingReadbackBuffers); ++i)
pickingReadbackBuffers[i].Shutdown();
DX12::Release(clusterVisRootSignature);
mainTarget.Shutdown();
tangentFrameTarget.Shutdown();
resolveTarget.Shutdown();
depthBuffer.Shutdown();
uvTarget.Shutdown();
uvGradientsTarget.Shutdown();
materialIDTarget.Shutdown();
deferredMSAATarget.Shutdown();
DX12::Release(resolveRootSignature);
}
void BindlessDeferred::CreatePSOs()
{
DXGI_FORMAT gBufferFormats[] = { tangentFrameTarget.Format(), uvTarget.Format(), materialIDTarget.Format(), uvGradientsTarget.Format() };
uint64 numGBuffers = AppSettings::ComputeUVGradients ? ArraySize_(gBufferFormats) - 1 : ArraySize_(gBufferFormats);
meshRenderer.CreatePSOs(mainTarget.Texture.Format, depthBuffer.DSVFormat, gBufferFormats, numGBuffers, mainTarget.MSAASamples);
skybox.CreatePSOs(mainTarget.Texture.Format, depthBuffer.DSVFormat, mainTarget.MSAASamples);
postProcessor.CreatePSOs();
{
// Clustering PSO
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.pRootSignature = clusterRS;
psoDesc.BlendState = DX12::GetBlendState(BlendState::Disabled);
psoDesc.DepthStencilState = DX12::GetDepthState(DepthState::Disabled);
psoDesc.SampleMask = UINT_MAX;
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
psoDesc.NumRenderTargets = 0;
psoDesc.VS = clusterVS.ByteCode();
ClusterRasterizationModes rastMode = AppSettings::ClusterRasterizationMode;
if(rastMode == ClusterRasterizationModes::MSAA4x || rastMode == ClusterRasterizationModes::MSAA8x)
{
psoDesc.SampleDesc.Count = clusterMSAATarget.MSAASamples;
psoDesc.SampleDesc.Quality = DX12::StandardMSAAPattern;
psoDesc.NumRenderTargets = 1;
psoDesc.RTVFormats[0] = clusterMSAATarget.Format();
}
else
psoDesc.SampleDesc.Count = 1;
D3D12_CONSERVATIVE_RASTERIZATION_MODE crMode = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
if(rastMode == ClusterRasterizationModes::Conservative)
crMode = D3D12_CONSERVATIVE_RASTERIZATION_MODE_ON;
psoDesc.PS = clusterFrontFacePS.ByteCode();
psoDesc.RasterizerState = DX12::GetRasterizerState(RasterizerState::BackFaceCull);
psoDesc.RasterizerState.ConservativeRaster = crMode;
DXCall(DX12::Device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&clusterFrontFacePSO)));
psoDesc.PS = clusterBackFacePS.ByteCode();
psoDesc.RasterizerState = DX12::GetRasterizerState(RasterizerState::FrontFaceCull);
psoDesc.RasterizerState.ConservativeRaster = crMode;
DXCall(DX12::Device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&clusterBackFacePSO)));
psoDesc.PS = clusterIntersectingPS.ByteCode();
psoDesc.RasterizerState = DX12::GetRasterizerState(RasterizerState::FrontFaceCull);
psoDesc.RasterizerState.ConservativeRaster = crMode;
DXCall(DX12::Device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&clusterIntersectingPSO)));
clusterFrontFacePSO->SetName(L"Cluster Front-Face PSO");
clusterBackFacePSO->SetName(L"Cluster Back-Face PSO");
clusterIntersectingPSO->SetName(L"Cluster Intersecting PSO");
}
const bool msaaEnabled = AppSettings::MSAAMode != MSAAModes::MSAANone;
const uint64 msaaModeIdx = uint64(AppSettings::MSAAMode);
if(msaaEnabled)
{
// MSAA mask PSO's
D3D12_COMPUTE_PIPELINE_STATE_DESC psoDesc = { };
psoDesc.CS = msaaMaskCS[uint64(AppSettings::MSAAMode)][0].ByteCode();
psoDesc.pRootSignature = msaaMaskRootSignature;
DXCall(DX12::Device->CreateComputePipelineState(&psoDesc, IID_PPV_ARGS(&msaaMaskPSOs[0])));
psoDesc.CS = msaaMaskCS[uint64(AppSettings::MSAAMode)][1].ByteCode();
DXCall(DX12::Device->CreateComputePipelineState(&psoDesc, IID_PPV_ARGS(&msaaMaskPSOs[1])));
}
if(taskSet == nullptr || EnableMultithreadedCompilation == false)
{
// Deferred rendering PSO
const uint64 uvGradIdx = AppSettings::ComputeUVGradients ? 1 : 0;
D3D12_COMPUTE_PIPELINE_STATE_DESC psoDesc = { };
psoDesc.CS = deferredCS[msaaModeIdx][uvGradIdx][0].ByteCode();
psoDesc.pRootSignature = deferredRootSignature;
DXCall(DX12::Device->CreateComputePipelineState(&psoDesc, IID_PPV_ARGS(&deferredPSOs[0])));
if(AppSettings::MSAAMode != MSAAModes::MSAANone)
{
psoDesc.CS = deferredCS[msaaModeIdx][uvGradIdx][1].ByteCode();
DXCall(DX12::Device->CreateComputePipelineState(&psoDesc, IID_PPV_ARGS(&deferredPSOs[1])));
}
}
if(msaaEnabled)
{
// Resolve PSO
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.pRootSignature = resolveRootSignature;
psoDesc.VS = fullScreenTriVS.ByteCode();
psoDesc.RasterizerState = DX12::GetRasterizerState(RasterizerState::NoCull);
psoDesc.BlendState = DX12::GetBlendState(BlendState::Disabled);
psoDesc.DepthStencilState = DX12::GetDepthState(DepthState::Disabled);
psoDesc.SampleMask = UINT_MAX;
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
psoDesc.NumRenderTargets = 1;
psoDesc.RTVFormats[0] = mainTarget.Format();
psoDesc.SampleDesc.Count = 1;
psoDesc.PS = resolvePS[msaaModeIdx][0].ByteCode();
DXCall(DX12::Device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&resolvePSOs[0])));
psoDesc.PS = resolvePS[msaaModeIdx][1].ByteCode();
DXCall(DX12::Device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&resolvePSOs[1])));
}
{
// Cluster visualizer PSO
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.pRootSignature = clusterVisRootSignature;
psoDesc.VS = fullScreenTriVS.ByteCode();
psoDesc.PS = clusterVisPS.ByteCode();
psoDesc.RasterizerState = DX12::GetRasterizerState(RasterizerState::NoCull);
psoDesc.BlendState = DX12::GetBlendState(BlendState::AlphaBlend);
psoDesc.DepthStencilState = DX12::GetDepthState(DepthState::Disabled);
psoDesc.SampleMask = UINT_MAX;
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
psoDesc.NumRenderTargets = 1;
psoDesc.RTVFormats[0] = swapChain.Format();
psoDesc.SampleDesc.Count = 1;
DXCall(DX12::Device->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&clusterVisPSO)));
}
{
// Picking PSO
D3D12_COMPUTE_PIPELINE_STATE_DESC psoDesc = { };
psoDesc.CS = pickingCS[0].ByteCode();
psoDesc.pRootSignature = pickingRS;
DXCall(DX12::Device->CreateComputePipelineState(&psoDesc, IID_PPV_ARGS(&pickingPSOs[0])));
psoDesc.CS = pickingCS[1].ByteCode();
DXCall(DX12::Device->CreateComputePipelineState(&psoDesc, IID_PPV_ARGS(&pickingPSOs[1])));
}
}
void BindlessDeferred::DestroyPSOs()
{
meshRenderer.DestroyPSOs();
skybox.DestroyPSOs();
postProcessor.DestroyPSOs();
DX12::DeferredRelease(clusterFrontFacePSO);
DX12::DeferredRelease(clusterBackFacePSO);
DX12::DeferredRelease(clusterIntersectingPSO);
DX12::DeferredRelease(pickingPSOs[0]);
DX12::DeferredRelease(pickingPSOs[1]);
DX12::DeferredRelease(clusterVisPSO);
for(uint64 i = 0; i < ArraySize_(msaaMaskPSOs); ++i)
DX12::DeferredRelease(msaaMaskPSOs[i]);
for(uint64 i = 0; i < ArraySize_(deferredPSOs); ++i)
DX12::DeferredRelease(deferredPSOs[i]);
for(uint64 i = 0; i < ArraySize_(resolvePSOs); ++i)
DX12::DeferredRelease(resolvePSOs[i]);
}
// Creates all required render targets
void BindlessDeferred::CreateRenderTargets()
{
uint32 width = swapChain.Width();
uint32 height = swapChain.Height();
const uint32 NumSamples = AppSettings::NumMSAASamples();
{
RenderTextureInit rtInit;
rtInit.Width = width;
rtInit.Height = height;
rtInit.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
rtInit.MSAASamples = NumSamples;
rtInit.ArraySize = 1;
rtInit.CreateUAV = NumSamples == 1;
rtInit.InitialState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
rtInit.Name = L"Main Target";
mainTarget.Initialize(rtInit);
}
{
RenderTextureInit rtInit;
rtInit.Width = width;
rtInit.Height = height;
rtInit.Format = DXGI_FORMAT_R10G10B10A2_UNORM;
rtInit.MSAASamples = NumSamples;
rtInit.ArraySize = 1;
rtInit.CreateUAV = false;
rtInit.InitialState = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
rtInit.Name = L"Tangent Frame Target";
tangentFrameTarget.Initialize(rtInit);
}
{
RenderTextureInit rtInit;
rtInit.Width = width;
rtInit.Height = height;
rtInit.Format = DXGI_FORMAT_R16G16B16A16_SNORM;
rtInit.MSAASamples = NumSamples;
rtInit.ArraySize = 1;
rtInit.CreateUAV = false;
rtInit.InitialState = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
rtInit.Name = L"UV Target";
uvTarget.Initialize(rtInit);
}
{
RenderTextureInit rtInit;
rtInit.Width = width;
rtInit.Height = height;
rtInit.Format = DXGI_FORMAT_R16G16B16A16_SNORM;
rtInit.MSAASamples = NumSamples;
rtInit.ArraySize = 1;
rtInit.CreateUAV = false;
rtInit.InitialState = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
rtInit.Name = L"UV Gradient Target";
uvGradientsTarget.Initialize(rtInit);
}
{
RenderTextureInit rtInit;
rtInit.Width = width;
rtInit.Height = height;
rtInit.Format = DXGI_FORMAT_R8_UINT;
rtInit.MSAASamples = NumSamples;
rtInit.ArraySize = 1;
rtInit.CreateUAV = false;
rtInit.InitialState = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
rtInit.Name = L"Material ID Target";
materialIDTarget.Initialize(rtInit);
}
if(NumSamples > 1)
{
{
RenderTextureInit rtInit;
rtInit.Width = width;
rtInit.Height = height;
rtInit.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
rtInit.MSAASamples = 1;
rtInit.ArraySize = 1;
rtInit.CreateUAV = false;
rtInit.InitialState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
rtInit.Name = L"Resolve Target";
resolveTarget.Initialize(rtInit);
}
{
RenderTextureInit rtInit;
rtInit.Width = width * 2;
rtInit.Height = NumSamples == 4 ? height * 2 : height;
rtInit.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
rtInit.MSAASamples = 1;
rtInit.ArraySize = 1;
rtInit.CreateUAV = true;
rtInit.InitialState = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
rtInit.Name = L"Deferred MSAA Target";
deferredMSAATarget.Initialize(rtInit);
}
}
{
DepthBufferInit dbInit;
dbInit.Width = width;
dbInit.Height = height;
dbInit.Format = DXGI_FORMAT_D32_FLOAT;
dbInit.MSAASamples = NumSamples;
dbInit.InitialState = D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_DEPTH_READ;
dbInit.Name = L"Main Depth Buffer";
depthBuffer.Initialize(dbInit);
}
AppSettings::NumXTiles = (width + (AppSettings::ClusterTileSize - 1)) / AppSettings::ClusterTileSize;
AppSettings::NumYTiles = (height + (AppSettings::ClusterTileSize - 1)) / AppSettings::ClusterTileSize;
const uint64 numXYZTiles = AppSettings::NumXTiles * AppSettings::NumYTiles * AppSettings::NumZTiles;
{
// Render target for forcing MSAA during cluster rasterization. Ideally we would use ForcedSampleCount for this,
// but it's currently causing the Nvidia driver to crash. :(
RenderTextureInit rtInit;
rtInit.Width = AppSettings::NumXTiles;
rtInit.Height = AppSettings::NumYTiles;
rtInit.Format = DXGI_FORMAT_R8_UNORM;
rtInit.MSAASamples = 1;
rtInit.ArraySize = 1;
rtInit.CreateUAV = false;
rtInit.Name = L"Deferred MSAA Target";
ClusterRasterizationModes rastMode = AppSettings::ClusterRasterizationMode;
if(rastMode == ClusterRasterizationModes::MSAA4x)
{
rtInit.MSAASamples = 4;
clusterMSAATarget.Initialize(rtInit);
}
else if(rastMode == ClusterRasterizationModes::MSAA8x)
{
rtInit.MSAASamples = 8;
clusterMSAATarget.Initialize(rtInit);
}
else
clusterMSAATarget.Shutdown();
}
{
// Decal cluster bitmask buffer
RawBufferInit rbInit;
rbInit.NumElements = numXYZTiles * AppSettings::DecalElementsPerCluster;
rbInit.CreateUAV = true;
decalClusterBuffer.Initialize(rbInit);
decalClusterBuffer.InternalBuffer.Resource->SetName(L"Decal Cluster Buffer");
}
{
// Spotlight cluster bitmask buffer
RawBufferInit rbInit;
rbInit.NumElements = numXYZTiles * AppSettings::SpotLightElementsPerCluster;
rbInit.CreateUAV = true;