From 245d379720696bf66ca44c810594702f15d54b5d Mon Sep 17 00:00:00 2001 From: Mr_Goldberg Date: Sun, 2 Feb 2020 19:06:44 -0500 Subject: [PATCH] Fix overlay not working in barotrauma. --- overlay_experimental/windows/DX11_Hook.cpp | 46 +++++++++++++++++++--- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/overlay_experimental/windows/DX11_Hook.cpp b/overlay_experimental/windows/DX11_Hook.cpp index 7f1fc0e..f458e89 100644 --- a/overlay_experimental/windows/DX11_Hook.cpp +++ b/overlay_experimental/windows/DX11_Hook.cpp @@ -50,7 +50,11 @@ void DX11_Hook::resetRenderState() { if (initialized) { - mainRenderTargetView->Release(); + if (mainRenderTargetView) { + mainRenderTargetView->Release(); + mainRenderTargetView = NULL; + } + pContext->Release(); ImGui_ImplDX11_Shutdown(); @@ -78,9 +82,34 @@ void DX11_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain) io.IniFilename = NULL; ID3D11Texture2D* pBackBuffer; - pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer)); - pDevice->CreateRenderTargetView(pBackBuffer, NULL, &mainRenderTargetView); + + ID3D11RenderTargetView *get_targets[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT] = {}; + pContext->OMGetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, get_targets, NULL); + bool bind_target = true; + + for (unsigned i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) { + if (get_targets[i]) { + ID3D11Resource *res = NULL; + get_targets[i]->GetResource(&res); + if (res) { + if (res == (ID3D11Resource*)pBackBuffer) { + bind_target = false; + } + + res->Release(); + } + + get_targets[i]->Release(); + } else { + break; + } + } + + if (bind_target) { + pDevice->CreateRenderTargetView(pBackBuffer, NULL, &mainRenderTargetView); + } + pBackBuffer->Release(); ImGui_ImplDX11_Init(pDevice, pContext); @@ -102,7 +131,10 @@ void DX11_Hook::prepareForOverlay(IDXGISwapChain* pSwapChain) ImGui::Render(); - pContext->OMSetRenderTargets(1, &mainRenderTargetView, NULL); + if (mainRenderTargetView) { + pContext->OMSetRenderTargets(1, &mainRenderTargetView, NULL); + } + ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); } } @@ -144,7 +176,11 @@ DX11_Hook::~DX11_Hook() if (initialized) { - mainRenderTargetView->Release(); + if (mainRenderTargetView) { + mainRenderTargetView->Release(); + mainRenderTargetView = NULL; + } + pContext->Release(); ImGui_ImplDX11_InvalidateDeviceObjects();