forked from microsoft/Xbox-ATG-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAO.cpp
1011 lines (846 loc) · 44.9 KB
/
AO.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
//--------------------------------------------------------------------------------------
// D3D12RaytracingAO.h
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "AO.h"
#include "CompiledShaders\AORaytracing.hlsl.h"
#include "CosineHemiSampler.h"
#include "UniformHemiSampler.h"
#include "UniformSampler.h"
#include "StratifiedSampler.h"
#include "RayTracingHelper.h"
using namespace DX;
using namespace DirectX;
using Microsoft::WRL::ComPtr;
const wchar_t* AO::c_hitGroupNames[] = {
L"AOHitGroup",
L"AOBounceHitGroup"
};
const wchar_t* AO::c_raygenShaderName = L"AORaygenShader";
const wchar_t* AO::c_closestHitShaderNames[] =
{
L"AOClosestHitShader",
L"AOBounceClosestHitShader"
};
const wchar_t* AO::c_missShaderNames[] =
{
L"AOMissShader",
L"AOBounceMissShader"
};
// Setup the root signatures for the shaders.
void AO::CreateRootSignatures()
{
ID3D12RaytracingFallbackDevice* fallbackDevice = m_deviceResources->GetRaytracingFallbackDevice();
auto dxrDevice = m_deviceResources->GetD3DRayTracingDevice();
// Global Root Signature
// This is a root signature that is shared across all raytracing shaders invoked during a DispatchRays() call.
{
CD3DX12_DESCRIPTOR_RANGE ranges[2]; // Perfomance TIP: Order from most frequent to least frequent.
ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 0); // 1 output texture.
ranges[1].Init(D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, 2, 0); // point wrap and linear clamp samplers.
CD3DX12_ROOT_PARAMETER rootParameters[AOGlobalRootSig::GlobalCount];
rootParameters[AOGlobalRootSig::GlobalOutputViewSlot ].InitAsDescriptorTable(1, &ranges[0]);
rootParameters[AOGlobalRootSig::GlobalAccelStructSlot ].InitAsShaderResourceView(0);
rootParameters[AOGlobalRootSig::GlobalSceneConstSlot ].InitAsConstantBufferView(0);
rootParameters[AOGlobalRootSig::GlobalAOConstSlot ].InitAsConstantBufferView(1);
rootParameters[AOGlobalRootSig::GlobalAOOptionsConstSlot].InitAsConstantBufferView(2);
rootParameters[AOGlobalRootSig::GlobalSamplerSlot ].InitAsDescriptorTable(1, &ranges[1]);
CD3DX12_ROOT_SIGNATURE_DESC globalRootSignatureDesc(_countof(rootParameters), rootParameters);
if (!m_deviceResources->m_isDxrNativelySupported)
{
SerializeAndCreateRaytracingRootSignature(fallbackDevice, globalRootSignatureDesc, &m_raytracingGlobalRootSignature);
}
else // DirectX Raytracing
{
ComPtr<ID3DBlob> blob;
ComPtr<ID3DBlob> error;
ThrowIfFailed(D3D12SerializeRootSignature(&globalRootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, &error), error ? static_cast<wchar_t*>(error->GetBufferPointer()) : nullptr);
ThrowIfFailed(dxrDevice->CreateRootSignature(1, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&(m_raytracingGlobalRootSignature))));
}
}
// Local Root Signature
// This is a root signature that enables a shader to have unique arguments that come from shader tables.
{
CD3DX12_DESCRIPTOR_RANGE ranges[1]; // Perfomance TIP: Order from most frequent to least frequent.
// indices, vertices, diffuse texture, specular texture, and normal map.
ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, AOPerObjectCSUDesc::CSUCount, 0, SPACE_LOCAL);
CD3DX12_ROOT_PARAMETER rootParameters[AOLocalRootSig::LocalCount];
rootParameters[AOLocalRootSig::LocalSRVBufferSlot].InitAsDescriptorTable(1, &ranges[0]);
rootParameters[AOLocalRootSig::LocalMeshConstSlot].InitAsConstants(RoundUp32(sizeof(MaterialConstantBuffer)), 0, SPACE_LOCAL);
CD3DX12_ROOT_SIGNATURE_DESC localRootSignatureDesc(_countof(rootParameters), rootParameters);
localRootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_LOCAL_ROOT_SIGNATURE;
if (!m_deviceResources->m_isDxrNativelySupported)
{
SerializeAndCreateRaytracingRootSignature(fallbackDevice, localRootSignatureDesc, &m_raytracingLocalRootSignature);
}
else
{
ComPtr<ID3DBlob> blob;
ComPtr<ID3DBlob> error;
ThrowIfFailed(D3D12SerializeRootSignature(&localRootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, &error), error ? static_cast<wchar_t*>(error->GetBufferPointer()) : nullptr);
ThrowIfFailed(dxrDevice->CreateRootSignature(1, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&(m_raytracingLocalRootSignature))));
}
}
#ifdef USE_NON_NULL_LOCAL_ROOT_SIG
// Empty local root signature
{
CD3DX12_ROOT_SIGNATURE_DESC localRootSignatureDesc(D3D12_DEFAULT);
localRootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_LOCAL_ROOT_SIGNATURE;
if (!m_deviceResources->m_isDxrNativelySupported)
{
SerializeAndCreateRaytracingRootSignature(fallbackDevice, localRootSignatureDesc, &m_raytracingLocalRootSignatureEmpty);
}
else
{
ComPtr<ID3DBlob> blob;
ComPtr<ID3DBlob> error;
ThrowIfFailed(D3D12SerializeRootSignature(&localRootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, &error), error ? static_cast<wchar_t*>(error->GetBufferPointer()) : nullptr);
ThrowIfFailed(dxrDevice->CreateRootSignature(1, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&(m_raytracingLocalRootSignatureEmpty))));
}
}
#endif
}
// Local root signature and shader association
// This is a root signature that enables a shader to have unique arguments that come from shader tables.
void AO::CreateLocalRootSignatureSubobjects(CD3D12_STATE_OBJECT_DESC* raytracingPipeline)
{
// Local root signature to be used in a ray gen shader.
{
auto localRootSignature = raytracingPipeline->CreateSubobject<CD3D12_LOCAL_ROOT_SIGNATURE_SUBOBJECT>();
localRootSignature->SetRootSignature(m_raytracingLocalRootSignature.Get());
// Shader association
auto rootSignatureAssociation = raytracingPipeline->CreateSubobject<CD3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION_SUBOBJECT>();
rootSignatureAssociation->SetSubobjectToAssociate(*localRootSignature);
rootSignatureAssociation->AddExports(c_hitGroupNames);
}
#ifdef USE_NON_NULL_LOCAL_ROOT_SIG
// Empty local root signature to be used in a miss shader and a hit group.
{
auto localRootSignature = raytracingPipeline->CreateSubobject<CD3D12_LOCAL_ROOT_SIGNATURE_SUBOBJECT>();
localRootSignature->SetRootSignature(m_raytracingLocalRootSignatureEmpty.Get());
// Shader association
auto rootSignatureAssociation = raytracingPipeline->CreateSubobject<CD3D12_SUBOBJECT_TO_EXPORTS_ASSOCIATION_SUBOBJECT>();
rootSignatureAssociation->SetSubobjectToAssociate(*localRootSignature);
rootSignatureAssociation->AddExport(c_raygenShaderName);
rootSignatureAssociation->AddExports(c_missShaderNames);
}
#endif
}
// Create a raytracing pipeline state object (RTPSO).
// An RTPSO represents a full set of shaders reachable by a DispatchRays() call,
// with all configuration options resolved, such as local signatures and other state.
void AO::CreateRaytracingPipelineStateObject()
{
ID3D12RaytracingFallbackDevice* fallbackDevice = m_deviceResources->GetRaytracingFallbackDevice();
ID3D12Device5* dxrDevice = m_deviceResources->GetD3DRayTracingDevice();
// Create 8 subobjects that combine into a RTPSO:
// Subobjects need to be associated with DXIL exports (i.e. shaders) either by way of default or explicit associations.
// Default association applies to every exported shader entrypoint that doesn't have any of the same type of subobject associated with it.
// This simple sample utilizes default shader association except for local root signature subobject
// which has an explicit association specified purely for demonstration purposes.
// 1 - DXIL library
// 2 - Triangle hit group
// 1 - Shader config
// 2 - Local root signature and association
// 1 - Global root signature
// 1 - Pipeline config
CD3D12_STATE_OBJECT_DESC raytracingPipeline{ D3D12_STATE_OBJECT_TYPE_RAYTRACING_PIPELINE };
// DXIL library
// This contains the shaders and their entrypoints for the state object.
// Since shaders are not considered a subobject, they need to be passed in via DXIL library subobjects.
auto lib = raytracingPipeline.CreateSubobject<CD3D12_DXIL_LIBRARY_SUBOBJECT>();
D3D12_SHADER_BYTECODE libdxil = CD3DX12_SHADER_BYTECODE((void *)g_pAORaytracing, sizeof(g_pAORaytracing));
lib->SetDXILLibrary(&libdxil);
// Define which shader exports to surface from the library.
// If no shader exports are defined for a DXIL library subobject, all shaders will be surfaced.
// In this sample, this could be ommited for convenience since the sample uses all shaders in the library.
{
lib->DefineExport(c_raygenShaderName);
lib->DefineExports(c_closestHitShaderNames);
lib->DefineExports(c_missShaderNames);
}
// Triangle hit group
// A hit group specifies closest hit, any hit and intersection shaders to be executed when a ray intersects the geometry's triangle/AABB.
// In this sample, we only use triangle geometry with a closest hit shader, so others are not set.
for (unsigned int i = 0; i < TraceRayParameters::HitGroup::Count; i++)
{
auto hitGroup = raytracingPipeline.CreateSubobject<CD3D12_HIT_GROUP_SUBOBJECT>();
hitGroup->SetClosestHitShaderImport(c_closestHitShaderNames[i]);
hitGroup->SetHitGroupExport(c_hitGroupNames[i]);
}
// Shader config
// Defines the maximum sizes in bytes for the ray payload and attribute structure.
auto shaderConfig = raytracingPipeline.CreateSubobject<CD3D12_RAYTRACING_SHADER_CONFIG_SUBOBJECT>();
unsigned int payloadSize = sizeof(XMFLOAT4); // float4 pixelColor
unsigned int attributeSize = sizeof(XMFLOAT2); // float2 barycentrics
shaderConfig->Config(payloadSize, attributeSize);
// Local root signature and shader association
// This is a root signature that enables a shader to have unique arguments that come from shader tables.
CreateLocalRootSignatureSubobjects(&raytracingPipeline);
// Global root signature
// This is a root signature that is shared across all raytracing shaders invoked during a DispatchRays() call.
auto globalRootSignature = raytracingPipeline.CreateSubobject<CD3D12_GLOBAL_ROOT_SIGNATURE_SUBOBJECT>();
globalRootSignature->SetRootSignature(m_raytracingGlobalRootSignature.Get());
// Pipeline config
// Defines the maximum TraceRay() recursion depth.
auto pipelineConfig = raytracingPipeline.CreateSubobject<CD3D12_RAYTRACING_PIPELINE_CONFIG_SUBOBJECT>();
// PERFOMANCE TIP: Set max recursion depth as low as needed
// as drivers may apply optimization strategies for low recursion depths.
unsigned int maxRecursionDepth = 2; // ~ primary rays and first bounce AO rays.
pipelineConfig->Config(maxRecursionDepth);
#if _DEBUG
PrintStateObjectDesc(raytracingPipeline);
#endif
if (!m_deviceResources->m_isDxrNativelySupported)
{
ThrowIfFailed(fallbackDevice->CreateStateObject(raytracingPipeline, IID_PPV_ARGS(&m_deviceResources->m_fallbackStateObject)), L"Couldn't create DirectX Raytracing state object.\n");
}
else
{
ThrowIfFailed(dxrDevice->CreateStateObject(raytracingPipeline, IID_PPV_ARGS(&m_deviceResources->m_dxrStateObject)), L"Couldn't create DirectX Raytracing state object.\n");
}
}
// Setup descriptor heaps.
void AO::CreateDescriptorHeaps()
{
auto device = m_deviceResources->GetD3DDevice();
// Allocate a csu heap.
{
const uint32_t c_csuCount = AOCSUDesc::CSUCount;
m_csuDescriptors = std::make_unique<DescriptorHeap>(
device, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, c_csuCount);
}
// Allocate a sampler heap.
{
const uint32_t c_samplerCount = AOSamplerDesc::SamplerCount;
m_samplerDescriptors = std::make_unique<DescriptorHeap>(
device, D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE, c_samplerCount);
}
// Set sampler heap values.
{
CreateTexture2DSampler(
device,
m_samplerDescriptors->GetCpuHandle(AOSamplerDesc::SamplerPointWrap),
D3D12_FILTER_MIN_MAG_MIP_POINT,
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
D3D12_TEXTURE_ADDRESS_MODE_WRAP,
D3D12_TEXTURE_ADDRESS_MODE_WRAP
);
CreateTexture2DSampler(
device,
m_samplerDescriptors->GetCpuHandle(AOSamplerDesc::SamplerLinearClamp),
D3D12_FILTER_MIN_MAG_MIP_LINEAR,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP,
D3D12_TEXTURE_ADDRESS_MODE_CLAMP
);
}
}
// Build acceleration structures needed for raytracing.
void AO::BuildAccelerationStructures()
{
auto device = m_deviceResources->GetD3DDevice();
auto dxrDevice = m_deviceResources->GetD3DRayTracingDevice();
auto commandList = m_deviceResources->GetCommandList();
auto dxrCommandList = m_deviceResources->GetRaytracingCommandList();
auto commandAllocator = m_deviceResources->GetCommandAllocator();
ID3D12RaytracingFallbackDevice* fallbackDevice = m_deviceResources->GetRaytracingFallbackDevice();
ID3D12RaytracingFallbackCommandList* raytracingfallbackCommandList = m_deviceResources->GetRaytracingFallbackCommandList();
// Reset the command list for the acceleration structure construction.
commandList->Reset(commandAllocator, nullptr);
PIXBeginEvent(commandList, PIX_COLOR_DEFAULT, L"Acceleration Structure");
std::vector<D3D12_RAYTRACING_GEOMETRY_DESC> geometryDescArr;
// Create a descriptor for all geometry in the scene.
{
// Setup format for acceleration structure construction.
D3D12_RAYTRACING_GEOMETRY_DESC geometryDesc = {};
geometryDesc.Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;
geometryDesc.Triangles.IndexFormat = DXGI_FORMAT_R32_UINT;
geometryDesc.Triangles.Transform3x4 = 0;
geometryDesc.Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT;
geometryDesc.Triangles.VertexBuffer.StrideInBytes = sizeof(Vertex);
// Mark the geometry as opaque.
// PERFORMANCE TIP: mark geometry as opaque whenever applicable as it can enable important ray processing optimizations.
// Note: When rays encounter opaque geometry an any hit shader will not be executed whether it is present or not.
geometryDesc.Flags = D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE;
for (auto el : *m_mesh)
{
geometryDesc.Triangles.IndexCount = el.numIndices;
geometryDesc.Triangles.IndexBuffer = el.indexResource->GetGPUVirtualAddress();
geometryDesc.Triangles.VertexCount = el.numVertices;
geometryDesc.Triangles.VertexBuffer.StartAddress = el.vertexResource->GetGPUVirtualAddress();
geometryDescArr.push_back(geometryDesc);
}
}
// Get required sizes for an acceleration structure.
// For the purposes of the demo, a tree that has fast ray tracing at the cost of construction time is built.
D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAGS buildFlags = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_BUILD_FLAG_PREFER_FAST_TRACE;
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC bottomLevelBuildDesc = {};
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS& bottomLevelInputs = bottomLevelBuildDesc.Inputs;
bottomLevelInputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
bottomLevelInputs.Flags = buildFlags;
bottomLevelInputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL;
bottomLevelInputs.NumDescs = static_cast<unsigned int>(geometryDescArr.size());
bottomLevelInputs.pGeometryDescs = geometryDescArr.data();
D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO bottomLevelPrebuildInfo = {};
if (!m_deviceResources->m_isDxrNativelySupported)
{
fallbackDevice->GetRaytracingAccelerationStructurePrebuildInfo(&bottomLevelInputs, &bottomLevelPrebuildInfo);
}
else // DirectX Raytracing
{
dxrDevice->GetRaytracingAccelerationStructurePrebuildInfo(&bottomLevelInputs, &bottomLevelPrebuildInfo);
}
ThrowIfFalse(bottomLevelPrebuildInfo.ResultDataMaxSizeInBytes > 0);
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC topLevelBuildDesc = bottomLevelBuildDesc;
D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS& topLevelInputs = topLevelBuildDesc.Inputs;
topLevelInputs.DescsLayout = D3D12_ELEMENTS_LAYOUT_ARRAY;
topLevelInputs.Flags = buildFlags;
topLevelInputs.NumDescs = 1;
topLevelInputs.pGeometryDescs = nullptr;
topLevelInputs.Type = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL;
D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO topLevelPrebuildInfo = {};
if (!m_deviceResources->m_isDxrNativelySupported)
{
fallbackDevice->GetRaytracingAccelerationStructurePrebuildInfo(&topLevelInputs, &topLevelPrebuildInfo);
}
else // DirectX Raytracing
{
dxrDevice->GetRaytracingAccelerationStructurePrebuildInfo(&topLevelInputs, &topLevelPrebuildInfo);
}
ThrowIfFalse(topLevelPrebuildInfo.ResultDataMaxSizeInBytes > 0);
// Allocate buffer to be used during acceleration structure construction.
ComPtr<ID3D12Resource> scratchResource;
{
AllocateUAVBuffer(device,
std::max(topLevelPrebuildInfo.ScratchDataSizeInBytes,
bottomLevelPrebuildInfo.ScratchDataSizeInBytes),
&scratchResource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
L"ScratchResource");
}
// Allocate resources for acceleration structures.
// Acceleration structures can only be placed in resources that are created in the default heap (or custom heap equivalent).
// Default heap is OK since the application doesnt need CPU read/write access to them.
// The resources that will contain acceleration structures must be created in the state D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE,
// and must have resource flag D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS. The ALLOW_UNORDERED_ACCESS requirement simply acknowledges both:
// - the system will be doing this type of access in its implementation of acceleration structure builds behind the scenes.
// - from the app point of view, synchronization of writes/reads to acceleration structures is accomplished using UAV barriers.
{
D3D12_RESOURCE_STATES initialResourceState;
if (!m_deviceResources->m_isDxrNativelySupported)
{
initialResourceState = fallbackDevice->GetAccelerationStructureResourceState();
}
else
{
initialResourceState = D3D12_RESOURCE_STATE_RAYTRACING_ACCELERATION_STRUCTURE;
}
AllocateUAVBuffer(device, bottomLevelPrebuildInfo.ResultDataMaxSizeInBytes, &m_bottomLevelAccelerationStructure, initialResourceState, L"BottomLevelAccelerationStructure");
AllocateUAVBuffer(device, topLevelPrebuildInfo.ResultDataMaxSizeInBytes, &m_topLevelAccelerationStructure, initialResourceState, L"TopLevelAccelerationStructure");
}
// Note on Emulated GPU pointers (AKA Wrapped pointers) requirement in Fallback Layer:
// The primary point of divergence between the DXR API and the compute-based Fallback layer is the handling of GPU pointers.
// DXR fundamentally requires that GPUs be able to dynamically read from arbitrary addresses in GPU memory.
// The existing Direct Compute API today is more rigid than DXR and requires apps to explicitly inform the GPU what blocks of memory it will access with SRVs/UAVs.
// In order to handle the requirements of DXR, the Fallback Layer uses the concept of Emulated GPU pointers,
// which requires apps to create views around all memory they will access for raytracing,
// but retains the DXR-like flexibility of only needing to bind the top level acceleration structure at DispatchRays.
//
// The Fallback Layer interface uses WRAPPED_GPU_POintER to encapsulate the underlying pointer
// which will either be an emulated GPU pointer for the compute - based path or a GPU_VIRTUAL_ADDRESS for the DXR path.
// Create an instance desc for the bottom-level acceleration structure.
ComPtr<ID3D12Resource> instanceDescs;
if (!m_deviceResources->m_isDxrNativelySupported)
{
D3D12_RAYTRACING_FALLBACK_INSTANCE_DESC instanceDesc = {};
instanceDesc.Transform[0][0] = instanceDesc.Transform[1][1] = instanceDesc.Transform[2][2] = 1;
instanceDesc.InstanceMask = 1;
unsigned int numBufferElements = static_cast<unsigned int>(bottomLevelPrebuildInfo.ResultDataMaxSizeInBytes) / sizeof(uint32_t);
instanceDesc.AccelerationStructure =
CreateFallbackWrappedPointer(
device,
fallbackDevice,
m_bottomLevelAccelerationStructure.Get(),
m_csuDescriptors->GetFirstCpuHandle(),
AOCSUDesc::SRVBottomLevelAccel,
numBufferElements,
unsigned int(m_csuDescriptors->Increment()));
AllocateUploadBuffer(device, &instanceDesc, sizeof(instanceDesc), &instanceDescs, L"InstanceDescs");
}
else
{
D3D12_RAYTRACING_INSTANCE_DESC instanceDesc = {};
instanceDesc.Transform[0][0] = instanceDesc.Transform[1][1] = instanceDesc.Transform[2][2] = 1;
instanceDesc.InstanceMask = 1;
instanceDesc.AccelerationStructure = m_bottomLevelAccelerationStructure->GetGPUVirtualAddress();
AllocateUploadBuffer(device, &instanceDesc, sizeof(instanceDesc), &instanceDescs, L"InstanceDescs");
}
// Create a wrapped pointer to the acceleration structure.
if (!m_deviceResources->m_isDxrNativelySupported)
{
unsigned int numBufferElements = static_cast<unsigned int>(topLevelPrebuildInfo.ResultDataMaxSizeInBytes) / sizeof(uint32_t);
m_fallbackTopLevelAccelerationStructurePointer =
CreateFallbackWrappedPointer(
device,
fallbackDevice,
m_topLevelAccelerationStructure.Get(),
m_csuDescriptors->GetFirstCpuHandle(),
AOCSUDesc::SRVTopLevelAccel,
numBufferElements,
unsigned int(m_csuDescriptors->Increment()));
}
// Bottom Level Acceleration Structure desc
bottomLevelBuildDesc.ScratchAccelerationStructureData = { scratchResource->GetGPUVirtualAddress() };
bottomLevelBuildDesc.DestAccelerationStructureData = { m_bottomLevelAccelerationStructure->GetGPUVirtualAddress() };
// Top Level Acceleration Structure desc
topLevelBuildDesc.DestAccelerationStructureData = { m_topLevelAccelerationStructure->GetGPUVirtualAddress() };
topLevelBuildDesc.Inputs.InstanceDescs = instanceDescs->GetGPUVirtualAddress();
topLevelBuildDesc.ScratchAccelerationStructureData = { scratchResource->GetGPUVirtualAddress() };
// Build the acceleration structure.
if (!m_deviceResources->m_isDxrNativelySupported)
{
// Set the descriptor heaps to be used during acceleration structure build for the Fallback Layer.
ID3D12DescriptorHeap *pDescriptorHeaps[] = { m_csuDescriptors->Heap() };
raytracingfallbackCommandList->SetDescriptorHeaps(_countof(pDescriptorHeaps), pDescriptorHeaps);
CD3DX12_RESOURCE_BARRIER bottomBarrier = CD3DX12_RESOURCE_BARRIER::UAV(m_bottomLevelAccelerationStructure.Get());
raytracingfallbackCommandList->BuildRaytracingAccelerationStructure(&bottomLevelBuildDesc, 0, nullptr);
commandList->ResourceBarrier(1, &bottomBarrier);
raytracingfallbackCommandList->BuildRaytracingAccelerationStructure(&topLevelBuildDesc, 0, nullptr);
}
else
{
CD3DX12_RESOURCE_BARRIER bottomBarrier = CD3DX12_RESOURCE_BARRIER::UAV(m_bottomLevelAccelerationStructure.Get());
dxrCommandList->BuildRaytracingAccelerationStructure(&bottomLevelBuildDesc, 0, nullptr);
commandList->ResourceBarrier(1, &bottomBarrier);
dxrCommandList->BuildRaytracingAccelerationStructure(&topLevelBuildDesc, 0, nullptr);
}
// End commandlist.
PIXEndEvent(commandList);
// Kick off acceleration structure construction.
m_deviceResources->ExecuteCommandList();
// Wait for GPU to finish as the locally created temporary GPU resources will get released once we go out of scope.
m_deviceResources->WaitForGpu();
}
// Create constant buffers.
void AO::CreateConstantBuffers()
{
auto device = m_deviceResources->GetD3DDevice();
// Create a constant buffer for AO.
{
// Fill AO const buffer with samples.
AlignedAOConstantBuffer aoConstBuffer = {};
AllocateUploadBuffer(
device,
&aoConstBuffer,
sizeof(aoConstBuffer),
&m_mappedAOConstantResource);
}
// Create Options constant buffer.
{
AlignedAOOptionsConstantBuffer aoOptionsConstBuffer = {};
AllocateUploadBuffer(
device,
&aoOptionsConstBuffer,
sizeof(aoOptionsConstBuffer),
&m_mappedAOOptionsConstantResource);
m_mappedAOOptionsConstantResource->Map(0, nullptr, reinterpret_cast<void**>(&m_mappedAOOptionsConstantData));
}
}
// Build shader tables.
// This encapsulates all shader records - shaders and the arguments for their local root signatures.
void AO::BuildShaderTables()
{
auto device = m_deviceResources->GetD3DDevice();
ID3D12RaytracingFallbackDevice* fallbackDevice = m_deviceResources->GetRaytracingFallbackDevice();
void* rayGenShaderIdentifier;
void* missShaderIdentifiers[TraceRayParameters::MissShader::Count];
void* hitGroupShaderIdentifiers[TraceRayParameters::MissShader::Count];
unsigned int shaderIdentifierSize;
// Record shader information.
if (!m_deviceResources->m_isDxrNativelySupported)
{
rayGenShaderIdentifier = m_deviceResources->m_fallbackStateObject->GetShaderIdentifier(c_raygenShaderName);
for (unsigned int i = 0; i < TraceRayParameters::MissShader::Count; i++)
missShaderIdentifiers[i] = m_deviceResources->m_fallbackStateObject->GetShaderIdentifier(c_missShaderNames[i]);
for (unsigned int i = 0; i < TraceRayParameters::HitGroup::Count; i++)
hitGroupShaderIdentifiers[i] = m_deviceResources->m_fallbackStateObject->GetShaderIdentifier(c_hitGroupNames[i]);
shaderIdentifierSize = fallbackDevice->GetShaderIdentifierSize();
}
else
{
ComPtr<ID3D12StateObjectPropertiesPrototype> stateObjectProperties;
ThrowIfFailed(m_deviceResources->m_dxrStateObject.As(&stateObjectProperties));
rayGenShaderIdentifier = stateObjectProperties->GetShaderIdentifier(c_raygenShaderName);
for (unsigned int i = 0; i < TraceRayParameters::MissShader::Count; i++)
missShaderIdentifiers[i] = stateObjectProperties->GetShaderIdentifier(c_missShaderNames[i]);
for (unsigned int i = 0; i < TraceRayParameters::HitGroup::Count; i++)
hitGroupShaderIdentifiers[i] = stateObjectProperties->GetShaderIdentifier(c_hitGroupNames[i]);
shaderIdentifierSize = D3D12_SHADER_IDENTIFIER_SIZE_IN_BYTES;
}
// Ray gen shader table
{
unsigned int numShaderRecords = 1;
unsigned int shaderRecordSize = shaderIdentifierSize;
ShaderTable rayGenShaderTable(device, numShaderRecords, shaderRecordSize, L"RayGenShaderTable");
rayGenShaderTable.Add(ShaderRecord(rayGenShaderIdentifier, shaderIdentifierSize));
rayGenShaderTable.Close();
m_rayGenShaderTable = rayGenShaderTable.GetResource();
}
// Miss shader table
{
unsigned int numShaderRecords = TraceRayParameters::MissShader::Count;
unsigned int shaderRecordSize = shaderIdentifierSize;
ShaderTable missShaderTable(device, numShaderRecords, shaderRecordSize, L"MissShaderTable");
for (unsigned int i = 0; i < numShaderRecords; i++)
missShaderTable.Add(ShaderRecord(missShaderIdentifiers[i], shaderIdentifierSize));
missShaderTable.Close();
m_missRecordSize = missShaderTable.GetShaderRecordSize();
m_missShaderTable = missShaderTable.GetResource();
}
// Hit group shader table
{
unsigned int numShaderRecords = unsigned int(TraceRayParameters::HitGroup::Count * m_mesh->size());
unsigned int descriptorOffset = Align(shaderIdentifierSize, sizeof(D3D12_GPU_DESCRIPTOR_HANDLE));
unsigned int meshConstantOffset = Align(descriptorOffset + sizeof(D3D12_GPU_DESCRIPTOR_HANDLE), sizeof(uint32_t));
unsigned int shaderRecordSize = meshConstantOffset + sizeof(MaterialConstantBuffer);
ShaderTable hitGroupShaderTable(device, numShaderRecords, shaderRecordSize, L"HitGroupShaderTable");
size_t offset = 0;
std::vector<byte> rootArgs(shaderRecordSize - shaderIdentifierSize);
for (auto el : *m_mesh)
{
// Copy descriptor location.
D3D12_GPU_DESCRIPTOR_HANDLE csuHandle = m_csuDescriptors->GetGpuHandle(AOCSUDesc::SRVPerObjectStart + offset);
memcpy(rootArgs.data() + descriptorOffset - shaderIdentifierSize, &csuHandle, sizeof(D3D12_GPU_DESCRIPTOR_HANDLE));
offset += AOPerObjectCSUDesc::CSUCount;
// Copy over constant buffer.
auto& material = m_mesh->getModel()->materials[el.matID];
MaterialConstantBuffer cb;
cb.ambient = material.ambientColor;
cb.diffuse = material.diffuseColor;
cb.specular = material.specularColor;
cb.isDiffuseTexture = (material.diffuseTextureIndex != -1);
cb.isSpecularTexture = (material.specularTextureIndex != -1);
cb.isNormalTexture = (material.normalTextureIndex != -1);
memcpy(
rootArgs.data() + meshConstantOffset - shaderIdentifierSize,
&cb,
sizeof(MaterialConstantBuffer));
for (unsigned int i = 0; i < TraceRayParameters::HitGroup::Count; i++)
{
hitGroupShaderTable.Add(
ShaderRecord(
hitGroupShaderIdentifiers[i],
shaderIdentifierSize,
rootArgs.data(),
unsigned int(rootArgs.size())
)
);
}
}
hitGroupShaderTable.Close();
m_hitGroupRecordSize = hitGroupShaderTable.GetShaderRecordSize();
m_hitGroupShaderTable = hitGroupShaderTable.GetResource();
}
}
// Create 2D output texture for raytracing.
void AO::CreateRaytracingOutputResource()
{
auto device = m_deviceResources->GetD3DDevice();
auto backbufferFormat = m_deviceResources->GetBackBufferFormat();
auto screenWidth = m_deviceResources->GetScreenWidth() * m_screenWidthScale;
auto screenHeight = m_deviceResources->GetScreenHeight();
// Create the output resource. The dimensions and format should match the swap-chain.
auto uavDesc = CD3DX12_RESOURCE_DESC::Tex2D(backbufferFormat, (UINT64)screenWidth, screenHeight, 1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
auto defaultHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
ThrowIfFailed(device->CreateCommittedResource(&defaultHeapProperties, D3D12_HEAP_FLAG_NONE, &uavDesc, D3D12_RESOURCE_STATE_UNORDERED_ACCESS, nullptr, IID_PPV_ARGS(&m_raytracingOutput)));
// Create the UAV resource
{
D3D12_UNORDERED_ACCESS_VIEW_DESC UAVDesc = {};
UAVDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
device->CreateUnorderedAccessView(m_raytracingOutput.Get(), nullptr, &UAVDesc, m_csuDescriptors->GetCpuHandle(AOCSUDesc::UAVRaytracingOut));
}
// Setup output SRV.
{
CreateTexture2DSRV(
device,
m_raytracingOutput.Get(),
m_csuDescriptors->GetCpuHandle(AOCSUDesc::SRVRaytracingOut),
m_raytracingOutput->GetDesc().Format
);
}
}
// AO algorithm.
void AO::RunAORaytracing(ComPtr<ID3D12Resource> pSceneConstantResource)
{
ID3D12GraphicsCommandList* commandList = m_deviceResources->GetCommandList();
ID3D12GraphicsCommandList5* dxrCommandList = m_deviceResources->GetRaytracingCommandList();
ID3D12RaytracingFallbackCommandList* raytracingfallbackCommandList = m_deviceResources->GetRaytracingFallbackCommandList();
PIXBeginEvent(commandList, PIX_COLOR_DEFAULT, L"AORaytracing");
commandList->SetComputeRootSignature(m_raytracingGlobalRootSignature.Get());
commandList->SetGraphicsRootSignature(m_raytracingGlobalRootSignature.Get());
// Setup screenspace.
auto viewport = m_deviceResources->GetScreenViewport();
viewport.Width *= m_screenWidthScale;
D3D12_RECT scissorRect = m_deviceResources->GetScissorRect();
commandList->RSSetViewports(1, &viewport);
commandList->RSSetScissorRects(1, &scissorRect);
// Bind the heaps, acceleration structure and dispatch rays.
{
// Set index and successive vertex buffer decriptor tables
{
// Setup heaps
{
auto cbGpuAddress = pSceneConstantResource->GetGPUVirtualAddress();
commandList->SetComputeRootConstantBufferView(AOGlobalRootSig::GlobalSceneConstSlot, cbGpuAddress);
cbGpuAddress = m_mappedAOConstantResource->GetGPUVirtualAddress();
commandList->SetComputeRootConstantBufferView(AOGlobalRootSig::GlobalAOConstSlot, cbGpuAddress);
cbGpuAddress = m_mappedAOOptionsConstantResource->GetGPUVirtualAddress();
commandList->SetComputeRootConstantBufferView(AOGlobalRootSig::GlobalAOOptionsConstSlot, cbGpuAddress);
ID3D12DescriptorHeap* ppHeaps[] = { m_csuDescriptors->Heap(), m_samplerDescriptors->Heap() };
if (!m_deviceResources->m_isDxrNativelySupported)
{
raytracingfallbackCommandList->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps);
}
else
{
commandList->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps);
}
}
commandList->SetComputeRootDescriptorTable(AOGlobalRootSig::GlobalOutputViewSlot, m_csuDescriptors->GetGpuHandle(AOCSUDesc::UAVRaytracingOut));
commandList->SetComputeRootDescriptorTable(AOGlobalRootSig::GlobalSamplerSlot, m_samplerDescriptors->GetGpuHandle(AOSamplerDesc::SamplerPointWrap));
}
// Bind the heaps, acceleration structure and dispatch rays.
{
if (!m_deviceResources->m_isDxrNativelySupported)
{
raytracingfallbackCommandList->SetTopLevelAccelerationStructure(AOGlobalRootSig::GlobalAccelStructSlot, m_fallbackTopLevelAccelerationStructurePointer);
}
else
{
commandList->SetComputeRootShaderResourceView(AOGlobalRootSig::GlobalAccelStructSlot, m_topLevelAccelerationStructure->GetGPUVirtualAddress());
}
// Dispatch rays.
{
D3D12_DISPATCH_RAYS_DESC dispatchDesc = {};
// Since each shader table has only one shader record, the stride is same as the size.
dispatchDesc.HitGroupTable.StartAddress = m_hitGroupShaderTable->GetGPUVirtualAddress();
dispatchDesc.HitGroupTable.SizeInBytes = m_hitGroupRecordSize;
dispatchDesc.HitGroupTable.StrideInBytes = dispatchDesc.HitGroupTable.SizeInBytes;
dispatchDesc.MissShaderTable.StartAddress = m_missShaderTable->GetGPUVirtualAddress();
dispatchDesc.MissShaderTable.SizeInBytes = m_missShaderTable->GetDesc().Width;
dispatchDesc.MissShaderTable.StrideInBytes = m_missRecordSize;
dispatchDesc.RayGenerationShaderRecord.StartAddress = m_rayGenShaderTable->GetGPUVirtualAddress();
dispatchDesc.RayGenerationShaderRecord.SizeInBytes = m_rayGenShaderTable->GetDesc().Width;
dispatchDesc.Width = unsigned int(std::max<long>(m_deviceResources->GetOutputSize().right - m_deviceResources->GetOutputSize().left, 1) * m_screenWidthScale);
dispatchDesc.Height = std::max<long>(m_deviceResources->GetOutputSize().bottom - m_deviceResources->GetOutputSize().top, 1);
dispatchDesc.Depth = 1;
if (!m_deviceResources->m_isDxrNativelySupported)
{
raytracingfallbackCommandList->DispatchRays(&dispatchDesc);
}
else
{
dxrCommandList->SetPipelineState1(m_deviceResources->m_dxrStateObject.Get());
dxrCommandList->DispatchRays(&dispatchDesc);
}
}
}
}
PIXEndEvent(commandList);
}
// Copy the raytracing output to the backbuffer.
void AO::CopyRaytracingOutputToBackbuffer()
{
auto commandList = m_deviceResources->GetCommandList();
auto renderTarget = m_deviceResources->GetRenderTarget();
if (m_screenWidthScale == 1.f)
{
D3D12_RESOURCE_BARRIER preCopyBarriers[] =
{
CD3DX12_RESOURCE_BARRIER::Transition(renderTarget, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_DEST),
CD3DX12_RESOURCE_BARRIER::Transition(m_raytracingOutput.Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE)
};
commandList->ResourceBarrier(_countof(preCopyBarriers), preCopyBarriers);
commandList->CopyResource(renderTarget, m_raytracingOutput.Get());
D3D12_RESOURCE_BARRIER postCopyBarriers[] =
{
CD3DX12_RESOURCE_BARRIER::Transition(renderTarget, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_RENDER_TARGET),
CD3DX12_RESOURCE_BARRIER::Transition(m_raytracingOutput.Get(), D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)
};
commandList->ResourceBarrier(_countof(postCopyBarriers), postCopyBarriers);
}
else
{
D3D12_RESOURCE_BARRIER preCopyBarriers[] =
{
CD3DX12_RESOURCE_BARRIER::Transition(m_raytracingOutput.Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE)
};
commandList->ResourceBarrier(_countof(preCopyBarriers), preCopyBarriers);
CD3DX12_CPU_DESCRIPTOR_HANDLE viewportHandle = m_deviceResources->GetRenderTargetView();
commandList->OMSetRenderTargets(1, &viewportHandle, FALSE, nullptr);
D3D12_VIEWPORT viewport = m_deviceResources->GetScreenViewport();
commandList->RSSetViewports(1, &viewport);
{
m_basicEffect->SetTexture(m_csuDescriptors->GetGpuHandle(AOCSUDesc::SRVRaytracingOut), m_samplerDescriptors->GetGpuHandle(AOSamplerDesc::SamplerLinearClamp));
m_basicEffect->Apply(commandList);
m_primitiveBatch->Begin(commandList);
{
// Right Quad.
{
m_primitiveBatch->DrawQuad(
{ XMFLOAT3(m_screenWidthScale, 0.f, 0.f),{ 0.f, 0.f } },
{ XMFLOAT3(1.f, 0.f, 0.f),{ 1.f, 0.f } },
{ XMFLOAT3(1.f, 1.f, 0.f),{ 1.f, 1.f } },
{ XMFLOAT3(m_screenWidthScale, 1.f, 0.f),{ 0.f, 1.f } }
);
}
}
m_primitiveBatch->End();
}
D3D12_RESOURCE_BARRIER postCopyBarriers[] =
{
CD3DX12_RESOURCE_BARRIER::Transition(m_raytracingOutput.Get(), D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_UNORDERED_ACCESS)
};
commandList->ResourceBarrier(_countof(postCopyBarriers), postCopyBarriers);
}
}
// Setup AO for the scene.
void AO::Setup(std::shared_ptr<DeviceResources> pDeviceResources)
{
// Run super class setup.
Lighting::Setup(pDeviceResources);
// Create raytracing root signature.
CreateRootSignatures();
// Create a raytracing pipeline state object which defines the binding of shaders, state, and resources to be used during raytracing.
CreateRaytracingPipelineStateObject();
// Create a heap for descriptors.
CreateDescriptorHeaps();
// Create constant buffers for the geometry and the scene.
CreateConstantBuffers();
// Create an output 2D texture to store the raytracing result to.
CreateRaytracingOutputResource();
}
// Run AO.
void AO::Run(ComPtr<ID3D12Resource> pSceneConstantResource)
{
// No reason to clear the backbuffer since the raytracing output will be copied to it.
RunAORaytracing(pSceneConstantResource);
CopyRaytracingOutputToBackbuffer();
}
void AO::SetMesh(std::shared_ptr<Mesh> pMesh)
{
auto device = m_deviceResources->GetD3DDevice();
// Store the args.
{
m_mesh = pMesh;
// Check for nullptr case.
if (!m_mesh)
return;
}
// Create SRVs for vertices and indices.
{
size_t offset = 0;
for (auto el : *m_mesh)
{
// Vertex buffer is passed to the shader along with index buffer as a descriptor table.
// Vertex buffer descriptor must follow index buffer descriptor in the descriptor heap.
// Note that raw SRV Buffers must use DXGI_FORMAT_R32_TYPELESS.
// Ergo, indices must be uploaded as 32bit values regardless of actual size.
// For the case of this sample, 32bit SRV's are used.
// Indices.
CreateBufferSRV(
device,
el.indexResource.Get(),
m_csuDescriptors->GetCpuHandle(
AOCSUDesc::SRVPerObjectStart + offset),
0,
el.numIndices,
0,
DXGI_FORMAT_R32_TYPELESS,
D3D12_BUFFER_SRV_FLAG_RAW);
offset++;
// Vertices.
CreateBufferSRV(
device,
el.vertexResource.Get(),
m_csuDescriptors->GetCpuHandle(
AOCSUDesc::SRVPerObjectStart + offset),
0,
el.numVertices,
el.stride);
offset++;
auto& material = m_mesh->getModel()->materials[el.matID];
// Diffuse.
m_mesh->SetTextureSRV(
device,
m_csuDescriptors->GetCpuHandle(
AOCSUDesc::SRVPerObjectStart + offset),
material.diffuseTextureIndex
);
offset++;
// Specular.
m_mesh->SetTextureSRV(
device,
m_csuDescriptors->GetCpuHandle(
AOCSUDesc::SRVPerObjectStart + offset),
material.specularTextureIndex
);
offset++;
// Normal.
m_mesh->SetTextureSRV(
device,
m_csuDescriptors->GetCpuHandle(
AOCSUDesc::SRVPerObjectStart + offset),
material.normalTextureIndex
);
offset++;
}
}
// Build raytracing acceleration structures from the generated geometry.
BuildAccelerationStructures();
// Build shader tables, which define shaders and their local root arguments.
BuildShaderTables();
}
void AO::OnSizeChanged()
{
// Recreate output texture.
CreateRaytracingOutputResource();
}
void AO::OnOptionUpdate(std::shared_ptr<Menus> pMenu)
{
// Update shader.
{
m_mappedAOOptionsConstantResource->Map(0, nullptr, reinterpret_cast<void**>(&m_mappedAOOptionsConstantData));
m_mappedAOOptionsConstantData->constants.m_distance = float(pMenu->m_aoDistance.Value());
m_mappedAOOptionsConstantData->constants.m_falloff = float(pMenu->m_aoFalloff.Value());
m_mappedAOOptionsConstantData->constants.m_numSamples = unsigned int(pMenu->m_aoNumSamples.Value());
m_mappedAOOptionsConstantData->constants.m_sampleType = unsigned int(pMenu->m_aoSampleType.Value());
m_mappedAOOptionsConstantResource->Unmap(0, nullptr);
}
// Update samples (must be done since stratified).
{
m_mappedAOConstantResource->Map(0, nullptr, reinterpret_cast<void**>(&m_mappedAOConstantData));
std::unique_ptr<Sampler> sampler;
Sampler::SetSeed(0);
switch (unsigned int(pMenu->m_aoSampleType.Value()))
{
case AOSampleType::Uniform:
sampler = std::unique_ptr<Sampler>{
new StratifiedSampler<UniformHemiSampler>()
};
break;
default: // Assume cosine.
sampler = std::unique_ptr<Sampler>{
new StratifiedSampler<CosineHemiSampler>()