Emscripten pthread tweaks (#15033)

* add more documentation to emscripten, pthread support, and flag to not show asset warnings

* typo in docs

---------

Co-authored-by: thelamer <ryankuba@gmail.com>
This commit is contained in:
Joe Osborn 2023-02-27 10:56:12 -08:00 committed by GitHub
parent 67372e0f38
commit a1903eee15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 9 deletions

View File

@ -10,12 +10,10 @@ endif
EOPT = USE_ZLIB=1 # Emscripten specific options
EOPTS = $(addprefix -s $(EMPTY), $(EOPT)) # Add '-s ' to each option
PTHREAD = 0
OS = Emscripten
OBJ :=
DEFINES := -DRARCH_INTERNAL -DHAVE_MAIN -s USE_PTHREADS=$(PTHREAD)
DEFINES := -DRARCH_INTERNAL -DHAVE_MAIN
DEFINES += -DHAVE_FILTERS_BUILTIN
HAVE_PATCH = 1
HAVE_DSP_FILTER = 1
HAVE_VIDEO_FILTER = 1
@ -59,6 +57,8 @@ ifeq ($(LIBRETRO), tyrquake)
LTO = 0
endif
PTHREAD ?= 0
MEMORY ?= 134217728
PRECISE_F32 = 1
@ -73,8 +73,10 @@ LDFLAGS := -L. --no-heap-copy -s $(LIBS) -s TOTAL_MEMORY=$(MEMORY) -s NO_EXIT_RU
--js-library emscripten/library_rwebaudio.js \
--js-library emscripten/library_rwebcam.js \
--js-library emscripten/library_errno_codes.js
ifneq ($(PTHREAD), 0)
LDFLAGS += -s USE_PTHREADS=$(PTHREAD) -s PTHREAD_POOL_SIZE=2
LDFLAGS += -s WASM_MEM_MAX=1073741824 -pthread -s PTHREAD_POOL_SIZE=$(PTHREAD)
CFLAGS += -pthread
endif
ifeq ($(ASYNC), 1)
@ -115,8 +117,8 @@ else
CFLAGS += -O3
endif
CFLAGS += -Wall -I. -Ilibretro-common/include -std=gnu99 $(LIBS) \
-s EXPORTED_FUNCTIONS="['_main', '_malloc', '_cmd_savefiles', '_cmd_save_state', '_cmd_take_screenshot']"
CFLAGS += -Wall -I. -Ilibretro-common/include -std=gnu99 $(LIBS) #\
# -s EXPORTED_FUNCTIONS="['_main', '_malloc', '_cmd_savefiles', '_cmd_save_state', '_cmd_take_screenshot']"
RARCH_OBJ := $(addprefix $(OBJDIR)/,$(OBJ))

View File

@ -200,7 +200,7 @@ 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=0
pthread=${pthread:-0}
lto=0
whole_archive=
big_stack=
@ -313,7 +313,7 @@ for f in `ls -v *_${platform}.${EXT}`; do
mv -f ../${name}_libretro.js ../pkg/emscripten/${name}_libretro.js
mv -f ../${name}_libretro.wasm ../pkg/emscripten/${name}_libretro.wasm
if [ $pthread != 0 ] ; then
mv -f ../pthread-main.js ../pkg/emscripten/pthread-main.js
mv -f ../${name}_libretro.worker.js ../pkg/emscripten/${name}_libretro.worker.js
fi
fi

View File

@ -4,7 +4,19 @@ The RetroArch Web Player is RetroArch compiled through [Emscripten](http://kripk
## Compiling
To compile RetroArch with Emscripten, you'll first have to [download and install the Emscripten SDK](http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html). Once it's loaded in your shell, you'll run something like the following...
To compile RetroArch with Emscripten, you'll first have to [download and install the Emscripten SDK](http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html) at 1.39.5:
```
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install 1.39.5
./emsdk activate 1.39.5
source emsdk_env.sh
```
Other later versions of emsdk will function and may be needed (like Async support), but in general emscripten is in a constant state of development and you may run into other problems by not pinning to 1.39.5. This is currently the version [https://web.libretro.com/](https://web.libretro.com/) is built against.
After emsdk is installed you will need to build an emulator core, move that output into Retroarch, and use helper scripts to produce web ready assets, in this example we will be building [https://github.com/libretro/libretro-fceumm](https://github.com/libretro/libretro-fceumm):
```
mkdir ~/retroarch
@ -18,6 +30,13 @@ cd ~/retroarch/RetroArch/dist-scripts
emmake ./dist-cores.sh emscripten
```
The resulting build output will be located in `~/retroarch/RetroArch/pkg/emscripten` as:
```
fceumm_libretro.js
fceumm_libretro.wasm
```
## Usage
Most of the magic happens on the browser so nothing really on that regard
@ -40,3 +59,74 @@ cd ${ROOT_WWW_PATH}/assets/cores
```
That should be it, you can add more cores to the list by editing index.html
# Threaded emulators
Some emulators can be compiled with `pthreads` support to increase performance. You will need to compile the core and frontend with special flags to support this and also serve the content from an HTTPS endpoint with specific headers.
## Compiling the code (Threaded)
In this example we will be building [melonDS](https://github.com/libretro/melonDS) with pthreads support. We assume you allready have emsdk setup and are familiar with the build process.
First clone the repo:
```
git clone https://github.com/libretro/melonDS.git
cd melonDS
```
Next modify the Makefile to enable threads:
```
else ifeq ($(platform), emscripten)
TARGET := $(TARGET_NAME)_libretro_emscripten.bc
fpic := -fPIC
SHARED := -shared -Wl,--version-script=$(CORE_DIR)/link.T -Wl
HAVE_THREADS = 1
CFLAGS += -pthread
LDFLAGS += -pthread
CXXFLAGS += -pthread
```
Build and move output to the frontend:
```
emmake make -f Makefile platform=emscripten
cp melonds_libretro_emscripten.bc ~/retroarch/RetroArch/dist-scripts/melonds_libretro_emscripten.bc
```
Now build the frontend with the pthreads env variable: (2 is the number of workers this can be any integer)
```
cd ~/retroarch/RetroArch/dist-scripts
pthread=2 emmake ./dist-cores.sh emscripten
```
Your resulting output will be located in:
```
~/retroarch/RetroArch/pkg/emscripten/melonds_libretro.js
~/retroarch/RetroArch/pkg/emscripten/melonds_libretro.wasm
~/retroarch/RetroArch/pkg/emscripten/melonds_libretro.worker.js
```
## Setting up your webserver (Threaded)
Unless loading from `localhost` you will need to server the content from an HTTPS endpoint with a valid SSL certificate. This is a security limitation imposed by the browser. Along with that you will need to set content control policies with special headers in your server:
In Nodejs with express:
```
app.use(function(req, res, next) {
res.header("Cross-Origin-Embedder-Policy", "require-corp");
res.header("Cross-Origin-Opener-Policy", "same-origin");
next();
});
```
In NGINX: (site config under `server {`)
```
add_header Cross-Origin-Opener-Policy same-origin;
add_header Cross-Origin-Embedder-Policy require-corp;
```