-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRender.cpp
1783 lines (1478 loc) · 71.5 KB
/
Render.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
#include "Render.h"
#include <dxgi1_6.h>
#include <d3dx12.h>
#include <dstorage.h>
#include <winrt/base.h>
#include <dxcapi.h>
#include "imgui.h"
#include "imgui_internal.h" // For PushItemFlag
#include "imgui_impl_win32.h"
#include "imgui_impl_dx12.h"
using winrt::com_ptr;
using winrt::check_hresult;
using winrt::check_bool;
#define PI 3.14159265358979323846f
#define PI_2 (PI * 2.0f)
#define PI_HALF (PI * 0.5f)
#define NUM_QUEUED_FRAMES 3
extern "C" { __declspec(dllexport) extern const UINT D3D12SDKVersion = 715; }
extern "C" { __declspec(dllexport) extern const char8_t* D3D12SDKPath = u8".\\D3D12\\"; }
enum class RenderTargets : int
{
BackBuffer0 = 0,
BackBuffer1,
BackBuffer2,
BackBufferLast = BackBuffer2, // needs to be NUM_QUEUED_FRAMES - 1
VBuffer,
ColorBuffer,
RenderTargetCount,
};
enum class DepthStencilTargets : int
{
MainDepth = 0,
DepthStencilTargetCount,
};
enum BufferFlags
{
BUFFER_FLAG_NONE = 0,
BUFFER_FLAG_RAW = 1,
BUFFER_FLAG_SRV = 2,
BUFFER_FLAG_UAV = 4,
BUFFER_FLAG_UAV_DESCRIPTOR = 8,
BUFFER_FLAG_CBV = 16,
};
struct BufferDesc
{
UINT count;
UINT stride;
UINT flags = BUFFER_FLAG_NONE;
UINT srvDescriptorOffset = UINT_MAX;
UINT uavDescriptorOffset = UINT_MAX;
//UINT cbvDescriptorOffset;
D3D12_HEAP_TYPE heapType = D3D12_HEAP_TYPE_DEFAULT;
D3D12_RESOURCE_STATES resrourceStates = D3D12_RESOURCE_STATE_COMMON;
LPCWSTR name = nullptr;
BufferDesc(UINT _count, UINT _stride) : count(_count), stride(_stride) {}
BufferDesc& WithSRV(UINT offset) { srvDescriptorOffset = offset; flags |= BUFFER_FLAG_SRV; return *this; }
BufferDesc& WithUAV() { flags |= BUFFER_FLAG_UAV; return *this; }
BufferDesc& WithUAV(UINT offset) { uavDescriptorOffset = offset; flags |= BUFFER_FLAG_UAV_DESCRIPTOR; return WithUAV(); }
BufferDesc& WithRAW() { flags |= BUFFER_FLAG_RAW; return *this; }
BufferDesc& WithHeapType(D3D12_HEAP_TYPE _heapType) { heapType = _heapType; return *this; }
BufferDesc& WithResourceState(D3D12_RESOURCE_STATES _resrourceStates) { resrourceStates = _resrourceStates; return *this; }
BufferDesc& WithName(LPCWSTR str) { name = str; return *this; }
};
struct Buffer
{
com_ptr<ID3D12Resource> resource;
D3D12_GPU_VIRTUAL_ADDRESS_RANGE addressRange = { 0, 0 };
D3D12_GPU_VIRTUAL_ADDRESS_RANGE_AND_STRIDE addressRangeAndStride = { 0, 0, 0 };
};
struct WireContainer
{
Buffer vertexBuffer;
float3* vertexPtr;
UINT maxVertices;
UINT numVertices;
void AddWireUnchecked(float3 p0, float3 p1)
{
vertexPtr[numVertices + 0] = p0;
vertexPtr[numVertices + 1] = p1;
numVertices += 2;
}
void AddWire(float3 p0, float3 p1)
{
if (numVertices + 2 > maxVertices)
return;
AddWireUnchecked(p0, p1);
}
void AddAABB(CenterExtentsAABB& aabb)
{
if (numVertices + 24 > maxVertices)
return;
float3 p0 = aabb.Center + float3(-aabb.Extents.x, -aabb.Extents.y, -aabb.Extents.z);
float3 p1 = aabb.Center + float3( aabb.Extents.x, -aabb.Extents.y, -aabb.Extents.z);
float3 p2 = aabb.Center + float3(-aabb.Extents.x, aabb.Extents.y, -aabb.Extents.z);
float3 p3 = aabb.Center + float3( aabb.Extents.x, aabb.Extents.y, -aabb.Extents.z);
float3 p4 = aabb.Center + float3(-aabb.Extents.x, -aabb.Extents.y, aabb.Extents.z);
float3 p5 = aabb.Center + float3( aabb.Extents.x, -aabb.Extents.y, aabb.Extents.z);
float3 p6 = aabb.Center + float3(-aabb.Extents.x, aabb.Extents.y, aabb.Extents.z);
float3 p7 = aabb.Center + float3( aabb.Extents.x, aabb.Extents.y, aabb.Extents.z);
AddWireUnchecked(p0, p1);
AddWireUnchecked(p2, p3);
AddWireUnchecked(p0, p2);
AddWireUnchecked(p1, p3);
AddWireUnchecked(p4, p5);
AddWireUnchecked(p6, p7);
AddWireUnchecked(p4, p6);
AddWireUnchecked(p5, p7);
AddWireUnchecked(p0, p4);
AddWireUnchecked(p1, p5);
AddWireUnchecked(p2, p6);
AddWireUnchecked(p3, p7);
}
void AddFrustum(float4x4& mat)
{
if (numVertices + 24 > maxVertices)
return;
float4 p0nt = transform(float4(-1.0f, -1.0f, 0.0f, 1.0f), mat);
float4 p1nt = transform(float4(1.0f, -1.0f, 0.0f, 1.0f), mat);
float4 p2nt = transform(float4(-1.0f, 1.0f, 0.0f, 1.0f), mat);
float4 p3nt = transform(float4(1.0f, 1.0f, 0.0f, 1.0f), mat);
float4 p0ft = transform(float4(-1.0f, -1.0f, 0.9999f, 1.0f), mat);
float4 p1ft = transform(float4(1.0f, -1.0f, 0.9999f, 1.0f), mat);
float4 p2ft = transform(float4(-1.0f, 1.0f, 0.9999f, 1.0f), mat);
float4 p3ft = transform(float4(1.0f, 1.0f, 0.9999f, 1.0f), mat);
float3 p0n = float3(p0nt.x, p0nt.y, p0nt.z) / p0nt.w;
float3 p1n = float3(p1nt.x, p1nt.y, p1nt.z) / p1nt.w;
float3 p2n = float3(p2nt.x, p2nt.y, p2nt.z) / p2nt.w;
float3 p3n = float3(p3nt.x, p3nt.y, p3nt.z) / p3nt.w;
float3 p0f = float3(p0ft.x, p0ft.y, p0ft.z) / p0ft.w;
float3 p1f = float3(p1ft.x, p1ft.y, p1ft.z) / p1ft.w;
float3 p2f = float3(p2ft.x, p2ft.y, p2ft.z) / p2ft.w;
float3 p3f = float3(p3ft.x, p3ft.y, p3ft.z) / p3ft.w;
AddWireUnchecked(p0n, p1n);
AddWireUnchecked(p2n, p3n);
AddWireUnchecked(p0n, p2n);
AddWireUnchecked(p1n, p3n);
AddWireUnchecked(p0f, p1f);
AddWireUnchecked(p2f, p3f);
AddWireUnchecked(p0f, p2f);
AddWireUnchecked(p1f, p3f);
AddWireUnchecked(p0n, p0f);
AddWireUnchecked(p1n, p1f);
AddWireUnchecked(p2n, p2f);
AddWireUnchecked(p3n, p3f);
}
};
struct Render
{
UINT width = 1280;
UINT height = 720;
HWND hwnd;
bool supportsWorkGraph = false;
com_ptr<IDXGIFactory6> dxgiFactory;
com_ptr<ID3D12Device14> device;
com_ptr<IDStorageFactory> storageFactory;
com_ptr<ID3D12CommandQueue> commandQueue;
com_ptr<IDStorageQueue> storageQueue;
com_ptr<IDXGISwapChain3> swapChain;
UINT frameIndex = 0;
com_ptr<ID3D12DescriptorHeap> rtvHeap;
com_ptr<ID3D12DescriptorHeap> dsvHeap;
com_ptr<ID3D12DescriptorHeap> uniHeap;
UINT rtvDescriptorSize = 0;
UINT dsvDescriptorSize = 0;
UINT uniDescriptorSize = 0;
com_ptr<ID3D12Resource> backBuffers[NUM_QUEUED_FRAMES];
CD3DX12_CPU_DESCRIPTOR_HANDLE backBufferRTVs[NUM_QUEUED_FRAMES];
com_ptr<ID3D12CommandAllocator> commandAllocators[NUM_QUEUED_FRAMES];
com_ptr<ID3D12Resource> vBuffer;
CD3DX12_CPU_DESCRIPTOR_HANDLE vBufferRTV;
com_ptr<ID3D12Resource> colorBuffer;
CD3DX12_CPU_DESCRIPTOR_HANDLE colorBufferRTV;
com_ptr<ID3D12Resource> depthStencil;
CD3DX12_CPU_DESCRIPTOR_HANDLE depthStencilDSV;
UINT numInstances = 0;
UINT numClusters = 0;
UINT maxNumClusters = 0;
Constants constantBufferData;
Buffer constantBuffer;
Buffer instancesBuffer;
Buffer meshesBuffer;
Buffer clustersBuffer;
Buffer positionsBuffer;
Buffer normalsBuffer;
Buffer tangentsBuffer;
Buffer texcoordsBuffer;
Buffer indexDataBuffer;
Buffer materialsBuffer;
Buffer workGraphBuffer;
Buffer workGraphBackingMemory;
Buffer workGraphNodeLocalRootArgumentsTable;
char* cbvDataBegin = nullptr;
Instance* instancesCpu = nullptr;
Mesh* meshesCpu = nullptr;
Cluster* clustersCpu = nullptr;
Buffer visibleInstances;
Buffer visibleClusters;
Buffer visibleInstancesCounter;
Buffer visibleClustersCounter;
Buffer readbackBuffer;
com_ptr<ID3D12RootSignature> drawRootSignature;
com_ptr<ID3D12RootSignature> drawWireRootSignature;
com_ptr<ID3D12PipelineState> drawMeshPSO;
com_ptr<ID3D12PipelineState> drawWirePSO;
com_ptr<ID3D12PipelineState> frameSetupPSO;
com_ptr<ID3D12PipelineState> instanceCullingPSO;
com_ptr<ID3D12PipelineState> clusterCullingPSO;
com_ptr<ID3D12PipelineState> materialPSO;
com_ptr<ID3D12StateObject> workGraphSO;
D3D12_PROGRAM_IDENTIFIER workGraphIdentifier;
com_ptr<ID3D12GraphicsCommandList10> commandList;
com_ptr<ID3D12Fence1> fence;
UINT64 fenceValues[NUM_QUEUED_FRAMES] = { 0};
HANDLE fenceEvent;
com_ptr<ID3D12CommandSignature> commandSignature;
CD3DX12_VIEWPORT viewport;
CD3DX12_RECT scissorRect;
struct Camera
{
float4x4 viewMat;
float4x4 projMat;
float pitch;
float yaw;
float3 pos;
} cullingCamera, drawingCamera;
double lastTime = 0.0;
WireContainer wireContainer[NUM_QUEUED_FRAMES];
int displayMode = DEBUG_MODE_NONE;
com_ptr<ID3DBlob> vBufferBlobMS;
com_ptr<ID3DBlob> vBufferBlobPS;
com_ptr<ID3DBlob> wireBlobVS;
com_ptr<ID3DBlob> wireBlobPS;
com_ptr<ID3DBlob> frameSetupBlobCS;
com_ptr<ID3DBlob> instanceCullingBlobCS;
com_ptr<ID3DBlob> clusterCullingBlobCS;
com_ptr<ID3DBlob> materialBlobCS;
com_ptr<ID3DBlob> workGraphBlob;
com_ptr<ID3D12RootSignature> globalRootSignature;
bool recreateResources = true;
bool visualizeInstances = false;
bool visualizeClusters = false;
bool fastMove = false;
bool lockedCullingCamera = false;
bool workGraph = false;
};
struct handle_closer
{
void operator()(HANDLE h) noexcept
{
assert(h != INVALID_HANDLE_VALUE);
if (h)
{
CloseHandle(h);
}
}
};
using ScopedHandle = std::unique_ptr<void, handle_closer>;
static void GetHardwareAdapter(IDXGIFactory6* factory, IDXGIAdapter4** ppAdapter)
{
*ppAdapter = nullptr;
com_ptr<IDXGIAdapter4> adapter;
for(UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != factory->EnumAdapterByGpuPreference(adapterIndex, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE, IID_PPV_ARGS(&adapter)); ++adapterIndex)
{
DXGI_ADAPTER_DESC1 desc;
adapter->GetDesc1(&desc);
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
continue;
if (SUCCEEDED(D3D12CreateDevice(adapter.get(), D3D_FEATURE_LEVEL_12_1, _uuidof(ID3D12Device), nullptr)))
break;
}
*ppAdapter = adapter.detach();
}
static HRESULT ReadDataFromFile(LPCWSTR filename, byte** data, UINT* size)
{
CREATEFILE2_EXTENDED_PARAMETERS extendedParams = {};
extendedParams.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
extendedParams.dwFileFlags = FILE_FLAG_SEQUENTIAL_SCAN;
extendedParams.dwSecurityQosFlags = SECURITY_ANONYMOUS;
extendedParams.lpSecurityAttributes = nullptr;
extendedParams.hTemplateFile = nullptr;
ScopedHandle file(CreateFile2(filename, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, &extendedParams));
if (file.get() == INVALID_HANDLE_VALUE)
{
throw std::exception();
}
FILE_STANDARD_INFO fileInfo = {};
if (!GetFileInformationByHandleEx(file.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo)))
{
throw std::exception();
}
if (fileInfo.EndOfFile.HighPart != 0)
{
throw std::exception();
}
*data = reinterpret_cast<byte*>(malloc(fileInfo.EndOfFile.LowPart));
*size = fileInfo.EndOfFile.LowPart;
if (!ReadFile(file.get(), *data, fileInfo.EndOfFile.LowPart, nullptr, nullptr))
{
throw std::exception();
}
return S_OK;
}
static com_ptr<ID3DBlob> CompileShader(Render* render, LPCWSTR entry_name, LPCWSTR shader_name, LPCWSTR target)
{
struct
{
byte* data;
uint32_t size;
} shaderSource;
ReadDataFromFile(shader_name, &shaderSource.data, &shaderSource.size);
com_ptr<IDxcCompiler3> compiler;
check_hresult(DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&compiler)));
com_ptr<IDxcUtils> utils;
check_hresult(DxcCreateInstance(CLSID_DxcUtils, IID_PPV_ARGS(&utils)));
com_ptr<IDxcBlobEncoding> source;
check_hresult(utils->CreateBlob(shaderSource.data, shaderSource.size, CP_UTF8, source.put()));
std::vector<LPCWSTR> arguments;
arguments.push_back(L"-T");
arguments.push_back(target);
if (entry_name)
{
arguments.push_back(L"-E");
arguments.push_back(entry_name);
}
arguments.push_back(L"-I");
arguments.push_back(L"C:/Code/DSTest/");
DxcBuffer sourceBuffer;
sourceBuffer.Ptr = source->GetBufferPointer();
sourceBuffer.Size = source->GetBufferSize();
sourceBuffer.Encoding = 0;
com_ptr<IDxcIncludeHandler> includeHandler;
utils->CreateDefaultIncludeHandler(includeHandler.put());
com_ptr<IDxcResult> compileResult;
winrt::hresult result = check_hresult(compiler->Compile(&sourceBuffer, arguments.data(), arguments.size(), includeHandler.get(), IID_PPV_ARGS(&compileResult)));
// Error Handling. Note that this will also include warnings unless disabled.
com_ptr<IDxcBlobUtf8> errors;
compileResult->GetOutput(DXC_OUT_ERRORS, IID_PPV_ARGS(&errors), nullptr);
if (errors && errors->GetStringLength() > 0)
{
const char* text = (char*)errors->GetBufferPointer();
OutputDebugStringA(text);
}
if (result != S_OK)
{
__debugbreak();
}
com_ptr<ID3DBlob> object;
compileResult->GetOutput(DXC_OUT_OBJECT, IID_PPV_ARGS(&object), nullptr);
return object;
}
static bool CompileShaders(Render* render)
{
render->vBufferBlobMS = CompileShader(render, L"main", L"VBufferMS.hlsl", L"ms_6_6");
render->vBufferBlobPS = CompileShader(render, L"main", L"VBufferPS.hlsl", L"ps_6_6");
render->wireBlobVS = CompileShader(render, L"main", L"WireVS.hlsl", L"vs_6_6");
render->wireBlobPS = CompileShader(render, L"main", L"WirePS.hlsl", L"ps_6_6");
render->frameSetupBlobCS = CompileShader(render, L"main", L"FrameSetup.hlsl", L"cs_6_6");
render->instanceCullingBlobCS = CompileShader(render, L"main", L"InstanceCulling.hlsl", L"cs_6_6");
render->clusterCullingBlobCS = CompileShader(render, L"main", L"ClusterCulling.hlsl", L"cs_6_6");
render->materialBlobCS = CompileShader(render, L"main", L"material.hlsl", L"cs_6_6");
render->workGraphBlob = CompileShader(render, nullptr, L"WorkGraph.hlsl", L"lib_6_9");
return true;
}
static com_ptr<ID3D12DescriptorHeap> CreateDescriptorHeap(Render* render, D3D12_DESCRIPTOR_HEAP_TYPE type, UINT count, D3D12_DESCRIPTOR_HEAP_FLAGS flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE)
{
D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
heapDesc.NumDescriptors = count;
heapDesc.Type = type;
heapDesc.Flags = flags;
com_ptr<ID3D12DescriptorHeap> heap;
check_hresult(render->device->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&heap)));
return heap;
}
static void CreateBuffer(Render* render, Buffer* out_buffer, const BufferDesc& desc)
{
UINT size = desc.stride * desc.count;
auto resourceFlags = D3D12_RESOURCE_FLAG_NONE;
bool isRaw = desc.flags & BUFFER_FLAG_RAW;
bool isUAV = desc.flags & BUFFER_FLAG_UAV;
if (isUAV)
resourceFlags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
auto heapProps = CD3DX12_HEAP_PROPERTIES(desc.heapType);
auto resourceDesc = CD3DX12_RESOURCE_DESC::Buffer(size, resourceFlags);
check_hresult(render->device->CreateCommittedResource(
&heapProps,
D3D12_HEAP_FLAG_NONE,
&resourceDesc,
D3D12_RESOURCE_STATE_COMMON,
nullptr,
IID_PPV_ARGS(out_buffer->resource.put())));
out_buffer->resource->SetName(desc.name);
if (desc.flags & BUFFER_FLAG_SRV)
{
CD3DX12_CPU_DESCRIPTOR_HANDLE handle = CD3DX12_CPU_DESCRIPTOR_HANDLE(render->uniHeap->GetCPUDescriptorHandleForHeapStart(), desc.srvDescriptorOffset, render->uniDescriptorSize);
DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
D3D12_BUFFER_SRV_FLAGS srvFlags = D3D12_BUFFER_SRV_FLAG_NONE;
if (isRaw)
{
format = DXGI_FORMAT_R32_TYPELESS;
srvFlags = D3D12_BUFFER_SRV_FLAG_RAW;
}
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Format = format;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Buffer.FirstElement = 0;
srvDesc.Buffer.NumElements = isRaw ? (size / 4) : desc.count;
srvDesc.Buffer.StructureByteStride = isRaw ? 0 : desc.stride;
srvDesc.Buffer.Flags = srvFlags;
render->device->CreateShaderResourceView(out_buffer->resource.get(), &srvDesc, handle);
}
if (desc.flags & BUFFER_FLAG_UAV_DESCRIPTOR)
{
CD3DX12_CPU_DESCRIPTOR_HANDLE handle = CD3DX12_CPU_DESCRIPTOR_HANDLE(render->uniHeap->GetCPUDescriptorHandleForHeapStart(), desc.uavDescriptorOffset, render->uniDescriptorSize);
DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN;
D3D12_BUFFER_UAV_FLAGS uavFlags = D3D12_BUFFER_UAV_FLAG_NONE;
if (isRaw)
{
format = DXGI_FORMAT_R32_TYPELESS;
uavFlags = D3D12_BUFFER_UAV_FLAG_RAW;
}
D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
uavDesc.Format = format;
uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
uavDesc.Buffer.FirstElement = 0;
uavDesc.Buffer.NumElements = isRaw ? (size / 4) : desc.count;
uavDesc.Buffer.StructureByteStride = isRaw ? 0 : desc.stride;
uavDesc.Buffer.Flags = uavFlags;
render->device->CreateUnorderedAccessView(out_buffer->resource.get(), nullptr, &uavDesc, handle);
}
out_buffer->addressRange.SizeInBytes = size;
out_buffer->addressRange.StartAddress = out_buffer->resource->GetGPUVirtualAddress();
out_buffer->addressRangeAndStride.SizeInBytes = size;
out_buffer->addressRangeAndStride.StartAddress = out_buffer->resource->GetGPUVirtualAddress();
out_buffer->addressRangeAndStride.StrideInBytes = desc.stride;
}
static void OpenFileForLoading(Render* render, LPCWSTR fileName, com_ptr<IDStorageFile>& file, UINT32& size)
{
check_hresult(render->storageFactory->OpenFile(fileName, IID_PPV_ARGS(file.put())));
BY_HANDLE_FILE_INFORMATION info = {};
check_hresult(file->GetFileInformation(&info));
size = info.nFileSizeLow;
}
static void LoadFileToGPU(Render* render, const com_ptr<IDStorageFile>& file, ID3D12Resource* resource, UINT32 size)
{
DSTORAGE_REQUEST request = {};
request.Options.SourceType = DSTORAGE_REQUEST_SOURCE_FILE;
request.Options.DestinationType = DSTORAGE_REQUEST_DESTINATION_BUFFER;
request.Options.CompressionFormat = DSTORAGE_COMPRESSION_FORMAT_NONE;
request.Source.File.Source = file.get();
request.Source.File.Offset = 0;
request.Source.File.Size = size;
request.UncompressedSize = size;
request.Destination.Buffer.Resource = resource;
request.Destination.Buffer.Offset = 0;
request.Destination.Buffer.Size = size;
render->storageQueue->EnqueueRequest(&request);
}
static void LoadFileToCPU(Render* render, const com_ptr<IDStorageFile>& file, void* buffer, UINT32 size)
{
DSTORAGE_REQUEST request = {};
request.Options.SourceType = DSTORAGE_REQUEST_SOURCE_FILE;
request.Options.DestinationType = DSTORAGE_REQUEST_DESTINATION_MEMORY;
request.Options.CompressionFormat = DSTORAGE_COMPRESSION_FORMAT_NONE;
request.Source.File.Source = file.get();
request.Source.File.Offset = 0;
request.Source.File.Size = size;
request.UncompressedSize = size;
request.Destination.Memory.Buffer = buffer;
request.Destination.Memory.Size = size;
render->storageQueue->EnqueueRequest(&request);
}
Render* CreateRender(UINT width, UINT height)
{
Render* render = new Render;
render->width = width;
render->height = height;
return render;
}
void Destroy(Render* render)
{
ImGui_ImplDX12_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
free(render->instancesCpu);
free(render->meshesCpu);
free(render->clustersCpu);
delete render;
}
void Initialize(Render* render, HWND hwnd, bool useWarp)
{
render->hwnd = hwnd;
UUID GPUWorkGraphExperimentalFeatures[2] = { D3D12ExperimentalShaderModels, D3D12StateObjectsExperiment };
check_hresult(D3D12EnableExperimentalFeatures(_countof(GPUWorkGraphExperimentalFeatures), GPUWorkGraphExperimentalFeatures, nullptr, nullptr));
UINT dxgiFactoryFlags = 0;
#if defined(_DEBUG)
{
com_ptr<ID3D12Debug> debugController;
HRESULT hr = D3D12GetDebugInterface(IID_PPV_ARGS(debugController.put()));
if (SUCCEEDED(hr))
{
debugController->EnableDebugLayer();
dxgiFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
com_ptr<ID3D12Debug1> debugController1;
hr = debugController->QueryInterface(IID_PPV_ARGS(debugController1.put()));
if (SUCCEEDED(hr))
{
debugController1->SetEnableGPUBasedValidation(true);
}
}
}
#endif
{
com_ptr<IDXGIFactory4> initialFactory;
check_hresult(CreateDXGIFactory2(dxgiFactoryFlags, IID_PPV_ARGS(&initialFactory)));
check_hresult(initialFactory->QueryInterface(IID_PPV_ARGS(render->dxgiFactory.put())));
}
if (useWarp)
{
com_ptr<IDXGIAdapter4> warpAdapter;
check_hresult(render->dxgiFactory->EnumWarpAdapter(IID_PPV_ARGS(&warpAdapter)));
check_hresult(D3D12CreateDevice(warpAdapter.get(), D3D_FEATURE_LEVEL_12_1, IID_PPV_ARGS(render->device.put())));
}
else
{
com_ptr<IDXGIAdapter4> hardwareAdapter;
GetHardwareAdapter(render->dxgiFactory.get(), hardwareAdapter.put());
check_hresult(D3D12CreateDevice(hardwareAdapter.get(), D3D_FEATURE_LEVEL_12_2, IID_PPV_ARGS(render->device.put())));
}
D3D12_FEATURE_DATA_D3D12_OPTIONS21 Options = {};
render->device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS21, &Options, sizeof(Options));
render->supportsWorkGraph = Options.WorkGraphsTier != D3D12_WORK_GRAPHS_TIER_NOT_SUPPORTED;
{
DSTORAGE_CONFIGURATION dsConfig{};
DStorageSetConfiguration(&dsConfig);
check_hresult(DStorageGetFactory(IID_PPV_ARGS(render->storageFactory.put())));
render->storageFactory->SetDebugFlags(DSTORAGE_DEBUG_BREAK_ON_ERROR | DSTORAGE_DEBUG_SHOW_ERRORS);
render->storageFactory->SetStagingBufferSize(128 * 1024 * 1024);
DSTORAGE_QUEUE_DESC queueDesc = {};
queueDesc.Capacity = DSTORAGE_MAX_QUEUE_CAPACITY;
queueDesc.Priority = DSTORAGE_PRIORITY_NORMAL;
queueDesc.SourceType = DSTORAGE_REQUEST_SOURCE_FILE;
queueDesc.Device = render->device.get();
check_hresult(render->storageFactory->CreateQueue(&queueDesc, IID_PPV_ARGS(render->storageQueue.put())));
}
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
check_hresult(render->device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&render->commandQueue)));
DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {};
swapChainDesc.BufferCount = NUM_QUEUED_FRAMES;
swapChainDesc.Width = render->width;
swapChainDesc.Height = render->height;
swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
swapChainDesc.SampleDesc.Count = 1;
com_ptr<IDXGISwapChain1> swapChain1;
check_hresult(render->dxgiFactory->CreateSwapChainForHwnd(
render->commandQueue.get(),
hwnd,
&swapChainDesc,
nullptr,
nullptr,
swapChain1.put()
));
check_hresult(swapChain1.as(IID_PPV_ARGS(render->swapChain.put())));
check_hresult(render->dxgiFactory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER));
render->frameIndex = render->swapChain->GetCurrentBackBufferIndex();
// Command allocators
for (UINT n = 0; n < NUM_QUEUED_FRAMES; n++)
{
check_hresult(render->device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(render->commandAllocators[n].put())));
render->commandAllocators[n]->SetName(L"Main CommandAllocator");
}
render->rtvHeap = CreateDescriptorHeap(render, D3D12_DESCRIPTOR_HEAP_TYPE_RTV, (int)RenderTargets::RenderTargetCount);
render->dsvHeap = CreateDescriptorHeap(render, D3D12_DESCRIPTOR_HEAP_TYPE_DSV, (int)DepthStencilTargets::DepthStencilTargetCount);
render->uniHeap = CreateDescriptorHeap(render, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1000000, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE);
render->rtvDescriptorSize = render->device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
render->dsvDescriptorSize = render->device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
render->uniDescriptorSize = render->device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
// Back buffers
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvBaseHandle(render->rtvHeap->GetCPUDescriptorHandleForHeapStart());
for (UINT n = 0; n < NUM_QUEUED_FRAMES; n++)
{
check_hresult(render->swapChain->GetBuffer(n, IID_PPV_ARGS(render->backBuffers[n].put())));
render->backBufferRTVs[n] = CD3DX12_CPU_DESCRIPTOR_HANDLE(rtvBaseHandle, (int)RenderTargets::BackBuffer0 + n, render->rtvDescriptorSize);
render->device->CreateRenderTargetView(render->backBuffers[n].get(), nullptr, render->backBufferRTVs[n]);
}
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Platform/Renderer backends
ImGui_ImplWin32_Init(render->hwnd);
CD3DX12_CPU_DESCRIPTOR_HANDLE cpuHandle = CD3DX12_CPU_DESCRIPTOR_HANDLE(render->uniHeap->GetCPUDescriptorHandleForHeapStart(), IMGUI_FONT_SRV, render->uniDescriptorSize);
CD3DX12_GPU_DESCRIPTOR_HANDLE gpuHandle = CD3DX12_GPU_DESCRIPTOR_HANDLE(render->uniHeap->GetGPUDescriptorHandleForHeapStart(), IMGUI_FONT_SRV, render->uniDescriptorSize);
ImGui_ImplDX12_Init(render->device.get(), NUM_QUEUED_FRAMES,
DXGI_FORMAT_R8G8B8A8_UNORM, render->uniHeap.get(),
cpuHandle,
gpuHandle);
}
static void WaitStorageIdle(Render* render)
{
com_ptr<ID3D12Fence> fence;
check_hresult(render->device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(fence.put())));
ScopedHandle fenceEvent(CreateEvent(nullptr, FALSE, FALSE, nullptr));
constexpr uint64_t fenceValue = 1;
check_hresult(fence->SetEventOnCompletion(fenceValue, fenceEvent.get()));
render->storageQueue->EnqueueSignal(fence.get(), fenceValue);
render->storageQueue->Submit();
WaitForSingleObject(fenceEvent.get(), INFINITE);
}
static void WaitGraphicsIdle(Render* render)
{
com_ptr<ID3D12Fence> fence;
check_hresult(render->device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(fence.put())));
ScopedHandle fenceEvent(CreateEvent(nullptr, FALSE, FALSE, nullptr));
constexpr uint64_t fenceValue = 1;
check_hresult(fence->SetEventOnCompletion(fenceValue, fenceEvent.get()));
render->commandQueue->Signal(fence.get(), fenceValue);
WaitForSingleObject(fenceEvent.get(), INFINITE);
}
static void RecreateResources(Render* render) {
/*
* Render Targets
*/
{
CD3DX12_CPU_DESCRIPTOR_HANDLE rtvBaseHandle(render->rtvHeap->GetCPUDescriptorHandleForHeapStart());
// VBuffer
{
CD3DX12_HEAP_PROPERTIES heapType(D3D12_HEAP_TYPE_DEFAULT);
D3D12_CLEAR_VALUE vBufferOptimizedClearValue = {};
vBufferOptimizedClearValue.Format = DXGI_FORMAT_R32_UINT;
vBufferOptimizedClearValue.Color[0] = 0.0f;
vBufferOptimizedClearValue.Color[1] = 0.0f;
vBufferOptimizedClearValue.Color[2] = 0.0f;
vBufferOptimizedClearValue.Color[3] = 0.0f;
auto desc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R32_UINT, render->width, render->height, 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
check_hresult(render->device->CreateCommittedResource(
&heapType,
D3D12_HEAP_FLAG_NONE,
&desc,
D3D12_RESOURCE_STATE_GENERIC_READ,
&vBufferOptimizedClearValue,
IID_PPV_ARGS(render->vBuffer.put())
));
render->vBufferRTV = CD3DX12_CPU_DESCRIPTOR_HANDLE(rtvBaseHandle, (int)RenderTargets::VBuffer, render->rtvDescriptorSize);
render->device->CreateRenderTargetView(render->vBuffer.get(), nullptr, render->vBufferRTV);
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Format = DXGI_FORMAT_R32_UINT;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Texture2D.MipLevels = 1;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.PlaneSlice = 0;
srvDesc.Texture2D.ResourceMinLODClamp = 0.0f;
render->device->CreateShaderResourceView(render->vBuffer.get(), &srvDesc, CD3DX12_CPU_DESCRIPTOR_HANDLE(render->uniHeap->GetCPUDescriptorHandleForHeapStart(), VBUFFER_SRV, render->uniDescriptorSize));
}
// Color buffer
{
CD3DX12_HEAP_PROPERTIES heapType(D3D12_HEAP_TYPE_DEFAULT);
auto desc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R8G8B8A8_UNORM, render->width, render->height, 1, 1, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
check_hresult(render->device->CreateCommittedResource(
&heapType,
D3D12_HEAP_FLAG_NONE,
&desc,
D3D12_RESOURCE_STATE_COPY_SOURCE,
nullptr,
IID_PPV_ARGS(render->colorBuffer.put())
));
render->colorBufferRTV = CD3DX12_CPU_DESCRIPTOR_HANDLE(rtvBaseHandle, (int)RenderTargets::ColorBuffer, render->rtvDescriptorSize);
render->device->CreateRenderTargetView(render->colorBuffer.get(), nullptr, render->colorBufferRTV);
D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
uavDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
uavDesc.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
uavDesc.Texture2D.PlaneSlice = 0;
uavDesc.Texture2D.MipSlice = 0;
render->device->CreateUnorderedAccessView(render->colorBuffer.get(), nullptr, &uavDesc, CD3DX12_CPU_DESCRIPTOR_HANDLE(render->uniHeap->GetCPUDescriptorHandleForHeapStart(), COLORBUFFER_UAV, render->uniDescriptorSize));
}
}
/*
* DepthStencil Targets
*/
{
D3D12_DEPTH_STENCIL_VIEW_DESC depthStencilDesc = {};
depthStencilDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthStencilDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
depthStencilDesc.Flags = D3D12_DSV_FLAG_NONE;
D3D12_CLEAR_VALUE depthOptimizedClearValue = {};
depthOptimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT;
depthOptimizedClearValue.DepthStencil.Depth = 1.0f;
depthOptimizedClearValue.DepthStencil.Stencil = 0;
CD3DX12_HEAP_PROPERTIES heapType(D3D12_HEAP_TYPE_DEFAULT);
auto desc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D32_FLOAT, render->width, render->height, 1, 0, 1, 0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
check_hresult(render->device->CreateCommittedResource(
&heapType,
D3D12_HEAP_FLAG_NONE,
&desc,
D3D12_RESOURCE_STATE_DEPTH_WRITE,
&depthOptimizedClearValue,
IID_PPV_ARGS(render->depthStencil.put())
));
CD3DX12_CPU_DESCRIPTOR_HANDLE dsvBaseHandle(render->dsvHeap->GetCPUDescriptorHandleForHeapStart());
render->depthStencilDSV = CD3DX12_CPU_DESCRIPTOR_HANDLE(dsvBaseHandle, (int)DepthStencilTargets::MainDepth, render->dsvDescriptorSize);
render->device->CreateDepthStencilView(render->depthStencil.get(), &depthStencilDesc, render->depthStencilDSV);
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.Format = DXGI_FORMAT_R32_FLOAT;
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Texture2D.MipLevels = 1;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.PlaneSlice = 0;
srvDesc.Texture2D.ResourceMinLODClamp = 0.0f;
CD3DX12_CPU_DESCRIPTOR_HANDLE srvBaseHandle(render->uniHeap->GetCPUDescriptorHandleForHeapStart());
CD3DX12_CPU_DESCRIPTOR_HANDLE srvHandle = CD3DX12_CPU_DESCRIPTOR_HANDLE(srvBaseHandle, DEPTHBUFFER_SRV, render->uniDescriptorSize);
render->device->CreateShaderResourceView(render->depthStencil.get(), &srvDesc, srvHandle);
}
/*
* Global Constant Buffer
*/
{
const UINT64 constantBufferSize = sizeof(Constants) * NUM_QUEUED_FRAMES;
CreateBuffer(render, &render->constantBuffer,
BufferDesc(NUM_QUEUED_FRAMES, sizeof(Constants))
.WithName(L"ConstantBuffer")
.WithHeapType(D3D12_HEAP_TYPE_UPLOAD)
.WithResourceState(D3D12_RESOURCE_STATE_GENERIC_READ));
CD3DX12_RANGE readRange(0, 0);
check_hresult(render->constantBuffer.resource->Map(0, &readRange, reinterpret_cast<void**>(&render->cbvDataBegin)));
}
/*
* Open files to query sizes
*/
com_ptr<IDStorageFile> instancesFile;
com_ptr<IDStorageFile> meshesFile;
com_ptr<IDStorageFile> clustersFile;
com_ptr<IDStorageFile> positionsFile;
com_ptr<IDStorageFile> normalsFile;
com_ptr<IDStorageFile> tangentsFile;
com_ptr<IDStorageFile> texcoordsFile;
com_ptr<IDStorageFile> indicesFile;
com_ptr<IDStorageFile> materialsFile;
UINT32 instancesSize = 0;
UINT32 meshesSize = 0;
UINT32 clustersSize = 0;
UINT32 positionsSize = 0;
UINT32 normalsSize = 0;
UINT32 tangentsSize = 0;
UINT32 texcoordsSize = 0;
UINT32 indicesSize = 0;
UINT32 materialsSize = 0;
OpenFileForLoading(render, L"instances.raw", instancesFile, instancesSize);
OpenFileForLoading(render, L"meshes.raw", meshesFile, meshesSize);
OpenFileForLoading(render, L"clusters.raw", clustersFile, clustersSize);
OpenFileForLoading(render, L"positions.raw", positionsFile, positionsSize);
OpenFileForLoading(render, L"normals.raw", normalsFile, normalsSize);
OpenFileForLoading(render, L"tangents.raw", tangentsFile, tangentsSize);
OpenFileForLoading(render, L"texcoords.raw", texcoordsFile, texcoordsSize);
OpenFileForLoading(render, L"indices.raw", indicesFile, indicesSize);
OpenFileForLoading(render, L"materials.raw", materialsFile, materialsSize);
render->numInstances = instancesSize / sizeof(Instance);
render->numClusters = clustersSize / sizeof(Cluster);
render->maxNumClusters = MAX_ELEMENTS;
assert(render->numInstances <= MAX_ELEMENTS);
assert(render->numClusters <= MAX_ELEMENTS);
/*
* Mesh and Instance Pool
*/
CreateBuffer(render, &render->instancesBuffer,
BufferDesc(instancesSize / sizeof(Instance), sizeof(Instance))
.WithName(L"InstancesBuffer")
.WithSRV(INSTANCE_BUFFER_SRV));
CreateBuffer(render, &render->meshesBuffer,
BufferDesc(meshesSize / sizeof(Mesh), sizeof(Mesh))
.WithName(L"MeshesBuffer")
.WithSRV(MESH_BUFFER_SRV));
CreateBuffer(render, &render->clustersBuffer,
BufferDesc(clustersSize / sizeof(Cluster), sizeof(Cluster))
.WithName(L"ClustersBuffer")
.WithSRV(CLUSTER_BUFFER_SRV));
CreateBuffer(render, &render->positionsBuffer,
BufferDesc(positionsSize / sizeof(float3), sizeof(float3))
.WithName(L"PositionsBuffer")
.WithSRV(POSITION_DATA_BUFFER_SRV)
.WithRAW());
CreateBuffer(render, &render->normalsBuffer,
BufferDesc(normalsSize / sizeof(float3), sizeof(float3))
.WithName(L"NormalsBuffer")
.WithSRV(NORMAL_DATA_BUFFER_SRV)
.WithRAW());
CreateBuffer(render, &render->tangentsBuffer,
BufferDesc(tangentsSize / sizeof(float4), sizeof(float4))
.WithName(L"TangentsBuffer")
.WithSRV(TANGENT_DATA_BUFFER_SRV)
.WithRAW());
CreateBuffer(render, &render->texcoordsBuffer,
BufferDesc(texcoordsSize / sizeof(float2), sizeof(float2))
.WithName(L"TexcoordsBuffer")
.WithSRV(TEXCOORD_DATA_BUFFER_SRV)
.WithRAW());
CreateBuffer(render, &render->indexDataBuffer,
BufferDesc(indicesSize / sizeof(UINT), sizeof(UINT))
.WithName(L"IndexDataBuffer")
.WithSRV(INDEX_DATA_BUFFER_SRV)
.WithRAW());
CreateBuffer(render, &render->materialsBuffer,
BufferDesc(materialsSize / sizeof(Material), sizeof(Material))
.WithName(L"MaterialsBuffer")
.WithSRV(MATERIAL_BUFFER_SRV));
CreateBuffer(render, &render->workGraphBuffer,
BufferDesc(16 * 1024 * 1024 / sizeof(uint), sizeof(uint))
.WithName(L"WorkGraphBuffer")
.WithUAV(WORK_GRAPH_UAV)
.WithRAW());
/*
* Intermediate buffers
*/
CreateBuffer(render, &render->visibleInstances,
BufferDesc(render->numInstances, sizeof(UINT))
.WithName(L"VisibleInstancesBuffer")
.WithSRV(VISIBLE_INSTANCES_SRV)
.WithUAV(VISIBLE_INSTANCES_UAV)
.WithRAW());
CreateBuffer(render, &render->visibleClusters,
BufferDesc(render->maxNumClusters, sizeof(UINT))