Skip to content

Commit

Permalink
Emscripten improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
BinBashBanana committed Jan 20, 2025
1 parent 391ba55 commit d9d10d8
Show file tree
Hide file tree
Showing 26 changed files with 1,008 additions and 615 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ retroarch.js
retroarch.js.mem
*.bc
*.wasm
*.wasm.map
obj-emscripten/

# only ignore .js files in the repo root
/*.js
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
- MENU: Add SSL support to the information list
- IOS: Ensure webserver notice can be dismissed
- VIDEO/SHADERS: Allow exact refresh rate sync with shader subframes
- EMSCRIPTEN: Scale window to correct size
- EMSCRIPTEN: Additional platform functions
- EMSCRIPTEN/RWEBINPUT: Add touch input support
- VIDEO/OpenGLES: Fix FP/sRGB FBO support

# 1.20.0
- AUDIO: Fix audio handling in case of RARCH_NETPLAY_CTL_USE_CORE_PACKET_INTERFACE
Expand Down
3 changes: 2 additions & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -1570,10 +1570,11 @@ ifeq ($(HAVE_GL_CONTEXT), 1)
DEF_FLAGS += $(OPENGLES_CFLAGS)
ifeq ($(HAVE_OPENGLES3), 1)
DEFINES += -DHAVE_OPENGLES3
OBJ += $(LIBRETRO_COMM_DIR)/glsym/glsym_es3.o
else
DEFINES += -DHAVE_OPENGLES2
OBJ += $(LIBRETRO_COMM_DIR)/glsym/glsym_es2.o
endif
OBJ += $(LIBRETRO_COMM_DIR)/glsym/glsym_es2.o
else
DEFINES += -DHAVE_GL_SYNC
OBJ += $(LIBRETRO_COMM_DIR)/glsym/glsym_gl.o
Expand Down
97 changes: 62 additions & 35 deletions Makefile.emscripten
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ HAVE_REWIND = 1
HAVE_AUDIOMIXER = 1
HAVE_CC_RESAMPLER = 1
HAVE_EGL = 1
HAVE_OPENGLES = 1
HAVE_RJPEG = 0
HAVE_RPNG = 1
HAVE_EMSCRIPTEN = 1
Expand Down Expand Up @@ -58,34 +57,49 @@ HAVE_CHD ?= 0
# You have been warned.
HAVE_RWEBAUDIO = 0

ASYNC ?= 0
ifeq ($(LIBRETRO), mupen64plus)
ASYNC = 1
endif
# help diagnose GL problems (can cause issues in normal operation)
GL_DEBUG ?= 0

LTO ?= 0
ifeq ($(LIBRETRO), tyrquake)
LTO = 0
endif
# enable javascript filesystem tracking
FS_DEBUG = 1

PTHREAD ?= 0
HAVE_OPENGLES ?= 1
HAVE_OPENGLES3 ?= 0

MEMORY ?= 134217728
ASYNC ?= 0
LTO ?= 0
PTHREAD ?= 0

PRECISE_F32 = 1
STACK_SIZE ?= 4194304
INITIAL_HEAP ?= 134217728

# 4194304 ----- 4 MiB (Stack: recommended)
# 8388608 ----- 8 MiB
# 16777216 ---- 16 MiB
# 33554432 ---- 32 MiB
# 67108864 ---- 64 MiB
# 134217728 --- 128 MiB (Heap: recommended) (Stack: recommended for some cores [mupen64plus_next])
# 268435456 --- 256 MiB (Heap: recommended for some cores [mupen64plus_next])
# 536870912 --- 512 MiB (Heap: needed for some cores [mednafen_psx(_hw)])
# 1073741824 -- 1 GiB
# 1610612736 -- 1.5 GiB
# 2147483648 -- 2 GiB

OBJDIR := obj-emscripten

#if you compile with SDL2 flag add this Emscripten flag "-s USE_SDL=2" to LDFLAGS:
EXPORTED_FUNCTIONS = _main,_malloc,_free,_cmd_savefiles,_cmd_save_state,_cmd_load_state,_cmd_undo_save_state,_cmd_undo_load_state,_cmd_take_screenshot,\
_cmd_toggle_menu,_cmd_reload_config,_cmd_toggle_grab_mouse,_cmd_toggle_game_focus,_cmd_reset,_cmd_toggle_pause,_cmd_pause,_cmd_unpause,\
_cmd_set_volume,_cmd_set_shader,_cmd_cheat_set_code,_cmd_cheat_get_code,_cmd_cheat_toggle_index,_cmd_cheat_get_code_state,_cmd_cheat_realloc,\
_cmd_cheat_get_size,_cmd_cheat_apply_cheats

LIBS := -s USE_ZLIB=1
LDFLAGS := -L. --no-heap-copy -s $(LIBS) -s TOTAL_MEMORY=$(MEMORY) -s NO_EXIT_RUNTIME=0 -s FULL_ES2=1 \
-s "EXPORTED_RUNTIME_METHODS=['callMain', 'FS', 'PATH', 'ERRNO_CODES']" \
-s ALLOW_MEMORY_GROWTH=1 -s "EXPORTED_FUNCTIONS=['_main', '_malloc', '_cmd_savefiles', '_cmd_save_state', '_cmd_load_state', '_cmd_take_screenshot']" \
LDFLAGS := -L. --no-heap-copy -s $(LIBS) -s STACK_SIZE=$(STACK_SIZE) -s INITIAL_MEMORY=$(INITIAL_HEAP) \
-s EXPORTED_RUNTIME_METHODS=callMain,FS,PATH,ERRNO_CODES,stringToNewUTF8,UTF8ToString \
-s ALLOW_MEMORY_GROWTH=1 -s EXPORTED_FUNCTIONS="$(EXPORTED_FUNCTIONS)" \
-s MODULARIZE=1 -s EXPORT_ES6=1 -s EXPORT_NAME="libretro_$(subst -,_,$(LIBRETRO))" \
-s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1 \
--js-library emscripten/library_errno_codes.js \
--js-library emscripten/library_rwebcam.js
--extern-pre-js emscripten/pre.js \
--js-library emscripten/library_rwebcam.js \
--js-library emscripten/library_platform_emscripten.js

ifeq ($(HAVE_RWEBAUDIO), 1)
LDFLAGS += --js-library emscripten/library_rwebaudio.js
Expand All @@ -94,11 +108,11 @@ endif
ifeq ($(HAVE_AL), 1)
LDFLAGS += -lopenal
DEFINES += -DHAVE_AL
ASYNC = 1
override ASYNC = 1
endif

ifneq ($(PTHREAD), 0)
LDFLAGS += -s WASM_MEM_MAX=1073741824 -pthread -s PTHREAD_POOL_SIZE=$(PTHREAD)
LDFLAGS += -s MAXIMUM_MEMORY=1073741824 -pthread -s PTHREAD_POOL_SIZE=$(PTHREAD)
CFLAGS += -pthread
HAVE_THREADS=1
else
Expand All @@ -108,10 +122,26 @@ endif
ifeq ($(ASYNC), 1)
LDFLAGS += -s ASYNCIFY=$(ASYNC) -s ASYNCIFY_STACK_SIZE=8192
ifeq ($(DEBUG), 1)
LDFLAGS += -s ASYNCIFY_DEBUG=1 # -s ASYNCIFY_ADVISE
LDFLAGS += -s ASYNCIFY_DEBUG=1 # -s ASYNCIFY_ADVISE
endif
endif

ifeq ($(HAVE_OPENGLES), 1)
ifeq ($(HAVE_OPENGLES3), 1)
LDFLAGS += -s FULL_ES3=1 -s MIN_WEBGL_VERSION=2 -s MAX_WEBGL_VERSION=2
else
LDFLAGS += -s FULL_ES2=1
endif
endif

ifeq ($(GL_DEBUG), 1)
LDFLAGS += -s GL_ASSERTIONS=1 -s GL_DEBUG=1
endif

ifeq ($(FS_DEBUG), 1)
LDFLAGS += -s FS_DEBUG=1
endif

ifeq ($(HAVE_SDL2), 1)
LIBS += -s USE_SDL=2
DEFINES += -DHAVE_SDL2
Expand All @@ -121,12 +151,14 @@ include Makefile.common

CFLAGS += $(DEF_FLAGS) -Ideps -Ideps/stb

libretro :=
libretro =
libretro_new =

ifeq ($(HAVE_STATIC_DUMMY),1)
DEFINES += -DHAVE_STATIC_DUMMY
else
libretro += libretro_emscripten.bc
libretro = libretro_emscripten.bc
libretro_new = libretro_emscripten.a
endif

ifneq ($(V), 1)
Expand All @@ -135,31 +167,26 @@ endif

ifeq ($(DEBUG), 1)
LDFLAGS += -O0 -g -gsource-map -s SAFE_HEAP=1 -s STACK_OVERFLOW_CHECK=2 -s ASSERTIONS=1
CFLAGS += -O0 -g -gsource-map -s SAFE_HEAP=1 -s SAFE_HEAP_LOG=1 -s STACK_OVERFLOW_CHECK=2 -s ASSERTIONS=1
CFLAGS += -O0 -g -gsource-map
else
LDFLAGS += -O3 -s WASM=1
LDFLAGS += -O3
# WARNING: some optimizations can break some cores (ex: LTO breaks tyrquake)
LDFLAGS += -s PRECISE_F32=$(PRECISE_F32)
ifeq ($(LTO), 1)
LDFLAGS += --llvm-lto 3
LDFLAGS += -flto
endif
CFLAGS += -O3
endif

# 128 * 1024, double the usual emscripten stack size
LDFLAGS += -s STACK_SIZE=131072

LDFLAGS += --extern-pre-js emscripten/pre.js

CFLAGS += -Wall -I. -Ilibretro-common/include -Ideps/7zip -std=gnu99

RARCH_OBJ := $(addprefix $(OBJDIR)/,$(OBJ))

all: $(TARGET)

$(TARGET): $(RARCH_OBJ) $(libretro)
@$(if $(Q), $(shell echo echo LD $@),)
$(Q)$(LD) -o $@ $(RARCH_OBJ) $(libretro) $(LIBS) $(LDFLAGS)
@$(if $(libretro), mv -f $(libretro) $(libretro_new),)
@$(if $(Q), $(shell echo echo "LD $@ \<obj\> $(libretro_new) $(LIBS) $(LDFLAGS)"),)
$(Q)$(LD) -o $@ $(RARCH_OBJ) $(libretro_new) $(LIBS) $(LDFLAGS)

$(OBJDIR)/%.o: %.c
@mkdir -p $(dir $@)
Expand Down
8 changes: 5 additions & 3 deletions cheat_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,13 @@ void cheat_manager_apply_cheats(void)
void cheat_manager_set_code(unsigned i, const char *str)
{
cheat_manager_t *cheat_st = &cheat_manager_state;
if (!cheat_st->cheats)
if (!cheat_st->cheats || string_is_empty(str))
return;

if (!string_is_empty(str))
strcpy(cheat_st->cheats[i].code, str);
if (cheat_st->cheats[i].code)
free(cheat_st->cheats[i].code);

cheat_st->cheats[i].code = strdup(str);

cheat_st->cheats[i].state = true;
}
Expand Down
1 change: 1 addition & 0 deletions command.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ enum event_command
CMD_EVENT_PRESENCE_UPDATE,
CMD_EVENT_OVERLAY_NEXT,
CMD_EVENT_OSK_TOGGLE,
CMD_EVENT_RELOAD_CONFIG,
#ifdef HAVE_MICROPHONE
/* Stops all enabled microphones. */
CMD_EVENT_MICROPHONE_STOP,
Expand Down
3 changes: 3 additions & 0 deletions config.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,9 @@
#define DEFAULT_MENU_TICKER_SPEED 2.0f
#define DEFAULT_MENU_TICKER_SMOOTH true

/* Don't skip rendering assets based on the absence of other assets */
#define DEFAULT_MENU_IGNORE_MISSING_ASSETS false

#if defined(HAVE_THREADS)
#define DEFAULT_MENU_SAVESTATE_RESUME true
#else
Expand Down
1 change: 1 addition & 0 deletions configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,7 @@ static struct config_bool_setting *populate_settings_bool(
SETTING_BOOL("menu_dynamic_wallpaper_enable", &settings->bools.menu_dynamic_wallpaper_enable, true, DEFAULT_MENU_DYNAMIC_WALLPAPER_ENABLE, false);
SETTING_BOOL("menu_ticker_smooth", &settings->bools.menu_ticker_smooth, true, DEFAULT_MENU_TICKER_SMOOTH, false);
SETTING_BOOL("menu_scroll_fast", &settings->bools.menu_scroll_fast, true, DEFAULT_MENU_SCROLL_FAST, false);
SETTING_BOOL("menu_ignore_missing_assets", &settings->bools.menu_ignore_missing_assets, true, DEFAULT_MENU_IGNORE_MISSING_ASSETS, false);

SETTING_BOOL("settings_show_drivers", &settings->bools.settings_show_drivers, true, DEFAULT_SETTINGS_SHOW_DRIVERS, false);
SETTING_BOOL("settings_show_video", &settings->bools.settings_show_video, true, DEFAULT_SETTINGS_SHOW_VIDEO, false);
Expand Down
1 change: 1 addition & 0 deletions configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,7 @@ typedef struct settings
bool menu_disable_left_analog;
bool menu_disable_right_analog;
bool menu_ticker_smooth;
bool menu_ignore_missing_assets;
bool settings_show_drivers;
bool settings_show_video;
bool settings_show_audio;
Expand Down
50 changes: 38 additions & 12 deletions dist-scripts/dist-cores.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ mkdir -p ../pkg/${platform}/build/rom
# Emscripten
elif [ $PLATFORM = "emscripten" ] ; then
platform=emscripten
# todo: change this to a
EXT=bc

if [ -z "$EMSCRIPTEN" ] ; then
Expand Down Expand Up @@ -199,26 +200,42 @@ for f in `ls -v *_${platform}.${EXT}`; do

echo Buildbot: building ${name} for ${platform}
name=`echo "$f" | sed "s/\(_libretro_${platform}\|\).${EXT}$//"`
async=0
pthread=${pthread:-0}
lto=0
whole_archive=
big_stack=

if [ $PLATFORM = "emscripten" ]; then
async=1 #emscripten needs async to sleep
fi
if [ $name = "nxengine" ] ; then
echo "Applying whole archive linking..."
whole_archive="WHOLE_ARCHIVE_LINK=1"
elif [ $name = "tyrquake" ] ; then
echo "Applying big stack..."
lto=0
big_stack="BIG_STACK=1"
elif [ $name = "mupen64plus" ] ; then
async=1
elif [ $name = "dosbox" ] ; then
fi
if [ $PLATFORM = "emscripten" ]; then
async=0
pthread=${pthread:-0}
gles3=0
stack_mem=4194304
heap_mem=134217728
if [ $name = "mupen64plus_next" ] ; then
gles3=1
async=1
stack_mem=134217728
heap_mem=268435456
elif [ $name = "parallel_n64" ] ; then
gles3=1
async=1
elif [ $name = "mednafen_psx" ] ; then
heap_mem=536870912
elif [ $name = "mednafen_psx_hw" ] ; then
gles3=1
heap_mem=536870912
elif [ $name = "dosbox" ] ; then
async=1
elif [ $name = "scummvm" ] ; then
async=1
fi
fi
echo "-- Building core: $name --"
if [ $PLATFORM = "unix" ]; then
Expand All @@ -227,15 +244,21 @@ for f in `ls -v *_${platform}.${EXT}`; do
cp -f "$f" ../libretro_${platform}.${EXT}
fi
echo NAME: $name
echo ASYNC: $async
echo LTO: $lto
if [ $PLATFORM = "emscripten" ]; then
echo ASYNC: $async
echo PTHREAD: $pthread
echo GLES3: $gles3
echo STACK_MEMORY: $stack_mem
echo HEAP_MEMORY: $heap_mem
fi

# Do cleanup if this is a big stack core
if [ "$big_stack" = "BIG_STACK=1" ] ; then
if [ $MAKEFILE_GRIFFIN = "yes" ]; then
make -C ../ -f Makefile.griffin platform=${platform} clean || exit 1
elif [ $PLATFORM = "emscripten" ]; then
make -C ../ -f Makefile.emscripten PTHREAD=$pthread ASYNC=$async LTO=$lto -j7 clean || exit 1
make -C ../ -f Makefile.emscripten PTHREAD=$pthread ASYNC=$async LTO=$lto HAVE_OPENGLES3=$gles3 -j7 clean || exit 1
elif [ $PLATFORM = "unix" ]; then
make -C ../ -f Makefile LINK=g++ LTO=$lto -j7 clean || exit 1
else
Expand All @@ -247,8 +270,8 @@ for f in `ls -v *_${platform}.${EXT}`; do
if [ $MAKEFILE_GRIFFIN = "yes" ]; then
make -C ../ -f Makefile.griffin $OPTS platform=${platform} $whole_archive $big_stack -j3 || exit 1
elif [ $PLATFORM = "emscripten" ]; then
echo "BUILD COMMAND: make -C ../ -f Makefile.emscripten PTHREAD=$pthread ASYNC=$async LTO=$lto -j7 LIBRETRO=${name} TARGET=${name}_libretro.js"
make -C ../ -f Makefile.emscripten $OPTS PTHREAD=$pthread ASYNC=$async LTO=$lto -j7 LIBRETRO=${name} TARGET=${name}_libretro.js || exit 1
echo "BUILD COMMAND: make -C ../ -f Makefile.emscripten PTHREAD=$pthread ASYNC=$async LTO=$lto HAVE_OPENGLES3=$gles3 STACK_SIZE=$stack_mem INITIAL_HEAP=$heap_mem -j7 LIBRETRO=${name} TARGET=${name}_libretro.js"
make -C ../ -f Makefile.emscripten $OPTS PTHREAD=$pthread ASYNC=$async LTO=$lto HAVE_OPENGLES3=$gles3 STACK_SIZE=$stack_mem INITIAL_HEAP=$heap_mem -j7 LIBRETRO=${name} TARGET=${name}_libretro.js || exit 1
elif [ $PLATFORM = "unix" ]; then
make -C ../ -f Makefile LINK=g++ $whole_archive $big_stack -j3 || exit 1
elif [ $PLATFORM = "ctr" ]; then
Expand Down Expand Up @@ -318,6 +341,9 @@ for f in `ls -v *_${platform}.${EXT}`; do
if [ $pthread != 0 ] ; then
mv -f ../${name}_libretro.worker.js ../pkg/emscripten/${name}_libretro.worker.js
fi
if [ -f ../${name}_libretro.wasm.map ] ; then
mv -f ../${name}_libretro.wasm.map ../pkg/emscripten/${name}_libretro.wasm.map
fi
fi

# Do manual executable step
Expand Down
15 changes: 0 additions & 15 deletions emscripten/library_errno_codes.js

This file was deleted.

Loading

0 comments on commit d9d10d8

Please sign in to comment.