Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

fix(winecx): size D3DMetal back buffers in pixels under RetinaMode

raymondchins233353d3e664
diff --git a/app/Package.swift b/app/Package.swift
index a8a5c54..9f99a4d 100644
--- a/app/Package.swift
+++ b/app/Package.swift
@@ -43,6 +43,7 @@ let package = Package(
.copy("Resources/winemac-winehq-11.15.so"),
.copy("Resources/detour2-winecx-11.15.bin"),
.copy("Resources/detour32-winecx-11.15.bin"),
+ .copy("Resources/winemac-winecx-11.15.so"),
],
swiftSettings: [.swiftLanguageMode(.v6)]
),
diff --git a/app/Sources/NotProtonApp/Model/FreeEngine.swift b/app/Sources/NotProtonApp/Model/FreeEngine.swift
index 109fd09..4bca13e 100644
--- a/app/Sources/NotProtonApp/Model/FreeEngine.swift
+++ b/app/Sources/NotProtonApp/Model/FreeEngine.swift
@@ -202,10 +202,15 @@ enum FreeEngine {
// Upstream Wine hides the macdrv functions DXMT looks up, so without this DXMT can
// create a device but no swapchain, and games abort on their first window.
static func installMacDriver(root: URL, release: FreeEngineRelease) throws {
- guard let source = Bundle.module.url(forResource: "winemac-\(release.build)", withExtension: "so"),
- Digest.sha256IfPresent(source) == release.macDriver
+ try installMacDriver(root: root, build: release.build, sha256: release.macDriver, step: step)
+ }
+
+ // Puts the bundled winemac-<build>.so, checked against its pin, over the engine's own.
+ static func installMacDriver(root: URL, build: String, sha256: String, step: String) throws {
+ guard let source = Bundle.module.url(forResource: "winemac-\(build)", withExtension: "so"),
+ Digest.sha256IfPresent(source) == sha256
else {
- throw StepFailure(step: step, detail: "NotProton is missing its winemac.so for \(release.build).")
+ throw StepFailure(step: step, detail: "NotProton is missing its winemac.so for \(build).")
}
let destination = root.appending(path: macDriverPath)
try? FileManager.default.removeItem(at: destination)
diff --git a/app/Sources/NotProtonApp/Model/FreeEngineWinecx.swift b/app/Sources/NotProtonApp/Model/FreeEngineWinecx.swift
index ca82ffe..687483a 100644
--- a/app/Sources/NotProtonApp/Model/FreeEngineWinecx.swift
+++ b/app/Sources/NotProtonApp/Model/FreeEngineWinecx.swift
@@ -34,6 +34,10 @@ struct WinecxRelease: Sendable {
// lib/wine/x86_64-windows/<name> -> sha256 of Wine's own copy, for the four DLLs a linked
// Game Porting Toolkit replaces (WinecxD3DMetal.forwarders).
let d3dStock: [String: String]
+ // sha256 of the winemac.so NotProton ships for this build: winecx's own with
+ // winemac-patch/winecx-d3dmetal-retina.patch, so D3DMetal sizes a Retina-mode
+ // swapchain in Win32 pixels rather than points.
+ let macDriver: String
}
enum FreeEngineWinecx {
@@ -68,14 +72,15 @@ enum FreeEngineWinecx {
sha256: "f79d417f675d00008375eb4ea2318af6fcd0b27971d604d3cf345a0dcfa998a0"),
],
// winecx's winemac.so already exports the macdrv_functions DXMT reads, and that
- // table is unchanged between 0.80 and master, so no winemac-patch is needed.
+ // table is unchanged between 0.80 and master, so DXMT needs no winemac-patch.
renderer: FreeEngine.dxmt,
d3dStock: [
"d3d10.dll": "bcbd3fb8a59a5992bd3035babc5c046327f19cdc0669ce9e7c438c3ce3a9546d",
"d3d11.dll": "84f5a337fdd7405bbbee5f0542427842a22aff749a3d061ba6334f10cf01dee4",
"d3d12.dll": "c6e285308bb3241efca3d94da4746b16a526a9fb5113f61922e7bd86694bebf9",
"dxgi.dll": "f1f24da777c65a583b29f74475c6a9464da35068d75def6cee806268def9ecbb",
- ]
+ ],
+ macDriver: "c611031855e596b2aeb2d2ea2c2ddef8cb843376d85ccf8bbe10c4fc727a527e"
)
// Appended to SupportedRunners.all. The hash-pinned loader is what makes it free, so a
@@ -169,7 +174,7 @@ enum FreeEngineWinecx {
// four Direct3D DLLs D3DMetal can replace are not in here: WinecxD3DMetal.layoutProblems
// checks them, since they are either Wine's own or links into a Game Porting Toolkit.
static func expectedFiles(_ release: WinecxRelease = current) -> [String: String] {
- var table: [String: String] = [:]
+ var table: [String: String] = [FreeEngine.macDriverPath: release.macDriver]
for file in release.backends { table["\(backendsPath)/\(file.destination)"] = file.sha256 }
for path in release.renderer.files.keys {
table[release.renderer.installPath(of: path)] = release.renderer.installedHash(of: path)
@@ -257,6 +262,7 @@ enum FreeEngineWinecx {
let root = SupportPaths.crossOverRoot(inBundle: shaped)
try fm.createDirectory(at: root.deletingLastPathComponent(), withIntermediateDirectories: true)
try fm.moveItem(at: wine, to: root)
+ try FreeEngine.installMacDriver(root: root, build: release.build, sha256: release.macDriver, step: step)
let libraries = wine.deletingLastPathComponent()
for file in release.backends {
diff --git a/app/Sources/NotProtonApp/Resources/winemac-winecx-11.15.so b/app/Sources/NotProtonApp/Resources/winemac-winecx-11.15.so
new file mode 100755
index 0000000..190c946
Binary files /dev/null and b/app/Sources/NotProtonApp/Resources/winemac-winecx-11.15.so differ
diff --git a/app/Tests/NotProtonAppTests/FreeEngineWinecxTests.swift b/app/Tests/NotProtonAppTests/FreeEngineWinecxTests.swift
index d405d57..0c8f603 100644
--- a/app/Tests/NotProtonAppTests/FreeEngineWinecxTests.swift
+++ b/app/Tests/NotProtonAppTests/FreeEngineWinecxTests.swift
@@ -79,6 +79,7 @@ struct FreeEngineWinecxTests {
#expect(table["lib/wine/\(arch)/winemetal.dll"] == FreeEngine.dxmt.files["\(arch)/winemetal.dll"])
}
#expect(table["lib/wine/x86_64-unix/winemetal.so"] == FreeEngine.dxmt.files["x86_64-unix/winemetal.so"])
+ #expect(table[FreeEngine.macDriverPath] == FreeEngineWinecx.current.macDriver)
#expect(Set(FreeEngineWinecx.current.d3dStock.keys) == Set(WinecxD3DMetal.forwarders))
#expect(FreeEngineWinecx.msyncMarker(in: URL(filePath: "/r")).path(percentEncoded: false)
== "/r/.notproton-msync")
@@ -86,6 +87,31 @@ struct FreeEngineWinecxTests {
== "/r/lib/notproton-backends/d3dmetal.enabled")
}
+ // winemac-patch/winecx-d3dmetal-retina.patch: an engine with winecx's own winemac.so is
+ // outdated, and the copy NotProton installs is the one it pins.
+ @Test("The patched winemac.so ships with the app and replaces winecx's own")
+ func patchedMacDriver() throws {
+ let release = FreeEngineWinecx.current
+ let bundled = try #require(Bundle.module.url(forResource: "winemac-\(release.build)", withExtension: "so"))
+ #expect(Digest.sha256IfPresent(bundled) == release.macDriver)
+
+ let root = try makeDirectory("macdriver")
+ defer { try? FileManager.default.removeItem(at: root) }
+ let driver = root.appending(path: FreeEngine.macDriverPath)
+ try FileManager.default.createDirectory(at: driver.deletingLastPathComponent(), withIntermediateDirectories: true)
+ try Data("winecx's own winemac.so".utf8).write(to: driver)
+ #expect(FreeEngineWinecx.layoutProblems(root: root).contains(FreeEngine.macDriverPath))
+
+ try FreeEngine.installMacDriver(root: root, build: release.build, sha256: release.macDriver,
+ step: FreeEngineWinecx.step)
+ #expect(Digest.sha256IfPresent(driver) == release.macDriver)
+ #expect(!FreeEngineWinecx.layoutProblems(root: root).contains(FreeEngine.macDriverPath))
+ #expect(throws: StepFailure.self) {
+ try FreeEngine.installMacDriver(root: root, build: release.build, sha256: String(repeating: "0", count: 64),
+ step: FreeEngineWinecx.step)
+ }
+ }
+
@Test("Each free engine is current, outdated or missing on its own pins")
func perEngineState() throws {
let winehqBuild = try #require(SupportedRunners.build(id: FreeEngine.current.build))
diff --git a/docs/WINECX.md b/docs/WINECX.md
index a57b36b..fab4d25 100644
--- a/docs/WINECX.md
+++ b/docs/WINECX.md
@@ -74,6 +74,13 @@ DXMT on this engine does **not** need `winemac-patch/`. winecx's `winemac.so` al
`_macdrv_functions` (`dlls/winemac.drv/d3dmetal.c`), and the table DXMT reads is unchanged
between 0.80 and master.
+The engine's `winemac.so` is still replaced, for D3DMetal: winecx's own let D3DMetal size a
+swapchain from its view's frame in points, so with High Resolution (Retina) on, a game got a
+half-size drawable and showed only the top-left quarter. NotProton installs
+`winemac-winecx-11.15.so`, built from winecx `5b45f45` with
+`winemac-patch/winecx-d3dmetal-retina.patch` (`winemac-patch/README.md`), pinned as
+`FreeEngineWinecx.current.macDriver`.
+
## D3DMetal: the builtin model
Apple's forwarders only work as builtins: only a builtin gets its unix half
diff --git a/winemac-patch/README.md b/winemac-patch/README.md
index 8ba4ace..bec3179 100644
--- a/winemac-patch/README.md
+++ b/winemac-patch/README.md
@@ -17,3 +17,38 @@ To rebuild `app/Sources/NotProtonApp/Resources/winemac-winehq-11.15.so`:
codesign -fs - dlls/winemac.drv/winemac.so
Then update `macDriver` in `FreeEngine.swift` with its sha256.
+
+## WineCX: `winecx-d3dmetal-retina.patch`
+
+D3DMetal (libd3dshared's `WineSwapchainCallbacks::InitializeForHWND`) sizes a window's
+backbuffer, and through it the `CAMetalLayer` drawable, from `-frame` of the view
+`macdrv_view_create_metal_view` returns. A frame is in points, while D3D sizes are Win32
+pixels, which are backing pixels in Retina mode, so a 1512x876-point window got a
+1512x876 drawable for a 3024x1752 game and showed only its top-left quarter. The patch
+hands D3DMetal a proxy view whose `-frame` is in Win32 pixels (`cgrect_win_from_mac`) and
+forwards everything else; DXMT never asks for `frame`.
+
+To rebuild `app/Sources/NotProtonApp/Resources/winemac-winecx-11.15.so` from winecx
+`5b45f45` (runtime 4.5.125), natively, with the CI's flags:
+
+ git -C winecx apply ../winemac-patch/winecx-d3dmetal-retina.patch
+ mkdir build && cd build
+ PATH="$(brew --prefix bison)/bin:$PATH" MACOSX_DEPLOYMENT_TARGET=26.0 ac_cv_func_pipe2=no \
+ ../winecx/configure --host=x86_64-apple-darwin24 --enable-archs=x86_64 --disable-tests \
+ --without-x --without-wayland --without-oss --without-alsa --without-pulse --without-sane \
+ --without-usb --without-v4l2 --without-pcap --without-capi --without-opencl --without-cups \
+ --without-freetype --without-gstreamer --without-ffmpeg \
+ CC="/usr/bin/clang -arch x86_64" CXX="/usr/bin/clang++ -arch x86_64" \
+ CFLAGS="-O2 -Wno-error=implicit-function-declaration"
+ make dlls/winemac.drv/<each object of winemac.so> # the .o list of its Makefile rule
+ # link as the Makefile rule does, against the runtime's own ntdll.so and win32u.so
+ clang -arch x86_64 -dynamiclib -install_name @rpath/winemac.so -Wl,-rpath,@loader_path/ \
+ -o winemac.so dlls/winemac.drv/*.o <runtime>/ntdll.so <runtime>/win32u.so \
+ -framework AppKit -framework Carbon -framework CoreVideo -framework Foundation \
+ -framework IOKit -framework Metal -framework OpenGL -framework QuartzCore -framework Security
+ codesign -fs - winemac.so
+
+Built this way without the patch, the result matches the runtime's `winemac.so` in every
+section (`__text`, `__cstring`, `__const`, `__data`, objc) and in its 2147 symbols; only
+`__LINKEDIT` and the SDK's framework versions differ. Then update `macDriver` in
+`FreeEngineWinecx.swift` with the sha256.
diff --git a/winemac-patch/winecx-d3dmetal-retina.patch b/winemac-patch/winecx-d3dmetal-retina.patch
new file mode 100644
index 0000000..3065590
--- /dev/null
+++ b/winemac-patch/winecx-d3dmetal-retina.patch
@@ -0,0 +1,151 @@
+diff --git a/dlls/winemac.drv/d3dmetal.c b/dlls/winemac.drv/d3dmetal.c
+index b5a03f6..a45aed5 100644
+--- a/dlls/winemac.drv/d3dmetal.c
++++ b/dlls/winemac.drv/d3dmetal.c
+@@ -186,22 +186,29 @@ static void my_macdrv_release_metal_device(macdrv_metal_device d)
+ macdrv_release_metal_device(d);
+ }
+
++/* D3DMetal sizes a swapchain from the frame of the view it gets here, which is in
++ * points, while D3D sizes are Win32 pixels. The wrapper reports the frame in Win32
++ * pixels (d3dmetal_objc.m), so Retina mode gets a full-size backbuffer. */
+ static macdrv_metal_view my_macdrv_view_create_metal_view(macdrv_view v, macdrv_metal_device d)
+ {
++ macdrv_metal_view view;
++
+ TRACE("macdrv_view_create_metal_view %p %p\n", v, d);
+- return macdrv_view_create_metal_view(v, d);
++ if (!(view = macdrv_view_create_metal_view(v, d))) return NULL;
++ return macdrv_d3dmetal_wrap_metal_view(view);
+ }
+
+ static macdrv_metal_layer my_macdrv_view_get_metal_layer(macdrv_metal_view v)
+ {
+ TRACE("macdrv_view_get_metal_layer %p\n", v);
+- return macdrv_view_get_metal_layer(v);
++ return macdrv_view_get_metal_layer(macdrv_d3dmetal_unwrap_metal_view(v));
+ }
+
+ static void my_macdrv_view_release_metal_view(macdrv_metal_view v)
+ {
+ TRACE("macdrv_view_release_metal_view %p\n", v);
+- return macdrv_view_release_metal_view(v);
++ macdrv_view_release_metal_view(macdrv_d3dmetal_unwrap_metal_view(v));
++ macdrv_d3dmetal_release_metal_view_wrapper(v);
+ }
+
+ static void my_OnMainThread(dispatch_block_t b)
+diff --git a/dlls/winemac.drv/d3dmetal_objc.m b/dlls/winemac.drv/d3dmetal_objc.m
+index 9b4a054..a470b79 100644
+--- a/dlls/winemac.drv/d3dmetal_objc.m
++++ b/dlls/winemac.drv/d3dmetal_objc.m
+@@ -72,4 +72,95 @@ @implementation WineMetalLayer
+
+ @end
+
++
++/* D3DMetal (libd3dshared's WineSwapchainCallbacks::InitializeForHWND) sizes a window's
++ * backbuffer, and through it the CAMetalLayer drawable, from -frame of the view
++ * macdrv_view_create_metal_view() returns. A frame is in points, but D3D sizes are Win32
++ * pixels, which are backing pixels in Retina mode, so the backbuffer came out at half the
++ * window's Win32 size and a game rendering for its client rect showed only the top-left
++ * quarter. D3DMetal gets this proxy instead: -frame in Win32 pixels, the same conversion
++ * every other path makes with cgrect_win_from_mac(), and everything else forwarded. */
++@interface WineD3DMetalViewProxy : NSProxy
++{
++ NSView* view;
++}
++
++ - (instancetype) initWithView:(NSView*)aView;
++ - (NSView*) wineView;
++
++@end
++
++@implementation WineD3DMetalViewProxy
++
++ - (instancetype) initWithView:(NSView*)aView
++ {
++ view = [aView retain];
++ return self;
++ }
++
++ - (void) dealloc
++ {
++ [view release];
++ [super dealloc];
++ }
++
++ - (NSView*) wineView
++ {
++ return view;
++ }
++
++ - (NSRect) frame
++ {
++ return cgrect_win_from_mac([view frame]);
++ }
++
++ - (id) forwardingTargetForSelector:(SEL)selector
++ {
++ return view;
++ }
++
++ - (NSMethodSignature*) methodSignatureForSelector:(SEL)selector
++ {
++ return [view methodSignatureForSelector:selector];
++ }
++
++ - (void) forwardInvocation:(NSInvocation*)invocation
++ {
++ [invocation invokeWithTarget:view];
++ }
++
++ - (BOOL) respondsToSelector:(SEL)selector
++ {
++ return [view respondsToSelector:selector];
++ }
++
++ - (BOOL) isKindOfClass:(Class)aClass
++ {
++ return [view isKindOfClass:aClass];
++ }
++
++@end
++
++macdrv_metal_view macdrv_d3dmetal_wrap_metal_view(macdrv_metal_view v)
++{
++ return (macdrv_metal_view)[[WineD3DMetalViewProxy alloc] initWithView:(NSView*)v];
++}
++
++macdrv_metal_view macdrv_d3dmetal_unwrap_metal_view(macdrv_metal_view v)
++{
++ id object = (id)v;
++
++ if (object && object_getClass(object) == [WineD3DMetalViewProxy class])
++ return (macdrv_metal_view)[(WineD3DMetalViewProxy*)object wineView];
++ return v;
++}
++
++void macdrv_d3dmetal_release_metal_view_wrapper(macdrv_metal_view v)
++{
++ id object = (id)v;
++
++ if (object && object_getClass(object) == [WineD3DMetalViewProxy class])
++ [object release];
++}
++
+ #endif
+diff --git a/dlls/winemac.drv/macdrv_cocoa.h b/dlls/winemac.drv/macdrv_cocoa.h
+index c6552ba..2551ab1 100644
+--- a/dlls/winemac.drv/macdrv_cocoa.h
++++ b/dlls/winemac.drv/macdrv_cocoa.h
+@@ -560,6 +560,9 @@ extern void macdrv_order_cocoa_window(macdrv_window w, macdrv_window prev,
+ extern macdrv_metal_view macdrv_view_create_metal_view(macdrv_view v, macdrv_metal_device d);
+ extern macdrv_metal_layer macdrv_view_get_metal_layer(macdrv_metal_view v);
+ extern void macdrv_view_release_metal_view(macdrv_metal_view v);
++extern macdrv_metal_view macdrv_d3dmetal_wrap_metal_view(macdrv_metal_view v);
++extern macdrv_metal_view macdrv_d3dmetal_unwrap_metal_view(macdrv_metal_view v);
++extern void macdrv_d3dmetal_release_metal_view_wrapper(macdrv_metal_view v);
+ extern macdrv_metal_swapchain macdrv_create_view_swapchain(macdrv_view v);
+ extern macdrv_metal_swapchain macdrv_create_offscreen_swapchain(void* hwnd, void* client_hwnd, CGRect container, CGRect frame);
+ extern void macdrv_swapchain_set_frame(macdrv_metal_swapchain swapchain, CGRect container, CGRect frame);