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

BAR on Metal #936

Open
svonava opened this issue Aug 3, 2023 · 63 comments
Open

BAR on Metal #936

svonava opened this issue Aug 3, 2023 · 63 comments

Comments

@svonava
Copy link

svonava commented Aug 3, 2023

Hi all,

I was wondering if the effort required to maintain a Metal port of BAR has ever been estimated cost-wise.

This could be a great way to bring more $$ into the project - I for one would gladly pay for a Apple-silicon compatible release of BAR.

Anybody knows?

Thanks,
Daniel

@lhog
Copy link
Collaborator

lhog commented Aug 3, 2023

I played around with Vulkan (which is compatible with Metal through MoltenVK). I added a very safe (as I thought) implementation to gather VK capabilities and unfortunately it crashes for many people with lesser drivers, so it had to be disabled. From that I concluded the native Vulkan way is not yet viable for our playerbase.

In the same time there's a promissing technology to emulate OpenGL on Vulkan, called Zink, as Vulkan backend is available through MoltenVK, this might be a way to go. It's not yet mature, but very promissing. Last time we checked it used to display most of the graphical stuff except a few items. I owe @Karolson , who is the main proponent of Zink, debugging of these remaining items.

@lhog
Copy link
Collaborator

lhog commented Aug 3, 2023

That said modern Apple gear added another level of complexity stepping out from x64 architecture, so it's no longer just about missing OpenGL capabilities, but also the need to recompile for ARM (or use x64 emulation for this matter)

@svonava
Copy link
Author

svonava commented Aug 3, 2023 via email

@sprunk
Copy link
Collaborator

sprunk commented Aug 3, 2023

is it that only a subset of the Apple hardware is needed, or the latest OS, or the installation is hard (eg user
has to compile?).

A safe Vulkan stub that doesn't actually render anything and just checks capabilities (see e6ac54e) already crashes for many people with lesser drivers. An implementation that does more would run into even more issues. The problem is partially social since you can't control what hardware players have and what drivers they have installed.

@svonava
Copy link
Author

svonava commented Aug 3, 2023 via email

@svonava
Copy link
Author

svonava commented Aug 3, 2023 via email

@lhog
Copy link
Collaborator

lhog commented Aug 3, 2023

My Vulkan attempt was unrelated to MacOS, I made it for the current Linux and Windows users. And there were related crashes for far too many people to ignore them.

As far as Mac, we don't have anything for it, even for x64 ones: no building pipelines, no libraries, only fragmental source code readiness. More than anything we need a motivated developer with Mac, who is not afraid of hitting roadblocks one after another. All previous attempts from contributors so far didn't succeed.

@sprunk
Copy link
Collaborator

sprunk commented Aug 3, 2023

Is there some way we can estimate the effort needed?

If you're eager to throw money at the problem you could just find and hire somebody competent, to look at things and produce such estimate

@svonava
Copy link
Author

svonava commented Aug 6, 2023

I'd be inclined to throw some money at the problem yes but probably a good idea to see if there is interest in the community first.

Is this something we could survey in-game?

@p2004a
Copy link
Collaborator

p2004a commented Aug 6, 2023

The results might be quite skewed because game currently doesn't support Mac, so likely not many Apple users in game at the moment.

Overall, there is interest:

We can't easily do polls in game, we can do them easily on forums like Discord, Reddit, not sure about website (it's webflow).

What exactly what you like to survey on top of the above question about Mac support?


From technical side, this post focuses on the graphical API, which is very problematic, because it's not exposed only to the engine, but is also available to Lua code of games. I would also like to point out that there is one more problem that is related to "sync": game simulation depends on the deterministic results of floating point operations across players using streflop library which doesn't have ARM support at the moment, and is potentially another hard problem to work around for supporting native ARM execution.

@svonava
Copy link
Author

svonava commented Aug 7, 2023 via email

@p2004a
Copy link
Collaborator

p2004a commented Aug 7, 2023

On a high level this sounds like something that can work out. It has an assumption that we will be able to find a competent developer willing to do this work, including ongoing maintenance, which I find not obvious.

We will though need somebody motivated and with enough free time on their hands to drive and push forward execution of such a plan.

Things move forward in this project only when there is an individual willing to spend their free time on it.

@marcushutchings
Copy link
Collaborator

Maybe try to get the x64 version running over Zink and MoltenVK?

The API exposed to games is OpenGL and that isn't going to change, so in essence one needs to convert OpenGL into Metal.

@ryansch
Copy link

ryansch commented Nov 19, 2023

I have an M1 Max based mac. I've installed Asahi Linux and tried to run BAR. There's an ARM64 flatpak but it appears that the spring/recoil binaries are actually built for x86_64.

I fiddled with the build system and managed to get through cmake but the generated Makefiles are trying to pass x86 options to gcc.

Is this primarily a build tooling issue or a larger porting effort (streflop issue notwithstanding)?

Edit: Note that my usecase is unrelated to the graphics issues in macOS.

@p2004a
Copy link
Collaborator

p2004a commented Nov 19, 2023

There's an ARM64 flatpak but it appears that the spring/recoil binaries are actually built for x86_64.

flathub/info.beyondallreason.bar#40

Is this primarily a build tooling issue or a larger porting effort (streflop issue notwithstanding)?

The main porting effort would be to make sure that all floating operations give the same results on arm and on x86.

@Karolson
Copy link
Contributor

Your best bet is probably running BAR through an x86 on ARM emulator (box64, FEX, etc.)
Though note than Asahi Linux supports OpenGL only up to 3.1 at the moment. No idea if that's enough to launch BAR

@marcushutchings
Copy link
Collaborator

We need OpenGL 4.3 for BAR, so that's the first issue to resolve.

@marcushutchings
Copy link
Collaborator

You'll also need to use a library like https://github.com/DLTcollab/sse2neon, we use SSE extensions directly in-engine.

@Karolson
Copy link
Contributor

We need OpenGL 4.3 for BAR, so that's the first issue to resolve.

BAR works fine with OpenGL 3, but doesn't have healthbars. Dunno if 3.1 would suffice, as latest 3.x version is 3.3
Trying to compile the engine for ARM is indeed a problem, so just see if emulators work first

@cellulosa
Copy link

cellulosa commented Nov 23, 2023

Doesn't this help? It mentions OpenGL 4 https://github.com/openglonmetal/MGL

@marcushutchings
Copy link
Collaborator

marcushutchings commented Dec 4, 2023

Unfortunately, that only supports OpenGL 4.6. We need capability that gets deprecated by the time of OpenGL 4.6. So it won't help here.

@12345swordy
Copy link
Contributor

From technical side, this post focuses on the graphical API, which is very problematic, because it's not exposed only to the engine, but is also available to Lua code of games. I would also like to point out that there is one more problem that is related to "sync": game simulation depends on the deterministic results of floating point operations across players using streflop library which doesn't have ARM support at the moment, and is potentially another hard problem to work around for supporting native ARM execution.

From my quick reading regarding deterministic results from float values. They say that if you strictly follow the IEEE 754 compliant mode standard you shouldn't get any problems.
https://gafferongames.com/post/floating_point_determinism/

@Trass3r
Copy link

Trass3r commented Apr 24, 2024

note than Asahi Linux supports OpenGL only up to 3.1 at the moment.

By now it's at 4.6: https://rosenzweig.io/blog/conformant-gl46-on-the-m1.html

@marcushutchings
Copy link
Collaborator

Unless Asahi also has Rosseta then, no.

@Karolson
Copy link
Contributor

You can try running the game with an emulator like box64/fex-emu

@Karolson
Copy link
Contributor

box64 now supports 16k pages required for M1, and Mesa 24.1 with OpenGL 4.6 on M1 has been released

@svonava
Copy link
Author

svonava commented May 23, 2024

@Karolson does that mean box64 now runs BAR? Could you confirm the performance / feature completness?

@Karolson
Copy link
Contributor

I don't have any desktop ARM hardware, can't confirm anything

@alanpaone
Copy link

I gave it a shot in Asahi with box64. Doesn't quite work! I'm not sure if this means that __gcov_dump, __gcov_flush, etc are missing/broken somehow, or if R_X86_64_JUMP_SLOT is affecting all of them. I'm not familiar with box64 tbh, and I have no idea if this is the only roadblock or just the first one, but they seem to be actively progressing on M1, I'll try it again on the next update.

Warning: Weak Symbol __gcov_dump not found, cannot apply R_X86_64_JUMP_SLOT @0x10a6aada0 (0xa128af6)
Warning: Weak Symbol __gcov_flush not found, cannot apply R_X86_64_JUMP_SLOT @0x10a6aada8 (0xa128b06)
Warning: Weak Symbol ZSTD_trace_decompress_begin not found, cannot apply R_X86_64_JUMP_SLOT @0x10a6aae8 (0xa128cc6)
Warning: Weak Symbol ZSTD_trace_decompress_end not found, cannot apply R_X86_64_JUMP_SLOT @0x10a6aae90 (0xa128cd6)
Warning: Weak Symbol _ZTHN2v88internal12trap_handler21g_thread_in_wasm_codeE not found, cannot apply R_X86_64_JUMP_SLOT @0x10a6ab518 (0xa1299e6)
Warning: Weak Symbol _ZTHN2v88internal19RwxMemoryWriteScope31code_space_write_nesting_level_E not found, cannot apply R_X86_64_JUMP_SLOT @0x10a6ab520 (0xa1299f6)
Warning: Weak Symbol OPENSSL_memory_alloc not found, cannot apply R_X86_64_JUMP_SLOT @0x10a6ab770 (0xa129e96)
Warning: Weak Symbol OPENSSL_memory_free not found, cannot apply R_X86_64_JUMP_SLOT @0x10a6ab778 (0xa129ea6)
Warning: Weak Symbol sdallocx not found, cannot apply R_X86_64_JUMP_SLOT @0x10a6ab780 (0xa129eb6)
Warning: Weak Symbol OPENSSL_memory_get_size not found, cannot apply R_X86_64_JUMP_SLOT @0x10a6ab788 (0xa129ec6)
3456|SIGILL @0xffff22ffde08 (???(0xffff22ffde08)) (x64pc=0x105ccd87f//tmp/.mount_BeyondYTg5uh/beyond-all-reason:"/tmp/.mount_BeyondYTg5uh/beyond-all-reason + 0x5ccd87f", rsp=0xffff13fffd30, stack=0xffff13800000:0xffff14000000 own=0xffff13800000 fp=0xffff13fffd70), for accessing 0xffff22ffde08 (code=1/prot=7), db=0xffff221ab990(0xffff22ffdda0:0xffff22ffde48/0x105ccd87f:0x105ccd880//tmp/.mount_BeyondYTg5uh/beyond-all-reason + 0x5ccd87f:clean, hash:cc/cc) handler=(nil)
RAX:0x0000000000000016 RCX:0x0000000000000000 RDX:0x0000ffff133ff660 RBX:0x0000000000001000
RSP:0x0000ffff13fffd30 RBP:0x0000ffff13fffd70 RSI:0x0000ffff28810f60 RDI:0x0000ffff13fffd44
R8:0x0000000000000000 R9:0x0000000000000001 R10:0x0000000101b7096c R11:0x0000000000000010
R12:0x0000000000000003 R13:0x0000000000000000 R14:0x0000000000000003 R15:0x00003e1800201000
ES:0x002b CS:0x0033 SS:0x002b DS:0x002b FS:0x0043 GS:0x0053
RSP-0x20:0x0000000000000000 RSP-0x18:0x0000000000000000 RSP-0x10:0x0000ffff13fffd70 RSP-0x08:0x0000000105ccd87f
RSP+0x00:0x0000000000000000 RSP+0x08:0xffffffff00000003 RSP+0x10:0x0000001600000000 RSP+0x18:0x000000010a7830b0 opcode=90 11 41 64 00 00 00 00 (CC 0F 0B CC CC)
Illegal instruction (core dumped)

@6AKU66
Copy link
Collaborator

6AKU66 commented Jun 17, 2024

The last thing BAR needs is people randomly throwing money at certain bits of development, it's one of the fastest ways to kill everything. Donate for server costs if you want to support the game.

People always want throw money to specific things that concern them.

@svonava
Copy link
Author

svonava commented Jun 17, 2024

I'd prefer to support the game in a way that allows me to play it on my default computer @TheSilverHornet - how exactly is that going to "kill" it?

@sprunk
Copy link
Collaborator

sprunk commented Jun 18, 2024

image

@TheSilverHornet
Copy link

It all builds on less interesting/glamourous/'demanded' work in the background. If you start throwing money at some devs and not others, the rest are likely to resent it and rapidly stop the unappreciated work, others try to fight over the lucrative coding, and still others give up entirely at the state of the whole mess. Poorly thought out monetisation is a very good way to kill a voluntary project.

@p2004a
Copy link
Collaborator

p2004a commented Jun 18, 2024

I believe what you fear @TheSilverHornet is effectively this https://ziglang.org/news/bounties-damage-open-source-projects/, which is indeed problematic.

But, explicitly hiring somebody to do the work, is different. It's not like BAR, even officially, isn't doing it already in the form of buying proprietary assets.

@TheSilverHornet
Copy link

Sometimes it can work in modular fashion, but it very much needs thought to avoid turning into a bounty type system via the back door. There is a large difference between buying external prerolled assets off the shelf, and 'bribing' an existing developer to do something specific. Safest route would probably be paying someone fully external to do it, if possible, but they'd probably ask so much it would be prohibitive.

@p2004a
Copy link
Collaborator

p2004a commented Jun 18, 2024

and 'bribing' an existing developer to do something specific

I don't believe that is what is happening in this thread.

paying someone fully external to do it

I believe this is what is happening.

@sprunk
Copy link
Collaborator

sprunk commented Jun 19, 2024

My 3c:

  • mac/metal support is a hot potato, it will introduce a maintenance burden for the indefinite future that cannot be solved by a one-time lump of work. The real task would be to find a long-term maintainer willing to take that burden up. In this case somebody with "skin in the game" is more reliable than a random one-time external developer
  • the dangerous thing about existing/external is not who implements it but who has the initiative of deciding priorities and vision. If the project already wants to do something (e.g. obtain an art asset) and decides to speedrun the implementation by applying money that's fine. If somebody shows up and says "i offer money but you must shift your priorities to do what i want" that sets a terrible precedent
  • keeping mac users away is a positive feature

@alanpaone
Copy link

Its a very classic Mac Guy attitude to try and just buy our way out of our own decisions. If you really want to play on your mac, parallels will mostly work, and judging by how much better parallels 19 is than 18, it will probably be better with august's update to 20. If you have coding knowledge, it is probably less ongoing support work to help improve box64. If you don't , and just want to throw money at this problem, buy a steam deck, and you'll get full support and be able to play right away. there are not enough of us to pay the salary of a mac gaming developer.

@12345swordy
Copy link
Contributor

12345swordy commented Jun 20, 2024

@12345swordy: How much money would it take to make somebody who knows what they are doing actually take a serious stab at this?

Assume 40 hours per week:

Best case: 1-2 months work. $5,000 - $10,000 dollars
Worst case: 3-4 months work. $15,000 - $20,000 dollars

Porting to arm architecture alone is very hard, given the engine requirement to have deterministic physics which in turn requires deterministic float point math, which itself very hard even for x86_64 hardware systems alone.
You don't need the engine to run on metal you just need it to run on ARM architecture. (Unless there are plans to implement vulkan, just run linux/windows on your arm machine)

@nimbusgo
Copy link

I noticed that fedora asahi linux has support for opengl 4.6 for a few months now on apple silicon.

My initial attempts at running BAR on an m1 with fedora asahi were unsuccessful because of the different architectures.

I'm wondering if using box64 on fedora asahi might work. Will give that a try. Anyone try anything like this yet?

@alanpaone
Copy link

alanpaone commented Jun 25, 2024

Anyone try anything like this yet

yes, it doesn't run, but that was last month, given the pace of development it probably gets a different error now

@loligans
Copy link

loligans commented Jul 1, 2024

Anyone try anything like this yet

yes, it doesn't run, but that was last month, given the pace of development it probably gets a different error now

I'll try again when I get my friends mac and post steps for others

@pmadhikar
Copy link

I use an old intel Macbook as a daily driver and I have access to an M1 Macbook. Can anyone point me to how to get started? Maybe I can start with getting builds to work on x86?

@lhog
Copy link
Collaborator

lhog commented Jul 19, 2024

The first thing I'd do is to grab some basic application running on OpenGL (something like https://github.com/JoeyDeVries/LearnOpenGL/tree/master/src/1.getting_started/2.1.hello_triangle) and make it run on Metal.

@marcushutchings
Copy link
Collaborator

Xplane have their OpenGL running on Zink over MoltenVK on Mac - so that approach has been proven to work. Starting with a super simple app and getting that working, as Ihog suggested, is a good starting point.

@svonava
Copy link
Author

svonava commented Jul 22, 2024

I use an old intel Macbook as a daily driver and I have access to an M1 Macbook. Can anyone point me to how to get started? Maybe I can start with getting builds to work on x86?

Nice! Keep us posted about the progress and perhaps make a buymeacoffee.com or www.patreon.com account as you make progress ^_^

@pmadhikar
Copy link

pmadhikar commented Jul 24, 2024

Thanks for the pointers guys!

I was just fiddling a bit with MGL before I noticed someone here saying it would probably not work because it supports only OpenGL 4.6. MGL seems to be somehow dependant on Xcode and I'm trying to make its build work without that dependency. I have the macbooks, but I normally target linux so it is taking me some time to get up to speed there.

BTW a couple of questions for @marcushutchings if I may

About

Unfortunately, that only supports OpenGL 4.6. We need capability that gets deprecated by the time of OpenGL 4.6. So it won't help here.

Does it make sense to try to get MGL to support older OpenGL? Do you know what got deprecated that we need?

Also, I don't know anything about zink. A cursory google lead me to an article [1] that indicates zink is part of mesa 3d drivers. Am I looking at the right thing here? If so, I don't think we can use it. It would force dependency on x11 emulation on macos [2] which in my experience is pretty dismal.

For now I'm trying to get MGL to build and to use it with the some basic OpenGL as @lhog suggested. Let me know if there's a better way!

[1] https://www.phoronix.com/review/zink-mesa-221
[2] https://docs.mesa3d.org/macos.html

@Karolson
Copy link
Contributor

Zink is OpenGL on Vulkan driver, and as MacOS doesn't support Vulkan either it will have to use MoltenVK Vulkan on Metal too, and it has nothing to do with X11

But that doesn't matter anyway, Spring has an OpenGL 3.3 render path and MacOS's deprecated OpenGL driver should support that out of the box

@pmadhikar
Copy link

pmadhikar commented Jul 24, 2024 via email

@Karolson
Copy link
Contributor

Zink is part of Mesa
https://gitlab.freedesktop.org/mesa/mesa

@Trass3r
Copy link

Trass3r commented Jul 24, 2024

Zink is indeed a part of Mesa and translates GL into Vulkan. I don't know about its dependencies on Mac.

Does it make sense to try to get MGL to support older OpenGL?

You'd need to do that yourself.
https://github.com/openglonmetal/MGL?tab=readme-ov-file#why-the-focus-on-46-functionality

openglonmetal/MGL#75

@lhog
Copy link
Collaborator

lhog commented Jul 25, 2024

The engine (and games) use OpenGL calls as old as from 2.x era as well as more modern 3.x and 4.x calls.

@tessellates
Copy link

tessellates commented Sep 11, 2024

Hello, there is something I don't understand, even though openGL is deprecated on mac, it is still supported for now. What am I missing what is the issue? Okay, I see now 4.3 is not straightfoward.

@AdamDorwart
Copy link

AdamDorwart commented Oct 2, 2024

I was able to get BAR building and running on a Macbook Pro M1 in Asahi. I had to port streflop to arm64 but it looks like it's passing the SpringMath::Init test. I'm trying to do more complete testing of streflop independently but I'm having trouble getting consistent results under Windows even before I add my changes.

Anyways after porting streflop and vendoring https://github.com/DLTcollab/sse2neon it was pretty straight forward to get building under gcc. There were a few other small unsuprising changes. There's of course a few code paths doing nothing atm (fpucheck and cpuid AFAIK), but I'm able to start a skirmish with an AI. Took me a while to figure out how to bootstrap BAR on the platform and get it to launch. I'm not sure I can recreate it yet. Took some unknown combo of pr-downloader, bar-lobby, chobby, and manually messing with files to put together a working game directory. Hopefully that was just me learning the architecture and someone could recreate it easily.

Graphics on lowest: ~105FPS @ 3024x1890
Screenshot_20241002_153535
Graphics on ultra: ~55FPS @ 3024x1890
ultra

Here's the logs from a short run using chobby:test to launch: Stdout & stderr

As you can see there's definitely more work to be done. Some graphical bugs on ultra make units invisible. Some menus seem to be crashing from nil references. The AI also looks like it's stuck although it will attack if I approach. Who knows maybe these issues are related to my botched game files.

It's not BAR on Metal but I think it's reasonable to say that it will be much easier to target Linux Arm64 first especially with Asahi now being fully OpenGL 4.6 compliant. At the very least these results are encouraging enough to say that MGL might get this running on MacOS in a similar state.

It's pretty hacked up right now but here's what it took. I will try to clean it up and see if it's something the maintainers would like to merge. Risk should be minimal to the existing clients since most of the changes will end up guarded by ifdefs for the target platform.

https://github.com/AdamDorwart/spring/pull/1/files

@6AKU66
Copy link
Collaborator

6AKU66 commented Oct 2, 2024

I'm trying to do more complete testing of streflop

I think as a test you can try to join into MP session as a spectator and watch entire game and see if you will start to get desync messages. Also you can try watch replays...

@AdamDorwart
Copy link

AdamDorwart commented Oct 3, 2024

I haven't gotten byar-chobby to work yet (again I think just messed up game files). The best I can do is chobby:test but when I try MP I can login but only see "Games are hidden, unsure why." under Battle List. It also doesn't show any replays. bar-lobby lists my replays but throws lots of errors trying to launch. Haven't tried to debug either yet.

I'm hoping I can figure out a spring command to launch replays but I'm not there yet.

Here's one of my replay files: replay.zip

FP inconsistency could come from either streflop (which I've spent the most time verifying independently) and sse2neon (haven't done much testing yet).

@Trass3r
Copy link

Trass3r commented Oct 3, 2024

@Trass3r
Copy link

Trass3r commented Oct 3, 2024

Your best bet is probably running BAR through an x86 on ARM emulator (box64, FEX, etc.)

box64 now supports 16k pages required for M1, and Mesa 24.1 with OpenGL 4.6 on M1 has been released

The question is how well it works/performs.
For FEX:
FEX-Emu/FEX#1650 (comment)
FEX-Emu/FEX#1921 (comment)

Seems like they've given up in favor of microVMs.

@lhog
Copy link
Collaborator

lhog commented Oct 3, 2024

Oh my gosh... Awesome advancement!
Make sure to visit either BAR or engine's Discord to say Hi!

I haven't gotten byar-chobby to work yet (again I think just messed up game files). The best I can do is chobby:test but when I try MP I can login but only see "Games are hidden, unsure why." under Battle List.

That is most probably due to the engine name / version mismatch with what BAR runs at. This can be worked/hacked around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests