Skip to content

Commit 9714275

Browse files
authoredFeb 18, 2025··
Merge pull request #1003 from Kaldaien/crash_fix
Fix overlay rendering issues that lead to crashing when other overlays are present
2 parents 952aeac + 816038d commit 9714275

File tree

5 files changed

+39
-23
lines changed

5 files changed

+39
-23
lines changed
 

‎src/d3d12/D3D12.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,28 @@
99

1010
void D3D12::SetTrapInputInImGui(const bool acEnabled)
1111
{
12+
// Must have an out-condition to this otherwise infinite loop
13+
static int constexpr maxCursorDepth = 8;
14+
int showCursorTries = 0;
1215
int showCursorState;
1316
if (acEnabled)
1417
do
1518
{
1619
showCursorState = ShowCursor(TRUE);
17-
} while (showCursorState < 0);
20+
} while (showCursorState < 0 && showCursorTries++ < maxCursorDepth);
1821
else
1922
do
2023
{
2124
showCursorState = ShowCursor(FALSE);
22-
} while (showCursorState >= 0);
25+
} while (showCursorState >= 0 && showCursorTries++ < maxCursorDepth);
26+
27+
// Turn off software cursor
28+
if (showCursorTries < maxCursorDepth || acEnabled == false)
29+
ImGui::GetIO().MouseDrawCursor = false;
30+
31+
// Enable software cursor as fallback if necessary
32+
else
33+
ImGui::GetIO().MouseDrawCursor = acEnabled;
2334

2435
m_trapInputInImGui = acEnabled;
2536
}

‎src/d3d12/D3D12.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct D3D12
3535
struct FrameContext
3636
{
3737
Microsoft::WRL::ComPtr<ID3D12CommandAllocator> CommandAllocator;
38+
Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> CommandList{};
3839
Microsoft::WRL::ComPtr<ID3D12Resource> BackBuffer;
3940
D3D12_CPU_DESCRIPTOR_HANDLE MainRenderTargetDescriptor{0};
4041
};
@@ -62,7 +63,6 @@ struct D3D12
6263
Microsoft::WRL::ComPtr<ID3D12Device> m_pd3d12Device{};
6364
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_pd3dRtvDescHeap{};
6465
Microsoft::WRL::ComPtr<ID3D12DescriptorHeap> m_pd3dSrvDescHeap{};
65-
Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList> m_pd3dCommandList{};
6666

6767
// borrowed resources from game, do not manipulate reference counts on these!
6868
Microsoft::WRL::ComPtr<IDXGISwapChain4> m_pdxgiSwapChain{nullptr};

‎src/d3d12/D3D12_Functions.cpp

+15-14
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ bool D3D12::ResetState(const bool acDestroyContext)
3535
m_pd3d12Device.Reset();
3636
m_pd3dRtvDescHeap.Reset();
3737
m_pd3dSrvDescHeap.Reset();
38-
m_pd3dCommandList.Reset();
3938

4039
m_pCommandQueue.Reset();
4140
m_pdxgiSwapChain.Reset();
@@ -113,17 +112,19 @@ bool D3D12::Initialize()
113112
}
114113

115114
for (auto& context : m_frameContexts)
115+
{
116116
if (FAILED(m_pd3d12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&context.CommandAllocator))))
117117
{
118118
Log::Error("D3D12::Initialize() - failed to create command allocator!");
119119
return ResetState();
120120
}
121121

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

129130
if (!InitializeImGui(buffersCounts))
@@ -404,18 +405,18 @@ void D3D12::Update()
404405

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

407-
m_pd3dCommandList->Reset(frameContext.CommandAllocator.Get(), nullptr);
408-
m_pd3dCommandList->ResourceBarrier(1, &barrier);
409-
m_pd3dCommandList->SetDescriptorHeaps(1, heaps);
410-
m_pd3dCommandList->OMSetRenderTargets(1, &frameContext.MainRenderTargetDescriptor, FALSE, nullptr);
408+
frameContext.CommandList->Reset(frameContext.CommandAllocator.Get(), nullptr);
409+
frameContext.CommandList->ResourceBarrier(1, &barrier);
410+
frameContext.CommandList->SetDescriptorHeaps(1, heaps);
411+
frameContext.CommandList->OMSetRenderTargets(1, &frameContext.MainRenderTargetDescriptor, FALSE, nullptr);
411412

412-
ImGui_ImplDX12_RenderDrawData(&m_imguiDrawDataBuffers[0], m_pd3dCommandList.Get());
413+
ImGui_ImplDX12_RenderDrawData(&m_imguiDrawDataBuffers[0], frameContext.CommandList.Get());
413414

414415
barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET;
415416
barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT;
416-
m_pd3dCommandList->ResourceBarrier(1, &barrier);
417-
m_pd3dCommandList->Close();
417+
frameContext.CommandList->ResourceBarrier(1, &barrier);
418+
frameContext.CommandList->Close();
418419

419-
ID3D12CommandList* commandLists[] = {m_pd3dCommandList.Get()};
420+
ID3D12CommandList* commandLists[] = {frameContext.CommandList.Get()};
420421
m_pCommandQueue->ExecuteCommandLists(1, commandLists);
421422
}

‎src/dllmain.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ static void Shutdown()
7070

7171
BOOL APIENTRY DllMain(HMODULE mod, DWORD ul_reason_for_call, LPVOID)
7272
{
73-
DisableThreadLibraryCalls(mod);
73+
// Not safe to do this, the DLL uses thread_local storage
74+
//DisableThreadLibraryCalls(mod);
7475

7576
switch (ul_reason_for_call)
7677
{

‎src/window/window.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ static BOOL CALLBACK EnumWindowsProcCP77(HWND ahWnd, LPARAM alParam)
1414
GetWindowThreadProcessId(ahWnd, &lpdwProcessId);
1515
if (lpdwProcessId == GetCurrentProcessId())
1616
{
17-
TCHAR name[512] = {0};
18-
GetWindowText(ahWnd, name, 511);
19-
if (_tcscmp(TEXT("Cyberpunk 2077 (C) 2020 by CD Projekt RED"), name) == 0)
17+
wchar_t name[512] = {0};
18+
RealGetWindowClassW(ahWnd,name,511);
19+
if (wcscmp (L"W2ViewportClass", name) == 0)
2020
{
2121
*reinterpret_cast<HWND*>(alParam) = ahWnd;
2222
return FALSE;
@@ -32,8 +32,11 @@ LRESULT APIENTRY Window::WndProc(HWND ahWnd, UINT auMsg, WPARAM awParam, LPARAM
3232
if (auMsg == WM_WINDOWPOSCHANGED)
3333
{
3434
const auto* wp = reinterpret_cast<WINDOWPOS*>(alParam);
35-
s_pWindow->m_wndPos = {wp->x, wp->y};
36-
s_pWindow->m_wndSize = {wp->cx, wp->cy};
35+
36+
if ((wp->flags & SWP_NOMOVE) == 0)
37+
s_pWindow->m_wndPos = {wp->x, wp->y};
38+
if ((wp->flags & SWP_NOSIZE) == 0)
39+
s_pWindow->m_wndSize = {wp->cx, wp->cy};
3740

3841
RECT cr;
3942
GetClientRect(ahWnd, &cr);

0 commit comments

Comments
 (0)
Please sign in to comment.