not-fl3 / macroquad

Cross-platform game engine in Rust.
Apache License 2.0
3.06k stars 298 forks source link

Wasm build doesn't work on iPhone #416

Open icewind opened 2 years ago

icewind commented 2 years ago

Hi. I'm trying to launch a simple wasm on iPhone and it fails with the following error

ERROR WASM failed to load, probably incompatible gl.js version
ERROR RuntimeError: Out of bounds memory access (evaluating 'a.exports.main()')

Meanwhile it works just fine on desktop(MacOS). I'm using macroquad version 0.3 with the following code

use macroquad::prelude::*;

#[macroquad::main("MQTest")]
async fn main() {
    let mut yspeed = 300.;
    let mut xspeed = 300.;
    let mut xpos = 0.;
    let mut ypos = 0.;

    const SIZE: f32 = 40.;

    let background_color = color_u8!(33, 33, 33, 255);

    loop {
        clear_background(background_color);

        ypos += yspeed * get_frame_time();
        xpos += xspeed * get_frame_time();

        if xpos >= screen_width() - SIZE || xpos <= 0. {
            xspeed = -xspeed;
        }

        if ypos >= screen_height() - SIZE || ypos <= 0. {
            yspeed = -yspeed;
        }

        draw_rectangle(xpos, ypos, SIZE, SIZE, WHITE);

        let current_fps = get_fps();
        draw_text(
            &format!("FPS: {current_fps}"),
            screen_width() - 100.,
            screen_height() - 20.,
            20.,
            WHITE,
        );

        next_frame().await
    }
}
not-fl3 commented 2 years ago

Hmm, what do you mean by "launch a simple wasm on iPhone"?

Are we talking about web build here?

icewind commented 2 years ago

Yep. I'm launching a web build in the mobile browser(Safari and Chrome). The same build work fine in a desktop browser(Chrome, Safari, and FF). By launching I mean I started a web server in my local network and opened the page using the phone. UPDATE: Looks like it is unrelated to any code. Because the empty application crashes with the same error

use macroquad::prelude::*;

#[macroquad::main("MQTest")]
async fn main() {
    loop {
        clear_background(RED);
        next_frame().await
    }
}

I'm also using pretty basic page to launch it

<!DOCTYPE html>
<html lang="en">
    <head>
        <title></title>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <link href="styles/style.css" rel="stylesheet" />
    </head>
    <body>
        <canvas id="glcanvas" tabindex="1" />
    </body>
    <script src="https://not-fl3.github.io/miniquad-samples/mq_js_bundle.js"></script>
    <script>
        load("/target/wasm32-unknown-unknown/debug/mqtest.wasm");
    </script>
</html>
icewind commented 2 years ago

Ok. I found the issue. Using custom optimisation level in Carto.toml the compiler produces wasm binary that is not starting on iOS. For some reason. Removed these lines

[profile.dev.package.'*']
opt-level = 3

Without it the binary compiled with both debug and release modes is working fine on iOS. Btw iOS version is 15.4.1

xav-ie commented 2 years ago

I am facing a similar issue, except removing the said lines or even changing the opt-level to 2 or 1 does not cause it to start, still. It starts just fine on Chrome, Firefox, DuckDuckGo and Brave browsers in iOS; my app does not start on Safari browser in iOS. I am using an iPad, but the issue seems to be something with Safari. There are no console errors, either. I know it is loading blah.wasm because I am running python http.server and it shows every request served, and I can see that safari receives it. I also had to manually update the version of mq_js_bundle.js because the gl.js version is out of date? I don’t know why it won’t work and willing to run tests or anything if that helps

not-fl3 commented 2 years ago

Hmm, any chance you can post the whole browser console output? Maybe there is at least something there?

Does the examples from macroquad.rs work?

https://macroquad.rs/examples/