-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathTests.cpp
4365 lines (3702 loc) · 167 KB
/
Tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2019-2025 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#include "Common.h"
#include "Tests.h"
#include <thread>
// Define to the same value as you did for D3D12MemAlloc.cpp.
#ifndef D3D12MA_DEBUG_MARGIN
#define D3D12MA_DEBUG_MARGIN 0
#endif
extern ID3D12GraphicsCommandList* BeginCommandList();
extern DXGI_ADAPTER_DESC1 g_AdapterDesc;
extern void EndCommandList(ID3D12GraphicsCommandList* cmdList);
enum CONFIG_TYPE
{
CONFIG_TYPE_MINIMUM,
CONFIG_TYPE_SMALL,
CONFIG_TYPE_AVERAGE,
CONFIG_TYPE_LARGE,
CONFIG_TYPE_MAXIMUM,
CONFIG_TYPE_COUNT
};
enum class FREE_ORDER { FORWARD, BACKWARD, RANDOM, COUNT };
static const char* CODE_DESCRIPTION = "D3D12MA Tests";
static constexpr UINT64 KILOBYTE = 1024;
static constexpr UINT64 MEGABYTE = 1024 * KILOBYTE;
static constexpr CONFIG_TYPE ConfigType = CONFIG_TYPE_AVERAGE;
static const char* FREE_ORDER_NAMES[] = { "FORWARD", "BACKWARD", "RANDOM", };
// Indexes match enum D3D12_HEAP_TYPE.
static const WCHAR* const HEAP_TYPE_NAMES[] =
{
L"",
L"DEFAULT",
L"UPLOAD",
L"READBACK",
L"CUSTOM",
L"GPU_UPLOAD",
};
bool operator==(const D3D12MA::Statistics& lhs, const D3D12MA::Statistics& rhs)
{
return lhs.BlockCount == rhs.BlockCount &&
lhs.AllocationCount == rhs.AllocationCount &&
lhs.BlockBytes == rhs.BlockBytes &&
lhs.AllocationBytes == rhs.AllocationBytes;
}
static void CurrentTimeToStr(std::string& out)
{
time_t rawTime; time(&rawTime);
struct tm timeInfo; localtime_s(&timeInfo, &rawTime);
char timeStr[128];
strftime(timeStr, _countof(timeStr), "%c", &timeInfo);
out = timeStr;
}
static float ToFloatSeconds(duration d)
{
return std::chrono::duration_cast<std::chrono::duration<float>>(d).count();
}
static const char* AlgorithmToStr(D3D12MA::POOL_FLAGS algorithm)
{
switch (algorithm)
{
case D3D12MA::POOL_FLAG_ALGORITHM_LINEAR:
return "Linear";
case 0:
return "TLSF";
default:
assert(0);
return "";
}
}
static const char* VirtualAlgorithmToStr(D3D12MA::VIRTUAL_BLOCK_FLAGS algorithm)
{
switch (algorithm)
{
case D3D12MA::VIRTUAL_BLOCK_FLAG_ALGORITHM_LINEAR:
return "Linear";
case 0:
return "TLSF";
default:
assert(0);
return "";
}
}
static const wchar_t* DefragmentationAlgorithmToStr(UINT32 algorithm)
{
switch (algorithm)
{
case D3D12MA::DEFRAGMENTATION_FLAG_ALGORITHM_BALANCED:
return L"Balanced";
case D3D12MA::DEFRAGMENTATION_FLAG_ALGORITHM_FAST:
return L"Fast";
case D3D12MA::DEFRAGMENTATION_FLAG_ALGORITHM_FULL:
return L"Full";
case 0:
return L"Default";
default:
assert(0);
return L"";
}
}
struct ResourceWithAllocation
{
ComPtr<ID3D12Resource> resource;
ComPtr<D3D12MA::Allocation> allocation;
UINT64 size = UINT64_MAX;
UINT dataSeed = 0;
void Reset()
{
resource.Reset();
allocation.Reset();
size = UINT64_MAX;
dataSeed = 0;
}
};
template<typename D3D12_RESOURCE_DESC_T>
static void FillResourceDescForBuffer(D3D12_RESOURCE_DESC_T& outResourceDesc, UINT64 size)
{
outResourceDesc = {};
outResourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
outResourceDesc.Alignment = 0;
outResourceDesc.Width = size;
outResourceDesc.Height = 1;
outResourceDesc.DepthOrArraySize = 1;
outResourceDesc.MipLevels = 1;
outResourceDesc.Format = DXGI_FORMAT_UNKNOWN;
outResourceDesc.SampleDesc.Count = 1;
outResourceDesc.SampleDesc.Quality = 0;
outResourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
outResourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
}
static void FillData(void* outPtr, const UINT64 sizeInBytes, UINT seed)
{
UINT* outValues = (UINT*)outPtr;
const UINT64 sizeInValues = sizeInBytes / sizeof(UINT);
UINT value = seed;
for(UINT i = 0; i < sizeInValues; ++i)
{
outValues[i] = value++;
}
}
static void FillAllocationsData(const ComPtr<D3D12MA::Allocation>* allocs, size_t allocCount, UINT seed)
{
std::for_each(allocs, allocs + allocCount, [seed](const ComPtr<D3D12MA::Allocation>& alloc)
{
D3D12_RANGE range = {};
void* ptr;
CHECK_HR(alloc->GetResource()->Map(0, &range, &ptr));
FillData(ptr, alloc->GetSize(), seed);
alloc->GetResource()->Unmap(0, nullptr);
});
}
static void FillAllocationsDataGPU(const TestContext& ctx, const ComPtr<D3D12MA::Allocation>* allocs, size_t allocCount, UINT seed)
{
D3D12MA::CALLOCATION_DESC allocDesc = D3D12MA::CALLOCATION_DESC{
D3D12_HEAP_TYPE_UPLOAD,
D3D12MA::ALLOCATION_FLAG_COMMITTED,
NULL, // privateData
D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS }; // extraHeapFlags
std::vector<D3D12_RESOURCE_BARRIER> barriers;
std::vector<ComPtr<D3D12MA::Allocation>> uploadAllocs;
barriers.reserve(allocCount);
uploadAllocs.reserve(allocCount);
// Move resource into right state
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
ID3D12GraphicsCommandList* cl = BeginCommandList();
std::for_each(allocs, allocs + allocCount, [&](const ComPtr<D3D12MA::Allocation>& alloc)
{
// Copy only buffers for now
D3D12_RESOURCE_DESC resDesc = alloc->GetResource()->GetDesc();
if (resDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)
{
ComPtr<D3D12MA::Allocation> uploadAlloc;
CHECK_HR(ctx.allocator->CreateResource(&allocDesc, &resDesc, D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr, &uploadAlloc, IID_NULL, nullptr));
D3D12_RANGE range = {};
void* ptr;
CHECK_HR(uploadAlloc->GetResource()->Map(0, &range, &ptr));
FillData(ptr, resDesc.Width, seed);
uploadAlloc->GetResource()->Unmap(0, nullptr);
cl->CopyResource(alloc->GetResource(), uploadAlloc->GetResource());
uploadAllocs.emplace_back(std::move(uploadAlloc));
}
barrier.Transition.pResource = alloc->GetResource();
barrier.Transition.StateAfter = (D3D12_RESOURCE_STATES)(uintptr_t)alloc->GetPrivateData();
barriers.emplace_back(barrier);
});
cl->ResourceBarrier(static_cast<UINT>(allocCount), barriers.data());
EndCommandList(cl);
}
static bool ValidateData(const void* ptr, const UINT64 sizeInBytes, UINT seed)
{
const UINT* values = (const UINT*)ptr;
const UINT64 sizeInValues = sizeInBytes / sizeof(UINT);
UINT value = seed;
for(UINT i = 0; i < sizeInValues; ++i)
{
if(values[i] != value++)
{
//CHECK_BOOL(0 && "ValidateData failed.");
return false;
}
}
return true;
}
static bool ValidateDataZero(const void* ptr, const UINT64 sizeInBytes)
{
const UINT* values = (const UINT*)ptr;
const UINT64 sizeInValues = sizeInBytes / sizeof(UINT);
for(UINT i = 0; i < sizeInValues; ++i)
{
if(values[i] != 0)
{
//CHECK_BOOL(0 && "ValidateData failed.");
return false;
}
}
return true;
}
static void ValidateAllocationsData(const ComPtr<D3D12MA::Allocation>* allocs, size_t allocCount, UINT seed)
{
std::for_each(allocs, allocs + allocCount, [seed](const ComPtr<D3D12MA::Allocation>& alloc)
{
D3D12_RANGE range = {};
void* ptr;
CHECK_HR(alloc->GetResource()->Map(0, &range, &ptr));
CHECK_BOOL(ValidateData(ptr, alloc->GetSize(), seed));
alloc->GetResource()->Unmap(0, nullptr);
});
}
static void ValidateAllocationsDataGPU(const TestContext& ctx, const ComPtr<D3D12MA::Allocation>* allocs, size_t allocCount, UINT seed)
{
D3D12MA::CALLOCATION_DESC allocDesc = D3D12MA::CALLOCATION_DESC{
D3D12_HEAP_TYPE_READBACK,
D3D12MA::ALLOCATION_FLAG_COMMITTED,
NULL, // privateData
D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS }; // extraHeapFlags
std::vector<D3D12_RESOURCE_BARRIER> barriers;
std::vector<ComPtr<D3D12MA::Allocation>> downloadAllocs;
barriers.reserve(allocCount);
downloadAllocs.reserve(allocCount);
// Move resource into right state
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE;
ID3D12GraphicsCommandList* cl = BeginCommandList();
size_t resCount = allocCount;
std::for_each(allocs, allocs + allocCount, [&](const ComPtr<D3D12MA::Allocation>& alloc)
{
// Check only buffers for now
D3D12_RESOURCE_DESC resDesc = alloc->GetResource()->GetDesc();
if (resDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)
{
ComPtr<D3D12MA::Allocation> downloadAlloc;
CHECK_HR(ctx.allocator->CreateResource(&allocDesc, &resDesc, D3D12_RESOURCE_STATE_COPY_DEST,
nullptr, &downloadAlloc, IID_NULL, nullptr));
barrier.Transition.pResource = alloc->GetResource();
barrier.Transition.StateBefore = (D3D12_RESOURCE_STATES)(uintptr_t)alloc->GetPrivateData();
barriers.emplace_back(barrier);
downloadAllocs.emplace_back(std::move(downloadAlloc));
}
else
--resCount;
});
cl->ResourceBarrier(static_cast<UINT>(resCount), barriers.data());
for (size_t i = 0, j = 0; i < resCount; ++j)
{
if (allocs[j]->GetResource()->GetDesc().Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)
{
cl->CopyResource(downloadAllocs.at(i)->GetResource(), allocs[j]->GetResource());
barriers.at(i).Transition.StateAfter = barriers.at(i).Transition.StateBefore;
barriers.at(i).Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_SOURCE;
++i;
}
}
cl->ResourceBarrier(static_cast<UINT>(resCount), barriers.data());
EndCommandList(cl);
for (auto& alloc : downloadAllocs)
{
D3D12_RANGE range = {};
void* ptr;
CHECK_HR(alloc->GetResource()->Map(0, &range, &ptr));
CHECK_BOOL(ValidateData(ptr, alloc->GetResource()->GetDesc().Width, seed));
alloc->GetResource()->Unmap(0, nullptr);
}
}
static void SaveStatsStringToFile(const TestContext& ctx, const wchar_t* dstFilePath, BOOL detailed = TRUE)
{
WCHAR* s = nullptr;
ctx.allocator->BuildStatsString(&s, detailed);
SaveFile(dstFilePath, s, wcslen(s) * sizeof(WCHAR));
ctx.allocator->FreeStatsString(s);
}
static void TestDebugMargin(const TestContext& ctx)
{
using namespace D3D12MA;
if(D3D12MA_DEBUG_MARGIN == 0)
{
return;
}
wprintf(L"Test D3D12MA_DEBUG_MARGIN = %u\n", (uint32_t)D3D12MA_DEBUG_MARGIN);
ALLOCATION_DESC allocDesc = {};
D3D12_RESOURCE_DESC resDesc = {};
CPOOL_DESC poolDesc = CPOOL_DESC{ D3D12_HEAP_TYPE_UPLOAD, D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS };
for(size_t algorithmIndex = 0; algorithmIndex < 2; ++algorithmIndex)
{
switch(algorithmIndex)
{
case 0: poolDesc.Flags = POOL_FLAG_NONE; break;
case 1: poolDesc.Flags = POOL_FLAG_ALGORITHM_LINEAR; break;
default: assert(0);
}
ComPtr<Pool> pool;
CHECK_HR(ctx.allocator->CreatePool(&poolDesc, &pool));
allocDesc.CustomPool = pool.Get();
// Create few buffers of different size.
const size_t BUF_COUNT = 10;
ComPtr<Allocation> buffers[BUF_COUNT];
for(size_t allocIndex = 0; allocIndex < 10; ++allocIndex)
{
const bool isLast = allocIndex == BUF_COUNT - 1;
FillResourceDescForBuffer(resDesc, (UINT64)(allocIndex + 1) * 0x10000);
CHECK_HR(ctx.allocator->CreateResource(
&allocDesc,
&resDesc,
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
&buffers[allocIndex],
IID_NULL, nullptr));
}
// JSON dump
wchar_t* json = nullptr;
ctx.allocator->BuildStatsString(&json, TRUE);
int I = 1; // Put breakpoint here to manually inspect json in a debugger.
// Check if their offsets preserve margin between them.
std::sort(buffers, buffers + BUF_COUNT, [](const ComPtr<Allocation>& lhs, const ComPtr<Allocation>& rhs) -> bool
{
if(lhs->GetHeap() != rhs->GetHeap())
{
return lhs->GetHeap() < rhs->GetHeap();
}
return lhs->GetOffset() < rhs->GetOffset();
});
for(size_t i = 1; i < BUF_COUNT; ++i)
{
if(buffers[i]->GetHeap() == buffers[i - 1]->GetHeap())
{
const UINT64 allocStart = buffers[i]->GetOffset();
const UINT64 prevAllocEnd = buffers[i - 1]->GetOffset() + buffers[i - 1]->GetSize();
CHECK_BOOL(allocStart >= prevAllocEnd + D3D12MA_DEBUG_MARGIN);
}
}
ctx.allocator->FreeStatsString(json);
}
}
static void TestDebugMarginNotInVirtualAllocator(const TestContext& ctx)
{
wprintf(L"Test D3D12MA_DEBUG_MARGIN not applied to virtual allocator\n");
using namespace D3D12MA;
constexpr size_t ALLOCATION_COUNT = 10;
for(size_t algorithmIndex = 0; algorithmIndex < 2; ++algorithmIndex)
{
CVIRTUAL_BLOCK_DESC blockDesc = CVIRTUAL_BLOCK_DESC{ ALLOCATION_COUNT * MEGABYTE };
switch(algorithmIndex)
{
case 0: blockDesc.Flags = VIRTUAL_BLOCK_FLAG_NONE; break;
case 1: blockDesc.Flags = VIRTUAL_BLOCK_FLAG_ALGORITHM_LINEAR; break;
default: assert(0);
}
ComPtr<VirtualBlock> block;
CHECK_HR(CreateVirtualBlock(&blockDesc, &block));
// Fill the entire block
VirtualAllocation allocs[ALLOCATION_COUNT];
for(size_t i = 0; i < ALLOCATION_COUNT; ++i)
{
CVIRTUAL_ALLOCATION_DESC allocDesc = CVIRTUAL_ALLOCATION_DESC{ 1 * MEGABYTE, 0 };
CHECK_HR(block->Allocate(&allocDesc, &allocs[i], nullptr));
}
block->Clear();
}
}
static void TestJson(const TestContext& ctx)
{
wprintf(L"Test JSON\n");
std::vector<ComPtr<D3D12MA::Pool>> pools;
std::vector<ComPtr<D3D12MA::Allocation>> allocs;
D3D12MA::ALLOCATION_DESC allocDesc = {};
D3D12_RESOURCE_DESC resDesc = {};
resDesc.Alignment = 0;
resDesc.MipLevels = 1;
resDesc.SampleDesc.Count = 1;
resDesc.SampleDesc.Quality = 0;
D3D12_RESOURCE_ALLOCATION_INFO allocInfo = {};
allocInfo.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
allocInfo.SizeInBytes = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
// Select if using custom pool or default
for (UINT8 poolType = 0; poolType < 2; ++poolType)
{
// Select different heaps
for (UINT8 heapType = 0; heapType < 5; ++heapType)
{
D3D12_RESOURCE_STATES state;
D3D12_CPU_PAGE_PROPERTY cpuPageType = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
D3D12_MEMORY_POOL memoryPool = D3D12_MEMORY_POOL_UNKNOWN;
switch (heapType)
{
case 0:
allocDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT;
state = D3D12_RESOURCE_STATE_COMMON;
break;
case 1:
allocDesc.HeapType = D3D12_HEAP_TYPE_UPLOAD;
state = D3D12_RESOURCE_STATE_GENERIC_READ;
break;
case 2:
allocDesc.HeapType = D3D12_HEAP_TYPE_READBACK;
state = D3D12_RESOURCE_STATE_COPY_DEST;
break;
case 3:
allocDesc.HeapType = D3D12_HEAP_TYPE_CUSTOM;
state = D3D12_RESOURCE_STATE_COMMON;
cpuPageType = D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE;
memoryPool = ctx.allocator->IsUMA() ? D3D12_MEMORY_POOL_L0 : D3D12_MEMORY_POOL_L1;
break;
case 4:
allocDesc.HeapType = D3D12_HEAP_TYPE_CUSTOM;
state = D3D12_RESOURCE_STATE_GENERIC_READ;
cpuPageType = D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE;
memoryPool = D3D12_MEMORY_POOL_L0;
break;
}
// Skip custom heaps for default pools
if (poolType == 0 && heapType > 2)
continue;
const bool texturesPossible = heapType == 0 || heapType == 3;
// Select different resource region types
for (UINT8 resType = 0; resType < 3; ++resType)
{
allocDesc.ExtraHeapFlags = D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS;
D3D12_RESOURCE_FLAGS resFlags = D3D12_RESOURCE_FLAG_NONE;
if (texturesPossible)
{
switch (resType)
{
case 1:
allocDesc.ExtraHeapFlags = D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES;
break;
case 2:
allocDesc.ExtraHeapFlags = D3D12_HEAP_FLAG_ALLOW_ONLY_RT_DS_TEXTURES;
resFlags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
break;
}
}
switch (poolType)
{
case 0:
allocDesc.CustomPool = nullptr;
break;
case 1:
{
ComPtr<D3D12MA::Pool> pool;
D3D12MA::POOL_DESC poolDesc = {};
poolDesc.HeapFlags = allocDesc.ExtraHeapFlags;
poolDesc.HeapProperties.Type = allocDesc.HeapType;
poolDesc.HeapProperties.CPUPageProperty = cpuPageType;
poolDesc.HeapProperties.MemoryPoolPreference = memoryPool;
CHECK_HR(ctx.allocator->CreatePool(&poolDesc, &pool));
allocDesc.CustomPool = pool.Get();
pools.emplace_back(std::move(pool));
break;
}
}
// Select different allocation flags
for (UINT8 allocFlag = 0; allocFlag < 2; ++allocFlag)
{
switch (allocFlag)
{
case 0:
allocDesc.Flags = D3D12MA::ALLOCATION_FLAG_NONE;
break;
case 1:
allocDesc.Flags = D3D12MA::ALLOCATION_FLAG_COMMITTED;
break;
}
// Select different alloc types (block, buffer, texture, etc.)
for (UINT8 allocType = 0; allocType < 5; ++allocType)
{
// Select different data stored in the allocation
for (UINT8 data = 0; data < 4; ++data)
{
ComPtr<D3D12MA::Allocation> alloc;
if (texturesPossible && resType != 0)
{
resDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
resDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
switch (allocType % 3)
{
case 0:
resDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE1D;
resDesc.Width = 512;
resDesc.Height = 1;
resDesc.DepthOrArraySize = 1;
resDesc.Flags = resFlags;
CHECK_HR(ctx.allocator->CreateResource(&allocDesc, &resDesc, state, nullptr, &alloc, IID_NULL, nullptr));
break;
case 1:
resDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
resDesc.Width = 1024;
resDesc.Height = 512;
resDesc.DepthOrArraySize = 1;
resDesc.Flags = resFlags;
CHECK_HR(ctx.allocator->CreateResource(&allocDesc, &resDesc, state, nullptr, &alloc, IID_NULL, nullptr));
break;
case 2:
resDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
resDesc.Width = 512;
resDesc.Height = 256;
resDesc.DepthOrArraySize = 128;
resDesc.Flags = resFlags;
CHECK_HR(ctx.allocator->CreateResource(&allocDesc, &resDesc, state, nullptr, &alloc, IID_NULL, nullptr));
break;
}
}
else
{
switch (allocType % 2)
{
case 0:
CHECK_HR(ctx.allocator->AllocateMemory(&allocDesc, &allocInfo, &alloc));
break;
case 1:
FillResourceDescForBuffer(resDesc, 1024);
CHECK_HR(ctx.allocator->CreateResource(&allocDesc, &resDesc, state, nullptr, &alloc, IID_NULL, nullptr));
break;
}
}
switch (data)
{
case 1:
alloc->SetPrivateData((void*)16112007);
break;
case 2:
alloc->SetName(L"SHEPURD");
break;
case 3:
alloc->SetPrivateData((void*)26012010);
alloc->SetName(L"JOKER");
break;
}
allocs.emplace_back(std::move(alloc));
}
}
}
}
}
}
SaveStatsStringToFile(ctx, L"JSON_D3D12.json");
}
static void TestCommittedResourcesAndJson(const TestContext& ctx)
{
wprintf(L"Test committed resources and JSON\n");
const UINT count = 4;
const UINT64 bufSize = 32ull * 1024;
const wchar_t* names[count] = {
L"Resource\nFoo\r\nBar",
L"Resource \"'&<>?#@!&-=_+[]{};:,./\\",
nullptr,
L"",
};
ResourceWithAllocation resources[count];
D3D12MA::CALLOCATION_DESC allocDesc = D3D12MA::CALLOCATION_DESC{
D3D12_HEAP_TYPE_DEFAULT,
D3D12MA::ALLOCATION_FLAG_COMMITTED };
D3D12_RESOURCE_DESC resourceDesc;
FillResourceDescForBuffer(resourceDesc, bufSize);
for(UINT i = 0; i < count; ++i)
{
const bool receiveExplicitResource = i < 2;
CHECK_HR( ctx.allocator->CreateResource(
&allocDesc,
&resourceDesc,
D3D12_RESOURCE_STATE_COPY_DEST,
NULL,
&resources[i].allocation,
__uuidof(ID3D12Resource),
receiveExplicitResource ? (void**)&resources[i].resource : NULL));
if(receiveExplicitResource)
{
ID3D12Resource* res = resources[i].resource.Get();
CHECK_BOOL(res && res == resources[i].allocation->GetResource());
const ULONG refCountAfterAdd = res->AddRef();
CHECK_BOOL(refCountAfterAdd == 3);
res->Release();
}
// Make sure it has implicit heap.
CHECK_BOOL( resources[i].allocation->GetHeap() == NULL && resources[i].allocation->GetOffset() == 0 );
resources[i].allocation->SetName(names[i]);
}
// Check names.
for(UINT i = 0; i < count; ++i)
{
const wchar_t* const allocName = resources[i].allocation->GetName();
if(allocName)
{
CHECK_BOOL( wcscmp(allocName, names[i]) == 0 );
}
else
{
CHECK_BOOL(names[i] == NULL);
}
}
WCHAR* jsonString;
ctx.allocator->BuildStatsString(&jsonString, TRUE);
CHECK_BOOL(wcsstr(jsonString, L"\"Resource\\nFoo\\r\\nBar\"") != NULL);
CHECK_BOOL(wcsstr(jsonString, L"\"Resource \\\"'&<>?#@!&-=_+[]{};:,.\\/\\\\\"") != NULL);
CHECK_BOOL(wcsstr(jsonString, L"\"\"") != NULL);
ctx.allocator->FreeStatsString(jsonString);
}
static void TestSmallBuffers(const TestContext& ctx)
{
wprintf(L"Test small buffers\n");
D3D12MA::CPOOL_DESC poolDesc = D3D12MA::CPOOL_DESC{
D3D12_HEAP_TYPE_DEFAULT,
D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS };
ComPtr<D3D12MA::Pool> pool;
CHECK_HR(ctx.allocator->CreatePool(&poolDesc, &pool));
D3D12MA::CALLOCATION_DESC allocDesc = D3D12MA::CALLOCATION_DESC{ pool.Get() };
D3D12_RESOURCE_DESC resDesc;
FillResourceDescForBuffer(resDesc, 8 * KILOBYTE);
D3D12_RESOURCE_DESC largeResDesc = resDesc;
largeResDesc.Width = 128 * KILOBYTE;
std::vector<ResourceWithAllocation> resources;
// A large buffer placed inside the heap to allocate first block.
{
resources.emplace_back();
ResourceWithAllocation& resWithAlloc = resources.back();
CHECK_HR(ctx.allocator->CreateResource(&allocDesc, &largeResDesc, D3D12_RESOURCE_STATE_COMMON,
nullptr, &resWithAlloc.allocation, IID_PPV_ARGS(&resWithAlloc.resource)));
CHECK_BOOL(resWithAlloc.allocation && resWithAlloc.allocation->GetResource());
CHECK_BOOL(resWithAlloc.allocation->GetHeap()); // Expected to be placed.
}
// Test 1: COMMITTED.
{
resources.emplace_back();
ResourceWithAllocation& resWithAlloc = resources.back();
allocDesc.Flags = D3D12MA::ALLOCATION_FLAG_COMMITTED;
CHECK_HR(ctx.allocator->CreateResource(&allocDesc, &resDesc, D3D12_RESOURCE_STATE_COMMON,
nullptr, &resWithAlloc.allocation, IID_PPV_ARGS(&resWithAlloc.resource)));
CHECK_BOOL(resWithAlloc.allocation && resWithAlloc.allocation->GetResource());
CHECK_BOOL(!resWithAlloc.allocation->GetHeap()); // Expected to be committed.
}
// Test 2: Default.
{
resources.emplace_back();
ResourceWithAllocation& resWithAlloc = resources.back();
allocDesc.Flags = D3D12MA::ALLOCATION_FLAG_NONE;
CHECK_HR(ctx.allocator->CreateResource(&allocDesc, &resDesc, D3D12_RESOURCE_STATE_COMMON,
nullptr, &resWithAlloc.allocation, IID_PPV_ARGS(&resWithAlloc.resource)));
CHECK_BOOL(resWithAlloc.allocation && resWithAlloc.allocation->GetResource());
// May or may not be committed, depending on the PREFER_SMALL_BUFFERS_COMMITTED
// and TIGHT_ALIGNMENT settings.
const bool isCommitted = resWithAlloc.allocation->GetHeap() == NULL;
if (isCommitted)
wprintf(L" Small buffer %llu B inside a custom pool was created as committed.\n", resDesc.Width);
else
wprintf(L" Small buffer %llu B inside a custom pool was created as placed.\n", resDesc.Width);
}
// Test 3: NEVER_ALLOCATE.
{
resources.emplace_back();
ResourceWithAllocation& resWithAlloc = resources.back();
allocDesc.Flags = D3D12MA::ALLOCATION_FLAG_NEVER_ALLOCATE;
CHECK_HR(ctx.allocator->CreateResource(&allocDesc, &resDesc, D3D12_RESOURCE_STATE_COMMON,
nullptr, &resWithAlloc.allocation, IID_PPV_ARGS(&resWithAlloc.resource)));
CHECK_BOOL(resWithAlloc.allocation && resWithAlloc.allocation->GetResource());
CHECK_BOOL(resWithAlloc.allocation->GetHeap()); // Expected to be placed.
}
}
static void TestCustomHeapFlags(const TestContext& ctx)
{
wprintf(L"Test custom heap flags\n");
// 1. Just memory heap with custom flags
{
D3D12MA::CALLOCATION_DESC allocDesc = D3D12MA::CALLOCATION_DESC{
D3D12_HEAP_TYPE_DEFAULT,
D3D12MA::ALLOCATION_FLAG_NONE,
NULL, // privateData
D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES |
D3D12_HEAP_FLAG_SHARED }; // Extra flag.
D3D12_RESOURCE_ALLOCATION_INFO resAllocInfo = {};
resAllocInfo.SizeInBytes = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
resAllocInfo.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
ResourceWithAllocation res;
CHECK_HR( ctx.allocator->AllocateMemory(&allocDesc, &resAllocInfo, &res.allocation) );
// Must be created as separate allocation.
CHECK_BOOL( res.allocation->GetOffset() == 0 );
}
// 2. Committed resource with custom flags
{
D3D12_RESOURCE_DESC resourceDesc = {};
resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
resourceDesc.Alignment = 0;
resourceDesc.Width = 1920;
resourceDesc.Height = 1080;
resourceDesc.DepthOrArraySize = 1;
resourceDesc.MipLevels = 1;
resourceDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
resourceDesc.SampleDesc.Count = 1;
resourceDesc.SampleDesc.Quality = 0;
resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
resourceDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER;
D3D12MA::CALLOCATION_DESC allocDesc = D3D12MA::CALLOCATION_DESC{
D3D12_HEAP_TYPE_DEFAULT,
D3D12MA::ALLOCATION_FLAG_NONE,
NULL, // privateData,
D3D12_HEAP_FLAG_SHARED | D3D12_HEAP_FLAG_SHARED_CROSS_ADAPTER };
ResourceWithAllocation res;
CHECK_HR( ctx.allocator->CreateResource(
&allocDesc,
&resourceDesc,
D3D12_RESOURCE_STATE_COMMON,
NULL,
&res.allocation,
IID_PPV_ARGS(&res.resource)) );
// Must be created as committed.
CHECK_BOOL( res.allocation->GetHeap() == NULL );
}
}
static void TestPlacedResources(const TestContext& ctx)
{
wprintf(L"Test placed resources\n");
const bool alwaysCommitted = (ctx.allocatorFlags & D3D12MA::ALLOCATOR_FLAG_ALWAYS_COMMITTED) != 0;
const UINT count = 4;
const UINT64 bufSize = 64ull * 1024;
ResourceWithAllocation resources[count];
D3D12MA::CALLOCATION_DESC allocDesc = D3D12MA::CALLOCATION_DESC{ D3D12_HEAP_TYPE_DEFAULT };
D3D12_RESOURCE_DESC resourceDesc;
FillResourceDescForBuffer(resourceDesc, bufSize);
for(UINT i = 0; i < count; ++i)
{
CHECK_HR( ctx.allocator->CreateResource(
&allocDesc,
&resourceDesc,
D3D12_RESOURCE_STATE_GENERIC_READ,
NULL,
&resources[i].allocation,
IID_PPV_ARGS(&resources[i].resource)) );
// Make sure it doesn't have implicit heap.
if(!alwaysCommitted)
{
CHECK_BOOL( resources[i].allocation->GetHeap() != NULL );
}
}
// Make sure at least some of the resources belong to the same heap, but their memory ranges don't overlap.
bool sameHeapFound = false;
for(size_t i = 0; i < count; ++i)
{
for(size_t j = i + 1; j < count; ++j)
{
const ResourceWithAllocation& resI = resources[i];
const ResourceWithAllocation& resJ = resources[j];
if(resI.allocation->GetHeap() != NULL &&
resI.allocation->GetHeap() == resJ.allocation->GetHeap())
{
sameHeapFound = true;
CHECK_BOOL(resI.allocation->GetOffset() + resI.allocation->GetSize() <= resJ.allocation->GetOffset() ||
resJ.allocation->GetOffset() + resJ.allocation->GetSize() <= resI.allocation->GetOffset());
}
}
}
if(!alwaysCommitted)
{
CHECK_BOOL(sameHeapFound);
}
// Additionally create a texture to see if no error occurs due to bad handling of Resource Tier.
resourceDesc = {};
resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
resourceDesc.Alignment = 0;
resourceDesc.Width = 1024;
resourceDesc.Height = 1024;
resourceDesc.DepthOrArraySize = 1;
resourceDesc.MipLevels = 1;
resourceDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
resourceDesc.SampleDesc.Count = 1;
resourceDesc.SampleDesc.Quality = 0;
resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
resourceDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
ResourceWithAllocation textureRes;
CHECK_HR( ctx.allocator->CreateResource(
&allocDesc,
&resourceDesc,
D3D12_RESOURCE_STATE_COPY_DEST,
NULL,
&textureRes.allocation,
IID_PPV_ARGS(&textureRes.resource)) );
// Additionally create an MSAA render target to see if no error occurs due to bad handling of Resource Tier.
resourceDesc = {};
resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
resourceDesc.Alignment = 0;
resourceDesc.Width = 1920;
resourceDesc.Height = 1080;
resourceDesc.DepthOrArraySize = 1;
resourceDesc.MipLevels = 1;
resourceDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
resourceDesc.SampleDesc.Count = 2;
resourceDesc.SampleDesc.Quality = 0;
resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
resourceDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
ResourceWithAllocation renderTargetRes;
CHECK_HR( ctx.allocator->CreateResource(
&allocDesc,
&resourceDesc,
D3D12_RESOURCE_STATE_RENDER_TARGET,
NULL,
&renderTargetRes.allocation,
IID_PPV_ARGS(&renderTargetRes.resource)) );
}
static void TestOtherComInterface(const TestContext& ctx)
{
wprintf(L"Test other COM interface\n");
D3D12_RESOURCE_DESC resDesc;
FillResourceDescForBuffer(resDesc, 0x10000);
for(uint32_t i = 0; i < 2; ++i)
{
D3D12MA::CALLOCATION_DESC allocDesc = D3D12MA::CALLOCATION_DESC{ D3D12_HEAP_TYPE_DEFAULT };
if(i == 1)
{
allocDesc.Flags = D3D12MA::ALLOCATION_FLAG_COMMITTED;
}
ComPtr<D3D12MA::Allocation> alloc;
ComPtr<ID3D12Pageable> pageable;
CHECK_HR(ctx.allocator->CreateResource(
&allocDesc,
&resDesc,
D3D12_RESOURCE_STATE_COMMON,
nullptr, // pOptimizedClearValue
&alloc,
IID_PPV_ARGS(&pageable)));
// Do something with the interface to make sure it's valid.
ComPtr<ID3D12Device> device;
CHECK_HR(pageable->GetDevice(IID_PPV_ARGS(&device)));
CHECK_BOOL(device.Get() == ctx.device);
}
}
static void TestCustomPools(const TestContext& ctx)
{
wprintf(L"Test custom pools\n");
// # Fetch global stats 1
D3D12MA::TotalStatistics globalStatsBeg = {};
ctx.allocator->CalculateStatistics(&globalStatsBeg);
// # Create pool, 1..2 blocks of 11 MB
D3D12MA::CPOOL_DESC poolDesc = D3D12MA::CPOOL_DESC{
D3D12_HEAP_TYPE_DEFAULT,
D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS,
D3D12MA::POOL_FLAG_NONE,
11 * MEGABYTE, // blockSize
1, // minBlockCount
2, // maxBlockCount
D3D12_RESIDENCY_PRIORITY_HIGH }; // Test some residency priority, by the way.