Pulse Engine on WebGL (Unity build)

Has anyone had success running Pulse Engine in a WebGL build (Unity), or otherwise running in a web application?

I have taken a stab at this several times, but I keep getting stuck and not knowing what to do

I first cross compile Pulse with emscripten via this dockcross container. This produces static libraries that I believe are what Unity wants.

I don’t see anything in the Unity documentation, but I believe that since these are static libraries, I have to mark the Pulse C# to Native C++ class with Internal when building for a WebGL platorm. So I changed that line to

public class PulseUnityEngine : PulseEngineBase
{
  #if UNITY_IOS || UNITY_WEBGL_API
    private const string Attribute = "__Internal";
  #else
    private const string Attribute = "PulseC";
  #endif

This is how we get iOS build support. For iOS, I also provide static libraries.

So, now when I build the Pulse VitalsMonitor scene for WebGL, the build seems to be running emscripten on the Pulse static libraries built from the docker contatiner.

But the build soon fails with

Building Library\Bee\artifacts\WebGL\build\debug_WebGL_wasm\build.js failed with output:
wasm-ld: warning: function signature mismatch: PulseHash
>>> defined as (i32) -> f64 in C:\\Users\\AARON~1.BRA\\AppData\\Local\\Temp\\tmpk80puxihGameAssembly.a(bizftqft4zrn.o)
>>> defined as (i32) -> void in C:\\Users\\AARON~1.BRA\\AppData\\Local\\Temp\\tmp9x6dm5f6PulseC.a(PulseEngineC.cpp.o)
[parse exception: attempted pop from empty stack / beyond block start boundary at 26994213 (at 0:26994213)]
Fatal: error in parsing input
emcc2: error: '"C:/Program Files/Unity/Hub/Editor/2021.3.30f1/Editor/Data/PlaybackEngines/WebGLSupport/BuildTools/Emscripten/binaryen\bin\wasm-emscripten-finalize" --minimize-wasm-changes -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 (1)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

It did this for a few other methods, and I just commented those methods out
I am not sure what this error is trying to tell me, and what is wrong with that method.
The method in question is:

extern "C"
C_EXPORT void C_CALL PulseHash(char** hash_str)
{
  std::string hash = PulseBuildInformation::Hash();
  *hash_str = c_strdup(hash.c_str(), hash.length());
}

Which is called from PulseUnityEngine.cs

  [DllImport(Attribute)]
  private static extern double PulseHash(out IntPtr version_str);

Which is not getting a double back…

Maybe I just need to clean up my interfaces…
type type type
Ok, I changed the PulseUnityEngine::PulseHash return to be void
And that got rid of that one error, but there is still other mysterious errors

I guess the next thing too google is that parse exception…

I think its becauase dockcross is on emscripen 3, and unity is using emscripten 2…

Awesome response - Thanks Aaron. I am definitely not worthy on getting this working on my own, but great base for conversation with Kitware tuesday.

Hi Abray,
I’m trying to run it on WEBGL using Unity 6. Unity 6 uses emscripten 3+. Can you provide the compiled files for WEBGL that I can try to run my WEBGL on?
Thanks

Here is a wasm build of Pulse 4.3.2

It should be built with against wasm 4.0.3

Thanks for sharing the files Abray. I was able to build these myself as well. And I also got stuck on the same error you were stuck last time (Stack out of bounds issue). Screenshot attached:

Do you happen to resolve this issue? I searched about it a lot and was able to find that it could be due to struct type mismatch in cpp code. The pulse engine is big enough and I cannot go through it on my own to review all the code.

My reference for the above conclusion is this thread: Unity webgl build error: [parse exception: attempted pop from empty stack / beyond block start boundary] · Issue #21895 · emscripten-core/emscripten · GitHub

Do you have any thoughts on how to solve this? This asset is only usable for us if we can support for iOS, android and WEBGL.

I am not confident that the dockcross version is compatible with what Unity is expecting.
That would the first thing I would investigate.

With that figured out, I would write a super simple C++ library with one function in it, such as sum(float, float) that just adds two numbers together, and see if I could get that into unity using the dockcross build process.

If you can get that figured out, knowing that process was correct, we could turn to compiling the Pulse library to see if we get the same errors.