Skip to content

Commit d983d18

Browse files
Synchronize changes from 1.6 branch [ci skip]
d756eea Fix borderless mode colors & brightness (for real this time) as well as a regression
2 parents 28353aa + d756eea commit d983d18

File tree

2 files changed

+60
-26
lines changed

2 files changed

+60
-26
lines changed

Client/core/DXHook/CProxyDirect3D9.cpp

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ HRESULT HandleCreateDeviceResult(HRESULT hResult, IDirect3D9* pDirect3D, UINT Ad
105105
std::vector<IDirect3D9*> ms_CreatedDirect3D9List;
106106
bool CreateDeviceSecondCallCheck(HRESULT& hOutResult, IDirect3D9* pDirect3D, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags,
107107
D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice9** ppReturnedDeviceInterface);
108+
void ApplyBorderlessColorCorrection(CProxyDirect3DDevice9* proxyDevice, const D3DPRESENT_PARAMETERS& presentationParameters);
108109

109110
CProxyDirect3D9::CProxyDirect3D9(IDirect3D9* pInterface)
110111
: m_pDevice(nullptr)
@@ -454,24 +455,7 @@ HRESULT CProxyDirect3D9::CreateDevice(UINT Adapter, D3DDEVTYPE DeviceType, HWND
454455
if (SUCCEEDED(hResult) && *ppReturnedDeviceInterface)
455456
{
456457
// Check if we're in borderless mode
457-
bool bIsBorderlessMode = pPresentationParameters->Windowed == TRUE;
458-
459-
if (bIsBorderlessMode)
460-
{
461-
// Enable sRGB correction for borderless mode to compensate for DWM composition
462-
CProxyDirect3DDevice9* pProxyDevice = static_cast<CProxyDirect3DDevice9*>(*ppReturnedDeviceInterface);
463-
464-
// Set initial render states for proper color handling
465-
pProxyDevice->SetRenderState(D3DRS_SRGBWRITEENABLE, TRUE);
466-
467-
// Configure samplers for sRGB texture reading
468-
for (DWORD i = 0; i < 8; i++)
469-
{
470-
pProxyDevice->SetSamplerState(i, D3DSAMP_SRGBTEXTURE, TRUE);
471-
}
472-
473-
WriteDebugEvent("Applied sRGB color correction for borderless mode");
474-
}
458+
ApplyBorderlessColorCorrection(static_cast<CProxyDirect3DDevice9*>(*ppReturnedDeviceInterface), *pPresentationParameters);
475459
}
476460

477461
return hResult;
@@ -549,6 +533,41 @@ namespace
549533
uint ms_uiCreationAttempts = 0;
550534
} // namespace
551535

536+
void ApplyBorderlessColorCorrection(CProxyDirect3DDevice9* proxyDevice, const D3DPRESENT_PARAMETERS& presentationParameters)
537+
{
538+
if (!proxyDevice)
539+
return;
540+
541+
bool bBorderless = false;
542+
if (CVideoModeManagerInterface* videoModeManager = GetVideoModeManager())
543+
{
544+
bBorderless = videoModeManager->IsDisplayModeWindowed() || videoModeManager->IsDisplayModeFullScreenWindow();
545+
}
546+
else
547+
{
548+
bBorderless = (presentationParameters.Windowed != 0);
549+
}
550+
551+
if (!bBorderless)
552+
{
553+
proxyDevice->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE);
554+
for (DWORD sampler = 0; sampler < 8; ++sampler)
555+
{
556+
proxyDevice->SetSamplerState(sampler, D3DSAMP_SRGBTEXTURE, FALSE);
557+
}
558+
WriteDebugEvent("Cleared sRGB color correction for non-borderless mode");
559+
return;
560+
}
561+
562+
proxyDevice->SetRenderState(D3DRS_SRGBWRITEENABLE, TRUE);
563+
for (DWORD sampler = 0; sampler < 8; ++sampler)
564+
{
565+
proxyDevice->SetSamplerState(sampler, D3DSAMP_SRGBTEXTURE, TRUE);
566+
}
567+
568+
WriteDebugEvent("Applied sRGB color correction for borderless mode");
569+
}
570+
552571
////////////////////////////////////////////////
553572
//
554573
// CreateDeviceInsist
@@ -958,21 +977,28 @@ bool CreateDeviceSecondCallCheck(HRESULT& hOutResult, IDirect3D9* pDirect3D, UIN
958977
D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DDevice9** ppReturnedDeviceInterface)
959978
{
960979
static uint uiCreateCount = 0;
980+
static IDirect3D9* lastTrackedInterface = nullptr;
961981

962-
// Also check for invalid size
963-
if (pPresentationParameters->BackBufferWidth == 0)
982+
if (!IsMainThread())
964983
{
965-
WriteDebugEvent(SString(" Passing through call #%d to CreateDevice because size is invalid", uiCreateCount));
984+
SString strMessage;
985+
strMessage = " Passing through CreateDevice because not main thread";
986+
WriteDebugEvent(strMessage);
987+
AddReportLog(8627, strMessage);
966988
hOutResult = pDirect3D->CreateDevice(Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface);
967989
return true;
968990
}
969991

970-
// Also check for calls from other threads
971-
if (!IsMainThread())
992+
if (pDirect3D != lastTrackedInterface)
972993
{
973-
SString strMessage(" Passing through call #%d to CreateDevice because not main thread", uiCreateCount);
974-
WriteDebugEvent(strMessage);
975-
AddReportLog(8627, strMessage);
994+
lastTrackedInterface = pDirect3D;
995+
uiCreateCount = 0;
996+
}
997+
998+
// Also check for invalid size
999+
if (pPresentationParameters->BackBufferWidth == 0)
1000+
{
1001+
WriteDebugEvent(SString(" Passing through call #%d to CreateDevice because size is invalid", uiCreateCount));
9761002
hOutResult = pDirect3D->CreateDevice(Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, ppReturnedDeviceInterface);
9771003
return true;
9781004
}
@@ -1084,6 +1110,8 @@ HRESULT HandleCreateDeviceResult(HRESULT hResult, IDirect3D9* pDirect3D, UINT Ad
10841110

10851111
WriteDebugEvent(SString(" Adapter:%d DeviceType:%d BehaviorFlags:0x%x ReadableDepth:%s", parameters.AdapterOrdinal, parameters.DeviceType,
10861112
parameters.BehaviorFlags, ReadableDepthFormat ? std::string((char*)&ReadableDepthFormat, 4).c_str() : "None"));
1113+
1114+
ApplyBorderlessColorCorrection(static_cast<CProxyDirect3DDevice9*>(*ppReturnedDeviceInterface), *pPresentationParameters);
10871115
}
10881116
}
10891117

@@ -1360,6 +1388,8 @@ HRESULT CCore::OnPostCreateDevice(HRESULT hResult, IDirect3D9* pDirect3D, UINT A
13601388
WriteDebugEvent(" Used creation parameters:");
13611389
WriteDebugEvent(SString(" Adapter:%d DeviceType:%d BehaviorFlags:0x%x ReadableDepth:%s", parameters.AdapterOrdinal, parameters.DeviceType,
13621390
parameters.BehaviorFlags, ReadableDepthFormat ? std::string((char*)&ReadableDepthFormat, 4).c_str() : "None"));
1391+
1392+
ApplyBorderlessColorCorrection(static_cast<CProxyDirect3DDevice9*>(*ppReturnedDeviceInterface), *pPresentationParameters);
13631393
}
13641394

13651395
bool bDetectOptimus = (GetModuleHandle("nvd3d9wrap.dll") != NULL);

Client/core/DXHook/CProxyDirect3DDevice9.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
class CProxyDirect3DDevice9;
2020
extern std::atomic<bool> g_bInMTAScene;
2121

22+
void ApplyBorderlessColorCorrection(CProxyDirect3DDevice9* proxyDevice, const D3DPRESENT_PARAMETERS& presentationParameters);
23+
2224
namespace
2325
{
2426
template <typename T>
@@ -670,6 +672,8 @@ HRESULT CProxyDirect3DDevice9::Reset(D3DPRESENT_PARAMETERS* pPresentationParamet
670672
WriteDebugEvent("CProxyDirect3DDevice9::Reset - Skipping PostReset because video mode manager is unavailable");
671673
}
672674

675+
ApplyBorderlessColorCorrection(this, *pPresentationParameters);
676+
673677
// Update our data.
674678
m_pData->StoreViewport(0, 0, pPresentationParameters->BackBufferWidth, pPresentationParameters->BackBufferHeight);
675679

0 commit comments

Comments
 (0)