Skip to content

Commit

Permalink
Fallback to alpha surface if create fails
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Jan 25, 2024
1 parent 82125a8 commit 03036c1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 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 6943
#define BUILD_NUMBER 6944
50 changes: 35 additions & 15 deletions ddraw/IDirectDrawSurfaceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3651,9 +3651,13 @@ HRESULT m_IDirectDrawSurfaceX::CreateD3d9Surface()
{
if (FAILED(((*d3d9Device)->CreateTexture(Width, Height, 1, 0, TextureFormat, TexturePool, &surface.Texture, nullptr))))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create surface texture. Size: " << Width << "x" << Height << " Format: " << surfaceFormat << " dwCaps: " << Logging::hex(surfaceDesc2.ddsCaps.dwCaps));
hr = DDERR_GENERIC;
break;
// Try failover format
if (FAILED(((*d3d9Device)->CreateTexture(Width, Height, 1, 0, GetFailoverFormat(TextureFormat), TexturePool, &surface.Texture, nullptr))))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create surface texture. Size: " << Width << "x" << Height << " Format: " << surfaceFormat << " dwCaps: " << Logging::hex(surfaceDesc2.ddsCaps.dwCaps));
hr = DDERR_GENERIC;
break;
}
}
}

Expand All @@ -3662,9 +3666,13 @@ HRESULT m_IDirectDrawSurfaceX::CreateD3d9Surface()
{
if (FAILED(((*d3d9Device)->CreateOffscreenPlainSurface(Width, Height, Format, D3DPOOL_DEFAULT, &primary.BlankSurface, nullptr))))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create blank surface. Size: " << Width << "x" << Height << " Format: " << surfaceFormat << " dwCaps: " << Logging::hex(surfaceDesc2.ddsCaps.dwCaps));
hr = DDERR_GENERIC;
break;
// Try failover format
if (FAILED(((*d3d9Device)->CreateOffscreenPlainSurface(Width, Height, GetFailoverFormat(Format), D3DPOOL_DEFAULT, &primary.BlankSurface, nullptr))))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create blank surface. Size: " << Width << "x" << Height << " Format: " << surfaceFormat << " dwCaps: " << Logging::hex(surfaceDesc2.ddsCaps.dwCaps));
hr = DDERR_GENERIC;
break;
}
}
}

Expand All @@ -3673,9 +3681,13 @@ HRESULT m_IDirectDrawSurfaceX::CreateD3d9Surface()
{
if (FAILED(((*d3d9Device)->CreateTexture(Width, Height, 1, 0, TextureFormat, D3DPOOL_DEFAULT, &PrimaryDisplayTexture, nullptr))))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create primary surface texture. Size: " << Width << "x" << Height << " Format: " << surfaceFormat << " dwCaps: " << Logging::hex(surfaceDesc2.ddsCaps.dwCaps));
hr = DDERR_GENERIC;
break;
// Try failover format
if (FAILED(((*d3d9Device)->CreateTexture(Width, Height, 1, 0, GetFailoverFormat(TextureFormat), D3DPOOL_DEFAULT, &PrimaryDisplayTexture, nullptr))))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create primary surface texture. Size: " << Width << "x" << Height << " Format: " << surfaceFormat << " dwCaps: " << Logging::hex(surfaceDesc2.ddsCaps.dwCaps));
hr = DDERR_GENERIC;
break;
}
}
}

Expand All @@ -3684,9 +3696,13 @@ HRESULT m_IDirectDrawSurfaceX::CreateD3d9Surface()
{
if (FAILED(((*d3d9Device)->CreateTexture(MaxPaletteSize, MaxPaletteSize, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &primary.PaletteTexture, nullptr))))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create palette surface texture");
hr = DDERR_GENERIC;
break;
// Try failover format
if (FAILED(((*d3d9Device)->CreateTexture(MaxPaletteSize, MaxPaletteSize, 1, 0, GetFailoverFormat(D3DFMT_X8R8G8B8), D3DPOOL_MANAGED, &primary.PaletteTexture, nullptr))))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create palette surface texture");
hr = DDERR_GENERIC;
break;
}
}
}

Expand Down Expand Up @@ -5829,9 +5845,13 @@ inline HRESULT m_IDirectDrawSurfaceX::CopyEmulatedPaletteSurface(LPRECT lpDestRe
LOG_LIMIT(3, __FUNCTION__ << " Creating palette display surface texture. Size: " << Width << "x" << Height << " dwCaps: " << Logging::hex(surfaceDesc2.ddsCaps.dwCaps));
if (FAILED(((*d3d9Device)->CreateTexture(Width, Height, 1, 0, D3DFMT_X8R8G8B8, TexturePool, &surface.DisplayTexture, nullptr))))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create palette display surface texture. Size: " << Width << "x" << Height << " Format: " << D3DFMT_X8R8G8B8 << " dwCaps: " << Logging::hex(surfaceDesc2.ddsCaps.dwCaps));
hr = DDERR_GENERIC;
break;
// Try failover format
if (FAILED(((*d3d9Device)->CreateTexture(Width, Height, 1, 0, GetFailoverFormat(D3DFMT_X8R8G8B8), TexturePool, &surface.DisplayTexture, nullptr))))
{
LOG_LIMIT(100, __FUNCTION__ << " Error: failed to create palette display surface texture. Size: " << Width << "x" << Height << " Format: " << D3DFMT_X8R8G8B8 << " dwCaps: " << Logging::hex(surfaceDesc2.ddsCaps.dwCaps));
hr = DDERR_GENERIC;
break;
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions ddraw/IDirectDrawTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,27 @@ D3DFORMAT ConvertSurfaceFormat(D3DFORMAT Format)
(Format == D3DFMT_A8B8G8R8) ? D3DFMT_A8R8G8B8 : Format;
}

D3DFORMAT GetFailoverFormat(D3DFORMAT Format)
{
std::vector<std::pair<D3DFORMAT, D3DFORMAT>> FormatVector =
{
{D3DFMT_X1R5G5B5, D3DFMT_A1R5G5B5},
{D3DFMT_X4R4G4B4, D3DFMT_A4R4G4B4},
{D3DFMT_X8R8G8B8, D3DFMT_A8R8G8B8},
{D3DFMT_X8B8G8R8, D3DFMT_A8B8G8R8},
};

for (const auto& FormatPair : FormatVector)
{
if (Format == FormatPair.first)
{
return FormatPair.second;
}
}

return Format;
}

D3DFORMAT GetDisplayFormat(DDPIXELFORMAT ddpfPixelFormat)
{
if (ddpfPixelFormat.dwSize != sizeof(DDPIXELFORMAT))
Expand Down
1 change: 1 addition & 0 deletions ddraw/IDirectDrawTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ DWORD GetByteAlignedWidth(DWORD Width, DWORD BitCount);
DWORD GetBitCount(DDPIXELFORMAT ddpfPixelFormat);
DWORD GetBitCount(D3DFORMAT Format);
D3DFORMAT ConvertSurfaceFormat(D3DFORMAT Format);
D3DFORMAT GetFailoverFormat(D3DFORMAT Format);
D3DFORMAT GetDisplayFormat(DDPIXELFORMAT ddpfPixelFormat);
void SetPixelDisplayFormat(D3DFORMAT Format, DDPIXELFORMAT &lpPixelFormat);
HRESULT SetDisplayFormat(DDPIXELFORMAT &ddpfPixelFormat, DWORD BPP);

0 comments on commit 03036c1

Please sign in to comment.