[win32] Use single ImGui context; get DXGI adapter descriptions

d3d11
jackun 4 years ago
parent 2778618f36
commit 46ed24db80

@ -18,6 +18,7 @@ static Present oPresent = NULL;
long __stdcall hkPresent11(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags) long __stdcall hkPresent11(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags)
{ {
static bool init = false; static bool init = false;
auto prev_ctx = ImGui::GetCurrentContext();
if (!init) if (!init)
{ {
@ -30,12 +31,15 @@ long __stdcall hkPresent11(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT F
ID3D11DeviceContext* context; ID3D11DeviceContext* context;
device->GetImmediateContext(&context); device->GetImmediateContext(&context);
ImGui::CreateContext(); imgui_create(context, device);
ImGui::SetCurrentContext(state.imgui_ctx);
ImGui_ImplWin32_Init(desc.OutputWindow); ImGui_ImplWin32_Init(desc.OutputWindow);
ImGui_ImplDX11_Init(device, context); ImGui_ImplDX11_Init(device, context);
imgui_create(context);
init = true; init = true;
} }
ImGui::SetCurrentContext(state.imgui_ctx);
update_hud_info(sw_stats, params, vendorID); update_hud_info(sw_stats, params, vendorID);
ImGui_ImplDX11_NewFrame(); ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame(); ImGui_ImplWin32_NewFrame();
@ -47,6 +51,8 @@ long __stdcall hkPresent11(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT F
ImGui::Render(); ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
ImGui::SetCurrentContext(prev_ctx);
return oPresent(pSwapChain, SyncInterval, Flags); return oPresent(pSwapChain, SyncInterval, Flags);
} }

@ -31,18 +31,22 @@ long __stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice)
{ {
static bool init = false; static bool init = false;
auto prev_ctx = ImGui::GetCurrentContext();
if (!init) if (!init)
{ {
D3DDEVICE_CREATION_PARAMETERS params; D3DDEVICE_CREATION_PARAMETERS params;
pDevice->GetCreationParameters(&params); pDevice->GetCreationParameters(&params);
auto context = ImGui::CreateContext(); imgui_create(pDevice, nullptr);
ImGui::SetCurrentContext(state.imgui_ctx);
ImGui_ImplWin32_Init(params.hFocusWindow); ImGui_ImplWin32_Init(params.hFocusWindow);
ImGui_ImplDX9_Init(pDevice); ImGui_ImplDX9_Init(pDevice);
imgui_create(context);
init = true; init = true;
} }
ImGui::SetCurrentContext(state.imgui_ctx);
update_hud_info(sw_stats, params, vendorID); update_hud_info(sw_stats, params, vendorID);
ImGui_ImplDX9_NewFrame(); ImGui_ImplDX9_NewFrame();
ImGui_ImplWin32_NewFrame(); ImGui_ImplWin32_NewFrame();
@ -53,6 +57,8 @@ long __stdcall hkEndScene(LPDIRECT3DDEVICE9 pDevice)
ImGui::Render(); ImGui::Render();
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
ImGui::SetCurrentContext(prev_ctx);
return oEndScene(pDevice); return oEndScene(pDevice);
} }

@ -1,4 +1,5 @@
#include "dx_shared.h" #include "dx_shared.h"
#include <dxgi.h>
bool cfg_inited = false; bool cfg_inited = false;
ImVec2 window_size; ImVec2 window_size;
@ -19,20 +20,34 @@ void imgui_init()
init_cpu_stats(params); init_cpu_stats(params);
} }
void imgui_create(void *ctx) void imgui_create(void *ctx, void *device)
{ {
if (inited) if (inited)
return; return;
inited = true; inited = true;
imgui_init(); imgui_init();
deviceName = "something";
if (deviceName.find("Radeon") != std::string::npos // DX10+
|| deviceName.find("AMD") != std::string::npos){ if (device) {
vendorID = 0x1002; IUnknown* pUnknown = reinterpret_cast<IUnknown*>(device);
} else { IDXGIDevice* pDXGIDevice;
vendorID = 0x10de; HRESULT hr = pUnknown->QueryInterface(__uuidof(IDXGIDevice), (void**)&pDXGIDevice);
if (S_OK == hr) {
IDXGIAdapter* pDXGIAdapter;
pDXGIDevice->GetAdapter(&pDXGIAdapter);
DXGI_ADAPTER_DESC adapterDesc;
hr = pDXGIAdapter->GetDesc(&adapterDesc);
if (S_OK == hr) {
vendorID = adapterDesc.VendorId;
char buf[256]{};
wcstombs_s(nullptr, buf, adapterDesc.Description, sizeof(adapterDesc.Description));
deviceName = buf;
}
}
} }
init_gpu_stats(vendorID, params); init_gpu_stats(vendorID, params);
// Setup Dear ImGui context // Setup Dear ImGui context
IMGUI_CHECKVERSION(); IMGUI_CHECKVERSION();

@ -19,5 +19,5 @@ extern uint32_t vendorID;
extern std::string deviceName; extern std::string deviceName;
extern bool inited; extern bool inited;
void imgui_create(void *ctx); void imgui_create(void *ctx, void *device);
void imgui_init(void); void imgui_init(void);
Loading…
Cancel
Save