From d817c4bfb3917a7482c353cae87c2a11218def94 Mon Sep 17 00:00:00 2001 From: narzoul Date: Sat, 12 Jan 2019 16:55:53 +0100 Subject: [PATCH] Simplified 8/16 bit display mode emulation --- DDrawCompat/D3dDdi/Device.cpp | 5 ++ DDrawCompat/DDraw/DirectDraw.cpp | 48 +---------- DDrawCompat/DDraw/DirectDraw.h | 10 --- DDrawCompat/DDraw/Surfaces/Surface.cpp | 10 +-- DDrawCompat/Dll/DllMain.cpp | 2 +- DDrawCompat/Gdi/VirtualScreen.cpp | 5 +- DDrawCompat/Win32/DisplayMode.cpp | 107 +------------------------ DDrawCompat/Win32/DisplayMode.h | 3 +- 8 files changed, 17 insertions(+), 173 deletions(-) diff --git a/DDrawCompat/D3dDdi/Device.cpp b/DDrawCompat/D3dDdi/Device.cpp index c1ede7b..21c644a 100644 --- a/DDrawCompat/D3dDdi/Device.cpp +++ b/DDrawCompat/D3dDdi/Device.cpp @@ -187,6 +187,11 @@ namespace D3dDdi } } + if (data.Flags.Primary) + { + data.Format = D3DDDIFMT_X8R8G8B8; + } + HRESULT result = origCreateResource(m_device, &data); if (SUCCEEDED(result) && data.Flags.RenderTarget && !data.Flags.Primary && isVidMemPool(data.Pool)) { diff --git a/DDrawCompat/DDraw/DirectDraw.cpp b/DDrawCompat/DDraw/DirectDraw.cpp index 9f50a45..aee5573 100644 --- a/DDrawCompat/DDraw/DirectDraw.cpp +++ b/DDrawCompat/DDraw/DirectDraw.cpp @@ -5,26 +5,6 @@ #include "DDraw/Surfaces/PrimarySurface.h" #include "Win32/DisplayMode.h" -namespace -{ - template - HRESULT setDisplayMode(TDirectDraw* This, DWORD width, DWORD height, DWORD bpp) - { - return DDraw::DirectDraw::s_origVtable.SetDisplayMode(This, width, height, bpp); - } - - template - HRESULT setDisplayMode(TDirectDraw* This, DWORD width, DWORD height, DWORD bpp, - DWORD refreshRate, DWORD flags) - { - Win32::DisplayMode::setDDrawBpp(bpp); - HRESULT result = DDraw::DirectDraw::s_origVtable.SetDisplayMode( - This, width, height, 32, refreshRate, flags); - Win32::DisplayMode::setDDrawBpp(0); - return result; - } -} - namespace DDraw { template @@ -42,7 +22,7 @@ namespace DDraw { DDSURFACEDESC2 dm = {}; dm.dwSize = sizeof(dm); - dd.get().lpVtbl->GetDisplayMode(&dd, &dm); + dd->GetDisplayMode(&dd, &dm); return dm; } @@ -97,10 +77,8 @@ namespace DDraw { vtable.CreateSurface = &CreateSurface; vtable.FlipToGDISurface = &FlipToGDISurface; - vtable.GetDisplayMode = &GetDisplayMode; vtable.GetGDISurface = &GetGDISurface; vtable.SetCooperativeLevel = &SetCooperativeLevel; - vtable.SetDisplayMode = &SetDisplayMode; vtable.WaitForVerticalBlank = &WaitForVerticalBlank; } @@ -132,18 +110,6 @@ namespace DDraw return PrimarySurface::flipToGdiSurface(); } - template - HRESULT STDMETHODCALLTYPE DirectDraw::GetDisplayMode( - TDirectDraw* This, TSurfaceDesc* lpDDSurfaceDesc) - { - HRESULT result = s_origVtable.GetDisplayMode(This, lpDDSurfaceDesc); - if (SUCCEEDED(result) && lpDDSurfaceDesc) - { - lpDDSurfaceDesc->ddpfPixelFormat = getRgbPixelFormat(Win32::DisplayMode::getBpp()); - } - return result; - } - template HRESULT STDMETHODCALLTYPE DirectDraw::GetGDISurface( TDirectDraw* /*This*/, TSurface** lplpGDIDDSSurface) @@ -182,18 +148,6 @@ namespace DDraw return result; } - template - template - HRESULT STDMETHODCALLTYPE DirectDraw::SetDisplayMode( - TDirectDraw* This, - DWORD dwWidth, - DWORD dwHeight, - DWORD dwBPP, - Params... params) - { - return setDisplayMode(This, dwWidth, dwHeight, dwBPP, params...); - } - template HRESULT STDMETHODCALLTYPE DirectDraw::WaitForVerticalBlank( TDirectDraw* This, DWORD dwFlags, HANDLE hEvent) diff --git a/DDrawCompat/DDraw/DirectDraw.h b/DDrawCompat/DDraw/DirectDraw.h index 401cc70..98aa794 100644 --- a/DDrawCompat/DDraw/DirectDraw.h +++ b/DDrawCompat/DDraw/DirectDraw.h @@ -32,19 +32,9 @@ namespace DDraw IUnknown* pUnkOuter); static HRESULT STDMETHODCALLTYPE FlipToGDISurface(TDirectDraw* This); - static HRESULT STDMETHODCALLTYPE GetDisplayMode(TDirectDraw* This, TSurfaceDesc* lpDDSurfaceDesc); static HRESULT STDMETHODCALLTYPE GetGDISurface(TDirectDraw* This, TSurface** lplpGDIDDSSurface); static HRESULT STDMETHODCALLTYPE Initialize(TDirectDraw* This, GUID* lpGUID); static HRESULT STDMETHODCALLTYPE SetCooperativeLevel(TDirectDraw* This, HWND hWnd, DWORD dwFlags); - - template - static HRESULT STDMETHODCALLTYPE SetDisplayMode( - TDirectDraw* This, - DWORD dwWidth, - DWORD dwHeight, - DWORD dwBPP, - Params... params); - static HRESULT STDMETHODCALLTYPE WaitForVerticalBlank(TDirectDraw* This, DWORD dwFlags, HANDLE hEvent); }; } diff --git a/DDrawCompat/DDraw/Surfaces/Surface.cpp b/DDrawCompat/DDraw/Surfaces/Surface.cpp index d22a882..40ff7cf 100644 --- a/DDrawCompat/DDraw/Surfaces/Surface.cpp +++ b/DDrawCompat/DDraw/Surfaces/Surface.cpp @@ -13,18 +13,12 @@ DEFINE_GUID(IID_CompatSurfacePrivateData, namespace { - void fixSurfaceDesc(DWORD& flags, DWORD& caps, DDPIXELFORMAT& pf) + void fixSurfaceDesc(DWORD& flags, DWORD& caps) { if ((flags & DDSD_WIDTH) && (flags & DDSD_HEIGHT) && !(caps & (DDSCAPS_ALPHA | DDSCAPS_ZBUFFER))) { - if (!(flags & DDSD_PIXELFORMAT)) - { - flags |= DDSD_PIXELFORMAT; - pf = DDraw::getRgbPixelFormat(Win32::DisplayMode::getBpp()); - } - if (!(caps & (DDSCAPS_OFFSCREENPLAIN | DDSCAPS_OVERLAY | DDSCAPS_TEXTURE | DDSCAPS_FRONTBUFFER | DDSCAPS_BACKBUFFER))) { @@ -129,7 +123,7 @@ namespace DDraw template HRESULT Surface::create(CompatRef dd, TSurfaceDesc desc, TSurface*& surface) { - fixSurfaceDesc(desc.dwFlags, desc.ddsCaps.dwCaps, desc.ddpfPixelFormat); + fixSurfaceDesc(desc.dwFlags, desc.ddsCaps.dwCaps); HRESULT result = dd->CreateSurface(&dd, &desc, &surface, nullptr); if (SUCCEEDED(result)) diff --git a/DDrawCompat/Dll/DllMain.cpp b/DDrawCompat/Dll/DllMain.cpp index 151bb1f..d7cf496 100644 --- a/DDrawCompat/Dll/DllMain.cpp +++ b/DDrawCompat/Dll/DllMain.cpp @@ -41,7 +41,7 @@ namespace Compat::Log() << "Installing Direct3D driver hooks"; D3dDdi::installHooks(g_origDDrawModule); Compat::Log() << "Installing display mode hooks"; - Win32::DisplayMode::installHooks(g_origDDrawModule); + Win32::DisplayMode::installHooks(); Gdi::VirtualScreen::init(); CompatPtr dd; diff --git a/DDrawCompat/Gdi/VirtualScreen.cpp b/DDrawCompat/Gdi/VirtualScreen.cpp index 606b4d4..f1acd34 100644 --- a/DDrawCompat/Gdi/VirtualScreen.cpp +++ b/DDrawCompat/Gdi/VirtualScreen.cpp @@ -184,7 +184,8 @@ namespace Gdi static auto prevDisplaySettingsUniqueness = Win32::DisplayMode::queryDisplaySettingsUniqueness() - 1; const auto currentDisplaySettingsUniqueness = Win32::DisplayMode::queryDisplaySettingsUniqueness(); - if (currentDisplaySettingsUniqueness == prevDisplaySettingsUniqueness) + const auto bpp = Win32::DisplayMode::getBpp(); + if (currentDisplaySettingsUniqueness == prevDisplaySettingsUniqueness && bpp == g_bpp) { return LOG_RESULT(false); } @@ -195,7 +196,7 @@ namespace Gdi EnumDisplayMonitors(nullptr, nullptr, addMonitorRectToRegion, reinterpret_cast(&g_region)); GetRgnBox(g_region, &g_bounds); - g_bpp = Win32::DisplayMode::getBpp(); + g_bpp = bpp; g_width = g_bounds.right - g_bounds.left; g_height = g_bounds.bottom - g_bounds.top; g_pitch = (g_width * g_bpp / 8 + 3) & ~3; diff --git a/DDrawCompat/Win32/DisplayMode.cpp b/DDrawCompat/Win32/DisplayMode.cpp index c10b082..8c248db 100644 --- a/DDrawCompat/Win32/DisplayMode.cpp +++ b/DDrawCompat/Win32/DisplayMode.cpp @@ -6,6 +6,7 @@ #include "DDraw/DirectDraw.h" #include "DDraw/ScopedThreadLock.h" #include "Gdi/Gdi.h" +#include "Gdi/VirtualScreen.h" #include "Win32/DisplayMode.h" BOOL WINAPI DWM8And16Bit_IsShimApplied_CallOut() { return FALSE; }; @@ -32,7 +33,6 @@ namespace DWORD g_origBpp = 0; DWORD g_currentBpp = 0; DWORD g_lastBpp = 0; - DWORD g_ddrawBpp = 0; BOOL WINAPI enumDisplaySettingsExA( LPCSTR lpszDeviceName, DWORD iModeNum, DEVMODEA* lpDevMode, DWORD dwFlags); @@ -94,6 +94,8 @@ namespace SetEvent(dwmDxFullScreenTransitionEvent); CloseHandle(dwmDxFullScreenTransitionEvent); } + + Gdi::VirtualScreen::update(); } return result; @@ -119,85 +121,6 @@ namespace lpszDeviceName, lpDevMode, hwnd, dwflags, lParam)); } - template - LONG ddrawChangeDisplaySettingsEx( - ChangeDisplaySettingsExFunc changeDisplaySettingsEx, - CStr lpszDeviceName, DevMode* lpDevMode, HWND hwnd, DWORD dwflags, LPVOID lParam) - { - if (lpDevMode && 0 != lpDevMode->dmBitsPerPel) - { - lpDevMode->dmBitsPerPel = (0 != g_ddrawBpp) ? g_ddrawBpp : g_lastBpp; - } - return changeDisplaySettingsEx(lpszDeviceName, lpDevMode, hwnd, dwflags, lParam); - } - - LONG WINAPI ddrawChangeDisplaySettingsA( - DEVMODEA* lpDevMode, DWORD dwflags) - { - return ddrawChangeDisplaySettingsEx(&changeDisplaySettingsExA, - nullptr, lpDevMode, nullptr, dwflags, nullptr); - } - - LONG WINAPI ddrawChangeDisplaySettingsW( - DEVMODEW* lpDevMode, DWORD dwflags) - { - return ddrawChangeDisplaySettingsEx(&changeDisplaySettingsExW, - nullptr, lpDevMode, nullptr, dwflags, nullptr); - } - - LONG WINAPI ddrawChangeDisplaySettingsExA( - LPCSTR lpszDeviceName, DEVMODEA* lpDevMode, HWND hwnd, DWORD dwflags, LPVOID lParam) - { - return ddrawChangeDisplaySettingsEx(&changeDisplaySettingsExA, - lpszDeviceName, lpDevMode, hwnd, dwflags, lParam); - } - - LONG WINAPI ddrawChangeDisplaySettingsExW( - LPCWSTR lpszDeviceName, DEVMODEW* lpDevMode, HWND hwnd, DWORD dwflags, LPVOID lParam) - { - return ddrawChangeDisplaySettingsEx(&changeDisplaySettingsExW, - lpszDeviceName, lpDevMode, hwnd, dwflags, lParam); - } - - template - BOOL WINAPI ddrawEnumDisplaySettingsEx( - EnumDisplaySettingsExFunc origEnumDisplaySettingsEx, - EnumDisplaySettingsExFunc enumDisplaySettingsEx, - CStr lpszDeviceName, DWORD iModeNum, DevMode* lpDevMode, DWORD dwFlags) - { - if (ENUM_CURRENT_SETTINGS == iModeNum) - { - return origEnumDisplaySettingsEx(lpszDeviceName, iModeNum, lpDevMode, dwFlags); - } - return enumDisplaySettingsEx(lpszDeviceName, iModeNum, lpDevMode, dwFlags); - } - - BOOL WINAPI ddrawEnumDisplaySettingsA(LPCSTR lpszDeviceName, DWORD iModeNum, DEVMODEA* lpDevMode) - { - return ddrawEnumDisplaySettingsEx(CALL_ORIG_FUNC(EnumDisplaySettingsExA), &enumDisplaySettingsExA, - lpszDeviceName, iModeNum, lpDevMode, 0); - } - - BOOL WINAPI ddrawEnumDisplaySettingsW(LPCWSTR lpszDeviceName, DWORD iModeNum, DEVMODEW* lpDevMode) - { - return ddrawEnumDisplaySettingsEx(CALL_ORIG_FUNC(EnumDisplaySettingsExW), &enumDisplaySettingsExW, - lpszDeviceName, iModeNum, lpDevMode, 0); - } - - BOOL WINAPI ddrawEnumDisplaySettingsExA( - LPCSTR lpszDeviceName, DWORD iModeNum, DEVMODEA* lpDevMode, DWORD dwFlags) - { - return ddrawEnumDisplaySettingsEx(CALL_ORIG_FUNC(EnumDisplaySettingsExA), &enumDisplaySettingsExA, - lpszDeviceName, iModeNum, lpDevMode, dwFlags); - } - - BOOL WINAPI ddrawEnumDisplaySettingsExW( - LPCWSTR lpszDeviceName, DWORD iModeNum, DEVMODEW* lpDevMode, DWORD dwFlags) - { - return ddrawEnumDisplaySettingsEx(CALL_ORIG_FUNC(EnumDisplaySettingsExW), &enumDisplaySettingsExW, - lpszDeviceName, iModeNum, lpDevMode, dwFlags); - } - BOOL WINAPI dwm8And16BitIsShimAppliedCallOut() { return FALSE; @@ -304,17 +227,12 @@ namespace Win32 return ddQueryDisplaySettingsUniqueness(); } - void setDDrawBpp(DWORD bpp) - { - g_ddrawBpp = bpp; - } - void disableDwm8And16BitMitigation() { HOOK_FUNCTION(apphelp, DWM8And16Bit_IsShimApplied_CallOut, dwm8And16BitIsShimAppliedCallOut); } - void installHooks(HMODULE origDDrawModule) + void installHooks() { DEVMODEA devMode = {}; devMode.dmSize = sizeof(devMode); @@ -334,23 +252,6 @@ namespace Win32 HOOK_FUNCTION(user32, EnumDisplaySettingsExA, enumDisplaySettingsExA); HOOK_FUNCTION(user32, EnumDisplaySettingsExW, enumDisplaySettingsExW); HOOK_FUNCTION(gdi32, GetDeviceCaps, getDeviceCaps); - - Compat::hookIatFunction(origDDrawModule, "user32.dll", "ChangeDisplaySettingsA", - &ddrawChangeDisplaySettingsA); - Compat::hookIatFunction(origDDrawModule, "user32.dll", "ChangeDisplaySettingsW", - &ddrawChangeDisplaySettingsW); - Compat::hookIatFunction(origDDrawModule, "user32.dll", "ChangeDisplaySettingsExA", - &ddrawChangeDisplaySettingsExA); - Compat::hookIatFunction(origDDrawModule, "user32.dll", "ChangeDisplaySettingsExW", - &ddrawChangeDisplaySettingsExW); - Compat::hookIatFunction(origDDrawModule, "user32.dll", "EnumDisplaySettingsA", - &ddrawEnumDisplaySettingsA); - Compat::hookIatFunction(origDDrawModule, "user32.dll", "EnumDisplaySettingsW", - &ddrawEnumDisplaySettingsW); - Compat::hookIatFunction(origDDrawModule, "user32.dll", "EnumDisplaySettingsExA", - &ddrawEnumDisplaySettingsExA); - Compat::hookIatFunction(origDDrawModule, "user32.dll", "EnumDisplaySettingsExW", - &ddrawEnumDisplaySettingsExW); } } } diff --git a/DDrawCompat/Win32/DisplayMode.h b/DDrawCompat/Win32/DisplayMode.h index 14bef03..7fcb1ca 100644 --- a/DDrawCompat/Win32/DisplayMode.h +++ b/DDrawCompat/Win32/DisplayMode.h @@ -10,9 +10,8 @@ namespace Win32 { DWORD getBpp(); ULONG queryDisplaySettingsUniqueness(); - void setDDrawBpp(DWORD bpp); void disableDwm8And16BitMitigation(); - void installHooks(HMODULE origDDrawModule); + void installHooks(); } }