Skip to content

Commit

Permalink
Change how device size is set with Direct3D
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Sep 6, 2023
1 parent c4eb602 commit 479e054
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 103 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 6792
#define BUILD_NUMBER 6793
9 changes: 1 addition & 8 deletions ddraw/IDirect3DDeviceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1507,14 +1507,7 @@ HRESULT m_IDirect3DDeviceX::SetViewport(LPD3DVIEWPORT7 lpViewport)
return DDERR_GENERIC;
}

HRESULT hr = (*d3d9Device)->SetViewport((D3DVIEWPORT9*)lpViewport);

if (SUCCEEDED(hr))
{
ddrawParent->SetNewViewport(lpViewport->dwWidth, lpViewport->dwHeight);
}

return hr;
return (*d3d9Device)->SetViewport((D3DVIEWPORT9*)lpViewport);
}

D3DVIEWPORT7 Viewport7;
Expand Down
4 changes: 2 additions & 2 deletions ddraw/IDirect3DDeviceX.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,9 @@ class m_IDirect3DDeviceX : public IUnknown, public AddressLookupTableDdrawObject
ddrawParent = ddraw;

// Store D3DDevice
if (ddrawParent)
if (ddrawParent && DeviceSurface)
{
ddrawParent->SetD3DDevice(this);
ddrawParent->SetD3DDevice(this, DeviceSurface);
}
}
void ClearDdraw() { ddrawParent = nullptr; colorkeyPixelShader = nullptr; }
Expand Down
2 changes: 1 addition & 1 deletion ddraw/IDirect3DX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ HRESULT m_IDirect3DX::CreateDevice(REFCLSID rclsid, LPDIRECTDRAWSURFACE7 lpDDS,
SetCriticalSection();
if (ddrawParent)
{
ddrawParent->SetD3DDevice(p_IDirect3DDeviceX);
ddrawParent->SetD3DDevice(p_IDirect3DDeviceX, DdrawSurface3D);
}
ReleaseCriticalSection();

Expand Down
2 changes: 1 addition & 1 deletion ddraw/IDirectDrawSurfaceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ HRESULT m_IDirectDrawSurfaceX::QueryInterface(REFIID riid, LPVOID FAR * ppvObj,
{
lpD3DDeviceX->SetDdrawParent(ddrawParent);

ddrawParent->SetD3DDevice(lpD3DDeviceX);
ddrawParent->SetD3DDevice(lpD3DDeviceX, this);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions ddraw/IDirectDrawSurfaceX.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ class m_IDirectDrawSurfaceX : public IUnknown, public AddressLookupTableDdrawObj
inline bool IsLockedFromOtherThread() { return (IsSurfaceBlitting() || IsSurfaceLocked()) && LockedWithID && LockedWithID != GetCurrentThreadId(); }
inline RECT GetSurfaceRect() { return { 0, 0, (LONG)surfaceDesc2.dwWidth, (LONG)surfaceDesc2.dwHeight }; }
inline bool CanSurfaceBeDeleted() { return (ComplexRoot || (surfaceDesc2.ddsCaps.dwCaps & DDSCAPS_COMPLEX) == 0); }
inline DWORD GetWidth() { return surfaceDesc2.dwWidth; }
inline DWORD GetHeight() { return surfaceDesc2.dwHeight; }
inline DDSCAPS2 GetSurfaceCaps() { return surfaceDesc2.ddsCaps; }
inline D3DFORMAT GetSurfaceFormat() { return surfaceFormat; }
inline bool CheckSurfaceExists(LPDIRECTDRAWSURFACE7 lpDDSrcSurface) { return
Expand Down Expand Up @@ -400,6 +398,8 @@ class m_IDirectDrawSurfaceX : public IUnknown, public AddressLookupTableDdrawObj
inline bool IsPalette() { return (surfaceFormat == D3DFMT_P8); }
inline bool IsDepthBuffer() { return (surfaceDesc2.ddpfPixelFormat.dwFlags & (DDPF_ZBUFFER | DDPF_STENCILBUFFER)) != 0; }
inline bool IsSurfaceManaged() { return (surfaceDesc2.ddsCaps.dwCaps2 & (DDSCAPS2_TEXTUREMANAGE | DDSCAPS2_D3DTEXTUREMANAGE)) != 0; }
inline DWORD GetWidth() { return surfaceDesc2.dwWidth; }
inline DWORD GetHeight() { return surfaceDesc2.dwHeight; }
bool GetColorKey(DWORD& ColorSpaceLowValue, DWORD& ColorSpaceHighValue);
inline bool IsUsingEmulation() { return (surface.emu && surface.emu->DC && surface.emu->GameDC && surface.emu->pBits); }
inline bool IsEmulationDCReady() { return (IsUsingEmulation() && !surface.emu->UsingGameDC); }
Expand Down
132 changes: 47 additions & 85 deletions ddraw/IDirectDrawX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,6 @@ DISPLAYSETTINGS DisplayMode;
// Device settings
DEVICESETTINGS Device;

// Viewport resolution
DWORD viewportWidth;
DWORD viewportHeight;

// Surface resolution
DWORD surfaceWidth;
DWORD surfaceHeight;

// Display pixel format
DDPIXELFORMAT DisplayPixelFormat;

Expand Down Expand Up @@ -640,18 +632,12 @@ HRESULT m_IDirectDrawX::CreateSurface2(LPDDSURFACEDESC2 lpDDSurfaceDesc2, LPDIRE
}
}

// Get surface size
if (!Device.Width && !Device.Height && (Desc2.dwFlags & (DDSD_WIDTH | DDSD_HEIGHT)) == (DDSD_WIDTH | DDSD_HEIGHT) &&
(Desc2.ddsCaps.dwCaps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE)))
// Check if there is a change in the present parameters
if ((Desc2.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) && !Device.Width && !Device.Height &&
(Desc2.dwFlags & (DDSD_WIDTH | DDSD_HEIGHT)) == (DDSD_WIDTH | DDSD_HEIGHT) &&
(Desc2.dwWidth != presParams.BackBufferWidth || Desc2.dwHeight != presParams.BackBufferHeight))
{
surfaceWidth = Desc2.dwWidth;
surfaceHeight = Desc2.dwHeight;

// Check if there is a change in the present parameters
if (surfaceWidth != presParams.BackBufferWidth || surfaceHeight != presParams.BackBufferHeight)
{
ResetD3D9Device = true;
}
ResetD3D9Device = true;
}

// Get present parameters
Expand All @@ -674,16 +660,16 @@ HRESULT m_IDirectDrawX::CreateSurface2(LPDDSURFACEDESC2 lpDDSurfaceDesc2, LPDIRE
}
}

// Reset d3d9 device
if (ResetD3D9Device)
// Create interface
m_IDirectDrawSurfaceX *p_IDirectDrawSurfaceX = new m_IDirectDrawSurfaceX(this, DirectXVersion, &Desc2);
*lplpDDSurface = (LPDIRECTDRAWSURFACE7)p_IDirectDrawSurfaceX->GetWrapperInterfaceX(DirectXVersion);

// Reset d3d9 device after creating interface if needed
if (ResetD3D9Device && d3d9Device)
{
CreateD3D9Device();
}

m_IDirectDrawSurfaceX *p_IDirectDrawSurfaceX = new m_IDirectDrawSurfaceX(this, DirectXVersion, &Desc2);

*lplpDDSurface = (LPDIRECTDRAWSURFACE7)p_IDirectDrawSurfaceX->GetWrapperInterfaceX(DirectXVersion);

return DD_OK;
}

Expand Down Expand Up @@ -1530,10 +1516,6 @@ HRESULT m_IDirectDrawX::RestoreDisplayMode()
DisplayMode.Height = 0;
DisplayMode.BPP = 0;
DisplayMode.RefreshRate = 0;
viewportWidth = 0;
viewportHeight = 0;
surfaceWidth = 0;
surfaceHeight = 0;

// Restore all existing surfaces
if (d3d9Device)
Expand Down Expand Up @@ -1841,14 +1823,6 @@ HRESULT m_IDirectDrawX::SetDisplayMode(DWORD dwWidth, DWORD dwHeight, DWORD dwBP
Exclusive.RefreshRate = dwRefreshRate;
}

// Reset viewport resolution
viewportWidth = 0;
viewportHeight = 0;

// Reset surface resolution
surfaceWidth = 0;
surfaceHeight = 0;

// Update the d3d9 device to use new display mode
if (LastWidth != Device.Width || LastHeight != Device.Height || (!Device.IsWindowed && LastRefreshRate != DisplayMode.RefreshRate))
{
Expand Down Expand Up @@ -2340,11 +2314,6 @@ void m_IDirectDrawX::InitDdraw(DWORD DirectXVersion)
}
Device.RefreshRate = (Config.DdrawOverrideRefreshRate) ? Config.DdrawOverrideRefreshRate : 0;

viewportWidth = 0;
viewportHeight = 0;
surfaceWidth = 0;
surfaceHeight = 0;

// Mouse hook
static bool EnableMouseHook = Config.DdrawEnableMouseHook &&
((Config.DdrawUseNativeResolution || Config.DdrawOverrideWidth || Config.DdrawOverrideHeight) &&
Expand Down Expand Up @@ -2625,24 +2594,16 @@ void m_IDirectDrawX::GetSurfaceDisplay(DWORD& Width, DWORD& Height, DWORD& BPP,
Height = presParams.BackBufferHeight;
BPP = Utils::GetBitCount(hWnd);
}
else if (viewportWidth && viewportHeight)
else if (Direct3DSurface)
{
Width = viewportWidth;
Height = viewportHeight;
Width = Direct3DSurface->GetWidth();
Height = Direct3DSurface->GetHeight();
BPP = Utils::GetBitCount(hWnd);
}
else if (surfaceWidth && surfaceHeight)
else if (PrimarySurface)
{
Width = surfaceWidth;
Height = surfaceHeight;
BPP = Utils::GetBitCount(hWnd);
}
else if (Device.IsWindowed && IsWindow(hWnd))
{
RECT Rect = {};
GetClientRect(hWnd, &Rect);
Width = Rect.right - Rect.left;
Height = Rect.bottom - Rect.top;
Width = PrimarySurface->GetWidth();
Height = PrimarySurface->GetHeight();
BPP = Utils::GetBitCount(hWnd);
}
else
Expand Down Expand Up @@ -2686,22 +2647,6 @@ void m_IDirectDrawX::GetDisplay(DWORD &Width, DWORD &Height)
Height = presParams.BackBufferHeight;
}

void m_IDirectDrawX::SetNewViewport(DWORD Width, DWORD Height)
{
if (Width && Height && !Device.Width && !Device.Height)
{
viewportWidth = Width;
viewportHeight = Height;

// Check if there is a change in the present parameters
if (d3d9Device && (viewportWidth != presParams.BackBufferWidth || viewportHeight != presParams.BackBufferHeight))
{
// Reset d3d9 device
CreateD3D9Device();
}
}
}

HRESULT m_IDirectDrawX::CheckInterface(char *FunctionName, bool CheckD3DDevice)
{
// Check for object, if not then create it
Expand Down Expand Up @@ -2729,6 +2674,26 @@ HRESULT m_IDirectDrawX::CheckInterface(char *FunctionName, bool CheckD3DDevice)
return DD_OK;
}

void m_IDirectDrawX::SetD3DDevice(m_IDirect3DDeviceX* D3DDevice, m_IDirectDrawSurfaceX* D3DSurface)
{
D3DDeviceInterface = D3DDevice;

if (!DoesSurfaceExist(D3DSurface))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: Direct3D surface does not exist!");
return;
}

Direct3DSurface = D3DSurface;

// Recreate Direct3D9 device
if (d3d9Device && !Device.Width && !Device.Height &&
Direct3DSurface->GetWidth() != presParams.BackBufferWidth || Direct3DSurface->GetHeight() != presParams.BackBufferHeight)
{
CreateD3D9Device();
}
}

bool m_IDirectDrawX::CheckD3D9Device()
{
if (!d3d9Device && FAILED(CreateD3D9Device()))
Expand Down Expand Up @@ -2818,22 +2783,15 @@ HRESULT m_IDirectDrawX::CreateD3D9Device()
DWORD BackBufferHeight = Device.Height;
if (!BackBufferWidth || !BackBufferHeight)
{
if (viewportWidth && viewportHeight)
{
BackBufferWidth = viewportWidth;
BackBufferHeight = viewportHeight;
}
else if (surfaceWidth && surfaceHeight)
if (Direct3DSurface)
{
BackBufferWidth = surfaceWidth;
BackBufferHeight = surfaceHeight;
BackBufferWidth = Direct3DSurface->GetWidth();
BackBufferHeight = Direct3DSurface->GetHeight();
}
else if (Device.IsWindowed && IsWindow(hWnd))
else if (PrimarySurface)
{
RECT Rect = {};
GetClientRect(hWnd, &Rect);
BackBufferWidth = Rect.right - Rect.left;
BackBufferHeight = Rect.bottom - Rect.top;
BackBufferWidth = PrimarySurface->GetWidth();
BackBufferHeight = PrimarySurface->GetHeight();
}
if (!BackBufferWidth || !BackBufferHeight)
{
Expand Down Expand Up @@ -3268,6 +3226,10 @@ void m_IDirectDrawX::RemoveSurfaceFromVector(m_IDirectDrawSurfaceX* lpSurfaceX)
PrimarySurface = nullptr;
DisplayPixelFormat = {};
}
if (lpSurfaceX == Direct3DSurface)
{
Direct3DSurface = nullptr;
}

auto it = std::find(SurfaceVector.begin(), SurfaceVector.end(), lpSurfaceX);

Expand Down
6 changes: 3 additions & 3 deletions ddraw/IDirectDrawX.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class m_IDirectDrawX : public IUnknown, public AddressLookupTableDdrawObject

// Store primary surface
m_IDirectDrawSurfaceX *PrimarySurface = nullptr;
m_IDirectDrawSurfaceX *Direct3DSurface = nullptr;

// Store a list of surfaces
std::vector<m_IDirectDrawSurfaceX*> SurfaceVector;
Expand Down Expand Up @@ -173,9 +174,9 @@ class m_IDirectDrawX : public IUnknown, public AddressLookupTableDdrawObject
// Direct3D interfaces
inline m_IDirect3DX** GetCurrentD3D() { return &D3DInterface; }
inline void ClearD3D() { D3DInterface = nullptr; }
inline void SetD3DDevice(m_IDirect3DDeviceX* D3DDevice) { D3DDeviceInterface = D3DDevice; }
void SetD3DDevice(m_IDirect3DDeviceX* D3DDevice, m_IDirectDrawSurfaceX* D3DSurface);
inline m_IDirect3DDeviceX** GetCurrentD3DDevice() { return &D3DDeviceInterface; }
inline void ClearD3DDevice() { D3DDeviceInterface = nullptr; Using3D = false; }
inline void ClearD3DDevice() { D3DDeviceInterface = nullptr; Direct3DSurface = nullptr; Using3D = false; }
inline void Enable3D() { Using3D = true; }
inline bool IsUsing3D() { return Using3D; }

Expand All @@ -198,7 +199,6 @@ class m_IDirectDrawX : public IUnknown, public AddressLookupTableDdrawObject
void GetSurfaceDisplay(DWORD& Width, DWORD& Height, DWORD& BPP, DWORD& RefreshRate);
void GetDisplayPixelFormat(DDPIXELFORMAT& ddpfPixelFormat, DWORD BPP);
void GetDisplay(DWORD &Width, DWORD &Height);
void SetNewViewport(DWORD Width, DWORD Height);

// Surface vector functions
void AddSurfaceToVector(m_IDirectDrawSurfaceX* lpSurfaceX);
Expand Down

0 comments on commit 479e054

Please sign in to comment.