godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.12k stars 69 forks source link

Add support for the RISC-V open source CPU architecture #3374

Closed aaronfranke closed 2 years ago

aaronfranke commented 2 years ago

Describe the project you are working on

Getting Godot running on RISC-V.

Describe the problem or limitation you are having in your project

Godot doesn't run on RISC-V yet.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

I propose that Godot adds RISC-V support. Not official builds, or even necessarily tested, but at least allowing users to compile for RISC-V. Note that this is more likely to appeal to existing RISC-V users rather than existing Godot users.

Why?

There are many reasons to add RISC-V support to Godot:

I'll also cite these reasons for Debian adding RISC-V support, a lot of which applies to Godot:

debian-riscv

Which?

RISC-V has 32-bit, 64-bit, and 128-bit versions, and various extensions.

This means that the full architecture name is rv64gc. The "RV" abbreviation is necessary for the architecture name and is a very common shortened form of "RISC-V".

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

master version: https://github.com/godotengine/godot/pull/53508

3.x version: https://github.com/godotengine/godot/pull/53509

These can be compiled with scons use_llvm=yes arch=rv64. I've done so on an emulated RV64 Ubuntu Server 20.04 VM, I don't actually own any real RISC-V hardware myself. EDIT: I do now.

I've been working on this for awhile by making sure the engine does not have strong dependencies on particular parts. For example, making the editor not require the RegEx module was not just for the sake of modularity, it's also to make it possible to compile the editor for RISC-V.

Here is a short list of modules that don't work on RISC-V (all non-essential):

Here is a chart of what works and what doesn't on RISC-V (EDIT: Updated 2024-06-06):

Clang compiles Clang runs GCC compiles GCC runs
master Editor debug βœ… πŸ—£οΈ ❌ ❌
master Editor optimized ❌ ❌ ❌ ❌
master Template debug βœ… πŸ—£οΈ ❌ ❌
master Template optimized ❌ ❌ ❌ ❌
3.x Editor debug βœ… βœ… ❌ ❌
3.x Editor optimized βœ… ❌ ❌ ❌
3.x Template debug βœ… βœ… ❌ ❌
3.x Template optimized βœ… ❌ βœ… ❌
3.x Headless debug βœ… βœ… ❌ ❌
3.x Headless optimized βœ… βœ… ❌ ❌
3.x Server debug βœ… βœ… ❌ ❌
3.x Server optimized βœ… βœ… βœ… βœ…

πŸ—£οΈ = It runs but only headlessly. It crashes with both the X11 and Wayland display drivers.

Several 3.x versions run and can display and render stuff. Even the editor works, but only in debug mode. However, it uses softpipe, so the performance is absolutely horrible (one frame every 5 seconds, I'm not joking).

GCC builds are all failing due to GCC bugs with atomics, but weirdly 3.x optimized template builds work. I have no clue why?

Old chart when I first opened this proposal (2021-10-01):

| | Clang compiles | Clang runs | GCC compiles | GCC runs | |-----------------------------|----------------|------------|--------------|----------| | `master` Editor debug | βœ… | πŸ—£οΈ | ❌ | ❌ | | `master` Editor optimized | ❌ | ❌ | ❌ | ❌ | | `master` Template debug | βœ… | πŸ—£οΈ | ❌ | ❌ | | `master` Template optimized | ❌ | ❌ | ❌ | ❌ | | `3.x` Editor debug | βœ… | ❔ | ❌ | ❌ | | `3.x` Editor optimized | ❌ | ❌ | ❌ | ❌ | | `3.x` Template debug | βœ… | ❔ | ❌ | ❌ | | `3.x` Template optimized | ❌ | ❌ | ❌ | ❌ | | `3.x` Headless debug | βœ… | βœ… | ❌ | ❌ | | `3.x` Headless optimized | ❌ | ❌ | ❌ | ❌ | | `3.x` Server debug | βœ… | βœ… | ❌ | ❌ | | `3.x` Server optimized | ❌ | ❌ | ❌ | ❌ | Godot doesn't compile with GCC, but does compile with Clang. It fails on GCC because of [GCC bugs with atomics](https://github.com/riscv-collab/riscv-gcc/issues/15). Clang works for compiling debug builds, but not optimized release builds. This is because LLVMgold.so is missing on RISC-V. I tested both Clang 10 and Clang 12. I have only tested Godot builds that offer headless modes (so, for 3.x, I only tested p=server and not p=x11, which is why those are ❔).

Here's a minimal test script:

func _ready():
    if OS.has_feature("riscv"):
        print("This is RISC-V!")
    else:
        print("This isn't RISC-V!")

When I run the test project with one of the succeeding headless-capable builds of Godot in headless mode, this output shows up:

Godot Engine v3.4.beta.custom_build.aa18d74c6 - https://godotengine.org

This is RISC-V!

If this enhancement will not be used often, can it be worked around with a few lines of script?

This won't be used often, and it could be worked around with a few edits to the SCons files, but it would make sense to merge these changes into Godot.

Is there a reason why this should be core and not an add-on in the asset library?

Platform support is a core feature.

Calinou commented 2 years ago

See also https://github.com/godotengine/godot-proposals/issues/988.

Pull requests for making Godot build on RISC-V are welcome, but I doubt official editor and export template binaries will be produced. (However, the Godot Flatpak could support RISC-V now that the FreeDesktop SDK supports it.)

atirut-w commented 2 years ago

If we can manage to make Godot run at a good performance level on RISC-V, this could benefit other platforms, too.

fire commented 2 years ago

Is this complete?

aaronfranke commented 2 years ago

I recently added RISC-V support to the build system of the GDNative C demos: https://github.com/godotengine/gdnative-demos/pull/63

ubuntu@ubuntu:~/gdnative-demos/c/simple/project$ ~/godot-3/bin/godot_server.x11.tools.rv64.llvm 
Godot Engine v3.4.3.rc.custom_build.5b5b52ce1 - https://godotengine.org

Hello World from GDNative Simple C demo on RISC-V!

(you have to add some code to get this to print out - by the default the demo doesn't print this)

I also have a branch for the C++ demos https://github.com/aaronfranke/gdnative-demos/tree/scons-cpu-arch, but it needs some changes in the godot-cpp repo to get godot-cpp to compile for RISC-V (and ARM while we're at it), so I am waiting on this PR to be merged before the C++ demos will run on RISC-V Linux (and ARM Linux). https://github.com/godotengine/godot-cpp/pull/714


Also, I have prebuilt Godot binaries for RISC-V for some releases of Godot available here https://drive.google.com/drive/u/0/folders/1mvopK6gcmXFW-KEqdedmBZXbizgug0yY

akien-mga commented 2 years ago

I think this can be closed as fixed by https://github.com/godotengine/godot/pull/53508.

We might want to keep track of other RISC-V related improvements in dedicated proposals (e.g. the possibility to provide official builds eventually).

aaronfranke commented 2 years ago

Support for RISC-V was also added to the godot-cpp repo recently for GDExtension for Godot 4.0: https://github.com/godotengine/godot-cpp/pull/759

ShalokShalom commented 2 years ago

Interesting in this context:

Two different RISCV SBC's, for a low level entry into development

Star64

https://www.pine64.org/2022/08/28/august-update-risc-and-reward/

Visionfive 2

https://www.kickstarter.com/projects/starfive/visionfive-2

CutestNekoAqua commented 1 year ago

@aaronfranke is there a way for us to implement LLVMgold.so so that we can produce release builds?

aaronfranke commented 1 year ago

@CutestNekoAqua I don't know, I never got that far. I will be looking into improving RISC-V support further once I get my hands on Ubuntu on the VisionFive 2.

CutestNekoAqua commented 1 year ago

@CutestNekoAqua I don't know, I never got that far. I will be looking into improving RISC-V support further once I get my hands on Ubuntu on the VisionFive 2.

@aaronfranke Would recomment looking into the archlinux riscv project too, as it seems to be more bleeding edge and thus easier to test new compiler versions.

aaronfranke commented 1 year ago

@CutestNekoAqua I'm actually interested in using Ubuntu because it is less bleeding edge. If something works on a stable version of a compiler then it will probably work on newer versions and the builds will be cross-compatible.

lokimckay commented 3 months ago

Hello, sorry to necro

but I doubt official editor and export template binaries will be produced

As a novice cross-compilation dev building a GDExtension (using Godot 4.2.2), I'm confused why this is the case and how to obtain the export templates on my own.

I incorrectly assumed that every item in the Project -> Export -> Binary Format -> Architecture would be available in the official export templates. In particular I was interested in rv64, ppc64 and ppc32.

From reading these unrelated docs about Compiling for Android, I am guessing that I would need to run ./gradlew generateGodotTemplates in the environment I'd like to target?

Perhaps a more general docs page could be considered for others like me?

aaronfranke commented 3 months ago

@lokimckay The instructions for compiling Godot for other architectures is the same as compiling normally, except you specify the architecture using arch=, and if you either need to use an environment with that architecture (either real hardware or a VM), or set up cross-compilation (which I don't know how to do). For example, try running scons arch=rv64 use_llvm=yes. If you run this from a Linux rv64 environment, it should succeed. scons arch=rv64 without the LLVM flag is intended to work in the future but there are bugs with GCC last time I tried it.

https://docs.godotengine.org/en/stable/contributing/development/compiling/compiling_for_linuxbsd.html

AThousandShips commented 3 months ago

The architecture options are explained in the documentation for building, the general details, here

lokimckay commented 3 months ago

Thanks, but those explanations and linked docs relate to compiling the engine from source correct? I would prefer not to do that. I will try to explain my use case better:

  1. Want to build and run a Godot project inside different OS's and architectures in order to run tests in those environments
  2. Do this via Github actions runners
  3. Create workflow that downloads Godot binary and Export templates to the runner
  4. Build and run the Godot project in the runner

This works fine for all build presets, except those that specify rv64, ppc64 or ppc32, because those architectures do not have export templates included in the official .tpz

I assume to solve my use-case I would need to compile Godot for those 3 architectures once in order to get the export templates, (or find them at an unofficial source), and then insert those to each run of the workflow via my own mechanism?

Calinou commented 3 months ago

I assume to solve my use-case I would need to compile Godot for those 3 architectures once in order to get the export templates, (or find them at an unofficial source), and then insert those to each run of the workflow via my own mechanism?

Yes πŸ™‚

You can store these kinds of "permanent" artifacts in a dedicated repository's Releases tab, like I do in https://github.com/Calinou/media.

aaronfranke commented 3 months ago

Update: I re-tested using the latest 2024-05 OS image. I updated the OP with a new table of checkmarks that shows what works and what doesn't.

The OS now ships with sufficient graphics support to launch the 3.x editor and export template with a GUI, but only debug builds, and only Clang, and only 3.x. There is no hardware acceleration yet, it runs through softpipe, so it runs agonizingly slow, about 1 frame every 5 seconds. Still, it boots:

Screenshot from 2024-06-03 20-51-32

It could be possible to get better performance by connecting a dedicated GPU. I only used the integrated one.

The terminal in the bottom-right shows what happens if I try to launch 4.x, and the same message also shows up when trying to launch 3.x optimized editor/templates. For all cases, it happens when trying to render stuff.

GCC still doesn't compile... except for optimized 3.x templates, both server and non-server? No clue why those succeed. The rest fail due to errors with atomics, like before.

fwsGonzo commented 4 days ago

The rest fail due to errors with atomics

Does Godot use custom atomics, __sync builtins or C++ standard library atomics?