Skip to content

Commit

Permalink
Backup and restore device state
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Dec 3, 2024
1 parent c1cd1c0 commit 6331528
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Dllmain/BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 7348
#define BUILD_NUMBER 7349
34 changes: 33 additions & 1 deletion ddraw/IDirect3DDeviceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4380,9 +4380,26 @@ HRESULT m_IDirect3DDeviceX::BackupStates()
// Backup viewport
(*d3d9Device)->GetViewport(&backup.viewport);

backup.IsBackedUp = true;

return D3D_OK;
}

HRESULT m_IDirect3DDeviceX::RestoreTextures()
{
if (!d3d9Device || !*d3d9Device)
{
Logging::Log() << __FUNCTION__ " Error: Failed to get the device state!";
return DDERR_GENERIC;
}

// Restore textures
for (UINT y = 0; y < MaxTextureStages; y++)
{
SetTexture(y, AttachedTexture[y]);
}
}

HRESULT m_IDirect3DDeviceX::RestoreStates()
{
if (!d3d9Device || !*d3d9Device)
Expand All @@ -4391,6 +4408,11 @@ HRESULT m_IDirect3DDeviceX::RestoreStates()
return DDERR_GENERIC;
}

if (!backup.IsBackedUp)
{
return D3D_OK;
}

// Restore render states
for (UINT x = 0; x < 255; x++)
{
Expand Down Expand Up @@ -4430,14 +4452,24 @@ HRESULT m_IDirect3DDeviceX::RestoreStates()
}
}

// Restore textures
RestoreTextures();

// Restore viewport
D3DVIEWPORT9 viewport = {};
(*d3d9Device)->GetViewport(&viewport);
backup.viewport.Width = viewport.Width;
backup.viewport.Height = viewport.Height;
(*d3d9Device)->SetViewport(&backup.viewport);

backup.IsBackedUp = false;

return D3D_OK;
}

void m_IDirect3DDeviceX::BeforeResetDevice()
{
BackupStates();
if (IsRecordingState)
{
DWORD dwBlockHandle = NULL;
Expand All @@ -4450,7 +4482,7 @@ void m_IDirect3DDeviceX::BeforeResetDevice()

void m_IDirect3DDeviceX::AfterResetDevice()
{
bSetDefaults = true;
RestoreStates();
}

void m_IDirect3DDeviceX::ClearDdraw()
Expand Down
2 changes: 2 additions & 0 deletions ddraw/IDirect3DDeviceX.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
#endif

struct {
bool IsBackedUp = false;
DWORD RenderState[255] = {};
DWORD TextureState[MaxTextureStages][255] = {};
DWORD SamplerState[MaxTextureStages][14] = {};
Expand Down Expand Up @@ -340,6 +341,7 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
}
void ClearDdraw();
void BeforeResetDevice();
HRESULT RestoreTextures();
void AfterResetDevice();
void ReleaseAllStateBlocks();
};
11 changes: 9 additions & 2 deletions ddraw/IDirectDrawX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4635,8 +4635,15 @@ HRESULT m_IDirectDrawX::DrawPrimarySurface()
} while (false);

// Reset textures
d3d9Device->SetTexture(0, nullptr);
d3d9Device->SetTexture(1, nullptr);
if (D3DDeviceInterface)
{
D3DDeviceInterface->RestoreTextures();
}
else
{
d3d9Device->SetTexture(0, nullptr);
d3d9Device->SetTexture(1, nullptr);
}

// Reset pixel shader
d3d9Device->SetPixelShader(nullptr);
Expand Down

0 comments on commit 6331528

Please sign in to comment.