-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.cpp
More file actions
699 lines (617 loc) · 28.8 KB
/
demo.cpp
File metadata and controls
699 lines (617 loc) · 28.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/**
* Copyright (C) 2025-2026, by Pavel Martishevsky
*
* This header is distributed under the MIT License. See notice at the end of this file.
*/
#include "compiler_warning.h"
#include <stdint.h>
#include <stdio.h>
#include <math.h>
COMPILER_WARNING_PUSH()
COMPILER_WARNING_DISABLE_MSVC(4820) /** warning C4820: 'Name A' : 'N' bytes padding added after data member 'Name B' */
COMPILER_WARNING_DISABLE_MSVC(4255) /** warning C4255: 'Name A' : no function prototype given: converting '()' to '(void)' */
COMPILER_WARNING_DISABLE_MSVC(4365) /** warning C4365: 'Name A' : 'return' : conversion from 'Type A' to 'Type B', signed/unsigned mismatch */
COMPILER_WARNING_DISABLE_MSVC(4480)
COMPILER_WARNING_DISABLE_MSVC(4668) /** warning C4668: 'Name A' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif' */
COMPILER_WARNING_DISABLE_MSVC(5039) /** warning C5039: 'Name A': pointer or reference to potentially throwing function passed to extern C function under -EHc. Undefined behavior may occur if this function throws an exception. Note: to implify migration, consider the temporary use of /Wv:18 flag with the version of the compiler with which you used to build without warnings */
#include <windows.h>
COMPILER_WARNING_POP()
COMPILER_WARNING_PUSH()
COMPILER_WARNING_DISABLE_MSVC(4255) /** warning C4255: 'Name A' : no function prototype given: converting '()' to '(void)' */
COMPILER_WARNING_DISABLE_MSVC(4820) /** warning C4820: 'Name A' : 'N' bytes padding added after data member 'Name B' */
#include <dxgi1_6.h>
COMPILER_WARNING_POP()
COMPILER_WARNING_PUSH()
COMPILER_WARNING_DISABLE_MSVC(4820) /** warning C4820: 'Name A' : 'N' bytes padding added after data member 'Name B' */
COMPILER_WARNING_DISABLE_MSVC(4201) /** warning C4201: nonstandard extension used : nameless struct/union */
#include <d3d12.h>
COMPILER_WARNING_POP()
COMPILER_WARNING_PUSH()
COMPILER_WARNING_DISABLE_MSVC(4365) /** warning C4365: '=': conversion from 'Type A' to 'Type B', signed/unsigned mismatch */
COMPILER_WARNING_DISABLE_MSVC(4820) /** warning C4820: 'Name A' : 'N' bytes padding added after data member 'Name B' */
COMPILER_WARNING_DISABLE_MSVC1912(5039)
#define STB_SPRINTF_IMPLEMENTATION 1
#include "stb_sprintf.h"
COMPILER_WARNING_POP()
#include <shader_threadgroup_bitonic_sort_1_1_0.h>
#include <shader_threadgroup_bitonic_sort_2_1_0.h>
#include <shader_threadgroup_bitonic_sort_2_2_0.h>
#include <shader_threadgroup_bitonic_sort_3_1_0.h>
#include <shader_threadgroup_bitonic_sort_3_2_0.h>
#include <shader_threadgroup_bitonic_sort_3_3_0.h>
#include <shader_threadgroup_bitonic_sort_4_2_0.h>
#include <shader_threadgroup_bitonic_sort_4_3_0.h>
#include <shader_threadgroup_bitonic_sort_4_4_0.h>
#include <shader_threadgroup_bitonic_sort_5_3_0.h>
#include <shader_threadgroup_bitonic_sort_5_4_0.h>
#include <shader_threadgroup_bitonic_sort_5_5_0.h>
#include <shader_threadgroup_bitonic_sort_6_4_0.h>
#include <shader_threadgroup_bitonic_sort_6_5_0.h>
#include <shader_threadgroup_bitonic_sort_6_6_0.h>
#include <shader_threadgroup_bitonic_sort_7_5_0.h>
#include <shader_threadgroup_bitonic_sort_7_6_0.h>
#include <shader_threadgroup_bitonic_sort_7_7_0.h>
#include <shader_threadgroup_bitonic_sort_8_6_0.h>
#include <shader_threadgroup_bitonic_sort_8_7_0.h>
#include <shader_threadgroup_bitonic_sort_8_8_0.h>
#include <shader_threadgroup_bitonic_sort_9_7_0.h>
#include <shader_threadgroup_bitonic_sort_9_8_0.h>
#include <shader_threadgroup_bitonic_sort_9_9_0.h>
#include <shader_threadgroup_bitonic_sort_10_8_0.h>
#include <shader_threadgroup_bitonic_sort_10_9_0.h>
#include <shader_threadgroup_bitonic_sort_10_10_0.h>
#include <shader_threadgroup_bitonic_sort_11_9_0.h>
#include <shader_threadgroup_bitonic_sort_11_10_0.h>
#include <shader_threadgroup_bitonic_sort_12_10_0.h>
#include <shader_threadgroup_bitonic_sort_1_1_1.h>
#include <shader_threadgroup_bitonic_sort_2_1_1.h>
#include <shader_threadgroup_bitonic_sort_2_2_1.h>
#include <shader_threadgroup_bitonic_sort_3_1_1.h>
#include <shader_threadgroup_bitonic_sort_3_2_1.h>
#include <shader_threadgroup_bitonic_sort_3_3_1.h>
#include <shader_threadgroup_bitonic_sort_4_2_1.h>
#include <shader_threadgroup_bitonic_sort_4_3_1.h>
#include <shader_threadgroup_bitonic_sort_4_4_1.h>
#include <shader_threadgroup_bitonic_sort_5_3_1.h>
#include <shader_threadgroup_bitonic_sort_5_4_1.h>
#include <shader_threadgroup_bitonic_sort_5_5_1.h>
#include <shader_threadgroup_bitonic_sort_6_4_1.h>
#include <shader_threadgroup_bitonic_sort_6_5_1.h>
#include <shader_threadgroup_bitonic_sort_6_6_1.h>
#include <shader_threadgroup_bitonic_sort_7_5_1.h>
#include <shader_threadgroup_bitonic_sort_7_6_1.h>
#include <shader_threadgroup_bitonic_sort_7_7_1.h>
#include <shader_threadgroup_bitonic_sort_8_6_1.h>
#include <shader_threadgroup_bitonic_sort_8_7_1.h>
#include <shader_threadgroup_bitonic_sort_8_8_1.h>
#include <shader_threadgroup_bitonic_sort_9_7_1.h>
#include <shader_threadgroup_bitonic_sort_9_8_1.h>
#include <shader_threadgroup_bitonic_sort_9_9_1.h>
#include <shader_threadgroup_bitonic_sort_10_8_1.h>
#include <shader_threadgroup_bitonic_sort_10_9_1.h>
#include <shader_threadgroup_bitonic_sort_10_10_1.h>
#include <shader_threadgroup_bitonic_sort_11_9_1.h>
#include <shader_threadgroup_bitonic_sort_11_10_1.h>
#include <shader_threadgroup_bitonic_sort_12_10_1.h>
#define USE_PIX 1
#if USE_PIX
COMPILER_WARNING_PUSH()
COMPILER_WARNING_DISABLE_MSVC(4820) /** warning C4820: 'Name A' : 'N' bytes padding added after data member 'Name B' */
COMPILER_WARNING_DISABLE_MSVC(4201) /** warning C4201: nonstandard extension used : nameless struct/union */
COMPILER_WARNING_DISABLE_MSVC(4365) /** warning C4365: 'initializing': conversion from 'type A' to 'type B' signed/unsigned mismatch */
COMPILER_WARNING_DISABLE_MSVC(5039) /** warning C5039: 'Name A': pointer or reference to potentially throwing function passed to extern C function under -EHc. Undefined behavior may occur if this function throws an exception. Note: to implify migration, consider the temporary use of /Wv:18 flag with the version of the compiler with which you used to build without warnings */
# include <pix3.h>
COMPILER_WARNING_POP()
#endif
static void debugPrint(const char *msg);
static void debugPrintF(const char *format, ...);
#define D3D12AID_CHECK(call) \
do \
{ \
HRESULT hr = call; \
if (S_OK != hr) \
{ \
debugPrintF("S_OK != 0x%08lx " #call "\n", hr); \
if (IsDebuggerPresent()) \
__debugbreak(); \
} \
} \
while(0)
#define D3D12AID_ASSERT(cond) \
do \
{ \
if (!(cond)) \
{ \
debugPrint("Assert with condition "#cond" failed.\n"); \
if (IsDebuggerPresent()) \
__debugbreak(); \
} \
} \
while(0)
#include "d3d12aid.h"
extern "C" { __declspec(dllexport) extern const UINT D3D12SDKVersion = D3D12_SDK_VERSION;}
extern "C" { __declspec(dllexport) extern const char* D3D12SDKPath = u8".\\"; }
typedef struct module_t
{
HMODULE handle;
HMODULE unload;
} module_t;
static void load_module(module_t *out_module, const wchar_t *name)
{
out_module->handle = GetModuleHandleW(name);
out_module->unload = NULL;
if (NULL == out_module->handle)
{
out_module->handle = out_module->unload = LoadLibraryW(name);
}
}
static void unload_module(module_t *inout_module)
{
if (NULL != inout_module->unload)
{
FreeLibrary(inout_module->unload);
}
inout_module->handle = NULL;
inout_module->unload = NULL;
}
#define DECLARE_IMPORT(name, ...) \
typedef HRESULT WINAPI pfn##name(__VA_ARGS__); \
union ucvt_##name \
{ \
pfn##name *typedFn; \
FARPROC farprFn; \
}; \
typedef ucvt_##name ucvt_##name; \
static pfn##name *fn##name = NULL; \
static void load_##name(const module_t *module) \
{ \
ucvt_##name cvt; \
cvt.farprFn = GetProcAddress(module->handle, #name); \
fn##name = cvt.typedFn; \
}
DECLARE_IMPORT(CreateDXGIFactory2, UINT Flags, REFIID riid, void **ppFactory);
DECLARE_IMPORT(D3D12CreateDevice, IUnknown *pAdapter, D3D_FEATURE_LEVEL MinimumFeatureLevel, REFIID riid, void **ppDevice);
typedef void benchmark_ForEachDeviceCback(IDXGIAdapter *adapter, const DXGI_ADAPTER_DESC1 *desc, ID3D12Device *device, const char *deviceName, void *userdata);
static int benchmark_ForEachDevice(benchmark_ForEachDeviceCback *callback, void *userdata)
{
module_t d3d12;
module_t dxgi;
IDXGIFactory7 *factory = NULL;
load_module(&dxgi, L"dxgi.dll");
load_module(&d3d12, L"d3d12.dll");
load_CreateDXGIFactory2(&dxgi);
load_D3D12CreateDevice(&d3d12);
D3D12AID_CHECK(fnCreateDXGIFactory2(0, D3D12AID_IID_PPV_ARGS(IDXGIFactory7, &factory)));
IDXGIAdapter1 *adapter = NULL;
for (uint32_t adapterIdx = 0; DXGI_ERROR_NOT_FOUND != factory->EnumAdapters1(adapterIdx, &adapter); ++adapterIdx)
{
DXGI_ADAPTER_DESC1 desc;
D3D12AID_CHECK(adapter->GetDesc1(&desc));
if (desc.VendorId != 0x1414)
{
static const char deviceNamePattern [] = "_venId_0x0000_devId_0x0000_revId_0x00";
static const char deviceNameDefault [] = "Unknown Device";
char deviceNameD3D12[_countof(desc.Description)];
char deviceFullName[_countof(deviceNameD3D12) + _countof(deviceNamePattern) - 1];
const char *deviceName = NULL;
if (0 != WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, deviceNameD3D12, _countof(deviceNameD3D12), NULL, NULL))
{
deviceName = deviceNameD3D12;
}
else
{
deviceName = deviceNameDefault;
}
stbsp_snprintf(deviceFullName, _countof(deviceFullName), "%s_venId_0x%04x_devId_0x%04x_revId_0x%02x", deviceName, desc.VendorId, desc.DeviceId, desc.Revision);
debugPrintF("Found: %s\n", deviceFullName);
ID3D12Device *device = NULL;
D3D12AID_CHECK(fnD3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device)));
D3D12AID_CHECK(device->SetStablePowerState(TRUE));
callback(adapter, &desc, device, deviceName, userdata);
D3D12AID_CHECK(device->SetStablePowerState(FALSE));
D3D12AID_SAFE_RELEASE(device);
}
D3D12AID_SAFE_RELEASE(adapter);
}
D3D12AID_SAFE_RELEASE(factory);
unload_module(&d3d12);
unload_module(&dxgi);
return 0;
}
static void debugPrint(const char *msg)
{
DWORD len = (DWORD)strlen(msg);
OutputDebugStringA(msg);
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), msg, len, NULL, NULL);
}
static void debugPrintF(const char *format, ...)
{
char buffer[1024];
int result;
va_list va;
va_start(va, format);
result = stbsp_vsnprintf(buffer, sizeof(buffer), format, va);
STBSP__NOTUSED(result);
debugPrint(buffer);
va_end(va);
}
struct ShaderBytecode
{
uint32_t kernelSizeLog2;
uint32_t tgroupSizeLog2;
size_t shaderBytecodeSizeInBytes;
const void *shaderBytecode;
};
typedef struct ShaderBytecode ShaderBytecode;
#define SHADER_LIST()\
SHADER(1 , 1 )\
SHADER(2 , 1 )\
SHADER(2 , 2 )\
SHADER(3 , 1 )\
SHADER(3 , 2 )\
SHADER(3 , 3 )\
SHADER(4 , 2 )\
SHADER(4 , 3 )\
SHADER(4 , 4 )\
SHADER(5 , 3 )\
SHADER(5 , 4 )\
SHADER(5 , 5 )\
SHADER(6 , 4 )\
SHADER(6 , 5 )\
SHADER(6 , 6 )\
SHADER(7 , 5 )\
SHADER(7 , 6 )\
SHADER(7 , 7 )\
SHADER(8 , 6 )\
SHADER(8 , 7 )\
SHADER(8 , 8 )\
SHADER(9 , 7 )\
SHADER(9 , 8 )\
SHADER(9 , 9 )\
SHADER(10, 8 )\
SHADER(10, 9 )\
SHADER(10, 10)\
SHADER(11, 9 )\
SHADER(11, 10)\
SHADER(12, 10)
static const ShaderBytecode GShaderBytecodes[] =
{
#define SHADER(K, T) { K , T , sizeof(shader_threadgroup_bitonic_sort_##K##_##T##_0), shader_threadgroup_bitonic_sort_##K##_##T##_0 },
SHADER_LIST()
#undef SHADER
};
static const uint32_t kShaderWithWaveIntrinsicsCount = _countof(GShaderBytecodes);
static const ShaderBytecode GShaderBytecodesNoWaveIntrinsics[] =
{
#define SHADER(K, T) { K , T , sizeof(shader_threadgroup_bitonic_sort_##K##_##T##_1), shader_threadgroup_bitonic_sort_##K##_##T##_1 },
SHADER_LIST()
#undef SHADER
};
static const uint32_t kShaderNoWaveIntrinsicsCount = _countof(GShaderBytecodesNoWaveIntrinsics);
static const uint32_t kShaderMaxCount = kShaderWithWaveIntrinsicsCount + kShaderNoWaveIntrinsicsCount;
typedef struct PerfData
{
uint64_t sumTime;
uint64_t minTime;
uint64_t maxTime;
double avgTime;
double varTime;
uint64_t addCount;
uint64_t frq;
} PerfData;
static void perfData_Init(PerfData *inoutPerfData, uint64_t frq)
{
inoutPerfData->sumTime = 0;
inoutPerfData->minTime = ~0ull;
inoutPerfData->maxTime = 0;
inoutPerfData->avgTime = 0.0;
inoutPerfData->varTime = 0.0;
inoutPerfData->addCount = 0;
inoutPerfData->frq = frq;
}
static void perfData_AddSample(PerfData *inoutPerfData, uint64_t sample)
{
double delta1 = 0.0;
double delta2 = 0.0;
double sampleF64 = (double)sample;
inoutPerfData->sumTime += sample;
if (inoutPerfData->minTime > sample)
{
inoutPerfData->minTime = sample;
}
if (inoutPerfData->maxTime < sample)
{
inoutPerfData->maxTime = sample;
}
/** use Welford's algorithm to compue running mean(avg) and variance(to compute standard deviation)*/
inoutPerfData->addCount += 1;
delta1 = sampleF64 - inoutPerfData->avgTime;
inoutPerfData->avgTime += delta1 / (double)inoutPerfData->addCount;
delta2 = sampleF64 - inoutPerfData->avgTime;
inoutPerfData->varTime += delta1 * delta2;
}
static void perfData_PrintHeader()
{
debugPrintF("%*s | GElem/s | Min Time (us) | Max Time (us) | Mean Time (us) | StdDev0 (us) | StdDev1 (us) | Total Time (us) | Mean Time per Elem (ns) |\n", 52, "");
}
static void perfData_Print(const PerfData *inPerfData, uint32_t workItemCount)
{
uint64_t popCount = inPerfData->addCount;
uint64_t smpCount = popCount - ((inPerfData->addCount == 0) ? 0u : 1u);
uint64_t minTimeUs = (inPerfData->minTime * 1000000ULL) / inPerfData->frq;
uint64_t maxTimeUs = (inPerfData->maxTime * 1000000ULL) / inPerfData->frq;
uint64_t sumTimeUs = (inPerfData->sumTime * 1000000ULL) / inPerfData->frq;
double avgTimeUs = (inPerfData->avgTime * 1000000.0) / (double)inPerfData->frq;
double popStdevTimeClocks = sqrt(inPerfData->varTime / (double) popCount);
double smpStdevTimeClocks = sqrt(inPerfData->varTime / (double) smpCount);
double popStdevTimeUs = (popStdevTimeClocks * 1000000) / (double)inPerfData->frq;
double smpStdevTimeUs = (smpStdevTimeClocks * 1000000) / (double)inPerfData->frq;
double avgTimeUsF64 = (double)avgTimeUs;
debugPrintF("| %7.3f | %13llu | %13llu | %14.3f | +/- %4.1f%% %8.3f | +/- %4.1f%% %8.3f | %15llu | %23.3f |\n",
(double)workItemCount / (avgTimeUs * 1000.0),
minTimeUs, maxTimeUs, avgTimeUs,
popStdevTimeUs * 100.0 / avgTimeUsF64, popStdevTimeUs,
smpStdevTimeUs * 100.0 / avgTimeUsF64, smpStdevTimeUs,
sumTimeUs,
avgTimeUs * 1000.0 / (double)workItemCount
);
}
typedef struct benchmark_SharedData
{
uint32_t options;
char captureDrive;
char padding[3];
} benchmark_SharedData;
static void benchmark_threadgroup_bitonic_sort_Cback(IDXGIAdapter *adapter, const DXGI_ADAPTER_DESC1 *desc, ID3D12Device *device, const char *deviceName, void *userdata);
static const uint32_t kCmdBufferInFlight = 3;
static const uint32_t kMaxSortKeysPerTg = 4096;
static const uint32_t kShaderBenchFrameCount = 10;
int main(int argc, char **argv)
{
benchmark_SharedData sharedData;
sharedData.options = 0;
for (int argi = 1; argi < argc; ++argi)
{
if (0 == strcmp("--pixcaptures", argv[argi]))
{
sharedData.options ^= 0x1u;
}
}
#if USE_PIX
if (sharedData.options & 0x1u)
{
PIXLoadLatestWinPixGpuCapturerLibrary();
}
#endif
sharedData.captureDrive = argv[0][0];
return benchmark_ForEachDevice(benchmark_threadgroup_bitonic_sort_Cback, &sharedData);
}
void benchmark_threadgroup_bitonic_sort_Cback(IDXGIAdapter *adapter, const DXGI_ADAPTER_DESC1 *desc, ID3D12Device *device, const char *deviceName, void *userdata)
{
(void)adapter;
(void)desc;
(void)deviceName;
const benchmark_SharedData *sharedData = (const benchmark_SharedData *)userdata;
D3D12_FEATURE_DATA_D3D12_OPTIONS1 options1;
D3D12AID_CHECK(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS1, &options1, sizeof(options1)));
debugPrintF("Total Lane Count %u\n", options1.TotalLaneCount);
const uint32_t kSortKeysPerDispatch = options1.TotalLaneCount * kMaxSortKeysPerTg;
uint64_t gpuTimestampDelta[kShaderBenchFrameCount * kShaderMaxCount];
debugPrintF("Creating D3D12 Command Queue, Allocators and Lists ... ");
d3d12aid_CmdQueue queue;
d3d12aid_CmdQueue_Create(&queue, device, kCmdBufferInFlight, 1, D3D12_COMMAND_LIST_TYPE_DIRECT);
debugPrintF("Completed.\n");
uint64_t gpuTimestampFreq = 0;
D3D12AID_CHECK(queue.queue->GetTimestampFrequency(&gpuTimestampFreq));
debugPrintF("Creating D3D12 Resources and PSOs ... ");
d3d12aid_Timestamps timestamps;
d3d12aid_Timestamps_Create(×tamps, device, 2, kCmdBufferInFlight);
d3d12aid_MappedBuffer sortInput;
d3d12aid_MappedBuffer sortOutput;
d3d12aid_MappedBuffer_Create(&sortInput, device, 1, sizeof(uint32_t) * kSortKeysPerDispatch, D3D12_HEAP_TYPE_UPLOAD);
d3d12aid_MappedBuffer_Create(&sortOutput, device, kCmdBufferInFlight, sizeof(uint32_t) * kSortKeysPerDispatch, D3D12_HEAP_TYPE_READBACK);
uint32_t *randomSortInput = (uint32_t *)malloc(sizeof(uint32_t) * kSortKeysPerDispatch);
// initialize with a sequence of increasing order to check it's going to be sorted in decreasing order
for (uint32_t i = 0; i < kSortKeysPerDispatch; ++i)
{
randomSortInput[i] = i;
}
d3d12aid_MappedBuffer_Append(&sortInput, 0, randomSortInput, sizeof(uint32_t) * kSortKeysPerDispatch);
free(randomSortInput);
// We test kernels w/ and w/o wave intrinsics support if possible
d3d12aid_ComputeRsPs rspsBitonicSort[kShaderMaxCount];
const ShaderBytecode *shaders[kShaderMaxCount];
uint32_t kShaderCount = kShaderNoWaveIntrinsicsCount;
for (uint32_t i = 0; i < kShaderNoWaveIntrinsicsCount; ++i)
{
shaders[i] = &GShaderBytecodesNoWaveIntrinsics[i];
d3d12aid_ComputeRsPs_Create(&rspsBitonicSort[i], device, shaders[i]->shaderBytecode, shaders[i]->shaderBytecodeSizeInBytes);
}
if (options1.WaveOps)
{
for (uint32_t i = kShaderNoWaveIntrinsicsCount; i < kShaderMaxCount; ++i)
{
shaders[i] = &GShaderBytecodes[i - kShaderNoWaveIntrinsicsCount];
d3d12aid_ComputeRsPs_Create(&rspsBitonicSort[i], device, shaders[i]->shaderBytecode, shaders[i]->shaderBytecodeSizeInBytes);
}
kShaderCount += kShaderWithWaveIntrinsicsCount;
}
debugPrintF("Completed.\n");
debugPrintF("Running benchmark ... ");
uint32_t failCount = 0;
#if USE_PIX
const uint32_t kPixFrameCount = (sharedData->options & 0x1) ? kShaderCount : 0u;
#else
const uint32_t kPixFrameCount = 0u;
#endif
const uint32_t kBenchFrameCount = kShaderBenchFrameCount * kShaderCount;
/** 'check' frames submit different shader per frame and read the result back for validation after `kCmdBufferInFlight` frames */
const uint32_t kCheckFrameCount = kShaderCount + kCmdBufferInFlight;
const uint32_t kCheckFrameStart = kBenchFrameCount;
const uint32_t kPixFrameStart = kCheckFrameStart + kCheckFrameCount;
const uint32_t kTotalFrameCount = kPixFrameStart + kPixFrameCount;
uint32_t kCmdBufferIndex = 0;
for (uint32_t frameIndex = 0; frameIndex < kTotalFrameCount; ++frameIndex)
{
uint32_t dispatchShaderId = 0;
if (frameIndex >= kPixFrameStart)
{
dispatchShaderId = frameIndex - kPixFrameStart;
}
else if (frameIndex >= kCheckFrameStart)
{
dispatchShaderId = (frameIndex - kCheckFrameStart) % kShaderCount;
}
else if (kBenchFrameCount > 0)
{
dispatchShaderId = (frameIndex / kShaderBenchFrameCount) % kShaderCount;
}
const ShaderBytecode *dispatchShaderBytecode = shaders[dispatchShaderId];
const uint32_t dispatchKernelSize = 1u << dispatchShaderBytecode->kernelSizeLog2;
const uint32_t dispatchTGroupSize = 1u << dispatchShaderBytecode->tgroupSizeLog2;
if (frameIndex >= kPixFrameStart)
{
char buffer[256];
wchar_t wbuffer[256];
stbsp_snprintf(buffer, _countof(buffer), "%c:\\tg_bitonic_kernel_%u_tgroup_%u_waveintr_%u_%s.wpix", sharedData->captureDrive, dispatchKernelSize, dispatchTGroupSize, dispatchShaderId >= kShaderNoWaveIntrinsicsCount ? 1 : 0, deviceName);
if (0 != MultiByteToWideChar(CP_UTF8, 0, buffer, -1, wbuffer, _countof(wbuffer)))
{
PIXCaptureParameters params;
params.GpuCaptureParameters.FileName = wbuffer;
PIXBeginCapture(PIX_CAPTURE_GPU, ¶ms);
}
}
ID3D12GraphicsCommandList *cmdList = d3d12aid_CmdQueue_StartCmdList(&queue, 0);
if (frameIndex == 0)
{
d3d12aid_MappedBuffer_Transfer(cmdList, &sortInput, 0);
D3D12_RESOURCE_BARRIER barrier;
d3d12aid_MappedBuffer_EndTransfer(&barrier, &sortInput, D3D12_RESOURCE_STATE_GENERIC_READ);
cmdList->ResourceBarrier(1, &barrier);
}
d3d12aid_ComputeRsPs_Set(&rspsBitonicSort[dispatchShaderId], cmdList);
cmdList->SetComputeRootShaderResourceView(0, sortInput.bufGpu->GetGPUVirtualAddress());
cmdList->SetComputeRootUnorderedAccessView(1, sortOutput.bufGpu->GetGPUVirtualAddress());
cmdList->SetComputeRoot32BitConstant(2, options1.TotalLaneCount, 0);
const uint32_t dispatchKernelCount = (kSortKeysPerDispatch + dispatchKernelSize - 1) >> dispatchShaderBytecode->kernelSizeLog2;
const uint32_t dispatchKernelCountY = dispatchKernelCount / options1.TotalLaneCount;
const uint32_t dispatchKernelCountX = options1.TotalLaneCount;
if (frameIndex < kBenchFrameCount)
{
d3d12aid_Timestamps_Push(×tamps, cmdList);
}
cmdList->Dispatch(dispatchKernelCountX, dispatchKernelCountY, 1);
if (frameIndex < kBenchFrameCount)
{
d3d12aid_Timestamps_Push(×tamps, cmdList);
}
{
D3D12_RESOURCE_BARRIER barrier;
d3d12aid_MappedBuffer_BeginTransfer(&barrier, &sortOutput, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
cmdList->ResourceBarrier(1, &barrier);
}
d3d12aid_MappedBuffer_Transfer(cmdList, &sortOutput, kCmdBufferIndex);
if (frameIndex >= kCmdBufferInFlight && frameIndex < kPixFrameStart)
{
const uint32_t readbackFrameIndex = frameIndex - kCmdBufferInFlight;
uint32_t readbackShaderId = 0;
if (readbackFrameIndex >= kCheckFrameStart)
{
readbackShaderId = (readbackFrameIndex - kCheckFrameStart) % kShaderCount;
}
else if (kBenchFrameCount > 0)
{
gpuTimestampDelta[readbackFrameIndex] = d3d12aid_Timestamps_GetDelta(×tamps, kCmdBufferIndex, 0, 1);
readbackShaderId = (readbackFrameIndex / kShaderBenchFrameCount) % kShaderCount;
}
const uint32_t readbackKernelSize = 1u << shaders[readbackShaderId]->kernelSizeLog2;
const uint32_t *readbackData = (const uint32_t *)sortOutput.bufMem[kCmdBufferIndex];
for (uint32_t i = 0; i < kSortKeysPerDispatch; i += readbackKernelSize)
{
for (uint32_t j = i; j < i + readbackKernelSize - 1; ++j)
{
if (!(readbackData[j] >= readbackData[j + 1]))
{
failCount += 1;
break;
}
}
}
}
if (frameIndex < kBenchFrameCount)
{
d3d12aid_Timestamps_AdvanceFrame(×tamps, cmdList);
}
d3d12aid_CmdQueue_SubmitCmdList(&queue, 0);
if (frameIndex >= kPixFrameStart)
{
PIXEndCapture(FALSE);
}
kCmdBufferIndex = (kCmdBufferIndex + 1) % kCmdBufferInFlight;
}
debugPrintF("Completed.\n");
debugPrintF("Waiting for GPU Idle ... ");
d3d12aid_CmdQueue_CpuWaitForGpuIdle(&queue);
debugPrintF("Completed.\n");
D3D12AID_ASSERT(0 == failCount);
if (failCount > 0)
{
debugPrintF("# of failures: %u\n", failCount);
}
debugPrintF("Destroying D3D12 Objects ... ");
for (uint32_t i = 0; i < kShaderCount; ++i)
{
d3d12aid_ComputeRsPs_Release(&rspsBitonicSort[i]);
}
d3d12aid_MappedBuffer_Release(&sortOutput);
d3d12aid_MappedBuffer_Release(&sortInput);
d3d12aid_Timestamps_Release(×tamps);
d3d12aid_CmdQueue_Release(&queue);
debugPrintF("Completed.\n");
if (kBenchFrameCount > 0)
{
PerfData perfData;
perfData_PrintHeader();
for (uint32_t i = 0; i < kBenchFrameCount; ++i)
{
const uint32_t readbackFrameIndex = i;
const uint32_t benchmarkFrameIndex = readbackFrameIndex % kShaderBenchFrameCount;
const uint32_t readbackShaderId = (readbackFrameIndex / kShaderBenchFrameCount) % kShaderCount;
const uint32_t readbackKernelSize = 1u << shaders[readbackShaderId]->kernelSizeLog2;
const uint32_t readbackTGroupSize = 1u << shaders[readbackShaderId]->tgroupSizeLog2;
/** if it's a the first benchmark run for currently selected shader variant, init 'perfData' information */
if (benchmarkFrameIndex == 0)
{
perfData_Init(&perfData, gpuTimestampFreq);
}
perfData_AddSample(&perfData, gpuTimestampDelta[readbackFrameIndex]);
/** if it's a the last benchmark run for currently selected shader variant, print 'perfData' information */
if (benchmarkFrameIndex == kShaderBenchFrameCount - 1u)
{
debugPrintF("[KernelSize=%4u, TGroupSize=%4u, WaveIntrinsics=%1u] ", readbackKernelSize, readbackTGroupSize, readbackShaderId >= kShaderNoWaveIntrinsicsCount ? 1 : 0);
perfData_Print(&perfData, kSortKeysPerDispatch);
}
}
}
}
/**
* Copyright (c) 2025-2026 Pavel Martishevsky
*
* 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.
*/