[gpu] Fix WebGPU error page not showing when adapter unavailable

Check navigator.gpu synchronously in main() before calling
the WebGPU C API.  Previously the error was reported in an
async callback that raced with onRuntimeInitialized, which
hid the loading div before the error message could appear.

Also re-show the loading div and set window._webgpuError in
the adapter callback as a fallback.

Assisted-by: Claude <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-04-02 01:26:54 -06:00
1 parent 63cff45ae8
commit 0234df4db6
1 file changed
+22
+22
View File
@@ -796,8 +796,10 @@ on_adapter (WGPURequestAdapterStatus status,
{
fprintf (stderr, "Failed to get WebGPU adapter\n");
EM_ASM ({
window._webgpuError = true;
var el = document.getElementById ('loading');
if (el) {
el.style.display = '';
var html = 'WebGPU adapter not available.<br>' +
'<a href="https://github.com/gpuweb/gpuweb/wiki/Implementation-Status" ' +
'style="color:#88f">Browser support status</a>';
@@ -1035,6 +1037,26 @@ main ()
css_w = (int) css_w_d;
css_h = (int) css_h_d;
/* Check WebGPU availability before trying the C API. */
bool has_webgpu = EM_ASM_INT ({ return !!navigator.gpu; });
if (!has_webgpu)
{
fprintf (stderr, "WebGPU not available in this browser\n");
EM_ASM ({
window._webgpuError = true;
var el = document.getElementById ('loading');
if (el) {
el.style.display = '';
el.innerHTML = 'WebGPU is not available in this browser.<br>' +
'<a href="index.html" style="color:#88f">Try the WebGL version</a>';
el.style.font = '18px sans-serif';
el.style.color = '#666';
el.style.textAlign = 'center';
}
});
return 0;
}
WGPUInstanceDescriptor instDesc = {};
WGPUInstance instance = wgpuCreateInstance (&instDesc);