Rise
The Vieneo Province
Loading...
Searching...
No Matches
DeviceDiagnostics11.h
Go to the documentation of this file.
1#pragma once
2#include <d3d11.h>
3#include <dxgi1_4.h>
4#include <psapi.h>
5#include <wrl/client.h>
6#include <cstdio>
7#include <string>
8#include <algorithm>
9#include <cstdint>
10
11namespace Rendering
12{
13 // Ordinary failure/recovery diagnostics only; never called by the crash
14 // handler. Fixed stack storage avoids a large allocation under pressure.
15 inline std::string MemoryDiagnostics11(ID3D11Device* device)
16 {
17 PROCESS_MEMORY_COUNTERS_EX process{};
18 const BOOL processOk=K32GetProcessMemoryInfo(GetCurrentProcess(),
19 reinterpret_cast<PROCESS_MEMORY_COUNTERS*>(&process),sizeof(process));
20 MEMORYSTATUSEX system{};system.dwLength=sizeof(system);
21 const BOOL systemOk=GlobalMemoryStatusEx(&system);
22 SIZE_T freeBytes=0,largestFree=0;
23 uintptr_t cursor=0;MEMORY_BASIC_INFORMATION region{};
24 while(VirtualQuery(reinterpret_cast<void*>(cursor),&region,sizeof(region)))
25 {
26 if(region.State==MEM_FREE){freeBytes+=region.RegionSize;largestFree=(std::max)(largestFree,region.RegionSize);}
27 const auto next=reinterpret_cast<uintptr_t>(region.BaseAddress)+region.RegionSize;
28 if(next<=cursor)break;cursor=next;
29 }
30 Microsoft::WRL::ComPtr<IDXGIDevice> dxgi;
31 Microsoft::WRL::ComPtr<IDXGIAdapter> adapter;
32 Microsoft::WRL::ComPtr<IDXGIAdapter3> budgets;
33 DXGI_QUERY_VIDEO_MEMORY_INFO local{},shared{};
34 HRESULT localResult=E_NOINTERFACE,sharedResult=E_NOINTERFACE;
35 if(device && SUCCEEDED(device->QueryInterface(IID_PPV_ARGS(&dxgi))) &&
36 SUCCEEDED(dxgi->GetAdapter(&adapter)) && SUCCEEDED(adapter.As(&budgets)))
37 {
38 localResult=budgets->QueryVideoMemoryInfo(0,DXGI_MEMORY_SEGMENT_GROUP_LOCAL,&local);
39 sharedResult=budgets->QueryVideoMemoryInfo(0,DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL,&shared);
40 }
41 char text[768]{};
42 sprintf_s(text,"Memory bytes: processQuery=%s private=%llu workingSet=%llu virtualFree=%llu largestFreeBlock=%llu "
43 "systemQuery=%s commitAvailable=%llu physicalAvailable=%llu "
44 "GPUlocalQuery=0x%08lx usage=%llu budget=%llu GPUsharedQuery=0x%08lx usage=%llu budget=%llu",
45 processOk ? "ok" : "unavailable",static_cast<unsigned long long>(process.PrivateUsage),
46 static_cast<unsigned long long>(process.WorkingSetSize),static_cast<unsigned long long>(freeBytes),
47 static_cast<unsigned long long>(largestFree),systemOk ? "ok" : "unavailable",
48 system.ullAvailPageFile,system.ullAvailPhys,static_cast<unsigned long>(localResult),local.CurrentUsage,local.Budget,
49 static_cast<unsigned long>(sharedResult),shared.CurrentUsage,shared.Budget);
50 return text;
51 }
52 // Query the adapter that owns the device (important on hybrid laptops).
53 // Diagnostics must not make an otherwise supported device fail startup.
54 inline std::string DriverDiagnostics11(ID3D11Device* device)
55 {
56 Microsoft::WRL::ComPtr<IDXGIDevice> dxgi;
57 Microsoft::WRL::ComPtr<IDXGIAdapter> adapter;
58 DXGI_ADAPTER_DESC description{};
59 HRESULT hr=device ? device->QueryInterface(IID_PPV_ARGS(&dxgi)) : E_POINTER;
60 if(SUCCEEDED(hr))hr=dxgi->GetAdapter(&adapter);
61 if(SUCCEEDED(hr))hr=adapter->GetDesc(&description);
62 char detail[768]{};
63 if(FAILED(hr))
64 {
65 sprintf_s(detail,"D3D11 driver: adapter information unavailable HRESULT=0x%08lx",static_cast<unsigned long>(hr));
66 return detail;
67 }
68 char name[512]{};
69 WideCharToMultiByte(CP_UTF8,0,description.Description,-1,name,sizeof(name),nullptr,nullptr);
70 LARGE_INTEGER version{};
71 // IDXGIDevice is the documented version query; ID3D11Device is not
72 // supported by CheckInterfaceSupport. Log the full Windows tuple.
73 const HRESULT versionResult=adapter->CheckInterfaceSupport(__uuidof(IDXGIDevice),&version);
74 char versionText[96]{};
75 if(SUCCEEDED(versionResult))
76 sprintf_s(versionText,"%u.%u.%u.%u",unsigned(HIWORD(version.HighPart)),unsigned(LOWORD(version.HighPart)),
77 unsigned(HIWORD(version.LowPart)),unsigned(LOWORD(version.LowPart)));
78 else sprintf_s(versionText,"unavailable (HRESULT=0x%08lx)",static_cast<unsigned long>(versionResult));
79 sprintf_s(detail,"D3D11 driver: adapter=%s driverVersion=%s vendorId=0x%04x deviceId=0x%04x subSysId=0x%08x revision=0x%02x",
80 name,versionText,description.VendorId,description.DeviceId,description.SubSysId,description.Revision);
81 return detail;
82 }
83
84 inline bool IsDeviceRemovalError11(HRESULT result)
85 {
86 return result==DXGI_ERROR_DEVICE_REMOVED || result==DXGI_ERROR_DEVICE_RESET ||
87 result==DXGI_ERROR_DEVICE_HUNG || result==DXGI_ERROR_DRIVER_INTERNAL_ERROR;
88 }
89 inline const char* DeviceRemovalReasonName11(HRESULT reason)
90 {
91 switch(reason)
92 {
93 case S_OK:return "S_OK (device reports healthy)";
94 case DXGI_ERROR_DEVICE_REMOVED:return "DXGI_ERROR_DEVICE_REMOVED";
95 case DXGI_ERROR_DEVICE_RESET:return "DXGI_ERROR_DEVICE_RESET";
96 case DXGI_ERROR_DEVICE_HUNG:return "DXGI_ERROR_DEVICE_HUNG";
97 case DXGI_ERROR_DRIVER_INTERNAL_ERROR:return "DXGI_ERROR_DRIVER_INTERNAL_ERROR";
98 case DXGI_ERROR_INVALID_CALL:return "DXGI_ERROR_INVALID_CALL";
99 default:return "unrecognized device removal reason";
100 }
101 }
102 // For ordinary error reporting only. Never query a graphics driver from
103 // the native exception handler, where the driver may already be damaged.
104 inline std::string DeviceFailureDiagnostics11(HRESULT operation,ID3D11Device* device)
105 {
106 char detail[128]{};
107 if(!device)
108 sprintf_s(detail," (0x%08lx; removalReason=unavailable: no device)",static_cast<unsigned long>(operation));
109 else
110 {
111 const HRESULT reason=device->GetDeviceRemovedReason();
112 // Both numeric codes fit inside the existing packet's 64-byte
113 // suffix allowance, even when its operation text is truncated.
114 sprintf_s(detail," (0x%08lx; removalReason=0x%08lx) [%s]",static_cast<unsigned long>(operation),
115 static_cast<unsigned long>(reason),DeviceRemovalReasonName11(reason));
116 }
117 return detail;
118 }
119}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
SCRIPT_CONTROL SCRIPT_STATE const void SCRIPT_CONTROL SCRIPT_STATE const int SCRIPT_TABDEF const BYTE SCRIPT_STRING_ANALYSIS BOOL
bool IsDeviceRemovalError11(HRESULT result)
std::string MemoryDiagnostics11(ID3D11Device *device)
std::string DriverDiagnostics11(ID3D11Device *device)
const char * DeviceRemovalReasonName11(HRESULT reason)
std::string DeviceFailureDiagnostics11(HRESULT operation, ID3D11Device *device)