From bfbf99dd12eace1db982c4a4ff63f4fa43c57d11 Mon Sep 17 00:00:00 2001 From: Elisha Riedlinger Date: Fri, 15 Nov 2024 14:29:51 -0800 Subject: [PATCH] Change how pitch is computed to reduce duplication --- Dllmain/BuildNo.rc | 2 +- ddraw/IDirectDrawSurfaceX.cpp | 167 +++++++++++++++++----------------- ddraw/IDirectDrawTypes.cpp | 125 ++++++++++++++++++++++--- ddraw/IDirectDrawTypes.h | 9 +- ddraw/IDirectDrawX.cpp | 6 +- 5 files changed, 206 insertions(+), 103 deletions(-) diff --git a/Dllmain/BuildNo.rc b/Dllmain/BuildNo.rc index 5eeb3a00..29d09f70 100644 --- a/Dllmain/BuildNo.rc +++ b/Dllmain/BuildNo.rc @@ -1 +1 @@ -#define BUILD_NUMBER 7324 +#define BUILD_NUMBER 7325 diff --git a/ddraw/IDirectDrawSurfaceX.cpp b/ddraw/IDirectDrawSurfaceX.cpp index 0654f37f..e0179d33 100644 --- a/ddraw/IDirectDrawSurfaceX.cpp +++ b/ddraw/IDirectDrawSurfaceX.cpp @@ -2392,85 +2392,78 @@ HRESULT m_IDirectDrawSurfaceX::GetSurfaceDesc2(LPDDSURFACEDESC2 lpDDSurfaceDesc2 // Copy surfacedesc to lpDDSurfaceDesc2 *lpDDSurfaceDesc2 = surfaceDesc2; - // Remove surface memory pointer - if (!surface.UsingSurfaceMemory) + // Handle mipmaps + if (MipMapLevel && MipMaps.size()) { - lpDDSurfaceDesc2->dwFlags &= ~DDSD_LPSURFACE; + // Remove a couple of flags + lpDDSurfaceDesc2->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH | DDSD_LINEARSIZE); lpDDSurfaceDesc2->lpSurface = nullptr; - } + lpDDSurfaceDesc2->lPitch = 0; - // Handle dummy mipmaps - if (IsDummyMipMap(MipMapLevel)) - { - DWORD Level = (MipMapLevel & ~DXW_IS_MIPMAP_DUMMY); - lpDDSurfaceDesc2->dwFlags &= ~DDSD_LPSURFACE; - lpDDSurfaceDesc2->dwFlags |= DDSD_PITCH; - DWORD BitCount = surface.BitCount ? surface.BitCount : GetBitCount(lpDDSurfaceDesc2->ddpfPixelFormat); - DWORD Width = surface.Width ? surface.Width : GetByteAlignedWidth(lpDDSurfaceDesc2->dwWidth, BitCount); - DWORD Height = surface.Height ? surface.Height : GetByteAlignedWidth(lpDDSurfaceDesc2->dwHeight, BitCount); - lpDDSurfaceDesc2->dwWidth = max(1, Width >> Level); - lpDDSurfaceDesc2->dwHeight = max(1, Height >> Level); - lpDDSurfaceDesc2->lpSurface = nullptr; - lpDDSurfaceDesc2->lPitch = ComputePitch(lpDDSurfaceDesc2->dwWidth, BitCount); + // Handle new v7 flag if (DirectXVersion == 7) { lpDDSurfaceDesc2->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL; } - if (lpDDSurfaceDesc2->dwFlags & DDSD_MIPMAPCOUNT) + + // Handle dummy mipmaps + if (IsDummyMipMap(MipMapLevel)) { - lpDDSurfaceDesc2->dwMipMapCount = 1; - } - } - // Handle MipMap sub-level - else if (MipMapLevel && MipMaps.size()) - { - // Check for device interface to ensure correct max MipMap level - CheckInterface(__FUNCTION__, true, true, false); + DWORD Level = (MipMapLevel & ~DXW_IS_MIPMAP_DUMMY); - // Remove a couple of flags - lpDDSurfaceDesc2->dwFlags &= ~(DDSD_LPSURFACE | DDSD_PITCH); - lpDDSurfaceDesc2->lpSurface = nullptr; + // Get width and height + DWORD BitCount = surface.BitCount ? surface.BitCount : GetBitCount(lpDDSurfaceDesc2->ddpfPixelFormat); + DWORD Width = surface.Width ? surface.Width : GetByteAlignedWidth(lpDDSurfaceDesc2->dwWidth, BitCount); + DWORD Height = surface.Height ? surface.Height : GetByteAlignedWidth(lpDDSurfaceDesc2->dwHeight, BitCount); + lpDDSurfaceDesc2->dwWidth = max(1, Width >> Level); + lpDDSurfaceDesc2->dwHeight = max(1, Height >> Level); - // Get surface level desc - DWORD Level = min(MipMaps.size(), MipMapLevel) - 1; - if ((!MipMaps[Level].dwWidth || !MipMaps[Level].dwHeight) && surface.Texture) - { - D3DSURFACE_DESC Desc = {}; - surface.Texture->GetLevelDesc(GetD3d9MipMapLevel(MipMapLevel), &Desc); - MipMaps[Level].dwWidth = Desc.Width; - MipMaps[Level].dwHeight = Desc.Height; - } - lpDDSurfaceDesc2->dwWidth = MipMaps[Level].dwWidth; - lpDDSurfaceDesc2->dwHeight = MipMaps[Level].dwHeight; - if (MipMaps[Level].lPitch) - { - lpDDSurfaceDesc2->lPitch = MipMaps[Level].lPitch; - lpDDSurfaceDesc2->dwFlags |= DDSD_PITCH; + // Mipmap count + lpDDSurfaceDesc2->dwMipMapCount = 1; } + // Handle normal mipmaps else { - DWORD BitCount = surface.BitCount ? surface.BitCount : GetBitCount(lpDDSurfaceDesc2->ddpfPixelFormat); - lpDDSurfaceDesc2->lPitch = ComputePitch(lpDDSurfaceDesc2->dwWidth, BitCount); - lpDDSurfaceDesc2->dwFlags |= DDSD_PITCH; + // Check for device interface to ensure correct max MipMap level + CheckInterface(__FUNCTION__, true, true, false); + + // Get width and height + DWORD Level = min(MipMaps.size(), MipMapLevel) - 1; + if ((!MipMaps[Level].dwWidth || !MipMaps[Level].dwHeight) && surface.Texture) + { + D3DSURFACE_DESC Desc = {}; + surface.Texture->GetLevelDesc(GetD3d9MipMapLevel(MipMapLevel), &Desc); + MipMaps[Level].dwWidth = Desc.Width; + MipMaps[Level].dwHeight = Desc.Height; + } + lpDDSurfaceDesc2->dwWidth = MipMaps[Level].dwWidth; + lpDDSurfaceDesc2->dwHeight = MipMaps[Level].dwHeight; + + // Set pitch + if (MipMaps[Level].lPitch) + { + lpDDSurfaceDesc2->dwFlags |= DDSD_PITCH; + lpDDSurfaceDesc2->lPitch = MipMaps[Level].lPitch; + } + + // Mipmap count + lpDDSurfaceDesc2->dwMipMapCount = MaxMipMapLevel + 1 > MipMapLevel ? MaxMipMapLevel + 1 - MipMapLevel : 1; } - if (MipMapLevel != 0 && DirectXVersion == 7) + + // Set pitch + if (!(lpDDSurfaceDesc2->dwFlags & DDSD_PITCH)) { - lpDDSurfaceDesc2->ddsCaps.dwCaps2 |= DDSCAPS2_MIPMAPSUBLEVEL; + DWORD Pitch = ComputePitch(GetDisplayFormat(lpDDSurfaceDesc2->ddpfPixelFormat), lpDDSurfaceDesc2->dwWidth, lpDDSurfaceDesc2->dwHeight); + if (Pitch) + { + lpDDSurfaceDesc2->dwFlags |= DDSD_PITCH; + lpDDSurfaceDesc2->lPitch = Pitch; + } } - lpDDSurfaceDesc2->dwMipMapCount = MaxMipMapLevel + 1 > MipMapLevel ? MaxMipMapLevel + 1 - MipMapLevel : 1; } - // Root mipmap or no mipmaps - else + else if (MipMapLevel) { - // Get lPitch if not set - if ((lpDDSurfaceDesc2->dwFlags & (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT)) == (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT) && - (lpDDSurfaceDesc2->ddpfPixelFormat.dwFlags & DDPF_RGB) && !(lpDDSurfaceDesc2->dwFlags & (DDSD_PITCH | DDSD_LINEARSIZE))) - { - DWORD BitCount = surface.BitCount ? surface.BitCount : GetBitCount(lpDDSurfaceDesc2->ddpfPixelFormat); - DWORD Width = surface.Width ? surface.Width : GetByteAlignedWidth(lpDDSurfaceDesc2->dwWidth, BitCount); - lpDDSurfaceDesc2->lPitch = ComputePitch(Width, BitCount); - lpDDSurfaceDesc2->dwFlags |= DDSD_PITCH; - } + LOG_LIMIT(100, __FUNCTION__ << " Warning: MipMap found with no MipMap list!"); } // Handle managed texture memory type @@ -2696,10 +2689,14 @@ HRESULT m_IDirectDrawSurfaceX::Lock2(LPRECT lpDestRect, LPDDSURFACEDESC2 lpDDSur // Handle dummy mipmaps if (IsDummyMipMap(MipMapLevel)) { - lpDDSurfaceDesc2->dwFlags |= DDSD_LPSURFACE | DDSD_PITCH; - lpDDSurfaceDesc2->lPitch = ComputePitch(lpDDSurfaceDesc2->dwWidth, surface.BitCount); + lpDDSurfaceDesc2->dwFlags |= DDSD_LPSURFACE; // Add surface size to dummy data address to ensure that each mipmap gets a unique address lpDDSurfaceDesc2->lpSurface = dummySurface.data() + (lpDDSurfaceDesc2->dwWidth * lpDDSurfaceDesc2->dwHeight * surface.BitCount); + if (!(lpDDSurfaceDesc2->dwFlags & DDSD_PITCH)) + { + LOG_LIMIT(100, __FUNCTION__ << " Error: no pitch found!"); + return DDERR_GENERIC; + } return DD_OK; } @@ -2845,8 +2842,8 @@ HRESULT m_IDirectDrawSurfaceX::Lock2(LPRECT lpDestRect, LPDDSURFACEDESC2 lpDDSur // Pitch for DXT surfaces in DirectDraw is the full surface byte size LockedRect.Pitch = - ISDXTEX(surface.Format) ? GetSurfaceSize(surface.Format, lpDDSurfaceDesc2->dwWidth, lpDDSurfaceDesc2->dwHeight, 0) : - (surface.Format == D3DFMT_YV12 || surface.Format == D3DFMT_NV12) ? surface.Width : + (ISDXTEX(surface.Format) || surface.Format == D3DFMT_YV12 || surface.Format == D3DFMT_NV12) ? + ComputePitch(surface.Format, lpDDSurfaceDesc2->dwWidth, lpDDSurfaceDesc2->dwHeight) : LockedRect.Pitch; lpDDSurfaceDesc2->lPitch = LockedRect.Pitch; lpDDSurfaceDesc2->dwFlags |= DDSD_PITCH; @@ -2869,7 +2866,7 @@ HRESULT m_IDirectDrawSurfaceX::Lock2(LPRECT lpDestRect, LPDDSURFACEDESC2 lpDDSur { LOG_LIMIT(100, __FUNCTION__ << " (" << this << ")" << " Warning: surface pitch does not match locked pitch! Format: " << surface.Format << " Width: " << surfaceDesc2.dwWidth << " Pitch: " << surfaceDesc2.lPitch << "->" << LockedRect.Pitch << - " Default: " << ComputePitch(surface.Width, surface.BitCount) << " BitCount: " << surface.BitCount); + " Default: " << ComputePitch(surface.Format, surface.Width, surface.BitCount) << " BitCount: " << surface.BitCount); } surfaceDesc2.lPitch = LockedRect.Pitch; surfaceDesc2.dwFlags |= DDSD_PITCH; @@ -4812,7 +4809,7 @@ inline bool m_IDirectDrawSurfaceX::DoesDCMatch(EMUSURFACE* pEmuSurface) // Adjust Width to be byte-aligned DWORD Width = GetByteAlignedWidth(surfaceDesc2.dwWidth, surface.BitCount); DWORD Height = surfaceDesc2.dwHeight; - DWORD Pitch = ComputePitch(Width, surface.BitCount); + DWORD Pitch = ComputePitch(surface.Format, Width, surface.BitCount); if (pEmuSurface->bmi->bmiHeader.biWidth == (LONG)Width && pEmuSurface->bmi->bmiHeader.biHeight == -(LONG)Height && @@ -5009,7 +5006,7 @@ HRESULT m_IDirectDrawSurfaceX::CreateDCSurface() } surface.emu->bmi->bmiHeader.biHeight = -(LONG)Height; surface.emu->Format = surface.Format; - surface.emu->Pitch = ComputePitch(surface.emu->bmi->bmiHeader.biWidth, surface.emu->bmi->bmiHeader.biBitCount); + surface.emu->Pitch = ComputePitch(surface.Format, surface.emu->bmi->bmiHeader.biWidth, surface.emu->bmi->bmiHeader.biBitCount); surface.emu->Size = Height * surface.emu->Pitch; return DD_OK; @@ -5048,15 +5045,10 @@ void m_IDirectDrawSurfaceX::UpdateAttachedDepthStencil(m_IDirectDrawSurfaceX* lp // Update surface description void m_IDirectDrawSurfaceX::UpdateSurfaceDesc() { - // Check for device interface - if (FAILED(CheckInterface(__FUNCTION__, false, false, false))) - { - return; - } - bool IsChanged = false; - if ((surfaceDesc2.dwFlags & (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT)) != (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT) || - ((surfaceDesc2.dwFlags & DDSD_REFRESHRATE) && !surfaceDesc2.dwRefreshRate)) + if (SUCCEEDED(CheckInterface(__FUNCTION__, false, false, false)) && + ((surfaceDesc2.dwFlags & (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT)) != (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT) || + ((surfaceDesc2.dwFlags & DDSD_REFRESHRATE) && !surfaceDesc2.dwRefreshRate))) { // Get resolution DWORD Width, Height, RefreshRate, BPP; @@ -5099,6 +5091,12 @@ void m_IDirectDrawSurfaceX::UpdateSurfaceDesc() } } } + // Remove surface memory pointer + if (!surface.UsingSurfaceMemory) + { + surfaceDesc2.dwFlags &= ~DDSD_LPSURFACE; + surfaceDesc2.lpSurface = nullptr; + } // Unset lPitch if ((((surfaceDesc2.dwFlags & (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT)) != (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT) || !(surfaceDesc2.dwFlags & DDSD_PITCH)) && !(surfaceDesc2.dwFlags & DDSD_LINEARSIZE)) || !surfaceDesc2.lPitch) @@ -5108,12 +5106,14 @@ void m_IDirectDrawSurfaceX::UpdateSurfaceDesc() } // Set lPitch if ((surfaceDesc2.dwFlags & (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT)) == (DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT) && - (surfaceDesc2.ddpfPixelFormat.dwFlags & DDPF_RGB) && !(surfaceDesc2.dwFlags & DDSD_LINEARSIZE) && !(surfaceDesc2.dwFlags & DDSD_PITCH)) + !(surfaceDesc2.dwFlags & DDSD_LINEARSIZE) && !(surfaceDesc2.dwFlags & DDSD_PITCH)) { - surfaceDesc2.dwFlags |= DDSD_PITCH; - DWORD BitCount = surface.BitCount ? surface.BitCount : GetBitCount(surfaceDesc2.ddpfPixelFormat); - DWORD Width = surface.Width ? surface.Width : GetByteAlignedWidth(surfaceDesc2.dwWidth, BitCount); - surfaceDesc2.lPitch = surface.UsingSurfaceMemory ? surfaceDesc2.dwWidth * BitCount : ComputePitch(Width, BitCount); + DWORD Pitch = ComputePitch(GetDisplayFormat(surfaceDesc2.ddpfPixelFormat), surfaceDesc2.dwWidth, surfaceDesc2.dwHeight); + if (Pitch) + { + surfaceDesc2.dwFlags |= DDSD_PITCH; + surfaceDesc2.lPitch = Pitch; + } } // Set surface format if (surface.Format == D3DFMT_UNKNOWN && (surfaceDesc2.dwFlags & DDSD_PIXELFORMAT)) @@ -6050,7 +6050,7 @@ inline void m_IDirectDrawSurfaceX::InitSurfaceDesc(DWORD DirectXVersion) } // Clear pitch - if (!(surfaceDesc2.dwFlags & DDSD_LPSURFACE) && !(surfaceDesc2.dwFlags & DDSD_LINEARSIZE)) + if (!(surfaceDesc2.dwFlags & DDSD_LPSURFACE)) { surfaceDesc2.dwFlags &= ~DDSD_PITCH; surfaceDesc2.lPitch = 0; @@ -6065,6 +6065,9 @@ inline void m_IDirectDrawSurfaceX::InitSurfaceDesc(DWORD DirectXVersion) } surfaceDesc2.ddsCaps.dwCaps4 = 0x00; surfaceDesc2.dwReserved = 0; + + // Clear unused values + ClearUnusedValues(surfaceDesc2); } // Add attached surface to map diff --git a/ddraw/IDirectDrawTypes.cpp b/ddraw/IDirectDrawTypes.cpp index 4db664b4..21eeacaa 100644 --- a/ddraw/IDirectDrawTypes.cpp +++ b/ddraw/IDirectDrawTypes.cpp @@ -123,6 +123,78 @@ void ConvertSurfaceDesc(DDSURFACEDESC2 &Desc2, DDSURFACEDESC &Desc) } } +void ClearUnusedValues(DDSURFACEDESC2& Desc2) +{ + // Check for supported dwSize + if (Desc2.dwSize != sizeof(DDSURFACEDESC2)) + { + LOG_LIMIT(100, __FUNCTION__ << " Error: Incorrect dwSize: " << Desc2.dwSize); + return; + } + + if (!(Desc2.dwFlags & DDSD_HEIGHT)) Desc2.dwHeight = 0; + if (!(Desc2.dwFlags & DDSD_WIDTH)) Desc2.dwWidth = 0; + if (!(Desc2.dwFlags & (DDSD_PITCH | DDSD_LINEARSIZE))) Desc2.lPitch = 0; + if (!(Desc2.dwFlags & (DDSD_BACKBUFFERCOUNT | DDSD_DEPTH))) Desc2.dwBackBufferCount = 0; + if (!(Desc2.dwFlags & (DDSD_MIPMAPCOUNT | DDSD_REFRESHRATE | DDSD_SRCVBHANDLE))) Desc2.dwMipMapCount = 0; + if (!(Desc2.dwFlags & DDSD_ALPHABITDEPTH)) Desc2.dwAlphaBitDepth = 0; + + Desc2.dwReserved = 0; + + if (!(Desc2.dwFlags & DDSD_LPSURFACE)) Desc2.lpSurface = nullptr; + + if (!(Desc2.dwFlags & DDSD_CKDESTOVERLAY)) + { + Desc2.ddckCKDestOverlay.dwColorSpaceLowValue = 0; + Desc2.ddckCKDestOverlay.dwColorSpaceHighValue = 0; + } + + if (!(Desc2.dwFlags & DDSD_CKDESTBLT)) + { + Desc2.ddckCKDestBlt.dwColorSpaceLowValue = 0; + Desc2.ddckCKDestBlt.dwColorSpaceHighValue = 0; + } + + if (!(Desc2.dwFlags & DDSD_CKSRCOVERLAY)) + { + Desc2.ddckCKSrcOverlay.dwColorSpaceLowValue = 0; + Desc2.ddckCKSrcOverlay.dwColorSpaceHighValue = 0; + } + + if (!(Desc2.dwFlags & DDSD_CKSRCBLT)) + { + Desc2.ddckCKSrcBlt.dwColorSpaceLowValue = 0; + Desc2.ddckCKSrcBlt.dwColorSpaceHighValue = 0; + } + + if (!(Desc2.dwFlags & DDSD_PIXELFORMAT)) + { + if (Desc2.dwFlags & DDSD_FVF) + { + // Preserve the dwFVF value and clear ddpfPixelFormat + DWORD dwFVF = Desc2.dwFVF; + Desc2.ddpfPixelFormat = {}; // Clears the pixel format + Desc2.dwFVF = dwFVF; // Restore dwFVF + } + else + { + Desc2.ddpfPixelFormat = {}; + } + } + else + { + Desc2.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT); + } + + if (!(Desc2.dwFlags & DDSD_CAPS)) + { + LOG_LIMIT(100, __FUNCTION__ << " Warning: Surface desc has no caps!"); + Desc2.ddsCaps = {}; + } + + if (!(Desc2.dwFlags & DDSD_TEXTURESTAGE)) Desc2.dwTextureStage = 0; +} + void ConvertPixelFormat(DDPIXELFORMAT& Format, DDS_PIXELFORMAT& Format2) { if (Format.dwSize != sizeof(DDPIXELFORMAT) || Format2.dwSize != sizeof(DDS_PIXELFORMAT)) @@ -604,7 +676,7 @@ float ConvertDepthValue(DWORD dwFillDepth, D3DFORMAT Format) } } -DWORD GetSurfaceSize(D3DFORMAT Format, DWORD Width, DWORD Height, INT Pitch) +DWORD ComputePitch(D3DFORMAT Format, DWORD Width, DWORD Height) { if (ISDXTEX(Format)) { @@ -612,17 +684,46 @@ DWORD GetSurfaceSize(D3DFORMAT Format, DWORD Width, DWORD Height, INT Pitch) } else if (Format == D3DFMT_YV12 || Format == D3DFMT_NV12) { - return GetByteAlignedWidth(Width, GetBitCount(Format)) * Height; + return GetByteAlignedWidth(Width, GetBitCount(Format)); } - else if (Format & 0xFF000000) + else if (Format & 0xFF000000) // Other FourCC types { - LOG_LIMIT(100, __FUNCTION__ << " Error: Surface size for surface format not Implemented: " << Format); return 0; } else + { + return ((((Width * GetBitCount(Format)) + 31) & ~31) >> 3); // Use Surface Stride for pitch + } +} + +DWORD GetSurfaceSize(D3DFORMAT Format, DWORD Width, DWORD Height, INT Pitch) +{ + if (Format & 0xFF000000) // All FourCC types + { + if (ISDXTEX(Format)) + { + return ComputePitch(Format, Width, Height); // Picth for DXT surfaces is the full surface size + } + else + { + DWORD Size = ComputePitch(Format, Width, Height); + if (Size) + { + return Size * Height; + } + else if (Pitch) + { + return Pitch * Height; + } + } + } + else { return Pitch * Height; } + + LOG_LIMIT(100, __FUNCTION__ << " Error: Surface size for surface format not Implemented: " << Format); + return 0; } // Count leading zeros and total number of bits @@ -1226,21 +1327,22 @@ void SetPixelDisplayFormat(D3DFORMAT Format, DDPIXELFORMAT &ddpfPixelFormat) } } -HRESULT SetDisplayFormat(DDPIXELFORMAT &ddpfPixelFormat, DWORD BPP) +D3DFORMAT SetDisplayFormat(DDPIXELFORMAT &ddpfPixelFormat, DWORD BPP) { + D3DFORMAT Format = D3DFMT_UNKNOWN; switch (BPP) { case 8: - SetPixelDisplayFormat(D3DFMT_P8, ddpfPixelFormat); + Format = D3DFMT_P8; break; case 16: - SetPixelDisplayFormat(D3DFMT_R5G6B5, ddpfPixelFormat); + Format = D3DFMT_R5G6B5; break; case 24: - SetPixelDisplayFormat(D3DFMT_R8G8B8, ddpfPixelFormat); + Format = D3DFMT_R8G8B8; break; case 32: - SetPixelDisplayFormat(D3DFMT_X8R8G8B8, ddpfPixelFormat); + Format = D3DFMT_X8R8G8B8; break; default: LOG_LIMIT(100, __FUNCTION__ << " Error: Bit mode not supported: " << BPP); @@ -1251,7 +1353,8 @@ HRESULT SetDisplayFormat(DDPIXELFORMAT &ddpfPixelFormat, DWORD BPP) ddpfPixelFormat.dwGBitMask = 0x0; ddpfPixelFormat.dwBBitMask = 0x0; ddpfPixelFormat.dwRGBAlphaBitMask = 0x0; - return DDERR_UNSUPPORTED; + return Format; } - return DD_OK; + SetPixelDisplayFormat(Format, ddpfPixelFormat); + return Format; } diff --git a/ddraw/IDirectDrawTypes.h b/ddraw/IDirectDrawTypes.h index 7a795b1e..1f71632b 100644 --- a/ddraw/IDirectDrawTypes.h +++ b/ddraw/IDirectDrawTypes.h @@ -128,13 +128,9 @@ static constexpr DWORD DDS_HEADER_FLAGS_PITCH = 0x00000008; static constexpr DWORD MaxPaletteSize = 256; -inline DWORD ComputePitch(DWORD Width, DWORD BitCount) -{ - return ((((Width * BitCount) + 31) & ~31) >> 3); // Use Surface Stride for pitch -} - void ConvertSurfaceDesc(DDSURFACEDESC &Desc, DDSURFACEDESC2 &Desc2); void ConvertSurfaceDesc(DDSURFACEDESC2 &Desc2, DDSURFACEDESC &Desc); +void ClearUnusedValues(DDSURFACEDESC2& Desc2); void ConvertPixelFormat(DDPIXELFORMAT& Format, DDS_PIXELFORMAT &Format2); void ConvertDeviceIdentifier(DDDEVICEIDENTIFIER &DeviceID, DDDEVICEIDENTIFIER2 &DeviceID2); void ConvertDeviceIdentifier(DDDEVICEIDENTIFIER2 &DeviceID2, DDDEVICEIDENTIFIER &DeviceID); @@ -149,6 +145,7 @@ DWORD GetMaxMipMapLevel(DWORD Width, DWORD Height); DWORD GetBitCount(DDPIXELFORMAT ddpfPixelFormat); DWORD GetBitCount(D3DFORMAT Format); float ConvertDepthValue(DWORD dwFillDepth, D3DFORMAT Format); +DWORD ComputePitch(D3DFORMAT Format, DWORD Width, DWORD Height); DWORD GetSurfaceSize(D3DFORMAT Format, DWORD Width, DWORD Height, INT Pitch); DWORD GetARGBColorKey(DWORD ColorKey, DDPIXELFORMAT& pixelFormat); void GetColorKeyArray(float(&lowColorKey)[4], float(&highColorKey)[4], DWORD lowColorSpace, DWORD highColorSpace, DDPIXELFORMAT& pixelFormat); @@ -156,4 +153,4 @@ D3DFORMAT ConvertSurfaceFormat(D3DFORMAT Format); D3DFORMAT GetFailoverFormat(D3DFORMAT Format); D3DFORMAT GetDisplayFormat(DDPIXELFORMAT ddpfPixelFormat); void SetPixelDisplayFormat(D3DFORMAT Format, DDPIXELFORMAT &lpPixelFormat); -HRESULT SetDisplayFormat(DDPIXELFORMAT &ddpfPixelFormat, DWORD BPP); +D3DFORMAT SetDisplayFormat(DDPIXELFORMAT &ddpfPixelFormat, DWORD BPP); diff --git a/ddraw/IDirectDrawX.cpp b/ddraw/IDirectDrawX.cpp index 58f7962e..c705bb75 100644 --- a/ddraw/IDirectDrawX.cpp +++ b/ddraw/IDirectDrawX.cpp @@ -1035,11 +1035,11 @@ HRESULT m_IDirectDrawX::EnumDisplayModes2(DWORD dwFlags, LPDDSURFACEDESC2 lpDDSu // Set adapter pixel format Desc2.dwFlags |= DDSD_PIXELFORMAT; Desc2.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT); - SetDisplayFormat(Desc2.ddpfPixelFormat, DisplayBitCount); + D3DFORMAT Format = SetDisplayFormat(Desc2.ddpfPixelFormat, DisplayBitCount); // Set pitch Desc2.dwFlags |= DDSD_PITCH; - Desc2.lPitch = ComputePitch(GetByteAlignedWidth(d3ddispmode.Width, DisplayBitCount), DisplayBitCount); + Desc2.lPitch = ComputePitch(Format, GetByteAlignedWidth(d3ddispmode.Width, DisplayBitCount), DisplayBitCount); if (lpEnumModesCallback2(&Desc2, lpContext) == DDENUMRET_CANCEL) { @@ -1395,7 +1395,7 @@ HRESULT m_IDirectDrawX::GetDisplayMode2(LPDDSURFACEDESC2 lpDDSurfaceDesc2) { lpDDSurfaceDesc2->ddpfPixelFormat = DisplayPixelFormat; } - else if (FAILED(SetDisplayFormat(lpDDSurfaceDesc2->ddpfPixelFormat, displayModeBits))) + else if (SetDisplayFormat(lpDDSurfaceDesc2->ddpfPixelFormat, displayModeBits)) { LOG_LIMIT(100, __FUNCTION__ << " Error: Not implemented bit count " << displayModeBits); return DDERR_UNSUPPORTED;