Skip to content

Commit

Permalink
Fix surface size when backing up surface
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Mar 13, 2024
1 parent 12885d2 commit d3bba67
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 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 6961
#define BUILD_NUMBER 6962
20 changes: 11 additions & 9 deletions ddraw/IDirectDrawSurfaceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2326,8 +2326,7 @@ HRESULT m_IDirectDrawSurfaceX::Lock2(LPRECT lpDestRect, LPDDSURFACEDESC2 lpDDSur
}
// Pitch for DXT surfaces in DirectDraw is the full surface byte size
LockedRect.Pitch =
(surfaceFormat == D3DFMT_DXT1) ? ((GetByteAlignedWidth(surfaceDesc2.dwWidth, surfaceBitCount) + 3) / 4) * ((surfaceDesc2.dwHeight + 3) / 4) * 8 :
ISDXTEX(surfaceFormat) ? ((GetByteAlignedWidth(surfaceDesc2.dwWidth, surfaceBitCount) + 3) / 4) * ((surfaceDesc2.dwHeight + 3) / 4) * 16 :
ISDXTEX(surfaceFormat) ? ((GetByteAlignedWidth(surfaceDesc2.dwWidth, surfaceBitCount) + 3) / 4) * ((surfaceDesc2.dwHeight + 3) / 4) * (surfaceFormat == D3DFMT_DXT1 ? 8 : 16) :
(surfaceFormat == D3DFMT_YV12) ? GetByteAlignedWidth(surfaceDesc2.dwWidth, surfaceBitCount) :
LockedRect.Pitch;
lpDDSurfaceDesc2->lPitch = LockedRect.Pitch;
Expand Down Expand Up @@ -3728,7 +3727,7 @@ HRESULT m_IDirectDrawSurfaceX::CreateD3d9Surface()
}
else if (!Backup.empty())
{
if (Backup.size() / surfaceDesc2.dwHeight == (DWORD)surfaceDesc2.lPitch)
if (Backup.size() == GetSurfaceSize(surfaceFormat, surfaceDesc2.dwWidth, surfaceDesc2.dwHeight, surfaceDesc2.lPitch))
{
IDirect3DSurface9* pDestSurfaceD9 = GetD3D9Surface();
if (pDestSurfaceD9)
Expand Down Expand Up @@ -4182,15 +4181,18 @@ void m_IDirectDrawSurfaceX::ReleaseD9Surface(bool BackupData, bool DeviceLost)
if (!IsUsingEmulation() && (surface.Texture || surface.Surface))
{
D3DLOCKED_RECT LockRect = {};
if (SUCCEEDED(LockD39Surface(&LockRect, nullptr, 0)))
if (SUCCEEDED(LockD39Surface(&LockRect, nullptr, D3DLOCK_READONLY)))
{
Logging::LogDebug() << __FUNCTION__ << " Storing Direct3D9 texture surface data";
Logging::LogDebug() << __FUNCTION__ << " Storing Direct3D9 texture surface data: " << surfaceFormat;

size_t size = surfaceDesc2.dwHeight * LockRect.Pitch;
size_t size = GetSurfaceSize(surfaceFormat, surfaceDesc2.dwWidth, surfaceDesc2.dwHeight, LockRect.Pitch);

Backup.resize(size);
if (size)
{
Backup.resize(size);

memcpy(Backup.data(), LockRect.pBits, size);
memcpy(Backup.data(), LockRect.pBits, size);
}

UnlockD39Surface();
}
Expand Down Expand Up @@ -5730,7 +5732,7 @@ HRESULT m_IDirectDrawSurfaceX::CopyToEmulatedSurface(LPRECT lpDestRect)

// Get lock for real surface
D3DLOCKED_RECT SrcLockRect = {};
if (FAILED(LockD39Surface(&SrcLockRect, &DestRect, 0)))
if (FAILED(LockD39Surface(&SrcLockRect, &DestRect, D3DLOCK_READONLY)))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: could not lock destination surface " << DestRect);
return (IsSurfaceLocked()) ? DDERR_SURFACEBUSY : DDERR_GENERIC;
Expand Down
21 changes: 21 additions & 0 deletions ddraw/IDirectDrawTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,27 @@ DWORD GetBitCount(D3DFORMAT Format)
};
}

DWORD GetSurfaceSize(D3DFORMAT Format, DWORD Width, DWORD Height, INT Pitch)
{
if (ISDXTEX(Format))
{
return ((GetByteAlignedWidth(Width, GetBitCount(Format)) + 3) / 4) * ((Height + 3) / 4) * (Format == D3DFMT_DXT1 ? 8 : 16);
}
else if (Format == D3DFMT_YV12)
{
return GetByteAlignedWidth(Width, GetBitCount(Format)) * Height;
}
else if (Format & 0xFF000000)
{
LOG_LIMIT(100, __FUNCTION__ << " Error: Surface size for surface format not Implemented: " << Format);
return 0;
}
else
{
return Pitch * Height;
}
}

D3DFORMAT ConvertSurfaceFormat(D3DFORMAT Format)
{
return (Format == D3DFMT_X8B8G8R8 || Format == D3DFMT_B8G8R8 || Format == D3DFMT_R8G8B8) ? D3DFMT_X8R8G8B8 :
Expand Down
1 change: 1 addition & 0 deletions ddraw/IDirectDrawTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ void ConvertCaps(DDCAPS &Caps7, D3DCAPS9 &Caps9);
DWORD GetByteAlignedWidth(DWORD Width, DWORD BitCount);
DWORD GetBitCount(DDPIXELFORMAT ddpfPixelFormat);
DWORD GetBitCount(D3DFORMAT Format);
DWORD GetSurfaceSize(D3DFORMAT Format, DWORD Width, DWORD Height, INT Pitch);
D3DFORMAT ConvertSurfaceFormat(D3DFORMAT Format);
D3DFORMAT GetFailoverFormat(D3DFORMAT Format);
D3DFORMAT GetDisplayFormat(DDPIXELFORMAT ddpfPixelFormat);
Expand Down

0 comments on commit d3bba67

Please sign in to comment.