Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run natively on Apple Silicon #32

Closed
diericx opened this issue Jul 22, 2023 · 18 comments
Closed

Run natively on Apple Silicon #32

diericx opened this issue Jul 22, 2023 · 18 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@diericx
Copy link

diericx commented Jul 22, 2023

I am super curious to see if we can get Valheim running natively. It looks like it would require getting Unity and the Steamworks.Net and PlayFabParty plugins running natively.

Unity is as simple as changing the variant variable in build.sh but I think it is better to start with Steamworks.

As of 1.52 Steamworks SDK supports arm. The latest SDK (1.57) ships with an ARM compatible steam_api.bundle.

$ file ./Steamworks.NET-Standalone_20.2.0/OSX-Linux-x64/steam_api.bundle/Contents/MacOS/libsteam_api.dylib
./Steamworks.NET-Standalone_20.2.0/OSX-Linux-x64/steam_api.bundle/Contents/MacOS/libsteam_api.dylib: Mach-O universal binary with 3 architectures: [i386:Mach-O dynamically linked shared library i386] [x86_64:Mach-O 64-bit dynamically linked shared library x86_64] [arm64:Mach-O 64-bit dynamically linked shared library arm64]
./Steamworks.NET-Standalone_20.2.0/OSX-Linux-x64/steam_api.bundle/Contents/MacOS/libsteam_api.dylib (for architecture i386):	Mach-O dynamically linked shared library i386
./Steamworks.NET-Standalone_20.2.0/OSX-Linux-x64/steam_api.bundle/Contents/MacOS/libsteam_api.dylib (for architecture x86_64):	Mach-O 64-bit dynamically linked shared library x86_64
./Steamworks.NET-Standalone_20.2.0/OSX-Linux-x64/steam_api.bundle/Contents/MacOS/libsteam_api.dylib (for architecture arm64):	Mach-O 64-bit dynamically linked shared library arm64

It seems like a stretch but would be super cool if we could just swap the new build in.

As this dll is compiled for both intel and amd it seems like a great first step as we can get it running without leaving Rosetta 2. I have tried swapping it into the artifact from build.sh without changing anything else. I am effectively doing this below.

$ ./build.sh
$ rm ./build/Valheim.app/Contents/PlugIns/steam_api.bundle
$ cp ~/Downloads/Steamworks.NET-Standalone_20.2.0/OSX-Linux-x64/steam_api.bundle ./build/Valheim.app/Contents/PlugIns

And then updating ./build/Valheim.app/Contents/PlugIns/Steamworks.NET.txt to the following grabbing values from here
https://github.com/rlabrecque/Steamworks.NET/blob/a3fe8091f7237eef3f38becc3faa29b2dc6f9ce9/com.rlabrecque.steamworks.net/Runtime/Version.cs#L16

Steamworks.NET created by Riley Labrecque
http://steamworks.github.io

Steamworks.NET Version: 20.2.0
Steamworks SDK Version: 1.57
Steam API DLL Version:  08.02.21.95
Steam API DLL Size:     266600
Steam API64 DLL Size:   298856

This crashes very early with a segfault... which is strange to me.

https://gist.github.com/diericx/d704b0a478fda2121764d037c8e63a50

I am wondering if anyone wants to work with me on this as I'm pretty stumped.

@nonoche2
Copy link

I think it's better to make sure that everything is compiled as a universal binary, did you try with an Arm-compatible Unity player along ?

@timkurvers timkurvers added enhancement New feature or request help wanted Extra attention is needed labels Jul 22, 2023
@diericx
Copy link
Author

diericx commented Jul 22, 2023

@nonoche2 Interesting idea.

-variant="macos_x64_nondevelopment_mono"
+variant="macos_x64arm64_nondevelopment_mono"

This change produces a build that crashes for me in the same way that changing it to macos_arm64_nondevelopment_mono does.

@nonoche2
Copy link

do you still get a mention of libRosettaRuntime in the crash report?

if so, can you check what you get with the File terminal command on every dylib and binary inside the app bundle?

@timkurvers
Copy link
Owner

timkurvers commented Jul 22, 2023

I encountered the same segfault during the initial phases of the custom build, and couldn't really figure out how to get it to play nicely. Thanks for opening the issue, would be nice to get this to work 🤩

What's interesting is that it completely crashes Terminal.app as well when invoked using ./Valheim.app/Contents/MacOS/Valheim -logfile /dev/stdout.

@diericx
Copy link
Author

diericx commented Jul 22, 2023

I am not getting a crash report with this, here is what I could capture via Console

default	18:31:22.583681+0200	lsd	unity.IronGate.Valheim-macOS-Custom/Unknown Persona:5:5:1:1:Building bundle record for app
default	18:31:22.583853+0200	lsd	unity.IronGate.Valheim-macOS-Custom/Unknown Persona:5:5:2:1:Built bundle record for app
default	18:31:22.590286+0200	lsd	unity.IronGate.Valheim-macOS-Custom/Unknown Persona:5:4:2:1:_LSServerRegisterItemInfo result = 0
default	18:31:22.595388+0200	runningboardd	Launch request for app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>[0] is using uid 501 (divined from auid 501 euid 501)
default	18:31:22.595442+0200	runningboardd	Acquiring assertion targeting app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)> from originator [osservice<com.apple.Finder(501)>:520] with description <RBSAssertionDescriptor| "LS launch unity.IronGate.Valheim-macOS-Custom" ID:251-520-173808 target:app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)> attributes:[
	<RBSDomainAttribute| domain:"com.apple.launchservicesd" name:"LaunchRoleUserInteractive" sourceEnvironment:"(null)">
	]>
default	18:31:22.595491+0200	runningboardd	Assertion 251-520-173808 (target:app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>) will be created as active
default	18:31:22.595884+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring jetsam update because this process is not memory-managed
default	18:31:22.595938+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring suspend because this process is not lifecycle managed
default	18:31:22.595896+0200	runningboardd	Calculated state for app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>: running-active (role: UserInteractive)
default	18:31:22.596141+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Set darwin role to: UserInteractive
default	18:31:22.596266+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring GPU update because this process is not GPU managed
default	18:31:22.596375+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring memory limit update because this process is not memory-managed
default	18:31:22.597095+0200	runningboardd	Executing launch request for app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)> (LS launch unity.IronGate.Valheim-macOS-Custom)
default	18:31:22.597259+0200	runningboardd	Checking PreventLaunch: global:0 exPath:/Users/*/code/timkurvers/valheim-macos/build/Valheim.app/Contents/MacOS/Valheim predicates:(null) allow:(null)
default	18:31:22.598433+0200	gamepolicyd	Received state update for 9361 (app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>, running-active-NotVisible
default	18:31:22.599674+0200	runningboardd	Acquiring assertion targeting [app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] from originator [osservice<com.apple.coreservices.launchservicesd>:152] with description <RBSAssertionDescriptor| "foregroundApp:9361" ID:251-152-173809 target:9361 attributes:[
	<RBSDomainAttribute| domain:"com.apple.launchservicesd" name:"RoleUserInteractiveNonFocal" sourceEnvironment:"(null)">
	]>
default	18:31:22.599721+0200	runningboardd	Assertion 251-152-173809 (target:[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361]) will be created as active
default	18:31:22.600286+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring jetsam update because this process is not memory-managed
default	18:31:22.600303+0200	runningboardd	Calculated state for app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>: running-active (role: UserInteractive)
default	18:31:22.600420+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring suspend because this process is not lifecycle managed
default	18:31:22.600539+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring GPU update because this process is not GPU managed
default	18:31:22.600666+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring memory limit update because this process is not memory-managed
default	18:31:22.603105+0200	distnoted	register name: com.apple.sharedfilelist.change object: com.apple.LSSharedFileList.ApplicationRecentDocuments/unity.irongate.valheim-macos-custom token: 1c500000055 pid: 512
default	18:31:22.604365+0200	loginwindow	-[PersistentAppsSupport applicationReady:] | App: Valheim.app, ready, updating active tracking timer
default	18:31:22.604409+0200	loginwindow	-[ApplicationManager checkInAppContext:eventData:] | ApplicationManager: Checked in app : Valheim.app
default	18:31:22.604922+0200	Finder	LAUNCH: 0x0-0x218d18b unity.IronGate.Valheim-macOS-Custom starting stopped process.
default	18:31:22.606121+0200	runningboardd	Invalidating assertion 251-520-173808 (target:app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>) from originator [osservice<com.apple.Finder(501)>:520]
default	18:31:22.601830+0200	gamepolicyd	Received state update for 9361 (app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>, running-active-NotVisible
default	18:31:22.655235+0200	dmd	Received xpc stream event (distributed notification matching) with name: com.apple.LaunchServices.applicationRegistered user info: {
    bundleIDs =     (
        "unity.IronGate.Valheim-macOS-Custom"
    );
    isPlaceholder = 0;
}
default	18:31:22.713329+0200	runningboardd	Calculated state for app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>: running-active (role: UserInteractiveNonFocal)
default	18:31:22.713361+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring jetsam update because this process is not memory-managed
default	18:31:22.713460+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring suspend because this process is not lifecycle managed
default	18:31:22.713824+0200	gamepolicyd	Received state update for 9361 (app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>, running-active-NotVisible
default	18:31:22.713629+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Set darwin role to: UserInteractiveNonFocal
default	18:31:22.713717+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring GPU update because this process is not GPU managed
default	18:31:22.713928+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring memory limit update because this process is not memory-managed
default	18:31:36.287652+0200	runningboardd	Invalidating assertion 251-177-173515 (target:[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361]) from originator [osservice<com.apple.WindowServer(88)>:177]
default	18:31:36.409358+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring jetsam update because this process is not memory-managed
default	18:31:36.409420+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring suspend because this process is not lifecycle managed
default	18:31:36.409450+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring GPU update because this process is not GPU managed
default	18:31:36.409557+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] Ignoring memory limit update because this process is not memory-managed
default	18:31:36.409677+0200	runningboardd	Calculated state for app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>: running-active (role: UserInteractiveNonFocal)
default	18:31:36.410844+0200	gamepolicyd	Received state update for 9361 (app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>, running-active-NotVisible
default	18:31:41.982909+0200	runningboardd	[app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361] termination reported by launchd (0, 0, 9)
default	18:31:41.982939+0200	runningboardd	Removing process: [app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361]
default	18:31:41.976463+0200	WindowManager	Connection invalidated | (9361) Valheim
default	18:31:41.983201+0200	runningboardd	Removing launch job for: [app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361]
default	18:31:41.983455+0200	runningboardd	Removed job for [app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361]
default	18:31:41.983539+0200	runningboardd	Removing assertions for terminated process: [app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361]
default	18:31:41.983958+0200	audiomxd	  ServerSessionManager.mm:358   { "action":"destroy_session", "session":{"ID":"0x17a7b00a","name":"Valheim(9361)"}, "details":null }
default	18:31:41.984008+0200	audiomxd	  ServerSessionManager.mm:758   destroy audio app instance since was created implicitly by session creation and all sessions now gone (audio app: {"name":"[implicit] Valheim","pid":9361})
default	18:31:41.984091+0200	audiomxd	-MXCoreSession- -[MXCoreSession updateIsPlaying:]: MXCoreSession unity.IronGate.Valheim-macOS-Custom with category/mode MediaPlayback/Default and coreSessionID = 11 stopping playing
default	18:31:41.984638+0200	audiomxd	-MXCoreSession- -[MXCoreSession endInterruption:]: Session <ID: 11, PID = 9361, Name = Valheim, BundleID = unity.IronGate.Valheim-macOS-Custom, Category = MediaPlayback, Mode = Default, Active = NO, Playing = NO, Recording = NO> is going inactive
default	18:31:41.984822+0200	audiomxd	-MXSessionManager- -[MXSessionManager requestForSharedOwnership:didNowPlayingInfoChange:didCategoryOrModeChange:]: Session <ID: 11, PID = 9361, Name = Valheim, BundleID = unity.IronGate.Valheim-macOS-Custom, Category = MediaPlayback, Mode = Default, Active = NO, Playing = NO, Recording = NO> requesting for shared ownership, didNowPlayingInfoChanged = NO, didCategoryOrModeChange = NO
default	18:31:41.985014+0200	audiomxd	-MXSessionManager- -[MXSessionManager requestForSharedOwnership:didNowPlayingInfoChange:didCategoryOrModeChange:]: Setting score as 201 for session <ID: 11, PID = 9361, Name = Valheim, BundleID = unity.IronGate.Valheim-macOS-Custom, Category = MediaPlayback, Mode = Default, Active = NO, Playing = NO, Recording = NO>
default	18:31:41.985630+0200	audiomxd	UpdateAudioState CID 0x6A01002C audioState Stop apps {
    "unity.IronGate.Valheim-macOS-Custom" : 201,
}
default	18:31:41.986777+0200	audiomxd	UpdateAudioState CID 0x6A01002D audioState Stop apps {
    "unity.IronGate.Valheim-macOS-Custom" : 201,
}
default	18:31:41.987097+0200	audioaccessoryd	Audio state update Stop apps {
    "unity.IronGate.Valheim-macOS-Custom" : 201,
}
default	18:31:41.986336+0200	powerd	Process Valheim.9361 ClientDied PreventUserIdleDisplaySleep "disable screen saver" age:00:00:40  id:21474876348 [System: PrevIdle DeclUser IntPrevDisp kDisp]
default	18:31:41.987627+0200	audioaccessoryd	Audio state update Stop apps {
    "unity.IronGate.Valheim-macOS-Custom" : 201,
}
default	18:31:41.994476+0200	audioaccessoryd	AudioStateChanged: Received audioState Stop apps {
    "unity.IronGate.Valheim-macOS-Custom" : 201,
}
default	18:31:41.994515+0200	audioaccessoryd	AudioStateChanged: Removed audio session app unity.IronGate.Valheim-macOS-Custom 201 count 0
default	18:31:41.994832+0200	audioaccessoryd	AudioStateChanged: Received audioState Stop apps {
    "unity.IronGate.Valheim-macOS-Custom" : 201,
}
default	18:31:41.993977+0200	runningboardd	XPC connection invalidated: [app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361]
default	18:31:41.995763+0200	runningboardd	Calculated state for app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>: none (role: None)
default	18:31:41.996242+0200	runningboardd	Calculated state for app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>: none (role: None)
default	18:31:41.997879+0200	launchservicesd	Hit the server for a process handle ca8c97000002491 that resolved to: [app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>:9361]
default	18:31:42.001156+0200	loginwindow	-[PersistentAppsSupport applicationQuit:] | for app:Valheim.app, _appTrackingState = 2
default	18:31:42.001174+0200	loginwindow	-[PersistentAppsSupport applicationQuit:] | App: Valheim.app, quit, updating active tracking timer
default	18:31:42.006453+0200	gamepolicyd	Received state update for 9361 (app<application.unity.IronGate.Valheim-macOS-Custom.38995945.38995959(501)>, none-NotVisible

@timkurvers
Copy link
Owner

The backtrace I get from lldb (using variant macos_arm64_development_mono):

>>> bt
* thread #1, name = 'tid_103', queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x00000001053457f8 UnityPlayer.dylib`Material::SetPassSlow(int, ShaderPassContext&, int, bool) + 100
    frame #1: 0x000000010502c4dc UnityPlayer.dylib`DrawQuad(RectT<float> const&, float, Texture*, ColorRGBAf const&, RectT<float> const&) + 128
    frame #2: 0x000000010501c95c UnityPlayer.dylib`DrawSplashScreen(bool) + 2032
    frame #3: 0x0000000105eccb5c UnityPlayer.dylib`-[PlayerAppDelegate UpdatePlayer] + 232
    frame #4: 0x0000000105ecf184 UnityPlayer.dylib`-[PlayerWindowView drawRect:] + 60
    frame #5: 0x0000000195e5fc18 AppKit`_NSViewDrawRect + 124
    frame #6: 0x00000001965106c0 AppKit`-[NSView _recursive:displayRectIgnoringOpacity:inContext:stopAtLayerBackedViews:] + 1088
    frame #7: 0x0000000195e5f48c AppKit`-[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] + 556
    ...

@diericx
Copy link
Author

diericx commented Jul 22, 2023

@timkurvers I am able to successfully run that via iTerm on Sonoma Beta 3 and get this in the log file (to confirm, only change being the one posted above switching to macos_x64arm64)

Mono path[0] = '/Users/*/code/timkurvers/valheim-macos/build/Valheim.app/Contents/Resources/Data/Managed'
Mono config path = '/Users/*/code/timkurvers/valheim-macos/build/Valheim.app/Contents/MonoBleedingEdge/etc'
New input system (experimental) initialized
Initialize engine version: 2020.3.45f1 (660cd1701bd5)
[Subsystems] Discovering subsystems at path /Users/*/code/timkurvers/valheim-macos/build/Valheim.app/Contents/Resources/Data/UnitySubsystems
GfxDevice: creating device client; threaded=1
NullGfxDevice:
    Version:  NULL 1.0 [1.0]
    Renderer: Null Device
    Vendor:   Unity Technologies
Begin MonoManager ReloadAssembly
- Completed reload, in  0.040 seconds
ERROR: Shader Sprites/Default shader is not supported on this GPU (none of subshaders/fallbacks are suitable)
ERROR: Shader Sprites/Mask shader is not supported on this GPU (none of subshaders/fallbacks are suitable)
Stacktrace:


Native stacktrace:

	0   libmonobdwgc-2.0.dylib              0x000000011d6e250c mono_unity_backtrace_from_context + 360
	1   libmonobdwgc-2.0.dylib              0x000000011d6615b0 mono_jit_set_domain + 8980
	2   libsystem_platform.dylib            0x000000018b949a24 _sigtramp + 56
	3   UnityPlayer.dylib                   0x0000000107fdac00 _ZdaPvRKSt9nothrow_t + 2546440
	4   UnityPlayer.dylib                   0x0000000107e6a91c _ZdaPvRKSt9nothrow_t + 1038372
	5   UnityPlayer.dylib                   0x0000000107e5bd64 _ZdaPvRKSt9nothrow_t + 978028
	6   UnityPlayer.dylib                   0x00000001086d1298 _ZN9MetalHeap14AliasResourcesEv + 113872
	7   UnityPlayer.dylib                   0x00000001086d384c _ZN9MetalHeap14AliasResourcesEv + 123524
	8   AppKit                              0x000000018f25ce8c _NSViewDrawRect + 124
	9   AppKit                              0x000000018fbafec4 -[NSView _recursive:displayRectIgnoringOpacity:inContext:stopAtLayerBackedViews:] + 1088
	10  AppKit                              0x000000018f25c700 -[NSView(NSLayerKitGlue) _drawViewBackingLayer:inContext:drawingHandler:] + 556
	11  AppKit                              0x000000018f75baf0 -[NSViewBackingLayer drawInContext:] + 56
	12  AppKit                              0x000000018f934bb0 -[NSViewBackingStore update:] + 344
	13  AppKit                              0x000000018f75aeac -[NSViewBackingLayerContents update:] + 52
	14  AppKit                              0x000000018f75b744 -[NSViewBackingLayer display] + 952
	15  QuartzCore                          0x0000000193982f8c _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 736
	16  QuartzCore                          0x0000000193b07038 _ZN2CA7Context18commit_transactionEPNS_11TransactionEdPd + 508
	17  QuartzCore                          0x0000000193965ed4 _ZN2CA11Transaction6commitEv + 648
	18  AppKit                              0x000000018f26c160 __62+[CATransaction(NSCATransaction) NS_setFlushesWithDisplayLink]_block_invoke + 272
	19  AppKit                              0x000000018fbec8d4 ___NSRunLoopObserverCreateWithHandler_block_invoke + 64
	20  CoreFoundation                      0x000000018b9f827c __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36
	21  CoreFoundation                      0x000000018b9f8168 __CFRunLoopDoObservers + 532
	22  CoreFoundation                      0x000000018b9f7798 __CFRunLoopRun + 776
	23  CoreFoundation                      0x000000018b9f6ddc CFRunLoopRunSpecific + 600
	24  HIToolbox                           0x0000000195f1b530 RunCurrentEventLoopInMode + 292
	25  HIToolbox                           0x0000000195f1b1c0 ReceiveNextEventCommon + 220
	26  HIToolbox                           0x0000000195f1b0c4 _BlockUntilNextEventMatchingListInModeWithFilter + 76
	27  AppKit                              0x000000018f1434a8 _DPSNextEvent + 660
	28  AppKit                              0x000000018f8fd8a0 -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 716
	29  AppKit                              0x000000018f136cb0 -[NSApplication run] + 476
	30  AppKit                              0x000000018f10e104 NSApplicationMain + 880
	31  UnityPlayer.dylib                   0x00000001086d87fc _Z10PlayerMainiPPKc + 848
	32  dyld                                0x000000018b5a1058 start + 2224

Debug info from gdb:

(lldb) command source -s 0 '/tmp/mono-gdb-commands.mNhP4R'
Executing commands in '/tmp/mono-gdb-commands.mNhP4R'.
(lldb) process attach --pid 10261
Process 10261 stopped
* thread #1, name = 'tid_103', stop reason = signal SIGSTOP
    frame #0: 0xffffffffffffffff 
Target 0: (No executable module.) stopped.
Architecture set to: .
(lldb) thread list
Process 10261 stopped
* thread #1: tid = 0x60ab8a, name = 'tid_103', stop reason = signal SIGSTOP
  ...
  thread #39: tid = 0x60ac41
(lldb) thread backtrace all
* thread #1, name = 'tid_103', stop reason = signal SIGSTOP
  frame #0: 0xffffffffffffffff 
 ...
  thread #39
    frame #0: 0xffffffffffffffff 
(lldb) detach
error: Detach failed: Sending isconnect packet failed.

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

Obtained 30 stack frames.
#0  0x00000107fdac10 in operator delete[](void*, std::nothrow_t const&)
#1  0x00000107e6a91c in operator delete[](void*, std::nothrow_t const&)
#2  0x00000107e5bd64 in operator delete[](void*, std::nothrow_t const&)
#3  0x000001086d1298 in MetalHeap::AliasResources()
#4  0x000001086d384c in MetalHeap::AliasResources()
#5  0x0000018f25ce8c in _NSViewDrawRect
#6  0x2f5e00018fbafec4 in -[NSView _recursive:displayRectIgnoringOpacity:inContext:stopAtLayerBackedViews:]

@nonoche2
Copy link

how about launching the app from the terminal with the -force-metal argument?

@timkurvers
Copy link
Owner

@nonoche2 With -force-metal, the game exits with Forced GfxDevice 'Metal' was not built from editor, shaders will not be available.

@diericx Thanks, that's interesting! Curious if Sonoma fixes this, or whether iTerm is more robust. Either way, you should get unobfuscated traces (so you can which functions/methods things are breaking in) by changing the variant from nondevelopment to development.

On second thought: I'm not entirely sure if running the binary directly from any terminal is reliable 🤔

@diericx
Copy link
Author

diericx commented Jul 23, 2023

Hmm... seems like this might be a dead end. I'm not sure we can get it to run natively without Metal support and I don't think we can get that without an updated build from IronGate.

Edit: Someone suggested this on Reddit

You may be able to try the Vulkan Valheim executable and integrate MoltenVK for runtime translation to Metal during the build step.

@timkurvers
Copy link
Owner

timkurvers commented Jul 25, 2023

Done some more digging, and it seems that OpenGL is actively disabled in the Apple Silicon / arm64 build of the Unity standalone player. This would also explain the NullGfxDevice showing up in the log, causing the game to crash when it tries to render the splashscreen.

OpenGL is no longer supported on Apple Silicon chips. The option for OpenGL in Player Settings only works for Intel chips. A warning will be displayed from 2022.2.0a4 and onwards that OpenGL is not supported on Apple Silicon chips and Metal will be used instead.

Source: https://issuetracker.unity3d.com/issues/m1-switching-to-openglcore-doesnt-actually-change-the-active-graphics-api

Future problem: OpenGL support for macOS will seemingly be removed in Unity 2023.1. Let's hope IronGate either port the game to Metal or they stay on an older version 😬

Leaving this issue open, hoping we find some workarounds.

@diericx
Copy link
Author

diericx commented Jul 27, 2023

But aren't we using the older version 2020.3.45f1?

unityversion="2020.3.45f1"

It sounds like OpenGL should still work, it is just deprecated.
https://developer.apple.com/forums/thread/694866

Is it only supported via translation through Rosetta?

@timkurvers
Copy link
Owner

timkurvers commented Jul 27, 2023

It sounds like OpenGL should still work, it is just deprecated.

Yeah, I think that's correct. This opengl-explorer project runs just fine as arm64. So, I wonder if Unity decided to just not support OpenGL for the arm64 build, which is very odd.

That said: while running Valheim arm64 with a debugger, I managed to force the OpenGL renderer (by hotpatching memory) and then the splashscreens render just fine. That seems to indicate that all the OpenGL functionality is still there in UnityPlayer.dylib (arm64) but it's unreachable; in particular the SelectRenderingApi function looks vastly different between x86_64 and arm64.

Here it's stuck on loading Steam:

Screenshot 2023-07-27 at 11 05 08

Bit unsure whether this hotpatching is feasible for the build script, and whether the newer libsteam_api.dylib will work, but worth a shot I guess 🙏 I'll report back as soon as I know more.

@diericx
Copy link
Author

diericx commented Jul 27, 2023

Do you know if it is possible to force Vulkan rather than trying to get OpenGL working? I am running Valheim like so

open ./build/Valheim.app --args -logfile ./build/logs.txt -force-vulkan

Using the master branch it seems to just use opengl no matter what.

Initialize engine version: 2020.3.45f1 (660cd1701bd5)
[Subsystems] Discovering subsystems at path /Applications/Valheim.app/Contents/Resources/Data/UnitySubsystems
GfxDevice: creating device client; threaded=1
Renderer: Apple M2
Vendor:   Apple
Version:  4.1 Metal - 86
GLES:     0
 GL_ARB_blend_func_extended GL_ARB_draw_buffers_blend GL_ARB_draw_indirect GL_ARB_ES2_compatibility GL_ARB_explicit_attrib_location GL_ARB_gpu_shader_fp64 GL_ARB_gpu_shader5 GL_ARB_instanced_arrays GL_ARB_internalformat_query GL_ARB_occlusion_query2 GL_ARB_sample_shading GL_ARB_sampler_objects GL_ARB_separate_shader_objects GL_ARB_shader_bit_encoding GL_ARB_shader_subroutine GL_ARB_shading_language_include GL_ARB_tessellation_shader GL_ARB_texture_buffer_object_rgb32 GL_ARB_texture_cube_map_array GL_ARB_texture_gather GL_ARB_texture_query_lod GL_ARB_texture_rgb10_a2ui GL_ARB_texture_storage GL_ARB_texture_swizzle GL_ARB_timer_query GL_ARB_transform_feedback2 GL_ARB_transform_feedback3 GL_ARB_vertex_attrib_64bit GL_ARB_vertex_type_2_10_10_10_rev GL_ARB_viewport_array GL_EXT_debug_label GL_EXT_debug_marker GL_EXT_framebuffer_multisample_blit_scaled GL_EXT_texture_compression_s3tc GL_EXT_texture_filter_anisotropic GL_EXT_texture_sRGB_decode GL_APPLE_client_storage GL_APPLE_container_object_shareable GL_APPLE_fl
ush_render GL_APPLE_rgb_422 GL_APPLE_row_bytes GL_APPLE_texture_range GL_NV_texture_barrier
OPENGL LOG: Creating OpenGL 4.1 graphics device ; Context level  <OpenGL 4.1> ; Context handle -1182185472
Begin MonoManager ReloadAssembly

I tried again using the change I mentioned above (using unity x86arm64) and it still just runs into NullGfxDevice... but unsure if it is trying to look for OpenGL or Vulkan.

GfxDevice: creating device client; threaded=1
NullGfxDevice:
    Version:  NULL 1.0 [1.0]
    Renderer: Null Device
    Vendor:   Unity Technologies

@timkurvers
Copy link
Owner

Do you know if it is possible to force Vulkan rather than trying to get OpenGL working?

From reverse engineering UnityPlayer.dylib it simply doesn't seem to support Vulkan whatsoever on macOS, only OpenGL/ES and Metal (or no graphics):

Screenshot 2023-07-27 at 13 12 50

There is no mention of force-vulkan either in ParseGfxDeviceArgs (internal method used for parsing command line arguments).

I tried again using the change I mentioned above (using unity x86arm64) and it still just runs into NullGfxDevice... but unsure if it is trying to look for OpenGL or Vulkan.

My current theory is that the arm64-build is either broken, or is solely looking for Metal, ending up with NullGfxDevice since Valheim does not have Metal support.

@diericx
Copy link
Author

diericx commented Jul 28, 2023

Wow great work. I wonder where this -force-vulkan flag I'm seeing around is implemented then, if at all.

I tried to update the minor version of Unity on a whim to test your bug theory but it naturally collided with the game data and wouldn't run.

@timkurvers timkurvers mentioned this issue Jul 28, 2023
6 tasks
@timkurvers
Copy link
Owner

timkurvers commented Jul 28, 2023

I wonder where this -force-vulkan flag I'm seeing around is implemented then, if at all.

I previously played the game on Windows and that had a 'Select game version: Valheim or Valheim (Vulkan)' popup, so I would imagine that at least UnityPlayer for Windows has that flag implemented.


Have opened #33 which should be able to build a universal build, but please use at your own risk! 😊

@timkurvers
Copy link
Owner

Closing this issue as the official macOS client is now available! 🥳 (see #104)


The official client is universal, supporting both Apple Silicon as well as older Intel Macs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants