-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGpuCapabilityTester.cpp
550 lines (468 loc) · 13.8 KB
/
GpuCapabilityTester.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
#include "GpuCapabilityTester.h"
#include <cassert>
#include <fstream>
#include <iostream>
#include <d3d9.h>
#include <d3d11.h>
#define SAFE_RELEASE( x ) if ( x ) { x->Release(); x = nullptr; }
using namespace std;
const char gOutputFileName[] = "GpuCapability.log";
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
bool CheckGraphicsAdapterBlacklisted( const wchar_t* _strAdapterDesc, const wchar_t* _srtBlacklist, wchar_t _cBlDelimiter );
GpuCapabilities GpuCapabilityTester::FindOptimalAdapter( const GpuRequirements& _Reqs )
{
IDXGIFactory* pFactory = nullptr;
// Clear caps
GpuCapabilities caps;
GpuCapabilities bestDeviceCaps;
memset( &caps, 0, sizeof( GpuCapabilities ) );
memset( &bestDeviceCaps, 0, sizeof( GpuCapabilities ) );
// Reset output file, if any
PrepareOutputFile();
// Creating a window, for the later usage in Direct3D9 Device creation
WNDCLASS windowClass = { 0 };
windowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
windowClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
windowClass.hInstance = nullptr;
windowClass.lpfnWndProc = WndProc;
windowClass.lpszClassName = TEXT("A dummy Window"); //needs to be the same name
//when creating the window as well
windowClass.style = CS_HREDRAW | CS_VREDRAW;
//also register the class
if (!RegisterClass(&windowClass))
{
LogMessageLine("Could not register a Window class");
}
HWND windowHandle = CreateWindowA("A dummy Window",
nullptr,
WS_POPUP, //borderless
0, //x coordinate of window start point
0, //y start point
GetSystemMetrics(SM_CXSCREEN), //width of window; this function
//retrieves the screen resolution.
GetSystemMetrics(SM_CYSCREEN), //height of the window
nullptr, //handles and such, not needed
nullptr,
nullptr,
nullptr);
//ShowWindow(windowHandle, SW_RESTORE);
// Define DirectX 9 Creation Parameters
unsigned int uCreationFlags = D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE | D3DCREATE_HARDWARE_VERTEXPROCESSING;
D3DPRESENT_PARAMETERS presentParams;
ZeroMemory(&presentParams, sizeof(presentParams));
presentParams.BackBufferFormat = D3DFMT_A8R8G8B8;
presentParams.BackBufferCount = 1;
presentParams.MultiSampleType = D3DMULTISAMPLE_NONE;
presentParams.MultiSampleQuality = 0;
presentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
presentParams.hDeviceWindow = windowHandle;
presentParams.Windowed = TRUE;
presentParams.EnableAutoDepthStencil = TRUE;
presentParams.AutoDepthStencilFormat = D3DFMT_D24S8;
presentParams.Flags = 0;
presentParams.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
presentParams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
presentParams.BackBufferWidth = 2;
presentParams.BackBufferHeight = 2;
LogMessageLine("### GPU Capability Test ###");
LogMessageFormatted(" Is BGRA feature required: %s\n", _Reqs.m_bSupportBgra ? "TRUE" : "FALSE");
// Create a DXGIFactory object.
if (FAILED(CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&pFactory))))
{
LogMessageLine("\nERROR: Unable to create IDXGI Factory.");
}
IDXGIAdapter* pAdapter;
size_t bestRank = 0;
DXGI_ADAPTER_DESC bestDesc;
UINT i = 0;
for (; pFactory->EnumAdapters(i, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++i)
{
DXGI_ADAPTER_DESC adapterDesc;
pAdapter->GetDesc(&adapterDesc);
bool bIsSoftware = false;
// Skip Microsoft Basic Render Driver
if (wcscmp(adapterDesc.Description, L"Microsoft Basic Render Driver") == 0)
{
bIsSoftware = true;
}
LogMessageFormattedW(L"\nExamining Graphics Adapter: %s\n", adapterDesc.Description);
LogMessageFormattedW(L" VRAM: %dMb\n", adapterDesc.DedicatedVideoMemory >> 20);
LogMessageFormattedW(L" DeviceId: %d\n", adapterDesc.DeviceId);
LogMessageLine("\n Visual Xccelerator Engine Direct3D9 Compatibility");
size_t rank = 0;
size_t dx9AdapterIndex = 0;
LogMessage( " Determines whether the adapter is blacklisted due to its unstable work... " );
bool bBlacklisted = CheckGraphicsAdapterBlacklisted( adapterDesc.Description, _Reqs.m_srtBlacklist.c_str(), _Reqs.m_cBlDelimiter );
LogMessageLine(bBlacklisted ? "TRUE" : "FALSE");
if (!bBlacklisted)
{
rank += 1000000;
}
if ( !bIsSoftware )
{
rank += 1000000;
}
LogMessage(" Trying to create Direct3D9 Device... ");
bool bD3d9Success;
try
{
IDirect3D9* pD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
IDirect3DDevice9* pD3dDevice9 = nullptr;
size_t iNumberAdapters = pD3d9->GetAdapterCount();
for (; dx9AdapterIndex < iNumberAdapters; ++dx9AdapterIndex)
{
D3DADAPTER_IDENTIFIER9 ai;
pD3d9->GetAdapterIdentifier(dx9AdapterIndex, 0, &ai);
if (ai.DeviceId != adapterDesc.DeviceId)
{
continue;
}
pD3d9->CreateDevice(dx9AdapterIndex, D3DDEVTYPE_HAL, nullptr, uCreationFlags, &presentParams, &pD3dDevice9);
break;
}
bD3d9Success = pD3dDevice9 != nullptr;
SAFE_RELEASE(pD3dDevice9);
SAFE_RELEASE(pD3d9);
}
catch (...)
{
bD3d9Success = false;
}
LogMessageLine( bD3d9Success ? "SUCCESS" : "FAILED");
caps.m_bD3d9Support = bD3d9Success;
LogMessageLine("\n Visual Xccelerator Engine Direct3D11 Compatibility");
LogMessage(" Trying to create Direct3D9Ex Device (WPF Compatibility)... ");
bool bD3d9ExSuccess;
try
{
IDirect3D9Ex* pD3d9 = nullptr;
IDirect3DDevice9Ex* pD3dDevice9 = nullptr;
bD3d9ExSuccess =
SUCCEEDED(Direct3DCreate9Ex(D3D_SDK_VERSION, &pD3d9)) &&
SUCCEEDED(pD3d9->CreateDeviceEx(dx9AdapterIndex, D3DDEVTYPE_HAL, nullptr, uCreationFlags, &presentParams, nullptr, &pD3dDevice9));
SAFE_RELEASE(pD3dDevice9);
SAFE_RELEASE(pD3d9);
}
catch (...)
{
bD3d9ExSuccess = false;
}
LogMessageLine( bD3d9ExSuccess ? "SUCCESS" : "FAILED");
LogMessage(" Trying to create Direct3D11 Device... ");
ID3D11Device* pDevice;
D3D_FEATURE_LEVEL featureLevel;
bool bD3d11Success;
try
{
bD3d11Success = SUCCEEDED(D3D11CreateDevice(pAdapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr,
_Reqs.m_bSupportBgra ? D3D11_CREATE_DEVICE_BGRA_SUPPORT : 0, nullptr, 0, 7,
&pDevice, &featureLevel, nullptr));
}
catch (...)
{
bD3d11Success = false;
}
LogMessageLine(bD3d11Success ? "SUCCESS" : "FAILED");
caps.m_bD3d11Support = bD3d11Success;
if (bD3d11Success)
{
// Is Features Level sufficient to run Visual Xccelerator Engine using Direct3D11?
if (static_cast< size_t >(featureLevel) >= _Reqs.m_D3d11MinFeatureLevel)
{
rank += 1000000;
}
else
{
switch (_Reqs.m_D3d11MinFeatureLevel)
{
case D3D_FEATURE_LEVEL_10_1:
LogMessageLine("\n NOTE: the Graphics Adapter does not support Feature Level 10.1");
break;
case D3D_FEATURE_LEVEL_11_0:
LogMessageLine("\n NOTE: the Graphics Adapter does not support Feature Level 11.0");
break;
default:
LogMessageLine("\n NOTE: the Graphics Adapter does not support specified Feature Level.");
break;
}
LogMessageLine(" The Visual Xccelerator Engine will use Direct3D 9, if this adapter is being used.");
LogMessageLine(" This might tend to low performance or visual errors.");
}
}
else if ( !bD3d9ExSuccess && bD3d11Success )
{
LogMessageLine( "\n NOTE: the adapter is able to create Direct3D11 Device," );
LogMessageLine( " however for some reason it's unable to create DirectX9Ex Device." );
LogMessageLine( " It looks like the adapter is not connected to a monitor." );
LogMessageLine( " If you would like to run the SciChart on this adapter," );
LogMessageLine( " please make sure that the monitor cable is plugged in." );
}
// Rank memory (in megabytes)
rank += adapterDesc.DedicatedVideoMemory >> 20;
// Print the rank
LogMessageFormatted( "\n Rank: %d Points\n", rank );
if (rank >= bestRank)
{
// Check if Low memory mode is required
bool bLowMem = (static_cast<double>(adapterDesc.DedicatedVideoMemory) / _Reqs.m_uLowVRamThreshold) < 1.0;
bestDeviceCaps.m_uAdapterDeviceId = adapterDesc.DeviceId;
bestDeviceCaps.m_bD3d9Support = bD3d9Success;
bestDeviceCaps.m_bD3d11Support = bD3d11Success;
bestDeviceCaps.m_bLowVRam = bLowMem;
bestDeviceCaps.m_bBlacklisted = bBlacklisted;
bestDeviceCaps.m_bIsSoftware = bIsSoftware;
bestRank = rank;
bestDesc = adapterDesc;
}
SAFE_RELEASE(pDevice);
SAFE_RELEASE(pAdapter);
}
if(i == 0)
{
LogMessageFormatted("\nNo Graphics Adapters were found.\n");
}
if ( bestDeviceCaps.m_uAdapterDeviceId )
{
LogMessageFormatted("\nSelected Graphics Adapter, where DeviceId is: %d\n", bestDeviceCaps.m_uAdapterDeviceId );
LogMessageFormatted(" Is Direct3D9 Supported: %s\n", bestDeviceCaps.m_bD3d9Support ? "TRUE" : "FALSE");
LogMessageFormatted(" Is Direct3D11 Supported: %s\n", bestDeviceCaps.m_bD3d11Support ? "TRUE" : "FALSE");
LogMessageFormatted(" Is Blacklisted: %s\n", bestDeviceCaps.m_bBlacklisted? "TRUE" : "FALSE");
}
if ( bestDeviceCaps.m_bIsSoftware )
{
LogMessageFormatted( "\nWarning : Graphics is using software emulation, if you are using a VM, please consider enabling 3D acceleration\n" );
}
if (m_bOutputFileReady)
{
m_bOutputFileReady = false;
LogMessage("\nPlease find the log file here: ");
char caFullPath[2048];
if (GetFullPathNameA(gOutputFileName, 2048, caFullPath, nullptr))
{
LogMessageLine(caFullPath);
}
else
{
LogMessage("\nOops, for some reason the file name cannot be obtained.");
}
}
// Print friendly message
if ( bestDeviceCaps.m_bLowVRam )
{
LogMessageFormattedW( L"\nHey this is SciChart here. Please help! Your GPU is too slow for my awesome graphics software! I detected you have an %s GPU. Please upgrade it because I'm feeling very constrained by %dMB of Video RAM. My super-powerful Visual Xccelerator engine can do so much better with 256MB+ of video memory. THX! :D", bestDesc.Description, bestDesc.DedicatedVideoMemory >> 20 );
}
// Output to string
bestDeviceCaps.m_srtLogMessages = m_StringStream.str();
DeleteObject(windowHandle);
SAFE_RELEASE(pFactory);
return bestDeviceCaps;
}
void GpuCapabilityTester::LogMessage(const char* _acMsg)
{
if (m_bOutputToDebug)
{
OutputDebugStringA(_acMsg);
}
if (m_bOutputToConsole)
{
cout << _acMsg;
}
if (m_bOutputToFile)
{
OutputToFile(_acMsg);
}
if ( m_bOutputToString )
{
m_StringStream << _acMsg;
}
}
void GpuCapabilityTester::LogMessageFormatted(const char* _acFormat, ...)
{
char aBuffer[2048];
va_list _ArgList;
__crt_va_start(_ArgList, _acFormat);
#pragma warning(suppress:28719) // 28719
vsnprintf(aBuffer, 2048, _acFormat, _ArgList);
__crt_va_end(_ArgList);
if (m_bOutputToDebug)
{
OutputDebugStringA(aBuffer);
}
if (m_bOutputToConsole)
{
cout << aBuffer;
}
if (m_bOutputToFile)
{
OutputToFile(aBuffer);
}
if ( m_bOutputToString )
{
m_StringStream << aBuffer;
}
}
void GpuCapabilityTester::LogMessageW(const wchar_t* _acMsg)
{
if (m_bOutputToDebug)
{
OutputDebugStringW(_acMsg);
}
if (m_bOutputToConsole)
{
wcout << _acMsg;
}
if (m_bOutputToFile)
{
OutputToFileW(_acMsg);
}
if ( m_bOutputToString )
{
m_StringStream << _acMsg;
}
}
void GpuCapabilityTester::LogMessageFormattedW(const wchar_t* _acFormat, ...)
{
wchar_t aBuffer[2048];
va_list _ArgList;
__crt_va_start(_ArgList, _acFormat);
#pragma warning(suppress:28719) // 28719
vswprintf(aBuffer, 2048, _acFormat, _ArgList);
__crt_va_end(_ArgList);
if (m_bOutputToDebug)
{
OutputDebugStringW(aBuffer);
}
if (m_bOutputToConsole)
{
wcout << aBuffer;
}
if (m_bOutputToFile)
{
OutputToFileW(aBuffer);
}
if ( m_bOutputToString )
{
m_StringStream << aBuffer;
}
}
void GpuCapabilityTester::LogMessageLine(const char* _acMsg)
{
if (m_bOutputToDebug)
{
OutputDebugStringA(_acMsg);
OutputDebugStringA("\n");
}
if (m_bOutputToConsole)
{
cout << _acMsg << endl;
}
if (m_bOutputToFile)
{
OutputToFile(_acMsg);
OutputToFile("\n");
}
if (m_bOutputToString)
{
m_StringStream << _acMsg << endl;
}
}
void GpuCapabilityTester::LogMessageLineW(const wchar_t* _acMsg)
{
if (m_bOutputToDebug)
{
OutputDebugStringW(_acMsg);
OutputDebugStringW(L"\n");
}
if (m_bOutputToConsole)
{
wcout << _acMsg << endl;
}
if (m_bOutputToFile)
{
OutputToFileW(_acMsg);
OutputToFileW(L"\n");
}
if ( m_bOutputToString )
{
m_StringStream << _acMsg << endl;
}
}
void GpuCapabilityTester::OutputToFile(const char* _acMsg)
{
if (m_bOutputFileReady)
{
ofstream ofs;
ofs.open(gOutputFileName, ofstream::app);
if (ofs.is_open())
{
ofs << _acMsg;
ofs.close();
}
else
{
m_bOutputFileReady = false;
}
}
}
void GpuCapabilityTester::PrepareOutputFile()
{
m_bOutputFileReady = false;
if (m_bOutputToFile)
{
ofstream ofs;
ofs.open(gOutputFileName, ofstream::trunc);
m_bOutputFileReady = ofs.is_open();
ofs.close();
}
}
void GpuCapabilityTester::OutputToFileW(const wchar_t* _acMsg)
{
if (m_bOutputFileReady)
{
wofstream ofs;
ofs.open(gOutputFileName, ofstream::app);
if (ofs.is_open())
{
ofs << _acMsg;
ofs.close();
}
else
{
m_bOutputFileReady = false;
}
}
}
bool CheckGraphicsAdapterBlacklisted( const wchar_t* _strAdapterDesc, const wchar_t* _srtBlacklist, wchar_t _cBlDelimiter )
{
const size_t _srtBlacklistLen = _srtBlacklist == nullptr ? 0 : wcslen( _srtBlacklist );
if ( _srtBlacklistLen )
{
const wchar_t* ptrEndPos;
const wchar_t* ptrBeginPos = _srtBlacklist;
do
{
ptrEndPos = wcschr( ptrBeginPos, _cBlDelimiter );
size_t count = ptrEndPos == nullptr
? _srtBlacklistLen - (ptrBeginPos - _srtBlacklist)
: ptrEndPos - ptrBeginPos;
if ( !wcsncmp( _strAdapterDesc, ptrBeginPos, count ) )
{
return true;
}
ptrBeginPos = ptrEndPos + 1;
} while ( ptrEndPos != nullptr );
}
return false;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hwnd, message, wparam, lparam );
}