nanogui icon indicating copy to clipboard operation
nanogui copied to clipboard

Web Assembly problems

Open paulocoutinhox opened this issue 3 years ago • 19 comments

Hi,

I found this problems when compile to WASM:

1 - This line throw error that GLFW_SCALE_TO_MONITOR don't exists (screen.cpp):

glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);

2 - This method glfwSetWindowContentScaleCallback throw error that don't exists (screen.cpp):

glfwSetWindowContentScaleCallback(m_glfw_window,
        [](GLFWwindow* w, float, float) {
            auto it = __nanogui_screens.find(w);
            if (it == __nanogui_screens.end())
                return;
            Screen* s = it->second;

            s->m_pixel_ratio = get_pixel_ratio(w);
            s->resize_callback_event(s->m_size.x(), s->m_size.y());
        }
    );

3 - Adding a fixed suffix .bc don't work on emsdk:

[ 83%] Linking CXX static library libnanogui.bc
em++: warning: object file output extension (.bc) used for non-object output.  If you meant to build an object file please use `-c, `-r`, or `-shared` [-Wemcc]
wasm-ld: error: unknown file type: _deps/nanogui-build/libnanogui.bc

em++: error: '/Users/paulo/.conan/data/emsdk/3.1.0/_/_/package/cf186363a0bf37e3a91ee26c25ea5d664ac71fa5/bin/upstream/bin/wasm-ld -o bin/nativium.wasm CMakeFiles/nativium.dir/modules/app-core/implementation/cpp/nativium/core/ApplicationCoreImpl.cpp.o CMakeFiles/nativium.dir/modules/app-core/gluecode/generated-src/wasm/nativium/core/NTVCoreApplicationCore.cpp.o CMakeFiles/nativium.dir/modules/support-lib/djinni/cpp/DataRef.cpp.o CMakeFiles/nativium.dir/modules/support-lib/djinni/wasm/DataRef_wasm.cpp.o CMakeFiles/nativium.dir/modules/support-lib/djinni/wasm/djinni_wasm.cpp.o _deps/nanogui-build/libnanogui.bc -L/Users/paulo/.conan/data/emsdk/3.1.0/_/_/package/cf186363a0bf37e3a91ee26c25ea5d664ac71fa5/bin/.emscripten_cache/sysroot/lib/wasm32-emscripten --whole-archive -lembind-rtti --no-whole-archive -lGL -lal -lhtml5 -lstubs-debug -lc-debug -lcompiler_rt -lc++ -lc++abi -lemmalloc -lc_rt -lsockets -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-cxx-exceptions -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --import-undefined --export-if-defined=main --export-if-defined=__start_em_asm --export-if-defined=__stop_em_asm --export-if-defined=fflush --export=emscripten_stack_get_end --export=emscripten_stack_get_free --export=emscripten_stack_init --export=stackSave --export=stackRestore --export=stackAlloc --export=__wasm_call_ctors --export=__errno_location --export=malloc --export=free --export=__cxa_is_pointer_type --export=__cxa_can_catch --export=setThrew --export-table -z stack-size=5242880 --initial-memory=16777216 --no-entry --max-memory=2147483648 --global-base=1024' failed (returned 1)
make[2]: *** [bin/nativium.js] Error 1

paulocoutinhox avatar Feb 04 '22 01:02 paulocoutinhox

I made a PR to fix all and all is working nice now. https://github.com/mitsuba-renderer/nanogui/pull/108

You can use this example to download nanogui with CPM (cmake package manager):

# cpm
include(get_cpm.cmake)

# nanogui
set(NANOGUI_BUILD_EXAMPLES OFF CACHE BOOL " " FORCE)
set(NANOGUI_BUILD_PYTHON  OFF CACHE BOOL " " FORCE)
set(NANOGUI_INSTALL       OFF CACHE BOOL " " FORCE)
set(NANOGUI_BUILD_SHARED  OFF CACHE BOOL " " FORCE)
set(NANOGUI_BUILD_GLFW    OFF CACHE BOOL " " FORCE)
set(NANOGUI_BACKEND       "GLES 3" CACHE STRING " " FORCE)

CPMAddPackage(
    NAME
    nanogui
    GIT_TAG
    wasm-fix
    GITHUB_REPOSITORY
    paulo-coutinho/nanogui
)

# add nanogui as project library
list(APPEND NATIVIUM_LIBRARY_LINKS "nanogui")

# add nano gui include as search path
nativium_add_search_path("${nanogui_SOURCE_DIR}/include")

And at least:

# wasm
set_target_properties(MY_PROJECT_NAME PROPERTIES LINK_FLAGS "-s EXPORTED_RUNTIME_METHODS=['ccall','cwrap'] --bind -s MALLOC=emmalloc -s WASM_BIGINT=1 -s USE_GLFW=3 -s ALLOW_MEMORY_GROWTH=1")

paulocoutinhox avatar Feb 04 '22 15:02 paulocoutinhox

My demo: https://nativium-nanogui.s3.amazonaws.com/demo/1.0.0/index.html

paulocoutinhox avatar Feb 04 '22 21:02 paulocoutinhox

DEAD ... Found a better way... See next comment.

After much pain I was able to get this to build on OSX also...

  1. You have to correct an issue with emscripten a. Open your version of emcc.py b. Find the run function ... "def run (args):" c Add these lines to the very top of the run function... Because of clang being installed these args are being passed to the emcc (em++) compilers and they don't don't how to handle them as of version 3.14. This just removes the illegal args.
    for argv in [ '-Wl,-search_paths_first', '-Wl,-headerpad_max_install_names' ]:
    if argv in args:
      args.remove (argv)
d. Hard part is done. 
  1. Go into the nanogui directory.
  2. Create a new directory called "web", or something appropriate; then cd to the directory.
  3. cmake .. -DCMAKE_CXX_COMPILER=em++ -DCMAKE_C_COMPILER=emcc -Wno-dev
  4. make
  5. python3 -m http.server
  6. In a webbrowser - http://127.0.0.1:8000/example1.html

mischievous avatar Jul 13 '22 18:07 mischievous

Follow the instructions from this page... (summarized) https://emscripten.org/docs/getting_started/downloads.html

  1. git clone https://github.com/emscripten-core/emsdk.git
  2. cd emsdk
  3. git pull
  4. ./emsdk install latest (3.1.34)
  5. ./emsdk activate latest
  6. source <path_to_emsdk_env.sh>

Checkout nanogui

  1. cd nanogui
  2. vi CMakelists.txt a. change set(CMAKE_EXECUTABLE_SUFFIX ".bc") -> set(CMAKE_EXECUTABLE_SUFFIX ".html") :: (optional, but I could not figure out how to run with out an *.html file) b. change set(CMAKE_CXX_CREATE_STATIC_LIBRARY "<CMAKE_CXX_COMPILER> -o <TARGET> <LINK_FLAGS> <OBJECTS>") -> set(CMAKE_CXX_CREATE_STATIC_LIBRARY "<CMAKE_CXX_COMPILER> -r -o <TARGET> <LINK_FLAGS> <OBJECTS>") :: Will no produce a static library without it. c. add add_compile_options(-Wno-unused-command-line-argument) :: removes some warnings.
  3. mkdir web
  4. cd web
  5. CXXFLAGS='-s USE_WEBGL2=1 -s USE_GLFW=3 -s WASM=1' emcmake cmake ..; emmake make
  6. python3 -m http.server

mischievous avatar Mar 15 '23 21:03 mischievous