Skip to content

Commit dceb776

Browse files
committed
Addendum to 37d8d4e
1 parent 37d8d4e commit dceb776

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

Client/core/DXHook/CDirect3DEvents9.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
#include "Graphics/CRenderItem.EffectTemplate.h"
2323

2424

25-
bool g_bInMTAScene = false;
26-
extern bool g_bInGTAScene;
25+
bool g_bInMTAScene = false;
26+
extern std::atomic<bool> g_bInGTAScene;
27+
void ResetGTASceneState();
2728
void ResetGTASceneState();
2829

2930
// Other variables
@@ -162,7 +163,7 @@ void CDirect3DEvents9::OnInvalidate(IDirect3DDevice9* pDevice)
162163
// Ensure any in-progress effect passes are wrapped up before ending the scene
163164
CloseActiveShader();
164165

165-
if (g_bInMTAScene || g_bInGTAScene)
166+
if (g_bInMTAScene || g_bInGTAScene.load(std::memory_order_acquire))
166167
{
167168
const HRESULT hrEndScene = pDevice->EndScene();
168169
if (FAILED(hrEndScene))
@@ -173,7 +174,7 @@ void CDirect3DEvents9::OnInvalidate(IDirect3DDevice9* pDevice)
173174
{
174175
CloseActiveShader(false);
175176

176-
if (g_bInMTAScene || g_bInGTAScene)
177+
if (g_bInMTAScene || g_bInGTAScene.load(std::memory_order_acquire))
177178
WriteDebugEvent("OnInvalidate: device lost, skipping EndScene and pending GPU work");
178179
}
179180

Client/core/DXHook/CProxyDirect3DDevice9.cpp

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void ReplaceInterface(T*& destination, T* source)
4242
}
4343
std::mutex g_proxyDeviceMutex;
4444
std::mutex g_gammaStateMutex;
45+
std::mutex g_deviceStateMutex;
4546
std::atomic<uint64_t> g_proxyRegistrationCounter{0};
4647
std::mutex g_sceneStateMutex;
4748
uint32_t g_gtaSceneActiveCount = 0;
@@ -50,7 +51,7 @@ uint64_t RegisterProxyDevice(CProxyDirect3DDevice9* instance);
5051
bool UnregisterProxyDevice(CProxyDirect3DDevice9* instance, uint64_t registrationId);
5152
}
5253

53-
bool g_bInGTAScene = false;
54+
std::atomic<bool> g_bInGTAScene{false};
5455
CProxyDirect3DDevice9* g_pProxyDevice = NULL;
5556
CProxyDirect3DDevice9::SD3DDeviceState* g_pDeviceState = NULL;
5657
SGammaState g_GammaState;
@@ -59,22 +60,22 @@ void IncrementGTASceneState()
5960
{
6061
std::lock_guard<std::mutex> lock(g_sceneStateMutex);
6162
++g_gtaSceneActiveCount;
62-
g_bInGTAScene = true;
63+
g_bInGTAScene.store(true, std::memory_order_release);
6364
}
6465

6566
void DecrementGTASceneState()
6667
{
6768
std::lock_guard<std::mutex> lock(g_sceneStateMutex);
6869
if (g_gtaSceneActiveCount > 0)
6970
--g_gtaSceneActiveCount;
70-
g_bInGTAScene = (g_gtaSceneActiveCount > 0);
71+
g_bInGTAScene.store(g_gtaSceneActiveCount > 0, std::memory_order_release);
7172
}
7273

7374
void ResetGTASceneState()
7475
{
7576
std::lock_guard<std::mutex> lock(g_sceneStateMutex);
7677
g_gtaSceneActiveCount = 0;
77-
g_bInGTAScene = false;
78+
g_bInGTAScene.store(false, std::memory_order_release);
7879
}
7980

8081
// Proxy constructor and destructor.
@@ -215,6 +216,8 @@ CProxyDirect3DDevice9::~CProxyDirect3DDevice9()
215216

216217
if (bWasRegistered)
217218
{
219+
ResetGTASceneState();
220+
218221
std::lock_guard<std::mutex> gammaLock(g_gammaStateMutex);
219222
if (g_GammaState.bOriginalGammaStored)
220223
{
@@ -540,7 +543,11 @@ HRESULT CProxyDirect3DDevice9::Reset(D3DPRESENT_PARAMETERS* pPresentationParamet
540543
{
541544
if (pSwapChain)
542545
{
543-
pSwapChain->GetPresentParameters(&g_pDeviceState->CreationState.PresentationParameters);
546+
std::lock_guard<std::mutex> stateGuard(g_deviceStateMutex);
547+
if (g_pDeviceState)
548+
{
549+
pSwapChain->GetPresentParameters(&g_pDeviceState->CreationState.PresentationParameters);
550+
}
544551
}
545552
else
546553
{
@@ -556,7 +563,18 @@ HRESULT CProxyDirect3DDevice9::Reset(D3DPRESENT_PARAMETERS* pPresentationParamet
556563
ReleaseInterface(pSwapChain);
557564

558565
// Store device creation parameters as well
559-
HRESULT hrCreationParams = m_pDevice->GetCreationParameters(&g_pDeviceState->CreationState.CreationParameters);
566+
HRESULT hrCreationParams = D3D_OK;
567+
{
568+
std::lock_guard<std::mutex> stateGuard(g_deviceStateMutex);
569+
if (g_pDeviceState)
570+
{
571+
hrCreationParams = m_pDevice->GetCreationParameters(&g_pDeviceState->CreationState.CreationParameters);
572+
}
573+
else
574+
{
575+
hrCreationParams = D3DERR_INVALIDCALL;
576+
}
577+
}
560578
if (FAILED(hrCreationParams))
561579
{
562580
WriteDebugEvent(SString("Warning: Failed to get creation parameters: %08x", hrCreationParams));
@@ -596,9 +614,15 @@ HRESULT CProxyDirect3DDevice9::Reset(D3DPRESENT_PARAMETERS* pPresentationParamet
596614
WriteDebugEvent(SString(" FullScreen_RefreshRateInHz:%d PresentationInterval:0x%08x", pPresentationParameters->FullScreen_RefreshRateInHz,
597615
pPresentationParameters->PresentationInterval));
598616

599-
const D3DDEVICE_CREATION_PARAMETERS& parameters = g_pDeviceState->CreationState.CreationParameters;
600-
601-
WriteDebugEvent(SString(" Adapter:%d DeviceType:%d BehaviorFlags:0x%x", parameters.AdapterOrdinal, parameters.DeviceType, parameters.BehaviorFlags));
617+
{
618+
std::lock_guard<std::mutex> stateGuard(g_deviceStateMutex);
619+
if (g_pDeviceState)
620+
{
621+
const D3DDEVICE_CREATION_PARAMETERS& parameters = g_pDeviceState->CreationState.CreationParameters;
622+
WriteDebugEvent(SString(" Adapter:%d DeviceType:%d BehaviorFlags:0x%x", parameters.AdapterOrdinal, parameters.DeviceType,
623+
parameters.BehaviorFlags));
624+
}
625+
}
602626
}
603627
else
604628
{
@@ -887,12 +911,13 @@ HRESULT CProxyDirect3DDevice9::EndScene()
887911
{
888912
// Call real routine.
889913
HRESULT hResult = m_pDevice->EndScene();
890-
DecrementGTASceneState();
891914

892915
CGraphics::GetSingleton().GetRenderItemManager()->SaveReadableDepthBuffer();
916+
DecrementGTASceneState();
893917
return hResult;
894918
}
895919

920+
DecrementGTASceneState();
896921
return D3D_OK;
897922
}
898923

@@ -1399,7 +1424,10 @@ uint64_t RegisterProxyDevice(CProxyDirect3DDevice9* instance)
13991424
std::lock_guard<std::mutex> guard(g_proxyDeviceMutex);
14001425
const uint64_t registrationId = g_proxyRegistrationCounter.fetch_add(1, std::memory_order_relaxed) + 1;
14011426
g_pProxyDevice = instance;
1402-
g_pDeviceState = instance ? &instance->DeviceState : nullptr;
1427+
{
1428+
std::lock_guard<std::mutex> stateGuard(g_deviceStateMutex);
1429+
g_pDeviceState = instance ? &instance->DeviceState : nullptr;
1430+
}
14031431
return registrationId;
14041432
}
14051433

@@ -1413,7 +1441,10 @@ bool UnregisterProxyDevice(CProxyDirect3DDevice9* instance, uint64_t registratio
14131441
return false;
14141442

14151443
g_pProxyDevice = nullptr;
1416-
g_pDeviceState = nullptr;
1444+
{
1445+
std::lock_guard<std::mutex> stateGuard(g_deviceStateMutex);
1446+
g_pDeviceState = nullptr;
1447+
}
14171448
return true;
14181449
}
14191450
} // namespace

Client/core/Graphics/CGraphics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "CMaterialPrimitive3DBatcher.h"
2121
#include "CAspectRatioConverter.h"
2222
extern CCore* g_pCore;
23-
extern bool g_bInGTAScene;
23+
extern std::atomic<bool> g_bInGTAScene;
2424
extern bool g_bInMTAScene;
2525

2626
using namespace std;
@@ -2143,7 +2143,7 @@ void CGraphics::DrawProgressMessage(bool bPreserveBackbuffer)
21432143
if (m_LastLostDeviceTimer.Get() < 1000)
21442144
return;
21452145

2146-
const bool bWasInScene = g_bInGTAScene || g_bInMTAScene;
2146+
const bool bWasInScene = g_bInGTAScene.load(std::memory_order_acquire) || g_bInMTAScene;
21472147
bool bInScene = bWasInScene;
21482148

21492149
// Skip of not in a scene and not forced with always flag

Client/core/Graphics/CRenderItemManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "CRenderItem.EffectCloner.h"
1515

1616
extern bool g_bInMTAScene;
17-
extern bool g_bInGTAScene;
17+
extern std::atomic<bool> g_bInGTAScene;
1818

1919
// Type of vertex used to emulate StretchRect for SwiftShader bug
2020
struct SRTVertex
@@ -1356,7 +1356,7 @@ void CRenderItemManager::SaveReadableDepthBuffer()
13561356

13571357
// Additional sync point for GPU driver
13581358
// Force immediate execution of depth buffer state changes when we can safely begin a scene
1359-
if (bDeviceReady && !g_bInMTAScene && !g_bInGTAScene)
1359+
if (bDeviceReady && !g_bInMTAScene && !g_bInGTAScene.load(std::memory_order_acquire))
13601360
{
13611361
const HRESULT hBeginScene = m_pDevice->BeginScene();
13621362
if (SUCCEEDED(hBeginScene))

0 commit comments

Comments
 (0)