Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Bug Error when building pre-compiled native library (mujoco) for WebGL

Discussion in 'Scripting' started by winster_unity, May 2, 2024.

  1. winster_unity

    winster_unity

    Joined:
    Mar 8, 2024
    Posts:
    5
    What I'm trying to do

    I'm trying to build mujoco (a native c++ physics library) into a Unity WebGL build. Fortunately, mujoco comes with a Unity plugin; however, the package relies on a DLL, which cannot be used in web builds. To get around this, I've tried pre-compiling the mujoco library from source via emscripten into static library .a files, which is what the Unity docs recommend. I made sure to use the correct emscripten version as well. I place the .a files into my plugins folder under assets, and mujoco has a bindings script that links itself to the .a files. In theory, this should be buildable for WebGL.

    Versions I'm using

    Unity 2022.3.27f1
    Emscripten 3.1.8
    Mujoco 3.1.1

    The error

    Here's the error I get after the WebGL build fails:
    Code (CSharp):
    1. Building Library\Bee\artifacts\WebGL\build\debug_WebGL_wasm\build.js failed with output:
    2. wasm-ld: warning: function signature mismatch: mjv_averageCamera
    3. >>> defined as (i32, i32) -> i32 in C:/Users/winster/projects/MujocoNewEmsdk/Library/Bee/artifacts/WebGL/il2cppOutput/build/GameAssembly.a(09gffyudb6z8.o)
    4. >>> defined as (i32, i32, i32) -> void in Assets/Plugins/build2/lib/libmujoco.a(engine_vis_interact.c.o)
    5.  
    6. wasm-ld: warning: function signature mismatch: mjr_maxViewport
    7. >>> defined as (i32) -> i32 in C:/Users/winster/projects/MujocoNewEmsdk/Library/Bee/artifacts/WebGL/il2cppOutput/build/GameAssembly.a(09gffyudb6z8.o)
    8. >>> defined as (i32, i32) -> void in Assets/Plugins/build2/lib/libmujoco.a(render_gl2.c.o)
    9.  
    10. wasm-ld: warning: function signature mismatch: mjui_themeColor
    11. >>> defined as (i32) -> i32 in C:/Users/winster/projects/MujocoNewEmsdk/Library/Bee/artifacts/WebGL/il2cppOutput/build/GameAssembly.a(09gffyudb6z8.o)
    12. >>> defined as (i32, i32) -> void in Assets/Plugins/build2/lib/libmujoco.a(ui_main.c.o)
    13. [parse exception: attempted pop from empty stack / beyond block start boundary at 8986318 (at 0:8986318)]
    14. Fatal: error in parsing input
    15. emcc: error: '"C:/Program Files/Unity/Hub/Editor/2022.3.27f1/Editor/Data/PlaybackEngines/WebGLSupport/BuildTools/Emscripten/binaryen\bin\wasm-emscripten-finalize" -g Library/Bee/artifacts/WebGL/build/debug_WebGL_wasm/build.wasm -o Library/Bee/artifacts/WebGL/build/debug_WebGL_wasm/build.wasm --detect-features' failed (returned 1)
    16.  
    What I've tried

    I've been able to fix the signature mismatches by manually editing them in the mujoco bindings script. However, the

    [parse exception: attempted pop from empty stack / beyond block start boundary at 8986318 (at 0:8986318)]
    Fatal: error in parsing input
    will not go away. I've tried different Unity versions, different build options, recompiled everything from source, and no luck. Everything results in this odd parse exception.

    I'd be more than happy to post any additional information or stack traces. I know this is pretty niche, so any feedback would be super helpful!
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,516
    Assuming this code is as it says fully ANSI C and doesn't depend on other libraries that cause it not to function on the Web eg I saw OpenGL and Python - they seem to be optional so make sure they are not compiled into the library.

    As to the error it's curious that it adds another int32 to each method call. This could indicate perhaps that a preprocessor directive is different between your library build and the il2cpp build. Check in those source files if there are multiple definitions of these methods with one additional int32 parameter or whether one of these parameters is dependent on a preprocessor symbol (eg DEBUG).

    I'm also thinking calling conventions mismatch but can't recall whether this could lead to additional/omitted parameters.
     
    Bunny83 likes this.
  3. winster_unity

    winster_unity

    Joined:
    Mar 8, 2024
    Posts:
    5
    Thanks for looking into this!

    Re: the python and OpenGL libraries, I've turned those off in the build config so they shouldn't be getting bundled into the build for Unity.

    I am a bit stumped on why it keeps adding an additional int32 to the function arguments. I've checked the file it references (and all project files), and each definition does not contain the additional int32. They are also not returning void anywhere, as it says. Somewhere along the build cycle they're getting altered, and I suspect that's at the root of the issue.