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

wasm support #213

Open
pmp-p opened this issue Aug 1, 2022 · 10 comments
Open

wasm support #213

pmp-p opened this issue Aug 1, 2022 · 10 comments

Comments

@pmp-p
Copy link

pmp-p commented Aug 1, 2022

Hi, i saw the note about pyodide support : that sounds promising :)

but is there any reason to build for pyodide instead of CPython upstream directly ?

CPython 3.11 already supports ctypes, cffi and pycparser and WebAssembly is a tier3 supported platform.

Also you're welcome to use the pygame toolchain directly in your CI to build in less than 1 minute

toolchain ( only use 3.11 ) :
https://github.com/pygame-web/python-wasm-sdk

ci yml demo:
https://github.com/pygame/pygame/blob/main/.github/workflows/build-emsdk.yml

pymunk running on web :
https://pmp-p.itch.io/stuntcat

running testsuites in the browser:
https://pygame-web.github.io/python-wasm-plus/python311.html?org.python3.11.0&all

quick guide to run CPython+pygame in the browser https://pygame-web.github.io/

@viblo
Copy link
Owner

viblo commented Aug 2, 2022

Interesting!

The note about pyodide was only to have a starting point / reminder about wasm. After I wrote it I managed to compile it (and import) pymunk out-of-the-box with pyodide using their instructions, but after that I haven't had any time to try more things. It seemed to me that at the time the packaging was not fully defined yet, e.g. pypi doesnt support such packages, for pyodide a custom meta.yml is used, it was not obvious if a wheel built using one method can be loaded by other methods etc. All in all I wasnt sure how to provide a pre-built wheel, even if things seems to be improving.

From what I can see pygame-web looks very interesting, would be very cool to make a couple of the pygame based pymunk examples available running in the browser!

@pmp-p
Copy link
Author

pmp-p commented Aug 2, 2022

with pygbag hosting config files and finding cdn for hosting wheels is not an issue, wheel can just be unzipped in game/app folder and it will be repacked with code and assets there is a mecanism in loader to preload wasm libraries. I'm sure a simple zip on github releases could help lot of people around for their game jams.

@viblo
Copy link
Owner

viblo commented Aug 2, 2022

I will definitely take a look once I have some time free for Pymunk development. 👍

@viblo
Copy link
Owner

viblo commented Aug 25, 2022

Reviving this issue. Today I had a bit time over as evident by the other ticket. After getting stuck with pygame-web I looked back at Pyodide. It looks like they have progressed since last time, and it is now capable of loading wheels built outside of it: https://pyodide.org/en/0.21.1/development/new-packages.html#building-python-wheels-out-of-tree

If I build a wheel using their method, producing something like pymunk-cp310-cp310-emscripten_3_1_14_wasm32.whl is it possible to load it dynamically in pygame-web?

@pmp-p
Copy link
Author

pmp-p commented Aug 25, 2022

There is no support for 3.10 i made a very quick and really dirty backport of 3.11 patches, but yes numpy from pyodide and pymunk can and could be loaded dynamically. But we already found some flaws when using numpy so i fear it could be the same at runtime with pymunk.

if you are stuck with docker build system maybe you would just incorporate some wasm changes so i can make an automatic build with pygbag github CI on next version ( which will have some wheels ) ?

This is the tree I used for running stuncat game https://github.com/pmp-p/pymunk-wasm/tree/pymunk-wasm. Maybe you can sort out my hacks. it should be easy to fence because when building for wasm sys.platform is equal to "emscripten".

You could also have to look to https://github.com/olivi-r/wasmpy-build it seems very promising.

@viblo
Copy link
Owner

viblo commented Aug 25, 2022

Ah, right, pyodide still doesnt support 3.11, while pygame-web build doesnt support 3.10. Hopefully pyodide will catch up soon, if not earlier then at least when 3.11 is released fully.

Oh, looking at stuntcat I can see that it uses a older version of Pymunk, the building has changes quite a lot since then. The more recent Pymunk versions uses the standard CFFI way of building, so there is no separate lib/dll anymore, only a pyd extension file. I would not be surprised if it works just out of the box actually, especially since it could build for pyodide without modifications. If you feel like it it would be great if you could try to build it. (I have not fully given up to do it myself either, maybe I will have time to do another try tomorrow or so).

In the short term my goal is to get something in place before pyweek starts, to both allow myself and possibly other to use it. But maybe that's too ambitious. On the little longer perspective it would be really cool to be able to have some live in-browser demos running on the pymynk webpage. For example the logo animation it has on the first page or some of the examples.

@pmp-p
Copy link
Author

pmp-p commented Feb 16, 2023

right cffi module builder works just fine, wheel built upon git code is in test

@robertpfeiffer suggested to add -stripped option to the build so wasm wheel can be exempted of "examples" and "tests" excess weight

@viblo
Copy link
Owner

viblo commented Feb 16, 2023

Ah, this sounds like a great idea. I will look into it.

@viblo
Copy link
Owner

viblo commented Feb 19, 2023

Some notes: If I just try to remove test and examples from the whl, its goes from 245KB for all, to 194KB without examples and 174KB without examples and tests.
The examples are quite useless on web at the moment I think, so they could be removed fully without issue. Tests are useful to validate that it does work, so if removed would need to be able to be imported in some other way.

What Im not sure about is how to do this the best (and easiest) way. For the non-web wheels I think its quite nice to have it included by default.

@pmp-p
Copy link
Author

pmp-p commented Feb 21, 2023

so if removed would need to be able to be imported in some other way.

Got an idea for that, since testing in the browser requires a specific async runner, why not release a test suite tailored for wasm as a pygbag apk zip on github release, and put only a loader ( probably less than 20 lines ! ) in the wheel
pymunk.test would download the zip for the release, mount and run testsuite from main.py.
( and as a pygbag app testsuite could also be run from github pages of pymunk directly )

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

2 participants