From d1d08e47049257060139bba5184918afe6dc41ca Mon Sep 17 00:00:00 2001 From: falkTX Date: Sat, 9 Jul 2022 23:11:02 +0100 Subject: [PATCH] More wasm details, deal with requirements for -sMAIN_MODULE Signed-off-by: falkTX --- Makefile | 1 - carla | 2 +- dpf | 2 +- include/common.hpp | 5 ++++- include/linux-compat/execinfo.h | 5 +---- plugins/Makefile | 32 ++++++++++++++++++++--------- src/CardinalCommon.cpp | 2 +- src/CardinalPlugin.cpp | 22 +++++++++++--------- src/CardinalUI.cpp | 24 ++++++++++++---------- src/Makefile | 12 +++++++++++ src/Makefile.cardinal.mk | 9 ++++++++- src/emscripten/WasmUtils.cpp | 36 +++++++++++++++++++++++++++++++++ 12 files changed, 112 insertions(+), 40 deletions(-) create mode 100644 src/emscripten/WasmUtils.cpp diff --git a/Makefile b/Makefile index 8107d22..aefe6b1 100644 --- a/Makefile +++ b/Makefile @@ -64,7 +64,6 @@ DGL_EXTRA_ARGS = \ NVG_DISABLE_SKIPPING_WHITESPACE=true \ NVG_FONT_TEXTURE_FLAGS=NVG_IMAGE_NEAREST \ USE_NANOVG_FBO=true \ - WASM_EXCEPTIONS=true \ WINDOWS_ICON_ID=401 # -------------------------------------------------------------- diff --git a/carla b/carla index b8fa8f2..7a9b7ba 160000 --- a/carla +++ b/carla @@ -1 +1 @@ -Subproject commit b8fa8f2faa42f69f6093a03fafb86b374ed46145 +Subproject commit 7a9b7ba35e261f519b4ab38b7f506e2745998e90 diff --git a/dpf b/dpf index b9e654c..02216ab 160000 --- a/dpf +++ b/dpf @@ -1 +1 @@ -Subproject commit b9e654c3d331933dbeae0413246be8dc2bd64a58 +Subproject commit 02216aba747685fe3d8c1e7b95d7fc008249fee2 diff --git a/include/common.hpp b/include/common.hpp index 94102b0..4de06be 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -30,16 +30,19 @@ #include_next "common.hpp" -// Workaround for wrong file permissions from zstd extraction +// Workaround for wrong file permissions from zstd extraction and system usage #ifdef __EMSCRIPTEN__ #define fopen fopen_wasm +#define system system_wasm extern "C" { FILE* fopen_wasm(const char* filename, const char* mode); +inline int system_wasm(const char*) { return 0; } } namespace std { using ::fopen_wasm; + using ::system_wasm; } #endif diff --git a/include/linux-compat/execinfo.h b/include/linux-compat/execinfo.h index 02b7f77..c569c92 100644 --- a/include/linux-compat/execinfo.h +++ b/include/linux-compat/execinfo.h @@ -17,16 +17,13 @@ #pragma once -#ifdef __HAIKU__ -// these are missing from HaikuOS #define pthread_setname_np(...) -static int pthread_getcpuclockid(pthread_t, clockid_t* const clock_id) +int pthread_getcpuclockid(pthread_t, clockid_t* const clock_id) { *clock_id = CLOCK_REALTIME; return 0; } -#endif static int backtrace(void**, int) { diff --git a/plugins/Makefile b/plugins/Makefile index 1e222c6..5b38c12 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -515,7 +515,7 @@ PLUGIN_FILES += $(filter-out Bidoo/src/plugin.cpp Bidoo/src/ANTN.cpp,$(wildcard PLUGIN_FILES += $(wildcard Bidoo/src/dep/*.cpp) PLUGIN_FILES += $(wildcard Bidoo/src/dep/filters/*.cpp) PLUGIN_FILES += $(wildcard Bidoo/src/dep/freeverb/*.cpp) -PLUGIN_FILES += $(wildcard Bidoo/src/dep/lodepng/*.cpp) +PLUGIN_FILES += $(filter-out Bidoo/src/dep/lodepng/pngdetail.cpp,$(wildcard Bidoo/src/dep/lodepng/*.cpp)) PLUGIN_FILES += $(filter-out Bidoo/src/dep/resampler/main.cpp,$(wildcard Bidoo/src/dep/resampler/*.cpp)) PLUGIN_FILES += BidooDark/plugin.cpp @@ -1024,6 +1024,10 @@ BASE_FLAGS += -I../src/Rack/dep/pffft ifeq ($(DEBUG),true) BASE_FLAGS += -UDEBUG +ifeq ($(WASM),true) +# SSE must always be enabled on wasm, even in debug builds +BASE_FLAGS += -msse -msse2 -msse3 -msimd128 +endif endif ifeq ($(HEADLESS),true) @@ -1098,6 +1102,23 @@ ifeq ($(MACOS),true) BASE_FLAGS += -Wno-unknown-warning-option endif +# -------------------------------------------------------------- +# Carla and friends build flags + +CARLA_FLAGS = \ + -DCARLA_BACKEND_NAMESPACE=Cardinal \ + -DREAL_BUILD \ + -DSTATIC_PLUGIN_TARGET \ + -I../deps/aubio/src \ + -I../carla/source/backend \ + -I../carla/source/includes \ + -I../carla/source/modules \ + -I../carla/source/utils + +ifeq ($(WASM),true) +CARLA_FLAGS += -DDISTRHO_RUNNER_INDIRECT_WASM_CALLS +endif + # -------------------------------------------------------------- # Build targets @@ -1314,14 +1335,7 @@ $(BUILD_DIR)/Cardinal/%.cpp.o: Cardinal/%.cpp -Dstbtt_fontinfo=stbtt_fontinfo_cardinal \ -Dstbrp_node=stbrp_node_cardinal \ -Dstbrp_rect=stbrp_rect_cardinal \ - -DCARLA_BACKEND_NAMESPACE=Cardinal \ - -DREAL_BUILD \ - -DSTATIC_PLUGIN_TARGET \ - -I../deps/aubio/src \ - -I../carla/source/backend \ - -I../carla/source/includes \ - -I../carla/source/modules \ - -I../carla/source/utils + $(CARLA_FLAGS) $(BUILD_DIR)/21kHz/%.cpp.o: 21kHz/%.cpp -@mkdir -p "$(shell dirname $(BUILD_DIR)/$<)" diff --git a/src/CardinalCommon.cpp b/src/CardinalCommon.cpp index 7d6dc45..8540ac3 100644 --- a/src/CardinalCommon.cpp +++ b/src/CardinalCommon.cpp @@ -284,7 +284,7 @@ void openBrowser(const std::string& url) window.open(UTF8ToString($0), '_blank'); }, url.c_str()); #else - patchUtils::openBrowser(url); + system::openBrowser(url); #endif } diff --git a/src/CardinalPlugin.cpp b/src/CardinalPlugin.cpp index 6ce52ce..7677bae 100644 --- a/src/CardinalPlugin.cpp +++ b/src/CardinalPlugin.cpp @@ -104,16 +104,18 @@ bool d_isDiffHigherThanLimit(const T& v1, const T& v2, const T& limit) // ----------------------------------------------------------------------------------------------------------- #ifdef DISTRHO_OS_WASM -EM_JS(char*, getPatchStorageSlug, (), { - var searchParams = new URLSearchParams(window.location.search); - var patch = searchParams.get('patchstorage'); - if (!patch) - return null; - var length = lengthBytesUTF8(patch) + 1; - var str = _malloc(length); - stringToUTF8(patch, str, length); - return str; -}); +static char* getPatchStorageSlug() { + return static_cast(EM_ASM_PTR({ + var searchParams = new URLSearchParams(window.location.search); + var patch = searchParams.get('patchstorage'); + if (!patch) + return null; + var length = lengthBytesUTF8(patch) + 1; + var str = _malloc(length); + stringToUTF8(patch, str, length); + return str; + })); +}; #endif // ----------------------------------------------------------------------------------------------------------- diff --git a/src/CardinalUI.cpp b/src/CardinalUI.cpp index e3d0f55..ed18084 100644 --- a/src/CardinalUI.cpp +++ b/src/CardinalUI.cpp @@ -388,20 +388,9 @@ public: #ifdef DISTRHO_OS_WASM if (rack::patchStorageSlug != nullptr) - { - std::string url("/patchstorage.php?slug="); - url += rack::patchStorageSlug; - std::free(rack::patchStorageSlug); - rack::patchStorageSlug = nullptr; - psDialog = new WasmPatchStorageLoadingDialog(); - emscripten_async_wget(url.c_str(), context->patch->templatePath.c_str(), - downloadPatchStorageSucceeded, downloadPatchStorageFailed); - } else - { new WasmWelcomeDialog(); - } #endif context->window->step(); @@ -443,6 +432,19 @@ public: { counterForSelfFocus = -1; getWindow().focus(); + + #ifdef DISTRHO_OS_WASM + if (rack::patchStorageSlug != nullptr) + { + std::string url("/patchstorage.php?slug="); + url += rack::patchStorageSlug; + std::free(rack::patchStorageSlug); + rack::patchStorageSlug = nullptr; + + emscripten_async_wget(url.c_str(), context->patch->templatePath.c_str(), + downloadPatchStorageSucceeded, downloadPatchStorageFailed); + } + #endif } if (filebrowserhandle != nullptr && fileBrowserIdle(filebrowserhandle)) diff --git a/src/Makefile b/src/Makefile index 0c1040f..009cd2f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -64,6 +64,10 @@ BASE_FLAGS += -IRack/dep/pffft ifeq ($(DEBUG),true) BASE_FLAGS += -UDEBUG +ifeq ($(WASM),true) +# SSE must always be enabled on wasm, even in debug builds +BASE_FLAGS += -msse -msse2 -msse3 -msimd128 +endif endif ifeq ($(HAVE_LIBLO),true) @@ -168,6 +172,10 @@ RACK_FILES += $(wildcard Rack/src/*/*.c) RACK_FILES += $(filter-out $(IGNORED_FILES),$(wildcard Rack/src/*.cpp)) RACK_FILES += $(filter-out $(IGNORED_FILES), $(wildcard Rack/src/*/*.cpp)) +ifeq ($(WASM),true) +RACK_FILES += emscripten/WasmUtils.cpp +endif + # -------------------------------------------------------------- # lots of warnings from VCV side @@ -194,6 +202,8 @@ all: $(TARGET) ifeq ($(MOD_BUILD),true) $(MAKE) -C Cardinal lv2 $(MAKE) -C CardinalFX lv2 +else ifeq ($(WASM),true) + $(MAKE) -C CardinalSynth jack else $(MAKE) -C Cardinal $(MAKE) -C CardinalFX $(CARDINAL_FX_ARGS) @@ -244,6 +254,8 @@ $(BUILD_DIR)/%.cpp.o: %.cpp @echo "Compiling $<" $(SILENT)$(CXX) $< $(BUILD_CXX_FLAGS) -c -o $@ +$(BUILD_DIR)/emscripten/WasmUtils.cpp.o: BUILD_CXX_FLAGS += -fno-exceptions + # -------------------------------------------------------------- -include $(RACK_OBJS:%.o=%.d) diff --git a/src/Makefile.cardinal.mk b/src/Makefile.cardinal.mk index 949692c..fb652e9 100644 --- a/src/Makefile.cardinal.mk +++ b/src/Makefile.cardinal.mk @@ -20,7 +20,6 @@ endif ifneq ($(STATIC_BUILD),true) CWD = ../../carla/source -STATIC_PLUGIN_TARGET = true include $(CWD)/Makefile.deps.mk CARLA_BUILD_DIR = ../../carla/build @@ -35,7 +34,9 @@ CARLA_EXTRA_LIBS += $(CARLA_BUILD_DIR)/modules/$(CARLA_BUILD_TYPE)/carla_engine_ CARLA_EXTRA_LIBS += $(CARLA_BUILD_DIR)/modules/$(CARLA_BUILD_TYPE)/carla_plugin.a CARLA_EXTRA_LIBS += $(CARLA_BUILD_DIR)/modules/$(CARLA_BUILD_TYPE)/native-plugins.a CARLA_EXTRA_LIBS += $(CARLA_BUILD_DIR)/modules/$(CARLA_BUILD_TYPE)/audio_decoder.a +ifneq ($(WASM),true) CARLA_EXTRA_LIBS += $(CARLA_BUILD_DIR)/modules/$(CARLA_BUILD_TYPE)/jackbridge.min.a +endif CARLA_EXTRA_LIBS += $(CARLA_BUILD_DIR)/modules/$(CARLA_BUILD_TYPE)/lilv.a CARLA_EXTRA_LIBS += $(CARLA_BUILD_DIR)/modules/$(CARLA_BUILD_TYPE)/rtmempool.a CARLA_EXTRA_LIBS += $(CARLA_BUILD_DIR)/modules/$(CARLA_BUILD_TYPE)/sfzero.a @@ -114,6 +115,11 @@ endif # -------------------------------------------------------------- +# FIXME +ifeq ($(WASM),true) +STATIC_CARLA_PLUGIN_LIBS = -lsndfile -lopus -lFLAC -lvorbisenc -lvorbis -logg -lm +endif + EXTRA_DEPENDENCIES = $(RACK_EXTRA_LIBS) $(CARLA_EXTRA_LIBS) EXTRA_LIBS = $(RACK_EXTRA_LIBS) $(CARLA_EXTRA_LIBS) $(STATIC_CARLA_PLUGIN_LIBS) @@ -206,6 +212,7 @@ BASE_FLAGS += -Wno-unused-variable ifeq ($(WASM),true) LINK_FLAGS += --preload-file=./jsfx +LINK_FLAGS += --preload-file=./lv2 LINK_FLAGS += --preload-file=./resources LINK_FLAGS += -sALLOW_MEMORY_GROWTH LINK_FLAGS += -sINITIAL_MEMORY=64Mb diff --git a/src/emscripten/WasmUtils.cpp b/src/emscripten/WasmUtils.cpp new file mode 100644 index 0000000..09b30ec --- /dev/null +++ b/src/emscripten/WasmUtils.cpp @@ -0,0 +1,36 @@ +/* + * DISTRHO Cardinal Plugin + * Copyright (C) 2021-2022 Filipe Coelho + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 3 of + * the License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For a full copy of the GNU General Public License see the LICENSE file. + */ + +#include + +// ----------------------------------------------------------------------------------------------------------- + +namespace CardinalDISTRHO { + +long d_emscripten_set_interval(void (*cb)(void* userData), double intervalMsecs, void* userData) +{ + return emscripten_set_interval(cb, intervalMsecs, userData); +} + +void d_emscripten_clear_interval(long setIntervalId) +{ + emscripten_clear_interval(setIntervalId); +} + +} + +// -----------------------------------------------------------------------------------------------------------