Skip to content

Commit

Permalink
Merge pull request #1003 from Kaldaien/crash_fix
Browse files Browse the repository at this point in the history
Fix overlay rendering issues that lead to crashing when other overlays are present
  • Loading branch information
maximegmd authored Feb 18, 2025
2 parents 952aeac + 816038d commit 9714275
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
15 changes: 13 additions & 2 deletions src/d3d12/D3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@

void D3D12::SetTrapInputInImGui(const bool acEnabled)
{
// Must have an out-condition to this otherwise infinite loop
static int constexpr maxCursorDepth = 8;
int showCursorTries = 0;
int showCursorState;
if (acEnabled)
do
{
showCursorState = ShowCursor(TRUE);
} while (showCursorState < 0);
} while (showCursorState < 0 && showCursorTries++ < maxCursorDepth);
else
do
{
showCursorState = ShowCursor(FALSE);
} while (showCursorState >= 0);
} while (showCursorState >= 0 && showCursorTries++ < maxCursorDepth);

// Turn off software cursor
if (showCursorTries < maxCursorDepth || acEnabled == false)
ImGui::GetIO().MouseDrawCursor = false;

// Enable software cursor as fallback if necessary
else
ImGui::GetIO().MouseDrawCursor = acEnabled;

m_trapInputInImGui = acEnabled;
}
Expand Down
2 changes: 1 addition & 1 deletion src/d3d12/D3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct D3D12
struct FrameContext
{
Microsoft::WRL::ComPtr<ID3D12CommandAllocator> CommandAllocator;
Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> CommandList{};
Microsoft::WRL::ComPtr<ID3D12Resource> BackBuffer;
D3D12_CPU_DESCRIPTOR_HANDLE MainRenderTargetDescriptor{0};
};
Expand Down Expand Up @@ -62,7 +63,6 @@ struct D3D12
Microsoft::WRL::ComPtr<ID3D12Device> m_pd3d12Device{};
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_pd3dRtvDescHeap{};
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_pd3dSrvDescHeap{};
Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> m_pd3dCommandList{};

// borrowed resources from game, do not manipulate reference counts on these!
Microsoft::WRL::ComPtr<IDXGISwapChain4> m_pdxgiSwapChain{nullptr};
Expand Down
29 changes: 15 additions & 14 deletions src/d3d12/D3D12_Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ bool D3D12::ResetState(const bool acDestroyContext)
m_pd3d12Device.Reset();
m_pd3dRtvDescHeap.Reset();
m_pd3dSrvDescHeap.Reset();
m_pd3dCommandList.Reset();

m_pCommandQueue.Reset();
m_pdxgiSwapChain.Reset();
Expand Down Expand Up @@ -113,17 +112,19 @@ bool D3D12::Initialize()
}

for (auto& context : m_frameContexts)
{
if (FAILED(m_pd3d12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&context.CommandAllocator))))
{
Log::Error("D3D12::Initialize() - failed to create command allocator!");
return ResetState();
}

if (FAILED(m_pd3d12Device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_frameContexts[0].CommandAllocator.Get(), nullptr, IID_PPV_ARGS(&m_pd3dCommandList))) ||
FAILED(m_pd3dCommandList->Close()))
{
Log::Error("D3D12::Initialize() - failed to create command list!");
return ResetState();
if (FAILED(m_pd3d12Device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, context.CommandAllocator.Get(), nullptr, IID_PPV_ARGS(&context.CommandList))) ||
FAILED(context.CommandList->Close()))
{
Log::Error("D3D12::Initialize() - failed to create command list!");
return ResetState();
}
}

if (!InitializeImGui(buffersCounts))
Expand Down Expand Up @@ -404,18 +405,18 @@ void D3D12::Update()

ID3D12DescriptorHeap* heaps[] = {m_pd3dSrvDescHeap.Get()};

m_pd3dCommandList->Reset(frameContext.CommandAllocator.Get(), nullptr);
m_pd3dCommandList->ResourceBarrier(1, &barrier);
m_pd3dCommandList->SetDescriptorHeaps(1, heaps);
m_pd3dCommandList->OMSetRenderTargets(1, &frameContext.MainRenderTargetDescriptor, FALSE, nullptr);
frameContext.CommandList->Reset(frameContext.CommandAllocator.Get(), nullptr);
frameContext.CommandList->ResourceBarrier(1, &barrier);
frameContext.CommandList->SetDescriptorHeaps(1, heaps);
frameContext.CommandList->OMSetRenderTargets(1, &frameContext.MainRenderTargetDescriptor, FALSE, nullptr);

ImGui_ImplDX12_RenderDrawData(&m_imguiDrawDataBuffers[0], m_pd3dCommandList.Get());
ImGui_ImplDX12_RenderDrawData(&m_imguiDrawDataBuffers[0], frameContext.CommandList.Get());

barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
m_pd3dCommandList->ResourceBarrier(1, &barrier);
m_pd3dCommandList->Close();
frameContext.CommandList->ResourceBarrier(1, &barrier);
frameContext.CommandList->Close();

ID3D12CommandList* commandLists[] = {m_pd3dCommandList.Get()};
ID3D12CommandList* commandLists[] = {frameContext.CommandList.Get()};
m_pCommandQueue->ExecuteCommandLists(1, commandLists);
}
3 changes: 2 additions & 1 deletion src/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ static void Shutdown()

BOOL APIENTRY DllMain(HMODULE mod, DWORD ul_reason_for_call, LPVOID)
{
DisableThreadLibraryCalls(mod);
// Not safe to do this, the DLL uses thread_local storage
//DisableThreadLibraryCalls(mod);

switch (ul_reason_for_call)
{
Expand Down
13 changes: 8 additions & 5 deletions src/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ static BOOL CALLBACK EnumWindowsProcCP77(HWND ahWnd, LPARAM alParam)
GetWindowThreadProcessId(ahWnd, &lpdwProcessId);
if (lpdwProcessId == GetCurrentProcessId())
{
TCHAR name[512] = {0};
GetWindowText(ahWnd, name, 511);
if (_tcscmp(TEXT("Cyberpunk 2077 (C) 2020 by CD Projekt RED"), name) == 0)
wchar_t name[512] = {0};
RealGetWindowClassW(ahWnd,name,511);
if (wcscmp (L"W2ViewportClass", name) == 0)
{
*reinterpret_cast<HWND*>(alParam) = ahWnd;
return FALSE;
Expand All @@ -32,8 +32,11 @@ LRESULT APIENTRY Window::WndProc(HWND ahWnd, UINT auMsg, WPARAM awParam, LPARAM
if (auMsg == WM_WINDOWPOSCHANGED)
{
const auto* wp = reinterpret_cast<WINDOWPOS*>(alParam);
s_pWindow->m_wndPos = {wp->x, wp->y};
s_pWindow->m_wndSize = {wp->cx, wp->cy};

if ((wp->flags & SWP_NOMOVE) == 0)
s_pWindow->m_wndPos = {wp->x, wp->y};
if ((wp->flags & SWP_NOSIZE) == 0)
s_pWindow->m_wndSize = {wp->cx, wp->cy};

RECT cr;
GetClientRect(ahWnd, &cr);
Expand Down

0 comments on commit 9714275

Please sign in to comment.