From aa3311c94e674abab563c6c851807f26f475ba44 Mon Sep 17 00:00:00 2001 From: Shitiz Garg Date: Sun, 8 Nov 2026 02:27:27 +1100 Subject: [PATCH 1/5] shell32: cache the base path of shell folders per (folder, flags) Same as pr/shell32-folder-path-cache (on since text13). Co-Authored-By: Claude Opus 5.5 Signed-off-by: Shitiz Garg --- dlls/shell32/shellpath.c | 52 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/dlls/shell32/shellpath.c b/dlls/shell32/shellpath.c index 6eb5d5cc3457..af3e89f5472d 100644 --- a/dlls/shell32/shellpath.c +++ b/dlls/shell32/shellpath.c @@ -2921,6 +2921,24 @@ HRESULT WINAPI SHGetFolderPathAndSubDirA( return hr; } +/* Madeira: PoE2 asks for Local AppData ~100x a second, and every answer is a chain of + * registry opens and reads (ProfileList, User Shell Folders, Shell Folders) through the + * wineserver. A shell folder's base path doesn't change during a session unless shell32 + * itself redirects or re-registers it, so keep the expanded base path per (folder, flags) + * for the calling user (hToken NULL). The subpath, existence check and create still run + * every call. */ +static SRWLOCK madeira_folder_lock = SRWLOCK_INIT; +static BYTE madeira_folder_valid[2][ARRAY_SIZE(CSIDL_Data)]; +static WCHAR madeira_folder_path[2][ARRAY_SIZE(CSIDL_Data)][MAX_PATH]; +static LONG madeira_folder_hits; + +static void madeira_folder_cache_clear(void) +{ + AcquireSRWLockExclusive(&madeira_folder_lock); + memset(madeira_folder_valid, 0, sizeof(madeira_folder_valid)); + ReleaseSRWLockExclusive(&madeira_folder_lock); +} + /************************************************************************* * SHGetFolderPathAndSubDirW [SHELL32.@] */ @@ -2937,6 +2955,7 @@ HRESULT WINAPI SHGetFolderPathAndSubDirW( DWORD folder = nFolder & CSIDL_FOLDER_MASK; CSIDL_Type type; int ret; + BOOL cacheable, cached = FALSE; TRACE("%p,%#x,%p,%#lx,%s,%p\n", hwndOwner, nFolder, hToken, dwFlags, debugstr_w(pszSubPath), pszPath); @@ -2952,6 +2971,26 @@ HRESULT WINAPI SHGetFolderPathAndSubDirW( return E_INVALIDARG; szTemp[0] = 0; type = CSIDL_Data[folder].type; + cacheable = !hToken && (type == CSIDL_Type_User || type == CSIDL_Type_AllUsers || + type == CSIDL_Type_ProgramData || type == CSIDL_Type_CurrVer); + if (cacheable) + { + AcquireSRWLockShared(&madeira_folder_lock); + if (madeira_folder_valid[dwFlags][folder]) + { + lstrcpyW(szBuildPath, madeira_folder_path[dwFlags][folder]); + cached = TRUE; + } + ReleaseSRWLockShared(&madeira_folder_lock); + } + if (cached) + { + LONG n = InterlockedIncrement(&madeira_folder_hits); + if (n == 1 || !(n & 16383)) + ERR("[folder-cache] #%ld csidl %#lx flags %lu served from cache: %s\n", n, folder, dwFlags, debugstr_w(szBuildPath)); + hr = S_OK; + goto have_path; + } switch (type) { case CSIDL_Type_Disallowed: @@ -2999,6 +3038,16 @@ HRESULT WINAPI SHGetFolderPathAndSubDirW( if (FAILED(hr)) goto end; + if (cacheable && hr == S_OK && szBuildPath[0]) + { + AcquireSRWLockExclusive(&madeira_folder_lock); + lstrcpyW(madeira_folder_path[dwFlags][folder], szBuildPath); + madeira_folder_valid[dwFlags][folder] = 1; + ReleaseSRWLockExclusive(&madeira_folder_lock); + } + +have_path: + if(pszSubPath) { /* make sure the new path does not exceed the buffer length * and remember to backslash and terminate it */ @@ -3098,6 +3147,7 @@ static HRESULT _SHRegisterFolders(HKEY hRootKey, HANDLE hToken, TRACE("%p,%p,%s,%p,%u\n", hRootKey, hToken, debugstr_w(szUserShellFolderPath), folders, foldersLen); + madeira_folder_cache_clear(); ret = RegCreateKeyW(hRootKey, szUserShellFolderPath, &hUserKey); if (ret) hr = HRESULT_FROM_WIN32(ret); @@ -3769,6 +3819,8 @@ static HRESULT redirect_known_folder( if(SUCCEEDED(hr)) hr = get_known_folder_redirection_place(rfid, &rootKey); + madeira_folder_cache_clear(); + /* write redirection information */ if(SUCCEEDED(hr)) hr = HRESULT_FROM_WIN32(RegCreateKeyExW(rootKey, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders", 0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL)); From 3b0f927285301e5b83fd98658a928a6fff4d88ea Mon Sep 17 00:00:00 2001 From: Shitiz Garg Date: Sun, 8 Nov 2026 01:59:13 +1100 Subject: [PATCH 2/5] win32u: opt-in snap of a top-level window that drops its frame at monitor size Path of Exile 2's borderless fullscreen creates a framed (WS_OVERLAPPEDWINDOW) window of its saved size plus the frame, then removes the frame with SWP_NOSIZE. The frameless window is all client, the game saves that client size, and the next launch adds the frame again: 1440x900 grew by 8x34 a launch to 1504x1156 on a 1440x900 desktop, which Madeira then showed shrunk with black bars. On Windows the framed window never gets past the monitor-sized maximum track size, so the size never creeps. With MADEIRA_SNAP_BORDERLESS=1, when a top-level window loses WS_CAPTION and WS_THICKFRAME while at least as large as the primary monitor in both dimensions (and not exactly its size), it is moved and sized to the monitor. Child windows, windows that keep a frame and smaller windows are untouched. Off by default. Tested on an iPhone: the window stayed at desktop size across launches and the bars were gone. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Shitiz Garg --- dlls/win32u/window.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/dlls/win32u/window.c b/dlls/win32u/window.c index b868454594e7..012d73ac8f68 100644 --- a/dlls/win32u/window.c +++ b/dlls/win32u/window.c @@ -1400,6 +1400,48 @@ static HWND set_window_owner( HWND hwnd, HWND owner ) } /* Helper function for SetWindowLong(). */ +/* madeira-duo (6 Nov 2026): a top-level window that loses its frame while at least as + * large as the monitor is snapped to the monitor. + * + * PoE2's borderless fullscreen creates a framed window of its saved size plus the frame, + * then removes the frame with SWP_NOSIZE. The frameless window is all client, the game + * saves that client size, and the next launch adds the frame again: 1440x900 grew 8x34 a + * launch to 1504x1156 on the 1440x900 desktop, which then showed shrunk with black bars. + * On Windows the framed window never got past the monitor-sized track limit. + * Opt-in: MADEIRA_SNAP_BORDERLESS=1 turns it on. */ +static void madeira_snap_borderless_window( HWND hwnd, DWORD old_style, DWORD new_style ) +{ + static int enabled = -1; + const DWORD frame_styles = WS_CAPTION | WS_THICKFRAME; + RECT window_rect, monitor_rect; + UINT dpi = get_thread_dpi(); + int window_width, window_height, monitor_width, monitor_height; + + if (enabled < 0) + { + const char *env = getenv( "MADEIRA_SNAP_BORDERLESS" ); + enabled = env && env[0] == '1' && !env[1]; + } + if (!enabled) return; + if (new_style & WS_CHILD) return; + if (!(old_style & frame_styles) || (new_style & frame_styles)) return; /* not a frame removal */ + if (NtUserGetAncestor( hwnd, GA_PARENT ) != get_desktop_window()) return; + if (!get_window_rect( hwnd, &window_rect, dpi )) return; + + monitor_rect = get_primary_monitor_rect( dpi ); + window_width = window_rect.right - window_rect.left; + window_height = window_rect.bottom - window_rect.top; + monitor_width = monitor_rect.right - monitor_rect.left; + monitor_height = monitor_rect.bottom - monitor_rect.top; + if (window_width < monitor_width || window_height < monitor_height) return; + if (window_width == monitor_width && window_height == monitor_height) return; + + TRACE( "snapping borderless %p from %s to monitor %s\n", hwnd, + wine_dbgstr_rect( &window_rect ), wine_dbgstr_rect( &monitor_rect )); + NtUserSetWindowPos( hwnd, 0, monitor_rect.left, monitor_rect.top, monitor_width, monitor_height, + SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER ); +} + LONG_PTR set_window_long( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOOL ansi ) { BOOL ok, made_visible = FALSE, layered = FALSE; @@ -1577,6 +1619,7 @@ LONG_PTR set_window_long( HWND hwnd, INT offset, UINT size, LONG_PTR newval, BOO user_driver->pSetWindowStyle( hwnd, offset, &style ); if (made_visible || layered) update_window_state( hwnd ); send_message( hwnd, WM_STYLECHANGED, offset, (LPARAM)&style ); + if (offset == GWL_STYLE) madeira_snap_borderless_window( hwnd, style.styleOld, style.styleNew ); } return retval; From 33433b51fd58e3e0cef03fe8177c2bb31c76fe1a Mon Sep 17 00:00:00 2001 From: Shitiz Garg Date: Sun, 8 Nov 2026 01:59:12 +1100 Subject: [PATCH 3/5] ntdll: RtlWakeAddress* skip the bucket spinlock when nobody waits in it Games release SRW locks and signal condition variables far more often than anything waits on them. Path of Exile 2 makes about a million RtlWakeAddressAll/Single calls a second against a few thousand waits, and every wake took its futex bucket's spinlock, a cache line bounced between all the game's threads for nothing. Each bucket now counts its queued waiters in the 4 bytes of padding after the lock, so struct futex_queue stays 24 bytes and keeps matching the unix side's shared table (struct ios_shared_futex_queue). A waiter increments the count under the lock before it compares the value, and decrements it when it leaves the queue (timeout, mismatch or wake). A waker stores, issues a full barrier and reads the count: zero means no waiter can have passed its compare, so it returns without the lock. Otherwise it takes the locked path as before. The unix side's ios_shared_futex_wake only alerts threads and never removes entries, so it needs no change; every PE ntdll build that queues on the shared table must carry this change (they are all built from this tree). Tested on an iPhone in every session since 4 Nov 2026 (Path of Exile 2, Steam): no hangs seen. The same waiter-count protocol, used by an x64 wake fast path we keep out of tree, passed a multi-threaded lost-wake stress test on the host (and failed it with the barrier removed). Co-Authored-By: Claude Opus 5.5 Signed-off-by: Shitiz Garg --- dlls/ntdll/sync.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/dlls/ntdll/sync.c b/dlls/ntdll/sync.c index b6e0c060fa35..2c93676c9d18 100644 --- a/dlls/ntdll/sync.c +++ b/dlls/ntdll/sync.c @@ -1287,6 +1287,12 @@ struct futex_queue { struct list queue; LONG lock; + /* Madeira wake fast path: number of entries currently queued in this + * bucket. It lives in the 4 bytes of padding after `lock`, so the struct + * stays 24 bytes and keeps matching the unix side's shared table + * (struct ios_shared_futex_queue, dlls/ntdll/unix/sync.c). Every ntdll + * copy that queues waiters on the shared table must maintain it. */ + LONG waiters; }; static struct futex_queue futex_queues[256]; @@ -1396,10 +1402,16 @@ NTSTATUS WINAPI RtlWaitOnAddress( const void *addr, const void *cmp, SIZE_T size spin_lock( &queue->lock ); + /* Announce the waiter before comparing. Paired with the barrier in + * futex_queue_has_waiters(): either this compare sees the waker's new + * value, or the waker sees waiters != 0 and takes the locked path. */ + InterlockedIncrement( &queue->waiters ); + /* Do the comparison inside of the spinlock, to reduce spurious wakeups. */ if (!compare_addr( addr, cmp, size )) { + InterlockedDecrement( &queue->waiters ); spin_unlock( &queue->lock ); return STATUS_SUCCESS; } @@ -1443,7 +1455,10 @@ NTSTATUS WINAPI RtlWaitOnAddress( const void *addr, const void *cmp, SIZE_T size { spin_lock( &queue->lock ); if (entry.addr) + { list_remove( &entry.entry ); + InterlockedDecrement( &queue->waiters ); + } spin_unlock( &queue->lock ); } @@ -1453,6 +1468,18 @@ NTSTATUS WINAPI RtlWaitOnAddress( const void *addr, const void *cmp, SIZE_T size return ret; } +/* Madeira wake fast path. Games release SRW locks and signal condition + * variables far more often than anyone waits on them (Path of Exile 2: about + * a million RtlWakeAddress* calls a second against a few thousand waits), and + * every one of those wakes took this bucket's spinlock: a shared cache line + * bounced between all the game's threads for nothing. A wake with no queued + * waiter in the bucket now returns after one full barrier and one load. */ +static inline BOOL futex_queue_has_waiters( struct futex_queue *queue ) +{ + MemoryBarrier(); /* order the caller's store to *addr before the load below */ + return ReadNoFence( &queue->waiters ) != 0; +} + /*********************************************************************** * RtlWakeAddressAll (NTDLL.@) */ @@ -1468,6 +1495,7 @@ void WINAPI RtlWakeAddressAll( const void *addr ) TRACE("%p\n", addr); if (!addr) return; + if (!futex_queue_has_waiters( queue )) return; spin_lock( &queue->lock ); @@ -1480,6 +1508,7 @@ void WINAPI RtlWakeAddressAll( const void *addr ) { entry->addr = NULL; list_remove( &entry->entry ); + InterlockedDecrement( &queue->waiters ); if (count == ARRAY_SIZE(tids)) { NtAlertMultipleThreadByThreadId( tids, count, NULL, NULL ); @@ -1511,6 +1540,7 @@ void WINAPI RtlWakeAddressSingle( const void *addr ) TRACE("%p\n", addr); if (!addr) return; + if (!futex_queue_has_waiters( queue )) return; spin_lock( &queue->lock ); @@ -1530,6 +1560,7 @@ void WINAPI RtlWakeAddressSingle( const void *addr ) * calls must wake at least two waiters if they exist. */ entry->addr = NULL; list_remove( &entry->entry ); + InterlockedDecrement( &queue->waiters ); break; } } From 078c295114c6d710b054b16dcfaebd47d82c1a23 Mon Sep 17 00:00:00 2001 From: Shitiz Garg Date: Sun, 8 Nov 2026 01:59:33 +1100 Subject: [PATCH 4/5] ntdll: MADEIRA_DXC_NATIVE=1 loads dxcompiler.dll from system32 D3D12 games ship Microsoft's x64 dxcompiler.dll next to their exe and compile shaders with it at run time, emulated. Path of Exile 2's compile threads then made 0.5-1.2M combase crossings a second. Madeira's iOS loader never replaces an app-folder image with a builtin, so WINEDLLOVERRIDES cannot swap it. With MADEIRA_DXC_NATIVE=1, a load of dxcompiler.dll by name or by full path searches system32 only, where the bundle's ARM64EC build of the same DXC release is linked (build/dxc-arm64ec in Madeira), like the existing madeira_force_builtin_dll names. Without the variable nothing changes. Needs the Madeira change that builds and bundles the ARM64EC dxcompiler.dll. Tested on an iPhone: the [dxc-native] line appears, the loaded image is the ARM64EC build, and the game's own dxcompiler.dll and dxil.dll are no longer loaded. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Shitiz Garg --- dlls/ntdll/loader.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c index 6c849accd4da..82d66a57fb8c 100644 --- a/dlls/ntdll/loader.c +++ b/dlls/ntdll/loader.c @@ -3990,6 +3990,35 @@ static BOOL madeira_force_builtin_dll( const WCHAR *name ) return FALSE; } +/*********************************************************************** + * madeira_dxc_native + * + * madeira-duo: with MADEIRA_DXC_NATIVE=1 in the environment, dxcompiler.dll + * is taken from system32, where the bundle's ARM64EC build of the same DXC + * release (arm64ec-windows/dxcompiler.dll, build/dxc-arm64ec) is linked, + * instead of the x64 copy a game ships next to its exe, which would run + * emulated. Like the XInput names above, the application directory is never + * searched for it then; a load by full path is treated as a load by name. + */ +static BOOL madeira_dxc_native( const WCHAR *name ) +{ + static int native = -1; + const WCHAR *base = name, *p; + + for (p = name; *p; p++) if (*p == '\\' || *p == '/') base = p + 1; + if (wcsicmp( base, L"dxcompiler.dll" ) && wcsicmp( base, L"dxcompiler" )) return FALSE; + if (native < 0) + { + UNICODE_STRING nm, val; + WCHAR buf[8]; + RtlInitUnicodeString( &nm, L"MADEIRA_DXC_NATIVE" ); + val.Buffer = buf; val.Length = 0; val.MaximumLength = sizeof(buf); + native = (!RtlQueryEnvironmentVariable_U( NULL, &nm, &val ) && val.Length && buf[0] == '1'); + if (native) ERR( "[dxc-native] dxcompiler.dll is loaded from system32 (MADEIRA_DXC_NATIVE=1)\n" ); + } + return native; +} + /*********************************************************************** * find_dll_file * @@ -4039,9 +4068,12 @@ static NTSTATUS find_dll_file( const WCHAR *load_path, const WCHAR *libname, UNI /* Win 7/2008R2 and up seem to re-enable WoW64 FS redirection when loading libraries */ RtlWow64EnableFsRedirectionEx( 0, &wow64_old_value ); + if (RtlDetermineDosPathNameType_U( libname ) != RtlPathTypeRelative && madeira_dxc_native( libname )) + libname = L"dxcompiler.dll"; + if (RtlDetermineDosPathNameType_U( libname ) == RtlPathTypeRelative) { - if (madeira_force_builtin_dll( libname )) + if (madeira_force_builtin_dll( libname ) || madeira_dxc_native( libname )) { TRACE( "%s: searching system32 only, for the builtin\n", debugstr_w(libname) ); load_path = system_dir; From 8a404df8193578749936e5e2fe7f20d9ebaea37b Mon Sep 17 00:00:00 2001 From: Shitiz Garg Date: Sun, 22 Nov 2026 17:05:59 +1100 Subject: [PATCH 5/5] ntdll: MADEIRA_XESS_METALFX=1 loads libxess.dll from system32 With MADEIRA_XESS_METALFX=1, libxess.dll resolves to Madeira's builtin in system32 (from the bundle's arm64ec-windows), which answers XeSS with MetalFX temporal upscaling, instead of the game's own Intel DLL. The iOS loader never replaces an image from the application folder with a builtin, so WINEDLLOVERRIDES cannot do it: Path of Exile 2 loaded its 74 MB x64 libxess with libxess=b, and with libxess= too. Path of Exile 2 loads it by full path, so a full path to libxess.dll is matched by its base name. Unset, nothing changes. Madeira's side is its xess-metalfx commit. Signed-off-by: Shitiz Garg Co-Authored-By: Claude Opus 5.5 --- dlls/ntdll/loader.c | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c index 82d66a57fb8c..4d603fd5e980 100644 --- a/dlls/ntdll/loader.c +++ b/dlls/ntdll/loader.c @@ -4019,6 +4019,35 @@ static BOOL madeira_dxc_native( const WCHAR *name ) return native; } +/*********************************************************************** + * madeira_xess_builtin + * + * madeira-duo xess-metalfx: with MADEIRA_XESS_METALFX=1, libxess.dll is + * Madeira's (system32, from the bundle's arm64ec-windows), which answers XeSS + * with MetalFX temporal upscaling, instead of the game's own Intel DLL: the iOS + * loader never replaces an app-folder image with a builtin, so + * WINEDLLOVERRIDES cannot do it (PoE2 loaded its 74 MB x64 libxess with + * libxess=b, and with libxess= too). + */ +static BOOL madeira_xess_builtin( const WCHAR *name ) +{ + static int on = -1; + const WCHAR *base = name, *p; + + for (p = name; *p; p++) if (*p == '\\' || *p == '/') base = p + 1; + if (wcsicmp( base, L"libxess.dll" ) && wcsicmp( base, L"libxess" )) return FALSE; + if (on < 0) + { + UNICODE_STRING nm, val; + WCHAR buf[8]; + RtlInitUnicodeString( &nm, L"MADEIRA_XESS_METALFX" ); + val.Buffer = buf; val.Length = 0; val.MaximumLength = sizeof(buf); + on = (!RtlQueryEnvironmentVariable_U( NULL, &nm, &val ) && val.Length && buf[0] == '1'); + if (on) ERR( "[xess-metalfx] libxess.dll is loaded from system32 (MADEIRA_XESS_METALFX=1)\n" ); + } + return on; +} + /*********************************************************************** * find_dll_file * @@ -4070,10 +4099,13 @@ static NTSTATUS find_dll_file( const WCHAR *load_path, const WCHAR *libname, UNI if (RtlDetermineDosPathNameType_U( libname ) != RtlPathTypeRelative && madeira_dxc_native( libname )) libname = L"dxcompiler.dll"; + /* PoE2 loads \libxess.dll by its full path */ + if (RtlDetermineDosPathNameType_U( libname ) != RtlPathTypeRelative && madeira_xess_builtin( libname )) + libname = L"libxess.dll"; if (RtlDetermineDosPathNameType_U( libname ) == RtlPathTypeRelative) { - if (madeira_force_builtin_dll( libname ) || madeira_dxc_native( libname )) + if (madeira_force_builtin_dll( libname ) || madeira_dxc_native( libname ) || madeira_xess_builtin( libname )) { TRACE( "%s: searching system32 only, for the builtin\n", debugstr_w(libname) ); load_path = system_dir;