Skip to content

Commit 3f35d2c

Browse files
Emscripten bugfixes/nits (#18562)
1 parent 566b9bd commit 3f35d2c

25 files changed

Lines changed: 842 additions & 320 deletions

Makefile.emscripten

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ TARGET_BASE := $(subst .js,,$(TARGET))
1212
OS = Emscripten
1313
OBJ :=
1414
DEFINES := -DRARCH_INTERNAL -DHAVE_MAIN -DEMSCRIPTEN
15-
DEFINES += -DHAVE_FILTERS_BUILTIN -DHAVE_ONLINE_UPDATER -DHAVE_UPDATE_ASSETS -DHAVE_UPDATE_CORE_INFO
15+
DEFINES += -DHAVE_FILTERS_BUILTIN -DHAVE_ONLINE_UPDATER -DHAVE_UPDATE_ASSETS -DHAVE_UPDATE_CORE_INFO -DHAVE_STB_VORBIS
1616

1717
HAVE_STATESTREAM ?= 1
1818
HAVE_PATCH = 1
@@ -26,7 +26,8 @@ HAVE_UPDATE_ASSETS = 1
2626
HAVE_ONLINE_UPDATER = 1
2727
HAVE_GLSL = 1
2828
HAVE_SCREENSHOTS = 1
29-
HAVE_REWIND = 1
29+
HAVE_REWIND ?= 1
30+
HAVE_RUNAHEAD ?= 1
3031
HAVE_AUDIOMIXER = 1
3132
HAVE_CC_RESAMPLER ?= 1
3233
HAVE_EGL ?= 0
@@ -51,7 +52,7 @@ HAVE_CHEATS = 1
5152
HAVE_IBXM = 1
5253
HAVE_CORE_INFO_CACHE = 1
5354
HAVE_7ZIP = 1
54-
HAVE_BSV_MOVIE = 1
55+
HAVE_BSV_MOVIE ?= 1
5556
HAVE_CHD ?= 0
5657
HAVE_NETPLAYDISCOVERY ?= 0
5758

@@ -65,39 +66,55 @@ HAVE_AUDIOWORKLET ?= 0
6566
# doesn't work on PROXY_TO_PTHREAD
6667
HAVE_RWEBAUDIO ?= 1
6768

68-
# requires ASYNC or PROXY_TO_PTHREAD
69+
# requires ASYNC or PROXY_TO_PTHREAD. Prefer another driver.
6970
HAVE_AL ?= 0
7071

7172
# whether the browser thread is allowed to block to wait for audio to play, not CPU usage-friendly!
7273
# currently this variable is only used by rwebaudio and audioworklet; openal will never busywait.
7374
ALLOW_AUDIO_BUSYWAIT ?= 0
7475

75-
# minimal asyncify; better performance than full asyncify,
76-
# but sleeping on the main thread is only possible in some places.
76+
# asyncify: enable fibers and sleeping on the browser thread,
77+
# this has a noticeable impact on compile time, output wasm size, and speed.
78+
# see https://emscripten.org/docs/porting/asyncify.html for more info
79+
ASYNC ?= 0
80+
81+
# minimal asyncify: better performance than full asyncify,
82+
# but sleeping on the browser thread is only possible in some places.
7783
MIN_ASYNC ?= 0
7884

79-
# runs RetroArch on a pthread instead of the browser thread; requires HAVE_THREADS
85+
# JSPI: experimental asyncify alternative, see https://webassembly.org/features/
86+
# currently (emscripten 4.0.15) this requires a patched emscripten toolchain.
87+
JSPI ?= 0
88+
89+
# runs RetroArch on a pthread instead of the browser thread; implies HAVE_THREADS
8090
PROXY_TO_PTHREAD ?= 0
8191

8292
# recommended FS when using HAVE_THREADS
8393
HAVE_WASMFS ?= 0
8494

85-
# enables OPFS (origin private file system) and FETCHFS, requires PROXY_TO_PTHREAD
95+
# enables OPFS (origin private file system) and FETCHFS, requires PROXY_TO_PTHREAD or JSPI
8696
HAVE_EXTRA_WASMFS ?= 0
8797

98+
# use the closure compiler to further minify output JS
99+
CLOSURE_COMPILER ?= 0
100+
88101
# enable javascript filesystem tracking, incompatible with HAVE_WASMFS
89102
FS_DEBUG ?= 0
90103

91104
# help diagnose GL problems (can cause issues in normal operation)
92105
GL_DEBUG ?= 0
93106

94-
# does nothing on its own, but automatically selected by some other options
95-
WASM_WORKERS = 0
107+
# enable runtime assertions (always enabled in debug builds)
108+
# please use this when testing new emscripten-specific features!
109+
ASSERTIONS ?= 0
96110

97-
HAVE_OPENGLES ?= 1
111+
# enable GLES 3.0 (WebGL 2).
112+
# if left disabled, GLES 2.0 (WebGL 1) will be used.
98113
HAVE_OPENGLES3 ?= 0
99114

100-
ASYNC ?= 0
115+
# does nothing on its own, but automatically selected by some other options
116+
WASM_WORKERS = 0
117+
101118
LTO ?= 0
102119
PTHREAD_POOL_SIZE ?= 4
103120

@@ -136,7 +153,10 @@ ifeq ($(HAVE_EXTRA_WASMFS), 1)
136153
DEFINES += -DHAVE_EXTRA_WASMFS
137154
override HAVE_WASMFS = 1
138155
ifeq ($(PROXY_TO_PTHREAD), 0)
139-
$(error ERROR: HAVE_EXTRA_WASMFS requires PROXY_TO_PTHREAD)
156+
# note: currently HAVE_EXTRA_WASMFS+JSPI requires ASSERTIONS to be turned off
157+
ifeq ($(JSPI), 0)
158+
$(error ERROR: HAVE_EXTRA_WASMFS requires PROXY_TO_PTHREAD or JSPI)
159+
endif
140160
endif
141161
endif
142162

@@ -154,6 +174,7 @@ ifeq ($(PROXY_TO_PTHREAD), 1)
154174
override STACK_SIZE = 4194304
155175
else ifeq ($(HAVE_AL), 1)
156176
override ASYNC = 1
177+
override MIN_ASYNC = 0
157178
endif
158179

159180
ifeq ($(HAVE_SDL2), 1)
@@ -167,7 +188,7 @@ LDFLAGS := -L. --no-heap-copy -s STACK_SIZE=$(STACK_SIZE) -s INITIAL_MEMORY=$(IN
167188
-s MODULARIZE=1 -s EXPORT_ES6=1 -s EXPORT_NAME="libretro_$(subst -,_,$(LIBRETRO))" \
168189
-s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=0 \
169190
-s ENVIRONMENT=web,worker -s WASM_BIGINT=1 \
170-
--extern-pre-js emscripten/pre.js \
191+
--extern-pre-js emscripten/extern_pre.js \
171192
--js-library emscripten/library_rwebcam.js \
172193
--js-library emscripten/library_platform_emscripten.js
173194

@@ -181,7 +202,6 @@ endif
181202

182203
ifeq ($(GL_DEBUG), 1)
183204
LDFLAGS += -s GL_ASSERTIONS=1 -s GL_DEBUG=1
184-
DEFINES += -DHAVE_GL_DEBUG_ES=1
185205
endif
186206

187207
ifeq ($(FS_DEBUG), 1)
@@ -211,6 +231,7 @@ ifeq ($(HAVE_AL), 1)
211231
endif
212232

213233
ifeq ($(PROXY_TO_PTHREAD), 1)
234+
else ifeq ($(JSPI), 1)
214235
else ifeq ($(ASYNC), 1)
215236
else
216237
DEFINES += -DEMSCRIPTEN_AUDIO_EXTERNAL_BLOCK
@@ -245,18 +266,26 @@ ifeq ($(WASM_WORKERS), 1)
245266
LDFLAGS += -s WASM_WORKERS=1
246267
endif
247268

248-
ifeq ($(ASYNC), 1)
249-
DEFINES += -DEMSCRIPTEN_ASYNCIFY -DEMSCRIPTEN_FULL_ASYNCIFY
250-
LDFLAGS += -s ASYNCIFY=1 -s ASYNCIFY_STACK_SIZE=8192
251-
ifeq ($(DEBUG), 1)
252-
#LDFLAGS += -s ASYNCIFY_DEBUG=1 # broken?
253-
endif
269+
ifeq ($(JSPI), 1)
270+
CFLAGS += -fwasm-exceptions
271+
DEFINES += -DEMSCRIPTEN_ASYNCIFY -DEMSCRIPTEN_FULL_ASYNCIFY -DEMSCRIPTEN_JSPI
272+
LDFLAGS += -s JSPI=1 -fwasm-exceptions
254273
else ifeq ($(MIN_ASYNC), 1)
255274
DEFINES += -DEMSCRIPTEN_ASYNCIFY -DEMSCRIPTEN_MIN_ASYNCIFY
256275
LDFLAGS += -s ASYNCIFY=1 -s ASYNCIFY_STACK_SIZE=8192 -s ASYNCIFY_IGNORE_INDIRECT=1 -s ASYNCIFY_ADD='$(ASYNCIFY_ADD)' -s ASYNCIFY_REMOVE='$(ASYNCIFY_REMOVE)'
257276
ifeq ($(DEBUG), 1)
258277
LDFLAGS += -s ASYNCIFY_ADVISE #-s ASYNCIFY_DEBUG=1
259278
endif
279+
else ifeq ($(ASYNC), 1)
280+
DEFINES += -DEMSCRIPTEN_ASYNCIFY -DEMSCRIPTEN_FULL_ASYNCIFY
281+
LDFLAGS += -s ASYNCIFY=1 -s ASYNCIFY_STACK_SIZE=8192
282+
ifeq ($(DEBUG), 1)
283+
#LDFLAGS += -s ASYNCIFY_DEBUG=1 # broken?
284+
endif
285+
endif
286+
287+
ifeq ($(CLOSURE_COMPILER), 1)
288+
LDFLAGS += --closure 1 --closure-args=--externs=emscripten/closure_externs.js --post-js emscripten/closure_post.js
260289
endif
261290

262291
include Makefile.common
@@ -281,9 +310,15 @@ ifneq ($(V), 1)
281310
endif
282311

283312
ifeq ($(DEBUG), 1)
284-
LDFLAGS += -O0 -g -gsource-map -s SAFE_HEAP=2 -s STACK_OVERFLOW_CHECK=2 -s ASSERTIONS=1
313+
LDFLAGS += -O0 -g -gsource-map -s STACK_OVERFLOW_CHECK=2
285314
# -O0 in cflags gives "too many locals" errors
286315
CFLAGS += -O1 -g -gsource-map
316+
override ASSERTIONS = 1
317+
# "WASM_WORKERS is not currently compatible with `-fsanitize` tools"
318+
ifeq ($(WASM_WORKERS), 0)
319+
CFLAGS += -fsanitize=undefined
320+
LDFLAGS += -fsanitize=undefined
321+
endif
287322
else
288323
LDFLAGS += -O3
289324
# WARNING: some optimizations can break some cores (ex: LTO breaks tyrquake)
@@ -293,6 +328,10 @@ else
293328
CFLAGS += -O3
294329
endif
295330

331+
ifneq ($(ASSERTIONS), 0)
332+
LDFLAGS += -s ASSERTIONS=$(ASSERTIONS)
333+
endif
334+
296335
CFLAGS += -Wall -I. -Ilibretro-common/include -Ideps/7zip -std=gnu99
297336

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

audio/drivers/rwebaudio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ static void *rwebaudio_init(const char *device, unsigned rate, unsigned latency,
9191
rwebaudio_static_data = rwebaudio;
9292
*new_rate = RWebAudioSampleRate();
9393
rwebaudio->tmpbuf_frames = RWEBAUDIO_BUFFER_SIZE_MS * *new_rate / 1000;
94-
rwebaudio->tmpbuf_left = memalign(sizeof(float), rwebaudio->tmpbuf_frames * sizeof(float));
95-
rwebaudio->tmpbuf_right = memalign(sizeof(float), rwebaudio->tmpbuf_frames * sizeof(float));
94+
rwebaudio->tmpbuf_left = memalign(16, rwebaudio->tmpbuf_frames * sizeof(float));
95+
rwebaudio->tmpbuf_right = memalign(16, rwebaudio->tmpbuf_frames * sizeof(float));
9696
RARCH_LOG("[RWebAudio] Device rate: %d Hz.\n", *new_rate);
9797
RARCH_LOG("[RWebAudio] Buffer size: %lu bytes.\n", RWebAudioBufferSizeFrames() * 2 * sizeof(float));
9898
return rwebaudio;

command.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1927,8 +1927,10 @@ bool command_set_shader(command_t *cmd, const char *arg)
19271927
{
19281928
enum rarch_shader_type type = video_shader_parse_type(arg);
19291929
settings_t *settings = config_get_ptr();
1930+
bool apply_new_shader = !string_is_empty(arg);
19301931

1931-
if (!string_is_empty(arg))
1932+
configuration_set_bool(settings, settings->bools.video_shader_enable, apply_new_shader);
1933+
if (apply_new_shader)
19321934
{
19331935
gfx_ctx_flags_t flags;
19341936
flags.flags = 0;

config.def.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@
868868
#define DEFAULT_MENU_FOOTER_OPACITY 1.000f
869869
#define DEFAULT_MENU_HEADER_OPACITY 1.000f
870870

871-
#if defined(HAVE_OPENGLES2) || (defined(__MACH__) && defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED < 101200))
871+
#if (defined(HAVE_OPENGLES2) && !defined(EMSCRIPTEN)) || (defined(__MACH__) && defined(MAC_OS_X_VERSION_MAX_ALLOWED) && (MAC_OS_X_VERSION_MAX_ALLOWED < 101200))
872872
#define DEFAULT_MENU_SHADER_PIPELINE 1
873873
#else
874874
#define DEFAULT_MENU_SHADER_PIPELINE 2

0 commit comments

Comments
 (0)