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

Add WebAssembly-based runtime for GDScript #3370

Open
kisg opened this issue Sep 29, 2021 · 14 comments
Open

Add WebAssembly-based runtime for GDScript #3370

kisg opened this issue Sep 29, 2021 · 14 comments

Comments

@kisg
Copy link

kisg commented Sep 29, 2021

Describe the project you are working on

Godot for a commercial AR project where we plan to contribute our enhancements.

Describe the problem or limitation you are having in your project

GDScript is using a custom interpreter that is too slow for computationaly demanding tasks. At the same time the developer UX is best with GDScript.

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

A new compiler should be added that generates WASM bytecode from the GDScript AST. The WASM bytecode could be executed in different ways:

  • on desktop / mobile a WASM runtime could be embedded into Godot with interpreter (debug), JIT and AOT modes
  • on the web the generated WASM bytecode would be included in the WASM module together with the engine code
  • in the web based editor a WASM interpreter would be embedded into the editor to run / debug GDScript code

Check out #147 for the generic WASM support proposal.

The generated WASM code would implement a script instance, or if #3369 is accepted a GDExtension class.

If the proposal is accepted we are willing to contribute the implementation.

The advantages of this implementation are:

  • No need to develop a Godot specific JIT for GDScripts, may reuse the existing WASM runtimes in either interpreted, JIT or AOT mode
  • WASM is supported on every target platform of Godot
  • WASM is a well defined and safe (easy to validate) binary format that can be used to send game logic additions dynamically in a networked game with the upcoming scene replicator feature. Since the WASM bytecode is cross-platform, scripts transmitted in this format will work on every platform at near-native speed (using JIT based runtime)

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

See above

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

No

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

This is a core change / addition to the GDScript functionality.

@atirut-w
Copy link

Does this imply that WASM also works outside of web browsers? If so, this could be great for GDScript because I think that Godot should try to turn GDScript into bytecodes instead of interpreting them for performance reasons.

@Calinou
Copy link
Member

Calinou commented Sep 30, 2021

Does this imply that WASM also works outside of web browsers?

Yes, the aforementioned WebAssembly runtimes can be embedded into any application. We'll most likely want to pick a runtime written in C or C++ (as opposed to Rust) to avoid making Godot's build process too complex.

@atirut-w
Copy link

Would be poggers if Godot pre-compile GDScripts into bytecode when building projects. God, just imagine the performance boost.

@Calinou
Copy link
Member

Calinou commented Sep 30, 2021

Would be poggers if Godot pre-compile GDScripts into bytecode when building projects. God, just imagine the performance boost.

GDScript is already compiled to bytecode in exported projects (.gdc files), in a way similar to Python. This only speeds up script loading, not script execution. To speed up script execution, you need to perform AOT or JIT compilation.

@atirut-w
Copy link

you need to perform AOT or JIT compilation

I think AOT compilation is a good tradeoff between performance and size here.

@kisg
Copy link
Author

kisg commented Sep 30, 2021

you need to perform AOT or JIT compilation

I think AOT compilation is a good tradeoff between performance and size here.

There are different use cases:

  • interpreted mode is useful during debugging and if a WASM script has to be loaded dynamically while running on the web, e.g. in the online editor (it is currently not possible to load a WASM script into an already running WASM module on the web)
  • for production builds, if code size is not a concern, AOT compilation to machine code can provide the best performance
  • where code size is a concern JIT compilation can provide the best of both worlds.

The good news is that every WASM runtime worth considering supports interpreted, JIT and AOT modes. For example the WASM Micro Runtime is such a candidate: https://github.com/bytecodealliance/wasm-micro-runtime

That said, it makes sense to implement the integration in such a way that the runtime can be replaced easily in a custom engine build (or if possible, through a GDExtension class).

@JoNax97
Copy link

JoNax97 commented Nov 12, 2021

Has there been any news regarding this? It sounds very interesting

@YuriSizov
Copy link
Contributor

Has there been any news regarding this?

No, this is not something that is planned for Godot 4. Any possible work will start after that.

@atirut-w
Copy link

this is not something that is planned for Godot 4.

Why not? This could be a great performance improvement.

@YuriSizov
Copy link
Contributor

Why not?

Because we want to release it at some point 🙃 Godot 4 is not the last version of Godot either, and doesn't have to include everything that is possibly good.

@Calinou Calinou changed the title Add WebAssembly based runtime for GDScript Add WebAssembly-based runtime for GDScript Nov 12, 2021
@EspeuteClement
Copy link

EspeuteClement commented Nov 14, 2022

Another benefit of supporting WASM inside of the engine is that it could enable "scripting" the engine with programming languages that can target WASM. This would allow users to use many compiled programming language in a portable way, as they would need to only compile their code once to WASM, instead of having to compile their code for each target separately, while retaining some of the performance benefits of compiled languages.

This could be also great for extensions.

@ShalokShalom
Copy link

ShalokShalom commented Sep 5, 2023

Written in C++, only to call from GDScript: https://github.com/ashtonmeuser/godot-wasm

Written in Rust, for all languages: https://github.com/Dheatly23/godot-wasm

As a runtime, I suggest this: https://lunatic.solutions/ and this
https://github.com/wasm3/wasm3

@atirut-w
Copy link

atirut-w commented Sep 6, 2023

Totally forgot this exists. Now that 4.0 is released, could this be included in a 4.x release?

@ShalokShalom
Copy link

They are not far enough developed on their own.
I assume there are good chances to include the GDScript and C++ based one and wasm3.

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

No branches or pull requests

7 participants