Skip to content

PS2 games in the browser: static recompilation to WebAssembly with a native runtime

C++

web: explain on the start screen when WebGPU is missing or on the CPU

Samir Nuri09fde8b5e969
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 093623e..2b85ee8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,8 +7,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(PS2_GENERATED "" CACHE PATH "Folder with the C++ generated for a game")
if(EMSCRIPTEN)
- add_compile_options(-msimd128 -mtail-call -fwasm-exceptions -pthread)
- add_link_options(-msimd128 -mtail-call -fwasm-exceptions -pthread)
+ add_compile_options(-msimd128 -mtail-call -fwasm-exceptions -sWASM_LEGACY_EXCEPTIONS=0 -pthread)
+ add_link_options(-msimd128 -mtail-call -fwasm-exceptions -sWASM_LEGACY_EXCEPTIONS=0 -pthread)
endif()
if(NOT EMSCRIPTEN)
diff --git a/web/shell.html b/web/shell.html
index 49746f8..2f854d4 100644
--- a/web/shell.html
+++ b/web/shell.html
@@ -14,13 +14,15 @@
#status { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#stats { font-variant-numeric: tabular-nums; color: #9ad; }
button { background: #2a3a55; color: #fff; border: 0; padding: 6px 14px; border-radius: 4px; cursor: pointer; }
- #overlay { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,.6); }
+ #overlay { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,.6); padding: 16px; }
+ #panel { display: flex; flex-direction: column; align-items: center; gap: 14px; max-width: 520px; text-align: center; }
+ #help { margin: 0; line-height: 1.5; color: #e8c37a; }
</style>
</head>
<body>
<div id="stage"><canvas id="canvas" tabindex="0"></canvas></div>
<div id="bar"><span id="status">Loading…</span><span id="stats"></span></div>
-<div id="overlay"><button id="start" disabled>Loading…</button></div>
+<div id="overlay"><div id="panel"><p id="help" hidden></p><button id="start" disabled>Loading…</button></div></div>
<script>
const statusEl = document.getElementById('status');
const statsEl = document.getElementById('stats');
@@ -159,8 +161,26 @@ async function canPresent(power) {
return failed ? null : name;
}
+function webgpuHelp(onCpu) {
+ const firefox = navigator.userAgent.includes('Firefox/');
+ const linux = navigator.userAgent.includes('Linux') && !navigator.userAgent.includes('Android');
+ if (firefox) return 'WebGPU is turned off in this Firefox, so the game runs on the slow software renderer. Set dom.webgpu.enabled to true in about:config and reload.';
+ if (linux && onCpu) return 'WebGPU runs on the CPU here, so the game is slow. Turn on chrome://flags/#enable-vulkan as well, then restart the browser.';
+ if (linux) return 'WebGPU is turned off in this browser, so the game runs on the slow software renderer. Turn on chrome://flags/#enable-unsafe-webgpu and chrome://flags/#enable-vulkan, then restart the browser.';
+ return 'This browser has no WebGPU, so the game runs on the slow software renderer. A current Chrome, Edge or Safari is much faster.';
+}
+
async function chooseGpu() {
- if (!navigator.gpu) return 'WebGPU unavailable, software renderer';
+ if (!navigator.gpu) {
+ Module.gpuHelp = webgpuHelp(false);
+ return 'WebGPU unavailable, software renderer';
+ }
+ const adapter = await navigator.gpu.requestAdapter({ powerPreference: 'high-performance' });
+ if (!adapter) {
+ Module.gpuHelp = webgpuHelp(false);
+ return 'WebGPU has no adapter, software renderer';
+ }
+ if (adapter.info.isFallbackAdapter || adapter.info.architecture === 'swiftshader') Module.gpuHelp = webgpuHelp(true);
if (navigator.userAgent.includes('Firefox/')) {
Module.presentCopy = true;
return 'Firefox, frames copied to the page';
@@ -188,7 +208,7 @@ var Module = {
presentCopy: params.has('copy'),
print: (text) => console.log(text),
printErr: (text) => console.log(text),
- setStatus: (text) => { if (text) statusEl.textContent = text; },
+ setStatus: (text) => { if (text && !Module.gpuInfo) statusEl.textContent = text; },
onRuntimeInitialized: () => {
const start = document.getElementById('start');
if (!crossOriginIsolated) {
@@ -204,6 +224,11 @@ var Module = {
Promise.all([fetch(Module.discUrl, { method: 'HEAD' }), chooseGpu().catch((e) => 'GPU check failed: ' + e)]).then(([r, gpu]) => {
Module.discSize = Number(r.headers.get('content-length')) || 0;
Module.gpuInfo = gpu;
+ if (Module.gpuHelp) {
+ const help = document.getElementById('help');
+ help.textContent = Module.gpuHelp;
+ help.hidden = false;
+ }
statusEl.textContent = (Module.discSize ? 'Disc ' + (Module.discSize / 1073741824).toFixed(2) + ' GB' : 'No disc image at ' + Module.discUrl) + ', ' + gpu;
Module.removeRunDependency('disc');
});