Skip to content

Commit a761596

Browse files
committed
workerized RA
1 parent e1b2e29 commit a761596

File tree

10 files changed

+337
-61
lines changed

10 files changed

+337
-61
lines changed

Makefile.common

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1496,7 +1496,10 @@ ifeq ($(HAVE_GL_CONTEXT), 1)
14961496
endif
14971497

14981498
ifeq ($(HAVE_EMSCRIPTEN), 1)
1499-
OBJ += gfx/drivers_context/emscriptenegl_ctx.o
1499+
ifeq ($(HAVE_EGL), 1)
1500+
OBJ += gfx/drivers_context/emscriptenegl_ctx.o
1501+
endif
1502+
OBJ += gfx/drivers_context/emscriptenwebgl_ctx.o
15001503
endif
15011504

15021505
ifeq ($(HAVE_MALI_FBDEV), 1)

Makefile.emscripten

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ HAVE_SCREENSHOTS = 1
2424
HAVE_REWIND = 1
2525
HAVE_AUDIOMIXER = 1
2626
HAVE_CC_RESAMPLER = 1
27-
HAVE_EGL = 1
27+
HAVE_EGL ?= 0
2828
HAVE_OPENGLES = 1
2929
HAVE_RJPEG = 0
3030
HAVE_RPNG = 1
@@ -48,6 +48,8 @@ HAVE_CORE_INFO_CACHE = 1
4848
HAVE_7ZIP = 1
4949
HAVE_BSV_MOVIE = 1
5050
HAVE_AL = 1
51+
HAVE_WASMFS ?= 1
52+
HAVE_WORKER ?= 1
5153

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

70-
PTHREAD ?= 0
72+
PTHREAD ?= 4
7173

7274
MEMORY ?= 134217728
7375

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

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

80-
LIBS := -s USE_ZLIB=1
81-
LDFLAGS := -L. --no-heap-copy -s $(LIBS) -s TOTAL_MEMORY=$(MEMORY) -s NO_EXIT_RUNTIME=0 -s FULL_ES2=1 \
82+
LIBS := -s USE_ZLIB=1 -lGL
83+
84+
ifeq ($(HAVE_SDL2), 1)
85+
LIBS += -s USE_SDL=2
86+
DEFINES += -DHAVE_SDL2
87+
endif
88+
89+
ifeq ($(HAVE_WASMFS), 1)
90+
LIBS += -s WASMFS -s FORCE_FILESYSTEM=1
91+
endif
92+
93+
ifeq ($(HAVE_WORKER), 1)
94+
LIBS += -s PROXY_TO_PTHREAD -s USE_ES6_IMPORT_META=0 -sENVIRONMENT=worker,web
95+
else
96+
ifeq ($(HAVE_AL), 1)
97+
ASYNC = 1
98+
endif
99+
endif
100+
101+
LDFLAGS := -L. --no-heap-copy $(LIBS) -s TOTAL_MEMORY=$(MEMORY) -s NO_EXIT_RUNTIME=0 -s FULL_ES2=1 \
82102
-s "EXPORTED_RUNTIME_METHODS=['callMain', 'FS', 'PATH', 'ERRNO_CODES']" \
83103
-s ALLOW_MEMORY_GROWTH=1 -s "EXPORTED_FUNCTIONS=['_main', '_malloc', '_cmd_savefiles', '_cmd_save_state', '_cmd_load_state', '_cmd_take_screenshot']" \
84104
-s MODULARIZE=1 -s EXPORT_ES6=1 -s EXPORT_NAME="libretro_$(subst -,_,$(LIBRETRO))" \
85-
-s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=1 \
105+
-s DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=0 \
106+
-s OFFSCREENCANVAS_SUPPORT -s MIN_WEBGL_VERSION=2 -s MAX_WEBGL_VERSION=2 \
107+
-s OFFSCREEN_FRAMEBUFFER \
86108
--js-library emscripten/library_errno_codes.js \
87109
--js-library emscripten/library_rwebcam.js
88110

@@ -93,29 +115,25 @@ endif
93115
ifeq ($(HAVE_AL), 1)
94116
LDFLAGS += -lopenal
95117
DEFINES += -DHAVE_AL
96-
ASYNC = 1
97118
endif
98119

99120
ifneq ($(PTHREAD), 0)
100121
LDFLAGS += -s WASM_MEM_MAX=1073741824 -pthread -s PTHREAD_POOL_SIZE=$(PTHREAD)
101-
CFLAGS += -pthread
122+
CFLAGS += -pthread -s SHARED_MEMORY
102123
HAVE_THREADS=1
103124
else
104125
HAVE_THREADS=0
105126
endif
106127

128+
107129
ifeq ($(ASYNC), 1)
130+
DEFINES += -DEMSCRIPTEN_ASYNCIFY
108131
LDFLAGS += -s ASYNCIFY=$(ASYNC) -s ASYNCIFY_STACK_SIZE=8192
109132
ifeq ($(DEBUG), 1)
110133
LDFLAGS += -s ASYNCIFY_DEBUG=1 # -s ASYNCIFY_ADVISE
111134
endif
112135
endif
113136

114-
ifeq ($(HAVE_SDL2), 1)
115-
LIBS += -s USE_SDL=2
116-
DEFINES += -DHAVE_SDL2
117-
endif
118-
119137
include Makefile.common
120138

121139
CFLAGS += $(DEF_FLAGS) -Ideps -Ideps/stb
@@ -125,15 +143,15 @@ libretro :=
125143
ifeq ($(HAVE_STATIC_DUMMY),1)
126144
DEFINES += -DHAVE_STATIC_DUMMY
127145
else
128-
libretro += libretro_emscripten.bc
146+
libretro += libretro_emscripten.a
129147
endif
130148

131149
ifneq ($(V), 1)
132150
Q := @
133151
endif
134152

135153
ifeq ($(DEBUG), 1)
136-
LDFLAGS += -O0 -g -gsource-map -s SAFE_HEAP=1 -s STACK_OVERFLOW_CHECK=2 -s ASSERTIONS=1
154+
LDFLAGS += -O0 -g -gsource-map -s SAFE_HEAP=1 -s STACK_OVERFLOW_CHECK=2 -s ASSERTIONS=1 -GL_DEBUG=1
137155
CFLAGS += -O0 -g -gsource-map -s SAFE_HEAP=1 -s SAFE_HEAP_LOG=1 -s STACK_OVERFLOW_CHECK=2 -s ASSERTIONS=1
138156
else
139157
LDFLAGS += -O3 -s WASM=1

dist-scripts/dist-cores.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ mkdir -p ../pkg/${platform}/build/rom
6161
# Emscripten
6262
elif [ $PLATFORM = "emscripten" ] ; then
6363
platform=emscripten
64-
EXT=bc
64+
EXT=a
6565

6666
if [ -z "$EMSCRIPTEN" ] ; then
6767
echo "run this script with emmake. Ex: emmake $0"
@@ -205,9 +205,6 @@ for f in `ls -v *_${platform}.${EXT}`; do
205205
whole_archive=
206206
big_stack=
207207

208-
if [ $PLATFORM = "emscripten" ]; then
209-
async=1 #emscripten needs async to sleep
210-
fi
211208
if [ $name = "nxengine" ] ; then
212209
echo "Applying whole archive linking..."
213210
whole_archive="WHOLE_ARCHIVE_LINK=1"

frontend/drivers/platform_emscripten.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,9 @@ int main(int argc, char *argv[])
158158
{
159159
dummyErrnoCodes();
160160

161-
EM_ASM({
162-
specialHTMLTargets["!canvas"] = Module.canvas;
163-
});
164-
165-
emscripten_set_canvas_element_size("!canvas", 800, 600);
166-
emscripten_set_element_css_size("!canvas", 800.0, 600.0);
167-
emscripten_set_main_loop(emscripten_mainloop, 0, 0);
168161
rarch_main(argc, argv, NULL);
162+
emscripten_set_main_loop(emscripten_mainloop, 0, 0);
163+
emscripten_set_main_loop_timing(EM_TIMING_RAF, 1);
169164

170165
return 0;
171166
}

gfx/drivers_context/emscriptenegl_ctx.c

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@
2929
#include "../../retroarch.h"
3030
#include "../../verbosity.h"
3131

32-
#ifdef HAVE_EGL
3332
#include "../common/egl_common.h"
34-
#endif
3533

3634
typedef struct
3735
{
38-
#ifdef HAVE_EGL
3936
egl_ctx_data_t egl;
40-
#endif
4137
int initial_width;
4238
int initial_height;
4339
unsigned fb_width;
@@ -70,7 +66,7 @@ static void gfx_ctx_emscripten_get_canvas_size(int *width, int *height)
7066

7167
if (!is_fullscreen)
7268
{
73-
r = emscripten_get_canvas_element_size("!canvas", width, height);
69+
r = emscripten_get_canvas_element_size("#canvas", width, height);
7470

7571
if (r != EMSCRIPTEN_RESULT_SUCCESS)
7672
{
@@ -105,14 +101,14 @@ static void gfx_ctx_emscripten_check_window(void *data, bool *quit,
105101
if ( (input_width != emscripten->fb_width)
106102
|| (input_height != emscripten->fb_height))
107103
{
108-
r = emscripten_set_canvas_element_size("!canvas",
104+
r = emscripten_set_canvas_element_size("#canvas",
109105
input_width, input_height);
110106

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

114110
/* fix Module.requestFullscreen messing with the canvas size */
115-
r = emscripten_set_element_css_size("!canvas",
111+
r = emscripten_set_element_css_size("#canvas",
116112
(double)input_width, (double)input_height);
117113

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

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

139133
static void gfx_ctx_emscripten_get_video_size(void *data,
@@ -155,16 +149,13 @@ static void gfx_ctx_emscripten_destroy(void *data)
155149
if (!emscripten)
156150
return;
157151

158-
#ifdef HAVE_EGL
159152
egl_destroy(&emscripten->egl);
160-
#endif
161153

162154
free(data);
163155
}
164156

165157
static void *gfx_ctx_emscripten_init(void *video_driver)
166158
{
167-
#ifdef HAVE_EGL
168159
unsigned width, height;
169160
EGLint major, minor;
170161
EGLint n;
@@ -183,7 +174,6 @@ static void *gfx_ctx_emscripten_init(void *video_driver)
183174
EGL_CONTEXT_CLIENT_VERSION, 2,
184175
EGL_NONE
185176
};
186-
#endif
187177
emscripten_ctx_data_t *emscripten = (emscripten_ctx_data_t*)
188178
calloc(1, sizeof(*emscripten));
189179

@@ -194,11 +184,10 @@ static void *gfx_ctx_emscripten_init(void *video_driver)
194184
* be grabbed? */
195185
if ( (emscripten->initial_width == 0)
196186
|| (emscripten->initial_height == 0))
197-
emscripten_get_canvas_element_size("!canvas",
187+
emscripten_get_canvas_element_size("#canvas",
198188
&emscripten->initial_width,
199189
&emscripten->initial_height);
200190

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

231219
return emscripten;
232220

@@ -251,10 +239,8 @@ static enum gfx_ctx_api gfx_ctx_emscripten_get_api(void *data) { return GFX_CTX_
251239
static bool gfx_ctx_emscripten_bind_api(void *data,
252240
enum gfx_ctx_api api, unsigned major, unsigned minor)
253241
{
254-
#ifdef HAVE_EGL
255242
if (api == GFX_CTX_OPENGL_ES_API)
256243
return egl_bind_api(EGL_OPENGL_ES_API);
257-
#endif
258244
return false;
259245
}
260246

@@ -283,10 +269,8 @@ static bool gfx_ctx_emscripten_write_egl_image(void *data,
283269

284270
static void gfx_ctx_emscripten_bind_hw_render(void *data, bool enable)
285271
{
286-
#ifdef HAVE_EGL
287272
emscripten_ctx_data_t *emscripten = (emscripten_ctx_data_t*)data;
288273
egl_bind_hw_render(&emscripten->egl, enable);
289-
#endif
290274
}
291275

292276
static uint32_t gfx_ctx_emscripten_get_flags(void *data)
@@ -320,11 +304,7 @@ const gfx_ctx_driver_t gfx_ctx_emscripten = {
320304
false,
321305
gfx_ctx_emscripten_swap_buffers,
322306
gfx_ctx_emscripten_input_driver,
323-
#ifdef HAVE_EGL
324307
egl_get_proc_address,
325-
#else
326-
NULL,
327-
#endif
328308
gfx_ctx_emscripten_init_egl_image_buffer,
329309
gfx_ctx_emscripten_write_egl_image,
330310
NULL,

0 commit comments

Comments
 (0)