Skip to content

Commit

Permalink
Better handling of ClearD3DDevice()
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Jul 9, 2024
1 parent 9258aec commit 85f51aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
41 changes: 31 additions & 10 deletions ddraw/IDirectDrawX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3050,11 +3050,7 @@ HRESULT m_IDirectDrawX::CreateD3D9Device()
d3d9RenderTarget = nullptr;
if (RenderTargetSurface)
{
if (SUCCEEDED(d3d9Device->GetRenderTarget(0, &d3d9RenderTarget)))
{
d3d9RenderTarget->Release();
}
hr = SetRenderTargetSurface(RenderTargetSurface);
SetRenderTargetSurface(RenderTargetSurface);
}

// Reset D3D device settings
Expand Down Expand Up @@ -3397,10 +3393,22 @@ HRESULT m_IDirectDrawX::TestD3D9CooperativeLevel()

HRESULT m_IDirectDrawX::SetRenderTargetSurface(m_IDirectDrawSurfaceX* lpSurface)
{
// Remove render target
if (!lpSurface)
{
LOG_LIMIT(100, __FUNCTION__ << " Error: Direct3D surface does not exist!");
return DDERR_INVALIDPARAMS;
RenderTargetSurface = lpSurface;

SetDepthStencilSurface(nullptr);

if (d3d9Device && d3d9RenderTarget)
{
if (SUCCEEDED(d3d9Device->SetRenderTarget(0, d3d9RenderTarget)))
{
d3d9RenderTarget = nullptr;
}
}

return D3D_OK;
}

// Check for Direct3D surface
Expand All @@ -3410,10 +3418,21 @@ HRESULT m_IDirectDrawX::SetRenderTargetSurface(m_IDirectDrawSurfaceX* lpSurface)
return DDERR_INVALIDPARAMS;
}

// Backup original render target
if (d3d9Device && !d3d9RenderTarget)
{
if (SUCCEEDED(d3d9Device->GetRenderTarget(0, &d3d9RenderTarget)))
{
d3d9RenderTarget->Release();
}
}

// Set surface as render target
RenderTargetSurface = lpSurface;
RenderTargetSurface->SetAsRenderTarget();

Logging::LogDebug() << __FUNCTION__ << " Setting 3D Device Surface: " << RenderTargetSurface;

HRESULT hr = D3D_OK;
if (d3d9Device)
{
Expand All @@ -3427,8 +3446,6 @@ HRESULT m_IDirectDrawX::SetRenderTargetSurface(m_IDirectDrawSurfaceX* lpSurface)
return hr;
}

Logging::LogDebug() << __FUNCTION__ << " Setting 3D Device Surface: " << RenderTargetSurface;

m_IDirectDrawSurfaceX* pSurfaceZBuffer = RenderTargetSurface->GetAttachedZBuffer();
hr = SetDepthStencilSurface(pSurfaceZBuffer);

Expand All @@ -3445,7 +3462,11 @@ HRESULT m_IDirectDrawX::SetDepthStencilSurface(m_IDirectDrawSurfaceX* lpSurface)
{
HRESULT hr = D3D_OK;

if (!lpSurface)
if (lpSurface == DepthStencilSurface)
{
return hr;
}
else if (!lpSurface)
{
DepthStencilSurface = nullptr;

Expand Down
2 changes: 1 addition & 1 deletion ddraw/IDirectDrawX.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class m_IDirectDrawX : public IUnknown, public AddressLookupTableDdrawObject
inline void ClearD3D() { D3DInterface = nullptr; }
void SetD3DDevice(m_IDirect3DDeviceX* D3DDevice);
inline m_IDirect3DDeviceX** GetCurrentD3DDevice() { return &D3DDeviceInterface; }
inline void ClearD3DDevice() { D3DDeviceInterface = nullptr; RenderTargetSurface = nullptr; Using3D = false; }
inline void ClearD3DDevice() { Using3D = false; D3DDeviceInterface = nullptr; SetRenderTargetSurface(nullptr); }
inline void Enable3D() { Using3D = true; }
inline bool IsUsing3D() { return Using3D; }
inline bool IsPrimaryRenderTarget() { return PrimarySurface ? RenderTargetSurface->IsRenderTarget() : false; }
Expand Down

0 comments on commit 85f51aa

Please sign in to comment.