Skip to content

Commit

Permalink
workerized RA
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeOsborn committed Nov 23, 2024
1 parent e1b2e29 commit a761596
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 61 deletions.
5 changes: 4 additions & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,10 @@ ifeq ($(HAVE_GL_CONTEXT), 1)
endif

ifeq ($(HAVE_EMSCRIPTEN), 1)
OBJ += gfx/drivers_context/emscriptenegl_ctx.o
ifeq ($(HAVE_EGL), 1)
OBJ += gfx/drivers_context/emscriptenegl_ctx.o
endif
OBJ += gfx/drivers_context/emscriptenwebgl_ctx.o
endif

ifeq ($(HAVE_MALI_FBDEV), 1)
Expand Down
46 changes: 32 additions & 14 deletions Makefile.emscripten
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ HAVE_SCREENSHOTS = 1
HAVE_REWIND = 1
HAVE_AUDIOMIXER = 1
HAVE_CC_RESAMPLER = 1
HAVE_EGL = 1
HAVE_EGL ?= 0
HAVE_OPENGLES = 1
HAVE_RJPEG = 0
HAVE_RPNG = 1
Expand All @@ -48,6 +48,8 @@ HAVE_CORE_INFO_CACHE = 1
HAVE_7ZIP = 1
HAVE_BSV_MOVIE = 1
HAVE_AL = 1
HAVE_WASMFS ?= 1
HAVE_WORKER ?= 1

# WARNING -- READ BEFORE ENABLING
# The rwebaudio driver is known to have several audio bugs, such as
Expand All @@ -67,7 +69,7 @@ ifeq ($(LIBRETRO), tyrquake)
LTO = 0
endif

PTHREAD ?= 0
PTHREAD ?= 4

MEMORY ?= 134217728

Expand All @@ -77,12 +79,32 @@ OBJDIR := obj-emscripten

#if you compile with SDL2 flag add this Emscripten flag "-s USE_SDL=2" to LDFLAGS:

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 \
LIBS := -s USE_ZLIB=1 -lGL

ifeq ($(HAVE_SDL2), 1)
LIBS += -s USE_SDL=2
DEFINES += -DHAVE_SDL2
endif

ifeq ($(HAVE_WASMFS), 1)
LIBS += -s WASMFS -s FORCE_FILESYSTEM=1
endif

ifeq ($(HAVE_WORKER), 1)
LIBS += -s PROXY_TO_PTHREAD -s USE_ES6_IMPORT_META=0 -sENVIRONMENT=worker,web
else
ifeq ($(HAVE_AL), 1)
ASYNC = 1
endif
endif

LDFLAGS := -L. --no-heap-copy $(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']" \
-s MODULARIZE=1 -s EXPORT_ES6=1 -s EXPORT_NAME="libretro_$(subst -,_,$(LIBRETRO))" \
-s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1 \
-s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=0 \
-s OFFSCREENCANVAS_SUPPORT -s MIN_WEBGL_VERSION=2 -s MAX_WEBGL_VERSION=2 \
-s OFFSCREEN_FRAMEBUFFER \
--js-library emscripten/library_errno_codes.js \
--js-library emscripten/library_rwebcam.js

Expand All @@ -93,29 +115,25 @@ endif
ifeq ($(HAVE_AL), 1)
LDFLAGS += -lopenal
DEFINES += -DHAVE_AL
ASYNC = 1
endif

ifneq ($(PTHREAD), 0)
LDFLAGS += -s WASM_MEM_MAX=1073741824 -pthread -s PTHREAD_POOL_SIZE=$(PTHREAD)
CFLAGS += -pthread
CFLAGS += -pthread -s SHARED_MEMORY
HAVE_THREADS=1
else
HAVE_THREADS=0
endif


ifeq ($(ASYNC), 1)
DEFINES += -DEMSCRIPTEN_ASYNCIFY
LDFLAGS += -s ASYNCIFY=$(ASYNC) -s ASYNCIFY_STACK_SIZE=8192
ifeq ($(DEBUG), 1)
LDFLAGS += -s ASYNCIFY_DEBUG=1 # -s ASYNCIFY_ADVISE
endif
endif

ifeq ($(HAVE_SDL2), 1)
LIBS += -s USE_SDL=2
DEFINES += -DHAVE_SDL2
endif

include Makefile.common

CFLAGS += $(DEF_FLAGS) -Ideps -Ideps/stb
Expand All @@ -125,15 +143,15 @@ libretro :=
ifeq ($(HAVE_STATIC_DUMMY),1)
DEFINES += -DHAVE_STATIC_DUMMY
else
libretro += libretro_emscripten.bc
libretro += libretro_emscripten.a
endif

ifneq ($(V), 1)
Q := @
endif

ifeq ($(DEBUG), 1)
LDFLAGS += -O0 -g -gsource-map -s SAFE_HEAP=1 -s STACK_OVERFLOW_CHECK=2 -s ASSERTIONS=1
LDFLAGS += -O0 -g -gsource-map -s SAFE_HEAP=1 -s STACK_OVERFLOW_CHECK=2 -s ASSERTIONS=1 -GL_DEBUG=1
CFLAGS += -O0 -g -gsource-map -s SAFE_HEAP=1 -s SAFE_HEAP_LOG=1 -s STACK_OVERFLOW_CHECK=2 -s ASSERTIONS=1
else
LDFLAGS += -O3 -s WASM=1
Expand Down
5 changes: 1 addition & 4 deletions dist-scripts/dist-cores.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mkdir -p ../pkg/${platform}/build/rom
# Emscripten
elif [ $PLATFORM = "emscripten" ] ; then
platform=emscripten
EXT=bc
EXT=a

if [ -z "$EMSCRIPTEN" ] ; then
echo "run this script with emmake. Ex: emmake $0"
Expand Down Expand Up @@ -205,9 +205,6 @@ for f in `ls -v *_${platform}.${EXT}`; do
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"
Expand Down
9 changes: 2 additions & 7 deletions frontend/drivers/platform_emscripten.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,9 @@ int main(int argc, char *argv[])
{
dummyErrnoCodes();

EM_ASM({
specialHTMLTargets["!canvas"] = Module.canvas;
});

emscripten_set_canvas_element_size("!canvas", 800, 600);
emscripten_set_element_css_size("!canvas", 800.0, 600.0);
emscripten_set_main_loop(emscripten_mainloop, 0, 0);
rarch_main(argc, argv, NULL);
emscripten_set_main_loop(emscripten_mainloop, 0, 0);
emscripten_set_main_loop_timing(EM_TIMING_RAF, 1);

return 0;
}
Expand Down
28 changes: 4 additions & 24 deletions gfx/drivers_context/emscriptenegl_ctx.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@
#include "../../retroarch.h"
#include "../../verbosity.h"

#ifdef HAVE_EGL
#include "../common/egl_common.h"
#endif

typedef struct
{
#ifdef HAVE_EGL
egl_ctx_data_t egl;
#endif
int initial_width;
int initial_height;
unsigned fb_width;
Expand Down Expand Up @@ -70,7 +66,7 @@ static void gfx_ctx_emscripten_get_canvas_size(int *width, int *height)

if (!is_fullscreen)
{
r = emscripten_get_canvas_element_size("!canvas", width, height);
r = emscripten_get_canvas_element_size("#canvas", width, height);

if (r != EMSCRIPTEN_RESULT_SUCCESS)
{
Expand Down Expand Up @@ -105,14 +101,14 @@ static void gfx_ctx_emscripten_check_window(void *data, bool *quit,
if ( (input_width != emscripten->fb_width)
|| (input_height != emscripten->fb_height))
{
r = emscripten_set_canvas_element_size("!canvas",
r = emscripten_set_canvas_element_size("#canvas",
input_width, input_height);

if (r != EMSCRIPTEN_RESULT_SUCCESS)
RARCH_ERR("[EMSCRIPTEN/EGL]: error resizing canvas: %d\n", r);

/* fix Module.requestFullscreen messing with the canvas size */
r = emscripten_set_element_css_size("!canvas",
r = emscripten_set_element_css_size("#canvas",
(double)input_width, (double)input_height);

if (r != EMSCRIPTEN_RESULT_SUCCESS)
Expand All @@ -128,12 +124,10 @@ static void gfx_ctx_emscripten_check_window(void *data, bool *quit,

static void gfx_ctx_emscripten_swap_buffers(void *data)
{
#ifdef HAVE_EGL
/* Doesn't really do anything in WebGL, but it might
* if we use WebGL workers in the future */
emscripten_ctx_data_t *emscripten = (emscripten_ctx_data_t*)data;
egl_swap_buffers(&emscripten->egl);
#endif
}

static void gfx_ctx_emscripten_get_video_size(void *data,
Expand All @@ -155,16 +149,13 @@ static void gfx_ctx_emscripten_destroy(void *data)
if (!emscripten)
return;

#ifdef HAVE_EGL
egl_destroy(&emscripten->egl);
#endif

free(data);
}

static void *gfx_ctx_emscripten_init(void *video_driver)
{
#ifdef HAVE_EGL
unsigned width, height;
EGLint major, minor;
EGLint n;
Expand All @@ -183,7 +174,6 @@ static void *gfx_ctx_emscripten_init(void *video_driver)
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
#endif
emscripten_ctx_data_t *emscripten = (emscripten_ctx_data_t*)
calloc(1, sizeof(*emscripten));

Expand All @@ -194,11 +184,10 @@ static void *gfx_ctx_emscripten_init(void *video_driver)
* be grabbed? */
if ( (emscripten->initial_width == 0)
|| (emscripten->initial_height == 0))
emscripten_get_canvas_element_size("!canvas",
emscripten_get_canvas_element_size("#canvas",
&emscripten->initial_width,
&emscripten->initial_height);

#ifdef HAVE_EGL
if (g_egl_inited)
{
RARCH_LOG("[EMSCRIPTEN/EGL]: Attempted to re-initialize driver.\n");
Expand Down Expand Up @@ -226,7 +215,6 @@ static void *gfx_ctx_emscripten_init(void *video_driver)
emscripten->fb_width = width;
emscripten->fb_height = height;
RARCH_LOG("[EMSCRIPTEN/EGL]: Dimensions: %ux%u\n", width, height);
#endif

return emscripten;

Expand All @@ -251,10 +239,8 @@ static enum gfx_ctx_api gfx_ctx_emscripten_get_api(void *data) { return GFX_CTX_
static bool gfx_ctx_emscripten_bind_api(void *data,
enum gfx_ctx_api api, unsigned major, unsigned minor)
{
#ifdef HAVE_EGL
if (api == GFX_CTX_OPENGL_ES_API)
return egl_bind_api(EGL_OPENGL_ES_API);
#endif
return false;
}

Expand Down Expand Up @@ -283,10 +269,8 @@ static bool gfx_ctx_emscripten_write_egl_image(void *data,

static void gfx_ctx_emscripten_bind_hw_render(void *data, bool enable)
{
#ifdef HAVE_EGL
emscripten_ctx_data_t *emscripten = (emscripten_ctx_data_t*)data;
egl_bind_hw_render(&emscripten->egl, enable);
#endif
}

static uint32_t gfx_ctx_emscripten_get_flags(void *data)
Expand Down Expand Up @@ -320,11 +304,7 @@ const gfx_ctx_driver_t gfx_ctx_emscripten = {
false,
gfx_ctx_emscripten_swap_buffers,
gfx_ctx_emscripten_input_driver,
#ifdef HAVE_EGL
egl_get_proc_address,
#else
NULL,
#endif
gfx_ctx_emscripten_init_egl_image_buffer,
gfx_ctx_emscripten_write_egl_image,
NULL,
Expand Down
Loading

0 comments on commit a761596

Please sign in to comment.