-
Notifications
You must be signed in to change notification settings - Fork 23
/
GPUDetect.cpp
809 lines (667 loc) · 19.9 KB
/
GPUDetect.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
////////////////////////////////////////////////////////////////////////////////
// Copyright 2017-2020 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
////////////////////////////////////////////////////////////////////////////////
#ifndef STRICT
#define STRICT
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN
#endif
#include <windows.h>
#include <dxgi.h>
#include <d3d11.h>
#ifdef _WIN32_WINNT_WIN10
#include <d3d11_3.h>
#endif
#include "ID3D10Extensions.h"
#include <winreg.h>
#include <tchar.h>
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include "GPUDetect.h"
// These should only be needed for reading data from the counter
struct IntelDeviceInfo1
{
DWORD GPUMaxFreq;
DWORD GPUMinFreq;
};
static_assert( sizeof(IntelDeviceInfo1) == 8, "struct size mismatch" );
struct IntelDeviceInfo2 : public IntelDeviceInfo1
{
DWORD GPUArchitecture; // should match INTEL_GPU_ARCHITECTURE
DWORD EUCount;
DWORD PackageTDP;
DWORD MaxFillRate;
};
static_assert( sizeof(IntelDeviceInfo2) == 24, "struct size mismatch" );
namespace GPUDetect
{
// Returns EXIT_SUCCESS if successfully initialized
int GetIntelDeviceInfo( IntelDeviceInfo2* deviceInfo, ID3D11Device* device );
int InitAll( GPUData* const gpuData, int adapterIndex )
{
if( gpuData == nullptr || adapterIndex < 0 )
{
return GPUDETECT_ERROR_BAD_DATA;
}
IDXGIAdapter* adapter = nullptr;
int returnCode = InitAdapter( &adapter, adapterIndex );
if( returnCode != EXIT_SUCCESS )
{
return returnCode;
}
ID3D11Device* device = nullptr;
returnCode = InitDevice( adapter, &device );
if( returnCode != EXIT_SUCCESS )
{
adapter->Release();
return returnCode;
}
returnCode = InitAll( gpuData, adapter, device );
adapter->Release();
device->Release();
return returnCode;
}
int InitAll( GPUData* const gpuData, IDXGIAdapter* adapter, ID3D11Device* device )
{
int returnCode = InitExtensionInfo( gpuData, adapter, device );
if( returnCode != EXIT_SUCCESS )
{
return returnCode;
}
returnCode = InitDxDriverVersion( gpuData );
if( returnCode != EXIT_SUCCESS )
{
return returnCode;
}
returnCode = InitCounterInfo( gpuData, device );
return returnCode;
}
int InitExtensionInfo( GPUData* const gpuData, int adapterIndex )
{
if( gpuData == nullptr || adapterIndex < 0 )
{
return GPUDETECT_ERROR_BAD_DATA;
}
IDXGIAdapter* adapter = nullptr;
int returnCode = InitAdapter( &adapter, adapterIndex );
if( returnCode != EXIT_SUCCESS )
{
return returnCode;
}
ID3D11Device* device = nullptr;
returnCode = InitDevice( adapter, &device );
if( returnCode != EXIT_SUCCESS )
{
adapter->Release();
return returnCode;
}
returnCode = InitExtensionInfo( gpuData, adapter, device );
adapter->Release();
device->Release();
return returnCode;
}
int InitExtensionInfo( GPUData* const gpuData, IDXGIAdapter* adapter, ID3D11Device* device )
{
if ( gpuData == nullptr || adapter == nullptr || device == nullptr )
{
return GPUDETECT_ERROR_BAD_DATA;
}
// basic DXGI information
DXGI_ADAPTER_DESC AdapterDesc = {};
if( FAILED( adapter->GetDesc( &AdapterDesc ) ) )
{
return GPUDETECT_ERROR_DXGI_ADAPTER_CREATION;
}
gpuData->dxAdapterAvailability = true;
gpuData->vendorID = AdapterDesc.VendorId;
gpuData->deviceID = AdapterDesc.DeviceId;
gpuData->adapterLUID = AdapterDesc.AdapterLuid;
wcscpy_s( gpuData->description, _countof( GPUData::description ), AdapterDesc.Description );
if( AdapterDesc.VendorId == INTEL_VENDOR_ID && AdapterDesc.DedicatedVideoMemory <= 512 * 1024 * 1024 )
{
gpuData->isUMAArchitecture = true;
}
#ifdef _WIN32_WINNT_WIN10
ID3D11Device3* pDevice3 = nullptr;
if( SUCCEEDED( device->QueryInterface( __uuidof( ID3D11Device3 ), (void**) &pDevice3 ) ) )
{
D3D11_FEATURE_DATA_D3D11_OPTIONS2 FeatureData = {};
if( SUCCEEDED( pDevice3->CheckFeatureSupport( D3D11_FEATURE_D3D11_OPTIONS2, &FeatureData, sizeof( FeatureData ) ) ) )
{
gpuData->isUMAArchitecture = FeatureData.UnifiedMemoryArchitecture == TRUE;
}
pDevice3->Release();
}
#endif // _WIN32_WINNT_WIN10
if( gpuData->isUMAArchitecture )
{
gpuData->videoMemory = AdapterDesc.SharedSystemMemory;
}
else
{
gpuData->videoMemory = AdapterDesc.DedicatedVideoMemory;
}
// Intel specific information
if( AdapterDesc.VendorId == INTEL_VENDOR_ID )
{
gpuData->architecture = GetIntelGPUArchitecture(gpuData->deviceID);
ID3D10::CAPS_EXTENSION intelExtCaps = {};
if (S_OK == GetExtensionCaps(device, &intelExtCaps))
{
gpuData->extensionVersion = intelExtCaps.DriverVersion;
gpuData->intelExtensionAvailability = (gpuData->extensionVersion >= ID3D10::EXTENSION_INTERFACE_VERSION_1_0);
}
}
return EXIT_SUCCESS;
}
int InitCounterInfo( GPUData* const gpuData, int adapterIndex )
{
if( gpuData == nullptr || adapterIndex < 0 )
{
return GPUDETECT_ERROR_BAD_DATA;
}
IDXGIAdapter* adapter = nullptr;
int returnCode = InitAdapter( &adapter, adapterIndex );
if( returnCode != EXIT_SUCCESS )
{
return returnCode;
}
ID3D11Device* device = nullptr;
returnCode = InitDevice( adapter, &device );
if( returnCode != EXIT_SUCCESS )
{
adapter->Release();
return returnCode;
}
returnCode = InitCounterInfo( gpuData, device );
adapter->Release();
return returnCode;
}
int InitCounterInfo( GPUData* const gpuData, ID3D11Device* device )
{
if( gpuData == nullptr || device == nullptr || gpuData->vendorID == 0 || gpuData->deviceID == 0 )
{
return GPUDETECT_ERROR_BAD_DATA;
}
//
// In DirectX, Intel exposes additional information through the driver that can be obtained
// querying a special DX counter
//
gpuData->counterAvailability = gpuData->vendorID == INTEL_VENDOR_ID;
if( !gpuData->counterAvailability )
{
return GPUDETECT_ERROR_NOT_SUPPORTED;
}
IntelDeviceInfo2 info = {};
const int deviceInfoReturnCode = GetIntelDeviceInfo( &info, device );
if (deviceInfoReturnCode != EXIT_SUCCESS)
{
return deviceInfoReturnCode;
}
else
{
gpuData->maxFrequency = info.GPUMaxFreq;
gpuData->minFrequency = info.GPUMinFreq;
//
// Older versions of the IntelDeviceInfo query only return
// GPUMaxFreq and GPUMinFreq, all other members will be zero.
//
if (info.GPUArchitecture != IGFX_UNKNOWN)
{
gpuData->advancedCounterDataAvailability = true;
gpuData->architecture = (INTEL_GPU_ARCHITECTURE)info.GPUArchitecture;
assert(gpuData->architecture == GetIntelGPUArchitecture(gpuData->deviceID));
gpuData->euCount = info.EUCount;
gpuData->packageTDP = info.PackageTDP;
gpuData->maxFillRate = info.MaxFillRate;
}
}
return EXIT_SUCCESS;
}
PresetLevel GetDefaultFidelityPreset( const GPUData* const gpuData )
{
// Return if prerequisite info is not met
if( !gpuData->dxAdapterAvailability )
{
return PresetLevel::Undefined;
}
//
// Look for a config file that qualifies devices from any vendor
// The code here looks for a file with one line per recognized graphics
// device in the following format:
//
// VendorIDHex, DeviceIDHex, CapabilityEnum ;Commented name of card
//
const char* cfgFileName = nullptr;
switch( gpuData->vendorID )
{
case INTEL_VENDOR_ID:
cfgFileName = "IntelGfx.cfg";
break;
// Add other cases in this fashion to allow for additional cfg files
//case SOME_VENDOR_ID:
// cfgFileName = "OtherBrandGfx.cfg";
// break;
default:
return PresetLevel::Undefined;;
}
PresetLevel presets = Undefined;
FILE* fp = nullptr;
fopen_s( &fp, cfgFileName, "r" );
if( fp )
{
char line[ 100 ];
//
// read one line at a time till EOF
//
while( fgets( line, _countof( line ), fp ) )
{
//
// Parse and remove the comment part of any line
//
unsigned int i = 0;
for( ; i < _countof( line ) - 1 && line[ i ] && line[ i ] != ';'; i++ )
{}
line[ i ] = '\0';
//
// Try to extract GPUVendorId, GPUDeviceId and recommended Default Preset Level
//
char* context = nullptr;
const char* const szVendorId = strtok_s( line, ",\n", &context );
const char* const szDeviceId = strtok_s( nullptr, ",\n", &context );
const char* const szPresetLevel = strtok_s( nullptr, ",\n", &context );
if( ( szVendorId == nullptr ) ||
( szDeviceId == nullptr ) ||
( szPresetLevel == nullptr ) )
{
continue; // blank or improper line in cfg file - skip to next line
}
unsigned int vId = 0;
int rv = sscanf_s( szVendorId, "%x", &vId );
assert( rv == 1 );
unsigned int dId = 0;
rv = sscanf_s( szDeviceId, "%x", &dId );
assert( rv == 1 );
//
// If current graphics device is found in the cfg file, use the
// pre-configured default Graphics Presets setting.
//
if( ( vId == gpuData->vendorID ) && ( dId == gpuData->deviceID ) )
{
char s[ 10 ] = {};
sscanf_s( szPresetLevel, "%s", s, (unsigned int) _countof( s ) );
if( !_stricmp( s, "Low" ) )
presets = Low;
else if( !_stricmp( s, "Medium" ) )
presets = Medium;
else if( !_stricmp( s, "Medium+" ) )
presets = MediumPlus;
else if( !_stricmp( s, "High" ) )
presets = High;
else
presets = NotCompatible;
break;
}
}
fclose( fp );
}
else
{
fprintf( stderr, "Error: %s not found! Fallback to default presets.\n", cfgFileName );
}
//
// If the current graphics device was not listed in any of the config
// files, or if config file not found, use Low settings as default.
// This should be changed to reflect the desired behavior for unknown
// graphics devices.
//
if( presets == Undefined )
{
presets = Low;
}
return presets;
}
int InitDxDriverVersion( GPUData* const gpuData )
{
if( gpuData == nullptr || !( gpuData->dxAdapterAvailability == true ) )
{
return GPUDETECT_ERROR_BAD_DATA;
}
if( gpuData->adapterLUID.HighPart == 0 && gpuData->adapterLUID.LowPart == 0 )
{
// This should not happen with an active/current adapter.
// But the registry can contain old leftover driver entries with LUID == 0.
return GPUDETECT_ERROR_BAD_DATA;
}
// Fetch registry data
HKEY dxKeyHandle = nullptr;
DWORD numOfAdapters = 0;
LSTATUS returnCode = ::RegOpenKeyEx( HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\DirectX"), 0, KEY_READ, &dxKeyHandle );
if( returnCode != ERROR_SUCCESS )
{
return GPUDETECT_ERROR_REG_NO_D3D_KEY;
}
// Find all subkeys
DWORD subKeyMaxLength = 0;
returnCode = ::RegQueryInfoKey(
dxKeyHandle,
nullptr,
nullptr,
nullptr,
&numOfAdapters,
&subKeyMaxLength,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr
);
if( returnCode != ERROR_SUCCESS )
{
return GPUDETECT_ERROR_REG_GENERAL_FAILURE;
}
subKeyMaxLength += 1; // include the null character
uint64_t driverVersionRaw = 0;
bool foundSubkey = false;
TCHAR* subKeyName = new TCHAR[subKeyMaxLength];
for( DWORD i = 0; i < numOfAdapters; ++i )
{
DWORD subKeyLength = subKeyMaxLength;
returnCode = ::RegEnumKeyEx(
dxKeyHandle,
i,
subKeyName,
&subKeyLength,
nullptr,
nullptr,
nullptr,
nullptr
);
if( returnCode == ERROR_SUCCESS )
{
LUID adapterLUID = {};
DWORD qwordSize = sizeof( uint64_t );
returnCode = ::RegGetValue(
dxKeyHandle,
subKeyName,
_T("AdapterLuid"),
RRF_RT_QWORD,
nullptr,
&adapterLUID,
&qwordSize
);
if( returnCode == ERROR_SUCCESS // If we were able to retrieve the registry values
&& adapterLUID.HighPart == gpuData->adapterLUID.HighPart && adapterLUID.LowPart == gpuData->adapterLUID.LowPart ) // and if the vendor ID and device ID match
{
// We have our registry key! Let's get the driver version num now
returnCode = ::RegGetValue(
dxKeyHandle,
subKeyName,
_T("DriverVersion"),
RRF_RT_QWORD,
nullptr,
&driverVersionRaw,
&qwordSize
);
if( returnCode == ERROR_SUCCESS )
{
foundSubkey = true;
break;
}
}
}
}
returnCode = ::RegCloseKey( dxKeyHandle );
assert( returnCode == ERROR_SUCCESS );
delete[] subKeyName;
if( !foundSubkey )
{
return GPUDETECT_ERROR_REG_MISSING_DRIVER_INFO;
}
// Now that we have our driver version as a DWORD, let's process that into something readable
gpuData->dxDriverVersion[ 0 ] = (unsigned int) ( ( driverVersionRaw & 0xFFFF000000000000 ) >> 16 * 3 );
gpuData->dxDriverVersion[ 1 ] = (unsigned int) ( ( driverVersionRaw & 0x0000FFFF00000000 ) >> 16 * 2 );
gpuData->dxDriverVersion[ 2 ] = (unsigned int) ( ( driverVersionRaw & 0x00000000FFFF0000 ) >> 16 * 1 );
gpuData->dxDriverVersion[ 3 ] = (unsigned int) ( ( driverVersionRaw & 0x000000000000FFFF ) );
gpuData->driverInfo.driverReleaseRevision = gpuData->dxDriverVersion[2];
gpuData->driverInfo.driverBuildNumber = gpuData->dxDriverVersion[3];
gpuData->d3dRegistryDataAvailability = true;
return EXIT_SUCCESS;
}
void GetDriverVersionAsCString( const GPUData* const gpuData, char* const outBuffer, size_t outBufferSize )
{
// let's assume 4 digits max per segment
const size_t kMaxBufferSize = (4 * 4) + 3;
if( gpuData != nullptr && outBuffer != nullptr && outBufferSize <= kMaxBufferSize)
{
sprintf_s( outBuffer, outBufferSize, "%u.%u.%u.%u", gpuData->dxDriverVersion[ 0 ], gpuData->dxDriverVersion[ 1 ], gpuData->dxDriverVersion[ 2 ], gpuData->dxDriverVersion[ 3 ] );
}
}
GPUDetect::IntelGraphicsGeneration GetIntelGraphicsGeneration( INTEL_GPU_ARCHITECTURE architecture )
{
switch( architecture )
{
case IGFX_SANDYBRIDGE:
return INTEL_GFX_GEN6;
case IGFX_IVYBRIDGE:
case IGFX_VALLEYVIEW:
return INTEL_GFX_GEN7;
case IGFX_HASWELL:
return INTEL_GFX_GEN7_5;
case IGFX_BROADWELL:
case IGFX_CHERRYVIEW:
return INTEL_GFX_GEN8;
case IGFX_SKYLAKE:
return INTEL_GFX_GEN9;
case IGFX_GEMINILAKE:
case IGFX_KABYLAKE:
case IGFX_WHISKEYLAKE:
case IGFX_COFFEELAKE:
case IGFX_COMETLAKE:
return INTEL_GFX_GEN9_5;
case IGFX_CANNONLAKE:
return INTEL_GFX_GEN10;
case IGFX_LAKEFIELD:
case IGFX_ICELAKE:
case IGFX_ICELAKE_LP:
return INTEL_GFX_GEN11;
case IGFX_TIGERLAKE_LP:
case IGFX_DG1:
case IGFX_ROCKETLAKE:
case IGFX_ADL:
return INTEL_GFX_GEN12;
case DGFX_ACM:
return INTEL_DGFX_ACM;
default:
return INTEL_GFX_GEN_UNKNOWN;
}
}
const char * GetIntelGraphicsGenerationString( const IntelGraphicsGeneration generation)
{
switch( generation )
{
case INTEL_GFX_GEN6: return "Gen6";
case INTEL_GFX_GEN7: return "Gen7";
case INTEL_GFX_GEN7_5: return "Gen7.5";
case INTEL_GFX_GEN8: return "Gen8";
case INTEL_GFX_GEN9: return "Gen9";
case INTEL_GFX_GEN9_5: return "Gen9.5";
case INTEL_GFX_GEN10: return "Gen10";
case INTEL_GFX_GEN11: return "Gen11";
case INTEL_GFX_GEN12: return "Gen12 / Xe";
case INTEL_DGFX_ACM: return "Xe High Performance Graphics";
case INTEL_GFX_GEN_UNKNOWN:
default: return "Unkown";
}
}
int InitAdapter( IDXGIAdapter** adapter, int adapterIndex )
{
if( adapter == nullptr || adapterIndex < 0 )
{
return GPUDETECT_ERROR_BAD_DATA;
}
//
// We are relying on DXGI (supported on Windows Vista and later) to query
// the adapter, so fail if it is not available.
//
// DXGIFactory1 is required by Windows Store Apps so try that first.
//
const HMODULE hDXGI = ::LoadLibrary( _T("dxgi.dll") );
if( hDXGI == nullptr )
{
return GPUDETECT_ERROR_DXGI_LOAD;
}
typedef HRESULT( WINAPI*LPCREATEDXGIFACTORY )( REFIID riid, void** ppFactory );
LPCREATEDXGIFACTORY pCreateDXGIFactory = (LPCREATEDXGIFACTORY) ::GetProcAddress( hDXGI, "CreateDXGIFactory1" );
if( pCreateDXGIFactory == nullptr )
{
pCreateDXGIFactory = (LPCREATEDXGIFACTORY) ::GetProcAddress( hDXGI, "CreateDXGIFactory" );
if( pCreateDXGIFactory == nullptr )
{
::FreeLibrary( hDXGI );
return GPUDETECT_ERROR_DXGI_FACTORY_CREATION;
}
}
//
// We have the CreateDXGIFactory function so use it to actually create the factory and enumerate
// through the adapters. Here, we are specifically looking for the Intel gfx adapter.
//
IDXGIFactory* pFactory = nullptr;
if( FAILED( pCreateDXGIFactory( __uuidof( IDXGIFactory ), (void**) ( &pFactory ) ) ) )
{
::FreeLibrary( hDXGI );
return GPUDETECT_ERROR_DXGI_FACTORY_CREATION;
}
if( FAILED( pFactory->EnumAdapters( adapterIndex, (IDXGIAdapter**) adapter ) ) )
{
pFactory->Release();
::FreeLibrary( hDXGI );
return GPUDETECT_ERROR_DXGI_ADAPTER_CREATION;
}
pFactory->Release();
::FreeLibrary( hDXGI );
return EXIT_SUCCESS;
}
int InitDevice( IDXGIAdapter* adapter, ID3D11Device** device )
{
if ( device == nullptr )
{
return GPUDETECT_ERROR_BAD_DATA;
}
if( FAILED( ::D3D11CreateDevice( adapter, D3D_DRIVER_TYPE_UNKNOWN, nullptr, 0, nullptr, 0, D3D11_SDK_VERSION, device, nullptr, nullptr ) ) )
{
return GPUDETECT_ERROR_DXGI_DEVICE_CREATION;
}
return EXIT_SUCCESS;
}
int GetIntelDeviceInfo( IntelDeviceInfo2* deviceInfo, ID3D11Device* device )
{
assert( deviceInfo != nullptr );
assert( device != nullptr );
//
// Grab the device context from the device.
//
ID3D11DeviceContext* deviceContext = nullptr;
device->GetImmediateContext( &deviceContext );
//
// Query the device to find the number of device dependent counters.
//
D3D11_COUNTER_INFO counterInfo = {};
device->CheckCounterInfo( &counterInfo );
if( counterInfo.LastDeviceDependentCounter == 0 )
{
deviceContext->Release();
return GPUDETECT_ERROR_DXGI_BAD_COUNTER;
}
//
// Search for the "Intel Device Information" counter and, if found, parse
// it's description to determine the supported version.
//
D3D11_COUNTER_DESC counterDesc = {};
int intelDeviceInfoVersion = 0;
bool intelDeviceInfo = false;
for( int i = D3D11_COUNTER_DEVICE_DEPENDENT_0; i <= counterInfo.LastDeviceDependentCounter; ++i )
{
counterDesc.Counter = static_cast<D3D11_COUNTER>( i );
D3D11_COUNTER_TYPE counterType = {};
UINT uiSlotsRequired = 0;
UINT uiNameLength = 0;
UINT uiUnitsLength = 0;
UINT uiDescLength = 0;
if( FAILED( device->CheckCounter( &counterDesc, &counterType, &uiSlotsRequired, nullptr, &uiNameLength, nullptr, &uiUnitsLength, nullptr, &uiDescLength ) ) )
{
continue;
}
LPSTR sName = new char[ uiNameLength ];
LPSTR sUnits = new char[ uiUnitsLength ];
LPSTR sDesc = new char[ uiDescLength ];
intelDeviceInfo =
SUCCEEDED( device->CheckCounter( &counterDesc, &counterType, &uiSlotsRequired, sName, &uiNameLength, sUnits, &uiUnitsLength, sDesc, &uiDescLength ) ) &&
( strcmp( sName, "Intel Device Information" ) == 0 );
if( intelDeviceInfo )
{
sscanf_s( sDesc, "Version %d", &intelDeviceInfoVersion );
}
delete[] sName;
delete[] sUnits;
delete[] sDesc;
if( intelDeviceInfo )
{
break;
}
}
//
// Create the information counter, and query it to get the data. GetData()
// returns a pointer to the data, not the actual data.
//
ID3D11Counter* counter = nullptr;
if( !intelDeviceInfo || FAILED( device->CreateCounter( &counterDesc, &counter ) ) )
{
deviceContext->Release();
return GPUDETECT_ERROR_DXGI_COUNTER_CREATION;
}
deviceContext->Begin( counter );
deviceContext->End( counter );
uintptr_t dataAddress = 0;
if( deviceContext->GetData( counter, reinterpret_cast<void*>(&dataAddress), sizeof( dataAddress ), 0 ) != S_OK )
{
counter->Release();
deviceContext->Release();
return GPUDETECT_ERROR_DXGI_COUNTER_GET_DATA;
}
//
// Copy the information into the user's structure
//
assert( intelDeviceInfoVersion == 1 || intelDeviceInfoVersion == 2 );
const size_t infoSize = intelDeviceInfoVersion == 1
? sizeof( IntelDeviceInfo1 )
: sizeof( IntelDeviceInfo2 );
assert( infoSize <= sizeof( *deviceInfo ) );
memset( deviceInfo, 0, sizeof( *deviceInfo ) );
memcpy( deviceInfo, reinterpret_cast<const void*>( dataAddress ), infoSize );
counter->Release();
deviceContext->Release();
return EXIT_SUCCESS;
}
}