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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkDirectXGPUInfoList.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkDirectXGPUInfoList.h"
#include "vtkGPUInfoListArray.h"
#include "vtkObjectFactory.h"
#include <cassert>
// DirectX, DXGI api
#include <dxgi.h>
// DirectX, WMI api
#include <wbemidl.h>
#include <ddraw.h> // for LPDIRECTDRAWENUMERATEEXA
vtkStandardNewMacro(vtkDirectXGPUInfoList);
// Used by ProbeInfoWithDXGI.
typedef HRESULT (WINAPI *LPCREATEDXGIFACTORY)(REFIID,void **);
// Used by ProbeInfoWithWMI.
typedef BOOL (WINAPI *PfnCoSetProxyBlanket)(IUnknown *pProxy,
DWORD dwAuthnSvc,
DWORD dwAuthzSvc,
OLECHAR *pServerPrincName,
DWORD dwAuthnLevel,
DWORD dwImpLevel,
RPC_AUTH_IDENTITY_HANDLE pAuthInfo,
DWORD dwCapabilities);
struct DDRAW_MATCH
{
GUID guid;
HMONITOR hMonitor;
CHAR strDriverName[512];
bool bFound;
};
// ----------------------------------------------------------------------------
BOOL WINAPI DDEnumCallbackEx(GUID FAR *lpGUID,
LPSTR vtkNotUsed(lpDriverDescription),
LPSTR lpDriverName,
LPVOID lpContext,
HMONITOR hm)
{
DDRAW_MATCH *pDDMatch=static_cast<DDRAW_MATCH *>(lpContext);
if(pDDMatch->hMonitor==hm)
{
pDDMatch->bFound=true;
size_t l=strlen(lpDriverName);
if(l>511) // because of CHAR strDriverName[512];
{
l=511;
}
strncpy(pDDMatch->strDriverName,lpDriverName,l+1);
if(l==511)
{
pDDMatch->strDriverName[l]='\0';
}
memcpy(&pDDMatch->guid,lpGUID,sizeof(GUID));
}
return TRUE;
}
// ----------------------------------------------------------------------------
// Description:
// Build the list of vtkInfoGPU if not done yet.
// \post probed: IsProbed()
void vtkDirectXGPUInfoList::Probe()
{
if(!this->Probed)
{
this->Probed=true;
this->Array=new vtkGPUInfoListArray;
IDirect3D9 *pD3D9=0;
pD3D9=Direct3DCreate9(D3D_SDK_VERSION);
if(pD3D9!=0)
{
size_t c=pD3D9->GetAdapterCount(); // there are `c' GPUS.
this->Array->v.resize(c);
size_t i=0;
while(i<c)
{
HMONITOR m=pD3D9->GetAdapterMonitor(static_cast<UINT>(i));
vtkGPUInfo *info=vtkGPUInfo::New();
// DXGI API (Windows Vista and later)
bool status=this->ProbeInfoWithDXGI(m,info);
if(!status)
{
// something went bad. Maybe DXGI is not supported or
// the memory was not found.
// Try WMI API (Windows XP)
this->ProbeInfoWithWMI(m,info);
}
this->Array->v[i]=info;
++i;
}
pD3D9->Release();
}
else
{
this->Array->v.resize(0); // no GPU.
}
}
assert("post: probed" && this->IsProbed());
}
// ----------------------------------------------------------------------------
bool vtkDirectXGPUInfoList::ProbeInfoWithDXGI(HMONITOR m,
vtkGPUInfo *info)
{
assert("pre: m_exists" && m!=0);
assert("pre: info_exist" && info!=0);
bool result; // true=supports DXGI and memory found.
// DXGI API initialization
#ifdef UNICODE
HINSTANCE hDXGI=LoadLibrary(L"dxgi.dll");
#else
HINSTANCE hDXGI=LoadLibrary("dxgi.dll");
#endif
result=hDXGI!=0;
if(result)
{
LPCREATEDXGIFACTORY pCreateDXGIFactory=NULL;
IDXGIFactory *pDXGIFactory=NULL;
pCreateDXGIFactory=reinterpret_cast<LPCREATEDXGIFACTORY>(
GetProcAddress(hDXGI,"CreateDXGIFactory"));
pCreateDXGIFactory(__uuidof(IDXGIFactory),
reinterpret_cast<LPVOID *>(&pDXGIFactory));
// Find the adapter that matches monitor m.
IDXGIAdapter *pAdapter=NULL;
bool found=false;
bool done=false;
int i=0;
while(!found && !done)
{
HRESULT hr=pDXGIFactory->EnumAdapters(i,&pAdapter);
done=FAILED(hr); // DXGIERR_NOT_FOUND is expected when the end of the list is hit
if(!done)
{
int j=0;
while(!found && !done)
{
IDXGIOutput *pOutput=NULL;
hr=pAdapter->EnumOutputs(j,&pOutput);
done=FAILED(hr); // DXGIERR_NOT_FOUND is expected when the end of the list is hit
if(!done)
{
DXGI_OUTPUT_DESC outputDesc;
ZeroMemory(&outputDesc,sizeof(DXGI_OUTPUT_DESC));
if(SUCCEEDED(pOutput->GetDesc(&outputDesc)))
{
found=m==outputDesc.Monitor;
}
pOutput->Release();
}
++j;
}
}
++i;
}
if(found)
{
DXGI_ADAPTER_DESC desc;
ZeroMemory(&desc,sizeof(DXGI_ADAPTER_DESC));
result=SUCCEEDED(pAdapter->GetDesc(&desc));
if(result)
{
info->SetDedicatedVideoMemory(desc.DedicatedVideoMemory);
info->SetDedicatedSystemMemory(desc.DedicatedSystemMemory);
info->SetSharedSystemMemory(desc.SharedSystemMemory);
}
}
FreeLibrary(hDXGI);
}
return result;
}
// ----------------------------------------------------------------------------
void vtkDirectXGPUInfoList::ProbeInfoWithWMI(HMONITOR m,
vtkGPUInfo *info)
{
assert("pre: m_exists" && m!=0);
assert("pre: info_exist" && info!=0);
HRESULT hrCoInitialize=CoInitialize(0);
IWbemLocator *pIWbemLocator=NULL;
HRESULT hr=CoCreateInstance(CLSID_WbemLocator,NULL,CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
reinterpret_cast<LPVOID *>(&pIWbemLocator));
if(SUCCEEDED(hr) && pIWbemLocator!=0)
{
// Using the locator, connect to WMI in the given namespace.
BSTR pNamespace=SysAllocString(L"\\\\.\\root\\cimv2");
IWbemServices *pIWbemServices=NULL;
hr=pIWbemLocator->ConnectServer(pNamespace,NULL,NULL,0L,0L,NULL,NULL,
&pIWbemServices);
if(SUCCEEDED(hr) && pIWbemServices!=NULL)
{
HINSTANCE hinstOle32=NULL;
hinstOle32=LoadLibraryW(L"ole32.dll");
if(hinstOle32!=0)
{
PfnCoSetProxyBlanket pfnCoSetProxyBlanket=NULL;
pfnCoSetProxyBlanket=reinterpret_cast<PfnCoSetProxyBlanket>(
GetProcAddress(hinstOle32,"CoSetProxyBlanket"));
if(pfnCoSetProxyBlanket!=NULL)
{
// Switch security level to IMPERSONATE.
pfnCoSetProxyBlanket(pIWbemServices,RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,NULL,RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,NULL,0);
}
FreeLibrary(hinstOle32);
}
IEnumWbemClassObject *pEnumVideoControllers=NULL;
BSTR pClassName=NULL;
pClassName=SysAllocString(L"Win32_VideoController");
hr=pIWbemServices->CreateInstanceEnum(pClassName,0,NULL,
&pEnumVideoControllers);
if(SUCCEEDED(hr) && pEnumVideoControllers!=0)
{
const int videoControllersCapacity=10; // just get the first 10.
IWbemClassObject *pVideoControllers[videoControllersCapacity]={0};
DWORD uReturned=0;
// Get the first one in the list
pEnumVideoControllers->Reset();
hr=pEnumVideoControllers->Next(5000, // timeout in 5 seconds
videoControllersCapacity,
pVideoControllers,&uReturned);
if(SUCCEEDED(hr))
{
WCHAR strInputDeviceID[512];
this->GetDeviceIDFromHMonitor(m,strInputDeviceID,512);
bool found=false;
UINT iController=0;
while(!found && iController<uReturned)
{
BSTR pPropName=SysAllocString(L"PNPDeviceID");
VARIANT var;
hr=pVideoControllers[iController]->Get(pPropName,0L,&var,NULL,
NULL);
if(SUCCEEDED(hr))
{
found=wcsstr( var.bstrVal,strInputDeviceID)!=0;
}
VariantClear(&var);
if(pPropName!=0)
{
SysFreeString(pPropName);
}
if(found)
{
pPropName=SysAllocString(L"AdapterRAM");
hr=pVideoControllers[iController]->Get(pPropName,0L,&var,NULL,
NULL);
if(SUCCEEDED(hr))
{
info->SetDedicatedVideoMemory(var.ulVal);
}
VariantClear(&var);
if(pPropName!=0)
{
SysFreeString(pPropName);
}
}
pVideoControllers[iController]->Release();
++iController;
}
}
}
if(pClassName!=0)
{
SysFreeString(pClassName);
}
if(pEnumVideoControllers!=0)
{
pEnumVideoControllers->Release();
}
}
if(pNamespace!=0)
{
SysFreeString(pNamespace);
}
if(pIWbemServices!=0)
{
pIWbemServices->Release();
}
}
if(pIWbemLocator!=0)
{
pIWbemLocator->Release();
}
if(SUCCEEDED(hrCoInitialize))
{
CoUninitialize();
}
}
// ----------------------------------------------------------------------------
bool vtkDirectXGPUInfoList::GetDeviceIDFromHMonitor(HMONITOR hm,
WCHAR *strDeviceID,
int cchDeviceID)
{
assert("pre: hm_exists" && hm!=0);
assert("pre: strDeviceID_exists" && strDeviceID!=0);
assert("pre: cchDeviceID_is_positive" && cchDeviceID>0);
bool result=false;
#ifdef UNICODE
HINSTANCE hInstDDraw=LoadLibrary(L"ddraw.dll");
#else
HINSTANCE hInstDDraw=LoadLibrary("ddraw.dll");
#endif
if(hInstDDraw!=0)
{
DDRAW_MATCH match;
ZeroMemory(&match,sizeof(DDRAW_MATCH));
match.hMonitor=hm;
LPDIRECTDRAWENUMERATEEXA pDirectDrawEnumerateEx=NULL;
pDirectDrawEnumerateEx=reinterpret_cast<LPDIRECTDRAWENUMERATEEXA>(
GetProcAddress(hInstDDraw, "DirectDrawEnumerateExA"));
if(pDirectDrawEnumerateEx!=0)
{
pDirectDrawEnumerateEx(DDEnumCallbackEx,static_cast<VOID*>(&match),
DDENUM_ATTACHEDSECONDARYDEVICES);
}
if(match.bFound)
{
LONG iDevice=0;
DISPLAY_DEVICEA dispdev;
ZeroMemory(&dispdev,sizeof(dispdev));
dispdev.cb=sizeof(dispdev);
bool done=false;
while(!done && EnumDisplayDevicesA(NULL,iDevice,
static_cast<DISPLAY_DEVICEA *>(&dispdev),0))
{
// Skip devices that are monitors that echo another display
// (DISPLAY_DEVICE_MIRRORING_DRIVER)
// Skip devices that aren't attached since they cause problems
// (DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)
done=!(dispdev.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER)
&& !((dispdev.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)==0)
&& _stricmp( match.strDriverName,dispdev.DeviceName)==0;
if(done)
{
MultiByteToWideChar(CP_ACP,0,dispdev.DeviceID,-1,strDeviceID,
cchDeviceID);
result=true;
}
iDevice++;
}
}
FreeLibrary(hInstDDraw);
}
return result;
}
// ----------------------------------------------------------------------------
vtkDirectXGPUInfoList::vtkDirectXGPUInfoList()
{
}
// ----------------------------------------------------------------------------
vtkDirectXGPUInfoList::~vtkDirectXGPUInfoList()
{
}
// ----------------------------------------------------------------------------
void vtkDirectXGPUInfoList::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
|